Skip to content

Commit 158c821

Browse files
committed
STUN: Fix building binding response
Building a binding success response with an XOR-MAPPED-ADDRESS attribute filed with: File "/home/simon/src/scapy/scapy/contrib/stun.py", line 152, in i2m return struct.pack(">i", struct.unpack(">i", inet_aton(x)) ^ MAGIC_COOKIE) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~ TypeError: While dissecting field 'attributes': While dissecting field 'xip': unsupported operand type(s) for ^: 'tuple' and 'int' Fixed by properly handling the return value of struct.unpack (a tuple with one int).
1 parent a4f958b commit 158c821

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

scapy/contrib/stun.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def m2i(self, pkt, x):
149149
def i2m(self, pkt, x):
150150
if x is None:
151151
return b"\x00\x00\x00\x00"
152-
return struct.pack(">i", struct.unpack(">i", inet_aton(x)) ^ MAGIC_COOKIE)
152+
return struct.pack(">i", struct.unpack(">i", inet_aton(x))[0] ^ MAGIC_COOKIE)
153153

154154

155155
class STUNXorMappedAddress(STUNGenericTlv):

test/contrib/stun.uts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,21 @@ stun = STUN(
145145
built = stun.build()
146146
parsed = STUN(built)
147147

148-
assert parsed.build() == built
148+
assert parsed.build() == built
149+
150+
= test STUN packet build with attributes
151+
stun = STUN(
152+
stun_message_type="Binding success response",
153+
transaction_id=0x3479476534635936316a796a,
154+
attributes=[
155+
STUNXorMappedAddress(xport=25000, xip="172.20.0.200"),
156+
STUNUsername(length=37, username="Ht11MaRZHc4GOLJUsbu1R3YCs72HYN25:oNph"),
157+
STUNMessageIntegrity(hmac_sha1=0x4b67036dfb65ca84d63bcac86c8d5981df657031),
158+
STUNFingerprint(crc_32=0x4041e9c3)
159+
]
160+
)
161+
162+
built = stun.build()
163+
parsed = STUN(built)
164+
165+
assert parsed.build() == built

0 commit comments

Comments
 (0)