mirror of
https://github.com/apache/nuttx.git
synced 2026-05-31 23:40:19 +08:00
Move TCP files from net/uip to net/tcp
This commit is contained in:
@@ -0,0 +1,223 @@
|
||||
/****************************************************************************
|
||||
* net/tcp/tcp_appsend.c
|
||||
*
|
||||
* Copyright (C) 2007-2010, 2014 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Adapted for NuttX from logic in uIP which also has a BSD-like license:
|
||||
*
|
||||
* Original author Adam Dunkels <adam@dunkels.com>
|
||||
* Copyright () 2001-2003, Adam Dunkels.
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP)
|
||||
|
||||
#include <stdint.h>
|
||||
#include <assert.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <nuttx/net/uip/uipopt.h>
|
||||
#include <nuttx/net/uip/uip.h>
|
||||
#include <nuttx/net/uip/uip-arch.h>
|
||||
|
||||
#include "uip/uip_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: uip_tcpappsend
|
||||
*
|
||||
* Description:
|
||||
* Handle application or TCP protocol response. If this function is called
|
||||
* with dev->d_sndlen > 0, then this is an application attempting to send
|
||||
* packet.
|
||||
*
|
||||
* Parameters:
|
||||
* dev - The device driver structure to use in the send operation
|
||||
* conn - The TCP connection structure holding connection information
|
||||
* result - App result event sent
|
||||
*
|
||||
* Return:
|
||||
* None
|
||||
*
|
||||
* Assumptions:
|
||||
* Called from the interrupt level or with interrupts disabled.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void uip_tcpappsend(struct uip_driver_s *dev, struct uip_conn *conn,
|
||||
uint16_t result)
|
||||
{
|
||||
/* Handle the result based on the application response */
|
||||
|
||||
nllvdbg("result: %04x d_sndlen: %d conn->unacked: %d\n",
|
||||
result, dev->d_sndlen, conn->unacked);
|
||||
|
||||
/* Check for connection aborted */
|
||||
|
||||
if ((result & UIP_ABORT) != 0)
|
||||
{
|
||||
dev->d_sndlen = 0;
|
||||
conn->tcpstateflags = UIP_CLOSED;
|
||||
nllvdbg("TCP state: UIP_CLOSED\n");
|
||||
|
||||
uip_tcpsend(dev, conn, TCP_RST | TCP_ACK, UIP_IPTCPH_LEN);
|
||||
}
|
||||
|
||||
/* Check for connection closed */
|
||||
|
||||
else if ((result & UIP_CLOSE) != 0)
|
||||
{
|
||||
conn->tcpstateflags = UIP_FIN_WAIT_1;
|
||||
conn->unacked = 1;
|
||||
conn->nrtx = 0;
|
||||
nllvdbg("TCP state: UIP_FIN_WAIT_1\n");
|
||||
|
||||
dev->d_sndlen = 0;
|
||||
uip_tcpsend(dev, conn, TCP_FIN | TCP_ACK, UIP_IPTCPH_LEN);
|
||||
}
|
||||
|
||||
/* None of the above */
|
||||
|
||||
else
|
||||
{
|
||||
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
|
||||
DEBUGASSERT(dev->d_sndlen >= 0 && dev->d_sndlen <= conn->mss);
|
||||
#else
|
||||
/* If d_sndlen > 0, the application has data to be sent. */
|
||||
|
||||
if (dev->d_sndlen > 0)
|
||||
{
|
||||
/* Remember how much data we send out now so that we know
|
||||
* when everything has been acknowledged. Just increment the amount
|
||||
* of data sent. This will be needed in sequence number calculations
|
||||
* and we know that this is not a re-transmission. Retransmissions
|
||||
* do not go through this path.
|
||||
*/
|
||||
|
||||
conn->unacked += dev->d_sndlen;
|
||||
|
||||
/* The application cannot send more than what is allowed by the
|
||||
* MSS (the minumum of the MSS and the available window).
|
||||
*/
|
||||
|
||||
DEBUGASSERT(dev->d_sndlen <= conn->mss);
|
||||
}
|
||||
|
||||
conn->nrtx = 0;
|
||||
#endif
|
||||
/* Then handle the rest of the operation just as for the rexmit case */
|
||||
|
||||
uip_tcprexmit(dev, conn, result);
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: uip_tcprexmit
|
||||
*
|
||||
* Description:
|
||||
* Handle application retransmission
|
||||
*
|
||||
* Parameters:
|
||||
* dev - The device driver structure to use in the send operation
|
||||
* conn - The TCP connection structure holding connection information
|
||||
* result - App result event sent
|
||||
*
|
||||
* Return:
|
||||
* None
|
||||
*
|
||||
* Assumptions:
|
||||
* Called from the interrupt level or with interrupts disabled.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void uip_tcprexmit(struct uip_driver_s *dev, struct uip_conn *conn,
|
||||
uint16_t result)
|
||||
{
|
||||
nllvdbg("result: %04x d_sndlen: %d conn->unacked: %d\n",
|
||||
result, dev->d_sndlen, conn->unacked);
|
||||
|
||||
dev->d_appdata = dev->d_snddata;
|
||||
|
||||
/* If the application has data to be sent, or if the incoming packet had
|
||||
* new data in it, we must send out a packet.
|
||||
*/
|
||||
|
||||
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
|
||||
if (dev->d_sndlen > 0)
|
||||
#else
|
||||
if (dev->d_sndlen > 0 && conn->unacked > 0)
|
||||
#endif
|
||||
{
|
||||
/* We always set the ACK flag in response packets adding the length of
|
||||
* the IP and TCP headers.
|
||||
*/
|
||||
|
||||
uip_tcpsend(dev, conn, TCP_ACK | TCP_PSH, dev->d_sndlen + UIP_TCPIP_HLEN);
|
||||
}
|
||||
|
||||
/* If there is no data to send, just send out a pure ACK if one is requested`. */
|
||||
|
||||
else if ((result & UIP_SNDACK) != 0)
|
||||
{
|
||||
uip_tcpsend(dev, conn, TCP_ACK, UIP_TCPIP_HLEN);
|
||||
}
|
||||
|
||||
/* There is nothing to do -- drop the packet */
|
||||
|
||||
else
|
||||
{
|
||||
dev->d_len = 0;
|
||||
}
|
||||
}
|
||||
#endif /* CONFIG_NET && CONFIG_NET_TCP */
|
||||
@@ -0,0 +1,406 @@
|
||||
/****************************************************************************
|
||||
* net/tcp/tcp_backlog.c
|
||||
*
|
||||
* Copyright (C) 2008-2009, 2011-2013 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "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
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/net/uip/uipopt.h>
|
||||
#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP) && defined(CONFIG_NET_TCPBACKLOG)
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <queue.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <nuttx/kmalloc.h>
|
||||
#include <nuttx/net/uip/uip.h>
|
||||
#include <nuttx/net/uip/uip-tcp.h>
|
||||
|
||||
#include "uip/uip_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Function: uip_backlogcreate
|
||||
*
|
||||
* Description:
|
||||
* Called from the listen() logic to setup the backlog as specified in the
|
||||
* the listen arguments.
|
||||
*
|
||||
* Assumptions:
|
||||
* Called from normal user code. Interrupts may be disabled.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int uip_backlogcreate(FAR struct uip_conn *conn, int nblg)
|
||||
{
|
||||
FAR struct uip_backlog_s *bls = NULL;
|
||||
FAR struct uip_blcontainer_s *blc;
|
||||
uip_lock_t flags;
|
||||
int size;
|
||||
int offset;
|
||||
int i;
|
||||
|
||||
nllvdbg("conn=%p nblg=%d\n", conn, nblg);
|
||||
|
||||
#ifdef CONFIG_DEBUG
|
||||
if (!conn)
|
||||
{
|
||||
return -EINVAL;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Then allocate the backlog as requested */
|
||||
|
||||
if (nblg > 0)
|
||||
{
|
||||
/* Align the list of backlog structures to 32-bit boundaries. This
|
||||
* may be excessive on 24-16-bit address machines; and insufficient
|
||||
* on 64-bit address machines -- REVISIT
|
||||
*/
|
||||
|
||||
offset = (sizeof(struct uip_backlog_s) + 3) & ~3;
|
||||
|
||||
/* Then determine the full size of the allocation include the
|
||||
* uip_backlog_s, a pre-allocated array of struct uip_blcontainer_s
|
||||
* and alignment padding
|
||||
*/
|
||||
|
||||
size = offset + nblg * sizeof(struct uip_blcontainer_s);
|
||||
|
||||
/* Then allocate that much */
|
||||
|
||||
bls = (FAR struct uip_backlog_s *)kzalloc(size);
|
||||
if (!bls)
|
||||
{
|
||||
nlldbg("Failed to allocate backlog\n");
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
/* Then add all of the pre-allocated containers to the free list */
|
||||
|
||||
blc = (FAR struct uip_blcontainer_s*)(((FAR uint8_t*)bls) + offset);
|
||||
for (i = 0; i < nblg; i++)
|
||||
{
|
||||
sq_addfirst(&blc->bc_node, &bls->bl_free);
|
||||
blc++;
|
||||
}
|
||||
}
|
||||
|
||||
/* Destroy any existing backlog (shouldn't be any) */
|
||||
|
||||
flags = uip_lock();
|
||||
uip_backlogdestroy(conn);
|
||||
|
||||
/* Now install the backlog tear-off in the connection. NOTE that bls may
|
||||
* actually be NULL if nblg is <= 0; In that case, we are disabling backlog
|
||||
* support. Since interrupts are disabled, destroying the old backlog and
|
||||
* replace it with the new is an atomic operation
|
||||
*/
|
||||
|
||||
conn->backlog = bls;
|
||||
uip_unlock(flags);
|
||||
return OK;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Function: uip_backlogdestroy
|
||||
*
|
||||
* Description:
|
||||
* (1) Called from uip_tcpfree() whenever a connection is freed.
|
||||
* (2) Called from uip_backlogcreate() to destroy any old backlog
|
||||
*
|
||||
* NOTE: This function may re-enter uip_tcpfree when a connection that
|
||||
* is freed that has pending connections.
|
||||
*
|
||||
* Assumptions:
|
||||
* The caller has disabled interrupts so that there can be no conflict
|
||||
* with ongoing, interrupt driven activity
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int uip_backlogdestroy(FAR struct uip_conn *conn)
|
||||
{
|
||||
FAR struct uip_backlog_s *blg;
|
||||
FAR struct uip_blcontainer_s *blc;
|
||||
FAR struct uip_conn *blconn;
|
||||
|
||||
nllvdbg("conn=%p\n", conn);
|
||||
|
||||
#ifdef CONFIG_DEBUG
|
||||
if (!conn)
|
||||
{
|
||||
return -EINVAL;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Make sure that the connection has a backlog to be destroyed */
|
||||
|
||||
if (conn->backlog)
|
||||
{
|
||||
/* Remove the backlog structure reference from the connection */
|
||||
|
||||
blg = conn->backlog;
|
||||
conn->backlog = NULL;
|
||||
|
||||
/* Handle any pending connections in the backlog */
|
||||
|
||||
while ((blc = (FAR struct uip_blcontainer_s*)sq_remfirst(&blg->bl_pending)) != NULL)
|
||||
{
|
||||
blconn = blc->bc_conn;
|
||||
if (blconn)
|
||||
{
|
||||
/* REVISIT -- such connections really need to be gracefully closed */
|
||||
|
||||
blconn->blparent = NULL;
|
||||
blconn->backlog = NULL;
|
||||
blconn->crefs = 0;
|
||||
uip_tcpfree(blconn);
|
||||
}
|
||||
}
|
||||
|
||||
/* Then free the entire backlog structure */
|
||||
|
||||
kfree(blg);
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Function: uip_backlogadd
|
||||
*
|
||||
* Description:
|
||||
* Called uip_listen when a new connection is made with a listener socket
|
||||
* but when there is no accept() in place to receive the connection. This
|
||||
* function adds the new connection to the backlog.
|
||||
*
|
||||
* Assumptions:
|
||||
* Called from the interrupt level with interrupts disabled
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int uip_backlogadd(FAR struct uip_conn *conn, FAR struct uip_conn *blconn)
|
||||
{
|
||||
FAR struct uip_backlog_s *bls;
|
||||
FAR struct uip_blcontainer_s *blc;
|
||||
int ret = -EINVAL;
|
||||
|
||||
nllvdbg("conn=%p blconn=%p\n", conn, blconn);
|
||||
|
||||
#ifdef CONFIG_DEBUG
|
||||
if (!conn)
|
||||
{
|
||||
return -EINVAL;
|
||||
}
|
||||
#endif
|
||||
|
||||
bls = conn->backlog;
|
||||
if (bls && blconn)
|
||||
{
|
||||
/* Allocate a container for the connection from the free list */
|
||||
|
||||
blc = (FAR struct uip_blcontainer_s *)sq_remfirst(&bls->bl_free);
|
||||
if (!blc)
|
||||
{
|
||||
nlldbg("Failed to allocate container\n");
|
||||
ret = -ENOMEM;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Save the connection reference in the container and put the
|
||||
* container at the end of the pending connection list (FIFO).
|
||||
*/
|
||||
|
||||
blc->bc_conn = blconn;
|
||||
sq_addlast(&blc->bc_node, &bls->bl_pending);
|
||||
ret = OK;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Function: uip_backlogremove
|
||||
*
|
||||
* Description:
|
||||
* Called from poll(). Before waiting for a new connection, poll will
|
||||
* call this API to see if there are pending connections in the backlog.
|
||||
*
|
||||
* Assumptions:
|
||||
* Called from normal user code, but with interrupts disabled,
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_DISABLE_POLL
|
||||
bool uip_backlogavailable(FAR struct uip_conn *conn)
|
||||
{
|
||||
return (conn && conn->backlog && !sq_empty(&conn->backlog->bl_pending));
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Function: uip_backlogremove
|
||||
*
|
||||
* Description:
|
||||
* Called from accept(). Before waiting for a new connection, accept will
|
||||
* call this API to see if there are pending connections in the backlog.
|
||||
*
|
||||
* Assumptions:
|
||||
* Called from normal user code, but with interrupts disabled,
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
struct uip_conn *uip_backlogremove(FAR struct uip_conn *conn)
|
||||
{
|
||||
FAR struct uip_backlog_s *bls;
|
||||
FAR struct uip_blcontainer_s *blc;
|
||||
FAR struct uip_conn *blconn = NULL;
|
||||
|
||||
#ifdef CONFIG_DEBUG
|
||||
if (!conn)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
#endif
|
||||
|
||||
bls = conn->backlog;
|
||||
if (bls)
|
||||
{
|
||||
/* Remove the a container at the head of the pending connection list
|
||||
* (FIFO)
|
||||
*/
|
||||
|
||||
blc = (FAR struct uip_blcontainer_s *)sq_remfirst(&bls->bl_pending);
|
||||
if (blc)
|
||||
{
|
||||
/* Extract the connection reference from the container and put
|
||||
* container in the free list
|
||||
*/
|
||||
|
||||
blconn = blc->bc_conn;
|
||||
blc->bc_conn = NULL;
|
||||
sq_addlast(&blc->bc_node, &bls->bl_free);
|
||||
}
|
||||
}
|
||||
|
||||
nllvdbg("conn=%p, returning %p\n", conn, blconn);
|
||||
return blconn;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Function: uip_backlogdelete
|
||||
*
|
||||
* Description:
|
||||
* Called from uip_tcpfree() when a connection is freed that this also
|
||||
* retained in the pending connectino list of a listener. We simply need
|
||||
* to remove the defunct connecton from the list.
|
||||
*
|
||||
* Assumptions:
|
||||
* Called from the interrupt level with interrupts disabled
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int uip_backlogdelete(FAR struct uip_conn *conn, FAR struct uip_conn *blconn)
|
||||
{
|
||||
FAR struct uip_backlog_s *bls;
|
||||
FAR struct uip_blcontainer_s *blc;
|
||||
FAR struct uip_blcontainer_s *prev;
|
||||
|
||||
nllvdbg("conn=%p blconn=%p\n", conn, blconn);
|
||||
|
||||
#ifdef CONFIG_DEBUG
|
||||
if (!conn)
|
||||
{
|
||||
return -EINVAL;
|
||||
}
|
||||
#endif
|
||||
|
||||
bls = conn->backlog;
|
||||
if (bls)
|
||||
{
|
||||
/* Find the container hold the connection */
|
||||
|
||||
for (blc = (FAR struct uip_blcontainer_s *)sq_peek(&bls->bl_pending), prev = NULL;
|
||||
blc;
|
||||
prev = blc, blc = (FAR struct uip_blcontainer_s *)sq_next(&blc->bc_node))
|
||||
{
|
||||
if (blc->bc_conn == blconn)
|
||||
{
|
||||
if (prev)
|
||||
{
|
||||
/* Remove the a container from the middle of the list of
|
||||
* pending connections
|
||||
*/
|
||||
|
||||
(void)sq_remafter(&prev->bc_node, &bls->bl_pending);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Remove the a container from the head of the list of
|
||||
* pending connections
|
||||
*/
|
||||
|
||||
(void)sq_remfirst(&bls->bl_pending);
|
||||
}
|
||||
|
||||
/* Put container in the free list */
|
||||
|
||||
blc->bc_conn = NULL;
|
||||
sq_addlast(&blc->bc_node, &bls->bl_free);
|
||||
return OK;
|
||||
}
|
||||
}
|
||||
|
||||
nlldbg("Failed to find pending connection\n");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
return OK;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_NET && CONFIG_NET_TCP && CONFIG_NET_TCPBACKLOG */
|
||||
@@ -0,0 +1,338 @@
|
||||
/****************************************************************************
|
||||
* net/tcp/tcp_callback.c
|
||||
*
|
||||
* Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "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
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP)
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <nuttx/net/uip/uipopt.h>
|
||||
#include <nuttx/net/uip/uip.h>
|
||||
#include <nuttx/net/uip/uip-arch.h>
|
||||
|
||||
#include "uip/uip_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Function: uip_readahead
|
||||
*
|
||||
* Description:
|
||||
* Copy as much received data as possible into the read-ahead buffer
|
||||
*
|
||||
* Assumptions:
|
||||
* This function is called at the interrupt level with interrupts disabled.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_NET_TCP_READAHEAD
|
||||
static int uip_readahead(FAR struct uip_readahead_s *readahead,
|
||||
FAR uint8_t *buf, int len)
|
||||
{
|
||||
int available = CONFIG_NET_TCP_READAHEAD_BUFSIZE - readahead->rh_nbytes;
|
||||
int recvlen = 0;
|
||||
|
||||
if (len > 0 && available > 0)
|
||||
{
|
||||
/* Get the length of the data to buffer. */
|
||||
|
||||
if (len > available)
|
||||
{
|
||||
recvlen = available;
|
||||
}
|
||||
else
|
||||
{
|
||||
recvlen = len;
|
||||
}
|
||||
|
||||
/* Copy the new appdata into the read-ahead buffer */
|
||||
|
||||
memcpy(&readahead->rh_buffer[readahead->rh_nbytes], buf, recvlen);
|
||||
readahead->rh_nbytes += recvlen;
|
||||
}
|
||||
|
||||
return recvlen;
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Function: uip_dataevent
|
||||
*
|
||||
* Description:
|
||||
* Handle data that is not accepted by the application because there is no
|
||||
* listener in place ready to receive the data.
|
||||
*
|
||||
* Assumptions:
|
||||
* - The caller has checked that UIP_NEWDATA is set in flags and that is no
|
||||
* other handler available to process the incoming data.
|
||||
* - This function is called at the interrupt level with interrupts disabled.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static inline uint16_t
|
||||
uip_dataevent(FAR struct uip_driver_s *dev, FAR struct uip_conn *conn,
|
||||
uint16_t flags)
|
||||
{
|
||||
uint16_t ret;
|
||||
|
||||
/* Assume that we will ACK the data. The data will be ACKed if it is
|
||||
* placed in the read-ahead buffer -OR- if it zero length
|
||||
*/
|
||||
|
||||
ret = (flags & ~UIP_NEWDATA) | UIP_SNDACK;
|
||||
|
||||
/* Is there new data? With non-zero length? (Certain connection events
|
||||
* can have zero-length with UIP_NEWDATA set just to cause an ACK).
|
||||
*/
|
||||
|
||||
if (dev->d_len > 0)
|
||||
{
|
||||
#ifdef CONFIG_NET_TCP_READAHEAD
|
||||
uint8_t *buffer = dev->d_appdata;
|
||||
int buflen = dev->d_len;
|
||||
uint16_t recvlen;
|
||||
#endif
|
||||
|
||||
nllvdbg("No listener on connection\n");
|
||||
|
||||
#ifdef CONFIG_NET_TCP_READAHEAD
|
||||
/* Save as much data as possible in the read-ahead buffers */
|
||||
|
||||
recvlen = uip_datahandler(conn, buffer, buflen);
|
||||
|
||||
/* There are several complicated buffering issues that are not addressed
|
||||
* properly here. For example, what if we cannot buffer the entire
|
||||
* packet? In that case, some data will be accepted but not ACKed.
|
||||
* Therefore it will be resent and duplicated. Fixing this could be tricky.
|
||||
*/
|
||||
|
||||
if (recvlen < buflen)
|
||||
#endif
|
||||
{
|
||||
/* There is no handler to receive new data and there are no free
|
||||
* read-ahead buffers to retain the data -- drop the packet.
|
||||
*/
|
||||
|
||||
nllvdbg("Dropped %d bytes\n", dev->d_len);
|
||||
|
||||
#ifdef CONFIG_NET_STATISTICS
|
||||
uip_stat.tcp.syndrop++;
|
||||
uip_stat.tcp.drop++;
|
||||
#endif
|
||||
/* Clear the UIP_SNDACK bit so that no ACK will be sent */
|
||||
|
||||
ret &= ~UIP_SNDACK;
|
||||
}
|
||||
}
|
||||
|
||||
/* In any event, the new data has now been handled */
|
||||
|
||||
dev->d_len = 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Function: uip_tcpcallback
|
||||
*
|
||||
* Description:
|
||||
* Inform the application holding the TCP socket of a change in state.
|
||||
*
|
||||
* Assumptions:
|
||||
* This function is called at the interrupt level with interrupts disabled.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
uint16_t uip_tcpcallback(struct uip_driver_s *dev, struct uip_conn *conn,
|
||||
uint16_t flags)
|
||||
{
|
||||
/* Preserve the UIP_ACKDATA, UIP_CLOSE, and UIP_ABORT in the response.
|
||||
* These is needed by uIP to handle responses and buffer state. The
|
||||
* UIP_NEWDATA indication will trigger the ACK response, but must be
|
||||
* explicitly set in the callback.
|
||||
*/
|
||||
|
||||
nllvdbg("flags: %04x\n", flags);
|
||||
|
||||
/* Perform the data callback. When a data callback is executed from 'list',
|
||||
* the input flags are normally returned, however, the implementation
|
||||
* may set one of the following:
|
||||
*
|
||||
* UIP_CLOSE - Gracefully close the current connection
|
||||
* UIP_ABORT - Abort (reset) the current connection on an error that
|
||||
* prevents UIP_CLOSE from working.
|
||||
*
|
||||
* And/Or set/clear the following:
|
||||
*
|
||||
* UIP_NEWDATA - May be cleared to indicate that the data was consumed
|
||||
* and that no further process of the new data should be
|
||||
* attempted.
|
||||
* UIP_SNDACK - If UIP_NEWDATA is cleared, then UIP_SNDACK may be set
|
||||
* to indicate that an ACK should be included in the response.
|
||||
* (In UIP_NEWDATA is cleared but UIP_SNDACK is not set, then
|
||||
* dev->d_len should also be cleared).
|
||||
*/
|
||||
|
||||
flags = uip_callbackexecute(dev, conn, flags, conn->list);
|
||||
|
||||
/* There may be no new data handler in place at them moment that the new
|
||||
* incoming data is received. If the new incoming data was not handled, then
|
||||
* either (1) put the unhandled incoming data in the read-ahead buffer (if
|
||||
* enabled) or (2) suppress the ACK to the data in the hope that it will
|
||||
* be re-transmitted at a better time.
|
||||
*/
|
||||
|
||||
if ((flags & UIP_NEWDATA) != 0)
|
||||
{
|
||||
/* Data was not handled.. dispose of it appropriately */
|
||||
|
||||
flags = uip_dataevent(dev, conn, flags);
|
||||
}
|
||||
|
||||
/* Check if there is a connection-related event and a connection
|
||||
* callback.
|
||||
*/
|
||||
|
||||
if (((flags & UIP_CONN_EVENTS) != 0) && conn->connection_event)
|
||||
{
|
||||
/* Perform the callback */
|
||||
|
||||
conn->connection_event(conn, flags);
|
||||
}
|
||||
|
||||
return flags;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Function: uip_datahandler
|
||||
*
|
||||
* Description:
|
||||
* Handle data that is not accepted by the application. This may be called
|
||||
* either (1) from the data receive logic if it cannot buffer the data, or
|
||||
* (2) from the TCP event logic is there is no listener in place ready to
|
||||
* receive the data.
|
||||
*
|
||||
* Input Parameters:
|
||||
* conn - A pointer to the TCP connection structure
|
||||
* buffer - A pointer to the buffer to be copied to the read-ahead
|
||||
* buffers
|
||||
* buflen - The number of bytes to copy to the read-ahead buffer.
|
||||
*
|
||||
* Returned value:
|
||||
* The number of bytes actually buffered. This could be less than 'nbytes'
|
||||
* if there is insufficient buffering available.
|
||||
*
|
||||
* Assumptions:
|
||||
* - The caller has checked that UIP_NEWDATA is set in flags and that is no
|
||||
* other handler available to process the incoming data.
|
||||
* - This function is called at the interrupt level with interrupts disabled.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_NET_TCP_READAHEAD
|
||||
uint16_t uip_datahandler(FAR struct uip_conn *conn, FAR uint8_t *buffer,
|
||||
uint16_t buflen)
|
||||
{
|
||||
FAR struct uip_readahead_s *readahead1;
|
||||
FAR struct uip_readahead_s *readahead2 = NULL;
|
||||
uint16_t remaining;
|
||||
uint16_t recvlen = 0;
|
||||
|
||||
/* First, we need to determine if we have space to buffer the data. This
|
||||
* needs to be verified before we actually begin buffering the data. We
|
||||
* will use any remaining space in the last allocated read-ahead buffer
|
||||
* plus as much one additional buffer. It is expected that the size of
|
||||
* read-ahead buffers are tuned so that one full packet will always fit
|
||||
* into one read-ahead buffer (for example if the buffer size is 420, then
|
||||
* a read-ahead buffer of 366 will hold a full packet of TCP data).
|
||||
*/
|
||||
|
||||
readahead1 = (FAR struct uip_readahead_s*)conn->readahead.tail;
|
||||
if ((readahead1 &&
|
||||
(CONFIG_NET_TCP_READAHEAD_BUFSIZE - readahead1->rh_nbytes) > buflen) ||
|
||||
(readahead2 = uip_tcpreadahead_alloc()) != NULL)
|
||||
{
|
||||
/* We have buffer space. Now try to append add as much data as possible
|
||||
* to the last read-ahead buffer attached to this connection.
|
||||
*/
|
||||
|
||||
remaining = buflen;
|
||||
if (readahead1)
|
||||
{
|
||||
recvlen = uip_readahead(readahead1, buffer, remaining);
|
||||
if (recvlen > 0)
|
||||
{
|
||||
buffer += recvlen;
|
||||
remaining -= recvlen;
|
||||
}
|
||||
}
|
||||
|
||||
/* Do we need to buffer into the newly allocated buffer as well? */
|
||||
|
||||
if (readahead2)
|
||||
{
|
||||
readahead2->rh_nbytes = 0;
|
||||
recvlen += uip_readahead(readahead2, buffer, remaining);
|
||||
|
||||
/* Save the read-ahead buffer in the connection structure where
|
||||
* it can be found with recv() is called.
|
||||
*/
|
||||
|
||||
sq_addlast(&readahead2->rh_node, &conn->readahead);
|
||||
}
|
||||
}
|
||||
|
||||
nllvdbg("Buffered %d bytes (of %d)\n", recvlen, buflen);
|
||||
return recvlen;
|
||||
}
|
||||
#endif /* CONFIG_NET_TCP_READAHEAD */
|
||||
|
||||
#endif /* CONFIG_NET && CONFIG_NET_TCP */
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,127 @@
|
||||
/****************************************************************************
|
||||
* net/tcp/tcp_poll.c
|
||||
* Poll for the availability of TCP TX data
|
||||
*
|
||||
* Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Adapted for NuttX from logic in uIP which also has a BSD-like license:
|
||||
*
|
||||
* Original author Adam Dunkels <adam@dunkels.com>
|
||||
* Copyright () 2001-2003, Adam Dunkels.
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP)
|
||||
|
||||
#include <stdint.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <nuttx/net/uip/uipopt.h>
|
||||
#include <nuttx/net/uip/uip.h>
|
||||
#include <nuttx/net/uip/uip-arch.h>
|
||||
|
||||
#include "uip/uip_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: uip_tcppoll
|
||||
*
|
||||
* Description:
|
||||
* Poll a TCP connection structure for availability of TX data
|
||||
*
|
||||
* Parameters:
|
||||
* dev - The device driver structure to use in the send operation
|
||||
* conn - The TCP "connection" to poll for TX data
|
||||
*
|
||||
* Return:
|
||||
* None
|
||||
*
|
||||
* Assumptions:
|
||||
* Called from the interrupt level or with interrupts disabled.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void uip_tcppoll(struct uip_driver_s *dev, struct uip_conn *conn)
|
||||
{
|
||||
uint8_t result;
|
||||
|
||||
/* Verify that the connection is established */
|
||||
|
||||
if ((conn->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED)
|
||||
{
|
||||
/* Set up for the callback */
|
||||
|
||||
dev->d_snddata = &dev->d_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN];
|
||||
dev->d_appdata = &dev->d_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN];
|
||||
|
||||
dev->d_len = 0;
|
||||
dev->d_sndlen = 0;
|
||||
|
||||
/* Perform the callback */
|
||||
|
||||
result = uip_tcpcallback(dev, conn, UIP_POLL);
|
||||
|
||||
/* Handle the callback response */
|
||||
|
||||
uip_tcpappsend(dev, conn, result);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Nothing to do for this connection */
|
||||
|
||||
dev->d_len = 0;
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* CONFIG_NET && CONFIG_NET_TCP */
|
||||
@@ -0,0 +1,143 @@
|
||||
/****************************************************************************
|
||||
* net/tcp/tcp_readahead.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2013-2014 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "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
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/net/uip/uipopt.h>
|
||||
#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP) && defined(CONFIG_NET_TCP_READAHEAD)
|
||||
|
||||
#include <queue.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <nuttx/net/uip/uip.h>
|
||||
|
||||
#include "uip/uip_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Types
|
||||
****************************************************************************/
|
||||
|
||||
/* Package all globals used by this logic into a structure */
|
||||
|
||||
struct readahead_s
|
||||
{
|
||||
/* This is the list of available write buffers */
|
||||
|
||||
sq_queue_t freebuffers;
|
||||
|
||||
/* These are the pre-allocated write buffers */
|
||||
|
||||
struct uip_readahead_s buffers[CONFIG_NET_NTCP_READAHEAD_BUFFERS];
|
||||
};
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
/* This is the state of the global read-ahead resource */
|
||||
|
||||
static struct readahead_s g_readahead;
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Function: uip_tcpreadahead_init
|
||||
*
|
||||
* Description:
|
||||
* Initialize the list of free read-ahead buffers
|
||||
*
|
||||
* Assumptions:
|
||||
* Called once early initialization.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void uip_tcpreadahead_init(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
sq_init(&g_readahead.freebuffers);
|
||||
for (i = 0; i < CONFIG_NET_NTCP_READAHEAD_BUFFERS; i++)
|
||||
{
|
||||
sq_addfirst(&g_readahead.buffers[i].rh_node, &g_readahead.freebuffers);
|
||||
}
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Function: uip_tcpreadahead_alloc
|
||||
*
|
||||
* Description:
|
||||
* Allocate a TCP read-ahead buffer by taking a pre-allocated buffer from
|
||||
* the free list. This function is called from TCP logic when new,
|
||||
* incoming TCP data is received but there is no user logic receiving the
|
||||
* the data. Note: kmalloc() cannot be used because this function is
|
||||
* called from interrupt level.
|
||||
*
|
||||
* Assumptions:
|
||||
* Called from interrupt level with interrupts disabled.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
FAR struct uip_readahead_s *uip_tcpreadahead_alloc(void)
|
||||
{
|
||||
return (FAR struct uip_readahead_s*)sq_remfirst(&g_readahead.freebuffers);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Function: uip_tcpreadahead_release
|
||||
*
|
||||
* Description:
|
||||
* Release a TCP read-ahead buffer by returning the buffer to the free list.
|
||||
* This function is called from user logic after it is consumed the buffered
|
||||
* data.
|
||||
*
|
||||
* Assumptions:
|
||||
* Called from user logic BUT with interrupts disabled.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void uip_tcpreadahead_release(FAR struct uip_readahead_s *readahead)
|
||||
{
|
||||
sq_addfirst(&readahead->rh_node, &g_readahead.freebuffers);
|
||||
}
|
||||
|
||||
#endif /* CONFIG_NET && CONFIG_NET_TCP && CONFIG_NET_TCP_READAHEAD */
|
||||
@@ -0,0 +1,369 @@
|
||||
/****************************************************************************
|
||||
* net//tcp_send.c
|
||||
*
|
||||
* Copyright (C) 2007-2010, 2012 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Adapted for NuttX from logic in uIP which also has a BSD-like license:
|
||||
*
|
||||
* Original author Adam Dunkels <adam@dunkels.com>
|
||||
* Copyright () 2001-2003, Adam Dunkels.
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP)
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <nuttx/net/uip/uipopt.h>
|
||||
#include <nuttx/net/uip/uip.h>
|
||||
#include <nuttx/net/uip/uip-arch.h>
|
||||
|
||||
#include "uip/uip_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#define BUF ((struct uip_tcpip_hdr *)&dev->d_buf[UIP_LLH_LEN])
|
||||
|
||||
/****************************************************************************
|
||||
* Public Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: uip_tcpsendcomplete
|
||||
*
|
||||
* Description:
|
||||
* Complete the final portions of the send operation. This function sets
|
||||
* up IP header and computes the TCP checksum
|
||||
*
|
||||
* Parameters:
|
||||
* dev - The device driver structure to use in the send operation
|
||||
*
|
||||
* Return:
|
||||
* None
|
||||
*
|
||||
* Assumptions:
|
||||
* Called from the interrupt level or with interrupts disabled.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static void uip_tcpsendcomplete(FAR struct uip_driver_s *dev)
|
||||
{
|
||||
FAR struct uip_tcpip_hdr *pbuf = BUF;
|
||||
|
||||
pbuf->ttl = UIP_TTL;
|
||||
|
||||
#ifdef CONFIG_NET_IPv6
|
||||
|
||||
/* For IPv6, the IP length field does not include the IPv6 IP header
|
||||
* length.
|
||||
*/
|
||||
|
||||
pbuf->len[0] = ((dev->d_len - UIP_IPH_LEN) >> 8);
|
||||
pbuf->len[1] = ((dev->d_len - UIP_IPH_LEN) & 0xff);
|
||||
|
||||
#else /* CONFIG_NET_IPv6 */
|
||||
|
||||
pbuf->len[0] = (dev->d_len >> 8);
|
||||
pbuf->len[1] = (dev->d_len & 0xff);
|
||||
|
||||
#endif /* CONFIG_NET_IPv6 */
|
||||
|
||||
pbuf->urgp[0] = pbuf->urgp[1] = 0;
|
||||
|
||||
/* Calculate TCP checksum. */
|
||||
|
||||
pbuf->tcpchksum = 0;
|
||||
pbuf->tcpchksum = ~(uip_tcpchksum(dev));
|
||||
|
||||
#ifdef CONFIG_NET_IPv6
|
||||
|
||||
pbuf->vtc = 0x60;
|
||||
pbuf->tcf = 0x00;
|
||||
pbuf->flow = 0x00;
|
||||
|
||||
#else /* CONFIG_NET_IPv6 */
|
||||
|
||||
pbuf->vhl = 0x45;
|
||||
pbuf->tos = 0;
|
||||
pbuf->ipoffset[0] = 0;
|
||||
pbuf->ipoffset[1] = 0;
|
||||
++g_ipid;
|
||||
pbuf->ipid[0] = g_ipid >> 8;
|
||||
pbuf->ipid[1] = g_ipid & 0xff;
|
||||
|
||||
/* Calculate IP checksum. */
|
||||
|
||||
pbuf->ipchksum = 0;
|
||||
pbuf->ipchksum = ~(uip_ipchksum(dev));
|
||||
|
||||
#endif /* CONFIG_NET_IPv6 */
|
||||
|
||||
nllvdbg("Outgoing TCP packet length: %d (%d)\n",
|
||||
dev->d_len, (pbuf->len[0] << 8) | pbuf->len[1]);
|
||||
|
||||
#ifdef CONFIG_NET_STATISTICS
|
||||
uip_stat.tcp.sent++;
|
||||
uip_stat.ip.sent++;
|
||||
#endif
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: uip_tcpsendcommon
|
||||
*
|
||||
* Description:
|
||||
* We're done with the input processing. We are now ready to send a reply
|
||||
* Our job is to fill in all the fields of the TCP and IP headers before
|
||||
* calculating the checksum and finally send the packet.
|
||||
*
|
||||
* Parameters:
|
||||
* dev - The device driver structure to use in the send operation
|
||||
* conn - The TCP connection structure holding connection information
|
||||
*
|
||||
* Return:
|
||||
* None
|
||||
*
|
||||
* Assumptions:
|
||||
* Called from the interrupt level or with interrupts disabled.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
static void uip_tcpsendcommon(FAR struct uip_driver_s *dev,
|
||||
FAR struct uip_conn *conn)
|
||||
{
|
||||
FAR struct uip_tcpip_hdr *pbuf = BUF;
|
||||
|
||||
memcpy(pbuf->ackno, conn->rcvseq, 4);
|
||||
memcpy(pbuf->seqno, conn->sndseq, 4);
|
||||
|
||||
pbuf->proto = UIP_PROTO_TCP;
|
||||
pbuf->srcport = conn->lport;
|
||||
pbuf->destport = conn->rport;
|
||||
|
||||
uiphdr_ipaddr_copy(pbuf->srcipaddr, &dev->d_ipaddr);
|
||||
uiphdr_ipaddr_copy(pbuf->destipaddr, &conn->ripaddr);
|
||||
|
||||
if (conn->tcpstateflags & UIP_STOPPED)
|
||||
{
|
||||
/* If the connection has issued uip_stop(), we advertise a zero
|
||||
* window so that the remote host will stop sending data.
|
||||
*/
|
||||
|
||||
pbuf->wnd[0] = 0;
|
||||
pbuf->wnd[1] = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
pbuf->wnd[0] = ((CONFIG_NET_RECEIVE_WINDOW) >> 8);
|
||||
pbuf->wnd[1] = ((CONFIG_NET_RECEIVE_WINDOW) & 0xff);
|
||||
}
|
||||
|
||||
/* Finish the IP portion of the message, calculate checksums and send
|
||||
* the message.
|
||||
*/
|
||||
|
||||
uip_tcpsendcomplete(dev);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: uip_tcpsend
|
||||
*
|
||||
* Description:
|
||||
* Setup to send a TCP packet
|
||||
*
|
||||
* Parameters:
|
||||
* dev - The device driver structure to use in the send operation
|
||||
* conn - The TCP connection structure holding connection information
|
||||
* flags - flags to apply to the TCP header
|
||||
* len - length of the message
|
||||
*
|
||||
* Return:
|
||||
* None
|
||||
*
|
||||
* Assumptions:
|
||||
* Called from the interrupt level or with interrupts disabled.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void uip_tcpsend(FAR struct uip_driver_s *dev, FAR struct uip_conn *conn,
|
||||
uint16_t flags, uint16_t len)
|
||||
{
|
||||
FAR struct uip_tcpip_hdr *pbuf = BUF;
|
||||
|
||||
pbuf->flags = flags;
|
||||
dev->d_len = len;
|
||||
pbuf->tcpoffset = (UIP_TCPH_LEN / 4) << 4;
|
||||
uip_tcpsendcommon(dev, conn);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: uip_tcpreset
|
||||
*
|
||||
* Description:
|
||||
* Send a TCP reset (no-data) message
|
||||
*
|
||||
* Parameters:
|
||||
* dev - The device driver structure to use in the send operation
|
||||
*
|
||||
* Return:
|
||||
* None
|
||||
*
|
||||
* Assumptions:
|
||||
* Called from the interrupt level or with interrupts disabled.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void uip_tcpreset(FAR struct uip_driver_s *dev)
|
||||
{
|
||||
FAR struct uip_tcpip_hdr *pbuf = BUF;
|
||||
uint16_t tmp16;
|
||||
uint8_t seqbyte;
|
||||
|
||||
#ifdef CONFIG_NET_STATISTICS
|
||||
uip_stat.tcp.rst++;
|
||||
#endif
|
||||
|
||||
pbuf->flags = TCP_RST | TCP_ACK;
|
||||
dev->d_len = UIP_IPTCPH_LEN;
|
||||
pbuf->tcpoffset = 5 << 4;
|
||||
|
||||
/* Flip the seqno and ackno fields in the TCP header. */
|
||||
|
||||
seqbyte = pbuf->seqno[3];
|
||||
pbuf->seqno[3] = pbuf->ackno[3];
|
||||
pbuf->ackno[3] = seqbyte;
|
||||
|
||||
seqbyte = pbuf->seqno[2];
|
||||
pbuf->seqno[2] = pbuf->ackno[2];
|
||||
pbuf->ackno[2] = seqbyte;
|
||||
|
||||
seqbyte = pbuf->seqno[1];
|
||||
pbuf->seqno[1] = pbuf->ackno[1];
|
||||
pbuf->ackno[1] = seqbyte;
|
||||
|
||||
seqbyte = pbuf->seqno[0];
|
||||
pbuf->seqno[0] = pbuf->ackno[0];
|
||||
pbuf->ackno[0] = seqbyte;
|
||||
|
||||
/* We also have to increase the sequence number we are
|
||||
* acknowledging. If the least significant byte overflowed, we need
|
||||
* to propagate the carry to the other bytes as well.
|
||||
*/
|
||||
|
||||
if (++(pbuf->ackno[3]) == 0)
|
||||
{
|
||||
if (++(pbuf->ackno[2]) == 0)
|
||||
{
|
||||
if (++(pbuf->ackno[1]) == 0)
|
||||
{
|
||||
++(pbuf->ackno[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Swap port numbers. */
|
||||
|
||||
tmp16 = pbuf->srcport;
|
||||
pbuf->srcport = pbuf->destport;
|
||||
pbuf->destport = tmp16;
|
||||
|
||||
/* Swap IP addresses. */
|
||||
|
||||
uiphdr_ipaddr_copy(pbuf->destipaddr, pbuf->srcipaddr);
|
||||
uiphdr_ipaddr_copy(pbuf->srcipaddr, &dev->d_ipaddr);
|
||||
|
||||
/* And send out the RST packet */
|
||||
|
||||
uip_tcpsendcomplete(dev);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: uip_tcpack
|
||||
*
|
||||
* Description:
|
||||
* Send the SYN or SYNACK response.
|
||||
*
|
||||
* Parameters:
|
||||
* dev - The device driver structure to use in the send operation
|
||||
* conn - The TCP connection structure holding connection information
|
||||
* ack - The ACK response to send
|
||||
*
|
||||
* Return:
|
||||
* None
|
||||
*
|
||||
* Assumptions:
|
||||
* Called from the interrupt level or with interrupts disabled.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void uip_tcpack(FAR struct uip_driver_s *dev, FAR struct uip_conn *conn,
|
||||
uint8_t ack)
|
||||
{
|
||||
struct uip_tcpip_hdr *pbuf = BUF;
|
||||
|
||||
/* Save the ACK bits */
|
||||
|
||||
pbuf->flags = ack;
|
||||
|
||||
/* We send out the TCP Maximum Segment Size option with our ack. */
|
||||
|
||||
pbuf->optdata[0] = TCP_OPT_MSS;
|
||||
pbuf->optdata[1] = TCP_OPT_MSS_LEN;
|
||||
pbuf->optdata[2] = (UIP_TCP_MSS) / 256;
|
||||
pbuf->optdata[3] = (UIP_TCP_MSS) & 255;
|
||||
dev->d_len = UIP_IPTCPH_LEN + TCP_OPT_MSS_LEN;
|
||||
pbuf->tcpoffset = ((UIP_TCPH_LEN + TCP_OPT_MSS_LEN) / 4) << 4;
|
||||
|
||||
/* Complete the common portions of the TCP message */
|
||||
|
||||
uip_tcpsendcommon(dev, conn);
|
||||
}
|
||||
|
||||
#endif /* CONFIG_NET && CONFIG_NET_TCP */
|
||||
@@ -0,0 +1,173 @@
|
||||
/****************************************************************************
|
||||
* net/tcp/tcp_seqno.c
|
||||
*
|
||||
* Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Large parts of this file were leveraged from uIP logic:
|
||||
*
|
||||
* Copyright (c) 2001-2003, Adam Dunkels.
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Compilation Switches
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP)
|
||||
|
||||
#include <stdint.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <nuttx/net/uip/uipopt.h>
|
||||
#include <nuttx/net/uip/uip.h>
|
||||
#include <nuttx/net/uip/uip-arch.h>
|
||||
|
||||
#include "uip/uip_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Public Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
/* g_tcpsequence is used to generate initial TCP sequence numbers */
|
||||
|
||||
static uint32_t g_tcpsequence;
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: uip_tcpsetsequence
|
||||
*
|
||||
* Description:
|
||||
* Set the TCP/IP sequence number
|
||||
*
|
||||
* Assumptions:
|
||||
* This function may called from the interrupt level
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void uip_tcpsetsequence(FAR uint8_t *seqno, uint32_t value)
|
||||
{
|
||||
/* Copy the sequence number in network (big-endian) order */
|
||||
|
||||
*seqno++ = value >> 24;
|
||||
*seqno++ = (value >> 16) & 0xff;
|
||||
*seqno++ = (value >> 8) & 0xff;
|
||||
*seqno = value & 0xff;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: uip_tcpgetsequence
|
||||
*
|
||||
* Description:
|
||||
* Get the TCP/IP sequence number
|
||||
*
|
||||
* Assumptions:
|
||||
* This function may called from the interrupt level
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
uint32_t uip_tcpgetsequence(FAR uint8_t *seqno)
|
||||
{
|
||||
uint32_t value;
|
||||
|
||||
/* Combine the sequence number from network (big-endian) order */
|
||||
|
||||
value = (uint32_t)seqno[0] << 24 |
|
||||
(uint32_t)seqno[1] << 16 |
|
||||
(uint32_t)seqno[2] << 8 |
|
||||
(uint32_t)seqno[3];
|
||||
return value;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: uip_tcpaddsequence
|
||||
*
|
||||
* Description:
|
||||
* Add the length to get the next TCP sequence number.
|
||||
*
|
||||
* Assumptions:
|
||||
* This function may called from the interrupt level
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
uint32_t uip_tcpaddsequence(FAR uint8_t *seqno, uint16_t len)
|
||||
{
|
||||
return uip_tcpgetsequence(seqno) + (uint32_t)len;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: uip_tcpinitsequence
|
||||
*
|
||||
* Description:
|
||||
* Set the (initial) the TCP/IP sequence number when a TCP connection is
|
||||
* established.
|
||||
*
|
||||
* Assumptions:
|
||||
* This function may called from the interrupt level
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void uip_tcpinitsequence(FAR uint8_t *seqno)
|
||||
{
|
||||
uip_tcpsetsequence(seqno, g_tcpsequence);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: uip_tcpnextsequence
|
||||
*
|
||||
* Description:
|
||||
* Increment the TCP/IP sequence number
|
||||
*
|
||||
* Assumptions:
|
||||
* This function is called from the interrupt level
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void uip_tcpnextsequence(void)
|
||||
{
|
||||
g_tcpsequence++;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_NET && CONFIG_NET_TCP */
|
||||
@@ -0,0 +1,263 @@
|
||||
/****************************************************************************
|
||||
* net/tcp/tcp_timer.c
|
||||
* Poll for the availability of TCP TX data
|
||||
*
|
||||
* Copyright (C) 2007-2010 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Adapted for NuttX from logic in uIP which also has a BSD-like license:
|
||||
*
|
||||
* Original author Adam Dunkels <adam@dunkels.com>
|
||||
* Copyright () 2001-2003, Adam Dunkels.
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP)
|
||||
|
||||
#include <stdint.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <nuttx/net/uip/uipopt.h>
|
||||
#include <nuttx/net/uip/uip.h>
|
||||
#include <nuttx/net/uip/uip-arch.h>
|
||||
|
||||
#include "uip/uip_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: uip_tcptimer
|
||||
*
|
||||
* Description:
|
||||
* Handle a TCP timer expiration for the provided TCP connection
|
||||
*
|
||||
* Parameters:
|
||||
* dev - The device driver structure to use in the send operation
|
||||
* conn - The TCP "connection" to poll for TX data
|
||||
* hsed - The polling interval in halves of a second
|
||||
*
|
||||
* Return:
|
||||
* None
|
||||
*
|
||||
* Assumptions:
|
||||
* Called from the interrupt level or with interrupts disabled.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void uip_tcptimer(FAR struct uip_driver_s *dev, FAR struct uip_conn *conn,
|
||||
int hsec)
|
||||
{
|
||||
uint8_t result;
|
||||
|
||||
dev->d_snddata = &dev->d_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN];
|
||||
dev->d_appdata = &dev->d_buf[UIP_IPTCPH_LEN + UIP_LLH_LEN];
|
||||
|
||||
/* Increase the TCP sequence number */
|
||||
|
||||
uip_tcpnextsequence();
|
||||
|
||||
/* Reset the length variables. */
|
||||
|
||||
dev->d_len = 0;
|
||||
dev->d_sndlen = 0;
|
||||
|
||||
/* Check if the connection is in a state in which we simply wait
|
||||
* for the connection to time out. If so, we increase the
|
||||
* connection's timer and remove the connection if it times
|
||||
* out.
|
||||
*/
|
||||
|
||||
if (conn->tcpstateflags == UIP_TIME_WAIT ||
|
||||
conn->tcpstateflags == UIP_FIN_WAIT_2)
|
||||
{
|
||||
/* Increment the connection timer */
|
||||
|
||||
conn->timer += hsec;
|
||||
if (conn->timer >= UIP_TIME_WAIT_TIMEOUT)
|
||||
{
|
||||
conn->tcpstateflags = UIP_CLOSED;
|
||||
|
||||
/* Notify upper layers about the timeout */
|
||||
|
||||
result = uip_tcpcallback(dev, conn, UIP_TIMEDOUT);
|
||||
|
||||
nllvdbg("TCP state: UIP_CLOSED\n");
|
||||
}
|
||||
}
|
||||
else if (conn->tcpstateflags != UIP_CLOSED)
|
||||
{
|
||||
/* If the connection has outstanding data, we increase the connection's
|
||||
* timer and see if it has reached the RTO value in which case we
|
||||
* retransmit.
|
||||
*/
|
||||
|
||||
if (conn->unacked > 0)
|
||||
{
|
||||
/* The connection has outstanding data */
|
||||
|
||||
if (conn->timer > hsec)
|
||||
{
|
||||
/* Will not yet decrement to zero */
|
||||
|
||||
conn->timer -= hsec;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Will decrement to zero */
|
||||
|
||||
conn->timer = 0;
|
||||
|
||||
/* Should we close the connection? */
|
||||
|
||||
if (
|
||||
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
|
||||
conn->expired > 0 ||
|
||||
#else
|
||||
conn->nrtx == UIP_MAXRTX ||
|
||||
#endif
|
||||
((conn->tcpstateflags == UIP_SYN_SENT ||
|
||||
conn->tcpstateflags == UIP_SYN_RCVD) &&
|
||||
conn->nrtx == UIP_MAXSYNRTX)
|
||||
)
|
||||
{
|
||||
conn->tcpstateflags = UIP_CLOSED;
|
||||
nllvdbg("TCP state: UIP_CLOSED\n");
|
||||
|
||||
/* We call uip_tcpcallback() with UIP_TIMEDOUT to
|
||||
* inform the application that the connection has
|
||||
* timed out.
|
||||
*/
|
||||
|
||||
result = uip_tcpcallback(dev, conn, UIP_TIMEDOUT);
|
||||
|
||||
/* We also send a reset packet to the remote host. */
|
||||
|
||||
uip_tcpsend(dev, conn, TCP_RST | TCP_ACK, UIP_IPTCPH_LEN);
|
||||
goto done;
|
||||
}
|
||||
|
||||
/* Exponential backoff. */
|
||||
|
||||
conn->timer = UIP_RTO << (conn->nrtx > 4 ? 4: conn->nrtx);
|
||||
(conn->nrtx)++;
|
||||
|
||||
/* Ok, so we need to retransmit. We do this differently
|
||||
* depending on which state we are in. In ESTABLISHED, we
|
||||
* call upon the application so that it may prepare the
|
||||
* data for the retransmit. In SYN_RCVD, we resend the
|
||||
* SYNACK that we sent earlier and in LAST_ACK we have to
|
||||
* retransmit our FINACK.
|
||||
*/
|
||||
|
||||
#ifdef CONFIG_NET_STATISTICS
|
||||
uip_stat.tcp.rexmit++;
|
||||
#endif
|
||||
switch(conn->tcpstateflags & UIP_TS_MASK)
|
||||
{
|
||||
case UIP_SYN_RCVD:
|
||||
/* In the SYN_RCVD state, we should retransmit our
|
||||
* SYNACK.
|
||||
*/
|
||||
|
||||
uip_tcpack(dev, conn, TCP_ACK | TCP_SYN);
|
||||
goto done;
|
||||
|
||||
case UIP_SYN_SENT:
|
||||
/* In the SYN_SENT state, we retransmit out SYN. */
|
||||
|
||||
uip_tcpack(dev, conn, TCP_SYN);
|
||||
goto done;
|
||||
|
||||
case UIP_ESTABLISHED:
|
||||
/* In the ESTABLISHED state, we call upon the application
|
||||
* to do the actual retransmit after which we jump into
|
||||
* the code for sending out the packet.
|
||||
*/
|
||||
|
||||
result = uip_tcpcallback(dev, conn, UIP_REXMIT);
|
||||
uip_tcprexmit(dev, conn, result);
|
||||
goto done;
|
||||
|
||||
case UIP_FIN_WAIT_1:
|
||||
case UIP_CLOSING:
|
||||
case UIP_LAST_ACK:
|
||||
/* In all these states we should retransmit a FINACK. */
|
||||
|
||||
uip_tcpsend(dev, conn, TCP_FIN | TCP_ACK, UIP_IPTCPH_LEN);
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* The connection does not have outstanding data */
|
||||
|
||||
else if ((conn->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED)
|
||||
{
|
||||
/* If there was no need for a retransmission, we poll the
|
||||
* application for new data.
|
||||
*/
|
||||
|
||||
result = uip_tcpcallback(dev, conn, UIP_POLL);
|
||||
uip_tcpappsend(dev, conn, result);
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
|
||||
/* Nothing to be done */
|
||||
|
||||
dev->d_len = 0;
|
||||
|
||||
done:
|
||||
return;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_NET && CONFIG_NET_TCP */
|
||||
@@ -0,0 +1,166 @@
|
||||
/****************************************************************************
|
||||
* net/tcp/tcp_wrbuffer.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2013-2014 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
* Jason Jiang <jasonj@live.cn>
|
||||
*
|
||||
* 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. Neither the name NuttX nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "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
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/net/uip/uipopt.h>
|
||||
#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP) && defined(CONFIG_NET_TCP_WRITE_BUFFERS)
|
||||
|
||||
#include <queue.h>
|
||||
#include <semaphore.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include "uip/uip_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Types
|
||||
****************************************************************************/
|
||||
|
||||
/* Package all globals used by this logic into a structure */
|
||||
|
||||
struct wrbuffer_s
|
||||
{
|
||||
/* The semaphore to protect the buffers */
|
||||
|
||||
sem_t sem;
|
||||
|
||||
/* This is the list of available write buffers */
|
||||
|
||||
sq_queue_t freebuffers;
|
||||
|
||||
/* These are the pre-allocated write buffers */
|
||||
|
||||
struct uip_wrbuffer_s buffers[CONFIG_NET_NTCP_WRITE_BUFFERS];
|
||||
};
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
/* This is the state of the global write buffer resource */
|
||||
|
||||
static struct wrbuffer_s g_wrbuffer;
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Function: uip_tcpwrbuffer_init
|
||||
*
|
||||
* Description:
|
||||
* Initialize the list of free write buffers
|
||||
*
|
||||
* Assumptions:
|
||||
* Called once early initialization.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void uip_tcpwrbuffer_init(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
sq_init(&g_wrbuffer.freebuffers);
|
||||
|
||||
for (i = 0; i < CONFIG_NET_NTCP_WRITE_BUFFERS; i++)
|
||||
{
|
||||
sq_addfirst(&g_wrbuffer.buffers[i].wb_node, &g_wrbuffer.freebuffers);
|
||||
}
|
||||
|
||||
sem_init(&g_wrbuffer.sem, 0, CONFIG_NET_NTCP_WRITE_BUFFERS);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Function: uip_tcpwrbuffer_alloc
|
||||
*
|
||||
* Description:
|
||||
* Allocate a TCP write buffer by taking a pre-allocated buffer from
|
||||
* the free list. This function is called from TCP logic when a buffer
|
||||
* of TCP data is about to sent
|
||||
*
|
||||
* Assumptions:
|
||||
* Called from user logic with interrupts enabled.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
FAR struct uip_wrbuffer_s *
|
||||
uip_tcpwrbuffer_alloc(FAR const struct timespec *abstime)
|
||||
{
|
||||
int ret;
|
||||
|
||||
if (abstime)
|
||||
{
|
||||
ret = sem_timedwait(&g_wrbuffer.sem, abstime);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = sem_wait(&g_wrbuffer.sem);
|
||||
}
|
||||
|
||||
if (ret != 0)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return (FAR struct uip_wrbuffer_s*)sq_remfirst(&g_wrbuffer.freebuffers);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Function: uip_tcpwrbuffer_release
|
||||
*
|
||||
* Description:
|
||||
* Release a TCP write buffer by returning the buffer to the free list.
|
||||
* This function is called from user logic after it is consumed the
|
||||
* buffered data.
|
||||
*
|
||||
* Assumptions:
|
||||
* Called from interrupt level with interrupts disabled.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
void uip_tcpwrbuffer_release(FAR struct uip_wrbuffer_s *wrbuffer)
|
||||
{
|
||||
sq_addlast(&wrbuffer->wb_node, &g_wrbuffer.freebuffers);
|
||||
sem_post(&g_wrbuffer.sem);
|
||||
}
|
||||
|
||||
#endif /* CONFIG_NET && CONFIG_NET_TCP && CONFIG_NET_TCP_WRITE_BUFFERS */
|
||||
Reference in New Issue
Block a user