Statistics
| Branch: | Revision:

ml / transmissionHandler.c @ 6214e30a

History | View | Annotate | Download (50.5 KB)

1 a0a1f630 KristianBeckers
/*
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 2a178954 KristianBeckers
#include <arpa/inet.h>
35 a0a1f630 KristianBeckers
#include <netinet/in.h>
36
#include <sys/socket.h>
37
#include <fcntl.h>
38
#include <event2/event.h>
39
#include <stdlib.h>
40
#include <unistd.h>
41
#include <stdio.h>
42
#include <stddef.h>
43 90cb1b52 TivadarSzemethy
#include <stdint.h>
44 a0a1f630 KristianBeckers
#include <string.h>
45
#include <sys/types.h>
46
#include <arpa/inet.h>
47 19c6ca17 TivadarSzemethy
#include <netdb.h>
48 a0a1f630 KristianBeckers
#include <errno.h>
49
#include <time.h>
50
#include <math.h>
51
#include "util/udpSocket.h"
52
#include "util/stun.h"
53
#include "transmissionHandler.h"
54 7455d709 TivadarSzemethy
55
#define LOG_MODULE "[ml] "
56 52f7925a TivadarSzemethy
#include "grapes_log.h"
57 a0a1f630 KristianBeckers
58 510f7930 RobertBirke
/*
59
 * a pointer to a libevent instance 
60
 */
61 2a178954 KristianBeckers
struct event_base *base;
62
63 510f7930 RobertBirke
/*
64
 * define the nr of connections the messaging layer can handle 
65
 */
66 a0a1f630 KristianBeckers
#define CONNECTBUFSIZE 10000
67 510f7930 RobertBirke
/*
68
 * define the nr of data that can be received parallel 
69
 */
70 a0a1f630 KristianBeckers
#define RECVDATABUFSIZE 10000
71 510f7930 RobertBirke
/*
72
 * define an array for message multiplexing 
73
 */
74 a0a1f630 KristianBeckers
#define MSGMULTIPLEXSIZE 127
75
76 510f7930 RobertBirke
/*
77
 * global variables 
78
 */
79
/*
80
 * define a buffer of pointers to connect structures 
81
 */
82 a0a1f630 KristianBeckers
connect_data *connectbuf[CONNECTBUFSIZE];
83
84 510f7930 RobertBirke
/*
85
 * define a pointer buffer with pointers to recv_data structures 
86
 */
87 a0a1f630 KristianBeckers
recvdata *recvdatabuf[RECVDATABUFSIZE];
88
89 510f7930 RobertBirke
/*
90
 * define a pointer buffer for message multiplexing 
91
 */
92 a0a1f630 KristianBeckers
receive_data_cb recvcbbuf[MSGMULTIPLEXSIZE];
93
94 510f7930 RobertBirke
/*
95
 * stun server address 
96
 */
97 a0a1f630 KristianBeckers
struct sockaddr_in stun_server;
98
99 510f7930 RobertBirke
/*
100
 * receive timeout 
101
 */
102 a0a1f630 KristianBeckers
struct timeval recv_timeout;
103
104 510f7930 RobertBirke
/*
105
 * boolean NAT traversal successful if true 
106
 */
107
boolean NAT_traversal;
108 2a178954 KristianBeckers
109 510f7930 RobertBirke
/*
110
 * file descriptor for local socket 
111
 */
112 2a178954 KristianBeckers
evutil_socket_t socketfd;
113
114 510f7930 RobertBirke
/*
115
 * local socketID 
116
 */
117 8b17961c KristianBeckers
socket_ID local_socketID;
118
119
socketID_handle loc_socketID = &local_socketID;
120 2a178954 KristianBeckers
121 510f7930 RobertBirke
/*
122
 * callback function pointers 
123
 */
124
/*
125
 * monitoring module callbacks 
126
 */
127 8601bccf RobertBirke
get_recv_pkt_inf_cb get_Recv_pkt_inf_cb = NULL;
128
get_send_pkt_inf_cb get_Send_pkt_inf_cb = NULL;
129
set_monitoring_header_pkt_cb set_Monitoring_header_pkt_cb = NULL;
130
get_recv_data_inf_cb get_Recv_data_inf_cb = NULL;
131
get_send_data_inf_cb get_Send_data_inf_cb = NULL;
132
set_monitoring_header_data_cb set_Monitoring_header_data_cb = NULL;
133 510f7930 RobertBirke
/*
134
 * connection callbacks 
135
 */
136 351ad48b RobertBirke
receive_connection_cb receive_Connection_cb = NULL;
137
connection_failed_cb failed_Connection_cb = NULL;
138 510f7930 RobertBirke
/*
139
 * local socketID callback 
140
 */
141
receive_localsocketID_cb receive_SocketID_cb;
142 a0a1f630 KristianBeckers
143 510f7930 RobertBirke
/*
144
 * boolean that defines if received data is transmitted to the upper layer 
145
 * via callback or via upper layer polling 
146
 */
147 a0a1f630 KristianBeckers
boolean recv_data_callback;
148
149 510f7930 RobertBirke
/*
150 86f87418 TivadarSzemethy
 * helper function to get rid of a warning
151
 */
152
int min(int a, int b) {
153
        if (a > b) return b;
154
        return a;
155
}
156
157
/*
158 510f7930 RobertBirke
 * functions 
159
 */
160
/*
161
 * register callback functions 
162
 */
163 a0a1f630 KristianBeckers
164 510f7930 RobertBirke
void register_get_recv_pkt_inf(get_recv_pkt_inf_cb recv_pkt_inf_cb)
165
{
166 8601bccf RobertBirke
        if (recv_pkt_inf_cb == NULL)
167
                printf("Register get_recv_pkt_inf_cb failed: NULL ptr  \n");
168
        else
169
                get_Recv_pkt_inf_cb = recv_pkt_inf_cb;
170
}
171 a0a1f630 KristianBeckers
172 8601bccf RobertBirke
void register_get_send_pkt_inf(get_send_pkt_inf_cb send_pkt_inf_cb)
173
{
174
        if (send_pkt_inf_cb == NULL)
175
                printf("Register get_send_pkt_inf_cb: NULL ptr  \n");
176
        else
177
                get_Send_pkt_inf_cb = send_pkt_inf_cb;
178 a0a1f630 KristianBeckers
}
179
180 510f7930 RobertBirke
void
181
register_set_monitoring_header_pkt_cb(set_monitoring_header_pkt_cb
182
                                      monitoring_header_pkt_cb)
183
{
184 8601bccf RobertBirke
        if (monitoring_header_pkt_cb == NULL)
185
                printf("Register set_monitoring_header_pkt_cb: NULL ptr  \n");
186
        else
187
                set_Monitoring_header_pkt_cb = monitoring_header_pkt_cb;
188 a0a1f630 KristianBeckers
}
189
190 510f7930 RobertBirke
void register_get_recv_data_inf(get_recv_data_inf_cb recv_data_inf_cb)
191
{
192 8601bccf RobertBirke
        if (recv_data_inf_cb == NULL)
193
                printf("Register get_recv_data_inf_cb: NULL ptr  \n");
194
        else
195
                get_Recv_data_inf_cb = recv_data_inf_cb;
196 a0a1f630 KristianBeckers
}
197
198 510f7930 RobertBirke
void register_get_send_data_inf(get_send_data_inf_cb send_data_inf_cb)
199
{
200 8601bccf RobertBirke
        if (send_data_inf_cb == NULL)
201
                printf("Register get_send_data_inf_cb: NULL ptr  \n");
202
        else
203
                get_Send_data_inf_cb = send_data_inf_cb;
204 a0a1f630 KristianBeckers
}
205
206 510f7930 RobertBirke
void
207
register_set_monitoring_header_data_cb(set_monitoring_header_data_cb
208
                                       monitoring_header_data_cb)
209
{
210 8601bccf RobertBirke
        if (monitoring_header_data_cb == NULL)
211
                printf("Register set_monitoring_header_data_cb : NULL ptr  \n");
212
        else
213 510f7930 RobertBirke
        set_Monitoring_header_data_cb = monitoring_header_data_cb;
214 a0a1f630 KristianBeckers
}
215
216 510f7930 RobertBirke
void register_recv_connection_cb(receive_connection_cb connection_cb)
217
{
218 8601bccf RobertBirke
        if (connection_cb == NULL)
219
                printf("Register receive_connection_cb: NULL ptr  \n");
220
        else
221
                receive_Connection_cb = connection_cb;
222 a0a1f630 KristianBeckers
}
223
224 510f7930 RobertBirke
void
225
register_recv_localsocketID_cb(receive_localsocketID_cb local_socketID_cb)
226
{
227 8601bccf RobertBirke
        if (local_socketID_cb == NULL)
228
                printf("Register receive_localsocketID_cb: NULL ptr \n");
229
        else
230 510f7930 RobertBirke
        receive_SocketID_cb = local_socketID_cb;
231 2a178954 KristianBeckers
}
232
233
234 510f7930 RobertBirke
void register_error_connection_cb(connection_failed_cb connection_fail)
235
{
236 8601bccf RobertBirke
        if (connection_fail == NULL)
237
                printf("Register connection_failed_cb: NULL ptr  \n");
238
        else
239 510f7930 RobertBirke
        failed_Connection_cb = connection_fail;
240 a0a1f630 KristianBeckers
}
241
242
243 510f7930 RobertBirke
void register_recv_data_cb(receive_data_cb data_cb, unsigned char msgtype)
244
{
245 a0a1f630 KristianBeckers
246 510f7930 RobertBirke
    if (msgtype > 126) {
247 a0a1f630 KristianBeckers
248 510f7930 RobertBirke
        printf
249
            ("transmissionHandler: Could not register recv_data callback. Msgtype is greater then 126 \n");
250 a0a1f630 KristianBeckers
251 510f7930 RobertBirke
    }
252 a0a1f630 KristianBeckers
253 510f7930 RobertBirke
    if (data_cb == NULL) {
254 a0a1f630 KristianBeckers
255 510f7930 RobertBirke
        printf("Register receive data callback: NUll ptr \n ");
256 a0a1f630 KristianBeckers
257 510f7930 RobertBirke
    } else {
258 a0a1f630 KristianBeckers
259 510f7930 RobertBirke
        recvcbbuf[msgtype] = data_cb;
260 a0a1f630 KristianBeckers
261 510f7930 RobertBirke
    }
262 a0a1f630 KristianBeckers
}
263
264 510f7930 RobertBirke
void
265
init_transmissionHandler(boolean recv_data_cb,
266
                         struct timeval timeout_value, const int port,
267
                         const char *ipaddr, const int stun_port,
268
                         const char *stun_ipaddr,
269
                         receive_localsocketID_cb local_socketID_cb,
270
                         void *arg)
271
{
272 351ad48b RobertBirke
        base = (struct event_base *) arg;
273
        recv_data_callback = recv_data_cb;
274
        setRecvTimeout(timeout_value);
275 bde41184 CsabaKiraly
        if (stun_ipaddr) {
276
                 setStunServer(stun_port, stun_ipaddr);
277
        } else {
278
        
279
        }
280 351ad48b RobertBirke
        register_recv_localsocketID_cb(local_socketID_cb);
281
        create_socket(port, ipaddr);
282 a0a1f630 KristianBeckers
}
283
284 510f7930 RobertBirke
/*
285
 * Sockets 
286
 */
287
/*
288
 * returns a handle to the socketID struct the ipaddr can be a null
289
 * pointer. Then all available ipaddr on the machine are choosen.
290
 */
