Skip to content

Commit

Permalink
Fix MQTT SubAck retcodes (#4257)
Browse files Browse the repository at this point in the history
  • Loading branch information
gpotter2 committed Feb 6, 2024
1 parent 970aa8a commit 286523c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
31 changes: 24 additions & 7 deletions scapy/contrib/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,17 @@
# scapy.contrib.status = loads

from scapy.packet import Packet, bind_layers
from scapy.fields import FieldLenField, BitEnumField, StrLenField, \
ShortField, ConditionalField, ByteEnumField, ByteField, PacketListField
from scapy.fields import (
BitEnumField,
ByteEnumField,
ByteField,
ConditionalField,
FieldLenField,
FieldListField,
PacketListField,
ShortField,
StrLenField,
)
from scapy.layers.inet import TCP
from scapy.error import Scapy_Exception
from scapy.compat import orb, chb
Expand Down Expand Up @@ -250,18 +259,26 @@ class MQTTSubscribe(Packet):


ALLOWED_RETURN_CODE = {
0: 'Success',
1: 'Success',
2: 'Success',
128: 'Failure'
0x00: 'Granted QoS 0',
0x01: 'Granted QoS 1',
0x02: 'Granted QoS 2',
0x80: 'Unspecified error',
0x83: 'Implementation specific error',
0x87: 'Not authorized',
0x8F: 'Topic Filter invalid',
0x91: 'Packet Identifier in use',
0x97: 'Quota exceeded',
0x9E: 'Shared Subscriptions not supported',
0xA1: 'Subscription Identifiers not supported',
0xA2: 'Wildcard Subscriptions not supported',
}


class MQTTSuback(Packet):
name = "MQTT suback"
fields_desc = [
ShortField("msgid", None),
ByteEnumField("retcode", None, ALLOWED_RETURN_CODE)
FieldListField("retcodes", None, ByteEnumField("", None, ALLOWED_RETURN_CODE))
]


Expand Down
13 changes: 9 additions & 4 deletions test/contrib/mqtt.uts
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,21 @@ assert subscribe.topics[0].QOS == 1


= MQTTSuback, packet instantiation
sk = MQTT()/MQTTSuback(msgid=1, retcode=0)
sk = MQTT()/MQTTSuback(msgid=1, retcodes=[0])
assert sk.type == 9
assert sk.msgid == 1
assert sk.retcode == 0
assert sk.retcodes == [0]

= MQTTSuback, packet dissection
s = b'\x90\x03\x00\x01\x00'
suback = MQTT(s)
assert suback.msgid == 1
assert suback.retcode == 0
assert suback.retcodes == [0]

s = b'\x90\x03\x00\x01\x00\x01'
suback = MQTT(s)
assert suback.msgid == 1
assert suback.retcodes == [0, 1]

= MQTTUnsubscribe, packet instantiation
unsb = MQTT()/MQTTUnsubscribe(msgid=1, topics=[MQTTTopic(topic='newtopic',length=0)])
Expand Down Expand Up @@ -181,4 +186,4 @@ assert MQTTUnsubscribe in u and len(u.topics) == 2 and u.topics[1].topic == b"c/

= MQTTSubscribe
u = MQTT(b'\x82\x10\x00\x01\x00\x03\x61\x2F\x62\x02\x00\x03\x63\x2F\x64\x00')
assert MQTTSubscribe in u and len(u.topics) == 2 and u.topics[1].topic == b"c/d"
assert MQTTSubscribe in u and len(u.topics) == 2 and u.topics[1].topic == b"c/d"

0 comments on commit 286523c

Please sign in to comment.