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
);
484 conn
->writebuffer
= 0;
488 conn
->pcb
.tcp
= NULL
;
490 #if (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_UDP_RECVMBOX_SIZE) && \
491 (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_TCP_RECVMBOX_SIZE)
492 size
= DEFAULT_RAW_RECVMBOX_SIZE
;
494 switch(NETCONNTYPE_GROUP(t
)) {
497 size
= DEFAULT_RAW_RECVMBOX_SIZE
;
499 #endif /* LWIP_RAW */
502 size
= DEFAULT_UDP_RECVMBOX_SIZE
;
504 #endif /* LWIP_UDP */
507 size
= DEFAULT_TCP_RECVMBOX_SIZE
;
509 #endif /* LWIP_TCP */
511 LWIP_ASSERT("netconn_alloc: undefined netconn_type", 0);
516 if ((conn
->op_completed
= sys_sem_new(0)) == SYS_SEM_NULL
) {
517 memp_free(MEMP_NETCONN
, conn
);
520 if ((conn
->recvmbox
= sys_mbox_new(size
)) == SYS_MBOX_NULL
) {
521 sys_sem_free(conn
->op_completed
);
522 memp_free(MEMP_NETCONN
, conn
);
526 conn
->acceptmbox
= SYS_MBOX_NULL
;
527 conn
->state
= NETCONN_NONE
;
528 /* initialize socket to -1 since 0 is a valid socket */
530 conn
->callback
= callback
;
531 conn
->recv_avail
= 0;
533 conn
->recv_timeout
= 0;
534 #endif /* LWIP_SO_RCVTIMEO */
536 conn
->recv_bufsize
= INT_MAX
;
537 #endif /* LWIP_SO_RCVBUF */
542 * Delete a netconn and all its resources.
543 * The pcb is NOT freed (since we might not be in the right thread context do this).
545 * @param conn the netconn to free
548 netconn_free(struct netconn
*conn
)
551 LWIP_ASSERT("PCB must be deallocated outside this function", conn
->pcb
.tcp
== NULL
);
553 /* Drain the recvmbox. */
554 if (conn
->recvmbox
!= SYS_MBOX_NULL
) {
555 while (sys_mbox_tryfetch(conn
->recvmbox
, &mem
) != SYS_MBOX_EMPTY
) {
556 if (conn
->type
== NETCONN_TCP
) {
558 pbuf_free((struct pbuf
*)mem
);
561 netbuf_delete((struct netbuf
*)mem
);
564 sys_mbox_free(conn
->recvmbox
);
565 conn
->recvmbox
= SYS_MBOX_NULL
;
568 /* Drain the acceptmbox. */
569 if (conn
->acceptmbox
!= SYS_MBOX_NULL
) {
570 while (sys_mbox_tryfetch(conn
->acceptmbox
, &mem
) != SYS_MBOX_EMPTY
) {
571 netconn_delete((struct netconn
*)mem
);
573 sys_mbox_free(conn
->acceptmbox
);
574 conn
->acceptmbox
= SYS_MBOX_NULL
;
577 //sys_sem_free(conn->op_completed);
578 conn
->op_completed
= SYS_SEM_NULL
;
580 memp_free(MEMP_NETCONN
, conn
);
585 * Internal helper function to close a TCP netconn: since this sometimes
586 * doesn't work at the first attempt, this function is called from multiple
589 * @param conn the TCP netconn to close
592 do_close_internal(struct netconn
*conn
)
596 LWIP_ASSERT("invalid conn", (conn
!= NULL
));
597 LWIP_ASSERT("this is for tcp netconns only", (conn
->type
== NETCONN_TCP
));
598 LWIP_ASSERT("conn must be in state NETCONN_CLOSE", (conn
->state
== NETCONN_CLOSE
));
599 LWIP_ASSERT("pcb already closed", (conn
->pcb
.tcp
!= NULL
));
601 /* Set back some callback pointers */
602 if (conn
->pcb
.tcp
->state
== LISTEN
) {
603 tcp_arg(conn
->pcb
.tcp
, NULL
);
604 tcp_accept(conn
->pcb
.tcp
, NULL
);
606 tcp_recv(conn
->pcb
.tcp
, NULL
);
608 /* Try to close the connection */
609 err
= tcp_close(conn
->pcb
.tcp
);
611 /* Closing succeeded */
612 conn
->state
= NETCONN_NONE
;
613 /* Set back some callback pointers as conn is going away */
614 tcp_err(conn
->pcb
.tcp
, NULL
);
615 tcp_poll(conn
->pcb
.tcp
, NULL
, 4);
616 tcp_sent(conn
->pcb
.tcp
, NULL
);
617 tcp_recv(conn
->pcb
.tcp
, NULL
);
618 tcp_arg(conn
->pcb
.tcp
, NULL
);
619 conn
->pcb
.tcp
= NULL
;
621 /* Trigger select() in socket layer. This send should something else so the
622 errorfd is set, not the read and write fd! */
623 API_EVENT(conn
, NETCONN_EVT_RCVPLUS
, 0);
624 API_EVENT(conn
, NETCONN_EVT_SENDPLUS
, 0);
625 /* wake up the application task */
626 sys_sem_signal(conn
->op_completed
);
628 /* If closing didn't succeed, we get called again either
629 from poll_tcp or from sent_tcp */
631 #endif /* LWIP_TCP */
634 * Delete the pcb inside a netconn.
635 * Called from netconn_delete.
637 * @param msg the api_msg_msg pointing to the connection
640 do_delconn(struct api_msg_msg
*msg
)
642 if (msg
->conn
->pcb
.tcp
!= NULL
) {
643 switch (NETCONNTYPE_GROUP(msg
->conn
->type
)) {
646 raw_remove(msg
->conn
->pcb
.raw
);
648 #endif /* LWIP_RAW */
651 msg
->conn
->pcb
.udp
->recv_arg
= NULL
;
652 udp_remove(msg
->conn
->pcb
.udp
);
654 #endif /* LWIP_UDP */
657 msg
->conn
->state
= NETCONN_CLOSE
;
658 do_close_internal(msg
->conn
);
659 /* API_EVENT is called inside do_close_internal, before releasing
660 the application thread, so we can return at this point! */
662 #endif /* LWIP_TCP */
667 /* tcp netconns don't come here! */
669 /* Trigger select() in socket layer. This send should something else so the
670 errorfd is set, not the read and write fd! */
671 API_EVENT(msg
->conn
, NETCONN_EVT_RCVPLUS
, 0);
672 API_EVENT(msg
->conn
, NETCONN_EVT_SENDPLUS
, 0);
674 if (msg
->conn
->op_completed
!= SYS_SEM_NULL
) {
675 sys_sem_signal(msg
->conn
->op_completed
);
680 * Bind a pcb contained in a netconn
681 * Called from netconn_bind.
683 * @param msg the api_msg_msg pointing to the connection and containing
684 * the IP address and port to bind to
687 do_bind(struct api_msg_msg
*msg
)
689 if (!ERR_IS_FATAL(msg
->conn
->err
)) {
690 if (msg
->conn
->pcb
.tcp
!= NULL
) {
691 switch (NETCONNTYPE_GROUP(msg
->conn
->type
)) {
694 msg
->conn
->err
= raw_bind(msg
->conn
->pcb
.raw
, msg
->msg
.bc
.ipaddr
);
696 #endif /* LWIP_RAW */
699 msg
->conn
->err
= udp_bind(msg
->conn
->pcb
.udp
, msg
->msg
.bc
.ipaddr
, msg
->msg
.bc
.port
);
701 #endif /* LWIP_UDP */
704 msg
->conn
->err
= tcp_bind(msg
->conn
->pcb
.tcp
, msg
->msg
.bc
.ipaddr
, msg
->msg
.bc
.port
);
706 #endif /* LWIP_TCP */
711 /* msg->conn->pcb is NULL */
712 msg
->conn
->err
= ERR_VAL
;
715 TCPIP_APIMSG_ACK(msg
);
720 * TCP callback function if a connection (opened by tcp_connect/do_connect) has
721 * been established (or reset by the remote host).
723 * @see tcp.h (struct tcp_pcb.connected) for parameters and return values
726 do_connected(void *arg
, struct tcp_pcb
*pcb
, err_t err
)
728 struct netconn
*conn
;
730 LWIP_UNUSED_ARG(pcb
);
739 if ((conn
->type
== NETCONN_TCP
) && (err
== ERR_OK
)) {
742 conn
->state
= NETCONN_NONE
;
743 sys_sem_signal(conn
->op_completed
);
746 #endif /* LWIP_TCP */
749 * Connect a pcb contained inside a netconn
750 * Called from netconn_connect.
752 * @param msg the api_msg_msg pointing to the connection and containing
753 * the IP address and port to connect to
756 do_connect(struct api_msg_msg
*msg
)
758 if (msg
->conn
->pcb
.tcp
== NULL
) {
759 sys_sem_signal(msg
->conn
->op_completed
);
763 switch (NETCONNTYPE_GROUP(msg
->conn
->type
)) {
766 msg
->conn
->err
= raw_connect(msg
->conn
->pcb
.raw
, msg
->msg
.bc
.ipaddr
);
767 sys_sem_signal(msg
->conn
->op_completed
);
769 #endif /* LWIP_RAW */
772 msg
->conn
->err
= udp_connect(msg
->conn
->pcb
.udp
, msg
->msg
.bc
.ipaddr
, msg
->msg
.bc
.port
);
773 sys_sem_signal(msg
->conn
->op_completed
);
775 #endif /* LWIP_UDP */
778 msg
->conn
->state
= NETCONN_CONNECT
;
779 setup_tcp(msg
->conn
);
780 msg
->conn
->err
= tcp_connect(msg
->conn
->pcb
.tcp
, msg
->msg
.bc
.ipaddr
, msg
->msg
.bc
.port
,
782 /* sys_sem_signal() is called from do_connected (or err_tcp()),
783 * when the connection is established! */
785 #endif /* LWIP_TCP */
792 * Connect a pcb contained inside a netconn
793 * Only used for UDP netconns.
794 * Called from netconn_disconnect.
796 * @param msg the api_msg_msg pointing to the connection to disconnect
799 do_disconnect(struct api_msg_msg
*msg
)
802 if (NETCONNTYPE_GROUP(msg
->conn
->type
) == NETCONN_UDP
) {
803 udp_disconnect(msg
->conn
->pcb
.udp
);
805 #endif /* LWIP_UDP */
806 TCPIP_APIMSG_ACK(msg
);
810 * Set a TCP pcb contained in a netconn into listen mode
811 * Called from netconn_listen.
813 * @param msg the api_msg_msg pointing to the connection
816 do_listen(struct api_msg_msg
*msg
)
819 if (!ERR_IS_FATAL(msg
->conn
->err
)) {
820 if (msg
->conn
->pcb
.tcp
!= NULL
) {
821 if (msg
->conn
->type
== NETCONN_TCP
) {
822 if (msg
->conn
->pcb
.tcp
->state
== CLOSED
) {
823 #if TCP_LISTEN_BACKLOG
824 struct tcp_pcb
* lpcb
= tcp_listen_with_backlog(msg
->conn
->pcb
.tcp
, msg
->msg
.lb
.backlog
);
825 #else /* TCP_LISTEN_BACKLOG */
826 struct tcp_pcb
* lpcb
= tcp_listen(msg
->conn
->pcb
.tcp
);
827 #endif /* TCP_LISTEN_BACKLOG */
829 msg
->conn
->err
= ERR_MEM
;
831 /* delete the recvmbox and allocate the acceptmbox */
832 if (msg
->conn
->recvmbox
!= SYS_MBOX_NULL
) {
833 /** @todo: should we drain the recvmbox here? */
834 sys_mbox_free(msg
->conn
->recvmbox
);
835 msg
->conn
->recvmbox
= SYS_MBOX_NULL
;
837 if (msg
->conn
->acceptmbox
== SYS_MBOX_NULL
) {
838 if ((msg
->conn
->acceptmbox
= sys_mbox_new(DEFAULT_ACCEPTMBOX_SIZE
)) == SYS_MBOX_NULL
) {
839 msg
->conn
->err
= ERR_MEM
;
842 if (msg
->conn
->err
== ERR_OK
) {
843 msg
->conn
->state
= NETCONN_LISTEN
;
844 msg
->conn
->pcb
.tcp
= lpcb
;
845 tcp_arg(msg
->conn
->pcb
.tcp
, msg
->conn
);
846 tcp_accept(msg
->conn
->pcb
.tcp
, accept_function
);
850 msg
->conn
->err
= ERR_CONN
;
855 #endif /* LWIP_TCP */
856 TCPIP_APIMSG_ACK(msg
);
860 * Send some data on a RAW or UDP pcb contained in a netconn
861 * Called from netconn_send
863 * @param msg the api_msg_msg pointing to the connection
866 do_send(struct api_msg_msg
*msg
)
868 if (!ERR_IS_FATAL(msg
->conn
->err
)) {
869 if (msg
->conn
->pcb
.tcp
!= NULL
) {
870 switch (NETCONNTYPE_GROUP(msg
->conn
->type
)) {
873 if (msg
->msg
.b
->addr
== NULL
) {
874 msg
->conn
->err
= raw_send(msg
->conn
->pcb
.raw
, msg
->msg
.b
->p
);
876 msg
->conn
->err
= raw_sendto(msg
->conn
->pcb
.raw
, msg
->msg
.b
->p
, msg
->msg
.b
->addr
);
882 if (msg
->msg
.b
->addr
== NULL
) {
883 msg
->conn
->err
= udp_send(msg
->conn
->pcb
.udp
, msg
->msg
.b
->p
);
885 msg
->conn
->err
= udp_sendto(msg
->conn
->pcb
.udp
, msg
->msg
.b
->p
, msg
->msg
.b
->addr
, msg
->msg
.b
->port
);
888 #endif /* LWIP_UDP */
894 TCPIP_APIMSG_ACK(msg
);
898 * Recv some data from a RAW or UDP pcb contained in a netconn
899 * Called from netconn_recv
901 * @param msg the api_msg_msg pointing to the connection
904 do_recv(struct api_msg_msg
*msg
)
907 if (!ERR_IS_FATAL(msg
->conn
->err
)) {
908 if (msg
->conn
->pcb
.tcp
!= NULL
) {
909 if (msg
->conn
->type
== NETCONN_TCP
) {
910 #if TCP_LISTEN_BACKLOG
911 if (msg
->conn
->pcb
.tcp
->state
== LISTEN
) {
912 tcp_accepted(msg
->conn
->pcb
.tcp
);
914 #endif /* TCP_LISTEN_BACKLOG */
916 tcp_recved(msg
->conn
->pcb
.tcp
, msg
->msg
.r
.len
);
921 #endif /* LWIP_TCP */
922 TCPIP_APIMSG_ACK(msg
);
927 * See if more data needs to be written from a previous call to netconn_write.
928 * Called initially from do_write. If the first call can't send all data
929 * (because of low memory or empty send-buffer), this function is called again
930 * from sent_tcp() or poll_tcp() to send more data. If all data is sent, the
931 * blocking application thread (waiting in netconn_write) is released.
933 * @param conn netconn (that is currently in state NETCONN_WRITE) to process
935 * ERR_MEM if LWIP_TCPIP_CORE_LOCKING=1 and sending hasn't yet finished
938 do_writemore(struct netconn
*conn
)
942 u16_t len
, available
;
943 u8_t write_finished
= 0;
945 LWIP_ASSERT("conn->state == NETCONN_WRITE", (conn
->state
== NETCONN_WRITE
));
947 dataptr
= (u8_t
*)conn
->write_msg
->msg
.w
.dataptr
+ conn
->write_offset
;
948 if ((conn
->write_msg
->msg
.w
.len
- conn
->write_offset
> 0xffff)) { /* max_u16_t */
950 #if LWIP_TCPIP_CORE_LOCKING
951 conn
->write_delayed
= 1;
954 len
= conn
->write_msg
->msg
.w
.len
- conn
->write_offset
;
956 available
= tcp_sndbuf(conn
->pcb
.tcp
);
957 if (available
< len
) {
958 /* don't try to write more than sendbuf */
960 #if LWIP_TCPIP_CORE_LOCKING
961 conn
->write_delayed
= 1;
965 err
= tcp_write(conn
->pcb
.tcp
, dataptr
, len
, conn
->write_msg
->msg
.w
.apiflags
);
966 LWIP_ASSERT("do_writemore: invalid length!", ((conn
->write_offset
+ len
) <= conn
->write_msg
->msg
.w
.len
));
968 conn
->write_offset
+= len
;
969 if (conn
->write_offset
== conn
->write_msg
->msg
.w
.len
) {
970 /* everything was written */
972 conn
->write_msg
= NULL
;
973 conn
->write_offset
= 0;
975 err
= tcp_output_nagle(conn
->pcb
.tcp
);
977 if ((err
== ERR_OK
) && (tcp_sndbuf(conn
->pcb
.tcp
) <= TCP_SNDLOWAT
)) {
978 API_EVENT(conn
, NETCONN_EVT_SENDMINUS
, len
);
980 } else if (err
== ERR_MEM
) {
981 /* If ERR_MEM, we wait for sent_tcp or poll_tcp to be called
982 we do NOT return to the application thread, since ERR_MEM is
983 only a temporary error! */
985 /* tcp_enqueue returned ERR_MEM, try tcp_output anyway */
986 err
= tcp_output(conn
->pcb
.tcp
);
988 #if LWIP_TCPIP_CORE_LOCKING
989 conn
->write_delayed
= 1;
992 /* On errors != ERR_MEM, we don't try writing any more but return
993 the error to the application thread. */
998 if (write_finished
) {
999 /* everything was written: set back connection state
1000 and back to application task */
1001 conn
->state
= NETCONN_NONE
;
1002 #if LWIP_TCPIP_CORE_LOCKING
1003 if (conn
->write_delayed
!= 0)
1006 sys_sem_signal(conn
->op_completed
);
1009 #if LWIP_TCPIP_CORE_LOCKING
1015 #endif /* LWIP_TCP */
1018 * Send some data on a TCP pcb contained in a netconn
1019 * Called from netconn_write
1021 * @param msg the api_msg_msg pointing to the connection
1024 do_write(struct api_msg_msg
*msg
)
1026 if (!ERR_IS_FATAL(msg
->conn
->err
)) {
1027 if ((msg
->conn
->pcb
.tcp
!= NULL
) && (msg
->conn
->type
== NETCONN_TCP
)) {
1029 msg
->conn
->state
= NETCONN_WRITE
;
1030 /* set all the variables used by do_writemore */
1031 msg
->conn
->write_msg
= msg
;
1032 msg
->conn
->write_offset
= 0;
1033 #if LWIP_TCPIP_CORE_LOCKING
1034 msg
->conn
->write_delayed
= 0;
1035 if (do_writemore(msg
->conn
) != ERR_OK
) {
1036 LWIP_ASSERT("state!", msg
->conn
->state
== NETCONN_WRITE
);
1037 UNLOCK_TCPIP_CORE();
1038 sys_arch_sem_wait(msg
->conn
->op_completed
, 0);
1040 LWIP_ASSERT("state!", msg
->conn
->state
== NETCONN_NONE
);
1043 do_writemore(msg
->conn
);
1045 /* for both cases: if do_writemore was called, don't ACK the APIMSG! */
1047 #endif /* LWIP_TCP */
1048 #if (LWIP_UDP || LWIP_RAW)
1050 msg
->conn
->err
= ERR_VAL
;
1051 #endif /* (LWIP_UDP || LWIP_RAW) */
1054 TCPIP_APIMSG_ACK(msg
);
1058 * Return a connection's local or remote address
1059 * Called from netconn_getaddr
1061 * @param msg the api_msg_msg pointing to the connection
1064 do_getaddr(struct api_msg_msg
*msg
)
1066 if (msg
->conn
->pcb
.ip
!= NULL
) {
1067 *(msg
->msg
.ad
.ipaddr
) = (msg
->msg
.ad
.local
?msg
->conn
->pcb
.ip
->local_ip
:msg
->conn
->pcb
.ip
->remote_ip
);
1069 switch (NETCONNTYPE_GROUP(msg
->conn
->type
)) {
1072 if (msg
->msg
.ad
.local
) {
1073 *(msg
->msg
.ad
.port
) = msg
->conn
->pcb
.raw
->protocol
;
1075 /* return an error as connecting is only a helper for upper layers */
1076 msg
->conn
->err
= ERR_CONN
;
1079 #endif /* LWIP_RAW */
1082 if (msg
->msg
.ad
.local
) {
1083 *(msg
->msg
.ad
.port
) = msg
->conn
->pcb
.udp
->local_port
;
1085 if ((msg
->conn
->pcb
.udp
->flags
& UDP_FLAGS_CONNECTED
) == 0) {
1086 msg
->conn
->err
= ERR_CONN
;
1088 *(msg
->msg
.ad
.port
) = msg
->conn
->pcb
.udp
->remote_port
;
1092 #endif /* LWIP_UDP */
1095 *(msg
->msg
.ad
.port
) = (msg
->msg
.ad
.local
?msg
->conn
->pcb
.tcp
->local_port
:msg
->conn
->pcb
.tcp
->remote_port
);
1097 #endif /* LWIP_TCP */
1100 msg
->conn
->err
= ERR_CONN
;
1102 TCPIP_APIMSG_ACK(msg
);
1106 * Close a TCP pcb contained in a netconn
1107 * Called from netconn_close
1109 * @param msg the api_msg_msg pointing to the connection
1112 do_close(struct api_msg_msg
*msg
)
1115 if ((msg
->conn
->pcb
.tcp
!= NULL
) && (msg
->conn
->type
== NETCONN_TCP
)) {
1116 msg
->conn
->state
= NETCONN_CLOSE
;
1117 do_close_internal(msg
->conn
);
1118 /* for tcp netconns, do_close_internal ACKs the message */
1120 #endif /* LWIP_TCP */
1122 msg
->conn
->err
= ERR_VAL
;
1123 TCPIP_APIMSG_ACK(msg
);
1129 * Join multicast groups for UDP netconns.
1130 * Called from netconn_join_leave_group
1132 * @param msg the api_msg_msg pointing to the connection
1135 do_join_leave_group(struct api_msg_msg
*msg
)
1137 if (!ERR_IS_FATAL(msg
->conn
->err
)) {
1138 if (msg
->conn
->pcb
.tcp
!= NULL
) {
1139 if (NETCONNTYPE_GROUP(msg
->conn
->type
) == NETCONN_UDP
) {
1141 if (msg
->msg
.jl
.join_or_leave
== NETCONN_JOIN
) {
1142 msg
->conn
->err
= igmp_joingroup(msg
->msg
.jl
.interface
, msg
->msg
.jl
.multiaddr
);
1144 msg
->conn
->err
= igmp_leavegroup(msg
->msg
.jl
.interface
, msg
->msg
.jl
.multiaddr
);
1146 #endif /* LWIP_UDP */
1147 #if (LWIP_TCP || LWIP_RAW)
1149 msg
->conn
->err
= ERR_VAL
;
1150 #endif /* (LWIP_TCP || LWIP_RAW) */
1154 TCPIP_APIMSG_ACK(msg
);
1156 #endif /* LWIP_IGMP */
1160 * Callback function that is called when DNS name is resolved
1161 * (or on timeout). A waiting application thread is waked up by
1162 * signaling the semaphore.
1165 do_dns_found(const char *name
, struct ip_addr
*ipaddr
, void *arg
)
1167 struct dns_api_msg
*msg
= (struct dns_api_msg
*)arg
;
1169 LWIP_ASSERT("DNS response for wrong host name", strcmp(msg
->name
, name
) == 0);
1171 if (ipaddr
== NULL
) {
1172 /* timeout or memory error */
1173 *msg
->err
= ERR_VAL
;
1175 /* address was resolved */
1177 *msg
->addr
= *ipaddr
;
1179 /* wake up the application task waiting in netconn_gethostbyname */
1180 sys_sem_signal(msg
->sem
);
1184 * Execute a DNS query
1185 * Called from netconn_gethostbyname
1187 * @param arg the dns_api_msg pointing to the query
1190 do_gethostbyname(void *arg
)
1192 struct dns_api_msg
*msg
= (struct dns_api_msg
*)arg
;
1194 *msg
->err
= dns_gethostbyname(msg
->name
, msg
->addr
, do_dns_found
, msg
);
1195 if (*msg
->err
!= ERR_INPROGRESS
) {
1196 /* on error or immediate success, wake up the application
1197 * task waiting in netconn_gethostbyname */
1198 sys_sem_signal(msg
->sem
);
1201 #endif /* LWIP_DNS */
1203 #endif /* LWIP_NETCONN */