291
void create_socket(const int port, const char *ipaddr)
292
{
293 351ad48b RobertBirke
        struct sockaddr_in udpaddr;
294
        udpaddr.sin_family = AF_INET;
295 fa71e35e CsabaKiraly
        if (ipaddr == NULL) {
296
                /*
297
                * try to guess the local IP address
298
                */
299
                const char *ipaddr_iface = autodetect_ipaddress();
300
                if (ipaddr_iface) {
301
                        udpaddr.sin_addr.s_addr = inet_addr(ipaddr_iface);
302
                } else {
303
                        udpaddr.sin_addr.s_addr = INADDR_ANY;
304
                }
305
        } else {
306 351ad48b RobertBirke
                udpaddr.sin_addr.s_addr = inet_addr(ipaddr);
307 fa71e35e CsabaKiraly
        }
308 351ad48b RobertBirke
        udpaddr.sin_port = htons(port);
309 510f7930 RobertBirke
310 351ad48b RobertBirke
        socketaddrgen udpgen;
311
        udpgen.udpaddr = udpaddr;
312
        local_socketID.internal_addr = udpgen;
313
        
314
        socketfd = createSocket(port, ipaddr);
315
        
316
        struct event *ev;
317
        ev = event_new(base, socketfd, EV_READ | EV_PERSIST, recv_pkg, NULL);
318
        
319
        event_add(ev, NULL);
320
        
321 bde41184 CsabaKiraly
        if (isStunDefined()) {
322
                /*
323
                * send the NAT traversal STUN request 
324
                */
325
                 send_stun_request(socketfd, &stun_server);
326
327
                /*
328
                * enter a NAT traversal timeout that takes care of retransmission 
329
                */
330
                struct event *ev1;
331
                struct timeval timeout_value_NAT_traversal = { 2, 0 };
332
                ev1 = evtimer_new(base, nat_traversal_timeout, NULL);
333
                event_add(ev1, &timeout_value_NAT_traversal);
334
335
                NAT_traversal = false;
336
        } else {
337
                /*
338
                * Assume we have accessibility and copy internal address to external one
339
                */
340
                local_socketID.external_addr = local_socketID.internal_addr;
341
                NAT_traversal = true; // @TODO: this is not really NAT traversal, but a flag that init is over
342
                // callback to the upper layer indicating that the socketID is now
343
                // ready to use
344
                (receive_SocketID_cb) (&local_socketID, 0);
345
        }
346 510f7930 RobertBirke
}
347 2a178954 KristianBeckers
348 510f7930 RobertBirke
void close_socket(socketID_handle socketID)
349
{
350 351ad48b RobertBirke
        free(socketID);
351 510f7930 RobertBirke
}
352 2a178954 KristianBeckers
353 510f7930 RobertBirke
/*
354
 * Connections 
355
 */
356
// the first socketID is the from socketID, the second one is the to
357
// socketID
358 2a178954 KristianBeckers
359 510f7930 RobertBirke
int
360 380cf1b9 RobertBirke
open_connection(socketID_handle external_socketID, receive_connection_cb connection_cb, void *arg)
361 510f7930 RobertBirke
{
362 380cf1b9 RobertBirke
        int con_id;
363 8601bccf RobertBirke
        if (external_socketID == NULL) {
364 380cf1b9 RobertBirke
                error("ML: cannot open connection: one of the socketIDs is NULL");
365
                return -1;
366 8601bccf RobertBirke
        } 
367
        if (NAT_traversal == false) {
368 380cf1b9 RobertBirke
                error("ML: cannot open connection: NAT traversal for socketID still in progress");
369
                return -1;
370 8601bccf RobertBirke
        }
371
        if (connection_cb == NULL) {
372 380cf1b9 RobertBirke
                error("ML: cannot open connection: connection_cb is NULL");
373
                return -1;
374 8601bccf RobertBirke
        }
375 a0a1f630 KristianBeckers
376 510f7930 RobertBirke
        // check if that connection already exist
377 380cf1b9 RobertBirke
378 6214e30a TivadarSzemethy
        con_id = connection_exist(external_socketID, false);
379 1af63d85 RobertBirke
        if (con_id >= 0) {
380 380cf1b9 RobertBirke
                // if so check if it is ready to use        
381 6bbd38ef RobertBirke
                if (connectbuf[con_id]->status == READY) {
382 380cf1b9 RobertBirke
                                // if so use the callback immidiatley
383
                                (connection_cb) (con_id, arg);
384
385 8601bccf RobertBirke
                // otherwise just write the connection cb and the arg pointer
386
                // into the connection struct
387 380cf1b9 RobertBirke
                } else {
388 8601bccf RobertBirke
                        struct receive_connection_cb_list *temp;
389
                        temp = malloc(sizeof(struct receive_connection_cb_list));
390
                        temp->next = NULL;
391 4f1a41e6 RobertBirke
                        temp->connection_cb = connection_cb;
392
                        temp->arg = arg;
393 380cf1b9 RobertBirke
                        if(connectbuf[con_id]->connection_last != NULL) {
394
                                connectbuf[con_id]->connection_last->next = temp;
395
                                connectbuf[con_id]->connection_last = temp;
396 8601bccf RobertBirke
                        } else
397 380cf1b9 RobertBirke
                                connectbuf[con_id]->connection_last = connectbuf[con_id]->connection_head = temp;
398 510f7930 RobertBirke
                }
399 380cf1b9 RobertBirke
                return con_id;
400 510f7930 RobertBirke
        }
401 380cf1b9 RobertBirke
        // make entry in connection_establishment array
402
        for (con_id = 0; con_id < CONNECTBUFSIZE; con_id++) {
403
                if (connectbuf[con_id] == NULL) {
404
                        connectbuf[con_id] = (connect_data *) malloc(sizeof(connect_data));
405
                        memset(connectbuf[con_id],0,sizeof(connect_data));
406
                        connectbuf[con_id]->starttime = time(NULL);
407
                        memcpy(&connectbuf[con_id]->external_socketID, external_socketID, sizeof(socket_ID));
408
                        connectbuf[con_id]->pmtusize = MAX;
409
                        connectbuf[con_id]->status = INVITE;
410
                        connectbuf[con_id]->seqnr = 0;
411
                        connectbuf[con_id]->internal_connect = !compare_external_address_socketIDs(external_socketID, &local_socketID);
412
                        /*
413
                        * timeout values for the pmtu discovery 
414
                        */
415
                        connectbuf[con_id]->timeout_value.tv_sec = 15;
416
                        connectbuf[con_id]->timeout_value.tv_usec = 0;
417
                        connectbuf[con_id]->connectionID = con_id;
418
419
                        connectbuf[con_id]->connection_head = connectbuf[con_id]->connection_last = malloc(sizeof(struct receive_connection_cb_list));
420
                        connectbuf[con_id]->connection_last->next = NULL;
421
                        connectbuf[con_id]->connection_last->connection_cb = connection_cb;
422
                        connectbuf[con_id]->connection_last->arg = arg;
423 1b0e6d04 RobertBirke
                        connectbuf[con_id]->external_connectionID = -1;
424 380cf1b9 RobertBirke
                        break;
425
                }
426
        } //end of for
427 2a178954 KristianBeckers
428 380cf1b9 RobertBirke
        if (con_id == CONNECTBUFSIZE) {
429
                error("ML: Could not open connection: connbuffer full");
430
                return -1;
431
        }
432 2a178954 KristianBeckers
433 510f7930 RobertBirke
        // create and send a connection message
434 1af63d85 RobertBirke
        send_conn_msg(con_id, MAX, INVITE);
435 2a178954 KristianBeckers
436 510f7930 RobertBirke
        struct event *ev;
437 d42c2650 TivadarSzemethy
        ev = evtimer_new(base, pmtu_timeout_cb, (void *) (long)con_id);
438 380cf1b9 RobertBirke
        event_add(ev, &connectbuf[con_id]->timeout_value);
439 2a178954 KristianBeckers
440 380cf1b9 RobertBirke
        return con_id;
441 510f7930 RobertBirke
}
442 2a178954 KristianBeckers
443 380cf1b9 RobertBirke
//done
444 510f7930 RobertBirke
void close_connection(const int connectionID)
445
{
446 351ad48b RobertBirke
        // remove it from the connection array
447
        if(connectbuf[connectionID]) {
448 380cf1b9 RobertBirke
                if(connectbuf[connectionID]->ctrl_msg_buf)
449
                        free(connectbuf[connectionID]->ctrl_msg_buf);
450 351ad48b RobertBirke
                free(connectbuf[connectionID]);
451
                connectbuf[connectionID] = NULL;
452
        }
453 510f7930 RobertBirke
}
454 2a178954 KristianBeckers
455 510f7930 RobertBirke
void keep_connection_alive(const int connectionID)
456
{
457 2a178954 KristianBeckers
458 510f7930 RobertBirke
    // to be done with the NAT traversal
459
    // send a message over the wire
460
    printf("\n");
461 2a178954 KristianBeckers
462
}
463
464 510f7930 RobertBirke
int
465
send_all_data(const int connectionID, send_all_data_container * container,
466
              int nr_entries, unsigned char msgtype, send_params * sParams)
467
{
468
469 a0a1f630 KristianBeckers
470
471 510f7930 RobertBirke
    if (nr_entries < 1 || nr_entries > 5) {
472 a0a1f630 KristianBeckers
473 510f7930 RobertBirke
        printf
474 380cf1b9 RobertBirke
            ("send_all_data ERROR: nr_enties is not between 1 and 5 \n ");
475 510f7930 RobertBirke
        return 0;
476 a0a1f630 KristianBeckers
477 510f7930 RobertBirke
    } else {
478 a0a1f630 KristianBeckers
479 510f7930 RobertBirke
        if (nr_entries == 1) {
480 a0a1f630 KristianBeckers
481 510f7930 RobertBirke
            send_data(connectionID, container->buffer_1,
482
                      container->length_1, msgtype, sParams);
483 a0a1f630 KristianBeckers
484 510f7930 RobertBirke
            return 1;
485 a0a1f630 KristianBeckers
486 510f7930 RobertBirke
        } else if (nr_entries == 2) {
487 a0a1f630 KristianBeckers
488 510f7930 RobertBirke
            int buflen = container->length_1 + container->length_2;
489
            char buf[buflen];
490
            memcpy(buf, container->buffer_1, container->length_1);
491
            memcpy(&buf[container->length_1], container->buffer_2,
492
                   container->length_2);
493
            send_data(connectionID, buf, buflen, msgtype, sParams);
494 a0a1f630 KristianBeckers
495 510f7930 RobertBirke
            return 1;
496 a0a1f630 KristianBeckers
497 510f7930 RobertBirke
        } else if (nr_entries == 3) {
498 a0a1f630 KristianBeckers
499 510f7930 RobertBirke
            int buflen =
500
                container->length_1 + container->length_2 +
501
                container->length_3;
502
            char buf[buflen];
503
            memcpy(buf, container->buffer_1, container->length_1);
504
            memcpy(&buf[container->length_1], container->buffer_2,
505
                   container->length_2);
506
            memcpy(&buf[container->length_2], container->buffer_3,
507
                   container->length_3);
508
            send_data(connectionID, buf, buflen, msgtype, sParams);
509 a0a1f630 KristianBeckers
510
511 510f7930 RobertBirke
            return 1;
512 a0a1f630 KristianBeckers
513 510f7930 RobertBirke
        } else if (nr_entries == 4) {
514 a0a1f630 KristianBeckers
515 510f7930 RobertBirke
            int buflen =
516
                container->length_1 + container->length_2 +
517
                container->length_3 + container->length_4;
518
            char buf[buflen];
519
            memcpy(buf, container->buffer_1, container->length_1);
520
            memcpy(&buf[container->length_1], container->buffer_2,
521
                   container->length_2);
522
            memcpy(&buf[container->length_2], container->buffer_3,
523
                   container->length_3);
524
            memcpy(&buf[container->length_3], container->buffer_4,
525
                   container->length_4);
526
            send_data(connectionID, buf, buflen, msgtype, sParams);
527 a0a1f630 KristianBeckers
528 510f7930 RobertBirke
            return 1;
529 a0a1f630 KristianBeckers
530 510f7930 RobertBirke
        } else {
531 a0a1f630 KristianBeckers
532 510f7930 RobertBirke
            int buflen =
533
                container->length_1 + container->length_2 +
534
                container->length_3 + container->length_4 +
535
                container->length_5;
536
            char buf[buflen];
537
            memcpy(buf, container->buffer_1, container->length_1);
538
            memcpy(&buf[container->length_1], container->buffer_2,
539
                   container->length_2);
540
            memcpy(&buf[container->length_2], container->buffer_3,
541
                   container->length_3);
542
            memcpy(&buf[container->length_3], container->buffer_4,
543
                   container->length_4);
544
            memcpy(&buf[container->length_4], container->buffer_5,
545
                   container->length_5);
546
            send_data(connectionID, buf, buflen, msgtype, sParams);
547 a0a1f630 KristianBeckers
548 510f7930 RobertBirke
            return 1;
549
        }
550 a0a1f630 KristianBeckers
551
    }
552
553
554 510f7930 RobertBirke
}
555 a0a1f630 KristianBeckers
556 510f7930 RobertBirke
/*
557
 * send an entire block of data 
558
 */
