ml / ml.c @ d5b9b14d
History | View | Annotate | Download (57.1 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 <arpa/inet.h> |
36 |
#include <netinet/in.h> |
37 |
#include <sys/socket.h> |
38 |
#include <fcntl.h> |
39 |
#include <event2/event.h> |
40 |
#include <stdlib.h> |
41 |
#include <unistd.h> |
42 |
#include <stdio.h> |
43 |
#include <stddef.h> |
44 |
#include <stdint.h> |
45 |
#include <string.h> |
46 |
#include <sys/types.h> |
47 |
#include <arpa/inet.h> |
48 |
#include <netdb.h> |
49 |
#include <errno.h> |
50 |
#include <time.h> |
51 |
#include <math.h> |
52 |
#include "msg_types.h" |
53 |
#include "util/udpSocket.h" |
54 |
#include "util/stun.h" |
55 |
#include "transmissionHandler.h" |
56 |
|
57 |
#define LOG_MODULE "[ml] " |
58 |
#include "ml_log.h" |
59 |
|
60 |
/**************************** START OF INTERNALS ***********************/
|
61 |
|
62 |
/*
|
63 |
* a pointer to a libevent instance
|
64 |
*/
|
65 |
struct event_base *base;
|
66 |
|
67 |
/*
|
68 |
* define the nr of connections the messaging layer can handle
|
69 |
*/
|
70 |
#define CONNECTBUFSIZE 10000 |
71 |
/*
|
72 |
* define the nr of data that can be received parallel
|
73 |
*/
|
74 |
#define RECVDATABUFSIZE 10000 |
75 |
/*
|
76 |
* define an array for message multiplexing
|
77 |
*/
|
78 |
#define MSGMULTIPLEXSIZE 127 |
79 |
|
80 |
|
81 |
/*
|
82 |
* timeout before thinking that the STUN server can't be connected
|
83 |
*/
|
84 |
#define NAT_TRAVERSAL_TIMEOUT { 1, 0 } |
85 |
|
86 |
/*
|
87 |
* timeout before thinking of an mtu problem (check MAX_TRIALS as well)
|
88 |
*/
|
89 |
#define PMTU_TIMEOUT { 0, 250000 } |
90 |
|
91 |
/*
|
92 |
* retry sending connection messages this many times before reducing pmtu
|
93 |
*/
|
94 |
#define MAX_TRIALS 3 |
95 |
|
96 |
/*
|
97 |
* default timeout value between the first and the last received packet of a message
|
98 |
*/
|
99 |
#define RECV_TIMEOUT_DEFAULT { 2, 0 } |
100 |
|
101 |
/*
|
102 |
* global variables
|
103 |
*/
|
104 |
/*
|
105 |
* define a buffer of pointers to connect structures
|
106 |
*/
|
107 |
connect_data *connectbuf[CONNECTBUFSIZE]; |
108 |
|
109 |
/*
|
110 |
* define a pointer buffer with pointers to recv_data structures
|
111 |
*/
|
112 |
recvdata *recvdatabuf[RECVDATABUFSIZE]; |
113 |
|
114 |
/*
|
115 |
* define a pointer buffer for message multiplexing
|
116 |
*/
|
117 |
receive_data_cb recvcbbuf[MSGMULTIPLEXSIZE]; |
118 |
|
119 |
/*
|
120 |
* stun server address
|
121 |
*/
|
122 |
struct sockaddr_in stun_server;
|
123 |
|
124 |
/*
|
125 |
* receive timeout
|
126 |
*/
|
127 |
static struct timeval recv_timeout = RECV_TIMEOUT_DEFAULT; |
128 |
|
129 |
/*
|
130 |
* boolean NAT traversal successful if true
|
131 |
*/
|
132 |
boolean NAT_traversal; |
133 |
|
134 |
/*
|
135 |
* file descriptor for local socket
|
136 |
*/
|
137 |
evutil_socket_t socketfd; |
138 |
|
139 |
/*
|
140 |
* local socketID
|
141 |
*/
|
142 |
socket_ID local_socketID; |
143 |
|
144 |
socketID_handle loc_socketID = &local_socketID; |
145 |
|
146 |
/*
|
147 |
* callback function pointers
|
148 |
*/
|
149 |
/*
|
150 |
* monitoring module callbacks
|
151 |
*/
|
152 |
get_recv_pkt_inf_cb get_Recv_pkt_inf_cb = NULL;
|
153 |
get_send_pkt_inf_cb get_Send_pkt_inf_cb = NULL;
|
154 |
set_monitoring_header_pkt_cb set_Monitoring_header_pkt_cb = NULL;
|
155 |
get_recv_data_inf_cb get_Recv_data_inf_cb = NULL;
|
156 |
get_send_data_inf_cb get_Send_data_inf_cb = NULL;
|
157 |
set_monitoring_header_data_cb set_Monitoring_header_data_cb = NULL;
|
158 |
/*
|
159 |
* connection callbacks
|
160 |
*/
|
161 |
receive_connection_cb receive_Connection_cb = NULL;
|
162 |
connection_failed_cb failed_Connection_cb = NULL;
|
163 |
/*
|
164 |
* local socketID callback
|
165 |
*/
|
166 |
receive_localsocketID_cb receive_SocketID_cb; |
167 |
|
168 |
/*
|
169 |
* boolean that defines if received data is transmitted to the upper layer
|
170 |
* via callback or via upper layer polling
|
171 |
*/
|
172 |
boolean recv_data_callback; |
173 |
|
174 |
/*
|
175 |
* helper function to get rid of a warning
|
176 |
*/
|
177 |
int min(int a, int b) { |
178 |
if (a > b) return b; |
179 |
return a;
|
180 |
} |
181 |
|
182 |
void register_recv_localsocketID_cb(receive_localsocketID_cb local_socketID_cb)
|
183 |
{ |
184 |
if (local_socketID_cb == NULL) |
185 |
error("ML : Register receive_localsocketID_cb: NULL ptr \n");
|
186 |
else
|
187 |
receive_SocketID_cb = local_socketID_cb; |
188 |
} |
189 |
|
190 |
|
191 |
//void keep_connection_alive(const int connectionID)
|
192 |
//{
|
193 |
//
|
194 |
// // to be done with the NAT traversal
|
195 |
// // send a message over the wire
|
196 |
// printf("\n");
|
197 |
//
|
198 |
//}
|
199 |
|
200 |
void unsetStunServer()
|
201 |
{ |
202 |
stun_server.sin_addr.s_addr = 0;
|
203 |
} |
204 |
|
205 |
bool isStunDefined()
|
206 |
{ |
207 |
return stun_server.sin_addr.s_addr;
|
208 |
} |
209 |
|
210 |
void send_msg(int con_id, int msg_type, char* msg, int msg_len, bool truncable, send_params * sParams) { |
211 |
socketaddrgen udpgen; |
212 |
bool retry;
|
213 |
int pkt_len, offset;
|
214 |
struct iovec iov[4]; |
215 |
|
216 |
char h_pkt[MON_HEADER_SPACE];
|
217 |
char h_data[MON_HEADER_SPACE];
|
218 |
|
219 |
struct msg_header msg_h;
|
220 |
|
221 |
char str[1000]; |
222 |
mlSocketIDToString(&connectbuf[con_id]->external_socketID,str,999);
|
223 |
debug("ML: send_msg to conID:%d extID:%d, %s\n",con_id,connectbuf[con_id]->external_connectionID,str);
|
224 |
|
225 |
iov[0].iov_base = &msg_h;
|
226 |
iov[0].iov_len = MSG_HEADER_SIZE;
|
227 |
|
228 |
msg_h.local_con_id = con_id; |
229 |
msg_h.remote_con_id = connectbuf[con_id]->external_connectionID; |
230 |
msg_h.msg_type = msg_type; |
231 |
msg_h.msg_seq_num = connectbuf[con_id]->seqnr++; |
232 |
|
233 |
|
234 |
iov[1].iov_len = iov[2].iov_len = 0; |
235 |
iov[1].iov_base = h_pkt;
|
236 |
iov[2].iov_base = h_data;
|
237 |
|
238 |
|
239 |
if (connectbuf[con_id]->internal_connect)
|
240 |
udpgen = connectbuf[con_id]->external_socketID.internal_addr; |
241 |
else
|
242 |
udpgen = connectbuf[con_id]->external_socketID.external_addr; |
243 |
|
244 |
do{
|
245 |
offset = 0;
|
246 |
retry = false;
|
247 |
// Monitoring layer hook
|
248 |
if(set_Monitoring_header_data_cb != NULL) { |
249 |
iov[2].iov_len = ((set_Monitoring_header_data_cb) (&(connectbuf[con_id]->external_socketID), msg_type));
|
250 |
} |
251 |
msg_h.len_mon_data_hdr = iov[2].iov_len;
|
252 |
|
253 |
if(get_Send_data_inf_cb != NULL && iov[2].iov_len != 0) { |
254 |
mon_data_inf sd_data_inf; |
255 |
|
256 |
sd_data_inf.remote_socketID = &(connectbuf[con_id]->external_socketID); |
257 |
sd_data_inf.buffer = msg; |
258 |
sd_data_inf.bufSize = msg_len; |
259 |
sd_data_inf.msgtype = msg_type; |
260 |
sd_data_inf.monitoringDataHeader = iov[2].iov_base;
|
261 |
sd_data_inf.monitoringDataHeaderLen = iov[2].iov_len;
|
262 |
sd_data_inf.priority = sParams->priority; |
263 |
sd_data_inf.padding = sParams->padding; |
264 |
sd_data_inf.confirmation = sParams->confirmation; |
265 |
sd_data_inf.reliable = sParams->reliable; |
266 |
memset(&sd_data_inf.arrival_time, 0, sizeof(struct timeval)); |
267 |
|
268 |
(get_Send_data_inf_cb) ((void *) &sd_data_inf);
|
269 |
} |
270 |
|
271 |
do {
|
272 |
if(set_Monitoring_header_pkt_cb != NULL) { |
273 |
iov[1].iov_len = (set_Monitoring_header_pkt_cb) (&(connectbuf[con_id]->external_socketID), msg_type);
|
274 |
} |
275 |
pkt_len = min(connectbuf[con_id]->pmtusize - iov[2].iov_len - iov[1].iov_len - iov[0].iov_len, msg_len - offset) ; |
276 |
|
277 |
iov[3].iov_len = pkt_len;
|
278 |
iov[3].iov_base = msg + offset;
|
279 |
|
280 |
//fill header
|
281 |
msg_h.len_mon_packet_hdr = iov[1].iov_len;
|
282 |
msg_h.offset = offset; |
283 |
msg_h.msg_length = truncable ? pkt_len : msg_len; |
284 |
|
285 |
//monitoring layer hook
|
286 |
if(get_Send_pkt_inf_cb != NULL && iov[1].iov_len) { |
287 |
mon_pkt_inf pkt_info; |
288 |
|
289 |
pkt_info.remote_socketID = &(connectbuf[con_id]->external_socketID); |
290 |
pkt_info.buffer = msg + offset; |
291 |
pkt_info.bufSize = pkt_len; |
292 |
pkt_info.msgtype = msg_type; |
293 |
pkt_info.dataID = connectbuf[con_id]->seqnr; |
294 |
pkt_info.offset = offset; |
295 |
pkt_info.datasize = msg_len; |
296 |
pkt_info.monitoringHeaderLen = iov[1].iov_len;
|
297 |
pkt_info.monitoringHeader = iov[1].iov_base;
|
298 |
pkt_info.ttl = -1;
|
299 |
memset(&(pkt_info.arrival_time),0,sizeof(struct timeval)); |
300 |
|
301 |
(get_Send_pkt_inf_cb) ((void *) &pkt_info);
|
302 |
} |
303 |
|
304 |
debug("ML: sending packet with rconID:%d lconID:%d\n",msg_h.remote_con_id,msg_h.local_con_id);
|
305 |
switch(sendPacket(socketfd, iov, 4, &udpgen.udpaddr)) { |
306 |
case MSGLEN:
|
307 |
info("ML: sending message failed, reducing MTU from %d to %d (conID:%d lconID:%d msgsize:%d offset:%d)\n", connectbuf[con_id]->pmtusize, pmtu_decrement(connectbuf[con_id]->pmtusize), msg_h.remote_con_id, msg_h.local_con_id, msg_len, offset);
|
308 |
connectbuf[con_id]->pmtusize = pmtu_decrement(connectbuf[con_id]->pmtusize); |
309 |
connectbuf[con_id]->delay = true;
|
310 |
retry = true;
|
311 |
offset = msg_len + msg_h.len_mon_data_hdr; // exit the while
|
312 |
break;
|
313 |
case FAILURE:
|
314 |
info("ML: sending message failed (conID:%d lconID:%d msgsize:%d offset:%d)\n", msg_h.remote_con_id, msg_h.local_con_id, msg_len, offset);
|
315 |
offset = msg_len + msg_h.len_mon_data_hdr; // exit the while
|
316 |
break;
|
317 |
case OK:
|
318 |
//update
|
319 |
offset += pkt_len + iov[2].iov_len;
|
320 |
//transmit data header only in the first packet
|
321 |
iov[2].iov_len = 0; |
322 |
break;
|
323 |
} |
324 |
} while(offset != msg_len + msg_h.len_mon_data_hdr && !truncable);
|
325 |
} while(retry);
|
326 |
} |
327 |
|
328 |
void pmtu_timeout_cb(int fd, short event, void *arg); |
329 |
|
330 |
void reschedule_conn_msg(int con_id) |
331 |
{ |
332 |
if (connectbuf[con_id]->timeout_event) {
|
333 |
/* delete old timout */
|
334 |
event_del(connectbuf[con_id]->timeout_event); |
335 |
event_free(connectbuf[con_id]->timeout_event); |
336 |
} |
337 |
connectbuf[con_id]->timeout_event = event_new(base, -1, EV_TIMEOUT, &pmtu_timeout_cb, (void *) (long)con_id); |
338 |
evtimer_add(connectbuf[con_id]->timeout_event, &connectbuf[con_id]->timeout_value); |
339 |
} |
340 |
|
341 |
void send_conn_msg(int con_id, int buf_size, int command_type) |
342 |
{ |
343 |
if (buf_size < sizeof(struct conn_msg)) { |
344 |
error("ML: requested connection message size is too small\n");
|
345 |
return;
|
346 |
} |
347 |
|
348 |
if(connectbuf[con_id]->ctrl_msg_buf == NULL) { |
349 |
connectbuf[con_id]->ctrl_msg_buf = malloc(buf_size); |
350 |
memset(connectbuf[con_id]->ctrl_msg_buf, 0, buf_size);
|
351 |
} |
352 |
|
353 |
if(connectbuf[con_id]->ctrl_msg_buf == NULL) { |
354 |
error("ML: can not allocate memory for connection message\n");
|
355 |
return;
|
356 |
} |
357 |
|
358 |
struct conn_msg *msg_header = (struct conn_msg*) connectbuf[con_id]->ctrl_msg_buf; |
359 |
|
360 |
msg_header->comand_type = command_type; |
361 |
msg_header->pmtu_size = connectbuf[con_id]->pmtusize; |
362 |
|
363 |
memcpy(&(msg_header->sock_id), loc_socketID, sizeof(socket_ID));
|
364 |
|
365 |
send_msg(con_id, ML_CON_MSG, connectbuf[con_id]->ctrl_msg_buf, buf_size, true, &(connectbuf[con_id]->defaultSendParams));
|
366 |
} |
367 |
|
368 |
void send_conn_msg_with_pmtu_discovery(int con_id, int buf_size, int command_type) |
369 |
{ |
370 |
struct timeval tout = PMTU_TIMEOUT;
|
371 |
connectbuf[con_id]->timeout_value = tout; |
372 |
connectbuf[con_id]->trials = 1;
|
373 |
send_conn_msg(con_id, buf_size, command_type); |
374 |
reschedule_conn_msg(con_id); |
375 |
} |
376 |
|
377 |
void resend_conn_msg(int con_id) |
378 |
{ |
379 |
connectbuf[con_id]->trials++; |
380 |
send_conn_msg(con_id, connectbuf[con_id]->pmtusize, connectbuf[con_id]->status); |
381 |
reschedule_conn_msg(con_id); |
382 |
} |
383 |
|
384 |
void
|
385 |
recv_conn_msg(struct msg_header *msg_h, char *msgbuf, int bufsize) |
386 |
{ |
387 |
struct conn_msg *con_msg;
|
388 |
int free_con_id, con_id;
|
389 |
|
390 |
time_t now = time(NULL);
|
391 |
double timediff = 0.0; |
392 |
char str[1000]; |
393 |
|
394 |
// Monitoring layer hook
|
395 |
if(get_Recv_data_inf_cb != NULL && msg_h->len_mon_data_hdr != 0) { |
396 |
// update pointer to the real data
|
397 |
msgbuf += msg_h->len_mon_data_hdr; |
398 |
bufsize -= msg_h->len_mon_data_hdr; |
399 |
con_msg = (struct conn_msg *)msgbuf;
|
400 |
|
401 |
mon_data_inf recv_data_inf; |
402 |
recv_data_inf.remote_socketID = &(con_msg->sock_id); |
403 |
recv_data_inf.buffer = msgbuf; |
404 |
recv_data_inf.bufSize = bufsize; |
405 |
recv_data_inf.msgtype = msg_h->msg_type; |
406 |
recv_data_inf.monitoringDataHeaderLen = msg_h->len_mon_data_hdr; |
407 |
recv_data_inf.monitoringDataHeader = msg_h->len_mon_data_hdr ? msgbuf : NULL;
|
408 |
gettimeofday(&recv_data_inf.arrival_time, NULL);
|
409 |
recv_data_inf.firstPacketArrived = true;
|
410 |
recv_data_inf.recvFragments = 1;
|
411 |
recv_data_inf.priority = false;
|
412 |
recv_data_inf.padding = false;
|
413 |
recv_data_inf.confirmation = false;
|
414 |
recv_data_inf.reliable = false;
|
415 |
|
416 |
// send data recv callback to monitoring module
|
417 |
(get_Recv_data_inf_cb) ((void *) &recv_data_inf);
|
418 |
} else {
|
419 |
con_msg = (struct conn_msg *) msgbuf;
|
420 |
} |
421 |
|
422 |
//decode sock_id for debug messages
|
423 |
mlSocketIDToString(&con_msg->sock_id,str,999);
|
424 |
|
425 |
// check the connection command type
|
426 |
switch (con_msg->comand_type) {
|
427 |
/*
|
428 |
* if INVITE: enter a new socket make new entry in connect array
|
429 |
* send an ok
|
430 |
*/
|
431 |
case INVITE:
|
432 |
info("ML: received INVITE from %s\n",str);
|
433 |
/*
|
434 |
* check if another connection for the external connectionID exist
|
435 |
* that was established within the last 2 seconds
|
436 |
*/
|
437 |
free_con_id = -1;
|
438 |
for (con_id = 0; con_id < CONNECTBUFSIZE; con_id++) { |
439 |
if (connectbuf[con_id] != NULL) { |
440 |
if (mlCompareSocketIDs(&(connectbuf[con_id]->external_socketID), &(con_msg->sock_id)) == 0) { |
441 |
//timediff = difftime(now, connectbuf[con_id]->starttime); //TODO: why this timeout? Shouldn't the connection be closed instead if there is a timeout?
|
442 |
//if (timediff < 2)
|
443 |
//update remote connection ID
|
444 |
if (connectbuf[con_id]->external_connectionID != msg_h->local_con_id) {
|
445 |
warn("ML: updating remote connection ID for %s: from %d to %d\n",str, connectbuf[con_id]->external_connectionID, msg_h->local_con_id);
|
446 |
connectbuf[con_id]->external_connectionID = msg_h->local_con_id; |
447 |
} |
448 |
break;
|
449 |
} |
450 |
} else if(free_con_id == -1) |
451 |
free_con_id = con_id; |
452 |
} |
453 |
|
454 |
if (con_id == CONNECTBUFSIZE) {
|
455 |
// create an entry in the connecttrybuf
|
456 |
if(free_con_id == -1) { |
457 |
error("ML: no new connect_buf available\n");
|
458 |
return;
|
459 |
} |
460 |
connectbuf[free_con_id] = (connect_data *) malloc(sizeof(connect_data));
|
461 |
memset(connectbuf[free_con_id],0,sizeof(connect_data)); |
462 |
connectbuf[free_con_id]->connection_head = connectbuf[free_con_id]->connection_last = NULL;
|
463 |
connectbuf[free_con_id]->starttime = time(NULL);
|
464 |
memcpy(&(connectbuf[free_con_id]->external_socketID), &(con_msg->sock_id), sizeof(socket_ID));
|
465 |
connectbuf[free_con_id]->pmtusize = con_msg->pmtu_size; // bootstrap pmtu from the other's size. Not strictly needed, but a good hint
|
466 |
connectbuf[free_con_id]->external_connectionID = msg_h->local_con_id; |
467 |
connectbuf[free_con_id]->internal_connect = |
468 |
!compare_external_address_socketIDs(&(con_msg->sock_id), loc_socketID); |
469 |
con_id = free_con_id; |
470 |
} |
471 |
|
472 |
//if(connectbuf[con_id]->status <= CONNECT) { //TODO: anwer anyway. Why the outher would invite otherwise?
|
473 |
//update status and send back answer
|
474 |
connectbuf[con_id]->status = CONNECT; |
475 |
send_conn_msg_with_pmtu_discovery(con_id, con_msg->pmtu_size, CONNECT); |
476 |
//}
|
477 |
break;
|
478 |
case CONNECT:
|
479 |
info("ML: received CONNECT from %s\n",str);
|
480 |
|
481 |
if(msg_h->remote_con_id != -1 && connectbuf[msg_h->remote_con_id] == NULL) { |
482 |
error("ML: received CONNECT for inexistent connection rconID:%d\n",msg_h->remote_con_id);
|
483 |
return;
|
484 |
} |
485 |
|
486 |
/*
|
487 |
* check if the connection status is not already 1 or 2
|
488 |
*/
|
489 |
if (connectbuf[msg_h->remote_con_id]->status == INVITE) {
|
490 |
// set the external connectionID
|
491 |
connectbuf[msg_h->remote_con_id]->external_connectionID = msg_h->local_con_id; |
492 |
// change status con_msg the connection_data
|
493 |
connectbuf[msg_h->remote_con_id]->status = READY; |
494 |
// change pmtusize in the connection_data: not needed. receiving a CONNECT means our INVITE went through. So why change pmtu?
|
495 |
//connectbuf[msg_h->remote_con_id]->pmtusize = con_msg->pmtu_size;
|
496 |
|
497 |
// send the READY
|
498 |
send_conn_msg_with_pmtu_discovery(msg_h->remote_con_id, con_msg->pmtu_size, READY); |
499 |
|
500 |
if (receive_Connection_cb != NULL) |
501 |
(receive_Connection_cb) (msg_h->remote_con_id, NULL);
|
502 |
|
503 |
// call all registered callbacks
|
504 |
while(connectbuf[msg_h->remote_con_id]->connection_head != NULL) { |
505 |
struct receive_connection_cb_list *temp;
|
506 |
temp = connectbuf[msg_h->remote_con_id]->connection_head; |
507 |
(temp->connection_cb) (msg_h->remote_con_id, temp->arg); |
508 |
connectbuf[msg_h->remote_con_id]->connection_head = temp->next; |
509 |
free(temp); |
510 |
} |
511 |
connectbuf[msg_h->remote_con_id]->connection_head = |
512 |
connectbuf[msg_h->remote_con_id]->connection_last = NULL;
|
513 |
} else {
|
514 |
// send the READY
|
515 |
send_conn_msg_with_pmtu_discovery(msg_h->remote_con_id, con_msg->pmtu_size, READY); |
516 |
} |
517 |
|
518 |
debug("ML: active connection established\n");
|
519 |
break;
|
520 |
|
521 |
/*
|
522 |
* if READY: find the entry in the connection array set the
|
523 |
* connection active change the pmtu size
|
524 |
*/
|
525 |
case READY:
|
526 |
info("ML: received READY from %s\n",str);
|
527 |
if(connectbuf[msg_h->remote_con_id] == NULL) { |
528 |
error("ML: received READY for inexistent connection\n");
|
529 |
return;
|
530 |
} |
531 |
/*
|
532 |
* checks if the connection is not already established
|
533 |
*/
|
534 |
if (connectbuf[msg_h->remote_con_id]->status == CONNECT) {
|
535 |
// change status of the connection
|
536 |
connectbuf[msg_h->remote_con_id]->status = READY; |
537 |
// change pmtusize: not needed. pmtu doesn't have to be symmetric
|
538 |
//connectbuf[msg_h->remote_con_id]->pmtusize = con_msg->pmtu_size;
|
539 |
|
540 |
if (receive_Connection_cb != NULL) |
541 |
(receive_Connection_cb) (msg_h->remote_con_id, NULL);
|
542 |
|
543 |
while(connectbuf[msg_h->remote_con_id]->connection_head != NULL) { |
544 |
struct receive_connection_cb_list *temp;
|
545 |
temp = connectbuf[msg_h->remote_con_id]->connection_head; |
546 |
(temp->connection_cb) (msg_h->remote_con_id, temp->arg); |
547 |
connectbuf[msg_h->remote_con_id]->connection_head = temp->next; |
548 |
free(temp); |
549 |
} |
550 |
connectbuf[msg_h->remote_con_id]->connection_head = |
551 |
connectbuf[msg_h->remote_con_id]->connection_last = NULL;
|
552 |
debug("ML: passive connection established\n");
|
553 |
} |
554 |
break;
|
555 |
} |
556 |
} |
557 |
|
558 |
void recv_stun_msg(char *msgbuf, int recvSize) |
559 |
{ |
560 |
/*
|
561 |
* create empty stun message struct
|
562 |
*/
|
563 |
StunMessage resp; |
564 |
memset(&resp, 0, sizeof(StunMessage)); |
565 |
/*
|
566 |
* parse the message
|
567 |
*/
|
568 |
int returnValue = 0; |
569 |
returnValue = recv_stun_message(msgbuf, recvSize, &resp); |
570 |
|
571 |
if (returnValue == 0) { |
572 |
/*
|
573 |
* read the reflexive Address into the local_socketID
|
574 |
*/
|
575 |
struct sockaddr_in reflexiveAddr;
|
576 |
reflexiveAddr.sin_family = AF_INET; |
577 |
reflexiveAddr.sin_addr.s_addr = htonl(resp.mappedAddress.ipv4.addr); |
578 |
reflexiveAddr.sin_port = htons(resp.mappedAddress.ipv4.port); |
579 |
socketaddrgen reflexiveAddres; |
580 |
reflexiveAddres.udpaddr = reflexiveAddr; |
581 |
local_socketID.external_addr = reflexiveAddres; |
582 |
NAT_traversal = true;
|
583 |
// callback to the upper layer indicating that the socketID is now
|
584 |
// ready to use
|
585 |
(receive_SocketID_cb) (&local_socketID, 0);
|
586 |
} |
587 |
} |
588 |
|
589 |
//done
|
590 |
void recv_timeout_cb(int fd, short event, void *arg) |
591 |
{ |
592 |
int recv_id = (long) arg; |
593 |
debug("ML: recv_timeout_cb called. Timeout for id:%d\n",recv_id);
|
594 |
|
595 |
if (recvdatabuf[recv_id] == NULL) { |
596 |
return;
|
597 |
} |
598 |
|
599 |
|
600 |
/* if(recvdatabuf[recv_id]->status == ACTIVE) {
|
601 |
//TODO make timeout at least a DEFINE
|
602 |
struct timeval timeout = { 4, 0 };
|
603 |
recvdatabuf[recv_id]->status = INACTIVE;
|
604 |
event_base_once(base, -1, EV_TIMEOUT, recv_timeout_cb,
|
605 |
arg, &timeout);
|
606 |
return;
|
607 |
}
|
608 |
*/
|
609 |
|
610 |
if(recvdatabuf[recv_id]->status == ACTIVE) {
|
611 |
// Monitoring layer hook
|
612 |
if(get_Recv_data_inf_cb != NULL) { |
613 |
mon_data_inf recv_data_inf; |
614 |
|
615 |
recv_data_inf.remote_socketID = |
616 |
&(connectbuf[recvdatabuf[recv_id]->connectionID]->external_socketID); |
617 |
recv_data_inf.buffer = recvdatabuf[recv_id]->recvbuf; |
618 |
recv_data_inf.bufSize = recvdatabuf[recv_id]->bufsize; |
619 |
recv_data_inf.msgtype = recvdatabuf[recv_id]->msgtype; |
620 |
recv_data_inf.monitoringDataHeaderLen = recvdatabuf[recv_id]->monitoringDataHeaderLen; |
621 |
recv_data_inf.monitoringDataHeader = recvdatabuf[recv_id]->monitoringDataHeaderLen ? |
622 |
recvdatabuf[recv_id]->recvbuf : NULL;
|
623 |
gettimeofday(&recv_data_inf.arrival_time, NULL);
|
624 |
recv_data_inf.firstPacketArrived = recvdatabuf[recv_id]->firstPacketArrived; |
625 |
recv_data_inf.recvFragments = recvdatabuf[recv_id]->recvFragments; |
626 |
recv_data_inf.priority = false;
|
627 |
recv_data_inf.padding = false;
|
628 |
recv_data_inf.confirmation = false;
|
629 |
recv_data_inf.reliable = false;
|
630 |
|
631 |
// send data recv callback to monitoring module
|
632 |
|
633 |
(get_Recv_data_inf_cb) ((void *) &recv_data_inf);
|
634 |
} |
635 |
|
636 |
// Get the right callback
|
637 |
receive_data_cb receive_data_callback = recvcbbuf[recvdatabuf[recv_id]->msgtype]; |
638 |
|
639 |
recv_params rParams; |
640 |
|
641 |
rParams.nrMissingBytes = recvdatabuf[recv_id]->bufsize - recvdatabuf[recv_id]->arrivedBytes; |
642 |
rParams.recvFragments = recvdatabuf[recv_id]->recvFragments; |
643 |
rParams.msgtype = recvdatabuf[recv_id]->msgtype; |
644 |
rParams.connectionID = recvdatabuf[recv_id]->connectionID; |
645 |
rParams.remote_socketID = |
646 |
&(connectbuf[recvdatabuf[recv_id]->connectionID]->external_socketID); |
647 |
rParams.firstPacketArrived = recvdatabuf[recv_id]->firstPacketArrived; |
648 |
|
649 |
(receive_data_callback) (recvdatabuf[recv_id]->recvbuf + recvdatabuf[recv_id]->monitoringDataHeaderLen, recvdatabuf[recv_id]->bufsize - recvdatabuf[recv_id]->monitoringDataHeaderLen, |
650 |
recvdatabuf[recv_id]->msgtype, &rParams); |
651 |
|
652 |
//clean up
|
653 |
free(recvdatabuf[recv_id]->recvbuf); |
654 |
free(recvdatabuf[recv_id]); |
655 |
recvdatabuf[recv_id] = NULL;
|
656 |
} |
657 |
} |
658 |
|
659 |
// process a single recv data message
|
660 |
void recv_data_msg(struct msg_header *msg_h, char *msgbuf, int bufsize) |
661 |
{ |
662 |
debug("ML: received packet of size %d with rconID:%d lconID:%d\n",bufsize,msg_h->remote_con_id,msg_h->local_con_id);
|
663 |
|
664 |
int recv_id, free_recv_id = -1; |
665 |
|
666 |
if(connectbuf[msg_h->remote_con_id] == NULL) { |
667 |
debug("ML: Received a message not related to any opened connection!\n");
|
668 |
return;
|
669 |
} |
670 |
|
671 |
// check if a recv_data exist and enter data
|
672 |
for (recv_id = 0; recv_id < RECVDATABUFSIZE; recv_id++) |
673 |
if (recvdatabuf[recv_id] != NULL) { |
674 |
if (msg_h->remote_con_id == recvdatabuf[recv_id]->connectionID &&
|
675 |
msg_h->msg_seq_num == recvdatabuf[recv_id]->seqnr) |
676 |
break;
|
677 |
} else
|
678 |
if(free_recv_id == -1) |
679 |
free_recv_id = recv_id; |
680 |
|
681 |
|
682 |
if(recv_id == RECVDATABUFSIZE) {
|
683 |
//no recv_data found: create one
|
684 |
recv_id = free_recv_id; |
685 |
recvdatabuf[recv_id] = (recvdata *) malloc(sizeof(recvdata));
|
686 |
memset(recvdatabuf[recv_id], 0, sizeof(recvdata)); |
687 |
recvdatabuf[recv_id]->connectionID = msg_h->remote_con_id; |
688 |
recvdatabuf[recv_id]->seqnr = msg_h->msg_seq_num; |
689 |
recvdatabuf[recv_id]->monitoringDataHeaderLen = msg_h->len_mon_data_hdr; |
690 |
recvdatabuf[recv_id]->bufsize = msg_h->msg_length + msg_h->len_mon_data_hdr; |
691 |
recvdatabuf[recv_id]->recvbuf = (char *) malloc(recvdatabuf[recv_id]->bufsize);
|
692 |
/*
|
693 |
* read the timeout data and set it
|
694 |
*/
|
695 |
recvdatabuf[recv_id]->timeout_value = recv_timeout; |
696 |
recvdatabuf[recv_id]->timeout_event = NULL;
|
697 |
recvdatabuf[recv_id]->recvID = recv_id; |
698 |
recvdatabuf[recv_id]->starttime = time(NULL);
|
699 |
recvdatabuf[recv_id]->msgtype = msg_h->msg_type; |
700 |
|
701 |
// fill the buffer with zeros
|
702 |
memset(recvdatabuf[recv_id]->recvbuf, 0, msg_h->msg_length);
|
703 |
debug(" new @ id:%d\n",recv_id);
|
704 |
} else { //message structure already exists, no need to create new |
705 |
debug(" found @ id:%d\n",recv_id);
|
706 |
} |
707 |
|
708 |
if (msg_h->offset == 0) |
709 |
recvdatabuf[recv_id]->firstPacketArrived = 1;
|
710 |
|
711 |
|
712 |
// increment fragmentnr
|
713 |
recvdatabuf[recv_id]->recvFragments++; |
714 |
// increment the arrivedBytes
|
715 |
recvdatabuf[recv_id]->arrivedBytes += bufsize; |
716 |
|
717 |
// enter the data into the buffer
|
718 |
memcpy(recvdatabuf[recv_id]->recvbuf + msg_h->offset, msgbuf, bufsize); |
719 |
|
720 |
//TODO very basic checkif all fragments arrived: has to be reviewed
|
721 |
if(recvdatabuf[recv_id]->arrivedBytes == recvdatabuf[recv_id]->bufsize)
|
722 |
recvdatabuf[recv_id]->status = COMPLETE; //buffer full -> msg completly arrived
|
723 |
else
|
724 |
recvdatabuf[recv_id]->status = ACTIVE; |
725 |
|
726 |
if (recv_data_callback) {
|
727 |
if(recvdatabuf[recv_id]->status == COMPLETE) {
|
728 |
// Monitoring layer hook
|
729 |
if(get_Recv_data_inf_cb != NULL) { |
730 |
mon_data_inf recv_data_inf; |
731 |
|
732 |
recv_data_inf.remote_socketID = |
733 |
&(connectbuf[recvdatabuf[recv_id]->connectionID]->external_socketID); |
734 |
recv_data_inf.buffer = recvdatabuf[recv_id]->recvbuf; |
735 |
recv_data_inf.bufSize = recvdatabuf[recv_id]->bufsize; |
736 |
recv_data_inf.msgtype = recvdatabuf[recv_id]->msgtype; |
737 |
recv_data_inf.monitoringDataHeaderLen = recvdatabuf[recv_id]->monitoringDataHeaderLen; |
738 |
recv_data_inf.monitoringDataHeader = recvdatabuf[recv_id]->monitoringDataHeaderLen ? |
739 |
recvdatabuf[recv_id]->recvbuf : NULL;
|
740 |
gettimeofday(&recv_data_inf.arrival_time, NULL);
|
741 |
recv_data_inf.firstPacketArrived = recvdatabuf[recv_id]->firstPacketArrived; |
742 |
recv_data_inf.recvFragments = recvdatabuf[recv_id]->recvFragments; |
743 |
recv_data_inf.priority = false;
|
744 |
recv_data_inf.padding = false;
|
745 |
recv_data_inf.confirmation = false;
|
746 |
recv_data_inf.reliable = false;
|
747 |
|
748 |
// send data recv callback to monitoring module
|
749 |
|
750 |
(get_Recv_data_inf_cb) ((void *) &recv_data_inf);
|
751 |
} |
752 |
|
753 |
// Get the right callback
|
754 |
receive_data_cb receive_data_callback = recvcbbuf[msg_h->msg_type]; |
755 |
if (receive_data_callback) {
|
756 |
|
757 |
recv_params rParams; |
758 |
|
759 |
rParams.nrMissingBytes = recvdatabuf[recv_id]->bufsize - recvdatabuf[recv_id]->arrivedBytes; |
760 |
rParams.recvFragments = recvdatabuf[recv_id]->recvFragments; |
761 |
rParams.msgtype = recvdatabuf[recv_id]->msgtype; |
762 |
rParams.connectionID = recvdatabuf[recv_id]->connectionID; |
763 |
rParams.remote_socketID = |
764 |
&(connectbuf[recvdatabuf[recv_id]->connectionID]->external_socketID); |
765 |
|
766 |
char str[1000]; |
767 |
mlSocketIDToString(rParams.remote_socketID,str,999);
|
768 |
debug("ML: received message from conID:%d, %s\n",recvdatabuf[recv_id]->connectionID,str);
|
769 |
rParams.firstPacketArrived = recvdatabuf[recv_id]->firstPacketArrived; |
770 |
|
771 |
(receive_data_callback) (recvdatabuf[recv_id]->recvbuf + recvdatabuf[recv_id]->monitoringDataHeaderLen, recvdatabuf[recv_id]->bufsize - recvdatabuf[recv_id]->monitoringDataHeaderLen, |
772 |
recvdatabuf[recv_id]->msgtype, (void *) &rParams);
|
773 |
} else {
|
774 |
warn("ML: callback not initialized for this message type: %d!\n",msg_h->msg_type);
|
775 |
} |
776 |
|
777 |
//clean up
|
778 |
if (recvdatabuf[recv_id]->timeout_event) {
|
779 |
int ret;
|
780 |
debug("ML: freeing timeout for %d",recv_id);
|
781 |
ret = event_del(recvdatabuf[recv_id]->timeout_event); |
782 |
event_free(recvdatabuf[recv_id]->timeout_event); |
783 |
debug(" ret: %d\n",ret);
|
784 |
} else {
|
785 |
debug("ML: received in 1 packet\n",recv_id);
|
786 |
} |
787 |
free(recvdatabuf[recv_id]->recvbuf); |
788 |
free(recvdatabuf[recv_id]); |
789 |
recvdatabuf[recv_id] = NULL;
|
790 |
} else { // not COMPLETE |
791 |
if (!recvdatabuf[recv_id]->timeout_event) {
|
792 |
//start time out
|
793 |
//TODO make timeout at least a DEFINE
|
794 |
recvdatabuf[recv_id]->timeout_event = event_new(base, -1, EV_TIMEOUT, &recv_timeout_cb, (void *) (long)recv_id); |
795 |
evtimer_add(recvdatabuf[recv_id]->timeout_event, &recv_timeout); |
796 |
} |
797 |
} |
798 |
} |
799 |
} |
800 |
|
801 |
//done
|
802 |
void pmtu_timeout_cb(int fd, short event, void *arg) |
803 |
{ |
804 |
|
805 |
int con_id = (long) arg; |
806 |
pmtu new_pmtusize; |
807 |
struct timeval timeout;
|
808 |
|
809 |
|
810 |
if(connectbuf[con_id] == NULL) { |
811 |
error("ML: pmtu timeout called on non existing con_id\n");
|
812 |
return;
|
813 |
} |
814 |
|
815 |
if(connectbuf[con_id]->status == READY) {
|
816 |
// nothing to do anymore
|
817 |
event_del(connectbuf[con_id]->timeout_event); |
818 |
event_free(connectbuf[con_id]->timeout_event); |
819 |
return;
|
820 |
} |
821 |
|
822 |
|
823 |
timeout = connectbuf[con_id]->timeout_value; |
824 |
|
825 |
if(connectbuf[con_id]->delay || connectbuf[con_id]->trials == MAX_TRIALS - 1) { |
826 |
double delay = timeout.tv_sec + timeout.tv_usec / 1000000.0; |
827 |
delay = delay * 2;
|
828 |
timeout.tv_sec = floor(delay); |
829 |
timeout.tv_usec = fmod(delay, 1.0) * 1000000.0; |
830 |
if(connectbuf[con_id]->delay) {
|
831 |
connectbuf[con_id]->delay = false;
|
832 |
reschedule_conn_msg(con_id); |
833 |
} |
834 |
} |
835 |
|
836 |
if(connectbuf[con_id]->trials == MAX_TRIALS) {
|
837 |
// decrement the pmtu size
|
838 |
struct timeval tout = PMTU_TIMEOUT;
|
839 |
connectbuf[con_id]->pmtusize = pmtu_decrement(connectbuf[con_id]->pmtusize); |
840 |
connectbuf[con_id]->timeout_value = tout; |
841 |
connectbuf[con_id]->trials = 0;
|
842 |
} |
843 |
|
844 |
//error in PMTU discovery?
|
845 |
if (connectbuf[con_id]->pmtusize == ERROR) {
|
846 |
if (connectbuf[con_id]->internal_connect == true) { |
847 |
//as of now we tried directly connecting, now let's try trough the NAT
|
848 |
connectbuf[con_id]->internal_connect = false;
|
849 |
connectbuf[con_id]->pmtusize = MAX; |
850 |
} else {
|
851 |
//nothing to do we have to give up
|
852 |
error("ML: Could not create connection with connectionID %i!\n",con_id);
|
853 |
// envoke the callback for failed connection establishment
|
854 |
if(failed_Connection_cb != NULL) |
855 |
(failed_Connection_cb) (con_id, NULL);
|
856 |
// delete the connection entry
|
857 |
mlCloseConnection(con_id); |
858 |
return;
|
859 |
} |
860 |
} |
861 |
|
862 |
//retry with new pmtu size
|
863 |
connectbuf[con_id]->trials++; |
864 |
resend_conn_msg(con_id); |
865 |
} |
866 |
|
867 |
|
868 |
int schedule_pmtu_timeout(int con_id) |
869 |
{ |
870 |
if (! connectbuf[con_id]->timeout_event) {
|
871 |
struct timeval tout = PMTU_TIMEOUT;
|
872 |
connectbuf[con_id]->timeout_value = tout; |
873 |
connectbuf[con_id]->trials = 1;
|
874 |
connectbuf[con_id]->timeout_event = event_new(base, -1, EV_TIMEOUT, &pmtu_timeout_cb, (void *) (long)con_id); |
875 |
evtimer_add(connectbuf[con_id]->timeout_event, &connectbuf[con_id]->timeout_value); |
876 |
} |
877 |
} |
878 |
|
879 |
/*
|
880 |
* decrements the mtu size
|
881 |
*/
|
882 |
pmtu pmtu_decrement(pmtu pmtusize) |
883 |
{ |
884 |
pmtu pmtu_return_size; |
885 |
switch(pmtusize) {
|
886 |
case MAX:
|
887 |
return DSL;
|
888 |
case DSL:
|
889 |
return DSLMEDIUM;
|
890 |
case DSLMEDIUM:
|
891 |
return DSLSLIM;
|
892 |
case DSLSLIM:
|
893 |
return BELOWDSL;
|
894 |
case BELOWDSL:
|
895 |
return MIN;
|
896 |
default:
|
897 |
return ERROR;
|
898 |
} |
899 |
} |
900 |
|
901 |
// called when an ICMP pmtu error message (type 3, code 4) is received
|
902 |
void pmtu_error_cb_th(char *msg, int msglen) |
903 |
{ |
904 |
debug("ML: pmtu_error callback called msg_size: %d\n",msglen);
|
905 |
//TODO debug
|
906 |
return;
|
907 |
|
908 |
char *msgbufptr = NULL; |
909 |
int msgtype;
|
910 |
int connectionID;
|
911 |
pmtu pmtusize; |
912 |
pmtu new_pmtusize; |
913 |
int dead = 0; |
914 |
|
915 |
// check the packettype
|
916 |
msgbufptr = &msg[0];
|
917 |
|
918 |
// check the msgtype
|
919 |
msgbufptr = &msg[1];
|
920 |
memcpy(&msgtype, msgbufptr, 4);
|
921 |
|
922 |
if (msgtype == 0) { |
923 |
|
924 |
// get the connectionID
|
925 |
msgbufptr = &msg[5];
|
926 |
memcpy(&connectionID, msgbufptr, 4);
|
927 |
|
928 |
int msgtype_c = connectbuf[connectionID]->status;
|
929 |
// pmtusize = connectbuf[connectionID]->pmtutrysize;
|
930 |
|
931 |
if (msgtype_c != msgtype) {
|
932 |
dead = 1;
|
933 |
} |
934 |
|
935 |
|
936 |
} else if (msgtype == 1) { |
937 |
|
938 |
// read the connectionID
|
939 |
msgbufptr = &msg[9];
|
940 |
memcpy(&connectionID, msgbufptr, 4);
|
941 |
|
942 |
int msgtype_c = connectbuf[connectionID]->status;
|
943 |
// pmtusize = connectbuf[connectionID]->pmtutrysize;
|
944 |
|
945 |
if (msgtype_c != msgtype) {
|
946 |
dead = 1;
|
947 |
} |
948 |
|
949 |
} |
950 |
// decrement the pmtu size
|
951 |
new_pmtusize = pmtu_decrement(pmtusize); |
952 |
|
953 |
// connectbuf[connectionID]->pmtutrysize = new_pmtusize;
|
954 |
|
955 |
if (new_pmtusize == ERROR) {
|
956 |
error("ML: Could not create connection with connectionID %i !\n",
|
957 |
connectionID); |
958 |
|
959 |
if(failed_Connection_cb != NULL) |
960 |
(failed_Connection_cb) (connectionID, NULL);
|
961 |
// set the message type to a non existent message
|
962 |
msgtype = 2;
|
963 |
// delete the connection entry
|
964 |
mlCloseConnection(connectionID); |
965 |
} |
966 |
|
967 |
if (msgtype == 0 && dead != 1) { |
968 |
|
969 |
// stop the timeout event
|
970 |
// timeout_del(connectbuf[connectionID]->timeout);
|
971 |
/*
|
972 |
* libevent2
|
973 |
*/
|
974 |
|
975 |
// event_del(connectbuf[connectionID]->timeout);
|
976 |
|
977 |
|
978 |
// create and send a connection message
|
979 |
// create_conn_msg(new_pmtusize, connectionID,
|
980 |
// &local_socketID, INVITE);
|
981 |
|
982 |
// send_conn_msg(connectionID, new_pmtusize);
|
983 |
|
984 |
// set a timeout event for the pmtu discovery
|
985 |
// timeout_set(connectbuf[connectionID]->timeout,pmtu_timeout_cb,(void
|
986 |
// *)&connectionID);
|
987 |
|
988 |
// timeout_add(connectbuf[connectionID]->timeout,&connectbuf[connectionID]->timeout_value);
|
989 |
|
990 |
/*
|
991 |
* libevent2
|
992 |
*/
|
993 |
|
994 |
struct event *ev;
|
995 |
ev = evtimer_new(base, pmtu_timeout_cb, |
996 |
(void *) connectbuf[connectionID]);
|
997 |
|
998 |
// connectbuf[connectionID]->timeout = ev;
|
999 |
|
1000 |
event_add(ev, &connectbuf[connectionID]->timeout_value); |
1001 |
|
1002 |
} else if (msgtype == 1 && dead != 1) { |
1003 |
|
1004 |
// stop the timeout event
|
1005 |
// timeout_del(connectbuf[connectionID]->timeout);
|
1006 |
|
1007 |
/*
|
1008 |
* libevent2
|
1009 |
*/
|
1010 |
// info("still here 11 \n");
|
1011 |
// printf("ev %d \n",connectbuf[connectionID]->timeout);
|
1012 |
// event_del(connectbuf[connectionID]->timeout );
|
1013 |
// evtimer_del(connectbuf[connectionID]->timeout );
|
1014 |
|
1015 |
|
1016 |
// // create and send a connection message
|
1017 |
// create_conn_msg(new_pmtusize,
|
1018 |
// connectbuf[connectionID]->connectionID,
|
1019 |
// NULL, CONNECT);
|
1020 |
|
1021 |
//send_conn_msg(connectionID, new_pmtusize);
|
1022 |
|
1023 |
// set a timeout event for the pmtu discovery
|
1024 |
// timeout_set(connectbuf[connectionID]->timeout,pmtu_timeout_cb,(void
|
1025 |
// *)&connectionID);
|
1026 |
// timeout_add(connectbuf[connectionID]->timeout,&connectbuf[connectionID]->timeout_value);
|
1027 |
|
1028 |
/*
|
1029 |
* libevent2
|
1030 |
*/
|
1031 |
// struct event *ev;
|
1032 |
// ev = evtimer_new(base,pmtu_timeout_cb, (void
|
1033 |
// *)connectbuf[connectionID]);
|
1034 |
// connectbuf[connectionID]->timeout = ev;
|
1035 |
// event_add(ev,&connectbuf[connectionID]->timeout_value);
|
1036 |
|
1037 |
} |
1038 |
} |
1039 |
|
1040 |
/*
|
1041 |
* what to do once a packet arrived if it is a conn packet send it to
|
1042 |
* recv_conn handler if it is a data packet send it to the recv_data
|
1043 |
* handler
|
1044 |
*/
|
1045 |
|
1046 |
//done --
|
1047 |
void recv_pkg(int fd, short event, void *arg) |
1048 |
{ |
1049 |
debug("ML: recv_pkg called\n");
|
1050 |
|
1051 |
struct msg_header *msg_h;
|
1052 |
char msgbuf[MAX];
|
1053 |
pmtu recvSize = MAX; |
1054 |
char *bufptr = msgbuf;
|
1055 |
int ttl;
|
1056 |
struct sockaddr_in recv_addr;
|
1057 |
int msg_size;
|
1058 |
|
1059 |
recvPacket(fd, msgbuf, &recvSize, &recv_addr, pmtu_error_cb_th, &ttl); |
1060 |
|
1061 |
|
1062 |
// check if it is not just an ERROR message
|
1063 |
if(recvSize < 0) |
1064 |
return;
|
1065 |
|
1066 |
// @TODO check if this simplistic STUN message recognition really always works, probably not
|
1067 |
unsigned short stun_bind_response = 0x0101; |
1068 |
unsigned short * msgspot = (unsigned short *) msgbuf; |
1069 |
if (*msgspot == stun_bind_response) {
|
1070 |
debug("ML: recv_pkg: parse stun message called\n");
|
1071 |
recv_stun_msg(msgbuf, recvSize); |
1072 |
return;
|
1073 |
} |
1074 |
|
1075 |
msg_h = (struct msg_header *) msgbuf;
|
1076 |
bufptr += MSG_HEADER_SIZE + msg_h->len_mon_packet_hdr; |
1077 |
msg_size = recvSize - MSG_HEADER_SIZE - msg_h->len_mon_packet_hdr; |
1078 |
|
1079 |
|
1080 |
if(get_Recv_pkt_inf_cb != NULL && msg_h->len_mon_packet_hdr != 0) { |
1081 |
mon_pkt_inf msginfNow; |
1082 |
msginfNow.monitoringHeaderLen = msg_h->len_mon_packet_hdr; |
1083 |
msginfNow.monitoringHeader = msg_h->len_mon_packet_hdr ? &msgbuf[0] + MSG_HEADER_SIZE : NULL; |
1084 |
//TODO rethink this ...
|
1085 |
if(msg_h->msg_type == ML_CON_MSG) {
|
1086 |
struct conn_msg *c_msg = (struct conn_msg *) bufptr; |
1087 |
msginfNow.remote_socketID = &(c_msg->sock_id); |
1088 |
} |
1089 |
else if(connectbuf[msg_h->remote_con_id] == NULL) { |
1090 |
error("ML: received pkg called with non existent connection\n");
|
1091 |
return;
|
1092 |
} else
|
1093 |
msginfNow.remote_socketID = &(connectbuf[msg_h->remote_con_id]->external_socketID); |
1094 |
msginfNow.buffer = bufptr; |
1095 |
msginfNow.bufSize = recvSize; |
1096 |
msginfNow.msgtype = msg_h->msg_type; |
1097 |
msginfNow.ttl = ttl; |
1098 |
msginfNow.dataID = msg_h->msg_seq_num; |
1099 |
msginfNow.offset = msg_h->offset; |
1100 |
msginfNow.datasize = msg_h->msg_length; |
1101 |
gettimeofday(&msginfNow.arrival_time, NULL);
|
1102 |
(get_Recv_pkt_inf_cb) ((void *) &msginfNow);
|
1103 |
} |
1104 |
|
1105 |
|
1106 |
switch(msg_h->msg_type) {
|
1107 |
case ML_CON_MSG:
|
1108 |
debug("ML: received conn pkg\n");
|
1109 |
recv_conn_msg(msg_h, bufptr, msg_size); |
1110 |
break;
|
1111 |
default:
|
1112 |
if(msg_h->msg_type < 127) { |
1113 |
debug("ML: received data pkg\n");
|
1114 |
recv_data_msg(msg_h, bufptr, msg_size); |
1115 |
break;
|
1116 |
} |
1117 |
debug("ML: unrecognised msg_type\n");
|
1118 |
break;
|
1119 |
} |
1120 |
} |
1121 |
|
1122 |
/*
|
1123 |
* compare the external IP address of two socketIDs
|
1124 |
*/
|
1125 |
int
|
1126 |
compare_external_address_socketIDs(socketID_handle sock1, socketID_handle sock2) |
1127 |
{ |
1128 |
if( sock1->external_addr.udpaddr.sin_addr.s_addr == sock2->external_addr.udpaddr.sin_addr.s_addr)
|
1129 |
return 0; |
1130 |
return 1; |
1131 |
} |
1132 |
|
1133 |
void try_stun();
|
1134 |
|
1135 |
/*
|
1136 |
* the timeout of the NAT traversal
|
1137 |
*/
|
1138 |
void nat_traversal_timeout(int fd, short event, void *arg) |
1139 |
{ |
1140 |
if (NAT_traversal == false) { |
1141 |
debug("ML: NAT traversal request re-send\n");
|
1142 |
if(receive_SocketID_cb)
|
1143 |
(receive_SocketID_cb) (&local_socketID, 2);
|
1144 |
try_stun(); |
1145 |
} |
1146 |
} |
1147 |
|
1148 |
unsigned long resolve(const char *ipaddr) |
1149 |
{ |
1150 |
struct hostent *h = gethostbyname(ipaddr);
|
1151 |
if (!h) {
|
1152 |
error("ML: Unable to resolve host name %s\n", ipaddr);
|
1153 |
exit(-1);
|
1154 |
} |
1155 |
unsigned long *addr = (unsigned long *) (h->h_addr); |
1156 |
return *addr;
|
1157 |
} |
1158 |
|
1159 |
|
1160 |
/*
|
1161 |
* returns a handle to the socketID struct the ipaddr can be a null
|
1162 |
* pointer. Then all available ipaddr on the machine are choosen.
|
1163 |
*/
|
1164 |
void create_socket(const int port, const char *ipaddr) |
1165 |
{ |
1166 |
struct sockaddr_in udpaddr;
|
1167 |
udpaddr.sin_family = AF_INET; |
1168 |
if (ipaddr == NULL) { |
1169 |
/*
|
1170 |
* try to guess the local IP address
|
1171 |
*/
|
1172 |
const char *ipaddr_iface = mlAutodetectIPAddress(); |
1173 |
if (ipaddr_iface) {
|
1174 |
udpaddr.sin_addr.s_addr = inet_addr(ipaddr_iface); |
1175 |
} else {
|
1176 |
udpaddr.sin_addr.s_addr = INADDR_ANY; |
1177 |
} |
1178 |
} else {
|
1179 |
udpaddr.sin_addr.s_addr = inet_addr(ipaddr); |
1180 |
} |
1181 |
udpaddr.sin_port = htons(port); |
1182 |
|
1183 |
socketaddrgen udpgen; |
1184 |
memset(&udpgen,0,sizeof(socketaddrgen)); //this will be sent over the net, so set it to 0 |
1185 |
udpgen.udpaddr = udpaddr; |
1186 |
local_socketID.internal_addr = udpgen; |
1187 |
|
1188 |
socketfd = createSocket(port, ipaddr); |
1189 |
|
1190 |
struct event *ev;
|
1191 |
ev = event_new(base, socketfd, EV_READ | EV_PERSIST, recv_pkg, NULL);
|
1192 |
|
1193 |
event_add(ev, NULL);
|
1194 |
|
1195 |
try_stun(); |
1196 |
} |
1197 |
|
1198 |
/*
|
1199 |
* try to figure out external IP using STUN, if defined
|
1200 |
*/
|
1201 |
void try_stun()
|
1202 |
{ |
1203 |
if (isStunDefined()) {
|
1204 |
/*
|
1205 |
* send the NAT traversal STUN request
|
1206 |
*/
|
1207 |
send_stun_request(socketfd, &stun_server); |
1208 |
|
1209 |
/*
|
1210 |
* enter a NAT traversal timeout that takes care of retransmission
|
1211 |
*/
|
1212 |
struct event *ev1;
|
1213 |
struct timeval timeout_value_NAT_traversal = NAT_TRAVERSAL_TIMEOUT;
|
1214 |
ev1 = evtimer_new(base, nat_traversal_timeout, NULL);
|
1215 |
event_add(ev1, &timeout_value_NAT_traversal); |
1216 |
|
1217 |
NAT_traversal = false;
|
1218 |
} else {
|
1219 |
/*
|
1220 |
* Assume we have accessibility and copy internal address to external one
|
1221 |
*/
|
1222 |
local_socketID.external_addr = local_socketID.internal_addr; |
1223 |
NAT_traversal = true; // @TODO: this is not really NAT traversal, but a flag that init is over |
1224 |
// callback to the upper layer indicating that the socketID is now
|
1225 |
// ready to use
|
1226 |
if(receive_SocketID_cb)
|
1227 |
(receive_SocketID_cb) (&local_socketID, 0); //success |
1228 |
} |
1229 |
} |
1230 |
|
1231 |
/**************************** END OF INTERNAL ***********************/
|
1232 |
|
1233 |
/**************************** MONL functions *************************/
|
1234 |
|
1235 |
void 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){ |
1236 |
|
1237 |
base = (struct event_base *) arg;
|
1238 |
recv_data_callback = recv_data_cb; |
1239 |
mlSetRecvTimeout(timeout_value); |
1240 |
if (stun_ipaddr) {
|
1241 |
mlSetStunServer(stun_port, stun_ipaddr); |
1242 |
} else {
|
1243 |
|
1244 |
} |
1245 |
register_recv_localsocketID_cb(local_socketID_cb); |
1246 |
create_socket(port, ipaddr); |
1247 |
|
1248 |
} |
1249 |
|
1250 |
/* register callbacks */
|
1251 |
void mlRegisterGetRecvPktInf(get_recv_pkt_inf_cb recv_pkt_inf_cb){
|
1252 |
|
1253 |
if (recv_pkt_inf_cb == NULL) |
1254 |
error("ML: Register get_recv_pkt_inf_cb failed: NULL ptr \n");
|
1255 |
else
|
1256 |
get_Recv_pkt_inf_cb = recv_pkt_inf_cb; |
1257 |
|
1258 |
} |
1259 |
|
1260 |
void mlRegisterGetSendPktInf(get_send_pkt_inf_cb send_pkt_inf_cb){
|
1261 |
|
1262 |
if (send_pkt_inf_cb == NULL) |
1263 |
error("ML: Register get_send_pkt_inf_cb: NULL ptr \n");
|
1264 |
else
|
1265 |
get_Send_pkt_inf_cb = send_pkt_inf_cb; |
1266 |
|
1267 |
} |
1268 |
|
1269 |
|
1270 |
void mlRegisterSetMonitoringHeaderPktCb(set_monitoring_header_pkt_cb monitoring_header_pkt_cb ){
|
1271 |
|
1272 |
if (monitoring_header_pkt_cb == NULL) |
1273 |
error("ML: Register set_monitoring_header_pkt_cb: NULL ptr \n");
|
1274 |
else
|
1275 |
set_Monitoring_header_pkt_cb = monitoring_header_pkt_cb; |
1276 |
|
1277 |
} |
1278 |
|
1279 |
void mlRegisterGetRecvDataInf(get_recv_data_inf_cb recv_data_inf_cb){
|
1280 |
|
1281 |
if (recv_data_inf_cb == NULL) |
1282 |
error("ML: Register get_recv_data_inf_cb: NULL ptr \n");
|
1283 |
else
|
1284 |
get_Recv_data_inf_cb = recv_data_inf_cb; |
1285 |
|
1286 |
} |
1287 |
|
1288 |
void mlRegisterGetSendDataInf(get_send_data_inf_cb send_data_inf_cb){
|
1289 |
|
1290 |
if (send_data_inf_cb == NULL) |
1291 |
error("ML: Register get_send_data_inf_cb: NULL ptr \n");
|
1292 |
else
|
1293 |
get_Send_data_inf_cb = send_data_inf_cb; |
1294 |
|
1295 |
} |
1296 |
|
1297 |
void mlRegisterSetMonitoringHeaderDataCb(set_monitoring_header_data_cb monitoring_header_data_cb){
|
1298 |
|
1299 |
if (monitoring_header_data_cb == NULL) |
1300 |
error("ML: Register set_monitoring_header_data_cb : NULL ptr \n");
|
1301 |
else
|
1302 |
set_Monitoring_header_data_cb = monitoring_header_data_cb; |
1303 |
|
1304 |
} |
1305 |
|
1306 |
void mlSetRecvTimeout(struct timeval timeout_value){ |
1307 |
|
1308 |
recv_timeout = timeout_value; |
1309 |
|
1310 |
} |
1311 |
|
1312 |
int mlGetStandardTTL(socketID_handle socketID,uint8_t *ttl){
|
1313 |
|
1314 |
return getTTL(socketfd, ttl);
|
1315 |
|
1316 |
} |
1317 |
|
1318 |
socketID_handle mlGetLocalSocketID(int *errorstatus){
|
1319 |
|
1320 |
if (NAT_traversal == false) { |
1321 |
*errorstatus = 2;
|
1322 |
return NULL; |
1323 |
} |
1324 |
|
1325 |
*errorstatus = 0;
|
1326 |
return &local_socketID;
|
1327 |
|
1328 |
} |
1329 |
|
1330 |
|
1331 |
/**************************** END of MONL functions *************************/
|
1332 |
|
1333 |
/**************************** GENERAL functions *************************/
|
1334 |
|
1335 |
void mlRegisterRecvConnectionCb(receive_connection_cb recv_conn_cb){
|
1336 |
|
1337 |
if (recv_conn_cb == NULL) |
1338 |
error("ML: Register receive_connection_cb: NULL ptr \n");
|
1339 |
else
|
1340 |
receive_Connection_cb = recv_conn_cb; |
1341 |
|
1342 |
} |
1343 |
|
1344 |
void mlRegisterErrorConnectionCb(connection_failed_cb conn_failed){
|
1345 |
|
1346 |
if (conn_failed == NULL) |
1347 |
error("ML: Register connection_failed_cb: NULL ptr \n");
|
1348 |
else
|
1349 |
failed_Connection_cb = conn_failed; |
1350 |
|
1351 |
} |
1352 |
|
1353 |
void mlRegisterRecvDataCb(receive_data_cb data_cb,unsigned char msgtype){ |
1354 |
|
1355 |
if (msgtype > 126) { |
1356 |
|
1357 |
error |
1358 |
("ML: Could not register recv_data callback. Msgtype is greater then 126 \n");
|
1359 |
|
1360 |
} |
1361 |
|
1362 |
if (data_cb == NULL) { |
1363 |
|
1364 |
error("ML: Register receive data callback: NUll ptr \n ");
|
1365 |
|
1366 |
} else {
|
1367 |
|
1368 |
recvcbbuf[msgtype] = data_cb; |
1369 |
|
1370 |
} |
1371 |
|
1372 |
} |
1373 |
|
1374 |
void mlCloseSocket(socketID_handle socketID){
|
1375 |
|
1376 |
free(socketID); |
1377 |
|
1378 |
} |
1379 |
|
1380 |
void keepalive_fn(evutil_socket_t fd, short what, void *arg) { |
1381 |
socketID_handle peer = arg; |
1382 |
|
1383 |
int con_id = mlConnectionExist(peer, false); |
1384 |
if (con_id < 0 || connectbuf[con_id]->defaultSendParams.keepalive <= 0) { |
1385 |
/* Connection fell from under us or keepalive was disabled */
|
1386 |
free(arg); |
1387 |
return;
|
1388 |
} |
1389 |
|
1390 |
/* do what we gotta do */
|
1391 |
if ( connectbuf[con_id]->status == READY) {
|
1392 |
char keepaliveMsg[32] = ""; |
1393 |
sprintf(keepaliveMsg, "KEEPALIVE %d", connectbuf[con_id]->keepalive_seq++);
|
1394 |
send_msg(con_id, MSG_TYPE_ML_KEEPALIVE, keepaliveMsg, 1 + strlen(keepaliveMsg), false, |
1395 |
&(connectbuf[con_id]->defaultSendParams)); |
1396 |
} |
1397 |
|
1398 |
/* re-schedule */
|
1399 |
struct timeval t = { 0,0 }; |
1400 |
t.tv_sec = connectbuf[con_id]->defaultSendParams.keepalive; |
1401 |
if (connectbuf[con_id]->defaultSendParams.keepalive)
|
1402 |
event_base_once(base, -1, EV_TIMEOUT, keepalive_fn, peer, &t);
|
1403 |
} |
1404 |
|
1405 |
void setupKeepalive(int conn_id) { |
1406 |
/* Save the peer's address for us */
|
1407 |
socketID_handle peer = malloc(sizeof(socket_ID));
|
1408 |
memcpy(peer, &connectbuf[conn_id]->external_socketID, sizeof(socket_ID));
|
1409 |
|
1410 |
struct timeval t = { 0,0 }; |
1411 |
t.tv_sec = connectbuf[conn_id]->defaultSendParams.keepalive; |
1412 |
|
1413 |
if (connectbuf[conn_id]->defaultSendParams.keepalive)
|
1414 |
event_base_once(base, -1, EV_TIMEOUT, keepalive_fn, peer, &t);
|
1415 |
} |
1416 |
|
1417 |
/* connection functions */
|
1418 |
int mlOpenConnection(socketID_handle external_socketID,receive_connection_cb connection_cb,void *arg, const send_params defaultSendParams){ |
1419 |
|
1420 |
int con_id;
|
1421 |
if (external_socketID == NULL) { |
1422 |
error("ML: cannot open connection: one of the socketIDs is NULL\n");
|
1423 |
return -1; |
1424 |
} |
1425 |
if (NAT_traversal == false) { |
1426 |
error("ML: cannot open connection: NAT traversal for socketID still in progress\n");
|
1427 |
return -1; |
1428 |
} |
1429 |
if (connection_cb == NULL) { |
1430 |
error("ML: cannot open connection: connection_cb is NULL\n");
|
1431 |
return -1; |
1432 |
} |
1433 |
|
1434 |
// check if that connection already exist
|
1435 |
|
1436 |
con_id = mlConnectionExist(external_socketID, false);
|
1437 |
if (con_id >= 0) { |
1438 |
// overwrite defaultSendParams
|
1439 |
bool newKeepalive =
|
1440 |
connectbuf[con_id]->defaultSendParams.keepalive == 0 && defaultSendParams.keepalive != 0; |
1441 |
connectbuf[con_id]->defaultSendParams = defaultSendParams; |
1442 |
if (newKeepalive) setupKeepalive(con_id);
|
1443 |
// if so check if it is ready to use
|
1444 |
if (connectbuf[con_id]->status == READY) {
|
1445 |
// if so use the callback immediately
|
1446 |
(connection_cb) (con_id, arg); |
1447 |
|
1448 |
// otherwise just write the connection cb and the arg pointer
|
1449 |
// into the connection struct
|
1450 |
} else {
|
1451 |
struct receive_connection_cb_list *temp;
|
1452 |
temp = malloc(sizeof(struct receive_connection_cb_list)); |
1453 |
temp->next = NULL;
|
1454 |
temp->connection_cb = connection_cb; |
1455 |
temp->arg = arg; |
1456 |
if(connectbuf[con_id]->connection_last != NULL) { |
1457 |
connectbuf[con_id]->connection_last->next = temp; |
1458 |
connectbuf[con_id]->connection_last = temp; |
1459 |
} else
|
1460 |
connectbuf[con_id]->connection_last = connectbuf[con_id]->connection_head = temp; |
1461 |
} |
1462 |
return con_id;
|
1463 |
} |
1464 |
// make entry in connection_establishment array
|
1465 |
for (con_id = 0; con_id < CONNECTBUFSIZE; con_id++) { |
1466 |
if (connectbuf[con_id] == NULL) { |
1467 |
connectbuf[con_id] = (connect_data *) malloc(sizeof(connect_data));
|
1468 |
memset(connectbuf[con_id],0,sizeof(connect_data)); |
1469 |
connectbuf[con_id]->starttime = time(NULL);
|
1470 |
memcpy(&connectbuf[con_id]->external_socketID, external_socketID, sizeof(socket_ID));
|
1471 |
connectbuf[con_id]->pmtusize = DSLSLIM; |
1472 |
connectbuf[con_id]->status = INVITE; |
1473 |
connectbuf[con_id]->seqnr = 0;
|
1474 |
connectbuf[con_id]->internal_connect = !compare_external_address_socketIDs(external_socketID, &local_socketID); |
1475 |
connectbuf[con_id]->connectionID = con_id; |
1476 |
|
1477 |
connectbuf[con_id]->connection_head = connectbuf[con_id]->connection_last = malloc(sizeof(struct receive_connection_cb_list)); |
1478 |
connectbuf[con_id]->connection_last->next = NULL;
|
1479 |
connectbuf[con_id]->connection_last->connection_cb = connection_cb; |
1480 |
connectbuf[con_id]->connection_last->arg = arg; |
1481 |
connectbuf[con_id]->external_connectionID = -1;
|
1482 |
|
1483 |
connectbuf[con_id]->defaultSendParams = defaultSendParams; |
1484 |
if (defaultSendParams.keepalive) setupKeepalive(con_id);
|
1485 |
break;
|
1486 |
} |
1487 |
} //end of for
|
1488 |
|
1489 |
if (con_id == CONNECTBUFSIZE) {
|
1490 |
error("ML: Could not open connection: connection buffer full\n");
|
1491 |
return -1; |
1492 |
} |
1493 |
|
1494 |
// create and send a connection message
|
1495 |
send_conn_msg_with_pmtu_discovery(con_id, MAX, INVITE); |
1496 |
|
1497 |
return con_id;
|
1498 |
|
1499 |
} |
1500 |
|
1501 |
void mlCloseConnection(const int connectionID){ |
1502 |
|
1503 |
// remove it from the connection array
|
1504 |
if(connectbuf[connectionID]) {
|
1505 |
if(connectbuf[connectionID]->ctrl_msg_buf) {
|
1506 |
free(connectbuf[connectionID]->ctrl_msg_buf); |
1507 |
} |
1508 |
// remove related events
|
1509 |
if (connectbuf[connectionID]->timeout_event) {
|
1510 |
event_del(connectbuf[connectionID]->timeout_event); |
1511 |
event_free(connectbuf[connectionID]->timeout_event); |
1512 |
} |
1513 |
free(connectbuf[connectionID]); |
1514 |
connectbuf[connectionID] = NULL;
|
1515 |
} |
1516 |
|
1517 |
} |
1518 |
|
1519 |
void mlSendData(const int connectionID,char *sendbuf,int bufsize,unsigned char msgtype,send_params *sParams){ |
1520 |
|
1521 |
if (connectionID < 0) { |
1522 |
error("ML: send data failed: connectionID does not exist\n");
|
1523 |
return;
|
1524 |
} |
1525 |
|
1526 |
if (connectbuf[connectionID] == NULL) { |
1527 |
error("ML: send data failed: connectionID does not exist\n");
|
1528 |
return;
|
1529 |
} |
1530 |
if (connectbuf[connectionID]->status != READY) {
|
1531 |
error("ML: send data failed: connection is not active\n");
|
1532 |
return;
|
1533 |
} |
1534 |
|
1535 |
if (sParams == NULL) { |
1536 |
sParams = &(connectbuf[connectionID]->defaultSendParams); |
1537 |
} |
1538 |
|
1539 |
send_msg(connectionID, msgtype, sendbuf, bufsize, false, sParams);
|
1540 |
|
1541 |
} |
1542 |
|
1543 |
/* transmit data functions */
|
1544 |
int mlSendAllData(const int connectionID,send_all_data_container *container,int nr_entries,unsigned char msgtype,send_params *sParams){ |
1545 |
|
1546 |
if (nr_entries < 1 || nr_entries > 5) { |
1547 |
|
1548 |
error |
1549 |
("ML : sendALlData : nr_enties is not between 1 and 5 \n ");
|
1550 |
return 0; |
1551 |
|
1552 |
} else {
|
1553 |
|
1554 |
if (nr_entries == 1) { |
1555 |
|
1556 |
mlSendData(connectionID, container->buffer_1, |
1557 |
container->length_1, msgtype, sParams); |
1558 |
|
1559 |
return 1; |
1560 |
|
1561 |
} else if (nr_entries == 2) { |
1562 |
|
1563 |
int buflen = container->length_1 + container->length_2;
|
1564 |
char buf[buflen];
|
1565 |
memcpy(buf, container->buffer_1, container->length_1); |
1566 |
memcpy(&buf[container->length_1], container->buffer_2, |
1567 |
container->length_2); |
1568 |
mlSendData(connectionID, buf, buflen, msgtype, sParams); |
1569 |
|
1570 |
return 1; |
1571 |
|
1572 |
} else if (nr_entries == 3) { |
1573 |
|
1574 |
int buflen =
|
1575 |
container->length_1 + container->length_2 + |
1576 |
container->length_3; |
1577 |
char buf[buflen];
|
1578 |
memcpy(buf, container->buffer_1, container->length_1); |
1579 |
memcpy(&buf[container->length_1], container->buffer_2, |
1580 |
container->length_2); |
1581 |
memcpy(&buf[container->length_2], container->buffer_3, |
1582 |
container->length_3); |
1583 |
mlSendData(connectionID, buf, buflen, msgtype, sParams); |
1584 |
|
1585 |
|
1586 |
return 1; |
1587 |
|
1588 |
} else if (nr_entries == 4) { |
1589 |
|
1590 |
int buflen =
|
1591 |
container->length_1 + container->length_2 + |
1592 |
container->length_3 + container->length_4; |
1593 |
char buf[buflen];
|
1594 |
memcpy(buf, container->buffer_1, container->length_1); |
1595 |
memcpy(&buf[container->length_1], container->buffer_2, |
1596 |
container->length_2); |
1597 |
memcpy(&buf[container->length_2], container->buffer_3, |
1598 |
container->length_3); |
1599 |
memcpy(&buf[container->length_3], container->buffer_4, |
1600 |
container->length_4); |
1601 |
mlSendData(connectionID, buf, buflen, msgtype, sParams); |
1602 |
|
1603 |
return 1; |
1604 |
|
1605 |
} else {
|
1606 |
|
1607 |
int buflen =
|
1608 |
container->length_1 + container->length_2 + |
1609 |
container->length_3 + container->length_4 + |
1610 |
container->length_5; |
1611 |
char buf[buflen];
|
1612 |
memcpy(buf, container->buffer_1, container->length_1); |
1613 |
memcpy(&buf[container->length_1], container->buffer_2, |
1614 |
container->length_2); |
1615 |
memcpy(&buf[container->length_2], container->buffer_3, |
1616 |
container->length_3); |
1617 |
memcpy(&buf[container->length_3], container->buffer_4, |
1618 |
container->length_4); |
1619 |
memcpy(&buf[container->length_4], container->buffer_5, |
1620 |
container->length_5); |
1621 |
mlSendData(connectionID, buf, buflen, msgtype, sParams); |
1622 |
|
1623 |
return 1; |
1624 |
} |
1625 |
|
1626 |
} |
1627 |
|
1628 |
} |
1629 |
|
1630 |
int mlRecvData(const int connectionID,char *recvbuf,int *bufsize,recv_params *rParams){ |
1631 |
|
1632 |
//TODO yet to be converted
|
1633 |
return 0; |
1634 |
#if 0
|
1635 |
if (rParams == NULL) {
|
1636 |
error("ML: recv_data failed: recv_params is a NULL ptr\n");
|
1637 |
return 0;
|
1638 |
} else {
|
1639 |
|
1640 |
info("ML: recv data called \n");
|
1641 |
|
1642 |
int i = 0;
|
1643 |
int returnValue = 0;
|
1644 |
double timeout = (double) recv_timeout.tv_sec;
|
1645 |
time_t endtime = time(NULL);
|
1646 |
|
1647 |
for (i = 0; i < RECVDATABUFSIZE; i++) {
|
1648 |
|
1649 |
if (recvdatabuf[i] != NULL) {
|
1650 |
|
1651 |
if (recvdatabuf[i]->connectionID == connectionID) {
|
1652 |
|
1653 |
info("ML: recv data has entry \n");
|
1654 |
|
1655 |
double timepass = difftime(endtime, recvdatabuf[i]->starttime);
|
1656 |
|
1657 |
// check if the specified connection has data and it
|
1658 |
// is complete
|
1659 |
// check the data seqnr
|
1660 |
// if(connectionID == recvdatabuf[i]->connectionID &&
|
1661 |
// 1 == recvdatabuf[i]->status){
|
1662 |
|
1663 |
if (1 == recvdatabuf[i]->status) {
|
1664 |
|
1665 |
// info("transmissionHandler: recv_data set is
|
1666 |
// complete \n" );
|
1667 |
|
1668 |
// debug("debud \n");
|
1669 |
|
1670 |
// exchange the pointers
|
1671 |
int buffersize = 0;
|
1672 |
buffersize = recvdatabuf[i]->bufsize;
|
1673 |
*bufsize = buffersize;
|
1674 |
// recvbuf = recvdatabuf[i]->recvbuf;
|
1675 |
|
1676 |
// info("buffersize %d \n",buffersize);
|
1677 |
memcpy(recvbuf, recvdatabuf[i]->recvbuf,
|
1678 |
buffersize);
|
1679 |
// debug(" recvbuf %s \n",recvbuf );
|
1680 |
|
1681 |
// double nrMissFrags =
|
1682 |
// (double) recvdatabuf[i]->nrFragments /
|
1683 |
// (double) recvdatabuf[i]->recvFragments;
|
1684 |
// int nrMissingFragments = (int) ceil(nrMissFrags);
|
1685 |
|
1686 |
// rParams->nrMissingFragments = nrMissingFragments;
|
1687 |
// rParams->nrFragments = recvdatabuf[i]->nrFragments;
|
1688 |
rParams->msgtype = recvdatabuf[i]->msgtype;
|
1689 |
rParams->connectionID =
|
1690 |
recvdatabuf[i]->connectionID;
|
1691 |
|
1692 |
// break from the loop
|
1693 |
// debug(" recvbuf %s \n ",recvbuf);
|
1694 |
|
1695 |
// double nrMissFrags =
|
1696 |
// (double)recvdatabuf[i]->nrFragments /
|
1697 |
// (double)recvdatabuf[i]->recvFragments;
|
1698 |
// int nrMissingFragments =
|
1699 |
// (int)ceil(nrMissFrags);
|
1700 |
|
1701 |
if(get_Recv_data_inf_cb != NULL) {
|
1702 |
mon_data_inf recv_data_inf;
|
1703 |
|
1704 |
recv_data_inf.remote_socketID = &(connectbuf[connectionID]->external_socketID);
|
1705 |
recv_data_inf.buffer = recvdatabuf[i]->recvbuf;
|
1706 |
recv_data_inf.bufSize = recvdatabuf[i]->bufsize;
|
1707 |
recv_data_inf.msgtype = recvdatabuf[i]->msgtype;
|
1708 |
// recv_data_inf.monitoringHeaderType = recvdatabuf[i]->monitoringHeaderType;
|
1709 |
// recv_data_inf.monitoringDataHeader = recvdatabuf[i]->monitoringDataHeader;
|
1710 |
gettimeofday(&recv_data_inf.arrival_time, NULL);
|
1711 |
recv_data_inf.firstPacketArrived = recvdatabuf[i]->firstPacketArrived;
|
1712 |
recv_data_inf.nrMissingFragments = nrMissingFragments;
|
1713 |
recv_data_inf.nrFragments = recvdatabuf[i]->nrFragments;
|
1714 |
recv_data_inf.priority = false;
|
1715 |
recv_data_inf.padding = false;
|
1716 |
recv_data_inf.confirmation = false;
|
1717 |
recv_data_inf.reliable = false;
|
1718 |
|
1719 |
// send data recv callback to monitoring module
|
1720 |
|
1721 |
(get_Recv_data_inf_cb) ((void *) &recv_data_inf);
|
1722 |
}
|
1723 |
|
1724 |
|
1725 |
// free the allocated memory
|
1726 |
free(recvdatabuf[i]);
|
1727 |
recvdatabuf[i] = NULL;
|
1728 |
|
1729 |
returnValue = 1;
|
1730 |
break;
|
1731 |
|
1732 |
}
|
1733 |
|
1734 |
if (recvdatabuf[i] != NULL) {
|
1735 |
|
1736 |
if (timepass > timeout) {
|
1737 |
|
1738 |
info("ML: recv_data timeout called \n");
|
1739 |
|
1740 |
// some data about the missing chunks should
|
1741 |
// be added here
|
1742 |
// exchange the pointers
|
1743 |
int buffersize = 0;
|
1744 |
buffersize = recvdatabuf[i]->bufsize;
|
1745 |
*bufsize = buffersize;
|
1746 |
// recvbuf = recvdatabuf[i]->recvbuf;
|
1747 |
|
1748 |
double nrMissFrags =
|
1749 |
(double) recvdatabuf[i]->nrFragments /
|
1750 |
(double) recvdatabuf[i]->recvFragments;
|
1751 |
int nrMissingFragments =
|
1752 |
(int) ceil(nrMissFrags);
|
1753 |
|
1754 |
// debug(" recvbuf %s \n",recvbuf );
|
1755 |
|
1756 |
memcpy(recvbuf, recvdatabuf[i]->recvbuf,
|
1757 |
buffersize);
|
1758 |
|
1759 |
rParams->nrMissingFragments =
|
1760 |
nrMissingFragments;
|
1761 |
rParams->nrFragments =
|
1762 |
recvdatabuf[i]->nrFragments;
|
1763 |
rParams->msgtype = recvdatabuf[i]->msgtype;
|
1764 |
rParams->connectionID =
|
1765 |
recvdatabuf[i]->connectionID;
|
1766 |
|
1767 |
if(get_Recv_data_inf_cb != NULL) {
|
1768 |
mon_data_inf recv_data_inf;
|
1769 |
|
1770 |
recv_data_inf.remote_socketID = &(connectbuf[connectionID]->external_socketID);
|
1771 |
recv_data_inf.buffer = recvdatabuf[i]->recvbuf;
|
1772 |
recv_data_inf.bufSize = recvdatabuf[i]->bufsize;
|
1773 |
recv_data_inf.msgtype = recvdatabuf[i]->msgtype;
|
1774 |
recv_data_inf.monitoringHeaderType = recvdatabuf[i]->monitoringHeaderType;
|
1775 |
recv_data_inf.monitoringDataHeader = recvdatabuf[i]->monitoringDataHeader;
|
1776 |
gettimeofday(&recv_data_inf.arrival_time, NULL);
|
1777 |
recv_data_inf.firstPacketArrived = recvdatabuf[i]->firstPacketArrived;
|
1778 |
recv_data_inf.nrMissingFragments = nrMissingFragments;
|
1779 |
recv_data_inf.nrFragments = recvdatabuf[i]->nrFragments;
|
1780 |
recv_data_inf.priority = false;
|
1781 |
recv_data_inf.padding = false;
|
1782 |
recv_data_inf.confirmation = false;
|
1783 |
recv_data_inf.reliable = false;
|
1784 |
|
1785 |
// send data recv callback to monitoring module
|
1786 |
|
1787 |
(get_Recv_data_inf_cb) ((void *) &recv_data_inf);
|
1788 |
}
|
1789 |
|
1790 |
// free the allocated memory
|
1791 |
free(recvdatabuf[i]);
|
1792 |
recvdatabuf[i] = NULL;
|
1793 |
|
1794 |
returnValue = 1;
|
1795 |
break;
|
1796 |
|
1797 |
}
|
1798 |
}
|
1799 |
|
1800 |
}
|
1801 |
|
1802 |
}
|
1803 |
// debug("2 recvbuf %s \n ",recvbuf);
|
1804 |
}
|
1805 |
return returnValue;
|
1806 |
}
|
1807 |
#endif
|
1808 |
|
1809 |
} |
1810 |
|
1811 |
int mlSocketIDToString(socketID_handle socketID,char* socketID_string, size_t len){ |
1812 |
|
1813 |
char internal_addr[INET_ADDRSTRLEN];
|
1814 |
char external_addr[INET_ADDRSTRLEN];
|
1815 |
inet_ntop(AF_INET, &(socketID->internal_addr.udpaddr.sin_addr.s_addr), internal_addr, INET_ADDRSTRLEN); |
1816 |
inet_ntop(AF_INET, &(socketID->external_addr.udpaddr.sin_addr.s_addr), external_addr, INET_ADDRSTRLEN); |
1817 |
|
1818 |
snprintf(socketID_string,len,"%s:%d-%s:%d", internal_addr, ntohs(socketID->internal_addr.udpaddr.sin_port),
|
1819 |
external_addr, ntohs(socketID->external_addr.udpaddr.sin_port)); |
1820 |
return 0; |
1821 |
|
1822 |
} |
1823 |
|
1824 |
int mlStringToSocketID(const char* socketID_string, socketID_handle socketID){ |
1825 |
|
1826 |
//@TODO add checks against malformed string
|
1827 |
char external_addr[INET_ADDRSTRLEN];
|
1828 |
int external_port;
|
1829 |
char internal_addr[INET_ADDRSTRLEN];
|
1830 |
int internal_port;
|
1831 |
|
1832 |
char *pch;
|
1833 |
char *s = strdup(socketID_string);
|
1834 |
|
1835 |
//replace ':' with a blank
|
1836 |
pch=strchr(s,':');
|
1837 |
while (pch!=NULL){ |
1838 |
*pch = ' ';
|
1839 |
pch=strchr(pch+1,':'); |
1840 |
} |
1841 |
pch=strchr(s,'-');
|
1842 |
if(pch) *pch = ' '; |
1843 |
|
1844 |
sscanf(s,"%s %d %s %d", internal_addr, &internal_port,
|
1845 |
external_addr, &external_port); |
1846 |
|
1847 |
if(inet_pton(AF_INET, internal_addr, &(socketID->internal_addr.udpaddr.sin_addr)) == 0) |
1848 |
return EINVAL;
|
1849 |
socketID->internal_addr.udpaddr.sin_family = AF_INET; |
1850 |
socketID->internal_addr.udpaddr.sin_port = htons(internal_port); |
1851 |
|
1852 |
|
1853 |
if(inet_pton(AF_INET, external_addr, &(socketID->external_addr.udpaddr.sin_addr)) ==0) |
1854 |
return EINVAL;
|
1855 |
socketID->external_addr.udpaddr.sin_family = AF_INET; |
1856 |
socketID->external_addr.udpaddr.sin_port = htons(external_port); |
1857 |
|
1858 |
free(s); |
1859 |
return 0; |
1860 |
|
1861 |
} |
1862 |
|
1863 |
int mlGetConnectionStatus(int connectionID){ |
1864 |
|
1865 |
if(connectbuf[connectionID])
|
1866 |
return connectbuf[connectionID]->status == READY;
|
1867 |
return -1; |
1868 |
|
1869 |
} |
1870 |
|
1871 |
|
1872 |
int mlConnectionExist(socketID_handle socketID, bool ready){ |
1873 |
|
1874 |
/*
|
1875 |
* check if another connection for the external connectionID exist
|
1876 |
* that was established \ within the last 2 seconds
|
1877 |
*/
|
1878 |
int i;
|
1879 |
for (i = 0; i < CONNECTBUFSIZE; i++) |
1880 |
if (connectbuf[i] != NULL) |
1881 |
if (mlCompareSocketIDs(&(connectbuf[i]->external_socketID), socketID) == 0) { |
1882 |
if (ready) return (connectbuf[i]->status == READY ? i : -1);; |
1883 |
return i;
|
1884 |
} |
1885 |
|
1886 |
return -1; |
1887 |
|
1888 |
} |
1889 |
|
1890 |
//Added by Robert Birke as comodity functions
|
1891 |
|
1892 |
//int mlPrintSocketID(socketID_handle socketID) {
|
1893 |
// char str[SOCKETID_STRING_SIZE];
|
1894 |
// mlSocketIDToString(socketID, str, sizeof(str));
|
1895 |
// printf(stderr,"int->%s<-ext\n",str);
|
1896 |
//}
|
1897 |
|
1898 |
/*
|
1899 |
* hash code of a socketID
|
1900 |
* TODO might think of a better way
|
1901 |
*/
|
1902 |
int mlHashSocketID(socketID_handle sock) {
|
1903 |
return sock->internal_addr.udpaddr.sin_port +
|
1904 |
sock->external_addr.udpaddr.sin_port; |
1905 |
} |
1906 |
|
1907 |
int mlCompareSocketIDs(socketID_handle sock1, socketID_handle sock2) {
|
1908 |
/*
|
1909 |
* compare internal addr
|
1910 |
*/
|
1911 |
if (sock1->internal_addr.udpaddr.sin_addr.s_addr !=
|
1912 |
sock2->internal_addr.udpaddr.sin_addr.s_addr) |
1913 |
return 1; |
1914 |
|
1915 |
if (sock1->internal_addr.udpaddr.sin_port !=
|
1916 |
sock2->internal_addr.udpaddr.sin_port) |
1917 |
return 1; |
1918 |
|
1919 |
/*
|
1920 |
* compare external addr
|
1921 |
*/
|
1922 |
if (sock1->external_addr.udpaddr.sin_addr.s_addr !=
|
1923 |
sock2->external_addr.udpaddr.sin_addr.s_addr) |
1924 |
return 1; |
1925 |
|
1926 |
if (sock1->external_addr.udpaddr.sin_port !=
|
1927 |
sock2->external_addr.udpaddr.sin_port) |
1928 |
return 1; |
1929 |
|
1930 |
return 0; |
1931 |
} |
1932 |
|
1933 |
int mlCompareSocketIDsByPort(socketID_handle sock1, socketID_handle sock2)
|
1934 |
{ |
1935 |
if (sock1->internal_addr.udpaddr.sin_port !=
|
1936 |
sock2->internal_addr.udpaddr.sin_port) |
1937 |
return 1; |
1938 |
|
1939 |
if (sock1->external_addr.udpaddr.sin_port !=
|
1940 |
sock2->external_addr.udpaddr.sin_port) |
1941 |
return 1; |
1942 |
return 0; |
1943 |
} |
1944 |
|
1945 |
int mlGetPathMTU(int ConnectionId) { |
1946 |
if(ConnectionId < 0 || ConnectionId >= CONNECTBUFSIZE) |
1947 |
return -1; |
1948 |
if (connectbuf[ConnectionId] != NULL) |
1949 |
return connectbuf[ConnectionId]->pmtusize;
|
1950 |
return -1; |
1951 |
} |
1952 |
|
1953 |
/**************************** END of GENERAL functions *************************/
|
1954 |
|
1955 |
/**************************** NAT functions *************************/
|
1956 |
|
1957 |
/* setter */
|
1958 |
void mlSetStunServer(const int port,const char *ipaddr){ |
1959 |
|
1960 |
stun_server.sin_family = AF_INET; |
1961 |
if (ipaddr == NULL) |
1962 |
stun_server.sin_addr.s_addr = htonl(INADDR_ANY); |
1963 |
else
|
1964 |
stun_server.sin_addr.s_addr = resolve(ipaddr); |
1965 |
stun_server.sin_port = htons(port); |
1966 |
|
1967 |
} |
1968 |
|
1969 |
int mlGetExternalIP(char* external_addr){ |
1970 |
|
1971 |
socketaddrgen udpgen; |
1972 |
struct sockaddr_in udpaddr;
|
1973 |
|
1974 |
udpgen = local_socketID.external_addr; |
1975 |
udpaddr = udpgen.udpaddr; |
1976 |
|
1977 |
inet_ntop(AF_INET, &(udpaddr.sin_addr), external_addr, |
1978 |
INET_ADDRSTRLEN); |
1979 |
|
1980 |
if (external_addr == NULL) { |
1981 |
|
1982 |
return -1; |
1983 |
|
1984 |
} else {
|
1985 |
|
1986 |
return 0; |
1987 |
|
1988 |
} |
1989 |
|
1990 |
} |
1991 |
|
1992 |
/**************************** END of NAT functions *************************/
|