2007-11-07 19:54:35 +01:00
|
|
|
/****************************************************************************
|
2014-06-18 18:18:53 +02:00
|
|
|
* net/tcp/tcp_callback.c
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
2014-06-24 23:38:00 +02:00
|
|
|
* Copyright (C) 2007-2009, 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
|
|
|
*
|
|
|
|
* 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>
|
2012-02-23 03:07:38 +01:00
|
|
|
|
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>
|
2007-11-20 00:09:39 +01:00
|
|
|
#include <string.h>
|
2007-11-07 19:54:35 +01:00
|
|
|
#include <debug.h>
|
|
|
|
|
2017-05-09 15:34:59 +02:00
|
|
|
#include <nuttx/mm/iob.h>
|
2014-06-24 16:53:28 +02:00
|
|
|
#include <nuttx/net/netconfig.h>
|
2014-06-24 17:28:44 +02:00
|
|
|
#include <nuttx/net/netdev.h>
|
2014-06-26 17:32:39 +02:00
|
|
|
#include <nuttx/net/netstats.h>
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2014-06-29 02:07:02 +02:00
|
|
|
#include "devif/devif.h"
|
2014-06-25 02:12:49 +02:00
|
|
|
#include "tcp/tcp.h"
|
2007-11-07 19:54:35 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2007-11-20 00:09:39 +01:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tcp_data_event
|
2007-11-20 00:09:39 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2012-02-23 03:07:38 +01:00
|
|
|
* Handle data that is not accepted by the application because there is no
|
|
|
|
* listener in place ready to receive the data.
|
2007-11-20 00:09:39 +01:00
|
|
|
*
|
|
|
|
* Assumptions:
|
2014-07-07 01:22:02 +02:00
|
|
|
* - The caller has checked that TCP_NEWDATA is set in flags and that is no
|
2008-09-01 15:59:54 +02:00
|
|
|
* other handler available to process the incoming data.
|
|
|
|
* - This function is called at the interrupt level with interrupts disabled.
|
2007-11-20 00:09:39 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2009-12-15 15:53:45 +01:00
|
|
|
static inline uint16_t
|
2014-07-01 03:17:22 +02:00
|
|
|
tcp_data_event(FAR struct net_driver_s *dev, FAR struct tcp_conn_s *conn,
|
|
|
|
uint16_t flags)
|
2007-11-20 00:09:39 +01:00
|
|
|
{
|
2009-12-15 15:53:45 +01:00
|
|
|
uint16_t ret;
|
2008-09-01 15:59:54 +02:00
|
|
|
|
|
|
|
/* 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
|
|
|
|
*/
|
|
|
|
|
2014-07-07 01:22:02 +02:00
|
|
|
ret = (flags & ~TCP_NEWDATA) | TCP_SNDACK;
|
2007-11-20 00:09:39 +01:00
|
|
|
|
|
|
|
/* Is there new data? With non-zero length? (Certain connection events
|
2014-07-07 01:22:02 +02:00
|
|
|
* can have zero-length with TCP_NEWDATA set just to cause an ACK).
|
2007-11-20 00:09:39 +01:00
|
|
|
*/
|
|
|
|
|
2008-09-01 15:59:54 +02:00
|
|
|
if (dev->d_len > 0)
|
2007-11-20 00:09:39 +01:00
|
|
|
{
|
2014-01-13 20:55:51 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_READAHEAD
|
2012-02-23 03:07:38 +01:00
|
|
|
uint8_t *buffer = dev->d_appdata;
|
|
|
|
int buflen = dev->d_len;
|
|
|
|
uint16_t recvlen;
|
2008-09-01 15:59:54 +02:00
|
|
|
#endif
|
2007-12-07 00:10:31 +01:00
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("No listener on connection\n");
|
2008-09-01 15:59:54 +02:00
|
|
|
|
2014-01-13 20:55:51 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_READAHEAD
|
2014-06-24 23:38:00 +02:00
|
|
|
/* Save as the packet data as in the read-ahead buffer. NOTE that
|
|
|
|
* partial packets will not be buffered.
|
2012-02-23 03:07:38 +01:00
|
|
|
*/
|
2007-11-20 00:09:39 +01:00
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
recvlen = tcp_datahandler(conn, buffer, buflen);
|
2014-06-24 23:38:00 +02:00
|
|
|
if (recvlen < buflen)
|
2007-11-20 00:09:39 +01:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* There is no handler to receive new data and there are no free
|
2008-09-01 15:59:54 +02:00
|
|
|
* read-ahead buffers to retain the data -- drop the packet.
|
2007-11-20 00:09:39 +01:00
|
|
|
*/
|
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("Dropped %d bytes\n", dev->d_len);
|
2008-09-01 15:59:54 +02:00
|
|
|
|
2015-09-05 17:10:48 +02:00
|
|
|
#ifdef CONFIG_NET_STATISTICS
|
2014-06-26 17:32:39 +02:00
|
|
|
g_netstats.tcp.syndrop++;
|
|
|
|
g_netstats.tcp.drop++;
|
2007-11-20 00:09:39 +01:00
|
|
|
#endif
|
2014-07-07 01:22:02 +02:00
|
|
|
/* Clear the TCP_SNDACK bit so that no ACK will be sent */
|
2008-09-01 15:59:54 +02:00
|
|
|
|
2014-07-07 01:22:02 +02:00
|
|
|
ret &= ~TCP_SNDACK;
|
2007-11-20 00:09:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-01 15:59:54 +02:00
|
|
|
/* In any event, the new data has now been handled */
|
|
|
|
|
|
|
|
dev->d_len = 0;
|
2007-11-20 00:09:39 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-11-07 19:54:35 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tcp_callback
|
2007-11-07 19:54:35 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-07-06 20:34:27 +02:00
|
|
|
uint16_t tcp_callback(FAR struct net_driver_s *dev,
|
|
|
|
FAR struct tcp_conn_s *conn, uint16_t flags)
|
2007-11-07 19:54:35 +01:00
|
|
|
{
|
2014-07-07 01:22:02 +02:00
|
|
|
/* Preserve the TCP_ACKDATA, TCP_CLOSE, and TCP_ABORT in the response.
|
2016-05-30 17:31:44 +02:00
|
|
|
* These is needed by the network to handle responses and buffer state. The
|
2014-07-07 01:22:02 +02:00
|
|
|
* TCP_NEWDATA indication will trigger the ACK response, but must be
|
2007-11-15 23:38:32 +01:00
|
|
|
* explicitly set in the callback.
|
2007-11-08 18:12:12 +01:00
|
|
|
*/
|
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("flags: %04x\n", flags);
|
2008-09-01 15:59:54 +02:00
|
|
|
|
|
|
|
/* 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:
|
|
|
|
*
|
2014-07-07 01:22:02 +02:00
|
|
|
* TCP_CLOSE - Gracefully close the current connection
|
|
|
|
* TCP_ABORT - Abort (reset) the current connection on an error that
|
|
|
|
* prevents TCP_CLOSE from working.
|
2008-09-01 15:59:54 +02:00
|
|
|
*
|
|
|
|
* And/Or set/clear the following:
|
|
|
|
*
|
2014-07-07 01:22:02 +02:00
|
|
|
* TCP_NEWDATA - May be cleared to indicate that the data was consumed
|
2008-09-01 15:59:54 +02:00
|
|
|
* and that no further process of the new data should be
|
|
|
|
* attempted.
|
2014-07-07 01:22:02 +02:00
|
|
|
* TCP_SNDACK - If TCP_NEWDATA is cleared, then TCP_SNDACK may be set
|
2008-09-01 15:59:54 +02:00
|
|
|
* to indicate that an ACK should be included in the response.
|
2014-07-07 01:22:02 +02:00
|
|
|
* (In TCP_NEWDATA is cleared but TCP_SNDACK is not set, then
|
2008-09-01 15:59:54 +02:00
|
|
|
* dev->d_len should also be cleared).
|
|
|
|
*/
|
2008-11-20 22:50:55 +01:00
|
|
|
|
2015-05-28 20:01:38 +02:00
|
|
|
flags = devif_conn_event(dev, conn, flags, conn->list);
|
2008-09-01 15:59:54 +02:00
|
|
|
|
|
|
|
/* 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.
|
2007-11-27 20:19:30 +01:00
|
|
|
*/
|
|
|
|
|
2014-07-07 01:22:02 +02:00
|
|
|
if ((flags & TCP_NEWDATA) != 0)
|
2007-11-15 23:38:32 +01:00
|
|
|
{
|
2008-09-01 15:59:54 +02:00
|
|
|
/* Data was not handled.. dispose of it appropriately */
|
2007-11-15 23:38:32 +01:00
|
|
|
|
2014-07-01 03:17:22 +02:00
|
|
|
flags = tcp_data_event(dev, conn, flags);
|
2007-11-08 17:01:18 +01:00
|
|
|
}
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2007-11-08 17:01:18 +01:00
|
|
|
/* Check if there is a connection-related event and a connection
|
|
|
|
* callback.
|
|
|
|
*/
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2014-07-07 01:22:02 +02:00
|
|
|
if (((flags & TCP_CONN_EVENTS) != 0) && conn->connection_event)
|
2007-11-08 17:01:18 +01:00
|
|
|
{
|
|
|
|
/* Perform the callback */
|
2007-11-07 19:54:35 +01:00
|
|
|
|
2015-05-30 19:29:47 +02:00
|
|
|
flags = conn->connection_event(dev, conn, conn->connection_private,
|
|
|
|
flags);
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
2007-11-08 17:01:18 +01:00
|
|
|
|
2013-10-08 17:24:37 +02:00
|
|
|
return flags;
|
2007-11-07 19:54:35 +01:00
|
|
|
}
|
|
|
|
|
2012-02-23 03:07:38 +01:00
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: tcp_datahandler
|
2012-02-23 03:07:38 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2014-04-12 20:13:01 +02:00
|
|
|
* Input Parameters:
|
2012-02-23 03:07:38 +01:00
|
|
|
* 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:
|
2014-06-24 23:38:00 +02:00
|
|
|
* The number of bytes actually buffered is returned. This will be either
|
|
|
|
* zero or equal to buflen; partial packets are not buffered.
|
2012-02-23 03:07:38 +01:00
|
|
|
*
|
|
|
|
* Assumptions:
|
2014-07-07 01:22:02 +02:00
|
|
|
* - The caller has checked that TCP_NEWDATA is set in flags and that is no
|
2012-02-23 03:07:38 +01:00
|
|
|
* other handler available to process the incoming data.
|
|
|
|
* - This function is called at the interrupt level with interrupts disabled.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-01-13 20:55:51 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_READAHEAD
|
2014-06-25 02:12:49 +02:00
|
|
|
uint16_t tcp_datahandler(FAR struct tcp_conn_s *conn, FAR uint8_t *buffer,
|
2012-02-23 03:07:38 +01:00
|
|
|
uint16_t buflen)
|
|
|
|
{
|
2014-06-24 23:38:00 +02:00
|
|
|
FAR struct iob_s *iob;
|
|
|
|
int ret;
|
2012-02-23 03:07:38 +01:00
|
|
|
|
2015-01-28 04:23:42 +01:00
|
|
|
/* Try to allocate on I/O buffer to start the chain without waiting (and
|
|
|
|
* throttling as necessary). If we would have to wait, then drop the
|
|
|
|
* packet.
|
|
|
|
*/
|
2014-06-24 23:38:00 +02:00
|
|
|
|
2015-01-28 04:23:42 +01:00
|
|
|
iob = iob_tryalloc(true);
|
2014-06-24 23:38:00 +02:00
|
|
|
if (iob == NULL)
|
2012-02-23 03:07:38 +01:00
|
|
|
{
|
2016-06-20 20:44:38 +02:00
|
|
|
nerr("ERROR: Failed to create new I/O buffer chain\n");
|
2014-06-24 23:38:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2012-02-23 03:07:38 +01:00
|
|
|
|
2015-01-28 04:23:42 +01:00
|
|
|
/* Copy the new appdata into the I/O buffer chain (without waiting) */
|
2012-02-23 03:07:38 +01:00
|
|
|
|
2015-01-28 04:23:42 +01:00
|
|
|
ret = iob_trycopyin(iob, buffer, buflen, 0, true);
|
2014-06-24 23:38:00 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* On a failure, iob_copyin return a negated error value but does
|
|
|
|
* not free any I/O buffers.
|
|
|
|
*/
|
2012-02-23 03:07:38 +01:00
|
|
|
|
2016-06-20 20:44:38 +02:00
|
|
|
nerr("ERROR: Failed to add data to the I/O buffer chain: %d\n", ret);
|
2014-06-24 23:38:00 +02:00
|
|
|
(void)iob_free_chain(iob);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-02-23 03:07:38 +01:00
|
|
|
|
2015-01-28 04:23:42 +01:00
|
|
|
/* Add the new I/O buffer chain to the tail of the read-ahead queue (again
|
|
|
|
* without waiting).
|
|
|
|
*/
|
2012-02-23 03:07:38 +01:00
|
|
|
|
2015-01-28 04:23:42 +01:00
|
|
|
ret = iob_tryadd_queue(iob, &conn->readahead);
|
2014-06-24 23:38:00 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-20 20:44:38 +02:00
|
|
|
nerr("ERROR: Failed to queue the I/O buffer chain: %d\n", ret);
|
2014-06-24 23:38:00 +02:00
|
|
|
(void)iob_free_chain(iob);
|
|
|
|
return 0;
|
2012-02-23 03:07:38 +01:00
|
|
|
}
|
|
|
|
|
2016-06-20 19:59:15 +02:00
|
|
|
ninfo("Buffered %d bytes\n", buflen);
|
2014-06-24 23:38:00 +02:00
|
|
|
return buflen;
|
2012-02-23 03:07:38 +01:00
|
|
|
}
|
2014-01-13 20:55:51 +01:00
|
|
|
#endif /* CONFIG_NET_TCP_READAHEAD */
|
2012-02-23 03:07:38 +01:00
|
|
|
|
2007-11-22 19:36:46 +01:00
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_TCP */
|