BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file DPReceive.c |
||
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 | #include <stddef.h> |
||
31 | #include <limits.h> |
||
32 | #include <string.h> |
||
33 | |||
34 | #include <protocol/dataproto.h> |
||
35 | #include <misc/byteorder.h> |
||
36 | #include <misc/offset.h> |
||
37 | #include <base/BLog.h> |
||
38 | |||
39 | #include <client/DPReceive.h> |
||
40 | |||
41 | #include <generated/blog_channel_DPReceive.h> |
||
42 | |||
43 | static DPReceivePeer * find_peer (DPReceiveDevice *o, peerid_t id) |
||
44 | { |
||
45 | for (LinkedList1Node *node = LinkedList1_GetFirst(&o->peers_list); node; node = LinkedList1Node_Next(node)) { |
||
46 | DPReceivePeer *p = UPPER_OBJECT(node, DPReceivePeer, list_node); |
||
47 | if (p->peer_id == id) { |
||
48 | return p; |
||
49 | } |
||
50 | } |
||
51 | |||
52 | return NULL; |
||
53 | } |
||
54 | |||
55 | static void receiver_recv_handler_send (DPReceiveReceiver *o, uint8_t *packet, int packet_len) |
||
56 | { |
||
57 | DebugObject_Access(&o->d_obj); |
||
58 | DPReceivePeer *peer = o->peer; |
||
59 | DPReceiveDevice *device = peer->device; |
||
60 | ASSERT(packet_len >= 0) |
||
61 | ASSERT(packet_len <= device->packet_mtu) |
||
62 | |||
63 | uint8_t *data = packet; |
||
64 | int data_len = packet_len; |
||
65 | |||
66 | int local = 0; |
||
67 | DPReceivePeer *src_peer; |
||
68 | DPReceivePeer *relay_dest_peer = NULL; |
||
69 | |||
70 | // check header |
||
71 | if (data_len < sizeof(struct dataproto_header)) { |
||
72 | BLog(BLOG_WARNING, "no dataproto header"); |
||
73 | goto out; |
||
74 | } |
||
75 | struct dataproto_header header; |
||
76 | memcpy(&header, data, sizeof(header)); |
||
77 | data += sizeof(header); |
||
78 | data_len -= sizeof(header); |
||
79 | uint8_t flags = ltoh8(header.flags); |
||
80 | peerid_t from_id = ltoh16(header.from_id); |
||
81 | int num_ids = ltoh16(header.num_peer_ids); |
||
82 | |||
83 | // check destination ID |
||
84 | if (!(num_ids == 0 || num_ids == 1)) { |
||
85 | BLog(BLOG_WARNING, "wrong number of destinations"); |
||
86 | goto out; |
||
87 | } |
||
88 | peerid_t to_id = 0; // to remove warning |
||
89 | if (num_ids == 1) { |
||
90 | if (data_len < sizeof(struct dataproto_peer_id)) { |
||
91 | BLog(BLOG_WARNING, "missing destination"); |
||
92 | goto out; |
||
93 | } |
||
94 | struct dataproto_peer_id id; |
||
95 | memcpy(&id, data, sizeof(id)); |
||
96 | to_id = ltoh16(id.id); |
||
97 | data += sizeof(id); |
||
98 | data_len -= sizeof(id); |
||
99 | } |
||
100 | |||
101 | // check remaining data |
||
102 | if (data_len > device->device_mtu) { |
||
103 | BLog(BLOG_WARNING, "frame too large"); |
||
104 | goto out; |
||
105 | } |
||
106 | |||
107 | // inform sink of received packet |
||
108 | if (peer->dp_sink) { |
||
109 | DataProtoSink_Received(peer->dp_sink, !!(flags & DATAPROTO_FLAGS_RECEIVING_KEEPALIVES)); |
||
110 | } |
||
111 | |||
112 | if (num_ids == 1) { |
||
113 | // find source peer |
||
114 | if (!(src_peer = find_peer(device, from_id))) { |
||
115 | BLog(BLOG_INFO, "source peer %d not known", (int)from_id); |
||
116 | goto out; |
||
117 | } |
||
118 | |||
119 | // is frame for device or another peer? |
||
120 | if (device->have_peer_id && to_id == device->peer_id) { |
||
121 | // let the frame decider analyze the frame |
||
122 | FrameDeciderPeer_Analyze(src_peer->decider_peer, data, data_len); |
||
123 | |||
124 | // pass frame to device |
||
125 | local = 1; |
||
126 | } else { |
||
127 | // check if relaying is allowed |
||
128 | if (!peer->is_relay_client) { |
||
129 | BLog(BLOG_WARNING, "relaying not allowed"); |
||
130 | goto out; |
||
131 | } |
||
132 | |||
133 | // provided source ID must be the peer sending the frame |
||
134 | if (src_peer != peer) { |
||
135 | BLog(BLOG_WARNING, "relay source must be the sending peer"); |
||
136 | goto out; |
||
137 | } |
||
138 | |||
139 | // find destination peer |
||
140 | DPReceivePeer *dest_peer = find_peer(device, to_id); |
||
141 | if (!dest_peer) { |
||
142 | BLog(BLOG_INFO, "relay destination peer not known"); |
||
143 | goto out; |
||
144 | } |
||
145 | |||
146 | // destination cannot be source |
||
147 | if (dest_peer == src_peer) { |
||
148 | BLog(BLOG_WARNING, "relay destination cannot be the source"); |
||
149 | goto out; |
||
150 | } |
||
151 | |||
152 | relay_dest_peer = dest_peer; |
||
153 | } |
||
154 | } |
||
155 | |||
156 | out: |
||
157 | // accept packet |
||
158 | PacketPassInterface_Done(&o->recv_if); |
||
159 | |||
160 | // pass packet to device |
||
161 | if (local) { |
||
162 | o->device->output_func(o->device->output_func_user, data, data_len); |
||
163 | } |
||
164 | |||
165 | // relay frame |
||
166 | if (relay_dest_peer) { |
||
167 | DPRelayRouter_SubmitFrame(&device->relay_router, &src_peer->relay_source, &relay_dest_peer->relay_sink, data, data_len, device->relay_flow_buffer_size, device->relay_flow_inactivity_time); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | int DPReceiveDevice_Init (DPReceiveDevice *o, int device_mtu, DPReceiveDevice_output_func output_func, void *output_func_user, BReactor *reactor, int relay_flow_buffer_size, int relay_flow_inactivity_time) |
||
172 | { |
||
173 | ASSERT(device_mtu >= 0) |
||
174 | ASSERT(device_mtu <= INT_MAX - DATAPROTO_MAX_OVERHEAD) |
||
175 | ASSERT(output_func) |
||
176 | ASSERT(relay_flow_buffer_size > 0) |
||
177 | |||
178 | // init arguments |
||
179 | o->device_mtu = device_mtu; |
||
180 | o->output_func = output_func; |
||
181 | o->output_func_user = output_func_user; |
||
182 | o->reactor = reactor; |
||
183 | o->relay_flow_buffer_size = relay_flow_buffer_size; |
||
184 | o->relay_flow_inactivity_time = relay_flow_inactivity_time; |
||
185 | |||
186 | // remember packet MTU |
||
187 | o->packet_mtu = DATAPROTO_MAX_OVERHEAD + o->device_mtu; |
||
188 | |||
189 | // init relay router |
||
190 | if (!DPRelayRouter_Init(&o->relay_router, o->device_mtu, o->reactor)) { |
||
191 | BLog(BLOG_ERROR, "DPRelayRouter_Init failed"); |
||
192 | goto fail0; |
||
193 | } |
||
194 | |||
195 | // have no peer ID |
||
196 | o->have_peer_id = 0; |
||
197 | |||
198 | // init peers list |
||
199 | LinkedList1_Init(&o->peers_list); |
||
200 | |||
201 | DebugObject_Init(&o->d_obj); |
||
202 | return 1; |
||
203 | |||
204 | fail0: |
||
205 | return 0; |
||
206 | } |
||
207 | |||
208 | void DPReceiveDevice_Free (DPReceiveDevice *o) |
||
209 | { |
||
210 | DebugObject_Free(&o->d_obj); |
||
211 | ASSERT(LinkedList1_IsEmpty(&o->peers_list)) |
||
212 | |||
213 | // free relay router |
||
214 | DPRelayRouter_Free(&o->relay_router); |
||
215 | } |
||
216 | |||
217 | void DPReceiveDevice_SetPeerID (DPReceiveDevice *o, peerid_t peer_id) |
||
218 | { |
||
219 | DebugObject_Access(&o->d_obj); |
||
220 | |||
221 | // remember peer ID |
||
222 | o->peer_id = peer_id; |
||
223 | o->have_peer_id = 1; |
||
224 | } |
||
225 | |||
226 | void DPReceivePeer_Init (DPReceivePeer *o, DPReceiveDevice *device, peerid_t peer_id, FrameDeciderPeer *decider_peer, int is_relay_client) |
||
227 | { |
||
228 | DebugObject_Access(&device->d_obj); |
||
229 | ASSERT(is_relay_client == 0 || is_relay_client == 1) |
||
230 | |||
231 | // init arguments |
||
232 | o->device = device; |
||
233 | o->peer_id = peer_id; |
||
234 | o->decider_peer = decider_peer; |
||
235 | o->is_relay_client = is_relay_client; |
||
236 | |||
237 | // init relay source |
||
238 | DPRelaySource_Init(&o->relay_source, &device->relay_router, o->peer_id, device->reactor); |
||
239 | |||
240 | // init relay sink |
||
241 | DPRelaySink_Init(&o->relay_sink, o->peer_id); |
||
242 | |||
243 | // have no sink |
||
244 | o->dp_sink = NULL; |
||
245 | |||
246 | // insert to peers list |
||
247 | LinkedList1_Append(&device->peers_list, &o->list_node); |
||
248 | |||
249 | DebugCounter_Init(&o->d_receivers_ctr); |
||
250 | DebugObject_Init(&o->d_obj); |
||
251 | } |
||
252 | |||
253 | void DPReceivePeer_Free (DPReceivePeer *o) |
||
254 | { |
||
255 | DebugObject_Free(&o->d_obj); |
||
256 | DebugCounter_Free(&o->d_receivers_ctr); |
||
257 | ASSERT(!o->dp_sink) |
||
258 | |||
259 | // remove from peers list |
||
260 | LinkedList1_Remove(&o->device->peers_list, &o->list_node); |
||
261 | |||
262 | // free relay sink |
||
263 | DPRelaySink_Free(&o->relay_sink); |
||
264 | |||
265 | // free relay source |
||
266 | DPRelaySource_Free(&o->relay_source); |
||
267 | } |
||
268 | |||
269 | void DPReceivePeer_AttachSink (DPReceivePeer *o, DataProtoSink *dp_sink) |
||
270 | { |
||
271 | DebugObject_Access(&o->d_obj); |
||
272 | ASSERT(!o->dp_sink) |
||
273 | ASSERT(dp_sink) |
||
274 | |||
275 | // attach relay sink |
||
276 | DPRelaySink_Attach(&o->relay_sink, dp_sink); |
||
277 | |||
278 | o->dp_sink = dp_sink; |
||
279 | } |
||
280 | |||
281 | void DPReceivePeer_DetachSink (DPReceivePeer *o) |
||
282 | { |
||
283 | DebugObject_Access(&o->d_obj); |
||
284 | ASSERT(o->dp_sink) |
||
285 | |||
286 | // detach relay sink |
||
287 | DPRelaySink_Detach(&o->relay_sink); |
||
288 | |||
289 | o->dp_sink = NULL; |
||
290 | } |
||
291 | |||
292 | void DPReceiveReceiver_Init (DPReceiveReceiver *o, DPReceivePeer *peer) |
||
293 | { |
||
294 | DebugObject_Access(&peer->d_obj); |
||
295 | DPReceiveDevice *device = peer->device; |
||
296 | |||
297 | // init arguments |
||
298 | o->peer = peer; |
||
299 | |||
300 | // remember device |
||
301 | o->device = device; |
||
302 | |||
303 | // init receive interface |
||
304 | PacketPassInterface_Init(&o->recv_if, device->packet_mtu, (PacketPassInterface_handler_send)receiver_recv_handler_send, o, BReactor_PendingGroup(device->reactor)); |
||
305 | |||
306 | DebugCounter_Increment(&peer->d_receivers_ctr); |
||
307 | DebugObject_Init(&o->d_obj); |
||
308 | } |
||
309 | |||
310 | void DPReceiveReceiver_Free (DPReceiveReceiver *o) |
||
311 | { |
||
312 | DebugObject_Free(&o->d_obj); |
||
313 | DebugCounter_Decrement(&o->peer->d_receivers_ctr); |
||
314 | |||
315 | // free receive interface |
||
316 | PacketPassInterface_Free(&o->recv_if); |
||
317 | } |
||
318 | |||
319 | PacketPassInterface * DPReceiveReceiver_GetInput (DPReceiveReceiver *o) |
||
320 | { |
||
321 | DebugObject_Access(&o->d_obj); |
||
322 | |||
323 | return &o->recv_if; |
||
324 | } |