Statistics
| Branch: | Revision:

ml / util / udpSocket.h @ 08a4230a

History | View | Annotate | Download (5.99 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 <string.h>
53
#include <netinet/in.h>
54

    
55
/**
56
 * The maximum buffer size for a send or received packet.  
57
 * The value is set to the maximal pmtu size.  
58
 */
59
#define MAXBUF 2000
60

    
61
/// @{
62
/**
63
  * socket error variable: No error occurred  
64
  */
65
#define SO_EE_ORIGIN_NONE       0
66

    
67
/**
68
  * Socket error variable: local error
69
  */
70
#define SO_EE_ORIGIN_LOCAL      1
71

    
72
/**
73
  * Socket error variable: icmp message received  
74
  */
75
#define SO_EE_ORIGIN_ICMP       2
76

    
77
/**
78
  * Socket error variable: icmp version 6 message received  
79
  */
80
#define SO_EE_ORIGIN_ICMP6      3
81
/// @}
82

    
83
typedef enum {OK = 0, MSGLEN, FAILURE, THROTTLE} error_codes;
84

    
85
/** 
86
 * A callback functions for received pmtu errors (icmp packets type 3 code 4) 
87
 * @param buf TODO
88
 * @param bufsize TODO
89
 */
90
typedef void(*icmp_error_cb)(char *buf,int bufsize);
91

    
92
/** 
93
 * Create a messaging layer socket 
94
 * @param port The port of the socket
95
 * @param ipaddr The ip address of the socket. If left NULL the socket is bound to all local interfaces (INADDR_ANY).
96
 * @return The udpSocket file descriptor that the OS assigned. <0 on error
97
 */
98
int createSocket(const int port,const char *ipaddr);
99

    
100
/** 
101
 * A function to get the standard TTL from the operating system. 
102
 * @param udpSocket The file descriptor of the udpSocket. 
103
 * @param *ttl A pointer to an int that is used to store the ttl information. 
104
 * @return 1 if the operation is successful and 0 if a mistake occured. 
105
 */
106
int getTTL(const int udpSocket,uint8_t *ttl);
107

    
108
/** 
109
 * Sends a udp packet.
110
 * @param udpSocket The udpSocket file descriptor.
111
 * @param *buffer A pointer to the send buffer. 
112
 * @param bufferSize The size of the send buffer. 
113
 * @param *socketaddr The address of the remote socket
114
 */
115
int sendPacket(const int udpSocket, struct iovec *iov, int len, struct sockaddr_in *socketaddr);
116

    
117
/** 
118
  * Decide if a packet should be throttled
119
  * The implementation follows a leaky bucket algorithm: 
120
  * if the packet would fill the bucket beyond its limit, it is to be discarded
121
  *
122
  * @param len The length of the packet to be sent
123
  * @return OK or THROTTLE 
124
*/
125
int outputRateControl(int len);
126

    
127
/**
128
  * Configure the parameters for output rate control.
129
  * These values may also be set while packets are being transmitted.
130
  * @param bucketsize The size of the bucket in kbytes
131
  * @param drainrate The amount of kbytes draining in a second. If drainrate is 0, then rateControl is completely disabled (all packets are passed).
132
*/
133
void setOutputRateParams(int bucketsize, int drainrate);
134

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

    
146
/** 
147
 * This function is used for Error Handling. If an icmp packet is received it processes the packet. 
148
 * @param udpSocket The udpSocket file descriptor
149
 * @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.
150
 * @param *buf A pointer to the send or receive buffer
151
 * @param *bufsize A pointer to an int that ha the size of buf
152
 * @param *addr The address of the remote socket
153
 * @param icmpcb_value A function pointer to a callback function from type icmp_error_cb
154
 * @param *ttl A pointer to an int that is used to store the ttl information
155
 * @return 0 if everything went well and -1 if an error occured
156
 */
157
int handleSocketError(const int udpSocket,const int iofunc,char *buf,int *bufsize,struct sockaddr_in *addr,icmp_error_cb icmpcb_value,int *ttl);
158

    
159
/** 
160
 * This function closes a socket  
161
 * @param udpSocket The file descriptor of the udp socket 
162
 * @return 0 if everything went well and -1 if an error occured 
163
 */
164
int closeSocket(const int udpSocket);
165

    
166
#endif