summaryrefslogtreecommitdiff
path: root/external/construct/protocols/layer2/ethernet.py
diff options
context:
space:
mode:
authorLivio Recchia <recchialivio@libero.it>2020-02-10 23:06:34 +0100
committerLivio Recchia <recchialivio@libero.it>2020-02-10 23:06:34 +0100
commit9a13903a2f7d3a65fdf15a65fb59cccd622e2066 (patch)
tree9403b7dff39eb5e5d7fa0f79efb69b496add4c4b /external/construct/protocols/layer2/ethernet.py
parent11cc316b74d5f3f283413a33e7693b314741aa4a (diff)
downloadmanachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.tar.gz
manachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.tar.bz2
manachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.tar.xz
manachat-9a13903a2f7d3a65fdf15a65fb59cccd622e2066.zip
Initial commit
Diffstat (limited to 'external/construct/protocols/layer2/ethernet.py')
-rw-r--r--external/construct/protocols/layer2/ethernet.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/external/construct/protocols/layer2/ethernet.py b/external/construct/protocols/layer2/ethernet.py
new file mode 100644
index 0000000..d8d4bdd
--- /dev/null
+++ b/external/construct/protocols/layer2/ethernet.py
@@ -0,0 +1,38 @@
+"""
+Ethernet (TCP/IP protocol stack)
+"""
+from construct import *
+from binascii import hexlify, unhexlify
+import six
+
+
+class MacAddressAdapter(Adapter):
+ def _encode(self, obj, context):
+ return unhexlify(obj.replace("-", ""))
+ def _decode(self, obj, context):
+ return "-".join(hexlify(b) for b in obj)
+
+def MacAddress(name):
+ return MacAddressAdapter(Bytes(name, 6))
+
+ethernet_header = Struct("ethernet_header",
+ MacAddress("destination"),
+ MacAddress("source"),
+ Enum(UBInt16("type"),
+ IPv4 = 0x0800,
+ ARP = 0x0806,
+ RARP = 0x8035,
+ X25 = 0x0805,
+ IPX = 0x8137,
+ IPv6 = 0x86DD,
+ _default_ = Pass,
+ ),
+)
+
+
+if __name__ == "__main__":
+ cap = unhexlify(six.b("0011508c283c0002e34260090800"))
+ obj = ethernet_header.parse(cap)
+ print (obj)
+ print (repr(ethernet_header.build(obj)))
+