Statistics
| Branch: | Revision:

ml / transmissionHandler.h @ 2c702104

History | View | Annotate | Download (14.6 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
#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
#include "ml.h"
59

    
60
#ifndef boolean
61
typedef bool boolean;
62
#endif
63
#ifndef TRUE
64
#define TRUE ((bool)1)
65
#endif
66
#ifndef FALSE
67
#define FALSE ((bool)0)
68
#endif
69

    
70
/**
71
 * This is the size of the monitoring module header that can be added to the messaging layer header
72
 */
73
#define MON_PACKET_HEADER_SIZE 21
74

    
75
/**
76
 * This is the size of the monitoring module header that can be added to a data buffer
77
 */
78
#define MON_DATA_HEADER_SIZE 16
79

    
80
/**
81
  * Define the size of the messaging layer header 
82
  */
83
#define MSGL_HEADER_SIZE 18
84

    
85
/**
86
  * Define a type that is a pointer to a libevent event 
87
  */
88
typedef struct event *sock_event_ptr;
89

    
90
/**
91
 * Defined a mtu size 
92
 */
93
typedef enum {max = 1500, dsl = 1492, dslmedium = 1450, dslslim = 1400, belowdsl = 1200, min = 500,error = -1} pmtu;
94

    
95
/**
96
 * Define connection message types 
97
 */
98
typedef enum {invite = 0, ok = 1, ack = 2} msgtypes;
99

    
100
/**
101
 * A union that considers IPv4 and IPv6 addresses 
102
 */
103
typedef union{
104

    
105
  struct sockaddr_in udpaddr; ///< ipv4 address
106
  struct sockaddr_in6 udpaddr6; ///< ipv6 address
107

    
108
} socketaddrgen;
109

    
110
/**
111
  the socketID contains the internal and the external IP+Port 
112
 */
113
typedef struct _socket_ID {
114

    
115
  socketaddrgen internal_addr; ///< internal address
116
  socketaddrgen external_addr; ///< external or reflexive address
117
} socket_ID;
118

    
119
/**
120
  * A struct that contains information about data that is being received
121
  */
122
typedef struct {
123

    
124
  int recvID; ///< the receive ID
125
  int connectionID; ///< The connectionID
126
  int seqnr; ///< The sequence number
127
  char *recvbuf; ///< A pointer to the receive buffer
128
  int bufsize; ///< The  buffersize
129
  int status; ///< The receive status. 1 if reveived finished
130
  char msgtype; ///< The message type
131
  char monitoringHeaderType; ///< The monitoring header type is 1 or 3 when a monitoring data header is present
132
  char *monitoringDataHeader; ///< A pointer to the monitoring data header
133
  char firstPacketArrived; ///< did the first packet arrive 
134
  int recvFragments; ///< the number of received framgents
135
  int nrFragments; ///< the overall number of fragments
136
  struct event *timeout; ///< a timeout event
137
  struct timeval timeout_value; ///< the value for a libevent timeout
138
  time_t starttime; ///< the start time
139
} recvdata;
140

    
141
struct receive_connection_cb_list{
142
        struct receive_connection_cb_list *next;
143
        receive_connection_cb connection_cb;
144
        void *arg;
145
};
146

    
147
/**
148
 * A struct with information about a connection that exist or a connection that is being established
149
 */
150
typedef struct {
151

    
152
  int connectionID; ///< the connection ID
153
  socketID_handle external_socketID; ///< the external socketID
154
  int external_connectionID; ///< the internal connectionID
155
  pmtu pmtusize; ///< the pmtu size
156
  pmtu pmtutrysize; ///< the last pmtu size that has been tried
157
  int status; ///< the status of the connection. status has the following encoding: 0: Invite send, 1: OK send, 2: connection established
158
  time_t starttime; ///< the time when the first connection attempt was made
159
  int seqnr; ///< sequence number for connections that have been tried
160
  //struct event *timeout;
161
  struct timeval timeout_value; ///< the timeout value for the connection estahlishment
162
  bool internal_connect; ///< set to true if a connection to the internall address could be established
163
  struct receive_connection_cb_list *connection_head;
164
  struct receive_connection_cb_list *connection_last;
165
} connect_data;
166

    
167
/**
168
 * The init method needs to be called to initialise the transmissionHandler
169
 * @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. 
170
 * @param timeout_value A timeout value for the data receival
171
 * @param port A port that will be used to create a new UDP socket 
172
 * @param ipaddr An ipaddress that will be used to create a new UDP socket. If left NULL th
173
e messaging layer binds it to localhost. 
174
 * @param stun_port The port of the stun server.
175
 * @param stun_ipaddr The ip address of the stun server. 
176
 * @param local_socketID_cb A callback from the type receive_localsocketID_cb that is envoke once the socketID is ready to use.
177
 * @param arg A void pointer that is used in this implementation to handover the libevent pointer. 
178
 */
179
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);
180

    
181
/**
182
 * create a socket: returns a socketID handle  
183
 * A socketID handle is a pointer to the opague data structre
184
 * @param port A port that will be used to create a new UDP socket
185
 * @param ipaddr An ipaddress that will be used to create a new UDP socket. If left NULL the messaging layer binds it to localhost. 
186
 */
