-
Notifications
You must be signed in to change notification settings - Fork 0
/
ICMP.py
66 lines (55 loc) · 2.51 KB
/
ICMP.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from Headers import *
from Debug import *
import ipaddress
class ICMPHandler:
def __init__(self, ID, linkid, ip, nmask, debug=1):
self.DEBUG = debug
self.id = ID
# If the "parent" ip is ever changed, it should be done with their setIP()
# function, which will manually update these variables
self.ip = ip
self.nmask = nmask
# Used to keep track of outgoing ICMP connections
self.icmp_table = {}
def sendICMP(self, targetIP, targetID):
# We take in targetID because we first ARP before calling this function,
# and must construct the L2 frame
ICMP = createICMPHeader(8)
p3 = makePacket_L3(self.ip, targetIP, proto="ICMP", data=ICMP)
p2 = makePacket_L2("IPv4", self.id, targetID)
p = makePacket(p2, p3)
self.icmp_table[ICMP["identifier"]] = False
if self.DEBUG:
Debug(self.id, "sending ICMP to", targetIP,
color="green", f=self.__class__.__name__
)
return p
def handleICMP(self, data):
if data["L3"]["Data"]["type"] == 8: # Got a request
ICMP = createICMPHeader(0, identifier=data["L3"]["Data"]["identifier"])
p3 = makePacket_L3(data["L3"]["DIP"], data["L3"]["SIP"], proto="ICMP", data=ICMP)
p2 = makePacket_L2("IPv4", self.id, data["L2"]["From"])
p = makePacket(p2, p3)
if self.DEBUG:
Debug(self.id, "Received ICMP request from", data["L3"]["SIP"], ", responding",
color="green", f=self.__class__.__name__
)
return p
elif data["L3"]["Data"]["type"] == 0: # Got a reply
if self.DEBUG:
Debug(self.id, "Received ICMP reply from", data["L3"]["SIP"],
color="green", f=self.__class__.__name__
)
if data["L3"]["Data"]["identifier"] in self.icmp_table:
self.icmp_table[data["L3"]["Data"]["identifier"]] = True
else:
if self.DEBUG:
Debug(self.id, data["L3"]["Data"]["identifier"], "not in ICMP table - did I send this request?",
color="red", f=self.__class__.__name__
)
return
else:
if self.DEBUG:
Debug(self.id, "Received malformed ICMP response type from", data["L2"]["From"], ":", data["L3"]["SIP"],
color="red", f=self.__class__.__name__
)