559 1af63d85 RobertBirke
void send_data(int con_id, char *sendbuf, int bufsize, int msgtype, send_params * sParams)
560 510f7930 RobertBirke
{
561 351ad48b RobertBirke
        if (sParams == NULL) {
562 1af63d85 RobertBirke
                error("ML: send data failed: send_params is a NULL ptr");
563 351ad48b RobertBirke
                return;
564
        }
565 510f7930 RobertBirke
566 1af63d85 RobertBirke
        if (con_id < 0) {
567
                error("ML: send data failed: connectionID does not exist");
568
                return;
569 510f7930 RobertBirke
        }
570
571 1af63d85 RobertBirke
        if (connectbuf[con_id] == NULL) {
572
                error("ML: send data failed: connectionID does not exist");
573
                return;
574 8601bccf RobertBirke
        }
575 6bbd38ef RobertBirke
        if (connectbuf[con_id]->status != READY) {
576 1af63d85 RobertBirke
            error("ML: send data failed: connection is not active");
577
            return;
578 510f7930 RobertBirke
        }
579 a0a1f630 KristianBeckers
580 1af63d85 RobertBirke
        send_msg(con_id, msgtype, sendbuf, bufsize, false, sParams);
581 a0a1f630 KristianBeckers
}
582
583 510f7930 RobertBirke
/*
584
 * recv data with polling 
585
 */
586
int
587
recv_data(const int connectionID, char *recvbuf, int *bufsize,
588
          recv_params * rParams)
589
{
590 1af63d85 RobertBirke
        //TODO yet to be converted
591
        return 0;
592
#if 0
593 380cf1b9 RobertBirke
        if (rParams == NULL) {
594
                error("ML: recv_data failed: recv_params is a NULL ptr");
595
                return 0;
596 510f7930 RobertBirke
    } else {
597 a0a1f630 KristianBeckers

598 510f7930 RobertBirke
        printf("transmissionhandler: recv data called \n");
599 a0a1f630 KristianBeckers

600 510f7930 RobertBirke
        int i = 0;
601
        int returnValue = 0;
602
        double timeout = (double) recv_timeout.tv_sec;
603
        time_t endtime = time(NULL);
604 a0a1f630 KristianBeckers

605 510f7930 RobertBirke
        for (i = 0; i < RECVDATABUFSIZE; i++) {
606 a0a1f630 KristianBeckers

607 510f7930 RobertBirke
            if (recvdatabuf[i] != NULL) {
608 a0a1f630 KristianBeckers

609 510f7930 RobertBirke
                if (recvdatabuf[i]->connectionID == connectionID) {
610 a0a1f630 KristianBeckers

611 510f7930 RobertBirke
                    printf("transmissionhandler: recv data has entry  \n");
612 a0a1f630 KristianBeckers

613 8601bccf RobertBirke
                    double timepass = difftime(endtime, recvdatabuf[i]->starttime);
614 a0a1f630 KristianBeckers

615 510f7930 RobertBirke
                    // check if the specified connection has data and it
616
                    // is complete
617
                    // check the data seqnr
618
                    // if(connectionID == recvdatabuf[i]->connectionID &&
619
                    // 1 == recvdatabuf[i]->status){
620 a0a1f630 KristianBeckers

621 510f7930 RobertBirke
                    if (1 == recvdatabuf[i]->status) {
622 a0a1f630 KristianBeckers

623 510f7930 RobertBirke
                        // printf("transmissionHandler: recv_data set is
624
                        // complete \n" );
625 a0a1f630 KristianBeckers

626 510f7930 RobertBirke
                        // printf("debud \n");
627 a0a1f630 KristianBeckers

628 510f7930 RobertBirke
                        // exchange the pointers
629
                        int buffersize = 0;
630
                        buffersize = recvdatabuf[i]->bufsize;
631
                        *bufsize = buffersize;
632
                        // recvbuf = recvdatabuf[i]->recvbuf;
633

634
                        // printf("buffersize %d \n",buffersize);
635
                        memcpy(recvbuf, recvdatabuf[i]->recvbuf,
636
                               buffersize);
637
                        // printf(" recvbuf %s \n",recvbuf );
638 a0a1f630 KristianBeckers

639 1af63d85 RobertBirke
//                         double nrMissFrags =
640
//                             (double) recvdatabuf[i]->nrFragments /
641
//                             (double) recvdatabuf[i]->recvFragments;
642
//                         int nrMissingFragments = (int) ceil(nrMissFrags);
643 a0a1f630 KristianBeckers

644 1af63d85 RobertBirke
//                        rParams->nrMissingFragments = nrMissingFragments;
645
//                         rParams->nrFragments = recvdatabuf[i]->nrFragments;
646 510f7930 RobertBirke
                        rParams->msgtype = recvdatabuf[i]->msgtype;
647
                        rParams->connectionID =
648
                            recvdatabuf[i]->connectionID;
649 a0a1f630 KristianBeckers

650 510f7930 RobertBirke
                        // break from the loop
651
                        // printf(" recvbuf %s \n ",recvbuf);
652 a0a1f630 KristianBeckers

653 510f7930 RobertBirke
                        // double nrMissFrags =
654
                        // (double)recvdatabuf[i]->nrFragments /
655
                        // (double)recvdatabuf[i]->recvFragments;
656
                        // int nrMissingFragments =
657
                        // (int)ceil(nrMissFrags);
658 a0a1f630 KristianBeckers

659 8601bccf RobertBirke
                        if(get_Recv_data_inf_cb != NULL) {
660
                                mon_data_inf recv_data_inf;
661

662 380cf1b9 RobertBirke
                                recv_data_inf.remote_socketID = &(connectbuf[connectionID]->external_socketID);
663 8601bccf RobertBirke
                                recv_data_inf.buffer = recvdatabuf[i]->recvbuf;
664
                                recv_data_inf.bufSize = recvdatabuf[i]->bufsize;
665
                                recv_data_inf.msgtype = recvdatabuf[i]->msgtype;
666 1af63d85 RobertBirke
//                                 recv_data_inf.monitoringHeaderType = recvdatabuf[i]->monitoringHeaderType;
667
//                                 recv_data_inf.monitoringDataHeader = recvdatabuf[i]->monitoringDataHeader;
668 8601bccf RobertBirke
                                gettimeofday(&recv_data_inf.arrival_time, NULL);
669
                                recv_data_inf.firstPacketArrived = recvdatabuf[i]->firstPacketArrived;
670
                                recv_data_inf.nrMissingFragments = nrMissingFragments;
671
                                recv_data_inf.nrFragments = recvdatabuf[i]->nrFragments;
672
                                recv_data_inf.priority = false;
673
                                recv_data_inf.padding = false;
674
                                recv_data_inf.confirmation = false;
675
                                recv_data_inf.reliable = false;
676

677
                                // send data recv callback to monitoring module
678

679
                                (get_Recv_data_inf_cb) ((void *) &recv_data_inf);
680
                        }
681 a0a1f630 KristianBeckers

682

683 510f7930 RobertBirke
                        // free the allocated memory
684
                        free(recvdatabuf[i]);
685
                        recvdatabuf[i] = NULL;
686 a0a1f630 KristianBeckers

687 510f7930 RobertBirke
                        returnValue = 1;
688
                        break;
689 a0a1f630 KristianBeckers

690 510f7930 RobertBirke
                    }
691 a0a1f630 KristianBeckers

692 510f7930 RobertBirke
                    if (recvdatabuf[i] != NULL) {
693 a0a1f630 KristianBeckers

694 510f7930 RobertBirke
                        if (timepass > timeout) {
695 a0a1f630 KristianBeckers

696 8601bccf RobertBirke
                            printf("transmissionHandler: recv_data timeout called  \n");
697 a0a1f630 KristianBeckers

698 510f7930 RobertBirke
                            // some data about the missing chunks should
699
                            // be added here
700
                            // exchange the pointers 
701
                            int buffersize = 0;
702
                            buffersize = recvdatabuf[i]->bufsize;
703
                            *bufsize = buffersize;
704
                            // recvbuf = recvdatabuf[i]->recvbuf;
705

706
                            double nrMissFrags =
707
                                (double) recvdatabuf[i]->nrFragments /
708
                                (double) recvdatabuf[i]->recvFragments;
709
                            int nrMissingFragments =
710
                                (int) ceil(nrMissFrags);
711

712
                            // printf(" recvbuf %s \n",recvbuf );
713

714
                            memcpy(recvbuf, recvdatabuf[i]->recvbuf,
715
                                   buffersize);
716

717
                            rParams->nrMissingFragments =
718
                                nrMissingFragments;
719
                            rParams->nrFragments =
720
                                recvdatabuf[i]->nrFragments;
721
                            rParams->msgtype = recvdatabuf[i]->msgtype;
722
                            rParams->connectionID =
723
                                recvdatabuf[i]->connectionID;
724 8601bccf RobertBirke

725
                                if(get_Recv_data_inf_cb != NULL) {
726
                                        mon_data_inf recv_data_inf;
727

728 380cf1b9 RobertBirke
                                        recv_data_inf.remote_socketID = &(connectbuf[connectionID]->external_socketID);
729 8601bccf RobertBirke
                                        recv_data_inf.buffer = recvdatabuf[i]->recvbuf;
730
                                        recv_data_inf.bufSize = recvdatabuf[i]->bufsize;
731
                                        recv_data_inf.msgtype = recvdatabuf[i]->msgtype;
732
                                        recv_data_inf.monitoringHeaderType = recvdatabuf[i]->monitoringHeaderType;
733
                                        recv_data_inf.monitoringDataHeader = recvdatabuf[i]->monitoringDataHeader;
734
                                        gettimeofday(&recv_data_inf.arrival_time, NULL);
735
                                        recv_data_inf.firstPacketArrived = recvdatabuf[i]->firstPacketArrived;
736
                                        recv_data_inf.nrMissingFragments = nrMissingFragments;
737
                                        recv_data_inf.nrFragments = recvdatabuf[i]->nrFragments;
738
                                        recv_data_inf.priority = false;
739
                                        recv_data_inf.padding = false;
740
                                        recv_data_inf.confirmation = false;
741
                                        recv_data_inf.reliable = false;
742

743
                                        // send data recv callback to monitoring module
744

745
                                        (get_Recv_data_inf_cb) ((void *) &recv_data_inf);
746
                                }
747

748 510f7930 RobertBirke
                            // free the allocated memory 
749
                            free(recvdatabuf[i]);
750
                            recvdatabuf[i] = NULL;
751

752
                            returnValue = 1;
753
                            break;
754

755
                        }
756
                    }
757

758
                }
759 a0a1f630 KristianBeckers

760 510f7930 RobertBirke
            }
761
            // printf("2 recvbuf %s \n ",recvbuf);
762
        }
763
        return returnValue;
764
    }
765 1af63d85 RobertBirke
#endif
766 a0a1f630 KristianBeckers
}
767
768 510f7930 RobertBirke
void setStunServer(const int port, const char *ipaddr)
769
{
770 351ad48b RobertBirke
        stun_server.sin_family = AF_INET;
771
        if (ipaddr == NULL)
772
                stun_server.sin_addr.s_addr = htonl(INADDR_ANY);
773
        else
774
                stun_server.sin_addr.s_addr = resolve(ipaddr);
775
        stun_server.sin_port = htons(port);
776 a0a1f630 KristianBeckers
}
777
778 bde41184 CsabaKiraly
void unsetStunServer()
779
{
780
        stun_server.sin_addr.s_addr = 0;
781
}
782
783
bool isStunDefined()
784
{
785
        return stun_server.sin_addr.s_addr;
786
}
787
788 1af63d85 RobertBirke
void setRecvTimeout(struct timeval timeout_value) {
789
        recv_timeout = timeout_value;
790 a0a1f630 KristianBeckers
}
791 1af63d85 RobertBirke
 
