Skip to content

Commit

Permalink
Parse ERROR-CODE attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
jablko committed Sep 16, 2011
1 parent c81181f commit e65a44f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
19 changes: 19 additions & 0 deletions stun.py
Expand Up @@ -33,10 +33,16 @@
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

class attribute:
__metaclass__ = type

def __init__(ctx, type, value):
ctx.type = type
ctx.value = value

__delitem__ = object.__delattr__
__getitem__ = object.__getattribute__
__setitem__ = object.__setattr__

class message:
def __init__(ctx):
ctx.attribute = untwisted.oneMany()
Expand Down Expand Up @@ -122,6 +128,19 @@ def request(server, messageMethod=binding):
elif IPv6 == itm.family:
itm.address = socket.inet_ntop(socket.AF_INET6, itm.value[4:])

elif ERROR_CODE == type:

# 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# | Reserved, should be 0 |Class| Number |
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
# | Reason Phrase (variable) ...
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

itm['class'] = ord(itm.value[2])
itm.number = ord(itm.value[3])
itm.reasonPhrase = itm.value[4:]

elif XOR_MAPPED_ADDRESS == type:

# 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F
Expand Down
32 changes: 32 additions & 0 deletions test/stun
Expand Up @@ -133,6 +133,38 @@ def errorResponse():

transport.recv.__self__('\1\x11\0\x10\x21\x12\xa4\x42abcdefghijkl\0\x09\0\x0a\0\0\5\x43Expect\0\0')

@test
def errorCode():
expect(4)

class transport:
recv = promise.sequence().shift
write = promise.sequence()

class callback:

@staticmethod
def throw(type, value, traceback):
equal(5, value.attribute['class'])
equal(67, value.attribute.number)
equal('Expect', value.attribute.reasonPhrase)

connect = udp.connect
urandom = os.urandom

udp.connect, os.urandom = lambda host, port: lambda: transport, lambda _: 'abcdefghijkl'

try:
stun.request('example.com').then(callback)

finally:
udp.connect = connect
os.urandom = urandom

transport.write.shift().then(untwisted.partial(equal, '\0\1\0\0\x21\x12\xa4\x42abcdefghijkl'))

transport.recv.__self__('\1\x11\0\x10\x21\x12\xa4\x42abcdefghijkl\0\x09\0\x0a\0\0\5\x43Expect\0\0')

@test
def many():
expect(5)
Expand Down

0 comments on commit e65a44f

Please sign in to comment.