Statistics
| Branch: | Revision:

ml / util / udpSocket.h @ 372c2c31

History | View | Annotate | Download (6.38 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 udpSocket.h 
37
 * @brief This file contains a udp socket abstraction. 
38
 *
39
 * The udpSocket is in the current implementation an abstraction for the linux operating system and developed with ubuntu version 8.04. Should it be neccassy to run the messaging layer under another OS the udpSocket would have to be replaced by an implementation for that operating system. 
40
 *
41
 * @author Kristian Beckers  <beckers@nw.neclab.eu> 
42
 * @author Sebastian Kiesel  <kiesel@nw.neclab.eu>
43
 * 
44
 * @date 7/28/2009
45
 */
46

    
47

    
48

    
49
#ifndef UDPSOCKET_H
50
#define UDPSOCKET_H
51

    
52
#include <stdlib.h>
53
#include <unistd.h>
54
#include <sys/types.h>
55
#include <event2/event.h>
56
#include <time.h>
57

    
58
#ifndef _WIN32
59
#include <arpa/inet.h>
60
#include <netdb.h>
61
#include <netinet/in.h>
62
#include <sys/socket.h>
63
#include <fcntl.h>
64
#else
65

    
66
#include <winsock2.h>
67

    
68
struct iovec {
69
  void *iov_base;
70
  size_t iov_len;
71
};
72

    
73
const char *inet_ntop(int af, const void *src,
74
       char *dst, size_t size);
75
int inet_pton(int af, const char * src, void * dst);
76
#endif
77

    
78
/**
79
 * The maximum buffer size for a send or received packet.  
80
 * The value is set to the maximal pmtu size.  
81
 */
82
#define MAXBUF 2000
83

    
84
/// @{
85
/**
86
  * socket error variable: No error occurred  
87
  */
88
#define SO_EE_ORIGIN_NONE       0
89

    
90
/**
91
  * Socket error variable: local error
92
  */
93
#define SO_EE_ORIGIN_LOCAL      1
94

    
95
/**
96
  * Socket error variable: icmp message received  
97
  */
98
#define SO_EE_ORIGIN_ICMP       2
99

    
100
/**
101
  * Socket error variable: icmp version 6 message received  
102
  */
103
#define SO_EE_ORIGIN_ICMP6      3
104
/// @}
105

    
106
typedef enum {OK = 0, MSGLEN, FAILURE, THROTTLE} error_codes;
107

    
108
/** 
109
 * A callback functions for received pmtu errors (icmp packets type 3 code 4) 
110
 * @param buf TODO
111
 * @param bufsize TODO
112
 */
113
typedef void(*icmp_error_cb)(char *buf,int bufsize);
114

    
115
/** 
116
 * Create a messaging layer socket 
117
 * @param port The port of the socket
118
 * @param ipaddr The ip address of the socket. If left NULL the socket is bound to all local interfaces (INADDR_ANY).
119
 * @return The udpSocket file descriptor that the OS assigned. <0 on error
120
 */
121
int createSocket(const int port,const char *ipaddr);
122

    
123
/** 
124
 * A function to get the standard TTL from the operating system. 
125
 * @param udpSocket The file descriptor of the udpSocket. 
126
 * @param *ttl A pointer to an int that is used to store the ttl information. 
127
 * @return 1 if the operation is successful and 0 if a mistake occured. 
128
 */
129
int getTTL(const int udpSocket,uint8_t *ttl);
130

    
131
/** 
132
 * Sends a udp packet.
133
 * @param udpSocket The udpSocket file descriptor.
134
 * @param *buffer A pointer to the send buffer. 
135
 * @param bufferSize The size of the send buffer. 
136
 * @param *socketaddr The address of the remote socket
137
 */
138
int sendPacketFinal(const int udpSocket, struct iovec *iov, int len, struct sockaddr_in *socketaddr);
139

    
140

    
141
/**
142
 * Receive a udp packet
143
 * @param udpSocket The udpSocket file descriptor.
144
 * @param *buffer A pointer to the receive buffer.
145
 * @param *recvSize A pointer to an int that is used to store the size of the recv buffer. 
146
 * @param *udpdst The socket address from the remote socket that send the packet.
147
 * @param icmpcb_value A function pointer to a callback function from type icmp_error_cb.   
148
 * @param *ttl A pointer to an int that is used to store the ttl information.
149
 */
150
void recvPacket(const int udpSocket,char *buffer,int *recvSize,struct sockaddr_in *udpdst,icmp_error_cb icmpcb_value,int *ttl);
151

    
152
/** 
153
 * This function is used for Error Handling. If an icmp packet is received it processes the packet. 
154
 * @param udpSocket The udpSocket file descriptor
155
 * @param iofunc An integer that states if the function was called from sendPacket or recvPaket. The value for sendPacket is 1 and for recvPacket 2.
156
 * @param *buf A pointer to the send or receive buffer
157
 * @param *bufsize A pointer to an int that ha the size of buf
158
 * @param *addr The address of the remote socket
159
 * @param icmpcb_value A function pointer to a callback function from type icmp_error_cb
160
 * @param *ttl A pointer to an int that is used to store the ttl information
161
 * @return 0 if everything went well and -1 if an error occured
162
 */
163
int handleSocketError(const int udpSocket,const int iofunc,char *buf,int *bufsize,struct sockaddr_in *addr,icmp_error_cb icmpcb_value,int *ttl);
164

    
165
/** 
166
 * This function closes a socket  
167
 * @param udpSocket The file descriptor of the udp socket 
168
 * @return 0 if everything went well and -1 if an error occured 
169
 */
170
int closeSocket(const int udpSocket);
171

    
172
/** 
173
  * Decide if a packet should be throttled
174
  * The implementation follows a leaky bucket algorithm: 
175
  * if the packet would fill the bucket beyond its limit, it is to be discarded
176
  *
177
  * @param len The length of the packet to be sent
178
  * @return OK or THROTTLE 
179
*/
180
int outputRateControl(int len);
181

    
182
/**
183
  * Configure the parameters for output rate control.
184
  * These values may also be set while packets are being transmitted.
185
  * @param bucketsize The size of the bucket in bytes
186
  * @param drainrate The darining rate in bits/s. If drainrate is 0, then rateControl is completely disabled (all packets are passed).
187
*/
188
void setOutputRateParams(int bucketsize, int drainrate);
189

    
190

    
191

    
192
#endif