packet

package
v1.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 13, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package packet implements functionality for encoding and decoding MQTT packets.

Index

Constants

View Source
const (
	Version31  byte = 3
	Version311 byte = 4
)

Variables

View Source
var ErrInvalidPacketType = errors.New("invalid packet type")

ErrInvalidPacketType is returned by New if the packet type is invalid.

View Source
var MaxVarint = uint64(268435455)

Functions

func Fuzz

func Fuzz(data []byte) int

Fuzz is a basic fuzzing test that works with https://github.com/dvyukov/go-fuzz:

$ go-fuzz-build github.com/gomqtt/packet
$ go-fuzz -bin=./packet-fuzz.zip -workdir=./fuzz

Types

type Connack

type Connack struct {
	SessionPresent bool
	ReturnCode     ConnackCode
}

func NewConnack

func NewConnack() *Connack

func (*Connack) Decode

func (c *Connack) Decode(src []byte) (int, error)

func (*Connack) Encode

func (c *Connack) Encode(dst []byte) (int, error)

func (*Connack) Len

func (c *Connack) Len() int

func (*Connack) String

func (c *Connack) String() string

func (*Connack) Type

func (c *Connack) Type() Type

type ConnackCode

type ConnackCode uint8
const (
	ConnectionAccepted ConnackCode = iota
	InvalidProtocolVersion
	IdentifierRejected
	ServerUnavailable
	BadUsernameOrPassword
	NotAuthorized
)

func (ConnackCode) String

func (cc ConnackCode) String() string

func (ConnackCode) Valid

func (cc ConnackCode) Valid() bool

type Connect

type Connect struct {
	ClientID     string
	KeepAlive    uint16
	Username     string
	Password     string
	CleanSession bool
	Will         *Message
	Version      byte
}

func NewConnect

func NewConnect() *Connect

func (*Connect) Decode

func (c *Connect) Decode(src []byte) (int, error)

func (*Connect) Encode

func (c *Connect) Encode(dst []byte) (int, error)

Encode writes the packet bytes into the byte slice from the argument. It returns the number of bytes encoded and whether there's any errors along the way. If there is an error, the byte slice should be considered invalid.

func (*Connect) Len

func (c *Connect) Len() int

func (*Connect) String

func (c *Connect) String() string

func (*Connect) Type

func (c *Connect) Type() Type

type Disconnect

type Disconnect struct{}

A Disconnect packet is sent from the client to the server. It indicates that the client is disconnecting cleanly.

func NewDisconnect

func NewDisconnect() *Disconnect

NewDisconnect creates a new Disconnect packet.

func (*Disconnect) Decode

func (d *Disconnect) Decode(src []byte) (int, error)

Decode reads from the byte slice argument. It returns the total number of bytes decoded, and whether there have been any errors during the process.

func (*Disconnect) Encode

func (d *Disconnect) Encode(dst []byte) (int, error)

Encode writes the packet bytes into the byte slice from the argument. It returns the number of bytes encoded and whether there's any errors along the way. If there is an error, the byte slice should be considered invalid.

func (*Disconnect) Len

func (d *Disconnect) Len() int

Len returns the byte length of the encoded packet.

func (*Disconnect) String

func (d *Disconnect) String() string

String returns a string representation of the packet.

func (*Disconnect) Type

func (d *Disconnect) Type() Type

Type returns the packets type.

type Error

type Error struct {
	Type Type
	// contains filtered or unexported fields
}

Error represents decoding and encoding errors.

func (*Error) Error

func (e *Error) Error() string

Error implements the error interface.

type Generic

type Generic interface {
	// Type returns the packets type.
	Type() Type

	// Len returns the byte length of the encoded packet.
	Len() int

	// Decode reads from the byte slice argument. It returns the total number of
	// bytes decoded, and whether there have been any errors during the process.
	Decode(src []byte) (int, error)

	// Encode writes the packet bytes into the byte slice from the argument. It
	// returns the number of bytes encoded and whether there's any errors along
	// the way. If there is an error, the byte slice should be considered invalid.
	Encode(dst []byte) (int, error)

	// String returns a string representation of the packet.
	String() string
}

Generic is an MQTT control packet that can be encoded to a buffer or decoded from a buffer.

type ID

type ID uint16

ID is the type used to store packet ids.

func GetID

func GetID(pkt Generic) (ID, bool)

GetID checks the packets type and returns its ID and true, or if it does not have a ID, zero and false.

func (ID) Valid

func (id ID) Valid() bool

Valid returns whether this packet id is valid.

type Message

type Message struct {
	// The Topic of the message.
	Topic string

	// The Payload of the message.
	Payload []byte

	// The QOS indicates the level of assurance for delivery.
	QOS QOS

	// If the Retain flag is set to true, the server must store the message,
	// so that it can be delivered to future subscribers whose subscriptions
	// match its topic name.
	Retain bool
}

