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 /* forward declarations */
59 static err_t
do_writemore(struct netconn
*conn
);
60 static void do_close_internal(struct netconn
*conn
);
65 * Receive callback function for RAW netconns.
66 * Doesn't 'eat' the packet, only references it and sends it to
69 * @see raw.h (struct raw_pcb.recv) for parameters and return value
72 recv_raw(void *arg
, struct raw_pcb
*pcb
, struct pbuf
*p
,
80 #endif /* LWIP_SO_RCVBUF */
82 LWIP_UNUSED_ARG(addr
);
86 SYS_ARCH_GET(conn
->recv_avail
, recv_avail
);
87 if ((conn
!= NULL
) && (conn
->recvmbox
!= SYS_MBOX_NULL
) &&
88 ((recv_avail
+ (int)(p
->tot_len
)) <= conn
->recv_bufsize
)) {
89 #else /* LWIP_SO_RCVBUF */
90 if ((conn
!= NULL
) && (conn
->recvmbox
!= SYS_MBOX_NULL
)) {
91 #endif /* LWIP_SO_RCVBUF */
92 /* copy the whole packet into new pbufs */
93 q
= pbuf_alloc(PBUF_RAW
, p
->tot_len
, PBUF_RAM
);
95 if (pbuf_copy(q
, p
) != ERR_OK
) {
102 buf
= memp_malloc(MEMP_NETBUF
);
110 buf
->addr
= &(((struct ip_hdr
*)(q
->payload
))->src
);
111 buf
->port
= pcb
->protocol
;
113 if (sys_mbox_trypost(conn
->recvmbox
, buf
) != ERR_OK
) {
117 SYS_ARCH_INC(conn
->recv_avail
, q
->tot_len
);
118 /* Register event with callback */
119 API_EVENT(conn
, NETCONN_EVT_RCVPLUS
, q
->tot_len
);
124 return 0; /* do not eat the packet */
130 * Receive callback function for UDP netconns.
131 * Posts the packet to conn->recvmbox or deletes it on memory error.
133 * @see udp.h (struct udp_pcb.recv) for parameters
136 recv_udp(void *arg
, struct udp_pcb
*pcb
, struct pbuf
*p
,
137 struct ip_addr
*addr
, u16_t port
)
140 struct netconn
*conn
;
143 #endif /* LWIP_SO_RCVBUF */
145 LWIP_UNUSED_ARG(pcb
); /* only used for asserts... */
146 LWIP_ASSERT("recv_udp must have a pcb argument", pcb
!= NULL
);
147 LWIP_ASSERT("recv_udp must have an argument", arg
!= NULL
);
149 LWIP_ASSERT("recv_udp: recv for wrong pcb!", conn
->pcb
.udp
== pcb
);
152 SYS_ARCH_GET(conn
->recv_avail
, recv_avail
);
153 if ((conn
== NULL
) || (conn
->recvmbox
== SYS_MBOX_NULL
) ||
154 ((recv_avail
+ (int)(p
->tot_len
)) > conn
->recv_bufsize
)) {
155 #else /* LWIP_SO_RCVBUF */
156 if ((conn
== NULL
) || (conn
->recvmbox
== SYS_MBOX_NULL
)) {
157 #endif /* LWIP_SO_RCVBUF */
162 buf
= memp_malloc(MEMP_NETBUF
);
171 #if LWIP_NETBUF_RECVINFO
173 const struct ip_hdr
* iphdr
= ip_current_header();
174 /* get the UDP header - always in the first pbuf, ensured by udp_input */
175 const struct udp_hdr
* udphdr
= (void*)(((char*)iphdr
) + IPH_LEN(iphdr
));
176 buf
->toaddr
= (struct ip_addr
*)&iphdr
->dest
;
177 buf
->toport
= udphdr
->dest
;
179 #endif /* LWIP_NETBUF_RECVINFO */
182 if (sys_mbox_trypost(conn
->recvmbox
, buf
) != ERR_OK
) {
186 SYS_ARCH_INC(conn
->recv_avail
, p
->tot_len
);
187 /* Register event with callback */
188 API_EVENT(conn
, NETCONN_EVT_RCVPLUS
, p
->tot_len
);
191 #endif /* LWIP_UDP */
195 * Receive callback function for TCP netconns.
196 * Posts the packet to conn->recvmbox, but doesn't delete it on errors.
198 * @see tcp.h (struct tcp_pcb.recv) for parameters and return value
201 recv_tcp(void *arg
, struct tcp_pcb
*pcb
, struct pbuf
*p
, err_t err
)
203 struct netconn
*conn
;
206 LWIP_UNUSED_ARG(pcb
);
207 LWIP_ASSERT("recv_tcp must have a pcb argument", pcb
!= NULL
);
208 LWIP_ASSERT("recv_tcp must have an argument", arg
!= NULL
);
210 LWIP_ASSERT("recv_tcp: recv for wrong pcb!", conn
->pcb
.tcp
== pcb
);
212 if ((conn
== NULL
) || (conn
->recvmbox
== SYS_MBOX_NULL
)) {
219 SYS_ARCH_INC(conn
->recv_avail
, len
);
224 if (sys_mbox_trypost(conn
->recvmbox
, p
) != ERR_OK
) {
227 /* Register event with callback */
228 API_EVENT(conn
, NETCONN_EVT_RCVPLUS
, len
);
235 * Poll callback function for TCP netconns.
236 * Wakes up an application thread that waits for a connection to close
237 * or data to be sent. The application thread then takes the
238 * appropriate action to go on.
240 * Signals the conn->sem.
241 * netconn_close waits for conn->sem if closing failed.
243 * @see tcp.h (struct tcp_pcb.poll) for parameters and return value
246 poll_tcp(void *arg
, struct tcp_pcb
*pcb
)
248 struct netconn
*conn
= arg
;
250 LWIP_UNUSED_ARG(pcb
);
251 LWIP_ASSERT("conn != NULL", (conn
!= NULL
));
253 if (conn
->state
== NETCONN_WRITE
) {
255 } else if (conn
->state
== NETCONN_CLOSE
) {
256 do_close_internal(conn
);
263 * Sent callback function for TCP netconns.
264 * Signals the conn->sem and calls API_EVENT.
265 * netconn_write waits for conn->sem if send buffer is low.
267 * @see tcp.h (struct tcp_pcb.sent) for parameters and return value
270 sent_tcp(void *arg
, struct tcp_pcb
*pcb
, u16_t len
)
272 struct netconn
*conn
= arg
;
274 LWIP_UNUSED_ARG(pcb
);
275 LWIP_ASSERT("conn != NULL", (conn
!= NULL
));
277 if (conn
->state
== NETCONN_WRITE
) {
278 LWIP_ASSERT("conn->pcb.tcp != NULL", conn
->pcb
.tcp
!= NULL
);
280 } else if (conn
->state
== NETCONN_CLOSE
) {
281 do_close_internal(conn
);
285 if ((conn
->pcb
.tcp
!= NULL
) && (tcp_sndbuf(conn
->pcb
.tcp
) > TCP_SNDLOWAT
)) {
286 API_EVENT(conn
, NETCONN_EVT_SENDPLUS
, len
);
294 * Error callback function for TCP netconns.
295 * Signals conn->sem, posts to all conn mboxes and calls API_EVENT.
296 * The application thread has then to decide what to do.
298 * @see tcp.h (struct tcp_pcb.err) for parameters
301 err_tcp(void *arg
, err_t err
)
303 struct netconn
*conn
;
306 LWIP_ASSERT("conn != NULL", (conn
!= NULL
));
308 conn
->pcb
.tcp
= NULL
;
311 if (conn
->recvmbox
!= SYS_MBOX_NULL
) {
312 /* Register event with callback */
313 API_EVENT(conn
, NETCONN_EVT_RCVPLUS
, 0);
314 sys_mbox_post(conn
->recvmbox
, NULL
);
316 if (conn
->op_completed
!= SYS_SEM_NULL
&& conn
->state
== NETCONN_CONNECT
) {
317 conn
->state
= NETCONN_NONE
;
318 sys_sem_signal(conn
->op_completed
);
320 if (conn
->acceptmbox
!= SYS_MBOX_NULL
) {
321 /* Register event with callback */
322 API_EVENT(conn
, NETCONN_EVT_RCVPLUS
, 0);
323 sys_mbox_post(conn
->acceptmbox
, NULL
);
325 if ((conn
->state
== NETCONN_WRITE
) || (conn
->state
== NETCONN_CLOSE
)) {
326 /* calling do_writemore/do_close_internal is not necessary
327 since the pcb has already been deleted! */
328 conn
->state
= NETCONN_NONE
;
329 /* wake up the waiting task */
330 sys_sem_signal(conn
->op_completed
);
335 * Setup a tcp_pcb with the correct callback function pointers
336 * and their arguments.
338 * @param conn the TCP netconn to setup
341 setup_tcp(struct netconn
*conn
)
347 tcp_recv(pcb
, recv_tcp
);
348 tcp_sent(pcb
, sent_tcp
);
349 tcp_poll(pcb
, poll_tcp
, 4);
350 tcp_err(pcb
, err_tcp
);
354 * Accept callback function for TCP netconns.
355 * Allocates a new netconn and posts that to conn->acceptmbox.
357 * @see tcp.h (struct tcp_pcb_listen.accept) for parameters and return value
360 accept_function(void *arg
, struct tcp_pcb
*newpcb
, err_t err
)
362 struct netconn
*newconn
;
363 struct netconn
*conn
;
367 tcp_debug_print_state(newpcb
->state
);
368 #endif /* TCP_DEBUG */
369 #endif /* API_MSG_DEBUG */
370 conn
= (struct netconn
*)arg
;
372 LWIP_ERROR("accept_function: invalid conn->acceptmbox",
373 conn
->acceptmbox
!= SYS_MBOX_NULL
, return ERR_VAL
;);
375 /* We have to set the callback here even though
376 * the new socket is unknown. conn->socket is marked as -1. */
377 newconn
= netconn_alloc(conn
->type
, conn
->callback
);
378 if (newconn
== NULL
) {
381 newconn
->pcb
.tcp
= newpcb
;
385 if (sys_mbox_trypost(conn
->acceptmbox
, newconn
) != ERR_OK
) {
386 /* When returning != ERR_OK, the connection is aborted in tcp_process(),
387 so do nothing here! */
388 newconn
->pcb
.tcp
= NULL
;
389 netconn_free(newconn
);
392 /* Register event with callback */
393 API_EVENT(conn
, NETCONN_EVT_RCVPLUS
, 0);
398 #endif /* LWIP_TCP */
401 * Create a new pcb of a specific type.
402 * Called from do_newconn().
404 * @param msg the api_msg_msg describing the connection type
405 * @return msg->conn->err, but the return value is currently ignored
408 pcb_new(struct api_msg_msg
*msg
)
410 msg
->conn
->err
= ERR_OK
;
412 LWIP_ASSERT("pcb_new: pcb already allocated", msg
->conn
->pcb
.tcp
== NULL
);
414 /* Allocate a PCB for this connection */
415 switch(NETCONNTYPE_GROUP(msg
->conn
->type
)) {
418 msg
->conn
->pcb
.raw
= raw_new(msg
->msg
.n
.proto
);
419 if(msg
->conn
->pcb
.raw
== NULL
) {
420 msg
->conn
->err
= ERR_MEM
;
423 raw_recv(msg
->conn
->pcb
.raw
, recv_raw
, msg
->conn
);
425 #endif /* LWIP_RAW */
428 msg
->conn
->pcb
.udp
= udp_new();
429 if(msg
->conn
->pcb
.udp
== NULL
) {
430 msg
->conn
->err
= ERR_MEM
;
434 if (msg
->conn
->type
==NETCONN_UDPLITE
) {
435 udp_setflags(msg
->conn
->pcb
.udp
, UDP_FLAGS_UDPLITE
);
437 #endif /* LWIP_UDPLITE */
438 if (msg
->conn
->type
==NETCONN_UDPNOCHKSUM
) {
439 udp_setflags(msg
->conn
->pcb
.udp
, UDP_FLAGS_NOCHKSUM
);
441 udp_recv(msg
->conn
->pcb
.udp
, recv_udp
, msg
->conn
);
443 #endif /* LWIP_UDP */
446 msg
->conn
->pcb
.tcp
= tcp_new();
447 if(msg
->conn
->pcb
.tcp
== NULL
) {
448 msg
->conn
->err
= ERR_MEM
;
451 setup_tcp(msg
->conn
);
453 #endif /* LWIP_TCP */
455 /* Unsupported netconn type, e.g. protocol disabled */
456 msg
->conn
->err
= ERR_VAL
;
460 return msg
->conn
->err
;
464 * Create a new pcb of a specific type inside a netconn.
465 * Called from netconn_new_with_proto_and_callback.
467 * @param msg the api_msg_msg describing the connection type
470 do_newconn(struct api_msg_msg
*msg
)
472 if(msg
->conn
->pcb
.tcp
== NULL
) {
475 /* Else? This "new" connection already has a PCB allocated. */
476 /* Is this an error condition? Should it be deleted? */
477 /* We currently just are happy and return. */
479 TCPIP_APIMSG_ACK(msg
);
483 * Create a new netconn (of a specific type) that has a callback function.
484 * The corresponding pcb is NOT created!
486 * @param t the type of 'connection' to create (@see enum netconn_type)
487 * @param proto the IP protocol for RAW IP pcbs
488 * @param callback a function to call on status changes (RX available, TX'ed)
489 * @return a newly allocated struct netconn or
490 * NULL on memory error
493 netconn_alloc(enum netconn_type t
, netconn_callback callback
)
495 struct netconn
*conn
;
498 conn
= memp_malloc(MEMP_NETCONN
);
505 conn
->pcb
.tcp
= NULL
;
507 #if (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_UDP_RECVMBOX_SIZE) && \
508 (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_TCP_RECVMBOX_SIZE)
509 size
= DEFAULT_RAW_RECVMBOX_SIZE
;
511 switch(NETCONNTYPE_GROUP(t
)) {
514 size
= DEFAULT_RAW_RECVMBOX_SIZE
;
516 #endif /* LWIP_RAW */
519 size
= DEFAULT_UDP_RECVMBOX_SIZE
;
521 #endif /* LWIP_UDP */
524 size
= DEFAULT_TCP_RECVMBOX_SIZE
;
526 #endif /* LWIP_TCP */
528 LWIP_ASSERT("netconn_alloc: undefined netconn_type", 0);
533 if ((conn
->op_completed
= sys_sem_new(0)) == SYS_SEM_NULL
) {
534 memp_free(MEMP_NETCONN
, conn
);
537 if ((conn
->recvmbox
= sys_mbox_new(size
)) == SYS_MBOX_NULL
) {
538 sys_sem_free(conn
->op_completed
);
539 memp_free(MEMP_NETCONN
, conn
);
543 conn
->acceptmbox
= SYS_MBOX_NULL
;
544 conn
->state
= NETCONN_NONE
;
545 /* initialize socket to -1 since 0 is a valid socket */
547 conn
->callback
= callback
;
548 conn
->recv_avail
= 0;
550 conn
->write_msg
= NULL
;
551 conn
->write_offset
= 0;
552 #if LWIP_TCPIP_CORE_LOCKING
553 conn
->write_delayed
= 0;
554 #endif /* LWIP_TCPIP_CORE_LOCKING */
555 #endif /* LWIP_TCP */
557 conn
->recv_timeout
= 0;
558 #endif /* LWIP_SO_RCVTIMEO */
560 conn
->recv_bufsize
= RECV_BUFSIZE_DEFAULT
;
561 #endif /* LWIP_SO_RCVBUF */
566 * Delete a netconn and all its resources.
567 * The pcb is NOT freed (since we might not be in the right thread context do this).
569 * @param conn the netconn to free
572 netconn_free(struct netconn
*conn
)
575 LWIP_ASSERT("PCB must be deallocated outside this function", conn
->pcb
.tcp
== NULL
);
577 /* Drain the recvmbox. */
578 if (conn
->recvmbox
!= SYS_MBOX_NULL
) {
579 while (sys_mbox_tryfetch(conn
->recvmbox
, &mem
) != SYS_MBOX_EMPTY
) {
580 if (conn
->type
== NETCONN_TCP
) {
582 pbuf_free((struct pbuf
*)mem
);
585 netbuf_delete((struct netbuf
*)mem
);
588 sys_mbox_free(conn
->recvmbox
);
589 conn
->recvmbox
= SYS_MBOX_NULL
;
592 /* Drain the acceptmbox. */
593 if (conn
->acceptmbox
!= SYS_MBOX_NULL
) {
594 while (sys_mbox_tryfetch(conn
->acceptmbox
, &mem
) != SYS_MBOX_EMPTY
) {
595 netconn_delete((struct netconn
*)mem
);
597 sys_mbox_free(conn
->acceptmbox
);
598 conn
->acceptmbox
= SYS_MBOX_NULL
;
601 sys_sem_free(conn
->op_completed
);
602 conn
->op_completed
= SYS_SEM_NULL
;
604 memp_free(MEMP_NETCONN
, conn
);
609 * Internal helper function to close a TCP netconn: since this sometimes
610 * doesn't work at the first attempt, this function is called from multiple
613 * @param conn the TCP netconn to close
616 do_close_internal(struct netconn
*conn
)
620 LWIP_ASSERT("invalid conn", (conn
!= NULL
));
621 LWIP_ASSERT("this is for tcp netconns only", (conn
->type
== NETCONN_TCP
));
622 LWIP_ASSERT("conn must be in state NETCONN_CLOSE", (conn
->state
== NETCONN_CLOSE
));
623 LWIP_ASSERT("pcb already closed", (conn
->pcb
.tcp
!= NULL
));
625 /* Set back some callback pointers */
626 tcp_arg(conn
->pcb
.tcp
, NULL
);
627 if (conn
->pcb
.tcp
->state
== LISTEN
) {
628 tcp_accept(conn
->pcb
.tcp
, NULL
);
630 tcp_recv(conn
->pcb
.tcp
, NULL
);
631 tcp_accept(conn
->pcb
.tcp
, NULL
);
632 /* some callbacks have to be reset if tcp_close is not successful */
633 tcp_sent(conn
->pcb
.tcp
, NULL
);
634 tcp_poll(conn
->pcb
.tcp
, NULL
, 4);
635 tcp_err(conn
->pcb
.tcp
, NULL
);
637 /* Try to close the connection */
638 err
= tcp_close(conn
->pcb
.tcp
);
640 /* Closing succeeded */
641 conn
->state
= NETCONN_NONE
;
642 /* Set back some callback pointers as conn is going away */
643 conn
->pcb
.tcp
= NULL
;
645 /* Trigger select() in socket layer. This send should something else so the
646 errorfd is set, not the read and write fd! */
647 API_EVENT(conn
, NETCONN_EVT_RCVPLUS
, 0);
648 API_EVENT(conn
, NETCONN_EVT_SENDPLUS
, 0);
649 /* wake up the application task */
650 sys_sem_signal(conn
->op_completed
);
652 /* Closing failed, restore some of the callbacks */
653 /* Closing of listen pcb will never fail! */
654 LWIP_ASSERT("Closing a listen pcb may not fail!", (conn
->pcb
.tcp
->state
!= LISTEN
));
655 tcp_sent(conn
->pcb
.tcp
, sent_tcp
);
656 tcp_poll(conn
->pcb
.tcp
, poll_tcp
, 4);
657 tcp_err(conn
->pcb
.tcp
, err_tcp
);
658 tcp_arg(conn
->pcb
.tcp
, conn
);
660 /* If closing didn't succeed, we get called again either
661 from poll_tcp or from sent_tcp */
663 #endif /* LWIP_TCP */
666 * Delete the pcb inside a netconn.
667 * Called from netconn_delete.
669 * @param msg the api_msg_msg pointing to the connection
672 do_delconn(struct api_msg_msg
*msg
)
674 if (msg
->conn
->pcb
.tcp
!= NULL
) {
675 switch (NETCONNTYPE_GROUP(msg
->conn
->type
)) {
678 raw_remove(msg
->conn
->pcb
.raw
);
680 #endif /* LWIP_RAW */
683 msg
->conn
->pcb
.udp
->recv_arg
= NULL
;
684 udp_remove(msg
->conn
->pcb
.udp
);
686 #endif /* LWIP_UDP */
689 msg
->conn
->state
= NETCONN_CLOSE
;
690 do_close_internal(msg
->conn
);
691 /* API_EVENT is called inside do_close_internal, before releasing
692 the application thread, so we can return at this point! */
694 #endif /* LWIP_TCP */
699 /* tcp netconns don't come here! */
701 /* Trigger select() in socket layer. This send should something else so the
702 errorfd is set, not the read and write fd! */
703 API_EVENT(msg
->conn
, NETCONN_EVT_RCVPLUS
, 0);
704 API_EVENT(msg
->conn
, NETCONN_EVT_SENDPLUS
, 0);
706 if (msg
->conn
->op_completed
!= SYS_SEM_NULL
) {
707 sys_sem_signal(msg
->conn
->op_completed
);
712 * Bind a pcb contained in a netconn
713 * Called from netconn_bind.
715 * @param msg the api_msg_msg pointing to the connection and containing
716 * the IP address and port to bind to
719 do_bind(struct api_msg_msg
*msg
)
721 if (!ERR_IS_FATAL(msg
->conn
->err
)) {
722 if (msg
->conn
->pcb
.tcp
!= NULL
) {
723 switch (NETCONNTYPE_GROUP(msg
->conn
->type
)) {
726 msg
->conn
->err
= raw_bind(msg
->conn
->pcb
.raw
, msg
->msg
.bc
.ipaddr
);
728 #endif /* LWIP_RAW */
731 msg
->conn
->err
= udp_bind(msg
->conn
->pcb
.udp
, msg
->msg
.bc
.ipaddr
, msg
->msg
.bc
.port
);
733 #endif /* LWIP_UDP */
736 msg
->conn
->err
= tcp_bind(msg
->conn
->pcb
.tcp
, msg
->msg
.bc
.ipaddr
, msg
->msg
.bc
.port
);
738 #endif /* LWIP_TCP */
743 /* msg->conn->pcb is NULL */
744 msg
->conn
->err
= ERR_VAL
;
747 TCPIP_APIMSG_ACK(msg
);
752 * TCP callback function if a connection (opened by tcp_connect/do_connect) has
753 * been established (or reset by the remote host).
755 * @see tcp.h (struct tcp_pcb.connected) for parameters and return values
758 do_connected(void *arg
, struct tcp_pcb
*pcb
, err_t err
)
760 struct netconn
*conn
;
762 LWIP_UNUSED_ARG(pcb
);
771 if ((conn
->type
== NETCONN_TCP
) && (err
== ERR_OK
)) {
774 conn
->state
= NETCONN_NONE
;
775 sys_sem_signal(conn
->op_completed
);
778 #endif /* LWIP_TCP */
781 * Connect a pcb contained inside a netconn
782 * Called from netconn_connect.
784 * @param msg the api_msg_msg pointing to the connection and containing
785 * the IP address and port to connect to
788 do_connect(struct api_msg_msg
*msg
)
790 if (msg
->conn
->pcb
.tcp
== NULL
) {
791 sys_sem_signal(msg
->conn
->op_completed
);
795 switch (NETCONNTYPE_GROUP(msg
->conn
->type
)) {
798 msg
->conn
->err
= raw_connect(msg
->conn
->pcb
.raw
, msg
->msg
.bc
.ipaddr
);
799 sys_sem_signal(msg
->conn
->op_completed
);
801 #endif /* LWIP_RAW */
804 msg
->conn
->err
= udp_connect(msg
->conn
->pcb
.udp
, msg
->msg
.bc
.ipaddr
, msg
->msg
.bc
.port
);
805 sys_sem_signal(msg
->conn
->op_completed
);
807 #endif /* LWIP_UDP */
810 msg
->conn
->state
= NETCONN_CONNECT
;
811 setup_tcp(msg
->conn
);
812 msg
->conn
->err
= tcp_connect(msg
->conn
->pcb
.tcp
, msg
->msg
.bc
.ipaddr
, msg
->msg
.bc
.port
,
814 /* sys_sem_signal() is called from do_connected (or err_tcp()),
815 * when the connection is established! */
817 #endif /* LWIP_TCP */
819 LWIP_ERROR("Invalid netconn type", 0, do{ msg
->conn
->err
= ERR_VAL
;
820 sys_sem_signal(msg
->conn
->op_completed
); }while(0));
826 * Connect a pcb contained inside a netconn
827 * Only used for UDP netconns.
828 * Called from netconn_disconnect.
830 * @param msg the api_msg_msg pointing to the connection to disconnect
833 do_disconnect(struct api_msg_msg
*msg
)
836 if (NETCONNTYPE_GROUP(msg
->conn
->type
) == NETCONN_UDP
) {
837 udp_disconnect(msg
->conn
->pcb
.udp
);
839 #endif /* LWIP_UDP */
840 TCPIP_APIMSG_ACK(msg
);
844 * Set a TCP pcb contained in a netconn into listen mode
845 * Called from netconn_listen.
847 * @param msg the api_msg_msg pointing to the connection
850 do_listen(struct api_msg_msg
*msg
)
853 if (!ERR_IS_FATAL(msg
->conn
->err
)) {
854 if (msg
->conn
->pcb
.tcp
!= NULL
) {
855 if (msg
->conn
->type
== NETCONN_TCP
) {
856 if (msg
->conn
->pcb
.tcp
->state
== CLOSED
) {
857 #if TCP_LISTEN_BACKLOG
858 struct tcp_pcb
* lpcb
= tcp_listen_with_backlog(msg
->conn
->pcb
.tcp
, msg
->msg
.lb
.backlog
);
859 #else /* TCP_LISTEN_BACKLOG */
860 struct tcp_pcb
* lpcb
= tcp_listen(msg
->conn
->pcb
.tcp
);
861 #endif /* TCP_LISTEN_BACKLOG */
863 msg
->conn
->err
= ERR_MEM
;
865 /* delete the recvmbox and allocate the acceptmbox */
866 if (msg
->conn
->recvmbox
!= SYS_MBOX_NULL
) {
867 /** @todo: should we drain the recvmbox here? */
868 sys_mbox_free(msg
->conn
->recvmbox
);
869 msg
->conn
->recvmbox
= SYS_MBOX_NULL
;
871 if (msg
->conn
->acceptmbox
== SYS_MBOX_NULL
) {
872 if ((msg
->conn
->acceptmbox
= sys_mbox_new(DEFAULT_ACCEPTMBOX_SIZE
)) == SYS_MBOX_NULL
) {
873 msg
->conn
->err
= ERR_MEM
;
876 if (msg
->conn
->err
== ERR_OK
) {
877 msg
->conn
->state
= NETCONN_LISTEN
;
878 msg
->conn
->pcb
.tcp
= lpcb
;
879 tcp_arg(msg
->conn
->pcb
.tcp
, msg
->conn
);
880 tcp_accept(msg
->conn
->pcb
.tcp
, accept_function
);
884 msg
->conn
->err
= ERR_CONN
;
889 #endif /* LWIP_TCP */
890 TCPIP_APIMSG_ACK(msg
);
894 * Send some data on a RAW or UDP pcb contained in a netconn
895 * Called from netconn_send
897 * @param msg the api_msg_msg pointing to the connection
900 do_send(struct api_msg_msg
*msg
)
902 if (!ERR_IS_FATAL(msg
->conn
->err
)) {
903 if (msg
->conn
->pcb
.tcp
!= NULL
) {
904 switch (NETCONNTYPE_GROUP(msg
->conn
->type
)) {
907 if (msg
->msg
.b
->addr
== NULL
) {
908 msg
->conn
->err
= raw_send(msg
->conn
->pcb
.raw
, msg
->msg
.b
->p
);
910 msg
->conn
->err
= raw_sendto(msg
->conn
->pcb
.raw
, msg
->msg
.b
->p
, msg
->msg
.b
->addr
);
916 if (msg
->msg
.b
->addr
== NULL
) {
917 msg
->conn
->err
= udp_send(msg
->conn
->pcb
.udp
, msg
->msg
.b
->p
);
919 msg
->conn
->err
= udp_sendto(msg
->conn
->pcb
.udp
, msg
->msg
.b
->p
, msg
->msg
.b
->addr
, msg
->msg
.b
->port
);
922 #endif /* LWIP_UDP */
928 TCPIP_APIMSG_ACK(msg
);
932 * Indicate data has been received from a TCP pcb contained in a netconn
933 * Called from netconn_recv
935 * @param msg the api_msg_msg pointing to the connection
938 do_recv(struct api_msg_msg
*msg
)
941 if (!ERR_IS_FATAL(msg
->conn
->err
)) {
942 if (msg
->conn
->pcb
.tcp
!= NULL
) {
943 if (msg
->conn
->type
== NETCONN_TCP
) {
944 #if TCP_LISTEN_BACKLOG
945 if (msg
->conn
->pcb
.tcp
->state
== LISTEN
) {
946 tcp_accepted(msg
->conn
->pcb
.tcp
);
948 #endif /* TCP_LISTEN_BACKLOG */
950 tcp_recved(msg
->conn
->pcb
.tcp
, msg
->msg
.r
.len
);
955 #endif /* LWIP_TCP */
956 TCPIP_APIMSG_ACK(msg
);
961 * See if more data needs to be written from a previous call to netconn_write.
962 * Called initially from do_write. If the first call can't send all data
963 * (because of low memory or empty send-buffer), this function is called again
964 * from sent_tcp() or poll_tcp() to send more data. If all data is sent, the
965 * blocking application thread (waiting in netconn_write) is released.
967 * @param conn netconn (that is currently in state NETCONN_WRITE) to process
969 * ERR_MEM if LWIP_TCPIP_CORE_LOCKING=1 and sending hasn't yet finished
972 do_writemore(struct netconn
*conn
)
976 u16_t len
, available
;
977 u8_t write_finished
= 0;
980 LWIP_ASSERT("conn->state == NETCONN_WRITE", (conn
->state
== NETCONN_WRITE
));
982 dataptr
= (u8_t
*)conn
->write_msg
->msg
.w
.dataptr
+ conn
->write_offset
;
983 diff
= conn
->write_msg
->msg
.w
.len
- conn
->write_offset
;
984 if (diff
> 0xffffUL
) { /* max_u16_t */
986 #if LWIP_TCPIP_CORE_LOCKING
987 conn
->write_delayed
= 1;
992 available
= tcp_sndbuf(conn
->pcb
.tcp
);
993 if (available
< len
) {
994 /* don't try to write more than sendbuf */
996 #if LWIP_TCPIP_CORE_LOCKING
997 conn
->write_delayed
= 1;
1001 err
= tcp_write(conn
->pcb
.tcp
, dataptr
, len
, conn
->write_msg
->msg
.w
.apiflags
);
1002 LWIP_ASSERT("do_writemore: invalid length!", ((conn
->write_offset
+ len
) <= conn
->write_msg
->msg
.w
.len
));
1003 if (err
== ERR_OK
) {
1004 conn
->write_offset
+= len
;
1005 if (conn
->write_offset
== conn
->write_msg
->msg
.w
.len
) {
1006 /* everything was written */
1008 conn
->write_msg
= NULL
;
1009 conn
->write_offset
= 0;
1010 /* API_EVENT might call tcp_tmr, so reset conn->state now */
1011 conn
->state
= NETCONN_NONE
;
1013 err
= tcp_output_nagle(conn
->pcb
.tcp
);
1015 if ((err
== ERR_OK
) && (tcp_sndbuf(conn
->pcb
.tcp
) <= TCP_SNDLOWAT
)) {
1016 API_EVENT(conn
, NETCONN_EVT_SENDMINUS
, len
);
1018 } else if (err
== ERR_MEM
) {
1019 /* If ERR_MEM, we wait for sent_tcp or poll_tcp to be called
1020 we do NOT return to the application thread, since ERR_MEM is
1021 only a temporary error! */
1023 /* tcp_enqueue returned ERR_MEM, try tcp_output anyway */
1024 err
= tcp_output(conn
->pcb
.tcp
);
1026 #if LWIP_TCPIP_CORE_LOCKING
1027 conn
->write_delayed
= 1;
1030 /* On errors != ERR_MEM, we don't try writing any more but return
1031 the error to the application thread. */
1036 if (write_finished
) {
1037 /* everything was written: set back connection state
1038 and back to application task */
1039 conn
->state
= NETCONN_NONE
;
1040 #if LWIP_TCPIP_CORE_LOCKING
1041 if (conn
->write_delayed
!= 0)
1044 sys_sem_signal(conn
->op_completed
);
1047 #if LWIP_TCPIP_CORE_LOCKING
1053 #endif /* LWIP_TCP */
1056 * Send some data on a TCP pcb contained in a netconn
1057 * Called from netconn_write
1059 * @param msg the api_msg_msg pointing to the connection
1062 do_write(struct api_msg_msg
*msg
)
1064 if (!ERR_IS_FATAL(msg
->conn
->err
)) {
1065 if ((msg
->conn
->pcb
.tcp
!= NULL
) && (msg
->conn
->type
== NETCONN_TCP
)) {
1067 msg
->conn
->state
= NETCONN_WRITE
;
1068 /* set all the variables used by do_writemore */
1069 LWIP_ASSERT("already writing", msg
->conn
->write_msg
== NULL
&&
1070 msg
->conn
->write_offset
== 0);
1071 msg
->conn
->write_msg
= msg
;
1072 msg
->conn
->write_offset
= 0;
1073 #if LWIP_TCPIP_CORE_LOCKING
1074 msg
->conn
->write_delayed
= 0;
1075 if (do_writemore(msg
->conn
) != ERR_OK
) {
1076 LWIP_ASSERT("state!", msg
->conn
->state
== NETCONN_WRITE
);
1077 UNLOCK_TCPIP_CORE();
1078 sys_arch_sem_wait(msg
->conn
->op_completed
, 0);
1080 LWIP_ASSERT("state!", msg
->conn
->state
== NETCONN_NONE
);
1083 do_writemore(msg
->conn
);
1085 /* for both cases: if do_writemore was called, don't ACK the APIMSG! */
1087 #endif /* LWIP_TCP */
1088 #if (LWIP_UDP || LWIP_RAW)
1090 msg
->conn
->err
= ERR_VAL
;
1091 #endif /* (LWIP_UDP || LWIP_RAW) */
1094 TCPIP_APIMSG_ACK(msg
);
1098 * Return a connection's local or remote address
1099 * Called from netconn_getaddr
1101 * @param msg the api_msg_msg pointing to the connection
1104 do_getaddr(struct api_msg_msg
*msg
)
1106 if (msg
->conn
->pcb
.ip
!= NULL
) {
1107 *(msg
->msg
.ad
.ipaddr
) = (msg
->msg
.ad
.local
?msg
->conn
->pcb
.ip
->local_ip
:msg
->conn
->pcb
.ip
->remote_ip
);
1109 switch (NETCONNTYPE_GROUP(msg
->conn
->type
)) {
1112 if (msg
->msg
.ad
.local
) {
1113 *(msg
->msg
.ad
.port
) = msg
->conn
->pcb
.raw
->protocol
;
1115 /* return an error as connecting is only a helper for upper layers */
1116 msg
->conn
->err
= ERR_CONN
;
1119 #endif /* LWIP_RAW */
1122 if (msg
->msg
.ad
.local
) {
1123 *(msg
->msg
.ad
.port
) = msg
->conn
->pcb
.udp
->local_port
;
1125 if ((msg
->conn
->pcb
.udp
->flags
& UDP_FLAGS_CONNECTED
) == 0) {
1126 msg
->conn
->err
= ERR_CONN
;
1128 *(msg
->msg
.ad
.port
) = msg
->conn
->pcb
.udp
->remote_port
;
1132 #endif /* LWIP_UDP */
1135 *(msg
->msg
.ad
.port
) = (msg
->msg
.ad
.local
?msg
->conn
->pcb
.tcp
->local_port
:msg
->conn
->pcb
.tcp
->remote_port
);
1137 #endif /* LWIP_TCP */
1140 msg
->conn
->err
= ERR_CONN
;
1142 TCPIP_APIMSG_ACK(msg
);
1146 * Close a TCP pcb contained in a netconn
1147 * Called from netconn_close
1149 * @param msg the api_msg_msg pointing to the connection
1152 do_close(struct api_msg_msg
*msg
)
1155 if ((msg
->conn
->pcb
.tcp
!= NULL
) && (msg
->conn
->type
== NETCONN_TCP
)) {
1156 msg
->conn
->state
= NETCONN_CLOSE
;
1157 do_close_internal(msg
->conn
);
1158 /* for tcp netconns, do_close_internal ACKs the message */
1160 #endif /* LWIP_TCP */
1162 msg
->conn
->err
= ERR_VAL
;
1163 sys_sem_signal(msg
->conn
->op_completed
);
1169 * Join multicast groups for UDP netconns.
1170 * Called from netconn_join_leave_group
1172 * @param msg the api_msg_msg pointing to the connection
1175 do_join_leave_group(struct api_msg_msg
*msg
)
1177 if (!ERR_IS_FATAL(msg
->conn
->err
)) {
1178 if (msg
->conn
->pcb
.tcp
!= NULL
) {
1179 if (NETCONNTYPE_GROUP(msg
->conn
->type
) == NETCONN_UDP
) {
1181 if (msg
->msg
.jl
.join_or_leave
== NETCONN_JOIN
) {
1182 msg
->conn
->err
= igmp_joingroup(msg
->msg
.jl
.interface
, msg
->msg
.jl
.multiaddr
);
1184 msg
->conn
->err
= igmp_leavegroup(msg
->msg
.jl
.interface
, msg
->msg
.jl
.multiaddr
);
1186 #endif /* LWIP_UDP */
1187 #if (LWIP_TCP || LWIP_RAW)
1189 msg
->conn
->err
= ERR_VAL
;
1190 #endif /* (LWIP_TCP || LWIP_RAW) */
1194 TCPIP_APIMSG_ACK(msg
);
1196 #endif /* LWIP_IGMP */
1200 * Callback function that is called when DNS name is resolved
1201 * (or on timeout). A waiting application thread is waked up by
1202 * signaling the semaphore.
1205 do_dns_found(const char *name
, struct ip_addr
*ipaddr
, void *arg
)
1207 struct dns_api_msg
*msg
= (struct dns_api_msg
*)arg
;
1209 LWIP_ASSERT("DNS response for wrong host name", strcmp(msg
->name
, name
) == 0);
1211 if (ipaddr
== NULL
) {
1212 /* timeout or memory error */
1213 *msg
->err
= ERR_VAL
;
1215 /* address was resolved */
1217 *msg
->addr
= *ipaddr
;
1219 /* wake up the application task waiting in netconn_gethostbyname */
1220 sys_sem_signal(msg
->sem
);
1224 * Execute a DNS query
1225 * Called from netconn_gethostbyname
1227 * @param arg the dns_api_msg pointing to the query
1230 do_gethostbyname(void *arg
)
1232 struct dns_api_msg
*msg
= (struct dns_api_msg
*)arg
;
1234 *msg
->err
= dns_gethostbyname(msg
->name
, msg
->addr
, do_dns_found
, msg
);
1235 if (*msg
->err
!= ERR_INPROGRESS
) {
1236 /* on error or immediate success, wake up the application
1237 * task waiting in netconn_gethostbyname */
1238 sys_sem_signal(msg
->sem
);
1241 #endif /* LWIP_DNS */
1243 #endif /* LWIP_NETCONN */