2015-11-27 23:39:14 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* net/netdev/netdev_findbyindex.c
|
|
|
|
*
|
2024-09-11 14:39:39 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
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
|
2015-11-27 23:39:14 +01:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-11-27 23:39:14 +01: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.
|
2015-11-27 23:39:14 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
2018-06-25 20:08:10 +02:00
|
|
|
#include <assert.h>
|
2015-11-27 23:39:14 +01:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/net/netdev.h>
|
|
|
|
|
|
|
|
#include "utils/utils.h"
|
|
|
|
#include "netdev/netdev.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: netdev_findbyindex
|
2015-11-27 23:39:14 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2018-06-25 20:08:10 +02:00
|
|
|
* Find a previously registered network device by assigned interface index.
|
2015-11-27 23:39:14 +01:00
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2018-06-26 00:55:45 +02:00
|
|
|
* ifindex - The interface index. This is a one-based index and must be
|
|
|
|
* greater than zero.
|
2015-11-27 23:39:14 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2018-06-25 20:08:10 +02:00
|
|
|
* Pointer to driver on success; NULL on failure. This function will return
|
|
|
|
* NULL only if there is no device corresponding to the provided index.
|
2015-11-27 23:39:14 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-06-25 20:08:10 +02:00
|
|
|
FAR struct net_driver_s *netdev_findbyindex(int ifindex)
|
2015-11-27 23:39:14 +01:00
|
|
|
{
|
|
|
|
FAR struct net_driver_s *dev;
|
2018-06-25 20:08:10 +02:00
|
|
|
#ifdef CONFIG_NETDEV_IFINDEX
|
|
|
|
/* The bit index is the interface index minus one. Zero is reserved in
|
|
|
|
* POSIX to mean no interface index.
|
|
|
|
*/
|
|
|
|
|
2018-06-26 00:55:45 +02:00
|
|
|
if (ifindex < 1 || ifindex > MAX_IFINDEX)
|
2018-06-25 20:08:10 +02:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-11-22 00:33:00 +01:00
|
|
|
#else
|
|
|
|
int i = 0;
|
|
|
|
|
2018-06-25 20:08:10 +02:00
|
|
|
#endif
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_lock();
|
2018-06-25 20:08:10 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_NETDEV_IFINDEX
|
|
|
|
/* Check if this index has been assigned */
|
|
|
|
|
2021-06-22 16:26:13 +02:00
|
|
|
if ((g_devset & (1UL << (ifindex - 1))) == 0)
|
2018-06-25 20:08:10 +02:00
|
|
|
{
|
|
|
|
/* This index has not been assigned */
|
|
|
|
|
|
|
|
net_unlock();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2022-11-22 00:33:00 +01:00
|
|
|
for (dev = g_netdevices; dev; dev = dev->flink)
|
2015-11-27 23:39:14 +01:00
|
|
|
{
|
2018-06-25 20:08:10 +02:00
|
|
|
#ifdef CONFIG_NETDEV_IFINDEX
|
|
|
|
/* Check if the index matches the index assigned when the device was
|
|
|
|
* registered.
|
|
|
|
*/
|
|
|
|
|
2019-07-03 02:02:23 +02:00
|
|
|
if (dev->d_ifindex == ifindex)
|
2018-06-25 20:08:10 +02:00
|
|
|
#else
|
|
|
|
/* NOTE that this option is not a safe way to enumerate network
|
|
|
|
* devices: There could be changes to the list of registered device
|
|
|
|
* causing a given index to be meaningless (unless, of course, the
|
|
|
|
* caller keeps the network locked).
|
|
|
|
*/
|
2018-06-26 00:55:45 +02:00
|
|
|
|
2022-11-22 00:33:00 +01:00
|
|
|
if (++i == ifindex)
|
2018-06-25 20:08:10 +02:00
|
|
|
#endif
|
2015-11-27 23:39:14 +01:00
|
|
|
{
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2015-11-27 23:39:14 +01:00
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-03 23:28:19 +01:00
|
|
|
net_unlock();
|
2015-11-27 23:39:14 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-06-25 20:08:10 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: netdev_nextindex
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return the interface index to the next valid device.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* ifindex - The first interface index to check. Usually in a traversal
|
2018-06-26 00:55:45 +02:00
|
|
|
* this would be the previous interface index plus 1.
|
2018-06-25 20:08:10 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The interface index for the next network driver. -ENODEV is returned if
|
|
|
|
* there are no further devices with assigned interface indices.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NETDEV_IFINDEX
|
|
|
|
int netdev_nextindex(int ifindex)
|
|
|
|
{
|
|
|
|
/* The bit index is the interface index minus one. Zero is reserved in
|
|
|
|
* POSIX to mean no interface index.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(ifindex > 0 && ifindex <= MAX_IFINDEX);
|
|
|
|
ifindex--;
|
|
|
|
|
|
|
|
if (ifindex >= 0 && ifindex < MAX_IFINDEX)
|
|
|
|
{
|
|
|
|
net_lock();
|
|
|
|
for (; ifindex < MAX_IFINDEX; ifindex++)
|
|
|
|
{
|
2021-06-22 16:26:13 +02:00
|
|
|
if ((g_devset & (1UL << ifindex)) != 0)
|
2018-06-25 20:08:10 +02:00
|
|
|
{
|
2018-10-29 23:25:57 +01:00
|
|
|
/* NOTE that the index + 1 is returned. Zero is reserved to
|
|
|
|
* mean no-index in the POSIX standards.
|
|
|
|
*/
|
2018-06-25 20:08:10 +02:00
|
|
|
|
2018-10-29 23:25:57 +01:00
|
|
|
net_unlock();
|
|
|
|
return ifindex + 1;
|
2019-07-03 02:02:23 +02:00
|
|
|
}
|
2018-06-25 20:08:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
net_unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
#endif
|