BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file DataProto.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 * Mudule for frame sending used in the VPN client program.
32 */
33  
34 #ifndef BADVPN_CLIENT_DATAPROTO_H
35 #define BADVPN_CLIENT_DATAPROTO_H
36  
37 #include <stdint.h>
38  
39 #include <misc/debugcounter.h>
40 #include <misc/debug.h>
41 #include <base/DebugObject.h>
42 #include <system/BReactor.h>
43 #include <flow/PacketPassFairQueue.h>
44 #include <flow/PacketPassNotifier.h>
45 #include <flow/PacketRecvBlocker.h>
46 #include <flow/SinglePacketBuffer.h>
47 #include <flow/PacketPassConnector.h>
48 #include <flow/PacketRouter.h>
49 #include <flowextra/PacketPassInactivityMonitor.h>
50 #include <client/DataProtoKeepaliveSource.h>
51  
52 typedef void (*DataProtoSink_handler) (void *user, int up);
53 typedef void (*DataProtoSource_handler) (void *user, const uint8_t *frame, int frame_len);
54 typedef void (*DataProtoFlow_handler_inactivity) (void *user);
55  
56 struct DataProtoFlow_buffer;
57  
58 /**
59 * Frame destination.
60 * Represents a peer as a destination for sending frames to.
61 */
62 typedef struct {
63 BReactor *reactor;
64 int frame_mtu;
65 PacketPassFairQueue queue;
66 PacketPassInactivityMonitor monitor;
67 PacketPassNotifier notifier;
68 DataProtoKeepaliveSource ka_source;
69 PacketRecvBlocker ka_blocker;
70 SinglePacketBuffer ka_buffer;
71 PacketPassFairQueueFlow ka_qflow;
72 BTimer receive_timer;
73 int up;
74 int up_report;
75 DataProtoSink_handler handler;
76 void *user;
77 BPending up_job;
78 struct DataProtoFlow_buffer *detaching_buffer;
79 DebugObject d_obj;
80 DebugCounter d_ctr;
81 } DataProtoSink;
82  
83 /**
84 * Receives frames from a {@link PacketRecvInterface} input and
85 * allows the user to route them to buffers in {@link DataProtoFlow}'s.
86 */
87 typedef struct {
88 DataProtoSource_handler handler;
89 void *user;
90 BReactor *reactor;
91 int frame_mtu;
92 PacketRouter router;
93 uint8_t *current_buf;
94 int current_recv_len;
95 DebugObject d_obj;
96 DebugCounter d_ctr;
97 } DataProtoSource;
98  
99 /**
100 * Contains a buffer for frames from a specific peer to a specific peer.
101 * Receives frames from a {@link DataProtoSource} as routed by the user.
102 * Can be attached to a {@link DataProtoSink} to send out frames.
103 */
104 typedef struct {
105 DataProtoSource *source;
106 peerid_t source_id;
107 peerid_t dest_id;
108 DataProtoSink *sink_desired;
109 struct DataProtoFlow_buffer *b;
110 DebugObject d_obj;
111 } DataProtoFlow;
112  
113 struct DataProtoFlow_buffer {
114 DataProtoFlow *flow;
115 int inactivity_time;
116 RouteBuffer rbuf;
117 PacketPassInactivityMonitor monitor;
118 PacketPassConnector connector;
119 DataProtoSink *sink;
120 PacketPassFairQueueFlow sink_qflow;
121 };
122  
123 /**
124 * Initializes the sink.
125 *
126 * @param o the object
127 * @param reactor reactor we live in
128 * @param output output interface. Must support cancel functionality. Its MTU must be
129 * >=DATAPROTO_MAX_OVERHEAD.
130 * @param keepalive_time keepalive time
131 * @param tolerance_time after how long of not having received anything from the peer
132 * to consider the link down
133 * @param handler up state handler
134 * @param user value to pass to handler
135 * @return 1 on success, 0 on failure
136 */
137 int DataProtoSink_Init (DataProtoSink *o, BReactor *reactor, PacketPassInterface *output, btime_t keepalive_time, btime_t tolerance_time, DataProtoSink_handler handler, void *user) WARN_UNUSED;
138  
139 /**
140 * Frees the sink.
141 * There must be no local sources attached.
142 *
143 * @param o the object
144 */
145 void DataProtoSink_Free (DataProtoSink *o);
146  
147 /**
148 * Notifies the sink that a packet was received from the peer.
149 * Must not be in freeing state.
150 *
151 * @param o the object
152 * @param peer_receiving whether the DATAPROTO_FLAGS_RECEIVING_KEEPALIVES flag was set in the packet.
153 * Must be 0 or 1.
154 */
155 void DataProtoSink_Received (DataProtoSink *o, int peer_receiving);
156  
157 /**
158 * Initiazes the source.
159 *
160 * @param o the object
161 * @param input frame input. Its input MTU must be <= INT_MAX - DATAPROTO_MAX_OVERHEAD.
162 * @param handler handler called when a frame arrives to allow the user to route it to
163 * appropriate {@link DataProtoFlow}'s.
164 * @param user value passed to handler
165 * @param reactor reactor we live in
166 * @return 1 on success, 0 on failure
167 */
168 int DataProtoSource_Init (DataProtoSource *o, PacketRecvInterface *input, DataProtoSource_handler handler, void *user, BReactor *reactor) WARN_UNUSED;
169  
170 /**
171 * Frees the source.
172 * There must be no {@link DataProtoFlow}'s using this source.
173 *
174 * @param o the object
175 */
176 void DataProtoSource_Free (DataProtoSource *o);
177  
178 /**
179 * Initializes the flow.
180 * The flow is initialized in not attached state.
181 *
182 * @param o the object
183 * @param source source to receive frames from
184 * @param source_id source peer ID to encode in the headers (i.e. our ID)
185 * @param dest_id destination peer ID to encode in the headers (i.e. ID if the peer this
186 * flow belongs to)
187 * @param num_packets number of packets the buffer should hold. Must be >0.
188 * @param inactivity_time milliseconds of output inactivity after which to call the
189 * inactivity handler; <0 to disable. Note that the flow is considered
190 * active as long as its buffer is non-empty, even if is not attached to
191 * a {@link DataProtoSink}.
192 * @param user value to pass to handler
193 * @param handler_inactivity inactivity handler, if inactivity_time >=0
194 * @return 1 on success, 0 on failure
195 */
196 int DataProtoFlow_Init (DataProtoFlow *o, DataProtoSource *source, peerid_t source_id, peerid_t dest_id, int num_packets, int inactivity_time, void *user,
197 DataProtoFlow_handler_inactivity handler_inactivity) WARN_UNUSED;
198  
199 /**
200 * Frees the flow.
201 * The flow must be in not attached state.
202 *
203 * @param o the object
204 */
205 void DataProtoFlow_Free (DataProtoFlow *o);
206  
207 /**
208 * Routes a frame from the flow's source to this flow.
209 * Must be called from within the job context of the {@link DataProtoSource_handler} handler.
210 * Must not be called after this has been called with more=0 for the current frame.
211 *
212 * @param o the object
213 * @param more whether the current frame may have to be routed to more
214 * flows. If 0, must not be called again until the handler is
215 * called for the next frame. Must be 0 or 1.
216 */
217 void DataProtoFlow_Route (DataProtoFlow *o, int more);
218  
219 /**
220 * Attaches the flow to a sink.
221 * The flow must be in not attached state.
222 *
223 * @param o the object
224 * @param sink sink to attach to. This flow's frame_mtu must be <=
225 * (output MTU of sink) - DATAPROTO_MAX_OVERHEAD.
226 */
227 void DataProtoFlow_Attach (DataProtoFlow *o, DataProtoSink *sink);
228  
229 /**
230 * Detaches the flow from a destination.
231 * The flow must be in attached state.
232 *
233 * @param o the object
234 */
235 void DataProtoFlow_Detach (DataProtoFlow *o);
236  
237 #endif