2 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
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.
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
27 * This file is part of the lwIP TCP/IP stack.
29 * Author: Adam Dunkels <adam@sics.se>
37 #include "lwip/memp.h"
38 #include "lwip/pbuf.h"
40 #include "netif/etharp.h"
43 #include "lwip/ip_frag.h"
47 #include "lwip/tcpip.h"
49 static void (* tcpip_init_done)(void *arg) = NULL;
50 static void *tcpip_init_done_arg;
51 static sys_mbox_t mbox;
54 static int tcpip_tcp_timer_active = 0;
57 tcpip_tcp_timer(void *arg)
61 /* call TCP timer handler */
63 /* timer still needed? */
64 if (tcp_active_pcbs || tcp_tw_pcbs) {
66 sys_timeout( TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
69 tcpip_tcp_timer_active = 0;
75 tcp_timer_needed(void)
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);
91 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: ip_reass_tmr()\n"));
93 sys_timeout( IP_TMR_INTERVAL, ip_timer, NULL);
100 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip: etharp_tmr()\n"));
102 sys_timeout( ARP_TMR_INTERVAL, arp_timer, NULL);
105 #if ETHARP_TCPIP_ETHINPUT
107 ethernet_input(struct pbuf *p, struct netif *netif)
109 struct eth_hdr* ethhdr;
111 /* points to packet payload, which starts with an Ethernet header */
114 switch (htons(ethhdr->type)) {
117 #if ETHARP_TRUST_IP_MAC
118 /* update ARP table */
119 etharp_ip_input( netif, p);
121 /* skip Ethernet header */
122 pbuf_header(p, -sizeof(struct eth_hdr));
123 /* pass to IP layer */
128 /* pass p to ARP module */
129 etharp_arp_input(netif, (struct eth_addr*)(netif->hwaddr), p);
138 #endif /* ETHARP_TCPIP_ETHINPUT */
141 tcpip_thread(void *arg)
143 struct tcpip_msg *msg;
146 sys_timeout( IP_TMR_INTERVAL, ip_timer, NULL);
148 sys_timeout( ARP_TMR_INTERVAL, arp_timer, NULL);
150 if (tcpip_init_done != NULL) {
151 tcpip_init_done(tcpip_init_done_arg);
154 while (1) { /* MAIN Loop */
155 sys_mbox_fetch(mbox, (void *)&msg, 0);
158 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: API message %p\n", (void *)msg));
159 api_msg_input(msg->msg.apimsg);
162 #if ETHARP_TCPIP_INPUT
163 case TCPIP_MSG_INPUT:
164 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: IP packet %p\n", (void *)msg));
165 ip_input(msg->msg.inp.p, msg->msg.inp.netif);
167 #endif /* ETHARP_TCPIP_INPUT */
169 #if ETHARP_TCPIP_ETHINPUT
170 case TCPIP_MSG_ETHINPUT:
171 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: Ethernet packet %p\n", (void *)msg));
172 ethernet_input(msg->msg.inp.p, msg->msg.inp.netif);
174 #endif /* ETHARP_TCPIP_ETHINPUT */
176 case TCPIP_MSG_CALLBACK:
177 LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: CALLBACK %p\n", (void *)msg));
178 msg->msg.cb.f(msg->msg.cb.ctx);
183 memp_free(MEMP_TCPIP_MSG, msg);
187 #if ETHARP_TCPIP_INPUT
189 tcpip_input(struct pbuf *p, struct netif *inp)
191 struct tcpip_msg *msg;
193 msg = memp_malloc(MEMP_TCPIP_MSG);
199 msg->type = TCPIP_MSG_INPUT;
201 msg->msg.inp.netif = inp;
202 sys_mbox_post(mbox, msg);
205 #endif /* ETHARP_TCPIP_INPUT */
207 #if ETHARP_TCPIP_ETHINPUT
209 tcpip_ethinput(struct pbuf *p, struct netif *inp)
211 struct tcpip_msg *msg;
213 msg = memp_malloc(MEMP_TCPIP_MSG);
219 msg->type = TCPIP_MSG_ETHINPUT;
221 msg->msg.inp.netif = inp;
222 sys_mbox_post(mbox, msg);
225 #endif /* ETHARP_TCPIP_ETHINPUT */
228 tcpip_callback(void (*f)(void *ctx), void *ctx)
230 struct tcpip_msg *msg;
232 msg = memp_malloc(MEMP_TCPIP_MSG);
237 msg->type = TCPIP_MSG_CALLBACK;
239 msg->msg.cb.ctx = ctx;
240 sys_mbox_post(mbox, msg);
245 tcpip_apimsg(struct api_msg *apimsg)
247 struct tcpip_msg *msg;
248 msg = memp_malloc(MEMP_TCPIP_MSG);
252 msg->type = TCPIP_MSG_API;
253 msg->msg.apimsg = apimsg;
254 sys_mbox_post(mbox, msg);
258 tcpip_init(void (* initfunc)(void *), void *arg)
268 tcpip_init_done = initfunc;
269 tcpip_init_done_arg = arg;
270 mbox = sys_mbox_new();
271 sys_thread_new(tcpip_thread, NULL, TCPIP_THREAD_PRIO);