792
void send_msg(int con_id, int msg_type, char* msg, int msg_len, bool truncable, send_params * sParams) {
793
        socketaddrgen udpgen;
794
        bool retry;
795
        int pkt_len, offset;
796
        struct iovec iov[4];
797 a0a1f630 KristianBeckers
798 1af63d85 RobertBirke
        char h_pkt[MON_HEADER_SPACE];
799
        char h_data[MON_HEADER_SPACE];
800 a0a1f630 KristianBeckers
801 1af63d85 RobertBirke
        struct msg_header msg_h;
802 380cf1b9 RobertBirke
803 1af63d85 RobertBirke
        iov[0].iov_base = &msg_h;
804
        iov[0].iov_len = MSG_HEADER_SIZE;
805 380cf1b9 RobertBirke
806 1af63d85 RobertBirke
        msg_h.local_con_id = con_id;
807 1b0e6d04 RobertBirke
        msg_h.remote_con_id = connectbuf[con_id]->external_connectionID;
808 1af63d85 RobertBirke
        msg_h.msg_type = msg_type;
809
        msg_h.msg_seq_num = connectbuf[con_id]->seqnr++;
810 a0a1f630 KristianBeckers
811 1b0e6d04 RobertBirke
812 1af63d85 RobertBirke
        iov[1].iov_len = iov[2].iov_len = 0;
813
        iov[1].iov_base = h_pkt;
814
        iov[2].iov_base = h_data;
815 a0a1f630 KristianBeckers
816
817 1af63d85 RobertBirke
        if (connectbuf[con_id]->internal_connect)
818
                udpgen = connectbuf[con_id]->external_socketID.internal_addr;
819
        else
820
                udpgen = connectbuf[con_id]->external_socketID.external_addr;
821
822
        do{
823
                offset = 0;
824
                retry = false;
825
                // Monitoring layer hook
826 6bbd38ef RobertBirke
                if(set_Monitoring_header_data_cb != NULL) {
827
                        iov[2].iov_len = ((set_Monitoring_header_data_cb) (&(connectbuf[con_id]->external_socketID), msg_type));
828
                }
829
                msg_h.len_mon_data_hdr = iov[2].iov_len;
830
831 a3fd121c RobertBirke
                if(get_Send_data_inf_cb != NULL && iov[2].iov_len != 0) {
832 1af63d85 RobertBirke
                        mon_data_inf sd_data_inf;
833
                        
834
                        sd_data_inf.remote_socketID = &(connectbuf[con_id]->external_socketID);
835
                        sd_data_inf.buffer = msg;
836
                        sd_data_inf.bufSize = msg_len;
837
                        sd_data_inf.msgtype = msg_type;
838
                        sd_data_inf.monitoringDataHeader = iov[2].iov_base;
839
                        sd_data_inf.monitoringDataHeaderLen = iov[2].iov_len;
840
                        sd_data_inf.priority = sParams->priority;
841
                        sd_data_inf.padding = sParams->padding;
842
                        sd_data_inf.confirmation = sParams->confirmation;
843
                        sd_data_inf.reliable = sParams->reliable;
844
                        memset(&sd_data_inf.arrival_time, 0, sizeof(struct timeval));
845
        
846
                        (get_Send_data_inf_cb) ((void *) &sd_data_inf);
847
                }
848 a0a1f630 KristianBeckers
849 1af63d85 RobertBirke
                do {
850 a3fd121c RobertBirke
                        if(set_Monitoring_header_pkt_cb != NULL) {
851
                                iov[1].iov_len = (set_Monitoring_header_pkt_cb) (&(connectbuf[con_id]->external_socketID), msg_type);
852
                        }
853 86f87418 TivadarSzemethy
                        pkt_len = min(connectbuf[con_id]->pmtusize - iov[2].iov_len - iov[1].iov_len - iov[0].iov_len, msg_len - offset) ;
854 a3fd121c RobertBirke
855 1af63d85 RobertBirke
                        iov[3].iov_len = pkt_len;
856
                        iov[3].iov_base = msg + offset;
857 a3fd121c RobertBirke
858 1af63d85 RobertBirke
                        //fill header
859
                        msg_h.len_mon_packet_hdr = iov[1].iov_len;
860
                        msg_h.offset = offset;
861 6bbd38ef RobertBirke
                        msg_h.msg_length = truncable ? pkt_len : msg_len;
862 1af63d85 RobertBirke
                        
863
                        //monitoring layer hook
864 a3fd121c RobertBirke
                        if(get_Send_pkt_inf_cb != NULL && iov[1].iov_len) {
865 1af63d85 RobertBirke
                                mon_pkt_inf pkt_info;
866
867
                                pkt_info.remote_socketID = &(connectbuf[con_id]->external_socketID);
868
                                pkt_info.buffer = msg + offset;
869
                                pkt_info.bufSize = pkt_len;
870
                                pkt_info.msgtype = msg_type;
871
                                pkt_info.dataID = connectbuf[con_id]->seqnr;
872
                                pkt_info.offset = offset;
873
                                pkt_info.datasize = msg_len;
874
                                pkt_info.monitoringHeaderLen = iov[1].iov_len;
875
                                pkt_info.monitoringHeader = iov[1].iov_base;
876
                                pkt_info.ttl = -1;
877
                                memset(&(pkt_info.arrival_time),0,sizeof(struct timeval));
878
        
879
                                (get_Send_pkt_inf_cb) ((void *) &pkt_info);
880
                        }
881 510f7930 RobertBirke
882 a0a1f630 KristianBeckers
883 1af63d85 RobertBirke
                        switch(sendPacket(socketfd, iov, 4, &udpgen.udpaddr)) {
884
                                case MSGLEN:
885
                                        connectbuf[con_id]->pmtusize = pmtu_decrement(connectbuf[con_id]->pmtusize);
886
                                        connectbuf[con_id]->delay = true;
887
                                        retry = true;
888
                                        offset = msg_len; // exit the while
889
                                        break;
890
                                case FAILURE:
891
                                        offset = msg_len; // exit the while
892
                                        break;
893
                                case OK:
894
                                        //update
895 6bbd38ef RobertBirke
                                        offset += pkt_len + iov[2].iov_len;
896
                                        //transmit data header only in the first packet
897
                                        iov[2].iov_len = 0;
898 1af63d85 RobertBirke
                                        break;
899
                        }
900 6bbd38ef RobertBirke
                } while(offset != msg_len + msg_h.len_mon_data_hdr && !truncable);
901 1af63d85 RobertBirke
        } while(retry);
902
}
903 a0a1f630 KristianBeckers
904 1af63d85 RobertBirke
void send_conn_msg(int con_id, int buf_size, int command_type)
905
{
906
        send_params sparams;
907
        if(connectbuf[con_id]->ctrl_msg_buf == NULL)
908
                connectbuf[con_id]->ctrl_msg_buf = malloc(MAX);
909 351ad48b RobertBirke
910 1af63d85 RobertBirke
        if(connectbuf[con_id]->ctrl_msg_buf == NULL) {
911
                error("ML: can not allocate memory for connection message");
912
                return;
913
        }
914 a0a1f630 KristianBeckers
915 1af63d85 RobertBirke
        struct conn_msg *msg_header = (struct conn_msg*) connectbuf[con_id]->ctrl_msg_buf;
916 a0a1f630 KristianBeckers
917 1af63d85 RobertBirke
        msg_header->comand_type = command_type;
918
        msg_header->pmtu_size = connectbuf[con_id]->pmtusize;
919 a0a1f630 KristianBeckers
920 1af63d85 RobertBirke
        memcpy(&(msg_header->sock_id), loc_socketID, sizeof(socket_ID));
921 a0a1f630 KristianBeckers
922 1af63d85 RobertBirke
        send_msg(con_id, ML_CON_MSG, connectbuf[con_id]->ctrl_msg_buf, buf_size, true, &sparams);
923 510f7930 RobertBirke
}
924 a0a1f630 KristianBeckers
925 510f7930 RobertBirke
void
926 1af63d85 RobertBirke
recv_conn_msg(struct msg_header *msg_h, char *msgbuf, int bufsize)
927 510f7930 RobertBirke
{
928 6bbd38ef RobertBirke
        struct conn_msg *con_msg;
929 380cf1b9 RobertBirke
        int free_con_id, con_id;
930 510f7930 RobertBirke
931 380cf1b9 RobertBirke
        time_t now = time(NULL);
932
        double timediff = 0.0;
933 a0a1f630 KristianBeckers
934 6bbd38ef RobertBirke
        // Monitoring layer hook
935
        if(get_Recv_data_inf_cb != NULL && msg_h->len_mon_data_hdr != 0) {
936
                // update pointer to the real data
937
                msgbuf += msg_h->len_mon_data_hdr;
938
                bufsize -= msg_h->len_mon_data_hdr;
939
                con_msg = (struct conn_msg *)msgbuf;
940
941
                mon_data_inf recv_data_inf;
942
                recv_data_inf.remote_socketID = &(con_msg->sock_id);
943
                recv_data_inf.buffer = msgbuf;
944
                recv_data_inf.bufSize = bufsize;
945
                recv_data_inf.msgtype = msg_h->msg_type;
946
                recv_data_inf.monitoringDataHeaderLen = msg_h->len_mon_data_hdr;
947
                recv_data_inf.monitoringDataHeader = msg_h->len_mon_data_hdr ? msgbuf : NULL;
948
                gettimeofday(&recv_data_inf.arrival_time, NULL);
949
                recv_data_inf.firstPacketArrived = true;
950
                recv_data_inf.recvFragments = 1;
951
                recv_data_inf.priority = false;
952
                recv_data_inf.padding = false;
953
                recv_data_inf.confirmation = false;
954
                recv_data_inf.reliable = false;
955
956
                // send data recv callback to monitoring module
957
                (get_Recv_data_inf_cb) ((void *) &recv_data_inf);
958
        } else
959
                con_msg = (struct conn_msg *) msgbuf;
960
961 380cf1b9 RobertBirke
        // check the connection command type
962 1af63d85 RobertBirke
        switch (con_msg->comand_type) {
963 380cf1b9 RobertBirke
                /*
964
                * if INVITE: enter a new socket make new entry in connect array
965
                * send an ok 
966
                */
967
                case INVITE:
968
                        debug("ML: received INVITE");
969
                        /*
970
                        * check if another connection for the external connectionID exist 
971
                        * that was established within the last 2 seconds 
972
                        */
973
                        free_con_id = -1;
974
                        for (con_id = 0; con_id < CONNECTBUFSIZE; con_id++) {
975
                                if (connectbuf[con_id] != NULL) {
976 1af63d85 RobertBirke
                                        if (compare_socketIDs(&(connectbuf[con_id]->external_socketID), &(con_msg->sock_id)) == 0) {
977 380cf1b9 RobertBirke
                                                timediff = difftime(now, connectbuf[con_id]->starttime);
978
                                                if (timediff < 2)
979
                                                        break;
980
                                        }
981 1af63d85 RobertBirke
                                } else if(free_con_id == -1)
982
                                        free_con_id = con_id;
983 351ad48b RobertBirke
                        }
984 a0a1f630 KristianBeckers
985 380cf1b9 RobertBirke
                        if (con_id == CONNECTBUFSIZE) {
986
                                // create an entry in the connecttrybuf
987
                                if(free_con_id == -1) {
988
                                        error("ML: no new connect_buf available");
989
                                        return;
990
                                }
991
                                connectbuf[free_con_id] = (connect_data *) malloc(sizeof(connect_data));
992 1af63d85 RobertBirke
                                memset(connectbuf[free_con_id],0,sizeof(connect_data));
993 380cf1b9 RobertBirke
                                connectbuf[free_con_id]->connection_head = connectbuf[free_con_id]->connection_last = NULL;
994
                                connectbuf[free_con_id]->starttime = time(NULL);
995 1af63d85 RobertBirke
                                memcpy(&(connectbuf[free_con_id]->external_socketID), &(con_msg->sock_id), sizeof(socket_ID));
996
                                connectbuf[free_con_id]->pmtusize = con_msg->pmtu_size;
997
                                connectbuf[free_con_id]->external_connectionID = msg_h->local_con_id;
998 380cf1b9 RobertBirke
                                connectbuf[free_con_id]->internal_connect =  
999 1af63d85 RobertBirke
                                        !compare_external_address_socketIDs(&(con_msg->sock_id), loc_socketID);
1000 380cf1b9 RobertBirke
                                con_id = free_con_id;
1001
                        }
1002 a0a1f630 KristianBeckers
1003 380cf1b9 RobertBirke
                        if(connectbuf[con_id]->status <= CONNECT) {
1004
                                //update status and send back answer
1005
                                connectbuf[con_id]->status = CONNECT;
1006 1af63d85 RobertBirke
                                send_conn_msg(con_id, con_msg->pmtu_size, CONNECT);
1007 380cf1b9 RobertBirke
                        }
1008
                        break;
1009
                case CONNECT:
1010
                        debug("ML: received CONNECT");
1011 a0a1f630 KristianBeckers
1012 1b0e6d04 RobertBirke
                        if(msg_h->remote_con_id != -1 && connectbuf[msg_h->remote_con_id] == NULL) {
1013 380cf1b9 RobertBirke
                                error("ML: received CONNECT for unexistent connection");
1014
                                return;
1015
                        }
1016 a0a1f630 KristianBeckers
1017 380cf1b9 RobertBirke
                        /*
1018
                        * check if the connection status is not already 1 or 2 
1019
                        */
1020 1af63d85 RobertBirke
                        if (connectbuf[msg_h->remote_con_id]->status == INVITE) {
1021 380cf1b9 RobertBirke
                                // set the external connectionID
1022 1af63d85 RobertBirke
                                connectbuf[msg_h->remote_con_id]->external_connectionID = msg_h->local_con_id;
1023
                                // change status con_msg the connection_data
1024
                                connectbuf[msg_h->remote_con_id]->status = READY;
1025 380cf1b9 RobertBirke
                                // change pmtusize in the connection_data
1026 1af63d85 RobertBirke
                                connectbuf[msg_h->remote_con_id]->pmtusize = con_msg->pmtu_size;
1027 380cf1b9 RobertBirke
1028 6bbd38ef RobertBirke
                                // send the READY
1029
                                send_conn_msg(msg_h->remote_con_id, con_msg->pmtu_size, READY);
1030
1031 380cf1b9 RobertBirke
                                if (receive_Connection_cb != NULL)
1032 1af63d85 RobertBirke
                                        (receive_Connection_cb) (msg_h->remote_con_id, NULL);
1033 380cf1b9 RobertBirke
1034
                                // call all registered callbacks
1035 1af63d85 RobertBirke
                                while(connectbuf[msg_h->remote_con_id]->connection_head != NULL) {
1036 380cf1b9 RobertBirke
                                        struct receive_connection_cb_list *temp;
1037 1af63d85 RobertBirke
                                        temp = connectbuf[msg_h->remote_con_id]->connection_head;
1038
                                        (temp->connection_cb) (msg_h->remote_con_id, temp->arg);
1039
                                        connectbuf[msg_h->remote_con_id]->connection_head = temp->next;
1040 380cf1b9 RobertBirke
                                        free(temp);
1041
                                }
1042 1af63d85 RobertBirke
                                connectbuf[msg_h->remote_con_id]->connection_head =
1043
                                        connectbuf[msg_h->remote_con_id]->connection_last = NULL;
1044 6bbd38ef RobertBirke
                        } else
1045
                                // send the READY
1046
                                send_conn_msg(msg_h->remote_con_id, con_msg->pmtu_size, READY);
1047 a0a1f630 KristianBeckers
1048 380cf1b9 RobertBirke
                        debug("ML: active connection established");
1049
                        break;
1050 a0a1f630 KristianBeckers
1051 380cf1b9 RobertBirke
                        /*
1052
                        * if READY: find the entry in the connection array set the
1053
                        * connection active change the pmtu size 
1054
                        */
1055
                case READY:
1056
                        debug("ML: received READY");
1057 1af63d85 RobertBirke
                        if(connectbuf[msg_h->remote_con_id] == NULL) {
1058 6bbd38ef RobertBirke
                                error("ML: received READY for unexistent connection");
1059 380cf1b9 RobertBirke
                                return;
1060
                        }
1061
                        /*
1062
                        * checks if the connection is not already established 
1063
                        */
1064 1af63d85 RobertBirke
                        if (connectbuf[msg_h->remote_con_id]->status == CONNECT) {
1065 380cf1b9 RobertBirke
                                // change status of the connection
1066 1af63d85 RobertBirke
                                connectbuf[msg_h->remote_con_id]->status = 2;
1067 380cf1b9 RobertBirke
                                // change pmtusize
1068 1af63d85 RobertBirke
                                connectbuf[msg_h->remote_con_id]->pmtusize = con_msg->pmtu_size;
1069 380cf1b9 RobertBirke
1070
                                if (receive_Connection_cb != NULL)
1071 1af63d85 RobertBirke
                                        (receive_Connection_cb) (msg_h->remote_con_id, NULL);
1072 380cf1b9 RobertBirke
                        
1073 1af63d85 RobertBirke
                                while(connectbuf[msg_h->remote_con_id]->connection_head != NULL) {
1074 380cf1b9 RobertBirke
                                        struct receive_connection_cb_list *temp;
1075 1af63d85 RobertBirke
                                        temp = connectbuf[msg_h->remote_con_id]->connection_head;
1076
                                        (temp->connection_cb) (msg_h->remote_con_id, temp->arg);
1077
                                        connectbuf[msg_h->remote_con_id]->connection_head = temp->next;
1078 380cf1b9 RobertBirke
                                        free(temp);
1079
                                }
1080 1af63d85 RobertBirke
                                connectbuf[msg_h->remote_con_id]->connection_head =
1081
                                        connectbuf[msg_h->remote_con_id]->connection_last = NULL;
1082 380cf1b9 RobertBirke
                                debug("ML: passiv connection established");
1083
                        }
1084
                        break;
1085 510f7930 RobertBirke
        }
1086 a0a1f630 KristianBeckers
}
1087
1088 510f7930 RobertBirke
/*
1089
 * what to do once a packet arrived if it is a conn packet send it to
1090
 * recv_conn handler if it is a data packet send it to the recv_data
1091
 * handler 
1092
 */
