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