Statistics
| Branch: | Revision:

ml / transmissionHandler.h @ 372c2c31

History | View | Annotate | Download (16.9 KB)

1
/*
2
 *          Policy Management
3
 *
4
 *          NEC Europe Ltd. PROPRIETARY INFORMATION
5
 *
6
 * This software is supplied under the terms of a license agreement
7
 * or nondisclosure agreement with NEC Europe Ltd. and may not be
8
 * copied or disclosed except in accordance with the terms of that
9
 * agreement.
10
 *
11
 *      Copyright (c) 2009 NEC Europe Ltd. All Rights Reserved.
12
 *
13
 * Authors: Kristian Beckers  <beckers@nw.neclab.eu>
14
 *          Sebastian Kiesel  <kiesel@nw.neclab.eu>
15
 *          
16
 *
17
 * NEC Europe Ltd. DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED,
18
 * INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY
19
 * AND FITNESS FOR A PARTICULAR PURPOSE AND THE WARRANTY AGAINST LATENT
20
 * DEFECTS, WITH RESPECT TO THE PROGRAM AND THE ACCOMPANYING
21
 * DOCUMENTATION.
22
 *
23
 * No Liability For Consequential Damages IN NO EVENT SHALL NEC Europe
24
 * Ltd., NEC Corporation OR ANY OF ITS SUBSIDIARIES BE LIABLE FOR ANY
25
 * DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS
26
 * OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF INFORMATION, OR
27
 * OTHER PECUNIARY LOSS AND INDIRECT, CONSEQUENTIAL, INCIDENTAL,
28
 * ECONOMIC OR PUNITIVE DAMAGES) ARISING OUT OF THE USE OF OR INABILITY
29
 * TO USE THIS PROGRAM, EVEN IF NEC Europe Ltd. HAS BEEN ADVISED OF THE
30
 * POSSIBILITY OF SUCH DAMAGES.
31
 *
32
 *     THIS HEADER MAY NOT BE EXTRACTED OR MODIFIED IN ANY WAY.
33
 */
34

    
35
/**
36
 * @file transmissionHandler.h
37
 * @brief The transmissionHandler takes care of connecting peers and exchanging data between them.
38
 *
39
 * @author Kristian Beckers  <beckers@nw.neclab.eu>                             
40
 * @author Sebastian Kiesel  <kiesel@nw.neclab.eu> 
41
 *
42
 *                                                                              
43
 * @date 7/28/2009 
44
 */
45

    
46
#ifndef TRANSMISSIONHANDLER_H
47
#define TRANSMISSIONHANDLER_H
48
#include <sys/time.h>
49
#ifndef _WIN32
50
#include <netinet/in.h>
51
#include <sys/socket.h>
52
#include <arpa/inet.h>
53
#include <sys/uio.h>
54
#endif
55
#include <fcntl.h>
56
#include <event2/event.h>
57
#include <sys/types.h>
58
#include "util/udpSocket.h"
59
#include "util/stun.h"
60
#include "ml.h"
61

    
62
#ifndef _WIN32
63
#ifndef boolean
64
typedef bool boolean;
65
#endif
66
#endif
67

    
68
#ifndef TRUE
69
#define TRUE ((bool)1)
70
#endif
71
#ifndef FALSE
72
#define FALSE ((bool)0)
73
#endif
74

    
75
#ifdef RTX
76
/**
77
 * This is the maximum number of gaps RTX can keep track of inside one message
78
 */
79
#define RTX_MAX_GAPS 25
80

    
81
#define ML_NACK_MSG 128
82
#endif
83
/**
84
 * This is the maximum size of the monitoring module header that can be added to the messaging layer header
85
 */
86
#define MON_DATA_HEADER_SPACE 32
87
#define MON_PKT_HEADER_SPACE 32
88

    
89
/**
90
  * Define the size of the messaging layer header 
91
  */
92
#define MSGL_HEADER_SIZE 18
93

    
94
/**
95
  * Define a type that is a pointer to a libevent event 
96
  */
97
typedef struct event *sock_event_ptr;
98

    
99
/**
100
 * Defined a mtu size - IP size - UDP size
101
 */
