1# $Id: stun.py 47 2008-05-27 02:10:00Z jon.oberheide $
2# -*- coding: utf-8 -*-
3"""Simple Traversal of UDP through NAT."""
4from __future__ import print_function
5from __future__ import absolute_import
6
7import struct
8
9from . import dpkt
10
11# STUN - RFC 3489
12# http://tools.ietf.org/html/rfc3489
13# Each packet has a 20 byte header followed by 0 or more attribute TLVs.
14
15# Message Types
16BINDING_REQUEST = 0x0001
17BINDING_RESPONSE = 0x0101
18BINDING_ERROR_RESPONSE = 0x0111
19SHARED_SECRET_REQUEST = 0x0002
20SHARED_SECRET_RESPONSE = 0x0102
21SHARED_SECRET_ERROR_RESPONSE = 0x0112
22
23# Message Attributes
24MAPPED_ADDRESS = 0x0001
25RESPONSE_ADDRESS = 0x0002
26CHANGE_REQUEST = 0x0003
27SOURCE_ADDRESS = 0x0004
28CHANGED_ADDRESS = 0x0005
29USERNAME = 0x0006
30PASSWORD = 0x0007
31MESSAGE_INTEGRITY = 0x0008
32ERROR_CODE = 0x0009
33UNKNOWN_ATTRIBUTES = 0x000a
34REFLECTED_FROM = 0x000b
35
36
37class STUN(dpkt.Packet):
38    """Simple Traversal of UDP through NAT.
39
40    STUN - RFC 3489
41    http://tools.ietf.org/html/rfc3489
42    Each packet has a 20 byte header followed by 0 or more attribute TLVs.
43
44    Attributes:
45        __hdr__: Header fields of STUN.
46        TODO.
47    """
48
49    __hdr__ = (
50        ('type', 'H', 0),
51        ('len', 'H', 0),
52        ('xid', '16s', 0)
53    )
54
55
56def tlv(buf):
57    n = 4
58    t, l_ = struct.unpack('>HH', buf[:n])
59    v = buf[n:n + l_]
60    pad = (n - l_ % n) % n
61    buf = buf[n + l_ + pad:]
62    return t, l_, v, buf
63
64
65def parse_attrs(buf):
66    """Parse STUN.data buffer into a list of (attribute, data) tuples."""
67    attrs = []
68    while buf:
69        t, _, v, buf = tlv(buf)
70        attrs.append((t, v))
71    return attrs
72
73
74def test_stun_response():
75    s = (b'\x01\x01\x00\x0c\x21\x12\xa4\x42\x53\x4f\x70\x43\x69\x69\x35\x4a\x66\x63\x31\x7a\x00\x01'
76         b'\x00\x08\x00\x01\x11\x22\x33\x44\x55\x66')
77    m = STUN(s)
78    assert m.type == BINDING_RESPONSE
79    assert m.len == 12
80
81    attrs = parse_attrs(m.data)
82    assert attrs == [(MAPPED_ADDRESS, b'\x00\x01\x11\x22\x33\x44\x55\x66'), ]
83
84
85def test_stun_padded():
86    s = (b'\x00\x01\x00\x54\x21\x12\xa4\x42\x35\x59\x53\x6e\x42\x71\x70\x56\x77\x61\x39\x4f\x00\x06'
87         b'\x00\x17\x70\x4c\x79\x5a\x48\x52\x3a\x47\x77\x4c\x33\x41\x48\x42\x6f\x76\x75\x62\x4c\x76'
88         b'\x43\x71\x6e\x00\x80\x2a\x00\x08\x18\x8b\x10\x4c\x69\x7b\xf6\x5b\x00\x25\x00\x00\x00\x24'
89         b'\x00\x04\x6e\x00\x1e\xff\x00\x08\x00\x14\x60\x2b\xc7\xfc\x0d\x10\x63\xaa\xc5\x38\x1c\xcb'
90         b'\x96\xa9\x73\x08\x73\x9a\x96\x0c\x80\x28\x00\x04\xd1\x62\xea\x65')
91    m = STUN(s)
92    assert m.type == BINDING_REQUEST
93    assert m.len == 84
94
95    attrs = parse_attrs(m.data)
96    assert len(attrs) == 6
97    assert attrs[0] == (USERNAME, b'pLyZHR:GwL3AHBovubLvCqn')
98    assert attrs[4][0] == MESSAGE_INTEGRITY
99