1093 a0a1f630 KristianBeckers
1094 380cf1b9 RobertBirke
//done --
1095 510f7930 RobertBirke
void recv_pkg(int fd, short event, void *arg)
1096
{
1097 380cf1b9 RobertBirke
        debug("ML: recv_pkg called");
1098 a0a1f630 KristianBeckers
1099 1af63d85 RobertBirke
        struct msg_header *msg_h;
1100 380cf1b9 RobertBirke
        char msgbuf[MAX];
1101 1af63d85 RobertBirke
        char *bufptr = msgbuf;
1102 380cf1b9 RobertBirke
        int ttl;
1103
        struct sockaddr_in recv_addr;
1104
        pmtu recvSize = MAX;
1105 6bbd38ef RobertBirke
        int msg_size;
1106 a0a1f630 KristianBeckers
1107 380cf1b9 RobertBirke
        recvPacket(fd, msgbuf, &recvSize, &recv_addr, pmtu_error_cb_th, &ttl);
1108 a0a1f630 KristianBeckers
1109 2a178954 KristianBeckers
1110 380cf1b9 RobertBirke
        // check if it is not just an ERROR message
1111 351ad48b RobertBirke
        if(recvSize < 0)
1112
                return;
1113
1114 1af63d85 RobertBirke
        // @TODO check if this simplistic STUN message recognition really always works, probably not
1115 510f7930 RobertBirke
        unsigned short stun_bind_response = 0x0101;
1116 380cf1b9 RobertBirke
        unsigned short * msgspot = (unsigned short *) msgbuf;
1117
        if (*msgspot == stun_bind_response) {
1118
                debug("ML: recv_pkg: parse stun message called");
1119
                recv_stun_msg(msgbuf, recvSize);
1120
                return;
1121
        }
1122 1af63d85 RobertBirke
1123
        msg_h = (struct msg_header *) msgbuf;
1124 6bbd38ef RobertBirke
        bufptr += MSG_HEADER_SIZE + msg_h->len_mon_packet_hdr;
1125
        msg_size = recvSize - MSG_HEADER_SIZE - msg_h->len_mon_packet_hdr;
1126 1af63d85 RobertBirke
1127
1128 6bbd38ef RobertBirke
        if(get_Recv_pkt_inf_cb != NULL && msg_h->len_mon_packet_hdr != 0) {
1129 1af63d85 RobertBirke
                mon_pkt_inf msginfNow;
1130
                msginfNow.monitoringHeaderLen = msg_h->len_mon_packet_hdr;
1131 6bbd38ef RobertBirke
                msginfNow.monitoringHeader = msg_h->len_mon_packet_hdr ? &msgbuf[0] + MSG_HEADER_SIZE : NULL;
1132 1af63d85 RobertBirke
                //TODO rethink this ...
1133
                if(msg_h->msg_type == ML_CON_MSG) {
1134
                        struct conn_msg *c_msg = (struct conn_msg *) bufptr;
1135
                        msginfNow.remote_socketID = &(c_msg->sock_id);
1136
                }
1137
                else if(connectbuf[msg_h->remote_con_id] == NULL) {
1138
                        error("ML: received pkg called with non existent connection");
1139
                        return;
1140
                } else
1141
                        msginfNow.remote_socketID = &(connectbuf[msg_h->remote_con_id]->external_socketID);
1142
                msginfNow.buffer = bufptr;
1143
                msginfNow.bufSize = recvSize;
1144
                msginfNow.msgtype = msg_h->msg_type;
1145
                msginfNow.ttl = ttl;
1146
                msginfNow.dataID = msg_h->msg_seq_num;
1147
                msginfNow.offset = msg_h->offset;
1148
                msginfNow.datasize = msg_h->msg_length;
1149
                gettimeofday(&msginfNow.arrival_time, NULL);
1150
                (get_Recv_pkt_inf_cb) ((void *) &msginfNow);
1151
        }
1152
1153
1154
        switch(msg_h->msg_type) {
1155 380cf1b9 RobertBirke
                case ML_CON_MSG:
1156
                        debug("ML: received conn pkg");
1157 6bbd38ef RobertBirke
                        recv_conn_msg(msg_h, bufptr, msg_size);
1158 380cf1b9 RobertBirke
                        break;
1159
                default:
1160 6bbd38ef RobertBirke
                        if(msg_h->msg_type < 127) {
1161 380cf1b9 RobertBirke
                                debug("ML: received data pkg");
1162 6bbd38ef RobertBirke
                                recv_data_msg(msg_h, bufptr, msg_size);
1163 380cf1b9 RobertBirke
                                break;
1164
                        }
1165
                        debug("ML: unrecognised msg_type");
1166
                        break;
1167 510f7930 RobertBirke
        }
1168
}
1169 a0a1f630 KristianBeckers
1170 510f7930 RobertBirke
void recv_stun_msg(char *msgbuf, int recvSize)
1171
{
1172
        /*
1173 351ad48b RobertBirke
        * create empty stun message struct 
1174
        */
1175
        StunMessage resp;
1176
        memset(&resp, 0, sizeof(StunMessage));
1177 510f7930 RobertBirke
        /*
1178 351ad48b RobertBirke
        * parse the message 
1179
        */
1180
        int returnValue = 0;
1181
        returnValue = recv_stun_message(msgbuf, recvSize, &resp);
1182
1183
        if (returnValue == 0) {
1184
                /*
1185
                * read the reflexive Address into the local_socketID 
1186
                */
1187
                struct sockaddr_in reflexiveAddr;
1188
                reflexiveAddr.sin_family = AF_INET;
1189
                reflexiveAddr.sin_addr.s_addr = htonl(resp.mappedAddress.ipv4.addr);
1190
                reflexiveAddr.sin_port = htons(resp.mappedAddress.ipv4.port);
1191
                socketaddrgen reflexiveAddres;
1192
                reflexiveAddres.udpaddr = reflexiveAddr;
1193
                local_socketID.external_addr = reflexiveAddres;
1194
                NAT_traversal = true;
1195
                // callback to the upper layer indicating that the socketID is now
1196
                // ready to use
1197
                (receive_SocketID_cb) (&local_socketID, 0);
1198
        }
1199 510f7930 RobertBirke
}
1200 a0a1f630 KristianBeckers
1201 6bbd38ef RobertBirke
// process a single recv data message
1202 1af63d85 RobertBirke
void recv_data_msg(struct msg_header *msg_h, char *msgbuf, int bufsize)
1203 510f7930 RobertBirke
{
1204 1af63d85 RobertBirke
        debug("transmissionHandler: received data message called");
1205 510f7930 RobertBirke
1206 1af63d85 RobertBirke
        int recv_id, free_recv_id = -1;
1207 a0a1f630 KristianBeckers
1208 1af63d85 RobertBirke
        if(connectbuf[msg_h->remote_con_id] == NULL) {
1209 85bc61b5 TivadarSzemethy
                debug("ML: Received a message not related to any opened connection!");
1210 380cf1b9 RobertBirke
                return;
1211
        }
1212
1213 1af63d85 RobertBirke
        // check if a recv_data exist and enter data
1214
        for (recv_id = 0; recv_id < RECVDATABUFSIZE; recv_id++)
1215 6bbd38ef RobertBirke
                if (recvdatabuf[recv_id] != NULL) {
1216 1af63d85 RobertBirke
                        if (msg_h->remote_con_id == recvdatabuf[recv_id]->connectionID && 
1217
                                        msg_h->msg_seq_num == recvdatabuf[recv_id]->seqnr)
1218
                                                break;
1219 6bbd38ef RobertBirke
                } else
1220 1af63d85 RobertBirke
                        if(free_recv_id == -1)
1221 6bbd38ef RobertBirke
                                free_recv_id = recv_id;
1222 1af63d85 RobertBirke
1223
1224
        if(recv_id == RECVDATABUFSIZE) { 
1225
                //no recv_data found: create one
1226
                recv_id = free_recv_id;
1227
                recvdatabuf[recv_id] = (recvdata *) malloc(sizeof(recvdata));
1228 6bbd38ef RobertBirke
                memset(recvdatabuf[recv_id], 0, sizeof(recvdata));
1229 1af63d85 RobertBirke
                recvdatabuf[recv_id]->connectionID = msg_h->remote_con_id;
1230 6bbd38ef RobertBirke
                recvdatabuf[recv_id]->seqnr = msg_h->msg_seq_num;
1231
                recvdatabuf[recv_id]->monitoringDataHeaderLen = msg_h->len_mon_data_hdr;
1232
                recvdatabuf[recv_id]->bufsize = msg_h->msg_length + msg_h->len_mon_data_hdr;
1233
                recvdatabuf[recv_id]->recvbuf = (char *) malloc(recvdatabuf[recv_id]->bufsize);
1234 1af63d85 RobertBirke
                /*
1235
                * read the timeout data and set it 
1236
                */
1237
                recvdatabuf[recv_id]->timeout_value.tv_sec = recv_timeout.tv_sec;
1238
                recvdatabuf[recv_id]->timeout_value.tv_usec = recv_timeout.tv_usec;
1239
                recvdatabuf[recv_id]->recvID = recv_id;
1240
                recvdatabuf[recv_id]->starttime = time(NULL);
1241
                recvdatabuf[recv_id]->msgtype = msg_h->msg_type;
1242
1243
                // fill the buffer with zeros
1244
                memset(recvdatabuf[recv_id]->recvbuf, 0, msg_h->msg_length);
1245 8601bccf RobertBirke
        }
1246
1247 1af63d85 RobertBirke
        if (msg_h->offset == 0)
1248
                recvdatabuf[recv_id]->firstPacketArrived = 1;
1249 510f7930 RobertBirke
1250 6bbd38ef RobertBirke
1251 1af63d85 RobertBirke
        // increment fragmentnr
1252
        recvdatabuf[recv_id]->recvFragments++;
1253
        // increment the arrivedBytes
1254
        recvdatabuf[recv_id]->arrivedBytes += bufsize;
1255 510f7930 RobertBirke
1256 1af63d85 RobertBirke
        // enter the data into the buffer
1257
        memcpy(recvdatabuf[recv_id]->recvbuf + msg_h->offset, msgbuf, bufsize);
1258
        
1259
        //TODO very basic checkif all fragments arrived: has to be reviewed
1260
        if(recvdatabuf[recv_id]->arrivedBytes == recvdatabuf[recv_id]->bufsize)
1261
                recvdatabuf[recv_id]->status = COMPLETE; //buffer full -> msg completly arrived
1262
        else
1263
                recvdatabuf[recv_id]->status = ACTIVE;
1264 510f7930 RobertBirke
1265 1af63d85 RobertBirke
        if (recv_data_callback) {
1266
                if(recvdatabuf[recv_id]->status == COMPLETE) {
1267
                        // Monitoring layer hook
1268
                        if(get_Recv_data_inf_cb != NULL) {
1269
                                mon_data_inf recv_data_inf;
1270
        
1271
                                recv_data_inf.remote_socketID =
1272
                                         &(connectbuf[recvdatabuf[recv_id]->connectionID]->external_socketID);
1273
                                recv_data_inf.buffer = recvdatabuf[recv_id]->recvbuf;
1274
                                recv_data_inf.bufSize = recvdatabuf[recv_id]->bufsize;
1275
                                recv_data_inf.msgtype = recvdatabuf[recv_id]->msgtype;
1276
                                recv_data_inf.monitoringDataHeaderLen = recvdatabuf[recv_id]->monitoringDataHeaderLen;
1277
                                recv_data_inf.monitoringDataHeader = recvdatabuf[recv_id]->monitoringDataHeaderLen ?
1278
                                        recvdatabuf[recv_id]->recvbuf : NULL;
1279
                                gettimeofday(&recv_data_inf.arrival_time, NULL);
1280
                                recv_data_inf.firstPacketArrived = recvdatabuf[recv_id]->firstPacketArrived;
1281
                                recv_data_inf.recvFragments = recvdatabuf[recv_id]->recvFragments;
1282
                                recv_data_inf.priority = false;
1283
                                recv_data_inf.padding = false;
1284
                                recv_data_inf.confirmation = false;
1285
                                recv_data_inf.reliable = false;
1286 510f7930 RobertBirke
1287 1af63d85 RobertBirke
                                // send data recv callback to monitoring module
1288 510f7930 RobertBirke
1289 1af63d85 RobertBirke
                                (get_Recv_data_inf_cb) ((void *) &recv_data_inf);
1290 510f7930 RobertBirke
                        }
1291
1292 1af63d85 RobertBirke
                        // Get the right callback
1293
                        receive_data_cb receive_data_callback = recvcbbuf[msg_h->msg_type];
1294 de18e918 CsabaKiraly
                        if (receive_data_callback) {
1295 1af63d85 RobertBirke
        
1296 de18e918 CsabaKiraly
                                recv_params rParams;
1297 1af63d85 RobertBirke
        
1298 de18e918 CsabaKiraly
                                rParams.nrMissingBytes = recvdatabuf[recv_id]->bufsize - recvdatabuf[recv_id]->arrivedBytes;
1299
                                rParams.recvFragments = recvdatabuf[recv_id]->recvFragments;
1300
                                rParams.msgtype = recvdatabuf[recv_id]->msgtype;
1301
                                rParams.connectionID = recvdatabuf[recv_id]->connectionID;
1302
                                rParams.remote_socketID = 
1303
                                        &(connectbuf[recvdatabuf[recv_id]->connectionID]->external_socketID);
1304
                                rParams.firstPacketArrived = recvdatabuf[recv_id]->firstPacketArrived;
1305 1af63d85 RobertBirke
        
1306 de18e918 CsabaKiraly
                                (receive_data_callback) (recvdatabuf[recv_id]->recvbuf + recvdatabuf[recv_id]->monitoringDataHeaderLen, recvdatabuf[recv_id]->bufsize - recvdatabuf[recv_id]->monitoringDataHeaderLen,
1307
                                        recvdatabuf[recv_id]->msgtype, (void *) &rParams);
1308
                        } else {
1309
                            fprintf(stderr,"ML: Warning: callback not initialized for this message type: %d!\n",msg_h->msg_type);
1310
                        }
1311
                        
1312 1af63d85 RobertBirke
                        //clean up
1313
                        free(recvdatabuf[recv_id]->recvbuf);
1314
                        free(recvdatabuf[recv_id]);
1315
                        recvdatabuf[recv_id] = NULL;
1316
                } else { // not COMPLETE
1317
                        //start time out
1318
                        //TODO make timeout at least a DEFINE
1319
                        struct timeval timeout = { 4, 0 };
1320 d42c2650 TivadarSzemethy
                        event_base_once(base, -1, EV_TIMEOUT, &recv_timeout_cb, (void *) (long)recv_id, &timeout);
1321 510f7930 RobertBirke
                }
1322
        }
1323
}
1324 a0a1f630 KristianBeckers
1325 1af63d85 RobertBirke
//done
1326 510f7930 RobertBirke
void recv_timeout_cb(int fd, short event, void *arg)
1327
{
1328 d42c2650 TivadarSzemethy
        int recv_id = (long) arg;
1329 1af63d85 RobertBirke
        debug("ML: recv_timeout_cb called");
1330 a0a1f630 KristianBeckers
1331 1af63d85 RobertBirke
        if (recvdatabuf[recv_id] == NULL) {
1332
                return;
1333
        }
1334 a0a1f630 KristianBeckers
1335
1336 6bbd38ef RobertBirke
        if(recvdatabuf[recv_id]->status == ACTIVE) {
1337 1af63d85 RobertBirke
                //TODO make timeout at least a DEFINE
1338
                struct timeval timeout = { 4, 0 };
1339
                recvdatabuf[recv_id]->status = INACTIVE;
1340
                event_base_once(base, -1, EV_TIMEOUT, recv_timeout_cb,
1341
                        arg, &timeout);
1342
                return;
1343
        }
1344 a0a1f630 KristianBeckers
1345 1af63d85 RobertBirke
        if(recvdatabuf[recv_id]->status == INACTIVE) {
1346
                // Monitoring layer hook
1347
                if(get_Recv_data_inf_cb != NULL) {
1348
                        mon_data_inf recv_data_inf;
1349
1350
                        recv_data_inf.remote_socketID =
1351
                                        &(connectbuf[recvdatabuf[recv_id]->connectionID]->external_socketID);
1352
                        recv_data_inf.buffer = recvdatabuf[recv_id]->recvbuf;
1353
                        recv_data_inf.bufSize = recvdatabuf[recv_id]->bufsize;
1354
                        recv_data_inf.msgtype = recvdatabuf[recv_id]->msgtype;
1355
                        recv_data_inf.monitoringDataHeaderLen = recvdatabuf[recv_id]->monitoringDataHeaderLen;
1356
                        recv_data_inf.monitoringDataHeader = recvdatabuf[recv_id]->monitoringDataHeaderLen ?
1357
                                recvdatabuf[recv_id]->recvbuf : NULL;
1358
                        gettimeofday(&recv_data_inf.arrival_time, NULL);
1359
                        recv_data_inf.firstPacketArrived = recvdatabuf[recv_id]->firstPacketArrived;
1360
                        recv_data_inf.recvFragments = recvdatabuf[recv_id]->recvFragments;
1361
                        recv_data_inf.priority = false;
1362
                        recv_data_inf.padding = false;
1363
                        recv_data_inf.confirmation = false;
1364
                        recv_data_inf.reliable = false;
1365
1366
                        // send data recv callback to monitoring module
1367
1368
                        (get_Recv_data_inf_cb) ((void *) &recv_data_inf);
1369 510f7930 RobertBirke
                }
1370 a0a1f630 KristianBeckers
1371 1af63d85 RobertBirke
                // Get the right callback
1372
                receive_data_cb receive_data_callback = recvcbbuf[recvdatabuf[recv_id]->msgtype];
1373 a0a1f630 KristianBeckers
1374 1af63d85 RobertBirke
                recv_params rParams;
1375 a0a1f630 KristianBeckers
1376 1af63d85 RobertBirke
                rParams.nrMissingBytes = recvdatabuf[recv_id]->bufsize - recvdatabuf[recv_id]->arrivedBytes;
1377
                rParams.recvFragments = recvdatabuf[recv_id]->recvFragments;
1378
                rParams.msgtype = recvdatabuf[recv_id]->msgtype;
1379
                rParams.connectionID = recvdatabuf[recv_id]->connectionID;
1380
                rParams.remote_socketID = 
1381
                        &(connectbuf[recvdatabuf[recv_id]->connectionID]->external_socketID);
1382
                rParams.firstPacketArrived = recvdatabuf[recv_id]->firstPacketArrived;
1383
1384 6bbd38ef RobertBirke
                (receive_data_callback) (recvdatabuf[recv_id]->recvbuf + recvdatabuf[recv_id]->monitoringDataHeaderLen, recvdatabuf[recv_id]->bufsize - recvdatabuf[recv_id]->monitoringDataHeaderLen,
1385 0e1f2ed3 CsabaKiraly
                        recvdatabuf[recv_id]->msgtype, &rParams);
1386 1af63d85 RobertBirke
1387
                //clean up
1388
                free(recvdatabuf[recv_id]->recvbuf);
1389
                free(recvdatabuf[recv_id]);
1390
                recvdatabuf[recv_id] = NULL;
1391 510f7930 RobertBirke
        }
1392
}
1393 a0a1f630 KristianBeckers
1394 380cf1b9 RobertBirke
//done
1395 510f7930 RobertBirke
void pmtu_timeout_cb(int fd, short event, void *arg)
1396
{
1397 380cf1b9 RobertBirke
        debug("ML: pmtu timeout called");
1398 1af63d85 RobertBirke
1399 d42c2650 TivadarSzemethy
        int con_id = (long) arg;
1400 351ad48b RobertBirke
        pmtu new_pmtusize;
1401 1af63d85 RobertBirke
        struct timeval timeout;
1402 351ad48b RobertBirke
        
1403 380cf1b9 RobertBirke
        if(connectbuf[con_id] == NULL) {
1404
                error("ML: pmtu timeout called on non existing con_id");
1405 1af63d85 RobertBirke
                return;
1406 380cf1b9 RobertBirke
        }
1407
1408 1af63d85 RobertBirke
        if(connectbuf[con_id]->status == READY) {
1409
                // nothing to do anymore
1410
                return;
1411
        }
1412
1413
        timeout = connectbuf[con_id]->timeout_value;
1414
1415
        if(connectbuf[con_id]->delay || connectbuf[con_id]->trials == MAX_TRIALS - 1) {
1416
                double delay = timeout.tv_sec + timeout.tv_usec / 1000000.0;
1417
                delay = delay * 5;
1418
                timeout.tv_sec = floor(delay);
1419
                timeout.tv_usec = fmod(delay, 1.0) * 1000000.0;
1420
                if(connectbuf[con_id]->delay) {
1421
                        connectbuf[con_id]->delay = false;
1422
                        goto reschedule;
1423
                }
1424
        }
1425
1426
        if(connectbuf[con_id]->trials == MAX_TRIALS) {
1427
                // decrement the pmtu size
1428
                connectbuf[con_id]->pmtusize = pmtu_decrement(connectbuf[con_id]->pmtusize);
1429
                connectbuf[con_id]->trials = 0;
1430
        }
1431 380cf1b9 RobertBirke
1432
        //error in PMTU discovery?
1433 1af63d85 RobertBirke
        if (connectbuf[con_id]->pmtusize == ERROR) {
1434 380cf1b9 RobertBirke
                if (connectbuf[con_id]->internal_connect == true) {
1435
                        //as of now we tried directly connecting, now let's try trough the NAT
1436
                        connectbuf[con_id]->internal_connect = false;
1437
                        connectbuf[con_id]->pmtusize = MAX;
1438 351ad48b RobertBirke
                } else {
1439 380cf1b9 RobertBirke
                        //nothing to do we have to give up
1440
                        error("ML: Could not create connection with connectionID %i!",con_id);
1441
                        // envoke the callback for failed connection establishment
1442 351ad48b RobertBirke
                        if(failed_Connection_cb != NULL)
1443 380cf1b9 RobertBirke
                                (failed_Connection_cb) (con_id, NULL);
1444 351ad48b RobertBirke
                        // delete the connection entry
1445 380cf1b9 RobertBirke
                        close_connection(con_id);
1446
                        return;
1447 351ad48b RobertBirke
                }
1448 510f7930 RobertBirke
        }
1449 a0a1f630 KristianBeckers
1450 380cf1b9 RobertBirke
        //retry with new pmtu size
1451 1af63d85 RobertBirke
        connectbuf[con_id]->trials++;
1452
        send_conn_msg(con_id, connectbuf[con_id]->pmtusize, connectbuf[con_id]->status);
1453 a0a1f630 KristianBeckers
1454 1af63d85 RobertBirke
reschedule:
1455 380cf1b9 RobertBirke
        /* reschedule */
1456 d42c2650 TivadarSzemethy
        event_base_once(base, -1, EV_TIMEOUT, pmtu_timeout_cb, (void *) (long)con_id, &timeout);
1457 510f7930 RobertBirke
}
1458 a0a1f630 KristianBeckers
1459 510f7930 RobertBirke
/*
1460
 * decrements the mtu size
1461
 */
