51 lines
1.5 KiB
Python
51 lines
1.5 KiB
Python
from ctypes import *
|
|
import socket
|
|
import struct
|
|
import ipaddress
|
|
import os
|
|
|
|
class IP_ctype(Structure):
|
|
_fields_ = [
|
|
("version", c_ubyte, 4),
|
|
("ihl", c_ubyte, 4),
|
|
("tos", c_ubyte, 8),
|
|
("len", c_ushort, 16),
|
|
("id", c_ushort, 16),
|
|
("offset", c_ushort, 16),
|
|
("ttl", c_ubyte, 8),
|
|
("protocol_num", c_ubyte, 8),
|
|
("sum", c_ushort, 16),
|
|
("src", c_uint32, 32),
|
|
("dst", c_uint32, 32)
|
|
]
|
|
|
|
def __new__(cls, socket_buffer=None):
|
|
return cls.from_buffer_copy(socket_buffer)
|
|
|
|
def __init__(self, socket_buffer=None):
|
|
self.src_address = socket.inet_ntoa(struct.pack("<L",self.src))
|
|
self.dst_address = socket.inet_ntoa(struct.pack("<L",self.dst))
|
|
self.protocol_map = {1:"ICMP", 2:"IGMP", 6:"TCP", 17: "UDP"}
|
|
try:
|
|
self.protocol = self.protocol_map[self.protocol_num]
|
|
except Exception as e:
|
|
print(f'{e} No known protocol for {self.protocol_num}')
|
|
|
|
|
|
|
|
|
|
# ICMP response packet
|
|
class ICMP_ctype(Structure):
|
|
_fields_ = [
|
|
("type", c_ubyte, 8),
|
|
("code", c_ubyte, 8),
|
|
("sum", c_ushort, 16),
|
|
("id", c_ushort, 16),
|
|
("seq", c_ushort, 16)
|
|
]
|
|
|
|
def __new__(cls, socket_buffer=None):
|
|
return cls.from_buffer_copy(socket_buffer)
|
|
|
|
|