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
) {
124 SYS_ARCH_INC(conn
->recv_avail
, len
);
125 #endif /* LWIP_SO_RCVBUF */
126 /* Register event with callback */
127 API_EVENT(conn
, NETCONN_EVT_RCVPLUS
, len
);
132 return 0; /* do not eat the packet */
138 * Receive callback function for UDP netconns.
139 * Posts the packet to conn->recvmbox or deletes it on memory error.
141 * @see udp.h (struct udp_pcb.recv) for parameters
144 recv_udp(void *arg
, struct udp_pcb
*pcb
, struct pbuf
*p
,
145 ip_addr_t
*addr
, u16_t port
)
148 struct netconn
*conn
;
152 #endif /* LWIP_SO_RCVBUF */
154 LWIP_UNUSED_ARG(pcb
); /* only used for asserts... */
155 LWIP_ASSERT("recv_udp must have a pcb argument", pcb
!= NULL
);
156 LWIP_ASSERT("recv_udp must have an argument", arg
!= NULL
);
157 conn
= (struct netconn
*)arg
;
158 LWIP_ASSERT("recv_udp: recv for wrong pcb!", conn
->pcb
.udp
== pcb
);
161 SYS_ARCH_GET(conn
->recv_avail
, recv_avail
);
162 if ((conn
== NULL
) || !sys_mbox_valid(&conn
->recvmbox
) ||
163 ((recv_avail
+ (int)(p
->tot_len
)) > conn
->recv_bufsize
)) {
164 #else /* LWIP_SO_RCVBUF */
165 if ((conn
== NULL
) || !sys_mbox_valid(&conn
->recvmbox
)) {
166 #endif /* LWIP_SO_RCVBUF */
171 buf
= (struct netbuf
*)memp_malloc(MEMP_NETBUF
);
178 ip_addr_set(&buf
->addr
, addr
);
180 #if LWIP_NETBUF_RECVINFO
182 const struct ip_hdr
* iphdr
= ip_current_header();
183 /* get the UDP header - always in the first pbuf, ensured by udp_input */
184 const struct udp_hdr
* udphdr
= (void*)(((char*)iphdr
) + IPH_LEN(iphdr
));
185 #if LWIP_CHECKSUM_ON_COPY
186 buf
->flags
= NETBUF_FLAG_DESTADDR
;
187 #endif /* LWIP_CHECKSUM_ON_COPY */
188 ip_addr_set(&buf
->toaddr
, ip_current_dest_addr());
189 buf
->toport_chksum
= udphdr
->dest
;
191 #endif /* LWIP_NETBUF_RECVINFO */
195 if (sys_mbox_trypost(&conn
->recvmbox
, buf
) != ERR_OK
) {
200 SYS_ARCH_INC(conn
->recv_avail
, len
);
201 #endif /* LWIP_SO_RCVBUF */
202 /* Register event with callback */
203 API_EVENT(conn
, NETCONN_EVT_RCVPLUS
, len
);
206 #endif /* LWIP_UDP */
210 * Receive callback function for TCP netconns.
211 * Posts the packet to conn->recvmbox, but doesn't delete it on errors.
213 * @see tcp.h (struct tcp_pcb.recv) for parameters and return value
216 recv_tcp(void *arg
, struct tcp_pcb
*pcb
, struct pbuf
*p
, err_t err
)
218 struct netconn
*conn
;
221 LWIP_UNUSED_ARG(pcb
);
222 LWIP_ASSERT("recv_tcp must have a pcb argument", pcb
!= NULL
);
223 LWIP_ASSERT("recv_tcp must have an argument", arg
!= NULL
);
224 conn
= (struct netconn
*)arg
;
225 LWIP_ASSERT("recv_tcp: recv for wrong pcb!", conn
->pcb
.tcp
== pcb
);
230 if (!sys_mbox_valid(&conn
->recvmbox
)) {
231 /* recvmbox already deleted */
233 tcp_recved(pcb
, p
->tot_len
);
238 /* Unlike for UDP or RAW pcbs, don't check for available space
239 using recv_avail since that could break the connection
240 (data is already ACKed) */
242 /* don't overwrite fatal errors! */
243 NETCONN_SET_SAFE_ERR(conn
, err
);
251 if (sys_mbox_trypost(&conn
->recvmbox
, p
) != ERR_OK
) {
252 /* don't deallocate p: it is presented to us later again from tcp_fasttmr! */
256 SYS_ARCH_INC(conn
->recv_avail
, len
);
257 #endif /* LWIP_SO_RCVBUF */
258 /* Register event with callback */
259 API_EVENT(conn
, NETCONN_EVT_RCVPLUS
, len
);
266 * Poll callback function for TCP netconns.
267 * Wakes up an application thread that waits for a connection to close
268 * or data to be sent. The application thread then takes the
269 * appropriate action to go on.
271 * Signals the conn->sem.
272 * netconn_close waits for conn->sem if closing failed.
274 * @see tcp.h (struct tcp_pcb.poll) for parameters and return value
277 poll_tcp(void *arg
, struct tcp_pcb
*pcb
)
279 struct netconn
*conn
= (struct netconn
*)arg
;
281 LWIP_UNUSED_ARG(pcb
);
282 LWIP_ASSERT("conn != NULL", (conn
!= NULL
));
284 if (conn
->state
== NETCONN_WRITE
) {
286 } else if (conn
->state
== NETCONN_CLOSE
) {
287 do_close_internal(conn
);
289 /* @todo: implement connect timeout here? */
291 /* Did a nonblocking write fail before? Then check available write-space. */
292 if (conn
->flags
& NETCONN_FLAG_CHECK_WRITESPACE
) {
293 /* If the queued byte- or pbuf-count drops below the configured low-water limit,
294 let select mark this pcb as writable again. */
295 if ((conn
->pcb
.tcp
!= NULL
) && (tcp_sndbuf(conn
->pcb
.tcp
) > TCP_SNDLOWAT
) &&
296 (tcp_sndqueuelen(conn
->pcb
.tcp
) < TCP_SNDQUEUELOWAT
)) {
297 conn
->flags
&= ~NETCONN_FLAG_CHECK_WRITESPACE
;
298 API_EVENT(conn
, NETCONN_EVT_SENDPLUS
, 0);
306 * Sent callback function for TCP netconns.
307 * Signals the conn->sem and calls API_EVENT.
308 * netconn_write waits for conn->sem if send buffer is low.
310 * @see tcp.h (struct tcp_pcb.sent) for parameters and return value
313 sent_tcp(void *arg
, struct tcp_pcb
*pcb
, u16_t len
)
315 struct netconn
*conn
= (struct netconn
*)arg
;
317 LWIP_UNUSED_ARG(pcb
);
318 LWIP_ASSERT("conn != NULL", (conn
!= NULL
));
320 if (conn
->state
== NETCONN_WRITE
) {
322 } else if (conn
->state
== NETCONN_CLOSE
) {
323 do_close_internal(conn
);
327 /* If the queued byte- or pbuf-count drops below the configured low-water limit,
328 let select mark this pcb as writable again. */
329 if ((conn
->pcb
.tcp
!= NULL
) && (tcp_sndbuf(conn
->pcb
.tcp
) > TCP_SNDLOWAT
) &&
330 (tcp_sndqueuelen(conn
->pcb
.tcp
) < TCP_SNDQUEUELOWAT
)) {
331 conn
->flags
&= ~NETCONN_FLAG_CHECK_WRITESPACE
;
332 API_EVENT(conn
, NETCONN_EVT_SENDPLUS
, len
);
340 * Error callback function for TCP netconns.
341 * Signals conn->sem, posts to all conn mboxes and calls API_EVENT.
342 * The application thread has then to decide what to do.
344 * @see tcp.h (struct tcp_pcb.err) for parameters
347 err_tcp(void *arg
, err_t err
)
349 struct netconn
*conn
;
350 enum netconn_state old_state
;
351 SYS_ARCH_DECL_PROTECT(lev
);
353 conn
= (struct netconn
*)arg
;
354 LWIP_ASSERT("conn != NULL", (conn
!= NULL
));
356 conn
->pcb
.tcp
= NULL
;
358 /* no check since this is always fatal! */
359 SYS_ARCH_PROTECT(lev
);
360 conn
->last_err
= err
;
361 SYS_ARCH_UNPROTECT(lev
);
363 /* reset conn->state now before waking up other threads */
364 old_state
= conn
->state
;
365 conn
->state
= NETCONN_NONE
;
367 /* Notify the user layer about a connection error. Used to signal
369 API_EVENT(conn
, NETCONN_EVT_ERROR
, 0);
370 /* Try to release selects pending on 'read' or 'write', too.
371 They will get an error if they actually try to read or write. */
372 API_EVENT(conn
, NETCONN_EVT_RCVPLUS
, 0);
373 API_EVENT(conn
, NETCONN_EVT_SENDPLUS
, 0);
375 /* pass NULL-message to recvmbox to wake up pending recv */
376 if (sys_mbox_valid(&conn
->recvmbox
)) {
377 /* use trypost to prevent deadlock */
378 sys_mbox_trypost(&conn
->recvmbox
, NULL
);
380 /* pass NULL-message to acceptmbox to wake up pending accept */
381 if (sys_mbox_valid(&conn
->acceptmbox
)) {
382 /* use trypost to preven deadlock */
383 sys_mbox_trypost(&conn
->acceptmbox
, NULL
);
386 if ((old_state
== NETCONN_WRITE
) || (old_state
== NETCONN_CLOSE
) ||
387 (old_state
== NETCONN_CONNECT
)) {
388 /* calling do_writemore/do_close_internal is not necessary
389 since the pcb has already been deleted! */
390 int was_nonblocking_connect
= IN_NONBLOCKING_CONNECT(conn
);
391 SET_NONBLOCKING_CONNECT(conn
, 0);
393 if (!was_nonblocking_connect
) {
394 /* set error return code */
395 LWIP_ASSERT("conn->current_msg != NULL", conn
->current_msg
!= NULL
);
396 conn
->current_msg
->err
= err
;
397 conn
->current_msg
= NULL
;
398 /* wake up the waiting task */
399 sys_sem_signal(&conn
->op_completed
);
402 LWIP_ASSERT("conn->current_msg == NULL", conn
->current_msg
== NULL
);
407 * Setup a tcp_pcb with the correct callback function pointers
408 * and their arguments.
410 * @param conn the TCP netconn to setup
413 setup_tcp(struct netconn
*conn
)
419 tcp_recv(pcb
, recv_tcp
);
420 tcp_sent(pcb
, sent_tcp
);
421 tcp_poll(pcb
, poll_tcp
, 4);
422 tcp_err(pcb
, err_tcp
);
426 * Accept callback function for TCP netconns.
427 * Allocates a new netconn and posts that to conn->acceptmbox.
429 * @see tcp.h (struct tcp_pcb_listen.accept) for parameters and return value
432 accept_function(void *arg
, struct tcp_pcb
*newpcb
, err_t err
)
434 struct netconn
*newconn
;
435 struct netconn
*conn
= (struct netconn
*)arg
;
437 LWIP_DEBUGF(API_MSG_DEBUG
, ("accept_function: newpcb->tate: %s\n", tcp_debug_state_str(newpcb
->state
)));
439 if (!sys_mbox_valid(&conn
->acceptmbox
)) {
440 LWIP_DEBUGF(API_MSG_DEBUG
, ("accept_function: acceptmbox already deleted\n"));
444 /* We have to set the callback here even though
445 * the new socket is unknown. conn->socket is marked as -1. */
446 newconn
= netconn_alloc(conn
->type
, conn
->callback
);
447 if (newconn
== NULL
) {
450 newconn
->pcb
.tcp
= newpcb
;
452 /* no protection: when creating the pcb, the netconn is not yet known
453 to the application thread */
454 newconn
->last_err
= err
;
456 if (sys_mbox_trypost(&conn
->acceptmbox
, newconn
) != ERR_OK
) {
457 /* When returning != ERR_OK, the pcb is aborted in tcp_process(),
458 so do nothing here! */
459 newconn
->pcb
.tcp
= NULL
;
460 /* no need to drain since we know the recvmbox is empty. */
461 sys_mbox_free(&newconn
->recvmbox
);
462 sys_mbox_set_invalid(&newconn
->recvmbox
);
463 netconn_free(newconn
);
466 /* Register event with callback */
467 API_EVENT(conn
, NETCONN_EVT_RCVPLUS
, 0);
472 #endif /* LWIP_TCP */
475 * Create a new pcb of a specific type.
476 * Called from do_newconn().
478 * @param msg the api_msg_msg describing the connection type
479 * @return msg->conn->err, but the return value is currently ignored
482 pcb_new(struct api_msg_msg
*msg
)
484 LWIP_ASSERT("pcb_new: pcb already allocated", msg
->conn
->pcb
.tcp
== NULL
);
486 /* Allocate a PCB for this connection */
487 switch(NETCONNTYPE_GROUP(msg
->conn
->type
)) {
490 msg
->conn
->pcb
.raw
= raw_new(msg
->msg
.n
.proto
);
491 if(msg
->conn
->pcb
.raw
== NULL
) {
495 raw_recv(msg
->conn
->pcb
.raw
, recv_raw
, msg
->conn
);
497 #endif /* LWIP_RAW */
500 msg
->conn
->pcb
.udp
= udp_new();
501 if(msg
->conn
->pcb
.udp
== NULL
) {
506 if (msg
->conn
->type
==NETCONN_UDPLITE
) {
507 udp_setflags(msg
->conn
->pcb
.udp
, UDP_FLAGS_UDPLITE
);
509 #endif /* LWIP_UDPLITE */
510 if (msg
->conn
->type
==NETCONN_UDPNOCHKSUM
) {
511 udp_setflags(msg
->conn
->pcb
.udp
, UDP_FLAGS_NOCHKSUM
);
513 udp_recv(msg
->conn
->pcb
.udp
, recv_udp
, msg
->conn
);
515 #endif /* LWIP_UDP */
518 msg
->conn
->pcb
.tcp
= tcp_new();
519 if(msg
->conn
->pcb
.tcp
== NULL
) {
523 setup_tcp(msg
->conn
);
525 #endif /* LWIP_TCP */
527 /* Unsupported netconn type, e.g. protocol disabled */
534 * Create a new pcb of a specific type inside a netconn.
535 * Called from netconn_new_with_proto_and_callback.
537 * @param msg the api_msg_msg describing the connection type
540 do_newconn(struct api_msg_msg
*msg
)
543 if(msg
->conn
->pcb
.tcp
== NULL
) {
546 /* Else? This "new" connection already has a PCB allocated. */
547 /* Is this an error condition? Should it be deleted? */
548 /* We currently just are happy and return. */
550 TCPIP_APIMSG_ACK(msg
);
554 * Create a new netconn (of a specific type) that has a callback function.
555 * The corresponding pcb is NOT created!
557 * @param t the type of 'connection' to create (@see enum netconn_type)
558 * @param proto the IP protocol for RAW IP pcbs
559 * @param callback a function to call on status changes (RX available, TX'ed)
560 * @return a newly allocated struct netconn or
561 * NULL on memory error
564 netconn_alloc(enum netconn_type t
, netconn_callback callback
)
566 struct netconn
*conn
;
569 conn
= (struct netconn
*)memp_malloc(MEMP_NETCONN
);
574 conn
->last_err
= ERR_OK
;
576 conn
->pcb
.tcp
= NULL
;
578 #if (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_UDP_RECVMBOX_SIZE) && \
579 (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_TCP_RECVMBOX_SIZE)
580 size
= DEFAULT_RAW_RECVMBOX_SIZE
;
582 switch(NETCONNTYPE_GROUP(t
)) {
585 size
= DEFAULT_RAW_RECVMBOX_SIZE
;
587 #endif /* LWIP_RAW */
590 size
= DEFAULT_UDP_RECVMBOX_SIZE
;
592 #endif /* LWIP_UDP */
595 size
= DEFAULT_TCP_RECVMBOX_SIZE
;
597 #endif /* LWIP_TCP */
599 LWIP_ASSERT("netconn_alloc: undefined netconn_type", 0);
604 if (sys_sem_new(&conn
->op_completed
, 0) != ERR_OK
) {
605 memp_free(MEMP_NETCONN
, conn
);
608 if (sys_mbox_new(&conn
->recvmbox
, size
) != ERR_OK
) {
609 sys_sem_free(&conn
->op_completed
);
610 memp_free(MEMP_NETCONN
, conn
);
615 sys_mbox_set_invalid(&conn
->acceptmbox
);
617 conn
->state
= NETCONN_NONE
;
619 /* initialize socket to -1 since 0 is a valid socket */
621 #endif /* LWIP_SOCKET */
622 conn
->callback
= callback
;
624 conn
->current_msg
= NULL
;
625 conn
->write_offset
= 0;
626 #endif /* LWIP_TCP */
628 conn
->recv_timeout
= 0;
629 #endif /* LWIP_SO_RCVTIMEO */
631 conn
->recv_bufsize
= RECV_BUFSIZE_DEFAULT
;
632 conn
->recv_avail
= 0;
633 #endif /* LWIP_SO_RCVBUF */
639 * Delete a netconn and all its resources.
640 * The pcb is NOT freed (since we might not be in the right thread context do this).
642 * @param conn the netconn to free
645 netconn_free(struct netconn
*conn
)
647 LWIP_ASSERT("PCB must be deallocated outside this function", conn
->pcb
.tcp
== NULL
);
648 LWIP_ASSERT("recvmbox must be deallocated before calling this function",
649 !sys_mbox_valid(&conn
->recvmbox
));
651 LWIP_ASSERT("acceptmbox must be deallocated before calling this function",
652 !sys_mbox_valid(&conn
->acceptmbox
));
653 #endif /* LWIP_TCP */
655 sys_sem_free(&conn
->op_completed
);
656 sys_sem_set_invalid(&conn
->op_completed
);
658 memp_free(MEMP_NETCONN
, conn
);
662 * Delete rcvmbox and acceptmbox of a netconn and free the left-over data in
665 * @param conn the netconn to free
666 * @bytes_drained bytes drained from recvmbox
667 * @accepts_drained pending connections drained from acceptmbox
670 netconn_drain(struct netconn
*conn
)
675 #endif /* LWIP_TCP */
677 /* This runs in tcpip_thread, so we don't need to lock against rx packets */
679 /* Delete and drain the recvmbox. */
680 if (sys_mbox_valid(&conn
->recvmbox
)) {
681 while (sys_mbox_tryfetch(&conn
->recvmbox
, &mem
) != SYS_MBOX_EMPTY
) {
683 if (conn
->type
== NETCONN_TCP
) {
685 p
= (struct pbuf
*)mem
;
686 /* pcb might be set to NULL already by err_tcp() */
687 if (conn
->pcb
.tcp
!= NULL
) {
688 tcp_recved(conn
->pcb
.tcp
, p
->tot_len
);
693 #endif /* LWIP_TCP */
695 netbuf_delete((struct netbuf
*)mem
);
698 sys_mbox_free(&conn
->recvmbox
);
699 sys_mbox_set_invalid(&conn
->recvmbox
);
702 /* Delete and drain the acceptmbox. */
704 if (sys_mbox_valid(&conn
->acceptmbox
)) {
705 while (sys_mbox_tryfetch(&conn
->acceptmbox
, &mem
) != SYS_MBOX_EMPTY
) {
706 struct netconn
*newconn
= (struct netconn
*)mem
;
707 /* Only tcp pcbs have an acceptmbox, so no need to check conn->type */
708 /* pcb might be set to NULL already by err_tcp() */
709 if (conn
->pcb
.tcp
!= NULL
) {
710 tcp_accepted(conn
->pcb
.tcp
);
713 netconn_drain(newconn
);
714 if (newconn
->pcb
.tcp
!= NULL
) {
715 tcp_abort(newconn
->pcb
.tcp
);
716 newconn
->pcb
.tcp
= NULL
;
718 netconn_free(newconn
);
720 sys_mbox_free(&conn
->acceptmbox
);
721 sys_mbox_set_invalid(&conn
->acceptmbox
);
723 #endif /* LWIP_TCP */
728 * Internal helper function to close a TCP netconn: since this sometimes
729 * doesn't work at the first attempt, this function is called from multiple
732 * @param conn the TCP netconn to close
735 do_close_internal(struct netconn
*conn
)
738 u8_t shut
, shut_rx
, shut_tx
, close
;
740 LWIP_ASSERT("invalid conn", (conn
!= NULL
));
741 LWIP_ASSERT("this is for tcp netconns only", (conn
->type
== NETCONN_TCP
));
742 LWIP_ASSERT("conn must be in state NETCONN_CLOSE", (conn
->state
== NETCONN_CLOSE
));
743 LWIP_ASSERT("pcb already closed", (conn
->pcb
.tcp
!= NULL
));
744 LWIP_ASSERT("conn->current_msg != NULL", conn
->current_msg
!= NULL
);
746 shut
= conn
->current_msg
->msg
.sd
.shut
;
747 shut_rx
= shut
& NETCONN_SHUT_RD
;
748 shut_tx
= shut
& NETCONN_SHUT_WR
;
749 /* shutting down both ends is the same as closing */
750 close
= shut
== NETCONN_SHUT_RDWR
;
752 /* Set back some callback pointers */
754 tcp_arg(conn
->pcb
.tcp
, NULL
);
756 if (conn
->pcb
.tcp
->state
== LISTEN
) {
757 tcp_accept(conn
->pcb
.tcp
, NULL
);
759 /* some callbacks have to be reset if tcp_close is not successful */
761 tcp_recv(conn
->pcb
.tcp
, NULL
);
762 tcp_accept(conn
->pcb
.tcp
, NULL
);
765 tcp_sent(conn
->pcb
.tcp
, NULL
);
768 tcp_poll(conn
->pcb
.tcp
, NULL
, 4);
769 tcp_err(conn
->pcb
.tcp
, NULL
);
772 /* Try to close the connection */
773 if (shut
== NETCONN_SHUT_RDWR
) {
774 err
= tcp_close(conn
->pcb
.tcp
);
776 err
= tcp_shutdown(conn
->pcb
.tcp
, shut
& NETCONN_SHUT_RD
, shut
& NETCONN_SHUT_WR
);
779 /* Closing succeeded */
780 conn
->current_msg
->err
= ERR_OK
;
781 conn
->current_msg
= NULL
;
782 conn
->state
= NETCONN_NONE
;
783 /* Set back some callback pointers as conn is going away */
784 conn
->pcb
.tcp
= NULL
;
785 /* Trigger select() in socket layer. Make sure everybody notices activity
786 on the connection, error first! */
788 API_EVENT(conn
, NETCONN_EVT_ERROR
, 0);
791 API_EVENT(conn
, NETCONN_EVT_RCVPLUS
, 0);
794 API_EVENT(conn
, NETCONN_EVT_SENDPLUS
, 0);
796 /* wake up the application task */
797 sys_sem_signal(&conn
->op_completed
);
799 /* Closing failed, restore some of the callbacks */
800 /* Closing of listen pcb will never fail! */
801 LWIP_ASSERT("Closing a listen pcb may not fail!", (conn
->pcb
.tcp
->state
!= LISTEN
));
802 tcp_sent(conn
->pcb
.tcp
, sent_tcp
);
803 tcp_poll(conn
->pcb
.tcp
, poll_tcp
, 4);
804 tcp_err(conn
->pcb
.tcp
, err_tcp
);
805 tcp_arg(conn
->pcb
.tcp
, conn
);
806 /* don't restore recv callback: we don't want to receive any more data */
808 /* If closing didn't succeed, we get called again either
809 from poll_tcp or from sent_tcp */
811 #endif /* LWIP_TCP */
814 * Delete the pcb inside a netconn.
815 * Called from netconn_delete.
817 * @param msg the api_msg_msg pointing to the connection
820 do_delconn(struct api_msg_msg
*msg
)
822 /* @todo TCP: abort running write/connect? */
823 if ((msg
->conn
->state
!= NETCONN_NONE
) &&
824 (msg
->conn
->state
!= NETCONN_LISTEN
) &&
825 (msg
->conn
->state
!= NETCONN_CONNECT
)) {
826 /* this only happens for TCP netconns */
827 LWIP_ASSERT("msg->conn->type == NETCONN_TCP", msg
->conn
->type
== NETCONN_TCP
);
828 msg
->err
= ERR_INPROGRESS
;
830 LWIP_ASSERT("blocking connect in progress",
831 (msg
->conn
->state
!= NETCONN_CONNECT
) || IN_NONBLOCKING_CONNECT(msg
->conn
));
832 /* Drain and delete mboxes */
833 netconn_drain(msg
->conn
);
835 if (msg
->conn
->pcb
.tcp
!= NULL
) {
837 switch (NETCONNTYPE_GROUP(msg
->conn
->type
)) {
840 raw_remove(msg
->conn
->pcb
.raw
);
842 #endif /* LWIP_RAW */
845 msg
->conn
->pcb
.udp
->recv_arg
= NULL
;
846 udp_remove(msg
->conn
->pcb
.udp
);
848 #endif /* LWIP_UDP */
851 LWIP_ASSERT("already writing or closing", msg
->conn
->current_msg
== NULL
&&
852 msg
->conn
->write_offset
== 0);
853 msg
->conn
->state
= NETCONN_CLOSE
;
854 msg
->msg
.sd
.shut
= NETCONN_SHUT_RDWR
;
855 msg
->conn
->current_msg
= msg
;
856 do_close_internal(msg
->conn
);
857 /* API_EVENT is called inside do_close_internal, before releasing
858 the application thread, so we can return at this point! */
860 #endif /* LWIP_TCP */
864 msg
->conn
->pcb
.tcp
= NULL
;
866 /* tcp netconns don't come here! */
868 /* @todo: this lets select make the socket readable and writable,
869 which is wrong! errfd instead? */
870 API_EVENT(msg
->conn
, NETCONN_EVT_RCVPLUS
, 0);
871 API_EVENT(msg
->conn
, NETCONN_EVT_SENDPLUS
, 0);
873 if (sys_sem_valid(&msg
->conn
->op_completed
)) {
874 sys_sem_signal(&msg
->conn
->op_completed
);
879 * Bind a pcb contained in a netconn
880 * Called from netconn_bind.
882 * @param msg the api_msg_msg pointing to the connection and containing
883 * the IP address and port to bind to
886 do_bind(struct api_msg_msg
*msg
)
888 if (ERR_IS_FATAL(msg
->conn
->last_err
)) {
889 msg
->err
= msg
->conn
->last_err
;
892 if (msg
->conn
->pcb
.tcp
!= NULL
) {
893 switch (NETCONNTYPE_GROUP(msg
->conn
->type
)) {
896 msg
->err
= raw_bind(msg
->conn
->pcb
.raw
, msg
->msg
.bc
.ipaddr
);
898 #endif /* LWIP_RAW */
901 msg
->err
= udp_bind(msg
->conn
->pcb
.udp
, msg
->msg
.bc
.ipaddr
, msg
->msg
.bc
.port
);
903 #endif /* LWIP_UDP */
906 msg
->err
= tcp_bind(msg
->conn
->pcb
.tcp
, msg
->msg
.bc
.ipaddr
, msg
->msg
.bc
.port
);
908 #endif /* LWIP_TCP */
914 TCPIP_APIMSG_ACK(msg
);
919 * TCP callback function if a connection (opened by tcp_connect/do_connect) has
920 * been established (or reset by the remote host).
922 * @see tcp.h (struct tcp_pcb.connected) for parameters and return values
925 do_connected(void *arg
, struct tcp_pcb
*pcb
, err_t err
)
927 struct netconn
*conn
;
930 LWIP_UNUSED_ARG(pcb
);
932 conn
= (struct netconn
*)arg
;
938 LWIP_ASSERT("conn->state == NETCONN_CONNECT", conn
->state
== NETCONN_CONNECT
);
939 LWIP_ASSERT("(conn->current_msg != NULL) || conn->in_non_blocking_connect",
940 (conn
->current_msg
!= NULL
) || IN_NONBLOCKING_CONNECT(conn
));
942 if (conn
->current_msg
!= NULL
) {
943 conn
->current_msg
->err
= err
;
945 if ((conn
->type
== NETCONN_TCP
) && (err
== ERR_OK
)) {
948 was_blocking
= !IN_NONBLOCKING_CONNECT(conn
);
949 SET_NONBLOCKING_CONNECT(conn
, 0);
950 conn
->current_msg
= NULL
;
951 conn
->state
= NETCONN_NONE
;
953 NETCONN_SET_SAFE_ERR(conn
, ERR_OK
);
955 API_EVENT(conn
, NETCONN_EVT_SENDPLUS
, 0);
958 sys_sem_signal(&conn
->op_completed
);
962 #endif /* LWIP_TCP */
965 * Connect a pcb contained inside a netconn
966 * Called from netconn_connect.
968 * @param msg the api_msg_msg pointing to the connection and containing
969 * the IP address and port to connect to
972 do_connect(struct api_msg_msg
*msg
)
974 if (msg
->conn
->pcb
.tcp
== NULL
) {
975 /* This may happen when calling netconn_connect() a second time */
978 switch (NETCONNTYPE_GROUP(msg
->conn
->type
)) {
981 msg
->err
= raw_connect(msg
->conn
->pcb
.raw
, msg
->msg
.bc
.ipaddr
);
983 #endif /* LWIP_RAW */
986 msg
->err
= udp_connect(msg
->conn
->pcb
.udp
, msg
->msg
.bc
.ipaddr
, msg
->msg
.bc
.port
);
988 #endif /* LWIP_UDP */
991 /* Prevent connect while doing any other action. */
992 if (msg
->conn
->state
!= NETCONN_NONE
) {
993 msg
->err
= ERR_ISCONN
;
995 setup_tcp(msg
->conn
);
996 msg
->err
= tcp_connect(msg
->conn
->pcb
.tcp
, msg
->msg
.bc
.ipaddr
,
997 msg
->msg
.bc
.port
, do_connected
);
998 if (msg
->err
== ERR_OK
) {
999 u8_t non_blocking
= netconn_is_nonblocking(msg
->conn
);
1000 msg
->conn
->state
= NETCONN_CONNECT
;
1001 SET_NONBLOCKING_CONNECT(msg
->conn
, non_blocking
);
1003 msg
->err
= ERR_INPROGRESS
;
1005 msg
->conn
->current_msg
= msg
;
1006 /* sys_sem_signal() is called from do_connected (or err_tcp()),
1007 * when the connection is established! */
1013 #endif /* LWIP_TCP */
1015 LWIP_ERROR("Invalid netconn type", 0, do{ msg
->err
= ERR_VAL
; }while(0));
1019 sys_sem_signal(&msg
->conn
->op_completed
);
1023 * Connect a pcb contained inside a netconn
1024 * Only used for UDP netconns.
1025 * Called from netconn_disconnect.
1027 * @param msg the api_msg_msg pointing to the connection to disconnect
1030 do_disconnect(struct api_msg_msg
*msg
)
1033 if (NETCONNTYPE_GROUP(msg
->conn
->type
) == NETCONN_UDP
) {
1034 udp_disconnect(msg
->conn
->pcb
.udp
);
1037 #endif /* LWIP_UDP */
1041 TCPIP_APIMSG_ACK(msg
);
1046 * Set a TCP pcb contained in a netconn into listen mode
1047 * Called from netconn_listen.
1049 * @param msg the api_msg_msg pointing to the connection
1052 do_listen(struct api_msg_msg
*msg
)
1054 if (ERR_IS_FATAL(msg
->conn
->last_err
)) {
1055 msg
->err
= msg
->conn
->last_err
;
1057 msg
->err
= ERR_CONN
;
1058 if (msg
->conn
->pcb
.tcp
!= NULL
) {
1059 if (msg
->conn
->type
== NETCONN_TCP
) {
1060 if (msg
->conn
->state
== NETCONN_NONE
) {
1061 #if TCP_LISTEN_BACKLOG
1062 struct tcp_pcb
* lpcb
= tcp_listen_with_backlog(msg
->conn
->pcb
.tcp
, msg
->msg
.lb
.backlog
);
1063 #else /* TCP_LISTEN_BACKLOG */
1064 struct tcp_pcb
* lpcb
= tcp_listen(msg
->conn
->pcb
.tcp
);
1065 #endif /* TCP_LISTEN_BACKLOG */
1067 /* in this case, the old pcb is still allocated */
1070 /* delete the recvmbox and allocate the acceptmbox */
1071 if (sys_mbox_valid(&msg
->conn
->recvmbox
)) {
1072 /** @todo: should we drain the recvmbox here? */
1073 sys_mbox_free(&msg
->conn
->recvmbox
);
1074 sys_mbox_set_invalid(&msg
->conn
->recvmbox
);
1077 if (!sys_mbox_valid(&msg
->conn
->acceptmbox
)) {
1078 msg
->err
= sys_mbox_new(&msg
->conn
->acceptmbox
, DEFAULT_ACCEPTMBOX_SIZE
);
1080 if (msg
->err
== ERR_OK
) {
1081 msg
->conn
->state
= NETCONN_LISTEN
;
1082 msg
->conn
->pcb
.tcp
= lpcb
;
1083 tcp_arg(msg
->conn
->pcb
.tcp
, msg
->conn
);
1084 tcp_accept(msg
->conn
->pcb
.tcp
, accept_function
);
1086 /* since the old pcb is already deallocated, free lpcb now */
1088 msg
->conn
->pcb
.tcp
= NULL
;
1095 TCPIP_APIMSG_ACK(msg
);
1097 #endif /* LWIP_TCP */
1100 * Send some data on a RAW or UDP pcb contained in a netconn
1101 * Called from netconn_send
1103 * @param msg the api_msg_msg pointing to the connection
1106 do_send(struct api_msg_msg
*msg
)
1108 if (ERR_IS_FATAL(msg
->conn
->last_err
)) {
1109 msg
->err
= msg
->conn
->last_err
;
1111 msg
->err
= ERR_CONN
;
1112 if (msg
->conn
->pcb
.tcp
!= NULL
) {
1113 switch (NETCONNTYPE_GROUP(msg
->conn
->type
)) {
1116 if (ip_addr_isany(&msg
->msg
.b
->addr
)) {
1117 msg
->err
= raw_send(msg
->conn
->pcb
.raw
, msg
->msg
.b
->p
);
1119 msg
->err
= raw_sendto(msg
->conn
->pcb
.raw
, msg
->msg
.b
->p
, &msg
->msg
.b
->addr
);
1125 #if LWIP_CHECKSUM_ON_COPY
1126 if (ip_addr_isany(&msg
->msg
.b
->addr
)) {
1127 msg
->err
= udp_send_chksum(msg
->conn
->pcb
.udp
, msg
->msg
.b
->p
,
1128 msg
->msg
.b
->flags
& NETBUF_FLAG_CHKSUM
, msg
->msg
.b
->toport_chksum
);
1130 msg
->err
= udp_sendto_chksum(msg
->conn
->pcb
.udp
, msg
->msg
.b
->p
,
1131 &msg
->msg
.b
->addr
, msg
->msg
.b
->port
,
1132 msg
->msg
.b
->flags
& NETBUF_FLAG_CHKSUM
, msg
->msg
.b
->toport_chksum
);
1134 #else /* LWIP_CHECKSUM_ON_COPY */
1135 if (ip_addr_isany(&msg
->msg
.b
->addr
)) {
1136 msg
->err
= udp_send(msg
->conn
->pcb
.udp
, msg
->msg
.b
->p
);
1138 msg
->err
= udp_sendto(msg
->conn
->pcb
.udp
, msg
->msg
.b
->p
, &msg
->msg
.b
->addr
, msg
->msg
.b
->port
);
1140 #endif /* LWIP_CHECKSUM_ON_COPY */
1142 #endif /* LWIP_UDP */
1148 TCPIP_APIMSG_ACK(msg
);
1153 * Indicate data has been received from a TCP pcb contained in a netconn
1154 * Called from netconn_recv
1156 * @param msg the api_msg_msg pointing to the connection
1159 do_recv(struct api_msg_msg
*msg
)
1162 if (msg
->conn
->pcb
.tcp
!= NULL
) {
1163 if (msg
->conn
->type
== NETCONN_TCP
) {
1164 #if TCP_LISTEN_BACKLOG
1165 if (msg
->conn
->pcb
.tcp
->state
== LISTEN
) {
1166 tcp_accepted(msg
->conn
->pcb
.tcp
);
1168 #endif /* TCP_LISTEN_BACKLOG */
1170 u32_t remaining
= msg
->msg
.r
.len
;
1172 u16_t recved
= (remaining
> 0xffff) ? 0xffff : (u16_t
)remaining
;
1173 tcp_recved(msg
->conn
->pcb
.tcp
, recved
);
1174 remaining
-= recved
;
1175 }while(remaining
!= 0);
1179 TCPIP_APIMSG_ACK(msg
);
1183 * See if more data needs to be written from a previous call to netconn_write.
1184 * Called initially from do_write. If the first call can't send all data
1185 * (because of low memory or empty send-buffer), this function is called again
1186 * from sent_tcp() or poll_tcp() to send more data. If all data is sent, the
1187 * blocking application thread (waiting in netconn_write) is released.
1189 * @param conn netconn (that is currently in state NETCONN_WRITE) to process
1191 * ERR_MEM if LWIP_TCPIP_CORE_LOCKING=1 and sending hasn't yet finished
1194 do_writemore(struct netconn
*conn
)
1198 u16_t len
, available
;
1199 u8_t write_finished
= 0;
1201 u8_t dontblock
= netconn_is_nonblocking(conn
) ||
1202 (conn
->current_msg
->msg
.w
.apiflags
& NETCONN_DONTBLOCK
);
1203 u8_t apiflags
= conn
->current_msg
->msg
.w
.apiflags
;
1205 LWIP_ASSERT("conn != NULL", conn
!= NULL
);
1206 LWIP_ASSERT("conn->state == NETCONN_WRITE", (conn
->state
== NETCONN_WRITE
));
1207 LWIP_ASSERT("conn->current_msg != NULL", conn
->current_msg
!= NULL
);
1208 LWIP_ASSERT("conn->pcb.tcp != NULL", conn
->pcb
.tcp
!= NULL
);
1209 LWIP_ASSERT("conn->write_offset < conn->current_msg->msg.w.len",
1210 conn
->write_offset
< conn
->current_msg
->msg
.w
.len
);
1212 dataptr
= (u8_t
*)conn
->current_msg
->msg
.w
.dataptr
+ conn
->write_offset
;
1213 diff
= conn
->current_msg
->msg
.w
.len
- conn
->write_offset
;
1214 if (diff
> 0xffffUL
) { /* max_u16_t */
1216 #if LWIP_TCPIP_CORE_LOCKING
1217 conn
->flags
|= NETCONN_FLAG_WRITE_DELAYED
;
1219 apiflags
|= TCP_WRITE_FLAG_MORE
;
1223 available
= tcp_sndbuf(conn
->pcb
.tcp
);
1224 if (available
< len
) {
1225 /* don't try to write more than sendbuf */
1227 #if LWIP_TCPIP_CORE_LOCKING
1228 conn
->flags
|= NETCONN_FLAG_WRITE_DELAYED
;
1230 apiflags
|= TCP_WRITE_FLAG_MORE
;
1232 if (dontblock
&& (len
< conn
->current_msg
->msg
.w
.len
)) {
1233 /* failed to send all data at once -> nonblocking write not possible */
1236 if (err
== ERR_OK
) {
1237 LWIP_ASSERT("do_writemore: invalid length!", ((conn
->write_offset
+ len
) <= conn
->current_msg
->msg
.w
.len
));
1238 err
= tcp_write(conn
->pcb
.tcp
, dataptr
, len
, apiflags
);
1240 if (dontblock
&& (err
== ERR_MEM
)) {
1241 /* nonblocking write failed */
1243 err
= ERR_WOULDBLOCK
;
1244 /* let poll_tcp check writable space to mark the pcb
1246 conn
->flags
|= NETCONN_FLAG_CHECK_WRITESPACE
;
1247 /* let select mark this pcb as non-writable. */
1248 API_EVENT(conn
, NETCONN_EVT_SENDMINUS
, len
);
1250 /* if OK or memory error, check available space */
1251 if (((err
== ERR_OK
) || (err
== ERR_MEM
)) &&
1252 ((tcp_sndbuf(conn
->pcb
.tcp
) <= TCP_SNDLOWAT
) ||
1253 (tcp_sndqueuelen(conn
->pcb
.tcp
) >= TCP_SNDQUEUELOWAT
))) {
1254 /* The queued byte- or pbuf-count exceeds the configured low-water limit,
1255 let select mark this pcb as non-writable. */
1256 API_EVENT(conn
, NETCONN_EVT_SENDMINUS
, len
);
1259 if (err
== ERR_OK
) {
1260 conn
->write_offset
+= len
;
1261 if (conn
->write_offset
== conn
->current_msg
->msg
.w
.len
) {
1262 /* everything was written */
1264 conn
->write_offset
= 0;
1266 tcp_output(conn
->pcb
.tcp
);
1267 } else if (err
== ERR_MEM
) {
1268 /* If ERR_MEM, we wait for sent_tcp or poll_tcp to be called
1269 we do NOT return to the application thread, since ERR_MEM is
1270 only a temporary error! */
1272 /* tcp_write returned ERR_MEM, try tcp_output anyway */
1273 tcp_output(conn
->pcb
.tcp
);
1275 #if LWIP_TCPIP_CORE_LOCKING
1276 conn
->flags
|= NETCONN_FLAG_WRITE_DELAYED
;
1279 /* On errors != ERR_MEM, we don't try writing any more but return
1280 the error to the application thread. */
1285 if (write_finished
) {
1286 /* everything was written: set back connection state
1287 and back to application task */
1288 conn
->current_msg
->err
= err
;
1289 conn
->current_msg
= NULL
;
1290 conn
->state
= NETCONN_NONE
;
1291 #if LWIP_TCPIP_CORE_LOCKING
1292 if ((conn
->flags
& NETCONN_FLAG_WRITE_DELAYED
) != 0)
1295 sys_sem_signal(&conn
->op_completed
);
1298 #if LWIP_TCPIP_CORE_LOCKING
1304 #endif /* LWIP_TCP */
1307 * Send some data on a TCP pcb contained in a netconn
1308 * Called from netconn_write
1310 * @param msg the api_msg_msg pointing to the connection
1313 do_write(struct api_msg_msg
*msg
)
1315 if (ERR_IS_FATAL(msg
->conn
->last_err
)) {
1316 msg
->err
= msg
->conn
->last_err
;
1318 if (msg
->conn
->type
== NETCONN_TCP
) {
1320 if (msg
->conn
->state
!= NETCONN_NONE
) {
1321 /* netconn is connecting, closing or in blocking write */
1322 msg
->err
= ERR_INPROGRESS
;
1323 } else if (msg
->conn
->pcb
.tcp
!= NULL
) {
1324 msg
->conn
->state
= NETCONN_WRITE
;
1325 /* set all the variables used by do_writemore */
1326 LWIP_ASSERT("already writing or closing", msg
->conn
->current_msg
== NULL
&&
1327 msg
->conn
->write_offset
== 0);
1328 LWIP_ASSERT("msg->msg.w.len != 0", msg
->msg
.w
.len
!= 0);
1329 msg
->conn
->current_msg
= msg
;
1330 msg
->conn
->write_offset
= 0;
1331 #if LWIP_TCPIP_CORE_LOCKING
1332 msg
->conn
->flags
&= ~NETCONN_FLAG_WRITE_DELAYED
;
1333 if (do_writemore(msg
->conn
) != ERR_OK
) {
1334 LWIP_ASSERT("state!", msg
->conn
->state
== NETCONN_WRITE
);
1335 UNLOCK_TCPIP_CORE();
1336 sys_arch_sem_wait(&msg
->conn
->op_completed
, 0);
1338 LWIP_ASSERT("state!", msg
->conn
->state
== NETCONN_NONE
);
1340 #else /* LWIP_TCPIP_CORE_LOCKING */
1341 do_writemore(msg
->conn
);
1342 #endif /* LWIP_TCPIP_CORE_LOCKING */
1343 /* for both cases: if do_writemore was called, don't ACK the APIMSG
1344 since do_writemore ACKs it! */
1347 msg
->err
= ERR_CONN
;
1349 #else /* LWIP_TCP */
1351 #endif /* LWIP_TCP */
1352 #if (LWIP_UDP || LWIP_RAW)
1355 #endif /* (LWIP_UDP || LWIP_RAW) */
1358 TCPIP_APIMSG_ACK(msg
);
1362 * Return a connection's local or remote address
1363 * Called from netconn_getaddr
1365 * @param msg the api_msg_msg pointing to the connection
1368 do_getaddr(struct api_msg_msg
*msg
)
1370 if (msg
->conn
->pcb
.ip
!= NULL
) {
1371 *(msg
->msg
.ad
.ipaddr
) = (msg
->msg
.ad
.local
? msg
->conn
->pcb
.ip
->local_ip
:
1372 msg
->conn
->pcb
.ip
->remote_ip
);
1375 switch (NETCONNTYPE_GROUP(msg
->conn
->type
)) {
1378 if (msg
->msg
.ad
.local
) {
1379 *(msg
->msg
.ad
.port
) = msg
->conn
->pcb
.raw
->protocol
;
1381 /* return an error as connecting is only a helper for upper layers */
1382 msg
->err
= ERR_CONN
;
1385 #endif /* LWIP_RAW */
1388 if (msg
->msg
.ad
.local
) {
1389 *(msg
->msg
.ad
.port
) = msg
->conn
->pcb
.udp
->local_port
;
1391 if ((msg
->conn
->pcb
.udp
->flags
& UDP_FLAGS_CONNECTED
) == 0) {
1392 msg
->err
= ERR_CONN
;
1394 *(msg
->msg
.ad
.port
) = msg
->conn
->pcb
.udp
->remote_port
;
1398 #endif /* LWIP_UDP */
1401 *(msg
->msg
.ad
.port
) = (msg
->msg
.ad
.local
?msg
->conn
->pcb
.tcp
->local_port
:msg
->conn
->pcb
.tcp
->remote_port
);
1403 #endif /* LWIP_TCP */
1405 LWIP_ASSERT("invalid netconn_type", 0);
1409 msg
->err
= ERR_CONN
;
1411 TCPIP_APIMSG_ACK(msg
);
1415 * Close a TCP pcb contained in a netconn
1416 * Called from netconn_close
1418 * @param msg the api_msg_msg pointing to the connection
1421 do_close(struct api_msg_msg
*msg
)
1424 /* @todo: abort running write/connect? */
1425 if ((msg
->conn
->state
!= NETCONN_NONE
) && (msg
->conn
->state
!= NETCONN_LISTEN
)) {
1426 /* this only happens for TCP netconns */
1427 LWIP_ASSERT("msg->conn->type == NETCONN_TCP", msg
->conn
->type
== NETCONN_TCP
);
1428 msg
->err
= ERR_INPROGRESS
;
1429 } else if ((msg
->conn
->pcb
.tcp
!= NULL
) && (msg
->conn
->type
== NETCONN_TCP
)) {
1430 if ((msg
->msg
.sd
.shut
!= NETCONN_SHUT_RDWR
) && (msg
->conn
->state
== NETCONN_LISTEN
)) {
1431 /* LISTEN doesn't support half shutdown */
1432 msg
->err
= ERR_CONN
;
1434 if (msg
->msg
.sd
.shut
& NETCONN_SHUT_RD
) {
1435 /* Drain and delete mboxes */
1436 netconn_drain(msg
->conn
);
1438 LWIP_ASSERT("already writing or closing", msg
->conn
->current_msg
== NULL
&&
1439 msg
->conn
->write_offset
== 0);
1440 msg
->conn
->state
= NETCONN_CLOSE
;
1441 msg
->conn
->current_msg
= msg
;
1442 do_close_internal(msg
->conn
);
1443 /* for tcp netconns, do_close_internal ACKs the message */
1447 #endif /* LWIP_TCP */
1451 sys_sem_signal(&msg
->conn
->op_completed
);
1456 * Join multicast groups for UDP netconns.
1457 * Called from netconn_join_leave_group
1459 * @param msg the api_msg_msg pointing to the connection
1462 do_join_leave_group(struct api_msg_msg
*msg
)
1464 if (ERR_IS_FATAL(msg
->conn
->last_err
)) {
1465 msg
->err
= msg
->conn
->last_err
;
1467 if (msg
->conn
->pcb
.tcp
!= NULL
) {
1468 if (NETCONNTYPE_GROUP(msg
->conn
->type
) == NETCONN_UDP
) {
1470 if (msg
->msg
.jl
.join_or_leave
== NETCONN_JOIN
) {
1471 msg
->err
= igmp_joingroup(msg
->msg
.jl
.netif_addr
, msg
->msg
.jl
.multiaddr
);
1473 msg
->err
= igmp_leavegroup(msg
->msg
.jl
.netif_addr
, msg
->msg
.jl
.multiaddr
);
1475 #endif /* LWIP_UDP */
1476 #if (LWIP_TCP || LWIP_RAW)
1479 #endif /* (LWIP_TCP || LWIP_RAW) */
1482 msg
->err
= ERR_CONN
;
1485 TCPIP_APIMSG_ACK(msg
);
1487 #endif /* LWIP_IGMP */
1491 * Callback function that is called when DNS name is resolved
1492 * (or on timeout). A waiting application thread is waked up by
1493 * signaling the semaphore.
1496 do_dns_found(const char *name
, ip_addr_t
*ipaddr
, void *arg
)
1498 struct dns_api_msg
*msg
= (struct dns_api_msg
*)arg
;
1500 LWIP_ASSERT("DNS response for wrong host name", strcmp(msg
->name
, name
) == 0);
1501 LWIP_UNUSED_ARG(name
);
1503 if (ipaddr
== NULL
) {
1504 /* timeout or memory error */
1505 *msg
->err
= ERR_VAL
;
1507 /* address was resolved */
1509 *msg
->addr
= *ipaddr
;
1511 /* wake up the application task waiting in netconn_gethostbyname */
1512 sys_sem_signal(msg
->sem
);
1516 * Execute a DNS query
1517 * Called from netconn_gethostbyname
1519 * @param arg the dns_api_msg pointing to the query
1522 do_gethostbyname(void *arg
)
1524 struct dns_api_msg
*msg
= (struct dns_api_msg
*)arg
;
1526 *msg
->err
= dns_gethostbyname(msg
->name
, msg
->addr
, do_dns_found
, msg
);
1527 if (*msg
->err
!= ERR_INPROGRESS
) {
1528 /* on error or immediate success, wake up the application
1529 * task waiting in netconn_gethostbyname */
1530 sys_sem_signal(msg
->sem
);
1533 #endif /* LWIP_DNS */
1535 #endif /* LWIP_NETCONN */