1462
pmtu pmtu_decrement(pmtu pmtusize)
1463
{
1464 351ad48b RobertBirke
        pmtu pmtu_return_size;
1465
        switch(pmtusize) {
1466 380cf1b9 RobertBirke
        case MAX:
1467
                return DSL;
1468
        case DSL:
1469
                return DSLMEDIUM;
1470
        case DSLMEDIUM:
1471
                return DSLSLIM;
1472
        case DSLSLIM:
1473
                return BELOWDSL;
1474
        case BELOWDSL:
1475
                return MIN;
1476 351ad48b RobertBirke
        default:
1477 380cf1b9 RobertBirke
                return ERROR;
1478 351ad48b RobertBirke
        }
1479 510f7930 RobertBirke
}
1480 a0a1f630 KristianBeckers
1481 510f7930 RobertBirke
void pmtu_error_cb_th(char *msg, int msglen)
1482
{
1483 380cf1b9 RobertBirke
        debug("ML: pmtu_error callback called msg_size: %d",msglen);
1484 1af63d85 RobertBirke
        //TODO debug
1485
        return;
1486 a0a1f630 KristianBeckers
1487 510f7930 RobertBirke
    char *msgbufptr = NULL;
1488
    int msgtype;
1489
    int connectionID;
1490
    pmtu pmtusize;
1491
    pmtu new_pmtusize;
1492
    int dead = 0;
1493 a0a1f630 KristianBeckers
1494 510f7930 RobertBirke
    // check the packettype
1495
    msgbufptr = &msg[0];
1496 a0a1f630 KristianBeckers
1497 510f7930 RobertBirke
    // check the msgtype
1498
    msgbufptr = &msg[1];
1499
    memcpy(&msgtype, msgbufptr, 4);
1500 a0a1f630 KristianBeckers
1501 510f7930 RobertBirke
    if (msgtype == 0) {
1502 a0a1f630 KristianBeckers
1503 510f7930 RobertBirke
        // get the connectionID
1504
        msgbufptr = &msg[5];
1505
        memcpy(&connectionID, msgbufptr, 4);
1506 a0a1f630 KristianBeckers
1507 510f7930 RobertBirke
        int msgtype_c = connectbuf[connectionID]->status;
1508 1af63d85 RobertBirke
//        pmtusize = connectbuf[connectionID]->pmtutrysize;
1509 a0a1f630 KristianBeckers
1510 510f7930 RobertBirke
        if (msgtype_c != msgtype) {
1511
            dead = 1;
1512
        }
1513 a0a1f630 KristianBeckers
1514
1515 510f7930 RobertBirke
    } else if (msgtype == 1) {
1516 a0a1f630 KristianBeckers
1517 510f7930 RobertBirke
        // read the connectionID
1518
        msgbufptr = &msg[9];
1519
        memcpy(&connectionID, msgbufptr, 4);
1520 a0a1f630 KristianBeckers
1521 510f7930 RobertBirke
        int msgtype_c = connectbuf[connectionID]->status;
1522 1af63d85 RobertBirke
//        pmtusize = connectbuf[connectionID]->pmtutrysize;
1523 a0a1f630 KristianBeckers
1524 510f7930 RobertBirke
        if (msgtype_c != msgtype) {
1525
            dead = 1;
1526
        }
1527 a0a1f630 KristianBeckers
1528
    }
1529 510f7930 RobertBirke
    // decrement the pmtu size 
1530
    new_pmtusize = pmtu_decrement(pmtusize);
1531 a0a1f630 KristianBeckers
1532 1af63d85 RobertBirke
//    connectbuf[connectionID]->pmtutrysize = new_pmtusize;
1533 a0a1f630 KristianBeckers
1534 380cf1b9 RobertBirke
    if (new_pmtusize == ERROR) {
1535 351ad48b RobertBirke
                fprintf(stderr,
1536
                        "transmissionHandler:  Could not create connection with connectionID %i !\n",
1537
                        connectionID);
1538
                
1539
                if(failed_Connection_cb != NULL)
1540
                        (failed_Connection_cb) (connectionID, NULL);
1541
                // set the message type to a non existent message 
1542
                msgtype = 2;
1543
                // delete the connection entry
1544 380cf1b9 RobertBirke
                 close_connection(connectionID);
1545 351ad48b RobertBirke
        }
1546 2a178954 KristianBeckers
1547 510f7930 RobertBirke
    if (msgtype == 0 && dead != 1) {
1548 a0a1f630 KristianBeckers
1549 510f7930 RobertBirke
        // stop the timeout event
1550
        // timeout_del(connectbuf[connectionID]->timeout);
1551
        /*
1552
         * libevent2 
1553
         */
1554 8b17961c KristianBeckers
1555 510f7930 RobertBirke
        // event_del(connectbuf[connectionID]->timeout);
1556 a0a1f630 KristianBeckers
1557
1558 510f7930 RobertBirke
        // create and send a connection message
1559 1af63d85 RobertBirke
//         create_conn_msg(new_pmtusize, connectionID, 
1560
//                         &local_socketID, INVITE);
1561 a0a1f630 KristianBeckers
1562 1af63d85 RobertBirke
//        send_conn_msg(connectionID, new_pmtusize);
1563 a0a1f630 KristianBeckers
1564 510f7930 RobertBirke
        // set a timeout event for the pmtu discovery 
1565
        // timeout_set(connectbuf[connectionID]->timeout,pmtu_timeout_cb,(void 
1566
        // *)&connectionID);
1567 a0a1f630 KristianBeckers
1568 510f7930 RobertBirke
        // timeout_add(connectbuf[connectionID]->timeout,&connectbuf[connectionID]->timeout_value);
1569 a0a1f630 KristianBeckers
1570 510f7930 RobertBirke
        /*
1571
         * libevent2 
1572
         */
1573 a0a1f630 KristianBeckers
1574 510f7930 RobertBirke
        struct event *ev;
1575
        ev = evtimer_new(base, pmtu_timeout_cb,
1576
                         (void *) connectbuf[connectionID]);
1577 a0a1f630 KristianBeckers
1578 510f7930 RobertBirke
        // connectbuf[connectionID]->timeout = ev;
1579 a0a1f630 KristianBeckers
1580 510f7930 RobertBirke
        event_add(ev, &connectbuf[connectionID]->timeout_value);
1581 a0a1f630 KristianBeckers
1582 510f7930 RobertBirke
    } else if (msgtype == 1 && dead != 1) {
1583 a0a1f630 KristianBeckers
1584 510f7930 RobertBirke
        // stop the timeout event
1585
        // timeout_del(connectbuf[connectionID]->timeout);
1586 a0a1f630 KristianBeckers
1587 510f7930 RobertBirke
        /*
1588
         * libevent2 
1589
         */
1590
        // printf("still here 11 \n");
1591
        // printf("ev %d \n",connectbuf[connectionID]->timeout);
1592
        // event_del(connectbuf[connectionID]->timeout );
1593
        // evtimer_del(connectbuf[connectionID]->timeout );
1594 a0a1f630 KristianBeckers
1595
1596 1af63d85 RobertBirke
//         // create and send a connection message
1597
//         create_conn_msg(new_pmtusize,
1598
//                         connectbuf[connectionID]->connectionID,
1599
//                         NULL, CONNECT);
1600 a0a1f630 KristianBeckers
1601 1af63d85 RobertBirke
        //send_conn_msg(connectionID, new_pmtusize);
1602 a0a1f630 KristianBeckers
1603 510f7930 RobertBirke
        // set a timeout event for the pmtu discovery 
1604
        // timeout_set(connectbuf[connectionID]->timeout,pmtu_timeout_cb,(void 
1605
        // *)&connectionID);
1606
        // timeout_add(connectbuf[connectionID]->timeout,&connectbuf[connectionID]->timeout_value);
1607 a0a1f630 KristianBeckers
1608 510f7930 RobertBirke
        /*
1609
         * libevent2 
1610
         */
1611
        // struct event *ev;
1612
        // ev = evtimer_new(base,pmtu_timeout_cb, (void
1613
        // *)connectbuf[connectionID]);
1614
        // connectbuf[connectionID]->timeout = ev;
1615
        // event_add(ev,&connectbuf[connectionID]->timeout_value);
1616 a0a1f630 KristianBeckers
1617
    }
1618
}
1619
1620 510f7930 RobertBirke
/*
1621
 * compare the external IP address of two socketIDs 
1622
 */