187
void create_socket(const int port,const char *ipaddr);
188

    
189
/**
190
 * destroys a socket 
191
 * @param socketID The socketID handle to the socket ID that shall be destroyed.
192
 */
193
void close_socket(socketID_handle socketID);
194

    
195
/**
196
 * opens a connection between two socketIDs
197
 * @param local_socketID The local socketID
198
 * @param external_socketID The remote socketID
199
 * @param connectionID A pointer to an int. The connectionID will be stored in that int. 
200
 * @param connection_cb  A function pointer to a callback function from the type receive_connection_cb
201
 * @param arg A void pointer that is returned via the callback.
202
 * @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.
203
 */
204
int open_connection(socketID_handle external_socketID,int *connectionID,receive_connection_cb connection_cb,void *arg);
205

    
206
/**
207
 * destroys a connections
208
 * @param connectionID 
209
 */
210
void close_connection(const int connectionID);
211

    
212

    
213
/**
214
  * Send a NAT traversal keep alive
215
  * @param connectionID
216
  */
217
void keep_connection_alive(const int connectionID);
218

    
219
/**
220
 * send all data
221
 * @param connectionID The connection the data should be send to.
222
 * @param container A container for several buffer pointer and length pairs from type send_all_data_container
223
 * @param nr_entries The number of buffer pointer and length pairs in the container. The maximum nr is 5.  
224
 * @param msgtype The message type.
225
 * @param sParams A pointer to a send_params struct.
226
 * @return 0 if a problem occured, 1 if everything was alright. 
227
 */
228
int send_all_data(const int connectionID,send_all_data_container *container,int nr_entries,unsigned char msgtype,send_params *sParams);
229

    
230
/**
231
 * send data  
232
 * @param connectionID The connection the data should be send to. 
233
 * @param sendbuf A pointer to the send buffer. 
234
 * @param bufsize The buffersize of the send buffer. 
235
 * @param msgtype The message type. 
236
 * @param sParams A pointer to a send_params struct. 
237
 */
238
void send_data(const int connectionID,char *sendbuf,int bufsize,unsigned char msgtype,send_params *sParams);
239

    
240
/**
241
 * receive data  
242
 * @param connectionID The connection the data came from. 
243
 * @param recvbuf A pointer to a buffer the data will be stored in. 
244
 * @param bufsize A pointer to an int that tells the size of the received data. 
245
 * @param rParams A pointer to a recv_params struct. 
246
 */
247
int recv_data(const int connectionID,char *recvbuf,int *bufsize,recv_params *rParams);
248

    
249
/**
250
 * set the stun server  
251
 * @param port The port of the stun server. 
252
 * @param ipaddr The ip address of the stun server. 
253
 */
254
void setStunServer(const int port,const char *ipaddr);
255

    
256
/**
257
 * set recv timeout  
258
 * @param timeout_value The timeout value for receiving data. 
259
 */
260
void setRecvTimeout(struct timeval timeout_value);
261

    
262
/**
263
  * create a message for the connection establishment
264
  * @param *msgbuf A buffer for the message
265
  * @param bufsize The buffersize 
266
  * @param local_connectionID The local connectionID
267
  * @param external_connectionID The external connectionID
268
  * @param local_socketID The local socketID
269
  * @param external_socketID The external socketID
270
  * @param msgtype The message type
271
  */
272
void create_conn_msg(char *msgbuf,int bufsize,int local_connectionID,int external_connectionID,socketID_handle local_socketID,socketID_handle external_socketID,int msgtype);
273

    
274
/**
275
  * send a connect message to another peer
276
  * @param connectionID the connectionID
277
  * @param *msgbuf A pointer to the message buffer
278
  * @param bufsize The buffersize
279
  */
280
void send_conn_msg(const int connectionID,char *msgbuf,int bufsize);
281

    
282
/**
283
  * process an incoming conenction message
284
  * @param local_socketID
285
  * @param *msgbuf
286
  * @param bufsize
287
  */
288
void recv_conn_msg(socketID_handle local_socketID,char *msgbuf,int bufsize);
289

    
290
/**
291
  * process an incoming message
292
  * @param fd A file descriptor
293
  * @param event A libevent event 
294
  * @param arg An arg pointer
295
  */
