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>
34 #include "lwip/arch.h"
35 #include "lwip/api_msg.h"
36 #include "lwip/memp.h"
38 #include "lwip/tcpip.h"
42 recv_raw(void *arg, struct raw_pcb *pcb, struct pbuf *p,
51 if (conn->recvmbox != SYS_MBOX_NULL) {
52 if (!(buf = memp_malloc(MEMP_NETBUF))) {
59 buf->fromport = pcb->protocol;
61 conn->recv_avail += p->tot_len;
62 /* Register event with callback */
64 (*conn->callback)(conn, NETCONN_EVT_RCVPLUS, p->tot_len);
65 sys_mbox_post(conn->recvmbox, buf);
68 return 0; /* do not eat the packet */
73 recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p,
74 struct ip_addr *addr, u16_t port)
85 if (conn->recvmbox != SYS_MBOX_NULL) {
86 buf = memp_malloc(MEMP_NETBUF);
97 conn->recv_avail += p->tot_len;
98 /* Register event with callback */
100 (*conn->callback)(conn, NETCONN_EVT_RCVPLUS, p->tot_len);
101 sys_mbox_post(conn->recvmbox, buf);
104 #endif /* LWIP_UDP */
108 recv_tcp(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
110 struct netconn *conn;
120 if (conn->recvmbox != SYS_MBOX_NULL) {
125 conn->recv_avail += len;
129 /* Register event with callback */
131 (*conn->callback)(conn, NETCONN_EVT_RCVPLUS, len);
132 sys_mbox_post(conn->recvmbox, p);
139 poll_tcp(void *arg, struct tcp_pcb *pcb)
141 struct netconn *conn;
145 (conn->state == NETCONN_WRITE || conn->state == NETCONN_CLOSE) &&
146 conn->sem != SYS_SEM_NULL) {
147 sys_sem_signal(conn->sem);
153 sent_tcp(void *arg, struct tcp_pcb *pcb, u16_t len)
155 struct netconn *conn;
158 if (conn != NULL && conn->sem != SYS_SEM_NULL) {
159 sys_sem_signal(conn->sem);
162 if (conn && conn->callback)
163 if (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT)
164 (*conn->callback)(conn, NETCONN_EVT_SENDPLUS, len);
170 err_tcp(void *arg, err_t err)
172 struct netconn *conn;
176 conn->pcb.tcp = NULL;
180 if (conn->recvmbox != SYS_MBOX_NULL) {
181 /* Register event with callback */
183 (*conn->callback)(conn, NETCONN_EVT_RCVPLUS, 0);
184 sys_mbox_post(conn->recvmbox, NULL);
186 if (conn->mbox != SYS_MBOX_NULL) {
187 sys_mbox_post(conn->mbox, NULL);
189 if (conn->acceptmbox != SYS_MBOX_NULL) {
190 /* Register event with callback */
192 (*conn->callback)(conn, NETCONN_EVT_RCVPLUS, 0);
193 sys_mbox_post(conn->acceptmbox, NULL);
195 if (conn->sem != SYS_SEM_NULL) {
196 sys_sem_signal(conn->sem);
201 setup_tcp(struct netconn *conn)
207 tcp_recv(pcb, recv_tcp);
208 tcp_sent(pcb, sent_tcp);
209 tcp_poll(pcb, poll_tcp, 4);
210 tcp_err(pcb, err_tcp);
214 accept_function(void *arg, struct tcp_pcb *newpcb, err_t err)
217 struct netconn *newconn;
218 struct netconn *conn;
222 tcp_debug_print_state(newpcb->state);
223 #endif /* TCP_DEBUG */
224 #endif /* API_MSG_DEBUG */
225 conn = (struct netconn *)arg;
226 mbox = conn->acceptmbox;
227 newconn = memp_malloc(MEMP_NETCONN);
228 if (newconn == NULL) {
231 newconn->type = NETCONN_TCP;
232 newconn->pcb.tcp = newpcb;
234 newconn->recvmbox = sys_mbox_new();
235 if (newconn->recvmbox == SYS_MBOX_NULL) {
236 memp_free(MEMP_NETCONN, newconn);
239 newconn->mbox = sys_mbox_new();
240 if (newconn->mbox == SYS_MBOX_NULL) {
241 sys_mbox_free(newconn->recvmbox);
242 memp_free(MEMP_NETCONN, newconn);
245 newconn->sem = sys_sem_new(0);
246 if (newconn->sem == SYS_SEM_NULL) {
247 sys_mbox_free(newconn->recvmbox);
248 sys_mbox_free(newconn->mbox);
249 memp_free(MEMP_NETCONN, newconn);
252 newconn->acceptmbox = SYS_MBOX_NULL;
254 /* Register event with callback */
257 (*conn->callback)(conn, NETCONN_EVT_RCVPLUS, 0);
258 /* We have to set the callback here even though
259 * the new socket is unknown. Mark the socket as -1. */
260 newconn->callback = conn->callback;
261 newconn->socket = -1;
264 sys_mbox_post(mbox, newconn);
267 #endif /* LWIP_TCP */
270 do_newconn(struct api_msg_msg *msg)
272 if(msg->conn->pcb.tcp != NULL) {
273 /* This "new" connection already has a PCB allocated. */
274 /* Is this an error condition? Should it be deleted?
275 We currently just are happy and return. */
276 sys_mbox_post(msg->conn->mbox, NULL);
280 msg->conn->err = ERR_OK;
282 /* Allocate a PCB for this connection */
283 switch(msg->conn->type) {
286 msg->conn->pcb.raw = raw_new(msg->msg.bc.port); /* misusing the port field */
287 raw_recv(msg->conn->pcb.raw, recv_raw, msg->conn);
292 case NETCONN_UDPLITE:
294 msg->conn->pcb.udp = udp_new();
295 if(msg->conn->pcb.udp == NULL) {
296 msg->conn->err = ERR_MEM;
299 udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_UDPLITE);
300 udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
302 #endif /* LWIP_UDP */
303 case NETCONN_UDPNOCHKSUM:
305 msg->conn->pcb.udp = udp_new();
306 if(msg->conn->pcb.udp == NULL) {
307 msg->conn->err = ERR_MEM;
310 udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_NOCHKSUM);
311 udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
313 #endif /* LWIP_UDP */
316 msg->conn->pcb.udp = udp_new();
317 if(msg->conn->pcb.udp == NULL) {
318 msg->conn->err = ERR_MEM;
321 udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
322 #endif /* LWIP_UDP */
327 msg->conn->pcb.tcp = tcp_new();
328 if(msg->conn->pcb.tcp == NULL) {
329 msg->conn->err = ERR_MEM;
332 setup_tcp(msg->conn);
338 sys_mbox_post(msg->conn->mbox, NULL);
343 do_delconn(struct api_msg_msg *msg)
345 if (msg->conn->pcb.tcp != NULL) {
346 switch (msg->conn->type) {
349 raw_remove(msg->conn->pcb.raw);
353 case NETCONN_UDPLITE:
355 case NETCONN_UDPNOCHKSUM:
358 msg->conn->pcb.udp->recv_arg = NULL;
359 udp_remove(msg->conn->pcb.udp);
361 #endif /* LWIP_UDP */
364 if (msg->conn->pcb.tcp->state == LISTEN) {
365 tcp_arg(msg->conn->pcb.tcp, NULL);
366 tcp_accept(msg->conn->pcb.tcp, NULL);
367 tcp_close(msg->conn->pcb.tcp);
369 tcp_arg(msg->conn->pcb.tcp, NULL);
370 tcp_sent(msg->conn->pcb.tcp, NULL);
371 tcp_recv(msg->conn->pcb.tcp, NULL);
372 tcp_poll(msg->conn->pcb.tcp, NULL, 0);
373 tcp_err(msg->conn->pcb.tcp, NULL);
374 if (tcp_close(msg->conn->pcb.tcp) != ERR_OK) {
375 tcp_abort(msg->conn->pcb.tcp);
383 /* Trigger select() in socket layer */
384 if (msg->conn->callback)
386 (*msg->conn->callback)(msg->conn, NETCONN_EVT_RCVPLUS, 0);
387 (*msg->conn->callback)(msg->conn, NETCONN_EVT_SENDPLUS, 0);
390 if (msg->conn->mbox != SYS_MBOX_NULL) {
391 sys_mbox_post(msg->conn->mbox, NULL);
396 do_bind(struct api_msg_msg *msg)
398 if (msg->conn->pcb.tcp == NULL) {
399 switch (msg->conn->type) {
402 msg->conn->pcb.raw = raw_new(msg->msg.bc.port); /* misusing the port field as protocol */
403 raw_recv(msg->conn->pcb.raw, recv_raw, msg->conn);
407 case NETCONN_UDPLITE:
408 msg->conn->pcb.udp = udp_new();
409 udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_UDPLITE);
410 udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
412 case NETCONN_UDPNOCHKSUM:
413 msg->conn->pcb.udp = udp_new();
414 udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_NOCHKSUM);
415 udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
418 msg->conn->pcb.udp = udp_new();
419 udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
421 #endif /* LWIP_UDP */
424 msg->conn->pcb.tcp = tcp_new();
425 setup_tcp(msg->conn);
426 #endif /* LWIP_TCP */
431 switch (msg->conn->type) {
434 msg->conn->err = raw_bind(msg->conn->pcb.raw,msg->msg.bc.ipaddr);
438 case NETCONN_UDPLITE:
440 case NETCONN_UDPNOCHKSUM:
443 msg->conn->err = udp_bind(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
445 #endif /* LWIP_UDP */
448 msg->conn->err = tcp_bind(msg->conn->pcb.tcp,
449 msg->msg.bc.ipaddr, msg->msg.bc.port);
450 #endif /* LWIP_TCP */
454 sys_mbox_post(msg->conn->mbox, NULL);
459 do_connected(void *arg, struct tcp_pcb *pcb, err_t err)
461 struct netconn *conn;
470 if (conn->type == NETCONN_TCP && err == ERR_OK) {
473 sys_mbox_post(conn->mbox, NULL);
479 do_connect(struct api_msg_msg *msg)
481 if (msg->conn->pcb.tcp == NULL) {
482 switch (msg->conn->type) {
485 msg->conn->pcb.raw = raw_new(msg->msg.bc.port); /* misusing the port field as protocol */
486 raw_recv(msg->conn->pcb.raw, recv_raw, msg->conn);
490 case NETCONN_UDPLITE:
491 msg->conn->pcb.udp = udp_new();
492 if (msg->conn->pcb.udp == NULL) {
493 msg->conn->err = ERR_MEM;
494 sys_mbox_post(msg->conn->mbox, NULL);
497 udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_UDPLITE);
498 udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
500 case NETCONN_UDPNOCHKSUM:
501 msg->conn->pcb.udp = udp_new();
502 if (msg->conn->pcb.udp == NULL) {
503 msg->conn->err = ERR_MEM;
504 sys_mbox_post(msg->conn->mbox, NULL);
507 udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_NOCHKSUM);
508 udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
511 msg->conn->pcb.udp = udp_new();
512 if (msg->conn->pcb.udp == NULL) {
513 msg->conn->err = ERR_MEM;
514 sys_mbox_post(msg->conn->mbox, NULL);
517 udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
519 #endif /* LWIP_UDP */
522 msg->conn->pcb.tcp = tcp_new();
523 if (msg->conn->pcb.tcp == NULL) {
524 msg->conn->err = ERR_MEM;
525 sys_mbox_post(msg->conn->mbox, NULL);
533 switch (msg->conn->type) {
536 raw_connect(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
537 sys_mbox_post(msg->conn->mbox, NULL);
541 case NETCONN_UDPLITE:
543 case NETCONN_UDPNOCHKSUM:
546 udp_connect(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
547 sys_mbox_post(msg->conn->mbox, NULL);
552 /* tcp_arg(msg->conn->pcb.tcp, msg->conn);*/
553 setup_tcp(msg->conn);
554 tcp_connect(msg->conn->pcb.tcp, msg->msg.bc.ipaddr, msg->msg.bc.port,
556 /*tcp_output(msg->conn->pcb.tcp);*/
565 do_disconnect(struct api_msg_msg *msg)
568 switch (msg->conn->type) {
570 /* Do nothing as connecting is only a helper for upper lwip layers */
573 case NETCONN_UDPLITE:
575 case NETCONN_UDPNOCHKSUM:
579 udp_disconnect(msg->conn->pcb.udp);
585 sys_mbox_post(msg->conn->mbox, NULL);
590 do_listen(struct api_msg_msg *msg)
592 if (msg->conn->pcb.tcp != NULL) {
593 switch (msg->conn->type) {
597 LWIP_DEBUGF(API_MSG_DEBUG, ("api_msg: listen RAW: cannot listen for RAW.\n"));
601 case NETCONN_UDPLITE:
603 case NETCONN_UDPNOCHKSUM:
607 LWIP_DEBUGF(API_MSG_DEBUG, ("api_msg: listen UDP: cannot listen for UDP.\n"));
608 #endif /* LWIP_UDP */
614 msg->conn->pcb.tcp = tcp_listen(msg->conn->pcb.tcp);
615 if (msg->conn->pcb.tcp == NULL)
617 msg->conn->err = ERR_MEM;
621 if (msg->conn->acceptmbox == SYS_MBOX_NULL)
623 msg->conn->acceptmbox = sys_mbox_new();
624 if (msg->conn->acceptmbox == SYS_MBOX_NULL)
626 msg->conn->err = ERR_MEM;
630 tcp_arg(msg->conn->pcb.tcp, msg->conn);
631 tcp_accept(msg->conn->pcb.tcp, accept_function);
640 sys_mbox_post(msg->conn->mbox, NULL);
644 do_accept(struct api_msg_msg *msg)
646 if (msg->conn->pcb.tcp != NULL) {
647 switch (msg->conn->type) {
651 LWIP_DEBUGF(API_MSG_DEBUG, ("api_msg: accept RAW: cannot accept for RAW.\n"));
655 case NETCONN_UDPLITE:
657 case NETCONN_UDPNOCHKSUM:
661 LWIP_DEBUGF(API_MSG_DEBUG, ("api_msg: accept UDP: cannot accept for UDP.\n"));
662 #endif /* LWIP_UDP */
671 do_send(struct api_msg_msg *msg)
673 if (msg->conn->pcb.tcp != NULL) {
674 switch (msg->conn->type) {
678 raw_send(msg->conn->pcb.raw, msg->msg.p);
682 case NETCONN_UDPLITE:
684 case NETCONN_UDPNOCHKSUM:
688 udp_send(msg->conn->pcb.udp, msg->msg.p);
689 #endif /* LWIP_UDP */
696 sys_mbox_post(msg->conn->mbox, NULL);
700 do_recv(struct api_msg_msg *msg)
703 if (msg->conn->pcb.tcp != NULL) {
704 if (msg->conn->type == NETCONN_TCP) {
705 tcp_recved(msg->conn->pcb.tcp, msg->msg.len);
709 sys_mbox_post(msg->conn->mbox, NULL);
713 do_write(struct api_msg_msg *msg)
718 if (msg->conn->pcb.tcp != NULL) {
719 switch (msg->conn->type) {
723 msg->conn->err = ERR_VAL;
728 case NETCONN_UDPLITE:
730 case NETCONN_UDPNOCHKSUM:
734 msg->conn->err = ERR_VAL;
735 #endif /* LWIP_UDP */
740 err = tcp_write(msg->conn->pcb.tcp, msg->msg.w.dataptr,
741 msg->msg.w.len, msg->msg.w.copy);
742 /* This is the Nagle algorithm: inhibit the sending of new TCP
743 segments when new outgoing data arrives from the user if any
744 previously transmitted data on the connection remains
746 if(err == ERR_OK && (msg->conn->pcb.tcp->unacked == NULL || (msg->conn->pcb.tcp->flags & TF_NODELAY)) ) {
747 tcp_output(msg->conn->pcb.tcp);
749 msg->conn->err = err;
750 if (msg->conn->callback)
753 if (tcp_sndbuf(msg->conn->pcb.tcp) <= TCP_SNDLOWAT)
754 (*msg->conn->callback)(msg->conn, NETCONN_EVT_SENDMINUS, msg->msg.w.len);
764 sys_mbox_post(msg->conn->mbox, NULL);
768 do_close(struct api_msg_msg *msg)
774 if (msg->conn->pcb.tcp != NULL) {
775 switch (msg->conn->type) {
781 case NETCONN_UDPLITE:
783 case NETCONN_UDPNOCHKSUM:
791 if (msg->conn->pcb.tcp->state == LISTEN) {
792 err = tcp_close(msg->conn->pcb.tcp);
794 msg->conn->err = err;
802 sys_mbox_post(msg->conn->mbox, NULL);
805 typedef void (* api_msg_decode)(struct api_msg_msg *msg);
806 static api_msg_decode decode[API_MSG_MAX] = {
820 api_msg_input(struct api_msg *msg)
822 decode[msg->type](&(msg->msg));
826 api_msg_post(struct api_msg *msg)