1623
int
1624 351ad48b RobertBirke
compare_external_address_socketIDs(socketID_handle sock1, socketID_handle sock2)
1625 510f7930 RobertBirke
{
1626 351ad48b RobertBirke
        if( sock1->external_addr.udpaddr.sin_addr.s_addr == sock2->external_addr.udpaddr.sin_addr.s_addr)
1627
                return 0;
1628
        return 1;
1629 2a178954 KristianBeckers
}
1630
1631 2c702104 RobertBirke
1632
/*
1633
 * hash code of a socketID
1634
 * TODO might think of a better way
1635
 */
1636
int hash_socketID(socketID_handle sock)
1637
{
1638
    return sock->internal_addr.udpaddr.sin_port +
1639
                          sock->external_addr.udpaddr.sin_port;
1640
}
1641
1642 510f7930 RobertBirke
/*
1643
 * compares 2 socketIDs: returns 0 if they are equl otherwise 1 
1644
 */
1645
int compare_socketIDs(socketID_handle sock1, socketID_handle sock2)
1646
{
1647 2c702104 RobertBirke
        /*
1648
        * compare internal addr 
1649
        */
1650
        if (sock1->internal_addr.udpaddr.sin_addr.s_addr != 
1651
            sock2->internal_addr.udpaddr.sin_addr.s_addr)
1652
                        return 1;
1653 a0a1f630 KristianBeckers
1654 2c702104 RobertBirke
        if (sock1->internal_addr.udpaddr.sin_port !=
1655
                 sock2->internal_addr.udpaddr.sin_port)
1656
                        return 1;
1657 a0a1f630 KristianBeckers
1658 2c702104 RobertBirke
        /*
1659
        * compare external addr 
1660
        */
1661
        if (sock1->external_addr.udpaddr.sin_addr.s_addr != 
1662
            sock2->external_addr.udpaddr.sin_addr.s_addr)
1663
                        return 1;
1664 a0a1f630 KristianBeckers
1665 2c702104 RobertBirke
        if (sock1->external_addr.udpaddr.sin_port !=
1666
                 sock2->external_addr.udpaddr.sin_port)
1667
                        return 1;
1668 a0a1f630 KristianBeckers
1669 2c702104 RobertBirke
        return 0;
1670 a0a1f630 KristianBeckers
}
1671
1672 510f7930 RobertBirke
/*
1673
 * the timeout of the NAT traversal 
1674
 */