A Message bundles data that is published between brokers and clients.

func (Message) Copy

func (m Message) Copy() *Message

Copy returns a copy of the message.

func (*Message) String

func (m *Message) String() string

String returns a string representation of the message.

type Pingreq

type Pingreq struct{}

A Pingreq packet is sent from a client to the server.

func NewPingreq

func NewPingreq() *Pingreq

NewPingreq creates a new Pingreq packet.

func (*Pingreq) Decode

func (p *Pingreq) Decode(src []byte) (int, error)

Decode reads from the byte slice argument. It returns the total number of bytes decoded, and whether there have been any errors during the process.

func (*Pingreq) Encode

func (p *Pingreq) Encode(dst []byte) (int, error)

Encode writes the packet bytes into the byte slice from the argument. It returns the number of bytes encoded and whether there's any errors along the way. If there is an error, the byte slice should be considered invalid.

func (*Pingreq) Len

func (p *Pingreq) Len() int

Len returns the byte length of the encoded packet.

func (*Pingreq) String

func (p *Pingreq) String() string

String returns a string representation of the packet.

func (*Pingreq) Type

func (p *Pingreq) Type() Type

Type returns the packets type.

type Pingresp

type Pingresp struct{}

A Pingresp packet is sent by the server to the client in response to a Pingreq. It indicates that the server is alive.

func NewPingresp

func NewPingresp() *Pingresp

NewPingresp creates a new Pingresp packet.

func (*Pingresp) Decode

func (p *Pingresp) Decode(src []byte) (int, error)

Decode reads from the byte slice argument. It returns the total number of bytes decoded, and whether there have been any errors during the process.

func (*Pingresp) Encode

func (p *Pingresp) Encode(dst []byte) (int, error)

Encode writes the packet bytes into the byte slice from the argument. It returns the number of bytes encoded and whether there's any errors along the way. If there is an error, the byte slice should be considered invalid.

func (*Pingresp) Len

func (p *Pingresp) Len() int

Len returns the byte length of the encoded packet.

func (*Pingresp) String

func (p *Pingresp) String() string

String returns a string representation of the packet.

func (*Pingresp) Type

func (p *Pingresp) Type() Type

Type returns the packets type.

type Puback

type Puback struct {
	// The packet identifier.
	ID ID
}

A Puback packet is the response to a Publish packet with QOS level 1.

func NewPuback

func NewPuback() *Puback

NewPuback creates a new Puback packet.

func (*Puback) Decode

func (p *Puback) Decode(src []byte) (int, error)

Decode reads from the byte slice argument. It returns the total number of bytes decoded, and whether there have been any errors during the process.

func (*Puback) Encode

func (p *Puback) Encode(dst []byte) (int, error)

Encode writes the packet bytes into the byte slice from the argument. It returns the number of bytes encoded and whether there's any errors along the way. If there is an error, the byte slice should be considered invalid.

func (*Puback) Len

func (p *Puback) Len() int

Len returns the byte length of the encoded packet.

func (*Puback) String

func (p *Puback) String() string

String returns a string representation of the packet.

func (*Puback) Type

func (p *Puback) Type() Type

Type returns the packets type.

type Pubcomp

type Pubcomp struct {
	// The packet identifier.
	ID ID
}

A Pubcomp packet is the response to a Pubrel. It is the fourth and final packet of the QOS 2 protocol exchange.

func NewPubcomp

func NewPubcomp() *Pubcomp

NewPubcomp creates a new Pubcomp packet.

func (*Pubcomp) Decode

func (p *Pubcomp) Decode(src []byte) (int, error)

Decode reads from the byte slice argument. It returns the total number of bytes decoded, and whether there have been any errors during the process.

func (*Pubcomp) Encode

func (p *Pubcomp) Encode(dst []byte) (int, error)

Encode writes the packet bytes into the byte slice from the argument. It returns the number of bytes encoded and whether there's any errors along the way. If there is an error, the byte slice should be considered invalid.

func (*Pubcomp) Len

func (p *Pubcomp) Len() int

Len returns the byte length of the encoded packet.

func (*Pubcomp) String

func (p *Pubcomp) String() string

String returns a string representation of the packet.

func (*Pubcomp) Type

func (p *Pubcomp) Type() Type

Type returns the packets type.

type Publish

type Publish struct {
	// The message to publish.
	Message Message

	// If the Dup flag is set to false, it indicates that this is the first
	// occasion that the client or server has attempted to send this
	// Publish packet. If the dup flag is set to true, it indicates that this
	// might be re-delivery of an earlier attempt to send the packet.
	Dup bool

	// The packet identifier.
	ID ID
}

