BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file RouteBuffer.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 <stdlib.h> |
||
32 | #include <string.h> |
||
33 | |||
34 | #include <misc/offset.h> |
||
35 | |||
36 | #include <flow/RouteBuffer.h> |
||
37 | |||
38 | static struct RouteBuffer_packet * alloc_packet (int mtu) |
||
39 | { |
||
40 | if (mtu > SIZE_MAX - sizeof(struct RouteBuffer_packet)) { |
||
41 | return NULL; |
||
42 | } |
||
43 | |||
44 | // allocate memory |
||
45 | struct RouteBuffer_packet *p = (struct RouteBuffer_packet *)malloc(sizeof(*p) + mtu); |
||
46 | if (!p) { |
||
47 | return NULL; |
||
48 | } |
||
49 | |||
50 | return p; |
||
51 | } |
||
52 | |||
53 | static int alloc_free_packet (RouteBuffer *o) |
||
54 | { |
||
55 | struct RouteBuffer_packet *p = alloc_packet(o->mtu); |
||
56 | if (!p) { |
||
57 | return 0; |
||
58 | } |
||
59 | |||
60 | // add to free packets list |
||
61 | LinkedList1_Append(&o->packets_free, &p->node); |
||
62 | |||
63 | return 1; |
||
64 | } |
||
65 | |||
66 | static void free_free_packets (RouteBuffer *o) |
||
67 | { |
||
68 | while (!LinkedList1_IsEmpty(&o->packets_free)) { |
||
69 | // get packet |
||
70 | struct RouteBuffer_packet *p = UPPER_OBJECT(LinkedList1_GetLast(&o->packets_free), struct RouteBuffer_packet, node); |
||
71 | |||
72 | // remove from free packets list |
||
73 | LinkedList1_Remove(&o->packets_free, &p->node); |
||
74 | |||
75 | // free memory |
||
76 | free(p); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | static void release_used_packet (RouteBuffer *o) |
||
81 | { |
||
82 | ASSERT(!LinkedList1_IsEmpty(&o->packets_used)) |
||
83 | |||
84 | // get packet |
||
85 | struct RouteBuffer_packet *p = UPPER_OBJECT(LinkedList1_GetFirst(&o->packets_used), struct RouteBuffer_packet, node); |
||
86 | |||
87 | // remove from used packets list |
||
88 | LinkedList1_Remove(&o->packets_used, &p->node); |
||
89 | |||
90 | // add to free packets list |
||
91 | LinkedList1_Append(&o->packets_free, &p->node); |
||
92 | } |
||
93 | |||
94 | static void send_used_packet (RouteBuffer *o) |
||
95 | { |
||
96 | ASSERT(!LinkedList1_IsEmpty(&o->packets_used)) |
||
97 | |||
98 | // get packet |
||
99 | struct RouteBuffer_packet *p = UPPER_OBJECT(LinkedList1_GetFirst(&o->packets_used), struct RouteBuffer_packet, node); |
||
100 | |||
101 | // send |
||
102 | PacketPassInterface_Sender_Send(o->output, (uint8_t *)(p + 1), p->len); |
||
103 | } |
||
104 | |||
105 | static void output_handler_done (RouteBuffer *o) |
||
106 | { |
||
107 | ASSERT(!LinkedList1_IsEmpty(&o->packets_used)) |
||
108 | DebugObject_Access(&o->d_obj); |
||
109 | |||
110 | // release packet |
||
111 | release_used_packet(o); |
||
112 | |||
113 | // send next packet if there is one |
||
114 | if (!LinkedList1_IsEmpty(&o->packets_used)) { |
||
115 | send_used_packet(o); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | int RouteBuffer_Init (RouteBuffer *o, int mtu, PacketPassInterface *output, int buf_size) |
||
120 | { |
||
121 | ASSERT(mtu >= 0) |
||
122 | ASSERT(PacketPassInterface_GetMTU(output) >= mtu) |
||
123 | ASSERT(buf_size > 0) |
||
124 | |||
125 | // init arguments |
||
126 | o->mtu = mtu; |
||
127 | o->output = output; |
||
128 | |||
129 | // init output |
||
130 | PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o); |
||
131 | |||
132 | // init free packets list |
||
133 | LinkedList1_Init(&o->packets_free); |
||
134 | |||
135 | // init used packets list |
||
136 | LinkedList1_Init(&o->packets_used); |
||
137 | |||
138 | // allocate packets |
||
139 | for (int i = 0; i < buf_size; i++) { |
||
140 | if (!alloc_free_packet(o)) { |
||
141 | goto fail1; |
||
142 | } |
||
143 | } |
||
144 | |||
145 | DebugObject_Init(&o->d_obj); |
||
146 | |||
147 | return 1; |
||
148 | |||
149 | fail1: |
||
150 | free_free_packets(o); |
||
151 | return 0; |
||
152 | } |
||
153 | |||
154 | void RouteBuffer_Free (RouteBuffer *o) |
||
155 | { |
||
156 | DebugObject_Free(&o->d_obj); |
||
157 | |||
158 | // release packets so they can be freed |
||
159 | while (!LinkedList1_IsEmpty(&o->packets_used)) { |
||
160 | release_used_packet(o); |
||
161 | } |
||
162 | |||
163 | // free packets |
||
164 | free_free_packets(o); |
||
165 | } |
||
166 | |||
167 | int RouteBuffer_GetMTU (RouteBuffer *o) |
||
168 | { |
||
169 | DebugObject_Access(&o->d_obj); |
||
170 | |||
171 | return o->mtu; |
||
172 | } |
||
173 | |||
174 | int RouteBufferSource_Init (RouteBufferSource *o, int mtu) |
||
175 | { |
||
176 | ASSERT(mtu >= 0) |
||
177 | |||
178 | // init arguments |
||
179 | o->mtu = mtu; |
||
180 | |||
181 | // allocate current packet |
||
182 | if (!(o->current_packet = alloc_packet(o->mtu))) { |
||
183 | goto fail0; |
||
184 | } |
||
185 | |||
186 | DebugObject_Init(&o->d_obj); |
||
187 | |||
188 | return 1; |
||
189 | |||
190 | fail0: |
||
191 | return 0; |
||
192 | } |
||
193 | |||
194 | void RouteBufferSource_Free (RouteBufferSource *o) |
||
195 | { |
||
196 | DebugObject_Free(&o->d_obj); |
||
197 | |||
198 | // free current packet |
||
199 | free(o->current_packet); |
||
200 | } |
||
201 | |||
202 | uint8_t * RouteBufferSource_Pointer (RouteBufferSource *o) |
||
203 | { |
||
204 | DebugObject_Access(&o->d_obj); |
||
205 | |||
206 | return (uint8_t *)(o->current_packet + 1); |
||
207 | } |
||
208 | |||
209 | int RouteBufferSource_Route (RouteBufferSource *o, int len, RouteBuffer *b, int copy_offset, int copy_len) |
||
210 | { |
||
211 | ASSERT(len >= 0) |
||
212 | ASSERT(len <= o->mtu) |
||
213 | ASSERT(b->mtu == o->mtu) |
||
214 | ASSERT(copy_offset >= 0) |
||
215 | ASSERT(copy_offset <= o->mtu) |
||
216 | ASSERT(copy_len >= 0) |
||
217 | ASSERT(copy_len <= o->mtu - copy_offset) |
||
218 | DebugObject_Access(&b->d_obj); |
||
219 | DebugObject_Access(&o->d_obj); |
||
220 | |||
221 | // check if there's space in the buffer |
||
222 | if (LinkedList1_IsEmpty(&b->packets_free)) { |
||
223 | return 0; |
||
224 | } |
||
225 | |||
226 | int was_empty = LinkedList1_IsEmpty(&b->packets_used); |
||
227 | |||
228 | struct RouteBuffer_packet *p = o->current_packet; |
||
229 | |||
230 | // set packet length |
||
231 | p->len = len; |
||
232 | |||
233 | // append packet to used packets list |
||
234 | LinkedList1_Append(&b->packets_used, &p->node); |
||
235 | |||
236 | // get a free packet |
||
237 | struct RouteBuffer_packet *np = UPPER_OBJECT(LinkedList1_GetLast(&b->packets_free), struct RouteBuffer_packet, node); |
||
238 | |||
239 | // remove it from free packets list |
||
240 | LinkedList1_Remove(&b->packets_free, &np->node); |
||
241 | |||
242 | // make it the current packet |
||
243 | o->current_packet = np; |
||
244 | |||
245 | // copy packet |
||
246 | if (copy_len > 0) { |
||
247 | memcpy((uint8_t *)(np + 1) + copy_offset, (uint8_t *)(p + 1) + copy_offset, copy_len); |
||
248 | } |
||
249 | |||
250 | // start sending if required |
||
251 | if (was_empty) { |
||
252 | send_used_packet(b); |
||
253 | } |
||
254 | |||
255 | return 1; |
||
256 | } |