ml / ml.c @ 6575ae37
History | View | Annotate | Download (72.7 KB)
1 |
/*
|
---|---|
2 |
* Policy Management
|
3 |
*
|
4 |
* NEC Europe Ltd. PROPRIETARY INFORMATION
|
5 |
*
|
6 |
* This software is supplied under the terms of a license agreement
|
7 |
* or nondisclosure agreement with NEC Europe Ltd. and may not be
|
8 |
* copied or disclosed except in accordance with the terms of that
|
9 |
* agreement.
|
10 |
*
|
11 |
* Copyright (c) 2009 NEC Europe Ltd. All Rights Reserved.
|
12 |
*
|
13 |
* Authors: Kristian Beckers <beckers@nw.neclab.eu>
|
14 |
* Sebastian Kiesel <kiesel@nw.neclab.eu>
|
15 |
*
|
16 |
*
|
17 |
* NEC Europe Ltd. DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED,
|
18 |
* INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY
|
19 |
* AND FITNESS FOR A PARTICULAR PURPOSE AND THE WARRANTY AGAINST LATENT
|
20 |
* DEFECTS, WITH RESPECT TO THE PROGRAM AND THE ACCOMPANYING
|
21 |
* DOCUMENTATION.
|
22 |
*
|
23 |
* No Liability For Consequential Damages IN NO EVENT SHALL NEC Europe
|
24 |
* Ltd., NEC Corporation OR ANY OF ITS SUBSIDIARIES BE LIABLE FOR ANY
|
25 |
* DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS
|
26 |
* OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF INFORMATION, OR
|
27 |
* OTHER PECUNIARY LOSS AND INDIRECT, CONSEQUENTIAL, INCIDENTAL,
|
28 |
* ECONOMIC OR PUNITIVE DAMAGES) ARISING OUT OF THE USE OF OR INABILITY
|
29 |
* TO USE THIS PROGRAM, EVEN IF NEC Europe Ltd. HAS BEEN ADVISED OF THE
|
30 |
* POSSIBILITY OF SUCH DAMAGES.
|
31 |
*
|
32 |
* THIS HEADER MAY NOT BE EXTRACTED OR MODIFIED IN ANY WAY.
|
33 |
*/
|
34 |
|
35 |
#include <ml_all.h> |
36 |
|
37 |
/**************************** START OF INTERNALS ***********************/
|
38 |
|
39 |
|
40 |
/*
|
41 |
* reserved message type for internal puposes
|
42 |
*/
|
43 |
#define MSG_TYPE_ML_KEEPALIVE 0x126 //TODO: check that it is really interpreted as internal |
44 |
|
45 |
/*
|
46 |
* a pointer to a libevent instance
|
47 |
*/
|
48 |
struct event_base *base;
|
49 |
|
50 |
/*
|
51 |
* define the nr of connections the messaging layer can handle
|
52 |
*/
|
53 |
#define CONNECTBUFSIZE 10000 |
54 |
/*
|
55 |
* define the nr of data that can be received parallel
|
56 |
*/
|
57 |
#define RECVDATABUFSIZE 10000 |
58 |
/*
|
59 |
* define an array for message multiplexing
|
60 |
*/
|
61 |
#define MSGMULTIPLEXSIZE 127 |
62 |
|
63 |
|
64 |
/*
|
65 |
* timeout before thinking that the STUN server can't be connected
|
66 |
*/
|
67 |
#define NAT_TRAVERSAL_TIMEOUT { 1, 0 } |
68 |
|
69 |
/*
|
70 |
* timeout before thinking of an mtu problem (check MAX_TRIALS as well)
|
71 |
*/
|
72 |
#define PMTU_TIMEOUT 500000 // in usec |
73 |
|
74 |
/*
|
75 |
* retry sending connection messages this many times before reducing pmtu
|
76 |
*/
|
77 |
#define MAX_TRIALS 3 |
78 |
|
79 |
/*
|
80 |
* default timeout value between the first and the last received packet of a message
|
81 |
*/
|
82 |
#define RECV_TIMEOUT_DEFAULT { 2, 0 } |
83 |
|
84 |
#ifdef RTX
|
85 |
/*
|
86 |
* default timeout value for a packet reception
|
87 |
*/
|
88 |
#define PKT_RECV_TIMEOUT_DEFAULT { 0, 50000 } // 50 ms |
89 |
|
90 |
/*
|
91 |
* default timeout value for a packet reception
|
92 |
*/
|
93 |
#define LAST_PKT_RECV_TIMEOUT_DEFAULT { 1, 700000 } |
94 |
|
95 |
/*
|
96 |
* default fraction of RECV_TIMEOUT_DEFAULT for a last packet(s) reception timeout
|
97 |
*/
|
98 |
#define LAST_PKT_RECV_TIMEOUT_FRACTION 0.7 |
99 |
|
100 |
#endif
|
101 |
|
102 |
|
103 |
/*
|
104 |
* global variables
|
105 |
*/
|
106 |
|
107 |
/*
|
108 |
* define a buffer of pointers to connect structures
|
109 |
*/
|
110 |
connect_data *connectbuf[CONNECTBUFSIZE]; |
111 |
|
112 |
/*
|
113 |
* define a pointer buffer with pointers to recv_data structures
|
114 |
*/
|
115 |
recvdata *recvdatabuf[RECVDATABUFSIZE]; |
116 |
|
117 |
/*
|
118 |
* define a pointer buffer for message multiplexing
|
119 |
*/
|
120 |
receive_data_cb recvcbbuf[MSGMULTIPLEXSIZE]; |
121 |
|
122 |
/*
|
123 |
* stun server address
|
124 |
*/
|
125 |
struct sockaddr_in stun_server;
|
126 |
|
127 |
/*
|
128 |
* receive timeout
|
129 |
*/
|
130 |
static struct timeval recv_timeout = RECV_TIMEOUT_DEFAULT; |
131 |
|
132 |
/*
|
133 |
* boolean NAT traversal successful if true
|
134 |
*/
|
135 |
boolean NAT_traversal; |
136 |
|
137 |
/*
|
138 |
* file descriptor for local socket
|
139 |
*/
|
140 |
evutil_socket_t socketfd; |
141 |
|
142 |
/*
|
143 |
* local socketID
|
144 |
*/
|
145 |
socket_ID local_socketID; |
146 |
|
147 |
socketID_handle loc_socketID = &local_socketID; |
148 |
|
149 |
/*
|
150 |
* callback function pointers
|
151 |
*/
|
152 |
/*
|
153 |
* monitoring module callbacks
|
154 |
*/
|
155 |
get_recv_pkt_inf_cb get_Recv_pkt_inf_cb = NULL;
|
156 |
get_send_pkt_inf_cb get_Send_pkt_inf_cb = NULL;
|
157 |
set_monitoring_header_pkt_cb set_Monitoring_header_pkt_cb = NULL;
|
158 |
get_recv_data_inf_cb get_Recv_data_inf_cb = NULL;
|
159 |
get_send_data_inf_cb get_Send_data_inf_cb = NULL;
|
160 |
set_monitoring_header_data_cb set_Monitoring_header_data_cb = NULL;
|
161 |
/*
|
162 |
* connection callbacks
|
163 |
*/
|
164 |
receive_connection_cb receive_Connection_cb = NULL;
|
165 |
connection_failed_cb failed_Connection_cb = NULL;
|
166 |
/*
|
167 |
* local socketID callback
|
168 |
*/
|
169 |
receive_localsocketID_cb receive_SocketID_cb; |
170 |
|
171 |
/*
|
172 |
* boolean that defines if received data is transmitted to the upper layer
|
173 |
* via callback or via upper layer polling
|
174 |
*/
|
175 |
boolean recv_data_callback; |
176 |
|
177 |
/*
|
178 |
* helper function to get rid of a warning
|
179 |
*/
|
180 |
#ifndef WIN32
|
181 |
int min(int a, int b) { |
182 |
if (a > b) return b; |
183 |
return a;
|
184 |
} |
185 |
#endif
|
186 |
|
187 |
#ifdef RTX
|
188 |
//*********Counters**********
|
189 |
|
190 |
struct Counters {
|
191 |
unsigned int receivedCompleteMsgCounter; |
192 |
unsigned int receivedIncompleteMsgCounter; |
193 |
unsigned int receivedDataPktCounter; |
194 |
unsigned int receivedRTXDataPktCounter; |
195 |
unsigned int receivedNACK1PktCounter; |
196 |
unsigned int receivedNACKMorePktCounter; |
197 |
unsigned int sentDataPktCounter; |
198 |
unsigned int sentRTXDataPktCtr; |
199 |
unsigned int sentNACK1PktCounter; |
200 |
unsigned int sentNACKMorePktCounter; |
201 |
} counters; |
202 |
|
203 |
extern unsigned int sentRTXDataPktCounter; |
204 |
|
205 |
/*
|
206 |
* receive timeout for a packet
|
207 |
*/
|
208 |
static struct timeval pkt_recv_timeout = PKT_RECV_TIMEOUT_DEFAULT; |
209 |
|
210 |
|
211 |
static struct timeval last_pkt_recv_timeout = LAST_PKT_RECV_TIMEOUT_DEFAULT; |
212 |
|
213 |
void mlShowCounters() {
|
214 |
counters.sentRTXDataPktCtr = sentRTXDataPktCounter; |
215 |
fprintf(stderr, "\nreceivedCompleteMsgCounter: %d\nreceivedIncompleteMsgCounter: %d\nreceivedDataPktCounter: %d\nreceivedRTXDataPktCounter: %d\nreceivedNACK1PktCounter: %d\nreceivedNACKMorePktCounter: %d\nsentDataPktCounter: %d\nsentRTXDataPktCtr: %d\nsentNACK1PktCounter: %d\nsentNACKMorePktCounter: %d\n", counters.receivedCompleteMsgCounter, counters.receivedIncompleteMsgCounter, counters.receivedDataPktCounter, counters.receivedRTXDataPktCounter, counters.receivedNACK1PktCounter, counters.receivedNACKMorePktCounter, counters.sentDataPktCounter, counters.sentRTXDataPktCtr, counters.sentNACK1PktCounter, counters.sentNACKMorePktCounter);
|
216 |
return;
|
217 |
} |
218 |
|
219 |
void recv_nack_msg(struct msg_header *msg_h, char *msgbuf, int msg_size) |
220 |
{ |
221 |
struct nack_msg *nackmsg;
|
222 |
|
223 |
msgbuf += msg_h->len_mon_data_hdr; |
224 |
msg_size -= msg_h->len_mon_data_hdr; |
225 |
nackmsg = (struct nack_msg*) msgbuf;
|
226 |
|
227 |
unsigned int gapSize = nackmsg->offsetTo - nackmsg->offsetFrom; |
228 |
//if (gapSize == 1349) counters.receivedNACK1PktCounter++;
|
229 |
//else counters.receivedNACKMorePktCounter++;
|
230 |
|
231 |
rtxPacketsFromTo(nackmsg->con_id, nackmsg->msg_seq_num, nackmsg->offsetFrom, nackmsg->offsetTo); |
232 |
} |
233 |
|
234 |
void send_msg(int con_id, int msg_type, void* msg, int msg_len, bool truncable, send_params * sParams); |
235 |
|
236 |
void pkt_recv_timeout_cb(int fd, short event, void *arg){ |
237 |
int recv_id = (long) arg; |
238 |
debug("ML: recv_timeout_cb called. Timeout for id:%d\n",recv_id);
|
239 |
|
240 |
//check if message still exists
|
241 |
if (recvdatabuf[recv_id] == NULL) return; |
242 |
|
243 |
//check if gap was filled in the meantime
|
244 |
if (recvdatabuf[recv_id]->gapArray[recvdatabuf[recv_id]->firstGap].offsetFrom == recvdatabuf[recv_id]->gapArray[recvdatabuf[recv_id]->firstGap].offsetTo) {
|
245 |
recvdatabuf[recv_id]->firstGap++; |
246 |
return;
|
247 |
} |
248 |
|
249 |
struct nack_msg nackmsg;
|
250 |
nackmsg.con_id = recvdatabuf[recv_id]->txConnectionID; |
251 |
nackmsg.msg_seq_num = recvdatabuf[recv_id]->seqnr; |
252 |
nackmsg.offsetFrom = recvdatabuf[recv_id]->gapArray[recvdatabuf[recv_id]->firstGap].offsetFrom; |
253 |
nackmsg.offsetTo = recvdatabuf[recv_id]->gapArray[recvdatabuf[recv_id]->firstGap].offsetTo; |
254 |
recvdatabuf[recv_id]->firstGap++; |
255 |
|
256 |
unsigned int gapSize = nackmsg.offsetTo - nackmsg.offsetFrom; |
257 |
|
258 |
send_msg(recvdatabuf[recv_id]->connectionID, ML_NACK_MSG, (char *) &nackmsg, sizeof(struct nack_msg), true, &(connectbuf[recvdatabuf[recv_id]->connectionID]->defaultSendParams)); |
259 |
} |
260 |
|
261 |
void last_pkt_recv_timeout_cb(int fd, short event, void *arg){ |
262 |
int recv_id = (long) arg; |
263 |
debug("ML: recv_timeout_cb called. Timeout for id:%d\n",recv_id);
|
264 |
|
265 |
if (recvdatabuf[recv_id] == NULL) { |
266 |
return;
|
267 |
} |
268 |
|
269 |
if (recvdatabuf[recv_id]->expectedOffset == recvdatabuf[recv_id]->bufsize - recvdatabuf[recv_id]->monitoringDataHeaderLen) return; |
270 |
|
271 |
struct nack_msg nackmsg;
|
272 |
nackmsg.con_id = recvdatabuf[recv_id]->txConnectionID; |
273 |
nackmsg.msg_seq_num = recvdatabuf[recv_id]->seqnr; |
274 |
nackmsg.offsetFrom = recvdatabuf[recv_id]->expectedOffset; |
275 |
nackmsg.offsetTo = recvdatabuf[recv_id]->bufsize - recvdatabuf[recv_id]->monitoringDataHeaderLen; |
276 |
|
277 |
unsigned int gapSize = nackmsg.offsetTo - nackmsg.offsetFrom; |
278 |
|
279 |
send_msg(recvdatabuf[recv_id]->connectionID, ML_NACK_MSG, &nackmsg, sizeof(struct nack_msg), true, &(connectbuf[recvdatabuf[recv_id]->connectionID]->defaultSendParams)); |
280 |
} |
281 |
|
282 |
#endif
|
283 |
|
284 |
|
285 |
|
286 |
/*
|
287 |
* convert a socketID to a string. It uses a static buffer, so either strdup is needed, or the string will get lost!
|
288 |
*/
|
289 |
const char *conid_to_string(int con_id) |
290 |
{ |
291 |
static char s[INET_ADDRSTRLEN+1+5+1+INET_ADDRSTRLEN+1+5+1]; |
292 |
mlSocketIDToString(&connectbuf[con_id]->external_socketID, s, sizeof(s));
|
293 |
return s;
|
294 |
} |
295 |
|
296 |
void register_recv_localsocketID_cb(receive_localsocketID_cb local_socketID_cb)
|
297 |
{ |
298 |
if (local_socketID_cb == NULL) { |
299 |
error("ML : Register receive_localsocketID_cb: NULL ptr \n");
|
300 |
} else {
|
301 |
receive_SocketID_cb = local_socketID_cb; |
302 |
} |
303 |
} |
304 |
|
305 |
|
306 |
//void keep_connection_alive(const int connectionID)
|
307 |
//{
|
308 |
//
|
309 |
// // to be done with the NAT traversal
|
310 |
// // send a message over the wire
|
311 |
// printf("\n");
|
312 |
//
|
313 |
//}
|
314 |
|
315 |
void unsetStunServer()
|
316 |
{ |
317 |
stun_server.sin_addr.s_addr = INADDR_NONE; |
318 |
} |
319 |
|
320 |
bool isStunDefined()
|
321 |
{ |
322 |
return stun_server.sin_addr.s_addr != INADDR_NONE;
|
323 |
} |
324 |
|
325 |
void send_msg(int con_id, int msg_type, void* msg, int msg_len, bool truncable, send_params * sParams) { |
326 |
socketaddrgen udpgen; |
327 |
bool retry;
|
328 |
int pkt_len, offset;
|
329 |
struct iovec iov[4]; |
330 |
|
331 |
char h_pkt[MON_PKT_HEADER_SPACE];
|
332 |
char h_data[MON_DATA_HEADER_SPACE];
|
333 |
|
334 |
struct msg_header msg_h;
|
335 |
|
336 |
debug("ML: send_msg to %s conID:%d extID:%d\n", conid_to_string(con_id), con_id, connectbuf[con_id]->external_connectionID);
|
337 |
|
338 |
iov[0].iov_base = &msg_h;
|
339 |
iov[0].iov_len = MSG_HEADER_SIZE;
|
340 |
|
341 |
msg_h.local_con_id = htonl(con_id); |
342 |
msg_h.remote_con_id = htonl(connectbuf[con_id]->external_connectionID); |
343 |
msg_h.msg_type = msg_type; |
344 |
msg_h.msg_seq_num = htonl(connectbuf[con_id]->seqnr++); |
345 |
|
346 |
|
347 |
iov[1].iov_len = iov[2].iov_len = 0; |
348 |
iov[1].iov_base = h_pkt;
|
349 |
iov[2].iov_base = h_data;
|
350 |
|
351 |
|
352 |
if (connectbuf[con_id]->internal_connect)
|
353 |
udpgen = connectbuf[con_id]->external_socketID.internal_addr; |
354 |
else
|
355 |
udpgen = connectbuf[con_id]->external_socketID.external_addr; |
356 |
|
357 |
do{
|
358 |
offset = 0;
|
359 |
retry = false;
|
360 |
// Monitoring layer hook
|
361 |
if(set_Monitoring_header_data_cb != NULL) { |
362 |
iov[2].iov_len = ((set_Monitoring_header_data_cb) (&(connectbuf[con_id]->external_socketID), msg_type));
|
363 |
} |
364 |
msg_h.len_mon_data_hdr = iov[2].iov_len;
|
365 |
|
366 |
if(get_Send_data_inf_cb != NULL && iov[2].iov_len != 0) { |
367 |
mon_data_inf sd_data_inf; |
368 |
|
369 |
memset(h_data, 0, MON_DATA_HEADER_SPACE);
|
370 |
|
371 |
sd_data_inf.remote_socketID = &(connectbuf[con_id]->external_socketID); |
372 |
sd_data_inf.buffer = msg; |
373 |
sd_data_inf.bufSize = msg_len; |
374 |
sd_data_inf.msgtype = msg_type; |
375 |
sd_data_inf.monitoringDataHeader = iov[2].iov_base;
|
376 |
sd_data_inf.monitoringDataHeaderLen = iov[2].iov_len;
|
377 |
sd_data_inf.priority = sParams->priority; |
378 |
sd_data_inf.padding = sParams->padding; |
379 |
sd_data_inf.confirmation = sParams->confirmation; |
380 |
sd_data_inf.reliable = sParams->reliable; |
381 |
memset(&sd_data_inf.arrival_time, 0, sizeof(struct timeval)); |
382 |
|
383 |
(get_Send_data_inf_cb) ((void *) &sd_data_inf);
|
384 |
} |
385 |
|
386 |
do {
|
387 |
if(set_Monitoring_header_pkt_cb != NULL) { |
388 |
iov[1].iov_len = (set_Monitoring_header_pkt_cb) (&(connectbuf[con_id]->external_socketID), msg_type);
|
389 |
} |
390 |
pkt_len = min(connectbuf[con_id]->pmtusize - iov[2].iov_len - iov[1].iov_len - iov[0].iov_len, msg_len - offset) ; |
391 |
|
392 |
iov[3].iov_len = pkt_len;
|
393 |
iov[3].iov_base = msg + offset;
|
394 |
|
395 |
//fill header
|
396 |
msg_h.len_mon_packet_hdr = iov[1].iov_len;
|
397 |
msg_h.offset = htonl(offset); |
398 |
msg_h.msg_length = htonl(truncable ? pkt_len : msg_len); |
399 |
|
400 |
|
401 |
debug("ML: sending packet to %s with rconID:%d lconID:%d\n", conid_to_string(con_id), ntohl(msg_h.remote_con_id), ntohl(msg_h.local_con_id));
|
402 |
int priority = 0; |
403 |
if ((msg_type == ML_CON_MSG)
|
404 |
#ifdef RTX
|
405 |
|| (msg_type == ML_NACK_MSG) |
406 |
#endif
|
407 |
) priority = HP; |
408 |
//fprintf(stderr,"*******************************ML.C: Sending packet: msg_h.offset: %d msg_h.msg_seq_num: %d\n",ntohl(msg_h.offset),ntohl(msg_h.msg_seq_num));
|
409 |
switch(queueOrSendPacket(socketfd, iov, 4, &udpgen.udpaddr,priority)) { |
410 |
case MSGLEN:
|
411 |
info("ML: sending message failed, reducing MTU from %d to %d (to:%s conID:%d lconID:%d msgsize:%d offset:%d)\n", connectbuf[con_id]->pmtusize, pmtu_decrement(connectbuf[con_id]->pmtusize), conid_to_string(con_id), ntohl(msg_h.remote_con_id), ntohl(msg_h.local_con_id), msg_len, offset);
|
412 |
// TODO: pmtu decremented here, but not in the "truncable" packet. That is currently resent without changing the claimed pmtu. Might need to be changed.
|
413 |
connectbuf[con_id]->pmtusize = pmtu_decrement(connectbuf[con_id]->pmtusize); |
414 |
if (connectbuf[con_id]->pmtusize > 0) { |
415 |
connectbuf[con_id]->delay = true;
|
416 |
retry = true;
|
417 |
} |
418 |
offset = msg_len; // exit the while
|
419 |
break;
|
420 |
case FAILURE:
|
421 |
info("ML: sending message failed (to:%s conID:%d lconID:%d msgsize:%d offset:%d)\n", conid_to_string(con_id), ntohl(msg_h.remote_con_id), ntohl(msg_h.local_con_id), msg_len, offset);
|
422 |
offset = msg_len; // exit the while
|
423 |
break;
|
424 |
case THROTTLE:
|
425 |
debug("THROTTLE on output");
|
426 |
offset = msg_len; // exit the while
|
427 |
break;
|
428 |
case OK:
|
429 |
#ifdef RTX
|
430 |
if (msg_type < 127) counters.sentDataPktCounter++; |
431 |
#endif
|
432 |
//update
|
433 |
offset += pkt_len; |
434 |
//transmit data header only in the first packet
|
435 |
iov[2].iov_len = 0; |
436 |
break;
|
437 |
} |
438 |
} while(offset != msg_len && !truncable);
|
439 |
} while(retry);
|
440 |
//fprintf(stderr, "sentDataPktCounter after msg_seq_num = %d: %d\n", msg_h.msg_seq_num, counters.sentDataPktCounter);
|
441 |
//fprintf(stderr, "sentRTXDataPktCounter after msg_seq_num = %d: %d\n", msg_h.msg_seq_num, counters.sentRTXDataPktCtr);
|
442 |
} |
443 |
|
444 |
void pmtu_timeout_cb(int fd, short event, void *arg); |
445 |
|
446 |
int sendPacket(const int udpSocket, struct iovec *iov, int len, struct sockaddr_in *socketaddr) { |
447 |
//monitoring layer hook
|
448 |
debug("SENDP1");
|
449 |
if(get_Send_pkt_inf_cb != NULL && iov[1].iov_len) { |
450 |
mon_pkt_inf pkt_info; |
451 |
|
452 |
struct msg_header *msg_h = (struct msg_header *) iov[0].iov_base; |
453 |
|
454 |
memset(iov[1].iov_base,0,iov[1].iov_len); |
455 |
|
456 |
pkt_info.remote_socketID = &(connectbuf[ntohl(msg_h->local_con_id)]->external_socketID); |
457 |
pkt_info.buffer = iov[3].iov_base;
|
458 |
pkt_info.bufSize = iov[3].iov_len;
|
459 |
pkt_info.msgtype = msg_h->msg_type; |
460 |
pkt_info.dataID = ntohl(msg_h->msg_seq_num); |
461 |
pkt_info.offset = ntohl(msg_h->offset); |
462 |
pkt_info.datasize = ntohl(msg_h->msg_length); |
463 |
pkt_info.monitoringHeaderLen = iov[1].iov_len;
|
464 |
pkt_info.monitoringHeader = iov[1].iov_base;
|
465 |
pkt_info.ttl = -1;
|
466 |
memset(&(pkt_info.arrival_time),0,sizeof(struct timeval)); |
467 |
|
468 |
(get_Send_pkt_inf_cb) ((void *) &pkt_info);
|
469 |
} |
470 |
|
471 |
//struct msg_header *msg_h;
|
472 |
//msg_h = (struct msg_header *) iov[0].iov_base;
|
473 |
|
474 |
//fprintf(stderr,"*** Sending packet - msgSeqNum: %d offset: %d\n",ntohl(msg_h->msg_seq_num),ntohl(msg_h->offset));
|
475 |
debug("SENDP2");
|
476 |
|
477 |
return sendPacketFinal(udpSocket, iov, len, socketaddr);
|
478 |
} |
479 |
|
480 |
void reschedule_conn_msg(int con_id) |
481 |
{ |
482 |
if (connectbuf[con_id]->timeout_event) {
|
483 |
/* delete old timout */
|
484 |
event_del(connectbuf[con_id]->timeout_event); |
485 |
event_free(connectbuf[con_id]->timeout_event); |
486 |
} |
487 |
connectbuf[con_id]->timeout_event = event_new(base, -1, EV_TIMEOUT, &pmtu_timeout_cb, (void *) (long)con_id); |
488 |
evtimer_add(connectbuf[con_id]->timeout_event, &connectbuf[con_id]->timeout_value); |
489 |
} |
490 |
|
491 |
void send_conn_msg(int con_id, int buf_size, int command_type) |
492 |
{ |
493 |
if (buf_size < sizeof(struct conn_msg)) { |
494 |
error("ML: requested connection message size is too small\n");
|
495 |
return;
|
496 |
} |
497 |
|
498 |
if(connectbuf[con_id]->ctrl_msg_buf == NULL) { |
499 |
connectbuf[con_id]->ctrl_msg_buf = malloc(buf_size); |
500 |
memset(connectbuf[con_id]->ctrl_msg_buf, 0, buf_size);
|
501 |
} |
502 |
|
503 |
if(connectbuf[con_id]->ctrl_msg_buf == NULL) { |
504 |
error("ML: can not allocate memory for connection message\n");
|
505 |
return;
|
506 |
} |
507 |
|
508 |
struct conn_msg *msg_header = (struct conn_msg*) connectbuf[con_id]->ctrl_msg_buf; |
509 |
|
510 |
msg_header->comand_type = command_type; |
511 |
msg_header->pmtu_size = connectbuf[con_id]->pmtusize; |
512 |
|
513 |
memcpy(&(msg_header->sock_id), loc_socketID, sizeof(socket_ID));
|
514 |
{ |
515 |
char buf[SOCKETID_STRING_SIZE];
|
516 |
mlSocketIDToString(&((struct conn_msg*)connectbuf[con_id]->ctrl_msg_buf)->sock_id,buf,sizeof(buf)); |
517 |
debug("Local socket_address sent in INVITE: %s, sizeof msg %ld\n", buf, sizeof(struct conn_msg)); |
518 |
} |
519 |
send_msg(con_id, ML_CON_MSG, connectbuf[con_id]->ctrl_msg_buf, buf_size, true, &(connectbuf[con_id]->defaultSendParams));
|
520 |
} |
521 |
|
522 |
void send_conn_msg_with_pmtu_discovery(int con_id, int buf_size, int command_type) |
523 |
{ |
524 |
struct timeval tout = {0,0}; |
525 |
tout.tv_usec = PMTU_TIMEOUT * (1.0+ 0.1 *((double)rand()/(double)RAND_MAX-0.5)); |
526 |
connectbuf[con_id]->timeout_value = tout; |
527 |
connectbuf[con_id]->trials = 1;
|
528 |
send_conn_msg(con_id, buf_size, command_type); |
529 |
reschedule_conn_msg(con_id); |
530 |
} |
531 |
|
532 |
void resend_conn_msg(int con_id) |
533 |
{ |
534 |
connectbuf[con_id]->trials++; |
535 |
send_conn_msg(con_id, connectbuf[con_id]->pmtusize, connectbuf[con_id]->status); |
536 |
reschedule_conn_msg(con_id); |
537 |
} |
538 |
|
539 |
void recv_conn_msg(struct msg_header *msg_h, char *msgbuf, int msg_size, struct sockaddr_in *recv_addr) |
540 |
{ |
541 |
struct conn_msg *con_msg;
|
542 |
int free_con_id, con_id;
|
543 |
|
544 |
time_t now = time(NULL);
|
545 |
double timediff = 0.0; |
546 |
char sock_id_str[1000]; |
547 |
|
548 |
msgbuf += msg_h->len_mon_data_hdr; |
549 |
msg_size -= msg_h->len_mon_data_hdr; |
550 |
con_msg = (struct conn_msg *)msgbuf;
|
551 |
|
552 |
//verify message validity
|
553 |
if (msg_size < sizeof(struct conn_msg)) { |
554 |
char recv_addr_str[INET_ADDRSTRLEN];
|
555 |
inet_ntop(AF_INET, &(recv_addr->sin_addr.s_addr), recv_addr_str, INET_ADDRSTRLEN); |
556 |
info("Invalid conn_msg received from %s\n", recv_addr_str);
|
557 |
return;
|
558 |
} |
559 |
|
560 |
//decode sock_id for debug messages
|
561 |
mlSocketIDToString(&con_msg->sock_id,sock_id_str,999);
|
562 |
|
563 |
if (con_msg->sock_id.internal_addr.udpaddr.sin_addr.s_addr != recv_addr->sin_addr.s_addr &&
|
564 |
con_msg->sock_id.external_addr.udpaddr.sin_addr.s_addr != recv_addr->sin_addr.s_addr ) { |
565 |
char recv_addr_str[INET_ADDRSTRLEN];
|
566 |
inet_ntop(AF_INET, &(recv_addr->sin_addr.s_addr), recv_addr_str, INET_ADDRSTRLEN); |
567 |
info("Conn msg received from %s, but claims to be from %s", recv_addr_str, sock_id_str);
|
568 |
return;
|
569 |
} |
570 |
|
571 |
// Monitoring layer hook
|
572 |
if(get_Recv_data_inf_cb != NULL) { |
573 |
// update pointer to the real data
|
574 |
mon_data_inf recv_data_inf; |
575 |
recv_data_inf.remote_socketID = &(con_msg->sock_id); |
576 |
recv_data_inf.buffer = msgbuf; |
577 |
recv_data_inf.bufSize = msg_size; |
578 |
recv_data_inf.msgtype = msg_h->msg_type; |
579 |
recv_data_inf.monitoringDataHeaderLen = msg_h->len_mon_data_hdr; |
580 |
recv_data_inf.monitoringDataHeader = msg_h->len_mon_data_hdr ? msgbuf : NULL;
|
581 |
gettimeofday(&recv_data_inf.arrival_time, NULL);
|
582 |
recv_data_inf.firstPacketArrived = true;
|
583 |
recv_data_inf.recvFragments = 1;
|
584 |
recv_data_inf.priority = false;
|
585 |
recv_data_inf.padding = false;
|
586 |
recv_data_inf.confirmation = false;
|
587 |
recv_data_inf.reliable = false;
|
588 |
|
589 |
// send data recv callback to monitoring module
|
590 |
(get_Recv_data_inf_cb) ((void *) &recv_data_inf);
|
591 |
} |
592 |
|
593 |
// check the connection command type
|
594 |
switch (con_msg->comand_type) {
|
595 |
/*
|
596 |
* if INVITE: enter a new socket make new entry in connect array
|
597 |
* send an ok
|
598 |
*/
|
599 |
case INVITE:
|
600 |
info("ML: received INVITE from %s (size:%d)\n", sock_id_str, msg_size);
|
601 |
/*
|
602 |
* check if another connection for the external connectionID exist
|
603 |
* that was established within the last 2 seconds
|
604 |
*/
|
605 |
free_con_id = -1;
|
606 |
for (con_id = 0; con_id < CONNECTBUFSIZE; con_id++) { |
607 |
if (connectbuf[con_id] != NULL) { |
608 |
if (mlCompareSocketIDs(&(connectbuf[con_id]->external_socketID), &(con_msg->sock_id)) == 0) { |
609 |
//timediff = difftime(now, connectbuf[con_id]->starttime); //TODO: why this timeout? Shouldn't the connection be closed instead if there is a timeout?
|
610 |
//if (timediff < 2)
|
611 |
//update remote connection ID
|
612 |
if (connectbuf[con_id]->external_connectionID != msg_h->local_con_id) {
|
613 |
warn("ML: updating remote connection ID for %s: from %d to %d\n",sock_id_str, connectbuf[con_id]->external_connectionID, msg_h->local_con_id);
|
614 |
connectbuf[con_id]->external_connectionID = msg_h->local_con_id; |
615 |
} |
616 |
break;
|
617 |
} |
618 |
} else if(free_con_id == -1) |
619 |
free_con_id = con_id; |
620 |
} |
621 |
|
622 |
if (con_id == CONNECTBUFSIZE) {
|
623 |
// create an entry in the connecttrybuf
|
624 |
if(free_con_id == -1) { |
625 |
error("ML: no new connect_buf available\n");
|
626 |
return;
|
627 |
} |
628 |
connectbuf[free_con_id] = (connect_data *) malloc(sizeof(connect_data));
|
629 |
memset(connectbuf[free_con_id],0,sizeof(connect_data)); |
630 |
connectbuf[free_con_id]->connection_head = connectbuf[free_con_id]->connection_last = NULL;
|
631 |
connectbuf[free_con_id]->starttime = time(NULL);
|
632 |
memcpy(&(connectbuf[free_con_id]->external_socketID), &(con_msg->sock_id), sizeof(socket_ID));
|
633 |
//Workaround to support reuse of socketID
|
634 |
connectbuf[free_con_id]->external_socketID.internal_addr.udpaddr.sin_family=AF_INET; |
635 |
connectbuf[free_con_id]->external_socketID.external_addr.udpaddr.sin_family=AF_INET; |
636 |
connectbuf[free_con_id]->pmtusize = con_msg->pmtu_size; // bootstrap pmtu from the other's size. Not strictly needed, but a good hint
|
637 |
connectbuf[free_con_id]->timeout_event = NULL;
|
638 |
connectbuf[free_con_id]->external_connectionID = msg_h->local_con_id; |
639 |
connectbuf[free_con_id]->internal_connect = |
640 |
!compare_external_address_socketIDs(&(con_msg->sock_id), loc_socketID); |
641 |
con_id = free_con_id; |
642 |
} |
643 |
|
644 |
//if(connectbuf[con_id]->status <= CONNECT) { //TODO: anwer anyway. Why the outher would invite otherwise?
|
645 |
//update status and send back answer
|
646 |
connectbuf[con_id]->status = CONNECT; |
647 |
send_conn_msg_with_pmtu_discovery(con_id, con_msg->pmtu_size, CONNECT); |
648 |
//}
|
649 |
break;
|
650 |
case CONNECT:
|
651 |
info("ML: received CONNECT from %s (size:%d)\n", sock_id_str, msg_size);
|
652 |
|
653 |
if(msg_h->remote_con_id != -1 && connectbuf[msg_h->remote_con_id] == NULL) { |
654 |
error("ML: received CONNECT for inexistent connection rconID:%d\n",msg_h->remote_con_id);
|
655 |
return;
|
656 |
} |
657 |
|
658 |
/*
|
659 |
* check if the connection status is not already 1 or 2
|
660 |
*/
|
661 |
if (connectbuf[msg_h->remote_con_id]->status == INVITE) {
|
662 |
// set the external connectionID
|
663 |
connectbuf[msg_h->remote_con_id]->external_connectionID = msg_h->local_con_id; |
664 |
// change status con_msg the connection_data
|
665 |
connectbuf[msg_h->remote_con_id]->status = READY; |
666 |
// change pmtusize in the connection_data: not needed. receiving a CONNECT means our INVITE went through. So why change pmtu?
|
667 |
//connectbuf[msg_h->remote_con_id]->pmtusize = con_msg->pmtu_size;
|
668 |
|
669 |
// send the READY
|
670 |
send_conn_msg_with_pmtu_discovery(msg_h->remote_con_id, con_msg->pmtu_size, READY); |
671 |
|
672 |
if (receive_Connection_cb != NULL) |
673 |
(receive_Connection_cb) (msg_h->remote_con_id, NULL);
|
674 |
|
675 |
// call all registered callbacks
|
676 |
while(connectbuf[msg_h->remote_con_id]->connection_head != NULL) { |
677 |
struct receive_connection_cb_list *temp;
|
678 |
temp = connectbuf[msg_h->remote_con_id]->connection_head; |
679 |
(temp->connection_cb) (msg_h->remote_con_id, temp->arg); |
680 |
connectbuf[msg_h->remote_con_id]->connection_head = temp->next; |
681 |
free(temp); |
682 |
} |
683 |
connectbuf[msg_h->remote_con_id]->connection_head = |
684 |
connectbuf[msg_h->remote_con_id]->connection_last = NULL;
|
685 |
} else {
|
686 |
// send the READY
|
687 |
send_conn_msg_with_pmtu_discovery(msg_h->remote_con_id, con_msg->pmtu_size, READY); |
688 |
} |
689 |
|
690 |
debug("ML: active connection established\n");
|
691 |
break;
|
692 |
|
693 |
/*
|
694 |
* if READY: find the entry in the connection array set the
|
695 |
* connection active change the pmtu size
|
696 |
*/
|
697 |
case READY:
|
698 |
info("ML: received READY from %s (size:%d)\n", sock_id_str, msg_size);
|
699 |
if(connectbuf[msg_h->remote_con_id] == NULL) { |
700 |
error("ML: received READY for inexistent connection\n");
|
701 |
return;
|
702 |
} |
703 |
/*
|
704 |
* checks if the connection is not already established
|
705 |
*/
|
706 |
if (connectbuf[msg_h->remote_con_id]->status == CONNECT) {
|
707 |
// change status of the connection
|
708 |
connectbuf[msg_h->remote_con_id]->status = READY; |
709 |
// change pmtusize: not needed. pmtu doesn't have to be symmetric
|
710 |
//connectbuf[msg_h->remote_con_id]->pmtusize = con_msg->pmtu_size;
|
711 |
|
712 |
if (receive_Connection_cb != NULL) |
713 |
(receive_Connection_cb) (msg_h->remote_con_id, NULL);
|
714 |
|
715 |
while(connectbuf[msg_h->remote_con_id]->connection_head != NULL) { |
716 |
struct receive_connection_cb_list *temp;
|
717 |
temp = connectbuf[msg_h->remote_con_id]->connection_head; |
718 |
(temp->connection_cb) (msg_h->remote_con_id, temp->arg); |
719 |
connectbuf[msg_h->remote_con_id]->connection_head = temp->next; |
720 |
free(temp); |
721 |
} |
722 |
connectbuf[msg_h->remote_con_id]->connection_head = |
723 |
connectbuf[msg_h->remote_con_id]->connection_last = NULL;
|
724 |
debug("ML: passive connection established\n");
|
725 |
} |
726 |
break;
|
727 |
} |
728 |
} |
729 |
|
730 |
void recv_stun_msg(char *msgbuf, int recvSize) |
731 |
{ |
732 |
/*
|
733 |
* create empty stun message struct
|
734 |
*/
|
735 |
StunMessage resp; |
736 |
memset(&resp, 0, sizeof(StunMessage)); |
737 |
/*
|
738 |
* parse the message
|
739 |
*/
|
740 |
int returnValue = 0; |
741 |
returnValue = recv_stun_message(msgbuf, recvSize, &resp); |
742 |
|
743 |
if (returnValue == 0) { |
744 |
/*
|
745 |
* read the reflexive Address into the local_socketID
|
746 |
*/
|
747 |
struct sockaddr_in reflexiveAddr = {0}; |
748 |
reflexiveAddr.sin_family = AF_INET; |
749 |
reflexiveAddr.sin_addr.s_addr = htonl(resp.mappedAddress.ipv4.addr); |
750 |
reflexiveAddr.sin_port = htons(resp.mappedAddress.ipv4.port); |
751 |
socketaddrgen reflexiveAddres = {0};
|
752 |
reflexiveAddres.udpaddr = reflexiveAddr; |
753 |
local_socketID.external_addr = reflexiveAddres; |
754 |
NAT_traversal = true;
|
755 |
// callback to the upper layer indicating that the socketID is now
|
756 |
// ready to use
|
757 |
{ |
758 |
char buf[SOCKETID_STRING_SIZE];
|
759 |
mlSocketIDToString(&local_socketID,buf,sizeof(buf));
|
760 |
debug("received local socket_address: %s\n", buf);
|
761 |
} |
762 |
(receive_SocketID_cb) (&local_socketID, 0);
|
763 |
} |
764 |
} |
765 |
|
766 |
//done
|
767 |
void recv_timeout_cb(int fd, short event, void *arg) |
768 |
{ |
769 |
int recv_id = (long) arg; |
770 |
debug("ML: recv_timeout_cb called. Timeout for id:%d\n",recv_id);
|
771 |
|
772 |
if (recvdatabuf[recv_id] == NULL) { |
773 |
return;
|
774 |
} |
775 |
|
776 |
|
777 |
/* if(recvdatabuf[recv_id]->status == ACTIVE) {
|
778 |
//TODO make timeout at least a DEFINE
|
779 |
struct timeval timeout = { 4, 0 };
|
780 |
recvdatabuf[recv_id]->status = INACTIVE;
|
781 |
event_base_once(base, -1, EV_TIMEOUT, recv_timeout_cb,
|
782 |
arg, &timeout);
|
783 |
return;
|
784 |
}
|
785 |
*/
|
786 |
|
787 |
if(recvdatabuf[recv_id]->status == ACTIVE) {
|
788 |
// Monitoring layer hook
|
789 |
if(get_Recv_data_inf_cb != NULL) { |
790 |
mon_data_inf recv_data_inf; |
791 |
|
792 |
recv_data_inf.remote_socketID = |
793 |
&(connectbuf[recvdatabuf[recv_id]->connectionID]->external_socketID); |
794 |
recv_data_inf.buffer = recvdatabuf[recv_id]->recvbuf; |
795 |
recv_data_inf.bufSize = recvdatabuf[recv_id]->bufsize; |
796 |
recv_data_inf.msgtype = recvdatabuf[recv_id]->msgtype; |
797 |
recv_data_inf.monitoringDataHeaderLen = recvdatabuf[recv_id]->monitoringDataHeaderLen; |
798 |
recv_data_inf.monitoringDataHeader = recvdatabuf[recv_id]->monitoringDataHeaderLen ? |
799 |
recvdatabuf[recv_id]->recvbuf : NULL;
|
800 |
gettimeofday(&recv_data_inf.arrival_time, NULL);
|
801 |
recv_data_inf.firstPacketArrived = recvdatabuf[recv_id]->firstPacketArrived; |
802 |
recv_data_inf.recvFragments = recvdatabuf[recv_id]->recvFragments; |
803 |
recv_data_inf.priority = false;
|
804 |
recv_data_inf.padding = false;
|
805 |
recv_data_inf.confirmation = false;
|
806 |
recv_data_inf.reliable = false;
|
807 |
|
808 |
// send data recv callback to monitoring module
|
809 |
|
810 |
// (get_Recv_data_inf_cb) ((void *) &recv_data_inf);
|
811 |
} |
812 |
|
813 |
// Get the right callback
|
814 |
receive_data_cb receive_data_callback = recvcbbuf[recvdatabuf[recv_id]->msgtype]; |
815 |
|
816 |
recv_params rParams; |
817 |
|
818 |
rParams.nrMissingBytes = recvdatabuf[recv_id]->bufsize - recvdatabuf[recv_id]->arrivedBytes; |
819 |
rParams.recvFragments = recvdatabuf[recv_id]->recvFragments; |
820 |
rParams.msgtype = recvdatabuf[recv_id]->msgtype; |
821 |
rParams.connectionID = recvdatabuf[recv_id]->connectionID; |
822 |
rParams.remote_socketID = |
823 |
&(connectbuf[recvdatabuf[recv_id]->connectionID]->external_socketID); |
824 |
rParams.firstPacketArrived = recvdatabuf[recv_id]->firstPacketArrived; |
825 |
|
826 |
#ifdef RTX
|
827 |
counters.receivedIncompleteMsgCounter++; |
828 |
//mlShowCounters();
|
829 |
//fprintf(stderr,"******Cleaning slot for inclomplete msg_seq_num: %d\n", recvdatabuf[recv_id]->seqnr);
|
830 |
#endif
|
831 |
//(receive_data_callback) (recvdatabuf[recv_id]->recvbuf + recvdatabuf[recv_id]->monitoringDataHeaderLen, recvdatabuf[recv_id]->bufsize - recvdatabuf[recv_id]->monitoringDataHeaderLen, recvdatabuf[recv_id]->msgtype, &rParams);
|
832 |
|
833 |
//clean up
|
834 |
if (recvdatabuf[recv_id]->timeout_event) {
|
835 |
event_del(recvdatabuf[recv_id]->timeout_event); |
836 |
event_free(recvdatabuf[recv_id]->timeout_event); |
837 |
recvdatabuf[recv_id]->timeout_event = NULL;
|
838 |
} |
839 |
free(recvdatabuf[recv_id]->recvbuf); |
840 |
free(recvdatabuf[recv_id]); |
841 |
recvdatabuf[recv_id] = NULL;
|
842 |
} |
843 |
} |
844 |
|
845 |
// process a single recv data message
|
846 |
void recv_data_msg(struct msg_header *msg_h, char *msgbuf, int bufsize) |
847 |
{ |
848 |
debug("ML: received packet of size %d with rconID:%d lconID:%d type:%d offset:%d inlength: %d\n",bufsize,msg_h->remote_con_id,msg_h->local_con_id,msg_h->msg_type,msg_h->offset, msg_h->msg_length);
|
849 |
|
850 |
int recv_id, free_recv_id = -1; |
851 |
|
852 |
if(connectbuf[msg_h->remote_con_id] == NULL) { |
853 |
debug("ML: Received a message not related to any opened connection!\n");
|
854 |
return;
|
855 |
} |
856 |
|
857 |
#ifdef RTX
|
858 |
counters.receivedDataPktCounter++; |
859 |
#endif
|
860 |
// check if a recv_data exist and enter data
|
861 |
for (recv_id = 0; recv_id < RECVDATABUFSIZE; recv_id++) { |
862 |
if (recvdatabuf[recv_id] != NULL) { |
863 |
if (msg_h->remote_con_id == recvdatabuf[recv_id]->connectionID &&
|
864 |
msg_h->msg_seq_num == recvdatabuf[recv_id]->seqnr) |
865 |
break;
|
866 |
} else
|
867 |
if(free_recv_id == -1) |
868 |
free_recv_id = recv_id; |
869 |
} |
870 |
|
871 |
if(recv_id == RECVDATABUFSIZE) {
|
872 |
debug(" recv id not found (free found: %d)\n", free_recv_id);
|
873 |
//no recv_data found: create one
|
874 |
recv_id = free_recv_id; |
875 |
recvdatabuf[recv_id] = (recvdata *) malloc(sizeof(recvdata));
|
876 |
memset(recvdatabuf[recv_id], 0, sizeof(recvdata)); |
877 |
recvdatabuf[recv_id]->connectionID = msg_h->remote_con_id; |
878 |
recvdatabuf[recv_id]->seqnr = msg_h->msg_seq_num; |
879 |
recvdatabuf[recv_id]->monitoringDataHeaderLen = msg_h->len_mon_data_hdr; |
880 |
recvdatabuf[recv_id]->bufsize = msg_h->msg_length + msg_h->len_mon_data_hdr; |
881 |
recvdatabuf[recv_id]->recvbuf = (char *) malloc(recvdatabuf[recv_id]->bufsize);
|
882 |
recvdatabuf[recv_id]->arrivedBytes = 0; //count this without the Mon headers |
883 |
#ifdef RTX
|
884 |
recvdatabuf[recv_id]->txConnectionID = msg_h->local_con_id; |
885 |
recvdatabuf[recv_id]->expectedOffset = 0;
|
886 |
recvdatabuf[recv_id]->gapCounter = 0;
|
887 |
recvdatabuf[recv_id]->firstGap = 0;
|
888 |
recvdatabuf[recv_id]->last_pkt_timeout_event = NULL;
|
889 |
#endif
|
890 |
|
891 |
/*
|
892 |
* read the timeout data and set it
|
893 |
*/
|
894 |
recvdatabuf[recv_id]->timeout_value = recv_timeout; |
895 |
recvdatabuf[recv_id]->timeout_event = NULL;
|
896 |
recvdatabuf[recv_id]->recvID = recv_id; |
897 |
recvdatabuf[recv_id]->starttime = time(NULL);
|
898 |
recvdatabuf[recv_id]->msgtype = msg_h->msg_type; |
899 |
|
900 |
// fill the buffer with zeros
|
901 |
memset(recvdatabuf[recv_id]->recvbuf, 0, recvdatabuf[recv_id]->bufsize);
|
902 |
debug(" new @ id:%d\n",recv_id);
|
903 |
} else { //message structure already exists, no need to create new |
904 |
debug(" found @ id:%d (arrived before this packet: bytes:%d fragments%d\n",recv_id, recvdatabuf[recv_id]->arrivedBytes, recvdatabuf[recv_id]->recvFragments);
|
905 |
} |
906 |
|
907 |
//if first packet extract mon data header and advance pointer
|
908 |
if (msg_h->offset == 0) { |
909 |
//fprintf(stderr,"Hoooooray!! We have first packet of some message!!\n");
|
910 |
memcpy(recvdatabuf[recv_id]->recvbuf, msgbuf, msg_h->len_mon_data_hdr); |
911 |
msgbuf += msg_h->len_mon_data_hdr; |
912 |
bufsize -= msg_h->len_mon_data_hdr; |
913 |
recvdatabuf[recv_id]->firstPacketArrived = 1;
|
914 |
} |
915 |
|
916 |
|
917 |
// increment fragmentnr
|
918 |
recvdatabuf[recv_id]->recvFragments++; |
919 |
// increment the arrivedBytes
|
920 |
recvdatabuf[recv_id]->arrivedBytes += bufsize; |
921 |
|
922 |
//fprintf(stderr,"Arrived bytes: %d Offset: %d Expected offset: %d\n",recvdatabuf[recv_id]->arrivedBytes/1349,msg_h->offset/1349,recvdatabuf[recv_id]->expectedOffset/1349);
|
923 |
|
924 |
// enter the data into the buffer
|
925 |
memcpy(recvdatabuf[recv_id]->recvbuf + msg_h->len_mon_data_hdr + msg_h->offset, msgbuf, bufsize); |
926 |
#ifdef RTX
|
927 |
// detecting a new gap
|
928 |
if (msg_h->offset > recvdatabuf[recv_id]->expectedOffset) {
|
929 |
recvdatabuf[recv_id]->gapArray[recvdatabuf[recv_id]->gapCounter].offsetFrom = recvdatabuf[recv_id]->expectedOffset; |
930 |
recvdatabuf[recv_id]->gapArray[recvdatabuf[recv_id]->gapCounter].offsetTo = msg_h->offset; |
931 |
if (recvdatabuf[recv_id]->gapCounter < RTX_MAX_GAPS - 1) recvdatabuf[recv_id]->gapCounter++; |
932 |
evtimer_add(event_new(base, -1, EV_TIMEOUT, &pkt_recv_timeout_cb, (void *) (long)recv_id), &pkt_recv_timeout); |
933 |
} |
934 |
|
935 |
//filling the gap by delayed packets
|
936 |
if (msg_h->offset < recvdatabuf[recv_id]->expectedOffset){
|
937 |
counters.receivedRTXDataPktCounter++; |
938 |
//skip retransmitted packets
|
939 |
if (recvdatabuf[recv_id]->firstGap < recvdatabuf[recv_id]->gapCounter && msg_h->offset >= recvdatabuf[recv_id]->gapArray[recvdatabuf[recv_id]->firstGap].offsetFrom) {
|
940 |
int i;
|
941 |
//fprintf(stderr,"firstGap: %d gapCounter: %d\n", recvdatabuf[recv_id]->firstGap, recvdatabuf[recv_id]->gapCounter);
|
942 |
for (i = recvdatabuf[recv_id]->firstGap; i < recvdatabuf[recv_id]->gapCounter; i++){
|
943 |
if (msg_h->offset == recvdatabuf[recv_id]->gapArray[i].offsetFrom) {
|
944 |
recvdatabuf[recv_id]->gapArray[i].offsetFrom += bufsize; |
945 |
break;
|
946 |
} |
947 |
if (msg_h->offset == (recvdatabuf[recv_id]->gapArray[i].offsetTo - bufsize)) {
|
948 |
recvdatabuf[recv_id]->gapArray[i].offsetTo -= bufsize; |
949 |
break;
|
950 |
} |
951 |
} |
952 |
} else {//fprintf(stderr,"Skipping retransmitted packets in filling the gap.\n"); |
953 |
//counters.receivedRTXDataPktCounter++;
|
954 |
} |
955 |
} |
956 |
|
957 |
//updating the expectedOffset
|
958 |
if (msg_h->offset >= recvdatabuf[recv_id]->expectedOffset) recvdatabuf[recv_id]->expectedOffset = msg_h->offset + bufsize;
|
959 |
#endif
|
960 |
|
961 |
//TODO very basic checkif all fragments arrived: has to be reviewed
|
962 |
if(recvdatabuf[recv_id]->arrivedBytes == recvdatabuf[recv_id]->bufsize - recvdatabuf[recv_id]->monitoringDataHeaderLen)
|
963 |
recvdatabuf[recv_id]->status = COMPLETE; //buffer full -> msg completly arrived
|
964 |
else
|
965 |
recvdatabuf[recv_id]->status = ACTIVE; |
966 |
|
967 |
if (recv_data_callback) {
|
968 |
if(recvdatabuf[recv_id]->status == COMPLETE) {
|
969 |
// Monitoring layer hook
|
970 |
if(get_Recv_data_inf_cb != NULL) { |
971 |
mon_data_inf recv_data_inf; |
972 |
|
973 |
recv_data_inf.remote_socketID = |
974 |
&(connectbuf[recvdatabuf[recv_id]->connectionID]->external_socketID); |
975 |
recv_data_inf.buffer = recvdatabuf[recv_id]->recvbuf; |
976 |
recv_data_inf.bufSize = recvdatabuf[recv_id]->bufsize; |
977 |
recv_data_inf.msgtype = recvdatabuf[recv_id]->msgtype; |
978 |
recv_data_inf.monitoringDataHeaderLen = recvdatabuf[recv_id]->monitoringDataHeaderLen; |
979 |
recv_data_inf.monitoringDataHeader = recvdatabuf[recv_id]->monitoringDataHeaderLen ? |
980 |
recvdatabuf[recv_id]->recvbuf : NULL;
|
981 |
gettimeofday(&recv_data_inf.arrival_time, NULL);
|
982 |
recv_data_inf.firstPacketArrived = recvdatabuf[recv_id]->firstPacketArrived; |
983 |
recv_data_inf.recvFragments = recvdatabuf[recv_id]->recvFragments; |
984 |
recv_data_inf.priority = false;
|
985 |
recv_data_inf.padding = false;
|
986 |
recv_data_inf.confirmation = false;
|
987 |
recv_data_inf.reliable = false;
|
988 |
|
989 |
// send data recv callback to monitoring module
|
990 |
|
991 |
(get_Recv_data_inf_cb) ((void *) &recv_data_inf);
|
992 |
} |
993 |
|
994 |
// Get the right callback
|
995 |
receive_data_cb receive_data_callback = recvcbbuf[msg_h->msg_type]; |
996 |
if (receive_data_callback) {
|
997 |
|
998 |
recv_params rParams; |
999 |
|
1000 |
rParams.nrMissingBytes = recvdatabuf[recv_id]->bufsize - recvdatabuf[recv_id]->monitoringDataHeaderLen - recvdatabuf[recv_id]->arrivedBytes; |
1001 |
rParams.recvFragments = recvdatabuf[recv_id]->recvFragments; |
1002 |
rParams.msgtype = recvdatabuf[recv_id]->msgtype; |
1003 |
rParams.connectionID = recvdatabuf[recv_id]->connectionID; |
1004 |
rParams.remote_socketID = |
1005 |
&(connectbuf[recvdatabuf[recv_id]->connectionID]->external_socketID); |
1006 |
|
1007 |
char str[1000]; |
1008 |
mlSocketIDToString(rParams.remote_socketID,str,999);
|
1009 |
debug("ML: received message from conID:%d, %s\n",recvdatabuf[recv_id]->connectionID,str);
|
1010 |
rParams.firstPacketArrived = recvdatabuf[recv_id]->firstPacketArrived; |
1011 |
|
1012 |
#ifdef RTX
|
1013 |
counters.receivedCompleteMsgCounter++; |
1014 |
//mlShowCounters();
|
1015 |
#endif
|
1016 |
|
1017 |
(receive_data_callback) (recvdatabuf[recv_id]->recvbuf + recvdatabuf[recv_id]->monitoringDataHeaderLen, recvdatabuf[recv_id]->bufsize - recvdatabuf[recv_id]->monitoringDataHeaderLen, |
1018 |
recvdatabuf[recv_id]->msgtype, (void *) &rParams);
|
1019 |
} else {
|
1020 |
warn("ML: callback not initialized for this message type: %d!\n",msg_h->msg_type);
|
1021 |
} |
1022 |
|
1023 |
//clean up
|
1024 |
if (recvdatabuf[recv_id]->timeout_event) {
|
1025 |
debug("ML: freeing timeout for %d",recv_id);
|
1026 |
event_del(recvdatabuf[recv_id]->timeout_event); |
1027 |
event_free(recvdatabuf[recv_id]->timeout_event); |
1028 |
recvdatabuf[recv_id]->timeout_event = NULL;
|
1029 |
} else {
|
1030 |
debug("ML: received in 1 packet\n",recv_id);
|
1031 |
} |
1032 |
#ifdef RTX
|
1033 |
if (recvdatabuf[recv_id]->last_pkt_timeout_event) {
|
1034 |
debug("ML: freeing last packet timeout for %d",recv_id);
|
1035 |
event_del(recvdatabuf[recv_id]->last_pkt_timeout_event); |
1036 |
event_free(recvdatabuf[recv_id]->last_pkt_timeout_event); |
1037 |
recvdatabuf[recv_id]->last_pkt_timeout_event = NULL;
|
1038 |
} |
1039 |
//fprintf(stderr,"******Cleaning slot for clomplete msg_seq_num: %d\n", recvdatabuf[recv_id]->seqnr);
|
1040 |
#endif
|
1041 |
free(recvdatabuf[recv_id]->recvbuf); |
1042 |
free(recvdatabuf[recv_id]); |
1043 |
recvdatabuf[recv_id] = NULL;
|
1044 |
} else { // not COMPLETE |
1045 |
if (!recvdatabuf[recv_id]->timeout_event) {
|
1046 |
//start time out
|
1047 |
//TODO make timeout at least a DEFINE
|
1048 |
recvdatabuf[recv_id]->timeout_event = event_new(base, -1, EV_TIMEOUT, &recv_timeout_cb, (void *) (long)recv_id); |
1049 |
evtimer_add(recvdatabuf[recv_id]->timeout_event, &recv_timeout); |
1050 |
#ifdef RTX
|
1051 |
recvdatabuf[recv_id]->last_pkt_timeout_event = event_new(base, -1, EV_TIMEOUT, &last_pkt_recv_timeout_cb, (void *) (long)recv_id); |
1052 |
evtimer_add(recvdatabuf[recv_id]->last_pkt_timeout_event, &last_pkt_recv_timeout); |
1053 |
#endif
|
1054 |
} |
1055 |
} |
1056 |
} |
1057 |
} |
1058 |
|
1059 |
//done
|
1060 |
void pmtu_timeout_cb(int fd, short event, void *arg) |
1061 |
{ |
1062 |
|
1063 |
int con_id = (long) arg; |
1064 |
pmtu new_pmtusize; |
1065 |
|
1066 |
debug("ML: pmtu timeout called (lcon:%d)\n",con_id);
|
1067 |
|
1068 |
if(connectbuf[con_id] == NULL) { |
1069 |
error("ML: pmtu timeout called on non existing con_id\n");
|
1070 |
return;
|
1071 |
} |
1072 |
|
1073 |
if(connectbuf[con_id]->status == READY) {
|
1074 |
// nothing to do anymore
|
1075 |
event_del(connectbuf[con_id]->timeout_event); |
1076 |
event_free(connectbuf[con_id]->timeout_event); |
1077 |
connectbuf[con_id]->timeout_event = NULL;
|
1078 |
return;
|
1079 |
} |
1080 |
|
1081 |
info("ML: pmtu timeout while connecting(to:%s lcon:%d status:%d size:%d trial:%d tout:%ld.%06ld)\n",conid_to_string(con_id), con_id, connectbuf[con_id]->status, connectbuf[con_id]->pmtusize, connectbuf[con_id]->trials, connectbuf[con_id]->timeout_value.tv_sec, connectbuf[con_id]->timeout_value.tv_usec);
|
1082 |
|
1083 |
if(connectbuf[con_id]->delay || connectbuf[con_id]->trials == MAX_TRIALS - 1) { |
1084 |
double delay = connectbuf[con_id]->timeout_value.tv_sec + connectbuf[con_id]->timeout_value.tv_usec / 1000000.0; |
1085 |
delay = delay * 2;
|
1086 |
info("\tML: increasing pmtu timeout to %f sec\n", delay);
|
1087 |
connectbuf[con_id]->timeout_value.tv_sec = floor(delay); |
1088 |
connectbuf[con_id]->timeout_value.tv_usec = fmod(delay, 1.0) * 1000000.0; |
1089 |
if(connectbuf[con_id]->delay) {
|
1090 |
connectbuf[con_id]->delay = false;
|
1091 |
reschedule_conn_msg(con_id); |
1092 |
} |
1093 |
} |
1094 |
|
1095 |
if(connectbuf[con_id]->trials == MAX_TRIALS) {
|
1096 |
// decrement the pmtu size
|
1097 |
struct timeval tout = {0,0}; |
1098 |
tout.tv_usec = PMTU_TIMEOUT * (1.0+ 0.1 *((double)rand()/(double)RAND_MAX-0.5)); |
1099 |
info("\tML: decreasing pmtu estimate from %d to %d\n", connectbuf[con_id]->pmtusize, pmtu_decrement(connectbuf[con_id]->pmtusize));
|
1100 |
connectbuf[con_id]->pmtusize = pmtu_decrement(connectbuf[con_id]->pmtusize); |
1101 |
connectbuf[con_id]->timeout_value = tout; |
1102 |
connectbuf[con_id]->trials = 0;
|
1103 |
} |
1104 |
|
1105 |
//error in PMTU discovery?
|
1106 |
if (connectbuf[con_id]->pmtusize == P_ERROR) {
|
1107 |
if (connectbuf[con_id]->internal_connect == true) { |
1108 |
//as of now we tried directly connecting, now let's try trough the NAT
|
1109 |
connectbuf[con_id]->internal_connect = false;
|
1110 |
connectbuf[con_id]->pmtusize = DSLSLIM; |
1111 |
} else {
|
1112 |
//nothing to do we have to give up
|
1113 |
error("ML: Could not create connection with connectionID %i!\n",con_id);
|
1114 |
// envoke the callback for failed connection establishment
|
1115 |
if(failed_Connection_cb != NULL) |
1116 |
(failed_Connection_cb) (con_id, NULL);
|
1117 |
// delete the connection entry
|
1118 |
mlCloseConnection(con_id); |
1119 |
return;
|
1120 |
} |
1121 |
} |
1122 |
|
1123 |
//retry
|
1124 |
resend_conn_msg(con_id); |
1125 |
} |
1126 |
|
1127 |
|
1128 |
int schedule_pmtu_timeout(int con_id) |
1129 |
{ |
1130 |
if (! connectbuf[con_id]->timeout_event) {
|
1131 |
struct timeval tout = {0,0}; |
1132 |
tout.tv_usec = PMTU_TIMEOUT * (1.0+ 0.1 *((double)rand()/(double)RAND_MAX-0.5)); |
1133 |
connectbuf[con_id]->timeout_value = tout; |
1134 |
connectbuf[con_id]->trials = 1;
|
1135 |
connectbuf[con_id]->timeout_event = event_new(base, -1, EV_TIMEOUT, &pmtu_timeout_cb, (void *) (long)con_id); |
1136 |
evtimer_add(connectbuf[con_id]->timeout_event, &connectbuf[con_id]->timeout_value); |
1137 |
} |
1138 |
} |
1139 |
|
1140 |
/*
|
1141 |
* decrements the mtu size
|
1142 |
*/
|
1143 |
pmtu pmtu_decrement(pmtu pmtusize) |
1144 |
{ |
1145 |
pmtu pmtu_return_size; |
1146 |
switch(pmtusize) {
|
1147 |
case MAX:
|
1148 |
//return DSL;
|
1149 |
return DSLSLIM; //shortcut to use less vales |
1150 |
case DSL:
|
1151 |
return DSLMEDIUM;
|
1152 |
case DSLMEDIUM:
|
1153 |
return DSLSLIM;
|
1154 |
case DSLSLIM:
|
1155 |
//return BELOWDSL;
|
1156 |
return MIN; //shortcut to use less vales |
1157 |
case BELOWDSL:
|
1158 |
return MIN;
|
1159 |
case MIN:
|
1160 |
return P_ERROR;
|
1161 |
default:
|
1162 |
warn("ML: strange pmtu size encountered:%d, changing to some safe value:%d\n", pmtusize, MIN);
|
1163 |
return MIN;
|
1164 |
} |
1165 |
} |
1166 |
|
1167 |
// called when an ICMP pmtu error message (type 3, code 4) is received
|
1168 |
void pmtu_error_cb_th(char *msg, int msglen) |
1169 |
{ |
1170 |
debug("ML: pmtu_error callback called msg_size: %d\n",msglen);
|
1171 |
//TODO debug
|
1172 |
return;
|
1173 |
|
1174 |
char *msgbufptr = NULL; |
1175 |
int msgtype;
|
1176 |
int connectionID;
|
1177 |
pmtu pmtusize; |
1178 |
pmtu new_pmtusize; |
1179 |
int dead = 0; |
1180 |
|
1181 |
// check the packettype
|
1182 |
msgbufptr = &msg[0];
|
1183 |
|
1184 |
// check the msgtype
|
1185 |
msgbufptr = &msg[1];
|
1186 |
memcpy(&msgtype, msgbufptr, 4);
|
1187 |
|
1188 |
if (msgtype == 0) { |
1189 |
|
1190 |
// get the connectionID
|
1191 |
msgbufptr = &msg[5];
|
1192 |
memcpy(&connectionID, msgbufptr, 4);
|
1193 |
|
1194 |
int msgtype_c = connectbuf[connectionID]->status;
|
1195 |
// pmtusize = connectbuf[connectionID]->pmtutrysize;
|
1196 |
|
1197 |
if (msgtype_c != msgtype) {
|
1198 |
dead = 1;
|
1199 |
} |
1200 |
|
1201 |
|
1202 |
} else if (msgtype == 1) { |
1203 |
|
1204 |
// read the connectionID
|
1205 |
msgbufptr = &msg[9];
|
1206 |
memcpy(&connectionID, msgbufptr, 4);
|
1207 |
|
1208 |
int msgtype_c = connectbuf[connectionID]->status;
|
1209 |
// pmtusize = connectbuf[connectionID]->pmtutrysize;
|
1210 |
|
1211 |
if (msgtype_c != msgtype) {
|
1212 |
dead = 1;
|
1213 |
} |
1214 |
|
1215 |
} |
1216 |
// decrement the pmtu size
|
1217 |
new_pmtusize = pmtu_decrement(pmtusize); |
1218 |
|
1219 |
// connectbuf[connectionID]->pmtutrysize = new_pmtusize;
|
1220 |
|
1221 |
if (new_pmtusize == P_ERROR) {
|
1222 |
error("ML: Could not create connection with connectionID %i !\n",
|
1223 |
connectionID); |
1224 |
|
1225 |
if(failed_Connection_cb != NULL) |
1226 |
(failed_Connection_cb) (connectionID, NULL);
|
1227 |
// set the message type to a non existent message
|
1228 |
msgtype = 2;
|
1229 |
// delete the connection entry
|
1230 |
mlCloseConnection(connectionID); |
1231 |
} |
1232 |
|
1233 |
if (msgtype == 0 && dead != 1) { |
1234 |
|
1235 |
// stop the timeout event
|
1236 |
// timeout_del(connectbuf[connectionID]->timeout);
|
1237 |
/*
|
1238 |
* libevent2
|
1239 |
*/
|
1240 |
|
1241 |
// event_del(connectbuf[connectionID]->timeout);
|
1242 |
|
1243 |
|
1244 |
// create and send a connection message
|
1245 |
// create_conn_msg(new_pmtusize, connectionID,
|
1246 |
// &local_socketID, INVITE);
|
1247 |
|
1248 |
// send_conn_msg(connectionID, new_pmtusize);
|
1249 |
|
1250 |
// set a timeout event for the pmtu discovery
|
1251 |
// timeout_set(connectbuf[connectionID]->timeout,pmtu_timeout_cb,(void
|
1252 |
// *)&connectionID);
|
1253 |
|
1254 |
// timeout_add(connectbuf[connectionID]->timeout,&connectbuf[connectionID]->timeout_value);
|
1255 |
|
1256 |
/*
|
1257 |
* libevent2
|
1258 |
*/
|
1259 |
|
1260 |
struct event *ev;
|
1261 |
ev = evtimer_new(base, pmtu_timeout_cb, |
1262 |
(void *) connectbuf[connectionID]);
|
1263 |
|
1264 |
// connectbuf[connectionID]->timeout = ev;
|
1265 |
|
1266 |
event_add(ev, &connectbuf[connectionID]->timeout_value); |
1267 |
|
1268 |
} else if (msgtype == 1 && dead != 1) { |
1269 |
|
1270 |
// stop the timeout event
|
1271 |
// timeout_del(connectbuf[connectionID]->timeout);
|
1272 |
|
1273 |
/*
|
1274 |
* libevent2
|
1275 |
*/
|
1276 |
// info("still here 11 \n");
|
1277 |
// printf("ev %d \n",connectbuf[connectionID]->timeout);
|
1278 |
// event_del(connectbuf[connectionID]->timeout );
|
1279 |
// evtimer_del(connectbuf[connectionID]->timeout );
|
1280 |
|
1281 |
|
1282 |
// // create and send a connection message
|
1283 |
// create_conn_msg(new_pmtusize,
|
1284 |
// connectbuf[connectionID]->connectionID,
|
1285 |
// NULL, CONNECT);
|
1286 |
|
1287 |
//send_conn_msg(connectionID, new_pmtusize);
|
1288 |
|
1289 |
// set a timeout event for the pmtu discovery
|
1290 |
// timeout_set(connectbuf[connectionID]->timeout,pmtu_timeout_cb,(void
|
1291 |
// *)&connectionID);
|
1292 |
// timeout_add(connectbuf[connectionID]->timeout,&connectbuf[connectionID]->timeout_value);
|
1293 |
|
1294 |
/*
|
1295 |
* libevent2
|
1296 |
*/
|
1297 |
// struct event *ev;
|
1298 |
// ev = evtimer_new(base,pmtu_timeout_cb, (void
|
1299 |
// *)connectbuf[connectionID]);
|
1300 |
// connectbuf[connectionID]->timeout = ev;
|
1301 |
// event_add(ev,&connectbuf[connectionID]->timeout_value);
|
1302 |
|
1303 |
} |
1304 |
} |
1305 |
|
1306 |
/*
|
1307 |
* what to do once a packet arrived if it is a conn packet send it to
|
1308 |
* recv_conn handler if it is a data packet send it to the recv_data
|
1309 |
* handler
|
1310 |
*/
|
1311 |
|
1312 |
//done --
|
1313 |
void recv_pkg(int fd, short event, void *arg) |
1314 |
{ |
1315 |
debug("ML: recv_pkg called\n");
|
1316 |
|
1317 |
struct msg_header *msg_h;
|
1318 |
char msgbuf[MAX];
|
1319 |
pmtu recvSize = MAX; |
1320 |
char *bufptr = msgbuf;
|
1321 |
int ttl;
|
1322 |
struct sockaddr_in recv_addr;
|
1323 |
int msg_size;
|
1324 |
|
1325 |
recvPacket(fd, msgbuf, &recvSize, &recv_addr, pmtu_error_cb_th, &ttl); |
1326 |
|
1327 |
|
1328 |
// check if it is not just an ERROR message
|
1329 |
if(recvSize < 0) |
1330 |
return;
|
1331 |
|
1332 |
// @TODO check if this simplistic STUN message recognition really always works, probably not
|
1333 |
unsigned short stun_bind_response = 0x0101; |
1334 |
unsigned short * msgspot = (unsigned short *) msgbuf; |
1335 |
if (*msgspot == stun_bind_response) {
|
1336 |
debug("ML: recv_pkg: parse stun message called on %d bytes\n", recvSize);
|
1337 |
recv_stun_msg(msgbuf, recvSize); |
1338 |
return;
|
1339 |
} |
1340 |
|
1341 |
msg_h = (struct msg_header *) msgbuf;
|
1342 |
|
1343 |
uint32_t inlen = ntohl(msg_h->msg_length); |
1344 |
if(inlen > 0x20000 || inlen < ntohl(msg_h->offset) || inlen == 0) { |
1345 |
warn("ML: BAD PACKET received from: %s:%d (len: %d < %d [=%08X] o:%d)",
|
1346 |
inet_ntoa(recv_addr.sin_addr), recv_addr.sin_port, |
1347 |
recvSize, inlen, inlen, ntohl(msg_h->offset)); |
1348 |
warn("ML: received %d: %02X %02X %02X %02X %02X %02X - %02X %02X %02X %02X %02X %02X", recvSize,
|
1349 |
msgbuf[0], msgbuf[1],msgbuf[2],msgbuf[3],msgbuf[4],msgbuf[5], |
1350 |
msgbuf[6],msgbuf[7],msgbuf[8],msgbuf[9],msgbuf[10],msgbuf[11]); |
1351 |
|
1352 |
return;
|
1353 |
} |
1354 |
|
1355 |
/* convert header from network to host order */
|
1356 |
msg_h->offset = ntohl(msg_h->offset); |
1357 |
msg_h->msg_length = ntohl(msg_h->msg_length); |
1358 |
msg_h->local_con_id = ntohl(msg_h->local_con_id); |
1359 |
msg_h->remote_con_id = ntohl(msg_h->remote_con_id); |
1360 |
msg_h->msg_seq_num = ntohl(msg_h->msg_seq_num); |
1361 |
|
1362 |
//verify minimum size
|
1363 |
if (recvSize < sizeof(struct msg_header)) { |
1364 |
info("UDP packet too small, can't be an ML packet");
|
1365 |
return;
|
1366 |
} |
1367 |
|
1368 |
//TODO add more verifications
|
1369 |
|
1370 |
bufptr += MSG_HEADER_SIZE + msg_h->len_mon_packet_hdr; |
1371 |
msg_size = recvSize - MSG_HEADER_SIZE - msg_h->len_mon_packet_hdr; |
1372 |
|
1373 |
//verify more fields
|
1374 |
if (msg_size < 0) { |
1375 |
info("Corrupted UDP packet received");
|
1376 |
return;
|
1377 |
} |
1378 |
|
1379 |
if(get_Recv_pkt_inf_cb != NULL) { |
1380 |
mon_pkt_inf msginfNow; |
1381 |
msginfNow.monitoringHeaderLen = msg_h->len_mon_packet_hdr; |
1382 |
msginfNow.monitoringHeader = msg_h->len_mon_packet_hdr ? &msgbuf[0] + MSG_HEADER_SIZE : NULL; |
1383 |
//TODO rethink this ...
|
1384 |
if(msg_h->msg_type == ML_CON_MSG) {
|
1385 |
struct conn_msg *c_msg = (struct conn_msg *) bufptr; |
1386 |
msginfNow.remote_socketID = &(c_msg->sock_id); |
1387 |
} |
1388 |
else if(msg_h->remote_con_id < 0 || |
1389 |
msg_h->remote_con_id >= CONNECTBUFSIZE || |
1390 |
connectbuf[msg_h->remote_con_id] == NULL) {
|
1391 |
error("ML: received pkg called with non existent connection\n");
|
1392 |
return;
|
1393 |
} else
|
1394 |
msginfNow.remote_socketID = &(connectbuf[msg_h->remote_con_id]->external_socketID); |
1395 |
msginfNow.buffer = bufptr; |
1396 |
msginfNow.bufSize = recvSize; |
1397 |
msginfNow.msgtype = msg_h->msg_type; |
1398 |
msginfNow.ttl = ttl; |
1399 |
msginfNow.dataID = msg_h->msg_seq_num; |
1400 |
msginfNow.offset = msg_h->offset; |
1401 |
msginfNow.datasize = msg_h->msg_length; |
1402 |
gettimeofday(&msginfNow.arrival_time, NULL);
|
1403 |
(get_Recv_pkt_inf_cb) ((void *) &msginfNow);
|
1404 |
} |
1405 |
|
1406 |
|
1407 |
switch(msg_h->msg_type) {
|
1408 |
case ML_CON_MSG:
|
1409 |
debug("ML: received conn pkg\n");
|
1410 |
recv_conn_msg(msg_h, bufptr, msg_size, &recv_addr); |
1411 |
break;
|
1412 |
#ifdef RTX
|
1413 |
case ML_NACK_MSG:
|
1414 |
debug("ML: received nack pkg\n");
|
1415 |
recv_nack_msg(msg_h, bufptr, msg_size); |
1416 |
break;
|
1417 |
#endif
|
1418 |
default:
|
1419 |
if(msg_h->msg_type < 127) { |
1420 |
debug("ML: received data pkg\n");
|
1421 |
recv_data_msg(msg_h, bufptr, msg_size); |
1422 |
break;
|
1423 |
} |
1424 |
debug("ML: unrecognised msg_type\n");
|
1425 |
break;
|
1426 |
} |
1427 |
} |
1428 |
|
1429 |
/*
|
1430 |
* compare the external IP address of two socketIDs
|
1431 |
*/
|
1432 |
int
|
1433 |
compare_external_address_socketIDs(socketID_handle sock1, socketID_handle sock2) |
1434 |
{ |
1435 |
if( sock1->external_addr.udpaddr.sin_addr.s_addr == sock2->external_addr.udpaddr.sin_addr.s_addr)
|
1436 |
return 0; |
1437 |
return 1; |
1438 |
} |
1439 |
|
1440 |
void try_stun();
|
1441 |
|
1442 |
/*
|
1443 |
* the timeout of the NAT traversal
|
1444 |
*/
|
1445 |
void nat_traversal_timeout(int fd, short event, void *arg) |
1446 |
{ |
1447 |
debug("X. NatTrTo %d\n", NAT_traversal);
|
1448 |
if (NAT_traversal == false) { |
1449 |
debug("ML: NAT traversal request re-send\n");
|
1450 |
if(receive_SocketID_cb)
|
1451 |
(receive_SocketID_cb) (&local_socketID, 2);
|
1452 |
try_stun(); |
1453 |
} |
1454 |
debug("X. NatTrTo\n");
|
1455 |
} |
1456 |
|
1457 |
//return IP address, or INADDR_NONE if can't resolve
|
1458 |
unsigned long resolve(const char *ipaddr) |
1459 |
{ |
1460 |
struct hostent *h = gethostbyname(ipaddr);
|
1461 |
if (!h) {
|
1462 |
error("ML: Unable to resolve host name %s\n", ipaddr);
|
1463 |
return INADDR_NONE;
|
1464 |
} |
1465 |
unsigned long *addr = (unsigned long *) (h->h_addr); |
1466 |
return *addr;
|
1467 |
} |
1468 |
|
1469 |
|
1470 |
/*
|
1471 |
* returns the file descriptor, or <0 on error. The ipaddr can be a null
|
1472 |
* pointer. Then all available ipaddr on the machine are choosen.
|
1473 |
*/
|
1474 |
int create_socket(const int port, const char *ipaddr) |
1475 |
{ |
1476 |
struct sockaddr_in udpaddr = {0}; |
1477 |
udpaddr.sin_family = AF_INET; |
1478 |
debug("X. create_socket %s, %d\n", ipaddr, port);
|
1479 |
if (ipaddr == NULL) { |
1480 |
/*
|
1481 |
* try to guess the local IP address
|
1482 |
*/
|
1483 |
const char *ipaddr_iface = mlAutodetectIPAddress(); |
1484 |
if (ipaddr_iface) {
|
1485 |
udpaddr.sin_addr.s_addr = inet_addr(ipaddr_iface); |
1486 |
} else {
|
1487 |
udpaddr.sin_addr.s_addr = INADDR_ANY; |
1488 |
} |
1489 |
} else {
|
1490 |
udpaddr.sin_addr.s_addr = inet_addr(ipaddr); |
1491 |
} |
1492 |
udpaddr.sin_port = htons(port); |
1493 |
|
1494 |
socketaddrgen udpgen; |
1495 |
memset(&udpgen,0,sizeof(socketaddrgen)); //this will be sent over the net, so set it to 0 |
1496 |
udpgen.udpaddr = udpaddr; |
1497 |
local_socketID.internal_addr = udpgen; |
1498 |
|
1499 |
socketfd = createSocket(port, ipaddr); |
1500 |
if (socketfd < 0){ |
1501 |
return socketfd;
|
1502 |
} |
1503 |
|
1504 |
struct event *ev;
|
1505 |
ev = event_new(base, socketfd, EV_READ | EV_PERSIST, recv_pkg, NULL);
|
1506 |
|
1507 |
event_add(ev, NULL);
|
1508 |
|
1509 |
try_stun(); |
1510 |
|
1511 |
return socketfd;
|
1512 |
} |
1513 |
|
1514 |
/*
|
1515 |
* try to figure out external IP using STUN, if defined
|
1516 |
*/
|
1517 |
void try_stun()
|
1518 |
{ |
1519 |
if (isStunDefined()) {
|
1520 |
/*
|
1521 |
* send the NAT traversal STUN request
|
1522 |
*/
|
1523 |
send_stun_request(socketfd, &stun_server); |
1524 |
|
1525 |
/*
|
1526 |
* enter a NAT traversal timeout that takes care of retransmission
|
1527 |
*/
|
1528 |
struct event *ev1;
|
1529 |
struct timeval timeout_value_NAT_traversal = NAT_TRAVERSAL_TIMEOUT;
|
1530 |
ev1 = evtimer_new(base, nat_traversal_timeout, NULL);
|
1531 |
event_add(ev1, &timeout_value_NAT_traversal); |
1532 |
|
1533 |
NAT_traversal = false;
|
1534 |
} else {
|
1535 |
/*
|
1536 |
* Assume we have accessibility and copy internal address to external one
|
1537 |
*/
|
1538 |
local_socketID.external_addr = local_socketID.internal_addr; |
1539 |
NAT_traversal = true; // @TODO: this is not really NAT traversal, but a flag that init is over |
1540 |
// callback to the upper layer indicating that the socketID is now
|
1541 |
// ready to use
|
1542 |
if(receive_SocketID_cb)
|
1543 |
(receive_SocketID_cb) (&local_socketID, 0); //success |
1544 |
} |
1545 |
} |
1546 |
|
1547 |
/**************************** END OF INTERNAL ***********************/
|
1548 |
|
1549 |
/**************************** MONL functions *************************/
|
1550 |
|
1551 |
int mlInit(bool recv_data_cb,struct timeval timeout_value,const int port,const char *ipaddr,const int stun_port,const char *stun_ipaddr,receive_localsocketID_cb local_socketID_cb,void *arg){ |
1552 |
|
1553 |
/*X*/ // fprintf(stderr,"MLINIT1 %s, %d, %s, %d\n", ipaddr, port, stun_ipaddr, stun_port); |
1554 |
base = (struct event_base *) arg;
|
1555 |
recv_data_callback = recv_data_cb; |
1556 |
mlSetRecvTimeout(timeout_value); |
1557 |
if (stun_ipaddr) {
|
1558 |
mlSetStunServer(stun_port, stun_ipaddr); |
1559 |
} else {
|
1560 |
|
1561 |
} |
1562 |
register_recv_localsocketID_cb(local_socketID_cb); |
1563 |
/*X*/ // fprintf(stderr,"MLINIT1\n"); |
1564 |
return create_socket(port, ipaddr);
|
1565 |
} |
1566 |
|
1567 |
void mlSetRateLimiterParams(int bucketsize, int drainrate, int maxQueueSize, int maxQueueSizeRTX, double maxTimeToHold) { |
1568 |
setOutputRateParams(bucketsize, drainrate); |
1569 |
setQueuesParams (maxQueueSize, maxQueueSizeRTX, maxTimeToHold); |
1570 |
} |
1571 |
|
1572 |
void mlSetVerbosity (int log_level) { |
1573 |
setLogLevel(log_level); |
1574 |
} |
1575 |
|
1576 |
/* register callbacks */
|
1577 |
void mlRegisterGetRecvPktInf(get_recv_pkt_inf_cb recv_pkt_inf_cb){
|
1578 |
|
1579 |
if (recv_pkt_inf_cb == NULL) { |
1580 |
error("ML: Register get_recv_pkt_inf_cb failed: NULL ptr \n");
|
1581 |
} else {
|
1582 |
get_Recv_pkt_inf_cb = recv_pkt_inf_cb; |
1583 |
} |
1584 |
} |
1585 |
|
1586 |
void mlRegisterGetSendPktInf(get_send_pkt_inf_cb send_pkt_inf_cb){
|
1587 |
|
1588 |
if (send_pkt_inf_cb == NULL) { |
1589 |
error("ML: Register get_send_pkt_inf_cb: NULL ptr \n");
|
1590 |
} else {
|
1591 |
get_Send_pkt_inf_cb = send_pkt_inf_cb; |
1592 |
} |
1593 |
} |
1594 |
|
1595 |
|
1596 |
void mlRegisterSetMonitoringHeaderPktCb(set_monitoring_header_pkt_cb monitoring_header_pkt_cb ){
|
1597 |
|
1598 |
if (monitoring_header_pkt_cb == NULL) { |
1599 |
error("ML: Register set_monitoring_header_pkt_cb: NULL ptr \n");
|
1600 |
} else {
|
1601 |
set_Monitoring_header_pkt_cb = monitoring_header_pkt_cb; |
1602 |
} |
1603 |
} |
1604 |
|
1605 |
void mlRegisterGetRecvDataInf(get_recv_data_inf_cb recv_data_inf_cb){
|
1606 |
|
1607 |
if (recv_data_inf_cb == NULL) { |
1608 |
error("ML: Register get_recv_data_inf_cb: NULL ptr \n");
|
1609 |
} else {
|
1610 |
get_Recv_data_inf_cb = recv_data_inf_cb; |
1611 |
} |
1612 |
} |
1613 |
|
1614 |
void mlRegisterGetSendDataInf(get_send_data_inf_cb send_data_inf_cb){
|
1615 |
|
1616 |
if (send_data_inf_cb == NULL) { |
1617 |
error("ML: Register get_send_data_inf_cb: NULL ptr \n");
|
1618 |
} else {
|
1619 |
get_Send_data_inf_cb = send_data_inf_cb; |
1620 |
} |
1621 |
} |
1622 |
|
1623 |
void mlRegisterSetMonitoringHeaderDataCb(set_monitoring_header_data_cb monitoring_header_data_cb){
|
1624 |
|
1625 |
if (monitoring_header_data_cb == NULL) { |
1626 |
error("ML: Register set_monitoring_header_data_cb : NULL ptr \n");
|
1627 |
} else {
|
1628 |
set_Monitoring_header_data_cb = monitoring_header_data_cb; |
1629 |
} |
1630 |
} |
1631 |
|
1632 |
void mlSetRecvTimeout(struct timeval timeout_value){ |
1633 |
|
1634 |
recv_timeout = timeout_value; |
1635 |
#ifdef RTX
|
1636 |
unsigned int total_usec = recv_timeout.tv_sec * 1000000 + recv_timeout.tv_usec; |
1637 |
total_usec = total_usec * LAST_PKT_RECV_TIMEOUT_FRACTION; |
1638 |
last_pkt_recv_timeout.tv_sec = total_usec / 1000000;
|
1639 |
last_pkt_recv_timeout.tv_usec = total_usec - last_pkt_recv_timeout.tv_sec * 1000000;
|
1640 |
fprintf(stderr,"Timeout for receiving message: %d : %d\n", recv_timeout.tv_sec, recv_timeout.tv_usec);
|
1641 |
fprintf(stderr,"Timeout for last pkt: %d : %d\n", last_pkt_recv_timeout.tv_sec, last_pkt_recv_timeout.tv_usec);
|
1642 |
#endif
|
1643 |
} |
1644 |
|
1645 |
int mlGetStandardTTL(socketID_handle socketID,uint8_t *ttl){
|
1646 |
|
1647 |
return getTTL(socketfd, ttl);
|
1648 |
|
1649 |
} |
1650 |
|
1651 |
socketID_handle mlGetLocalSocketID(int *errorstatus){
|
1652 |
|
1653 |
if (NAT_traversal == false) { |
1654 |
*errorstatus = 2;
|
1655 |
return NULL; |
1656 |
} |
1657 |
|
1658 |
*errorstatus = 0;
|
1659 |
return &local_socketID;
|
1660 |
|
1661 |
} |
1662 |
|
1663 |
|
1664 |
/**************************** END of MONL functions *************************/
|
1665 |
|
1666 |
/**************************** GENERAL functions *************************/
|
1667 |
|
1668 |
void mlRegisterRecvConnectionCb(receive_connection_cb recv_conn_cb){
|
1669 |
|
1670 |
if (recv_conn_cb == NULL) { |
1671 |
error("ML: Register receive_connection_cb: NULL ptr \n");
|
1672 |
}else {
|
1673 |
receive_Connection_cb = recv_conn_cb; |
1674 |
} |
1675 |
} |
1676 |
|
1677 |
void mlRegisterErrorConnectionCb(connection_failed_cb conn_failed){
|
1678 |
|
1679 |
if (conn_failed == NULL) { |
1680 |
error("ML: Register connection_failed_cb: NULL ptr \n");
|
1681 |
} else {
|
1682 |
failed_Connection_cb = conn_failed; |
1683 |
} |
1684 |
} |
1685 |
|
1686 |
void mlRegisterRecvDataCb(receive_data_cb data_cb,unsigned char msgtype){ |
1687 |
|
1688 |
if (msgtype > 126) { |
1689 |
|
1690 |
error |
1691 |
("ML: Could not register recv_data callback. Msgtype is greater then 126 \n");
|
1692 |
|
1693 |
} |
1694 |
|
1695 |
if (data_cb == NULL) { |
1696 |
|
1697 |
error("ML: Register receive data callback: NUll ptr \n ");
|
1698 |
|
1699 |
} else {
|
1700 |
|
1701 |
recvcbbuf[msgtype] = data_cb; |
1702 |
|
1703 |
} |
1704 |
|
1705 |
} |
1706 |
|
1707 |
void mlCloseSocket(socketID_handle socketID){
|
1708 |
|
1709 |
free(socketID); |
1710 |
|
1711 |
} |
1712 |
|
1713 |
void keepalive_fn(evutil_socket_t fd, short what, void *arg) { |
1714 |
socketID_handle peer = arg; |
1715 |
|
1716 |
int con_id = mlConnectionExist(peer, false); |
1717 |
if (con_id < 0 || connectbuf[con_id]->defaultSendParams.keepalive <= 0) { |
1718 |
/* Connection fell from under us or keepalive was disabled */
|
1719 |
free(arg); |
1720 |
return;
|
1721 |
} |
1722 |
|
1723 |
/* do what we gotta do */
|
1724 |
if ( connectbuf[con_id]->status == READY) {
|
1725 |
char keepaliveMsg[32] = ""; |
1726 |
sprintf(keepaliveMsg, "KEEPALIVE %d", connectbuf[con_id]->keepalive_seq++);
|
1727 |
send_msg(con_id, MSG_TYPE_ML_KEEPALIVE, keepaliveMsg, 1 + strlen(keepaliveMsg), false, |
1728 |
&(connectbuf[con_id]->defaultSendParams)); |
1729 |
} |
1730 |
|
1731 |
/* re-schedule */
|
1732 |
struct timeval t = { 0,0 }; |
1733 |
t.tv_sec = connectbuf[con_id]->defaultSendParams.keepalive; |
1734 |
if (connectbuf[con_id]->defaultSendParams.keepalive)
|
1735 |
event_base_once(base, -1, EV_TIMEOUT, keepalive_fn, peer, &t);
|
1736 |
} |
1737 |
|
1738 |
void setupKeepalive(int conn_id) { |
1739 |
/* Save the peer's address for us */
|
1740 |
socketID_handle peer = malloc(sizeof(socket_ID));
|
1741 |
memcpy(peer, &connectbuf[conn_id]->external_socketID, sizeof(socket_ID));
|
1742 |
|
1743 |
struct timeval t = { 0,0 }; |
1744 |
t.tv_sec = connectbuf[conn_id]->defaultSendParams.keepalive; |
1745 |
|
1746 |
if (connectbuf[conn_id]->defaultSendParams.keepalive)
|
1747 |
event_base_once(base, -1, EV_TIMEOUT, keepalive_fn, peer, &t);
|
1748 |
} |
1749 |
|
1750 |
/* connection functions */
|
1751 |
int mlOpenConnection(socketID_handle external_socketID,receive_connection_cb connection_cb,void *arg, const send_params defaultSendParams){ |
1752 |
|
1753 |
int con_id;
|
1754 |
if (external_socketID == NULL) { |
1755 |
error("ML: cannot open connection: one of the socketIDs is NULL\n");
|
1756 |
return -1; |
1757 |
} |
1758 |
if (NAT_traversal == false) { |
1759 |
error("ML: cannot open connection: NAT traversal for socketID still in progress\n");
|
1760 |
return -1; |
1761 |
} |
1762 |
if (connection_cb == NULL) { |
1763 |
error("ML: cannot open connection: connection_cb is NULL\n");
|
1764 |
return -1; |
1765 |
} |
1766 |
|
1767 |
// check if that connection already exist
|
1768 |
|
1769 |
con_id = mlConnectionExist(external_socketID, false);
|
1770 |
if (con_id >= 0) { |
1771 |
// overwrite defaultSendParams
|
1772 |
bool newKeepalive =
|
1773 |
connectbuf[con_id]->defaultSendParams.keepalive == 0 && defaultSendParams.keepalive != 0; |
1774 |
connectbuf[con_id]->defaultSendParams = defaultSendParams; |
1775 |
if (newKeepalive) setupKeepalive(con_id);
|
1776 |
// if so check if it is ready to use
|
1777 |
if (connectbuf[con_id]->status == READY) {
|
1778 |
// if so use the callback immediately
|
1779 |
(connection_cb) (con_id, arg); |
1780 |
|
1781 |
// otherwise just write the connection cb and the arg pointer
|
1782 |
// into the connection struct
|
1783 |
} else {
|
1784 |
struct receive_connection_cb_list *temp;
|
1785 |
temp = malloc(sizeof(struct receive_connection_cb_list)); |
1786 |
temp->next = NULL;
|
1787 |
temp->connection_cb = connection_cb; |
1788 |
temp->arg = arg; |
1789 |
if(connectbuf[con_id]->connection_last != NULL) { |
1790 |
connectbuf[con_id]->connection_last->next = temp; |
1791 |
connectbuf[con_id]->connection_last = temp; |
1792 |
} else
|
1793 |
connectbuf[con_id]->connection_last = connectbuf[con_id]->connection_head = temp; |
1794 |
} |
1795 |
return con_id;
|
1796 |
} |
1797 |
// make entry in connection_establishment array
|
1798 |
for (con_id = 0; con_id < CONNECTBUFSIZE; con_id++) { |
1799 |
if (connectbuf[con_id] == NULL) { |
1800 |
connectbuf[con_id] = (connect_data *) malloc(sizeof(connect_data));
|
1801 |
memset(connectbuf[con_id],0,sizeof(connect_data)); |
1802 |
connectbuf[con_id]->starttime = time(NULL);
|
1803 |
memcpy(&connectbuf[con_id]->external_socketID, external_socketID, sizeof(socket_ID));
|
1804 |
connectbuf[con_id]->pmtusize = DSLSLIM; |
1805 |
connectbuf[con_id]->timeout_event = NULL;
|
1806 |
connectbuf[con_id]->status = INVITE; |
1807 |
connectbuf[con_id]->seqnr = 0;
|
1808 |
connectbuf[con_id]->internal_connect = !compare_external_address_socketIDs(external_socketID, &local_socketID); |
1809 |
connectbuf[con_id]->connectionID = con_id; |
1810 |
|
1811 |
connectbuf[con_id]->connection_head = connectbuf[con_id]->connection_last = malloc(sizeof(struct receive_connection_cb_list)); |
1812 |
connectbuf[con_id]->connection_last->next = NULL;
|
1813 |
connectbuf[con_id]->connection_last->connection_cb = connection_cb; |
1814 |
connectbuf[con_id]->connection_last->arg = arg; |
1815 |
connectbuf[con_id]->external_connectionID = -1;
|
1816 |
|
1817 |
connectbuf[con_id]->defaultSendParams = defaultSendParams; |
1818 |
if (defaultSendParams.keepalive) setupKeepalive(con_id);
|
1819 |
break;
|
1820 |
} |
1821 |
} //end of for
|
1822 |
|
1823 |
if (con_id == CONNECTBUFSIZE) {
|
1824 |
error("ML: Could not open connection: connection buffer full\n");
|
1825 |
return -1; |
1826 |
} |
1827 |
|
1828 |
// create and send a connection message
|
1829 |
info("ML:Sending INVITE to %s (lconn:%d)\n",conid_to_string(con_id), con_id);
|
1830 |
send_conn_msg_with_pmtu_discovery(con_id, connectbuf[con_id]->pmtusize, INVITE); |
1831 |
|
1832 |
return con_id;
|
1833 |
|
1834 |
} |
1835 |
|
1836 |
void mlCloseConnection(const int connectionID){ |
1837 |
|
1838 |
// remove it from the connection array
|
1839 |
if(connectbuf[connectionID]) {
|
1840 |
if(connectbuf[connectionID]->ctrl_msg_buf) {
|
1841 |
free(connectbuf[connectionID]->ctrl_msg_buf); |
1842 |
} |
1843 |
// remove related events
|
1844 |
if (connectbuf[connectionID]->timeout_event) {
|
1845 |
event_del(connectbuf[connectionID]->timeout_event); |
1846 |
event_free(connectbuf[connectionID]->timeout_event); |
1847 |
connectbuf[connectionID]->timeout_event = NULL;
|
1848 |
} |
1849 |
free(connectbuf[connectionID]); |
1850 |
connectbuf[connectionID] = NULL;
|
1851 |
} |
1852 |
|
1853 |
} |
1854 |
|
1855 |
void mlSendData(const int connectionID,char *sendbuf,int bufsize,unsigned char msgtype,send_params *sParams){ |
1856 |
|
1857 |
if (connectionID < 0) { |
1858 |
error("ML: send data failed: connectionID does not exist\n");
|
1859 |
return;
|
1860 |
} |
1861 |
|
1862 |
if (connectbuf[connectionID] == NULL) { |
1863 |
error("ML: send data failed: connectionID does not exist\n");
|
1864 |
return;
|
1865 |
} |
1866 |
if (connectbuf[connectionID]->status != READY) {
|
1867 |
error("ML: send data failed: connection is not active\n");
|
1868 |
return;
|
1869 |
} |
1870 |
|
1871 |
if (sParams == NULL) { |
1872 |
sParams = &(connectbuf[connectionID]->defaultSendParams); |
1873 |
} |
1874 |
|
1875 |
send_msg(connectionID, msgtype, sendbuf, bufsize, false, sParams);
|
1876 |
|
1877 |
} |
1878 |
|
1879 |
/* transmit data functions */
|
1880 |
int mlSendAllData(const int connectionID,send_all_data_container *container,int nr_entries,unsigned char msgtype,send_params *sParams){ |
1881 |
|
1882 |
if (nr_entries < 1 || nr_entries > 5) { |
1883 |
|
1884 |
error |
1885 |
("ML : sendALlData : nr_enties is not between 1 and 5 \n ");
|
1886 |
return 0; |
1887 |
|
1888 |
} else {
|
1889 |
|
1890 |
if (nr_entries == 1) { |
1891 |
|
1892 |
mlSendData(connectionID, container->buffer_1, |
1893 |
container->length_1, msgtype, sParams); |
1894 |
|
1895 |
return 1; |
1896 |
|
1897 |
} else if (nr_entries == 2) { |
1898 |
|
1899 |
int buflen = container->length_1 + container->length_2;
|
1900 |
char buf[buflen];
|
1901 |
memcpy(buf, container->buffer_1, container->length_1); |
1902 |
memcpy(&buf[container->length_1], container->buffer_2, |
1903 |
container->length_2); |
1904 |
mlSendData(connectionID, buf, buflen, msgtype, sParams); |
1905 |
|
1906 |
return 1; |
1907 |
|
1908 |
} else if (nr_entries == 3) { |
1909 |
|
1910 |
int buflen =
|
1911 |
container->length_1 + container->length_2 + |
1912 |
container->length_3; |
1913 |
char buf[buflen];
|
1914 |
memcpy(buf, container->buffer_1, container->length_1); |
1915 |
memcpy(&buf[container->length_1], container->buffer_2, |
1916 |
container->length_2); |
1917 |
memcpy(&buf[container->length_2], container->buffer_3, |
1918 |
container->length_3); |
1919 |
mlSendData(connectionID, buf, buflen, msgtype, sParams); |
1920 |
|
1921 |
|
1922 |
return 1; |
1923 |
|
1924 |
} else if (nr_entries == 4) { |
1925 |
|
1926 |
int buflen =
|
1927 |
container->length_1 + container->length_2 + |
1928 |
container->length_3 + container->length_4; |
1929 |
char buf[buflen];
|
1930 |
memcpy(buf, container->buffer_1, container->length_1); |
1931 |
memcpy(&buf[container->length_1], container->buffer_2, |
1932 |
container->length_2); |
1933 |
memcpy(&buf[container->length_2], container->buffer_3, |
1934 |
container->length_3); |
1935 |
memcpy(&buf[container->length_3], container->buffer_4, |
1936 |
container->length_4); |
1937 |
mlSendData(connectionID, buf, buflen, msgtype, sParams); |
1938 |
|
1939 |
return 1; |
1940 |
|
1941 |
} else {
|
1942 |
|
1943 |
int buflen =
|
1944 |
container->length_1 + container->length_2 + |
1945 |
container->length_3 + container->length_4 + |
1946 |
container->length_5; |
1947 |
char buf[buflen];
|
1948 |
memcpy(buf, container->buffer_1, container->length_1); |
1949 |
memcpy(&buf[container->length_1], container->buffer_2, |
1950 |
container->length_2); |
1951 |
memcpy(&buf[container->length_2], container->buffer_3, |
1952 |
container->length_3); |
1953 |
memcpy(&buf[container->length_3], container->buffer_4, |
1954 |
container->length_4); |
1955 |
memcpy(&buf[container->length_4], container->buffer_5, |
1956 |
container->length_5); |
1957 |
mlSendData(connectionID, buf, buflen, msgtype, sParams); |
1958 |
|
1959 |
return 1; |
1960 |
} |
1961 |
|
1962 |
} |
1963 |
|
1964 |
} |
1965 |
|
1966 |
int mlRecvData(const int connectionID,char *recvbuf,int *bufsize,recv_params *rParams){ |
1967 |
|
1968 |
//TODO yet to be converted
|
1969 |
return 0; |
1970 |
#if 0
|
1971 |
if (rParams == NULL) {
|
1972 |
error("ML: recv_data failed: recv_params is a NULL ptr\n");
|
1973 |
return 0;
|
1974 |
} else {
|
1975 |
|
1976 |
info("ML: recv data called \n");
|
1977 |
|
1978 |
int i = 0;
|
1979 |
int returnValue = 0;
|
1980 |
double timeout = (double) recv_timeout.tv_sec;
|
1981 |
time_t endtime = time(NULL);
|
1982 |
|
1983 |
for (i = 0; i < RECVDATABUFSIZE; i++) {
|
1984 |
|
1985 |
if (recvdatabuf[i] != NULL) {
|
1986 |
|
1987 |
if (recvdatabuf[i]->connectionID == connectionID) {
|
1988 |
|
1989 |
info("ML: recv data has entry \n");
|
1990 |
|
1991 |
double timepass = difftime(endtime, recvdatabuf[i]->starttime);
|
1992 |
|
1993 |
// check if the specified connection has data and it
|
1994 |
// is complete
|
1995 |
// check the data seqnr
|
1996 |
// if(connectionID == recvdatabuf[i]->connectionID &&
|
1997 |
// 1 == recvdatabuf[i]->status){
|
1998 |
|
1999 |
if (1 == recvdatabuf[i]->status) {
|
2000 |
|
2001 |
// info("transmissionHandler: recv_data set is
|
2002 |
// complete \n" );
|
2003 |
|
2004 |
// debug("debud \n");
|
2005 |
|
2006 |
// exchange the pointers
|
2007 |
int buffersize = 0;
|
2008 |
buffersize = recvdatabuf[i]->bufsize;
|
2009 |
*bufsize = buffersize;
|
2010 |
// recvbuf = recvdatabuf[i]->recvbuf;
|
2011 |
|
2012 |
// info("buffersize %d \n",buffersize);
|
2013 |
memcpy(recvbuf, recvdatabuf[i]->recvbuf,
|
2014 |
buffersize);
|
2015 |
// debug(" recvbuf %s \n",recvbuf );
|
2016 |
|
2017 |
// double nrMissFrags =
|
2018 |
// (double) recvdatabuf[i]->nrFragments /
|
2019 |
// (double) recvdatabuf[i]->recvFragments;
|
2020 |
// int nrMissingFragments = (int) ceil(nrMissFrags);
|
2021 |
|
2022 |
// rParams->nrMissingFragments = nrMissingFragments;
|
2023 |
// rParams->nrFragments = recvdatabuf[i]->nrFragments;
|
2024 |
rParams->msgtype = recvdatabuf[i]->msgtype;
|
2025 |
rParams->connectionID =
|
2026 |
recvdatabuf[i]->connectionID;
|
2027 |
|
2028 |
// break from the loop
|
2029 |
// debug(" recvbuf %s \n ",recvbuf);
|
2030 |
|
2031 |
// double nrMissFrags =
|
2032 |
// (double)recvdatabuf[i]->nrFragments /
|
2033 |
// (double)recvdatabuf[i]->recvFragments;
|
2034 |
// int nrMissingFragments =
|
2035 |
// (int)ceil(nrMissFrags);
|
2036 |
|
2037 |
if(get_Recv_data_inf_cb != NULL) {
|
2038 |
mon_data_inf recv_data_inf;
|
2039 |
|
2040 |
recv_data_inf.remote_socketID = &(connectbuf[connectionID]->external_socketID);
|
2041 |
recv_data_inf.buffer = recvdatabuf[i]->recvbuf;
|
2042 |
recv_data_inf.bufSize = recvdatabuf[i]->bufsize;
|
2043 |
recv_data_inf.msgtype = recvdatabuf[i]->msgtype;
|
2044 |
// recv_data_inf.monitoringHeaderType = recvdatabuf[i]->monitoringHeaderType;
|
2045 |
// recv_data_inf.monitoringDataHeader = recvdatabuf[i]->monitoringDataHeader;
|
2046 |
gettimeofday(&recv_data_inf.arrival_time, NULL);
|
2047 |
recv_data_inf.firstPacketArrived = recvdatabuf[i]->firstPacketArrived;
|
2048 |
recv_data_inf.nrMissingFragments = nrMissingFragments;
|
2049 |
recv_data_inf.nrFragments = recvdatabuf[i]->nrFragments;
|
2050 |
recv_data_inf.priority = false;
|
2051 |
recv_data_inf.padding = false;
|
2052 |
recv_data_inf.confirmation = false;
|
2053 |
recv_data_inf.reliable = false;
|
2054 |
|
2055 |
// send data recv callback to monitoring module
|
2056 |
|
2057 |
(get_Recv_data_inf_cb) ((void *) &recv_data_inf);
|
2058 |
}
|
2059 |
|
2060 |
|
2061 |
// free the allocated memory
|
2062 |
free(recvdatabuf[i]);
|
2063 |
recvdatabuf[i] = NULL;
|
2064 |
|
2065 |
returnValue = 1;
|
2066 |
break;
|
2067 |
|
2068 |
}
|
2069 |
|
2070 |
if (recvdatabuf[i] != NULL) {
|
2071 |
|
2072 |
if (timepass > timeout) {
|
2073 |
|
2074 |
info("ML: recv_data timeout called \n");
|
2075 |
|
2076 |
// some data about the missing chunks should
|
2077 |
// be added here
|
2078 |
// exchange the pointers
|
2079 |
int buffersize = 0;
|
2080 |
buffersize = recvdatabuf[i]->bufsize;
|
2081 |
*bufsize = buffersize;
|
2082 |
// recvbuf = recvdatabuf[i]->recvbuf;
|
2083 |
|
2084 |
double nrMissFrags =
|
2085 |
(double) recvdatabuf[i]->nrFragments /
|
2086 |
(double) recvdatabuf[i]->recvFragments;
|
2087 |
int nrMissingFragments =
|
2088 |
(int) ceil(nrMissFrags);
|
2089 |
|
2090 |
// debug(" recvbuf %s \n",recvbuf );
|
2091 |
|
2092 |
memcpy(recvbuf, recvdatabuf[i]->recvbuf,
|
2093 |
buffersize);
|
2094 |
|
2095 |
rParams->nrMissingFragments =
|
2096 |
nrMissingFragments;
|
2097 |
rParams->nrFragments =
|
2098 |
recvdatabuf[i]->nrFragments;
|
2099 |
rParams->msgtype = recvdatabuf[i]->msgtype;
|
2100 |
rParams->connectionID =
|
2101 |
recvdatabuf[i]->connectionID;
|
2102 |
|
2103 |
if(get_Recv_data_inf_cb != NULL) {
|
2104 |
mon_data_inf recv_data_inf;
|
2105 |
|
2106 |
recv_data_inf.remote_socketID = &(connectbuf[connectionID]->external_socketID);
|
2107 |
recv_data_inf.buffer = recvdatabuf[i]->recvbuf;
|
2108 |
recv_data_inf.bufSize = recvdatabuf[i]->bufsize;
|
2109 |
recv_data_inf.msgtype = recvdatabuf[i]->msgtype;
|
2110 |
recv_data_inf.monitoringHeaderType = recvdatabuf[i]->monitoringHeaderType;
|
2111 |
recv_data_inf.monitoringDataHeader = recvdatabuf[i]->monitoringDataHeader;
|
2112 |
gettimeofday(&recv_data_inf.arrival_time, NULL);
|
2113 |
recv_data_inf.firstPacketArrived = recvdatabuf[i]->firstPacketArrived;
|
2114 |
recv_data_inf.nrMissingFragments = nrMissingFragments;
|
2115 |
recv_data_inf.nrFragments = recvdatabuf[i]->nrFragments;
|
2116 |
recv_data_inf.priority = false;
|
2117 |
recv_data_inf.padding = false;
|
2118 |
recv_data_inf.confirmation = false;
|
2119 |
recv_data_inf.reliable = false;
|
2120 |
|
2121 |
// send data recv callback to monitoring module
|
2122 |
|
2123 |
(get_Recv_data_inf_cb) ((void *) &recv_data_inf);
|
2124 |
}
|
2125 |
|
2126 |
// free the allocated memory
|
2127 |
free(recvdatabuf[i]);
|
2128 |
recvdatabuf[i] = NULL;
|
2129 |
|
2130 |
returnValue = 1;
|
2131 |
break;
|
2132 |
|
2133 |
}
|
2134 |
}
|
2135 |
|
2136 |
}
|
2137 |
|
2138 |
}
|
2139 |
// debug("2 recvbuf %s \n ",recvbuf);
|
2140 |
}
|
2141 |
return returnValue;
|
2142 |
}
|
2143 |
#endif
|
2144 |
|
2145 |
} |
2146 |
|
2147 |
int mlSocketIDToString(socketID_handle socketID,char* socketID_string, size_t len){ |
2148 |
|
2149 |
char internal_addr[INET_ADDRSTRLEN];
|
2150 |
char external_addr[INET_ADDRSTRLEN];
|
2151 |
|
2152 |
assert(socketID); |
2153 |
|
2154 |
inet_ntop(AF_INET, &(socketID->internal_addr.udpaddr.sin_addr.s_addr), internal_addr, INET_ADDRSTRLEN); |
2155 |
inet_ntop(AF_INET, &(socketID->external_addr.udpaddr.sin_addr.s_addr), external_addr, INET_ADDRSTRLEN); |
2156 |
|
2157 |
snprintf(socketID_string,len,"%s:%d-%s:%d", internal_addr, ntohs(socketID->internal_addr.udpaddr.sin_port),
|
2158 |
external_addr, ntohs(socketID->external_addr.udpaddr.sin_port)); |
2159 |
return 0; |
2160 |
|
2161 |
} |
2162 |
|
2163 |
int mlStringToSocketID(const char* socketID_string, socketID_handle socketID){ |
2164 |
|
2165 |
//@TODO add checks against malformed string
|
2166 |
char external_addr[INET_ADDRSTRLEN];
|
2167 |
int external_port;
|
2168 |
char internal_addr[INET_ADDRSTRLEN];
|
2169 |
int internal_port;
|
2170 |
|
2171 |
char *pch;
|
2172 |
char *s = strdup(socketID_string);
|
2173 |
|
2174 |
//replace ':' with a blank
|
2175 |
pch=strchr(s,':');
|
2176 |
while (pch!=NULL){ |
2177 |
*pch = ' ';
|
2178 |
pch=strchr(pch+1,':'); |
2179 |
} |
2180 |
pch=strchr(s,'-');
|
2181 |
if(pch) *pch = ' '; |
2182 |
|
2183 |
sscanf(s,"%s %d %s %d", internal_addr, &internal_port,
|
2184 |
external_addr, &external_port); |
2185 |
|
2186 |
//set structure to 0, we initialize each byte, since it will be sent on the net later
|
2187 |
memset(socketID, 0, sizeof(struct _socket_ID)); |
2188 |
|
2189 |
if(inet_pton(AF_INET, internal_addr, &(socketID->internal_addr.udpaddr.sin_addr)) == 0) |
2190 |
return EINVAL;
|
2191 |
socketID->internal_addr.udpaddr.sin_family = AF_INET; |
2192 |
socketID->internal_addr.udpaddr.sin_port = htons(internal_port); |
2193 |
|
2194 |
|
2195 |
if(inet_pton(AF_INET, external_addr, &(socketID->external_addr.udpaddr.sin_addr)) ==0) |
2196 |
return EINVAL;
|
2197 |
socketID->external_addr.udpaddr.sin_family = AF_INET; |
2198 |
socketID->external_addr.udpaddr.sin_port = htons(external_port); |
2199 |
|
2200 |
free(s); |
2201 |
return 0; |
2202 |
|
2203 |
} |
2204 |
|
2205 |
int mlGetConnectionStatus(int connectionID){ |
2206 |
|
2207 |
if(connectbuf[connectionID])
|
2208 |
return connectbuf[connectionID]->status == READY;
|
2209 |
return -1; |
2210 |
|
2211 |
} |
2212 |
|
2213 |
|
2214 |
int mlConnectionExist(socketID_handle socketID, bool ready){ |
2215 |
|
2216 |
/*
|
2217 |
* check if another connection for the external connectionID exist
|
2218 |
* that was established \ within the last 2 seconds
|
2219 |
*/
|
2220 |
int i;
|
2221 |
for (i = 0; i < CONNECTBUFSIZE; i++) |
2222 |
if (connectbuf[i] != NULL) |
2223 |
if (mlCompareSocketIDs(&(connectbuf[i]->external_socketID), socketID) == 0) { |
2224 |
if (ready) return (connectbuf[i]->status == READY ? i : -1);; |
2225 |
return i;
|
2226 |
} |
2227 |
|
2228 |
return -1; |
2229 |
|
2230 |
} |
2231 |
|
2232 |
//Added by Robert Birke as comodity functions
|
2233 |
|
2234 |
//int mlPrintSocketID(socketID_handle socketID) {
|
2235 |
// char str[SOCKETID_STRING_SIZE];
|
2236 |
// mlSocketIDToString(socketID, str, sizeof(str));
|
2237 |
// printf(stderr,"int->%s<-ext\n",str);
|
2238 |
//}
|
2239 |
|
2240 |
/*
|
2241 |
* hash code of a socketID
|
2242 |
* TODO might think of a better way
|
2243 |
*/
|
2244 |
int mlHashSocketID(socketID_handle sock) {
|
2245 |
//assert(sock);
|
2246 |
return sock->internal_addr.udpaddr.sin_port +
|
2247 |
sock->external_addr.udpaddr.sin_port; |
2248 |
} |
2249 |
|
2250 |
int mlCompareSocketIDs(socketID_handle sock1, socketID_handle sock2) {
|
2251 |
|
2252 |
assert(sock1 && sock2); |
2253 |
|
2254 |
/*
|
2255 |
* compare internal addr
|
2256 |
*/
|
2257 |
if(sock1 == NULL || sock2 == NULL) |
2258 |
return 1; |
2259 |
|
2260 |
if (sock1->internal_addr.udpaddr.sin_addr.s_addr !=
|
2261 |
sock2->internal_addr.udpaddr.sin_addr.s_addr) |
2262 |
return 1; |
2263 |
|
2264 |
if (sock1->internal_addr.udpaddr.sin_port !=
|
2265 |
sock2->internal_addr.udpaddr.sin_port) |
2266 |
return 1; |
2267 |
|
2268 |
/*
|
2269 |
* compare external addr
|
2270 |
*/
|
2271 |
if (sock1->external_addr.udpaddr.sin_addr.s_addr !=
|
2272 |
sock2->external_addr.udpaddr.sin_addr.s_addr) |
2273 |
return 1; |
2274 |
|
2275 |
if (sock1->external_addr.udpaddr.sin_port !=
|
2276 |
sock2->external_addr.udpaddr.sin_port) |
2277 |
return 1; |
2278 |
|
2279 |
return 0; |
2280 |
} |
2281 |
|
2282 |
int mlCompareSocketIDsByPort(socketID_handle sock1, socketID_handle sock2)
|
2283 |
{ |
2284 |
if(sock1 == NULL || sock2 == NULL) |
2285 |
return 1; |
2286 |
|
2287 |
if (sock1->internal_addr.udpaddr.sin_port !=
|
2288 |
sock2->internal_addr.udpaddr.sin_port) |
2289 |
return 1; |
2290 |
|
2291 |
if (sock1->external_addr.udpaddr.sin_port !=
|
2292 |
sock2->external_addr.udpaddr.sin_port) |
2293 |
return 1; |
2294 |
return 0; |
2295 |
} |
2296 |
|
2297 |
int mlGetPathMTU(int ConnectionId) { |
2298 |
if(ConnectionId < 0 || ConnectionId >= CONNECTBUFSIZE) |
2299 |
return -1; |
2300 |
if (connectbuf[ConnectionId] != NULL) |
2301 |
return connectbuf[ConnectionId]->pmtusize;
|
2302 |
return -1; |
2303 |
} |
2304 |
|
2305 |
/**************************** END of GENERAL functions *************************/
|
2306 |
|
2307 |
/**************************** NAT functions *************************/
|
2308 |
|
2309 |
/* setter */
|
2310 |
void mlSetStunServer(const int port,const char *ipaddr){ |
2311 |
|
2312 |
stun_server.sin_family = AF_INET; |
2313 |
if (ipaddr == NULL) |
2314 |
stun_server.sin_addr.s_addr = htonl(INADDR_NONE); |
2315 |
else
|
2316 |
stun_server.sin_addr.s_addr = resolve(ipaddr); |
2317 |
stun_server.sin_port = htons(port); |
2318 |
|
2319 |
} |
2320 |
|
2321 |
int mlGetExternalIP(char* external_addr){ |
2322 |
|
2323 |
socketaddrgen udpgen; |
2324 |
struct sockaddr_in udpaddr;
|
2325 |
|
2326 |
udpgen = local_socketID.external_addr; |
2327 |
udpaddr = udpgen.udpaddr; |
2328 |
|
2329 |
inet_ntop(AF_INET, &(udpaddr.sin_addr), external_addr, |
2330 |
INET_ADDRSTRLEN); |
2331 |
|
2332 |
if (external_addr == NULL) { |
2333 |
|
2334 |
return -1; |
2335 |
|
2336 |
} else {
|
2337 |
|
2338 |
return 0; |
2339 |
|
2340 |
} |
2341 |
|
2342 |
} |
2343 |
|
2344 |
/**************************** END of NAT functions *************************/
|