2007-11-07 19:54:35 +01:00
|
|
|
/****************************************************************************
|
2014-06-18 18:18:53 +02:00
|
|
|
* net/tcp/tcp_appsend.c
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
2014-01-14 15:30:35 +01:00
|
|
|
* Copyright (C) 2007-2010, 2014 Gregory Nutt. All rights reserved.
|
2012-09-13 20:32:24 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
|
|
|
* 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>
|
2007-11-22 19:36:46 +01:00
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_TCP)
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2009-12-15 15:53:45 +01:00
|
|
|
#include <stdint.h>
|
2011-01-23 18:40:10 +01:00
|
|
|
#include <assert.h>
|
2007-11-07 19:54:35 +01:00
|
|
|
#include <debug.h>
|
|
|
|
|
2014-06-24 16:53:28 +02:00
|
|
|
#include <nuttx/net/netconfig.h>
|
2014-06-24 18:14:15 +02:00
|
|
|
#include <nuttx/net/uip.h>
|
2014-06-24 17:28:44 +02:00
|
|
|
#include <nuttx/net/netdev.h>
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2014-06-24 18:14:15 +02:00
|
|
|
#include "uip/uip.h"
|
2014-06-25 02:12:49 +02:00
|
|
|
#include "tcp/tcp.h"
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2014-04-12 20:13:01 +02:00
|
|
|
* Pre-processor Definitions
|
2007-11-07 19:54:35 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Variables
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Variables
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-25 02:12:49 +02:00
|
|
|
* Name: tcp_appsend
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2010-11-26 06:26:27 +01:00
|
|
|
* 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.
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
void tcp_appsend(FAR struct uip_driver_s *dev, FAR struct tcp_conn_s *conn,
|
|
|
|
uint16_t result)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
|
|
|
/* Handle the result based on the application response */
|
|
|
|
|
2010-11-27 02:01:26 +01:00
|
|
|
nllvdbg("result: %04x d_sndlen: %d conn->unacked: %d\n",
|
|
|
|
result, dev->d_sndlen, conn->unacked);
|
2007-11-08 17:43:06 +01:00
|
|
|
|
2007-11-15 23:38:32 +01:00
|
|
|
/* Check for connection aborted */
|
|
|
|
|
2010-11-26 06:26:27 +01:00
|
|
|
if ((result & UIP_ABORT) != 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
|
|
|
dev->d_sndlen = 0;
|
|
|
|
conn->tcpstateflags = UIP_CLOSED;
|
2009-09-15 17:44:14 +02:00
|
|
|
nllvdbg("TCP state: UIP_CLOSED\n");
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
tcp_send(dev, conn, TCP_RST | TCP_ACK, UIP_IPTCPH_LEN);
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
2007-11-15 23:38:32 +01:00
|
|
|
/* Check for connection closed */
|
|
|
|
|
2010-11-26 06:26:27 +01:00
|
|
|
else if ((result & UIP_CLOSE) != 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
|
|
|
conn->tcpstateflags = UIP_FIN_WAIT_1;
|
2010-11-27 02:01:26 +01:00
|
|
|
conn->unacked = 1;
|
|
|
|
conn->nrtx = 0;
|
2009-09-15 17:44:14 +02:00
|
|
|
nllvdbg("TCP state: UIP_FIN_WAIT_1\n");
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2010-11-27 02:01:26 +01:00
|
|
|
dev->d_sndlen = 0;
|
2014-06-25 02:12:49 +02:00
|
|
|
tcp_send(dev, conn, TCP_FIN | TCP_ACK, UIP_IPTCPH_LEN);
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
2007-11-15 23:38:32 +01:00
|
|
|
/* None of the above */
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2007-11-15 23:38:32 +01:00
|
|
|
else
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2014-01-14 15:30:35 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
|
|
|
|
DEBUGASSERT(dev->d_sndlen >= 0 && dev->d_sndlen <= conn->mss);
|
|
|
|
#else
|
2007-11-15 23:38:32 +01:00
|
|
|
/* If d_sndlen > 0, the application has data to be sent. */
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2007-11-15 23:38:32 +01:00
|
|
|
if (dev->d_sndlen > 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2010-11-26 06:26:27 +01:00
|
|
|
/* Remember how much data we send out now so that we know
|
2010-11-27 23:50:27 +01:00
|
|
|
* when everything has been acknowledged. Just increment the amount
|
|
|
|
* of data sent. This will be needed in sequence number calculations
|
2014-01-14 15:30:35 +01:00
|
|
|
* and we know that this is not a re-transmission. Retransmissions
|
2010-11-27 23:50:27 +01:00
|
|
|
* do not go through this path.
|
2007-11-07 19:54:35 +01:00
|
|
|
*/
|
|
|
|
|
2010-11-27 02:01:26 +01:00
|
|
|
conn->unacked += dev->d_sndlen;
|
2007-11-15 23:38:32 +01:00
|
|
|
|
2010-11-26 06:26:27 +01:00
|
|
|
/* The application cannot send more than what is allowed by the
|
|
|
|
* MSS (the minumum of the MSS and the available window).
|
|
|
|
*/
|
2007-11-15 23:38:32 +01:00
|
|
|
|
2010-11-26 06:26:27 +01:00
|
|
|
DEBUGASSERT(dev->d_sndlen <= conn->mss);
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
2014-01-14 15:30:35 +01:00
|
|
|
conn->nrtx = 0;
|
|
|
|
#endif
|
2007-11-07 19:54:35 +01:00
|
|
|
/* Then handle the rest of the operation just as for the rexmit case */
|
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
tcp_rexmit(dev, conn, result);
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-06-25 02:12:49 +02:00
|
|
|
* Name: tcp_rexmit
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
void tcp_rexmit(FAR struct uip_driver_s *dev, FAR struct tcp_conn_s *conn,
|
|
|
|
uint16_t result)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2010-11-27 02:01:26 +01:00
|
|
|
nllvdbg("result: %04x d_sndlen: %d conn->unacked: %d\n",
|
|
|
|
result, dev->d_sndlen, conn->unacked);
|
2007-11-08 17:43:06 +01:00
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2014-01-14 15:30:35 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
|
|
|
|
if (dev->d_sndlen > 0)
|
|
|
|
#else
|
2010-11-27 02:01:26 +01:00
|
|
|
if (dev->d_sndlen > 0 && conn->unacked > 0)
|
2014-01-14 15:30:35 +01:00
|
|
|
#endif
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
|
|
|
/* We always set the ACK flag in response packets adding the length of
|
|
|
|
* the IP and TCP headers.
|
|
|
|
*/
|
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
tcp_send(dev, conn, TCP_ACK | TCP_PSH, dev->d_sndlen + UIP_TCPIP_HLEN);
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
2008-09-01 15:59:54 +02:00
|
|
|
/* If there is no data to send, just send out a pure ACK if one is requested`. */
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2010-11-26 06:26:27 +01:00
|
|
|
else if ((result & UIP_SNDACK) != 0)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2014-06-25 02:12:49 +02:00
|
|
|
tcp_send(dev, conn, TCP_ACK, UIP_TCPIP_HLEN);
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* There is nothing to do -- drop the packet */
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dev->d_len = 0;
|
|
|
|
}
|
|
|
|
}
|
2007-11-22 19:36:46 +01:00
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_TCP */
|