1
0
Fork 0

Parse IP header (src, dst)

master
Alex Kotov 3 years ago
parent 7ccd4c43f7
commit 88fd54bc54
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
  1. 34
      main.py

@ -1,6 +1,27 @@
#!/usr/bin/env python3
import pytun
import struct
import ipaddress
class IpHeader:
def __init__(self, raw):
self.ver_ihl, \
self.tos, \
self.total_length, \
self.ident, \
self.flags_fragoffset, \
self.ttl, \
self.proto, \
self.chksum, \
src, \
dst, \
self.opt1, \
self.opt2, \
self.pad = struct.unpack('>BBHHHBBHIIHBB', raw)
self.src = ipaddress.IPv4Address(src)
self.dst = ipaddress.IPv4Address(dst)
def main():
tun_iface = pytun.TunTapDevice()
@ -16,7 +37,18 @@ def main():
while True:
buf = tun_iface.read(tun_iface.mtu)
print(buf)
flags = buf[:2]
proto = buf[2:4]
if proto != b'\x08\x00':
continue
ip_header = IpHeader(buf[4:28])
body = buf[28:]
print(ip_header.src, '', ip_header.dst, 'len: %d' % ip_header.total_length)
if __name__ == '__main__':
main()