Parse IP header (src, dst)
This commit is contained in:
parent
7ccd4c43f7
commit
88fd54bc54
1 changed files with 33 additions and 1 deletions
34
main.py
34
main.py
|
@ -1,6 +1,27 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import pytun
|
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():
|
def main():
|
||||||
tun_iface = pytun.TunTapDevice()
|
tun_iface = pytun.TunTapDevice()
|
||||||
|
@ -16,7 +37,18 @@ def main():
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
buf = tun_iface.read(tun_iface.mtu)
|
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__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
Reference in a new issue