Skip to content

Commit

Permalink
Check client side maximum packet size eclipse-paho#3
Browse files Browse the repository at this point in the history
Signed-off-by: Daichi Tomaru <[email protected]>
  • Loading branch information
tomatod committed Jan 15, 2023
1 parent d63b3b2 commit 75608e1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packets/packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ func (c *ControlPacket) PacketID() uint16 {
}
}

func (c *ControlPacket) GetPacketSize() int {
return 1 + len(encodeVBI(c.FixedHeader.remainingLength)) + c.remainingLength
}

func (c *ControlPacket) PacketType() string {
return [...]string{
"",
Expand Down
8 changes: 8 additions & 0 deletions packets/packets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@ func TestReadPacketConnect(t *testing.T) {
assert.Equal(t, uint32(30), *c.Content.(*Connect).Properties.SessionExpiryInterval)
}

func TestGetPacketSize(t *testing.T) {
// PUBLISH packet (topic: test, message: test)
p := []byte{0x30, 0x0b, 0x00, 0x04, 0x74, 0x65, 0x73, 0x74, 0x00, 0x74, 0x65, 0x73, 0x74}
c, err := ReadPacket(bufio.NewReader(bytes.NewReader(p)))
require.Nil(t, err)
assert.Equal(t, len(p), c.GetPacketSize())
}

func TestReadStringWriteString(t *testing.T) {
var b bytes.Buffer
writeString("Test string", &b)
Expand Down
13 changes: 13 additions & 0 deletions paho/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,13 @@ func (c *Client) incoming() {
go c.error(err)
return
}
if c.clientProps.MaximumPacketSize != 0 && recv.GetPacketSize() > int(c.clientProps.MaximumPacketSize) {
go c.errorWithDisconnect(
errors.New("received a packet whose size exceeds the client's maximum packet size limit."),
&Disconnect{ReasonCode: 0x95},
)
return
}
switch recv.Type {
case packets.CONNACK:
c.debug.Println("received CONNACK")
Expand Down Expand Up @@ -558,6 +565,12 @@ func (c *Client) error(e error) {
go c.OnClientError(e)
}

func (c *Client) errorWithDisconnect(e error, d *Disconnect) {
c.debug.Println("error called:", e)
c.Disconnect(d)
go c.OnClientError(e)
}

func (c *Client) serverDisconnect(d *Disconnect) {
c.close()
c.workers.Wait()
Expand Down

0 comments on commit 75608e1

Please sign in to comment.