2007-08-09 22:21:44 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Sequential API Internal 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>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
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_msg.h"
|
2007-11-06 20:31:28 +00:00
|
|
|
|
|
|
|
#include "lwip/ip.h"
|
|
|
|
#include "lwip/udp.h"
|
|
|
|
#include "lwip/tcp.h"
|
|
|
|
#include "lwip/raw.h"
|
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
#include "lwip/memp.h"
|
|
|
|
#include "lwip/tcpip.h"
|
2007-03-11 19:16:38 +00:00
|
|
|
#include "lwip/igmp.h"
|
2007-11-16 17:16:17 +00:00
|
|
|
#include "lwip/dns.h"
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2009-07-27 15:13:24 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2010-02-09 18:41:07 +00:00
|
|
|
#define SET_NONBLOCKING_CONNECT(conn, val) do { if(val) { \
|
|
|
|
(conn)->flags |= NETCONN_FLAG_IN_NONBLOCKING_CONNECT; \
|
|
|
|
} else { \
|
|
|
|
(conn)->flags &= ~ NETCONN_FLAG_IN_NONBLOCKING_CONNECT; }} while(0)
|
|
|
|
#define IN_NONBLOCKING_CONNECT(conn) (((conn)->flags & NETCONN_FLAG_IN_NONBLOCKING_CONNECT) != 0)
|
|
|
|
|
2007-06-21 18:40:21 +00:00
|
|
|
/* forward declarations */
|
|
|
|
#if LWIP_TCP
|
|
|
|
static err_t do_writemore(struct netconn *conn);
|
2007-07-03 19:29:59 +00:00
|
|
|
static void do_close_internal(struct netconn *conn);
|
2007-06-21 18:40:21 +00:00
|
|
|
#endif
|
|
|
|
|
2003-11-14 13:17:23 +00:00
|
|
|
#if LWIP_RAW
|
2007-06-16 15:07:11 +00:00
|
|
|
/**
|
|
|
|
* Receive callback function for RAW netconns.
|
|
|
|
* Doesn't 'eat' the packet, only references it and sends it to
|
|
|
|
* conn->recvmbox
|
|
|
|
*
|
|
|
|
* @see raw.h (struct raw_pcb.recv) for parameters and return value
|
|
|
|
*/
|
2004-07-21 08:55:26 +00:00
|
|
|
static u8_t
|
2003-11-14 13:17:23 +00:00
|
|
|
recv_raw(void *arg, struct raw_pcb *pcb, struct pbuf *p,
|
2010-02-04 18:47:44 +00:00
|
|
|
ip_addr_t *addr)
|
2003-11-14 13:17:23 +00:00
|
|
|
{
|
2008-03-12 10:55:54 +00:00
|
|
|
struct pbuf *q;
|
2003-11-14 13:17:23 +00:00
|
|
|
struct netbuf *buf;
|
|
|
|
struct netconn *conn;
|
|
|
|
|
2008-03-13 19:57:11 +00:00
|
|
|
LWIP_UNUSED_ARG(addr);
|
2010-01-25 08:24:30 +00:00
|
|
|
conn = (struct netconn *)arg;
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2010-02-13 10:06:42 +00:00
|
|
|
if ((conn != NULL) && sys_mbox_valid(&conn->recvmbox)) {
|
2007-11-02 10:37:08 +00:00
|
|
|
#if LWIP_SO_RCVBUF
|
2010-02-13 10:06:42 +00:00
|
|
|
int recv_avail;
|
|
|
|
SYS_ARCH_GET(conn->recv_avail, recv_avail);
|
|
|
|
if ((recv_avail + (int)(p->tot_len)) > conn->recv_bufsize) {
|
|
|
|
return 0;
|
|
|
|
}
|
2007-11-02 10:37:08 +00:00
|
|
|
#endif /* LWIP_SO_RCVBUF */
|
2008-03-12 10:55:54 +00:00
|
|
|
/* copy the whole packet into new pbufs */
|
|
|
|
q = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
|
|
|
|
if(q != NULL) {
|
|
|
|
if (pbuf_copy(q, p) != ERR_OK) {
|
|
|
|
pbuf_free(q);
|
|
|
|
q = NULL;
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
}
|
|
|
|
|
2010-01-29 22:09:31 +00:00
|
|
|
if (q != NULL) {
|
|
|
|
u16_t len;
|
2010-01-25 08:24:30 +00:00
|
|
|
buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
|
2008-03-12 10:55:54 +00:00
|
|
|
if (buf == NULL) {
|
|
|
|
pbuf_free(q);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf->p = q;
|
|
|
|
buf->ptr = q;
|
2008-03-12 12:58:07 +00:00
|
|
|
buf->addr = &(((struct ip_hdr*)(q->payload))->src);
|
2008-03-12 10:55:54 +00:00
|
|
|
buf->port = pcb->protocol;
|
|
|
|
|
2010-01-29 22:09:31 +00:00
|
|
|
len = q->tot_len;
|
2010-02-12 13:49:21 +00:00
|
|
|
if (sys_mbox_trypost(&conn->recvmbox, buf) != ERR_OK) {
|
2008-03-12 10:55:54 +00:00
|
|
|
netbuf_delete(buf);
|
2009-07-09 09:35:07 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
2010-01-29 22:09:31 +00:00
|
|
|
SYS_ARCH_INC(conn->recv_avail, len);
|
2009-07-09 09:35:07 +00:00
|
|
|
/* Register event with callback */
|
2010-01-29 22:09:31 +00:00
|
|
|
API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
|
2008-03-12 10:55:54 +00:00
|
|
|
}
|
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
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0; /* do not eat the packet */
|
|
|
|
}
|
2007-03-30 09:42:16 +00:00
|
|
|
#endif /* LWIP_RAW*/
|
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
#if LWIP_UDP
|
2007-06-16 15:07:11 +00:00
|
|
|
/**
|
|
|
|
* Receive callback function for UDP netconns.
|
|
|
|
* Posts the packet to conn->recvmbox or deletes it on memory error.
|
|
|
|
*
|
|
|
|
* @see udp.h (struct udp_pcb.recv) for parameters
|
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
static void
|
|
|
|
recv_udp(void *arg, struct udp_pcb *pcb, struct pbuf *p,
|
2010-02-04 18:47:44 +00:00
|
|
|
ip_addr_t *addr, u16_t port)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
|
|
|
struct netbuf *buf;
|
|
|
|
struct netconn *conn;
|
2010-01-29 22:09:31 +00:00
|
|
|
u16_t len;
|
2007-12-21 14:59:10 +00:00
|
|
|
#if LWIP_SO_RCVBUF
|
|
|
|
int recv_avail;
|
|
|
|
#endif /* LWIP_SO_RCVBUF */
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2007-11-30 12:54:10 +00:00
|
|
|
LWIP_UNUSED_ARG(pcb); /* only used for asserts... */
|
|
|
|
LWIP_ASSERT("recv_udp must have a pcb argument", pcb != NULL);
|
|
|
|
LWIP_ASSERT("recv_udp must have an argument", arg != NULL);
|
2010-01-25 08:24:30 +00:00
|
|
|
conn = (struct netconn *)arg;
|
2007-11-30 12:54:10 +00:00
|
|
|
LWIP_ASSERT("recv_udp: recv for wrong pcb!", conn->pcb.udp == pcb);
|
2007-05-19 16:24:40 +00:00
|
|
|
|
2007-11-02 10:37:08 +00:00
|
|
|
#if LWIP_SO_RCVBUF
|
2007-12-21 14:59:10 +00:00
|
|
|
SYS_ARCH_GET(conn->recv_avail, recv_avail);
|
2010-02-12 13:49:21 +00:00
|
|
|
if ((conn == NULL) || !sys_mbox_valid(&conn->recvmbox) ||
|
2007-12-21 14:59:10 +00:00
|
|
|
((recv_avail + (int)(p->tot_len)) > conn->recv_bufsize)) {
|
2007-11-02 10:37:08 +00:00
|
|
|
#else /* LWIP_SO_RCVBUF */
|
2010-02-13 10:06:42 +00:00
|
|
|
if ((conn == NULL) || !sys_mbox_valid(&conn->recvmbox)) {
|
2007-11-02 10:37:08 +00:00
|
|
|
#endif /* LWIP_SO_RCVBUF */
|
2002-10-19 12:59:30 +00:00
|
|
|
pbuf_free(p);
|
|
|
|
return;
|
|
|
|
}
|
2007-05-19 16:24:40 +00:00
|
|
|
|
2010-01-25 08:24:30 +00:00
|
|
|
buf = (struct netbuf *)memp_malloc(MEMP_NETBUF);
|
2007-07-13 14:28:15 +00:00
|
|
|
if (buf == NULL) {
|
|
|
|
pbuf_free(p);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
buf->p = p;
|
|
|
|
buf->ptr = p;
|
|
|
|
buf->addr = addr;
|
|
|
|
buf->port = port;
|
2009-10-07 17:58:30 +00:00
|
|
|
#if LWIP_NETBUF_RECVINFO
|
|
|
|
{
|
|
|
|
const struct ip_hdr* iphdr = ip_current_header();
|
|
|
|
/* get the UDP header - always in the first pbuf, ensured by udp_input */
|
|
|
|
const struct udp_hdr* udphdr = (void*)(((char*)iphdr) + IPH_LEN(iphdr));
|
2010-02-04 18:47:44 +00:00
|
|
|
buf->toaddr = (ip_addr_t*)&iphdr->dest;
|
2009-10-07 17:58:30 +00:00
|
|
|
buf->toport = udphdr->dest;
|
|
|
|
}
|
|
|
|
#endif /* LWIP_NETBUF_RECVINFO */
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2007-07-13 14:28:15 +00:00
|
|
|
|
2010-01-29 22:09:31 +00:00
|
|
|
len = p->tot_len;
|
2010-02-12 13:49:21 +00:00
|
|
|
if (sys_mbox_trypost(&conn->recvmbox, buf) != ERR_OK) {
|
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
|
|
|
netbuf_delete(buf);
|
|
|
|
return;
|
2009-07-09 09:35:07 +00:00
|
|
|
} else {
|
2010-01-29 22:09:31 +00:00
|
|
|
SYS_ARCH_INC(conn->recv_avail, len);
|
2009-07-09 09:35:07 +00:00
|
|
|
/* Register event with callback */
|
2010-01-29 22:09:31 +00:00
|
|
|
API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
|
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
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
#endif /* LWIP_UDP */
|
2007-03-30 09:42:16 +00:00
|
|
|
|
2003-03-21 10:48:21 +00:00
|
|
|
#if LWIP_TCP
|
2007-06-16 15:07:11 +00:00
|
|
|
/**
|
|
|
|
* Receive callback function for TCP netconns.
|
2008-01-14 21:07:08 +00:00
|
|
|
* Posts the packet to conn->recvmbox, but doesn't delete it on errors.
|
2007-06-16 15:07:11 +00:00
|
|
|
*
|
|
|
|
* @see tcp.h (struct tcp_pcb.recv) for parameters and return value
|
|
|
|
*/
|
2003-03-21 10:48:21 +00:00
|
|
|
static err_t
|
|
|
|
recv_tcp(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
|
|
|
|
{
|
|
|
|
struct netconn *conn;
|
|
|
|
u16_t len;
|
2007-05-19 16:24:40 +00:00
|
|
|
|
2007-04-04 19:54:22 +00:00
|
|
|
LWIP_UNUSED_ARG(pcb);
|
2007-11-30 12:54:10 +00:00
|
|
|
LWIP_ASSERT("recv_tcp must have a pcb argument", pcb != NULL);
|
|
|
|
LWIP_ASSERT("recv_tcp must have an argument", arg != NULL);
|
2010-01-25 08:24:30 +00:00
|
|
|
conn = (struct netconn *)arg;
|
2007-11-30 12:54:10 +00:00
|
|
|
LWIP_ASSERT("recv_tcp: recv for wrong pcb!", conn->pcb.tcp == pcb);
|
2003-03-21 10:48:21 +00:00
|
|
|
|
2010-01-08 15:10:03 +00:00
|
|
|
if (conn == NULL) {
|
2003-03-21 10:48:21 +00:00
|
|
|
return ERR_VAL;
|
|
|
|
}
|
2010-02-12 13:49:21 +00:00
|
|
|
if (!sys_mbox_valid(&conn->recvmbox)) {
|
2010-01-08 15:10:03 +00:00
|
|
|
/* recvmbox already deleted */
|
|
|
|
if (p != NULL) {
|
|
|
|
tcp_recved(pcb, p->tot_len);
|
|
|
|
pbuf_free(p);
|
|
|
|
}
|
|
|
|
return ERR_OK;
|
|
|
|
}
|
2010-01-29 22:09:31 +00:00
|
|
|
/* Unlike for UDP or RAW pcbs, don't check for available space
|
|
|
|
using recv_avail since that could break the connection
|
|
|
|
(data is already ACKed) */
|
2003-03-21 10:48:21 +00:00
|
|
|
|
2010-01-17 18:28:56 +00:00
|
|
|
/* don't overwrite fatal errors! */
|
|
|
|
NETCONN_SET_SAFE_ERR(conn, err);
|
|
|
|
|
2007-07-13 14:28:15 +00:00
|
|
|
if (p != NULL) {
|
|
|
|
len = p->tot_len;
|
|
|
|
} else {
|
|
|
|
len = 0;
|
|
|
|
}
|
2009-07-09 09:35:07 +00:00
|
|
|
|
2010-02-12 13:49:21 +00:00
|
|
|
if (sys_mbox_trypost(&conn->recvmbox, p) != ERR_OK) {
|
2010-01-29 22:09:31 +00:00
|
|
|
/* don't deallocate p: it is presented to us later again from tcp_fasttmr! */
|
2008-01-14 21:07:08 +00:00
|
|
|
return ERR_MEM;
|
2009-07-09 09:35:07 +00:00
|
|
|
} else {
|
2010-01-29 22:09:31 +00:00
|
|
|
SYS_ARCH_INC(conn->recv_avail, len);
|
2009-07-09 09:35:07 +00:00
|
|
|
/* Register event with callback */
|
|
|
|
API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
|
2008-01-14 21:07:08 +00:00
|
|
|
}
|
2007-07-13 14:28:15 +00:00
|
|
|
|
2003-03-21 10:48:21 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
|
2007-06-16 15:07:11 +00:00
|
|
|
/**
|
|
|
|
* Poll callback function for TCP netconns.
|
|
|
|
* Wakes up an application thread that waits for a connection to close
|
|
|
|
* or data to be sent. The application thread then takes the
|
|
|
|
* appropriate action to go on.
|
|
|
|
*
|
|
|
|
* Signals the conn->sem.
|
|
|
|
* netconn_close waits for conn->sem if closing failed.
|
|
|
|
*
|
|
|
|
* @see tcp.h (struct tcp_pcb.poll) for parameters and return value
|
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
static err_t
|
|
|
|
poll_tcp(void *arg, struct tcp_pcb *pcb)
|
|
|
|
{
|
2010-01-25 08:24:30 +00:00
|
|
|
struct netconn *conn = (struct netconn *)arg;
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2007-04-04 19:54:22 +00:00
|
|
|
LWIP_UNUSED_ARG(pcb);
|
2007-06-21 18:40:21 +00:00
|
|
|
LWIP_ASSERT("conn != NULL", (conn != NULL));
|
2007-05-19 16:24:40 +00:00
|
|
|
|
2007-06-21 18:40:21 +00:00
|
|
|
if (conn->state == NETCONN_WRITE) {
|
|
|
|
do_writemore(conn);
|
2007-07-03 19:29:59 +00:00
|
|
|
} else if (conn->state == NETCONN_CLOSE) {
|
|
|
|
do_close_internal(conn);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2010-01-29 22:09:31 +00:00
|
|
|
/* @todo: implement connect timeout here? */
|
2007-06-21 18:40:21 +00:00
|
|
|
|
2010-02-13 17:08:40 +00:00
|
|
|
/* Did a nonblocking write fail before? Then check available write-space. */
|
|
|
|
if (conn->flags & NETCONN_FLAG_CHECK_WRITESPACE) {
|
|
|
|
/* If the queued byte- or pbuf-count drops below the configured low-water limit,
|
|
|
|
let select mark this pcb as writable again. */
|
|
|
|
if ((conn->pcb.tcp != NULL) && (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT) &&
|
|
|
|
(tcp_sndqueuelen(conn->pcb.tcp) < TCP_SNDQUEUELOWAT)) {
|
|
|
|
conn->flags &= ~NETCONN_FLAG_CHECK_WRITESPACE;
|
|
|
|
API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-06-16 15:07:11 +00:00
|
|
|
/**
|
|
|
|
* Sent callback function for TCP netconns.
|
2007-10-24 12:09:18 +00:00
|
|
|
* Signals the conn->sem and calls API_EVENT.
|
2007-06-16 15:07:11 +00:00
|
|
|
* netconn_write waits for conn->sem if send buffer is low.
|
|
|
|
*
|
|
|
|
* @see tcp.h (struct tcp_pcb.sent) for parameters and return value
|
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
static err_t
|
|
|
|
sent_tcp(void *arg, struct tcp_pcb *pcb, u16_t len)
|
|
|
|
{
|
2010-01-25 08:24:30 +00:00
|
|
|
struct netconn *conn = (struct netconn *)arg;
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2007-04-04 19:54:22 +00:00
|
|
|
LWIP_UNUSED_ARG(pcb);
|
2007-06-21 18:40:21 +00:00
|
|
|
LWIP_ASSERT("conn != NULL", (conn != NULL));
|
2007-05-19 16:24:40 +00:00
|
|
|
|
2007-06-21 18:40:21 +00:00
|
|
|
if (conn->state == NETCONN_WRITE) {
|
|
|
|
do_writemore(conn);
|
2007-07-03 19:29:59 +00:00
|
|
|
} else if (conn->state == NETCONN_CLOSE) {
|
|
|
|
do_close_internal(conn);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-02-06 22:18:56 +00:00
|
|
|
|
2007-10-24 12:09:18 +00:00
|
|
|
if (conn) {
|
2010-01-27 18:24:57 +00:00
|
|
|
/* If the queued byte- or pbuf-count drops below the configured low-water limit,
|
|
|
|
let select mark this pcb as writable again. */
|
|
|
|
if ((conn->pcb.tcp != NULL) && (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT) &&
|
|
|
|
(tcp_sndqueuelen(conn->pcb.tcp) < TCP_SNDQUEUELOWAT)) {
|
2010-02-13 17:08:40 +00:00
|
|
|
conn->flags &= ~NETCONN_FLAG_CHECK_WRITESPACE;
|
2007-10-24 12:09:18 +00:00
|
|
|
API_EVENT(conn, NETCONN_EVT_SENDPLUS, len);
|
2007-07-25 19:24:27 +00:00
|
|
|
}
|
|
|
|
}
|
2003-02-06 22:18: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 15:07:11 +00:00
|
|
|
/**
|
|
|
|
* Error callback function for TCP netconns.
|
2007-10-24 12:09:18 +00:00
|
|
|
* Signals conn->sem, posts to all conn mboxes and calls API_EVENT.
|
2007-06-16 15:07:11 +00:00
|
|
|
* The application thread has then to decide what to do.
|
|
|
|
*
|
|
|
|
* @see tcp.h (struct tcp_pcb.err) for parameters
|
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
static void
|
|
|
|
err_tcp(void *arg, err_t err)
|
|
|
|
{
|
|
|
|
struct netconn *conn;
|
2010-01-17 18:28:56 +00:00
|
|
|
enum netconn_state old_state;
|
2010-01-19 18:34:01 +00:00
|
|
|
SYS_ARCH_DECL_PROTECT(lev);
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2010-01-25 08:24:30 +00:00
|
|
|
conn = (struct netconn *)arg;
|
2007-06-21 18:40:21 +00:00
|
|
|
LWIP_ASSERT("conn != NULL", (conn != NULL));
|
2002-10-19 12:59:30 +00:00
|
|
|
|
|
|
|
conn->pcb.tcp = NULL;
|
|
|
|
|
2010-01-29 23:05:33 +00:00
|
|
|
/* no check since this is always fatal! */
|
2010-01-19 18:34:01 +00:00
|
|
|
SYS_ARCH_PROTECT(lev);
|
2010-01-17 18:28:56 +00:00
|
|
|
conn->last_err = err;
|
2010-01-19 18:34:01 +00:00
|
|
|
SYS_ARCH_UNPROTECT(lev);
|
2010-01-17 18:28:56 +00:00
|
|
|
|
2010-01-29 22:09:31 +00:00
|
|
|
/* reset conn->state now before waking up other threads */
|
2010-01-17 18:28:56 +00:00
|
|
|
old_state = conn->state;
|
|
|
|
conn->state = NETCONN_NONE;
|
|
|
|
|
2010-01-29 23:05:33 +00:00
|
|
|
/* Notify the user layer about a connection error. Used to signal
|
|
|
|
select. */
|
|
|
|
API_EVENT(conn, NETCONN_EVT_ERROR, 0);
|
|
|
|
/* Try to release selects pending on 'read' or 'write', too.
|
|
|
|
They will get an error if they actually try to read or write. */
|
|
|
|
API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
|
|
|
|
API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
|
|
|
|
|
|
|
|
/* pass NULL-message to recvmbox to wake up pending recv */
|
2010-02-12 13:49:21 +00:00
|
|
|
if (sys_mbox_valid(&conn->recvmbox)) {
|
2010-01-29 23:05:33 +00:00
|
|
|
/* use trypost to prevent deadlock */
|
2010-02-12 13:49:21 +00:00
|
|
|
sys_mbox_trypost(&conn->recvmbox, NULL);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2010-01-29 23:05:33 +00:00
|
|
|
/* pass NULL-message to acceptmbox to wake up pending accept */
|
2010-02-12 13:49:21 +00:00
|
|
|
if (sys_mbox_valid(&conn->acceptmbox)) {
|
2010-01-29 23:05:33 +00:00
|
|
|
/* use trypost to preven deadlock */
|
2010-02-12 13:49:21 +00:00
|
|
|
sys_mbox_trypost(&conn->acceptmbox, NULL);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2010-01-29 23:05:33 +00:00
|
|
|
|
2010-01-17 18:28:56 +00:00
|
|
|
if ((old_state == NETCONN_WRITE) || (old_state == NETCONN_CLOSE) ||
|
|
|
|
(old_state == NETCONN_CONNECT)) {
|
2007-07-03 19:29:59 +00:00
|
|
|
/* calling do_writemore/do_close_internal is not necessary
|
2007-06-21 18:40:21 +00:00
|
|
|
since the pcb has already been deleted! */
|
2010-02-09 18:41:07 +00:00
|
|
|
int was_nonblocking_connect = IN_NONBLOCKING_CONNECT(conn);
|
|
|
|
SET_NONBLOCKING_CONNECT(conn, 0);
|
2010-01-29 22:09:31 +00:00
|
|
|
|
|
|
|
if (!was_nonblocking_connect) {
|
|
|
|
/* set error return code */
|
|
|
|
LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL);
|
|
|
|
conn->current_msg->err = err;
|
|
|
|
conn->current_msg = NULL;
|
|
|
|
/* wake up the waiting task */
|
2010-02-12 13:49:21 +00:00
|
|
|
sys_sem_signal(&conn->op_completed);
|
2010-01-29 22:09:31 +00:00
|
|
|
}
|
2010-01-17 18:28:56 +00:00
|
|
|
} else {
|
|
|
|
LWIP_ASSERT("conn->current_msg == NULL", conn->current_msg == NULL);
|
2007-07-03 19:29:59 +00:00
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-06-16 15:07:11 +00:00
|
|
|
/**
|
|
|
|
* Setup a tcp_pcb with the correct callback function pointers
|
|
|
|
* and their arguments.
|
|
|
|
*
|
|
|
|
* @param conn the TCP netconn to setup
|
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
static void
|
|
|
|
setup_tcp(struct netconn *conn)
|
|
|
|
{
|
|
|
|
struct tcp_pcb *pcb;
|
2007-05-19 16:24:40 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
pcb = conn->pcb.tcp;
|
|
|
|
tcp_arg(pcb, conn);
|
|
|
|
tcp_recv(pcb, recv_tcp);
|
|
|
|
tcp_sent(pcb, sent_tcp);
|
|
|
|
tcp_poll(pcb, poll_tcp, 4);
|
|
|
|
tcp_err(pcb, err_tcp);
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-06-16 15:07:11 +00:00
|
|
|
/**
|
|
|
|
* Accept callback function for TCP netconns.
|
|
|
|
* Allocates a new netconn and posts that to conn->acceptmbox.
|
|
|
|
*
|
|
|
|
* @see tcp.h (struct tcp_pcb_listen.accept) for parameters and return value
|
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
static err_t
|
|
|
|
accept_function(void *arg, struct tcp_pcb *newpcb, err_t err)
|
|
|
|
{
|
|
|
|
struct netconn *newconn;
|
2010-02-21 10:56:00 +00:00
|
|
|
struct netconn *conn = (struct netconn *)arg;
|
2007-05-19 16:24:40 +00:00
|
|
|
|
2010-02-21 10:56:00 +00:00
|
|
|
LWIP_DEBUGF(API_MSG_DEBUG, ("accept_function: newpcb->tate: %s\n", tcp_debug_state_str(newpcb->state)));
|
2007-06-16 15:07:11 +00:00
|
|
|
|
2010-02-12 13:49:21 +00:00
|
|
|
if (!sys_mbox_valid(&conn->acceptmbox)) {
|
2010-01-08 15:10:03 +00:00
|
|
|
LWIP_DEBUGF(API_MSG_DEBUG, ("accept_function: acceptmbox already deleted\n"));
|
|
|
|
return ERR_VAL;
|
|
|
|
}
|
2007-06-16 15:07:11 +00:00
|
|
|
|
2007-11-30 12:54:10 +00:00
|
|
|
/* We have to set the callback here even though
|
|
|
|
* the new socket is unknown. conn->socket is marked as -1. */
|
2008-01-04 23:07:44 +00:00
|
|
|
newconn = netconn_alloc(conn->type, conn->callback);
|
2003-05-01 13:24:01 +00:00
|
|
|
if (newconn == NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_MEM;
|
|
|
|
}
|
2006-06-14 08:02:35 +00:00
|
|
|
newconn->pcb.tcp = newpcb;
|
|
|
|
setup_tcp(newconn);
|
2010-01-17 18:28:56 +00:00
|
|
|
/* no protection: when creating the pcb, the netconn is not yet known
|
|
|
|
to the application thread */
|
|
|
|
newconn->last_err = err;
|
2007-05-19 16:24:40 +00:00
|
|
|
|
2010-02-12 13:49:21 +00:00
|
|
|
if (sys_mbox_trypost(&conn->acceptmbox, newconn) != ERR_OK) {
|
2010-01-29 22:09:31 +00:00
|
|
|
/* When returning != ERR_OK, the pcb is aborted in tcp_process(),
|
2008-01-06 14:28:04 +00:00
|
|
|
so do nothing here! */
|
|
|
|
newconn->pcb.tcp = NULL;
|
2010-01-13 15:59:47 +00:00
|
|
|
/* no need to drain since we know the recvmbox is empty. */
|
2010-02-12 13:49:21 +00:00
|
|
|
sys_mbox_free(&newconn->recvmbox);
|
|
|
|
sys_mbox_set_invalid(&newconn->recvmbox);
|
2008-01-06 14:28:04 +00:00
|
|
|
netconn_free(newconn);
|
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
|
|
|
return ERR_MEM;
|
2009-07-09 09:35:07 +00:00
|
|
|
} else {
|
|
|
|
/* Register event with callback */
|
|
|
|
API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
|
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
|
|
|
}
|
2009-07-09 09:35:07 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
2003-03-21 10:48:21 +00:00
|
|
|
#endif /* LWIP_TCP */
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-06-16 15:07:11 +00:00
|
|
|
/**
|
|
|
|
* Create a new pcb of a specific type.
|
|
|
|
* Called from do_newconn().
|
|
|
|
*
|
|
|
|
* @param msg the api_msg_msg describing the connection type
|
|
|
|
* @return msg->conn->err, but the return value is currently ignored
|
|
|
|
*/
|
2010-01-17 18:28:56 +00:00
|
|
|
static void
|
2007-03-30 09:42:16 +00:00
|
|
|
pcb_new(struct api_msg_msg *msg)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
2010-01-17 18:28:56 +00:00
|
|
|
LWIP_ASSERT("pcb_new: pcb already allocated", msg->conn->pcb.tcp == NULL);
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2010-01-17 18:28:56 +00:00
|
|
|
/* Allocate a PCB for this connection */
|
|
|
|
switch(NETCONNTYPE_GROUP(msg->conn->type)) {
|
2003-11-14 13:17:23 +00:00
|
|
|
#if LWIP_RAW
|
2010-01-17 18:28:56 +00:00
|
|
|
case NETCONN_RAW:
|
|
|
|
msg->conn->pcb.raw = raw_new(msg->msg.n.proto);
|
|
|
|
if(msg->conn->pcb.raw == NULL) {
|
|
|
|
msg->err = ERR_MEM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
raw_recv(msg->conn->pcb.raw, recv_raw, msg->conn);
|
|
|
|
break;
|
2007-03-30 09:42:16 +00:00
|
|
|
#endif /* LWIP_RAW */
|
2003-11-14 13:17:23 +00:00
|
|
|
#if LWIP_UDP
|
2010-01-17 18:28:56 +00:00
|
|
|
case NETCONN_UDP:
|
|
|
|
msg->conn->pcb.udp = udp_new();
|
|
|
|
if(msg->conn->pcb.udp == NULL) {
|
|
|
|
msg->err = ERR_MEM;
|
|
|
|
break;
|
|
|
|
}
|
2007-06-10 12:02:24 +00:00
|
|
|
#if LWIP_UDPLITE
|
2010-01-17 18:28:56 +00:00
|
|
|
if (msg->conn->type==NETCONN_UDPLITE) {
|
|
|
|
udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_UDPLITE);
|
|
|
|
}
|
2007-06-10 20:02:43 +00:00
|
|
|
#endif /* LWIP_UDPLITE */
|
2010-01-17 18:28:56 +00:00
|
|
|
if (msg->conn->type==NETCONN_UDPNOCHKSUM) {
|
|
|
|
udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_NOCHKSUM);
|
|
|
|
}
|
|
|
|
udp_recv(msg->conn->pcb.udp, recv_udp, msg->conn);
|
|
|
|
break;
|
2003-11-14 13:17:23 +00:00
|
|
|
#endif /* LWIP_UDP */
|
|
|
|
#if LWIP_TCP
|
2010-01-17 18:28:56 +00:00
|
|
|
case NETCONN_TCP:
|
|
|
|
msg->conn->pcb.tcp = tcp_new();
|
|
|
|
if(msg->conn->pcb.tcp == NULL) {
|
|
|
|
msg->err = ERR_MEM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
setup_tcp(msg->conn);
|
|
|
|
break;
|
2007-03-30 09:42:16 +00:00
|
|
|
#endif /* LWIP_TCP */
|
2010-01-17 18:28:56 +00:00
|
|
|
default:
|
|
|
|
/* Unsupported netconn type, e.g. protocol disabled */
|
|
|
|
msg->err = ERR_VAL;
|
|
|
|
break;
|
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-06-16 15:07:11 +00:00
|
|
|
/**
|
|
|
|
* Create a new pcb of a specific type inside a netconn.
|
|
|
|
* Called from netconn_new_with_proto_and_callback.
|
|
|
|
*
|
|
|
|
* @param msg the api_msg_msg describing the connection type
|
|
|
|
*/
|
2007-05-11 08:58:23 +00:00
|
|
|
void
|
2007-03-30 09:42:16 +00:00
|
|
|
do_newconn(struct api_msg_msg *msg)
|
|
|
|
{
|
2010-01-17 18:28:56 +00:00
|
|
|
msg->err = ERR_OK;
|
|
|
|
if(msg->conn->pcb.tcp == NULL) {
|
|
|
|
pcb_new(msg);
|
|
|
|
}
|
|
|
|
/* Else? This "new" connection already has a PCB allocated. */
|
|
|
|
/* Is this an error condition? Should it be deleted? */
|
|
|
|
/* We currently just are happy and return. */
|
|
|
|
|
|
|
|
TCPIP_APIMSG_ACK(msg);
|
2007-03-30 09:42:16 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-11-30 12:54:10 +00:00
|
|
|
/**
|
|
|
|
* Create a new netconn (of a specific type) that has a callback function.
|
|
|
|
* The corresponding pcb is NOT 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
|
|
|
|
*/
|
|
|
|
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_alloc(enum netconn_type t, netconn_callback callback)
|
2007-11-30 12:54:10 +00:00
|
|
|
{
|
|
|
|
struct netconn *conn;
|
2008-01-11 23:50:10 +00:00
|
|
|
int size;
|
2007-11-30 12:54:10 +00:00
|
|
|
|
2010-01-25 08:24:30 +00:00
|
|
|
conn = (struct netconn *)memp_malloc(MEMP_NETCONN);
|
2007-11-30 12:54:10 +00:00
|
|
|
if (conn == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-01-17 18:28:56 +00:00
|
|
|
conn->last_err = ERR_OK;
|
2007-11-30 12:54:10 +00:00
|
|
|
conn->type = t;
|
|
|
|
conn->pcb.tcp = NULL;
|
|
|
|
|
2008-01-11 23:50:10 +00:00
|
|
|
#if (DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_UDP_RECVMBOX_SIZE) && \
|
|
|
|
(DEFAULT_RAW_RECVMBOX_SIZE == DEFAULT_TCP_RECVMBOX_SIZE)
|
|
|
|
size = DEFAULT_RAW_RECVMBOX_SIZE;
|
|
|
|
#else
|
|
|
|
switch(NETCONNTYPE_GROUP(t)) {
|
|
|
|
#if LWIP_RAW
|
|
|
|
case NETCONN_RAW:
|
|
|
|
size = DEFAULT_RAW_RECVMBOX_SIZE;
|
|
|
|
break;
|
|
|
|
#endif /* LWIP_RAW */
|
|
|
|
#if LWIP_UDP
|
|
|
|
case NETCONN_UDP:
|
|
|
|
size = DEFAULT_UDP_RECVMBOX_SIZE;
|
|
|
|
break;
|
|
|
|
#endif /* LWIP_UDP */
|
|
|
|
#if LWIP_TCP
|
|
|
|
case NETCONN_TCP:
|
|
|
|
size = DEFAULT_TCP_RECVMBOX_SIZE;
|
|
|
|
break;
|
|
|
|
#endif /* LWIP_TCP */
|
|
|
|
default:
|
|
|
|
LWIP_ASSERT("netconn_alloc: undefined netconn_type", 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-02-12 13:49:21 +00:00
|
|
|
if (sys_sem_new(&conn->op_completed, 0) != ERR_OK) {
|
2007-11-30 12:54:10 +00:00
|
|
|
memp_free(MEMP_NETCONN, conn);
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-02-12 13:49:21 +00:00
|
|
|
if (sys_mbox_new(&conn->recvmbox, size) != ERR_OK) {
|
|
|
|
sys_sem_free(&conn->op_completed);
|
2007-11-30 12:54:10 +00:00
|
|
|
memp_free(MEMP_NETCONN, conn);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-01-11 23:50:10 +00:00
|
|
|
|
2010-02-12 13:49:21 +00:00
|
|
|
sys_mbox_set_invalid(&conn->acceptmbox);
|
2007-11-30 12:54:10 +00:00
|
|
|
conn->state = NETCONN_NONE;
|
2010-02-12 09:58:44 +00:00
|
|
|
#if LWIP_SOCKET
|
2010-02-12 13:49:21 +00:00
|
|
|
/* initialize socket to -1 since 0 is a valid socket */
|
2007-11-30 12:54:10 +00:00
|
|
|
conn->socket = -1;
|
2010-02-12 09:58:44 +00:00
|
|
|
#endif /* LWIP_SOCKET */
|
2007-11-30 12:54:10 +00:00
|
|
|
conn->callback = callback;
|
|
|
|
conn->recv_avail = 0;
|
2009-06-25 10:17:18 +00:00
|
|
|
#if LWIP_TCP
|
2010-01-17 18:28:56 +00:00
|
|
|
conn->current_msg = NULL;
|
2009-06-25 10:17:18 +00:00
|
|
|
conn->write_offset = 0;
|
|
|
|
#endif /* LWIP_TCP */
|
2007-11-30 12:54:10 +00:00
|
|
|
#if LWIP_SO_RCVTIMEO
|
|
|
|
conn->recv_timeout = 0;
|
|
|
|
#endif /* LWIP_SO_RCVTIMEO */
|
|
|
|
#if LWIP_SO_RCVBUF
|
2009-02-11 19:07:22 +00:00
|
|
|
conn->recv_bufsize = RECV_BUFSIZE_DEFAULT;
|
2007-11-30 12:54:10 +00:00
|
|
|
#endif /* LWIP_SO_RCVBUF */
|
2010-02-09 18:41:07 +00:00
|
|
|
conn->flags = 0;
|
2007-11-30 12:54:10 +00:00
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
2008-01-06 14:28:04 +00:00
|
|
|
/**
|
|
|
|
* Delete a netconn and all its resources.
|
|
|
|
* The pcb is NOT freed (since we might not be in the right thread context do this).
|
|
|
|
*
|
|
|
|
* @param conn the netconn to free
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
netconn_free(struct netconn *conn)
|
|
|
|
{
|
|
|
|
LWIP_ASSERT("PCB must be deallocated outside this function", conn->pcb.tcp == NULL);
|
2010-01-08 15:10:03 +00:00
|
|
|
LWIP_ASSERT("recvmbox must be deallocated before calling this function",
|
2010-02-12 13:49:21 +00:00
|
|
|
!sys_mbox_valid(&conn->recvmbox));
|
2010-01-08 15:10:03 +00:00
|
|
|
LWIP_ASSERT("acceptmbox must be deallocated before calling this function",
|
2010-02-12 13:49:21 +00:00
|
|
|
!sys_mbox_valid(&conn->acceptmbox));
|
2008-01-06 14:28:04 +00:00
|
|
|
|
2010-02-12 13:49:21 +00:00
|
|
|
sys_sem_free(&conn->op_completed);
|
|
|
|
sys_sem_set_invalid(&conn->op_completed);
|
2010-01-08 15:10:03 +00:00
|
|
|
|
|
|
|
memp_free(MEMP_NETCONN, conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete rcvmbox and acceptmbox of a netconn and free the left-over data in
|
|
|
|
* these mboxes
|
|
|
|
*
|
|
|
|
* @param conn the netconn to free
|
|
|
|
* @bytes_drained bytes drained from recvmbox
|
|
|
|
* @accepts_drained pending connections drained from acceptmbox
|
|
|
|
*/
|
|
|
|
void
|
2010-01-13 15:59:47 +00:00
|
|
|
netconn_drain(struct netconn *conn)
|
2010-01-08 15:10:03 +00:00
|
|
|
{
|
|
|
|
void *mem;
|
|
|
|
struct pbuf *p;
|
2010-02-12 13:49:21 +00:00
|
|
|
|
|
|
|
/* This runs in tcpip_thread, so we don't need to lock against rx packets */
|
2010-01-08 15:10:03 +00:00
|
|
|
|
|
|
|
/* Delete and drain the recvmbox. */
|
2010-02-12 13:49:21 +00:00
|
|
|
if (sys_mbox_valid(&conn->recvmbox)) {
|
|
|
|
while (sys_mbox_tryfetch(&conn->recvmbox, &mem) != SYS_MBOX_EMPTY) {
|
2008-01-06 14:28:04 +00:00
|
|
|
if (conn->type == NETCONN_TCP) {
|
|
|
|
if(mem != NULL) {
|
2010-01-08 15:10:03 +00:00
|
|
|
p = (struct pbuf*)mem;
|
2010-01-14 15:43:11 +00:00
|
|
|
/* pcb might be set to NULL already by err_tcp() */
|
|
|
|
if (conn->pcb.tcp != NULL) {
|
|
|
|
tcp_recved(conn->pcb.tcp, p->tot_len);
|
|
|
|
}
|
2010-01-08 15:10:03 +00:00
|
|
|
pbuf_free(p);
|
2008-01-06 14:28:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
netbuf_delete((struct netbuf *)mem);
|
|
|
|
}
|
|
|
|
}
|
2010-02-12 13:49:21 +00:00
|
|
|
sys_mbox_free(&conn->recvmbox);
|
|
|
|
sys_mbox_set_invalid(&conn->recvmbox);
|
2008-01-06 14:28:04 +00:00
|
|
|
}
|
|
|
|
|
2010-01-08 15:10:03 +00:00
|
|
|
/* Delete and drain the acceptmbox. */
|
2010-02-12 13:49:21 +00:00
|
|
|
if (sys_mbox_valid(&conn->acceptmbox)) {
|
|
|
|
while (sys_mbox_tryfetch(&conn->acceptmbox, &mem) != SYS_MBOX_EMPTY) {
|
2010-01-14 15:43:11 +00:00
|
|
|
/* Only tcp pcbs have an acceptmbox, so no need to check conn->type */
|
|
|
|
/* pcb might be set to NULL already by err_tcp() */
|
|
|
|
if (conn->pcb.tcp != NULL) {
|
|
|
|
tcp_accepted(conn->pcb.tcp);
|
|
|
|
}
|
2008-01-06 14:28:04 +00:00
|
|
|
netconn_delete((struct netconn *)mem);
|
|
|
|
}
|
2010-02-12 13:49:21 +00:00
|
|
|
sys_mbox_free(&conn->acceptmbox);
|
|
|
|
sys_mbox_set_invalid(&conn->acceptmbox);
|
2008-01-06 14:28:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-29 08:11:33 +00:00
|
|
|
#if LWIP_TCP
|
2008-01-06 14:28:04 +00:00
|
|
|
/**
|
|
|
|
* Internal helper function to close a TCP netconn: since this sometimes
|
|
|
|
* doesn't work at the first attempt, this function is called from multiple
|
|
|
|
* places.
|
|
|
|
*
|
|
|
|
* @param conn the TCP netconn to close
|
|
|
|
*/
|
2007-07-03 19:29:59 +00:00
|
|
|
static void
|
|
|
|
do_close_internal(struct netconn *conn)
|
|
|
|
{
|
2007-07-25 19:24:27 +00:00
|
|
|
err_t err;
|
|
|
|
|
2007-07-03 19:29:59 +00:00
|
|
|
LWIP_ASSERT("invalid conn", (conn != NULL));
|
|
|
|
LWIP_ASSERT("this is for tcp netconns only", (conn->type == NETCONN_TCP));
|
|
|
|
LWIP_ASSERT("conn must be in state NETCONN_CLOSE", (conn->state == NETCONN_CLOSE));
|
|
|
|
LWIP_ASSERT("pcb already closed", (conn->pcb.tcp != NULL));
|
2010-01-17 18:28:56 +00:00
|
|
|
LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL);
|
2007-07-03 19:29:59 +00:00
|
|
|
|
2007-07-25 19:24:27 +00:00
|
|
|
/* Set back some callback pointers */
|
2008-07-14 20:12:36 +00:00
|
|
|
tcp_arg(conn->pcb.tcp, NULL);
|
2007-07-03 19:29:59 +00:00
|
|
|
if (conn->pcb.tcp->state == LISTEN) {
|
|
|
|
tcp_accept(conn->pcb.tcp, NULL);
|
2007-07-25 19:24:27 +00:00
|
|
|
} else {
|
|
|
|
tcp_recv(conn->pcb.tcp, NULL);
|
2008-07-14 20:12:36 +00:00
|
|
|
tcp_accept(conn->pcb.tcp, NULL);
|
|
|
|
/* some callbacks have to be reset if tcp_close is not successful */
|
|
|
|
tcp_sent(conn->pcb.tcp, NULL);
|
|
|
|
tcp_poll(conn->pcb.tcp, NULL, 4);
|
|
|
|
tcp_err(conn->pcb.tcp, NULL);
|
2007-07-25 19:24:27 +00:00
|
|
|
}
|
|
|
|
/* Try to close the connection */
|
|
|
|
err = tcp_close(conn->pcb.tcp);
|
|
|
|
if (err == ERR_OK) {
|
|
|
|
/* Closing succeeded */
|
2010-01-17 18:28:56 +00:00
|
|
|
conn->current_msg->err = ERR_OK;
|
|
|
|
conn->current_msg = NULL;
|
2007-07-03 19:29:59 +00:00
|
|
|
conn->state = NETCONN_NONE;
|
2007-08-26 23:58:33 +00:00
|
|
|
/* Set back some callback pointers as conn is going away */
|
2007-07-03 19:29:59 +00:00
|
|
|
conn->pcb.tcp = NULL;
|
2010-01-29 23:05:33 +00:00
|
|
|
/* Trigger select() in socket layer. Make sure everybody notices activity
|
|
|
|
on the connection, error first! */
|
|
|
|
API_EVENT(conn, NETCONN_EVT_ERROR, 0);
|
2007-10-24 12:09:18 +00:00
|
|
|
API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
|
|
|
|
API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
|
2007-07-25 19:24:27 +00:00
|
|
|
/* wake up the application task */
|
2010-02-12 13:49:21 +00:00
|
|
|
sys_sem_signal(&conn->op_completed);
|
2008-07-14 20:12:36 +00:00
|
|
|
} else {
|
|
|
|
/* Closing failed, restore some of the callbacks */
|
|
|
|
/* Closing of listen pcb will never fail! */
|
|
|
|
LWIP_ASSERT("Closing a listen pcb may not fail!", (conn->pcb.tcp->state != LISTEN));
|
|
|
|
tcp_sent(conn->pcb.tcp, sent_tcp);
|
|
|
|
tcp_poll(conn->pcb.tcp, poll_tcp, 4);
|
|
|
|
tcp_err(conn->pcb.tcp, err_tcp);
|
|
|
|
tcp_arg(conn->pcb.tcp, conn);
|
2007-07-03 19:29:59 +00:00
|
|
|
}
|
2007-07-25 19:24:27 +00:00
|
|
|
/* If closing didn't succeed, we get called again either
|
|
|
|
from poll_tcp or from sent_tcp */
|
2007-07-03 19:29:59 +00:00
|
|
|
}
|
2007-07-29 08:11:33 +00:00
|
|
|
#endif /* LWIP_TCP */
|
2007-07-03 19:29:59 +00:00
|
|
|
|
2007-06-16 15:07:11 +00:00
|
|
|
/**
|
|
|
|
* Delete the pcb inside a netconn.
|
|
|
|
* Called from netconn_delete.
|
|
|
|
*
|
|
|
|
* @param msg the api_msg_msg pointing to the connection
|
|
|
|
*/
|
2007-05-11 08:58:23 +00:00
|
|
|
void
|
2002-10-19 12:59:30 +00:00
|
|
|
do_delconn(struct api_msg_msg *msg)
|
|
|
|
{
|
2010-01-29 22:09:31 +00:00
|
|
|
/* @todo TCP: abort running write/connect? */
|
2010-02-12 15:21:37 +00:00
|
|
|
if ((msg->conn->state != NETCONN_NONE) &&
|
|
|
|
(msg->conn->state != NETCONN_LISTEN) &&
|
|
|
|
(msg->conn->state != NETCONN_CONNECT)) {
|
2010-01-29 22:09:31 +00:00
|
|
|
/* this only happens for TCP netconns */
|
|
|
|
LWIP_ASSERT("msg->conn->type == NETCONN_TCP", msg->conn->type == NETCONN_TCP);
|
|
|
|
msg->err = ERR_INPROGRESS;
|
|
|
|
} else {
|
2010-02-12 15:21:37 +00:00
|
|
|
LWIP_ASSERT("blocking connect in progress",
|
|
|
|
(msg->conn->state != NETCONN_CONNECT) || IN_NONBLOCKING_CONNECT(msg->conn));
|
2010-01-29 22:09:31 +00:00
|
|
|
/* Drain and delete mboxes */
|
|
|
|
netconn_drain(msg->conn);
|
2010-01-13 15:59:47 +00:00
|
|
|
|
2010-01-29 22:09:31 +00:00
|
|
|
if (msg->conn->pcb.tcp != NULL) {
|
2010-01-13 15:59:47 +00:00
|
|
|
|
2010-01-29 22:09:31 +00:00
|
|
|
switch (NETCONNTYPE_GROUP(msg->conn->type)) {
|
2003-11-14 13:17:23 +00:00
|
|
|
#if LWIP_RAW
|
2010-01-29 22:09:31 +00:00
|
|
|
case NETCONN_RAW:
|
|
|
|
raw_remove(msg->conn->pcb.raw);
|
|
|
|
break;
|
2007-03-30 09:42:16 +00:00
|
|
|
#endif /* LWIP_RAW */
|
2002-10-19 12:59:30 +00:00
|
|
|
#if LWIP_UDP
|
2010-01-29 22:09:31 +00:00
|
|
|
case NETCONN_UDP:
|
|
|
|
msg->conn->pcb.udp->recv_arg = NULL;
|
|
|
|
udp_remove(msg->conn->pcb.udp);
|
|
|
|
break;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* LWIP_UDP */
|
2007-03-30 09:42:16 +00:00
|
|
|
#if LWIP_TCP
|
2010-01-29 22:09:31 +00:00
|
|
|
case NETCONN_TCP:
|
|
|
|
LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL &&
|
|
|
|
msg->conn->write_offset == 0);
|
|
|
|
msg->conn->state = NETCONN_CLOSE;
|
|
|
|
msg->conn->current_msg = msg;
|
|
|
|
do_close_internal(msg->conn);
|
|
|
|
/* API_EVENT is called inside do_close_internal, before releasing
|
|
|
|
the application thread, so we can return at this point! */
|
|
|
|
return;
|
2007-03-30 09:42:16 +00:00
|
|
|
#endif /* LWIP_TCP */
|
2010-01-29 22:09:31 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
msg->conn->pcb.tcp = NULL;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2010-01-29 22:09:31 +00:00
|
|
|
/* tcp netconns don't come here! */
|
2007-05-19 16:24:40 +00:00
|
|
|
|
2010-01-29 22:09:31 +00:00
|
|
|
/* @todo: this lets select make the socket readable and writable,
|
|
|
|
which is wrong! errfd instead? */
|
|
|
|
API_EVENT(msg->conn, NETCONN_EVT_RCVPLUS, 0);
|
|
|
|
API_EVENT(msg->conn, NETCONN_EVT_SENDPLUS, 0);
|
|
|
|
}
|
2010-02-12 13:49:21 +00:00
|
|
|
if (sys_sem_valid(&msg->conn->op_completed)) {
|
|
|
|
sys_sem_signal(&msg->conn->op_completed);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-06-16 15:07:11 +00:00
|
|
|
/**
|
|
|
|
* Bind a pcb contained in a netconn
|
|
|
|
* Called from netconn_bind.
|
|
|
|
*
|
|
|
|
* @param msg the api_msg_msg pointing to the connection and containing
|
|
|
|
* the IP address and port to bind to
|
|
|
|
*/
|
2007-05-11 08:58:23 +00:00
|
|
|
void
|
2002-10-19 12:59:30 +00:00
|
|
|
do_bind(struct api_msg_msg *msg)
|
|
|
|
{
|
2010-01-17 18:28:56 +00:00
|
|
|
if (ERR_IS_FATAL(msg->conn->last_err)) {
|
|
|
|
msg->err = msg->conn->last_err;
|
|
|
|
} else {
|
|
|
|
msg->err = ERR_VAL;
|
2007-05-19 16:27:02 +00:00
|
|
|
if (msg->conn->pcb.tcp != NULL) {
|
2007-05-23 17:46:53 +00:00
|
|
|
switch (NETCONNTYPE_GROUP(msg->conn->type)) {
|
2003-11-14 13:17:23 +00:00
|
|
|
#if LWIP_RAW
|
2007-05-19 16:27:02 +00:00
|
|
|
case NETCONN_RAW:
|
2010-01-17 18:28:56 +00:00
|
|
|
msg->err = raw_bind(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
|
2007-05-19 16:27:02 +00:00
|
|
|
break;
|
2007-03-30 09:42:16 +00:00
|
|
|
#endif /* LWIP_RAW */
|
2002-10-19 12:59:30 +00:00
|
|
|
#if LWIP_UDP
|
2007-05-19 16:27:02 +00:00
|
|
|
case NETCONN_UDP:
|
2010-01-17 18:28:56 +00:00
|
|
|
msg->err = udp_bind(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
|
2007-05-19 16:27:02 +00:00
|
|
|
break;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* LWIP_UDP */
|
2007-03-30 09:42:16 +00:00
|
|
|
#if LWIP_TCP
|
2007-05-19 16:27:02 +00:00
|
|
|
case NETCONN_TCP:
|
2010-01-17 18:28:56 +00:00
|
|
|
msg->err = tcp_bind(msg->conn->pcb.tcp, msg->msg.bc.ipaddr, msg->msg.bc.port);
|
2007-05-22 09:54:00 +00:00
|
|
|
break;
|
2003-03-21 10:48:21 +00:00
|
|
|
#endif /* LWIP_TCP */
|
2007-05-19 16:27:02 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-08 19:27:59 +00:00
|
|
|
TCPIP_APIMSG_ACK(msg);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-03-30 09:42:16 +00:00
|
|
|
#if LWIP_TCP
|
2007-06-16 15:07:11 +00:00
|
|
|
/**
|
|
|
|
* TCP callback function if a connection (opened by tcp_connect/do_connect) has
|
|
|
|
* been established (or reset by the remote host).
|
|
|
|
*
|
|
|
|
* @see tcp.h (struct tcp_pcb.connected) for parameters and return values
|
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
static err_t
|
|
|
|
do_connected(void *arg, struct tcp_pcb *pcb, err_t err)
|
|
|
|
{
|
|
|
|
struct netconn *conn;
|
2010-01-29 22:09:31 +00:00
|
|
|
int was_blocking;
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2007-04-04 19:54:22 +00:00
|
|
|
LWIP_UNUSED_ARG(pcb);
|
2007-05-19 16:24:40 +00:00
|
|
|
|
2010-01-25 08:24:30 +00:00
|
|
|
conn = (struct netconn *)arg;
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn == NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_VAL;
|
|
|
|
}
|
2007-05-19 16:24:40 +00:00
|
|
|
|
2010-01-17 18:28:56 +00:00
|
|
|
LWIP_ASSERT("conn->state == NETCONN_CONNECT", conn->state == NETCONN_CONNECT);
|
2010-01-29 22:09:31 +00:00
|
|
|
LWIP_ASSERT("(conn->current_msg != NULL) || conn->in_non_blocking_connect",
|
2010-02-09 18:41:07 +00:00
|
|
|
(conn->current_msg != NULL) || IN_NONBLOCKING_CONNECT(conn));
|
2010-01-17 18:28:56 +00:00
|
|
|
|
2010-01-29 22:09:31 +00:00
|
|
|
if (conn->current_msg != NULL) {
|
|
|
|
conn->current_msg->err = err;
|
|
|
|
}
|
2007-11-27 21:15:44 +00:00
|
|
|
if ((conn->type == NETCONN_TCP) && (err == ERR_OK)) {
|
2002-10-19 12:59:30 +00:00
|
|
|
setup_tcp(conn);
|
2007-03-21 16:38:58 +00:00
|
|
|
}
|
2010-02-09 18:41:07 +00:00
|
|
|
was_blocking = !IN_NONBLOCKING_CONNECT(conn);
|
|
|
|
SET_NONBLOCKING_CONNECT(conn, 0);
|
2010-01-17 18:28:56 +00:00
|
|
|
conn->current_msg = NULL;
|
2007-04-05 16:54:20 +00:00
|
|
|
conn->state = NETCONN_NONE;
|
2010-01-29 22:09:31 +00:00
|
|
|
if (!was_blocking) {
|
|
|
|
SYS_ARCH_DECL_PROTECT(lev);
|
|
|
|
SYS_ARCH_PROTECT(lev);
|
|
|
|
if (conn->last_err == ERR_INPROGRESS) {
|
|
|
|
conn->last_err = ERR_OK;
|
|
|
|
}
|
|
|
|
SYS_ARCH_UNPROTECT(lev);
|
|
|
|
}
|
|
|
|
API_EVENT(conn, NETCONN_EVT_SENDPLUS, 0);
|
|
|
|
|
|
|
|
if (was_blocking) {
|
2010-02-12 13:49:21 +00:00
|
|
|
sys_sem_signal(&conn->op_completed);
|
2010-01-29 22:09:31 +00:00
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_OK;
|
|
|
|
}
|
2007-03-30 09:42:16 +00:00
|
|
|
#endif /* LWIP_TCP */
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-06-16 15:07:11 +00:00
|
|
|
/**
|
|
|
|
* Connect a pcb contained inside a netconn
|
|
|
|
* Called from netconn_connect.
|
|
|
|
*
|
|
|
|
* @param msg the api_msg_msg pointing to the connection and containing
|
|
|
|
* the IP address and port to connect to
|
|
|
|
*/
|
2007-05-11 08:58:23 +00:00
|
|
|
void
|
2002-10-19 12:59:30 +00:00
|
|
|
do_connect(struct api_msg_msg *msg)
|
|
|
|
{
|
2003-05-01 13:24:01 +00:00
|
|
|
if (msg->conn->pcb.tcp == NULL) {
|
2010-01-14 19:49:27 +00:00
|
|
|
/* This may happen when calling netconn_connect() a second time */
|
2010-01-17 18:28:56 +00:00
|
|
|
msg->err = ERR_CLSD;
|
2010-01-29 22:09:31 +00:00
|
|
|
} else {
|
|
|
|
switch (NETCONNTYPE_GROUP(msg->conn->type)) {
|
2003-11-14 13:17:23 +00:00
|
|
|
#if LWIP_RAW
|
|
|
|
case NETCONN_RAW:
|
2010-01-17 18:28:56 +00:00
|
|
|
msg->err = raw_connect(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
|
2003-11-14 13:17:23 +00:00
|
|
|
break;
|
2007-03-30 09:42:16 +00:00
|
|
|
#endif /* LWIP_RAW */
|
2002-10-19 12:59:30 +00:00
|
|
|
#if LWIP_UDP
|
|
|
|
case NETCONN_UDP:
|
2010-01-17 18:28:56 +00:00
|
|
|
msg->err = udp_connect(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
|
2002-10-19 12:59:30 +00:00
|
|
|
break;
|
2007-03-30 09:42:16 +00:00
|
|
|
#endif /* LWIP_UDP */
|
|
|
|
#if LWIP_TCP
|
2002-10-19 12:59:30 +00:00
|
|
|
case NETCONN_TCP:
|
2010-01-29 22:09:31 +00:00
|
|
|
/* Prevent connect while doing any other action. */
|
|
|
|
if (msg->conn->state != NETCONN_NONE) {
|
|
|
|
msg->err = ERR_ISCONN;
|
2010-01-21 18:43:37 +00:00
|
|
|
} else {
|
2010-01-29 22:09:31 +00:00
|
|
|
setup_tcp(msg->conn);
|
|
|
|
msg->err = tcp_connect(msg->conn->pcb.tcp, msg->msg.bc.ipaddr,
|
|
|
|
msg->msg.bc.port, do_connected);
|
|
|
|
if (msg->err == ERR_OK) {
|
2010-02-09 18:41:07 +00:00
|
|
|
u8_t non_blocking = netconn_is_nonblocking(msg->conn);
|
2010-01-29 22:09:31 +00:00
|
|
|
msg->conn->state = NETCONN_CONNECT;
|
2010-02-09 18:41:07 +00:00
|
|
|
SET_NONBLOCKING_CONNECT(msg->conn, non_blocking);
|
|
|
|
if (non_blocking) {
|
2010-01-29 22:09:31 +00:00
|
|
|
msg->err = ERR_INPROGRESS;
|
|
|
|
} else {
|
|
|
|
msg->conn->current_msg = msg;
|
|
|
|
/* sys_sem_signal() is called from do_connected (or err_tcp()),
|
|
|
|
* when the connection is established! */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2010-01-21 18:43:37 +00:00
|
|
|
}
|
2007-05-22 09:54:00 +00:00
|
|
|
break;
|
2007-03-30 09:42:16 +00:00
|
|
|
#endif /* LWIP_TCP */
|
2003-03-21 10:48:21 +00:00
|
|
|
default:
|
2010-01-29 22:09:31 +00:00
|
|
|
LWIP_ERROR("Invalid netconn type", 0, do{ msg->err = ERR_VAL; }while(0));
|
2002-10-19 12:59:30 +00:00
|
|
|
break;
|
2010-01-29 22:09:31 +00:00
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2010-02-12 13:49:21 +00:00
|
|
|
sys_sem_signal(&msg->conn->op_completed);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-01-22 16:18:05 +00:00
|
|
|
|
2007-06-16 15:07:11 +00:00
|
|
|
/**
|
|
|
|
* Connect a pcb contained inside a netconn
|
|
|
|
* Only used for UDP netconns.
|
|
|
|
* Called from netconn_disconnect.
|
|
|
|
*
|
|
|
|
* @param msg the api_msg_msg pointing to the connection to disconnect
|
|
|
|
*/
|
2007-05-11 08:58:23 +00:00
|
|
|
void
|
2003-01-22 16:18:05 +00:00
|
|
|
do_disconnect(struct api_msg_msg *msg)
|
|
|
|
{
|
|
|
|
#if LWIP_UDP
|
2007-05-23 17:46:53 +00:00
|
|
|
if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
|
2003-01-22 16:18:05 +00:00
|
|
|
udp_disconnect(msg->conn->pcb.udp);
|
2010-01-17 18:28:56 +00:00
|
|
|
msg->err = ERR_OK;
|
|
|
|
} else
|
2007-05-23 17:46:53 +00:00
|
|
|
#endif /* LWIP_UDP */
|
2010-01-17 18:28:56 +00:00
|
|
|
{
|
|
|
|
msg->err = ERR_VAL;
|
|
|
|
}
|
2007-06-08 19:27:59 +00:00
|
|
|
TCPIP_APIMSG_ACK(msg);
|
2003-01-22 16:18:05 +00:00
|
|
|
}
|
|
|
|
|
2007-06-16 15:07:11 +00:00
|
|
|
/**
|
|
|
|
* Set a TCP pcb contained in a netconn into listen mode
|
|
|
|
* Called from netconn_listen.
|
|
|
|
*
|
|
|
|
* @param msg the api_msg_msg pointing to the connection
|
|
|
|
*/
|
2007-05-11 08:58:23 +00:00
|
|
|
void
|
2002-10-19 12:59:30 +00:00
|
|
|
do_listen(struct api_msg_msg *msg)
|
|
|
|
{
|
2007-03-30 09:42:16 +00:00
|
|
|
#if LWIP_TCP
|
2010-01-17 18:28:56 +00:00
|
|
|
if (ERR_IS_FATAL(msg->conn->last_err)) {
|
|
|
|
msg->err = msg->conn->last_err;
|
|
|
|
} else {
|
|
|
|
msg->err = ERR_CONN;
|
2007-05-19 16:27:02 +00:00
|
|
|
if (msg->conn->pcb.tcp != NULL) {
|
|
|
|
if (msg->conn->type == NETCONN_TCP) {
|
2010-01-29 22:09:31 +00:00
|
|
|
if (msg->conn->state == NETCONN_NONE) {
|
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
|
|
|
|
struct tcp_pcb* lpcb = tcp_listen_with_backlog(msg->conn->pcb.tcp, msg->msg.lb.backlog);
|
|
|
|
#else /* TCP_LISTEN_BACKLOG */
|
2007-07-24 07:41:55 +00:00
|
|
|
struct tcp_pcb* lpcb = tcp_listen(msg->conn->pcb.tcp);
|
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-07-24 07:41:55 +00:00
|
|
|
if (lpcb == NULL) {
|
2010-01-29 22:09:31 +00:00
|
|
|
/* in this case, the old pcb is still allocated */
|
2010-01-17 18:28:56 +00:00
|
|
|
msg->err = ERR_MEM;
|
2007-07-24 07:41:55 +00:00
|
|
|
} else {
|
2007-12-02 14:53:50 +00:00
|
|
|
/* delete the recvmbox and allocate the acceptmbox */
|
2010-02-12 13:49:21 +00:00
|
|
|
if (sys_mbox_valid(&msg->conn->recvmbox)) {
|
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
|
|
|
/** @todo: should we drain the recvmbox here? */
|
2010-02-12 13:49:21 +00:00
|
|
|
sys_mbox_free(&msg->conn->recvmbox);
|
|
|
|
sys_mbox_set_invalid(&msg->conn->recvmbox);
|
2007-12-02 14:53:50 +00:00
|
|
|
}
|
2010-01-17 18:28:56 +00:00
|
|
|
msg->err = ERR_OK;
|
2010-02-12 13:49:21 +00:00
|
|
|
if (!sys_mbox_valid(&msg->conn->acceptmbox)) {
|
|
|
|
msg->err = sys_mbox_new(&msg->conn->acceptmbox, DEFAULT_ACCEPTMBOX_SIZE);
|
2007-07-24 07:41:55 +00:00
|
|
|
}
|
2010-01-17 18:28:56 +00:00
|
|
|
if (msg->err == ERR_OK) {
|
2007-08-27 10:08:53 +00:00
|
|
|
msg->conn->state = NETCONN_LISTEN;
|
2007-07-24 07:41:55 +00:00
|
|
|
msg->conn->pcb.tcp = lpcb;
|
|
|
|
tcp_arg(msg->conn->pcb.tcp, msg->conn);
|
|
|
|
tcp_accept(msg->conn->pcb.tcp, accept_function);
|
2010-01-29 22:09:31 +00:00
|
|
|
} else {
|
|
|
|
/* since the old pcb is already deallocated, free lpcb now */
|
|
|
|
tcp_close(lpcb);
|
|
|
|
msg->conn->pcb.tcp = NULL;
|
2007-05-19 16:27:02 +00:00
|
|
|
}
|
|
|
|
}
|
2007-03-21 16:38:58 +00:00
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-05-19 16:27:02 +00:00
|
|
|
#endif /* LWIP_TCP */
|
2007-06-08 19:27:59 +00:00
|
|
|
TCPIP_APIMSG_ACK(msg);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-06-16 15:07:11 +00:00
|
|
|
/**
|
|
|
|
* Send some data on a RAW or UDP pcb contained in a netconn
|
|
|
|
* Called from netconn_send
|
|
|
|
*
|
|
|
|
* @param msg the api_msg_msg pointing to the connection
|
|
|
|
*/
|
2007-05-11 08:58:23 +00:00
|
|
|
void
|
2002-10-19 12:59:30 +00:00
|
|
|
do_send(struct api_msg_msg *msg)
|
|
|
|
{
|
2010-01-17 18:28:56 +00:00
|
|
|
if (ERR_IS_FATAL(msg->conn->last_err)) {
|
|
|
|
msg->err = msg->conn->last_err;
|
|
|
|
} else {
|
|
|
|
msg->err = ERR_CONN;
|
2007-04-05 16:54:20 +00:00
|
|
|
if (msg->conn->pcb.tcp != NULL) {
|
2007-05-23 17:46:53 +00:00
|
|
|
switch (NETCONNTYPE_GROUP(msg->conn->type)) {
|
2003-11-14 13:17:23 +00:00
|
|
|
#if LWIP_RAW
|
2007-04-05 16:54:20 +00:00
|
|
|
case NETCONN_RAW:
|
2007-05-22 09:54:00 +00:00
|
|
|
if (msg->msg.b->addr == NULL) {
|
2010-01-17 18:28:56 +00:00
|
|
|
msg->err = raw_send(msg->conn->pcb.raw, msg->msg.b->p);
|
2007-05-04 15:18:29 +00:00
|
|
|
} else {
|
2010-01-17 18:28:56 +00:00
|
|
|
msg->err = raw_sendto(msg->conn->pcb.raw, msg->msg.b->p, msg->msg.b->addr);
|
2007-05-04 15:18:29 +00:00
|
|
|
}
|
2007-04-05 16:54:20 +00:00
|
|
|
break;
|
2003-11-14 13:17:23 +00:00
|
|
|
#endif
|
2002-10-19 12:59:30 +00:00
|
|
|
#if LWIP_UDP
|
2007-04-05 16:54:20 +00:00
|
|
|
case NETCONN_UDP:
|
2007-05-22 09:54:00 +00:00
|
|
|
if (msg->msg.b->addr == NULL) {
|
2010-01-17 18:28:56 +00:00
|
|
|
msg->err = udp_send(msg->conn->pcb.udp, msg->msg.b->p);
|
2007-05-04 15:18:29 +00:00
|
|
|
} else {
|
2010-01-17 18:28:56 +00:00
|
|
|
msg->err = udp_sendto(msg->conn->pcb.udp, msg->msg.b->p, msg->msg.b->addr, msg->msg.b->port);
|
2007-05-04 15:18:29 +00:00
|
|
|
}
|
2007-04-05 16:54:20 +00:00
|
|
|
break;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* LWIP_UDP */
|
2007-05-22 09:54:00 +00:00
|
|
|
default:
|
2007-05-21 09:52:23 +00:00
|
|
|
break;
|
2007-04-05 16:54:20 +00:00
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-08 19:27:59 +00:00
|
|
|
TCPIP_APIMSG_ACK(msg);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-06-16 15:07:11 +00:00
|
|
|
/**
|
2008-11-12 19:14:21 +00:00
|
|
|
* Indicate data has been received from a TCP pcb contained in a netconn
|
2007-06-16 15:07:11 +00:00
|
|
|
* Called from netconn_recv
|
|
|
|
*
|
|
|
|
* @param msg the api_msg_msg pointing to the connection
|
|
|
|
*/
|
2007-05-11 08:58:23 +00:00
|
|
|
void
|
2002-10-19 12:59:30 +00:00
|
|
|
do_recv(struct api_msg_msg *msg)
|
|
|
|
{
|
2003-03-21 10:48:21 +00:00
|
|
|
#if LWIP_TCP
|
2010-01-17 18:28:56 +00:00
|
|
|
msg->err = ERR_OK;
|
|
|
|
if (msg->conn->pcb.tcp != NULL) {
|
|
|
|
if (msg->conn->type == NETCONN_TCP) {
|
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 18:28:56 +00:00
|
|
|
if (msg->conn->pcb.tcp->state == LISTEN) {
|
|
|
|
tcp_accepted(msg->conn->pcb.tcp);
|
|
|
|
} else
|
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 */
|
2010-01-17 18:28:56 +00:00
|
|
|
{
|
2010-02-09 20:23:39 +00:00
|
|
|
u32_t remaining = msg->msg.r.len;
|
|
|
|
do {
|
|
|
|
u16_t recved = (remaining > 0xffff) ? 0xffff : (u16_t)remaining;
|
|
|
|
tcp_recved(msg->conn->pcb.tcp, recved);
|
|
|
|
remaining -= recved;
|
|
|
|
}while(remaining != 0);
|
2007-05-19 16:27:02 +00:00
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
}
|
2007-05-19 16:24:40 +00:00
|
|
|
#endif /* LWIP_TCP */
|
2007-06-08 19:27:59 +00:00
|
|
|
TCPIP_APIMSG_ACK(msg);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-06-21 18:40:21 +00:00
|
|
|
#if LWIP_TCP
|
|
|
|
/**
|
|
|
|
* See if more data needs to be written from a previous call to netconn_write.
|
|
|
|
* Called initially from do_write. If the first call can't send all data
|
|
|
|
* (because of low memory or empty send-buffer), this function is called again
|
|
|
|
* from sent_tcp() or poll_tcp() to send more data. If all data is sent, the
|
|
|
|
* blocking application thread (waiting in netconn_write) is released.
|
|
|
|
*
|
|
|
|
* @param conn netconn (that is currently in state NETCONN_WRITE) to process
|
|
|
|
* @return ERR_OK
|
|
|
|
* ERR_MEM if LWIP_TCPIP_CORE_LOCKING=1 and sending hasn't yet finished
|
|
|
|
*/
|
2007-11-24 21:19:47 +00:00
|
|
|
static err_t
|
2007-06-21 18:40:21 +00:00
|
|
|
do_writemore(struct netconn *conn)
|
|
|
|
{
|
2010-02-13 17:08:40 +00:00
|
|
|
err_t err = ERR_OK;
|
2007-06-21 18:40:21 +00:00
|
|
|
void *dataptr;
|
|
|
|
u16_t len, available;
|
|
|
|
u8_t write_finished = 0;
|
2009-02-16 19:33:51 +00:00
|
|
|
size_t diff;
|
2010-02-13 17:08:40 +00:00
|
|
|
u8_t dontblock = netconn_is_nonblocking(conn) ||
|
|
|
|
(conn->current_msg->msg.w.apiflags & NETCONN_DONTBLOCK);
|
2007-06-21 18:40:21 +00:00
|
|
|
|
2010-01-17 18:28:56 +00:00
|
|
|
LWIP_ASSERT("conn != NULL", conn != NULL);
|
2007-06-21 19:14:02 +00:00
|
|
|
LWIP_ASSERT("conn->state == NETCONN_WRITE", (conn->state == NETCONN_WRITE));
|
2010-01-17 18:28:56 +00:00
|
|
|
LWIP_ASSERT("conn->current_msg != NULL", conn->current_msg != NULL);
|
2010-01-29 22:09:31 +00:00
|
|
|
LWIP_ASSERT("conn->pcb.tcp != NULL", conn->pcb.tcp != NULL);
|
|
|
|
LWIP_ASSERT("conn->write_offset < conn->current_msg->msg.w.len",
|
|
|
|
conn->write_offset < conn->current_msg->msg.w.len);
|
2007-06-21 18:40:21 +00:00
|
|
|
|
2010-01-17 18:28:56 +00:00
|
|
|
dataptr = (u8_t*)conn->current_msg->msg.w.dataptr + conn->write_offset;
|
|
|
|
diff = conn->current_msg->msg.w.len - conn->write_offset;
|
2009-02-16 19:33:51 +00:00
|
|
|
if (diff > 0xffffUL) { /* max_u16_t */
|
2007-06-21 19:32:26 +00:00
|
|
|
len = 0xffff;
|
|
|
|
#if LWIP_TCPIP_CORE_LOCKING
|
2010-02-12 13:49:21 +00:00
|
|
|
conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
|
2007-06-21 19:32:26 +00:00
|
|
|
#endif
|
|
|
|
} else {
|
2009-02-16 19:33:51 +00:00
|
|
|
len = (u16_t)diff;
|
2007-06-21 19:32:26 +00:00
|
|
|
}
|
2007-06-21 18:40:21 +00:00
|
|
|
available = tcp_sndbuf(conn->pcb.tcp);
|
|
|
|
if (available < len) {
|
|
|
|
/* don't try to write more than sendbuf */
|
|
|
|
len = available;
|
|
|
|
#if LWIP_TCPIP_CORE_LOCKING
|
2010-02-12 13:49:21 +00:00
|
|
|
conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
|
2007-06-21 18:40:21 +00:00
|
|
|
#endif
|
|
|
|
}
|
2010-02-13 17:08:40 +00:00
|
|
|
if (dontblock && (len < conn->current_msg->msg.w.len)) {
|
|
|
|
/* failed to send all data at once -> nonblocking write not possible */
|
|
|
|
err = ERR_MEM;
|
2010-01-29 22:09:31 +00:00
|
|
|
}
|
2007-06-21 18:40:21 +00:00
|
|
|
if (err == ERR_OK) {
|
2010-02-13 17:08:40 +00:00
|
|
|
LWIP_ASSERT("do_writemore: invalid length!", ((conn->write_offset + len) <= conn->current_msg->msg.w.len));
|
|
|
|
err = tcp_write(conn->pcb.tcp, dataptr, len, conn->current_msg->msg.w.apiflags);
|
|
|
|
}
|
|
|
|
if (dontblock && (err == ERR_MEM)) {
|
|
|
|
/* nonblocking write failed */
|
|
|
|
write_finished = 1;
|
|
|
|
err = ERR_WOULBLOCK;
|
|
|
|
/* let poll_tcp check writable space to mark the pcb
|
|
|
|
writable again */
|
|
|
|
conn->flags |= NETCONN_FLAG_CHECK_WRITESPACE;
|
|
|
|
/* let select mark this pcb as non-writable. */
|
|
|
|
API_EVENT(conn, NETCONN_EVT_SENDMINUS, len);
|
|
|
|
} else {
|
|
|
|
/* if OK or memory error, check available space */
|
|
|
|
if (((err == ERR_OK) || (err == ERR_MEM)) &&
|
|
|
|
((tcp_sndbuf(conn->pcb.tcp) <= TCP_SNDLOWAT) ||
|
|
|
|
(tcp_sndqueuelen(conn->pcb.tcp) >= TCP_SNDQUEUELOWAT))) {
|
|
|
|
/* The queued byte- or pbuf-count exceeds the configured low-water limit,
|
|
|
|
let select mark this pcb as non-writable. */
|
|
|
|
API_EVENT(conn, NETCONN_EVT_SENDMINUS, len);
|
2007-06-21 18:40:21 +00:00
|
|
|
}
|
2007-11-21 17:59:03 +00:00
|
|
|
|
2010-02-13 17:08:40 +00:00
|
|
|
if (err == ERR_OK) {
|
|
|
|
conn->write_offset += len;
|
|
|
|
if (conn->write_offset == conn->current_msg->msg.w.len) {
|
|
|
|
/* everything was written */
|
|
|
|
write_finished = 1;
|
|
|
|
conn->write_offset = 0;
|
|
|
|
}
|
2010-02-21 10:56:00 +00:00
|
|
|
tcp_output(conn->pcb.tcp);
|
2010-02-13 17:08:40 +00:00
|
|
|
} else if (err == ERR_MEM) {
|
|
|
|
/* If ERR_MEM, we wait for sent_tcp or poll_tcp to be called
|
|
|
|
we do NOT return to the application thread, since ERR_MEM is
|
|
|
|
only a temporary error! */
|
|
|
|
|
2010-02-21 19:28:46 +00:00
|
|
|
/* tcp_write returned ERR_MEM, try tcp_output anyway */
|
2010-02-13 17:08:40 +00:00
|
|
|
tcp_output(conn->pcb.tcp);
|
|
|
|
|
|
|
|
#if LWIP_TCPIP_CORE_LOCKING
|
|
|
|
conn->flags |= NETCONN_FLAG_WRITE_DELAYED;
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
/* On errors != ERR_MEM, we don't try writing any more but return
|
|
|
|
the error to the application thread. */
|
|
|
|
write_finished = 1;
|
|
|
|
}
|
2007-06-21 18:40:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (write_finished) {
|
|
|
|
/* everything was written: set back connection state
|
|
|
|
and back to application task */
|
2010-01-17 18:28:56 +00:00
|
|
|
conn->current_msg->err = err;
|
|
|
|
conn->current_msg = NULL;
|
2007-06-21 18:40:21 +00:00
|
|
|
conn->state = NETCONN_NONE;
|
|
|
|
#if LWIP_TCPIP_CORE_LOCKING
|
2010-02-12 13:49:21 +00:00
|
|
|
if ((conn->flags & NETCONN_FLAG_WRITE_DELAYED) != 0)
|
2007-06-21 18:40:21 +00:00
|
|
|
#endif
|
|
|
|
{
|
2010-02-12 13:49:21 +00:00
|
|
|
sys_sem_signal(&conn->op_completed);
|
2007-06-21 18:40:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#if LWIP_TCPIP_CORE_LOCKING
|
|
|
|
else
|
|
|
|
return ERR_MEM;
|
|
|
|
#endif
|
|
|
|
return ERR_OK;
|
|
|
|
}
|
|
|
|
#endif /* LWIP_TCP */
|
|
|
|
|
2007-06-16 15:07:11 +00:00
|
|
|
/**
|
|
|
|
* Send some data on a TCP pcb contained in a netconn
|
|
|
|
* Called from netconn_write
|
|
|
|
*
|
|
|
|
* @param msg the api_msg_msg pointing to the connection
|
|
|
|
*/
|
2007-05-11 08:58:23 +00:00
|
|
|
void
|
2002-10-19 12:59:30 +00:00
|
|
|
do_write(struct api_msg_msg *msg)
|
|
|
|
{
|
2010-01-17 18:28:56 +00:00
|
|
|
if (ERR_IS_FATAL(msg->conn->last_err)) {
|
|
|
|
msg->err = msg->conn->last_err;
|
|
|
|
} else {
|
|
|
|
if (msg->conn->type == NETCONN_TCP) {
|
2007-05-22 09:54:00 +00:00
|
|
|
#if LWIP_TCP
|
2010-01-29 22:09:31 +00:00
|
|
|
if (msg->conn->state != NETCONN_NONE) {
|
2010-02-13 17:08:40 +00:00
|
|
|
/* netconn is connecting, closing or in blocking write */
|
2010-01-29 22:09:31 +00:00
|
|
|
msg->err = ERR_INPROGRESS;
|
|
|
|
} else if (msg->conn->pcb.tcp != NULL) {
|
2010-01-17 18:28:56 +00:00
|
|
|
msg->conn->state = NETCONN_WRITE;
|
|
|
|
/* set all the variables used by do_writemore */
|
|
|
|
LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL &&
|
|
|
|
msg->conn->write_offset == 0);
|
2010-01-29 22:09:31 +00:00
|
|
|
LWIP_ASSERT("msg->msg.w.len != 0", msg->msg.w.len != 0);
|
2010-01-17 18:28:56 +00:00
|
|
|
msg->conn->current_msg = msg;
|
|
|
|
msg->conn->write_offset = 0;
|
2007-06-21 18:40:21 +00:00
|
|
|
#if LWIP_TCPIP_CORE_LOCKING
|
2010-02-12 13:49:21 +00:00
|
|
|
msg->conn->flags &= ~NETCONN_FLAG_WRITE_DELAYED;
|
2010-01-17 18:28:56 +00:00
|
|
|
if (do_writemore(msg->conn) != ERR_OK) {
|
|
|
|
LWIP_ASSERT("state!", msg->conn->state == NETCONN_WRITE);
|
|
|
|
UNLOCK_TCPIP_CORE();
|
2010-02-12 13:49:21 +00:00
|
|
|
sys_arch_sem_wait(&msg->conn->op_completed, 0);
|
2010-01-17 18:28:56 +00:00
|
|
|
LOCK_TCPIP_CORE();
|
|
|
|
LWIP_ASSERT("state!", msg->conn->state == NETCONN_NONE);
|
|
|
|
}
|
|
|
|
#else /* LWIP_TCPIP_CORE_LOCKING */
|
|
|
|
do_writemore(msg->conn);
|
|
|
|
#endif /* LWIP_TCPIP_CORE_LOCKING */
|
|
|
|
/* for both cases: if do_writemore was called, don't ACK the APIMSG
|
|
|
|
since do_writemore ACKs it! */
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
msg->err = ERR_CONN;
|
2007-06-21 18:40:21 +00:00
|
|
|
}
|
2010-01-17 18:28:56 +00:00
|
|
|
#else /* LWIP_TCP */
|
|
|
|
msg->err = ERR_VAL;
|
2007-05-19 16:27:02 +00:00
|
|
|
#endif /* LWIP_TCP */
|
|
|
|
#if (LWIP_UDP || LWIP_RAW)
|
2007-06-21 18:40:21 +00:00
|
|
|
} else {
|
2010-01-17 18:28:56 +00:00
|
|
|
msg->err = ERR_VAL;
|
2007-05-19 16:27:02 +00:00
|
|
|
#endif /* (LWIP_UDP || LWIP_RAW) */
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-08 19:27:59 +00:00
|
|
|
TCPIP_APIMSG_ACK(msg);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-11-12 22:39:24 +00:00
|
|
|
/**
|
|
|
|
* Return a connection's local or remote address
|
|
|
|
* Called from netconn_getaddr
|
|
|
|
*
|
|
|
|
* @param msg the api_msg_msg pointing to the connection
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
do_getaddr(struct api_msg_msg *msg)
|
|
|
|
{
|
|
|
|
if (msg->conn->pcb.ip != NULL) {
|
2010-02-12 16:31:18 +00:00
|
|
|
*(msg->msg.ad.ipaddr) = (msg->msg.ad.local ? msg->conn->pcb.ip->local_ip :
|
|
|
|
msg->conn->pcb.ip->remote_ip);
|
2010-01-17 18:28:56 +00:00
|
|
|
|
|
|
|
msg->err = ERR_OK;
|
2007-11-12 22:39:24 +00:00
|
|
|
switch (NETCONNTYPE_GROUP(msg->conn->type)) {
|
|
|
|
#if LWIP_RAW
|
|
|
|
case NETCONN_RAW:
|
|
|
|
if (msg->msg.ad.local) {
|
|
|
|
*(msg->msg.ad.port) = msg->conn->pcb.raw->protocol;
|
|
|
|
} else {
|
|
|
|
/* return an error as connecting is only a helper for upper layers */
|
2010-01-17 18:28:56 +00:00
|
|
|
msg->err = ERR_CONN;
|
2007-11-12 22:39:24 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif /* LWIP_RAW */
|
|
|
|
#if LWIP_UDP
|
|
|
|
case NETCONN_UDP:
|
|
|
|
if (msg->msg.ad.local) {
|
|
|
|
*(msg->msg.ad.port) = msg->conn->pcb.udp->local_port;
|
|
|
|
} else {
|
|
|
|
if ((msg->conn->pcb.udp->flags & UDP_FLAGS_CONNECTED) == 0) {
|
2010-01-17 18:28:56 +00:00
|
|
|
msg->err = ERR_CONN;
|
2007-11-12 22:39:24 +00:00
|
|
|
} else {
|
|
|
|
*(msg->msg.ad.port) = msg->conn->pcb.udp->remote_port;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif /* LWIP_UDP */
|
|
|
|
#if LWIP_TCP
|
|
|
|
case NETCONN_TCP:
|
|
|
|
*(msg->msg.ad.port) = (msg->msg.ad.local?msg->conn->pcb.tcp->local_port:msg->conn->pcb.tcp->remote_port);
|
|
|
|
break;
|
|
|
|
#endif /* LWIP_TCP */
|
|
|
|
}
|
|
|
|
} else {
|
2010-01-17 18:28:56 +00:00
|
|
|
msg->err = ERR_CONN;
|
2007-11-12 22:39:24 +00:00
|
|
|
}
|
|
|
|
TCPIP_APIMSG_ACK(msg);
|
|
|
|
}
|
|
|
|
|
2007-06-16 15:07:11 +00:00
|
|
|
/**
|
|
|
|
* Close a TCP pcb contained in a netconn
|
|
|
|
* Called from netconn_close
|
|
|
|
*
|
|
|
|
* @param msg the api_msg_msg pointing to the connection
|
|
|
|
*/
|
2007-05-11 08:58:23 +00:00
|
|
|
void
|
2002-10-19 12:59:30 +00:00
|
|
|
do_close(struct api_msg_msg *msg)
|
|
|
|
{
|
2003-03-21 10:48:21 +00:00
|
|
|
#if LWIP_TCP
|
2010-01-29 22:09:31 +00:00
|
|
|
/* @todo: abort running write/connect? */
|
|
|
|
if ((msg->conn->state != NETCONN_NONE) && (msg->conn->state != NETCONN_LISTEN)) {
|
|
|
|
/* this only happens for TCP netconns */
|
|
|
|
LWIP_ASSERT("msg->conn->type == NETCONN_TCP", msg->conn->type == NETCONN_TCP);
|
|
|
|
msg->err = ERR_INPROGRESS;
|
|
|
|
} else if ((msg->conn->pcb.tcp != NULL) && (msg->conn->type == NETCONN_TCP)) {
|
|
|
|
/* Drain and delete mboxes */
|
2010-01-13 15:59:47 +00:00
|
|
|
netconn_drain(msg->conn);
|
2010-01-17 18:28:56 +00:00
|
|
|
LWIP_ASSERT("already writing or closing", msg->conn->current_msg == NULL &&
|
|
|
|
msg->conn->write_offset == 0);
|
2010-01-13 15:59:47 +00:00
|
|
|
msg->conn->state = NETCONN_CLOSE;
|
2010-01-17 18:28:56 +00:00
|
|
|
msg->conn->current_msg = msg;
|
2010-01-13 15:59:47 +00:00
|
|
|
do_close_internal(msg->conn);
|
|
|
|
/* for tcp netconns, do_close_internal ACKs the message */
|
2010-01-29 22:09:31 +00:00
|
|
|
return;
|
2007-07-03 19:29:59 +00:00
|
|
|
} else
|
2007-05-19 16:27:02 +00:00
|
|
|
#endif /* LWIP_TCP */
|
2007-07-03 19:29:59 +00:00
|
|
|
{
|
2010-01-17 18:28:56 +00:00
|
|
|
msg->err = ERR_VAL;
|
2007-07-03 19:29:59 +00:00
|
|
|
}
|
2010-02-12 13:49:21 +00:00
|
|
|
sys_sem_signal(&msg->conn->op_completed);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2007-05-22 08:20:18 +00:00
|
|
|
|
2007-03-11 19:16:38 +00:00
|
|
|
#if LWIP_IGMP
|
2007-06-16 15:07:11 +00:00
|
|
|
/**
|
|
|
|
* Join multicast groups for UDP netconns.
|
|
|
|
* Called from netconn_join_leave_group
|
|
|
|
*
|
|
|
|
* @param msg the api_msg_msg pointing to the connection
|
|
|
|
*/
|
2007-05-11 08:58:23 +00:00
|
|
|
void
|
2007-03-11 19:16:38 +00:00
|
|
|
do_join_leave_group(struct api_msg_msg *msg)
|
2007-05-19 16:27:02 +00:00
|
|
|
{
|
2010-01-17 18:28:56 +00:00
|
|
|
if (ERR_IS_FATAL(msg->conn->last_err)) {
|
|
|
|
msg->err = msg->conn->last_err;
|
|
|
|
} else {
|
2007-04-05 16:54:20 +00:00
|
|
|
if (msg->conn->pcb.tcp != NULL) {
|
2007-05-23 17:46:53 +00:00
|
|
|
if (NETCONNTYPE_GROUP(msg->conn->type) == NETCONN_UDP) {
|
2007-03-11 19:16:38 +00:00
|
|
|
#if LWIP_UDP
|
2007-05-22 09:54:00 +00:00
|
|
|
if (msg->msg.jl.join_or_leave == NETCONN_JOIN) {
|
2010-01-28 18:42:40 +00:00
|
|
|
msg->err = igmp_joingroup(msg->msg.jl.netif_addr, msg->msg.jl.multiaddr);
|
2007-05-22 09:54:00 +00:00
|
|
|
} else {
|
2010-01-28 18:42:40 +00:00
|
|
|
msg->err = igmp_leavegroup(msg->msg.jl.netif_addr, msg->msg.jl.multiaddr);
|
2007-04-05 16:54:20 +00:00
|
|
|
}
|
2007-03-11 19:16:38 +00:00
|
|
|
#endif /* LWIP_UDP */
|
2007-05-19 16:27:02 +00:00
|
|
|
#if (LWIP_TCP || LWIP_RAW)
|
2007-05-23 17:46:53 +00:00
|
|
|
} else {
|
2010-01-17 18:28:56 +00:00
|
|
|
msg->err = ERR_VAL;
|
2007-05-19 16:27:02 +00:00
|
|
|
#endif /* (LWIP_TCP || LWIP_RAW) */
|
2007-04-05 16:54:20 +00:00
|
|
|
}
|
2010-01-17 18:28:56 +00:00
|
|
|
} else {
|
|
|
|
msg->err = ERR_CONN;
|
2007-03-11 19:16:38 +00:00
|
|
|
}
|
|
|
|
}
|
2007-06-08 19:27:59 +00:00
|
|
|
TCPIP_APIMSG_ACK(msg);
|
2007-03-11 19:16:38 +00:00
|
|
|
}
|
|
|
|
#endif /* LWIP_IGMP */
|
2007-05-18 11:27:46 +00:00
|
|
|
|
2007-11-16 17:16:17 +00:00
|
|
|
#if LWIP_DNS
|
|
|
|
/**
|
|
|
|
* Callback function that is called when DNS name is resolved
|
2007-11-24 21:19:47 +00:00
|
|
|
* (or on timeout). A waiting application thread is waked up by
|
|
|
|
* signaling the semaphore.
|
2007-11-16 17:16:17 +00:00
|
|
|
*/
|
|
|
|
static void
|
2010-02-04 18:47:44 +00:00
|
|
|
do_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
|
2007-11-16 17:16:17 +00:00
|
|
|
{
|
|
|
|
struct dns_api_msg *msg = (struct dns_api_msg*)arg;
|
|
|
|
|
|
|
|
LWIP_ASSERT("DNS response for wrong host name", strcmp(msg->name, name) == 0);
|
2010-01-23 17:48:36 +00:00
|
|
|
LWIP_UNUSED_ARG(name);
|
2007-11-16 17:16:17 +00:00
|
|
|
|
|
|
|
if (ipaddr == NULL) {
|
|
|
|
/* timeout or memory error */
|
|
|
|
*msg->err = ERR_VAL;
|
|
|
|
} else {
|
|
|
|
/* address was resolved */
|
|
|
|
*msg->err = ERR_OK;
|
|
|
|
*msg->addr = *ipaddr;
|
|
|
|
}
|
|
|
|
/* wake up the application task waiting in netconn_gethostbyname */
|
2010-02-13 15:43:19 +00:00
|
|
|
sys_sem_signal(msg->sem);
|
2007-11-16 17:16:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute a DNS query
|
|
|
|
* Called from netconn_gethostbyname
|
|
|
|
*
|
2007-11-24 22:13:25 +00:00
|
|
|
* @param arg the dns_api_msg pointing to the query
|
2007-11-16 17:16:17 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
do_gethostbyname(void *arg)
|
|
|
|
{
|
|
|
|
struct dns_api_msg *msg = (struct dns_api_msg*)arg;
|
|
|
|
|
2007-12-13 23:06:49 +00:00
|
|
|
*msg->err = dns_gethostbyname(msg->name, msg->addr, do_dns_found, msg);
|
|
|
|
if (*msg->err != ERR_INPROGRESS) {
|
2007-11-16 17:16:17 +00:00
|
|
|
/* on error or immediate success, wake up the application
|
|
|
|
* task waiting in netconn_gethostbyname */
|
2010-02-13 15:43:19 +00:00
|
|
|
sys_sem_signal(msg->sem);
|
2007-11-16 17:16:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* LWIP_DNS */
|
|
|
|
|
2007-09-07 23:01:59 +00:00
|
|
|
#endif /* LWIP_NETCONN */
|