Statistics
| Branch: | Revision:

ml / transmissionHandler.h @ abbdb922

History | View | Annotate | Download (19.2 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

    
49
#include <netinet/in.h>
50
#include <sys/socket.h>
51
#include <fcntl.h>
52
//#include <event2/event.h>
53
#include <sys/types.h>
54
#include <netinet/in.h>
55
#include <arpa/inet.h>
56
#include "util/udpSocket.h"
57
#include "util/stun.h"
58

    
59
/**
60
 * This is the size of the monitoring module header that can be added to the messaging layer header
61
 */
62
#define MON_PACKET_HEADER_SIZE 16
63

    
64
/**
65
 * This is the size of the monitoring module header that can be added to a data buffer
66
 */
67
#define MON_DATA_HEADER_SIZE 16
68

    
69
/**
70
  * Define the size of the messaging layer header 
71
  */
72
#define MSGL_HEADER_SIZE 18
73

    
74
/**
75
  * Define a type that is a pointer to a libevent event 
76
  */
77
typedef struct event *sock_event_ptr;
78

    
79
/**
80
 * Defined a boolean as int  
81
 */
82
typedef enum { FALSE = 0, TRUE } boolean;
83

    
84
/**
85
 * Defined a mtu size 
86
 */
87
typedef enum {max = 1500, dsl = 1492, dslmedium = 1450, dslslim = 1400, belowdsl = 1200, min = 500,error = -1} pmtu;
88

    
89
/**
90
 * Define connection message types 
91
 */
92
typedef enum {invite = 0, ok = 1, ack = 2} msgtypes;
93

    
94
/**
95
 * A union that considers IPv4 and IPv6 addresses 
96
 */
97
typedef union{
98

    
99
  struct sockaddr_in udpaddr; ///< ipv4 address
100
  struct sockaddr_in6 udpaddr6; ///< ipv6 address
101

    
102
} socketaddrgen;
103

    
104
/**
105
  the socketID contains the internal and the external IP+Port 
106
 */
107
typedef struct {
108

    
109
  socketaddrgen internal_addr; ///< internal address
110
  socketaddrgen external_addr; ///< external or reflexive address
111
  int socket_fd;
112
  //evutil_socket_t socket_fd; ///< libevent socket file descriptor
113
  boolean NAT_traversal; ///< boolean NAT traversal successful if true
114
} socket_ID;
115

    
116
/**
117
 * A struct with direction information of a packet or data that is about to be send. This structure is used to providethe monitoring module with information about where a data/packet will be sent and what the messagetype is. * Typedef that defines a socketID handle a pointer to a socket_ID struct
118
 */
119
typedef socket_ID *socketID_handle;
120

    
121
/**
122
 * A struct with direction information of a packet or data that is about to be send.
123
 */
124
typedef struct {
125

    
126
  socketID_handle remote_socketID; ///< The remote socketID
127
  char msgtype; ///< The message type   
128

    
129
} send_pkt_type_and_dst_inf;
130

    
131
/**
132
  * A struct that contains information about data that is being received
133
  */
134
typedef struct {
135

    
136
  int recvID; ///< the receive ID
137
  int connectionID; ///< The connectionID
138
  int seqnr; ///< The sequence number
139
  char *recvbuf; ///< A pointer to the receive buffer
140
  int bufsize; ///< The  buffersize
141
  int status; ///< The receive status. 1 if reveived finished
142
  char msgtype; ///< The message type
143
  char monitoringHeaderType; ///< The monitoring header type is 1 or 3 when a monitoring data header is present
144
  char *monitoringDataHeader; ///< A pointer to the monitoring data header
145
  char firstPacketArrived; ///< did the first packet arrive 
146
  int recvFragments; ///< the number of received framgents
147
  int nrFragments; ///< the overall number of fragments
148
  struct event *timeout; ///< a timeout event
149
  struct timeval timeout_value; ///< the value for a libevent timeout
150
  time_t starttime; ///< the start time
151
} recvdata;
152

    
153
/**
154
 * A struct with information about a connection that exist or a connection that is being established
155
*/
156
typedef struct {
157

    
158
  int connectionID; ///< the connection ID
159
  socketID_handle local_socketID; ///< the local socketID
160
  socketID_handle external_socketID; ///< the external socketID
161
  int external_connectionID; ///< the internal connectionID
162
  pmtu pmtusize; ///< the pmtu size
163
  pmtu pmtutrysize; ///< the last pmtu size that has been tried
164
  int status; ///< the status of the connection. status has the following encoding: 0: Invite send, 1: OK send, 2: connection established
165
  time_t starttime; ///< the time when the first connection attempt was made
166
  int seqnr; ///< sequence number for connections that have been tried
167
  //struct event *timeout;
168
  struct timeval timeout_value; ///< the timeout value for the connection estahlishment
169

    
170
} connect_data;
171

    
172
/**
173
  * A structure for parameters that is used in the send_Data function. This structure contains options of how the sending should occur.
174
 */
175
typedef struct
176
{
177
  boolean priority; ///<  A boolean variable that states if the data shall be send with priority.
178
  boolean padding; ///<  A boolean that states if missing fragments shall be replaced with '0'
179
  boolean confirmation; ///<   A boolean that causes a receive confirmation               
180
  boolean reliable; ///< A boolean that sends the data over a reliable protocol. Warning: If the messaging layer fails to establish a connection with a reliable protocol it falls back to UDP.
181
} send_params;
182

    
183
/** 
184
 * A struct that is returned from the messaging layer with metadata about the received data.  
185
*/
186
typedef struct {
187

    
188
  char msgtype; ///< The message type.
189
  int connectionID; ///< The connectionID.
190
  int nrMissingFragments; ///< The number of fragments that did not arrive. 
191
  int nrFragments; ///< The overall number of fragments. 
192
  char firstPacketArrived; ///< A boolean that states if the first fragment arrived.
193

    
194
} recv_params;
195

    
196
/**
197
  * A struct that contains metadata about messaging layer data. It used to transfer metadata about arrived messaging layer data to the monitoring module.
198
 * 
199
 */
200
typedef struct {
201

    
202
  socketID_handle remote_socketID; ///< The remote socketID 
203
  char *buffer; ///< A pointer to the data that was received
204
  int bufSize; ///< The size of the data that was received
205
  char msgtype; ///< The message type
206
  char monitoringHeaderType; ///<  This value indicates if a monitoring header was added to the data. The header is added, when the value is eiter 1 or 3.
207
  char *monitoringDataHeader; ///<  A pointer to the monitoring header.     
208
  time_t arrival_time; ///< The arrival time of the data
209
  char firstPacketArrived; ///< A boolean that states if the first fragment arrived. 
210
  int nrMissingFragments; ///<  The number of fragments that did not arrive. 
211
  int nrFragments; ///< The overall number of fragments.    
212
  boolean priority; ///< A boolean variable that states if the data was send with priority.     
213
  boolean padding; ///< A boolean that states if missing fragments were replaced with '0'
214
  boolean confirmation; ///<  A boolean that states if a receive confirmation was send
215
  boolean reliable; ///< A boolean that the data was send  over a reliable protocol.
216

    
217
} mon_data_inf; 
218

    
219
/**
220
 * A struct of metadata about messaging layer packets. It is used to inform the monitoring module about arrived packets
221
 */
222
typedef struct {
223

    
224
  socketID_handle remote_socketID; ///< The remote socketID
225
  char msgtype; ///<  The message type
226
  int dataID; ///< The data ID field from the messaging layer header. 
227
  int offset; ///< The data offset field from the messaging layer header.
228
  int datasize; ///< The datasize field from the messaging layer header.      
229
  char monitoringHeaderType; ///< This value indicates if a monitoring header was added to the packet. The header is added, when the value is eiter 2 or 3.
230
  char *monitoringHeader; ///< A pointer to the monitoring header.
231
  int ttl; ///< The TTL Field from the IP Header of the packet.
232
  time_t arrival_time; ///< The arrival time of the packet.
233
  
234
} mon_pkt_inf;
235

    
236
/**
237
 * Typedef for a callback when a UDP packet is received
238
 * the callback receives a pointer to a mon_pkt_inf struct as param
239
 * This shall be used by the monitoring module
240
*/
241
typedef void (*get_recv_pkt_inf_cb)(void *arg);
242

    
243
/**
244
 * Typedef for a callback that asks if a monitoring module packet header shall be set
245
 * the first param is a pointer to the monitoring module packet header
246
 * the second param is a pointer to a send_pkt_type_and_dst_inf struct
247
 * return value returns 0 for no header and 1 for header 
248
 */
249
typedef int (*set_monitoring_header_pkt_cb)(void *arg,send_pkt_type_and_dst_inf *send_pkt_inf);
250

    
251
/**
252
 * Typedef for a callback that is envoked when data has been received
253
 * The param is a pointer to a mon_data_inf struct 
254
 */
255
typedef void (*get_recv_data_inf_cb)(void *arg);
256

    
257
/**
258
 * Typedef for a callback when data has been send
259
 * The param is a pointer to a mon_data_inf struct   
260
 */
261
typedef void (*get_send_data_inf_cb)(void *arg);
262

    
263
/**
264
 * Typedef for a callback that asks if a monitoring module header for data shall be set 
265
 * the first param is a pointer to the monitoring module data header
266
 * the second param is a pointer to a send_pkt_type_and_dst_inf struct  
267
 * return value returns 0 for no header and 1 for header 
268
 */
269
typedef int (*set_monitoring_header_data_cb)(void *arg,send_pkt_type_and_dst_inf *send_pkt_inf);
270

    
271
/**
272
 * Typedef for a callback when a new connection is established
273
 */
274
typedef void (*receive_connection_cb)(int connectionID, void *arg);
275

    
276
/**
277
 * Typedef for a callback when a connection attemped failed 
278
 */
279
typedef void (*connection_failed_cb)(int connectionID,void *arg);
280

    
281
/**
282
 * Typedef for a callback when data is received
283
 * The first param is a pointer to a buffer that has the data
284
 * The second param is an int that holds the length of the data
285
 * The third param is the message type
286
 * The fourth param is a pointer to a recv_params struct 
287
*/
288
typedef void (*receive_data_cb)(char *buffer,int buflen,unsigned char msgtype,void *arg);
289

    
290
/**
291
 * This function is to register a callback that is envoked when a messaging layer packet is received.  
292
 * @param recv_pkt_inf_cb A function pointer to a callback function from the type get_recv_pkt_inf_cb
293
 */
294
void register_get_recv_pkt_inf(get_recv_pkt_inf_cb recv_pkt_inf_cb);
295

    
296

    
297
/**
298
 *  This function is to register a callback that allows to add a monitoring module header to a packet. 
299
 * @param monitoring_header_pkt_cb A function pointer to a callback function from the type set_monitoring_header_pkt_cb
300
*/
301
void register_set_monitoring_header_pkt_cb(set_monitoring_header_pkt_cb monitoring_header_pkt_cb);
302

    
303
/**
304
 * Register a callback that is called when data is received  
305
 * @param recv_data_inf_cb  A function pointer to a callback function from the type get_recv_data_inf_cb
306
*/
307
void register_get_recv_data_inf(get_recv_data_inf_cb recv_data_inf_cb);
308

    
309
/**
310
 * Register a callback that is invoked when data is sent  
311
 * @param send_data_inf_cb  A function pointer to a callback function from the type get_send_data_inf_cb
312
*/
313
void register_get_send_data_inf(get_send_data_inf_cb  send_data_inf_cb);
314

    
315
/**
316
 * Register a callback that allows the monitoring module to set a header on data  
317
 * @param monitoring_header_data_cb A function pointer to a callback function from the type set_monitoring_header_data_cb
318
*/
319
void register_set_monitoring_header_data_cb(set_monitoring_header_data_cb monitoring_header_data_cb);
320

    
321
/**
322
 * This function is to register a callback that informs the upper layer that a new connection is established.
323
 * @param connection_cb  A function pointer to a callback function from the type receive_connection_cb
324
 */
325
void register_recv_connection_cb(receive_connection_cb connection_cb);
326

    
327
/**
328
 * This function is to register a calllback that informs the upper layer that a connection attempt failed. 
329
 * @param connection_fail  A function pointer to a callback function from the type connection_failed_cb
330
 */
331
void register_error_connection_cb(connection_failed_cb connection_fail);
332

    
333
/**
334
 * This function is to register callbacks for received messages
335
 * at maximum 127 callbacks can be registered, one per message type,
336
 * each registered callback returns a value between 0 and 126
337
 * this value must be set as message type while sending data. 
338
 * When a message type is set twice with a different function pointer it will simply override the first. 
339
 * @param data_cb A function pointer to a callback function from the type receive_data_cb 
340
 * @param msgtype The message type
341
 */
342
void register_recv_data_cb(receive_data_cb data_cb,unsigned char msgtype);
343

    
344
/**
345
 * The init method needs to be called to initialise the transmissionHandler
346
 * @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. 
347
 * @param timeout_value A timeout value for the data receival. 
348
 * @param stun_port The port of the stun server.
349
 * @param stun_ipaddr The ip address of the stun server. 
350
 */
351
void init_transmissionHandler(boolean recv_data_cb,struct timeval timeout_value,const int stun_port,const char *stun_ipaddr);
352

    
353
/**
354
 * create a socket: returns a socketID handle  
355
 * A socketID handle is a pointer to the opague data structre
356
 * @param port A port that will be used to create a new UDP socket
357
 * @param ipaddr An ipaddress that will be used to create a new UDP socket. If left NULL the messaging layer binds it to localhost. 
358
*/
359
socketID_handle create_socket(const int port,const char *ipaddr);
360

    
361
/**
362
 * destroys a socket 
363
 * @param socketID The socketID handle to the socket ID that shall be destroyed.
364
*/
365
void close_socket(socketID_handle socketID);
366

    
367
/**
368
 * opens a connection between two socketIDs
369
 * @param local_socketID The local socketID
370
 * @param external_socketID The remote socketID
371
 * @param *connectionID A pointer to an int. The connectionID will be stored in that int. 
372
 * @return returnValue A value that indicates if the messaging layer could try to establish the connection. It returns 1 for success and 0 for a failure.
373
 */
374
int open_connection(socketID_handle local_socketID,socketID_handle external_socketID,int *connectionID);
375

    
376
/**
377
 * destroys a connections
378
 * @param connectionID 
379
 */
380
void close_connection(const int connectionID);
381

    
382

    
383
/**
384
  * Send a NAT traversal keep alive
385
  * @param connectionID
386
  */
387
void keep_connection_alive(const int connectionID);
388

    
389
/**
390
 * send data  
391
 * @param connectionID The connection the data should be send to. 
392
 * @param sendbuf A pointer to the send buffer. 
393
 * @param bufsize The buffersize of the send buffer. 
394
 * @param msgtype The message type. 
395
 * @param sParams A pointer to a send_params struct. 
396
 */
397
void send_data(const int connectionID,char *sendbuf,int bufsize,unsigned char msgtype,send_params *sParams);
398

    
399
/**
400
 * receive data  
401
 * @param connectionID The connection the data came from. 
402
 * @param recvbuf A pointer to a buffer the data will be stored in. 
403
 * @param bufsize A pointer to an int that tells the size of the received data. 
404
 * @param rParams A pointer to a recv_params struct. 
405
 */
406
int recv_data(const int connectionID,char *recvbuf,int *bufsize,recv_params *rParams);
407

    
408
/**
409
 * set the stun server  
410
 * @param port The port of the stun server. 
411
 * @param ipaddr The ip address of the stun server. 
412
 */
413
void setStunServer(const int port,const char *ipaddr);
414

    
415
/**
416
 * set recv timeout  
417
 * @param timeout_value The timeout value for receiving data. 
418
 */
419
void setRecvTimeout(struct timeval timeout_value);
420

    
421
/**
422
  * create a message for the connection establishment
423
  * @param *msgbuf A buffer for the message
424
  * @param bufsize The buffersize 
425
  * @param local_connectionID The local connectionID
426
  * @param external_connectionID The external connectionID
427
  * @param local_socketID The local socketID
428
  * @param external_socketID The external socketID
429
  * @param msgtype The message type
430
  */
431
void create_conn_msg(char *msgbuf,int bufsize,int local_connectionID,int external_connectionID,socketID_handle local_socketID,socketID_handle external_socketID,int msgtype);
432

    
433
/**
434
  * send a connect message to another peer
435
  * @param connectionID the connectionID
436
  * @param *msgbuf A pointer to the message buffer
437
  * @param bufsize The buffersize
438
  */
439
void send_conn_msg(const int connectionID,char *msgbuf,int bufsize);
440

    
441
/**
442
  * process an incoming conenction message
443
  * @param local_socketID
444
  * @param *msgbuf
445
  * @param bufsize
446
  */
447
void recv_conn_msg(socketID_handle local_socketID,char *msgbuf,int bufsize);
448

    
449
/**
450
  * process an incoming message
451
  * @param fd A file descriptor
452
  * @param event A libevent event 
453
  * @param arg An arg pointer
454
  */
455
void recv_pkg(int fd, short event, void *arg);
456

    
457
/**
458
  * receive a stun message
459
  * @param *msgbuf A pointer to a message buffer
460
  * @param recvSize The buffersize
461
  * @param local_socketID The local socketID
462
  */
463
void recv_stun_msg(char *msgbuf,int recvSize,socketID_handle local_socketID);
464

    
465
/**
466
  * process an incoming data message
467
  * @param *msgbuf A pointer to a message buffer
468
  * @param bufsize The buffersize
469
  * @param ttl The ttl value of the packet
470
  */
471
void recv_data_msg(char *msgbuf,int bufsize,int ttl);
472

    
473
/**
474
  * A receive timeout callback for the connections
475
  * @param fd A file descriptor
476
  * @param event A libevent event 
477
  * @param arg An arg pointer          
478
  */
479
void recv_timeout_cb(int fd,short event,void *arg);
480

    
481
/**
482
  * A pmtu timeout callback
483
  * @param fd A file descriptor
484
  * @param event A libevent event 
485
  * @param arg An arg pointer
486
  */
487
void pmtu_timeout_cb(int fd,short event,void *arg);
488

    
489
/**
490
  * A function to decrement the pmtu value
491
  * @param pmtusize A pmtusize 
492
  */
493
pmtu pmtu_decrement(pmtu pmtusize);
494

    
495
/**
496
  * A pmtu error callback
497
  * @param *msg A pointer to a message buffer
498
  * @param msglen The buffersize
499
  */
500
void pmtu_error_cb_th(char *msg,int msglen);
501

    
502
/**
503
  * Compare two socket IDs
504
  * @param sock1 The first socketID
505
  * @param sock2 The second socketID
506
  */
507
int compare_socketIDs(socketID_handle sock1,socketID_handle sock2);
508

    
509
/**
510
  * A NAT traversal timeout callback
511
  * @param fd A file descriptor
512
  * @param event A libevent event 
513
  * @param arg An arg pointer
514
  */
515
void  nat_traversal_timeout(int fd, short event, void *arg);
516

    
517
/**
518
 * A function that returns the standard TTL value the operating system places i
519
n every IP packet                                                              
520
 * @param socketID A local socketID   
521
 * @param ttl A pointer to an integer 
522
 * @return 0 if everything went well and -1 if an error occured
523
 */
524
int get_standard_ttl(socketID_handle socketID,int *ttl);
525

    
526
#endif