1675
void nat_traversal_timeout(int fd, short event, void *arg)
1676
{
1677 6bbd38ef RobertBirke
        if (NAT_traversal == false) {
1678
                debug("ML: NAT traversal request resend");
1679
                send_stun_request(socketfd, &stun_server);
1680 a0a1f630 KristianBeckers
1681 6bbd38ef RobertBirke
                /*
1682
                * enter a NAT traversal timeout that takes care of retransmission 
1683
                */
1684
                struct event *ev1;
1685
                struct timeval timeout_value_NAT_traversal = { 1, 0 };
1686
                ev1 = evtimer_new(base, nat_traversal_timeout, NULL);
1687
                event_add(ev1, &timeout_value_NAT_traversal);
1688
                if(receive_SocketID_cb)
1689
                        (receive_SocketID_cb) (&local_socketID, 2);
1690
        }
1691 a0a1f630 KristianBeckers
}
1692
1693 8601bccf RobertBirke
int get_standard_ttl(socketID_handle socketID, uint8_t *ttl)
1694 510f7930 RobertBirke
{
1695 6bbd38ef RobertBirke
        return getTTL(socketfd, ttl);
1696 a0a1f630 KristianBeckers
}
1697 2a178954 KristianBeckers
1698 380cf1b9 RobertBirke
socketID_handle get_local_socketID(int *error_status)
1699 510f7930 RobertBirke
{
1700 380cf1b9 RobertBirke
        if (NAT_traversal == false) {
1701
                *error_status = 2;
1702
                return NULL;
1703
        }
1704 2a178954 KristianBeckers
1705 380cf1b9 RobertBirke
        *error_status = 0;
1706
        return &local_socketID;
1707 2a178954 KristianBeckers
}
1708
1709 510f7930 RobertBirke
int get_external_IP(char *external_addr)
1710
{
1711 6bbd38ef RobertBirke
        socketaddrgen udpgen;
1712
        struct sockaddr_in udpaddr;
1713 2a178954 KristianBeckers
1714 6bbd38ef RobertBirke
        udpgen = local_socketID.external_addr;
1715
        udpaddr = udpgen.udpaddr;
1716 2a178954 KristianBeckers
1717 6bbd38ef RobertBirke
        inet_ntop(AF_INET, &(udpaddr.sin_addr), external_addr,
1718
                        INET_ADDRSTRLEN);
1719
        
1720
        if (external_addr == NULL) {
1721
        
1722 510f7930 RobertBirke
        return -1;
1723 6bbd38ef RobertBirke
        
1724
        } else {
1725
        
1726 510f7930 RobertBirke
        return 0;
1727 6bbd38ef RobertBirke
        
1728
        }
1729
        
1730 2a178954 KristianBeckers
}
1731
1732 351ad48b RobertBirke
int print_socketID(socketID_handle sock)
1733 510f7930 RobertBirke
{
1734 351ad48b RobertBirke
        char str[SOCKETID_STRING_SIZE];
1735 7455d709 TivadarSzemethy
        socketID_to_String(sock, str, sizeof(str));
1736 351ad48b RobertBirke
        printf("int->%s<-ext\n",str);
1737 2c702104 RobertBirke
}
1738 2a178954 KristianBeckers
1739 7455d709 TivadarSzemethy
int socketID_to_String(socketID_handle sock, char *sock_string, size_t len)
1740 2c702104 RobertBirke
{
1741
        char internal_addr[INET_ADDRSTRLEN];
1742
        char external_addr[INET_ADDRSTRLEN];
1743 7455d709 TivadarSzemethy
        inet_ntop(AF_INET, &(sock->internal_addr.udpaddr.sin_addr.s_addr), internal_addr, INET_ADDRSTRLEN);
1744 2c702104 RobertBirke
        inet_ntop(AF_INET, &(sock->external_addr.udpaddr.sin_addr.s_addr), external_addr, INET_ADDRSTRLEN);
1745
        
1746 7455d709 TivadarSzemethy
        snprintf(sock_string,len,"%s:%d-%s:%d", internal_addr, ntohs(sock->internal_addr.udpaddr.sin_port),
1747 351ad48b RobertBirke
                external_addr,        ntohs(sock->external_addr.udpaddr.sin_port));
1748 510f7930 RobertBirke
        return 0;
1749 2a178954 KristianBeckers
}
1750
1751 5842fcf1 TivadarSzemethy
int string_to_socketID(const char* socketID_string, socketID_handle sock)
1752 510f7930 RobertBirke
{
1753 2c702104 RobertBirke
        //@TODO add checks against malformed string
1754
        char external_addr[INET_ADDRSTRLEN];
1755
        int external_port;
1756
        char internal_addr[INET_ADDRSTRLEN];
1757
        int internal_port;
1758
1759
        char *pch;
1760
1761
        //replace ':' with a blank
1762
        pch=strchr(socketID_string,':');
1763
        while (pch!=NULL){
1764
                                *pch = ' ';
1765
                pch=strchr(pch+1,':');
1766
        }
1767 351ad48b RobertBirke
        pch=strchr(socketID_string,'-');
1768 1af63d85 RobertBirke
        if(pch) *pch = ' ';
1769 2a178954 KristianBeckers
1770 2c702104 RobertBirke
        sscanf(socketID_string,"%s %d %s %d", internal_addr, &internal_port,
1771
                external_addr, &external_port);
1772 2a178954 KristianBeckers
1773 351ad48b RobertBirke
        if(inet_pton(AF_INET, internal_addr, &(sock->internal_addr.udpaddr.sin_addr)) == 0)
1774
                return EINVAL;
1775 a3fd121c RobertBirke
        sock->internal_addr.udpaddr.sin_family = AF_INET;
1776 351ad48b RobertBirke
        sock->internal_addr.udpaddr.sin_port = htons(internal_port);
1777 a3fd121c RobertBirke
        
1778 351ad48b RobertBirke
1779
        if(inet_pton(AF_INET, external_addr, &(sock->external_addr.udpaddr.sin_addr)) ==0)
1780
                return EINVAL;
1781 a3fd121c RobertBirke
        sock->external_addr.udpaddr.sin_family = AF_INET;
1782 351ad48b RobertBirke
        sock->external_addr.udpaddr.sin_port = htons(external_port);
1783 2a178954 KristianBeckers
1784 2c702104 RobertBirke
        return 0;
1785 2a178954 KristianBeckers
}
1786
1787 6bbd38ef RobertBirke
1788 380cf1b9 RobertBirke
int get_connection_status(int con_id)
1789 510f7930 RobertBirke
{
1790 380cf1b9 RobertBirke
        if(connectbuf[con_id])
1791 6bbd38ef RobertBirke
                return connectbuf[con_id]->status == READY;
1792 380cf1b9 RobertBirke
        return -1;
1793 2a178954 KristianBeckers
}
1794
1795
1796 6214e30a TivadarSzemethy
int connection_exist(socketID_handle socketID, bool ready) {
1797 510f7930 RobertBirke
    /*
1798
     * check if another connection for the external connectionID exist
1799 351ad48b RobertBirke
     * that was established \ within the last 2 seconds
1800 510f7930 RobertBirke
     */
1801 351ad48b RobertBirke
        int i;
1802
        for (i = 0; i < CONNECTBUFSIZE; i++)
1803
                if (connectbuf[i] != NULL)
1804 6214e30a TivadarSzemethy
                        if (compare_socketIDs(&(connectbuf[i]->external_socketID), socketID) == 0) {
1805
                                if (ready) return (connectbuf[i]->status == READY ? i : -1);;
1806 351ad48b RobertBirke
                                return i;
1807 6214e30a TivadarSzemethy
                                }
1808 2a178954 KristianBeckers
1809 510f7930 RobertBirke
    return -1;
1810 2a178954 KristianBeckers
}
1811 19c6ca17 TivadarSzemethy
1812 bcf9c2dd RobertBirke
int get_path_mtu(int ConnectionId) {
1813
        if(ConnectionId < 0 || ConnectionId >= CONNECTBUFSIZE)
1814
                return -1;
1815
        if (connectbuf[ConnectionId] != NULL)
1816
                return connectbuf[ConnectionId]->pmtusize;
1817
        return -1;
1818
}
1819
1820 510f7930 RobertBirke
unsigned long resolve(const char *ipaddr)
1821
{
1822 6bbd38ef RobertBirke
        struct hostent *h = gethostbyname(ipaddr);
1823
        if (!h) {
1824
                error("Unable to resolve hostname %s\n", ipaddr);
1825 510f7930 RobertBirke
        exit(-1);
1826
    }
1827
    unsigned long *addr = (unsigned long *) (h->h_addr);
1828
    return *addr;
1829 19c6ca17 TivadarSzemethy
}