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 ip_addr_copy(buf
->addr
, *ip_current_src_addr());
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
);
176 ip_addr_set(&buf
->addr
, addr
);
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 */
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
)
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 */
748 tcp_arg(conn
->pcb
.tcp
, NULL
);
750 if (conn
->pcb
.tcp
->state
== LISTEN
) {
751 tcp_accept(conn
->pcb
.tcp
, NULL
);
753 /* some callbacks have to be reset if tcp_close is not successful */
755 tcp_recv(conn
->pcb
.tcp
, NULL
);
756 tcp_accept(conn
->pcb
.tcp
, NULL
);
759 tcp_sent(conn
->pcb
.tcp
, NULL
);
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
);
770 err
= tcp_shutdown(conn
->pcb
.tcp
, shut
& NETCONN_SHUT_RD
, shut
& NETCONN_SHUT_WR
);
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! */
782 API_EVENT(conn
, NETCONN_EVT_ERROR
, 0);
785 API_EVENT(conn
, NETCONN_EVT_RCVPLUS
, 0);
788 API_EVENT(conn
, NETCONN_EVT_SENDPLUS
, 0);
790 /* wake up the application task */
791 sys_sem_signal(&conn
->op_completed
);
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
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
;
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
)) {
834 raw_remove(msg
->conn
->pcb
.raw
);
836 #endif /* LWIP_RAW */
839 msg
->conn
->pcb
.udp
->recv_arg
= NULL
;
840 udp_remove(msg
->conn
->pcb
.udp
);
842 #endif /* LWIP_UDP */
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! */
854 #endif /* LWIP_TCP */
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
880 do_bind(struct api_msg_msg
*msg
)
882 if (ERR_IS_FATAL(msg
->conn
->last_err
)) {
883 msg
->err
= msg
->conn
->last_err
;
886 if (msg
->conn
->pcb
.tcp
!= NULL
) {
887 switch (NETCONNTYPE_GROUP(msg
->conn
->type
)) {
890 msg
->err
= raw_bind(msg
->conn
->pcb
.raw
, msg
->msg
.bc
.ipaddr
);
892 #endif /* LWIP_RAW */
895 msg
->err
= udp_bind(msg
->conn
->pcb
.udp
, msg
->msg
.bc
.ipaddr
, msg
->msg
.bc
.port
);
897 #endif /* LWIP_UDP */
900 msg
->err
= tcp_bind(msg
->conn
->pcb
.tcp
, msg
->msg
.bc
.ipaddr
, msg
->msg
.bc
.port
);
902 #endif /* LWIP_TCP */
908 TCPIP_APIMSG_ACK(msg
);
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
919 do_connected(void *arg
, struct tcp_pcb
*pcb
, err_t err
)
921 struct netconn
*conn
;
924 LWIP_UNUSED_ARG(pcb
);
926 conn
= (struct netconn
*)arg
;
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
)) {
942 was_blocking
= !IN_NONBLOCKING_CONNECT(conn
);
943 SET_NONBLOCKING_CONNECT(conn
, 0);
944 conn
->current_msg
= NULL
;
945 conn
->state
= NETCONN_NONE
;
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);
957 sys_sem_signal(&conn
->op_completed
);
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
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 */
977 switch (NETCONNTYPE_GROUP(msg
->conn
->type
)) {
980 msg
->err
= raw_connect(msg
->conn
->pcb
.raw
, msg
->msg
.bc
.ipaddr
);
982 #endif /* LWIP_RAW */
985 msg
->err
= udp_connect(msg
->conn
->pcb
.udp
, msg
->msg
.bc
.ipaddr
, msg
->msg
.bc
.port
);
987 #endif /* LWIP_UDP */
990 /* Prevent connect while doing any other action. */
991 if (msg
->conn
->state
!= NETCONN_NONE
) {
992 msg
->err
= ERR_ISCONN
;
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
);
1002 msg
->err
= ERR_INPROGRESS
;
1004 msg
->conn
->current_msg
= msg
;
1005 /* sys_sem_signal() is called from do_connected (or err_tcp()),
1006 * when the connection is established! */
1012 #endif /* LWIP_TCP */
1014 LWIP_ERROR("Invalid netconn type", 0, do{ msg
->err
= ERR_VAL
; }while(0));
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
1029 do_disconnect(struct api_msg_msg
*msg
)
1032 if (NETCONNTYPE_GROUP(msg
->conn
->type
) == NETCONN_UDP
) {
1033 udp_disconnect(msg
->conn
->pcb
.udp
);
1036 #endif /* LWIP_UDP */
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
1050 do_listen(struct api_msg_msg
*msg
)
1053 if (ERR_IS_FATAL(msg
->conn
->last_err
)) {
1054 msg
->err
= msg
->conn
->last_err
;
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 */
1066 /* in this case, the old pcb is still allocated */
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
);
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
);
1085 /* since the old pcb is already deallocated, free lpcb now */
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
1105 do_send(struct api_msg_msg
*msg
)
1107 if (ERR_IS_FATAL(msg
->conn
->last_err
)) {
1108 msg
->err
= msg
->conn
->last_err
;
1110 msg
->err
= ERR_CONN
;
1111 if (msg
->conn
->pcb
.tcp
!= NULL
) {
1112 switch (NETCONNTYPE_GROUP(msg
->conn
->type
)) {
1115 if (ip_addr_isany(&msg
->msg
.b
->addr
)) {
1116 msg
->err
= raw_send(msg
->conn
->pcb
.raw
, msg
->msg
.b
->p
);
1118 msg
->err
= raw_sendto(msg
->conn
->pcb
.raw
, msg
->msg
.b
->p
, &msg
->msg
.b
->addr
);
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
);
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
);
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 */
1141 #endif /* LWIP_UDP */
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
1157 do_recv(struct api_msg_msg
*msg
)
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
);
1167 #endif /* TCP_LISTEN_BACKLOG */
1169 u32_t remaining
= msg
->msg
.r
.len
;
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
);
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
1192 * ERR_MEM if LWIP_TCPIP_CORE_LOCKING=1 and sending hasn't yet finished
1195 do_writemore(struct netconn
*conn
)
1199 u16_t len
, available
;
1200 u8_t write_finished
= 0;
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 */
1217 #if LWIP_TCPIP_CORE_LOCKING
1218 conn
->flags
|= NETCONN_FLAG_WRITE_DELAYED
;
1220 apiflags
|= TCP_WRITE_FLAG_MORE
;
1224 available
= tcp_sndbuf(conn
->pcb
.tcp
);
1225 if (available
< len
) {
1226 /* don't try to write more than sendbuf */
1228 #if LWIP_TCPIP_CORE_LOCKING
1229 conn
->flags
|= NETCONN_FLAG_WRITE_DELAYED
;
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 */
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 */
1244 err
= ERR_WOULDBLOCK
;
1245 /* let poll_tcp check writable space to mark the pcb
1247 conn
->flags
|= NETCONN_FLAG_CHECK_WRITESPACE
;
1248 /* let select mark this pcb as non-writable. */
1249 API_EVENT(conn
, NETCONN_EVT_SENDMINUS
, len
);
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 */
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
;
1280 /* On errors != ERR_MEM, we don't try writing any more but return
1281 the error to the application thread. */
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)
1296 sys_sem_signal(&conn
->op_completed
);
1299 #if LWIP_TCPIP_CORE_LOCKING
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
1314 do_write(struct api_msg_msg
*msg
)
1316 if (ERR_IS_FATAL(msg
->conn
->last_err
)) {
1317 msg
->err
= msg
->conn
->last_err
;
1319 if (msg
->conn
->type
== NETCONN_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);
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! */
1348 msg
->err
= ERR_CONN
;
1350 #else /* LWIP_TCP */
1352 #endif /* LWIP_TCP */
1353 #if (LWIP_UDP || LWIP_RAW)
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
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
);
1376 switch (NETCONNTYPE_GROUP(msg
->conn
->type
)) {
1379 if (msg
->msg
.ad
.local
) {
1380 *(msg
->msg
.ad
.port
) = msg
->conn
->pcb
.raw
->protocol
;
1382 /* return an error as connecting is only a helper for upper layers */
1383 msg
->err
= ERR_CONN
;
1386 #endif /* LWIP_RAW */
1389 if (msg
->msg
.ad
.local
) {
1390 *(msg
->msg
.ad
.port
) = msg
->conn
->pcb
.udp
->local_port
;
1392 if ((msg
->conn
->pcb
.udp
->flags
& UDP_FLAGS_CONNECTED
) == 0) {
1393 msg
->err
= ERR_CONN
;
1395 *(msg
->msg
.ad
.port
) = msg
->conn
->pcb
.udp
->remote_port
;
1399 #endif /* LWIP_UDP */
1402 *(msg
->msg
.ad
.port
) = (msg
->msg
.ad
.local
?msg
->conn
->pcb
.tcp
->local_port
:msg
->conn
->pcb
.tcp
->remote_port
);
1404 #endif /* LWIP_TCP */
1406 LWIP_ASSERT("invalid netconn_type", 0);
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
1422 do_close(struct api_msg_msg
*msg
)
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
;
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 */
1448 #endif /* LWIP_TCP */
1452 sys_sem_signal(&msg
->conn
->op_completed
);
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
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
;
1468 if (msg
->conn
->pcb
.tcp
!= NULL
) {
1469 if (NETCONNTYPE_GROUP(msg
->conn
->type
) == NETCONN_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
);
1474 msg
->err
= igmp_leavegroup(msg
->msg
.jl
.netif_addr
, msg
->msg
.jl
.multiaddr
);
1476 #endif /* LWIP_UDP */
1477 #if (LWIP_TCP || LWIP_RAW)
1480 #endif /* (LWIP_TCP || LWIP_RAW) */
1483 msg
->err
= ERR_CONN
;
1486 TCPIP_APIMSG_ACK(msg
);
1488 #endif /* LWIP_IGMP */
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.
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
;
1508 /* address was resolved */
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
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 */