296
void recv_pkg(int fd, short event, void *arg);
297

    
298
/**
299
  * receive a stun message
300
  * @param *msgbuf A pointer to a message buffer
301
  * @param recvSize The buffersize
302
  */
303
void recv_stun_msg(char *msgbuf,int recvSize);
304

    
305
/**
306
  * process an incoming data message
307
  * @param *msgbuf A pointer to a message buffer
308
  * @param bufsize The buffersize
309
  * @param ttl The ttl value of the packet
310
  */
311
void recv_data_msg(char *msgbuf,int bufsize,int ttl);
312

    
313
/**
314
  * A receive timeout callback for the connections
315
  * @param fd A file descriptor
316
  * @param event A libevent event 
317
  * @param arg An arg pointer          
318
  */
319
void recv_timeout_cb(int fd,short event,void *arg);
320

    
321
/**
322
  * A pmtu timeout callback
323
  * @param fd A file descriptor
324
  * @param event A libevent event 
325
  * @param arg An arg pointer
326
  */
327
void pmtu_timeout_cb(int fd,short event,void *arg);
328

    
329
/**
330
  * A function to decrement the pmtu value
331
  * @param pmtusize A pmtusize 
332
  */
333
pmtu pmtu_decrement(pmtu pmtusize);
334

    
335
/**
336
  * A pmtu error callback
337
  * @param *msg A pointer to a message buffer
338
  * @param msglen The buffersize
339
  */
340
void pmtu_error_cb_th(char *msg,int msglen);
341

    
342
/**
343
 * Compare two socket IDs and check if the external IPaddresses match
344
 * @param sock1 The first socketID                                                                
345
 * @param sock2 The second socketID                                                               
346
 * @return 0 if they match, otherwise 1
347
 */
348
int compare_external_address_socketIDs(socketID_handle sock1,socketID_handle sock2);
349

    
350
/**
351
  * Compare two socket IDs
352
  * @param sock1 The first socketID
353
  * @param sock2 The second socketID
354
  * @return 0 if they match, otherwise 1
355
  */
356
int compare_socketIDs(socketID_handle sock1,socketID_handle sock2);
357

    
358
/**
359
  * A NAT traversal timeout callback
360
  * @param fd A file descriptor
361
  * @param event A libevent event 
362
  * @param arg An arg pointer
363
  */
364
void nat_traversal_timeout(int fd, short event, void *arg);
365

    
366
/**
367
 * A function that returns the standard TTL value the operating system places i
368
n every IP packet                                                              
369
 * @param socketID A local socketID   
370
 * @param ttl A pointer to an integer 
371
 * @return 0 if everything went well and -1 if an error occured
372
 */
373
int get_standard_ttl(socketID_handle socketID,uint8_t *ttl);
374

    
375
/**
376
 * A funtion that returns a handle for the local socketID
377
 * @param errorstatus Returns 0 if the socketID is operational and 2 if the NAT traversal failed
378
 * @return A socketID handle
379
 */
380
socketID_handle get_local_socketID(int *errorstatus);
381

    
382
/**
383
 * A funtion that returns the external IP from the local socketID
384
 * @param external_addr A pointer to a char buffer that holds the externall address
385
 * @return -1 if an error occurred and 0 if it succeded
386
 */
387
int get_external_IP(char* external_addr);
388

    
389
/**
390
 * A funtion that returns a string from a socketID handler
391
 * @param socketID
392
 * @param socketID_string
393
 * @return -1 if an error occurred and 0 if it succeded
394
 */
395
int socketID_to_String(socketID_handle socketID,char* socketID_string);
396

    
397
/**
398
 * A funtion that returns a socketID handler from a string
399
 * @param socketID A socketID in string format
400
 * @return a pointer to a socketID
401
 */
402
int string_to_socketID(char* socketID_string, socketID_handle socketID);
403

    
404
/**
405
 * A funtion that returns the whenever a connection is ready to use or not. 
406
 * @param connectionID The connectionID of the connection that is queried.
407
 * @return -1 if the connection is not ready to use and 0 if it is ready
408
 */
409
int get_connection_status(int connectionID);
410

    
411
/**
412
 * A funtion that returns the whenever a connection is ready to use or not.
413
 * @param socketID A remote socketID to which the connection has been established.
414
 * @return -1 if the connection does not exist and the connection ID if it exists.
415
 */
416
int connection_exist(socketID_handle socketID);
417

    
418
/**
419
 * Resolve a hostname to an unsigned long ready to be loaded into struct in_addr.s_addr
420
 * @param ipaddr the ip addr in a textual format
421
 * @return the resolved address, in network byte ordering
422
 */
423
unsigned long resolve(const char *ipaddr);
424

    
425

    
426
#endif