102
typedef enum {MAX = 1472, DSL = 1464, DSLMEDIUM = 1422, DSLSLIM = 1372, BELOWDSL = 1172, MIN = 472, P_ERROR = -1} pmtu;
103

    
104
/**
105
 * Define connection command types
106
 */
107
typedef enum {INVITE = 0, CONNECT, READY} con_msg_types;
108

    
109
/**
110
 * Define receiver buffer status
111
 */
112

    
113
typedef enum {ACTIVE = 0, INACTIVE, COMPLETE} recvbuf_status_types;
114

    
115
/**
116
 * A union that considers IPv4 and IPv6 addresses 
117
 */
118
typedef union{
119

    
120
  struct sockaddr_in udpaddr; ///< ipv4 address
121
  struct sockaddr_in6 udpaddr6; ///< ipv6 address
122

    
123
} socketaddrgen;
124

    
125
/**
126
  the socketID contains the internal and the external IP+Port 
127
 */
128
typedef struct _socket_ID {
129

    
130
  socketaddrgen internal_addr; ///< internal address
131
  socketaddrgen external_addr; ///< external or reflexive address
132
} socket_ID;
133

    
134
#ifdef RTX
135
struct gap {
136
        int offsetFrom;
137
        int offsetTo;
138
};
139
#endif
140

    
141
/**
142
  * A struct that contains information about data that is being received
143
  */
144
typedef struct {
145

    
146
  int recvID; ///< the receive ID
147
  int connectionID; ///< The connectionID
148
  int seqnr; ///< The sequence number
149
  char *recvbuf; ///< A pointer to the receive buffer
150
  int bufsize; ///< The  buffersize
151
  int status; ///< The receive status. 1 if receiving finished
152
  char msgtype; ///< The message type
153
  char firstPacketArrived; ///< did the first packet arrive 
154
  int recvFragments; ///< the number of received framgents
155
  int arrivedBytes; ///< the number of received Bytes
156
  int monitoringDataHeaderLen; ///< size of the monitoring data header (0 == no header)
157
  struct event *timeout_event; ///< a timeout event
158
  struct timeval timeout_value; ///< the value for a libevent timeout
159
  time_t starttime; ///< the start time
160
#ifdef RTX
161
  struct event* last_pkt_timeout_event;
162
  int txConnectionID;
163
  int expectedOffset;
164
  int gapCounter; //index of the first "free slot"
165
  int firstGap;        //first gap which hasn't been handled yet (for which the NACK hasn't been sent yet)
166
  struct gap gapArray[RTX_MAX_GAPS];
167
#endif
168
} recvdata;
169

    
170
struct receive_connection_cb_list{
171
        struct receive_connection_cb_list *next;
172
        receive_connection_cb connection_cb;
173
        void *arg;
174
};
175

    
176
/**
177
 * A struct with information about a connection that exist or a connection that is being established
178
 */
179
typedef struct {
180

    
181
  int connectionID; ///< the connection ID
182
  socket_ID external_socketID; ///< the external socketID
183
  int external_connectionID; ///< the internal connectionID
184
  pmtu pmtusize; ///< the pmtu size
185
  bool delay;
186
  int trials;
187
  char *ctrl_msg_buf;
188
  int status; ///< the status of the connection. status has the following encoding: 0: INVITE send, 1: CONNECT send, 2: connection established
189
  time_t starttime; ///< the time when the first connection attempt was made
190
  uint32_t seqnr; ///< sequence number for connections that have been tried
191
  struct event *timeout_event;
192
  struct timeval timeout_value; ///< the timeout value for the connection establishment
193
  bool internal_connect; ///< set to true if a connection to the internal address could be established
194
  struct receive_connection_cb_list *connection_head;
195
  struct receive_connection_cb_list *connection_last;
196
  send_params defaultSendParams;
197
  uint32_t keepalive_seq; 
198
} connect_data;
199

    
200
#define ML_CON_MSG 127
201

    
202

    
203
/**
204
 * A struct with the messaging layer header for connection handling messages
205
 */
