Improved networking stability and thread safety.
[planlOS.git] / system / kernel / lwip / api / api_msg.c
blobdceb4919a2f06bd0806ab6f230bcaafe74a28f07
1 /**
2 * @file
3 * Sequential API Internal module
5 */
7 /*
8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
14 * 1. Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
31 * OF SUCH DAMAGE.
33 * This file is part of the lwIP TCP/IP stack.
35 * Author: Adam Dunkels <adam@sics.se>
39 #include "lwip/opt.h"
41 #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
43 #include "lwip/api_msg.h"
45 #include "lwip/ip.h"
46 #include "lwip/udp.h"
47 #include "lwip/tcp.h"
48 #include "lwip/raw.h"
50 #include "lwip/memp.h"
51 #include "lwip/tcpip.h"
52 #include "lwip/igmp.h"
53 #include "lwip/dns.h"
55 /* forward declarations */
56 #if LWIP_TCP
57 static err_t do_writemore(struct netconn *conn);
58 static void do_close_internal(struct netconn *conn);
59 #endif
61 #if LWIP_RAW
62 /**
63 * Receive callback function for RAW netconns.
64 * Doesn't 'eat' the packet, only references it and sends it to
65 * conn->recvmbox
67 * @see raw.h (struct raw_pcb.recv) for parameters and return value
69 static u8_t
70 recv_raw(void *arg, struct raw_pcb *pcb, struct pbuf *p,
71 struct ip_addr *addr)
73 struct pbuf *q;
74 struct netbuf *buf;
75 struct netconn *conn;
76 #if LWIP_SO_RCVBUF
77 int recv_avail;
78 #endif /* LWIP_SO_RCVBUF */
80 LWIP_UNUSED_ARG(addr);
81 conn = arg;
83 #if LWIP_SO_RCVBUF
84 SYS_ARCH_GET(conn->recv_avail, recv_avail);
85 if ((conn != NULL) && (conn->recvmbox != SYS_MBOX_NULL) &&
86 ((recv_avail + (int)(p->tot_len)) <= conn->recv_bufsize)) {
87 #else /* LWIP_SO_RCVBUF */
88 if ((conn != NULL) && (conn->recvmbox != SYS_MBOX_NULL)) {
89 #endif /* LWIP_SO_RCVBUF */
90 /* copy the whole packet into new pbufs */
91 q = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
92 if(q != NULL) {
93 if (pbuf_copy(q, p) != ERR_OK) {
94 pbuf_free(q);
95 q = NULL;
99 if(q != NULL) {
100 buf = memp_malloc(MEMP_NETBUF);
101 if (buf == NULL) {
102 pbuf_free(q);
103 return 0;
106 buf->p = q;
107 buf->ptr = q;
108 buf->addr = &(((struct ip_hdr*)(q->payload))->src);
109 buf->port = pcb->protocol;
111 SYS_ARCH_INC(conn->recv_avail, q->tot_len);
112 /* Register event with callback */
113 API_EVENT(conn, NETCONN_EVT_RCVPLUS, q->tot_len);
114 if (sys_mbox_trypost(conn->recvmbox, buf) != ERR_OK) {
115 netbuf_delete(buf);
120 return 0; /* do not eat the packet */
122 #endif /* LWIP_RAW*/
124 #if LWIP_UDP
126 * Receive callback function for UDP netconns.
127 * Posts the packet to conn->recvmbox or deletes it on memory error.
129 * @see udp.h (struct udp_pcb.recv) for parameters
131 static void
132 recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p,
133 struct ip_addr *addr, u16_t port)
135 struct netbuf *buf;
136 struct netconn *conn;
137 #if LWIP_SO_RCVBUF
138 int recv_avail;
139 #endif /* LWIP_SO_RCVBUF */
141 LWIP_UNUSED_ARG(pcb); /* only used for asserts... */
142 LWIP_ASSERT("recv_udp must have a pcb argument", pcb != NULL);
143 LWIP_ASSERT("recv_udp must have an argument", arg != NULL);
144 conn = arg;
145 LWIP_ASSERT("recv_udp: recv for wrong pcb!", conn->pcb.udp == pcb);
147 #if LWIP_SO_RCVBUF
148 SYS_ARCH_GET(conn->recv_avail, recv_avail);
149 if ((conn == NULL) || (conn->recvmbox == SYS_MBOX_NULL) ||
150 ((recv_avail + (int)(p->tot_len)) > conn->recv_bufsize)) {
151 #else /* LWIP_SO_RCVBUF */
152 if ((conn == NULL) || (conn->recvmbox == SYS_MBOX_NULL)) {
153 #endif /* LWIP_SO_RCVBUF */
154 pbuf_free(p);
155 return;
158 buf = memp_malloc(MEMP_NETBUF);
159 if (buf == NULL) {
160 pbuf_free(p);
161 return;
162 } else {
163 buf->p = p;
164 buf->ptr = p;
165 buf->addr = addr;
166 buf->port = port;
169 SYS_ARCH_INC(conn->recv_avail, p->tot_len);
170 /* Register event with callback */
171 API_EVENT(conn, NETCONN_EVT_RCVPLUS, p->tot_len);
172 if (sys_mbox_trypost(conn->recvmbox, buf) != ERR_OK) {
173 netbuf_delete(buf);
174 return;
177 #endif /* LWIP_UDP */
179 #if LWIP_TCP
181 * Receive callback function for TCP netconns.
182 * Posts the packet to conn->recvmbox, but doesn't delete it on errors.
184 * @see tcp.h (struct tcp_pcb.recv) for parameters and return value
186 static err_t
187 recv_tcp(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
189 struct netconn *conn;
190 u16_t len;
192 LWIP_UNUSED_ARG(pcb);
193 LWIP_ASSERT("recv_tcp must have a pcb argument", pcb != NULL);
194 LWIP_ASSERT("recv_tcp must have an argument", arg != NULL);
195 conn = arg;
196 LWIP_ASSERT("recv_tcp: recv for wrong pcb!", conn->pcb.tcp == pcb);
198 if ((conn == NULL) || (conn->recvmbox == SYS_MBOX_NULL)) {
199 return ERR_VAL;
202 conn->err = err;
203 if (p != NULL) {
204 len = p->tot_len;
205 SYS_ARCH_INC(conn->recv_avail, len);
206 } else {
207 len = 0;
209 /* Register event with callback */
210 API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
211 if (sys_mbox_trypost(conn->recvmbox, p) != ERR_OK) {
212 return ERR_MEM;
215 return ERR_OK;
219 * Poll callback function for TCP netconns.
220 * Wakes up an application thread that waits for a connection to close
221 * or data to be sent. The application thread then takes the
222 * appropriate action to go on.
224 * Signals the conn->sem.
225 * netconn_close waits for conn->sem if closing failed.
227 * @see tcp.h (struct tcp_pcb.poll) for parameters and return value
229 static err_t
230 poll_tcp(void *arg, struct tcp_pcb *pcb)
232 struct netconn *conn = arg;
234 LWIP_UNUSED_ARG(pcb);
235 LWIP_ASSERT("conn != NULL", (conn != NULL));
237 if (conn->state == NETCONN_WRITE) {
238 do_writemore(conn);
239 } else if (conn->state == NETCONN_CLOSE) {
240 do_close_internal(conn);
243 return ERR_OK;
247 * Sent callback function for TCP netconns.
248 * Signals the conn->sem and calls API_EVENT.
249 * netconn_write waits for conn->sem if send buffer is low.
251 * @see tcp.h (struct tcp_pcb.sent) for parameters and return value
253 static err_t
254 sent_tcp(void *arg, struct tcp_pcb *pcb, u16_t len)
256 struct netconn *conn = arg;
258 LWIP_UNUSED_ARG(pcb);
259 LWIP_ASSERT("conn != NULL", (conn != NULL));
261 if (conn->state == NETCONN_WRITE) {
262 LWIP_ASSERT("conn->pcb.tcp != NULL", conn->pcb.tcp != NULL);
263 do_writemore(conn);
264 } else if (conn->state == NETCONN_CLOSE) {
265 do_close_internal(conn);
268 if (conn) {
269 if ((conn->pcb.tcp != NULL) && (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT)) {
270 API_EVENT(conn, NETCONN_EVT_SENDPLUS, len);
274 return ERR_OK;
278 * Error callback function for TCP netconns.
279 * Signals conn->sem, posts to all conn mboxes and calls API_EVENT.
280 * The application thread has then to decide what to do.
282 * @see tcp.h (struct tcp_pcb.err) for parameters
284 static void
285 err_tcp(void *arg, err_t err)
287 struct netconn *conn;
289 conn = arg;
290 LWIP_ASSERT("conn != NULL", (conn != NULL));
292 conn->pcb.tcp = NULL;
294 conn->err = err;
295 if (conn->recvmbox != SYS_MBOX_NULL) {
296 /* Register event with callback */
297 API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
298 sys_mbox_post(conn->recvmbox, NULL);
300 if (conn->op_completed != SYS_SEM_NULL && conn->state == NETCONN_CONNECT) {
301 conn->state = NETCONN_NONE;
302 sys_sem_signal(conn->op_completed);
304 if (conn->acceptmbox != SYS_MBOX_NULL) {
305 /* Register event with callback */
306 API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
307 sys_mbox_post(conn->acceptmbox, NULL);
309 if ((conn->state == NETCONN_WRITE) || (conn->state == NETCONN_CLOSE)) {
310 /* calling do_writemore/do_close_internal is not necessary
311 since the pcb has already been deleted! */
312 conn->state = NETCONN_NONE;
313 /* wake up the waiting task */
314 sys_sem_signal(conn->op_completed);
319 * Setup a tcp_pcb with the correct callback function pointers
320 * and their arguments.
322 * @param conn the TCP netconn to setup
324 static void
325 setup_tcp(struct netconn *conn)
327 struct tcp_pcb *pcb;
329 pcb = conn->pcb.tcp;
330 tcp_arg(pcb, conn);
331 tcp_recv(pcb, recv_tcp);
332 tcp_sent(pcb, sent_tcp);
333 tcp_poll(pcb, poll_tcp, 4);
334 tcp_err(pcb, err_tcp);
338 * Accept callback function for TCP netconns.
339 * Allocates a new netconn and posts that to conn->acceptmbox.
341 * @see tcp.h (struct tcp_pcb_listen.accept) for parameters and return value
343 static err_t
344 accept_function(void *arg, struct tcp_pcb *newpcb, err_t err)
346 struct netconn *newconn;
347 struct netconn *conn;
349 #if API_MSG_DEBUG
350 #if TCP_DEBUG
351 tcp_debug_print_state(newpcb->state);
352 #endif /* TCP_DEBUG */
353 #endif /* API_MSG_DEBUG */
354 conn = (struct netconn *)arg;
356 LWIP_ERROR("accept_function: invalid conn->acceptmbox",
357 conn->acceptmbox != SYS_MBOX_NULL, return ERR_VAL;);
359 /* We have to set the callback here even though
360 * the new socket is unknown. conn->socket is marked as -1. */
361 newconn = netconn_alloc(conn->type, conn->callback);
362 if (newconn == NULL) {
363 return ERR_MEM;
365 newconn->pcb.tcp = newpcb;
366 setup_tcp(newconn);
367 newconn->err = err;
368 /* Register event with callback */
369 API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
371 if (sys_mbox_trypost(conn->acceptmbox, newconn) != ERR_OK) {
372 /* When returning != ERR_OK, the connection is aborted in tcp_process(),
373 so do nothing here! */
374 newconn->pcb.tcp = NULL;
375 netconn_free(newconn);
376 return ERR_MEM;
378 return ERR_OK;
380 #endif /* LWIP_TCP */
383 * Create a new pcb of a specific type.
384 * Called from do_newconn().
386 * @param msg the api_msg_msg describing the connection type
387 * @return msg->conn->err, but the return value is currently ignored
389 static err_t
390 pcb_new(struct api_msg_msg *msg)
392 msg->conn->err = ERR_OK;
394 LWIP_ASSERT("pcb_new: pcb already allocated", msg->conn->pcb.tcp == NULL);
396 /* Allocate a PCB for this connection */
397 switch(NETCONNTYPE_GROUP(msg->conn->type)) {
398 #if LWIP_RAW
399 case NETCONN_RAW:
400 msg->conn->pcb.raw = raw_new(msg->msg.n.proto);
401 if(msg->conn->pcb.raw == NULL) {
402 msg->conn->err = ERR_MEM;
403 break;
405 raw_recv(msg->conn->pcb.raw, recv_raw, msg->conn);
406 break;
407 #endif /* LWIP_RAW */
408 #if LWIP_UDP
409 case NETCONN_UDP:
410 msg->conn->pcb.udp = udp_new();
411 if(msg->conn->pcb.udp == NULL) {
412 msg->conn->err = ERR_MEM;
413 break;
415 #if LWIP_UDPLITE
416 if (msg->conn->type==NETCONN_UDPLITE) {
417 udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_UDPLITE);
419 #endif /* LWIP_UDPLITE */
420 if (msg->conn->type==NETCONN_UDPNOCHKSUM) {
421 udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_NOCHKSUM);
423 udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
424 break;
425 #endif /* LWIP_UDP */
426 #if LWIP_TCP
427 case NETCONN_TCP:
428 msg->conn->pcb.tcp = tcp_new();
429 if(msg->conn->pcb.tcp == NULL) {
430 msg->conn->err = ERR_MEM;
431 break;
433 setup_tcp(msg->conn);
434 break;
435 #endif /* LWIP_TCP */
436 default:
437 /* Unsupported netconn type, e.g. protocol disabled */
438 msg->conn->err = ERR_VAL;
439 break;
442 return msg->conn->err;
446 * Create a new pcb of a specific type inside a netconn.
447 * Called from netconn_new_with_proto_and_callback.
449 * @param msg the api_msg_msg describing the connection type
451 void
452 do_newconn(struct api_msg_msg *msg)
454 if(msg->conn->pcb.tcp == NULL) {
455 pcb_new(msg);
457 /* Else? This "new" connection already has a PCB allocated. */
458 /* Is this an error condition? Should it be deleted? */
459 /* We currently just are happy and return. */
461 TCPIP_APIMSG_ACK(msg);
465 * Create a new netconn (of a specific type) that has a callback function.
466 * The corresponding pcb is NOT created!
468 * @param t the type of 'connection' to create (@see enum netconn_type)
469 * @param proto the IP protocol for RAW IP pcbs
470 * @param callback a function to call on status changes (RX available, TX'ed)
471 * @return a newly allocated struct netconn or
472 * NULL on memory error
474 struct netconn*
475 netconn_alloc(enum netconn_type t, netconn_callback callback)
477 struct netconn *conn;
478 int size;
480 conn = memp_malloc(MEMP_NETCONN);
481 if (conn == NULL) {
482 return NULL;
484 conn->writebuffer = 0;
486 conn->err = ERR_OK;
487 conn->type = t;
488 conn->pcb.tcp = NULL;
490 #if (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_UDP_RECVMBOX_SIZE) && \
491 (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_TCP_RECVMBOX_SIZE)
492 size = DEFAULT_RAW_RECVMBOX_SIZE;
493 #else
494 switch(NETCONNTYPE_GROUP(t)) {
495 #if LWIP_RAW
496 case NETCONN_RAW:
497 size = DEFAULT_RAW_RECVMBOX_SIZE;
498 break;
499 #endif /* LWIP_RAW */
500 #if LWIP_UDP
501 case NETCONN_UDP:
502 size = DEFAULT_UDP_RECVMBOX_SIZE;
503 break;
504 #endif /* LWIP_UDP */
505 #if LWIP_TCP
506 case NETCONN_TCP:
507 size = DEFAULT_TCP_RECVMBOX_SIZE;
508 break;
509 #endif /* LWIP_TCP */
510 default:
511 LWIP_ASSERT("netconn_alloc: undefined netconn_type", 0);
512 break;
514 #endif
516 if ((conn->op_completed = sys_sem_new(0)) == SYS_SEM_NULL) {
517 memp_free(MEMP_NETCONN, conn);
518 return NULL;
520 if ((conn->recvmbox = sys_mbox_new(size)) == SYS_MBOX_NULL) {
521 sys_sem_free(conn->op_completed);
522 memp_free(MEMP_NETCONN, conn);
523 return NULL;
526 conn->acceptmbox = SYS_MBOX_NULL;
527 conn->state = NETCONN_NONE;
528 /* initialize socket to -1 since 0 is a valid socket */
529 conn->socket = -1;
530 conn->callback = callback;
531 conn->recv_avail = 0;
532 #if LWIP_SO_RCVTIMEO
533 conn->recv_timeout = 0;
534 #endif /* LWIP_SO_RCVTIMEO */
535 #if LWIP_SO_RCVBUF
536 conn->recv_bufsize = INT_MAX;
537 #endif /* LWIP_SO_RCVBUF */
538 return conn;
542 * Delete a netconn and all its resources.
543 * The pcb is NOT freed (since we might not be in the right thread context do this).
545 * @param conn the netconn to free
547 void
548 netconn_free(struct netconn *conn)
550 void *mem;
551 LWIP_ASSERT("PCB must be deallocated outside this function", conn->pcb.tcp == NULL);
553 /* Drain the recvmbox. */
554 if (conn->recvmbox != SYS_MBOX_NULL) {
555 while (sys_mbox_tryfetch(conn->recvmbox, &mem) != SYS_MBOX_EMPTY) {
556 if (conn->type == NETCONN_TCP) {
557 if(mem != NULL) {
558 pbuf_free((struct pbuf *)mem);
560 } else {
561 netbuf_delete((struct netbuf *)mem);
564 sys_mbox_free(conn->recvmbox);
565 conn->recvmbox = SYS_MBOX_NULL;
568 /* Drain the acceptmbox. */
569 if (conn->acceptmbox != SYS_MBOX_NULL) {
570 while (sys_mbox_tryfetch(conn->acceptmbox, &mem) != SYS_MBOX_EMPTY) {
571 netconn_delete((struct netconn *)mem);
573 sys_mbox_free(conn->acceptmbox);
574 conn->acceptmbox = SYS_MBOX_NULL;
577 //sys_sem_free(conn->op_completed);
578 conn->op_completed = SYS_SEM_NULL;
580 memp_free(MEMP_NETCONN, conn);
583 #if LWIP_TCP
585 * Internal helper function to close a TCP netconn: since this sometimes
586 * doesn't work at the first attempt, this function is called from multiple
587 * places.
589 * @param conn the TCP netconn to close
591 static void
592 do_close_internal(struct netconn *conn)
594 err_t err;
596 LWIP_ASSERT("invalid conn", (conn != NULL));
597 LWIP_ASSERT("this is for tcp netconns only", (conn->type == NETCONN_TCP));
598 LWIP_ASSERT("conn must be in state NETCONN_CLOSE", (conn->state == NETCONN_CLOSE));
599 LWIP_ASSERT("pcb already closed", (conn->pcb.tcp != NULL));
601 /* Set back some callback pointers */
602 if (conn->pcb.tcp->state == LISTEN) {
603 tcp_arg(conn->pcb.tcp, NULL);
604 tcp_accept(conn->pcb.tcp, NULL);
605 } else {
606 tcp_recv(conn->pcb.tcp, NULL);
608 /* Try to close the connection */
609 err = tcp_close(conn->pcb.tcp);
610 if (err == ERR_OK) {
611 /* Closing succeeded */
612 conn->state = NETCONN_NONE;
613 /* Set back some callback pointers as conn is going away */
614 tcp_err(conn->pcb.tcp, NULL);
615 tcp_poll(conn->pcb.tcp, NULL, 4);
616 tcp_sent(conn->pcb.tcp, NULL);
617 tcp_recv(conn->pcb.tcp, NULL);
618 tcp_arg(conn->pcb.tcp, NULL);
619 conn->pcb.tcp = NULL;
620 conn->err = ERR_OK;
621 /* Trigger select() in socket layer. This send should something else so the
622 errorfd is set, not the read and write fd! */
623 API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
624 API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
625 /* wake up the application task */
626 sys_sem_signal(conn->op_completed);
628 /* If closing didn't succeed, we get called again either
629 from poll_tcp or from sent_tcp */
631 #endif /* LWIP_TCP */
634 * Delete the pcb inside a netconn.
635 * Called from netconn_delete.
637 * @param msg the api_msg_msg pointing to the connection
639 void
640 do_delconn(struct api_msg_msg *msg)
642 if (msg->conn->pcb.tcp != NULL) {
643 switch (NETCONNTYPE_GROUP(msg->conn->type)) {
644 #if LWIP_RAW
645 case NETCONN_RAW:
646 raw_remove(msg->conn->pcb.raw);
647 break;
648 #endif /* LWIP_RAW */
649 #if LWIP_UDP
650 case NETCONN_UDP:
651 msg->conn->pcb.udp->recv_arg = NULL;
652 udp_remove(msg->conn->pcb.udp);
653 break;
654 #endif /* LWIP_UDP */
655 #if LWIP_TCP
656 case NETCONN_TCP:
657 msg->conn->state = NETCONN_CLOSE;
658 do_close_internal(msg->conn);
659 /* API_EVENT is called inside do_close_internal, before releasing
660 the application thread, so we can return at this point! */
661 return;
662 #endif /* LWIP_TCP */
663 default:
664 break;
667 /* tcp netconns don't come here! */
669 /* Trigger select() in socket layer. This send should something else so the
670 errorfd is set, not the read and write fd! */
671 API_EVENT(msg->conn, NETCONN_EVT_RCVPLUS, 0);
672 API_EVENT(msg->conn, NETCONN_EVT_SENDPLUS, 0);
674 if (msg->conn->op_completed != SYS_SEM_NULL) {
675 sys_sem_signal(msg->conn->op_completed);
680 * Bind a pcb contained in a netconn
681 * Called from netconn_bind.
683 * @param msg the api_msg_msg pointing to the connection and containing
684 * the IP address and port to bind to
686 void
687 do_bind(struct api_msg_msg *msg)
689 if (!ERR_IS_FATAL(msg->conn->err)) {
690 if (msg->conn->pcb.tcp != NULL) {
691 switch (NETCONNTYPE_GROUP(msg->conn->type)) {
692 #if LWIP_RAW
693 case NETCONN_RAW:
694 msg->conn->err = raw_bind(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
695 break;
696 #endif /* LWIP_RAW */
697 #if LWIP_UDP
698 case NETCONN_UDP:
699 msg->conn->err = udp_bind(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
700 break;
701 #endif /* LWIP_UDP */
702 #if LWIP_TCP
703 case NETCONN_TCP:
704 msg->conn->err = tcp_bind(msg->conn->pcb.tcp, msg->msg.bc.ipaddr, msg->msg.bc.port);
705 break;
706 #endif /* LWIP_TCP */
707 default:
708 break;
710 } else {
711 /* msg->conn->pcb is NULL */
712 msg->conn->err = ERR_VAL;
715 TCPIP_APIMSG_ACK(msg);
718 #if LWIP_TCP
720 * TCP callback function if a connection (opened by tcp_connect/do_connect) has
721 * been established (or reset by the remote host).
723 * @see tcp.h (struct tcp_pcb.connected) for parameters and return values
725 static err_t
726 do_connected(void *arg, struct tcp_pcb *pcb, err_t err)
728 struct netconn *conn;
730 LWIP_UNUSED_ARG(pcb);
732 conn = arg;
734 if (conn == NULL) {
735 return ERR_VAL;
738 conn->err = err;
739 if ((conn->type == NETCONN_TCP) && (err == ERR_OK)) {
740 setup_tcp(conn);
742 conn->state = NETCONN_NONE;
743 sys_sem_signal(conn->op_completed);
744 return ERR_OK;
746 #endif /* LWIP_TCP */
749 * Connect a pcb contained inside a netconn
750 * Called from netconn_connect.
752 * @param msg the api_msg_msg pointing to the connection and containing
753 * the IP address and port to connect to
755 void
756 do_connect(struct api_msg_msg *msg)
758 if (msg->conn->pcb.tcp == NULL) {
759 sys_sem_signal(msg->conn->op_completed);
760 return;
763 switch (NETCONNTYPE_GROUP(msg->conn->type)) {
764 #if LWIP_RAW
765 case NETCONN_RAW:
766 msg->conn->err = raw_connect(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
767 sys_sem_signal(msg->conn->op_completed);
768 break;
769 #endif /* LWIP_RAW */
770 #if LWIP_UDP
771 case NETCONN_UDP:
772 msg->conn->err = udp_connect(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
773 sys_sem_signal(msg->conn->op_completed);
774 break;
775 #endif /* LWIP_UDP */
776 #if LWIP_TCP
777 case NETCONN_TCP:
778 msg->conn->state = NETCONN_CONNECT;
779 setup_tcp(msg->conn);
780 msg->conn->err = tcp_connect(msg->conn->pcb.tcp, msg->msg.bc.ipaddr, msg->msg.bc.port,
781 do_connected);
782 /* sys_sem_signal() is called from do_connected (or err_tcp()),
783 * when the connection is established! */
784 break;
785 #endif /* LWIP_TCP */
786 default:
787 break;
792 * Connect a pcb contained inside a netconn
793 * Only used for UDP netconns.
794 * Called from netconn_disconnect.
796 * @param msg the api_msg_msg pointing to the connection to disconnect
798 void
799 do_disconnect(struct api_msg_msg *msg)
801 #if LWIP_UDP
802 if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
803 udp_disconnect(msg->conn->pcb.udp);
805 #endif /* LWIP_UDP */
806 TCPIP_APIMSG_ACK(msg);
810 * Set a TCP pcb contained in a netconn into listen mode
811 * Called from netconn_listen.
813 * @param msg the api_msg_msg pointing to the connection
815 void
816 do_listen(struct api_msg_msg *msg)
818 #if LWIP_TCP
819 if (!ERR_IS_FATAL(msg->conn->err)) {
820 if (msg->conn->pcb.tcp != NULL) {
821 if (msg->conn->type == NETCONN_TCP) {
822 if (msg->conn->pcb.tcp->state == CLOSED) {
823 #if TCP_LISTEN_BACKLOG
824 struct tcp_pcb* lpcb = tcp_listen_with_backlog(msg->conn->pcb.tcp, msg->msg.lb.backlog);
825 #else /* TCP_LISTEN_BACKLOG */
826 struct tcp_pcb* lpcb = tcp_listen(msg->conn->pcb.tcp);
827 #endif /* TCP_LISTEN_BACKLOG */
828 if (lpcb == NULL) {
829 msg->conn->err = ERR_MEM;
830 } else {
831 /* delete the recvmbox and allocate the acceptmbox */
832 if (msg->conn->recvmbox != SYS_MBOX_NULL) {
833 /** @todo: should we drain the recvmbox here? */
834 sys_mbox_free(msg->conn->recvmbox);
835 msg->conn->recvmbox = SYS_MBOX_NULL;
837 if (msg->conn->acceptmbox == SYS_MBOX_NULL) {
838 if ((msg->conn->acceptmbox = sys_mbox_new(DEFAULT_ACCEPTMBOX_SIZE)) == SYS_MBOX_NULL) {
839 msg->conn->err = ERR_MEM;
842 if (msg->conn->err == ERR_OK) {
843 msg->conn->state = NETCONN_LISTEN;
844 msg->conn->pcb.tcp = lpcb;
845 tcp_arg(msg->conn->pcb.tcp, msg->conn);
846 tcp_accept(msg->conn->pcb.tcp, accept_function);
849 } else {
850 msg->conn->err = ERR_CONN;
855 #endif /* LWIP_TCP */
856 TCPIP_APIMSG_ACK(msg);
860 * Send some data on a RAW or UDP pcb contained in a netconn
861 * Called from netconn_send
863 * @param msg the api_msg_msg pointing to the connection
865 void
866 do_send(struct api_msg_msg *msg)
868 if (!ERR_IS_FATAL(msg->conn->err)) {
869 if (msg->conn->pcb.tcp != NULL) {
870 switch (NETCONNTYPE_GROUP(msg->conn->type)) {
871 #if LWIP_RAW
872 case NETCONN_RAW:
873 if (msg->msg.b->addr == NULL) {
874 msg->conn->err = raw_send(msg->conn->pcb.raw, msg->msg.b->p);
875 } else {
876 msg->conn->err = raw_sendto(msg->conn->pcb.raw, msg->msg.b->p, msg->msg.b->addr);
878 break;
879 #endif
880 #if LWIP_UDP
881 case NETCONN_UDP:
882 if (msg->msg.b->addr == NULL) {
883 msg->conn->err = udp_send(msg->conn->pcb.udp, msg->msg.b->p);
884 } else {
885 msg->conn->err = udp_sendto(msg->conn->pcb.udp, msg->msg.b->p, msg->msg.b->addr, msg->msg.b->port);
887 break;
888 #endif /* LWIP_UDP */
889 default:
890 break;
894 TCPIP_APIMSG_ACK(msg);
898 * Recv some data from a RAW or UDP pcb contained in a netconn
899 * Called from netconn_recv
901 * @param msg the api_msg_msg pointing to the connection
903 void
904 do_recv(struct api_msg_msg *msg)
906 #if LWIP_TCP
907 if (!ERR_IS_FATAL(msg->conn->err)) {
908 if (msg->conn->pcb.tcp != NULL) {
909 if (msg->conn->type == NETCONN_TCP) {
910 #if TCP_LISTEN_BACKLOG
911 if (msg->conn->pcb.tcp->state == LISTEN) {
912 tcp_accepted(msg->conn->pcb.tcp);
913 } else
914 #endif /* TCP_LISTEN_BACKLOG */
916 tcp_recved(msg->conn->pcb.tcp, msg->msg.r.len);
921 #endif /* LWIP_TCP */
922 TCPIP_APIMSG_ACK(msg);
925 #if LWIP_TCP
927 * See if more data needs to be written from a previous call to netconn_write.
928 * Called initially from do_write. If the first call can't send all data
929 * (because of low memory or empty send-buffer), this function is called again
930 * from sent_tcp() or poll_tcp() to send more data. If all data is sent, the
931 * blocking application thread (waiting in netconn_write) is released.
933 * @param conn netconn (that is currently in state NETCONN_WRITE) to process
934 * @return ERR_OK
935 * ERR_MEM if LWIP_TCPIP_CORE_LOCKING=1 and sending hasn't yet finished
937 static err_t
938 do_writemore(struct netconn *conn)
940 err_t err;
941 void *dataptr;
942 u16_t len, available;
943 u8_t write_finished = 0;
945 LWIP_ASSERT("conn->state == NETCONN_WRITE", (conn->state == NETCONN_WRITE));
947 dataptr = (u8_t*)conn->write_msg->msg.w.dataptr + conn->write_offset;
948 if ((conn->write_msg->msg.w.len - conn->write_offset > 0xffff)) { /* max_u16_t */
949 len = 0xffff;
950 #if LWIP_TCPIP_CORE_LOCKING
951 conn->write_delayed = 1;
952 #endif
953 } else {
954 len = conn->write_msg->msg.w.len - conn->write_offset;
956 available = tcp_sndbuf(conn->pcb.tcp);
957 if (available < len) {
958 /* don't try to write more than sendbuf */
959 len = available;
960 #if LWIP_TCPIP_CORE_LOCKING
961 conn->write_delayed = 1;
962 #endif
965 err = tcp_write(conn->pcb.tcp, dataptr, len, conn->write_msg->msg.w.apiflags);
966 LWIP_ASSERT("do_writemore: invalid length!", ((conn->write_offset + len) <= conn->write_msg->msg.w.len));
967 if (err == ERR_OK) {
968 conn->write_offset += len;
969 if (conn->write_offset == conn->write_msg->msg.w.len) {
970 /* everything was written */
971 write_finished = 1;
972 conn->write_msg = NULL;
973 conn->write_offset = 0;
975 err = tcp_output_nagle(conn->pcb.tcp);
976 conn->err = err;
977 if ((err == ERR_OK) && (tcp_sndbuf(conn->pcb.tcp) <= TCP_SNDLOWAT)) {
978 API_EVENT(conn, NETCONN_EVT_SENDMINUS, len);
980 } else if (err == ERR_MEM) {
981 /* If ERR_MEM, we wait for sent_tcp or poll_tcp to be called
982 we do NOT return to the application thread, since ERR_MEM is
983 only a temporary error! */
985 /* tcp_enqueue returned ERR_MEM, try tcp_output anyway */
986 err = tcp_output(conn->pcb.tcp);
988 #if LWIP_TCPIP_CORE_LOCKING
989 conn->write_delayed = 1;
990 #endif
991 } else {
992 /* On errors != ERR_MEM, we don't try writing any more but return
993 the error to the application thread. */
994 conn->err = err;
995 write_finished = 1;
998 if (write_finished) {
999 /* everything was written: set back connection state
1000 and back to application task */
1001 conn->state = NETCONN_NONE;
1002 #if LWIP_TCPIP_CORE_LOCKING
1003 if (conn->write_delayed != 0)
1004 #endif
1006 sys_sem_signal(conn->op_completed);
1009 #if LWIP_TCPIP_CORE_LOCKING
1010 else
1011 return ERR_MEM;
1012 #endif
1013 return ERR_OK;
1015 #endif /* LWIP_TCP */
1018 * Send some data on a TCP pcb contained in a netconn
1019 * Called from netconn_write
1021 * @param msg the api_msg_msg pointing to the connection
1023 void
1024 do_write(struct api_msg_msg *msg)
1026 if (!ERR_IS_FATAL(msg->conn->err)) {
1027 if ((msg->conn->pcb.tcp != NULL) && (msg->conn->type == NETCONN_TCP)) {
1028 #if LWIP_TCP
1029 msg->conn->state = NETCONN_WRITE;
1030 /* set all the variables used by do_writemore */
1031 msg->conn->write_msg = msg;
1032 msg->conn->write_offset = 0;
1033 #if LWIP_TCPIP_CORE_LOCKING
1034 msg->conn->write_delayed = 0;
1035 if (do_writemore(msg->conn) != ERR_OK) {
1036 LWIP_ASSERT("state!", msg->conn->state == NETCONN_WRITE);
1037 UNLOCK_TCPIP_CORE();
1038 sys_arch_sem_wait(msg->conn->op_completed, 0);
1039 LOCK_TCPIP_CORE();
1040 LWIP_ASSERT("state!", msg->conn->state == NETCONN_NONE);
1042 #else
1043 do_writemore(msg->conn);
1044 #endif
1045 /* for both cases: if do_writemore was called, don't ACK the APIMSG! */
1046 return;
1047 #endif /* LWIP_TCP */
1048 #if (LWIP_UDP || LWIP_RAW)
1049 } else {
1050 msg->conn->err = ERR_VAL;
1051 #endif /* (LWIP_UDP || LWIP_RAW) */
1054 TCPIP_APIMSG_ACK(msg);
1058 * Return a connection's local or remote address
1059 * Called from netconn_getaddr
1061 * @param msg the api_msg_msg pointing to the connection
1063 void
1064 do_getaddr(struct api_msg_msg *msg)
1066 if (msg->conn->pcb.ip != NULL) {
1067 *(msg->msg.ad.ipaddr) = (msg->msg.ad.local?msg->conn->pcb.ip->local_ip:msg->conn->pcb.ip->remote_ip);
1069 switch (NETCONNTYPE_GROUP(msg->conn->type)) {
1070 #if LWIP_RAW
1071 case NETCONN_RAW:
1072 if (msg->msg.ad.local) {
1073 *(msg->msg.ad.port) = msg->conn->pcb.raw->protocol;
1074 } else {
1075 /* return an error as connecting is only a helper for upper layers */
1076 msg->conn->err = ERR_CONN;
1078 break;
1079 #endif /* LWIP_RAW */
1080 #if LWIP_UDP
1081 case NETCONN_UDP:
1082 if (msg->msg.ad.local) {
1083 *(msg->msg.ad.port) = msg->conn->pcb.udp->local_port;
1084 } else {
1085 if ((msg->conn->pcb.udp->flags & UDP_FLAGS_CONNECTED) == 0) {
1086 msg->conn->err = ERR_CONN;
1087 } else {
1088 *(msg->msg.ad.port) = msg->conn->pcb.udp->remote_port;
1091 break;
1092 #endif /* LWIP_UDP */
1093 #if LWIP_TCP
1094 case NETCONN_TCP:
1095 *(msg->msg.ad.port) = (msg->msg.ad.local?msg->conn->pcb.tcp->local_port:msg->conn->pcb.tcp->remote_port);
1096 break;
1097 #endif /* LWIP_TCP */
1099 } else {
1100 msg->conn->err = ERR_CONN;
1102 TCPIP_APIMSG_ACK(msg);
1106 * Close a TCP pcb contained in a netconn
1107 * Called from netconn_close
1109 * @param msg the api_msg_msg pointing to the connection
1111 void
1112 do_close(struct api_msg_msg *msg)
1114 #if LWIP_TCP
1115 if ((msg->conn->pcb.tcp != NULL) && (msg->conn->type == NETCONN_TCP)) {
1116 msg->conn->state = NETCONN_CLOSE;
1117 do_close_internal(msg->conn);
1118 /* for tcp netconns, do_close_internal ACKs the message */
1119 } else
1120 #endif /* LWIP_TCP */
1122 msg->conn->err = ERR_VAL;
1123 TCPIP_APIMSG_ACK(msg);
1127 #if LWIP_IGMP
1129 * Join multicast groups for UDP netconns.
1130 * Called from netconn_join_leave_group
1132 * @param msg the api_msg_msg pointing to the connection
1134 void
1135 do_join_leave_group(struct api_msg_msg *msg)
1137 if (!ERR_IS_FATAL(msg->conn->err)) {
1138 if (msg->conn->pcb.tcp != NULL) {
1139 if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
1140 #if LWIP_UDP
1141 if (msg->msg.jl.join_or_leave == NETCONN_JOIN) {
1142 msg->conn->err = igmp_joingroup(msg->msg.jl.interface, msg->msg.jl.multiaddr);
1143 } else {
1144 msg->conn->err = igmp_leavegroup(msg->msg.jl.interface, msg->msg.jl.multiaddr);
1146 #endif /* LWIP_UDP */
1147 #if (LWIP_TCP || LWIP_RAW)
1148 } else {
1149 msg->conn->err = ERR_VAL;
1150 #endif /* (LWIP_TCP || LWIP_RAW) */
1154 TCPIP_APIMSG_ACK(msg);
1156 #endif /* LWIP_IGMP */
1158 #if LWIP_DNS
1160 * Callback function that is called when DNS name is resolved
1161 * (or on timeout). A waiting application thread is waked up by
1162 * signaling the semaphore.
1164 static void
1165 do_dns_found(const char *name, struct ip_addr *ipaddr, void *arg)
1167 struct dns_api_msg *msg = (struct dns_api_msg*)arg;
1169 LWIP_ASSERT("DNS response for wrong host name", strcmp(msg->name, name) == 0);
1171 if (ipaddr == NULL) {
1172 /* timeout or memory error */
1173 *msg->err = ERR_VAL;
1174 } else {
1175 /* address was resolved */
1176 *msg->err = ERR_OK;
1177 *msg->addr = *ipaddr;
1179 /* wake up the application task waiting in netconn_gethostbyname */
1180 sys_sem_signal(msg->sem);
1184 * Execute a DNS query
1185 * Called from netconn_gethostbyname
1187 * @param arg the dns_api_msg pointing to the query
1189 void
1190 do_gethostbyname(void *arg)
1192 struct dns_api_msg *msg = (struct dns_api_msg*)arg;
1194 *msg->err = dns_gethostbyname(msg->name, msg->addr, do_dns_found, msg);
1195 if (*msg->err != ERR_INPROGRESS) {
1196 /* on error or immediate success, wake up the application
1197 * task waiting in netconn_gethostbyname */
1198 sys_sem_signal(msg->sem);
1201 #endif /* LWIP_DNS */
1203 #endif /* LWIP_NETCONN */