-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathudp_server.c
391 lines (246 loc) · 9.57 KB
/
udp_server.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
/*
* udp_server.c
*
* Copyright (C) 2006-2012 B.A.T.M.A.N. contributors:
*
* Marek Lindner
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*
*/
#include <string.h>
#include <stdio.h>
#include "vis.h"
void handle_node( unsigned int sender_ip, unsigned char *buff, int buff_len, unsigned char gw_class, uint16_t tq_max, uint8_t version, uint16_t sizeofdata ) {
struct node *orig_node;
struct secif *secif;
struct secif_lst *secif_lst;
struct hashtable_t *swaphash;
struct neighbour *neigh;
struct hna *hna;
struct list_head *list_pos;
// struct vis_data *vis_data;
int packet_count, i;
/*char from_str[16];
char to_str[16];
addr_to_string( sender_ip, from_str, sizeof(from_str) );
// addr_to_string( neigh_ip, to_str, sizeof(to_str) );
printf( "UDP data from %s \n", from_str );*/
if ( node_hash->elements * 4 > node_hash->size ) {
swaphash = hash_resize( node_hash, node_hash->size * 2 );
if ( swaphash == NULL )
exit_error( "Couldn't resize node hash table \n" );
node_hash = swaphash;
}
/* the node which send the packet */
orig_node = (struct node *)hash_find( node_hash, &sender_ip );
/* node not found */
if ( orig_node == NULL ) {
orig_node = (struct node *)debugMalloc( sizeof(struct node), 1100 );
memset( orig_node, 0, sizeof(struct node) );
orig_node->addr = sender_ip;
INIT_LIST_HEAD_FIRST( orig_node->neigh_list );
INIT_LIST_HEAD_FIRST( orig_node->secif_list );
INIT_LIST_HEAD_FIRST( orig_node->hna_list );
hash_add( node_hash, orig_node );
}
orig_node->last_seen = 20;
orig_node->gw_class = gw_class;
orig_node->tq_max = tq_max;
packet_count = buff_len / sizeofdata;
for( i = 0; i < packet_count; i++ ) {
uint32_t vis_data_ip = 0;
uint8_t vis_data_type = 0;
uint16_t vis_data_data = 0;
switch ( version ) {
case VIS_COMPAT_VERSION21:
case VIS_COMPAT_VERSION23:
vis_data_ip = ((struct vis_data21 *)(buff + i * sizeofdata))->ip;
vis_data_type = ((struct vis_data21 *)(buff + i * sizeofdata))->type;
vis_data_data = ((struct vis_data21 *)(buff + i * sizeofdata))->data;
break;
case VIS_COMPAT_VERSION22:
vis_data_ip = ((struct vis_data22 *)(buff + i * sizeofdata))->ip;
vis_data_type = ((struct vis_data22 *)(buff + i * sizeofdata))->type;
vis_data_data = ntohs( ((struct vis_data22 *)(buff + i * sizeofdata))->data );
break;
}
if ( vis_data_ip != 0 ) {
/* is neighbour */
if ( vis_data_type == DATA_TYPE_NEIGH ) {
if ( vis_data_data > orig_node->tq_max )
continue;
neigh = NULL;
/* find neighbor in neighbour list of originator */
list_for_each( list_pos, &orig_node->neigh_list ) {
neigh = list_entry( list_pos, struct neighbour, list );
if ( vis_data_ip == neigh->addr )
break;
else
neigh = NULL;
}
/* if neighbour does not exist create it */
if ( neigh == NULL ) {
neigh = debugMalloc( sizeof(struct neighbour), 1101 );
memset( neigh, 0, sizeof(struct neighbour) );
neigh->addr = vis_data_ip;
INIT_LIST_HEAD( &neigh->list );
list_add_tail( &neigh->list, &orig_node->neigh_list );
}
/* save new tq value */
neigh->tq_avg = vis_data_data;
neigh->last_seen = 20;
/* is secondary interface */
} else if ( vis_data_type == DATA_TYPE_SEC_IF ) {
if ( secif_hash->elements * 4 > secif_hash->size ) {
swaphash = hash_resize( secif_hash, secif_hash->size * 2 );
if ( swaphash == NULL )
exit_error( "Couldn't resize secif hash table \n" );
secif_hash = swaphash;
}
/* use hash for fast processing of secondary interfaces in write_data_in_buffer() */
secif = (struct secif *)hash_find( secif_hash, &vis_data_ip );
if ( secif == NULL ) {
secif = (struct secif *)debugMalloc( sizeof(struct secif), 1102 );
secif->addr = vis_data_ip;
secif->orig = orig_node;
hash_add( secif_hash, secif );
}
/* maintain list of own secondary interfaces which must be removed from the hash if the originator is purged */
secif_lst = NULL;
/* find secondary interface in secondary if list of originator */
list_for_each( list_pos, &orig_node->secif_list ) {
secif_lst = list_entry( list_pos, struct secif_lst, list );
if ( vis_data_ip == secif_lst->addr )
break;
else
secif_lst = NULL;
}
/* if secondary interface does not exist create it */
if ( secif_lst == NULL ) {
secif_lst = debugMalloc( sizeof(struct secif_lst), 1103 );
memset( secif_lst, 0, sizeof(struct secif_lst) );
secif_lst->addr = vis_data_ip;
INIT_LIST_HEAD( &secif_lst->list );
list_add_tail( &secif_lst->list, &orig_node->secif_list );
}
secif_lst->last_seen = 20;
} else if ( vis_data_type == DATA_TYPE_HNA ) {
if ( vis_data_data > 32 )
continue;
hna = NULL;
/* find hna in hna list of originator */
list_for_each( list_pos, &orig_node->hna_list ) {
hna = list_entry( list_pos, struct hna, list );
if ( ( vis_data_ip == hna->addr ) && ( vis_data_data == hna->netmask ) )
break;
else
hna = NULL;
}
/* if hna does not exist create it */
if ( hna == NULL ) {
hna = debugMalloc( sizeof(struct hna), 1104 );
memset( hna, 0, sizeof(struct hna) );
hna->addr = vis_data_ip;
hna->netmask = vis_data_data;
INIT_LIST_HEAD( &hna->list );
list_add_tail( &hna->list, &orig_node->hna_list );
}
hna->last_seen = 20;
}
}
}
return;
}
void *udp_server() {
struct vis_if *vis_if;
struct list_head *list_pos;
struct sockaddr_in client;
struct timeval tv;
unsigned char receive_buff[MAXCHAR];
int max_sock = 0, buff_len;
fd_set wait_sockets, tmp_wait_sockets;
socklen_t len;
FD_ZERO(&wait_sockets);
list_for_each( list_pos, &vis_if_list ) {
vis_if = list_entry( list_pos, struct vis_if, list );
if ( vis_if->udp_sock > max_sock )
max_sock = vis_if->udp_sock;
FD_SET(vis_if->udp_sock, &wait_sockets);
}
while ( !is_aborted() ) {
len = sizeof(client);
memcpy( &tmp_wait_sockets, &wait_sockets, sizeof(fd_set) );
tv.tv_sec = 1;
tv.tv_usec = 0;
if ( select( max_sock + 1, &tmp_wait_sockets, NULL, NULL, &tv ) > 0 ) {
list_for_each( list_pos, &vis_if_list ) {
vis_if = list_entry( list_pos, struct vis_if, list );
if ( FD_ISSET( vis_if->udp_sock, &tmp_wait_sockets ) ) {
buff_len = recvfrom( vis_if->udp_sock, receive_buff, sizeof(receive_buff), 0, (struct sockaddr*)&client, &len );
uint8_t v=0;
/* drop packet if it has not minumum packet size or not the correct version */
if ( ( (buff_len > sizeof(struct vis_packet21)) &&
(buff_len - sizeof(struct vis_packet21)) % sizeof(struct vis_data21) == 0 &&
((v=((struct vis_packet21 *)receive_buff)->version) == VIS_COMPAT_VERSION21) ) ||
( (buff_len > sizeof(struct vis_packet22)) &&
(buff_len - sizeof(struct vis_packet22)) % sizeof(struct vis_data22) == 0 &&
((v=((struct vis_packet22 *)receive_buff)->version) == VIS_COMPAT_VERSION22) ) ||
( (buff_len > sizeof(struct vis_packet23)) &&
(buff_len - sizeof(struct vis_packet23)) % sizeof(struct vis_data23) == 0 &&
((v=((struct vis_packet23 *)receive_buff)->version) == VIS_COMPAT_VERSION23) ) ) {
if ( ((struct vis_packet21 *)receive_buff)->sender_ip != 0 ) {
if ( pthread_mutex_trylock( &hash_mutex ) == 0 ) {
switch( v ) {
case VIS_COMPAT_VERSION21:
case VIS_COMPAT_VERSION23:
handle_node( ((struct vis_packet21 *)receive_buff)->sender_ip,
receive_buff + sizeof(struct vis_packet21),
buff_len - sizeof(struct vis_packet21),
((struct vis_packet21 *)receive_buff)->gw_class,
((struct vis_packet21 *)receive_buff)->tq_max,
v, sizeof( struct vis_data21) );
break;
case VIS_COMPAT_VERSION22:
handle_node( ((struct vis_packet22 *)receive_buff)->sender_ip,
receive_buff + sizeof(struct vis_packet22),
buff_len - sizeof(struct vis_packet22),
((struct vis_packet22 *)receive_buff)->gw_class,
ntohs( ((struct vis_packet22 *)receive_buff)->tq_max ),
v, sizeof( struct vis_data22) );
break;
}
if ( pthread_mutex_unlock( &hash_mutex ) < 0 )
debug_output( "Error - could not unlock mutex (udp server): %s \n", strerror( errno ) );
} else {
debug_output( "Warning - dropping UDP packet: hash mutext is locked (%s)\n", strerror( EBUSY ) );
}
}
} else {
if ( (buff_len >= sizeof(struct vis_packet21)) ) {
char ip_str[ADDR_STR_LEN];
addr_to_string( ((struct vis_packet21 *)receive_buff)->sender_ip, ip_str, sizeof (ip_str) );
debug_output( "Warning - dropping invalid UDP packet: VIS_COMPAT_VERSION? %d from node? %s\n",
((struct vis_packet21 *)receive_buff)->version, ip_str );
} else {
debug_output( "Warning - dropping invalid UDP packet !\n" );
}
}
}
}
}
}
return NULL;
}