summaryrefslogtreecommitdiff
path: root/external/construct/protocols/layer3/ipv6.py
blob: 18a0955a3fc6c65cf66f2eb5253151910cbea1d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
Internet Protocol version 6 (TCP/IP protocol stack)
"""
from construct import *
from ipv4 import ProtocolEnum
from binascii import unhexlify
import six


class Ipv6AddressAdapter(Adapter):
    def _encode(self, obj, context):
        if bytes is str:
            return "".join(part.decode("hex") for part in obj.split(":"))
        else:
            return bytes(int(part, 16) for part in obj.split(":"))
    def _decode(self, obj, context):
        if bytes is str:
            return ":".join(b.encode("hex") for b in obj)
        else:
            return ":".join("%02x" % (b,) for b in obj)

def Ipv6Address(name):
    return Ipv6AddressAdapter(Bytes(name, 16))


ipv6_header = Struct("ip_header",
    EmbeddedBitStruct(
        OneOf(Bits("version", 4), [6]),
        Bits("traffic_class", 8),
        Bits("flow_label", 20),
    ),
    UBInt16("payload_length"),
    ProtocolEnum(UBInt8("protocol")),
    UBInt8("hoplimit"),
    Alias("ttl", "hoplimit"),
    Ipv6Address("source"),
    Ipv6Address("destination"),
)


if __name__ == "__main__":
    o = ipv6_header.parse(six.b("\x6f\xf0\x00\x00\x01\x02\x06\x80"
        "0123456789ABCDEF" "FEDCBA9876543210"
        ))
    print (o)
    print (repr(ipv6_header.build(o)))