-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrouter_simulator.py
More file actions
179 lines (160 loc) · 7.4 KB
/
router_simulator.py
File metadata and controls
179 lines (160 loc) · 7.4 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import multiprocessing
import os
import binascii
import socket
import threading
import time
import struct
from Queue import Empty
from arp_mac_table import ARPnMACRow, ARPnMACTable
from netaddr import EUI
from netaddr import IPAddress
from arp import ARPPacket
from ethernet import EthernetFrame
from interface import Interface
from ip import IPDatagram
from logger import LOG
from rip_simulator import RIPSimulator
from routing_table import RoutingTable
'''
RouterSimulator class simulated an Router in simple way.
'''
class RouterSimulator:
def __init__(self, name):
self.intList = [] # interface list
self.name = name
self.message_content = ""
self.route_table = RoutingTable(router=self)
self.arp_mac_table = ARPnMACTable()
self.config_file_path = "config/" + self.name
self.arp_mac_table_path = "config/" + self.name + "_apr_mac"
self.initialize_router()
self.received_datagram_queue = multiprocessing.Queue()
self.received_frame_data_queue = multiprocessing.Queue()
self.receive = threading.Thread(target=self.receive_frame)
self.receive.start()
self.routing_packets = threading.Thread(target=self.route_ip_datagram) # forwarding IP packets
self.routing_packets.start()
self.rip_simulator = RIPSimulator(self)
def initialize_router(self):
int_name_list = "faster 0/0", "faster 1/1", "ser 0/0", "ser 0/1"
int_type_list = (1, 1, 0, 0)
for i in range(0, 4): # each router equipped with four ports
mac = binascii.b2a_hex(os.urandom(6)) # random generate 48-bit Mac Address presented by 12 hex numbers
interface = Interface()
interface.mac = str(EUI(mac))
interface.router = self
interface.name = int_name_list[i]
interface.type = int_type_list[i]
self.intList.append(interface)
self.load_config()
def save_config(self):
if os.path.exists(self.config_file_path):
os.remove(self.config_file_path)
conf_file = open(self.config_file_path, 'a+') # Trying to create a new file or open one
print len(self.intList)
for port in self.intList:
conf_file.write(port.name + " : " + str(port.type) + " : " + port.ip_addr + " : " + port.mac + os.linesep)
conf_file.close()
self.init_arp_mac_table()
self.arp_mac_table.save_table(self.arp_mac_table_path)
def load_config(self):
LOG.info(self.name + "*******************load_config******************************")
if os.path.exists(str(self.config_file_path)):
conf_file = open(self.config_file_path, 'r')
i = 0
for line in conf_file.readlines():
line = line.split(":")
self.intList[i].name = str(line[0]).strip()
self.intList[i].type = str(line[1]).strip()
self.intList[i].ip_addr = str(line[2]).strip()
self.intList[i].mac = str(line[3]).strip()
i += 1
self.arp_mac_table.mac_table = []
self.arp_mac_table.load_table_config(self.arp_mac_table_path)
self.route_table.init_routing_table(self)
def show_config(self):
for port in self.intList:
LOG.info(port.name + " : " + str(port.type) + " : " + port.ip_addr + " : " + port.mac + "\n")
LOG.info("----------------------------------------\n")
def receive_frame(self):
LOG.info(self.name + ":starting listening and receiving frame.....")
time.sleep(0.01)
data_frame = EthernetFrame(dest_mac="", src_mac="")
while True:
try:
ip_data_frame = self.received_frame_data_queue.get(0)
data_frame.unpack(ip_data_frame)
LOG.debug(self.name + ":from receive_frame:" + data_frame.__repr__())
if 0x0800 == data_frame.eth_tcode: # ip protocol
self.received_datagram_queue.put(data_frame.data)
except Empty:
pass
# finally:
# self.receive_frame()
def route_ip_datagram(self):
LOG.info(self.name + ":starting listening and routing...")
time.sleep(0.01)
ip_data = IPDatagram("", "", data="")
while True:
try:
ip_data_packet = self.received_datagram_queue.get()
ip_data.unpack(ip_data_packet)
if ip_data.ip_proto == socket.IPPROTO_UDP:
self.rip_simulator.received_queue.put(ip_data.data)
else:
LOG.info(self.name + ":from route_ip_datagram+:" + ip_data.__repr__())
dest_ip = socket.inet_ntoa(ip_data.ip_dest_addr)
LOG.debug(self.name + ":from des_ip+:" + dest_ip)
# inter_ip = self.route_table.find_longest_match_network(dest_ip)
match_row = self.route_table.find_shortest_path(dest_ip)
if match_row:
LOG.debug(self.name + ":matched interface IP:" + match_row.inter_ip)
for interface in self.intList:
if interface.ip_addr != "0.0.0.0" and IPAddress(interface.ip_addr) == \
IPAddress(match_row.inter_ip):
LOG.debug("matched interface=" + interface.name)
if match_row.metric == 0:
interface.send_queue.put([match_row.dest_ip, ip_data_packet])
else:
interface.send_queue.put([match_row.next_ip, ip_data_packet])
else:
LOG.debug(self.name + ":**** dest ip =" + dest_ip + " not reachable or current router address!!")
except Empty:
pass
# finally:
# self.route_ip_datagram()
def init_arp_mac_table(self):
self.arp_mac_table.mac_table = []
for inter in self.intList:
if inter.ip_addr != "0.0.0.0":
arp_n_mac_row = ARPnMACRow(ip_addr=inter.ip_addr, mac=inter.mac, mac_type=1,
inter_name=inter.name, age=-1)
self.arp_mac_table.mac_table.append(arp_n_mac_row)
def main():
test_mac = struct.pack('!6B',
int('7b', 16), int('4c', 16), int('95', 16),
int('23', 16), int('e8', 16), int('89', 16))
tha = struct.pack('!6B',
int('FF', 16), int('FF', 16), int('FF', 16),
int('FF', 16), int('FF', 16), int('FF', 16))
ip = struct.pack('4B', 101, 104, 10, 10)
router = RouterSimulator("Router1")
arp_packet = ARPPacket(sha=test_mac, spa=ip, tha=tha, tpa=ip)
e = EthernetFrame(test_mac, test_mac, tcode=0x0806, data=arp_packet.pack())
router.received_frame_data_queue.put(e.pack())
time.sleep(1)
router.received_frame_data_queue.put(e.pack())
ip = struct.pack('4B', 192, 168, 1, 1)
ip_data = IPDatagram(ip, ip, data="2879834kjkjdsfrhsdkd233")
e.data = ip_data.pack()
e.eth_tcode = 0x0800
if e.eth_tcode == 0x0800:
print True
router.received_frame_data_queue.put(e.pack())
router.receive.join()
router.routing_packets.join()
for interface in router.intList:
interface.send_thread.join()
if __name__ == "__main__":
main()