Debian Codegraph @

contrib/ports/unix/netif/delif.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25 * OF SUCH DAMAGE.
26 *
27 * This file is part of the lwIP TCP/IP stack.
28 *
29 * Author: Adam Dunkels <[email protected]>
30 *
31 */
32
33 #include "lwip/debug.h"
34
35 #include <stdlib.h>
36
37 #include "lwip/def.h"
38 #include "netif/delif.h"
39
40 #ifdef linux
41 #include "netif/tapif.h"
42 #else /* linux */
43 #include "netif/tunif.h"
44 #endif /* linux */
45
46 #include "lwip/sys.h"
47
48
49 #define DELIF_INPUT_DROPRATE 0.1
50 #define DELIF_OUTPUT_DROPRATE 0.1
51
52 #define DELIF_INPUT_DELAY 500 /* Miliseconds. */
53 #define DELIF_OUTPUT_DELAY 500 /* Miliseconds. */
54
55 #define DELIF_TIMEOUT 10
56
57 struct delif {
58 err_t (* input)(struct pbuf *p, struct netif *inp);
59 struct netif *netif;
60 };
61
62 struct delif_pbuf {
63 struct delif_pbuf *next;
64 struct pbuf *p;
65 ip_addr_t *ipaddr;
66 unsigned int time;
67 };
68
69 static struct delif_pbuf *input_list = NULL;
70 static struct delif_pbuf *output_list = NULL;
71 /*-----------------------------------------------------------------------------------*/
72 static void
73 delif_input_timeout(void *arg)
74 {
75 struct netif *netif;
76 struct delif *delif;
77 struct delif_pbuf *dp;
78 unsigned int timeout, now;
79
80 timeout = DELIF_TIMEOUT;
81
82 netif = arg;
83 delif = netif->state;
84
85
86 /* Check if there is anything on the input list. */
87 dp = input_list;
88 while (dp != NULL) {
89 now = sys_now();
90
91 if (dp->time <= now) {
92 delif->input(dp->p, netif);
93 if (dp->next != NULL) {
94 if (dp->next->time > now) {
95 timeout = dp->next->time - now;
96 } else {
97 timeout = 0;
98 }
99 LWIP_DEBUGF(DELIF_DEBUG, ("delif_output_timeout: timeout %u.\n", timeout));
100
101 }
102 input_list = dp->next;
103 free(dp);
104 dp = input_list;
105 } else {
106 dp = dp->next;
107 }
108 }
109
110 sys_timeout(timeout, delif_input_timeout, arg);
111 }
112 /*-----------------------------------------------------------------------------------*/
113 static void
114 delif_output_timeout(void *arg)
115 {
116 struct netif *netif;
117 struct delif *delif;
118 struct delif_pbuf *dp;
119 unsigned int timeout, now;
120
121 timeout = DELIF_TIMEOUT;
122
123 netif = arg;
124 delif = netif->state;
125
126 /* Check if there is anything on the output list. */
127 dp = output_list;
128 while (dp != NULL) {
129 now = sys_now();
130 if (dp->time <= now) {
131 LWIP_DEBUGF(DELIF_DEBUG, ("delif_output_timeout: now %u dp->time %u\n",
132 now, dp->time));
133 delif->netif->output(delif->netif, dp->p, dp->ipaddr);
134 if (dp->next != NULL) {
135 if (dp->next->time > now) {
136 timeout = dp->next->time - now;
137 } else {
138 timeout = 0;
139 }
140 LWIP_DEBUGF(DELIF_DEBUG, ("delif_output_timeout: timeout %u.\n", timeout));
141
142 }
143 pbuf_free(dp->p);
144
145 output_list = dp->next;
146 free(dp);
147 dp = output_list;
148 } else {
149 dp = dp->next;
150 }
151 }
152
153
154 sys_timeout(timeout, delif_output_timeout, arg);
155 }
156 /*-----------------------------------------------------------------------------------*/
157 static err_t
158 delif_output(struct netif *netif, struct pbuf *p, ip_addr_t *ipaddr)
159 {
160 struct delif_pbuf *dp, *np;
161 struct pbuf *q;
162 int i, j;
163 char *data;
164
165 LWIP_DEBUGF(DELIF_DEBUG, ("delif_output\n"));
166
167 #ifdef DELIF_OUTPUT_DROPRATE
168 if (((double)rand()/(double)RAND_MAX) < DELIF_OUTPUT_DROPRATE) {
169 LWIP_DEBUGF(DELIF_DEBUG, ("delif_output: Packet dropped\n"));
170 return 0;
171 }
172 #endif /* DELIF_OUTPUT_DROPRATE */
173
174
175 LWIP_DEBUGF(DELIF_DEBUG, ("delif_output\n"));
176
177
178 dp = malloc(sizeof(struct delif_pbuf));
179 data = malloc(p->tot_len);
180
181 i = 0;
182 for(q = p; q != NULL; q = q->next) {
183 for(j = 0; j < q->len; j++) {
184 data[i] = ((char *)q->payload)[j];
185 i++;
186 }
187 }
188
189
190 dp->p = pbuf_alloc(PBUF_LINK, 0, PBUF_ROM);
191 dp->p->payload = data;
192 dp->p->len = p->tot_len;
193 dp->p->tot_len = p->tot_len;
194 dp->ipaddr = ipaddr;
195 dp->time = sys_now() + DELIF_OUTPUT_DELAY;
196 dp->next = NULL;
197 if (output_list == NULL) {
198 output_list = dp;
199 } else {
200 for(np = output_list; np->next != NULL; np = np->next);
201 np->next = dp;
202 }
203
204
205 return ERR_OK;
206
207
208 }
209 /*-----------------------------------------------------------------------------------*/
210 static err_t
211 delif_input(struct pbuf *p, struct netif *inp)
212 {
213 struct delif_pbuf *dp, *np;
214
215 LWIP_DEBUGF(DELIF_DEBUG, ("delif_input\n"));
216 #ifdef DELIF_INPUT_DROPRATE
217 if (((double)rand()/(double)RAND_MAX) < DELIF_INPUT_DROPRATE) {
218 LWIP_DEBUGF(DELIF_DEBUG, ("delif_input: Packet dropped\n"));
219 pbuf_free(p);
220 return ERR_OK;
221 }
222 #endif /* DELIF_INPUT_DROPRATE */
223
224
225 dp = malloc(sizeof(struct delif_pbuf));
226 dp->p = p;
227 dp->time = sys_now() + DELIF_INPUT_DELAY;
228 dp->next = NULL;
229 if (input_list == NULL) {
230 input_list = dp;
231 } else {
232 for(np = input_list; np->next != NULL; np = np->next);
233 np->next = dp;
234 }
235 return ERR_OK;
236 }
237 /*-----------------------------------------------------------------------------------*/
238 err_t
239 delif_init(struct netif *netif)
240 {
241 struct delif *del;
242
243 del = malloc(sizeof(struct delif));
244 if (!del)
245 return ERR_MEM;
246 netif->state = del;
247 netif->name[0] = 'd';
248 netif->name[1] = 'e';
249 netif->output = delif_output;
250
251 del->netif = malloc(sizeof(struct netif));
252 if (!del->netif) {
253 free(del);
254 return ERR_MEM;
255 }
256 #ifdef linux
257 /* tapif_init(del->netif);*/
258 tunif_init(del->netif);
259 #else /* linux */
260 tunif_init(del->netif);
261 #endif /* linux */
262 del->input = netif->input;
263 del->netif->input = delif_input;
264 sys_timeout(DELIF_TIMEOUT, delif_input_timeout, netif);
265 sys_timeout(DELIF_TIMEOUT, delif_output_timeout, netif);
266 return ERR_OK;
267 }
268
269 /*-----------------------------------------------------------------------------------*/
270 static void
271 delif_thread(void *arg)
272 {
273 struct netif *netif = arg;
274 struct delif *del;
275 sys_sem_t sem;
276
277 del = netif->state;
278 #ifdef linux
279 tapif_init(del->netif);
280 #else /* linux */
281 tunif_init(del->netif);
282 #endif /* linux */
283
284 sys_timeout(DELIF_TIMEOUT, delif_input_timeout, netif);
285 sys_timeout(DELIF_TIMEOUT, delif_output_timeout, netif);
286
287 if(sys_sem_new(&sem, 0) != ERR_OK) {
288 LWIP_ASSERT("Failed to create semaphore", 0);
289 }
290 sys_sem_wait(&sem);
291
292 }
293 /*-----------------------------------------------------------------------------------*/
294 err_t
295 delif_init_thread(struct netif *netif)
296 {
297 struct delif *del;
298
299 LWIP_DEBUGF(DELIF_DEBUG, ("delif_init_thread\n"));
300
301 del = malloc(sizeof(struct delif));
302 if (!del)
303 return ERR_MEM;
304 netif->state = del;
305 netif->name[0] = 'd';
306 netif->name[1] = 'e';
307 netif->output = delif_output;
308
309 del->netif = malloc(sizeof(struct netif));
310 if (!del->netif) {
311 free(del);
312 return ERR_MEM;
313 }
314 del->netif->ip_addr = netif->ip_addr;
315 del->netif->gw = netif->gw;
316 del->netif->netmask = netif->netmask;
317 del->input = netif->input;
318 del->netif->input = delif_input;
319 sys_thread_new("delif_thread", delif_thread, netif, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
320 return ERR_OK;
321 }
322
323 /*-----------------------------------------------------------------------------------*/