BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file BDatagram.h
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29  
30 #ifndef BADVPN_SYSTEM_BDATAGRAM
31 #define BADVPN_SYSTEM_BDATAGRAM
32  
33 #include <misc/debug.h>
34 #include <flow/PacketPassInterface.h>
35 #include <flow/PacketRecvInterface.h>
36 #include <system/BAddr.h>
37 #include <system/BReactor.h>
38 #include <system/BNetwork.h>
39  
40 struct BDatagram_s;
41  
42 /**
43 * Represents datagram communication. This is usually UDP, but may also be Linux packet sockets.
44 * Sending and receiving is performed via {@link PacketPassInterface} and {@link PacketRecvInterface},
45 * respectively.
46 */
47 typedef struct BDatagram_s BDatagram;
48  
49 #define BDATAGRAM_EVENT_ERROR 1
50  
51 /**
52 * Handler called when an error occurs with the datagram object.
53 * The datagram object is no longer usable and must be freed from withing the job closure of
54 * this handler. No further I/O, interface initialization, binding and send address setting
55 * must occur.
56 *
57 * @param user as in {@link BDatagram_Init}
58 * @param event always BDATAGRAM_EVENT_ERROR
59 */
60 typedef void (*BDatagram_handler) (void *user, int event);
61  
62 /**
63 * Checks if the given address family (from {@link BAddr.h}) is supported by {@link BDatagram}
64 * and related objects.
65 *
66 * @param family family to check
67 * @return 1 if supported, 0 if not
68 */
69 int BDatagram_AddressFamilySupported (int family);
70  
71 /**
72 * Initializes the object.
73 * {@link BNetwork_GlobalInit} must have been done.
74 *
75 * @param o the object
76 * @param family address family. Must be supported according to {@link BDatagram_AddressFamilySupported}.
77 * @param reactor reactor we live in
78 * @param user argument to handler
79 * @param handler handler called when an error occurs
80 * @return 1 on success, 0 on failure
81 */
82 int BDatagram_Init (BDatagram *o, int family, BReactor *reactor, void *user,
83 BDatagram_handler handler) WARN_UNUSED;
84  
85 /**
86 * Frees the object.
87 * The send and receive interfaces must not be initialized.
88 *
89 * @param o the object
90 */
91 void BDatagram_Free (BDatagram *o);
92  
93 /**
94 * Binds to the given local address.
95 * May initiate I/O.
96 *
97 * @param o the object
98 * @param addr address to bind to. Its family must be supported according to {@link BDatagram_AddressFamilySupported}.
99 * @return 1 on success, 0 on failure
100 */
101 int BDatagram_Bind (BDatagram *o, BAddr addr) WARN_UNUSED;
102  
103 /**
104 * Sets addresses for sending.
105 * May initiate I/O.
106 *
107 * @param o the object
108 * @param remote_addr destination address for sending datagrams. Its family must be supported according
109 * to {@link BDatagram_AddressFamilySupported}.
110 * @param local_addr local source IP address. May be an invalid address, otherwise its family must be
111 * supported according to {@link BDatagram_AddressFamilySupported}.
112 */
113 void BDatagram_SetSendAddrs (BDatagram *o, BAddr remote_addr, BIPAddr local_addr);
114  
115 /**
116 * Returns the remote and local address of the last datagram received.
117 * Fails if and only if no datagrams have been received yet.
118 *
119 * @param o the object
120 * @param remote_addr returns the remote source address of the datagram. May be an invalid address, theoretically.
121 * @param local_addr returns the local destination IP address. May be an invalid address.
122 * @return 1 on success, 0 on failure
123 */
124 int BDatagram_GetLastReceiveAddrs (BDatagram *o, BAddr *remote_addr, BIPAddr *local_addr);
125  
126 /**
127 * Determines the local address.
128 *
129 * This calls getsockname() to determine the local address and returns the result as
130 * BAddr. This function fails if the address cannot be determined or translated to
131 * BAddr (it never succeeds but returns a BADDR_TYPE_NONE address).
132 *
133 * @param o the object
134 * @param local_addr returns the local bound address.
135 * @return 1 on success, 0 on failure
136 */
137 int BDatagram_GetLocalAddr (BDatagram *o, BAddr *local_addr);
138  
139 /**
140 * Returns the local port.
141 *
142 * This is a convenience function implemented based on BDatagram_GetLocalAddr.
143 *
144 * @param o the object
145 * @param local_port returns the local bound port.
146 * @return 1 on success, 0 on failure
147 */
148 int BDatagram_GetLocalPort (BDatagram *o, uint16_t *local_port);
149  
150 #ifndef BADVPN_USE_WINAPI
151 /**
152 * Returns the underlying socket file descriptor of the datagram object.
153 * Available on Unix-like systems only.
154 *
155 * @param o the object
156 * @return file descriptor
157 */
158 int BDatagram_GetFd (BDatagram *o);
159 #endif
160  
161 /**
162 * Sets the SO_REUSEADDR option for the underlying socket.
163 *
164 * @param o the object
165 * @param reuse value of the option. Must be 0 or 1.
166 */
167 int BDatagram_SetReuseAddr (BDatagram *o, int reuse);
168  
169 /**
170 * Initializes the send interface.
171 * The send interface must not be initialized.
172 *
173 * @param o the object
174 * @param mtu maximum transmission unit. Must be >=0.
175 */
176 void BDatagram_SendAsync_Init (BDatagram *o, int mtu);
177  
178 /**
179 * Frees the send interface.
180 * The send interface must be initialized.
181 * If the send interface was busy when this is called, the datagram object is no longer usable and must be
182 * freed before any further I/O or interface initialization.
183 *
184 * @param o the object
185 */
186 void BDatagram_SendAsync_Free (BDatagram *o);
187  
188 /**
189 * Returns the send interface.
190 * The send interface must be initialized.
191 * The MTU of the interface will be as in {@link BDatagram_SendAsync_Init}.
192 *
193 * @param o the object
194 * @return send interface
195 */
196 PacketPassInterface * BDatagram_SendAsync_GetIf (BDatagram *o);
197  
198 /**
199 * Initializes the receive interface.
200 * The receive interface must not be initialized.
201 *
202 * @param o the object
203 * @param mtu maximum transmission unit. Must be >=0.
204 */
205 void BDatagram_RecvAsync_Init (BDatagram *o, int mtu);
206  
207 /**
208 * Frees the receive interface.
209 * The receive interface must be initialized.
210 * If the receive interface was busy when this is called, the datagram object is no longer usable and must be
211 * freed before any further I/O or interface initialization.
212 *
213 * @param o the object
214 */
215 void BDatagram_RecvAsync_Free (BDatagram *o);
216  
217 /**
218 * Returns the receive interface.
219 * The receive interface must be initialized.
220 * The MTU of the interface will be as in {@link BDatagram_RecvAsync_Init}.
221 *
222 * @param o the object
223 * @return receive interface
224 */
225 PacketRecvInterface * BDatagram_RecvAsync_GetIf (BDatagram *o);
226  
227 #ifdef BADVPN_USE_WINAPI
228 #include "BDatagram_win.h"
229 #else
230 #include "BDatagram_unix.h"
231 #endif
232  
233 #endif