A Publish packet is sent from a client to a server or from server to a client to transport an application message.

func NewPublish

func NewPublish() *Publish

NewPublish creates a new Publish packet.

func (*Publish) Decode

func (p *Publish) Decode(src []byte) (int, error)

Decode reads from the byte slice argument. It returns the total number of bytes decoded, and whether there have been any errors during the process.

func (*Publish) Encode

func (p *Publish) Encode(dst []byte) (int, error)

Encode writes the packet bytes into the byte slice from the argument. It returns the number of bytes encoded and whether there's any errors along the way. If there is an error, the byte slice should be considered invalid.

func (*Publish) Len

func (p *Publish) Len() int

Len returns the byte length of the encoded packet.

func (*Publish) String

func (p *Publish) String() string

String returns a string representation of the packet.

func (*Publish) Type

func (p *Publish) Type() Type

Type returns the packets type.

type Pubrec

type Pubrec struct {
	// Shared packet identifier.
	ID ID
}

A Pubrec packet is the response to a Publish packet with QOS 2. It is the second packet of the QOS 2 protocol exchange.

func NewPubrec

func NewPubrec() *Pubrec

NewPubrec creates a new Pubrec packet.

func (*Pubrec) Decode

func (p *Pubrec) Decode(src []byte) (int, error)

Decode reads from the byte slice argument. It returns the total number of bytes decoded, and whether there have been any errors during the process.

func (*Pubrec) Encode

func (p *Pubrec) Encode(dst []byte) (int, error)

Encode writes the packet bytes into the byte slice from the argument. It returns the number of bytes encoded and whether there's any errors along the way. If there is an error, the byte slice should be considered invalid.

func (*Pubrec) Len

func (p *Pubrec) Len() int

Len returns the byte length of the encoded packet.

func (*Pubrec) String

func (p *Pubrec) String() string

String returns a string representation of the packet.

func (*Pubrec) Type

func (p *Pubrec) Type() Type

Type returns the packets type.

type Pubrel

type Pubrel struct {
	// Shared packet identifier.
	ID ID
}

A Pubrel packet is the response to a Pubrec packet. It is the third packet of the QOS 2 protocol exchange.

func NewPubrel

func NewPubrel() *Pubrel

NewPubrel creates a new Pubrel packet.

func (*Pubrel) Decode

func (p *Pubrel) Decode(src []byte) (int, error)

Decode reads from the byte slice argument. It returns the total number of bytes decoded, and whether there have been any errors during the process.

func (*Pubrel) Encode

func (p *Pubrel) Encode(dst []byte) (int, error)

Encode writes the packet bytes into the byte slice from the argument. It returns the number of bytes encoded and whether there's any errors along the way. If there is an error, the byte slice should be considered invalid.

func (*Pubrel) Len

func (p *Pubrel) Len() int

Len returns the byte length of the encoded packet.

func (*Pubrel) String

func (p *Pubrel) String() string

String returns a string representation of the packet.

func (*Pubrel) Type

func (p *Pubrel) Type() Type

Type returns the packets type.

type QOS

type QOS byte

QOS is the type used to store quality of service levels.

const (
	// QOSAtMostOnce defines that the message is delivered at most once, or it
	// may not be delivered at all.
	QOSAtMostOnce QOS = iota

	// QOSAtLeastOnce defines that the message is always delivered at least once.
	QOSAtLeastOnce QOS = iota

	// QOSExactlyOnce defines that the message is always delivered exactly once.
	QOSExactlyOnce QOS = iota

	// QOSFailure indicates that there has been an error while subscribing
	// to a specific topic.
	QOSFailure QOS = 0x80
)

func (QOS) Successful

func (qos QOS) Successful() bool

Successful returns if the provided quality of service level represents a successful value.

type Suback

type Suback struct {
	// The granted QOS levels for the requested subscriptions.
	ReturnCodes []QOS

	// The packet identifier.
	ID ID
}

A Suback packet is sent by the server to the client to confirm receipt and processing of a Subscribe packet. The Suback packet contains a list of return codes, that specify the maximum QOS levels that have been granted.

func NewSuback

func NewSuback() *Suback

NewSuback creates a new Suback packet.

func (*Suback) Decode

func (s *Suback) Decode(src []byte) (int, error)

Decode reads from the byte slice argument. It returns the total number of bytes decoded, and whether there have been any errors during the process.

func (*Suback) Encode

func (s *Suback) Encode(dst []byte) (int, error)

Encode writes the packet bytes into the byte slice from the argument. It returns the number of bytes encoded and whether there's any errors along the way. If there is an error, the byte slice should be considered invalid.

func (*Suback) Len

func (s *Suback) Len() int

Len returns the byte length of the encoded packet.

func (*Suback) String

func (s *Suback) String() string

