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