-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.c
More file actions
248 lines (216 loc) · 5.12 KB
/
Copy pathlib.c
File metadata and controls
248 lines (216 loc) · 5.12 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
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
#include "lib.h"
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <linux/if_packet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <net/if.h>
#include <unistd.h>
#include <asm/byteorder.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int interfaces[ROUTER_NUM_INTERFACES];
int get_sock(const char *if_name)
{
int res;
int s = socket(AF_PACKET, SOCK_RAW, 768);
DIE(s == -1, "socket");
struct ifreq intf;
strcpy(intf.ifr_name, if_name);
res = ioctl(s, SIOCGIFINDEX, &intf);
DIE(res, "ioctl SIOCGIFINDEX");
struct sockaddr_ll addr;
memset(&addr, 0x00, sizeof(addr));
addr.sll_family = AF_PACKET;
addr.sll_ifindex = intf.ifr_ifindex;
res = bind(s, (struct sockaddr *)&addr , sizeof(addr));
DIE(res == -1, "bind");
return s;
}
int send_to_link(size_t length, char *frame_data, size_t intidx)
{
/*
* Note that "buffer" should be at least the MTU size of the
* interface, eg 1500 bytes
*/
int ret;
ret = write(interfaces[intidx], frame_data, length);
DIE(ret == -1, "write");
return ret;
}
ssize_t receive_from_link(int intidx, char *frame_data)
{
ssize_t ret;
ret = read(interfaces[intidx], frame_data, MAX_PACKET_LEN);
return ret;
}
int socket_receive_message(int sockfd, char *frame_data, size_t *len)
{
/*
* Note that "buffer" should be at least the MTU size of the
* interface, eg 1500 bytes
* */
int ret = read(sockfd, frame_data, MAX_PACKET_LEN);
DIE(ret < 0, "read");
*len = ret;
return 0;
}
size_t recv_from_any_link(char *frame_data, size_t *length) {
int res;
fd_set set;
FD_ZERO(&set);
while (1) {
for (int i = 0; i < ROUTER_NUM_INTERFACES; i++) {
FD_SET(interfaces[i], &set);
}
res = select(interfaces[ROUTER_NUM_INTERFACES - 1] + 1, &set,
NULL, NULL, NULL);
DIE(res == -1, "select");
for (int i = 0; i < ROUTER_NUM_INTERFACES; i++) {
if (FD_ISSET(interfaces[i], &set)) {
ssize_t ret = receive_from_link(i, frame_data);
DIE(ret < 0, "receive_from_link");
*length = ret;
return i;
}
}
}
return -1;
}
char *get_interface_ip(int interface)
{
struct ifreq ifr;
int ret;
if (interface == 0)
sprintf(ifr.ifr_name, "rr-0-1");
else {
sprintf(ifr.ifr_name, "r-%u", interface - 1);
}
ret = ioctl(interfaces[interface], SIOCGIFADDR, &ifr);
DIE(ret == -1, "ioctl SIOCGIFADDR");
return inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
}
void get_interface_mac(size_t interface, uint8_t *mac)
{
struct ifreq ifr;
int ret;
if (interface == 0)
sprintf(ifr.ifr_name, "rr-0-1");
else {
sprintf(ifr.ifr_name, "r-%lu", interface - 1);
}
ret = ioctl(interfaces[interface], SIOCGIFHWADDR, &ifr);
DIE(ret == -1, "ioctl SIOCGIFHWADDR");
memcpy(mac, ifr.ifr_addr.sa_data, 6);
}
static int hex2num(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return -1;
}
int hex2byte(const char *hex)
{
int a, b;
a = hex2num(*hex++);
if (a < 0)
return -1;
b = hex2num(*hex++);
if (b < 0)
return -1;
return (a << 4) | b;
}
int hwaddr_aton(const char *txt, uint8_t *addr)
{
int i;
for (i = 0; i < 6; i++) {
int a, b;
a = hex2num(*txt++);
if (a < 0)
return -1;
b = hex2num(*txt++);
if (b < 0)
return -1;
*addr++ = (a << 4) | b;
if (i < 5 && *txt++ != ':')
return -1;
}
return 0;
}
void init(char *argv[], int argc)
{
for (int i = 0; i < argc; ++i) {
printf("Setting up interface: %s\n", argv[i]);
interfaces[i] = get_sock(argv[i]);
}
}
uint16_t checksum(uint16_t *data, size_t length)
{
unsigned long checksum = 0;
uint16_t extra_byte;
while (length > 1) {
checksum += ntohs(*data++);
length -= 2;
}
if (length) {
*(uint8_t *)&extra_byte = *(uint8_t *)data;
checksum += extra_byte;
}
checksum = (checksum >> 16) + (checksum & 0xffff);
checksum += (checksum >>16);
return (uint16_t)(~checksum);
}
int read_rtable(const char *path, struct route_table_entry *rtable)
{
FILE *fp = fopen(path, "r");
int j = 0, i;
char *p, line[64];
while (fgets(line, sizeof(line), fp) != NULL) {
p = strtok(line, " .");
i = 0;
while (p != NULL) {
if (i < 4)
*(((unsigned char *)&rtable[j].prefix) + i % 4) = (unsigned char)atoi(p);
if (i >= 4 && i < 8)
*(((unsigned char *)&rtable[j].next_hop) + i % 4) = atoi(p);
if (i >= 8 && i < 12)
*(((unsigned char *)&rtable[j].mask) + i % 4) = atoi(p);
if (i == 12)
rtable[j].interface = atoi(p);
p = strtok(NULL, " .");
i++;
}
j++;
}
return j;
}
int parse_arp_table(char *path, struct arp_table_entry *arp_table)
{
FILE *f;
fprintf(stderr, "Parsing ARP table\n");
f = fopen(path, "r");
DIE(f == NULL, "Failed to open %s", path);
char line[100];
int i = 0;
for(i = 0; fgets(line, sizeof(line), f); i++) {
char ip_str[50], mac_str[50];
sscanf(line, "%s %s", ip_str, mac_str);
fprintf(stderr, "IP: %s MAC: %s\n", ip_str, mac_str);
arp_table[i].ip = inet_addr(ip_str);
int rc = hwaddr_aton(mac_str, arp_table[i].mac);
DIE(rc < 0, "invalid MAC");
}
fclose(f);
fprintf(stderr, "Done parsing ARP table.\n");
return i;
}