2007-08-09 22:21:44 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Sequential API External module
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
/*
|
2004-02-07 00:30:03 +00:00
|
|
|
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
|
2002-10-19 12:59:30 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
* are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
|
|
|
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
|
|
|
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
|
|
|
* OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the lwIP TCP/IP stack.
|
|
|
|
*
|
|
|
|
* Author: Adam Dunkels <adam@sics.se>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* This is the part of the API that is linked with
|
|
|
|
the application */
|
|
|
|
|
2003-02-21 16:43:46 +00:00
|
|
|
#include "lwip/opt.h"
|
2007-09-07 23:01:59 +00:00
|
|
|
|
|
|
|
#if LWIP_NETCONN /* don't build if not configured for use in lwipopts.h */
|
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
#include "lwip/api.h"
|
2007-05-11 08:58:23 +00:00
|
|
|
#include "lwip/tcpip.h"
|
2002-10-19 12:59:30 +00:00
|
|
|
#include "lwip/memp.h"
|
|
|
|
|
2007-11-06 20:31:28 +00:00
|
|
|
#include "lwip/ip.h"
|
|
|
|
#include "lwip/raw.h"
|
|
|
|
#include "lwip/udp.h"
|
|
|
|
#include "lwip/tcp.h"
|
|
|
|
|
2007-09-07 23:01:59 +00:00
|
|
|
#include <string.h>
|
2007-06-16 13:57:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new netconn (of a specific type) that has a callback function.
|
|
|
|
* The corresponding pcb is also created.
|
|
|
|
*
|
|
|
|
* @param t the type of 'connection' to create (@see enum netconn_type)
|
|
|
|
* @param proto the IP protocol for RAW IP pcbs
|
|
|
|
* @param callback a function to call on status changes (RX available, TX'ed)
|
|
|
|
* @return a newly allocated struct netconn or
|
|
|
|
* NULL on memory error
|
|
|
|
*/
|
2007-11-30 12:54:10 +00:00
|
|
|
struct netconn*
|
sys_arch.txt, api.h, api_lib.c, api_msg.h, api_msg.c, tcpip.c, sys.h, opt.h: Introduce changes for task #7490 "Add return value to sys_mbox_post" with some modifications in the sys_mbox api: sys_mbox_new take a "size" parameters which indicate the number of pointers query by the mailbox. There is three defines in opt.h to indicate sizes for tcpip::mbox, netconn::recvmbox, and for the netconn::acceptmbox. Port maintainers, you can decide to just add this new parameter in your implementation, but to ignore it to keep the previous behavior. The new sys_mbox_trypost function return a value to know if the mailbox is full or if the message is posted. Take a look to sys_arch.txt for more details. This new function is used in tcpip_input (so, can be called in an interrupt context since the function is not blocking), and in recv_udp and recv_raw.
2008-01-05 21:10:32 +00:00
|
|
|
netconn_new_with_proto_and_callback(enum netconn_type t, u8_t proto, netconn_callback callback)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
|
|
|
struct netconn *conn;
|
2007-03-04 14:49:46 +00:00
|
|
|
struct api_msg msg;
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2008-01-04 23:07:44 +00:00
|
|
|
conn = netconn_alloc(t, callback);
|
2010-01-17 18:28:56 +00:00
|
|
|
if (conn != NULL) {
|
2007-11-30 12:54:10 +00:00
|
|
|
msg.function = do_newconn;
|
|
|
|
msg.msg.msg.n.proto = proto;
|
|
|
|
msg.msg.conn = conn;
|
2010-01-17 18:28:56 +00:00
|
|
|
if (TCPIP_APIMSG(&msg) != ERR_OK) {
|
2007-11-30 12:54:10 +00:00
|
|
|
LWIP_ASSERT("freeing conn without freeing pcb", conn->pcb.tcp == NULL);
|
2008-01-12 11:52:21 +00:00
|
|
|
LWIP_ASSERT("conn has no op_completed", conn->op_completed != SYS_SEM_NULL);
|
2007-12-02 14:53:50 +00:00
|
|
|
LWIP_ASSERT("conn has no recvmbox", conn->recvmbox != SYS_MBOX_NULL);
|
2007-12-16 16:15:08 +00:00
|
|
|
LWIP_ASSERT("conn->acceptmbox shouldn't exist", conn->acceptmbox == SYS_MBOX_NULL);
|
2008-01-12 11:52:21 +00:00
|
|
|
sys_sem_free(conn->op_completed);
|
2007-11-30 12:54:10 +00:00
|
|
|
sys_mbox_free(conn->recvmbox);
|
|
|
|
memp_free(MEMP_NETCONN, conn);
|
|
|
|
return NULL;
|
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
return conn;
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-06-16 13:57:30 +00:00
|
|
|
/**
|
|
|
|
* Close a netconn 'connection' and free its resources.
|
|
|
|
* UDP and RAW connection are completely closed, TCP pcbs might still be in a waitstate
|
|
|
|
* after this returns.
|
|
|
|
*
|
|
|
|
* @param conn the netconn to delete
|
|
|
|
* @return ERR_OK if the connection was deleted
|
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
err_t
|
|
|
|
netconn_delete(struct netconn *conn)
|
|
|
|
{
|
2007-03-04 14:49:46 +00:00
|
|
|
struct api_msg msg;
|
2007-05-23 18:43:30 +00:00
|
|
|
|
|
|
|
/* No ASSERT here because possible to get a (conn == NULL) if we got an accept error */
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn == NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
2007-05-23 18:43:30 +00:00
|
|
|
|
2007-05-11 08:58:23 +00:00
|
|
|
msg.function = do_delconn;
|
2007-03-04 14:49:46 +00:00
|
|
|
msg.msg.conn = conn;
|
2007-07-03 19:29:59 +00:00
|
|
|
tcpip_apimsg(&msg);
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2008-01-06 14:28:04 +00:00
|
|
|
conn->pcb.tcp = NULL;
|
|
|
|
netconn_free(conn);
|
2007-03-21 16:38:58 +00:00
|
|
|
|
2010-01-17 18:28:56 +00:00
|
|
|
/* don't care for return value of do_delconn since it only calls void functions */
|
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-06-16 13:57:30 +00:00
|
|
|
/**
|
2007-11-12 22:39:24 +00:00
|
|
|
* Get the local or remote IP address and port of a netconn.
|
2007-06-16 13:57:30 +00:00
|
|
|
* For RAW netconns, this returns the protocol instead of a port!
|
|
|
|
*
|
|
|
|
* @param conn the netconn to query
|
2007-11-12 22:39:24 +00:00
|
|
|
* @param addr a pointer to which to save the IP address
|
|
|
|
* @param port a pointer to which to save the port (or protocol for RAW)
|
2007-11-24 22:13:25 +00:00
|
|
|
* @param local 1 to get the local IP address, 0 to get the remote one
|
2007-10-07 17:26:54 +00:00
|
|
|
* @return ERR_CONN for invalid connections
|
|
|
|
* ERR_OK if the information was retrieved
|
2007-06-16 13:57:30 +00:00
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
err_t
|
2007-11-12 22:39:24 +00:00
|
|
|
netconn_getaddr(struct netconn *conn, struct ip_addr *addr, u16_t *port, u8_t local)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
2007-11-12 22:39:24 +00:00
|
|
|
struct api_msg msg;
|
2010-01-17 18:28:56 +00:00
|
|
|
err_t err;
|
2007-11-12 22:39:24 +00:00
|
|
|
|
|
|
|
LWIP_ERROR("netconn_getaddr: invalid conn", (conn != NULL), return ERR_ARG;);
|
|
|
|
LWIP_ERROR("netconn_getaddr: invalid addr", (addr != NULL), return ERR_ARG;);
|
|
|
|
LWIP_ERROR("netconn_getaddr: invalid port", (port != NULL), return ERR_ARG;);
|
|
|
|
|
|
|
|
msg.function = do_getaddr;
|
|
|
|
msg.msg.conn = conn;
|
|
|
|
msg.msg.msg.ad.ipaddr = addr;
|
|
|
|
msg.msg.msg.ad.port = port;
|
|
|
|
msg.msg.msg.ad.local = local;
|
2010-01-17 18:28:56 +00:00
|
|
|
err = TCPIP_APIMSG(&msg);
|
2007-11-12 22:39:24 +00:00
|
|
|
|
2010-01-17 18:28:56 +00:00
|
|
|
NETCONN_SET_SAFE_ERR(conn, err);
|
|
|
|
return err;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-06-16 13:57:30 +00:00
|
|
|
/**
|
|
|
|
* Bind a netconn to a specific local IP address and port.
|
|
|
|
* Binding one netconn twice might not always be checked correctly!
|
|
|
|
*
|
|
|
|
* @param conn the netconn to bind
|
|
|
|
* @param addr the local IP address to bind the netconn to (use IP_ADDR_ANY
|
|
|
|
* to bind to all addresses)
|
|
|
|
* @param port the local port to bind the netconn to (not used for RAW)
|
|
|
|
* @return ERR_OK if bound, any other err_t on failure
|
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
err_t
|
2007-03-28 17:26:06 +00:00
|
|
|
netconn_bind(struct netconn *conn, struct ip_addr *addr, u16_t port)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
2007-03-04 14:49:46 +00:00
|
|
|
struct api_msg msg;
|
2010-01-17 18:28:56 +00:00
|
|
|
err_t err;
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2007-06-22 20:50:21 +00:00
|
|
|
LWIP_ERROR("netconn_bind: invalid conn", (conn != NULL), return ERR_ARG;);
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2007-05-11 08:58:23 +00:00
|
|
|
msg.function = do_bind;
|
2007-03-04 14:49:46 +00:00
|
|
|
msg.msg.conn = conn;
|
|
|
|
msg.msg.msg.bc.ipaddr = addr;
|
|
|
|
msg.msg.msg.bc.port = port;
|
2010-01-17 18:28:56 +00:00
|
|
|
err = TCPIP_APIMSG(&msg);
|
|
|
|
|
|
|
|
NETCONN_SET_SAFE_ERR(conn, err);
|
|
|
|
return err;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-01-22 16:18:05 +00:00
|
|
|
|
2007-06-16 13:57:30 +00:00
|
|
|
/**
|
|
|
|
* Connect a netconn to a specific remote IP address and port.
|
|
|
|
*
|
|
|
|
* @param conn the netconn to connect
|
|
|
|
* @param addr the remote IP address to connect to
|
|
|
|
* @param port the remote port to connect to (no used for RAW)
|
|
|
|
* @return ERR_OK if connected, return value of tcp_/udp_/raw_connect otherwise
|
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
err_t
|
2007-03-28 17:26:06 +00:00
|
|
|
netconn_connect(struct netconn *conn, struct ip_addr *addr, u16_t port)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
2007-03-04 14:49:46 +00:00
|
|
|
struct api_msg msg;
|
2010-01-17 18:28:56 +00:00
|
|
|
err_t err;
|
2007-06-29 13:37:33 +00:00
|
|
|
|
2007-06-22 20:50:21 +00:00
|
|
|
LWIP_ERROR("netconn_connect: invalid conn", (conn != NULL), return ERR_ARG;);
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2007-05-11 08:58:23 +00:00
|
|
|
msg.function = do_connect;
|
2007-05-23 17:46:53 +00:00
|
|
|
msg.msg.conn = conn;
|
2007-03-04 14:49:46 +00:00
|
|
|
msg.msg.msg.bc.ipaddr = addr;
|
|
|
|
msg.msg.msg.bc.port = port;
|
2007-06-08 19:27:59 +00:00
|
|
|
/* This is the only function which need to not block tcpip_thread */
|
2010-01-17 18:28:56 +00:00
|
|
|
err = tcpip_apimsg(&msg);
|
|
|
|
|
|
|
|
NETCONN_SET_SAFE_ERR(conn, err);
|
|
|
|
return err;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-01-22 16:18:05 +00:00
|
|
|
|
2007-06-16 13:57:30 +00:00
|
|
|
/**
|
|
|
|
* Disconnect a netconn from its current peer (only valid for UDP netconns).
|
|
|
|
*
|
|
|
|
* @param conn the netconn to disconnect
|
|
|
|
* @return TODO: return value is not set here...
|
|
|
|
*/
|
2003-01-22 16:18:05 +00:00
|
|
|
err_t
|
|
|
|
netconn_disconnect(struct netconn *conn)
|
|
|
|
{
|
2007-03-04 14:49:46 +00:00
|
|
|
struct api_msg msg;
|
2010-01-17 18:28:56 +00:00
|
|
|
err_t err;
|
2007-06-29 13:37:33 +00:00
|
|
|
|
2007-06-22 20:50:21 +00:00
|
|
|
LWIP_ERROR("netconn_disconnect: invalid conn", (conn != NULL), return ERR_ARG;);
|
2003-01-22 16:18:05 +00:00
|
|
|
|
2007-05-11 08:58:23 +00:00
|
|
|
msg.function = do_disconnect;
|
2007-05-23 17:46:53 +00:00
|
|
|
msg.msg.conn = conn;
|
2010-01-17 18:28:56 +00:00
|
|
|
err = TCPIP_APIMSG(&msg);
|
|
|
|
|
|
|
|
NETCONN_SET_SAFE_ERR(conn, err);
|
|
|
|
return err;
|
2003-01-22 16:18:05 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-06-16 13:57:30 +00:00
|
|
|
/**
|
|
|
|
* Set a TCP netconn into listen mode
|
|
|
|
*
|
|
|
|
* @param conn the tcp netconn to set to listen mode
|
rawapi.txt, api.h, api_lib.c, api_msg.h, api_msg.c, sockets.c, tcp.h, tcp.c, tcp_in.c, init.c, opt.h: rename backlog options with TCP_ prefix, limit the "backlog" parameter in an u8_t, 0 is interpreted as "smallest queue", add documentation in the rawapi.txt file.
2008-01-04 22:18:27 +00:00
|
|
|
* @param backlog the listen backlog, only used if TCP_LISTEN_BACKLOG==1
|
2007-06-16 13:57:30 +00:00
|
|
|
* @return ERR_OK if the netconn was set to listen (UDP and RAW netconns
|
|
|
|
* don't return any error (yet?))
|
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
err_t
|
2007-12-21 16:47:56 +00:00
|
|
|
netconn_listen_with_backlog(struct netconn *conn, u8_t backlog)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
2007-03-04 14:49:46 +00:00
|
|
|
struct api_msg msg;
|
2010-01-17 18:28:56 +00:00
|
|
|
err_t err;
|
2002-10-19 12:59:30 +00:00
|
|
|
|
rawapi.txt, api.h, api_lib.c, api_msg.h, api_msg.c, sockets.c, tcp.h, tcp.c, tcp_in.c, init.c, opt.h: rename backlog options with TCP_ prefix, limit the "backlog" parameter in an u8_t, 0 is interpreted as "smallest queue", add documentation in the rawapi.txt file.
2008-01-04 22:18:27 +00:00
|
|
|
/* This does no harm. If TCP_LISTEN_BACKLOG is off, backlog is unused. */
|
2007-12-21 16:47:56 +00:00
|
|
|
LWIP_UNUSED_ARG(backlog);
|
|
|
|
|
2007-06-22 20:50:21 +00:00
|
|
|
LWIP_ERROR("netconn_listen: invalid conn", (conn != NULL), return ERR_ARG;);
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2007-05-11 08:58:23 +00:00
|
|
|
msg.function = do_listen;
|
2007-03-04 14:49:46 +00:00
|
|
|
msg.msg.conn = conn;
|
rawapi.txt, api.h, api_lib.c, api_msg.h, api_msg.c, sockets.c, tcp.h, tcp.c, tcp_in.c, init.c, opt.h: rename backlog options with TCP_ prefix, limit the "backlog" parameter in an u8_t, 0 is interpreted as "smallest queue", add documentation in the rawapi.txt file.
2008-01-04 22:18:27 +00:00
|
|
|
#if TCP_LISTEN_BACKLOG
|
|
|
|
msg.msg.msg.lb.backlog = backlog;
|
|
|
|
#endif /* TCP_LISTEN_BACKLOG */
|
2010-01-17 18:28:56 +00:00
|
|
|
err = TCPIP_APIMSG(&msg);
|
|
|
|
|
|
|
|
NETCONN_SET_SAFE_ERR(conn, err);
|
|
|
|
return err;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-06-16 13:57:30 +00:00
|
|
|
/**
|
|
|
|
* Accept a new connection on a TCP listening netconn.
|
|
|
|
*
|
|
|
|
* @param conn the TCP listen netconn
|
2010-01-17 16:21:07 +00:00
|
|
|
* @param new_conn pointer where the new connection is stored
|
|
|
|
* @return ERR_OK if a new connection has been received or an error
|
|
|
|
* code otherwise
|
2007-06-16 13:57:30 +00:00
|
|
|
*/
|
2010-01-17 16:21:07 +00:00
|
|
|
err_t
|
|
|
|
netconn_accept(struct netconn *conn, struct netconn **new_conn)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
|
|
|
struct netconn *newconn;
|
2010-01-17 18:28:56 +00:00
|
|
|
err_t err;
|
2010-01-17 16:21:07 +00:00
|
|
|
#if TCP_LISTEN_BACKLOG
|
|
|
|
struct api_msg msg;
|
|
|
|
#endif /* TCP_LISTEN_BACKLOG */
|
2007-05-23 17:46:53 +00:00
|
|
|
|
2010-01-17 18:28:56 +00:00
|
|
|
LWIP_ERROR("netconn_accept: invalid pointer", (new_conn != NULL), return ERR_ARG;);
|
|
|
|
*new_conn = NULL;
|
2010-01-17 16:21:07 +00:00
|
|
|
LWIP_ERROR("netconn_accept: invalid conn", (conn != NULL), return ERR_ARG;);
|
|
|
|
LWIP_ERROR("netconn_accept: invalid acceptmbox", (conn->acceptmbox != SYS_MBOX_NULL), return ERR_ARG;);
|
2007-05-23 17:46:53 +00:00
|
|
|
|
2010-01-17 18:28:56 +00:00
|
|
|
err = conn->last_err;
|
|
|
|
if (ERR_IS_FATAL(err)) {
|
|
|
|
/* don't recv on fatal errors: this might block the application task
|
|
|
|
waiting on acceptmbox forever! */
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2007-10-09 19:30:17 +00:00
|
|
|
#if LWIP_SO_RCVTIMEO
|
2007-12-21 16:47:56 +00:00
|
|
|
if (sys_arch_mbox_fetch(conn->acceptmbox, (void *)&newconn, conn->recv_timeout) == SYS_ARCH_TIMEOUT) {
|
2010-01-17 18:28:56 +00:00
|
|
|
NETCONN_SET_SAFE_ERR(conn, ERR_TIMEOUT);
|
2010-01-17 16:21:07 +00:00
|
|
|
return ERR_TIMEOUT;
|
|
|
|
}
|
2007-10-09 19:30:17 +00:00
|
|
|
#else
|
2007-05-22 20:51:34 +00:00
|
|
|
sys_arch_mbox_fetch(conn->acceptmbox, (void *)&newconn, 0);
|
2007-10-09 19:30:17 +00:00
|
|
|
#endif /* LWIP_SO_RCVTIMEO*/
|
2010-01-17 16:21:07 +00:00
|
|
|
/* Register event with callback */
|
|
|
|
API_EVENT(conn, NETCONN_EVT_RCVMINUS, 0);
|
2007-12-21 16:47:56 +00:00
|
|
|
|
2010-01-17 16:21:07 +00:00
|
|
|
if (newconn == NULL) {
|
|
|
|
/* connection has been closed */
|
2010-01-17 18:28:56 +00:00
|
|
|
NETCONN_SET_SAFE_ERR(conn, ERR_CLSD);
|
2010-01-17 16:21:07 +00:00
|
|
|
return ERR_CLSD;
|
|
|
|
}
|
rawapi.txt, api.h, api_lib.c, api_msg.h, api_msg.c, sockets.c, tcp.h, tcp.c, tcp_in.c, init.c, opt.h: rename backlog options with TCP_ prefix, limit the "backlog" parameter in an u8_t, 0 is interpreted as "smallest queue", add documentation in the rawapi.txt file.
2008-01-04 22:18:27 +00:00
|
|
|
#if TCP_LISTEN_BACKLOG
|
2010-01-17 16:21:07 +00:00
|
|
|
/* Let the stack know that we have accepted the connection. */
|
|
|
|
msg.function = do_recv;
|
|
|
|
msg.msg.conn = conn;
|
2010-01-17 18:28:56 +00:00
|
|
|
/* don't care for the return value of do_recv */
|
2010-01-17 16:21:07 +00:00
|
|
|
TCPIP_APIMSG(&msg);
|
rawapi.txt, api.h, api_lib.c, api_msg.h, api_msg.c, sockets.c, tcp.h, tcp.c, tcp_in.c, init.c, opt.h: rename backlog options with TCP_ prefix, limit the "backlog" parameter in an u8_t, 0 is interpreted as "smallest queue", add documentation in the rawapi.txt file.
2008-01-04 22:18:27 +00:00
|
|
|
#endif /* TCP_LISTEN_BACKLOG */
|
2007-06-29 13:37:33 +00:00
|
|
|
|
2010-01-17 16:21:07 +00:00
|
|
|
*new_conn = newconn;
|
2010-01-17 18:28:56 +00:00
|
|
|
/* don't set conn->last_err: it's only ERR_OK, anyway */
|
2010-01-17 16:21:07 +00:00
|
|
|
return ERR_OK;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-06-16 13:57:30 +00:00
|
|
|
/**
|
|
|
|
* Receive data (in form of a netbuf containing a packet buffer) from a netconn
|
|
|
|
*
|
|
|
|
* @param conn the netconn from which to receive data
|
2010-01-17 16:21:07 +00:00
|
|
|
* @param new_buf pointer where a new netbuf is stored when received data
|
|
|
|
* @return ERR_OK if data has been received, an error code otherwise (timeout,
|
|
|
|
* memory error or another error)
|
2007-06-16 13:57:30 +00:00
|
|
|
*/
|
2010-01-17 16:21:07 +00:00
|
|
|
err_t
|
|
|
|
netconn_recv(struct netconn *conn, struct netbuf **new_buf)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
2007-03-04 14:49:46 +00:00
|
|
|
struct api_msg msg;
|
|
|
|
struct netbuf *buf = NULL;
|
2002-10-19 12:59:30 +00:00
|
|
|
struct pbuf *p;
|
2003-02-11 21:00:14 +00:00
|
|
|
u16_t len;
|
2010-01-17 18:28:56 +00:00
|
|
|
err_t err;
|
2007-06-29 13:37:33 +00:00
|
|
|
|
2010-01-17 16:21:07 +00:00
|
|
|
LWIP_ERROR("netconn_recv: invalid pointer", (new_buf != NULL), return ERR_ARG;);
|
|
|
|
*new_buf = NULL;
|
2010-01-17 18:28:56 +00:00
|
|
|
LWIP_ERROR("netconn_recv: invalid conn", (conn != NULL), return ERR_ARG;);
|
|
|
|
LWIP_ERROR("netconn_accept: invalid recvmbox", (conn->recvmbox != SYS_MBOX_NULL), return ERR_CONN;);
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2010-01-17 18:28:56 +00:00
|
|
|
err = conn->last_err;
|
|
|
|
if (ERR_IS_FATAL(err)) {
|
|
|
|
/* don't recv on fatal errors: this might block the application task
|
|
|
|
waiting on recvmbox forever! */
|
|
|
|
return err;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn->type == NETCONN_TCP) {
|
2007-05-19 13:54:56 +00:00
|
|
|
#if LWIP_TCP
|
2010-01-17 16:21:07 +00:00
|
|
|
/* This is not a listening netconn, since recvmbox is set */
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2003-06-27 20:46:11 +00:00
|
|
|
buf = memp_malloc(MEMP_NETBUF);
|
2003-05-01 13:24:01 +00:00
|
|
|
if (buf == NULL) {
|
2010-01-17 18:28:56 +00:00
|
|
|
NETCONN_SET_SAFE_ERR(conn, ERR_MEM);
|
2010-01-17 16:21:07 +00:00
|
|
|
return ERR_MEM;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2007-06-29 13:37:33 +00:00
|
|
|
|
2007-05-23 19:18:09 +00:00
|
|
|
#if LWIP_SO_RCVTIMEO
|
|
|
|
if (sys_arch_mbox_fetch(conn->recvmbox, (void *)&p, conn->recv_timeout)==SYS_ARCH_TIMEOUT) {
|
2009-10-15 14:33:18 +00:00
|
|
|
memp_free(MEMP_NETBUF, buf);
|
2010-01-17 18:28:56 +00:00
|
|
|
NETCONN_SET_SAFE_ERR(conn, ERR_TIMEOUT);
|
2010-01-17 16:21:07 +00:00
|
|
|
return ERR_TIMEOUT;
|
2007-05-23 19:18:09 +00:00
|
|
|
}
|
|
|
|
#else
|
2007-05-22 20:51:34 +00:00
|
|
|
sys_arch_mbox_fetch(conn->recvmbox, (void *)&p, 0);
|
2007-05-23 19:18:09 +00:00
|
|
|
#endif /* LWIP_SO_RCVTIMEO*/
|
2003-02-11 16:33:02 +00:00
|
|
|
|
2007-05-22 21:29:04 +00:00
|
|
|
if (p != NULL) {
|
|
|
|
len = p->tot_len;
|
2007-12-21 14:59:10 +00:00
|
|
|
SYS_ARCH_DEC(conn->recv_avail, len);
|
2007-05-22 21:29:04 +00:00
|
|
|
} else {
|
2010-01-17 16:21:07 +00:00
|
|
|
/* This means the connection has been closed */
|
2007-05-22 21:29:04 +00:00
|
|
|
len = 0;
|
2003-02-11 16:33:02 +00:00
|
|
|
}
|
2007-06-29 13:37:33 +00:00
|
|
|
|
2003-02-11 21:00:14 +00:00
|
|
|
/* Register event with callback */
|
2007-10-24 12:09:18 +00:00
|
|
|
API_EVENT(conn, NETCONN_EVT_RCVMINUS, len);
|
2003-02-06 22:18:56 +00:00
|
|
|
|
2007-05-23 17:53:35 +00:00
|
|
|
/* If we are closed, we indicate that we no longer wish to use the socket */
|
2003-05-01 13:24:01 +00:00
|
|
|
if (p == NULL) {
|
2003-06-27 20:46:11 +00:00
|
|
|
memp_free(MEMP_NETBUF, buf);
|
2007-10-24 12:09:18 +00:00
|
|
|
/* Avoid to lose any previous error code */
|
2010-01-17 18:28:56 +00:00
|
|
|
NETCONN_SET_SAFE_ERR(conn, ERR_CLSD);
|
2010-01-17 16:21:07 +00:00
|
|
|
return ERR_CLSD;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buf->p = p;
|
|
|
|
buf->ptr = p;
|
2007-05-04 15:18:29 +00:00
|
|
|
buf->port = 0;
|
|
|
|
buf->addr = NULL;
|
2002-10-19 12:59:30 +00:00
|
|
|
|
|
|
|
/* Let the stack know that we have taken the data. */
|
2010-01-17 16:21:07 +00:00
|
|
|
/* TODO: Speedup: Don't block and wait for the answer here
|
|
|
|
(to prevent multiple thread-switches). */
|
2007-05-11 08:58:23 +00:00
|
|
|
msg.function = do_recv;
|
2007-03-04 14:49:46 +00:00
|
|
|
msg.msg.conn = conn;
|
2003-05-01 13:24:01 +00:00
|
|
|
if (buf != NULL) {
|
2007-05-22 09:54:00 +00:00
|
|
|
msg.msg.msg.r.len = buf->p->tot_len;
|
2002-10-19 12:59:30 +00:00
|
|
|
} else {
|
2007-05-22 09:54:00 +00:00
|
|
|
msg.msg.msg.r.len = 1;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2010-01-17 18:28:56 +00:00
|
|
|
/* don't care for the return value of do_recv */
|
2007-06-08 19:27:59 +00:00
|
|
|
TCPIP_APIMSG(&msg);
|
2007-05-19 13:54:56 +00:00
|
|
|
#endif /* LWIP_TCP */
|
2002-10-19 12:59:30 +00:00
|
|
|
} else {
|
2007-05-19 13:54:56 +00:00
|
|
|
#if (LWIP_UDP || LWIP_RAW)
|
2007-03-08 20:58:46 +00:00
|
|
|
#if LWIP_SO_RCVTIMEO
|
2007-05-22 21:29:04 +00:00
|
|
|
if (sys_arch_mbox_fetch(conn->recvmbox, (void *)&buf, conn->recv_timeout)==SYS_ARCH_TIMEOUT) {
|
2010-01-17 18:28:56 +00:00
|
|
|
NETCONN_SET_SAFE_ERR(conn, ERR_TIMEOUT);
|
2010-01-17 16:21:07 +00:00
|
|
|
return ERR_TIMEOUT;
|
2007-05-22 21:29:04 +00:00
|
|
|
}
|
2007-03-08 20:58:46 +00:00
|
|
|
#else
|
2007-05-22 20:51:34 +00:00
|
|
|
sys_arch_mbox_fetch(conn->recvmbox, (void *)&buf, 0);
|
2007-03-08 20:58:46 +00:00
|
|
|
#endif /* LWIP_SO_RCVTIMEO*/
|
2010-01-17 16:21:07 +00:00
|
|
|
LWIP_ASSERT("buf != NULL", buf != NULL);
|
|
|
|
|
|
|
|
SYS_ARCH_DEC(conn->recv_avail, buf->p->tot_len);
|
|
|
|
/* Register event with callback */
|
|
|
|
API_EVENT(conn, NETCONN_EVT_RCVMINUS, buf->p->tot_len);
|
2007-05-19 13:54:56 +00:00
|
|
|
#endif /* (LWIP_UDP || LWIP_RAW) */
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2010-01-17 16:21:07 +00:00
|
|
|
LWIP_ASSERT("buf != NULL", buf != NULL);
|
2007-06-13 18:00:54 +00:00
|
|
|
|
2010-01-17 16:21:07 +00:00
|
|
|
LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_recv: received %p\n", (void *)buf));
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2010-01-17 16:21:07 +00:00
|
|
|
*new_buf = buf;
|
2010-01-17 18:28:56 +00:00
|
|
|
/* don't set conn->last_err: it's only ERR_OK, anyway */
|
2010-01-17 16:21:07 +00:00
|
|
|
return ERR_OK;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-06-16 13:57:30 +00:00
|
|
|
/**
|
|
|
|
* Send data (in form of a netbuf) to a specific remote IP address and port.
|
|
|
|
* Only to be used for UDP and RAW netconns (not TCP).
|
|
|
|
*
|
|
|
|
* @param conn the netconn over which to send data
|
|
|
|
* @param buf a netbuf containing the data to send
|
|
|
|
* @param addr the remote IP address to which to send the data
|
2007-08-09 22:21:44 +00:00
|
|
|
* @param port the remote port to which to send the data
|
2007-06-16 13:57:30 +00:00
|
|
|
* @return ERR_OK if data was sent, any other err_t on error
|
|
|
|
*/
|
2007-05-04 15:18:29 +00:00
|
|
|
err_t
|
|
|
|
netconn_sendto(struct netconn *conn, struct netbuf *buf, struct ip_addr *addr, u16_t port)
|
2007-06-16 13:57:30 +00:00
|
|
|
{
|
|
|
|
if (buf != NULL) {
|
2007-05-04 15:18:29 +00:00
|
|
|
buf->addr = addr;
|
|
|
|
buf->port = port;
|
2007-06-16 13:57:30 +00:00
|
|
|
return netconn_send(conn, buf);
|
2007-05-04 15:18:29 +00:00
|
|
|
}
|
|
|
|
return ERR_VAL;
|
2007-06-29 13:37:33 +00:00
|
|
|
}
|
2007-05-04 15:18:29 +00:00
|
|
|
|
2007-06-16 13:57:30 +00:00
|
|
|
/**
|
|
|
|
* Send data over a UDP or RAW netconn (that is already connected).
|
|
|
|
*
|
|
|
|
* @param conn the UDP or RAW netconn over which to send data
|
|
|
|
* @param buf a netbuf containing the data to send
|
|
|
|
* @return ERR_OK if data was sent, any other err_t on error
|
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
err_t
|
|
|
|
netconn_send(struct netconn *conn, struct netbuf *buf)
|
|
|
|
{
|
2007-03-04 14:49:46 +00:00
|
|
|
struct api_msg msg;
|
2010-01-17 18:28:56 +00:00
|
|
|
err_t err;
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2007-06-22 20:50:21 +00:00
|
|
|
LWIP_ERROR("netconn_send: invalid conn", (conn != NULL), return ERR_ARG;);
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2009-02-18 21:13:06 +00:00
|
|
|
LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_send: sending %"U16_F" bytes\n", buf->p->tot_len));
|
2007-05-11 08:58:23 +00:00
|
|
|
msg.function = do_send;
|
2007-03-04 14:49:46 +00:00
|
|
|
msg.msg.conn = conn;
|
2007-05-04 15:18:29 +00:00
|
|
|
msg.msg.msg.b = buf;
|
2010-01-17 18:28:56 +00:00
|
|
|
err = TCPIP_APIMSG(&msg);
|
|
|
|
|
|
|
|
NETCONN_SET_SAFE_ERR(conn, err);
|
|
|
|
return err;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-06-16 13:57:30 +00:00
|
|
|
/**
|
|
|
|
* Send data over a TCP netconn.
|
|
|
|
*
|
|
|
|
* @param conn the TCP netconn over which to send data
|
|
|
|
* @param dataptr pointer to the application buffer that contains the data to send
|
|
|
|
* @param size size of the application data to send
|
2007-11-01 17:37:50 +00:00
|
|
|
* @param apiflags combination of following flags :
|
|
|
|
* - NETCONN_COPY (0x01) data will be copied into memory belonging to the stack
|
|
|
|
* - NETCONN_MORE (0x02) for TCP connection, PSH flag will be set on last segment sent
|
2007-06-16 13:57:30 +00:00
|
|
|
* @return ERR_OK if data was sent, any other err_t on error
|
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
err_t
|
2009-02-16 19:33:51 +00:00
|
|
|
netconn_write(struct netconn *conn, const void *dataptr, size_t size, u8_t apiflags)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
2007-03-04 14:49:46 +00:00
|
|
|
struct api_msg msg;
|
2010-01-17 18:28:56 +00:00
|
|
|
err_t err;
|
2007-06-21 18:40:21 +00:00
|
|
|
|
2007-06-22 20:50:21 +00:00
|
|
|
LWIP_ERROR("netconn_write: invalid conn", (conn != NULL), return ERR_ARG;);
|
|
|
|
LWIP_ERROR("netconn_write: invalid conn->type", (conn->type == NETCONN_TCP), return ERR_VAL;);
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2007-05-11 08:58:23 +00:00
|
|
|
msg.function = do_write;
|
2007-03-04 14:49:46 +00:00
|
|
|
msg.msg.conn = conn;
|
2007-06-21 18:40:21 +00:00
|
|
|
msg.msg.msg.w.dataptr = dataptr;
|
2007-11-01 17:37:50 +00:00
|
|
|
msg.msg.msg.w.apiflags = apiflags;
|
2007-06-21 18:40:21 +00:00
|
|
|
msg.msg.msg.w.len = size;
|
|
|
|
/* For locking the core: this _can_ be delayed on low memory/low send buffer,
|
|
|
|
but if it is, this is done inside api_msg.c:do_write(), so we can use the
|
|
|
|
non-blocking version here. */
|
2010-01-17 18:28:56 +00:00
|
|
|
err = TCPIP_APIMSG(&msg);
|
|
|
|
|
|
|
|
NETCONN_SET_SAFE_ERR(conn, err);
|
|
|
|
return err;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-06-16 13:57:30 +00:00
|
|
|
/**
|
|
|
|
* Close a TCP netconn (doesn't delete it).
|
|
|
|
*
|
|
|
|
* @param conn the TCP netconn to close
|
|
|
|
* @return ERR_OK if the netconn was closed, any other err_t on error
|
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
err_t
|
|
|
|
netconn_close(struct netconn *conn)
|
|
|
|
{
|
2007-03-04 14:49:46 +00:00
|
|
|
struct api_msg msg;
|
2010-01-17 18:28:56 +00:00
|
|
|
err_t err;
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2007-06-22 20:50:21 +00:00
|
|
|
LWIP_ERROR("netconn_close: invalid conn", (conn != NULL), return ERR_ARG;);
|
2007-06-13 18:00:54 +00:00
|
|
|
|
2007-05-11 08:58:23 +00:00
|
|
|
msg.function = do_close;
|
2007-03-04 14:49:46 +00:00
|
|
|
msg.msg.conn = conn;
|
2010-01-17 18:28:56 +00:00
|
|
|
/* because of the LWIP_TCPIP_CORE_LOCKING implementation of do_close,
|
|
|
|
don't use TCPIP_APIMSG here */
|
|
|
|
err = tcpip_apimsg(&msg);
|
|
|
|
|
|
|
|
NETCONN_SET_SAFE_ERR(conn, err);
|
|
|
|
return err;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-03-11 19:16:38 +00:00
|
|
|
#if LWIP_IGMP
|
2007-06-16 13:57:30 +00:00
|
|
|
/**
|
|
|
|
* Join multicast groups for UDP netconns.
|
|
|
|
*
|
|
|
|
* @param conn the UDP netconn for which to change multicast addresses
|
|
|
|
* @param multiaddr IP address of the multicast group to join or leave
|
|
|
|
* @param interface the IP address of the network interface on which to send
|
|
|
|
* the igmp message
|
|
|
|
* @param join_or_leave flag whether to send a join- or leave-message
|
|
|
|
* @return ERR_OK if the action was taken, any err_t on error
|
|
|
|
*/
|
2007-03-11 19:16:38 +00:00
|
|
|
err_t
|
2007-06-16 13:57:30 +00:00
|
|
|
netconn_join_leave_group(struct netconn *conn,
|
|
|
|
struct ip_addr *multiaddr,
|
|
|
|
struct ip_addr *interface,
|
|
|
|
enum netconn_igmp join_or_leave)
|
2007-03-11 19:16:38 +00:00
|
|
|
{
|
|
|
|
struct api_msg msg;
|
2010-01-17 18:28:56 +00:00
|
|
|
err_t err;
|
2007-03-11 19:16:38 +00:00
|
|
|
|
2007-06-22 20:50:21 +00:00
|
|
|
LWIP_ERROR("netconn_join_leave_group: invalid conn", (conn != NULL), return ERR_ARG;);
|
2007-03-11 19:16:38 +00:00
|
|
|
|
2007-05-11 08:58:23 +00:00
|
|
|
msg.function = do_join_leave_group;
|
2007-03-21 16:38:58 +00:00
|
|
|
msg.msg.conn = conn;
|
2007-05-22 09:54:00 +00:00
|
|
|
msg.msg.msg.jl.multiaddr = multiaddr;
|
|
|
|
msg.msg.msg.jl.interface = interface;
|
|
|
|
msg.msg.msg.jl.join_or_leave = join_or_leave;
|
2010-01-17 18:28:56 +00:00
|
|
|
err = TCPIP_APIMSG(&msg);
|
|
|
|
|
|
|
|
NETCONN_SET_SAFE_ERR(conn, err);
|
|
|
|
return err;
|
2007-03-11 19:16:38 +00:00
|
|
|
}
|
|
|
|
#endif /* LWIP_IGMP */
|
|
|
|
|
2007-11-16 17:16:17 +00:00
|
|
|
#if LWIP_DNS
|
|
|
|
/**
|
|
|
|
* Execute a DNS query, only one IP address is returned
|
|
|
|
*
|
|
|
|
* @param name a string representation of the DNS host name to query
|
|
|
|
* @param addr a preallocated struct ip_addr where to store the resolved IP address
|
2007-11-20 21:23:24 +00:00
|
|
|
* @return ERR_OK: resolving succeeded
|
|
|
|
* ERR_MEM: memory error, try again later
|
|
|
|
* ERR_ARG: dns client not initialized or invalid hostname
|
|
|
|
* ERR_VAL: dns server response was invalid
|
2007-11-16 17:16:17 +00:00
|
|
|
*/
|
|
|
|
err_t
|
|
|
|
netconn_gethostbyname(const char *name, struct ip_addr *addr)
|
|
|
|
{
|
|
|
|
struct dns_api_msg msg;
|
|
|
|
err_t err;
|
|
|
|
sys_sem_t sem;
|
|
|
|
|
|
|
|
LWIP_ERROR("netconn_gethostbyname: invalid name", (name != NULL), return ERR_ARG;);
|
|
|
|
LWIP_ERROR("netconn_gethostbyname: invalid addr", (addr != NULL), return ERR_ARG;);
|
|
|
|
|
|
|
|
sem = sys_sem_new(0);
|
|
|
|
if (sem == SYS_SEM_NULL) {
|
|
|
|
return ERR_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.name = name;
|
|
|
|
msg.addr = addr;
|
|
|
|
msg.err = &err;
|
|
|
|
msg.sem = sem;
|
|
|
|
|
|
|
|
tcpip_callback(do_gethostbyname, &msg);
|
|
|
|
sys_sem_wait(sem);
|
|
|
|
sys_sem_free(sem);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
#endif /* LWIP_DNS*/
|
|
|
|
|
2007-09-07 23:01:59 +00:00
|
|
|
#endif /* LWIP_NETCONN */
|