2012-02-22 17:03:10 +01:00
|
|
|
/****************************************************************************
|
2014-06-29 01:25:18 +02:00
|
|
|
* net/socket/net_monitor.c
|
2012-02-22 17:03:10 +01:00
|
|
|
*
|
2013-01-20 18:21:42 +01:00
|
|
|
* Copyright (C) 2007-2013 Gregory Nutt. All rights reserved.
|
2012-02-22 17:03:10 +01:00
|
|
|
* 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>
|
2012-04-23 22:33:25 +02:00
|
|
|
#include <assert.h>
|
2012-02-22 17:03:10 +01:00
|
|
|
#include <debug.h>
|
|
|
|
|
2014-06-26 17:32:39 +02:00
|
|
|
#include <nuttx/net/tcp.h>
|
|
|
|
|
2014-06-29 02:07:02 +02:00
|
|
|
#include "devif/devif.h"
|
2014-07-06 20:34:27 +02:00
|
|
|
#include "tcp/tcp.h"
|
|
|
|
#include "socket/socket.h"
|
2012-02-22 17:03:10 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-05-30 19:49:55 +02:00
|
|
|
static void connection_closed(FAR struct socket *psock, uint16_t flags);
|
2015-05-30 19:29:47 +02:00
|
|
|
static uint16_t connection_event(FAR struct net_driver_s *dev,
|
|
|
|
FAR void *pvconn, FAR void *pvpriv,
|
|
|
|
uint16_t flags);
|
2012-02-22 17:03:10 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
2016-02-26 14:35:55 +01:00
|
|
|
|
2015-05-30 19:49:55 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: connection_closed
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Called when a loss-of-connection event has occurred.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* psock The TCP socket structure associated.
|
|
|
|
* flags Set of connection events events
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* The caller holds the network lock.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void connection_closed(FAR struct socket *psock, uint16_t flags)
|
|
|
|
{
|
|
|
|
/* These loss-of-connection events may be reported:
|
|
|
|
*
|
|
|
|
* TCP_CLOSE: The remote host has closed the connection
|
|
|
|
* TCP_ABORT: The remote host has aborted the connection
|
|
|
|
* TCP_TIMEDOUT: Connection aborted due to too many retransmissions.
|
|
|
|
* NETDEV_DOWN: The network device went down
|
|
|
|
*
|
|
|
|
* And we need to set these two socket status bits appropriately:
|
|
|
|
*
|
|
|
|
* _SF_CONNECTED==1 && _SF_CLOSED==0 - the socket is connected
|
|
|
|
* _SF_CONNECTED==0 && _SF_CLOSED==1 - the socket was gracefully disconnected
|
|
|
|
* _SF_CONNECTED==0 && _SF_CLOSED==0 - the socket was rudely disconnected
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((flags & TCP_CLOSE) != 0)
|
|
|
|
{
|
|
|
|
/* The peer gracefully closed the connection. Marking the
|
|
|
|
* connection as disconnected will suppress some subsequent
|
|
|
|
* ENOTCONN errors from receive. A graceful disconnection is
|
|
|
|
* not handle as an error but as an "end-of-file"
|
|
|
|
*/
|
|
|
|
|
|
|
|
psock->s_flags &= ~_SF_CONNECTED;
|
|
|
|
psock->s_flags |= _SF_CLOSED;
|
|
|
|
}
|
|
|
|
else if ((flags & (TCP_ABORT | TCP_TIMEDOUT | NETDEV_DOWN)) != 0)
|
|
|
|
{
|
|
|
|
/* The loss of connection was less than graceful. This will (eventually)
|
|
|
|
* be reported as an ENOTCONN error.
|
|
|
|
*/
|
|
|
|
|
2015-10-08 23:10:04 +02:00
|
|
|
psock->s_flags &= ~(_SF_CONNECTED | _SF_CLOSED);
|
2015-05-30 19:49:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-22 17:03:10 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: connection_event
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Some connection related event has occurred
|
|
|
|
*
|
|
|
|
* Parameters:
|
2015-05-30 19:29:47 +02:00
|
|
|
* dev The device which as active when the event was detected.
|
2012-02-22 17:03:10 +01:00
|
|
|
* conn The connection structure associated with the socket
|
|
|
|
* flags Set of events describing why the callback was invoked
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2015-05-30 19:29:47 +02:00
|
|
|
* The network is locked.
|
2012-02-22 17:03:10 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-05-30 19:29:47 +02:00
|
|
|
static uint16_t connection_event(FAR struct net_driver_s *dev,
|
|
|
|
FAR void *pvconn, FAR void *pvpriv,
|
|
|
|
uint16_t flags)
|
2012-02-22 17:03:10 +01:00
|
|
|
{
|
2015-05-30 19:29:47 +02:00
|
|
|
FAR struct socket *psock = (FAR struct socket *)pvpriv;
|
2012-02-22 17:03:10 +01:00
|
|
|
|
|
|
|
if (psock)
|
|
|
|
{
|
2016-06-11 19:59:51 +02:00
|
|
|
nllinfo("flags: %04x s_flags: %02x\n", flags, psock->s_flags);
|
2012-02-22 17:03:10 +01:00
|
|
|
|
2015-05-30 17:12:27 +02:00
|
|
|
/* TCP_DISCONN_EVENTS: TCP_CLOSE, TCP_ABORT, TCP_TIMEDOUT, or
|
|
|
|
* NETDEV_DOWN. All loss-of-connection events.
|
|
|
|
*/
|
2012-02-22 17:03:10 +01:00
|
|
|
|
2015-05-30 17:12:27 +02:00
|
|
|
if ((flags & TCP_DISCONN_EVENTS) != 0)
|
2012-02-22 17:03:10 +01:00
|
|
|
{
|
2015-05-30 19:49:55 +02:00
|
|
|
connection_closed(psock, flags);
|
2012-02-22 17:03:10 +01:00
|
|
|
}
|
|
|
|
|
2014-07-07 01:22:02 +02:00
|
|
|
/* TCP_CONNECTED: The socket is successfully connected */
|
2012-02-22 17:03:10 +01:00
|
|
|
|
2014-07-07 01:22:02 +02:00
|
|
|
else if ((flags & TCP_CONNECTED) != 0)
|
2012-02-22 17:03:10 +01:00
|
|
|
{
|
2015-08-27 18:38:43 +02:00
|
|
|
#if 0 /* REVISIT: Assertion fires. Why? */
|
|
|
|
#ifdef CONFIG_NETDEV_MULTINIC
|
|
|
|
FAR struct tcp_conn_s *conn = (FAR struct tcp_conn_s *)psock->s_conn;
|
|
|
|
|
|
|
|
/* Make sure that this is the device bound to the connection */
|
|
|
|
|
|
|
|
DEBUGASSERT(conn->dev == NULL || conn->dev == dev);
|
|
|
|
conn->dev = dev;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* If there is no local address assigned to the socket (perhaps
|
|
|
|
* because it was INADDR_ANY), then assign it the address of the
|
|
|
|
* connecting device.
|
|
|
|
*
|
|
|
|
* TODO: Implement this.
|
|
|
|
*/
|
|
|
|
|
2012-02-22 17:03:10 +01:00
|
|
|
/* Indicate that the socket is now connected */
|
|
|
|
|
|
|
|
psock->s_flags |= _SF_CONNECTED;
|
|
|
|
psock->s_flags &= ~_SF_CLOSED;
|
|
|
|
}
|
|
|
|
}
|
2015-05-30 19:29:47 +02:00
|
|
|
|
|
|
|
return flags;
|
2012-02-22 17:03:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: net_startmonitor
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set up to receive TCP connection state changes for a given socket
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock - The socket of interest
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2015-05-28 16:23:51 +02:00
|
|
|
* On success, net_startmonitor returns OK; On any failure,
|
|
|
|
* net_startmonitor will return a negated errno value. The only failure
|
|
|
|
* that can occur is if the socket has already been closed and, in this
|
|
|
|
* case, -ENOTCONN is returned.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2015-05-30 19:29:47 +02:00
|
|
|
* The caller holds the network lock (if not, it will be locked momentarily
|
|
|
|
* by this function).
|
2012-02-22 17:03:10 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int net_startmonitor(FAR struct socket *psock)
|
|
|
|
{
|
2015-05-30 19:49:55 +02:00
|
|
|
FAR struct tcp_conn_s *conn;
|
2015-05-30 19:29:47 +02:00
|
|
|
FAR struct devif_callback_s *cb;
|
|
|
|
net_lock_t save;
|
2012-02-22 17:03:10 +01:00
|
|
|
|
2015-05-30 19:49:55 +02:00
|
|
|
DEBUGASSERT(psock != NULL && psock->s_conn != NULL);
|
|
|
|
conn = (FAR struct tcp_conn_s *)psock->s_conn;
|
2012-02-22 17:03:10 +01:00
|
|
|
|
2015-05-28 16:23:51 +02:00
|
|
|
/* Check if the connection has already been closed before any callbacks
|
|
|
|
* have been registered. (Maybe the connection is lost before accept has
|
|
|
|
* registered the monitoring callback.)
|
2013-09-06 16:25:03 +02:00
|
|
|
*/
|
|
|
|
|
2015-05-30 19:29:47 +02:00
|
|
|
save = net_lock();
|
2014-07-07 00:10:26 +02:00
|
|
|
if (!(conn->tcpstateflags == TCP_ESTABLISHED ||
|
|
|
|
conn->tcpstateflags == TCP_SYN_RCVD))
|
2013-09-06 16:25:03 +02:00
|
|
|
{
|
2015-05-28 16:23:51 +02:00
|
|
|
/* Invoke the TCP_CLOSE connection event now */
|
|
|
|
|
2015-05-30 19:29:47 +02:00
|
|
|
(void)connection_event(NULL, conn, psock, TCP_CLOSE);
|
2015-05-28 16:23:51 +02:00
|
|
|
|
|
|
|
/* Make sure that the monitor is stopped */
|
|
|
|
|
|
|
|
conn->connection_private = NULL;
|
2015-05-30 19:29:47 +02:00
|
|
|
conn->connection_devcb = NULL;
|
2015-05-28 16:23:51 +02:00
|
|
|
conn->connection_event = NULL;
|
|
|
|
|
|
|
|
/* And return -ENOTCONN to indicate the the monitor was not started
|
|
|
|
* because the socket was already disconnected.
|
|
|
|
*/
|
|
|
|
|
2015-05-30 19:29:47 +02:00
|
|
|
net_unlock(save);
|
2015-05-28 16:23:51 +02:00
|
|
|
return -ENOTCONN;
|
2013-09-06 16:25:03 +02:00
|
|
|
}
|
|
|
|
|
2015-05-30 19:29:47 +02:00
|
|
|
DEBUGASSERT(conn->connection_event == NULL &&
|
|
|
|
conn->connection_devcb == NULL);
|
|
|
|
|
|
|
|
/* Allocate a callback structure that we will use to get callbacks if
|
|
|
|
* the network goes down.
|
|
|
|
*/
|
|
|
|
|
|
|
|
cb = tcp_monitor_callback_alloc(conn);
|
|
|
|
if (cb != NULL)
|
|
|
|
{
|
|
|
|
cb->event = connection_event;
|
2015-10-08 23:10:04 +02:00
|
|
|
cb->priv = (FAR void *)psock;
|
2015-05-30 19:29:47 +02:00
|
|
|
cb->flags = NETDEV_DOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn->connection_devcb = cb;
|
|
|
|
|
2015-05-28 16:23:51 +02:00
|
|
|
/* Set up to receive callbacks on connection-related events */
|
|
|
|
|
2015-10-08 23:10:04 +02:00
|
|
|
conn->connection_private = (FAR void *)psock;
|
2015-05-28 16:23:51 +02:00
|
|
|
conn->connection_event = connection_event;
|
|
|
|
|
2015-05-30 19:29:47 +02:00
|
|
|
net_unlock(save);
|
2012-02-22 17:03:10 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: net_stopmonitor
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Stop monitoring TCP connection changes for a given socket
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* conn - The TCP connection of interest
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
2015-05-30 19:29:47 +02:00
|
|
|
* Assumptions:
|
|
|
|
* The caller holds the network lock (if not, it will be locked momentarily
|
|
|
|
* by this function).
|
|
|
|
*
|
2012-02-22 17:03:10 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-25 02:12:49 +02:00
|
|
|
void net_stopmonitor(FAR struct tcp_conn_s *conn)
|
2012-02-22 17:03:10 +01:00
|
|
|
{
|
2015-05-30 19:29:47 +02:00
|
|
|
net_lock_t save;
|
|
|
|
|
2012-02-22 17:03:10 +01:00
|
|
|
DEBUGASSERT(conn);
|
|
|
|
|
2015-05-30 19:29:47 +02:00
|
|
|
/* Free any allocated device event callback structure */
|
|
|
|
|
|
|
|
save = net_lock();
|
|
|
|
if (conn->connection_devcb)
|
|
|
|
{
|
|
|
|
tcp_monitor_callback_free(conn, conn->connection_devcb);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Nullify all connection event data */
|
|
|
|
|
2012-02-22 17:03:10 +01:00
|
|
|
conn->connection_private = NULL;
|
2015-05-30 19:29:47 +02:00
|
|
|
conn->connection_devcb = NULL;
|
2012-02-22 17:03:10 +01:00
|
|
|
conn->connection_event = NULL;
|
2015-05-30 19:29:47 +02:00
|
|
|
net_unlock(save);
|
2012-02-22 17:03:10 +01:00
|
|
|
}
|
|
|
|
|
2013-01-20 18:21:42 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: net_lostconnection
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Called when a loss-of-connection event has occurred.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* psock The TCP socket structure associated.
|
|
|
|
* flags Set of connection events events
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
2015-05-30 19:49:55 +02:00
|
|
|
* The caller holds the network lock (if not, it will be locked momentarily
|
|
|
|
* by this function).
|
2013-01-20 18:21:42 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void net_lostconnection(FAR struct socket *psock, uint16_t flags)
|
|
|
|
{
|
2015-05-30 19:49:55 +02:00
|
|
|
net_lock_t save;
|
2013-01-20 18:21:42 +01:00
|
|
|
|
2015-05-30 19:49:55 +02:00
|
|
|
DEBUGASSERT(psock != NULL && psock->s_conn != NULL);
|
2013-01-20 18:21:42 +01:00
|
|
|
|
2015-05-30 19:49:55 +02:00
|
|
|
/* Close the connection */
|
2013-01-20 18:21:42 +01:00
|
|
|
|
2015-05-30 19:49:55 +02:00
|
|
|
save = net_lock();
|
|
|
|
connection_closed(psock, flags);
|
2013-01-20 18:21:42 +01:00
|
|
|
|
2015-05-30 19:49:55 +02:00
|
|
|
/* Stop the network monitor */
|
|
|
|
|
|
|
|
net_stopmonitor((FAR struct tcp_conn_s *)psock->s_conn);
|
|
|
|
net_unlock(save);
|
2013-01-20 18:21:42 +01:00
|
|
|
}
|
|
|
|
|
2012-02-22 17:03:10 +01:00
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_TCP */
|