BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file DatagramPeerIO.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 * @section DESCRIPTION
30 *
31 * Object for comminicating with a peer using a datagram socket.
32 */
33  
34 #ifndef BADVPN_CLIENT_DATAGRAMPEERIO_H
35 #define BADVPN_CLIENT_DATAGRAMPEERIO_H
36  
37 #include <stdint.h>
38  
39 #include <misc/debug.h>
40 #include <protocol/spproto.h>
41 #include <protocol/fragmentproto.h>
42 #include <base/DebugObject.h>
43 #include <base/BLog.h>
44 #include <system/BReactor.h>
45 #include <system/BAddr.h>
46 #include <system/BDatagram.h>
47 #include <system/BTime.h>
48 #include <flow/PacketPassInterface.h>
49 #include <flow/PacketPassConnector.h>
50 #include <flow/SinglePacketBuffer.h>
51 #include <flow/PacketRecvConnector.h>
52 #include <flow/PacketPassNotifier.h>
53 #include <client/FragmentProtoDisassembler.h>
54 #include <client/FragmentProtoAssembler.h>
55 #include <client/SPProtoEncoder.h>
56 #include <client/SPProtoDecoder.h>
57  
58 /**
59 * Callback function invoked when an error occurs with the peer connection.
60 * The object has entered default state.
61 * May be called from within a sending Send call.
62 *
63 * @param user as in {@link DatagramPeerIO_SetHandlers}
64 */
65 typedef void (*DatagramPeerIO_handler_error) (void *user);
66  
67 /**
68 * Handler function invoked when the number of used OTPs has reached
69 * the specified warning number in {@link DatagramPeerIO_SetOTPWarningHandler}.
70 * May be called from within a sending Send call.
71 *
72 * @param user as in {@link DatagramPeerIO_SetHandlers}
73 */
74 typedef void (*DatagramPeerIO_handler_otp_warning) (void *user);
75  
76 /**
77 * Handler called when OTP generation for a new receive seed is finished.
78 *
79 * @param user as in {@link DatagramPeerIO_SetHandlers}
80 */
81 typedef void (*DatagramPeerIO_handler_otp_ready) (void *user);
82  
83 /**
84 * Object for comminicating with a peer using a datagram socket.
85 *
86 * The user provides data for sending to the peer through {@link PacketPassInterface}.
87 * Received data is provided to the user through {@link PacketPassInterface}.
88 *
89 * The object has a logical state called a mode, which is one of the following:
90 * - default - nothing is send or received
91 * - connecting - an address was provided by the user for sending datagrams to.
92 * Datagrams are being sent to that address through a socket,
93 * and datagrams are being received on the same socket.
94 * - binding - an address was provided by the user to bind a socket to.
95 * Datagrams are being received on the socket. Datagrams are not being
96 * sent initially. When a datagram is received, its source address is
97 * used as a destination address for sending datagrams.
98 */
99 typedef struct {
100 DebugObject d_obj;
101 BReactor *reactor;
102 int payload_mtu;
103 struct spproto_security_params sp_params;
104 void *user;
105 BLog_logfunc logfunc;
106 DatagramPeerIO_handler_error handler_error;
107 int spproto_payload_mtu;
108 int effective_socket_mtu;
109  
110 // sending base
111 FragmentProtoDisassembler send_disassembler;
112 SPProtoEncoder send_encoder;
113 SinglePacketBuffer send_buffer;
114 PacketPassConnector send_connector;
115  
116 // receiving
117 PacketRecvConnector recv_connector;
118 SinglePacketBuffer recv_buffer;
119 SPProtoDecoder recv_decoder;
120 PacketPassNotifier recv_notifier;
121 FragmentProtoAssembler recv_assembler;
122  
123 // mode
124 int mode;
125  
126 // datagram object
127 BDatagram dgram;
128 } DatagramPeerIO;
129  
130 /**
131 * Initializes the object.
132 * The interface is initialized in default mode.
133 * {@link BLog_Init} must have been done.
134 * {@link BNetwork_GlobalInit} must have been done.
135 * {@link BSecurity_GlobalInitThreadSafe} must have been done if
136 * {@link BThreadWorkDispatcher_UsingThreads}(twd) = 1.
137 *
138 * @param o the object
139 * @param reactor {@link BReactor} we live in
140 * @param payload_mtu maximum payload size. Must be >=0.
141 * @param socket_mtu maximum datagram size for the socket. Must be >=0. Must be large enough so it is possible to
142 * send a FragmentProto chunk with one byte of data over SPProto, i.e. the following has to hold:
143 * spproto_payload_mtu_for_carrier_mtu(sp_params, socket_mtu) > sizeof(struct fragmentproto_chunk_header)
144 * @param sp_params SPProto security parameters
145 * @param latency latency parameter to {@link FragmentProtoDisassembler_Init}.
146 * @param num_frames num_frames parameter to {@link FragmentProtoAssembler_Init}. Must be >0.
147 * @param recv_userif interface to pass received packets to the user. Its MTU must be >=payload_mtu.
148 * @param otp_warning_count If using OTPs, after how many encoded packets to call the handler.
149 * In this case, must be >0 and <=sp_params.otp_num.
150 * @param twd thread work dispatcher
151 * @param user value to pass to handlers
152 * @param logfunc function which prepends the log prefix using {@link BLog_Append}
153 * @param handler_error error handler
154 * @param handler_otp_warning OTP warning handler
155 * @param handler_otp_ready handler called when OTP generation for a new receive seed is finished
156 * @return 1 on success, 0 on failure
157 */
158 int DatagramPeerIO_Init (
159 DatagramPeerIO *o,
160 BReactor *reactor,
161 int payload_mtu,
162 int socket_mtu,
163 struct spproto_security_params sp_params,
164 btime_t latency,
165 int num_frames,
166 PacketPassInterface *recv_userif,
167 int otp_warning_count,
168 BThreadWorkDispatcher *twd,
169 void *user,
170 BLog_logfunc logfunc,
171 DatagramPeerIO_handler_error handler_error,
172 DatagramPeerIO_handler_otp_warning handler_otp_warning,
173 DatagramPeerIO_handler_otp_ready handler_otp_ready
174 ) WARN_UNUSED;
175  
176 /**
177 * Frees the object.
178 *
179 * @param o the object
180 */
181 void DatagramPeerIO_Free (DatagramPeerIO *o);
182  
183 /**
184 * Returns an interface the user should use to send packets.
185 * The OTP warning handler may be called from within Send calls
186 * to the interface.
187 *
188 * @param o the object
189 * @return sending interface
190 */
191 PacketPassInterface * DatagramPeerIO_GetSendInput (DatagramPeerIO *o);
192  
193 /**
194 * Attempts to establish connection to the peer which has bound to an address.
195 * On success, the interface enters connecting mode.
196 * On failure, the interface enters default mode.
197 *
198 * @param o the object
199 * @param addr address to send packets to
200 * @return 1 on success, 0 on failure
201 */
202 int DatagramPeerIO_Connect (DatagramPeerIO *o, BAddr addr) WARN_UNUSED;
203  
204 /**
205 * Attempts to establish connection to the peer by binding to an address.
206 * On success, the interface enters connecting mode.
207 * On failure, the interface enters default mode.
208 *
209 * @param o the object
210 * @param addr address to bind to. Must be supported according to
211 * {@link BDatagram_AddressFamilySupported}.
212 * @return 1 on success, 0 on failure
213 */
214 int DatagramPeerIO_Bind (DatagramPeerIO *o, BAddr addr) WARN_UNUSED;
215  
216 /**
217 * Sets the encryption key to use for sending and receiving.
218 * Encryption must be enabled.
219 *
220 * @param o the object
221 * @param encryption_key key to use
222 */
223 void DatagramPeerIO_SetEncryptionKey (DatagramPeerIO *o, uint8_t *encryption_key);
224  
225 /**
226 * Removed the encryption key to use for sending and receiving.
227 * Encryption must be enabled.
228 *
229 * @param o the object
230 */
231 void DatagramPeerIO_RemoveEncryptionKey (DatagramPeerIO *o);
232  
233 /**
234 * Sets the OTP seed for sending.
235 * OTPs must be enabled.
236 *
237 * @param o the object
238 * @param seed_id seed identifier
239 * @param key OTP encryption key
240 * @param iv OTP initialization vector
241 */
242 void DatagramPeerIO_SetOTPSendSeed (DatagramPeerIO *o, uint16_t seed_id, uint8_t *key, uint8_t *iv);
243  
244 /**
245 * Removes the OTP seed for sending of one is configured.
246 * OTPs must be enabled.
247 *
248 * @param o the object
249 */
250 void DatagramPeerIO_RemoveOTPSendSeed (DatagramPeerIO *o);
251  
252 /**
253 * Adds an OTP seed for reciving.
254 * OTPs must be enabled.
255 *
256 * @param o the object
257 * @param seed_id seed identifier
258 * @param key OTP encryption key
259 * @param iv OTP initialization vector
260 */
261 void DatagramPeerIO_AddOTPRecvSeed (DatagramPeerIO *o, uint16_t seed_id, uint8_t *key, uint8_t *iv);
262  
263 /**
264 * Removes all OTP seeds for reciving.
265 * OTPs must be enabled.
266 *
267 * @param o the object
268 */
269 void DatagramPeerIO_RemoveOTPRecvSeeds (DatagramPeerIO *o);
270  
271 #endif