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
|
|
|
|
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,
|
|
|
|
struct ip_addr *addr)
|
|
|
|
{
|
|
|
|
struct netbuf *buf;
|
|
|
|
struct netconn *conn;
|
|
|
|
|
|
|
|
conn = arg;
|
|
|
|
|
2007-11-02 10:37:08 +00:00
|
|
|
#if LWIP_SO_RCVBUF
|
|
|
|
if ((conn != NULL) && (conn->recvmbox != SYS_MBOX_NULL) &&
|
|
|
|
(((int)(conn->recv_avail) + (int)(p->tot_len)) <= conn->recv_bufsize)) {
|
|
|
|
#else /* LWIP_SO_RCVBUF */
|
2007-07-13 14:28:15 +00:00
|
|
|
if ((conn != NULL) && (conn->recvmbox != SYS_MBOX_NULL)) {
|
2007-11-02 10:37:08 +00:00
|
|
|
#endif /* LWIP_SO_RCVBUF */
|
2007-08-16 18:24:29 +00:00
|
|
|
buf = memp_malloc(MEMP_NETBUF);
|
2007-08-17 00:30:27 +00:00
|
|
|
if (buf == NULL) {
|
2003-11-14 13:17:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
pbuf_ref(p);
|
|
|
|
buf->p = p;
|
|
|
|
buf->ptr = p;
|
2007-05-04 15:18:29 +00:00
|
|
|
buf->addr = addr;
|
|
|
|
buf->port = pcb->protocol;
|
2003-11-14 13:17:23 +00:00
|
|
|
|
|
|
|
conn->recv_avail += p->tot_len;
|
|
|
|
/* Register event with callback */
|
2007-10-24 12:09:18 +00:00
|
|
|
API_EVENT(conn, NETCONN_EVT_RCVPLUS, p->tot_len);
|
2003-11-14 13:17:23 +00:00
|
|
|
sys_mbox_post(conn->recvmbox, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2003-06-09 21:14:47 +00:00
|
|
|
struct ip_addr *addr, u16_t port)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
|
|
|
struct netbuf *buf;
|
|
|
|
struct netconn *conn;
|
|
|
|
|
2007-04-04 19:54:22 +00:00
|
|
|
LWIP_UNUSED_ARG(pcb);
|
2007-03-04 16:25:04 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
conn = arg;
|
2007-05-19 16:24:40 +00:00
|
|
|
|
2007-11-02 10:37:08 +00:00
|
|
|
#if LWIP_SO_RCVBUF
|
|
|
|
if ((conn == NULL) || (conn->recvmbox == SYS_MBOX_NULL) ||
|
|
|
|
(((int)(conn->recv_avail) + (int)(p->tot_len)) > conn->recv_bufsize)) {
|
|
|
|
#else /* LWIP_SO_RCVBUF */
|
2007-07-13 14:28:15 +00:00
|
|
|
if ((conn == NULL) || (conn->recvmbox == SYS_MBOX_NULL)) {
|
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
|
|
|
|
2007-07-13 14:28:15 +00:00
|
|
|
buf = memp_malloc(MEMP_NETBUF);
|
|
|
|
if (buf == NULL) {
|
|
|
|
pbuf_free(p);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
buf->p = p;
|
|
|
|
buf->ptr = p;
|
|
|
|
buf->addr = addr;
|
|
|
|
buf->port = port;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2007-07-13 14:28:15 +00:00
|
|
|
|
|
|
|
conn->recv_avail += p->tot_len;
|
|
|
|
/* Register event with callback */
|
2007-10-24 12:09:18 +00:00
|
|
|
API_EVENT(conn, NETCONN_EVT_RCVPLUS, p->tot_len);
|
2007-07-13 14:28:15 +00:00
|
|
|
sys_mbox_post(conn->recvmbox, buf);
|
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.
|
|
|
|
* Posts the packet to conn->recvmbox or deletes it on memory error.
|
|
|
|
*
|
|
|
|
* @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-05-19 16:24:40 +00:00
|
|
|
|
2003-03-21 10:48:21 +00:00
|
|
|
conn = arg;
|
|
|
|
|
2007-07-13 14:28:15 +00:00
|
|
|
if ((conn == NULL) || (conn->recvmbox == SYS_MBOX_NULL)) {
|
2003-03-21 10:48:21 +00:00
|
|
|
pbuf_free(p);
|
|
|
|
return ERR_VAL;
|
|
|
|
}
|
|
|
|
|
2007-07-13 14:28:15 +00:00
|
|
|
conn->err = err;
|
|
|
|
if (p != NULL) {
|
|
|
|
len = p->tot_len;
|
|
|
|
conn->recv_avail += len;
|
|
|
|
} else {
|
|
|
|
len = 0;
|
|
|
|
}
|
|
|
|
/* Register event with callback */
|
2007-10-24 12:09:18 +00:00
|
|
|
API_EVENT(conn, NETCONN_EVT_RCVPLUS, len);
|
2007-07-13 14:28:15 +00:00
|
|
|
sys_mbox_post(conn->recvmbox, p);
|
|
|
|
|
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)
|
|
|
|
{
|
2007-06-21 18:40:21 +00:00
|
|
|
struct netconn *conn = 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
|
|
|
}
|
2007-06-21 18:40:21 +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
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2007-06-21 18:40:21 +00:00
|
|
|
struct netconn *conn = 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) {
|
2007-07-25 19:24:27 +00:00
|
|
|
LWIP_ASSERT("conn->pcb.tcp != NULL", conn->pcb.tcp != NULL);
|
2007-06-21 18:40:21 +00:00
|
|
|
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) {
|
2007-07-25 19:24:27 +00:00
|
|
|
if ((conn->pcb.tcp != NULL) && (tcp_sndbuf(conn->pcb.tcp) > TCP_SNDLOWAT)) {
|
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;
|
|
|
|
|
|
|
|
conn = 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;
|
|
|
|
|
|
|
|
conn->err = err;
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn->recvmbox != SYS_MBOX_NULL) {
|
2003-02-06 22:18:56 +00:00
|
|
|
/* Register event with callback */
|
2007-10-24 12:09:18 +00:00
|
|
|
API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
|
2002-10-19 12:59:30 +00:00
|
|
|
sys_mbox_post(conn->recvmbox, NULL);
|
|
|
|
}
|
2007-04-05 16:54:20 +00:00
|
|
|
if (conn->mbox != SYS_MBOX_NULL && conn->state == NETCONN_CONNECT) {
|
|
|
|
conn->state = NETCONN_NONE;
|
2002-10-19 12:59:30 +00:00
|
|
|
sys_mbox_post(conn->mbox, NULL);
|
|
|
|
}
|
2003-05-01 13:24:01 +00:00
|
|
|
if (conn->acceptmbox != SYS_MBOX_NULL) {
|
2007-10-24 12:09:18 +00:00
|
|
|
/* Register event with callback */
|
|
|
|
API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
|
2002-10-19 12:59:30 +00:00
|
|
|
sys_mbox_post(conn->acceptmbox, NULL);
|
|
|
|
}
|
2007-07-03 19:29:59 +00:00
|
|
|
if ((conn->state == NETCONN_WRITE) || (conn->state == NETCONN_CLOSE)) {
|
|
|
|
/* calling do_writemore/do_close_internal is not necessary
|
2007-06-21 18:40:21 +00:00
|
|
|
since the pcb has already been deleted! */
|
|
|
|
conn->state = NETCONN_NONE;
|
|
|
|
/* wake up the waiting task */
|
|
|
|
sys_mbox_post(conn->mbox, 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;
|
2003-02-06 22:18:56 +00:00
|
|
|
struct netconn *conn;
|
2007-05-19 16:24:40 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
#if API_MSG_DEBUG
|
|
|
|
#if TCP_DEBUG
|
|
|
|
tcp_debug_print_state(newpcb->state);
|
|
|
|
#endif /* TCP_DEBUG */
|
|
|
|
#endif /* API_MSG_DEBUG */
|
2003-02-06 22:18:56 +00:00
|
|
|
conn = (struct netconn *)arg;
|
2007-06-16 15:07:11 +00:00
|
|
|
|
|
|
|
LWIP_ERROR("accept_function: invalid conn->acceptmbox",
|
2007-06-22 20:50:21 +00:00
|
|
|
conn->acceptmbox != SYS_MBOX_NULL, return ERR_VAL;);
|
2007-06-16 15:07:11 +00:00
|
|
|
|
2003-06-27 20:46:11 +00:00
|
|
|
newconn = memp_malloc(MEMP_NETCONN);
|
2003-05-01 13:24:01 +00:00
|
|
|
if (newconn == NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return ERR_MEM;
|
|
|
|
}
|
|
|
|
newconn->recvmbox = sys_mbox_new();
|
2003-05-01 13:24:01 +00:00
|
|
|
if (newconn->recvmbox == SYS_MBOX_NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
memp_free(MEMP_NETCONN, newconn);
|
|
|
|
return ERR_MEM;
|
|
|
|
}
|
|
|
|
newconn->mbox = sys_mbox_new();
|
2003-05-01 13:24:01 +00:00
|
|
|
if (newconn->mbox == SYS_MBOX_NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
sys_mbox_free(newconn->recvmbox);
|
|
|
|
memp_free(MEMP_NETCONN, newconn);
|
|
|
|
return ERR_MEM;
|
|
|
|
}
|
2006-06-14 08:02:35 +00:00
|
|
|
/* Allocations were OK, setup the PCB etc */
|
|
|
|
newconn->type = NETCONN_TCP;
|
|
|
|
newconn->pcb.tcp = newpcb;
|
|
|
|
setup_tcp(newconn);
|
2007-08-16 18:24:29 +00:00
|
|
|
newconn->state = NETCONN_NONE;
|
2002-10-19 12:59:30 +00:00
|
|
|
newconn->acceptmbox = SYS_MBOX_NULL;
|
|
|
|
newconn->err = err;
|
2003-02-06 22:18:56 +00:00
|
|
|
/* Register event with callback */
|
2007-10-24 12:09:18 +00:00
|
|
|
API_EVENT(conn, NETCONN_EVT_RCVPLUS, 0);
|
2006-06-27 10:33:52 +00:00
|
|
|
/* We have to set the callback here even though
|
|
|
|
* the new socket is unknown. Mark the socket as -1. */
|
|
|
|
newconn->callback = conn->callback;
|
|
|
|
newconn->socket = -1;
|
|
|
|
newconn->recv_avail = 0;
|
2007-03-28 18:21:24 +00:00
|
|
|
#if LWIP_SO_RCVTIMEO
|
|
|
|
newconn->recv_timeout = 0;
|
2007-03-30 09:42:16 +00:00
|
|
|
#endif /* LWIP_SO_RCVTIMEO */
|
2007-11-02 10:37:08 +00:00
|
|
|
#if LWIP_SO_RCVBUF
|
|
|
|
newconn->recv_bufsize = INT_MAX;
|
|
|
|
#endif /* LWIP_SO_RCVBUF */
|
2007-05-19 16:24:40 +00:00
|
|
|
|
2007-06-16 15:07:11 +00:00
|
|
|
sys_mbox_post(conn->acceptmbox, newconn);
|
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
|
|
|
|
*/
|
2007-03-30 09:42:16 +00:00
|
|
|
static err_t
|
|
|
|
pcb_new(struct api_msg_msg *msg)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
2003-11-14 13:17:23 +00:00
|
|
|
msg->conn->err = ERR_OK;
|
|
|
|
|
2007-11-24 21:19:47 +00:00
|
|
|
LWIP_ASSERT("pcb_new: pcb already allocated", msg->conn->pcb.tcp == NULL);
|
|
|
|
|
2003-11-14 13:17:23 +00:00
|
|
|
/* Allocate a PCB for this connection */
|
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
|
|
|
|
case NETCONN_RAW:
|
2007-05-22 09:54:00 +00:00
|
|
|
msg->conn->pcb.raw = raw_new(msg->msg.n.proto);
|
|
|
|
if(msg->conn->pcb.raw == NULL) {
|
|
|
|
msg->conn->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
|
|
|
|
case NETCONN_UDP:
|
2007-05-22 09:54:00 +00:00
|
|
|
msg->conn->pcb.udp = udp_new();
|
|
|
|
if(msg->conn->pcb.udp == NULL) {
|
|
|
|
msg->conn->err = ERR_MEM;
|
|
|
|
break;
|
|
|
|
}
|
2007-06-10 12:02:24 +00:00
|
|
|
#if LWIP_UDPLITE
|
2007-10-22 19:59:52 +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 */
|
2007-10-22 19:59:52 +00:00
|
|
|
if (msg->conn->type==NETCONN_UDPNOCHKSUM) {
|
|
|
|
udp_setflags(msg->conn->pcb.udp, UDP_FLAGS_NOCHKSUM);
|
|
|
|
}
|
2007-05-22 09:54:00 +00:00
|
|
|
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
|
|
|
|
case NETCONN_TCP:
|
2007-05-22 09:54:00 +00:00
|
|
|
msg->conn->pcb.tcp = tcp_new();
|
|
|
|
if(msg->conn->pcb.tcp == NULL) {
|
|
|
|
msg->conn->err = ERR_MEM;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
setup_tcp(msg->conn);
|
|
|
|
break;
|
2007-03-30 09:42:16 +00:00
|
|
|
#endif /* LWIP_TCP */
|
2007-05-22 09:54:00 +00:00
|
|
|
default:
|
2007-06-13 19:00:21 +00:00
|
|
|
/* Unsupported netconn type, e.g. protocol disabled */
|
|
|
|
msg->conn->err = ERR_VAL;
|
2007-05-22 09:54:00 +00:00
|
|
|
break;
|
2003-11-14 13:17:23 +00:00
|
|
|
}
|
2007-05-19 16:24:40 +00:00
|
|
|
|
2007-03-30 09:42:16 +00:00
|
|
|
return msg->conn->err;
|
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)
|
|
|
|
{
|
|
|
|
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. */
|
|
|
|
|
2007-06-08 19:27:59 +00:00
|
|
|
TCPIP_APIMSG_ACK(msg);
|
2007-03-30 09:42:16 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-07-29 08:11:33 +00:00
|
|
|
#if LWIP_TCP
|
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));
|
|
|
|
|
2007-07-25 19:24:27 +00:00
|
|
|
/* Set back some callback pointers */
|
2007-07-03 19:29:59 +00:00
|
|
|
if (conn->pcb.tcp->state == LISTEN) {
|
|
|
|
tcp_arg(conn->pcb.tcp, NULL);
|
|
|
|
tcp_accept(conn->pcb.tcp, NULL);
|
2007-07-25 19:24:27 +00:00
|
|
|
} else {
|
|
|
|
tcp_recv(conn->pcb.tcp, NULL);
|
|
|
|
}
|
|
|
|
/* Try to close the connection */
|
|
|
|
err = tcp_close(conn->pcb.tcp);
|
|
|
|
if (err == ERR_OK) {
|
|
|
|
/* Closing succeeded */
|
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 */
|
|
|
|
tcp_err(conn->pcb.tcp, NULL);
|
|
|
|
tcp_poll(conn->pcb.tcp, NULL, 4);
|
|
|
|
tcp_sent(conn->pcb.tcp, NULL);
|
|
|
|
tcp_recv(conn->pcb.tcp, NULL);
|
|
|
|
tcp_arg(conn->pcb.tcp, NULL);
|
2007-07-03 19:29:59 +00:00
|
|
|
conn->pcb.tcp = NULL;
|
2007-10-22 19:59:52 +00:00
|
|
|
conn->err = ERR_OK;
|
2007-10-24 12:09:18 +00:00
|
|
|
/* Trigger select() in socket layer. This send should something else so the
|
|
|
|
errorfd is set, not the read and write fd! */
|
|
|
|
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 */
|
|
|
|
sys_mbox_post(conn->mbox, NULL);
|
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)
|
|
|
|
{
|
2003-05-01 13:24:01 +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
|
|
|
|
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
|
|
|
|
case NETCONN_UDP:
|
|
|
|
msg->conn->pcb.udp->recv_arg = NULL;
|
|
|
|
udp_remove(msg->conn->pcb.udp);
|
|
|
|
break;
|
|
|
|
#endif /* LWIP_UDP */
|
2007-03-30 09:42:16 +00:00
|
|
|
#if LWIP_TCP
|
2002-10-19 12:59:30 +00:00
|
|
|
case NETCONN_TCP:
|
2007-07-03 19:29:59 +00:00
|
|
|
msg->conn->state = NETCONN_CLOSE;
|
|
|
|
do_close_internal(msg->conn);
|
2007-10-24 12:09:18 +00:00
|
|
|
/* API_EVENT is called inside do_close_internal, before releasing
|
2007-07-03 20:20:33 +00:00
|
|
|
the application thread, so we can return at this point! */
|
|
|
|
return;
|
2007-03-30 09:42:16 +00:00
|
|
|
#endif /* LWIP_TCP */
|
2007-03-21 16:38:58 +00:00
|
|
|
default:
|
2007-05-22 09:54:00 +00:00
|
|
|
break;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
}
|
2007-07-03 20:20:33 +00:00
|
|
|
/* tcp netconns don't come here! */
|
|
|
|
|
2007-10-24 12:09:18 +00:00
|
|
|
/* Trigger select() in socket layer. This send should something else so the
|
|
|
|
errorfd is set, not the read and write fd! */
|
|
|
|
API_EVENT(msg->conn, NETCONN_EVT_RCVPLUS, 0);
|
|
|
|
API_EVENT(msg->conn, NETCONN_EVT_SENDPLUS, 0);
|
2007-05-19 16:24:40 +00:00
|
|
|
|
2007-07-03 20:20:33 +00:00
|
|
|
if (msg->conn->mbox != SYS_MBOX_NULL) {
|
2007-07-24 17:27:48 +00:00
|
|
|
sys_mbox_post(msg->conn->mbox, NULL);
|
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)
|
|
|
|
{
|
2007-05-19 16:27:02 +00:00
|
|
|
if (msg->conn->err == ERR_OK) {
|
|
|
|
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:
|
2007-06-16 15:07:11 +00:00
|
|
|
msg->conn->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:
|
|
|
|
msg->conn->err = udp_bind(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
|
|
|
|
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:
|
|
|
|
msg->conn->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;
|
|
|
|
}
|
2007-06-16 15:07:11 +00:00
|
|
|
} else {
|
|
|
|
/* msg->conn->pcb is NULL */
|
|
|
|
msg->conn->err = ERR_VAL;
|
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;
|
|
|
|
|
2007-04-04 19:54:22 +00:00
|
|
|
LWIP_UNUSED_ARG(pcb);
|
2007-05-19 16:24:40 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
conn = arg;
|
|
|
|
|
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
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
conn->err = err;
|
2003-05-01 13:24:01 +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
|
|
|
}
|
2007-04-05 16:54:20 +00:00
|
|
|
conn->state = NETCONN_NONE;
|
2002-10-19 12:59:30 +00:00
|
|
|
sys_mbox_post(conn->mbox, NULL);
|
|
|
|
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) {
|
2007-06-16 15:07:11 +00:00
|
|
|
sys_mbox_post(msg->conn->mbox, NULL);
|
|
|
|
return;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2007-03-30 09:42:16 +00:00
|
|
|
|
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
|
|
|
|
case NETCONN_RAW:
|
2007-05-19 16:27:02 +00:00
|
|
|
msg->conn->err = raw_connect(msg->conn->pcb.raw, msg->msg.bc.ipaddr);
|
2003-11-14 13:17:23 +00:00
|
|
|
sys_mbox_post(msg->conn->mbox, NULL);
|
|
|
|
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:
|
2007-05-19 16:27:02 +00:00
|
|
|
msg->conn->err = udp_connect(msg->conn->pcb.udp, msg->msg.bc.ipaddr, msg->msg.bc.port);
|
2002-10-19 12:59:30 +00:00
|
|
|
sys_mbox_post(msg->conn->mbox, NULL);
|
|
|
|
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:
|
2007-04-05 16:54:20 +00:00
|
|
|
msg->conn->state = NETCONN_CONNECT;
|
2002-10-19 12:59:30 +00:00
|
|
|
setup_tcp(msg->conn);
|
2007-05-19 16:27:02 +00:00
|
|
|
msg->conn->err = tcp_connect(msg->conn->pcb.tcp, msg->msg.bc.ipaddr, msg->msg.bc.port,
|
2007-06-21 18:40:21 +00:00
|
|
|
do_connected);
|
2007-10-22 19:59:52 +00:00
|
|
|
/* sys_mbox_post() is called from do_connected (or err_tcp()),
|
|
|
|
* when the connection is established! */
|
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:
|
2002-10-19 12:59:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
}
|
2007-05-23 17:46:53 +00:00
|
|
|
#endif /* LWIP_UDP */
|
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
|
2007-05-19 16:27:02 +00:00
|
|
|
if (msg->conn->err == ERR_OK) {
|
|
|
|
if (msg->conn->pcb.tcp != NULL) {
|
|
|
|
if (msg->conn->type == NETCONN_TCP) {
|
2007-07-24 07:41:55 +00:00
|
|
|
if (msg->conn->pcb.tcp->state == CLOSED) {
|
|
|
|
struct tcp_pcb* lpcb = tcp_listen(msg->conn->pcb.tcp);
|
|
|
|
if (lpcb == NULL) {
|
|
|
|
msg->conn->err = ERR_MEM;
|
|
|
|
} else {
|
|
|
|
if (msg->conn->acceptmbox == SYS_MBOX_NULL) {
|
|
|
|
if ((msg->conn->acceptmbox = sys_mbox_new()) == SYS_MBOX_NULL) {
|
|
|
|
msg->conn->err = ERR_MEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (msg->conn->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);
|
2007-05-19 16:27:02 +00:00
|
|
|
}
|
|
|
|
}
|
2007-07-24 07:41:55 +00:00
|
|
|
} else {
|
|
|
|
msg->conn->err = ERR_CONN;
|
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)
|
|
|
|
{
|
2007-05-19 16:27:02 +00:00
|
|
|
if (msg->conn->err == ERR_OK) {
|
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) {
|
2007-05-16 18:55:25 +00:00
|
|
|
msg->conn->err = raw_send(msg->conn->pcb.raw, msg->msg.b->p);
|
2007-05-04 15:18:29 +00:00
|
|
|
} else {
|
2007-05-16 18:55:25 +00:00
|
|
|
msg->conn->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) {
|
2007-05-16 18:55:25 +00:00
|
|
|
msg->conn->err = udp_send(msg->conn->pcb.udp, msg->msg.b->p);
|
2007-05-04 15:18:29 +00:00
|
|
|
} else {
|
2007-05-16 18:55:25 +00:00
|
|
|
msg->conn->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
|
|
|
/**
|
|
|
|
* Recv some data from a RAW or UDP pcb contained in a netconn
|
|
|
|
* 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
|
2007-05-19 16:27:02 +00:00
|
|
|
if (msg->conn->err == ERR_OK) {
|
|
|
|
if (msg->conn->pcb.tcp != NULL) {
|
|
|
|
if (msg->conn->type == NETCONN_TCP) {
|
2007-05-22 09:54:00 +00:00
|
|
|
tcp_recved(msg->conn->pcb.tcp, msg->msg.r.len);
|
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)
|
|
|
|
{
|
|
|
|
err_t err;
|
|
|
|
void *dataptr;
|
|
|
|
u16_t len, available;
|
|
|
|
u8_t write_finished = 0;
|
|
|
|
|
2007-06-21 19:14:02 +00:00
|
|
|
LWIP_ASSERT("conn->state == NETCONN_WRITE", (conn->state == NETCONN_WRITE));
|
2007-06-21 18:40:21 +00:00
|
|
|
|
|
|
|
dataptr = (u8_t*)conn->write_msg->msg.w.dataptr + conn->write_offset;
|
2007-06-21 19:32:26 +00:00
|
|
|
if ((conn->write_msg->msg.w.len - conn->write_offset > 0xffff)) { /* max_u16_t */
|
|
|
|
len = 0xffff;
|
|
|
|
#if LWIP_TCPIP_CORE_LOCKING
|
|
|
|
conn->write_delayed = 1;
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
len = conn->write_msg->msg.w.len - conn->write_offset;
|
|
|
|
}
|
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
|
|
|
|
conn->write_delayed = 1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-11-01 17:37:50 +00:00
|
|
|
err = tcp_write(conn->pcb.tcp, dataptr, len, conn->write_msg->msg.w.apiflags);
|
2007-06-21 18:40:21 +00:00
|
|
|
LWIP_ASSERT("do_writemore: invalid length!", ((conn->write_offset + len) <= conn->write_msg->msg.w.len));
|
|
|
|
if (err == ERR_OK) {
|
|
|
|
conn->write_offset += len;
|
|
|
|
if (conn->write_offset == conn->write_msg->msg.w.len) {
|
|
|
|
/* everything was written */
|
|
|
|
write_finished = 1;
|
|
|
|
conn->write_msg = NULL;
|
|
|
|
conn->write_offset = 0;
|
|
|
|
}
|
2007-06-21 18:55:09 +00:00
|
|
|
err = tcp_output_nagle(conn->pcb.tcp);
|
2007-06-21 18:40:21 +00:00
|
|
|
conn->err = err;
|
2007-10-24 12:09:18 +00:00
|
|
|
if ((err == ERR_OK) && (tcp_sndbuf(conn->pcb.tcp) <= TCP_SNDLOWAT)) {
|
|
|
|
API_EVENT(conn, NETCONN_EVT_SENDMINUS, len);
|
2007-06-21 18:40:21 +00:00
|
|
|
}
|
|
|
|
} else if (err == ERR_MEM) {
|
2007-11-21 17:59:03 +00:00
|
|
|
/* tcp_enqueue returned ERR_MEM, try tcp_output anyway */
|
|
|
|
err = tcp_output(conn->pcb.tcp);
|
|
|
|
|
2007-06-21 18:40:21 +00:00
|
|
|
#if LWIP_TCPIP_CORE_LOCKING
|
|
|
|
conn->write_delayed = 1;
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
/* if ERR_MEM, we wait for sent_tcp or poll_tcp to be called */
|
|
|
|
conn->err = err;
|
|
|
|
/* since we've had an error (except temporary running out of memory,
|
|
|
|
we don't try writing any more */
|
|
|
|
write_finished = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (write_finished) {
|
|
|
|
/* everything was written: set back connection state
|
|
|
|
and back to application task */
|
|
|
|
conn->state = NETCONN_NONE;
|
|
|
|
#if LWIP_TCPIP_CORE_LOCKING
|
|
|
|
if (conn->write_delayed != 0)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
sys_mbox_post(conn->mbox, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#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)
|
|
|
|
{
|
2007-05-19 16:27:02 +00:00
|
|
|
if (msg->conn->err == ERR_OK) {
|
2007-06-21 18:40:21 +00:00
|
|
|
if ((msg->conn->pcb.tcp != NULL) && (msg->conn->type == NETCONN_TCP)) {
|
2007-05-22 09:54:00 +00:00
|
|
|
#if LWIP_TCP
|
2007-06-21 18:40:21 +00:00
|
|
|
msg->conn->state = NETCONN_WRITE;
|
|
|
|
/* set all the variables used by do_writemore */
|
|
|
|
msg->conn->write_msg = msg;
|
|
|
|
msg->conn->write_offset = 0;
|
|
|
|
#if LWIP_TCPIP_CORE_LOCKING
|
|
|
|
msg->conn->write_delayed = 0;
|
2007-06-21 19:14:02 +00:00
|
|
|
if (do_writemore(msg->conn) != ERR_OK) {
|
2007-06-21 18:40:21 +00:00
|
|
|
LWIP_ASSERT("state!", msg->conn->state == NETCONN_WRITE);
|
|
|
|
UNLOCK_TCPIP_CORE();
|
|
|
|
sys_arch_mbox_fetch(msg->conn->mbox, NULL, 0);
|
|
|
|
LOCK_TCPIP_CORE();
|
2007-06-21 19:14:02 +00:00
|
|
|
LWIP_ASSERT("state!", msg->conn->state == NETCONN_NONE);
|
2007-06-21 18:40:21 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
do_writemore(msg->conn);
|
|
|
|
#endif
|
|
|
|
/* for both cases: if do_writemore was called, don't ACK the APIMSG! */
|
|
|
|
return;
|
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 {
|
|
|
|
msg->conn->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) {
|
|
|
|
*(msg->msg.ad.ipaddr) = (msg->msg.ad.local?msg->conn->pcb.ip->local_ip:msg->conn->pcb.ip->remote_ip);
|
|
|
|
|
|
|
|
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 */
|
|
|
|
msg->conn->err = ERR_CONN;
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
msg->conn->err = ERR_CONN;
|
|
|
|
} 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 {
|
|
|
|
msg->conn->err =ERR_CONN;
|
|
|
|
}
|
|
|
|
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
|
2007-07-03 19:29:59 +00:00
|
|
|
if ((msg->conn->pcb.tcp != NULL) && (msg->conn->type == NETCONN_TCP)) {
|
|
|
|
msg->conn->state = NETCONN_CLOSE;
|
|
|
|
do_close_internal(msg->conn);
|
|
|
|
/* for tcp netconns, do_close_internal ACKs the message */
|
|
|
|
} else
|
2007-05-19 16:27:02 +00:00
|
|
|
#endif /* LWIP_TCP */
|
2007-07-03 19:29:59 +00:00
|
|
|
{
|
|
|
|
msg->conn->err = ERR_VAL;
|
|
|
|
TCPIP_APIMSG_ACK(msg);
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
if (msg->conn->err == ERR_OK) {
|
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) {
|
2007-10-22 19:59:52 +00:00
|
|
|
msg->conn->err = igmp_joingroup(msg->msg.jl.interface, msg->msg.jl.multiaddr);
|
2007-05-22 09:54:00 +00:00
|
|
|
} else {
|
2007-10-22 19:59:52 +00:00
|
|
|
msg->conn->err = igmp_leavegroup(msg->msg.jl.interface, 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 {
|
2007-05-19 16:27:02 +00:00
|
|
|
msg->conn->err = ERR_VAL;
|
|
|
|
#endif /* (LWIP_TCP || LWIP_RAW) */
|
2007-04-05 16:54:20 +00:00
|
|
|
}
|
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
|
|
|
|
do_dns_found(const char *name, struct ip_addr *ipaddr, void *arg)
|
|
|
|
{
|
|
|
|
struct dns_api_msg *msg = (struct dns_api_msg*)arg;
|
|
|
|
|
|
|
|
LWIP_ASSERT("DNS response for wrong host name", strcmp(msg->name, name) == 0);
|
|
|
|
|
|
|
|
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 */
|
|
|
|
sys_sem_signal(msg->sem);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
DNS_RESULT res;
|
|
|
|
struct dns_api_msg *msg = (struct dns_api_msg*)arg;
|
|
|
|
|
|
|
|
res = dns_gethostbyname(msg->name, msg->addr, do_dns_found, msg);
|
|
|
|
if (res != DNS_QUERY_QUEUED) {
|
|
|
|
/* If not queued, return to app thread directly */
|
2007-11-18 14:19:27 +00:00
|
|
|
if (res == DNS_COMPLETE) {
|
2007-11-16 17:16:17 +00:00
|
|
|
/* name was already in octet notation or cached */
|
|
|
|
*msg->err = ERR_OK;
|
2007-11-19 22:42:01 +00:00
|
|
|
} else if (res == DNS_ERR_MEM) {
|
|
|
|
/* memory allocation error */
|
|
|
|
*msg->err = ERR_MEM;
|
2007-11-18 14:19:27 +00:00
|
|
|
} else {
|
|
|
|
/* some error occurred */
|
|
|
|
*msg->err = ERR_ARG;
|
|
|
|
};
|
2007-11-16 17:16:17 +00:00
|
|
|
/* on error or immediate success, wake up the application
|
|
|
|
* task waiting in netconn_gethostbyname */
|
|
|
|
sys_sem_signal(msg->sem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* LWIP_DNS */
|
|
|
|
|
2007-09-07 23:01:59 +00:00
|
|
|
#endif /* LWIP_NETCONN */
|
2007-11-12 22:39:24 +00:00
|
|
|
|