3 * Sequential API External 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>
39 /* This is the part of the API that is linked with
44 #if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
47 #include "lwip/tcpip.h"
48 #include "lwip/memp.h"
58 * Create a new netconn (of a specific type) that has a callback function.
59 * The corresponding pcb is also created.
61 * @param t the type of 'connection' to create (@see enum netconn_type)
62 * @param proto the IP protocol for RAW IP pcbs
63 * @param callback a function to call on status changes (RX available, TX'ed)
64 * @return a newly allocated struct netconn or
65 * NULL on memory error
68 netconn_new_with_proto_and_callback(enum netconn_type t
, u8_t proto
, netconn_callback callback
)
73 conn
= netconn_alloc(t
, callback
);
75 msg
.function
= do_newconn
;
76 msg
.msg
.msg
.n
.proto
= proto
;
78 if (TCPIP_APIMSG(&msg
) != ERR_OK
) {
79 LWIP_ASSERT("freeing conn without freeing pcb", conn
->pcb
.tcp
== NULL
);
80 LWIP_ASSERT("conn has no op_completed", sys_sem_valid(&conn
->op_completed
));
81 LWIP_ASSERT("conn has no recvmbox", sys_mbox_valid(&conn
->recvmbox
));
83 LWIP_ASSERT("conn->acceptmbox shouldn't exist", !sys_mbox_valid(&conn
->acceptmbox
));
85 sys_sem_free(&conn
->op_completed
);
86 sys_mbox_free(&conn
->recvmbox
);
87 memp_free(MEMP_NETCONN
, conn
);
95 * Close a netconn 'connection' and free its resources.
96 * UDP and RAW connection are completely closed, TCP pcbs might still be in a waitstate
99 * @param conn the netconn to delete
100 * @return ERR_OK if the connection was deleted
103 netconn_delete(struct netconn
*conn
)
107 /* No ASSERT here because possible to get a (conn == NULL) if we got an accept error */
112 msg
.function
= do_delconn
;
118 /* don't care for return value of do_delconn since it only calls void functions */
124 * Get the local or remote IP address and port of a netconn.
125 * For RAW netconns, this returns the protocol instead of a port!
127 * @param conn the netconn to query
128 * @param addr a pointer to which to save the IP address
129 * @param port a pointer to which to save the port (or protocol for RAW)
130 * @param local 1 to get the local IP address, 0 to get the remote one
131 * @return ERR_CONN for invalid connections
132 * ERR_OK if the information was retrieved
135 netconn_getaddr(struct netconn
*conn
, ip_addr_t
*addr
, u16_t
*port
, u8_t local
)
140 LWIP_ERROR("netconn_getaddr: invalid conn", (conn
!= NULL
), return ERR_ARG
;);
141 LWIP_ERROR("netconn_getaddr: invalid addr", (addr
!= NULL
), return ERR_ARG
;);
142 LWIP_ERROR("netconn_getaddr: invalid port", (port
!= NULL
), return ERR_ARG
;);
144 msg
.function
= do_getaddr
;
146 msg
.msg
.msg
.ad
.ipaddr
= addr
;
147 msg
.msg
.msg
.ad
.port
= port
;
148 msg
.msg
.msg
.ad
.local
= local
;
149 err
= TCPIP_APIMSG(&msg
);
151 NETCONN_SET_SAFE_ERR(conn
, err
);
156 * Bind a netconn to a specific local IP address and port.
157 * Binding one netconn twice might not always be checked correctly!
159 * @param conn the netconn to bind
160 * @param addr the local IP address to bind the netconn to (use IP_ADDR_ANY
161 * to bind to all addresses)
162 * @param port the local port to bind the netconn to (not used for RAW)
163 * @return ERR_OK if bound, any other err_t on failure
166 netconn_bind(struct netconn
*conn
, ip_addr_t
*addr
, u16_t port
)
171 LWIP_ERROR("netconn_bind: invalid conn", (conn
!= NULL
), return ERR_ARG
;);
173 msg
.function
= do_bind
;
175 msg
.msg
.msg
.bc
.ipaddr
= addr
;
176 msg
.msg
.msg
.bc
.port
= port
;
177 err
= TCPIP_APIMSG(&msg
);
179 NETCONN_SET_SAFE_ERR(conn
, err
);
184 * Connect a netconn to a specific remote IP address and port.
186 * @param conn the netconn to connect
187 * @param addr the remote IP address to connect to
188 * @param port the remote port to connect to (no used for RAW)
189 * @return ERR_OK if connected, return value of tcp_/udp_/raw_connect otherwise
192 netconn_connect(struct netconn
*conn
, ip_addr_t
*addr
, u16_t port
)
197 LWIP_ERROR("netconn_connect: invalid conn", (conn
!= NULL
), return ERR_ARG
;);
199 msg
.function
= do_connect
;
201 msg
.msg
.msg
.bc
.ipaddr
= addr
;
202 msg
.msg
.msg
.bc
.port
= port
;
203 /* This is the only function which need to not block tcpip_thread */
204 err
= tcpip_apimsg(&msg
);
206 NETCONN_SET_SAFE_ERR(conn
, err
);
211 * Disconnect a netconn from its current peer (only valid for UDP netconns).
213 * @param conn the netconn to disconnect
214 * @return TODO: return value is not set here...
217 netconn_disconnect(struct netconn
*conn
)
222 LWIP_ERROR("netconn_disconnect: invalid conn", (conn
!= NULL
), return ERR_ARG
;);
224 msg
.function
= do_disconnect
;
226 err
= TCPIP_APIMSG(&msg
);
228 NETCONN_SET_SAFE_ERR(conn
, err
);
233 * Set a TCP netconn into listen mode
235 * @param conn the tcp netconn to set to listen mode
236 * @param backlog the listen backlog, only used if TCP_LISTEN_BACKLOG==1
237 * @return ERR_OK if the netconn was set to listen (UDP and RAW netconns
238 * don't return any error (yet?))
241 netconn_listen_with_backlog(struct netconn
*conn
, u8_t backlog
)
247 /* This does no harm. If TCP_LISTEN_BACKLOG is off, backlog is unused. */
248 LWIP_UNUSED_ARG(backlog
);
250 LWIP_ERROR("netconn_listen: invalid conn", (conn
!= NULL
), return ERR_ARG
;);
252 msg
.function
= do_listen
;
254 #if TCP_LISTEN_BACKLOG
255 msg
.msg
.msg
.lb
.backlog
= backlog
;
256 #endif /* TCP_LISTEN_BACKLOG */
257 err
= TCPIP_APIMSG(&msg
);
259 NETCONN_SET_SAFE_ERR(conn
, err
);
262 LWIP_UNUSED_ARG(conn
);
263 LWIP_UNUSED_ARG(backlog
);
265 #endif /* LWIP_TCP */
269 * Accept a new connection on a TCP listening netconn.
271 * @param conn the TCP listen netconn
272 * @param new_conn pointer where the new connection is stored
273 * @return ERR_OK if a new connection has been received or an error
277 netconn_accept(struct netconn
*conn
, struct netconn
**new_conn
)
280 struct netconn
*newconn
;
282 #if TCP_LISTEN_BACKLOG
284 #endif /* TCP_LISTEN_BACKLOG */
286 LWIP_ERROR("netconn_accept: invalid pointer", (new_conn
!= NULL
), return ERR_ARG
;);
288 LWIP_ERROR("netconn_accept: invalid conn", (conn
!= NULL
), return ERR_ARG
;);
289 LWIP_ERROR("netconn_accept: invalid acceptmbox", sys_mbox_valid(&conn
->acceptmbox
), return ERR_ARG
;);
291 err
= conn
->last_err
;
292 if (ERR_IS_FATAL(err
)) {
293 /* don't recv on fatal errors: this might block the application task
294 waiting on acceptmbox forever! */
299 if (sys_arch_mbox_fetch(&conn
->acceptmbox
, (void **)&newconn
, conn
->recv_timeout
) == SYS_ARCH_TIMEOUT
) {
300 NETCONN_SET_SAFE_ERR(conn
, ERR_TIMEOUT
);
304 sys_arch_mbox_fetch(&conn
->acceptmbox
, (void **)&newconn
, 0);
305 #endif /* LWIP_SO_RCVTIMEO*/
306 /* Register event with callback */
307 API_EVENT(conn
, NETCONN_EVT_RCVMINUS
, 0);
309 if (newconn
== NULL
) {
310 /* connection has been aborted */
311 NETCONN_SET_SAFE_ERR(conn
, ERR_ABRT
);
314 #if TCP_LISTEN_BACKLOG
315 /* Let the stack know that we have accepted the connection. */
316 msg
.function
= do_recv
;
318 /* don't care for the return value of do_recv */
320 #endif /* TCP_LISTEN_BACKLOG */
323 /* don't set conn->last_err: it's only ERR_OK, anyway */
326 LWIP_UNUSED_ARG(conn
);
327 LWIP_UNUSED_ARG(new_conn
);
329 #endif /* LWIP_TCP */
333 * Receive data: actual implementation that doesn't care whether pbuf or netbuf
336 * @param conn the netconn from which to receive data
337 * @param new_buf pointer where a new pbuf/netbuf is stored when received data
338 * @return ERR_OK if data has been received, an error code otherwise (timeout,
339 * memory error or another error)
342 netconn_recv_data(struct netconn
*conn
, void **new_buf
)
349 #endif /* LWIP_TCP */
351 LWIP_ERROR("netconn_recv: invalid pointer", (new_buf
!= NULL
), return ERR_ARG
;);
353 LWIP_ERROR("netconn_recv: invalid conn", (conn
!= NULL
), return ERR_ARG
;);
354 LWIP_ERROR("netconn_accept: invalid recvmbox", sys_mbox_valid(&conn
->recvmbox
), return ERR_CONN
;);
356 err
= conn
->last_err
;
357 if (ERR_IS_FATAL(err
)) {
358 /* don't recv on fatal errors: this might block the application task
359 waiting on recvmbox forever! */
360 /* @todo: this does not allow us to fetch data that has been put into recvmbox
361 before the fatal error occurred - is that a problem? */
366 if (sys_arch_mbox_fetch(&conn
->recvmbox
, &buf
, conn
->recv_timeout
) == SYS_ARCH_TIMEOUT
) {
367 NETCONN_SET_SAFE_ERR(conn
, ERR_TIMEOUT
);
371 sys_arch_mbox_fetch(&conn
->recvmbox
, &buf
, 0);
372 #endif /* LWIP_SO_RCVTIMEO*/
375 if (conn
->type
== NETCONN_TCP
) {
376 if (!netconn_get_noautorecved(conn
) || (buf
== NULL
)) {
377 /* Let the stack know that we have taken the data. */
378 /* TODO: Speedup: Don't block and wait for the answer here
379 (to prevent multiple thread-switches). */
380 msg
.function
= do_recv
;
383 msg
.msg
.msg
.r
.len
= ((struct pbuf
*)buf
)->tot_len
;
385 msg
.msg
.msg
.r
.len
= 1;
387 /* don't care for the return value of do_recv */
391 /* If we are closed, we indicate that we no longer wish to use the socket */
393 API_EVENT(conn
, NETCONN_EVT_RCVMINUS
, 0);
394 /* Avoid to lose any previous error code */
395 NETCONN_SET_SAFE_ERR(conn
, ERR_CLSD
);
398 len
= ((struct pbuf
*)buf
)->tot_len
;
400 #endif /* LWIP_TCP */
401 #if LWIP_TCP && (LWIP_UDP || LWIP_RAW)
403 #endif /* LWIP_TCP && (LWIP_UDP || LWIP_RAW) */
404 #if (LWIP_UDP || LWIP_RAW)
406 LWIP_ASSERT("buf != NULL", buf
!= NULL
);
407 len
= netbuf_len((struct netbuf
*)buf
);
409 #endif /* (LWIP_UDP || LWIP_RAW) */
412 SYS_ARCH_DEC(conn
->recv_avail
, len
);
413 #endif /* LWIP_SO_RCVBUF */
414 /* Register event with callback */
415 API_EVENT(conn
, NETCONN_EVT_RCVMINUS
, len
);
417 LWIP_DEBUGF(API_LIB_DEBUG
, ("netconn_recv_data: received %p, len=%"U16_F
"\n", buf
, len
));
420 /* don't set conn->last_err: it's only ERR_OK, anyway */
425 * Receive data (in form of a pbuf) from a TCP netconn
427 * @param conn the netconn from which to receive data
428 * @param new_buf pointer where a new pbuf is stored when received data
429 * @return ERR_OK if data has been received, an error code otherwise (timeout,
430 * memory error or another error)
431 * ERR_ARG if conn is not a TCP netconn
434 netconn_recv_tcp_pbuf(struct netconn
*conn
, struct pbuf
**new_buf
)
436 LWIP_ERROR("netconn_recv: invalid conn", (conn
!= NULL
) &&
437 netconn_type(conn
) == NETCONN_TCP
, return ERR_ARG
;);
439 return netconn_recv_data(conn
, (void **)new_buf
);
443 * Receive data (in form of a netbuf containing a packet buffer) from a netconn
445 * @param conn the netconn from which to receive data
446 * @param new_buf pointer where a new netbuf is stored when received data
447 * @return ERR_OK if data has been received, an error code otherwise (timeout,
448 * memory error or another error)
451 netconn_recv(struct netconn
*conn
, struct netbuf
**new_buf
)
454 struct netbuf
*buf
= NULL
;
456 #endif /* LWIP_TCP */
458 LWIP_ERROR("netconn_recv: invalid pointer", (new_buf
!= NULL
), return ERR_ARG
;);
460 LWIP_ERROR("netconn_recv: invalid conn", (conn
!= NULL
), return ERR_ARG
;);
461 LWIP_ERROR("netconn_accept: invalid recvmbox", sys_mbox_valid(&conn
->recvmbox
), return ERR_CONN
;);
464 if (conn
->type
== NETCONN_TCP
) {
465 struct pbuf
*p
= NULL
;
466 /* This is not a listening netconn, since recvmbox is set */
468 buf
= (struct netbuf
*)memp_malloc(MEMP_NETBUF
);
470 NETCONN_SET_SAFE_ERR(conn
, ERR_MEM
);
474 err
= netconn_recv_data(conn
, (void **)&p
);
476 memp_free(MEMP_NETBUF
, buf
);
479 LWIP_ASSERT("p != NULL", p
!= NULL
);
484 ip_addr_set_any(&buf
->addr
);
486 /* don't set conn->last_err: it's only ERR_OK, anyway */
489 #endif /* LWIP_TCP */
491 #if (LWIP_UDP || LWIP_RAW)
492 return netconn_recv_data(conn
, (void **)new_buf
);
493 #endif /* (LWIP_UDP || LWIP_RAW) */
498 * TCP: update the receive window: by calling this, the application
499 * tells the stack that it has processed data and is able to accept
501 * ATTENTION: use with care, this is mainly used for sockets!
502 * Can only be used when calling netconn_set_noautorecved(conn, 1) before.
504 * @param conn the netconn for which to update the receive window
505 * @param length amount of data processed (ATTENTION: this must be accurate!)
508 netconn_recved(struct netconn
*conn
, u32_t length
)
511 if ((conn
!= NULL
) && (conn
->type
== NETCONN_TCP
) &&
512 (netconn_get_noautorecved(conn
))) {
514 /* Let the stack know that we have taken the data. */
515 /* TODO: Speedup: Don't block and wait for the answer here
516 (to prevent multiple thread-switches). */
517 msg
.function
= do_recv
;
519 msg
.msg
.msg
.r
.len
= length
;
520 /* don't care for the return value of do_recv */
524 LWIP_UNUSED_ARG(conn
);
525 LWIP_UNUSED_ARG(length
);
526 #endif /* LWIP_TCP */
530 * Send data (in form of a netbuf) to a specific remote IP address and port.
531 * Only to be used for UDP and RAW netconns (not TCP).
533 * @param conn the netconn over which to send data
534 * @param buf a netbuf containing the data to send
535 * @param addr the remote IP address to which to send the data
536 * @param port the remote port to which to send the data
537 * @return ERR_OK if data was sent, any other err_t on error
540 netconn_sendto(struct netconn
*conn
, struct netbuf
*buf
, ip_addr_t
*addr
, u16_t port
)
543 ip_addr_set(&buf
->addr
, addr
);
545 return netconn_send(conn
, buf
);
551 * Send data over a UDP or RAW netconn (that is already connected).
553 * @param conn the UDP or RAW netconn over which to send data
554 * @param buf a netbuf containing the data to send
555 * @return ERR_OK if data was sent, any other err_t on error
558 netconn_send(struct netconn
*conn
, struct netbuf
*buf
)
563 LWIP_ERROR("netconn_send: invalid conn", (conn
!= NULL
), return ERR_ARG
;);
565 LWIP_DEBUGF(API_LIB_DEBUG
, ("netconn_send: sending %"U16_F
" bytes\n", buf
->p
->tot_len
));
566 msg
.function
= do_send
;
569 err
= TCPIP_APIMSG(&msg
);
571 NETCONN_SET_SAFE_ERR(conn
, err
);
576 * Send data over a TCP netconn.
578 * @param conn the TCP netconn over which to send data
579 * @param dataptr pointer to the application buffer that contains the data to send
580 * @param size size of the application data to send
581 * @param apiflags combination of following flags :
582 * - NETCONN_COPY: data will be copied into memory belonging to the stack
583 * - NETCONN_MORE: for TCP connection, PSH flag will be set on last segment sent
584 * - NETCONN_DONTBLOCK: only write the data if all dat can be written at once
585 * @return ERR_OK if data was sent, any other err_t on error
588 netconn_write(struct netconn
*conn
, const void *dataptr
, size_t size
, u8_t apiflags
)
593 LWIP_ERROR("netconn_write: invalid conn", (conn
!= NULL
), return ERR_ARG
;);
594 LWIP_ERROR("netconn_write: invalid conn->type", (conn
->type
== NETCONN_TCP
), return ERR_VAL
;);
599 /* @todo: for non-blocking write, check if 'size' would ever fit into
600 snd_queue or snd_buf */
601 msg
.function
= do_write
;
603 msg
.msg
.msg
.w
.dataptr
= dataptr
;
604 msg
.msg
.msg
.w
.apiflags
= apiflags
;
605 msg
.msg
.msg
.w
.len
= size
;
606 /* For locking the core: this _can_ be delayed on low memory/low send buffer,
607 but if it is, this is done inside api_msg.c:do_write(), so we can use the
608 non-blocking version here. */
609 err
= TCPIP_APIMSG(&msg
);
611 NETCONN_SET_SAFE_ERR(conn
, err
);
616 * Close ot shutdown a TCP netconn (doesn't delete it).
618 * @param conn the TCP netconn to close or shutdown
619 * @param how fully close or only shutdown one side?
620 * @return ERR_OK if the netconn was closed, any other err_t on error
623 netconn_close_shutdown(struct netconn
*conn
, u8_t how
)
628 LWIP_ERROR("netconn_close: invalid conn", (conn
!= NULL
), return ERR_ARG
;);
630 msg
.function
= do_close
;
632 /* shutting down both ends is the same as closing */
633 msg
.msg
.msg
.sd
.shut
= how
;
634 /* because of the LWIP_TCPIP_CORE_LOCKING implementation of do_close,
635 don't use TCPIP_APIMSG here */
636 err
= tcpip_apimsg(&msg
);
638 NETCONN_SET_SAFE_ERR(conn
, err
);
643 * Close a TCP netconn (doesn't delete it).
645 * @param conn the TCP netconn to close
646 * @return ERR_OK if the netconn was closed, any other err_t on error
649 netconn_close(struct netconn
*conn
)
651 /* shutting down both ends is the same as closing */
652 return netconn_close_shutdown(conn
, NETCONN_SHUT_RDWR
);
656 * Shut down one or both sides of a TCP netconn (doesn't delete it).
658 * @param conn the TCP netconn to shut down
659 * @return ERR_OK if the netconn was closed, any other err_t on error
662 netconn_shutdown(struct netconn
*conn
, u8_t shut_rx
, u8_t shut_tx
)
664 return netconn_close_shutdown(conn
, (shut_rx
? NETCONN_SHUT_RD
: 0) | (shut_tx
? NETCONN_SHUT_WR
: 0));
669 * Join multicast groups for UDP netconns.
671 * @param conn the UDP netconn for which to change multicast addresses
672 * @param multiaddr IP address of the multicast group to join or leave
673 * @param netif_addr the IP address of the network interface on which to send
675 * @param join_or_leave flag whether to send a join- or leave-message
676 * @return ERR_OK if the action was taken, any err_t on error
679 netconn_join_leave_group(struct netconn
*conn
,
680 ip_addr_t
*multiaddr
,
681 ip_addr_t
*netif_addr
,
682 enum netconn_igmp join_or_leave
)
687 LWIP_ERROR("netconn_join_leave_group: invalid conn", (conn
!= NULL
), return ERR_ARG
;);
689 msg
.function
= do_join_leave_group
;
691 msg
.msg
.msg
.jl
.multiaddr
= multiaddr
;
692 msg
.msg
.msg
.jl
.netif_addr
= netif_addr
;
693 msg
.msg
.msg
.jl
.join_or_leave
= join_or_leave
;
694 err
= TCPIP_APIMSG(&msg
);
696 NETCONN_SET_SAFE_ERR(conn
, err
);
699 #endif /* LWIP_IGMP */
703 * Execute a DNS query, only one IP address is returned
705 * @param name a string representation of the DNS host name to query
706 * @param addr a preallocated ip_addr_t where to store the resolved IP address
707 * @return ERR_OK: resolving succeeded
708 * ERR_MEM: memory error, try again later
709 * ERR_ARG: dns client not initialized or invalid hostname
710 * ERR_VAL: dns server response was invalid
713 netconn_gethostbyname(const char *name
, ip_addr_t
*addr
)
715 struct dns_api_msg msg
;
719 LWIP_ERROR("netconn_gethostbyname: invalid name", (name
!= NULL
), return ERR_ARG
;);
720 LWIP_ERROR("netconn_gethostbyname: invalid addr", (addr
!= NULL
), return ERR_ARG
;);
722 err
= sys_sem_new(&sem
, 0);
732 tcpip_callback(do_gethostbyname
, &msg
);
740 #endif /* LWIP_NETCONN */