-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.cpp
301 lines (255 loc) · 8.94 KB
/
node.cpp
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <inttypes.h>
#include <arpa/inet.h>
#include <iostream>
#include <errno.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include <thread>
#include <functional>
#include "link.hpp"
#include "ip.hpp"
#ifdef READLINE
#include <readline/readline.h>
#include <readline/history.h>
#endif
#include "dbg.hpp"
#include "lnxparse.hpp"
#include "routing.hpp"
using namespace std;
std::thread sending_routing_table_thread;
std::thread recv_routing_table_thread;
std::thread delete_expired_nodes_thread;
Link *link_layer;
Routing *routing;
vector <string> traceroute_host;
void help_cmd(const char *line) {
(void) line;
printf("- help, h: Print this list of commands\n"
"- interfaces, li: Print information about each interface, one per line\n"
"- routes, lr: Print information about the route to each known destination, one per line\n"
"- up [integer]: Bring an interface \"up\" (it must be an existing interface, probably one you brought down)\n"
"- down [integer]: Bring an interface \"down\"\n"
"- send [ip] [protocol] [payload]: sends payload with protocol=protocol to virtual-ip ip\n"
"- traceroute [ip]: show traceroute\n"
"- q: quit this node\n");
}
void interfaces_cmd(const char *line){
(void) line;
print_interfaces(routing->get_interfaces());
}
void routes_cmd(const char *line){
(void) line;
print_routes(routing->get_routes());
}
void down_cmd(const char *line){
unsigned interface;
if (sscanf(line, "down %u", &interface) != 1) {
dbg(DBG_ERROR, "syntax error (usage: down [interface])\n");
return;
}
routing->down_interface(interface);
}
void up_cmd(const char *line){
unsigned interface;
if (sscanf(line, "up %u", &interface) != 1) {
dbg(DBG_ERROR, "syntax error (usage: up [interface])\n");
return;
}
routing->up_interface(interface);
}
void send_cmd(const char *line){
char ip_string[INET_ADDRSTRLEN];
struct in_addr ip_addr;
uint8_t protocol;
int num_consumed;
char *data;
if (sscanf(line, "send %s %" SCNu8 "%n", ip_string, &protocol, &num_consumed) != 2) {
dbg(DBG_ERROR, "syntax error (usage: send [ip] [protocol] [payload])\n");
return;
}
if (inet_pton(AF_INET, ip_string, &ip_addr) == 0) {
dbg(DBG_ERROR, "syntax error (malformed ip address)\n");
return;
}
data = ((char *)line) + num_consumed + 1;
if (strlen(data) < 1) {
dbg(DBG_ERROR, "syntax error (payload unspecified)\n");
return;
}
link_layer->send_user_data(ip_string, data, routing, IPPROTO_DATA);
}
void traceroute_cmd(const char *line){
char ip_string[INET_ADDRSTRLEN];
struct in_addr ip_addr;
if (sscanf(line, "traceroute %s", ip_string) != 1) {
dbg(DBG_ERROR, "syntax error (usage: traceroute [ip])\n");
return;
}
if (inet_pton(AF_INET, ip_string, &ip_addr) == 0) {
dbg(DBG_ERROR, "syntax error (malformed ip address)\n");
return;
}
string vaddr = routing->get_adj_mapping()[routing->get_routing_table()[routing->get_nodes_info()[ip_string].port].best_route_port];
if(routing->get_routing_table().count(routing->get_nodes_info()[ip_string].port) == 0) {
dbg(DBG_ERROR, "this ip is not reachable.\n");
return;
}
string tracemsg = "Traceroute from " + vaddr + " to " + ip_string;
traceroute_host.push_back(tracemsg);
traceroute_host.push_back(vaddr);
link_layer->send_user_data(ip_string, "traceroute", routing, IPPROTO_TRACEROUTE);
}
struct {
const char *command;
void (*handler)(const char *);
} cmd_table[] = {
{"help", help_cmd},
{"h", help_cmd},
{"interfaces", interfaces_cmd},
{"li", interfaces_cmd},
{"routes", routes_cmd},
{"lr", routes_cmd},
{"down", down_cmd},
{"up", up_cmd},
{"send", send_cmd},
{"traceroute", traceroute_cmd}
};
void quit_msg_handler(std::string data, iphdr header){
routing->delete_node(header.saddr);
}
void recv_data_handler(std::string data, iphdr header) {
string lhip = header.lhIP;
if(!routing->does_interface_up(lhip))
return ;
if(header.daddr == (uint32_t) link_layer->get_self_port()){
std::cout << "------Node received packet!------\n" << endl;
std::cout << "\t arrived link: \t\t" << link_layer->get_arrived_interface(header.lhaddr, routing) << endl;
std::cout << "\t source IP: \t\t" << header.sourceIP << endl;
std::cout << "\t destination IP: \t" << header.desIP << endl;
std::cout << "\t protocol: \t\t" << (int)header.protocol <<endl;
std::cout << "\t payload length: \t" << data.length() << endl;
std::cout << "\t payload: \t\t" << data <<endl;
std::cout << "----------------------------------" << endl;
return;
}
link_layer->forwarding(data, header, routing, IPPROTO_DATA);
}
void recv_routing_table_handler(std::string data, iphdr header) {
string lhip = header.lhIP;
if(!routing->does_interface_up(lhip))
return ;
std::map<int, routing_table_info> routing_table =
Link::deserialize_routing_table(data);
routing->update_distance_table((int) header.saddr, routing_table);
}
void recv_nodes_info_handler(std::string data, iphdr header) {
string lhip = header.lhIP;
if(!routing->does_interface_up(lhip))
return ;
std::map<std::string, node_physical_info> nodes_info =
Link::deserialize_nodes_info(data);
routing->update_nodes_info(nodes_info);
}
void recv_traceroute_msg(std::string data, iphdr header){
if(header.daddr == (uint32_t) link_layer->get_self_port()){
traceroute_host.push_back(header.sourceIP);
if(data == "traceroute finished"){
cout << traceroute_host[0] << endl;
for(uint i = 1; i < traceroute_host.size(); i++){
cout << i << " " << traceroute_host[i] << endl;
}
cout << "Traceroute finished in " << traceroute_host.size() - 1 << " hobs" << endl;
traceroute_host.clear();
}
return;
}
link_layer->forwarding(data, header, routing, IPPROTO_TRACEROUTE_RESPONSE);
}
void send_traceroute_msg(std::string data, iphdr header){
string lhip = header.lhIP;
if(!routing->does_interface_up(lhip))
return;
if((header.daddr == (uint32_t) link_layer->get_self_port())){
link_layer->send_user_data(header.sourceIP,"traceroute finished",routing, IPPROTO_TRACEROUTE_RESPONSE);
}else{
link_layer->send_user_data(header.sourceIP,"traceroute",routing, IPPROTO_TRACEROUTE_RESPONSE);
link_layer->forwarding(data, header, routing, IPPROTO_TRACEROUTE);
}
}
struct protocol_handler get_handler(void (*f)(std::string,iphdr) , int protocol){
struct protocol_handler temp;
temp.protocol_num = protocol;
temp.handler = f;
return temp;
}
int main(int argc, char **argv){
if (argc != 2) {
dbg(DBG_ERROR, "usage: %s <linkfile>\n", argv[0]);
return -1;
}
#ifdef READLINE
char* line;
rl_bind_key('\t', rl_complete);
#else
char line[LINE_MAX];
#endif
char cmd[LINE_MAX];
unsigned i;
int ret;
lnxinfo_t *links_info = parse_links(argv[1]);
link_layer = new Link(links_info->local_phys_port);
routing = new Routing(links_info);
link_layer->register_handler(get_handler(&recv_data_handler , IPPROTO_DATA));
link_layer->register_handler(get_handler(&recv_routing_table_handler, IPPROTO_ROUTING_TABLE));
link_layer->register_handler(get_handler(&recv_nodes_info_handler, IPPROTO_NODES_INFO));
link_layer->register_handler(get_handler(&quit_msg_handler, IPPROTO_QUIT_MSG));
link_layer->register_handler(get_handler(&send_traceroute_msg, IPPROTO_TRACEROUTE));
link_layer->register_handler(get_handler(&recv_traceroute_msg, IPPROTO_TRACEROUTE_RESPONSE));
std::thread sending_routing_table_thread(&Routing::send_routing_to_adj, routing, *link_layer);
std::thread recv_routing_table_thread(&Link::recv_data, link_layer);
std::thread delete_expired_nodes_thread(&Routing::delete_expired_nodes, routing);
while (1) {
#ifdef READLINE
if (!(line = readline("> "))) break;
#else
dbg(DBG_ERROR, "> "); (void)fflush(stdout);
if (!fgets(line, sizeof(line), stdin)) break;
if (strlen(line) > 0 && line[strlen(line)-1] == '\n')
line[strlen(line)-1] = 0;
#endif
ret = sscanf(line, "%s", cmd);
if (ret != 1) {
if (ret != EOF) help_cmd(line);
continue;
}
if (!strcmp(cmd, "q")){
routing->send_quit_to_adj(*link_layer);
break;
}
for (i=0; i < sizeof(cmd_table) / sizeof(cmd_table[0]); i++){
if (!strcmp(cmd, cmd_table[i].command)){
cmd_table[i].handler(line);
break;
}
}
if (i == sizeof(cmd_table) / sizeof(cmd_table[0])){
dbg(DBG_ERROR, "error: no valid command specified\n");
help_cmd(line);
continue;
}
#ifdef READLINE
add_history(line);
free(line);
#endif
}
sending_routing_table_thread.detach();
recv_routing_table_thread.detach();
delete_expired_nodes_thread.detach();
printf("\nGoodbye!\n\n");
return 0;
}