This repository has been archived by the owner on Jan 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathn2n.c
536 lines (437 loc) · 14.6 KB
/
n2n.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/*
* (C) 2007-09 - Luca Deri <deri@ntop.org>
* Richard Andrews <andrews@ntop.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>
*
* Code contributions courtesy of:
* Massimo Torquati <torquati@ntop.org>
* Matt Gilg
*
*/
#include "n2n.h"
#include "minilzo.h"
#include <assert.h>
/* sglib hash table implementation */
SGLIB_DEFINE_LIST_FUNCTIONS(peer_info_t, PEER_INFO_COMPARATOR, next)
SGLIB_DEFINE_HASHED_CONTAINER_FUNCTIONS(peer_info_t, PEER_HASH_TAB_SIZE, peer_info_t_hash_function)
unsigned int peer_info_t_hash_function(peer_info_t *e) {
#if N2N_MAC_SIZE != 6
#error not implemented yet!
#else
short i = 0;
uint32_t tmp = 0;
/* Q: why is the hashing implemented like this?
* A: because the MAC addresses basically consist of two parts
* the first three bytes are a organisation unique identifier
* the second three bytes are a interface unique identifier
* therefore if all the (randomly) generated MAC addresses of the
* tun/tap interfaces all have the same organisation unique identifier
* the hashing performance is best if only the interface unique identifier
* is taken into account
*/
for(; i < N2N_MAC_SIZE / 2; i++) {
tmp |= (e->mac_addr[i] ^ e->mac_addr[(N2N_MAC_SIZE/2)+i])
<< (N2N_MAC_SIZE/2-1-i);
}
return tmp;
#endif
};
const uint8_t broadcast_addr[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
const uint8_t multicast_addr[6] = { 0x01, 0x00, 0x5E, 0x00, 0x00, 0x00 }; /* First 3 bytes are meaningful */
const uint8_t ipv6_multicast_addr[6] = { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 }; /* First 2 bytes are meaningful */
/* ************************************** */
SOCKET open_socket(int local_port, int bind_any) {
SOCKET sock_fd;
struct sockaddr_in local_address;
int sockopt = 1;
if((sock_fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
traceEvent(TRACE_ERROR, "Unable to create socket [%s][%d]\n",
strerror(errno), sock_fd);
return(-1);
}
#ifndef WIN32
/* fcntl(sock_fd, F_SETFL, O_NONBLOCK); */
#endif
setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&sockopt, sizeof(sockopt));
memset(&local_address, 0, sizeof(local_address));
local_address.sin_family = AF_INET;
local_address.sin_port = htons(local_port);
local_address.sin_addr.s_addr = htonl(bind_any?INADDR_ANY:INADDR_LOOPBACK);
if(bind(sock_fd, (struct sockaddr*) &local_address, sizeof(local_address)) == -1) {
traceEvent(TRACE_ERROR, "Bind error [%s]\n", strerror(errno));
return(-1);
}
return(sock_fd);
}
int traceLevel = 2 /* NORMAL */;
int useSyslog = 0, syslog_opened = 0;
#ifdef __ANDROID_NDK__
slog_t* slog = NULL;
int android_log_level(int lvl)
{
switch (lvl) {
case 0: // ERROR
return ANDROID_LOG_ERROR;
case 1: // WARNING
return ANDROID_LOG_WARN;
case 2: // NORMAL
return ANDROID_LOG_INFO;
case 3: // INFO
return ANDROID_LOG_DEBUG;
case 4: // DEBUG
return ANDROID_LOG_VERBOSE;
default: // NORMAL
return ANDROID_LOG_INFO;
}
}
#endif /* #ifdef __ANDROID_NDK__ */
#define N2N_TRACE_DATESIZE 32
void traceEvent(int eventTraceLevel, char* file, int line, const char * format, ...) {
va_list va_ap;
if(eventTraceLevel <= traceLevel) {
char buf[2048];
char out_buf[640];
char theDate[N2N_TRACE_DATESIZE];
char *extra_msg = "";
time_t theTime = time(NULL);
#ifdef WIN32
int i;
#endif
/* We have two paths - one if we're logging, one if we aren't
* Note that the no-log case is those systems which don't support it (WIN32),
* those without the headers !defined(USE_SYSLOG)
* those where it's parametrically off...
*/
memset(buf, 0, sizeof(buf));
strftime(theDate, N2N_TRACE_DATESIZE, "%d/%b/%Y %H:%M:%S", localtime(&theTime));
va_start (va_ap, format);
vsnprintf(buf, sizeof(buf)-1, format, va_ap);
va_end(va_ap);
if(eventTraceLevel == 0 /* TRACE_ERROR */)
extra_msg = "ERROR: ";
else if(eventTraceLevel == 1 /* TRACE_WARNING */)
extra_msg = "WARNING: ";
while(buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = '\0';
#ifndef WIN32
if(useSyslog) {
if(!syslog_opened) {
openlog("n2n", LOG_PID, LOG_DAEMON);
syslog_opened = 1;
}
snprintf(out_buf, sizeof(out_buf), "%s%s", extra_msg, buf);
syslog(LOG_INFO, "%s", out_buf);
} else {
#ifdef __ANDROID_NDK__
char * p = strrchr(file, '/');
file = (p ? p + 1 : file);
#endif /* #ifdef __ANDROID_NDK__ */
snprintf(out_buf, sizeof(out_buf), "%s [%11s:%4d] %s%s", theDate, file, line, extra_msg, buf);
#ifdef __ANDROID_NDK__
slog = writeslog(slog, android_log_level(eventTraceLevel), "n2n_v2s", out_buf);
#else /* #ifdef __ANDROID_NDK__ */
printf("%s\n", out_buf);
fflush(stdout);
#endif /* #ifdef __ANDROID_NDK__ */
}
#else
/* this is the WIN32 code */
OutputDebugStringA(buf);
for(i=strlen(file)-1; i>0; i--) if(file[i] == '\\') { i++; break; };
snprintf(out_buf, sizeof(out_buf), "%s [%11s:%4d] %s%s", theDate, &file[i], line, extra_msg, buf);
printf("%s\n", out_buf);
fflush(stdout);
#endif
}
}
/* *********************************************** */
/* addr should be in network order. Things are so much simpler that way. */
char* intoa(uint32_t /* host order */ addr, char* buf, uint16_t buf_len) {
char *cp, *retStr;
uint8_t byteval;
int n;
cp = &buf[buf_len];
*--cp = '\0';
n = 4;
do {
byteval = addr & 0xff;
*--cp = byteval % 10 + '0';
byteval /= 10;
if (byteval > 0) {
*--cp = byteval % 10 + '0';
byteval /= 10;
if (byteval > 0)
*--cp = byteval + '0';
}
*--cp = '.';
addr >>= 8;
} while (--n > 0);
/* Convert the string to lowercase */
retStr = (char*)(cp+1);
return(retStr);
}
/* *********************************************** */
char * macaddr_str( macstr_t buf,
const n2n_mac_t mac )
{
snprintf(buf, N2N_MACSTR_SIZE, "%02X:%02X:%02X:%02X:%02X:%02X",
mac[0] & 0xFF, mac[1] & 0xFF, mac[2] & 0xFF,
mac[3] & 0xFF, mac[4] & 0xFF, mac[5] & 0xFF);
return(buf);
}
/* *********************************************** */
uint8_t is_multi_broadcast(const uint8_t * dest_mac) {
int is_broadcast = ( memcmp(broadcast_addr, dest_mac, 6) == 0 );
int is_multicast = ( memcmp(multicast_addr, dest_mac, 3) == 0 );
int is_ipv6_multicast = ( memcmp(ipv6_multicast_addr, dest_mac, 2) == 0 );
return is_broadcast || is_multicast || is_ipv6_multicast;
}
/* http://www.faqs.org/rfcs/rfc908.html */
/* *********************************************** */
char* msg_type2str(uint16_t msg_type) {
switch(msg_type) {
case MSG_TYPE_REGISTER: return("MSG_TYPE_REGISTER");
case MSG_TYPE_DEREGISTER: return("MSG_TYPE_DEREGISTER");
case MSG_TYPE_PACKET: return("MSG_TYPE_PACKET");
case MSG_TYPE_REGISTER_ACK: return("MSG_TYPE_REGISTER_ACK");
case MSG_TYPE_REGISTER_SUPER: return("MSG_TYPE_REGISTER_SUPER");
case MSG_TYPE_REGISTER_SUPER_ACK: return("MSG_TYPE_REGISTER_SUPER_ACK");
case MSG_TYPE_REGISTER_SUPER_NAK: return("MSG_TYPE_REGISTER_SUPER_NAK");
case MSG_TYPE_FEDERATION: return("MSG_TYPE_FEDERATION");
default: return("???");
}
return("???");
}
/* *********************************************** */
void hexdump(const uint8_t * buf, size_t len)
{
size_t i;
if ( 0 == len ) { return; }
for(i=0; i<len; i++)
{
if((i > 0) && ((i % 16) == 0)) { printf("\n"); }
printf("%02X ", buf[i] & 0xFF);
}
printf("\n");
}
/* *********************************************** */
void print_n2n_version(int trace) {
const char* bufline = "Welcome to n2n v.%s for %s\n"
"Built on %s\n"
"Copyright 2007-09 - http://www.ntop.org\n"
"Modify version %s\n"
"Modify by %s\n"
"Modify Copyright 2018 - %d - https://github.com/switch-iot/hin2n\n\n";
time_t t = time(NULL);
if (trace == 1) {
traceEvent(TRACE_NORMAL, bufline, n2n_sw_version, n2n_sw_osName, n2n_sw_buildDate, n2n_mod_version, n2n_mod_author, localtime(&t)->tm_year + 1900);
} else {
printf(bufline, bufline, n2n_sw_version, n2n_sw_osName, n2n_sw_buildDate, n2n_mod_version, n2n_mod_author, localtime(&t)->tm_year + 1900);
}
}
const char* random_device_mac(void)
{
const char key[] = "0123456789abcdef";
static char mac[18];
int i;
srand(gettid());
for (i = 0; i < sizeof(mac) - 1; ++i)
{
if ((i + 1) % 3 == 0) {
mac[i] = ':';
continue;
}
mac[i] = key[random() % sizeof(key)];
}
mac[sizeof(mac) - 1] = '\0';
return mac;
}
/** Find the peer entry in list with mac_addr equal to mac.
*
* Does not modify the list.
*
* @return NULL if not found; otherwise pointer to peer entry.
*/
peer_info_t * find_peer_by_mac( peer_info_t ** list, const n2n_mac_t mac )
{
peer_info_t tmp;
memcpy(tmp.mac_addr, mac, sizeof(n2n_mac_t));
return sglib_hashed_peer_info_t_find_member(list, &tmp);
}
/** Return the number of elements in the list.
*
*/
size_t peer_list_size( const struct peer_info * list )
{
size_t retval=0;
while ( list )
{
++retval;
list = list->next;
}
return retval;
}
size_t hashed_peer_list_t_size(peer_info_t** htab) {
peer_info_t *ll;
struct sglib_hashed_peer_info_t_iterator it;
size_t retval = 0;
for(ll=sglib_hashed_peer_info_t_it_init(&it,htab); ll!=NULL; ll=sglib_hashed_peer_info_t_it_next(&it)) {
++retval;
}
return retval;
}
/** Add new to the head of list. If list is NULL; create it.
*
* The item new is added to the head of the list. New is modified during
* insertion. list takes ownership of new.
*/
void peer_list_add( struct peer_info * * list,
struct peer_info * new )
{
new->next = *list;
new->last_seen = time(NULL);
*list = new;
}
size_t purge_with_function(struct peer_info ** peer_list, size_t(*purger)(struct peer_info ** peer_list, time_t purge_before)) {
time_t now = time(NULL);
size_t num_reg = 0;
traceEvent(TRACE_INFO, "Purging old registrations");
num_reg = purger( peer_list, now-REGISTRATION_TIMEOUT );
traceEvent(TRACE_INFO, "Remove %ld registrations", num_reg);
return num_reg;
}
void dealloc_peer( peer_info_t* peer )
{
free(peer->sockets);
free(peer);
}
/** Purge old items from the peer_list and return the number of items that were removed. */
size_t purge_peer_list( struct peer_info ** peer_list,
time_t purge_before )
{
struct peer_info *scan;
struct peer_info *prev;
size_t retval=0;
scan = *peer_list;
prev = NULL;
while(scan != NULL) {
if(scan->last_seen < purge_before) {
struct peer_info *next = scan->next;
if(prev == NULL) {
*peer_list = next;
} else {
prev->next = next;
}
++retval;
dealloc_peer(scan);
scan = next;
} else {
prev = scan;
scan = scan->next;
}
}
return retval;
}
size_t purge_hashed_peer_list_t(peer_info_t ** peer_list, time_t purge_before) {
peer_info_t *ll;
struct sglib_hashed_peer_info_t_iterator it;
size_t retval = 0;
for(ll=sglib_hashed_peer_info_t_it_init(&it,peer_list); ll!=NULL;
ll=sglib_hashed_peer_info_t_it_next(&it)) {
if(ll->last_seen < purge_before) {
++retval;
sglib_hashed_peer_info_t_delete(peer_list, ll);
dealloc_peer(ll);
}
}
return retval;
}
size_t purge_expired_registrations( struct peer_info ** peer_list ) {
return purge_with_function(peer_list, purge_peer_list);
}
size_t hashed_purge_expired_registrations(peer_info_t ** peer_list) {
return purge_with_function(peer_list, purge_hashed_peer_list_t);
}
size_t clear_hashed_peer_info_t_list(peer_info_t ** peer_list) {
peer_info_t *ll;
struct sglib_hashed_peer_info_t_iterator it;
size_t retval = 0;
for(ll=sglib_hashed_peer_info_t_it_init(&it,peer_list); ll!=NULL; ll=sglib_hashed_peer_info_t_it_next(&it)) {
++retval;
sglib_hashed_peer_info_t_delete(peer_list, ll);
dealloc_peer(ll);
}
return retval;
}
static uint8_t hex2byte( const char * s )
{
char tmp[3];
tmp[0]=s[0];
tmp[1]=s[1];
tmp[2]=0; /* NULL term */
return((uint8_t)strtol( tmp, NULL, 16 ));
}
extern int str2mac( uint8_t * outmac /* 6 bytes */, const char * s )
{
size_t i;
/* break it down as one case for the first "HH", the 5 x through loop for
* each ":HH" where HH is a two hex nibbles in ASCII. */
*outmac=hex2byte(s);
++outmac;
s+=2; /* don't skip colon yet - helps generalise loop. */
for (i=1; i<6; ++i )
{
s+=1;
*outmac=hex2byte(s);
++outmac;
s+=2;
}
return 0; /* ok */
}
extern char * sock_to_cstr( n2n_sock_str_t out,
const n2n_sock_t * sock )
{
if ( NULL == out ) { return NULL; }
memset(out, 0, N2N_SOCKBUF_SIZE);
if ( AF_INET6 == sock->family )
{
/* INET6 not written yet */
snprintf( out, N2N_SOCKBUF_SIZE, "XXXX:%hu", sock->port );
return out;
}
else
{
const uint8_t * a = sock->addr.v4;
snprintf( out, N2N_SOCKBUF_SIZE, "%d.%d.%d.%d:%d", (a[0] & 0xff),
(a[1] & 0xff), (a[2] & 0xff), (a[3] & 0xff), sock->port );
return out;
}
}
/* @return zero if the two sockets are equivalent. */
int sock_equal( const n2n_sock_t * a,
const n2n_sock_t * b )
{
if ( a->port != b->port ) { return 1; }
if ( a->family != b->family ) { return 1; }
switch (a->family) /* they are the same */
{
case AF_INET:
if ( 0 != memcmp( a->addr.v4, b->addr.v4, IPV4_SIZE ) ) { return 1;};
break;
default:
if ( 0 != memcmp( a->addr.v6, b->addr.v6, IPV6_SIZE ) ) { return 1;};
break;
}
return 0;
}