Skip to content

Commit 286523c

Browse files
authored
Fix MQTT SubAck retcodes (#4257)
1 parent 970aa8a commit 286523c

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

scapy/contrib/mqtt.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,17 @@
77
# scapy.contrib.status = loads
88

99
from scapy.packet import Packet, bind_layers
10-
from scapy.fields import FieldLenField, BitEnumField, StrLenField, \
11-
ShortField, ConditionalField, ByteEnumField, ByteField, PacketListField
10+
from scapy.fields import (
11+
BitEnumField,
12+
ByteEnumField,
13+
ByteField,
14+
ConditionalField,
15+
FieldLenField,
16+
FieldListField,
17+
PacketListField,
18+
ShortField,
19+
StrLenField,
20+
)
1221
from scapy.layers.inet import TCP
1322
from scapy.error import Scapy_Exception
1423
from scapy.compat import orb, chb
@@ -250,18 +259,26 @@ class MQTTSubscribe(Packet):
250259

251260

252261
ALLOWED_RETURN_CODE = {
253-
0: 'Success',
254-
1: 'Success',
255-
2: 'Success',
256-
128: 'Failure'
262+
0x00: 'Granted QoS 0',
263+
0x01: 'Granted QoS 1',
264+
0x02: 'Granted QoS 2',
265+
0x80: 'Unspecified error',
266+
0x83: 'Implementation specific error',
267+
0x87: 'Not authorized',
268+
0x8F: 'Topic Filter invalid',
269+
0x91: 'Packet Identifier in use',
270+
0x97: 'Quota exceeded',
271+
0x9E: 'Shared Subscriptions not supported',
272+
0xA1: 'Subscription Identifiers not supported',
273+
0xA2: 'Wildcard Subscriptions not supported',
257274
}
258275

259276

260277
class MQTTSuback(Packet):
261278
name = "MQTT suback"
262279
fields_desc = [
263280
ShortField("msgid", None),
264-
ByteEnumField("retcode", None, ALLOWED_RETURN_CODE)
281+
FieldListField("retcodes", None, ByteEnumField("", None, ALLOWED_RETURN_CODE))
265282
]
266283

267284

test/contrib/mqtt.uts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,21 @@ assert subscribe.topics[0].QOS == 1
112112

113113

114114
= MQTTSuback, packet instantiation
115-
sk = MQTT()/MQTTSuback(msgid=1, retcode=0)
115+
sk = MQTT()/MQTTSuback(msgid=1, retcodes=[0])
116116
assert sk.type == 9
117117
assert sk.msgid == 1
118-
assert sk.retcode == 0
118+
assert sk.retcodes == [0]
119119

120120
= MQTTSuback, packet dissection
121121
s = b'\x90\x03\x00\x01\x00'
122122
suback = MQTT(s)
123123
assert suback.msgid == 1
124-
assert suback.retcode == 0
124+
assert suback.retcodes == [0]
125+
126+
s = b'\x90\x03\x00\x01\x00\x01'
127+
suback = MQTT(s)
128+
assert suback.msgid == 1
129+
assert suback.retcodes == [0, 1]
125130

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

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

0 commit comments

Comments
 (0)