206
#pragma pack(push)  /* push current alignment to stack */
207
#pragma pack(1)     /* set alignment to 1 byte boundary */
208
struct conn_msg {
209
        uint32_t comand_type; ///< see con_msg_types
210
        int32_t pmtu_size;        /// the pmtu size 
211
        socket_ID sock_id;        /// the socketId of the sender
212
} __attribute__((packed));
213

    
214
#ifdef RTX
215
/************modifications-START************/
216

    
217
struct nack_msg {
218
        int32_t con_id;                ///local connectionID of the transmitter
219
        int32_t msg_seq_num;
220
        uint32_t offsetFrom;
221
        uint32_t offsetTo;
222
} __attribute__((packed));
223

    
224
/************modifications-END**************/
225
#endif
226

    
227
struct msg_header {
228
        uint32_t offset;
229
        uint32_t msg_length;
230
        int32_t local_con_id;        ///> the local connection id
231
        int32_t remote_con_id;        ///> the remote connection id
232
        int32_t msg_seq_num;
233
        uint8_t msg_type; ///< set to ML_CON_MSG
234
        uint8_t len_mon_data_hdr;
235
        uint8_t len_mon_packet_hdr;
236
};
237
#define MSG_HEADER_SIZE (sizeof(struct msg_header))
238
#pragma pack(pop)   /* restore original alignment from stack */
239

    
240
int sendPacket(const int udpSocket, struct iovec *iov, int len, struct sockaddr_in *socketaddr);
241

    
242
//
243
///**
244
// * The init method needs to be called to initialise the transmissionHandler
245
// * @param recv_data_cb This is a boolean that decides how an upper layer receives data. When the boolean is set to true data is received via callback. When it is set to false the upper layer has to poll with the recv_data function.
246
// * @param timeout_value A timeout value for the data receival
247
// * @param port A port that will be used to create a new UDP socket
248
// * @param ipaddr An ipaddress that will be used to create a new UDP socket. If left NULL th
249
//e messaging layer binds it to localhost.
250
// * @param stun_port The port of the stun server.
251
// * @param stun_ipaddr The ip address of the stun server.
252
// * @param local_socketID_cb A callback from the type receive_localsocketID_cb that is envoke once the socketID is ready to use.
253
// * @param arg A void pointer that is used in this implementation to handover the libevent pointer.
254
// */
255
//void init_transmissionHandler(bool recv_data_cb,struct timeval timeout_value,const int port,const char *ipaddr,const int stun_port,const char *stun_ipaddr,receive_localsocketID_cb local_socketID_cb,void *arg);
256
//
257
///**
258
// * create a socket: returns a socketID handle
259
// * A socketID handle is a pointer to the opague data structre
260
// * @param port A port that will be used to create a new UDP socket
261
// * @param ipaddr An ipaddress that will be used to create a new UDP socket. If left NULL the messaging layer binds it to localhost.
262
// */
263
//void create_socket(const int port,const char *ipaddr);
264
//
265
///**
266
// * destroys a socket
267
// * @param socketID The socketID handle to the socket ID that shall be destroyed.
268
// */
269
//void close_socket(socketID_handle socketID);
270
//
271
///**
272
// * opens a connection between two socketIDs
273
// * @param local_socketID The local socketID
274
// * @param external_socketID The remote socketID
275
// * @param connectionID A pointer to an int. The connectionID will be stored in that int.
276
// * @param connection_cb  A function pointer to a callback function from the type receive_connection_cb
277
// * @param arg A void pointer that is returned via the callback.
278
// * @return returnValue the connectionId, < 0 for a failure.
279
// */
280
//int open_connection(socketID_handle external_socketID,receive_connection_cb connection_cb,void *arg);
281
//
282
///**
283
// * destroys a connections
284
// * @param connectionID
285
// */
286
//void close_connection(const int connectionID);
287
//
288
//
289
///**
290
//  * Send a NAT traversal keep alive
291
//  * @param connectionID
292
//  */
293
//void keep_connection_alive(const int connectionID);
294
//
295
///**
296
// * send all data
297
// * @param connectionID The connection the data should be send to.
298
// * @param container A container for several buffer pointer and length pairs from type send_all_data_container
299
// * @param nr_entries The number of buffer pointer and length pairs in the container. The maximum nr is 5.
300
// * @param msgtype The message type.
301
// * @param sParams A pointer to a send_params struct.
302
// * @return 0 if a problem occured, 1 if everything was alright.
303
// */
304
//int send_all_data(const int connectionID,send_all_data_container *container,int nr_entries,unsigned char msgtype,send_params *sParams);
305
//
306
///**
307
// * send data
308
// * @param connectionID The connection the data should be send to.
309
// * @param sendbuf A pointer to the send buffer.
310
// * @param bufsize The buffersize of the send buffer.
311
// * @param msgtype The message type.
312
// * @param sParams A pointer to a send_params struct.
313
// */
314
//void send_data(const int connectionID,char *sendbuf,int bufsize,int msgtype,send_params *sParams);
315
//
316
//void send_msg(int con_id, int msg_type, char* msg, int msg_len, bool truncable, send_params * sparam);
317
///**
318
// * receive data
319
// * @param connectionID The connection the data came from.
320
// * @param recvbuf A pointer to a buffer the data will be stored in.
321
// * @param bufsize A pointer to an int that tells the size of the received data.
322
// * @param rParams A pointer to a recv_params struct.
323
// */
324
//int recv_data(const int connectionID,char *recvbuf,int *bufsize,recv_params *rParams);
325
//
326
///**
327
// * set the stun server
328
// * @param port The port of the stun server.
329
// * @param ipaddr The ip address of the stun server.
330
// */
331
//void setStunServer(const int port,const char *ipaddr);
332
//
333
///**
334
// * unset the stun server
335
// */
336
//void unsetStunServer();
337
//
338
///**
339
// * checks whether a stun server was defined
340
// * @returns true if a stun serve rwas defined
341
// */
342
//bool isStunDefined();
343
//
344
///**
345
// * set recv timeout
346
// * @param timeout_value The timeout value for receiving data.
347
// */
348
//void setRecv1Timeout(struct timeval timeout_value);
349
//
350
///**
351
//  * create a message for the connection establishment
352
//  * @param *msgbuf A buffer for the message
353
//  * @param bufsize The buffersize
354
//  * @param local_connectionID The local connectionID
355
//  * @param local_socketID The local socketID
356
//  * @param command_type The command type
357
//  * @return -1 for failure otherwise the size of the allocated control message buffer
358
//  */
359
//
360
//int
361
//create_conn_msg(int pmtusize,int local_connectionID, socketID_handle local_socketID, con_msg_types command_type);
362
//
363
///**
364
//  * send a connect message to another peer
365
//  * @param connectionID the connectionID
366
//  * @param *msgbuf A pointer to the message buffer
367
//  * @param bufsize The buffersize
368
//  */
369
//void send_conn_msg(const int connectionID,int bufsize, int command);
370
//
371
///**
372
//  * process an incoming conenction message
373
//  * @param local_socketID
374
//  * @param bufsize
375
//  */
376
//void recv_conn_msg(struct msg_header *msg_h,char *msgbuf,int bufsize);
377
//
378
///**
379
//  * process an incoming message
380
//  * @param fd A file descriptor
381
//  * @param event A libevent event
382
//  * @param arg An arg pointer
383
//  */
384
//void recv_pkg(int fd, short event, void *arg);
385
//
386
///**
387
//  * receive a stun message
388
//  * @param *msgbuf A pointer to a message buffer
389
//  * @param recvSize The buffersize
390
//  */
391
//void recv_stun_msg(char *msgbuf,int recvSize);
392
//
393
///**
394
//  * process an incoming data message
395
//  * @param *msgbuf A pointer to a message buffer
396
//  * @param bufsize The buffersize
397
//  * @param ttl The ttl value of the packet
398
//  */
399
//void recv_data_msg(struct msg_header *msg_h, char *msgbuf, int bufsize);
400
//
401
///**
402
//  * A receive timeout callback for the connections
403
//  * @param fd A file descriptor
404
//  * @param event A libevent event
405
//  * @param arg An arg pointer
406
//  */
407
//void recv_timeout_cb(int fd,short event,void *arg);
408
//
409
///**
410
//  * A pmtu timeout callback
411
//  * @param fd A file descriptor
412
//  * @param event A libevent event
413
//  * @param arg An arg pointer
414
//  */
415
//void pmtu_timeout_cb(int fd,short event,void *arg);
416
//
417
///**
418
//  * A function to decrement the pmtu value
419
//  * @param pmtusize A pmtusize
420
//  */
421
//pmtu pmtu_decrement(pmtu pmtusize);
422
//
423
///**
424
//  * A pmtu error callback
425
//  * @param *msg A pointer to a message buffer
426
//  * @param msglen The buffersize
427
//  */
428
//void pmtu_error_cb_th(char *msg,int msglen);
429
//
430
///**
431
// * Compare two socket IDs and check if the external IPaddresses match
432
// * @param sock1 The first socketID
433
// * @param sock2 The second socketID
434
// * @return 0 if they match, otherwise 1
435
// */
436
//int compare_external_address_socketIDs(socketID_handle sock1,socketID_handle sock2);
437
//
438
///**
439
//  * Compare two socket IDs
440
//  * @param sock1 The first socketID
441
//  * @param sock2 The second socketID
442
//  * @return 0 if they match, otherwise 1
443
//  */
444
//int mlCompareSocketIDs(socketID_handle sock1,socketID_handle sock2);
445
//
446
///**
447
//  * A NAT traversal timeout callback
448
//  * @param fd A file descriptor
449
//  * @param event A libevent event
450
//  * @param arg An arg pointer
451
//  */
452
//void nat_traversal_timeout(int fd, short event, void *arg);
453
//
454
///**
455
// * A function that returns the standard TTL value the operating system places i
456
//n every IP packet
457
// * @param socketID A local socketID
458
// * @param ttl A pointer to an integer
459
// * @return 0 if everything went well and -1 if an error occured
460
// */
461
//int get_standard_ttl(socketID_handle socketID,uint8_t *ttl);
462
//
463
///**
464
// * A funtion that returns a handle for the local socketID
465
// * @param errorstatus Returns 0 if the socketID is operational and 2 if the NAT traversal failed
466
// * @return A socketID handle
467
// */
468
//socketID_handle get_local_socketID(int *errorstatus);
469
//
470
///**
471
// * A funtion that returns the external IP from the local socketID
472
// * @param external_addr A pointer to a char buffer that holds the externall address
473
// * @return -1 if an error occurred and 0 if it succeded
474
// */
475
//int get_external_IP(char* external_addr);
476
//
477
///**
478
// * A funtion that returns a string from a socketID handler
479
// * @param socketID
480
// * @param socketID_string
481
// * @param len size of socketID_string buffer
482
// * @return -1 if an error occurred and 0 if it succeded
483
// */
484
//int mlSoc2ketIDToString(socketID_handle socketID,char* socketID_string, size_t len);
485
//
486
///**
487
// * A funtion that returns a socketID handler from a string
488
// * @param socketID A socketID in string format
489
// * @return a pointer to a socketID
490
// */
491
//int string_to_socketID(const char* socketID_string, socketID_handle socketID);
492
//
493
///**
494
// * A funtion that returns the whenever a connection is ready to use or not.
495
// * @param connectionID The connectionID of the connection that is queried.
496
// * @return the status or -1 if the connectionID does not exist
497
// */
498
//int get_connection_status(int connectionID);
499
//
500
///**
501
// * A funtion that returns the whenever a connection is ready to use or not.
502
// * @param socketID A remote socketID to which the connection has been established.
503
// * @param ready if true,return the connID for READY connections only
504
// * @return -1 if the connection does not exist and the connection ID if it exists.
505
// */
506
//int connection_exist(socketID_handle socketID,bool ready);
507
//
508
///**
509
// * Resolve a hostname to an unsigned long ready to be loaded into struct in_addr.s_addr
510
// * @param ipaddr the ip addr in a textual format
511
// * @return the resolved address, in network byte ordering
512
// */
513
//unsigned long resolve(const char *ipaddr);
514
//
515
//int get_path_mtu(int ConnectionId);
516

    
517

    
518
#endif