Statistics
| Branch: | Revision:

ml / transmissionHandler.h @ 0b960be3

History | View | Annotate | Download (15.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
#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 <sys/uio.h>
57
#include "util/udpSocket.h"
58
#include "util/stun.h"
59
#include "ml.h"
60

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

    
71
/**
72
 * This is the maximum size of the monitoring module header that can be added to the messaging layer header
73
 */
74
#define MON_HEADER_SPACE 32
75

    
76

    
77
/**
78
  * Define the size of the messaging layer header 
79
  */
80
#define MSGL_HEADER_SIZE 18
81

    
82
#define MAX_TRIALS 3
83

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

    
89
/**
90
 * Defined a mtu size - IP size - UDP size
91
 */
92
typedef enum {MAX = 1472, DSL = 1464, DSLMEDIUM = 1422, DSLSLIM = 1372, BELOWDSL = 1172, MIN = 472,ERROR = -1} pmtu;
93

    
94
/**
95
 * Define connection command types
96
 */
97
typedef enum {INVITE = 0, CONNECT, READY} con_msg_types;
98

    
99
/**
100
 * Define receiver buffer status
101
 */
102

    
103
typedef enum {ACTIVE = 0, INACTIVE, COMPLETE} recvbuf_status_types;
104

    
105
/**
106
 * A union that considers IPv4 and IPv6 addresses 
107
 */
108
typedef union{
109

    
110
  struct sockaddr_in udpaddr; ///< ipv4 address
111
  struct sockaddr_in6 udpaddr6; ///< ipv6 address
112

    
113
} socketaddrgen;
114

    
115
/**
116
  the socketID contains the internal and the external IP+Port 
117
 */
118
typedef struct _socket_ID {
119

    
120
  socketaddrgen internal_addr; ///< internal address
121
  socketaddrgen external_addr; ///< external or reflexive address
122
} socket_ID;
123

    
124
/**
125
  * A struct that contains information about data that is being received
126
  */
127
typedef struct {
128

    
129
  int recvID; ///< the receive ID
130
  int connectionID; ///< The connectionID
131
  int seqnr; ///< The sequence number
132
  char *recvbuf; ///< A pointer to the receive buffer
133
  int bufsize; ///< The  buffersize
134
  int status; ///< The receive status. 1 if reveived finished
135
  char msgtype; ///< The message type
136
  char firstPacketArrived; ///< did the first packet arrive 
137
  int recvFragments; ///< the number of received framgents
138
  int arrivedBytes; ///< the number of received Bytes
139
  int monitoringDataHeaderLen; ///< size of the monitoring data header (0 == no header)
140
  struct event *timeout; ///< a timeout event
141
  struct timeval timeout_value; ///< the value for a libevent timeout
142
  time_t starttime; ///< the start time
143
} recvdata;
144

    
145
struct receive_connection_cb_list{
146
        struct receive_connection_cb_list *next;
147
        receive_connection_cb connection_cb;
148
        void *arg;
149
};
150

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

    
156
  int connectionID; ///< the connection ID
157
  socket_ID external_socketID; ///< the external socketID
158
  int external_connectionID; ///< the internal connectionID
159
  pmtu pmtusize; ///< the pmtu size
160
  bool delay;
161
  int trials;
162
  char *ctrl_msg_buf;
163
  int status; ///< the status of the connection. status has the following encoding: 0: INVITE send, 1: CONNECT send, 2: connection established
164
  time_t starttime; ///< the time when the first connection attempt was made
165
  uint32_t seqnr; ///< sequence number for connections that have been tried
166
  //struct event *timeout;
167
  struct timeval timeout_value; ///< the timeout value for the connection estahlishment
168
  bool internal_connect; ///< set to true if a connection to the internall address could be established
169
  struct receive_connection_cb_list *connection_head;
170
  struct receive_connection_cb_list *connection_last;
171
  send_params defaultSendParams;
172
  uint32_t keepalive_seq; 
173
} connect_data;
174

    
175
#define ML_CON_MSG 127
176

    
177
/**
178
 * A struct with the messaging layer header for connection handling messages
179
 */
180
#pragma pack(push)  /* push current alignment to stack */
181
#pragma pack(1)     /* set alignment to 1 byte boundary */
182
struct conn_msg {
183
        uint32_t comand_type; ///< see con_msg_types
184
        int32_t pmtu_size;        /// the pmtu size 
185
        socket_ID sock_id;        /// the socketId of the sender
186
} __attribute__((packed));
187

    
188
struct msg_header {
189
        uint8_t msg_type; ///< set to ML_CON_MSG
190
        uint8_t len_mon_data_hdr;
191
        uint8_t len_mon_packet_hdr;
192
        uint32_t offset;
193
        uint32_t msg_length;
194
        int32_t local_con_id;        ///> the local connection id
195
        int32_t remote_con_id;        ///> the remote connection id
196
        int32_t msg_seq_num;
197
};
198
#define MSG_HEADER_SIZE 23
199
#pragma pack(pop)   /* restore original alignment from stack */
200

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

    
476

    
477
#endif