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"
55 /* forward declarations */
57 static err_t
do_writemore(struct netconn
*conn
);
58 static void do_close_internal(struct netconn
*conn
);
63 * Receive callback function for RAW netconns.
64 * Doesn't 'eat' the packet, only references it and sends it to
67 * @see raw.h (struct raw_pcb.recv) for parameters and return value
70 recv_raw(void *arg
, struct raw_pcb
*pcb
, struct pbuf
*p
,
78 #endif /* LWIP_SO_RCVBUF */
80 LWIP_UNUSED_ARG(addr
);
84 SYS_ARCH_GET(conn
->recv_avail
, recv_avail
);
85 if ((conn
!= NULL
) && (conn
->recvmbox
!= SYS_MBOX_NULL
) &&
86 ((recv_avail
+ (int)(p
->tot_len
)) <= conn
->recv_bufsize
)) {
87 #else /* LWIP_SO_RCVBUF */
88 if ((conn
!= NULL
) && (conn
->recvmbox
!= SYS_MBOX_NULL
)) {
89 #endif /* LWIP_SO_RCVBUF */
90 /* copy the whole packet into new pbufs */
91 q
= pbuf_alloc(PBUF_RAW
, p
->tot_len
, PBUF_RAM
);
93 if (pbuf_copy(q
, p
) != ERR_OK
) {
100 buf
= memp_malloc(MEMP_NETBUF
);
108 buf
->addr
= &(((struct ip_hdr
*)(q
->payload
))->src
);
109 buf
->port
= pcb
->protocol
;
111 SYS_ARCH_INC(conn
->recv_avail
, q
->tot_len
);
112 /* Register event with callback */
113 API_EVENT(conn
, NETCONN_EVT_RCVPLUS
, q
->tot_len
);
114 if (sys_mbox_trypost(conn
->recvmbox
, buf
) != ERR_OK
) {
120 return 0; /* do not eat the packet */
126 * Receive callback function for UDP netconns.
127 * Posts the packet to conn->recvmbox or deletes it on memory error.
129 * @see udp.h (struct udp_pcb.recv) for parameters
132 recv_udp(void *arg
, struct udp_pcb
*pcb
, struct pbuf
*p
,
133 struct ip_addr
*addr
, u16_t port
)
136 struct netconn
*conn
;
139 #endif /* LWIP_SO_RCVBUF */
141 LWIP_UNUSED_ARG(pcb
); /* only used for asserts... */
142 LWIP_ASSERT("recv_udp must have a pcb argument", pcb
!= NULL
);
143 LWIP_ASSERT("recv_udp must have an argument", arg
!= NULL
);
145 LWIP_ASSERT("recv_udp: recv for wrong pcb!", conn
->pcb
.udp
== pcb
);
148 SYS_ARCH_GET(conn
->recv_avail
, recv_avail
);
149 if ((conn
== NULL
) || (conn
->recvmbox
== SYS_MBOX_NULL
) ||
150 ((recv_avail
+ (int)(p
->tot_len
)) > conn
->recv_bufsize
)) {
151 #else /* LWIP_SO_RCVBUF */
152 if ((conn
== NULL
) || (conn
->recvmbox
== SYS_MBOX_NULL
)) {
153 #endif /* LWIP_SO_RCVBUF */
158 buf
= memp_malloc(MEMP_NETBUF
);
169 SYS_ARCH_INC(conn
->recv_avail
, p
->tot_len
);
170 /* Register event with callback */
171 API_EVENT(conn
, NETCONN_EVT_RCVPLUS
, p
->tot_len
);
172 if (sys_mbox_trypost(conn
->recvmbox
, buf
) != ERR_OK
) {
177 #endif /* LWIP_UDP */
181 * Receive callback function for TCP netconns.
182 * Posts the packet to conn->recvmbox, but doesn't delete it on errors.
184 * @see tcp.h (struct tcp_pcb.recv) for parameters and return value
187 recv_tcp(void *arg
, struct tcp_pcb
*pcb
, struct pbuf
*p
, err_t err
)
189 struct netconn
*conn
;
192 LWIP_UNUSED_ARG(pcb
);
193 LWIP_ASSERT("recv_tcp must have a pcb argument", pcb
!= NULL
);
194 LWIP_ASSERT("recv_tcp must have an argument", arg
!= NULL
);
196 LWIP_ASSERT("recv_tcp: recv for wrong pcb!", conn
->pcb
.tcp
== pcb
);
198 if ((conn
== NULL
) || (conn
->recvmbox
== SYS_MBOX_NULL
)) {
205 SYS_ARCH_INC(conn
->recv_avail
, len
);
209 /* Register event with callback */
210 API_EVENT(conn
, NETCONN_EVT_RCVPLUS
, len
);
211 if (sys_mbox_trypost(conn
->recvmbox
, p
) != ERR_OK
) {
219 * Poll callback function for TCP netconns.
220 * Wakes up an application thread that waits for a connection to close
221 * or data to be sent. The application thread then takes the
222 * appropriate action to go on.
224 * Signals the conn->sem.
225 * netconn_close waits for conn->sem if closing failed.
227 * @see tcp.h (struct tcp_pcb.poll) for parameters and return value
230 poll_tcp(void *arg
, struct tcp_pcb
*pcb
)
232 struct netconn
*conn
= arg
;
234 LWIP_UNUSED_ARG(pcb
);
235 LWIP_ASSERT("conn != NULL", (conn
!= NULL
));
237 if (conn
->state
== NETCONN_WRITE
) {
239 } else if (conn
->state
== NETCONN_CLOSE
) {
240 do_close_internal(conn
);
247 * Sent callback function for TCP netconns.
248 * Signals the conn->sem and calls API_EVENT.
249 * netconn_write waits for conn->sem if send buffer is low.
251 * @see tcp.h (struct tcp_pcb.sent) for parameters and return value
254 sent_tcp(void *arg
, struct tcp_pcb
*pcb
, u16_t len
)
256 struct netconn
*conn
= arg
;
258 LWIP_UNUSED_ARG(pcb
);
259 LWIP_ASSERT("conn != NULL", (conn
!= NULL
));
261 if (conn
->state
== NETCONN_WRITE
) {
262 LWIP_ASSERT("conn->pcb.tcp != NULL", conn
->pcb
.tcp
!= NULL
);
264 } else if (conn
->state
== NETCONN_CLOSE
) {
265 do_close_internal(conn
);
269 if ((conn
->pcb
.tcp
!= NULL
) && (tcp_sndbuf(conn
->pcb
.tcp
) > TCP_SNDLOWAT
)) {
270 API_EVENT(conn
, NETCONN_EVT_SENDPLUS
, len
);
278 * Error callback function for TCP netconns.
279 * Signals conn->sem, posts to all conn mboxes and calls API_EVENT.
280 * The application thread has then to decide what to do.
282 * @see tcp.h (struct tcp_pcb.err) for parameters
285 err_tcp(void *arg
, err_t err
)
287 struct netconn
*conn
;
290 LWIP_ASSERT("conn != NULL", (conn
!= NULL
));
292 conn
->pcb
.tcp
= NULL
;
295 if (conn
->recvmbox
!= SYS_MBOX_NULL
) {
296 /* Register event with callback */
297 API_EVENT(conn
, NETCONN_EVT_RCVPLUS
, 0);
298 sys_mbox_post(conn
->recvmbox
, NULL
);
300 if (conn
->op_completed
!= SYS_SEM_NULL
&& conn
->state
== NETCONN_CONNECT
) {
301 conn
->state
= NETCONN_NONE
;
302 sys_sem_signal(conn
->op_completed
);
304 if (conn
->acceptmbox
!= SYS_MBOX_NULL
) {
305 /* Register event with callback */
306 API_EVENT(conn
, NETCONN_EVT_RCVPLUS
, 0);
307 sys_mbox_post(conn
->acceptmbox
, NULL
);
309 if ((conn
->state
== NETCONN_WRITE
) || (conn
->state
== NETCONN_CLOSE
)) {
310 /* calling do_writemore/do_close_internal is not necessary
311 since the pcb has already been deleted! */
312 conn
->state
= NETCONN_NONE
;
313 /* wake up the waiting task */
314 sys_sem_signal(conn
->op_completed
);
319 * Setup a tcp_pcb with the correct callback function pointers
320 * and their arguments.
322 * @param conn the TCP netconn to setup
325 setup_tcp(struct netconn
*conn
)
331 tcp_recv(pcb
, recv_tcp
);
332 tcp_sent(pcb
, sent_tcp
);
333 tcp_poll(pcb
, poll_tcp
, 4);
334 tcp_err(pcb
, err_tcp
);
338 * Accept callback function for TCP netconns.
339 * Allocates a new netconn and posts that to conn->acceptmbox.
341 * @see tcp.h (struct tcp_pcb_listen.accept) for parameters and return value
344 accept_function(void *arg
, struct tcp_pcb
*newpcb
, err_t err
)
346 struct netconn
*newconn
;
347 struct netconn
*conn
;
351 tcp_debug_print_state(newpcb
->state
);
352 #endif /* TCP_DEBUG */
353 #endif /* API_MSG_DEBUG */
354 conn
= (struct netconn
*)arg
;
356 LWIP_ERROR("accept_function: invalid conn->acceptmbox",
357 conn
->acceptmbox
!= SYS_MBOX_NULL
, return ERR_VAL
;);
359 /* We have to set the callback here even though
360 * the new socket is unknown. conn->socket is marked as -1. */
361 newconn
= netconn_alloc(conn
->type
, conn
->callback
);
362 if (newconn
== NULL
) {
365 newconn
->pcb
.tcp
= newpcb
;
368 /* Register event with callback */
369 API_EVENT(conn
, NETCONN_EVT_RCVPLUS
, 0);
371 if (sys_mbox_trypost(conn
->acceptmbox
, newconn
) != ERR_OK
) {
372 /* When returning != ERR_OK, the connection is aborted in tcp_process(),
373 so do nothing here! */
374 newconn
->pcb
.tcp
= NULL
;
375 netconn_free(newconn
);
380 #endif /* LWIP_TCP */
383 * Create a new pcb of a specific type.
384 * Called from do_newconn().
386 * @param msg the api_msg_msg describing the connection type
387 * @return msg->conn->err, but the return value is currently ignored
390 pcb_new(struct api_msg_msg
*msg
)
392 msg
->conn
->err
= ERR_OK
;
394 LWIP_ASSERT("pcb_new: pcb already allocated", msg
->conn
->pcb
.tcp
== NULL
);
396 /* Allocate a PCB for this connection */
397 switch(NETCONNTYPE_GROUP(msg
->conn
->type
)) {
400 msg
->conn
->pcb
.raw
= raw_new(msg
->msg
.n
.proto
);
401 if(msg
->conn
->pcb
.raw
== NULL
) {
402 msg
->conn
->err
= ERR_MEM
;
405 raw_recv(msg
->conn
->pcb
.raw
, recv_raw
, msg
->conn
);
407 #endif /* LWIP_RAW */
410 msg
->conn
->pcb
.udp
= udp_new();
411 if(msg
->conn
->pcb
.udp
== NULL
) {
412 msg
->conn
->err
= ERR_MEM
;
416 if (msg
->conn
->type
==NETCONN_UDPLITE
) {
417 udp_setflags(msg
->conn
->pcb
.udp
, UDP_FLAGS_UDPLITE
);
419 #endif /* LWIP_UDPLITE */
420 if (msg
->conn
->type
==NETCONN_UDPNOCHKSUM
) {
421 udp_setflags(msg
->conn
->pcb
.udp
, UDP_FLAGS_NOCHKSUM
);
423 udp_recv(msg
->conn
->pcb
.udp
, recv_udp
, msg
->conn
);
425 #endif /* LWIP_UDP */
428 msg
->conn
->pcb
.tcp
= tcp_new();
429 if(msg
->conn
->pcb
.tcp
== NULL
) {
430 msg
->conn
->err
= ERR_MEM
;
433 setup_tcp(msg
->conn
);
435 #endif /* LWIP_TCP */
437 /* Unsupported netconn type, e.g. protocol disabled */
438 msg
->conn
->err
= ERR_VAL
;
442 return msg
->conn
->err
;
446 * Create a new pcb of a specific type inside a netconn.
447 * Called from netconn_new_with_proto_and_callback.
449 * @param msg the api_msg_msg describing the connection type
452 do_newconn(struct api_msg_msg
*msg
)
454 if(msg
->conn
->pcb
.tcp
== NULL
) {
457 /* Else? This "new" connection already has a PCB allocated. */
458 /* Is this an error condition? Should it be deleted? */
459 /* We currently just are happy and return. */
461 TCPIP_APIMSG_ACK(msg
);
465 * Create a new netconn (of a specific type) that has a callback function.
466 * The corresponding pcb is NOT created!
468 * @param t the type of 'connection' to create (@see enum netconn_type)
469 * @param proto the IP protocol for RAW IP pcbs
470 * @param callback a function to call on status changes (RX available, TX'ed)
471 * @return a newly allocated struct netconn or
472 * NULL on memory error
475 netconn_alloc(enum netconn_type t
, netconn_callback callback
)
477 struct netconn
*conn
;
480 conn
= memp_malloc(MEMP_NETCONN
);
487 conn
->pcb
.tcp
= NULL
;
489 #if (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_UDP_RECVMBOX_SIZE) && \
490 (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_TCP_RECVMBOX_SIZE)
491 size
= DEFAULT_RAW_RECVMBOX_SIZE
;
493 switch(NETCONNTYPE_GROUP(t
)) {
496 size
= DEFAULT_RAW_RECVMBOX_SIZE
;
498 #endif /* LWIP_RAW */
501 size
= DEFAULT_UDP_RECVMBOX_SIZE
;
503 #endif /* LWIP_UDP */
506 size
= DEFAULT_TCP_RECVMBOX_SIZE
;
508 #endif /* LWIP_TCP */
510 LWIP_ASSERT("netconn_alloc: undefined netconn_type", 0);
515 if ((conn
->op_completed
= sys_sem_new(0)) == SYS_SEM_NULL
) {
516 memp_free(MEMP_NETCONN
, conn
);
519 if ((conn
->recvmbox
= sys_mbox_new(size
)) == SYS_MBOX_NULL
) {
520 sys_sem_free(conn
->op_completed
);
521 memp_free(MEMP_NETCONN
, conn
);
525 conn
->acceptmbox
= SYS_MBOX_NULL
;
526 conn
->state
= NETCONN_NONE
;
527 /* initialize socket to -1 since 0 is a valid socket */
529 conn
->callback
= callback
;
530 conn
->recv_avail
= 0;
532 conn
->recv_timeout
= 0;
533 #endif /* LWIP_SO_RCVTIMEO */
535 conn
->recv_bufsize
= INT_MAX
;
536 #endif /* LWIP_SO_RCVBUF */
541 * Delete a netconn and all its resources.
542 * The pcb is NOT freed (since we might not be in the right thread context do this).
544 * @param conn the netconn to free
547 netconn_free(struct netconn
*conn
)
550 LWIP_ASSERT("PCB must be deallocated outside this function", conn
->pcb
.tcp
== NULL
);
552 /* Drain the recvmbox. */
553 if (conn
->recvmbox
!= SYS_MBOX_NULL
) {
554 while (sys_mbox_tryfetch(conn
->recvmbox
, &mem
) != SYS_MBOX_EMPTY
) {
555 if (conn
->type
== NETCONN_TCP
) {
557 pbuf_free((struct pbuf
*)mem
);
560 netbuf_delete((struct netbuf
*)mem
);
563 sys_mbox_free(conn
->recvmbox
);
564 conn
->recvmbox
= SYS_MBOX_NULL
;
567 /* Drain the acceptmbox. */
568 if (conn
->acceptmbox
!= SYS_MBOX_NULL
) {
569 while (sys_mbox_tryfetch(conn
->acceptmbox
, &mem
) != SYS_MBOX_EMPTY
) {
570 netconn_delete((struct netconn
*)mem
);
572 sys_mbox_free(conn
->acceptmbox
);
573 conn
->acceptmbox
= SYS_MBOX_NULL
;
576 sys_sem_free(conn
->op_completed
);
577 conn
->op_completed
= SYS_SEM_NULL
;
579 memp_free(MEMP_NETCONN
, conn
);
584 * Internal helper function to close a TCP netconn: since this sometimes
585 * doesn't work at the first attempt, this function is called from multiple
588 * @param conn the TCP netconn to close
591 do_close_internal(struct netconn
*conn
)
595 LWIP_ASSERT("invalid conn", (conn
!= NULL
));
596 LWIP_ASSERT("this is for tcp netconns only", (conn
->type
== NETCONN_TCP
));
597 LWIP_ASSERT("conn must be in state NETCONN_CLOSE", (conn
->state
== NETCONN_CLOSE
));
598 LWIP_ASSERT("pcb already closed", (conn
->pcb
.tcp
!= NULL
));
600 /* Set back some callback pointers */
601 if (conn
->pcb
.tcp
->state
== LISTEN
) {
602 tcp_arg(conn
->pcb
.tcp
, NULL
);
603 tcp_accept(conn
->pcb
.tcp
, NULL
);
605 tcp_recv(conn
->pcb
.tcp
, NULL
);
607 /* Try to close the connection */
608 err
= tcp_close(conn
->pcb
.tcp
);
610 /* Closing succeeded */
611 conn
->state
= NETCONN_NONE
;
612 /* Set back some callback pointers as conn is going away */
613 tcp_err(conn
->pcb
.tcp
, NULL
);
614 tcp_poll(conn
->pcb
.tcp
, NULL
, 4);
615 tcp_sent(conn
->pcb
.tcp
, NULL
);
616 tcp_recv(conn
->pcb
.tcp
, NULL
);
617 tcp_arg(conn
->pcb
.tcp
, NULL
);
618 conn
->pcb
.tcp
= NULL
;
620 /* Trigger select() in socket layer. This send should something else so the
621 errorfd is set, not the read and write fd! */
622 API_EVENT(conn
, NETCONN_EVT_RCVPLUS
, 0);
623 API_EVENT(conn
, NETCONN_EVT_SENDPLUS
, 0);
624 /* wake up the application task */
625 sys_sem_signal(conn
->op_completed
);
627 /* If closing didn't succeed, we get called again either
628 from poll_tcp or from sent_tcp */
630 #endif /* LWIP_TCP */
633 * Delete the pcb inside a netconn.
634 * Called from netconn_delete.
636 * @param msg the api_msg_msg pointing to the connection
639 do_delconn(struct api_msg_msg
*msg
)
641 if (msg
->conn
->pcb
.tcp
!= NULL
) {
642 switch (NETCONNTYPE_GROUP(msg
->conn
->type
)) {
645 raw_remove(msg
->conn
->pcb
.raw
);
647 #endif /* LWIP_RAW */
650 msg
->conn
->pcb
.udp
->recv_arg
= NULL
;
651 udp_remove(msg
->conn
->pcb
.udp
);
653 #endif /* LWIP_UDP */
656 msg
->conn
->state
= NETCONN_CLOSE
;
657 do_close_internal(msg
->conn
);
658 /* API_EVENT is called inside do_close_internal, before releasing
659 the application thread, so we can return at this point! */
661 #endif /* LWIP_TCP */
666 /* tcp netconns don't come here! */
668 /* Trigger select() in socket layer. This send should something else so the
669 errorfd is set, not the read and write fd! */
670 API_EVENT(msg
->conn
, NETCONN_EVT_RCVPLUS
, 0);
671 API_EVENT(msg
->conn
, NETCONN_EVT_SENDPLUS
, 0);
673 if (msg
->conn
->op_completed
!= SYS_SEM_NULL
) {
674 sys_sem_signal(msg
->conn
->op_completed
);
679 * Bind a pcb contained in a netconn
680 * Called from netconn_bind.
682 * @param msg the api_msg_msg pointing to the connection and containing
683 * the IP address and port to bind to
686 do_bind(struct api_msg_msg
*msg
)
688 if (!ERR_IS_FATAL(msg
->conn
->err
)) {
689 if (msg
->conn
->pcb
.tcp
!= NULL
) {
690 switch (NETCONNTYPE_GROUP(msg
->conn
->type
)) {
693 msg
->conn
->err
= raw_bind(msg
->conn
->pcb
.raw
, msg
->msg
.bc
.ipaddr
);
695 #endif /* LWIP_RAW */
698 msg
->conn
->err
= udp_bind(msg
->conn
->pcb
.udp
, msg
->msg
.bc
.ipaddr
, msg
->msg
.bc
.port
);
700 #endif /* LWIP_UDP */
703 msg
->conn
->err
= tcp_bind(msg
->conn
->pcb
.tcp
, msg
->msg
.bc
.ipaddr
, msg
->msg
.bc
.port
);
705 #endif /* LWIP_TCP */
710 /* msg->conn->pcb is NULL */
711 msg
->conn
->err
= ERR_VAL
;
714 TCPIP_APIMSG_ACK(msg
);
719 * TCP callback function if a connection (opened by tcp_connect/do_connect) has
720 * been established (or reset by the remote host).
722 * @see tcp.h (struct tcp_pcb.connected) for parameters and return values
725 do_connected(void *arg
, struct tcp_pcb
*pcb
, err_t err
)
727 struct netconn
*conn
;
729 LWIP_UNUSED_ARG(pcb
);
738 if ((conn
->type
== NETCONN_TCP
) && (err
== ERR_OK
)) {
741 conn
->state
= NETCONN_NONE
;
742 sys_sem_signal(conn
->op_completed
);
745 #endif /* LWIP_TCP */
748 * Connect a pcb contained inside a netconn
749 * Called from netconn_connect.
751 * @param msg the api_msg_msg pointing to the connection and containing
752 * the IP address and port to connect to
755 do_connect(struct api_msg_msg
*msg
)
757 if (msg
->conn
->pcb
.tcp
== NULL
) {
758 sys_sem_signal(msg
->conn
->op_completed
);
762 switch (NETCONNTYPE_GROUP(msg
->conn
->type
)) {
765 msg
->conn
->err
= raw_connect(msg
->conn
->pcb
.raw
, msg
->msg
.bc
.ipaddr
);
766 sys_sem_signal(msg
->conn
->op_completed
);
768 #endif /* LWIP_RAW */
771 msg
->conn
->err
= udp_connect(msg
->conn
->pcb
.udp
, msg
->msg
.bc
.ipaddr
, msg
->msg
.bc
.port
);
772 sys_sem_signal(msg
->conn
->op_completed
);
774 #endif /* LWIP_UDP */
777 msg
->conn
->state
= NETCONN_CONNECT
;
778 setup_tcp(msg
->conn
);
779 msg
->conn
->err
= tcp_connect(msg
->conn
->pcb
.tcp
, msg
->msg
.bc
.ipaddr
, msg
->msg
.bc
.port
,
781 /* sys_sem_signal() is called from do_connected (or err_tcp()),
782 * when the connection is established! */
784 #endif /* LWIP_TCP */
791 * Connect a pcb contained inside a netconn
792 * Only used for UDP netconns.
793 * Called from netconn_disconnect.
795 * @param msg the api_msg_msg pointing to the connection to disconnect
798 do_disconnect(struct api_msg_msg
*msg
)
801 if (NETCONNTYPE_GROUP(msg
->conn
->type
) == NETCONN_UDP
) {
802 udp_disconnect(msg
->conn
->pcb
.udp
);
804 #endif /* LWIP_UDP */
805 TCPIP_APIMSG_ACK(msg
);
809 * Set a TCP pcb contained in a netconn into listen mode
810 * Called from netconn_listen.
812 * @param msg the api_msg_msg pointing to the connection
815 do_listen(struct api_msg_msg
*msg
)
818 if (!ERR_IS_FATAL(msg
->conn
->err
)) {
819 if (msg
->conn
->pcb
.tcp
!= NULL
) {
820 if (msg
->conn
->type
== NETCONN_TCP
) {
821 if (msg
->conn
->pcb
.tcp
->state
== CLOSED
) {
822 #if TCP_LISTEN_BACKLOG
823 struct tcp_pcb
* lpcb
= tcp_listen_with_backlog(msg
->conn
->pcb
.tcp
, msg
->msg
.lb
.backlog
);
824 #else /* TCP_LISTEN_BACKLOG */
825 struct tcp_pcb
* lpcb
= tcp_listen(msg
->conn
->pcb
.tcp
);
826 #endif /* TCP_LISTEN_BACKLOG */
828 msg
->conn
->err
= ERR_MEM
;
830 /* delete the recvmbox and allocate the acceptmbox */
831 if (msg
->conn
->recvmbox
!= SYS_MBOX_NULL
) {
832 /** @todo: should we drain the recvmbox here? */
833 sys_mbox_free(msg
->conn
->recvmbox
);
834 msg
->conn
->recvmbox
= SYS_MBOX_NULL
;
836 if (msg
->conn
->acceptmbox
== SYS_MBOX_NULL
) {
837 if ((msg
->conn
->acceptmbox
= sys_mbox_new(DEFAULT_ACCEPTMBOX_SIZE
)) == SYS_MBOX_NULL
) {
838 msg
->conn
->err
= ERR_MEM
;
841 if (msg
->conn
->err
== ERR_OK
) {
842 msg
->conn
->state
= NETCONN_LISTEN
;
843 msg
->conn
->pcb
.tcp
= lpcb
;
844 tcp_arg(msg
->conn
->pcb
.tcp
, msg
->conn
);
845 tcp_accept(msg
->conn
->pcb
.tcp
, accept_function
);
849 msg
->conn
->err
= ERR_CONN
;
854 #endif /* LWIP_TCP */
855 TCPIP_APIMSG_ACK(msg
);
859 * Send some data on a RAW or UDP pcb contained in a netconn
860 * Called from netconn_send
862 * @param msg the api_msg_msg pointing to the connection
865 do_send(struct api_msg_msg
*msg
)
867 if (!ERR_IS_FATAL(msg
->conn
->err
)) {
868 if (msg
->conn
->pcb
.tcp
!= NULL
) {
869 switch (NETCONNTYPE_GROUP(msg
->conn
->type
)) {
872 if (msg
->msg
.b
->addr
== NULL
) {
873 msg
->conn
->err
= raw_send(msg
->conn
->pcb
.raw
, msg
->msg
.b
->p
);
875 msg
->conn
->err
= raw_sendto(msg
->conn
->pcb
.raw
, msg
->msg
.b
->p
, msg
->msg
.b
->addr
);
881 if (msg
->msg
.b
->addr
== NULL
) {
882 msg
->conn
->err
= udp_send(msg
->conn
->pcb
.udp
, msg
->msg
.b
->p
);
884 msg
->conn
->err
= udp_sendto(msg
->conn
->pcb
.udp
, msg
->msg
.b
->p
, msg
->msg
.b
->addr
, msg
->msg
.b
->port
);
887 #endif /* LWIP_UDP */
893 TCPIP_APIMSG_ACK(msg
);
897 * Recv some data from a RAW or UDP pcb contained in a netconn
898 * Called from netconn_recv
900 * @param msg the api_msg_msg pointing to the connection
903 do_recv(struct api_msg_msg
*msg
)
906 if (!ERR_IS_FATAL(msg
->conn
->err
)) {
907 if (msg
->conn
->pcb
.tcp
!= NULL
) {
908 if (msg
->conn
->type
== NETCONN_TCP
) {
909 #if TCP_LISTEN_BACKLOG
910 if (msg
->conn
->pcb
.tcp
->state
== LISTEN
) {
911 tcp_accepted(msg
->conn
->pcb
.tcp
);
913 #endif /* TCP_LISTEN_BACKLOG */
915 tcp_recved(msg
->conn
->pcb
.tcp
, msg
->msg
.r
.len
);
920 #endif /* LWIP_TCP */
921 TCPIP_APIMSG_ACK(msg
);
926 * See if more data needs to be written from a previous call to netconn_write.
927 * Called initially from do_write. If the first call can't send all data
928 * (because of low memory or empty send-buffer), this function is called again
929 * from sent_tcp() or poll_tcp() to send more data. If all data is sent, the
930 * blocking application thread (waiting in netconn_write) is released.
932 * @param conn netconn (that is currently in state NETCONN_WRITE) to process
934 * ERR_MEM if LWIP_TCPIP_CORE_LOCKING=1 and sending hasn't yet finished
937 do_writemore(struct netconn
*conn
)
941 u16_t len
, available
;
942 u8_t write_finished
= 0;
944 LWIP_ASSERT("conn->state == NETCONN_WRITE", (conn
->state
== NETCONN_WRITE
));
946 dataptr
= (u8_t
*)conn
->write_msg
->msg
.w
.dataptr
+ conn
->write_offset
;
947 if ((conn
->write_msg
->msg
.w
.len
- conn
->write_offset
> 0xffff)) { /* max_u16_t */
949 #if LWIP_TCPIP_CORE_LOCKING
950 conn
->write_delayed
= 1;
953 len
= conn
->write_msg
->msg
.w
.len
- conn
->write_offset
;
955 available
= tcp_sndbuf(conn
->pcb
.tcp
);
956 if (available
< len
) {
957 /* don't try to write more than sendbuf */
959 #if LWIP_TCPIP_CORE_LOCKING
960 conn
->write_delayed
= 1;
964 err
= tcp_write(conn
->pcb
.tcp
, dataptr
, len
, conn
->write_msg
->msg
.w
.apiflags
);
965 LWIP_ASSERT("do_writemore: invalid length!", ((conn
->write_offset
+ len
) <= conn
->write_msg
->msg
.w
.len
));
967 conn
->write_offset
+= len
;
968 if (conn
->write_offset
== conn
->write_msg
->msg
.w
.len
) {
969 /* everything was written */
971 conn
->write_msg
= NULL
;
972 conn
->write_offset
= 0;
974 err
= tcp_output_nagle(conn
->pcb
.tcp
);
976 if ((err
== ERR_OK
) && (tcp_sndbuf(conn
->pcb
.tcp
) <= TCP_SNDLOWAT
)) {
977 API_EVENT(conn
, NETCONN_EVT_SENDMINUS
, len
);
979 } else if (err
== ERR_MEM
) {
980 /* If ERR_MEM, we wait for sent_tcp or poll_tcp to be called
981 we do NOT return to the application thread, since ERR_MEM is
982 only a temporary error! */
984 /* tcp_enqueue returned ERR_MEM, try tcp_output anyway */
985 err
= tcp_output(conn
->pcb
.tcp
);
987 #if LWIP_TCPIP_CORE_LOCKING
988 conn
->write_delayed
= 1;
991 /* On errors != ERR_MEM, we don't try writing any more but return
992 the error to the application thread. */
997 if (write_finished
) {
998 /* everything was written: set back connection state
999 and back to application task */
1000 conn
->state
= NETCONN_NONE
;
1001 #if LWIP_TCPIP_CORE_LOCKING
1002 if (conn
->write_delayed
!= 0)
1005 sys_sem_signal(conn
->op_completed
);
1008 #if LWIP_TCPIP_CORE_LOCKING
1014 #endif /* LWIP_TCP */
1017 * Send some data on a TCP pcb contained in a netconn
1018 * Called from netconn_write
1020 * @param msg the api_msg_msg pointing to the connection
1023 do_write(struct api_msg_msg
*msg
)
1025 if (!ERR_IS_FATAL(msg
->conn
->err
)) {
1026 if ((msg
->conn
->pcb
.tcp
!= NULL
) && (msg
->conn
->type
== NETCONN_TCP
)) {
1028 msg
->conn
->state
= NETCONN_WRITE
;
1029 /* set all the variables used by do_writemore */
1030 msg
->conn
->write_msg
= msg
;
1031 msg
->conn
->write_offset
= 0;
1032 #if LWIP_TCPIP_CORE_LOCKING
1033 msg
->conn
->write_delayed
= 0;
1034 if (do_writemore(msg
->conn
) != ERR_OK
) {
1035 LWIP_ASSERT("state!", msg
->conn
->state
== NETCONN_WRITE
);
1036 UNLOCK_TCPIP_CORE();
1037 sys_arch_sem_wait(msg
->conn
->op_completed
, 0);
1039 LWIP_ASSERT("state!", msg
->conn
->state
== NETCONN_NONE
);
1042 do_writemore(msg
->conn
);
1044 /* for both cases: if do_writemore was called, don't ACK the APIMSG! */
1046 #endif /* LWIP_TCP */
1047 #if (LWIP_UDP || LWIP_RAW)
1049 msg
->conn
->err
= ERR_VAL
;
1050 #endif /* (LWIP_UDP || LWIP_RAW) */
1053 TCPIP_APIMSG_ACK(msg
);
1057 * Return a connection's local or remote address
1058 * Called from netconn_getaddr
1060 * @param msg the api_msg_msg pointing to the connection
1063 do_getaddr(struct api_msg_msg
*msg
)
1065 if (msg
->conn
->pcb
.ip
!= NULL
) {
1066 *(msg
->msg
.ad
.ipaddr
) = (msg
->msg
.ad
.local
?msg
->conn
->pcb
.ip
->local_ip
:msg
->conn
->pcb
.ip
->remote_ip
);
1068 switch (NETCONNTYPE_GROUP(msg
->conn
->type
)) {
1071 if (msg
->msg
.ad
.local
) {
1072 *(msg
->msg
.ad
.port
) = msg
->conn
->pcb
.raw
->protocol
;
1074 /* return an error as connecting is only a helper for upper layers */
1075 msg
->conn
->err
= ERR_CONN
;
1078 #endif /* LWIP_RAW */
1081 if (msg
->msg
.ad
.local
) {
1082 *(msg
->msg
.ad
.port
) = msg
->conn
->pcb
.udp
->local_port
;
1084 if ((msg
->conn
->pcb
.udp
->flags
& UDP_FLAGS_CONNECTED
) == 0) {
1085 msg
->conn
->err
= ERR_CONN
;
1087 *(msg
->msg
.ad
.port
) = msg
->conn
->pcb
.udp
->remote_port
;
1091 #endif /* LWIP_UDP */
1094 *(msg
->msg
.ad
.port
) = (msg
->msg
.ad
.local
?msg
->conn
->pcb
.tcp
->local_port
:msg
->conn
->pcb
.tcp
->remote_port
);
1096 #endif /* LWIP_TCP */
1099 msg
->conn
->err
= ERR_CONN
;
1101 TCPIP_APIMSG_ACK(msg
);
1105 * Close a TCP pcb contained in a netconn
1106 * Called from netconn_close
1108 * @param msg the api_msg_msg pointing to the connection
1111 do_close(struct api_msg_msg
*msg
)
1114 if ((msg
->conn
->pcb
.tcp
!= NULL
) && (msg
->conn
->type
== NETCONN_TCP
)) {
1115 msg
->conn
->state
= NETCONN_CLOSE
;
1116 do_close_internal(msg
->conn
);
1117 /* for tcp netconns, do_close_internal ACKs the message */
1119 #endif /* LWIP_TCP */
1121 msg
->conn
->err
= ERR_VAL
;
1122 TCPIP_APIMSG_ACK(msg
);
1128 * Join multicast groups for UDP netconns.
1129 * Called from netconn_join_leave_group
1131 * @param msg the api_msg_msg pointing to the connection
1134 do_join_leave_group(struct api_msg_msg
*msg
)
1136 if (!ERR_IS_FATAL(msg
->conn
->err
)) {
1137 if (msg
->conn
->pcb
.tcp
!= NULL
) {
1138 if (NETCONNTYPE_GROUP(msg
->conn
->type
) == NETCONN_UDP
) {
1140 if (msg
->msg
.jl
.join_or_leave
== NETCONN_JOIN
) {
1141 msg
->conn
->err
= igmp_joingroup(msg
->msg
.jl
.interface
, msg
->msg
.jl
.multiaddr
);
1143 msg
->conn
->err
= igmp_leavegroup(msg
->msg
.jl
.interface
, msg
->msg
.jl
.multiaddr
);
1145 #endif /* LWIP_UDP */
1146 #if (LWIP_TCP || LWIP_RAW)
1148 msg
->conn
->err
= ERR_VAL
;
1149 #endif /* (LWIP_TCP || LWIP_RAW) */
1153 TCPIP_APIMSG_ACK(msg
);
1155 #endif /* LWIP_IGMP */
1159 * Callback function that is called when DNS name is resolved
1160 * (or on timeout). A waiting application thread is waked up by
1161 * signaling the semaphore.
1164 do_dns_found(const char *name
, struct ip_addr
*ipaddr
, void *arg
)
1166 struct dns_api_msg
*msg
= (struct dns_api_msg
*)arg
;
1168 LWIP_ASSERT("DNS response for wrong host name", strcmp(msg
->name
, name
) == 0);
1170 if (ipaddr
== NULL
) {
1171 /* timeout or memory error */
1172 *msg
->err
= ERR_VAL
;
1174 /* address was resolved */
1176 *msg
->addr
= *ipaddr
;
1178 /* wake up the application task waiting in netconn_gethostbyname */
1179 sys_sem_signal(msg
->sem
);
1183 * Execute a DNS query
1184 * Called from netconn_gethostbyname
1186 * @param arg the dns_api_msg pointing to the query
1189 do_gethostbyname(void *arg
)
1191 struct dns_api_msg
*msg
= (struct dns_api_msg
*)arg
;
1193 *msg
->err
= dns_gethostbyname(msg
->name
, msg
->addr
, do_dns_found
, msg
);
1194 if (*msg
->err
!= ERR_INPROGRESS
) {
1195 /* on error or immediate success, wake up the application
1196 * task waiting in netconn_gethostbyname */
1197 sys_sem_signal(msg
->sem
);
1200 #endif /* LWIP_DNS */
1202 #endif /* LWIP_NETCONN */