worldstone: add -s for statistical profiling
[minix.git] / lib / liblwip / api / api_msg.c
blob366f5d9a2afb6e743d615725eb8809702c3a934f
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 #include <string.h>
57 #define SET_NONBLOCKING_CONNECT(conn, val) do { if(val) { \
58 (conn)->flags |= NETCONN_FLAG_IN_NONBLOCKING_CONNECT; \
59 } else { \
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 */
64 #if LWIP_TCP
65 static err_t do_writemore(struct netconn *conn);
66 static void do_close_internal(struct netconn *conn);
67 #endif
69 #if LWIP_RAW
70 /**
71 * Receive callback function for RAW netconns.
72 * Doesn't 'eat' the packet, only references it and sends it to
73 * conn->recvmbox
75 * @see raw.h (struct raw_pcb.recv) for parameters and return value
77 static u8_t
78 recv_raw(void *arg, struct raw_pcb *pcb, struct pbuf *p,
79 ip_addr_t *addr)
81 struct pbuf *q;
82 struct netbuf *buf;
83 struct netconn *conn;
85 LWIP_UNUSED_ARG(addr);
86 conn = (struct netconn *)arg;
88 if ((conn != NULL) && sys_mbox_valid(&conn->recvmbox)) {
89 #if LWIP_SO_RCVBUF
90 int recv_avail;
91 SYS_ARCH_GET(conn->recv_avail, recv_avail);
92 if ((recv_avail + (int)(p->tot_len)) > conn->recv_bufsize) {
93 return 0;
95 #endif /* LWIP_SO_RCVBUF */
96 /* copy the whole packet into new pbufs */
97 q = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
98 if(q != NULL) {
99 if (pbuf_copy(q, p) != ERR_OK) {
100 pbuf_free(q);
101 q = NULL;
105 if (q != NULL) {
106 u16_t len;
107 buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
108 if (buf == NULL) {
109 pbuf_free(q);
110 return 0;
113 buf->p = q;
114 buf->ptr = q;
115 ip_addr_copy(buf->addr, *ip_current_src_addr());
116 buf->port = pcb->protocol;
118 len = q->tot_len;
119 if (sys_mbox_trypost(&conn->recvmbox, buf) != ERR_OK) {
120 netbuf_delete(buf);
121 return 0;
122 } else {
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 */
132 #endif /* LWIP_RAW*/
134 #if LWIP_UDP
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
141 static void
142 recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p,
143 ip_addr_t *addr, u16_t port)
145 struct netbuf *buf;
146 struct netconn *conn;
147 u16_t len;
148 #if LWIP_SO_RCVBUF
149 int recv_avail;
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);
158 #if LWIP_SO_RCVBUF
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 */
165 pbuf_free(p);
166 return;
169 buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
170 if (buf == NULL) {
171 pbuf_free(p);
172 return;
173 } else {
174 buf->p = p;
175 buf->ptr = p;
176 ip_addr_set(&buf->addr, addr);
177 buf->port = port;
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 ip_addr_set(&buf->toaddr, ip_current_dest_addr());
187 buf->toport_chksum = udphdr->dest;
189 #endif /* LWIP_NETBUF_RECVINFO */
192 len = p->tot_len;
193 if (sys_mbox_trypost(&conn->recvmbox, buf) != ERR_OK) {
194 netbuf_delete(buf);
195 return;
196 } else {
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 */
204 #if LWIP_TCP
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
211 static err_t
212 recv_tcp(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
214 struct netconn *conn;
215 u16_t len;
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);
223 if (conn == NULL) {
224 return ERR_VAL;
226 if (!sys_mbox_valid(&conn->recvmbox)) {
227 /* recvmbox already deleted */
228 if (p != NULL) {
229 tcp_recved(pcb, p->tot_len);
230 pbuf_free(p);
232 return ERR_OK;
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);
241 if (p != NULL) {
242 len = p->tot_len;
243 } else {
244 len = 0;
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! */
249 return ERR_MEM;
250 } else {
251 SYS_ARCH_INC(conn->recv_avail, len);
252 /* Register event with callback */
253 API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
256 return ERR_OK;
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
270 static err_t
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) {
279 do_writemore(conn);
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);
296 return ERR_OK;
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
306 static err_t
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) {
315 do_writemore(conn);
316 } else if (conn->state == NETCONN_CLOSE) {
317 do_close_internal(conn);
320 if (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);
330 return ERR_OK;
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
340 static void
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
362 select. */
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);
395 } else {
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
406 static void
407 setup_tcp(struct netconn *conn)
409 struct tcp_pcb *pcb;
411 pcb = conn->pcb.tcp;
412 tcp_arg(pcb, 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
425 static err_t
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"));
435 return ERR_VAL;
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) {
442 return ERR_MEM;
444 newconn->pcb.tcp = newpcb;
445 setup_tcp(newconn);
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);
458 return ERR_MEM;
459 } else {
460 /* Register event with callback */
461 API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
464 return ERR_OK;
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
475 static void
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)) {
482 #if LWIP_RAW
483 case NETCONN_RAW:
484 msg->conn->pcb.raw = raw_new(msg->msg.n.proto);
485 if(msg->conn->pcb.raw == NULL) {
486 msg->err = ERR_MEM;
487 break;
489 raw_recv(msg->conn->pcb.raw, recv_raw, msg->conn);
490 break;
491 #endif /* LWIP_RAW */
492 #if LWIP_UDP
493 case NETCONN_UDP:
494 msg->conn->pcb.udp = udp_new();
495 if(msg->conn->pcb.udp == NULL) {
496 msg->err = ERR_MEM;
497 break;
499 #if LWIP_UDPLITE
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);
508 break;
509 #endif /* LWIP_UDP */
510 #if LWIP_TCP
511 case NETCONN_TCP:
512 msg->conn->pcb.tcp = tcp_new();
513 if(msg->conn->pcb.tcp == NULL) {
514 msg->err = ERR_MEM;
515 break;
517 setup_tcp(msg->conn);
518 break;
519 #endif /* LWIP_TCP */
520 default:
521 /* Unsupported netconn type, e.g. protocol disabled */
522 msg->err = ERR_VAL;
523 break;
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
533 void
534 do_newconn(struct api_msg_msg *msg)
536 msg->err = ERR_OK;
537 if(msg->conn->pcb.tcp == NULL) {
538 pcb_new(msg);
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
557 struct netconn*
558 netconn_alloc(enum netconn_type t, netconn_callback callback)
560 struct netconn *conn;
561 int size;
563 conn = (struct netconn *)memp_malloc(MEMP_NETCONN);
564 if (conn == NULL) {
565 return NULL;
568 conn->last_err = ERR_OK;
569 conn->type = t;
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;
575 #else
576 switch(NETCONNTYPE_GROUP(t)) {
577 #if LWIP_RAW
578 case NETCONN_RAW:
579 size = DEFAULT_RAW_RECVMBOX_SIZE;
580 break;
581 #endif /* LWIP_RAW */
582 #if LWIP_UDP
583 case NETCONN_UDP:
584 size = DEFAULT_UDP_RECVMBOX_SIZE;
585 break;
586 #endif /* LWIP_UDP */
587 #if LWIP_TCP
588 case NETCONN_TCP:
589 size = DEFAULT_TCP_RECVMBOX_SIZE;
590 break;
591 #endif /* LWIP_TCP */
592 default:
593 LWIP_ASSERT("netconn_alloc: undefined netconn_type", 0);
594 break;
596 #endif
598 if (sys_sem_new(&conn->op_completed, 0) != ERR_OK) {
599 memp_free(MEMP_NETCONN, conn);
600 return NULL;
602 if (sys_mbox_new(&conn->recvmbox, size) != ERR_OK) {
603 sys_sem_free(&conn->op_completed);
604 memp_free(MEMP_NETCONN, conn);
605 return NULL;
608 #if LWIP_TCP
609 sys_mbox_set_invalid(&conn->acceptmbox);
610 #endif
611 conn->state = NETCONN_NONE;
612 #if LWIP_SOCKET
613 /* initialize socket to -1 since 0 is a valid socket */
614 conn->socket = -1;
615 #endif /* LWIP_SOCKET */
616 conn->callback = callback;
617 conn->recv_avail = 0;
618 #if LWIP_TCP
619 conn->current_msg = NULL;
620 conn->write_offset = 0;
621 #endif /* LWIP_TCP */
622 #if LWIP_SO_RCVTIMEO
623 conn->recv_timeout = 0;
624 #endif /* LWIP_SO_RCVTIMEO */
625 #if LWIP_SO_RCVBUF
626 conn->recv_bufsize = RECV_BUFSIZE_DEFAULT;
627 #endif /* LWIP_SO_RCVBUF */
628 conn->flags = 0;
629 return conn;
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
638 void
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));
644 #if LWIP_TCP
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
657 * these mboxes
659 * @param conn the netconn to free
660 * @bytes_drained bytes drained from recvmbox
661 * @accepts_drained pending connections drained from acceptmbox
663 static void
664 netconn_drain(struct netconn *conn)
666 void *mem;
667 #if LWIP_TCP
668 struct pbuf *p;
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) {
676 #if LWIP_TCP
677 if (conn->type == NETCONN_TCP) {
678 if(mem != NULL) {
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);
684 pbuf_free(p);
686 } else
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. */
697 #if LWIP_TCP
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);
706 /* drain recvmbox */
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 */
720 #if 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
724 * places.
726 * @param conn the TCP netconn to close
728 static void
729 do_close_internal(struct netconn *conn)
731 err_t err;
732 u8_t shut, shut_rx, shut_tx, close;
734 LWIP_ASSERT("invalid conn", (conn != NULL));
735 LWIP_ASSERT("this is for tcp netconns only", (conn->type == NETCONN_TCP));
736 LWIP_ASSERT("conn must be in state NETCONN_CLOSE", (conn->state == NETCONN_CLOSE));
737 LWIP_ASSERT("pcb already closed", (conn->pcb.tcp != NULL));
738 LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL);
740 shut = conn->current_msg->msg.sd.shut;
741 shut_rx = shut & NETCONN_SHUT_RD;
742 shut_tx = shut & NETCONN_SHUT_WR;
743 /* shutting down both ends is the same as closing */
744 close = shut == NETCONN_SHUT_RDWR;
746 /* Set back some callback pointers */
747 if (close) {
748 tcp_arg(conn->pcb.tcp, NULL);
750 if (conn->pcb.tcp->state == LISTEN) {
751 tcp_accept(conn->pcb.tcp, NULL);
752 } else {
753 /* some callbacks have to be reset if tcp_close is not successful */
754 if (shut_rx) {
755 tcp_recv(conn->pcb.tcp, NULL);
756 tcp_accept(conn->pcb.tcp, NULL);
758 if (shut_tx) {
759 tcp_sent(conn->pcb.tcp, NULL);
761 if (close) {
762 tcp_poll(conn->pcb.tcp, NULL, 4);
763 tcp_err(conn->pcb.tcp, NULL);
766 /* Try to close the connection */
767 if (shut == NETCONN_SHUT_RDWR) {
768 err = tcp_close(conn->pcb.tcp);
769 } else {
770 err = tcp_shutdown(conn->pcb.tcp, shut & NETCONN_SHUT_RD, shut & NETCONN_SHUT_WR);
772 if (err == ERR_OK) {
773 /* Closing succeeded */
774 conn->current_msg->err = ERR_OK;
775 conn->current_msg = NULL;
776 conn->state = NETCONN_NONE;
777 /* Set back some callback pointers as conn is going away */
778 conn->pcb.tcp = NULL;
779 /* Trigger select() in socket layer. Make sure everybody notices activity
780 on the connection, error first! */
781 if (close) {
782 API_EVENT(conn, NETCONN_EVT_ERROR, 0);
784 if (shut_rx) {
785 API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
787 if (shut_tx) {
788 API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
790 /* wake up the application task */
791 sys_sem_signal(&conn->op_completed);
792 } else {
793 /* Closing failed, restore some of the callbacks */
794 /* Closing of listen pcb will never fail! */
795 LWIP_ASSERT("Closing a listen pcb may not fail!", (conn->pcb.tcp->state != LISTEN));
796 tcp_sent(conn->pcb.tcp, sent_tcp);
797 tcp_poll(conn->pcb.tcp, poll_tcp, 4);
798 tcp_err(conn->pcb.tcp, err_tcp);
799 tcp_arg(conn->pcb.tcp, conn);
800 /* don't restore recv callback: we don't want to receive any more data */
802 /* If closing didn't succeed, we get called again either
803 from poll_tcp or from sent_tcp */
805 #endif /* LWIP_TCP */
808 * Delete the pcb inside a netconn.
809 * Called from netconn_delete.
811 * @param msg the api_msg_msg pointing to the connection
813 void
814 do_delconn(struct api_msg_msg *msg)
816 /* @todo TCP: abort running write/connect? */
817 if ((msg->conn->state != NETCONN_NONE) &&
818 (msg->conn->state != NETCONN_LISTEN) &&
819 (msg->conn->state != NETCONN_CONNECT)) {
820 /* this only happens for TCP netconns */
821 LWIP_ASSERT("msg->conn->type == NETCONN_TCP", msg->conn->type == NETCONN_TCP);
822 msg->err = ERR_INPROGRESS;
823 } else {
824 LWIP_ASSERT("blocking connect in progress",
825 (msg->conn->state != NETCONN_CONNECT) || IN_NONBLOCKING_CONNECT(msg->conn));
826 /* Drain and delete mboxes */
827 netconn_drain(msg->conn);
829 if (msg->conn->pcb.tcp != NULL) {
831 switch (NETCONNTYPE_GROUP(msg->conn->type)) {
832 #if LWIP_RAW
833 case NETCONN_RAW:
834 raw_remove(msg->conn->pcb.raw);
835 break;
836 #endif /* LWIP_RAW */
837 #if LWIP_UDP
838 case NETCONN_UDP:
839 msg->conn->pcb.udp->recv_arg = NULL;
840 udp_remove(msg->conn->pcb.udp);
841 break;
842 #endif /* LWIP_UDP */
843 #if LWIP_TCP
844 case NETCONN_TCP:
845 LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL &&
846 msg->conn->write_offset == 0);
847 msg->conn->state = NETCONN_CLOSE;
848 msg->msg.sd.shut = NETCONN_SHUT_RDWR;
849 msg->conn->current_msg = msg;
850 do_close_internal(msg->conn);
851 /* API_EVENT is called inside do_close_internal, before releasing
852 the application thread, so we can return at this point! */
853 return;
854 #endif /* LWIP_TCP */
855 default:
856 break;
858 msg->conn->pcb.tcp = NULL;
860 /* tcp netconns don't come here! */
862 /* @todo: this lets select make the socket readable and writable,
863 which is wrong! errfd instead? */
864 API_EVENT(msg->conn, NETCONN_EVT_RCVPLUS, 0);
865 API_EVENT(msg->conn, NETCONN_EVT_SENDPLUS, 0);
867 if (sys_sem_valid(&msg->conn->op_completed)) {
868 sys_sem_signal(&msg->conn->op_completed);
873 * Bind a pcb contained in a netconn
874 * Called from netconn_bind.
876 * @param msg the api_msg_msg pointing to the connection and containing
877 * the IP address and port to bind to
879 void
880 do_bind(struct api_msg_msg *msg)
882 if (ERR_IS_FATAL(msg->conn->last_err)) {
883 msg->err = msg->conn->last_err;
884 } else {
885 msg->err = ERR_VAL;
886 if (msg->conn->pcb.tcp != NULL) {
887 switch (NETCONNTYPE_GROUP(msg->conn->type)) {
888 #if LWIP_RAW
889 case NETCONN_RAW:
890 msg->err = raw_bind(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
891 break;
892 #endif /* LWIP_RAW */
893 #if LWIP_UDP
894 case NETCONN_UDP:
895 msg->err = udp_bind(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
896 break;
897 #endif /* LWIP_UDP */
898 #if LWIP_TCP
899 case NETCONN_TCP:
900 msg->err = tcp_bind(msg->conn->pcb.tcp, msg->msg.bc.ipaddr, msg->msg.bc.port);
901 break;
902 #endif /* LWIP_TCP */
903 default:
904 break;
908 TCPIP_APIMSG_ACK(msg);
911 #if LWIP_TCP
913 * TCP callback function if a connection (opened by tcp_connect/do_connect) has
914 * been established (or reset by the remote host).
916 * @see tcp.h (struct tcp_pcb.connected) for parameters and return values
918 static err_t
919 do_connected(void *arg, struct tcp_pcb *pcb, err_t err)
921 struct netconn *conn;
922 int was_blocking;
924 LWIP_UNUSED_ARG(pcb);
926 conn = (struct netconn *)arg;
928 if (conn == NULL) {
929 return ERR_VAL;
932 LWIP_ASSERT("conn->state == NETCONN_CONNECT", conn->state == NETCONN_CONNECT);
933 LWIP_ASSERT("(conn->current_msg != NULL) || conn->in_non_blocking_connect",
934 (conn->current_msg != NULL) || IN_NONBLOCKING_CONNECT(conn));
936 if (conn->current_msg != NULL) {
937 conn->current_msg->err = err;
939 if ((conn->type == NETCONN_TCP) && (err == ERR_OK)) {
940 setup_tcp(conn);
942 was_blocking = !IN_NONBLOCKING_CONNECT(conn);
943 SET_NONBLOCKING_CONNECT(conn, 0);
944 conn->current_msg = NULL;
945 conn->state = NETCONN_NONE;
946 if (!was_blocking) {
947 SYS_ARCH_DECL_PROTECT(lev);
948 SYS_ARCH_PROTECT(lev);
949 if (conn->last_err == ERR_INPROGRESS) {
950 conn->last_err = ERR_OK;
952 SYS_ARCH_UNPROTECT(lev);
954 API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
956 if (was_blocking) {
957 sys_sem_signal(&conn->op_completed);
959 return ERR_OK;
961 #endif /* LWIP_TCP */
964 * Connect a pcb contained inside a netconn
965 * Called from netconn_connect.
967 * @param msg the api_msg_msg pointing to the connection and containing
968 * the IP address and port to connect to
970 void
971 do_connect(struct api_msg_msg *msg)
973 if (msg->conn->pcb.tcp == NULL) {
974 /* This may happen when calling netconn_connect() a second time */
975 msg->err = ERR_CLSD;
976 } else {
977 switch (NETCONNTYPE_GROUP(msg->conn->type)) {
978 #if LWIP_RAW
979 case NETCONN_RAW:
980 msg->err = raw_connect(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
981 break;
982 #endif /* LWIP_RAW */
983 #if LWIP_UDP
984 case NETCONN_UDP:
985 msg->err = udp_connect(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
986 break;
987 #endif /* LWIP_UDP */
988 #if LWIP_TCP
989 case NETCONN_TCP:
990 /* Prevent connect while doing any other action. */
991 if (msg->conn->state != NETCONN_NONE) {
992 msg->err = ERR_ISCONN;
993 } else {
994 setup_tcp(msg->conn);
995 msg->err = tcp_connect(msg->conn->pcb.tcp, msg->msg.bc.ipaddr,
996 msg->msg.bc.port, do_connected);
997 if (msg->err == ERR_OK) {
998 u8_t non_blocking = netconn_is_nonblocking(msg->conn);
999 msg->conn->state = NETCONN_CONNECT;
1000 SET_NONBLOCKING_CONNECT(msg->conn, non_blocking);
1001 if (non_blocking) {
1002 msg->err = ERR_INPROGRESS;
1003 } else {
1004 msg->conn->current_msg = msg;
1005 /* sys_sem_signal() is called from do_connected (or err_tcp()),
1006 * when the connection is established! */
1007 return;
1011 break;
1012 #endif /* LWIP_TCP */
1013 default:
1014 LWIP_ERROR("Invalid netconn type", 0, do{ msg->err = ERR_VAL; }while(0));
1015 break;
1018 sys_sem_signal(&msg->conn->op_completed);
1022 * Connect a pcb contained inside a netconn
1023 * Only used for UDP netconns.
1024 * Called from netconn_disconnect.
1026 * @param msg the api_msg_msg pointing to the connection to disconnect
1028 void
1029 do_disconnect(struct api_msg_msg *msg)
1031 #if LWIP_UDP
1032 if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
1033 udp_disconnect(msg->conn->pcb.udp);
1034 msg->err = ERR_OK;
1035 } else
1036 #endif /* LWIP_UDP */
1038 msg->err = ERR_VAL;
1040 TCPIP_APIMSG_ACK(msg);
1044 * Set a TCP pcb contained in a netconn into listen mode
1045 * Called from netconn_listen.
1047 * @param msg the api_msg_msg pointing to the connection
1049 void
1050 do_listen(struct api_msg_msg *msg)
1052 #if LWIP_TCP
1053 if (ERR_IS_FATAL(msg->conn->last_err)) {
1054 msg->err = msg->conn->last_err;
1055 } else {
1056 msg->err = ERR_CONN;
1057 if (msg->conn->pcb.tcp != NULL) {
1058 if (msg->conn->type == NETCONN_TCP) {
1059 if (msg->conn->state == NETCONN_NONE) {
1060 #if TCP_LISTEN_BACKLOG
1061 struct tcp_pcb* lpcb = tcp_listen_with_backlog(msg->conn->pcb.tcp, msg->msg.lb.backlog);
1062 #else /* TCP_LISTEN_BACKLOG */
1063 struct tcp_pcb* lpcb = tcp_listen(msg->conn->pcb.tcp);
1064 #endif /* TCP_LISTEN_BACKLOG */
1065 if (lpcb == NULL) {
1066 /* in this case, the old pcb is still allocated */
1067 msg->err = ERR_MEM;
1068 } else {
1069 /* delete the recvmbox and allocate the acceptmbox */
1070 if (sys_mbox_valid(&msg->conn->recvmbox)) {
1071 /** @todo: should we drain the recvmbox here? */
1072 sys_mbox_free(&msg->conn->recvmbox);
1073 sys_mbox_set_invalid(&msg->conn->recvmbox);
1075 msg->err = ERR_OK;
1076 if (!sys_mbox_valid(&msg->conn->acceptmbox)) {
1077 msg->err = sys_mbox_new(&msg->conn->acceptmbox, DEFAULT_ACCEPTMBOX_SIZE);
1079 if (msg->err == ERR_OK) {
1080 msg->conn->state = NETCONN_LISTEN;
1081 msg->conn->pcb.tcp = lpcb;
1082 tcp_arg(msg->conn->pcb.tcp, msg->conn);
1083 tcp_accept(msg->conn->pcb.tcp, accept_function);
1084 } else {
1085 /* since the old pcb is already deallocated, free lpcb now */
1086 tcp_close(lpcb);
1087 msg->conn->pcb.tcp = NULL;
1094 #endif /* LWIP_TCP */
1095 TCPIP_APIMSG_ACK(msg);
1099 * Send some data on a RAW or UDP pcb contained in a netconn
1100 * Called from netconn_send
1102 * @param msg the api_msg_msg pointing to the connection
1104 void
1105 do_send(struct api_msg_msg *msg)
1107 if (ERR_IS_FATAL(msg->conn->last_err)) {
1108 msg->err = msg->conn->last_err;
1109 } else {
1110 msg->err = ERR_CONN;
1111 if (msg->conn->pcb.tcp != NULL) {
1112 switch (NETCONNTYPE_GROUP(msg->conn->type)) {
1113 #if LWIP_RAW
1114 case NETCONN_RAW:
1115 if (ip_addr_isany(&msg->msg.b->addr)) {
1116 msg->err = raw_send(msg->conn->pcb.raw, msg->msg.b->p);
1117 } else {
1118 msg->err = raw_sendto(msg->conn->pcb.raw, msg->msg.b->p, &msg->msg.b->addr);
1120 break;
1121 #endif
1122 #if LWIP_UDP
1123 case NETCONN_UDP:
1124 #if LWIP_CHECKSUM_ON_COPY
1125 if (ip_addr_isany(&msg->msg.b->addr)) {
1126 msg->err = udp_send_chksum(msg->conn->pcb.udp, msg->msg.b->p,
1127 msg->msg.b->flags & NETBUF_FLAG_CHKSUM, msg->msg.b->toport_chksum);
1128 } else {
1129 msg->err = udp_sendto_chksum(msg->conn->pcb.udp, msg->msg.b->p,
1130 &msg->msg.b->addr, msg->msg.b->port,
1131 msg->msg.b->flags & NETBUF_FLAG_CHKSUM, msg->msg.b->toport_chksum);
1133 #else /* LWIP_CHECKSUM_ON_COPY */
1134 if (ip_addr_isany(&msg->msg.b->addr)) {
1135 msg->err = udp_send(msg->conn->pcb.udp, msg->msg.b->p);
1136 } else {
1137 msg->err = udp_sendto(msg->conn->pcb.udp, msg->msg.b->p, &msg->msg.b->addr, msg->msg.b->port);
1139 #endif /* LWIP_CHECKSUM_ON_COPY */
1140 break;
1141 #endif /* LWIP_UDP */
1142 default:
1143 break;
1147 TCPIP_APIMSG_ACK(msg);
1151 * Indicate data has been received from a TCP pcb contained in a netconn
1152 * Called from netconn_recv
1154 * @param msg the api_msg_msg pointing to the connection
1156 void
1157 do_recv(struct api_msg_msg *msg)
1159 #if LWIP_TCP
1160 msg->err = ERR_OK;
1161 if (msg->conn->pcb.tcp != NULL) {
1162 if (msg->conn->type == NETCONN_TCP) {
1163 #if TCP_LISTEN_BACKLOG
1164 if (msg->conn->pcb.tcp->state == LISTEN) {
1165 tcp_accepted(msg->conn->pcb.tcp);
1166 } else
1167 #endif /* TCP_LISTEN_BACKLOG */
1169 u32_t remaining = msg->msg.r.len;
1170 do {
1171 u16_t recved = (remaining > 0xffff) ? 0xffff : (u16_t)remaining;
1172 tcp_recved(msg->conn->pcb.tcp, recved);
1173 remaining -= recved;
1174 }while(remaining != 0);
1178 #endif /* LWIP_TCP */
1179 TCPIP_APIMSG_ACK(msg);
1182 #if LWIP_TCP
1184 * See if more data needs to be written from a previous call to netconn_write.
1185 * Called initially from do_write. If the first call can't send all data
1186 * (because of low memory or empty send-buffer), this function is called again
1187 * from sent_tcp() or poll_tcp() to send more data. If all data is sent, the
1188 * blocking application thread (waiting in netconn_write) is released.
1190 * @param conn netconn (that is currently in state NETCONN_WRITE) to process
1191 * @return ERR_OK
1192 * ERR_MEM if LWIP_TCPIP_CORE_LOCKING=1 and sending hasn't yet finished
1194 static err_t
1195 do_writemore(struct netconn *conn)
1197 err_t err = ERR_OK;
1198 void *dataptr;
1199 u16_t len, available;
1200 u8_t write_finished = 0;
1201 size_t diff;
1202 u8_t dontblock = netconn_is_nonblocking(conn) ||
1203 (conn->current_msg->msg.w.apiflags & NETCONN_DONTBLOCK);
1204 u8_t apiflags = conn->current_msg->msg.w.apiflags;
1206 LWIP_ASSERT("conn != NULL", conn != NULL);
1207 LWIP_ASSERT("conn->state == NETCONN_WRITE", (conn->state == NETCONN_WRITE));
1208 LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL);
1209 LWIP_ASSERT("conn->pcb.tcp != NULL", conn->pcb.tcp != NULL);
1210 LWIP_ASSERT("conn->write_offset < conn->current_msg->msg.w.len",
1211 conn->write_offset < conn->current_msg->msg.w.len);
1213 dataptr = (u8_t*)conn->current_msg->msg.w.dataptr + conn->write_offset;
1214 diff = conn->current_msg->msg.w.len - conn->write_offset;
1215 if (diff > 0xffffUL) { /* max_u16_t */
1216 len = 0xffff;
1217 #if LWIP_TCPIP_CORE_LOCKING
1218 conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
1219 #endif
1220 apiflags |= TCP_WRITE_FLAG_MORE;
1221 } else {
1222 len = (u16_t)diff;
1224 available = tcp_sndbuf(conn->pcb.tcp);
1225 if (available < len) {
1226 /* don't try to write more than sendbuf */
1227 len = available;
1228 #if LWIP_TCPIP_CORE_LOCKING
1229 conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
1230 #endif
1231 apiflags |= TCP_WRITE_FLAG_MORE;
1233 if (dontblock && (len < conn->current_msg->msg.w.len)) {
1234 /* failed to send all data at once -> nonblocking write not possible */
1235 err = ERR_MEM;
1237 if (err == ERR_OK) {
1238 LWIP_ASSERT("do_writemore: invalid length!", ((conn->write_offset + len) <= conn->current_msg->msg.w.len));
1239 err = tcp_write(conn->pcb.tcp, dataptr, len, apiflags);
1241 if (dontblock && (err == ERR_MEM)) {
1242 /* nonblocking write failed */
1243 write_finished = 1;
1244 err = ERR_WOULDBLOCK;
1245 /* let poll_tcp check writable space to mark the pcb
1246 writable again */
1247 conn->flags |= NETCONN_FLAG_CHECK_WRITESPACE;
1248 /* let select mark this pcb as non-writable. */
1249 API_EVENT(conn, NETCONN_EVT_SENDMINUS, len);
1250 } else {
1251 /* if OK or memory error, check available space */
1252 if (((err == ERR_OK) || (err == ERR_MEM)) &&
1253 ((tcp_sndbuf(conn->pcb.tcp) <= TCP_SNDLOWAT) ||
1254 (tcp_sndqueuelen(conn->pcb.tcp) >= TCP_SNDQUEUELOWAT))) {
1255 /* The queued byte- or pbuf-count exceeds the configured low-water limit,
1256 let select mark this pcb as non-writable. */
1257 API_EVENT(conn, NETCONN_EVT_SENDMINUS, len);
1260 if (err == ERR_OK) {
1261 conn->write_offset += len;
1262 if (conn->write_offset == conn->current_msg->msg.w.len) {
1263 /* everything was written */
1264 write_finished = 1;
1265 conn->write_offset = 0;
1267 tcp_output(conn->pcb.tcp);
1268 } else if (err == ERR_MEM) {
1269 /* If ERR_MEM, we wait for sent_tcp or poll_tcp to be called
1270 we do NOT return to the application thread, since ERR_MEM is
1271 only a temporary error! */
1273 /* tcp_write returned ERR_MEM, try tcp_output anyway */
1274 tcp_output(conn->pcb.tcp);
1276 #if LWIP_TCPIP_CORE_LOCKING
1277 conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
1278 #endif
1279 } else {
1280 /* On errors != ERR_MEM, we don't try writing any more but return
1281 the error to the application thread. */
1282 write_finished = 1;
1286 if (write_finished) {
1287 /* everything was written: set back connection state
1288 and back to application task */
1289 conn->current_msg->err = err;
1290 conn->current_msg = NULL;
1291 conn->state = NETCONN_NONE;
1292 #if LWIP_TCPIP_CORE_LOCKING
1293 if ((conn->flags & NETCONN_FLAG_WRITE_DELAYED) != 0)
1294 #endif
1296 sys_sem_signal(&conn->op_completed);
1299 #if LWIP_TCPIP_CORE_LOCKING
1300 else
1301 return ERR_MEM;
1302 #endif
1303 return ERR_OK;
1305 #endif /* LWIP_TCP */
1308 * Send some data on a TCP pcb contained in a netconn
1309 * Called from netconn_write
1311 * @param msg the api_msg_msg pointing to the connection
1313 void
1314 do_write(struct api_msg_msg *msg)
1316 if (ERR_IS_FATAL(msg->conn->last_err)) {
1317 msg->err = msg->conn->last_err;
1318 } else {
1319 if (msg->conn->type == NETCONN_TCP) {
1320 #if LWIP_TCP
1321 if (msg->conn->state != NETCONN_NONE) {
1322 /* netconn is connecting, closing or in blocking write */
1323 msg->err = ERR_INPROGRESS;
1324 } else if (msg->conn->pcb.tcp != NULL) {
1325 msg->conn->state = NETCONN_WRITE;
1326 /* set all the variables used by do_writemore */
1327 LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL &&
1328 msg->conn->write_offset == 0);
1329 LWIP_ASSERT("msg->msg.w.len != 0", msg->msg.w.len != 0);
1330 msg->conn->current_msg = msg;
1331 msg->conn->write_offset = 0;
1332 #if LWIP_TCPIP_CORE_LOCKING
1333 msg->conn->flags &= ~NETCONN_FLAG_WRITE_DELAYED;
1334 if (do_writemore(msg->conn) != ERR_OK) {
1335 LWIP_ASSERT("state!", msg->conn->state == NETCONN_WRITE);
1336 UNLOCK_TCPIP_CORE();
1337 sys_arch_sem_wait(&msg->conn->op_completed, 0);
1338 LOCK_TCPIP_CORE();
1339 LWIP_ASSERT("state!", msg->conn->state == NETCONN_NONE);
1341 #else /* LWIP_TCPIP_CORE_LOCKING */
1342 do_writemore(msg->conn);
1343 #endif /* LWIP_TCPIP_CORE_LOCKING */
1344 /* for both cases: if do_writemore was called, don't ACK the APIMSG
1345 since do_writemore ACKs it! */
1346 return;
1347 } else {
1348 msg->err = ERR_CONN;
1350 #else /* LWIP_TCP */
1351 msg->err = ERR_VAL;
1352 #endif /* LWIP_TCP */
1353 #if (LWIP_UDP || LWIP_RAW)
1354 } else {
1355 msg->err = ERR_VAL;
1356 #endif /* (LWIP_UDP || LWIP_RAW) */
1359 TCPIP_APIMSG_ACK(msg);
1363 * Return a connection's local or remote address
1364 * Called from netconn_getaddr
1366 * @param msg the api_msg_msg pointing to the connection
1368 void
1369 do_getaddr(struct api_msg_msg *msg)
1371 if (msg->conn->pcb.ip != NULL) {
1372 *(msg->msg.ad.ipaddr) = (msg->msg.ad.local ? msg->conn->pcb.ip->local_ip :
1373 msg->conn->pcb.ip->remote_ip);
1375 msg->err = ERR_OK;
1376 switch (NETCONNTYPE_GROUP(msg->conn->type)) {
1377 #if LWIP_RAW
1378 case NETCONN_RAW:
1379 if (msg->msg.ad.local) {
1380 *(msg->msg.ad.port) = msg->conn->pcb.raw->protocol;
1381 } else {
1382 /* return an error as connecting is only a helper for upper layers */
1383 msg->err = ERR_CONN;
1385 break;
1386 #endif /* LWIP_RAW */
1387 #if LWIP_UDP
1388 case NETCONN_UDP:
1389 if (msg->msg.ad.local) {
1390 *(msg->msg.ad.port) = msg->conn->pcb.udp->local_port;
1391 } else {
1392 if ((msg->conn->pcb.udp->flags & UDP_FLAGS_CONNECTED) == 0) {
1393 msg->err = ERR_CONN;
1394 } else {
1395 *(msg->msg.ad.port) = msg->conn->pcb.udp->remote_port;
1398 break;
1399 #endif /* LWIP_UDP */
1400 #if LWIP_TCP
1401 case NETCONN_TCP:
1402 *(msg->msg.ad.port) = (msg->msg.ad.local?msg->conn->pcb.tcp->local_port:msg->conn->pcb.tcp->remote_port);
1403 break;
1404 #endif /* LWIP_TCP */
1405 default:
1406 LWIP_ASSERT("invalid netconn_type", 0);
1407 break;
1409 } else {
1410 msg->err = ERR_CONN;
1412 TCPIP_APIMSG_ACK(msg);
1416 * Close a TCP pcb contained in a netconn
1417 * Called from netconn_close
1419 * @param msg the api_msg_msg pointing to the connection
1421 void
1422 do_close(struct api_msg_msg *msg)
1424 #if LWIP_TCP
1425 /* @todo: abort running write/connect? */
1426 if ((msg->conn->state != NETCONN_NONE) && (msg->conn->state != NETCONN_LISTEN)) {
1427 /* this only happens for TCP netconns */
1428 LWIP_ASSERT("msg->conn->type == NETCONN_TCP", msg->conn->type == NETCONN_TCP);
1429 msg->err = ERR_INPROGRESS;
1430 } else if ((msg->conn->pcb.tcp != NULL) && (msg->conn->type == NETCONN_TCP)) {
1431 if ((msg->msg.sd.shut != NETCONN_SHUT_RDWR) && (msg->conn->state == NETCONN_LISTEN)) {
1432 /* LISTEN doesn't support half shutdown */
1433 msg->err = ERR_CONN;
1434 } else {
1435 if (msg->msg.sd.shut & NETCONN_SHUT_RD) {
1436 /* Drain and delete mboxes */
1437 netconn_drain(msg->conn);
1439 LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL &&
1440 msg->conn->write_offset == 0);
1441 msg->conn->state = NETCONN_CLOSE;
1442 msg->conn->current_msg = msg;
1443 do_close_internal(msg->conn);
1444 /* for tcp netconns, do_close_internal ACKs the message */
1445 return;
1447 } else
1448 #endif /* LWIP_TCP */
1450 msg->err = ERR_VAL;
1452 sys_sem_signal(&msg->conn->op_completed);
1455 #if LWIP_IGMP
1457 * Join multicast groups for UDP netconns.
1458 * Called from netconn_join_leave_group
1460 * @param msg the api_msg_msg pointing to the connection
1462 void
1463 do_join_leave_group(struct api_msg_msg *msg)
1465 if (ERR_IS_FATAL(msg->conn->last_err)) {
1466 msg->err = msg->conn->last_err;
1467 } else {
1468 if (msg->conn->pcb.tcp != NULL) {
1469 if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
1470 #if LWIP_UDP
1471 if (msg->msg.jl.join_or_leave == NETCONN_JOIN) {
1472 msg->err = igmp_joingroup(msg->msg.jl.netif_addr, msg->msg.jl.multiaddr);
1473 } else {
1474 msg->err = igmp_leavegroup(msg->msg.jl.netif_addr, msg->msg.jl.multiaddr);
1476 #endif /* LWIP_UDP */
1477 #if (LWIP_TCP || LWIP_RAW)
1478 } else {
1479 msg->err = ERR_VAL;
1480 #endif /* (LWIP_TCP || LWIP_RAW) */
1482 } else {
1483 msg->err = ERR_CONN;
1486 TCPIP_APIMSG_ACK(msg);
1488 #endif /* LWIP_IGMP */
1490 #if LWIP_DNS
1492 * Callback function that is called when DNS name is resolved
1493 * (or on timeout). A waiting application thread is waked up by
1494 * signaling the semaphore.
1496 static void
1497 do_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
1499 struct dns_api_msg *msg = (struct dns_api_msg*)arg;
1501 LWIP_ASSERT("DNS response for wrong host name", strcmp(msg->name, name) == 0);
1502 LWIP_UNUSED_ARG(name);
1504 if (ipaddr == NULL) {
1505 /* timeout or memory error */
1506 *msg->err = ERR_VAL;
1507 } else {
1508 /* address was resolved */
1509 *msg->err = ERR_OK;
1510 *msg->addr = *ipaddr;
1512 /* wake up the application task waiting in netconn_gethostbyname */
1513 sys_sem_signal(msg->sem);
1517 * Execute a DNS query
1518 * Called from netconn_gethostbyname
1520 * @param arg the dns_api_msg pointing to the query
1522 void
1523 do_gethostbyname(void *arg)
1525 struct dns_api_msg *msg = (struct dns_api_msg*)arg;
1527 *msg->err = dns_gethostbyname(msg->name, msg->addr, do_dns_found, msg);
1528 if (*msg->err != ERR_INPROGRESS) {
1529 /* on error or immediate success, wake up the application
1530 * task waiting in netconn_gethostbyname */
1531 sys_sem_signal(msg->sem);
1534 #endif /* LWIP_DNS */
1536 #endif /* LWIP_NETCONN */