BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file PacketRouter.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 <flow/PacketRouter.h>
31  
32 static void input_handler_done (PacketRouter *o, int data_len)
33 {
34 ASSERT(data_len >= 0)
35 ASSERT(data_len <= o->mtu - o->recv_offset)
36 ASSERT(!BPending_IsSet(&o->next_job))
37 DebugObject_Access(&o->d_obj);
38  
39 // set next job
40 BPending_Set(&o->next_job);
41  
42 // call handler
43 o->handler(o->user, RouteBufferSource_Pointer(&o->rbs), data_len);
44 return;
45 }
46  
47 static void next_job_handler (PacketRouter *o)
48 {
49 DebugObject_Access(&o->d_obj);
50  
51 // receive
52 PacketRecvInterface_Receiver_Recv(o->input, RouteBufferSource_Pointer(&o->rbs) + o->recv_offset);
53 }
54  
55 int PacketRouter_Init (PacketRouter *o, int mtu, int recv_offset, PacketRecvInterface *input, PacketRouter_handler handler, void *user, BPendingGroup *pg)
56 {
57 ASSERT(mtu >= 0)
58 ASSERT(recv_offset >= 0)
59 ASSERT(recv_offset <= mtu)
60 ASSERT(PacketRecvInterface_GetMTU(input) <= mtu - recv_offset)
61  
62 // init arguments
63 o->mtu = mtu;
64 o->recv_offset = recv_offset;
65 o->input = input;
66 o->handler = handler;
67 o->user = user;
68  
69 // init input
70 PacketRecvInterface_Receiver_Init(o->input, (PacketRecvInterface_handler_done)input_handler_done, o);
71  
72 // init RouteBufferSource
73 if (!RouteBufferSource_Init(&o->rbs, mtu)) {
74 goto fail0;
75 }
76  
77 // init next job
78 BPending_Init(&o->next_job, pg, (BPending_handler)next_job_handler, o);
79  
80 // receive
81 PacketRecvInterface_Receiver_Recv(o->input, RouteBufferSource_Pointer(&o->rbs) + o->recv_offset);
82  
83 DebugObject_Init(&o->d_obj);
84  
85 return 1;
86  
87 fail0:
88 return 0;
89 }
90  
91 void PacketRouter_Free (PacketRouter *o)
92 {
93 DebugObject_Free(&o->d_obj);
94  
95 // free next job
96 BPending_Free(&o->next_job);
97  
98 // free RouteBufferSource
99 RouteBufferSource_Free(&o->rbs);
100 }
101  
102 int PacketRouter_Route (PacketRouter *o, int len, RouteBuffer *output, uint8_t **next_buf, int copy_offset, int copy_len)
103 {
104 ASSERT(len >= 0)
105 ASSERT(len <= o->mtu)
106 ASSERT(RouteBuffer_GetMTU(output) == o->mtu)
107 ASSERT(copy_offset >= 0)
108 ASSERT(copy_offset <= o->mtu)
109 ASSERT(copy_len >= 0)
110 ASSERT(copy_len <= o->mtu - copy_offset)
111 ASSERT(BPending_IsSet(&o->next_job))
112 DebugObject_Access(&o->d_obj);
113  
114 if (!RouteBufferSource_Route(&o->rbs, len, output, copy_offset, copy_len)) {
115 return 0;
116 }
117  
118 if (next_buf) {
119 *next_buf = RouteBufferSource_Pointer(&o->rbs);
120 }
121  
122 return 1;
123 }
124  
125 void PacketRouter_AssertRoute (PacketRouter *o)
126 {
127 ASSERT(BPending_IsSet(&o->next_job))
128 DebugObject_Access(&o->d_obj);
129 }