2017-10-23 16:45:12 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* net/icmp/icmp_netpoll.c
|
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2017-10-23 16:45:12 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-10-23 16:45:12 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2017-10-23 16:45:12 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <poll.h>
|
2021-06-08 20:00:55 +02:00
|
|
|
#include <assert.h>
|
2017-10-23 16:45:12 +02:00
|
|
|
#include <debug.h>
|
|
|
|
|
2020-01-02 21:37:14 +01:00
|
|
|
#include <nuttx/semaphore.h>
|
2017-10-23 16:45:12 +02:00
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
|
2019-12-31 16:26:14 +01:00
|
|
|
#include "devif/devif.h"
|
2018-08-26 23:37:03 +02:00
|
|
|
#include "netdev/netdev.h"
|
2017-10-23 16:45:12 +02:00
|
|
|
#include "icmp/icmp.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: icmp_poll_eventhandler
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function is called to perform the actual UDP receive operation
|
|
|
|
* via the device interface layer.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-10-23 16:45:12 +02:00
|
|
|
* dev The structure of the network driver that caused the event
|
2022-08-25 15:17:57 +02:00
|
|
|
* pvpriv An instance of struct icmp_poll_s cast to void*
|
2017-10-23 16:45:12 +02:00
|
|
|
* flags Set of events describing why the callback was invoked
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* This function must be called with the network locked.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static uint16_t icmp_poll_eventhandler(FAR struct net_driver_s *dev,
|
|
|
|
FAR void *pvpriv, uint16_t flags)
|
|
|
|
{
|
2022-08-25 15:17:57 +02:00
|
|
|
FAR struct icmp_poll_s *info = pvpriv;
|
2017-11-21 17:41:20 +01:00
|
|
|
FAR struct icmp_conn_s *conn;
|
|
|
|
FAR struct socket *psock;
|
2017-10-23 16:45:12 +02:00
|
|
|
pollevent_t eventset;
|
|
|
|
|
|
|
|
ninfo("flags: %04x\n", flags);
|
|
|
|
|
2017-11-22 18:37:25 +01:00
|
|
|
DEBUGASSERT(info == NULL || info->fds != NULL);
|
2017-10-23 16:45:12 +02:00
|
|
|
|
|
|
|
/* 'priv' might be null in some race conditions (?). Only process the
|
|
|
|
* the event if this poll is from the same device that the request was
|
|
|
|
* sent out on.
|
|
|
|
*/
|
|
|
|
|
2017-11-21 17:41:20 +01:00
|
|
|
if (info != NULL)
|
2017-10-23 16:45:12 +02:00
|
|
|
{
|
2018-10-29 23:25:57 +01:00
|
|
|
/* Is this a response on the same device that we sent the request out
|
|
|
|
* on?
|
|
|
|
*/
|
|
|
|
|
|
|
|
psock = info->psock;
|
|
|
|
DEBUGASSERT(psock != NULL && psock->s_conn != NULL);
|
|
|
|
conn = psock->s_conn;
|
|
|
|
if (dev != conn->dev)
|
|
|
|
{
|
|
|
|
ninfo("Wrong device\n");
|
|
|
|
return flags;
|
|
|
|
}
|
2017-11-21 17:41:20 +01:00
|
|
|
|
2017-10-23 16:45:12 +02:00
|
|
|
/* Check for data or connection availability events. */
|
|
|
|
|
|
|
|
eventset = 0;
|
2019-11-27 15:09:51 +01:00
|
|
|
if ((flags & ICMP_NEWDATA) != 0)
|
2017-10-23 16:45:12 +02:00
|
|
|
{
|
2022-09-19 05:08:57 +02:00
|
|
|
eventset |= POLLIN;
|
2017-10-23 16:45:12 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 16:59:50 +01:00
|
|
|
/* Check for loss of connection events. */
|
2017-10-23 16:45:12 +02:00
|
|
|
|
2019-11-25 16:59:50 +01:00
|
|
|
if ((flags & NETDEV_DOWN) != 0)
|
2017-10-23 16:45:12 +02:00
|
|
|
{
|
2019-11-25 16:59:50 +01:00
|
|
|
eventset |= (POLLHUP | POLLERR);
|
2017-10-23 16:45:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Awaken the caller of poll() is requested event occurred. */
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(&info->fds, 1, eventset);
|
2017-10-23 16:45:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: icmp_pollsetup
|
|
|
|
*
|
|
|
|
* Description:
|
2017-10-24 19:23:08 +02:00
|
|
|
* Setup to monitor events on one ICMP socket
|
2017-10-23 16:45:12 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
2017-10-24 19:23:08 +02:00
|
|
|
* psock - The IPPROTO_ICMP socket of interest
|
2017-10-23 16:45:12 +02:00
|
|
|
* fds - The structure describing the events to be monitored, OR NULL if
|
|
|
|
* this is a request to stop monitoring events.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0: Success; Negated errno on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int icmp_pollsetup(FAR struct socket *psock, FAR struct pollfd *fds)
|
|
|
|
{
|
2022-07-08 22:43:11 +02:00
|
|
|
FAR struct icmp_conn_s *conn;
|
2017-10-23 16:45:12 +02:00
|
|
|
FAR struct icmp_poll_s *info;
|
|
|
|
FAR struct devif_callback_s *cb;
|
2022-09-19 05:08:57 +02:00
|
|
|
pollevent_t eventset = 0;
|
2020-01-02 21:40:08 +01:00
|
|
|
int ret = OK;
|
2017-10-23 16:45:12 +02:00
|
|
|
|
2022-07-08 22:43:11 +02:00
|
|
|
/* Some of the following must be atomic */
|
2017-10-23 16:45:12 +02:00
|
|
|
|
|
|
|
net_lock();
|
|
|
|
|
2022-07-08 22:43:11 +02:00
|
|
|
conn = psock->s_conn;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
if (!conn || !fds)
|
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
|
|
|
|
2019-12-31 16:26:14 +01:00
|
|
|
/* Find a container to hold the poll information */
|
|
|
|
|
|
|
|
info = conn->pollinfo;
|
|
|
|
while (info->psock != NULL)
|
|
|
|
{
|
|
|
|
if (++info >= &conn->pollinfo[CONFIG_NET_ICMP_NPOLLWAITERS])
|
|
|
|
{
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-23 16:45:12 +02:00
|
|
|
/* Allocate a ICMP callback structure */
|
|
|
|
|
2019-03-19 16:43:32 +01:00
|
|
|
cb = icmp_callback_alloc(conn->dev, conn);
|
2017-10-23 16:45:12 +02:00
|
|
|
if (cb == NULL)
|
|
|
|
{
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto errout_with_lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the poll info container */
|
|
|
|
|
2017-11-21 17:41:20 +01:00
|
|
|
info->psock = psock;
|
|
|
|
info->fds = fds;
|
|
|
|
info->cb = cb;
|
2017-10-23 16:45:12 +02:00
|
|
|
|
|
|
|
/* Initialize the callback structure. Save the reference to the info
|
|
|
|
* structure as callback private data so that it will be available during
|
|
|
|
* callback processing.
|
|
|
|
*/
|
|
|
|
|
2019-11-25 16:57:49 +01:00
|
|
|
cb->flags = NETDEV_DOWN;
|
2017-10-23 16:45:12 +02:00
|
|
|
cb->priv = (FAR void *)info;
|
|
|
|
cb->event = icmp_poll_eventhandler;
|
|
|
|
|
2019-11-25 16:57:49 +01:00
|
|
|
if ((fds->events & POLLIN) != 0)
|
2017-10-23 16:45:12 +02:00
|
|
|
{
|
2018-08-26 23:37:03 +02:00
|
|
|
cb->flags |= ICMP_NEWDATA;
|
2017-10-23 16:45:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the reference in the poll info structure as fds private as well
|
|
|
|
* for use during poll teardown as well.
|
|
|
|
*/
|
|
|
|
|
|
|
|
fds->priv = (FAR void *)info;
|
|
|
|
|
|
|
|
/* Check for read data availability now */
|
|
|
|
|
|
|
|
if (!IOB_QEMPTY(&conn->readahead))
|
|
|
|
{
|
|
|
|
/* Normal data may be read without blocking. */
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
eventset |= POLLRDNORM;
|
2017-10-23 16:45:12 +02:00
|
|
|
}
|
|
|
|
|
2019-11-27 15:04:14 +01:00
|
|
|
/* Always report POLLWRNORM if caller request it because we don't utilize
|
|
|
|
* IOB buffer for sending.
|
|
|
|
*/
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
eventset |= POLLWRNORM;
|
2019-11-27 15:04:14 +01:00
|
|
|
|
2017-10-23 16:45:12 +02:00
|
|
|
/* Check if any requested events are already in effect */
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(&fds, 1, eventset);
|
2017-10-23 16:45:12 +02:00
|
|
|
|
|
|
|
errout_with_lock:
|
|
|
|
net_unlock();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: icmp_pollteardown
|
|
|
|
*
|
|
|
|
* Description:
|
2017-10-24 19:23:08 +02:00
|
|
|
* Teardown monitoring of events on an ICMP socket
|
2017-10-23 16:45:12 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock - The IPPROTO_ICMP socket of interest
|
|
|
|
* fds - The structure describing the events to be monitored, OR NULL if
|
|
|
|
* this is a request to stop monitoring events.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0: Success; Negated errno on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int icmp_pollteardown(FAR struct socket *psock, FAR struct pollfd *fds)
|
|
|
|
{
|
|
|
|
FAR struct icmp_conn_s *conn;
|
|
|
|
FAR struct icmp_poll_s *info;
|
|
|
|
|
2022-07-08 22:43:11 +02:00
|
|
|
/* Some of the following must be atomic */
|
|
|
|
|
|
|
|
net_lock();
|
2017-10-23 16:45:12 +02:00
|
|
|
|
|
|
|
conn = psock->s_conn;
|
|
|
|
|
2022-07-08 22:43:11 +02:00
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
if (!conn || !fds->priv)
|
|
|
|
{
|
|
|
|
net_unlock();
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2017-10-23 16:45:12 +02:00
|
|
|
/* Recover the socket descriptor poll state info from the poll structure */
|
|
|
|
|
|
|
|
info = (FAR struct icmp_poll_s *)fds->priv;
|
2022-07-08 22:43:11 +02:00
|
|
|
DEBUGASSERT(info->fds != NULL && info->cb != NULL);
|
2017-10-23 16:45:12 +02:00
|
|
|
|
|
|
|
if (info != NULL)
|
|
|
|
{
|
|
|
|
/* Release the callback */
|
|
|
|
|
2019-03-19 16:43:32 +01:00
|
|
|
icmp_callback_free(conn->dev, conn, info->cb);
|
2017-10-23 16:45:12 +02:00
|
|
|
|
|
|
|
/* Release the poll/select data slot */
|
|
|
|
|
|
|
|
info->fds->priv = NULL;
|
|
|
|
|
|
|
|
/* Then free the poll info container */
|
|
|
|
|
2019-12-31 16:26:14 +01:00
|
|
|
info->psock = NULL;
|
2017-10-23 16:45:12 +02:00
|
|
|
}
|
|
|
|
|
2022-07-08 22:43:11 +02:00
|
|
|
net_unlock();
|
|
|
|
|
2017-10-23 16:45:12 +02:00
|
|
|
return OK;
|
|
|
|
}
|