2007-08-09 22:21:44 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Sequential API External module
|
2016-08-07 10:05:34 +02:00
|
|
|
*
|
|
|
|
* @defgroup netconn Netconn API
|
2016-09-29 12:51:45 +02:00
|
|
|
* @ingroup sequential_api
|
2016-08-07 10:05:34 +02:00
|
|
|
* Thread-safe, to be called from non-TCPIP threads only.
|
|
|
|
* TX/RX handling based on @ref netbuf (containing @ref pbuf)
|
|
|
|
* to avoid copying data around.
|
|
|
|
*
|
|
|
|
* @defgroup netconn_common Common functions
|
|
|
|
* @ingroup netconn
|
|
|
|
* For use with TCP and UDP
|
|
|
|
*
|
|
|
|
* @defgroup netconn_tcp TCP only
|
|
|
|
* @ingroup netconn
|
|
|
|
* TCP only functions
|
|
|
|
*
|
|
|
|
* @defgroup netconn_udp UDP only
|
|
|
|
* @ingroup netconn
|
|
|
|
* UDP only functions
|
2007-08-09 22:21:44 +00:00
|
|
|
*/
|
2015-10-06 21:08:28 +02:00
|
|
|
|
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.
|
2015-10-06 21:13:15 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
2002-10-19 12:59:30 +00:00
|
|
|
* 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
|
2015-10-06 21:13:15 +02:00
|
|
|
* derived from this software without specific prior written permission.
|
2002-10-19 12:59:30 +00:00
|
|
|
*
|
2015-10-06 21:13:15 +02:00
|
|
|
* 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
|
2002-10-19 12:59:30 +00:00
|
|
|
* OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the lwIP TCP/IP stack.
|
2015-10-06 21:13:15 +02:00
|
|
|
*
|
2002-10-19 12:59:30 +00:00
|
|
|
* Author: Adam Dunkels <adam@sics.se>
|
2016-07-26 17:40:55 +02:00
|
|
|
*/
|
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
/* 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"
|
|
|
|
#include "lwip/memp.h"
|
|
|
|
|
2007-11-06 20:31:28 +00:00
|
|
|
#include "lwip/ip.h"
|
|
|
|
#include "lwip/raw.h"
|
|
|
|
#include "lwip/udp.h"
|
2016-03-07 20:18:11 +01:00
|
|
|
#include "lwip/priv/api_msg.h"
|
2015-10-09 21:58:58 +02:00
|
|
|
#include "lwip/priv/tcp_priv.h"
|
|
|
|
#include "lwip/priv/tcpip_priv.h"
|
2007-11-06 20:31:28 +00:00
|
|
|
|
2007-09-07 23:01:59 +00:00
|
|
|
#include <string.h>
|
2007-06-16 13:57:30 +00:00
|
|
|
|
2016-04-26 21:33:02 +02:00
|
|
|
#define API_MSG_VAR_REF(name) API_VAR_REF(name)
|
|
|
|
#define API_MSG_VAR_DECLARE(name) API_VAR_DECLARE(struct api_msg, name)
|
|
|
|
#define API_MSG_VAR_ALLOC(name) API_VAR_ALLOC(struct api_msg, MEMP_API_MSG, name, ERR_MEM)
|
|
|
|
#define API_MSG_VAR_ALLOC_RETURN_NULL(name) API_VAR_ALLOC(struct api_msg, MEMP_API_MSG, name, NULL)
|
|
|
|
#define API_MSG_VAR_FREE(name) API_VAR_FREE(MEMP_API_MSG, name)
|
2014-02-20 19:50:17 +01:00
|
|
|
|
2015-02-18 22:21:26 +01:00
|
|
|
static err_t netconn_close_shutdown(struct netconn *conn, u8_t how);
|
|
|
|
|
2016-03-07 20:18:11 +01:00
|
|
|
/**
|
|
|
|
* Call the lower part of a netconn_* function
|
|
|
|
* This function is then running in the thread context
|
|
|
|
* of tcpip_thread and has exclusive access to lwIP core code.
|
|
|
|
*
|
2016-03-16 20:16:06 +01:00
|
|
|
* @param fn function to call
|
2016-03-07 20:18:11 +01:00
|
|
|
* @param apimsg a struct containing the function to call and its parameters
|
|
|
|
* @return ERR_OK if the function was called, another err_t if not
|
|
|
|
*/
|
|
|
|
static err_t
|
2016-03-16 22:02:47 +01:00
|
|
|
netconn_apimsg(tcpip_callback_fn fn, struct api_msg *apimsg)
|
2016-03-07 20:18:11 +01:00
|
|
|
{
|
2016-04-27 12:49:06 +02:00
|
|
|
err_t err;
|
|
|
|
|
2016-03-07 20:18:11 +01:00
|
|
|
#ifdef LWIP_DEBUG
|
|
|
|
/* catch functions that don't set err */
|
2016-03-16 21:31:19 +01:00
|
|
|
apimsg->err = ERR_VAL;
|
2016-03-16 20:16:06 +01:00
|
|
|
#endif /* LWIP_DEBUG */
|
|
|
|
|
2016-03-07 20:18:11 +01:00
|
|
|
#if LWIP_NETCONN_SEM_PER_THREAD
|
2016-03-16 21:31:19 +01:00
|
|
|
apimsg->op_completed_sem = LWIP_NETCONN_THREAD_SEM_GET();
|
2016-03-16 20:16:06 +01:00
|
|
|
#endif /* LWIP_NETCONN_SEM_PER_THREAD */
|
2016-04-27 12:49:06 +02:00
|
|
|
|
|
|
|
err = tcpip_send_msg_wait_sem(fn, apimsg, LWIP_API_MSG_SEM(apimsg));
|
|
|
|
if (err == ERR_OK) {
|
2016-04-26 22:03:18 +02:00
|
|
|
return apimsg->err;
|
|
|
|
}
|
2016-04-27 12:49:06 +02:00
|
|
|
return err;
|
2016-03-07 20:18:11 +01:00
|
|
|
}
|
|
|
|
|
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;
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_DECLARE(msg);
|
2016-04-26 21:33:02 +02:00
|
|
|
API_MSG_VAR_ALLOC_RETURN_NULL(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) {
|
2012-02-28 12:45:59 +01:00
|
|
|
err_t err;
|
2016-04-26 21:33:02 +02:00
|
|
|
|
2016-03-16 21:31:19 +01:00
|
|
|
API_MSG_VAR_REF(msg).msg.n.proto = proto;
|
|
|
|
API_MSG_VAR_REF(msg).conn = conn;
|
2016-03-16 22:02:47 +01:00
|
|
|
err = netconn_apimsg(lwip_netconn_do_newconn, &API_MSG_VAR_REF(msg));
|
2012-02-28 12:45:59 +01:00
|
|
|
if (err != ERR_OK) {
|
2007-11-30 12:54:10 +00:00
|
|
|
LWIP_ASSERT("freeing conn without freeing pcb", conn->pcb.tcp == NULL);
|
2010-02-12 13:49:21 +00:00
|
|
|
LWIP_ASSERT("conn has no recvmbox", sys_mbox_valid(&conn->recvmbox));
|
2010-03-26 16:09:02 +00:00
|
|
|
#if LWIP_TCP
|
2010-02-12 13:49:21 +00:00
|
|
|
LWIP_ASSERT("conn->acceptmbox shouldn't exist", !sys_mbox_valid(&conn->acceptmbox));
|
2010-03-26 16:09:02 +00:00
|
|
|
#endif /* LWIP_TCP */
|
2014-12-10 20:45:01 +01:00
|
|
|
#if !LWIP_NETCONN_SEM_PER_THREAD
|
|
|
|
LWIP_ASSERT("conn has no op_completed", sys_sem_valid(&conn->op_completed));
|
2010-02-12 13:49:21 +00:00
|
|
|
sys_sem_free(&conn->op_completed);
|
2014-12-10 20:45:01 +01:00
|
|
|
#endif /* !LWIP_NETCONN_SEM_PER_THREAD */
|
2010-02-12 13:49:21 +00:00
|
|
|
sys_mbox_free(&conn->recvmbox);
|
2007-11-30 12:54:10 +00:00
|
|
|
memp_free(MEMP_NETCONN, conn);
|
2016-04-26 21:33:02 +02:00
|
|
|
API_MSG_VAR_FREE(msg);
|
2007-11-30 12:54:10 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2016-04-26 21:33:02 +02:00
|
|
|
API_MSG_VAR_FREE(msg);
|
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
|
|
|
/**
|
2016-07-26 17:40:55 +02:00
|
|
|
* @ingroup netconn_common
|
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)
|
|
|
|
{
|
2015-01-17 21:54:17 +01:00
|
|
|
err_t err;
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_DECLARE(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
|
|
|
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_ALLOC(msg);
|
2016-03-16 21:31:19 +01:00
|
|
|
API_MSG_VAR_REF(msg).conn = conn;
|
2015-02-10 22:15:54 +01:00
|
|
|
#if LWIP_SO_SNDTIMEO || LWIP_SO_LINGER
|
|
|
|
/* get the time we started, which is later compared to
|
|
|
|
sys_now() + conn->send_timeout */
|
2016-03-16 21:31:19 +01:00
|
|
|
API_MSG_VAR_REF(msg).msg.sd.time_started = sys_now();
|
2015-02-10 22:15:54 +01:00
|
|
|
#else /* LWIP_SO_SNDTIMEO || LWIP_SO_LINGER */
|
2015-08-18 20:32:09 +02:00
|
|
|
#if LWIP_TCP
|
2016-03-16 21:31:19 +01:00
|
|
|
API_MSG_VAR_REF(msg).msg.sd.polls_left =
|
2015-02-10 22:15:54 +01:00
|
|
|
((LWIP_TCP_CLOSE_TIMEOUT_MS_DEFAULT + TCP_SLOW_INTERVAL - 1) / TCP_SLOW_INTERVAL) + 1;
|
2015-08-18 20:32:09 +02:00
|
|
|
#endif /* LWIP_TCP */
|
2015-02-10 22:15:54 +01:00
|
|
|
#endif /* LWIP_SO_SNDTIMEO || LWIP_SO_LINGER */
|
2016-03-16 22:02:47 +01:00
|
|
|
err = netconn_apimsg(lwip_netconn_do_delconn, &API_MSG_VAR_REF(msg));
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_FREE(msg);
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2015-01-17 21:54:17 +01:00
|
|
|
if (err != ERR_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
2007-03-21 16:38:58 +00:00
|
|
|
|
2015-01-17 21:54:17 +01:00
|
|
|
netconn_free(conn);
|
2010-01-17 18:28:56 +00:00
|
|
|
|
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
|
2010-02-04 18:47:44 +00:00
|
|
|
netconn_getaddr(struct netconn *conn, ip_addr_t *addr, u16_t *port, u8_t local)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_DECLARE(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;);
|
|
|
|
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_ALLOC(msg);
|
2016-03-16 21:31:19 +01:00
|
|
|
API_MSG_VAR_REF(msg).conn = conn;
|
|
|
|
API_MSG_VAR_REF(msg).msg.ad.local = local;
|
2014-02-20 19:50:17 +01:00
|
|
|
#if LWIP_MPU_COMPATIBLE
|
2016-04-25 20:50:14 +02:00
|
|
|
err = netconn_apimsg(lwip_netconn_do_getaddr, &API_MSG_VAR_REF(msg));
|
2016-03-16 21:31:19 +01:00
|
|
|
*addr = msg->msg.ad.ipaddr;
|
|
|
|
*port = msg->msg.ad.port;
|
2014-02-20 19:50:17 +01:00
|
|
|
#else /* LWIP_MPU_COMPATIBLE */
|
2016-03-16 21:31:19 +01:00
|
|
|
msg.msg.ad.ipaddr = addr;
|
|
|
|
msg.msg.ad.port = port;
|
2016-03-16 22:02:47 +01:00
|
|
|
err = netconn_apimsg(lwip_netconn_do_getaddr, &msg);
|
2014-02-20 19:50:17 +01:00
|
|
|
#endif /* LWIP_MPU_COMPATIBLE */
|
|
|
|
API_MSG_VAR_FREE(msg);
|
2007-11-12 22:39:24 +00:00
|
|
|
|
2010-01-17 18:28:56 +00:00
|
|
|
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
|
|
|
/**
|
2016-07-26 17:40:55 +02:00
|
|
|
* @ingroup netconn_common
|
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
|
2016-09-28 12:56:39 +02:00
|
|
|
* @param addr the local IP address to bind the netconn to
|
|
|
|
* (use IP4_ADDR_ANY/IP6_ADDR_ANY to bind to all addresses)
|
2007-06-16 13:57:30 +00:00
|
|
|
* @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
|
2015-02-22 21:46:35 +01:00
|
|
|
netconn_bind(struct netconn *conn, const ip_addr_t *addr, u16_t port)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_DECLARE(msg);
|
2010-01-17 18:28:56 +00:00
|
|
|
err_t err;
|
2016-03-03 22:49:55 +01: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
|
|
|
|
2016-12-05 22:01:58 +01:00
|
|
|
#if LWIP_IPV4
|
2016-03-03 22:49:55 +01:00
|
|
|
/* Don't propagate NULL pointer (IP_ADDR_ANY alias) to subsequent functions */
|
2014-02-20 19:50:17 +01:00
|
|
|
if (addr == NULL) {
|
2016-09-28 12:56:39 +02:00
|
|
|
addr = IP4_ADDR_ANY;
|
2014-02-20 19:50:17 +01:00
|
|
|
}
|
2016-12-05 22:01:58 +01:00
|
|
|
#endif /* LWIP_IPV4 */
|
2016-11-16 22:58:38 +01:00
|
|
|
|
2016-11-17 09:12:38 +01:00
|
|
|
#if LWIP_IPV4 && LWIP_IPV6
|
2016-11-17 12:41:00 +01:00
|
|
|
/* "Socket API like" dual-stack support: If IP to bind to is IP6_ADDR_ANY,
|
|
|
|
* and NETCONN_FLAG_IPV6_V6ONLY is 0, use IP_ANY_TYPE to bind
|
|
|
|
*/
|
|
|
|
if ((netconn_get_ipv6only(conn) == 0) &&
|
|
|
|
ip_addr_cmp(addr, IP6_ADDR_ANY)) {
|
|
|
|
addr = IP_ANY_TYPE;
|
|
|
|
}
|
2016-11-16 22:58:38 +01:00
|
|
|
#endif /* LWIP_IPV4 && LWIP_IPV6 */
|
2016-03-03 22:49:55 +01:00
|
|
|
|
2016-11-17 12:41:00 +01:00
|
|
|
API_MSG_VAR_ALLOC(msg);
|
|
|
|
API_MSG_VAR_REF(msg).conn = conn;
|
|
|
|
API_MSG_VAR_REF(msg).msg.bc.ipaddr = API_MSG_VAR_REF(addr);
|
|
|
|
API_MSG_VAR_REF(msg).msg.bc.port = port;
|
|
|
|
err = netconn_apimsg(lwip_netconn_do_bind, &API_MSG_VAR_REF(msg));
|
|
|
|
API_MSG_VAR_FREE(msg);
|
2010-01-17 18:28:56 +00:00
|
|
|
|
|
|
|
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
|
|
|
/**
|
2016-07-26 17:40:55 +02:00
|
|
|
* @ingroup netconn_common
|
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
|
2015-02-22 21:46:35 +01:00
|
|
|
netconn_connect(struct netconn *conn, const ip_addr_t *addr, u16_t port)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_DECLARE(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
|
|
|
|
2016-12-05 22:01:58 +01:00
|
|
|
#if LWIP_IPV4
|
2016-03-03 22:49:55 +01:00
|
|
|
/* Don't propagate NULL pointer (IP_ADDR_ANY alias) to subsequent functions */
|
2014-02-20 19:50:17 +01:00
|
|
|
if (addr == NULL) {
|
2016-09-28 12:56:39 +02:00
|
|
|
addr = IP4_ADDR_ANY;
|
2014-02-20 19:50:17 +01:00
|
|
|
}
|
2016-12-05 22:01:58 +01:00
|
|
|
#endif /* LWIP_IPV4 */
|
2016-03-03 22:49:55 +01:00
|
|
|
|
2016-11-17 12:41:00 +01:00
|
|
|
API_MSG_VAR_ALLOC(msg);
|
|
|
|
API_MSG_VAR_REF(msg).conn = conn;
|
|
|
|
API_MSG_VAR_REF(msg).msg.bc.ipaddr = API_MSG_VAR_REF(addr);
|
|
|
|
API_MSG_VAR_REF(msg).msg.bc.port = port;
|
|
|
|
err = netconn_apimsg(lwip_netconn_do_connect, &API_MSG_VAR_REF(msg));
|
|
|
|
API_MSG_VAR_FREE(msg);
|
2010-01-17 18:28:56 +00:00
|
|
|
|
|
|
|
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
|
|
|
/**
|
2016-07-26 17:40:55 +02:00
|
|
|
* @ingroup netconn_udp
|
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
|
2016-10-09 12:21:39 +02:00
|
|
|
* @return See @ref err_t
|
2007-06-16 13:57:30 +00:00
|
|
|
*/
|
2003-01-22 16:18:05 +00:00
|
|
|
err_t
|
|
|
|
netconn_disconnect(struct netconn *conn)
|
|
|
|
{
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_DECLARE(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
|
|
|
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_ALLOC(msg);
|
2016-03-16 21:31:19 +01:00
|
|
|
API_MSG_VAR_REF(msg).conn = conn;
|
2016-03-16 22:02:47 +01:00
|
|
|
err = netconn_apimsg(lwip_netconn_do_disconnect, &API_MSG_VAR_REF(msg));
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_FREE(msg);
|
2010-01-17 18:28:56 +00:00
|
|
|
|
|
|
|
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
|
|
|
/**
|
2016-07-26 17:40:55 +02:00
|
|
|
* @ingroup netconn_tcp
|
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
|
|
|
{
|
2010-07-29 19:25:50 +00:00
|
|
|
#if LWIP_TCP
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_DECLARE(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
|
|
|
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_ALLOC(msg);
|
2016-03-16 21:31:19 +01:00
|
|
|
API_MSG_VAR_REF(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
|
2016-03-16 21:31:19 +01:00
|
|
|
API_MSG_VAR_REF(msg).msg.lb.backlog = backlog;
|
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 */
|
2016-03-16 22:02:47 +01:00
|
|
|
err = netconn_apimsg(lwip_netconn_do_listen, &API_MSG_VAR_REF(msg));
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_FREE(msg);
|
2010-01-17 18:28:56 +00:00
|
|
|
|
|
|
|
return err;
|
2010-07-29 19:25:50 +00:00
|
|
|
#else /* LWIP_TCP */
|
|
|
|
LWIP_UNUSED_ARG(conn);
|
|
|
|
LWIP_UNUSED_ARG(backlog);
|
|
|
|
return ERR_ARG;
|
|
|
|
#endif /* LWIP_TCP */
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-06-16 13:57:30 +00:00
|
|
|
/**
|
2016-07-26 17:40:55 +02:00
|
|
|
* @ingroup netconn_tcp
|
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
|
|
|
{
|
2010-03-26 16:09:02 +00:00
|
|
|
#if LWIP_TCP
|
2016-04-05 21:42:10 +02:00
|
|
|
void *accept_ptr;
|
2002-10-19 12:59:30 +00:00
|
|
|
struct netconn *newconn;
|
2010-01-17 16:21:07 +00:00
|
|
|
#if TCP_LISTEN_BACKLOG
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_DECLARE(msg);
|
2010-01-17 16:21:07 +00:00
|
|
|
#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;);
|
2007-05-23 17:46:53 +00:00
|
|
|
|
2016-12-18 21:31:14 +01:00
|
|
|
if (ERR_IS_FATAL(conn->last_err)) {
|
2010-01-17 18:28:56 +00:00
|
|
|
/* don't recv on fatal errors: this might block the application task
|
|
|
|
waiting on acceptmbox forever! */
|
2016-12-18 21:31:14 +01:00
|
|
|
return conn->last_err;
|
2010-01-17 18:28:56 +00:00
|
|
|
}
|
2016-04-05 21:42:10 +02:00
|
|
|
if (!sys_mbox_valid(&conn->acceptmbox)) {
|
|
|
|
return ERR_CLSD;
|
|
|
|
}
|
2016-04-27 12:49:06 +02:00
|
|
|
|
|
|
|
#if TCP_LISTEN_BACKLOG
|
2017-01-12 16:36:17 +01:00
|
|
|
/* need to allocate API message here so empty message pool does not result in event loss
|
|
|
|
* see bug #47512: MPU_COMPATIBLE may fail on empty pool */
|
2016-04-27 12:49:06 +02:00
|
|
|
API_MSG_VAR_ALLOC(msg);
|
|
|
|
#endif /* TCP_LISTEN_BACKLOG */
|
|
|
|
|
2017-03-02 20:38:11 +01:00
|
|
|
if (netconn_is_nonblocking(conn)) {
|
|
|
|
if (sys_arch_mbox_tryfetch(&conn->acceptmbox, &accept_ptr) == SYS_ARCH_TIMEOUT) {
|
|
|
|
#if TCP_LISTEN_BACKLOG
|
|
|
|
API_MSG_VAR_FREE(msg);
|
|
|
|
#endif /* TCP_LISTEN_BACKLOG */
|
|
|
|
return ERR_WOULDBLOCK;
|
|
|
|
}
|
|
|
|
} else {
|
2007-10-09 19:30:17 +00:00
|
|
|
#if LWIP_SO_RCVTIMEO
|
2017-03-02 20:38:11 +01:00
|
|
|
if (sys_arch_mbox_fetch(&conn->acceptmbox, &accept_ptr, conn->recv_timeout) == SYS_ARCH_TIMEOUT) {
|
2016-04-27 12:49:06 +02:00
|
|
|
#if TCP_LISTEN_BACKLOG
|
2017-03-02 20:38:11 +01:00
|
|
|
API_MSG_VAR_FREE(msg);
|
2016-04-27 12:49:06 +02:00
|
|
|
#endif /* TCP_LISTEN_BACKLOG */
|
2017-03-02 20:38:11 +01:00
|
|
|
return ERR_TIMEOUT;
|
|
|
|
}
|
2007-10-09 19:30:17 +00:00
|
|
|
#else
|
2017-03-02 20:38:11 +01:00
|
|
|
sys_arch_mbox_fetch(&conn->acceptmbox, &accept_ptr, 0);
|
2007-10-09 19:30:17 +00:00
|
|
|
#endif /* LWIP_SO_RCVTIMEO*/
|
2017-03-02 20:38:11 +01:00
|
|
|
}
|
2016-04-05 21:42:10 +02:00
|
|
|
newconn = (struct netconn *)accept_ptr;
|
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
|
|
|
|
2016-04-05 21:42:10 +02:00
|
|
|
if (accept_ptr == &netconn_aborted) {
|
|
|
|
/* a connection has been aborted: out of pcbs or out of netconns during accept */
|
|
|
|
/* @todo: set netconn error, but this would be fatal and thus block further accepts */
|
2016-04-27 12:49:06 +02:00
|
|
|
#if TCP_LISTEN_BACKLOG
|
|
|
|
API_MSG_VAR_FREE(msg);
|
|
|
|
#endif /* TCP_LISTEN_BACKLOG */
|
2016-04-05 21:42:10 +02:00
|
|
|
return ERR_ABRT;
|
|
|
|
}
|
2010-01-17 16:21:07 +00:00
|
|
|
if (newconn == NULL) {
|
2011-03-13 11:17:18 +00:00
|
|
|
/* connection has been aborted */
|
2015-03-19 21:20:29 +01:00
|
|
|
/* in this special case, we set the netconn error from application thread, as
|
|
|
|
on a ready-to-accept listening netconn, there should not be anything running
|
|
|
|
in tcpip_thread */
|
2016-04-05 21:42:10 +02:00
|
|
|
NETCONN_SET_SAFE_ERR(conn, ERR_CLSD);
|
2016-04-27 12:49:06 +02:00
|
|
|
#if TCP_LISTEN_BACKLOG
|
|
|
|
API_MSG_VAR_FREE(msg);
|
|
|
|
#endif /* TCP_LISTEN_BACKLOG */
|
2016-04-05 21:42:10 +02:00
|
|
|
return ERR_CLSD;
|
2010-01-17 16:21:07 +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
|
|
|
#if TCP_LISTEN_BACKLOG
|
2010-01-17 16:21:07 +00:00
|
|
|
/* Let the stack know that we have accepted the connection. */
|
2016-03-22 07:30:44 +01:00
|
|
|
API_MSG_VAR_REF(msg).conn = newconn;
|
2016-04-26 22:03:18 +02:00
|
|
|
/* don't care for the return value of lwip_netconn_do_recv */
|
2016-03-22 07:30:44 +01:00
|
|
|
netconn_apimsg(lwip_netconn_do_accepted, &API_MSG_VAR_REF(msg));
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_FREE(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;
|
2010-03-26 16:09:02 +00:00
|
|
|
#else /* LWIP_TCP */
|
|
|
|
LWIP_UNUSED_ARG(conn);
|
|
|
|
LWIP_UNUSED_ARG(new_conn);
|
|
|
|
return ERR_ARG;
|
|
|
|
#endif /* LWIP_TCP */
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-06-16 13:57:30 +00:00
|
|
|
/**
|
2016-07-26 17:40:55 +02:00
|
|
|
* @ingroup netconn_common
|
2010-03-06 11:29:01 +00:00
|
|
|
* Receive data: actual implementation that doesn't care whether pbuf or netbuf
|
2017-03-02 20:38:11 +01:00
|
|
|
* is received (this is internal, it's just here for describing common errors)
|
2007-06-16 13:57:30 +00:00
|
|
|
*
|
|
|
|
* @param conn the netconn from which to receive data
|
2010-03-06 11:29:01 +00:00
|
|
|
* @param new_buf pointer where a new pbuf/netbuf is stored when received data
|
2017-03-02 20:38:11 +01:00
|
|
|
* @param apiflags flags that control function behaviour. For now only:
|
|
|
|
* - NETCONN_DONTBLOCK: only read data that is available now, don't wait for more data
|
2010-01-17 16:21:07 +00:00
|
|
|
* @return ERR_OK if data has been received, an error code otherwise (timeout,
|
|
|
|
* memory error or another error)
|
2017-03-02 20:38:11 +01:00
|
|
|
* ERR_CONN if not connected
|
|
|
|
* ERR_CLSD if TCP connection has been closed
|
|
|
|
* ERR_WOULDBLOCK if the netconn is nonblocking but would block to wait for data
|
|
|
|
* ERR_TIMEOUT if the netconn has a receive timeout and no data was received
|
2007-06-16 13:57:30 +00:00
|
|
|
*/
|
2010-03-06 11:29:01 +00:00
|
|
|
static err_t
|
2017-03-02 20:38:11 +01:00
|
|
|
netconn_recv_data(struct netconn *conn, void **new_buf, u8_t apiflags)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
2010-03-06 11:29:01 +00:00
|
|
|
void *buf = NULL;
|
2003-02-11 21:00:14 +00:00
|
|
|
u16_t len;
|
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;);
|
2015-02-11 21:39:45 +01:00
|
|
|
LWIP_ERROR("netconn_recv: invalid recvmbox", sys_mbox_valid(&conn->recvmbox), return ERR_CONN;);
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2016-12-18 21:31:14 +01:00
|
|
|
if (ERR_IS_FATAL(conn->last_err)) {
|
2010-01-17 18:28:56 +00:00
|
|
|
/* don't recv on fatal errors: this might block the application task
|
|
|
|
waiting on recvmbox forever! */
|
2010-02-12 13:49:21 +00:00
|
|
|
/* @todo: this does not allow us to fetch data that has been put into recvmbox
|
|
|
|
before the fatal error occurred - is that a problem? */
|
2016-12-18 21:31:14 +01:00
|
|
|
return conn->last_err;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2016-07-21 21:54:13 +02:00
|
|
|
|
2017-03-02 20:38:11 +01:00
|
|
|
if (netconn_is_nonblocking(conn) || (apiflags & NETCONN_DONTBLOCK)) {
|
|
|
|
if (sys_arch_mbox_tryfetch(&conn->recvmbox, &buf) == SYS_ARCH_TIMEOUT) {
|
|
|
|
return ERR_WOULDBLOCK;
|
2016-04-27 21:14:17 +02:00
|
|
|
}
|
2017-03-02 20:38:11 +01:00
|
|
|
} else {
|
|
|
|
#if LWIP_SO_RCVTIMEO
|
|
|
|
if (sys_arch_mbox_fetch(&conn->recvmbox, &buf, conn->recv_timeout) == SYS_ARCH_TIMEOUT) {
|
|
|
|
return ERR_TIMEOUT;
|
|
|
|
}
|
2007-05-23 19:18:09 +00:00
|
|
|
#else
|
2017-03-02 20:38:11 +01:00
|
|
|
sys_arch_mbox_fetch(&conn->recvmbox, &buf, 0);
|
2007-05-23 19:18:09 +00:00
|
|
|
#endif /* LWIP_SO_RCVTIMEO*/
|
2017-03-02 20:38:11 +01:00
|
|
|
}
|
2003-02-11 16:33:02 +00:00
|
|
|
|
2010-03-06 11:29:01 +00:00
|
|
|
#if LWIP_TCP
|
2011-10-13 19:25:11 +02:00
|
|
|
#if (LWIP_UDP || LWIP_RAW)
|
|
|
|
if (NETCONNTYPE_GROUP(conn->type) == NETCONN_TCP)
|
|
|
|
#endif /* (LWIP_UDP || LWIP_RAW) */
|
|
|
|
{
|
2017-03-06 21:27:33 +01:00
|
|
|
/* If we received a NULL pointer, we are closed */
|
2010-03-06 11:29:01 +00:00
|
|
|
if (buf == NULL) {
|
2017-03-06 21:27:33 +01:00
|
|
|
/* new_buf has been zeroed above alredy */
|
|
|
|
return ERR_OK;
|
2010-03-06 11:29:01 +00:00
|
|
|
}
|
|
|
|
len = ((struct pbuf *)buf)->tot_len;
|
|
|
|
}
|
2007-05-19 13:54:56 +00:00
|
|
|
#endif /* LWIP_TCP */
|
2010-03-06 11:29:01 +00:00
|
|
|
#if LWIP_TCP && (LWIP_UDP || LWIP_RAW)
|
|
|
|
else
|
|
|
|
#endif /* LWIP_TCP && (LWIP_UDP || LWIP_RAW) */
|
2007-05-19 13:54:56 +00:00
|
|
|
#if (LWIP_UDP || LWIP_RAW)
|
2010-03-06 11:29:01 +00:00
|
|
|
{
|
2010-01-17 16:21:07 +00:00
|
|
|
LWIP_ASSERT("buf != NULL", buf != NULL);
|
2016-11-17 08:51:07 +01:00
|
|
|
len = netbuf_len((struct netbuf*)buf);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2010-03-06 11:29:01 +00:00
|
|
|
#endif /* (LWIP_UDP || LWIP_RAW) */
|
|
|
|
|
2010-12-20 18:03:51 +00:00
|
|
|
#if LWIP_SO_RCVBUF
|
2010-03-06 11:29:01 +00:00
|
|
|
SYS_ARCH_DEC(conn->recv_avail, len);
|
2010-12-20 18:03:51 +00:00
|
|
|
#endif /* LWIP_SO_RCVBUF */
|
2010-03-06 11:29:01 +00:00
|
|
|
/* Register event with callback */
|
|
|
|
API_EVENT(conn, NETCONN_EVT_RCVMINUS, len);
|
2007-06-13 18:00:54 +00:00
|
|
|
|
2010-03-06 11:29:01 +00:00
|
|
|
LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_recv_data: received %p, len=%"U16_F"\n", buf, len));
|
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
|
|
|
|
2017-03-06 21:33:35 +01:00
|
|
|
static err_t
|
2017-03-06 22:20:41 +01:00
|
|
|
netconn_tcp_recvd_msg(struct netconn *conn, u32_t len, struct api_msg* msg)
|
2017-03-06 21:33:35 +01:00
|
|
|
{
|
|
|
|
LWIP_ERROR("netconn_recv_tcp_pbuf: invalid conn", (conn != NULL) &&
|
|
|
|
NETCONNTYPE_GROUP(netconn_type(conn)) == NETCONN_TCP, return ERR_ARG;);
|
|
|
|
|
|
|
|
msg->conn = conn;
|
|
|
|
msg->msg.r.len = len;
|
|
|
|
|
|
|
|
return netconn_apimsg(lwip_netconn_do_recv, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
err_t
|
2017-03-06 22:20:41 +01:00
|
|
|
netconn_tcp_recvd(struct netconn *conn, u32_t len)
|
2017-03-06 21:33:35 +01:00
|
|
|
{
|
|
|
|
err_t err;
|
|
|
|
API_MSG_VAR_DECLARE(msg);
|
|
|
|
LWIP_ERROR("netconn_recv_tcp_pbuf: invalid conn", (conn != NULL) &&
|
|
|
|
NETCONNTYPE_GROUP(netconn_type(conn)) == NETCONN_TCP, return ERR_ARG;);
|
|
|
|
|
|
|
|
API_MSG_VAR_ALLOC(msg);
|
|
|
|
err = netconn_tcp_recvd_msg(conn, len, &API_VAR_REF(msg));
|
|
|
|
API_MSG_VAR_FREE(msg);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-03-06 21:27:33 +01:00
|
|
|
#if LWIP_TCP
|
|
|
|
static err_t
|
|
|
|
netconn_recv_data_tcp(struct netconn *conn, struct pbuf **new_buf, u8_t apiflags)
|
|
|
|
{
|
|
|
|
err_t err;
|
|
|
|
struct pbuf *buf;
|
|
|
|
#if LWIP_TCP
|
|
|
|
API_MSG_VAR_DECLARE(msg);
|
|
|
|
#if LWIP_MPU_COMPATIBLE
|
|
|
|
msg = NULL;
|
|
|
|
#endif
|
|
|
|
#endif /* LWIP_TCP */
|
|
|
|
|
|
|
|
if (!sys_mbox_valid(&conn->recvmbox)) {
|
|
|
|
/* This happens when calling this function after receiving FIN */
|
|
|
|
return sys_mbox_valid(&conn->acceptmbox) ? ERR_CONN : ERR_CLSD;
|
|
|
|
}
|
|
|
|
|
2017-03-06 21:33:35 +01:00
|
|
|
if (!(apiflags & NETCONN_NOAUTORCVD)) {
|
|
|
|
/* need to allocate API message here so empty message pool does not result in event loss
|
|
|
|
* see bug #47512: MPU_COMPATIBLE may fail on empty pool */
|
|
|
|
API_MSG_VAR_ALLOC(msg);
|
|
|
|
}
|
2017-03-06 21:27:33 +01:00
|
|
|
|
|
|
|
err = netconn_recv_data(conn, (void **)new_buf, apiflags);
|
|
|
|
if (err != ERR_OK) {
|
2017-03-06 21:33:35 +01:00
|
|
|
if (!(apiflags & NETCONN_NOAUTORCVD)) {
|
|
|
|
API_MSG_VAR_FREE(msg);
|
|
|
|
}
|
2017-03-06 21:27:33 +01:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
buf = *new_buf;
|
2017-03-06 21:33:35 +01:00
|
|
|
if (!(apiflags & NETCONN_NOAUTORCVD)) {
|
|
|
|
/* Let the stack know that we have taken the data. */
|
|
|
|
u16_t len = buf ? buf->tot_len : 1;
|
|
|
|
/* don't care for the return value of lwip_netconn_do_recv */
|
|
|
|
/* @todo: this should really be fixed, e.g. by retrying in poll on error */
|
|
|
|
netconn_tcp_recvd_msg(conn, len, &API_VAR_REF(msg));
|
|
|
|
API_MSG_VAR_FREE(msg);
|
|
|
|
}
|
2017-03-06 21:27:33 +01:00
|
|
|
|
|
|
|
/* If we are closed, we indicate that we no longer wish to use the socket */
|
|
|
|
if (buf == NULL) {
|
|
|
|
API_EVENT(conn, NETCONN_EVT_RCVMINUS, 0);
|
|
|
|
if (conn->pcb.ip == NULL) {
|
|
|
|
/* race condition: RST during recv */
|
|
|
|
return conn->last_err == ERR_OK ? ERR_RST : conn->last_err;
|
|
|
|
}
|
|
|
|
/* RX side is closed, so deallocate the recvmbox */
|
|
|
|
netconn_close_shutdown(conn, NETCONN_SHUT_RD);
|
|
|
|
/* Don' store ERR_CLSD as conn->err since we are only half-closed */
|
|
|
|
return ERR_CLSD;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2010-03-06 11:29:01 +00:00
|
|
|
/**
|
2016-07-26 17:40:55 +02:00
|
|
|
* @ingroup netconn_tcp
|
2010-03-06 11:29:01 +00:00
|
|
|
* Receive data (in form of a pbuf) from a TCP netconn
|
|
|
|
*
|
|
|
|
* @param conn the netconn from which to receive data
|
|
|
|
* @param new_buf pointer where a new pbuf is stored when received data
|
|
|
|
* @return ERR_OK if data has been received, an error code otherwise (timeout,
|
2017-03-02 20:38:11 +01:00
|
|
|
* memory error or another error, @see netconn_recv_data)
|
2010-03-06 11:29:01 +00:00
|
|
|
* ERR_ARG if conn is not a TCP netconn
|
|
|
|
*/
|
|
|
|
err_t
|
|
|
|
netconn_recv_tcp_pbuf(struct netconn *conn, struct pbuf **new_buf)
|
|
|
|
{
|
2017-02-10 22:01:21 +01:00
|
|
|
LWIP_ERROR("netconn_recv_tcp_pbuf: invalid conn", (conn != NULL) &&
|
2011-05-17 19:35:14 +00:00
|
|
|
NETCONNTYPE_GROUP(netconn_type(conn)) == NETCONN_TCP, return ERR_ARG;);
|
2010-03-06 11:29:01 +00:00
|
|
|
|
2017-03-06 21:27:33 +01:00
|
|
|
return netconn_recv_data_tcp(conn, new_buf, 0);
|
2017-03-02 20:38:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup netconn_tcp
|
|
|
|
* Receive data (in form of a pbuf) from a TCP netconn
|
|
|
|
*
|
|
|
|
* @param conn the netconn from which to receive data
|
|
|
|
* @param new_buf pointer where a new pbuf is stored when received data
|
|
|
|
* @param apiflags flags that control function behaviour. For now only:
|
|
|
|
* - NETCONN_DONTBLOCK: only read data that is available now, don't wait for more data
|
|
|
|
* @return ERR_OK if data has been received, an error code otherwise (timeout,
|
|
|
|
* memory error or another error, @see netconn_recv_data)
|
|
|
|
* ERR_ARG if conn is not a TCP netconn
|
|
|
|
*/
|
|
|
|
err_t
|
|
|
|
netconn_recv_tcp_pbuf_flags(struct netconn *conn, struct pbuf **new_buf, u8_t apiflags)
|
|
|
|
{
|
|
|
|
LWIP_ERROR("netconn_recv_tcp_pbuf: invalid conn", (conn != NULL) &&
|
|
|
|
NETCONNTYPE_GROUP(netconn_type(conn)) == NETCONN_TCP, return ERR_ARG;);
|
|
|
|
|
2017-03-06 21:27:33 +01:00
|
|
|
return netconn_recv_data_tcp(conn, new_buf, apiflags);
|
2010-03-06 11:29:01 +00:00
|
|
|
}
|
2017-03-06 21:27:33 +01:00
|
|
|
#endif /* LWIP_TCP */
|
2010-03-06 11:29:01 +00:00
|
|
|
|
2017-02-10 22:01:21 +01:00
|
|
|
/**
|
|
|
|
* Receive data (in form of a netbuf) from a UDP or RAW netconn
|
|
|
|
*
|
|
|
|
* @param conn the netconn from which to receive data
|
|
|
|
* @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)
|
|
|
|
* ERR_ARG if conn is not a UDP/RAW netconn
|
|
|
|
*/
|
|
|
|
err_t
|
|
|
|
netconn_recv_udp_raw_netbuf(struct netconn *conn, struct netbuf **new_buf)
|
|
|
|
{
|
|
|
|
LWIP_ERROR("netconn_recv_udp_raw_netbuf: invalid conn", (conn != NULL) &&
|
|
|
|
NETCONNTYPE_GROUP(netconn_type(conn)) != NETCONN_TCP, return ERR_ARG;);
|
|
|
|
|
2017-03-02 20:38:11 +01:00
|
|
|
return netconn_recv_data(conn, (void **)new_buf, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Receive data (in form of a netbuf) from a UDP or RAW netconn
|
|
|
|
*
|
|
|
|
* @param conn the netconn from which to receive data
|
|
|
|
* @param new_buf pointer where a new netbuf is stored when received data
|
|
|
|
* @param apiflags flags that control function behaviour. For now only:
|
|
|
|
* - NETCONN_DONTBLOCK: only read data that is available now, don't wait for more data
|
|
|
|
* @return ERR_OK if data has been received, an error code otherwise (timeout,
|
|
|
|
* memory error or another error)
|
|
|
|
* ERR_ARG if conn is not a UDP/RAW netconn
|
|
|
|
*/
|
|
|
|
err_t
|
|
|
|
netconn_recv_udp_raw_netbuf_flags(struct netconn *conn, struct netbuf **new_buf, u8_t apiflags)
|
|
|
|
{
|
|
|
|
LWIP_ERROR("netconn_recv_udp_raw_netbuf: invalid conn", (conn != NULL) &&
|
|
|
|
NETCONNTYPE_GROUP(netconn_type(conn)) != NETCONN_TCP, return ERR_ARG;);
|
|
|
|
|
|
|
|
return netconn_recv_data(conn, (void **)new_buf, apiflags);
|
2017-02-10 22:01:21 +01:00
|
|
|
}
|
|
|
|
|
2010-03-06 11:29:01 +00:00
|
|
|
/**
|
2016-07-26 17:40:55 +02:00
|
|
|
* @ingroup netconn_common
|
2010-03-06 11:29:01 +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
|
|
|
|
* @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)
|
|
|
|
*/
|
|
|
|
err_t
|
|
|
|
netconn_recv(struct netconn *conn, struct netbuf **new_buf)
|
|
|
|
{
|
2010-03-26 16:09:02 +00:00
|
|
|
#if LWIP_TCP
|
2010-03-06 11:29:01 +00:00
|
|
|
struct netbuf *buf = NULL;
|
|
|
|
err_t err;
|
2010-03-26 16:09:02 +00:00
|
|
|
#endif /* LWIP_TCP */
|
2010-03-06 11:29:01 +00:00
|
|
|
|
|
|
|
LWIP_ERROR("netconn_recv: invalid pointer", (new_buf != NULL), return ERR_ARG;);
|
|
|
|
*new_buf = NULL;
|
|
|
|
LWIP_ERROR("netconn_recv: invalid conn", (conn != NULL), return ERR_ARG;);
|
|
|
|
|
|
|
|
#if LWIP_TCP
|
2011-10-13 19:25:11 +02:00
|
|
|
#if (LWIP_UDP || LWIP_RAW)
|
|
|
|
if (NETCONNTYPE_GROUP(conn->type) == NETCONN_TCP)
|
|
|
|
#endif /* (LWIP_UDP || LWIP_RAW) */
|
|
|
|
{
|
2010-03-06 11:29:01 +00:00
|
|
|
struct pbuf *p = NULL;
|
|
|
|
/* This is not a listening netconn, since recvmbox is set */
|
|
|
|
|
|
|
|
buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
|
|
|
|
if (buf == NULL) {
|
|
|
|
return ERR_MEM;
|
|
|
|
}
|
|
|
|
|
2017-03-06 21:27:33 +01:00
|
|
|
err = netconn_recv_data_tcp(conn, &p, 0);
|
2010-03-06 11:29:01 +00:00
|
|
|
if (err != ERR_OK) {
|
|
|
|
memp_free(MEMP_NETBUF, buf);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
LWIP_ASSERT("p != NULL", p != NULL);
|
|
|
|
|
|
|
|
buf->p = p;
|
|
|
|
buf->ptr = p;
|
|
|
|
buf->port = 0;
|
2015-04-09 22:21:15 +02:00
|
|
|
ip_addr_set_zero(&buf->addr);
|
2010-03-06 11:29:01 +00:00
|
|
|
*new_buf = buf;
|
|
|
|
/* don't set conn->last_err: it's only ERR_OK, anyway */
|
|
|
|
return ERR_OK;
|
2011-10-13 19:25:11 +02:00
|
|
|
}
|
2010-03-06 11:29:01 +00:00
|
|
|
#endif /* LWIP_TCP */
|
2011-10-13 19:25:11 +02:00
|
|
|
#if LWIP_TCP && (LWIP_UDP || LWIP_RAW)
|
|
|
|
else
|
|
|
|
#endif /* LWIP_TCP && (LWIP_UDP || LWIP_RAW) */
|
2010-03-26 16:09:02 +00:00
|
|
|
{
|
2010-03-06 11:29:01 +00:00
|
|
|
#if (LWIP_UDP || LWIP_RAW)
|
2017-03-02 20:38:11 +01:00
|
|
|
return netconn_recv_data(conn, (void **)new_buf, 0);
|
2010-03-06 11:29:01 +00:00
|
|
|
#endif /* (LWIP_UDP || LWIP_RAW) */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-16 13:57:30 +00:00
|
|
|
/**
|
2016-07-26 17:40:55 +02:00
|
|
|
* @ingroup netconn_udp
|
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
|
2015-02-22 21:46:35 +01:00
|
|
|
netconn_sendto(struct netconn *conn, struct netbuf *buf, const ip_addr_t *addr, u16_t port)
|
2007-06-16 13:57:30 +00:00
|
|
|
{
|
|
|
|
if (buf != NULL) {
|
2015-04-09 22:21:15 +02:00
|
|
|
ip_addr_set(&buf->addr, addr);
|
2007-05-04 15:18:29 +00:00
|
|
|
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
|
|
|
/**
|
2016-07-26 17:40:55 +02:00
|
|
|
* @ingroup netconn_udp
|
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)
|
|
|
|
{
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_DECLARE(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));
|
2016-11-16 22:58:38 +01:00
|
|
|
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_ALLOC(msg);
|
2016-03-16 21:31:19 +01:00
|
|
|
API_MSG_VAR_REF(msg).conn = conn;
|
|
|
|
API_MSG_VAR_REF(msg).msg.b = buf;
|
2016-03-16 22:02:47 +01:00
|
|
|
err = netconn_apimsg(lwip_netconn_do_send, &API_MSG_VAR_REF(msg));
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_FREE(msg);
|
2010-01-17 18:28:56 +00:00
|
|
|
|
|
|
|
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
|
|
|
/**
|
2016-07-26 17:40:55 +02:00
|
|
|
* @ingroup netconn_tcp
|
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 :
|
2010-03-21 12:19:56 +00:00
|
|
|
* - NETCONN_COPY: data will be copied into memory belonging to the stack
|
|
|
|
* - NETCONN_MORE: for TCP connection, PSH flag will be set on last segment sent
|
2014-09-18 21:15:13 +02:00
|
|
|
* - NETCONN_DONTBLOCK: only write the data if all data can be written at once
|
2011-07-22 22:05:24 +02:00
|
|
|
* @param bytes_written pointer to a location that receives the number of written bytes
|
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
|
2011-07-22 22:05:24 +02:00
|
|
|
netconn_write_partly(struct netconn *conn, const void *dataptr, size_t size,
|
|
|
|
u8_t apiflags, size_t *bytes_written)
|
2017-02-27 11:09:33 -06:00
|
|
|
{
|
|
|
|
struct netvector vector;
|
|
|
|
vector.ptr = dataptr;
|
|
|
|
vector.len = size;
|
|
|
|
return netconn_write_vectors_partly(conn, &vector, 1, apiflags, bytes_written);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send vectorized data atomically over a TCP netconn.
|
|
|
|
*
|
|
|
|
* @param conn the TCP netconn over which to send data
|
|
|
|
* @param vectors array of vectors containing data to send
|
|
|
|
* @param vectorcnt number of vectors in the array
|
|
|
|
* @param apiflags combination of following flags :
|
|
|
|
* - NETCONN_COPY: data will be copied into memory belonging to the stack
|
|
|
|
* - NETCONN_MORE: for TCP connection, PSH flag will be set on last segment sent
|
|
|
|
* - NETCONN_DONTBLOCK: only write the data if all data can be written at once
|
|
|
|
* @param bytes_written pointer to a location that receives the number of written bytes
|
|
|
|
* @return ERR_OK if data was sent, any other err_t on error
|
|
|
|
*/
|
|
|
|
err_t
|
|
|
|
netconn_write_vectors_partly(struct netconn *conn, struct netvector *vectors, u16_t vectorcnt,
|
|
|
|
u8_t apiflags, size_t *bytes_written)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_DECLARE(msg);
|
2010-01-17 18:28:56 +00:00
|
|
|
err_t err;
|
2011-07-22 22:05:24 +02:00
|
|
|
u8_t dontblock;
|
2017-02-27 11:09:33 -06:00
|
|
|
size_t size;
|
|
|
|
int i;
|
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;);
|
2011-05-17 19:35:14 +00:00
|
|
|
LWIP_ERROR("netconn_write: invalid conn->type", (NETCONNTYPE_GROUP(conn->type)== NETCONN_TCP), return ERR_VAL;);
|
2017-02-27 11:09:33 -06:00
|
|
|
size = 0;
|
|
|
|
for (i = 0; i < vectorcnt; i++) {
|
|
|
|
size += vectors[i].len;
|
|
|
|
}
|
2010-01-29 22:09:31 +00:00
|
|
|
if (size == 0) {
|
|
|
|
return ERR_OK;
|
|
|
|
}
|
2011-07-22 22:05:24 +02:00
|
|
|
dontblock = netconn_is_nonblocking(conn) || (apiflags & NETCONN_DONTBLOCK);
|
2016-11-28 12:51:45 +01:00
|
|
|
#if LWIP_SO_SNDTIMEO
|
|
|
|
if (conn->send_timeout != 0) {
|
2016-11-28 12:54:17 +01:00
|
|
|
dontblock = 1;
|
2016-11-28 12:51:45 +01:00
|
|
|
}
|
|
|
|
#endif /* LWIP_SO_SNDTIMEO */
|
2011-07-22 22:05:24 +02:00
|
|
|
if (dontblock && !bytes_written) {
|
|
|
|
/* This implies netconn_write() cannot be used for non-blocking send, since
|
|
|
|
it has no way to return the number of bytes written. */
|
|
|
|
return ERR_VAL;
|
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_ALLOC(msg);
|
2011-07-22 22:05:24 +02:00
|
|
|
/* non-blocking write sends as much */
|
2016-03-16 21:31:19 +01:00
|
|
|
API_MSG_VAR_REF(msg).conn = conn;
|
2017-02-27 11:09:33 -06:00
|
|
|
API_MSG_VAR_REF(msg).msg.w.vector = vectors;
|
|
|
|
API_MSG_VAR_REF(msg).msg.w.vector_cnt = vectorcnt;
|
|
|
|
API_MSG_VAR_REF(msg).msg.w.vector_off = 0;
|
2016-03-16 21:31:19 +01:00
|
|
|
API_MSG_VAR_REF(msg).msg.w.apiflags = apiflags;
|
|
|
|
API_MSG_VAR_REF(msg).msg.w.len = size;
|
2017-02-17 13:26:16 -06:00
|
|
|
API_MSG_VAR_REF(msg).msg.w.offset = 0;
|
2011-09-21 19:36:09 +02:00
|
|
|
#if LWIP_SO_SNDTIMEO
|
|
|
|
if (conn->send_timeout != 0) {
|
|
|
|
/* get the time we started, which is later compared to
|
|
|
|
sys_now() + conn->send_timeout */
|
2016-03-17 20:15:22 +01:00
|
|
|
API_MSG_VAR_REF(msg).msg.w.time_started = sys_now();
|
2011-09-21 19:36:09 +02:00
|
|
|
} else {
|
2016-03-17 20:15:22 +01:00
|
|
|
API_MSG_VAR_REF(msg).msg.w.time_started = 0;
|
2011-09-21 19:36:09 +02:00
|
|
|
}
|
|
|
|
#endif /* LWIP_SO_SNDTIMEO */
|
|
|
|
|
2007-06-21 18:40:21 +00:00
|
|
|
/* 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. */
|
2016-03-16 22:02:47 +01:00
|
|
|
err = netconn_apimsg(lwip_netconn_do_write, &API_MSG_VAR_REF(msg));
|
2017-02-24 15:32:48 -06:00
|
|
|
if (err == ERR_OK) {
|
|
|
|
if (bytes_written != NULL) {
|
2017-02-17 13:26:16 -06:00
|
|
|
*bytes_written = API_MSG_VAR_REF(msg).msg.w.offset;
|
2017-02-24 15:32:48 -06:00
|
|
|
}
|
|
|
|
/* for blocking, check all requested bytes were written, NOTE: send_timeout is
|
|
|
|
treated as dontblock (see dontblock assignment above) */
|
|
|
|
if (!dontblock) {
|
|
|
|
LWIP_ASSERT("do_write failed to write all bytes", API_MSG_VAR_REF(msg).msg.w.offset == size);
|
2011-07-22 22:05:24 +02:00
|
|
|
}
|
|
|
|
}
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_FREE(msg);
|
2010-01-17 18:28:56 +00:00
|
|
|
|
|
|
|
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
|
|
|
/**
|
2016-07-26 17:40:55 +02:00
|
|
|
* @ingroup netconn_tcp
|
2014-09-18 21:15:13 +02:00
|
|
|
* Close or shutdown a TCP netconn (doesn't delete it).
|
2007-06-16 13:57:30 +00:00
|
|
|
*
|
2010-06-24 19:33:14 +00:00
|
|
|
* @param conn the TCP netconn to close or shutdown
|
|
|
|
* @param how fully close or only shutdown one side?
|
2007-06-16 13:57:30 +00:00
|
|
|
* @return ERR_OK if the netconn was closed, any other err_t on error
|
|
|
|
*/
|
2010-06-24 19:33:14 +00:00
|
|
|
static err_t
|
|
|
|
netconn_close_shutdown(struct netconn *conn, u8_t how)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_DECLARE(msg);
|
2010-01-17 18:28:56 +00:00
|
|
|
err_t err;
|
2015-11-16 09:37:56 +01:00
|
|
|
LWIP_UNUSED_ARG(how);
|
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
|
|
|
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_ALLOC(msg);
|
2016-03-16 21:31:19 +01:00
|
|
|
API_MSG_VAR_REF(msg).conn = conn;
|
2015-11-16 09:37:56 +01:00
|
|
|
#if LWIP_TCP
|
2010-06-24 19:33:14 +00:00
|
|
|
/* shutting down both ends is the same as closing */
|
2016-03-16 21:31:19 +01:00
|
|
|
API_MSG_VAR_REF(msg).msg.sd.shut = how;
|
2015-02-10 22:15:54 +01:00
|
|
|
#if LWIP_SO_SNDTIMEO || LWIP_SO_LINGER
|
|
|
|
/* get the time we started, which is later compared to
|
|
|
|
sys_now() + conn->send_timeout */
|
2016-03-16 21:31:19 +01:00
|
|
|
API_MSG_VAR_REF(msg).msg.sd.time_started = sys_now();
|
2015-02-10 22:15:54 +01:00
|
|
|
#else /* LWIP_SO_SNDTIMEO || LWIP_SO_LINGER */
|
2016-03-16 21:31:19 +01:00
|
|
|
API_MSG_VAR_REF(msg).msg.sd.polls_left =
|
2015-02-10 22:15:54 +01:00
|
|
|
((LWIP_TCP_CLOSE_TIMEOUT_MS_DEFAULT + TCP_SLOW_INTERVAL - 1) / TCP_SLOW_INTERVAL) + 1;
|
|
|
|
#endif /* LWIP_SO_SNDTIMEO || LWIP_SO_LINGER */
|
2015-11-16 09:37:56 +01:00
|
|
|
#endif /* LWIP_TCP */
|
2016-03-16 22:02:47 +01:00
|
|
|
err = netconn_apimsg(lwip_netconn_do_close, &API_MSG_VAR_REF(msg));
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_FREE(msg);
|
2010-01-17 18:28:56 +00:00
|
|
|
|
|
|
|
return err;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2010-06-24 19:33:14 +00:00
|
|
|
/**
|
2016-07-26 17:40:55 +02:00
|
|
|
* @ingroup netconn_tcp
|
2010-06-24 19:33:14 +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
|
|
|
|
*/
|
|
|
|
err_t
|
|
|
|
netconn_close(struct netconn *conn)
|
|
|
|
{
|
|
|
|
/* shutting down both ends is the same as closing */
|
|
|
|
return netconn_close_shutdown(conn, NETCONN_SHUT_RDWR);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-26 17:40:55 +02:00
|
|
|
* @ingroup netconn_tcp
|
2010-06-24 19:33:14 +00:00
|
|
|
* Shut down one or both sides of a TCP netconn (doesn't delete it).
|
|
|
|
*
|
|
|
|
* @param conn the TCP netconn to shut down
|
2016-05-24 10:01:03 +02:00
|
|
|
* @param shut_rx shut down the RX side (no more read possible after this)
|
|
|
|
* @param shut_tx shut down the TX side (no more write possible after this)
|
2010-06-24 19:33:14 +00:00
|
|
|
* @return ERR_OK if the netconn was closed, any other err_t on error
|
|
|
|
*/
|
|
|
|
err_t
|
|
|
|
netconn_shutdown(struct netconn *conn, u8_t shut_rx, u8_t shut_tx)
|
|
|
|
{
|
|
|
|
return netconn_close_shutdown(conn, (shut_rx ? NETCONN_SHUT_RD : 0) | (shut_tx ? NETCONN_SHUT_WR : 0));
|
|
|
|
}
|
|
|
|
|
2011-05-17 19:35:14 +00:00
|
|
|
#if LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD)
|
2007-06-16 13:57:30 +00:00
|
|
|
/**
|
2016-07-26 17:40:55 +02:00
|
|
|
* @ingroup netconn_udp
|
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
|
2010-01-28 18:42:40 +00:00
|
|
|
* @param netif_addr the IP address of the network interface on which to send
|
2007-06-16 13:57:30 +00:00
|
|
|
* 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,
|
2015-02-22 21:46:35 +01:00
|
|
|
const ip_addr_t *multiaddr,
|
|
|
|
const ip_addr_t *netif_addr,
|
2007-06-16 13:57:30 +00:00
|
|
|
enum netconn_igmp join_or_leave)
|
2007-03-11 19:16:38 +00:00
|
|
|
{
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_DECLARE(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
|
|
|
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_ALLOC(msg);
|
2016-03-03 22:49:55 +01:00
|
|
|
|
2016-12-05 22:01:58 +01:00
|
|
|
#if LWIP_IPV4
|
2016-03-03 22:49:55 +01:00
|
|
|
/* Don't propagate NULL pointer (IP_ADDR_ANY alias) to subsequent functions */
|
2014-02-20 19:50:17 +01:00
|
|
|
if (multiaddr == NULL) {
|
2016-09-28 12:56:39 +02:00
|
|
|
multiaddr = IP4_ADDR_ANY;
|
2014-02-20 19:50:17 +01:00
|
|
|
}
|
|
|
|
if (netif_addr == NULL) {
|
2016-09-28 12:56:39 +02:00
|
|
|
netif_addr = IP4_ADDR_ANY;
|
2014-02-20 19:50:17 +01:00
|
|
|
}
|
2016-12-05 22:01:58 +01:00
|
|
|
#endif /* LWIP_IPV4 */
|
2016-03-03 22:49:55 +01:00
|
|
|
|
2016-03-16 21:31:19 +01:00
|
|
|
API_MSG_VAR_REF(msg).conn = conn;
|
|
|
|
API_MSG_VAR_REF(msg).msg.jl.multiaddr = API_MSG_VAR_REF(multiaddr);
|
|
|
|
API_MSG_VAR_REF(msg).msg.jl.netif_addr = API_MSG_VAR_REF(netif_addr);
|
|
|
|
API_MSG_VAR_REF(msg).msg.jl.join_or_leave = join_or_leave;
|
2016-03-16 22:02:47 +01:00
|
|
|
err = netconn_apimsg(lwip_netconn_do_join_leave_group, &API_MSG_VAR_REF(msg));
|
2014-02-20 19:50:17 +01:00
|
|
|
API_MSG_VAR_FREE(msg);
|
2010-01-17 18:28:56 +00:00
|
|
|
|
|
|
|
return err;
|
2007-03-11 19:16:38 +00:00
|
|
|
}
|
2011-05-17 19:35:14 +00:00
|
|
|
#endif /* LWIP_IGMP || (LWIP_IPV6 && LWIP_IPV6_MLD) */
|
2007-03-11 19:16:38 +00:00
|
|
|
|
2007-11-16 17:16:17 +00:00
|
|
|
#if LWIP_DNS
|
|
|
|
/**
|
2016-07-27 08:05:38 +02:00
|
|
|
* @ingroup netconn_common
|
2007-11-16 17:16:17 +00:00
|
|
|
* Execute a DNS query, only one IP address is returned
|
|
|
|
*
|
|
|
|
* @param name a string representation of the DNS host name to query
|
2010-02-04 18:47:44 +00:00
|
|
|
* @param addr a preallocated ip_addr_t where to store the resolved IP address
|
2016-06-22 20:18:23 +02:00
|
|
|
* @param dns_addrtype IP address type (IPv4 / IPv6)
|
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
|
|
|
*/
|
2015-09-04 13:57:20 +02:00
|
|
|
#if LWIP_IPV4 && LWIP_IPV6
|
|
|
|
err_t
|
|
|
|
netconn_gethostbyname_addrtype(const char *name, ip_addr_t *addr, u8_t dns_addrtype)
|
|
|
|
#else
|
2007-11-16 17:16:17 +00:00
|
|
|
err_t
|
2010-02-04 18:47:44 +00:00
|
|
|
netconn_gethostbyname(const char *name, ip_addr_t *addr)
|
2015-09-04 13:57:20 +02:00
|
|
|
#endif
|
2007-11-16 17:16:17 +00:00
|
|
|
{
|
2014-02-20 19:50:17 +01:00
|
|
|
API_VAR_DECLARE(struct dns_api_msg, msg);
|
|
|
|
#if !LWIP_MPU_COMPATIBLE
|
2007-11-16 17:16:17 +00:00
|
|
|
sys_sem_t sem;
|
2014-02-20 19:50:17 +01:00
|
|
|
#endif /* LWIP_MPU_COMPATIBLE */
|
|
|
|
err_t err;
|
2016-12-16 15:44:00 +01:00
|
|
|
err_t cberr;
|
2007-11-16 17:16:17 +00:00
|
|
|
|
|
|
|
LWIP_ERROR("netconn_gethostbyname: invalid name", (name != NULL), return ERR_ARG;);
|
|
|
|
LWIP_ERROR("netconn_gethostbyname: invalid addr", (addr != NULL), return ERR_ARG;);
|
2014-09-02 21:40:05 +02:00
|
|
|
#if LWIP_MPU_COMPATIBLE
|
2014-12-10 20:45:01 +01:00
|
|
|
if (strlen(name) >= DNS_MAX_NAME_LENGTH) {
|
2014-09-02 21:40:05 +02:00
|
|
|
return ERR_ARG;
|
|
|
|
}
|
|
|
|
#endif
|
2007-11-16 17:16:17 +00:00
|
|
|
|
2016-04-25 21:00:28 +02:00
|
|
|
API_VAR_ALLOC(struct dns_api_msg, MEMP_DNS_API_MSG, msg, ERR_MEM);
|
2014-02-20 19:50:17 +01:00
|
|
|
#if LWIP_MPU_COMPATIBLE
|
2014-02-21 09:04:39 +01:00
|
|
|
strncpy(API_VAR_REF(msg).name, name, DNS_MAX_NAME_LENGTH-1);
|
|
|
|
API_VAR_REF(msg).name[DNS_MAX_NAME_LENGTH-1] = 0;
|
|
|
|
#else /* LWIP_MPU_COMPATIBLE */
|
2014-02-20 19:50:17 +01:00
|
|
|
msg.err = &err;
|
|
|
|
msg.sem = &sem;
|
|
|
|
API_VAR_REF(msg).addr = API_VAR_REF(addr);
|
2014-02-21 09:04:39 +01:00
|
|
|
API_VAR_REF(msg).name = name;
|
2014-02-20 19:50:17 +01:00
|
|
|
#endif /* LWIP_MPU_COMPATIBLE */
|
2015-09-04 13:57:20 +02:00
|
|
|
#if LWIP_IPV4 && LWIP_IPV6
|
|
|
|
API_VAR_REF(msg).dns_addrtype = dns_addrtype;
|
2015-09-15 08:19:26 +02:00
|
|
|
#endif /* LWIP_IPV4 && LWIP_IPV6 */
|
2015-09-04 13:57:20 +02:00
|
|
|
#if LWIP_NETCONN_SEM_PER_THREAD
|
|
|
|
API_VAR_REF(msg).sem = LWIP_NETCONN_THREAD_SEM_GET();
|
|
|
|
#else /* LWIP_NETCONN_SEM_PER_THREAD*/
|
2014-02-20 19:50:17 +01:00
|
|
|
err = sys_sem_new(API_EXPR_REF(API_VAR_REF(msg).sem), 0);
|
2010-02-13 15:43:19 +00:00
|
|
|
if (err != ERR_OK) {
|
2014-02-20 19:50:17 +01:00
|
|
|
API_VAR_FREE(MEMP_DNS_API_MSG, msg);
|
2010-02-13 15:43:19 +00:00
|
|
|
return err;
|
2007-11-16 17:16:17 +00:00
|
|
|
}
|
2015-09-04 13:57:20 +02:00
|
|
|
#endif /* LWIP_NETCONN_SEM_PER_THREAD */
|
2007-11-16 17:16:17 +00:00
|
|
|
|
2016-12-16 15:44:00 +01:00
|
|
|
cberr = tcpip_callback(lwip_netconn_do_gethostbyname, &API_VAR_REF(msg));
|
|
|
|
if (cberr != ERR_OK) {
|
2015-12-01 18:59:04 +08:00
|
|
|
#if !LWIP_NETCONN_SEM_PER_THREAD
|
|
|
|
sys_sem_free(API_EXPR_REF(API_VAR_REF(msg).sem));
|
|
|
|
#endif /* !LWIP_NETCONN_SEM_PER_THREAD */
|
|
|
|
API_VAR_FREE(MEMP_DNS_API_MSG, msg);
|
2016-12-16 15:44:00 +01:00
|
|
|
return cberr;
|
2015-12-01 18:59:04 +08:00
|
|
|
}
|
2015-10-09 21:00:21 +02:00
|
|
|
sys_sem_wait(API_EXPR_REF_SEM(API_VAR_REF(msg).sem));
|
2015-09-04 13:57:20 +02:00
|
|
|
#if !LWIP_NETCONN_SEM_PER_THREAD
|
2014-02-20 19:50:17 +01:00
|
|
|
sys_sem_free(API_EXPR_REF(API_VAR_REF(msg).sem));
|
2015-09-04 13:57:20 +02:00
|
|
|
#endif /* !LWIP_NETCONN_SEM_PER_THREAD */
|
2007-11-16 17:16:17 +00:00
|
|
|
|
2014-02-20 19:50:17 +01:00
|
|
|
#if LWIP_MPU_COMPATIBLE
|
|
|
|
*addr = msg->addr;
|
|
|
|
err = msg->err;
|
|
|
|
#endif /* LWIP_MPU_COMPATIBLE */
|
2007-11-16 17:16:17 +00:00
|
|
|
|
2014-02-20 19:50:17 +01:00
|
|
|
API_VAR_FREE(MEMP_DNS_API_MSG, msg);
|
2007-11-16 17:16:17 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
#endif /* LWIP_DNS*/
|
|
|
|
|
2014-12-10 20:45:01 +01:00
|
|
|
#if LWIP_NETCONN_SEM_PER_THREAD
|
2015-10-06 21:57:40 +02:00
|
|
|
void
|
|
|
|
netconn_thread_init(void)
|
2014-12-10 20:45:01 +01:00
|
|
|
{
|
|
|
|
sys_sem_t *sem = LWIP_NETCONN_THREAD_SEM_GET();
|
2015-08-19 09:57:59 +02:00
|
|
|
if ((sem == NULL) || !sys_sem_valid(sem)) {
|
2014-12-10 20:45:01 +01:00
|
|
|
/* call alloc only once */
|
|
|
|
LWIP_NETCONN_THREAD_SEM_ALLOC();
|
2015-08-18 20:29:16 +02:00
|
|
|
LWIP_ASSERT("LWIP_NETCONN_THREAD_SEM_ALLOC() failed", sys_sem_valid(LWIP_NETCONN_THREAD_SEM_GET()));
|
2014-12-10 20:45:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-06 21:57:40 +02:00
|
|
|
void
|
|
|
|
netconn_thread_cleanup(void)
|
2014-12-10 20:45:01 +01:00
|
|
|
{
|
|
|
|
sys_sem_t *sem = LWIP_NETCONN_THREAD_SEM_GET();
|
2015-08-19 09:57:59 +02:00
|
|
|
if ((sem != NULL) && sys_sem_valid(sem)) {
|
2014-12-10 20:45:01 +01:00
|
|
|
/* call free only once */
|
|
|
|
LWIP_NETCONN_THREAD_SEM_FREE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* LWIP_NETCONN_SEM_PER_THREAD */
|
|
|
|
|
2007-09-07 23:01:59 +00:00
|
|
|
#endif /* LWIP_NETCONN */
|