tile38/vendor/github.com/eclipse/paho.mqtt.golang/packets/packets.go

347 lines
9.1 KiB
Go
Raw Normal View History

package packets
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
)
//ControlPacket defines the interface for structs intended to hold
//decoded MQTT packets, either from being read or before being
//written
type ControlPacket interface {
Write(io.Writer) error
Unpack(io.Reader) error
String() string
Details() Details
}
//PacketNames maps the constants for each of the MQTT packet types
//to a string representation of their name.
var PacketNames = map[uint8]string{
1: "CONNECT",
2: "CONNACK",
3: "PUBLISH",
4: "PUBACK",
5: "PUBREC",
6: "PUBREL",
7: "PUBCOMP",
8: "SUBSCRIBE",
9: "SUBACK",
10: "UNSUBSCRIBE",
11: "UNSUBACK",
12: "PINGREQ",
13: "PINGRESP",
14: "DISCONNECT",
}
//Below are the constants assigned to each of the MQTT packet types
const (
Connect = 1
Connack = 2
Publish = 3
Puback = 4
Pubrec = 5
Pubrel = 6
Pubcomp = 7
Subscribe = 8
Suback = 9
Unsubscribe = 10
Unsuback = 11
Pingreq = 12
Pingresp = 13
Disconnect = 14
)
//Below are the const definitions for error codes returned by
//Connect()
const (
Accepted = 0x00
ErrRefusedBadProtocolVersion = 0x01
ErrRefusedIDRejected = 0x02
ErrRefusedServerUnavailable = 0x03
ErrRefusedBadUsernameOrPassword = 0x04
ErrRefusedNotAuthorised = 0x05
ErrNetworkError = 0xFE
ErrProtocolViolation = 0xFF
)
//ConnackReturnCodes is a map of the error codes constants for Connect()
//to a string representation of the error
var ConnackReturnCodes = map[uint8]string{
0: "Connection Accepted",
1: "Connection Refused: Bad Protocol Version",
2: "Connection Refused: Client Identifier Rejected",
3: "Connection Refused: Server Unavailable",
4: "Connection Refused: Username or Password in unknown format",
5: "Connection Refused: Not Authorised",
254: "Connection Error",
255: "Connection Refused: Protocol Violation",
}
//ConnErrors is a map of the errors codes constants for Connect()
//to a Go error
var ConnErrors = map[byte]error{
Accepted: nil,
ErrRefusedBadProtocolVersion: errors.New("Unnacceptable protocol version"),
ErrRefusedIDRejected: errors.New("Identifier rejected"),
ErrRefusedServerUnavailable: errors.New("Server Unavailable"),
ErrRefusedBadUsernameOrPassword: errors.New("Bad user name or password"),
ErrRefusedNotAuthorised: errors.New("Not Authorized"),
ErrNetworkError: errors.New("Network Error"),
ErrProtocolViolation: errors.New("Protocol Violation"),
}
//ReadPacket takes an instance of an io.Reader (such as net.Conn) and attempts
//to read an MQTT packet from the stream. It returns a ControlPacket
//representing the decoded MQTT packet and an error. One of these returns will
//always be nil, a nil ControlPacket indicating an error occurred.
2020-09-23 02:43:58 +03:00
func ReadPacket(r io.Reader) (ControlPacket, error) {
var fh FixedHeader
b := make([]byte, 1)
2020-09-23 02:43:58 +03:00
_, err := io.ReadFull(r, b)
if err != nil {
return nil, err
}
2020-09-23 02:43:58 +03:00
err = fh.unpack(b[0], r)
if err != nil {
return nil, err
}
cp, err := NewControlPacketWithHeader(fh)
if err != nil {
return nil, err
}
2020-09-23 02:43:58 +03:00
packetBytes := make([]byte, fh.RemainingLength)
2020-09-23 02:43:58 +03:00
n, err := io.ReadFull(r, packetBytes)
if err != nil {
return nil, err
}
2020-09-23 02:43:58 +03:00
if n != fh.RemainingLength {
return nil, errors.New("Failed to read expected data")
}
err = cp.Unpack(bytes.NewBuffer(packetBytes))
return cp, err
}
//NewControlPacket is used to create a new ControlPacket of the type specified
//by packetType, this is usually done by reference to the packet type constants
//defined in packets.go. The newly created ControlPacket is empty and a pointer
//is returned.
2020-09-23 02:43:58 +03:00
func NewControlPacket(packetType byte) ControlPacket {
switch packetType {
case Connect:
2020-09-23 02:43:58 +03:00
return &ConnectPacket{FixedHeader: FixedHeader{MessageType: Connect}}
case Connack:
2020-09-23 02:43:58 +03:00
return &ConnackPacket{FixedHeader: FixedHeader{MessageType: Connack}}
case Disconnect:
2020-09-23 02:43:58 +03:00
return &DisconnectPacket{FixedHeader: FixedHeader{MessageType: Disconnect}}
case Publish:
2020-09-23 02:43:58 +03:00
return &PublishPacket{FixedHeader: FixedHeader{MessageType: Publish}}
case Puback:
2020-09-23 02:43:58 +03:00
return &PubackPacket{FixedHeader: FixedHeader{MessageType: Puback}}
case Pubrec:
2020-09-23 02:43:58 +03:00
return &PubrecPacket{FixedHeader: FixedHeader{MessageType: Pubrec}}
case Pubrel:
2020-09-23 02:43:58 +03:00
return &PubrelPacket{FixedHeader: FixedHeader{MessageType: Pubrel, Qos: 1}}
case Pubcomp:
2020-09-23 02:43:58 +03:00
return &PubcompPacket{FixedHeader: FixedHeader{MessageType: Pubcomp}}
case Subscribe:
2020-09-23 02:43:58 +03:00
return &SubscribePacket{FixedHeader: FixedHeader{MessageType: Subscribe, Qos: 1}}
case Suback:
2020-09-23 02:43:58 +03:00
return &SubackPacket{FixedHeader: FixedHeader{MessageType: Suback}}
case Unsubscribe:
2020-09-23 02:43:58 +03:00
return &UnsubscribePacket{FixedHeader: FixedHeader{MessageType: Unsubscribe, Qos: 1}}
case Unsuback:
2020-09-23 02:43:58 +03:00
return &UnsubackPacket{FixedHeader: FixedHeader{MessageType: Unsuback}}
case Pingreq:
2020-09-23 02:43:58 +03:00
return &PingreqPacket{FixedHeader: FixedHeader{MessageType: Pingreq}}
case Pingresp:
2020-09-23 02:43:58 +03:00
return &PingrespPacket{FixedHeader: FixedHeader{MessageType: Pingresp}}
}
2020-09-23 02:43:58 +03:00
return nil
}
//NewControlPacketWithHeader is used to create a new ControlPacket of the type
//specified within the FixedHeader that is passed to the function.
//The newly created ControlPacket is empty and a pointer is returned.
2020-09-23 02:43:58 +03:00
func NewControlPacketWithHeader(fh FixedHeader) (ControlPacket, error) {
switch fh.MessageType {
case Connect:
2020-09-23 02:43:58 +03:00
return &ConnectPacket{FixedHeader: fh}, nil
case Connack:
2020-09-23 02:43:58 +03:00
return &ConnackPacket{FixedHeader: fh}, nil
case Disconnect:
2020-09-23 02:43:58 +03:00
return &DisconnectPacket{FixedHeader: fh}, nil
case Publish:
2020-09-23 02:43:58 +03:00
return &PublishPacket{FixedHeader: fh}, nil
case Puback:
2020-09-23 02:43:58 +03:00
return &PubackPacket{FixedHeader: fh}, nil
case Pubrec:
2020-09-23 02:43:58 +03:00
return &PubrecPacket{FixedHeader: fh}, nil
case Pubrel:
2020-09-23 02:43:58 +03:00
return &PubrelPacket{FixedHeader: fh}, nil
case Pubcomp:
2020-09-23 02:43:58 +03:00
return &PubcompPacket{FixedHeader: fh}, nil
case Subscribe:
2020-09-23 02:43:58 +03:00
return &SubscribePacket{FixedHeader: fh}, nil
case Suback:
2020-09-23 02:43:58 +03:00
return &SubackPacket{FixedHeader: fh}, nil
case Unsubscribe:
2020-09-23 02:43:58 +03:00
return &UnsubscribePacket{FixedHeader: fh}, nil
case Unsuback:
2020-09-23 02:43:58 +03:00
return &UnsubackPacket{FixedHeader: fh}, nil
case Pingreq:
2020-09-23 02:43:58 +03:00
return &PingreqPacket{FixedHeader: fh}, nil
case Pingresp:
2020-09-23 02:43:58 +03:00
return &PingrespPacket{FixedHeader: fh}, nil
}
2020-09-23 02:43:58 +03:00
return nil, fmt.Errorf("unsupported packet type 0x%x", fh.MessageType)
}
//Details struct returned by the Details() function called on
//ControlPackets to present details of the Qos and MessageID
//of the ControlPacket
type Details struct {
Qos byte
MessageID uint16
}
//FixedHeader is a struct to hold the decoded information from
//the fixed header of an MQTT ControlPacket
type FixedHeader struct {
MessageType byte
Dup bool
Qos byte
Retain bool
RemainingLength int
}
func (fh FixedHeader) String() string {
return fmt.Sprintf("%s: dup: %t qos: %d retain: %t rLength: %d", PacketNames[fh.MessageType], fh.Dup, fh.Qos, fh.Retain, fh.RemainingLength)
}
func boolToByte(b bool) byte {
switch b {
case true:
return 1
default:
return 0
}
}
func (fh *FixedHeader) pack() bytes.Buffer {
var header bytes.Buffer
header.WriteByte(fh.MessageType<<4 | boolToByte(fh.Dup)<<3 | fh.Qos<<1 | boolToByte(fh.Retain))
header.Write(encodeLength(fh.RemainingLength))
return header
}
2020-09-23 02:43:58 +03:00
func (fh *FixedHeader) unpack(typeAndFlags byte, r io.Reader) error {
fh.MessageType = typeAndFlags >> 4
fh.Dup = (typeAndFlags>>3)&0x01 > 0
fh.Qos = (typeAndFlags >> 1) & 0x03
fh.Retain = typeAndFlags&0x01 > 0
2020-09-23 02:43:58 +03:00
var err error
fh.RemainingLength, err = decodeLength(r)
return err
}
2020-09-23 02:43:58 +03:00
func decodeByte(b io.Reader) (byte, error) {
num := make([]byte, 1)
2020-09-23 02:43:58 +03:00
_, err := b.Read(num)
if err != nil {
return 0, err
}
return num[0], nil
}
2020-09-23 02:43:58 +03:00
func decodeUint16(b io.Reader) (uint16, error) {
num := make([]byte, 2)
2020-09-23 02:43:58 +03:00
_, err := b.Read(num)
if err != nil {
return 0, err
}
return binary.BigEndian.Uint16(num), nil
}
func encodeUint16(num uint16) []byte {
bytes := make([]byte, 2)
binary.BigEndian.PutUint16(bytes, num)
return bytes
}
func encodeString(field string) []byte {
2020-09-23 02:43:58 +03:00
return encodeBytes([]byte(field))
}
2020-09-23 02:43:58 +03:00
func decodeString(b io.Reader) (string, error) {
buf, err := decodeBytes(b)
return string(buf), err
}
2020-09-23 02:43:58 +03:00
func decodeBytes(b io.Reader) ([]byte, error) {
fieldLength, err := decodeUint16(b)
if err != nil {
return nil, err
}
field := make([]byte, fieldLength)
2020-09-23 02:43:58 +03:00
_, err = b.Read(field)
if err != nil {
return nil, err
}
return field, nil
}
func encodeBytes(field []byte) []byte {
fieldLength := make([]byte, 2)
binary.BigEndian.PutUint16(fieldLength, uint16(len(field)))
return append(fieldLength, field...)
}
func encodeLength(length int) []byte {
var encLength []byte
for {
digit := byte(length % 128)
length /= 128
if length > 0 {
digit |= 0x80
}
encLength = append(encLength, digit)
if length == 0 {
break
}
}
return encLength
}
2020-09-23 02:43:58 +03:00
func decodeLength(r io.Reader) (int, error) {
var rLength uint32
var multiplier uint32
b := make([]byte, 1)
for multiplier < 27 { //fix: Infinite '(digit & 128) == 1' will cause the dead loop
2020-09-23 02:43:58 +03:00
_, err := io.ReadFull(r, b)
if err != nil {
return 0, err
}
digit := b[0]
rLength |= uint32(digit&127) << multiplier
if (digit & 128) == 0 {
break
}
multiplier += 7
}
2020-09-23 02:43:58 +03:00
return int(rLength), nil
}