2020-03-01 16:06:34 +00:00
|
|
|
package packets
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2021-01-17 18:00:46 +00:00
|
|
|
// ConnectPacket is an internal representation of the fields of the
|
|
|
|
// Connect MQTT packet
|
2020-03-01 16:06:34 +00:00
|
|
|
type ConnectPacket struct {
|
|
|
|
FixedHeader
|
|
|
|
ProtocolName string
|
|
|
|
ProtocolVersion byte
|
|
|
|
CleanSession bool
|
|
|
|
WillFlag bool
|
|
|
|
WillQos byte
|
|
|
|
WillRetain bool
|
|
|
|
UsernameFlag bool
|
|
|
|
PasswordFlag bool
|
|
|
|
ReservedBit byte
|
|
|
|
Keepalive uint16
|
|
|
|
|
|
|
|
ClientIdentifier string
|
|
|
|
WillTopic string
|
|
|
|
WillMessage []byte
|
|
|
|
Username string
|
|
|
|
Password []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConnectPacket) String() string {
|
2021-01-17 18:00:46 +00:00
|
|
|
return fmt.Sprintf("%s protocolversion: %d protocolname: %s cleansession: %t willflag: %t WillQos: %d WillRetain: %t Usernameflag: %t Passwordflag: %t keepalive: %d clientId: %s willtopic: %s willmessage: %s Username: %s Password: %s", c.FixedHeader, c.ProtocolVersion, c.ProtocolName, c.CleanSession, c.WillFlag, c.WillQos, c.WillRetain, c.UsernameFlag, c.PasswordFlag, c.Keepalive, c.ClientIdentifier, c.WillTopic, c.WillMessage, c.Username, c.Password)
|
2020-03-01 16:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ConnectPacket) Write(w io.Writer) error {
|
|
|
|
var body bytes.Buffer
|
|
|
|
var err error
|
|
|
|
|
|
|
|
body.Write(encodeString(c.ProtocolName))
|
|
|
|
body.WriteByte(c.ProtocolVersion)
|
|
|
|
body.WriteByte(boolToByte(c.CleanSession)<<1 | boolToByte(c.WillFlag)<<2 | c.WillQos<<3 | boolToByte(c.WillRetain)<<5 | boolToByte(c.PasswordFlag)<<6 | boolToByte(c.UsernameFlag)<<7)
|
|
|
|
body.Write(encodeUint16(c.Keepalive))
|
|
|
|
body.Write(encodeString(c.ClientIdentifier))
|
|
|
|
if c.WillFlag {
|
|
|
|
body.Write(encodeString(c.WillTopic))
|
|
|
|
body.Write(encodeBytes(c.WillMessage))
|
|
|
|
}
|
|
|
|
if c.UsernameFlag {
|
|
|
|
body.Write(encodeString(c.Username))
|
|
|
|
}
|
|
|
|
if c.PasswordFlag {
|
|
|
|
body.Write(encodeBytes(c.Password))
|
|
|
|
}
|
|
|
|
c.FixedHeader.RemainingLength = body.Len()
|
|
|
|
packet := c.FixedHeader.pack()
|
|
|
|
packet.Write(body.Bytes())
|
|
|
|
_, err = packet.WriteTo(w)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-17 18:00:46 +00:00
|
|
|
// Unpack decodes the details of a ControlPacket after the fixed
|
|
|
|
// header has been read
|
2020-03-01 16:06:34 +00:00
|
|
|
func (c *ConnectPacket) Unpack(b io.Reader) error {
|
|
|
|
var err error
|
|
|
|
c.ProtocolName, err = decodeString(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.ProtocolVersion, err = decodeByte(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
options, err := decodeByte(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.ReservedBit = 1 & options
|
|
|
|
c.CleanSession = 1&(options>>1) > 0
|
|
|
|
c.WillFlag = 1&(options>>2) > 0
|
|
|
|
c.WillQos = 3 & (options >> 3)
|
|
|
|
c.WillRetain = 1&(options>>5) > 0
|
|
|
|
c.PasswordFlag = 1&(options>>6) > 0
|
|
|
|
c.UsernameFlag = 1&(options>>7) > 0
|
|
|
|
c.Keepalive, err = decodeUint16(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.ClientIdentifier, err = decodeString(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if c.WillFlag {
|
|
|
|
c.WillTopic, err = decodeString(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.WillMessage, err = decodeBytes(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.UsernameFlag {
|
|
|
|
c.Username, err = decodeString(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.PasswordFlag {
|
|
|
|
c.Password, err = decodeBytes(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-17 18:00:46 +00:00
|
|
|
// Validate performs validation of the fields of a Connect packet
|
2020-03-01 16:06:34 +00:00
|
|
|
func (c *ConnectPacket) Validate() byte {
|
|
|
|
if c.PasswordFlag && !c.UsernameFlag {
|
|
|
|
return ErrRefusedBadUsernameOrPassword
|
|
|
|
}
|
|
|
|
if c.ReservedBit != 0 {
|
2021-01-17 18:00:46 +00:00
|
|
|
// Bad reserved bit
|
2020-03-01 16:06:34 +00:00
|
|
|
return ErrProtocolViolation
|
|
|
|
}
|
|
|
|
if (c.ProtocolName == "MQIsdp" && c.ProtocolVersion != 3) || (c.ProtocolName == "MQTT" && c.ProtocolVersion != 4) {
|
2021-01-17 18:00:46 +00:00
|
|
|
// Mismatched or unsupported protocol version
|
2020-03-01 16:06:34 +00:00
|
|
|
return ErrRefusedBadProtocolVersion
|
|
|
|
}
|
|
|
|
if c.ProtocolName != "MQIsdp" && c.ProtocolName != "MQTT" {
|
2021-01-17 18:00:46 +00:00
|
|
|
// Bad protocol name
|
2020-03-01 16:06:34 +00:00
|
|
|
return ErrProtocolViolation
|
|
|
|
}
|
|
|
|
if len(c.ClientIdentifier) > 65535 || len(c.Username) > 65535 || len(c.Password) > 65535 {
|
2021-01-17 18:00:46 +00:00
|
|
|
// Bad size field
|
2020-03-01 16:06:34 +00:00
|
|
|
return ErrProtocolViolation
|
|
|
|
}
|
|
|
|
if len(c.ClientIdentifier) == 0 && !c.CleanSession {
|
2021-01-17 18:00:46 +00:00
|
|
|
// Bad client identifier
|
2020-03-01 16:06:34 +00:00
|
|
|
return ErrRefusedIDRejected
|
|
|
|
}
|
|
|
|
return Accepted
|
|
|
|
}
|
|
|
|
|
2021-01-17 18:00:46 +00:00
|
|
|
// Details returns a Details struct containing the Qos and
|
|
|
|
// MessageID of this ControlPacket
|
2020-03-01 16:06:34 +00:00
|
|
|
func (c *ConnectPacket) Details() Details {
|
|
|
|
return Details{Qos: 0, MessageID: 0}
|
|
|
|
}
|