2021-08-26 15:08:24 +02:00
|
|
|
package packets
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2021-10-01 22:18:48 +02:00
|
|
|
"bytes"
|
2021-08-26 15:08:24 +02:00
|
|
|
"errors"
|
|
|
|
"io"
|
2021-09-28 12:30:32 +02:00
|
|
|
"strings"
|
2021-08-26 15:08:24 +02:00
|
|
|
|
|
|
|
"badat.dev/maeqtt/v2/mqtt/properties"
|
|
|
|
"badat.dev/maeqtt/v2/mqtt/types"
|
|
|
|
)
|
|
|
|
|
2021-09-28 12:30:32 +02:00
|
|
|
type Topic struct {
|
|
|
|
Fields []string
|
|
|
|
}
|
|
|
|
|
|
|
|
var multiLevelWildcardNotLast = errors.New("Multi level wildcard isn't the field in a topic")
|
2021-10-01 22:18:48 +02:00
|
|
|
|
|
|
|
func ParseTopic(topic_name string) (Topic, error) {
|
2021-09-28 12:30:32 +02:00
|
|
|
topic := Topic{}
|
|
|
|
fields := strings.Split(topic_name, "/")
|
|
|
|
for i, field := range fields {
|
|
|
|
if field == "#" && len(fields) > i+1 {
|
|
|
|
return topic, multiLevelWildcardNotLast
|
|
|
|
}
|
|
|
|
}
|
|
|
|
topic.Fields = fields
|
|
|
|
|
|
|
|
return topic, nil
|
|
|
|
}
|
|
|
|
|
2021-08-26 15:08:24 +02:00
|
|
|
type TopicFilter struct {
|
2021-09-28 12:30:32 +02:00
|
|
|
Topic Topic
|
2021-08-26 15:08:24 +02:00
|
|
|
MaxQoS uint
|
|
|
|
NoLocal bool
|
|
|
|
RetainAsPublished bool
|
|
|
|
RetainHandling uint
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseTopicFilter(r *bufio.Reader) (TopicFilter, error) {
|
|
|
|
filter := TopicFilter{}
|
|
|
|
var err error
|
|
|
|
|
2021-09-28 12:30:32 +02:00
|
|
|
topic_str, err := types.DecodeUTF8String(r)
|
|
|
|
if err != nil {
|
|
|
|
return filter, err
|
|
|
|
}
|
|
|
|
|
2021-10-01 22:18:48 +02:00
|
|
|
filter.Topic, err = ParseTopic(topic_str)
|
2021-08-26 15:08:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return filter, err
|
|
|
|
}
|
|
|
|
|
|
|
|
options, err := types.DecodeBits(r)
|
|
|
|
if err != nil {
|
|
|
|
return filter, err
|
|
|
|
}
|
|
|
|
filter.MaxQoS = types.BoolsToUint(options[0], options[1])
|
|
|
|
filter.NoLocal = options[2]
|
|
|
|
filter.RetainAsPublished = options[3]
|
|
|
|
filter.RetainHandling = types.BoolsToUint(options[4], options[5])
|
|
|
|
return filter, nil
|
|
|
|
}
|
|
|
|
|
2021-10-01 22:18:48 +02:00
|
|
|
type SubscribePacket struct {
|
2021-08-26 15:08:24 +02:00
|
|
|
PacketId uint16
|
|
|
|
TopicFilters []TopicFilter
|
2021-10-01 22:18:48 +02:00
|
|
|
Properties properties.SubscribePacketProperties
|
2021-08-26 15:08:24 +02:00
|
|
|
}
|
|
|
|
|
2021-10-01 22:18:48 +02:00
|
|
|
func parseSubscribePacket(control controlPacket) (SubscribePacket, error) {
|
|
|
|
if control.packetType != PacketTypeSubscribe {
|
|
|
|
panic("Wrong packet type for parseSubscribePacket")
|
|
|
|
}
|
|
|
|
|
|
|
|
packet := SubscribePacket{}
|
|
|
|
|
2021-08-26 15:08:24 +02:00
|
|
|
r := bufio.NewReader(control.reader)
|
|
|
|
|
|
|
|
if control.flags != 2 {
|
|
|
|
return packet, errors.New("Malformed subscription packet")
|
|
|
|
}
|
|
|
|
|
2021-10-01 22:18:48 +02:00
|
|
|
var err error
|
2021-08-26 15:08:24 +02:00
|
|
|
packet.PacketId, err = types.DecodeUint16(r)
|
|
|
|
if err != nil {
|
|
|
|
return packet, err
|
|
|
|
}
|
|
|
|
|
2021-10-01 22:18:48 +02:00
|
|
|
err = properties.ParseProperties(r, packet.Properties.ArrayOf())
|
2021-08-26 15:08:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return packet, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for err != io.EOF {
|
|
|
|
filter, err := parseTopicFilter(r)
|
|
|
|
packet.TopicFilters = append(packet.TopicFilters, filter)
|
|
|
|
if err != nil {
|
|
|
|
return packet, err
|
|
|
|
}
|
|
|
|
_, err = r.Peek(1)
|
2021-09-28 12:30:32 +02:00
|
|
|
if err != nil && err != io.EOF {
|
2021-08-26 15:08:24 +02:00
|
|
|
return packet, err
|
|
|
|
}
|
2021-09-28 12:30:32 +02:00
|
|
|
if err == io.EOF {
|
|
|
|
return packet, nil
|
|
|
|
}
|
2021-08-26 15:08:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return packet, nil
|
|
|
|
}
|
|
|
|
|
2021-09-28 12:30:32 +02:00
|
|
|
func (p SubscribePacket) Visit(v PacketVisitor) {
|
|
|
|
v.VisitSubscribe(p)
|
|
|
|
}
|
|
|
|
|
2021-08-26 15:08:24 +02:00
|
|
|
type SubackReasonCode byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
SubackReasonGrantedQoSZero PubackReasonCode = 0
|
|
|
|
SubackReasonGrantedQoSOne = 1
|
|
|
|
SubackReasonGrantedQoSTwo = 2
|
|
|
|
SubackReasonUnspecified = 128
|
|
|
|
SubackReasonImplSpecificError = 131
|
|
|
|
SubackReasonNotAuthorized = 135
|
|
|
|
SubackReasonTopicFilterInvalid = 143
|
|
|
|
SubackReasonPacketIDInUse = 145
|
|
|
|
SubackReasonQuotaExceeded = 151
|
2021-10-01 22:18:48 +02:00
|
|
|
SubackReasonSharedSubNotSupported = 158
|
|
|
|
SubackReasonSubIDUnsupported = 161
|
|
|
|
SubackReasonWildcardSubUnsupported = 162
|
2021-08-26 15:08:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type SubAckPacket struct {
|
|
|
|
PacketID uint16
|
|
|
|
Properties properties.SubackPacketProperties
|
2021-10-01 22:18:48 +02:00
|
|
|
Reason SubackReasonCode
|
2021-08-26 15:08:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p SubAckPacket) Write(w io.Writer) error {
|
2021-10-01 22:18:48 +02:00
|
|
|
buf := bytes.NewBuffer([]byte{})
|
|
|
|
err := types.WriteUint16(buf, p.PacketID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-08-26 15:08:24 +02:00
|
|
|
}
|
2021-10-01 22:18:48 +02:00
|
|
|
|
|
|
|
err = properties.WriteProps(buf, p.Properties.ArrayOf())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = buf.WriteByte(byte(p.Reason))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
conPack := controlPacket{
|
|
|
|
packetType: PacketTypeSuback,
|
|
|
|
flags: 0,
|
|
|
|
reader: buf,
|
|
|
|
}
|
|
|
|
return conPack.write(w)
|
2021-08-26 15:08:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type UnsubscribePacket struct {
|
2021-10-01 22:18:48 +02:00
|
|
|
PacketID uint16
|
|
|
|
Topics []Topic
|
|
|
|
Properties properties.UnsubscribePacketProperties
|
2021-08-26 15:08:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseUnsubscribePacket(control controlPacket) (UnsubscribePacket, error) {
|
|
|
|
if control.packetType != PacketTypeUnsubscribe {
|
|
|
|
panic("Wrong packet type for parseSubscribePacket")
|
|
|
|
}
|
|
|
|
|
2021-10-01 22:18:48 +02:00
|
|
|
packet := UnsubscribePacket{}
|
|
|
|
r := bufio.NewReader(control.reader)
|
|
|
|
|
|
|
|
if control.flags != 2 {
|
|
|
|
return packet, errors.New("Malformed subscription packet")
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
packet.PacketID, err = types.DecodeUint16(r)
|
|
|
|
if err != nil {
|
|
|
|
return packet, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = properties.ParseProperties(r, packet.Properties.ArrayOf())
|
2021-08-26 15:08:24 +02:00
|
|
|
if err != nil {
|
2021-10-01 22:18:48 +02:00
|
|
|
return packet, err
|
2021-08-26 15:08:24 +02:00
|
|
|
}
|
2021-10-01 22:18:48 +02:00
|
|
|
|
|
|
|
for err != io.EOF {
|
|
|
|
topic_str, err := types.DecodeUTF8String(r)
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
return packet, err
|
|
|
|
} else if err == io.EOF {
|
|
|
|
return packet, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
filter, err := ParseTopic(topic_str)
|
|
|
|
if err != nil {
|
|
|
|
return packet, err
|
|
|
|
}
|
|
|
|
|
|
|
|
packet.Topics = append(packet.Topics, filter)
|
|
|
|
}
|
|
|
|
|
|
|
|
return packet, nil
|
2021-08-26 15:08:24 +02:00
|
|
|
}
|
|
|
|
|
2021-09-28 12:30:32 +02:00
|
|
|
func (p UnsubscribePacket) Visit(v PacketVisitor) {
|
|
|
|
v.VisitUnsubscribe(p)
|
|
|
|
}
|
|
|
|
|
2021-08-26 15:08:24 +02:00
|
|
|
type UnsubackReasonCode byte
|
|
|
|
|
|
|
|
const (
|
2021-10-01 22:18:48 +02:00
|
|
|
UnsubackReasonSuccess UnsubackReasonCode = 0
|
|
|
|
UnSubackReasonUnspecified = 128
|
|
|
|
UnSubackReasonImplSpecificError = 131
|
|
|
|
UnSubackReasonNotAuthorized = 135
|
|
|
|
UnSubackReasonTopicFilterInvalid = 143
|
|
|
|
UnSubackReasonPacketIDInUse = 145
|
2021-08-26 15:08:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type UnsubAckPacket struct {
|
|
|
|
PacketID uint16
|
|
|
|
Properties properties.UnsubackPacketProperties
|
|
|
|
Reason UnsubackReasonCode
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p UnsubAckPacket) Write(w io.Writer) error {
|
2021-10-01 22:18:48 +02:00
|
|
|
buf := bytes.NewBuffer([]byte{})
|
|
|
|
err := types.WriteUint16(buf, p.PacketID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = properties.WriteProps(buf, p.Properties.ArrayOf())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = buf.WriteByte(byte(p.Reason))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
conPack := controlPacket{
|
|
|
|
packetType: PacketTypeUnsuback,
|
|
|
|
flags: 0,
|
|
|
|
reader: buf,
|
2021-08-26 15:08:24 +02:00
|
|
|
}
|
2021-10-01 22:18:48 +02:00
|
|
|
return conPack.write(w)
|
2021-08-26 15:08:24 +02:00
|
|
|
}
|