-
Notifications
You must be signed in to change notification settings - Fork 3
/
device.c
417 lines (373 loc) · 12.6 KB
/
device.c
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <config.h>
#include <device.h>
#include <asm/eth.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include <interface.h>
extern conf_info_t *info;
char* get_type(enum device_type type)
{
switch(type){
case DEV_ROUTER:
return "router";
break;
case DEV_SWITCH:
return "switch";
break;
case DEV_HUB:
return "hub";
break;
default:
return "unknown";
break;
}
}
enum device_type get_device_type(char *type)
{
if (!strcmp("router", type)) {
return DEV_ROUTER;
}
if (!strcmp("switch", type)) {
return DEV_SWITCH;
}
if (!strcmp("hub", type)) {
return DEV_HUB;
}
if (!strcmp("bridge", type)) {
return DEV_BRIDGE;
}
return DEV_UNKNOWN;
}
int hypervisor_socket(){
struct hostent *host;
struct sockaddr_in hypervisor;
int err, sock;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
perror("could no open socket");
return sock;
}
host = gethostbyname("127.0.0.1");
hypervisor.sin_family = AF_INET;
hypervisor.sin_port = htons(57200);
hypervisor.sin_addr = *((struct in_addr*)host->h_addr);
err = connect(sock, (struct sockaddr*) &hypervisor, sizeof(struct sockaddr));
if (err < 0) {
perror("could not connect");
return err;
}
return sock;
}
socket_t* get_device_socket(device_t *device)
{
char *address;
socket_t *socket = malloc(sizeof(*socket));
memset(socket, 0, sizeof(*socket));
if (device->type == DEV_HUB) {
strcpy(socket->address,"127.0.0.1");
socket->port = device->port;
} else {
conf_info_t *dev_info = malloc(sizeof(*dev_info));
config_init(dev_info);
config_read_file(dev_info, device->config);
address = inet_ntoa(dev_info->general.address);
memcpy(socket->address, address, strlen(address));
socket->port = dev_info->general.port;
config_free(dev_info);
//free(address);
}
return socket;
}
socket_t* get_remote_device_socket(char *device)
{
socket_t *dev_socket;
int sock = hypervisor_socket();
hyper_info_t hinfo, *response;
hinfo.type = REQUEST_DEVICE_NAME;
hinfo.length = sizeof(hyper_info_header) + strlen(device) + 1;
memset(hinfo.padding, 0, PACKAGE_PADDING);
memcpy(hinfo.padding, device, strlen(device));
send_hyper(sock, &hinfo);
response = recv_hyper(sock);
if (response->type == REQUEST_ERROR_DEVICE_NOT_FOUND) {
close(sock);
return NULL;
}
dev_socket = (socket_t*) response->padding;
close(sock);
return dev_socket;
}
int send_hyper(int sock, hyper_info_t *hinfo)
{
int err;
err = send(sock, hinfo, sizeof(hyper_info_header), 0);
if (err < 0) {
perror("could not send header");
return -1;
}
err = send(sock, hinfo->padding, hinfo->length-2*sizeof(unsigned int), 0);
if (err < 0) {
perror("could not send data");
return -1;
}
return 0;
}
hyper_info_t* recv_hyper(int sock)
{
int err;
hyper_info_t *hinfo = malloc(sizeof(*hinfo));
err = recv(sock, hinfo, sizeof(hyper_info_header), 0);
if (err < 0) {
perror("could not receive header");
return NULL;
}
err = recv(sock, hinfo->padding, hinfo->length - 2*sizeof(unsigned int), 0);
if (err < 0) {
perror("could not receive data");
return NULL;
}
return hinfo;
}
#ifdef ISLKL
void start_device_thread()
{
int err;
pthread_t request;
err = pthread_create(&request, NULL, device_request_thread, NULL);
if (err < 0) {
perror("LKL NET :: could not start request thread\n");
exit(-1);
}
}
#define MAX_CONNECTIONS 1024
void* device_request_thread(void *params)
{
struct epoll_event event;
int req_socket, err, one = 1, epoll_fd;
socklen_t addrlen = sizeof(struct sockaddr_in);
struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_port = info->general.port,
};
printf("REQUEST THREAD PORT %d\n",info->general.port);
struct sockaddr_in raddr;
struct hostent *he = gethostbyname("127.0.0.1");
addr.sin_addr = *(struct in_addr*)he->h_addr;
printf("LKL NET :: started request thread\n");
req_socket = socket(AF_INET, SOCK_STREAM, 0);
if (req_socket < 0) {
perror("LKL NET :: request thread :: Could not open request socket\n");
exit(-1);
}
err = bind(req_socket, (struct sockaddr*) &addr, sizeof(addr));
if (err < 0) {
perror("LKL NET :: request thread :: Could not bind socket\n");
exit(-1);
}
err = setsockopt(req_socket, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int));
if (err < 0) {
perror("LKL NET :: request thread :: Could not set reuse address on socket\n");
exit(-1);
}
err = listen(req_socket, MAX_CONNECTIONS);
if (err < 0) {
perror("LKL NET :: request thread :: Could not listen on socket\n");
exit(-1);
}
//dump config file
getsockname(req_socket, (struct sockaddr*) &raddr, &addrlen);
if (!info->read){
struct params params;
info->general.address = raddr.sin_addr;
info->general.port = raddr.sin_port;
params.p[0] = info->config_file;
do_dump_config_file(¶ms);
}
epoll_fd = epoll_create(16);
if (epoll_fd < 0) {
perror("LKL NET :: request thread :: Could not intialise epoll\n");
exit(-1);
}
event.data.fd = req_socket;
event.events = EPOLLIN;
err = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, req_socket, &event);
if (err < 0) {
perror("LKL NET :: request thread :: Could not add socket to epoll\n");
exit(-1);
}
while (1) {
struct epoll_event ret_event;
err = epoll_wait(epoll_fd, &ret_event, 1, -1);
if (err < 0) {
perror("LKL NET :: request thread :: Error on wait\n");
}
//TODO: process requests
if (ret_event.data.fd == req_socket) {
struct epoll_event new_event;
int dev_sock = accept(req_socket, NULL, NULL);
if (dev_sock < 0) {
perror("could not accept connection");
continue;
}
new_event.data.fd = dev_sock;
new_event.events = EPOLLIN;
err = epoll_ctl(epoll_fd, EPOLL_CTL_ADD, dev_sock, &new_event);
} else {
int err, sock = ret_event.data.fd;
request_t *request = malloc(sizeof(*request));
err = recv(sock, request, sizeof(*request), 0);
if (err < 0) {
printf("could not receive");
}
switch (request->type) {
case REQ_ADD_IF:
{
interface_t *interface = malloc(sizeof(*interface));
int size;
err = recv(sock, interface, sizeof(*interface), 0);
if (err < 0) {
perror("could not recv if");
}
err = recv(sock, &size, sizeof(size), 0);
if (err < 0) {
perror("could not recv size1");
}
interface->dev = malloc(size);
err = recv(sock, interface->dev, size, 0);
if (err < 0) {
perror("could not recv dev");
}
err = recv(sock, &size, sizeof(size), 0);
if (err < 0) {
perror("could not recv size2");
}
if (size > 0) {
interface->link = malloc(size);
err = recv(sock, interface->link, size, 0);
if (err < 0) {
perror("could not recv link");
}
}
interface->mac = malloc(sizeof(*(interface->mac)));
err = recv(sock, interface->mac, sizeof(*(interface->mac)), 0);
if (err < 0) {
perror("could not recv mac");
}
lkl_init_interface_short(interface);
#ifdef ISSWITCH
int ifindex = get_dev_index(interface->dev);
//lkl_if_down(get_dev_index("brd0"));
if (lkl_br_add_interface("brd0", ifindex) < 0) {\
printf("LKL-net :: could not add interface %s to switch\n", interface->dev);
}
//lkl_if_up(get_dev_index("brd0"));
#endif
break;
}
case REQ_DEL_IF:
break;
case REQ_ADD_LINK:
{
char *eth;
int size;
socket_t *socket = malloc(sizeof(*socket));
err = recv(sock, socket, sizeof(*socket), 0);
if (err < 0) {
perror("could not recv socket");
}
err = recv(sock, &size, sizeof(size), 0);
if (err < 0) {
perror("could not recv size\n");
}
eth = malloc(size);
err = recv(sock, eth, size, 0);
if (err < 0) {
perror("could not recv err name");
}
struct tun_device *td = malloc(sizeof(*td));
td->type = TUN_HUB;
td->port = socket->port;
td->address = inet_addr(socket->address);
lkl_if_set_info(eth, td);
printf("changed info %s %s %d\n", eth, socket->address, socket->port);
break;
}
default:
break;
}
epoll_ctl(sock, EPOLL_CTL_DEL, sock, NULL);
}
}
shutdown(req_socket, SHUT_RDWR);
return NULL;
}
#endif
int do_send_pid(enum device_type type, unsigned short port)
{
int err, sock = 0;
struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_port = htons(57200),
};
struct hostent *he = gethostbyname("127.0.0.1");
addr.sin_addr = *(struct in_addr*)he->h_addr;
hyper_info_t hinfo;
sock = socket(AF_INET, SOCK_STREAM, 0);
err = connect(sock, (struct sockaddr*) &addr, sizeof(addr));
if (err < 0) {
perror("could not connect to hypervisor");
}
hinfo.length = 16;
hinfo.type = REQUEST_SET_PORT;
err = send(sock, &hinfo, hinfo.length, 0);
if (err < 0) {
perror("could not send data");
}
err = send(sock, &type, sizeof(type), 0);
if (err < 0) {
perror("could not send data");
}
if (port == 0) {
int size = strlen(info->general.hostname)+1;
err = send(sock, &size, sizeof(size), 0);
if (err < 0) {
perror("could not send data");
}
err = send(sock, info->general.hostname, size, 0);
if (err < 0) {
perror("could not send data");
}
} else {
err = send(sock, &port, sizeof(port), 0);
if (err < 0) {
perror("could not send data");
}
}
pid_t pid = getpid();
err = send(sock, &pid, sizeof(pid_t), 0);
if (err < 0) {
perror("could not send data");
}
return 0;
}
int do_dump_config_file(params *parameters)
{
int fd = open(parameters->p[0], O_CREAT | O_WRONLY | O_TRUNC, 0666);
if (fd < 0) {
perror("Could not open file");
return -1;
}
dump_config_file(fd, info);
return 0;
}