]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lwip.git/blob - src/api/tcpip.c
* Fix all uses of pbuf_header to check the return value. In some
[pes-rpp/rpp-lwip.git] / src / api / tcpip.c
1 /*
2  * Copyright (c) 2001-2004 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 <adam@sics.se>
30  *
31  */
32
33 #include "lwip/opt.h"
34
35 #include "lwip/sys.h"
36
37 #include "lwip/memp.h"
38 #include "lwip/pbuf.h"
39
40 #include "netif/etharp.h"
41
42 #include "lwip/ip.h"
43 #include "lwip/ip_frag.h"
44 #include "lwip/udp.h"
45 #include "lwip/tcp.h"
46
47 #include "lwip/tcpip.h"
48 #include "lwip/igmp.h"
49
50 static void (* tcpip_init_done)(void *arg) = NULL;
51 static void *tcpip_init_done_arg           = NULL;
52 static sys_mbox_t mbox                     = SYS_MBOX_NULL;
53
54 #if LWIP_TCP
55 static int tcpip_tcp_timer_active = 0;
56
57 static void
58 tcpip_tcp_timer(void *arg)
59 {
60   (void)arg;
61
62   /* call TCP timer handler */
63   tcp_tmr();
64   /* timer still needed? */
65   if (tcp_active_pcbs || tcp_tw_pcbs) {
66     /* restart timer */
67     sys_timeout( TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
68   } else {
69     /* disable timer */
70     tcpip_tcp_timer_active = 0;
71   }
72 }
73
74 #if !NO_SYS
75 void
76 tcp_timer_needed(void)
77 {
78   /* timer is off but needed again? */
79   if (!tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) {
80     /* enable and start timer */
81     tcpip_tcp_timer_active = 1;
82     sys_timeout( TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
83   }
84 }
85 #endif /* !NO_SYS */
86 #endif /* LWIP_TCP */
87
88 #if IP_REASSEMBLY
89 static void
90 ip_timer(void *data)
91 {
92   LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: ip_reass_tmr()\n"));
93   ip_reass_tmr();
94   sys_timeout( IP_TMR_INTERVAL, ip_timer, NULL);
95 }
96 #endif
97
98 static void
99 arp_timer(void *arg)
100 {
101   LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: etharp_tmr()\n"));
102   etharp_tmr();
103   sys_timeout( ARP_TMR_INTERVAL, arp_timer, NULL);
104 }
105
106 #if LWIP_DHCP
107 static void
108 dhcp_timer_coarse(void *arg)
109 {
110   LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: dhcp_coarse_tmr()\n"));
111   dhcp_coarse_tmr();
112   sys_timeout(DHCP_COARSE_TIMER_SECS*1000, dhcp_timer_coarse, NULL);
113 }
114
115 static void
116 dhcp_timer_fine(void *arg)
117 {
118   LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: dhcp_fine_tmr()\n"));
119   dhcp_fine_tmr();
120   sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
121 }
122 #endif
123
124 #if ETHARP_TCPIP_ETHINPUT
125 static void
126 ethernet_input(struct pbuf *p, struct netif *netif)
127 {
128   struct eth_hdr* ethhdr;
129
130   /* points to packet payload, which starts with an Ethernet header */
131   ethhdr = p->payload;
132   
133   switch (htons(ethhdr->type)) {
134     /* IP packet? */
135     case ETHTYPE_IP:
136       #if ETHARP_TRUST_IP_MAC
137       /* update ARP table */
138       etharp_ip_input( netif, p);
139       #endif
140       /* skip Ethernet header */
141       if(pbuf_header(p, (s16_t)(-sizeof(struct eth_hdr)))) {
142         LWIP_ASSERT("Can't move over header in packet", 0);
143         pbuf_free(p);
144         p = NULL;
145       }
146       else
147         /* pass to IP layer */
148         ip_input(p, netif);
149       break;
150       
151     case ETHTYPE_ARP:
152       /* pass p to ARP module  */
153       etharp_arp_input(netif, (struct eth_addr*)(netif->hwaddr), p);
154       break;
155
156     default:
157       pbuf_free(p);
158       p = NULL;
159       break;
160   }
161 }
162 #endif /* ETHARP_TCPIP_ETHINPUT */
163
164 static void
165 tcpip_thread(void *arg)
166 {
167   struct tcpip_msg *msg;
168
169 #if IP_REASSEMBLY
170   sys_timeout( IP_TMR_INTERVAL, ip_timer, NULL);
171 #endif
172   sys_timeout( ARP_TMR_INTERVAL, arp_timer, NULL);
173 #if LWIP_DHCP
174   sys_timeout(DHCP_COARSE_TIMER_SECS*1000, dhcp_timer_coarse, NULL);
175   sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
176 #endif
177 #if LWIP_IGMP
178   igmp_init();
179 #endif
180
181   if (tcpip_init_done != NULL) {
182     tcpip_init_done(tcpip_init_done_arg);
183   }
184
185   while (1) {                          /* MAIN Loop */
186     sys_mbox_fetch(mbox, (void *)&msg);
187     switch (msg->type) {
188     case TCPIP_MSG_API:
189       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API message %p\n", (void *)msg));
190       api_msg_input(msg->msg.apimsg);
191       break;
192
193 #if ETHARP_TCPIP_INPUT      
194     case TCPIP_MSG_INPUT:
195       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: IP packet %p\n", (void *)msg));
196       ip_input(msg->msg.inp.p, msg->msg.inp.netif);
197       break;
198 #endif /* ETHARP_TCPIP_INPUT */
199
200 #if ETHARP_TCPIP_ETHINPUT
201     case TCPIP_MSG_ETHINPUT:
202       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: Ethernet packet %p\n", (void *)msg));
203       ethernet_input(msg->msg.inp.p, msg->msg.inp.netif);
204       break;
205 #endif /* ETHARP_TCPIP_ETHINPUT */
206
207     case TCPIP_MSG_CALLBACK:
208       LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK %p\n", (void *)msg));
209       msg->msg.cb.f(msg->msg.cb.ctx);
210       break;
211     default:
212       break;
213     }
214     memp_free(MEMP_TCPIP_MSG, msg);
215   }
216 }
217
218 #if ETHARP_TCPIP_INPUT
219 err_t
220 tcpip_input(struct pbuf *p, struct netif *inp)
221 {
222   struct tcpip_msg *msg;
223
224   if (mbox != SYS_MBOX_NULL) {
225     msg = memp_malloc(MEMP_TCPIP_MSG);
226     if (msg == NULL) {
227       pbuf_free(p);
228       return ERR_MEM;
229     }
230
231     msg->type = TCPIP_MSG_INPUT;
232     msg->msg.inp.p = p;
233     msg->msg.inp.netif = inp;
234     sys_mbox_post(mbox, msg);
235     return ERR_OK;
236   }
237   return ERR_VAL;
238 }
239 #endif /* ETHARP_TCPIP_INPUT */
240
241 #if ETHARP_TCPIP_ETHINPUT
242 err_t
243 tcpip_ethinput(struct pbuf *p, struct netif *inp)
244 {
245   struct tcpip_msg *msg;
246
247   if (mbox != SYS_MBOX_NULL) {
248     msg = memp_malloc(MEMP_TCPIP_MSG);
249     if (msg == NULL) {
250       pbuf_free(p);    
251       return ERR_MEM;  
252     }
253     
254     msg->type = TCPIP_MSG_ETHINPUT;
255     msg->msg.inp.p = p;
256     msg->msg.inp.netif = inp;
257     sys_mbox_post(mbox, msg);
258     return ERR_OK;
259   }
260   return ERR_VAL;
261 }
262 #endif /* ETHARP_TCPIP_ETHINPUT */
263
264 err_t
265 tcpip_callback(void (*f)(void *ctx), void *ctx)
266 {
267   struct tcpip_msg *msg;
268
269   if (mbox != SYS_MBOX_NULL) {
270     msg = memp_malloc(MEMP_TCPIP_MSG);
271     if (msg == NULL) {
272       return ERR_MEM;
273     }
274
275     msg->type = TCPIP_MSG_CALLBACK;
276     msg->msg.cb.f = f;
277     msg->msg.cb.ctx = ctx;
278     sys_mbox_post(mbox, msg);
279     return ERR_OK;
280   }
281   return ERR_VAL;
282 }
283
284 err_t
285 tcpip_apimsg(struct api_msg *apimsg)
286 {
287   struct tcpip_msg *msg;
288   
289   if (mbox != SYS_MBOX_NULL) {
290     msg = memp_malloc(MEMP_TCPIP_MSG);
291     if (msg == NULL) {
292       return ERR_MEM;
293     }
294     msg->type = TCPIP_MSG_API;
295     msg->msg.apimsg = apimsg;
296     sys_mbox_post(mbox, msg);
297     return ERR_OK;
298   }
299   return ERR_VAL;
300 }
301
302 void
303 tcpip_init(void (* initfunc)(void *), void *arg)
304 {
305   ip_init();
306 #if LWIP_UDP
307   udp_init();
308 #endif
309 #if LWIP_TCP
310   tcp_init();
311 #endif
312
313   tcpip_init_done = initfunc;
314   tcpip_init_done_arg = arg;
315   mbox = sys_mbox_new();
316   sys_thread_new(tcpip_thread, NULL, TCPIP_THREAD_PRIO);
317 }
318