String returns a string representation of the packet.

func (*Suback) Type

func (s *Suback) Type() Type

Type returns the packets type.

type Subscribe

type Subscribe struct {
	// The subscriptions.
	Subscriptions []Subscription

	// The packet identifier.
	ID ID
}

A Subscribe packet is sent from the client to the server to create one or more Subscriptions. The server will forward application messages that match these subscriptions using PublishPackets.

func NewSubscribe

func NewSubscribe() *Subscribe

NewSubscribe creates a new Subscribe packet.

func (*Subscribe) Decode

func (s *Subscribe) Decode(src []byte) (int, error)

Decode reads from the byte slice argument. It returns the total number of bytes decoded, and whether there have been any errors during the process.

func (*Subscribe) Encode

func (s *Subscribe) Encode(dst []byte) (int, error)

Encode writes the packet bytes into the byte slice from the argument. It returns the number of bytes encoded and whether there's any errors along the way. If there is an error, the byte slice should be considered invalid.

func (*Subscribe) Len

func (s *Subscribe) Len() int

Len returns the byte length of the encoded packet.

func (*Subscribe) String

func (s *Subscribe) String() string

String returns a string representation of the packet.

func (*Subscribe) Type

func (s *Subscribe) Type() Type

Type returns the packets type.

type Subscription

type Subscription struct {
	// The topic to subscribe.
	Topic string

	// The requested maximum QOS level.
	QOS QOS
}

A Subscription is a single subscription in a Subscribe packet.

func (*Subscription) String

func (s *Subscription) String() string

type Type

type Type byte

Type represents the MQTT packet types.

const (
	CONNECT Type
	CONNACK
	PUBLISH
	PUBACK
	PUBREC
	PUBREL
	PUBCOMP
	SUBSCRIBE
	SUBACK
	UNSUBSCRIBE
	UNSUBACK
	PINGREQ
	PINGRESP
	DISCONNECT
)

All packet types.

func DetectPacket

func DetectPacket(src []byte) (int, Type)

DetectPacket tries to detect the next packet in a buffer. It returns a length greater than zero if the packet has been detected as well as its Type.

func Types

func Types() []Type

Types returns a list of all known packet types.

func (Type) New

func (t Type) New() (Generic, error)

New creates a new packet based on the type. It is a shortcut to call one of the New*Packet functions. An error is returned if the type is invalid.

func (Type) String

func (t Type) String() string

String returns the type as a string.

func (Type) Valid

func (t Type) Valid() bool

Valid returns a boolean indicating whether the type is valid or not.

type Unsuback

type Unsuback struct {
	// Shared packet identifier.
	ID ID
}

An Unsuback packet is sent by the server to the client to confirm receipt of an Unsubscribe packet.

func NewUnsuback

func NewUnsuback() *Unsuback

NewUnsuback creates a new Unsuback packet.

func (*Unsuback) Decode

func (u *Unsuback) Decode(src []byte) (int, error)

Decode reads from the byte slice argument. It returns the total number of bytes decoded, and whether there have been any errors during the process.

func (*Unsuback) Encode

func (u *Unsuback) Encode(dst []byte) (int, error)

Encode writes the packet bytes into the byte slice from the argument. It returns the number of bytes encoded and whether there's any errors along the way. If there is an error, the byte slice should be considered invalid.

func (*Unsuback) Len

func (u *Unsuback) Len() int

Len returns the byte length of the encoded packet.

func (*Unsuback) String

func (u *Unsuback) String() string

String returns a string representation of the packet.

func (*Unsuback) Type

func (u *Unsuback) Type() Type

Type returns the packets type.

type Unsubscribe

type Unsubscribe struct {
	// The topics to unsubscribe from.
	Topics []string

	// The packet identifier.
	ID ID
}

An Unsubscribe packet is sent by the client to the server.

func NewUnsubscribe

func NewUnsubscribe() *Unsubscribe

NewUnsubscribe creates a new Unsubscribe packet.

func (*Unsubscribe) Decode

func (u *Unsubscribe) Decode(src []byte) (int, error)

Decode reads from the byte slice argument. It returns the total number of bytes decoded, and whether there have been any errors during the process.

func (*Unsubscribe) Encode

func (u *Unsubscribe) Encode(dst []byte) (int, error)

Encode writes the packet bytes into the byte slice from the argument. It returns the number of bytes encoded and whether there's any errors along the way. If there is an error, the byte slice should be considered invalid.

func (*Unsubscribe) Len

func (u *Unsubscribe) Len() int

Len returns the byte length of the encoded packet.

func (*Unsubscribe) String

func (u *Unsubscribe) String() string

String returns a string representation of the packet.

func (*Unsubscribe) Type

func (u *Unsubscribe) Type() Type

Type returns the packets type.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL