Skip to content

Commit

Permalink
add 0x04 Reason Code functionality (#395)
Browse files Browse the repository at this point in the history
* add 0x04 Reason Code functionality

* fix tpackets test

* simplify logic

---------

Co-authored-by: Derek Duncan <derekduncan@gmail.com>
  • Loading branch information
dgduncan and Derek Duncan committed Apr 30, 2024
1 parent 57997ef commit 64ea905
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packets/tpackets.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ const (
TDisconnect
TDisconnectTakeover
TDisconnectMqtt5
TDisconnectMqtt5DisconnectWithWillMessage
TDisconnectSecondConnect
TDisconnectReceiveMaximum
TDisconnectDropProperties
Expand Down Expand Up @@ -3781,6 +3782,31 @@ var TPacketData = map[byte]TPacketCases{
},
},
},
{
Case: TDisconnectMqtt5DisconnectWithWillMessage,
Desc: "mqtt5 disconnect with will message",
Primary: true,
RawBytes: append([]byte{
Disconnect << 4, 38, // fixed header
CodeDisconnectWillMessage.Code, // Reason Code
36, // Properties Length
17, 0, 0, 0, 120, // Session Expiry Interval (17)
31, 0, 28, // Reason String (31)
}, []byte(CodeDisconnectWillMessage.Reason)...),
Packet: &Packet{
ProtocolVersion: 5,
FixedHeader: FixedHeader{
Type: Disconnect,
Remaining: 22,
},
ReasonCode: CodeDisconnectWillMessage.Code,
Properties: Properties{
ReasonString: CodeDisconnectWillMessage.Reason,
SessionExpiryInterval: 120,
SessionExpiryIntervalFlag: true,
},
},
},
{
Case: TDisconnectSecondConnect,
Desc: "second connect packet mqtt5",
Expand Down
4 changes: 4 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,10 @@ func (s *Server) processDisconnect(cl *Client, pk packets.Packet) error {
cl.Properties.Props.SessionExpiryIntervalFlag = true
}

if pk.ReasonCode == packets.CodeDisconnectWillMessage.Code { // [MQTT-3.1.2.5] Non-normative comment
return packets.CodeDisconnectWillMessage
}

s.loop.willDelayed.Delete(cl.ID) // [MQTT-3.1.3-9] [MQTT-3.1.2-8]
cl.Stop(packets.CodeDisconnect) // [MQTT-3.14.4-2]

Expand Down
16 changes: 16 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3138,6 +3138,22 @@ func TestServerProcessPacketDisconnectNonZeroExpiryViolation(t *testing.T) {
require.ErrorIs(t, err, packets.ErrProtocolViolationZeroNonZeroExpiry)
}

func TestServerProcessPacketDisconnectDisconnectWithWillMessage(t *testing.T) {
s := newServer()
cl, _, _ := newTestClient()
cl.Properties.Props.SessionExpiryInterval = 30
cl.Properties.ProtocolVersion = 5

s.loop.willDelayed.Add(cl.ID, packets.Packet{TopicName: "a/b/c", Payload: []byte("hello")})
require.Equal(t, 1, s.loop.willDelayed.Len())

err := s.processPacket(cl, *packets.TPacketData[packets.Disconnect].Get(packets.TDisconnectMqtt5DisconnectWithWillMessage).Packet)
require.Error(t, err)

require.Equal(t, 1, s.loop.willDelayed.Len())
require.False(t, cl.Closed())
}

func TestServerProcessPacketAuth(t *testing.T) {
s := newServer()
cl, r, w := newTestClient()
Expand Down

0 comments on commit 64ea905

Please sign in to comment.