3 * Sequential API Internal module
8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
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
33 * This file is part of the lwIP TCP/IP stack.
35 * Author: Adam Dunkels <adam@sics.se>
41 #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
43 #include "lwip/api_msg.h"
50 #include "lwip/memp.h"
51 #include "lwip/tcpip.h"
52 #include "lwip/igmp.h"
57 #define SET_NONBLOCKING_CONNECT(conn, val) do { if(val) { \
58 (conn)->flags |= NETCONN_FLAG_IN_NONBLOCKING_CONNECT; \
60 (conn)->flags &= ~ NETCONN_FLAG_IN_NONBLOCKING_CONNECT; }} while(0)
61 #define IN_NONBLOCKING_CONNECT(conn) (((conn)->flags & NETCONN_FLAG_IN_NONBLOCKING_CONNECT) != 0)
63 /* forward declarations */
65 static err_t do_writemore(struct netconn *conn);
66 static void do_close_internal(struct netconn *conn);
71 * Receive callback function for RAW netconns.
72 * Doesn't 'eat' the packet, only references it and sends it to
75 * @see raw.h (struct raw_pcb.recv) for parameters and return value
78 recv_raw(void *arg, struct raw_pcb *pcb, struct pbuf *p,
85 LWIP_UNUSED_ARG(addr);
86 conn = (struct netconn *)arg;
88 if ((conn != NULL) && sys_mbox_valid(&conn->recvmbox)) {
91 SYS_ARCH_GET(conn->recv_avail, recv_avail);
92 if ((recv_avail + (int)(p->tot_len)) > conn->recv_bufsize) {
95 #endif /* LWIP_SO_RCVBUF */
96 /* copy the whole packet into new pbufs */
97 q = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
99 if (pbuf_copy(q, p) != ERR_OK) {
107 buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
115 buf->addr = &(((struct ip_hdr*)(q->payload))->src);
116 buf->port = pcb->protocol;
119 if (sys_mbox_trypost(&conn->recvmbox, buf) != ERR_OK) {
123 SYS_ARCH_INC(conn->recv_avail, len);
124 /* Register event with callback */
125 API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
130 return 0; /* do not eat the packet */
136 * Receive callback function for UDP netconns.
137 * Posts the packet to conn->recvmbox or deletes it on memory error.
139 * @see udp.h (struct udp_pcb.recv) for parameters
142 recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p,
143 ip_addr_t *addr, u16_t port)
146 struct netconn *conn;
150 #endif /* LWIP_SO_RCVBUF */
152 LWIP_UNUSED_ARG(pcb); /* only used for asserts... */
153 LWIP_ASSERT("recv_udp must have a pcb argument", pcb != NULL);
154 LWIP_ASSERT("recv_udp must have an argument", arg != NULL);
155 conn = (struct netconn *)arg;
156 LWIP_ASSERT("recv_udp: recv for wrong pcb!", conn->pcb.udp == pcb);
159 SYS_ARCH_GET(conn->recv_avail, recv_avail);
160 if ((conn == NULL) || !sys_mbox_valid(&conn->recvmbox) ||
161 ((recv_avail + (int)(p->tot_len)) > conn->recv_bufsize)) {
162 #else /* LWIP_SO_RCVBUF */
163 if ((conn == NULL) || !sys_mbox_valid(&conn->recvmbox)) {
164 #endif /* LWIP_SO_RCVBUF */
169 buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
178 #if LWIP_NETBUF_RECVINFO
180 const struct ip_hdr* iphdr = ip_current_header();
181 /* get the UDP header - always in the first pbuf, ensured by udp_input */
182 const struct udp_hdr* udphdr = (void*)(((char*)iphdr) + IPH_LEN(iphdr));
183 #if LWIP_CHECKSUM_ON_COPY
184 buf->flags = NETBUF_FLAG_DESTADDR;
185 #endif /* LWIP_CHECKSUM_ON_COPY */
186 buf->toaddr = (ip_addr_t*)&iphdr->dest;
187 buf->toport_chksum = udphdr->dest;
189 #endif /* LWIP_NETBUF_RECVINFO */
193 if (sys_mbox_trypost(&conn->recvmbox, buf) != ERR_OK) {
197 SYS_ARCH_INC(conn->recv_avail, len);
198 /* Register event with callback */
199 API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
202 #endif /* LWIP_UDP */
206 * Receive callback function for TCP netconns.
207 * Posts the packet to conn->recvmbox, but doesn't delete it on errors.
209 * @see tcp.h (struct tcp_pcb.recv) for parameters and return value
212 recv_tcp(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
214 struct netconn *conn;
217 LWIP_UNUSED_ARG(pcb);
218 LWIP_ASSERT("recv_tcp must have a pcb argument", pcb != NULL);
219 LWIP_ASSERT("recv_tcp must have an argument", arg != NULL);
220 conn = (struct netconn *)arg;
221 LWIP_ASSERT("recv_tcp: recv for wrong pcb!", conn->pcb.tcp == pcb);
226 if (!sys_mbox_valid(&conn->recvmbox)) {
227 /* recvmbox already deleted */
229 tcp_recved(pcb, p->tot_len);
234 /* Unlike for UDP or RAW pcbs, don't check for available space
235 using recv_avail since that could break the connection
236 (data is already ACKed) */
238 /* don't overwrite fatal errors! */
239 NETCONN_SET_SAFE_ERR(conn, err);
247 if (sys_mbox_trypost(&conn->recvmbox, p) != ERR_OK) {
248 /* don't deallocate p: it is presented to us later again from tcp_fasttmr! */
251 SYS_ARCH_INC(conn->recv_avail, len);
252 /* Register event with callback */
253 API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
260 * Poll callback function for TCP netconns.
261 * Wakes up an application thread that waits for a connection to close
262 * or data to be sent. The application thread then takes the
263 * appropriate action to go on.
265 * Signals the conn->sem.
266 * netconn_close waits for conn->sem if closing failed.
268 * @see tcp.h (struct tcp_pcb.poll) for parameters and return value
271 poll_tcp(void *arg, struct tcp_pcb *pcb)
273 struct netconn *conn = (struct netconn *)arg;
275 LWIP_UNUSED_ARG(pcb);
276 LWIP_ASSERT("conn != NULL", (conn != NULL));
278 if (conn->state == NETCONN_WRITE) {
280 } else if (conn->state == NETCONN_CLOSE) {
281 do_close_internal(conn);
283 /* @todo: implement connect timeout here? */
285 /* Did a nonblocking write fail before? Then check available write-space. */
286 if (conn->flags & NETCONN_FLAG_CHECK_WRITESPACE) {
287 /* If the queued byte- or pbuf-count drops below the configured low-water limit,
288 let select mark this pcb as writable again. */
289 if ((conn->pcb.tcp != NULL) && (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT) &&
290 (tcp_sndqueuelen(conn->pcb.tcp) < TCP_SNDQUEUELOWAT)) {
291 conn->flags &= ~NETCONN_FLAG_CHECK_WRITESPACE;
292 API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
300 * Sent callback function for TCP netconns.
301 * Signals the conn->sem and calls API_EVENT.
302 * netconn_write waits for conn->sem if send buffer is low.
304 * @see tcp.h (struct tcp_pcb.sent) for parameters and return value
307 sent_tcp(void *arg, struct tcp_pcb *pcb, u16_t len)
309 struct netconn *conn = (struct netconn *)arg;
311 LWIP_UNUSED_ARG(pcb);
312 LWIP_ASSERT("conn != NULL", (conn != NULL));
314 if (conn->state == NETCONN_WRITE) {
316 } else if (conn->state == NETCONN_CLOSE) {
317 do_close_internal(conn);
321 /* If the queued byte- or pbuf-count drops below the configured low-water limit,
322 let select mark this pcb as writable again. */
323 if ((conn->pcb.tcp != NULL) && (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT) &&
324 (tcp_sndqueuelen(conn->pcb.tcp) < TCP_SNDQUEUELOWAT)) {
325 conn->flags &= ~NETCONN_FLAG_CHECK_WRITESPACE;
326 API_EVENT(conn, NETCONN_EVT_SENDPLUS, len);
334 * Error callback function for TCP netconns.
335 * Signals conn->sem, posts to all conn mboxes and calls API_EVENT.
336 * The application thread has then to decide what to do.
338 * @see tcp.h (struct tcp_pcb.err) for parameters
341 err_tcp(void *arg, err_t err)
343 struct netconn *conn;
344 enum netconn_state old_state;
345 SYS_ARCH_DECL_PROTECT(lev);
347 conn = (struct netconn *)arg;
348 LWIP_ASSERT("conn != NULL", (conn != NULL));
350 conn->pcb.tcp = NULL;
352 /* no check since this is always fatal! */
353 SYS_ARCH_PROTECT(lev);
354 conn->last_err = err;
355 SYS_ARCH_UNPROTECT(lev);
357 /* reset conn->state now before waking up other threads */
358 old_state = conn->state;
359 conn->state = NETCONN_NONE;
361 /* Notify the user layer about a connection error. Used to signal
363 API_EVENT(conn, NETCONN_EVT_ERROR, 0);
364 /* Try to release selects pending on 'read' or 'write', too.
365 They will get an error if they actually try to read or write. */
366 API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
367 API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
369 /* pass NULL-message to recvmbox to wake up pending recv */
370 if (sys_mbox_valid(&conn->recvmbox)) {
371 /* use trypost to prevent deadlock */
372 sys_mbox_trypost(&conn->recvmbox, NULL);
374 /* pass NULL-message to acceptmbox to wake up pending accept */
375 if (sys_mbox_valid(&conn->acceptmbox)) {
376 /* use trypost to preven deadlock */
377 sys_mbox_trypost(&conn->acceptmbox, NULL);
380 if ((old_state == NETCONN_WRITE) || (old_state == NETCONN_CLOSE) ||
381 (old_state == NETCONN_CONNECT)) {
382 /* calling do_writemore/do_close_internal is not necessary
383 since the pcb has already been deleted! */
384 int was_nonblocking_connect = IN_NONBLOCKING_CONNECT(conn);
385 SET_NONBLOCKING_CONNECT(conn, 0);
387 if (!was_nonblocking_connect) {
388 /* set error return code */
389 LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL);
390 conn->current_msg->err = err;
391 conn->current_msg = NULL;
392 /* wake up the waiting task */
393 sys_sem_signal(&conn->op_completed);
396 LWIP_ASSERT("conn->current_msg == NULL", conn->current_msg == NULL);
401 * Setup a tcp_pcb with the correct callback function pointers
402 * and their arguments.
404 * @param conn the TCP netconn to setup
407 setup_tcp(struct netconn *conn)
413 tcp_recv(pcb, recv_tcp);
414 tcp_sent(pcb, sent_tcp);
415 tcp_poll(pcb, poll_tcp, 4);
416 tcp_err(pcb, err_tcp);
420 * Accept callback function for TCP netconns.
421 * Allocates a new netconn and posts that to conn->acceptmbox.
423 * @see tcp.h (struct tcp_pcb_listen.accept) for parameters and return value
426 accept_function(void *arg, struct tcp_pcb *newpcb, err_t err)
428 struct netconn *newconn;
429 struct netconn *conn = (struct netconn *)arg;
431 LWIP_DEBUGF(API_MSG_DEBUG, ("accept_function: newpcb->tate: %s\n", tcp_debug_state_str(newpcb->state)));
433 if (!sys_mbox_valid(&conn->acceptmbox)) {
434 LWIP_DEBUGF(API_MSG_DEBUG, ("accept_function: acceptmbox already deleted\n"));
438 /* We have to set the callback here even though
439 * the new socket is unknown. conn->socket is marked as -1. */
440 newconn = netconn_alloc(conn->type, conn->callback);
441 if (newconn == NULL) {
444 newconn->pcb.tcp = newpcb;
446 /* no protection: when creating the pcb, the netconn is not yet known
447 to the application thread */
448 newconn->last_err = err;
450 if (sys_mbox_trypost(&conn->acceptmbox, newconn) != ERR_OK) {
451 /* When returning != ERR_OK, the pcb is aborted in tcp_process(),
452 so do nothing here! */
453 newconn->pcb.tcp = NULL;
454 /* no need to drain since we know the recvmbox is empty. */
455 sys_mbox_free(&newconn->recvmbox);
456 sys_mbox_set_invalid(&newconn->recvmbox);
457 netconn_free(newconn);
460 /* Register event with callback */
461 API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
466 #endif /* LWIP_TCP */
469 * Create a new pcb of a specific type.
470 * Called from do_newconn().
472 * @param msg the api_msg_msg describing the connection type
473 * @return msg->conn->err, but the return value is currently ignored
476 pcb_new(struct api_msg_msg *msg)
478 LWIP_ASSERT("pcb_new: pcb already allocated", msg->conn->pcb.tcp == NULL);
480 /* Allocate a PCB for this connection */
481 switch(NETCONNTYPE_GROUP(msg->conn->type)) {
484 msg->conn->pcb.raw = raw_new(msg->msg.n.proto);
485 if(msg->conn->pcb.raw == NULL) {
489 raw_recv(msg->conn->pcb.raw, recv_raw, msg->conn);
491 #endif /* LWIP_RAW */
494 msg->conn->pcb.udp = udp_new();
495 if(msg->conn->pcb.udp == NULL) {
500 if (msg->conn->type==NETCONN_UDPLITE) {
501 udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_UDPLITE);
503 #endif /* LWIP_UDPLITE */
504 if (msg->conn->type==NETCONN_UDPNOCHKSUM) {
505 udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_NOCHKSUM);
507 udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
509 #endif /* LWIP_UDP */
512 msg->conn->pcb.tcp = tcp_new();
513 if(msg->conn->pcb.tcp == NULL) {
517 setup_tcp(msg->conn);
519 #endif /* LWIP_TCP */
521 /* Unsupported netconn type, e.g. protocol disabled */
528 * Create a new pcb of a specific type inside a netconn.
529 * Called from netconn_new_with_proto_and_callback.
531 * @param msg the api_msg_msg describing the connection type
534 do_newconn(struct api_msg_msg *msg)
537 if(msg->conn->pcb.tcp == NULL) {
540 /* Else? This "new" connection already has a PCB allocated. */
541 /* Is this an error condition? Should it be deleted? */
542 /* We currently just are happy and return. */
544 TCPIP_APIMSG_ACK(msg);
548 * Create a new netconn (of a specific type) that has a callback function.
549 * The corresponding pcb is NOT created!
551 * @param t the type of 'connection' to create (@see enum netconn_type)
552 * @param proto the IP protocol for RAW IP pcbs
553 * @param callback a function to call on status changes (RX available, TX'ed)
554 * @return a newly allocated struct netconn or
555 * NULL on memory error
558 netconn_alloc(enum netconn_type t, netconn_callback callback)
560 struct netconn *conn;
563 conn = (struct netconn *)memp_malloc(MEMP_NETCONN);
568 conn->last_err = ERR_OK;
570 conn->pcb.tcp = NULL;
572 #if (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_UDP_RECVMBOX_SIZE) && \
573 (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_TCP_RECVMBOX_SIZE)
574 size = DEFAULT_RAW_RECVMBOX_SIZE;
576 switch(NETCONNTYPE_GROUP(t)) {
579 size = DEFAULT_RAW_RECVMBOX_SIZE;
581 #endif /* LWIP_RAW */
584 size = DEFAULT_UDP_RECVMBOX_SIZE;
586 #endif /* LWIP_UDP */
589 size = DEFAULT_TCP_RECVMBOX_SIZE;
591 #endif /* LWIP_TCP */
593 LWIP_ASSERT("netconn_alloc: undefined netconn_type", 0);
598 if (sys_sem_new(&conn->op_completed, 0) != ERR_OK) {
599 memp_free(MEMP_NETCONN, conn);
602 if (sys_mbox_new(&conn->recvmbox, size) != ERR_OK) {
603 sys_sem_free(&conn->op_completed);
604 memp_free(MEMP_NETCONN, conn);
609 sys_mbox_set_invalid(&conn->acceptmbox);
611 conn->state = NETCONN_NONE;
613 /* initialize socket to -1 since 0 is a valid socket */
615 #endif /* LWIP_SOCKET */
616 conn->callback = callback;
617 conn->recv_avail = 0;
619 conn->current_msg = NULL;
620 conn->write_offset = 0;
621 #endif /* LWIP_TCP */
623 conn->recv_timeout = 0;
624 #endif /* LWIP_SO_RCVTIMEO */
626 conn->recv_bufsize = RECV_BUFSIZE_DEFAULT;
627 #endif /* LWIP_SO_RCVBUF */
633 * Delete a netconn and all its resources.
634 * The pcb is NOT freed (since we might not be in the right thread context do this).
636 * @param conn the netconn to free
639 netconn_free(struct netconn *conn)
641 LWIP_ASSERT("PCB must be deallocated outside this function", conn->pcb.tcp == NULL);
642 LWIP_ASSERT("recvmbox must be deallocated before calling this function",
643 !sys_mbox_valid(&conn->recvmbox));
645 LWIP_ASSERT("acceptmbox must be deallocated before calling this function",
646 !sys_mbox_valid(&conn->acceptmbox));
647 #endif /* LWIP_TCP */
649 sys_sem_free(&conn->op_completed);
650 sys_sem_set_invalid(&conn->op_completed);
652 memp_free(MEMP_NETCONN, conn);
656 * Delete rcvmbox and acceptmbox of a netconn and free the left-over data in
659 * @param conn the netconn to free
660 * @bytes_drained bytes drained from recvmbox
661 * @accepts_drained pending connections drained from acceptmbox
664 netconn_drain(struct netconn *conn)
669 #endif /* LWIP_TCP */
671 /* This runs in tcpip_thread, so we don't need to lock against rx packets */
673 /* Delete and drain the recvmbox. */
674 if (sys_mbox_valid(&conn->recvmbox)) {
675 while (sys_mbox_tryfetch(&conn->recvmbox, &mem) != SYS_MBOX_EMPTY) {
677 if (conn->type == NETCONN_TCP) {
679 p = (struct pbuf*)mem;
680 /* pcb might be set to NULL already by err_tcp() */
681 if (conn->pcb.tcp != NULL) {
682 tcp_recved(conn->pcb.tcp, p->tot_len);
687 #endif /* LWIP_TCP */
689 netbuf_delete((struct netbuf *)mem);
692 sys_mbox_free(&conn->recvmbox);
693 sys_mbox_set_invalid(&conn->recvmbox);
696 /* Delete and drain the acceptmbox. */
698 if (sys_mbox_valid(&conn->acceptmbox)) {
699 while (sys_mbox_tryfetch(&conn->acceptmbox, &mem) != SYS_MBOX_EMPTY) {
700 struct netconn *newconn = (struct netconn *)mem;
701 /* Only tcp pcbs have an acceptmbox, so no need to check conn->type */
702 /* pcb might be set to NULL already by err_tcp() */
703 if (conn->pcb.tcp != NULL) {
704 tcp_accepted(conn->pcb.tcp);
707 netconn_drain(newconn);
708 if (newconn->pcb.tcp != NULL) {
709 tcp_abort(newconn->pcb.tcp);
710 newconn->pcb.tcp = NULL;
712 netconn_free(newconn);
714 sys_mbox_free(&conn->acceptmbox);
715 sys_mbox_set_invalid(&conn->acceptmbox);
717 #endif /* LWIP_TCP */
722 * Internal helper function to close a TCP netconn: since this sometimes
723 * doesn't work at the first attempt, this function is called from multiple
726 * @param conn the TCP netconn to close
729 do_close_internal(struct netconn *conn)
733 LWIP_ASSERT("invalid conn", (conn != NULL));
734 LWIP_ASSERT("this is for tcp netconns only", (conn->type == NETCONN_TCP));
735 LWIP_ASSERT("conn must be in state NETCONN_CLOSE", (conn->state == NETCONN_CLOSE));
736 LWIP_ASSERT("pcb already closed", (conn->pcb.tcp != NULL));
737 LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL);
739 /* Set back some callback pointers */
740 tcp_arg(conn->pcb.tcp, NULL);
741 if (conn->pcb.tcp->state == LISTEN) {
742 tcp_accept(conn->pcb.tcp, NULL);
744 tcp_recv(conn->pcb.tcp, NULL);
745 tcp_accept(conn->pcb.tcp, NULL);
746 /* some callbacks have to be reset if tcp_close is not successful */
747 tcp_sent(conn->pcb.tcp, NULL);
748 tcp_poll(conn->pcb.tcp, NULL, 4);
749 tcp_err(conn->pcb.tcp, NULL);
751 /* Try to close the connection */
752 err = tcp_close(conn->pcb.tcp);
754 /* Closing succeeded */
755 conn->current_msg->err = ERR_OK;
756 conn->current_msg = NULL;
757 conn->state = NETCONN_NONE;
758 /* Set back some callback pointers as conn is going away */
759 conn->pcb.tcp = NULL;
760 /* Trigger select() in socket layer. Make sure everybody notices activity
761 on the connection, error first! */
762 API_EVENT(conn, NETCONN_EVT_ERROR, 0);
763 API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
764 API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
765 /* wake up the application task */
766 sys_sem_signal(&conn->op_completed);
768 /* Closing failed, restore some of the callbacks */
769 /* Closing of listen pcb will never fail! */
770 LWIP_ASSERT("Closing a listen pcb may not fail!", (conn->pcb.tcp->state != LISTEN));
771 tcp_sent(conn->pcb.tcp, sent_tcp);
772 tcp_poll(conn->pcb.tcp, poll_tcp, 4);
773 tcp_err(conn->pcb.tcp, err_tcp);
774 tcp_arg(conn->pcb.tcp, conn);
776 /* If closing didn't succeed, we get called again either
777 from poll_tcp or from sent_tcp */
779 #endif /* LWIP_TCP */
782 * Delete the pcb inside a netconn.
783 * Called from netconn_delete.
785 * @param msg the api_msg_msg pointing to the connection
788 do_delconn(struct api_msg_msg *msg)
790 /* @todo TCP: abort running write/connect? */
791 if ((msg->conn->state != NETCONN_NONE) &&
792 (msg->conn->state != NETCONN_LISTEN) &&
793 (msg->conn->state != NETCONN_CONNECT)) {
794 /* this only happens for TCP netconns */
795 LWIP_ASSERT("msg->conn->type == NETCONN_TCP", msg->conn->type == NETCONN_TCP);
796 msg->err = ERR_INPROGRESS;
798 LWIP_ASSERT("blocking connect in progress",
799 (msg->conn->state != NETCONN_CONNECT) || IN_NONBLOCKING_CONNECT(msg->conn));
800 /* Drain and delete mboxes */
801 netconn_drain(msg->conn);
803 if (msg->conn->pcb.tcp != NULL) {
805 switch (NETCONNTYPE_GROUP(msg->conn->type)) {
808 raw_remove(msg->conn->pcb.raw);
810 #endif /* LWIP_RAW */
813 msg->conn->pcb.udp->recv_arg = NULL;
814 udp_remove(msg->conn->pcb.udp);
816 #endif /* LWIP_UDP */
819 LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL &&
820 msg->conn->write_offset == 0);
821 msg->conn->state = NETCONN_CLOSE;
822 msg->conn->current_msg = msg;
823 do_close_internal(msg->conn);
824 /* API_EVENT is called inside do_close_internal, before releasing
825 the application thread, so we can return at this point! */
827 #endif /* LWIP_TCP */
831 msg->conn->pcb.tcp = NULL;
833 /* tcp netconns don't come here! */
835 /* @todo: this lets select make the socket readable and writable,
836 which is wrong! errfd instead? */
837 API_EVENT(msg->conn, NETCONN_EVT_RCVPLUS, 0);
838 API_EVENT(msg->conn, NETCONN_EVT_SENDPLUS, 0);
840 if (sys_sem_valid(&msg->conn->op_completed)) {
841 sys_sem_signal(&msg->conn->op_completed);
846 * Bind a pcb contained in a netconn
847 * Called from netconn_bind.
849 * @param msg the api_msg_msg pointing to the connection and containing
850 * the IP address and port to bind to
853 do_bind(struct api_msg_msg *msg)
855 if (ERR_IS_FATAL(msg->conn->last_err)) {
856 msg->err = msg->conn->last_err;
859 if (msg->conn->pcb.tcp != NULL) {
860 switch (NETCONNTYPE_GROUP(msg->conn->type)) {
863 msg->err = raw_bind(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
865 #endif /* LWIP_RAW */
868 msg->err = udp_bind(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
870 #endif /* LWIP_UDP */
873 msg->err = tcp_bind(msg->conn->pcb.tcp, msg->msg.bc.ipaddr, msg->msg.bc.port);
875 #endif /* LWIP_TCP */
881 TCPIP_APIMSG_ACK(msg);
886 * TCP callback function if a connection (opened by tcp_connect/do_connect) has
887 * been established (or reset by the remote host).
889 * @see tcp.h (struct tcp_pcb.connected) for parameters and return values
892 do_connected(void *arg, struct tcp_pcb *pcb, err_t err)
894 struct netconn *conn;
897 LWIP_UNUSED_ARG(pcb);
899 conn = (struct netconn *)arg;
905 LWIP_ASSERT("conn->state == NETCONN_CONNECT", conn->state == NETCONN_CONNECT);
906 LWIP_ASSERT("(conn->current_msg != NULL) || conn->in_non_blocking_connect",
907 (conn->current_msg != NULL) || IN_NONBLOCKING_CONNECT(conn));
909 if (conn->current_msg != NULL) {
910 conn->current_msg->err = err;
912 if ((conn->type == NETCONN_TCP) && (err == ERR_OK)) {
915 was_blocking = !IN_NONBLOCKING_CONNECT(conn);
916 SET_NONBLOCKING_CONNECT(conn, 0);
917 conn->current_msg = NULL;
918 conn->state = NETCONN_NONE;
920 SYS_ARCH_DECL_PROTECT(lev);
921 SYS_ARCH_PROTECT(lev);
922 if (conn->last_err == ERR_INPROGRESS) {
923 conn->last_err = ERR_OK;
925 SYS_ARCH_UNPROTECT(lev);
927 API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
930 sys_sem_signal(&conn->op_completed);
934 #endif /* LWIP_TCP */
937 * Connect a pcb contained inside a netconn
938 * Called from netconn_connect.
940 * @param msg the api_msg_msg pointing to the connection and containing
941 * the IP address and port to connect to
944 do_connect(struct api_msg_msg *msg)
946 if (msg->conn->pcb.tcp == NULL) {
947 /* This may happen when calling netconn_connect() a second time */
950 switch (NETCONNTYPE_GROUP(msg->conn->type)) {
953 msg->err = raw_connect(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
955 #endif /* LWIP_RAW */
958 msg->err = udp_connect(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
960 #endif /* LWIP_UDP */
963 /* Prevent connect while doing any other action. */
964 if (msg->conn->state != NETCONN_NONE) {
965 msg->err = ERR_ISCONN;
967 setup_tcp(msg->conn);
968 msg->err = tcp_connect(msg->conn->pcb.tcp, msg->msg.bc.ipaddr,
969 msg->msg.bc.port, do_connected);
970 if (msg->err == ERR_OK) {
971 u8_t non_blocking = netconn_is_nonblocking(msg->conn);
972 msg->conn->state = NETCONN_CONNECT;
973 SET_NONBLOCKING_CONNECT(msg->conn, non_blocking);
975 msg->err = ERR_INPROGRESS;
977 msg->conn->current_msg = msg;
978 /* sys_sem_signal() is called from do_connected (or err_tcp()),
979 * when the connection is established! */
985 #endif /* LWIP_TCP */
987 LWIP_ERROR("Invalid netconn type", 0, do{ msg->err = ERR_VAL; }while(0));
991 sys_sem_signal(&msg->conn->op_completed);
995 * Connect a pcb contained inside a netconn
996 * Only used for UDP netconns.
997 * Called from netconn_disconnect.
999 * @param msg the api_msg_msg pointing to the connection to disconnect
1002 do_disconnect(struct api_msg_msg *msg)
1005 if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
1006 udp_disconnect(msg->conn->pcb.udp);
1009 #endif /* LWIP_UDP */
1013 TCPIP_APIMSG_ACK(msg);
1017 * Set a TCP pcb contained in a netconn into listen mode
1018 * Called from netconn_listen.
1020 * @param msg the api_msg_msg pointing to the connection
1023 do_listen(struct api_msg_msg *msg)
1026 if (ERR_IS_FATAL(msg->conn->last_err)) {
1027 msg->err = msg->conn->last_err;
1029 msg->err = ERR_CONN;
1030 if (msg->conn->pcb.tcp != NULL) {
1031 if (msg->conn->type == NETCONN_TCP) {
1032 if (msg->conn->state == NETCONN_NONE) {
1033 #if TCP_LISTEN_BACKLOG
1034 struct tcp_pcb* lpcb = tcp_listen_with_backlog(msg->conn->pcb.tcp, msg->msg.lb.backlog);
1035 #else /* TCP_LISTEN_BACKLOG */
1036 struct tcp_pcb* lpcb = tcp_listen(msg->conn->pcb.tcp);
1037 #endif /* TCP_LISTEN_BACKLOG */
1039 /* in this case, the old pcb is still allocated */
1042 /* delete the recvmbox and allocate the acceptmbox */
1043 if (sys_mbox_valid(&msg->conn->recvmbox)) {
1044 /** @todo: should we drain the recvmbox here? */
1045 sys_mbox_free(&msg->conn->recvmbox);
1046 sys_mbox_set_invalid(&msg->conn->recvmbox);
1049 if (!sys_mbox_valid(&msg->conn->acceptmbox)) {
1050 msg->err = sys_mbox_new(&msg->conn->acceptmbox, DEFAULT_ACCEPTMBOX_SIZE);
1052 if (msg->err == ERR_OK) {
1053 msg->conn->state = NETCONN_LISTEN;
1054 msg->conn->pcb.tcp = lpcb;
1055 tcp_arg(msg->conn->pcb.tcp, msg->conn);
1056 tcp_accept(msg->conn->pcb.tcp, accept_function);
1058 /* since the old pcb is already deallocated, free lpcb now */
1060 msg->conn->pcb.tcp = NULL;
1067 #endif /* LWIP_TCP */
1068 TCPIP_APIMSG_ACK(msg);
1072 * Send some data on a RAW or UDP pcb contained in a netconn
1073 * Called from netconn_send
1075 * @param msg the api_msg_msg pointing to the connection
1078 do_send(struct api_msg_msg *msg)
1080 if (ERR_IS_FATAL(msg->conn->last_err)) {
1081 msg->err = msg->conn->last_err;
1083 msg->err = ERR_CONN;
1084 if (msg->conn->pcb.tcp != NULL) {
1085 switch (NETCONNTYPE_GROUP(msg->conn->type)) {
1088 if (msg->msg.b->addr == NULL) {
1089 msg->err = raw_send(msg->conn->pcb.raw, msg->msg.b->p);
1091 msg->err = raw_sendto(msg->conn->pcb.raw, msg->msg.b->p, msg->msg.b->addr);
1097 #if LWIP_CHECKSUM_ON_COPY
1098 if (msg->msg.b->addr == NULL) {
1099 msg->err = udp_send_chksum(msg->conn->pcb.udp, msg->msg.b->p,
1100 msg->msg.b->flags & NETBUF_FLAG_CHKSUM, msg->msg.b->toport_chksum);
1102 msg->err = udp_sendto_chksum(msg->conn->pcb.udp, msg->msg.b->p,
1103 msg->msg.b->addr, msg->msg.b->port,
1104 msg->msg.b->flags & NETBUF_FLAG_CHKSUM, msg->msg.b->toport_chksum);
1106 #else /* LWIP_CHECKSUM_ON_COPY */
1107 if (msg->msg.b->addr == NULL) {
1108 msg->err = udp_send(msg->conn->pcb.udp, msg->msg.b->p);
1110 msg->err = udp_sendto(msg->conn->pcb.udp, msg->msg.b->p, msg->msg.b->addr, msg->msg.b->port);
1112 #endif /* LWIP_CHECKSUM_ON_COPY */
1114 #endif /* LWIP_UDP */
1120 TCPIP_APIMSG_ACK(msg);
1124 * Indicate data has been received from a TCP pcb contained in a netconn
1125 * Called from netconn_recv
1127 * @param msg the api_msg_msg pointing to the connection
1130 do_recv(struct api_msg_msg *msg)
1134 if (msg->conn->pcb.tcp != NULL) {
1135 if (msg->conn->type == NETCONN_TCP) {
1136 #if TCP_LISTEN_BACKLOG
1137 if (msg->conn->pcb.tcp->state == LISTEN) {
1138 tcp_accepted(msg->conn->pcb.tcp);
1140 #endif /* TCP_LISTEN_BACKLOG */
1142 u32_t remaining = msg->msg.r.len;
1144 u16_t recved = (remaining > 0xffff) ? 0xffff : (u16_t)remaining;
1145 tcp_recved(msg->conn->pcb.tcp, recved);
1146 remaining -= recved;
1147 }while(remaining != 0);
1151 #endif /* LWIP_TCP */
1152 TCPIP_APIMSG_ACK(msg);
1157 * See if more data needs to be written from a previous call to netconn_write.
1158 * Called initially from do_write. If the first call can't send all data
1159 * (because of low memory or empty send-buffer), this function is called again
1160 * from sent_tcp() or poll_tcp() to send more data. If all data is sent, the
1161 * blocking application thread (waiting in netconn_write) is released.
1163 * @param conn netconn (that is currently in state NETCONN_WRITE) to process
1165 * ERR_MEM if LWIP_TCPIP_CORE_LOCKING=1 and sending hasn't yet finished
1168 do_writemore(struct netconn *conn)
1172 u16_t len, available;
1173 u8_t write_finished = 0;
1175 u8_t dontblock = netconn_is_nonblocking(conn) ||
1176 (conn->current_msg->msg.w.apiflags & NETCONN_DONTBLOCK);
1177 u8_t apiflags = conn->current_msg->msg.w.apiflags;
1179 LWIP_ASSERT("conn != NULL", conn != NULL);
1180 LWIP_ASSERT("conn->state == NETCONN_WRITE", (conn->state == NETCONN_WRITE));
1181 LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL);
1182 LWIP_ASSERT("conn->pcb.tcp != NULL", conn->pcb.tcp != NULL);
1183 LWIP_ASSERT("conn->write_offset < conn->current_msg->msg.w.len",
1184 conn->write_offset < conn->current_msg->msg.w.len);
1186 dataptr = (u8_t*)conn->current_msg->msg.w.dataptr + conn->write_offset;
1187 diff = conn->current_msg->msg.w.len - conn->write_offset;
1188 if (diff > 0xffffUL) { /* max_u16_t */
1190 #if LWIP_TCPIP_CORE_LOCKING
1191 conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
1193 apiflags |= TCP_WRITE_FLAG_MORE;
1197 available = tcp_sndbuf(conn->pcb.tcp);
1198 if (available < len) {
1199 /* don't try to write more than sendbuf */
1201 #if LWIP_TCPIP_CORE_LOCKING
1202 conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
1204 apiflags |= TCP_WRITE_FLAG_MORE;
1206 if (dontblock && (len < conn->current_msg->msg.w.len)) {
1207 /* failed to send all data at once -> nonblocking write not possible */
1210 if (err == ERR_OK) {
1211 LWIP_ASSERT("do_writemore: invalid length!", ((conn->write_offset + len) <= conn->current_msg->msg.w.len));
1212 err = tcp_write(conn->pcb.tcp, dataptr, len, apiflags);
1214 if (dontblock && (err == ERR_MEM)) {
1215 /* nonblocking write failed */
1217 err = ERR_WOULDBLOCK;
1218 /* let poll_tcp check writable space to mark the pcb
1220 conn->flags |= NETCONN_FLAG_CHECK_WRITESPACE;
1221 /* let select mark this pcb as non-writable. */
1222 API_EVENT(conn, NETCONN_EVT_SENDMINUS, len);
1224 /* if OK or memory error, check available space */
1225 if (((err == ERR_OK) || (err == ERR_MEM)) &&
1226 ((tcp_sndbuf(conn->pcb.tcp) <= TCP_SNDLOWAT) ||
1227 (tcp_sndqueuelen(conn->pcb.tcp) >= TCP_SNDQUEUELOWAT))) {
1228 /* The queued byte- or pbuf-count exceeds the configured low-water limit,
1229 let select mark this pcb as non-writable. */
1230 API_EVENT(conn, NETCONN_EVT_SENDMINUS, len);
1233 if (err == ERR_OK) {
1234 conn->write_offset += len;
1235 if (conn->write_offset == conn->current_msg->msg.w.len) {
1236 /* everything was written */
1238 conn->write_offset = 0;
1240 tcp_output(conn->pcb.tcp);
1241 } else if (err == ERR_MEM) {
1242 /* If ERR_MEM, we wait for sent_tcp or poll_tcp to be called
1243 we do NOT return to the application thread, since ERR_MEM is
1244 only a temporary error! */
1246 /* tcp_write returned ERR_MEM, try tcp_output anyway */
1247 tcp_output(conn->pcb.tcp);
1249 #if LWIP_TCPIP_CORE_LOCKING
1250 conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
1253 /* On errors != ERR_MEM, we don't try writing any more but return
1254 the error to the application thread. */
1259 if (write_finished) {
1260 /* everything was written: set back connection state
1261 and back to application task */
1262 conn->current_msg->err = err;
1263 conn->current_msg = NULL;
1264 conn->state = NETCONN_NONE;
1265 #if LWIP_TCPIP_CORE_LOCKING
1266 if ((conn->flags & NETCONN_FLAG_WRITE_DELAYED) != 0)
1269 sys_sem_signal(&conn->op_completed);
1272 #if LWIP_TCPIP_CORE_LOCKING
1278 #endif /* LWIP_TCP */
1281 * Send some data on a TCP pcb contained in a netconn
1282 * Called from netconn_write
1284 * @param msg the api_msg_msg pointing to the connection
1287 do_write(struct api_msg_msg *msg)
1289 if (ERR_IS_FATAL(msg->conn->last_err)) {
1290 msg->err = msg->conn->last_err;
1292 if (msg->conn->type == NETCONN_TCP) {
1294 if (msg->conn->state != NETCONN_NONE) {
1295 /* netconn is connecting, closing or in blocking write */
1296 msg->err = ERR_INPROGRESS;
1297 } else if (msg->conn->pcb.tcp != NULL) {
1298 msg->conn->state = NETCONN_WRITE;
1299 /* set all the variables used by do_writemore */
1300 LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL &&
1301 msg->conn->write_offset == 0);
1302 LWIP_ASSERT("msg->msg.w.len != 0", msg->msg.w.len != 0);
1303 msg->conn->current_msg = msg;
1304 msg->conn->write_offset = 0;
1305 #if LWIP_TCPIP_CORE_LOCKING
1306 msg->conn->flags &= ~NETCONN_FLAG_WRITE_DELAYED;
1307 if (do_writemore(msg->conn) != ERR_OK) {
1308 LWIP_ASSERT("state!", msg->conn->state == NETCONN_WRITE);
1309 UNLOCK_TCPIP_CORE();
1310 sys_arch_sem_wait(&msg->conn->op_completed, 0);
1312 LWIP_ASSERT("state!", msg->conn->state == NETCONN_NONE);
1314 #else /* LWIP_TCPIP_CORE_LOCKING */
1315 do_writemore(msg->conn);
1316 #endif /* LWIP_TCPIP_CORE_LOCKING */
1317 /* for both cases: if do_writemore was called, don't ACK the APIMSG
1318 since do_writemore ACKs it! */
1321 msg->err = ERR_CONN;
1323 #else /* LWIP_TCP */
1325 #endif /* LWIP_TCP */
1326 #if (LWIP_UDP || LWIP_RAW)
1329 #endif /* (LWIP_UDP || LWIP_RAW) */
1332 TCPIP_APIMSG_ACK(msg);
1336 * Return a connection's local or remote address
1337 * Called from netconn_getaddr
1339 * @param msg the api_msg_msg pointing to the connection
1342 do_getaddr(struct api_msg_msg *msg)
1344 if (msg->conn->pcb.ip != NULL) {
1345 *(msg->msg.ad.ipaddr) = (msg->msg.ad.local ? msg->conn->pcb.ip->local_ip :
1346 msg->conn->pcb.ip->remote_ip);
1349 switch (NETCONNTYPE_GROUP(msg->conn->type)) {
1352 if (msg->msg.ad.local) {
1353 *(msg->msg.ad.port) = msg->conn->pcb.raw->protocol;
1355 /* return an error as connecting is only a helper for upper layers */
1356 msg->err = ERR_CONN;
1359 #endif /* LWIP_RAW */
1362 if (msg->msg.ad.local) {
1363 *(msg->msg.ad.port) = msg->conn->pcb.udp->local_port;
1365 if ((msg->conn->pcb.udp->flags & UDP_FLAGS_CONNECTED) == 0) {
1366 msg->err = ERR_CONN;
1368 *(msg->msg.ad.port) = msg->conn->pcb.udp->remote_port;
1372 #endif /* LWIP_UDP */
1375 *(msg->msg.ad.port) = (msg->msg.ad.local?msg->conn->pcb.tcp->local_port:msg->conn->pcb.tcp->remote_port);
1377 #endif /* LWIP_TCP */
1379 LWIP_ASSERT("invalid netconn_type", 0);
1383 msg->err = ERR_CONN;
1385 TCPIP_APIMSG_ACK(msg);
1389 * Close a TCP pcb contained in a netconn
1390 * Called from netconn_close
1392 * @param msg the api_msg_msg pointing to the connection
1395 do_close(struct api_msg_msg *msg)
1398 /* @todo: abort running write/connect? */
1399 if ((msg->conn->state != NETCONN_NONE) && (msg->conn->state != NETCONN_LISTEN)) {
1400 /* this only happens for TCP netconns */
1401 LWIP_ASSERT("msg->conn->type == NETCONN_TCP", msg->conn->type == NETCONN_TCP);
1402 msg->err = ERR_INPROGRESS;
1403 } else if ((msg->conn->pcb.tcp != NULL) && (msg->conn->type == NETCONN_TCP)) {
1404 /* Drain and delete mboxes */
1405 netconn_drain(msg->conn);
1406 LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL &&
1407 msg->conn->write_offset == 0);
1408 msg->conn->state = NETCONN_CLOSE;
1409 msg->conn->current_msg = msg;
1410 do_close_internal(msg->conn);
1411 /* for tcp netconns, do_close_internal ACKs the message */
1414 #endif /* LWIP_TCP */
1418 sys_sem_signal(&msg->conn->op_completed);
1423 * Join multicast groups for UDP netconns.
1424 * Called from netconn_join_leave_group
1426 * @param msg the api_msg_msg pointing to the connection
1429 do_join_leave_group(struct api_msg_msg *msg)
1431 if (ERR_IS_FATAL(msg->conn->last_err)) {
1432 msg->err = msg->conn->last_err;
1434 if (msg->conn->pcb.tcp != NULL) {
1435 if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
1437 if (msg->msg.jl.join_or_leave == NETCONN_JOIN) {
1438 msg->err = igmp_joingroup(msg->msg.jl.netif_addr, msg->msg.jl.multiaddr);
1440 msg->err = igmp_leavegroup(msg->msg.jl.netif_addr, msg->msg.jl.multiaddr);
1442 #endif /* LWIP_UDP */
1443 #if (LWIP_TCP || LWIP_RAW)
1446 #endif /* (LWIP_TCP || LWIP_RAW) */
1449 msg->err = ERR_CONN;
1452 TCPIP_APIMSG_ACK(msg);
1454 #endif /* LWIP_IGMP */
1458 * Callback function that is called when DNS name is resolved
1459 * (or on timeout). A waiting application thread is waked up by
1460 * signaling the semaphore.
1463 do_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
1465 struct dns_api_msg *msg = (struct dns_api_msg*)arg;
1467 LWIP_ASSERT("DNS response for wrong host name", strcmp(msg->name, name) == 0);
1468 LWIP_UNUSED_ARG(name);
1470 if (ipaddr == NULL) {
1471 /* timeout or memory error */
1472 *msg->err = ERR_VAL;
1474 /* address was resolved */
1476 *msg->addr = *ipaddr;
1478 /* wake up the application task waiting in netconn_gethostbyname */
1479 sys_sem_signal(msg->sem);
1483 * Execute a DNS query
1484 * Called from netconn_gethostbyname
1486 * @param arg the dns_api_msg pointing to the query
1489 do_gethostbyname(void *arg)
1491 struct dns_api_msg *msg = (struct dns_api_msg*)arg;
1493 *msg->err = dns_gethostbyname(msg->name, msg->addr, do_dns_found, msg);
1494 if (*msg->err != ERR_INPROGRESS) {
1495 /* on error or immediate success, wake up the application
1496 * task waiting in netconn_gethostbyname */
1497 sys_sem_signal(msg->sem);
1500 #endif /* LWIP_DNS */
1502 #endif /* LWIP_NETCONN */