net: Add if_nameindex and if_freenameindex API

Specified here:
https://man7.org/linux/man-pages/man3/if_nameindex.3.html

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
Change-Id: Ia5daed775bd11fa5978d54860632554da8f7b416
This commit is contained in:
Xiang Xiao 2021-06-22 11:19:06 +08:00 committed by Abdelatif Guettouche
parent b88b5b14c8
commit 20f5a28e49
5 changed files with 195 additions and 10 deletions

View file

@ -32,6 +32,12 @@
* Pre-processor Definitions
****************************************************************************/
/* If CONFIG_NETDEV_IFINDEX is enabled then there is limit to the number of
* devices that can be registered due to the nature of some static data.
*/
#define MAX_IFINDEX 32
/* Sizing parameters */
#define IFNAMSIZ 16 /* Older naming standard */
@ -103,6 +109,12 @@
* Public Type Definitions
****************************************************************************/
struct if_nameindex
{
unsigned int if_index; /* 1, 2, ... */
FAR char *if_name; /* null terminated name: "eth0", ... */
};
/* Structure passed with the SIOCMIINOTIFY ioctl command to enable
* notification of of PHY state changes.
*/
@ -298,6 +310,48 @@ unsigned int if_nametoindex(FAR const char *ifname);
FAR char *if_indextoname(unsigned int ifindex, FAR char *ifname);
/****************************************************************************
* Name: if_nameindex
*
* Description:
* The if_nameindex() function returns an array of if_nameindex structures,
* each containing information about one of the network interfaces on the
* local system. The if_nameindex structure contains at least the following
* entries:
* unsigned int if_index;
* FAR char *if_name;
* The if_index field contains the interface index. The if_name field
* points to the null-terminated interface name. The end of the array
* is indicated by entry with if_index set to zero and if_name set to NULL.
*
* Input Parameters:
* None
*
* Returned Value:
* On success, if_nameindex() returns pointer to the array; on error, NULL
* is returned, and errno is set to indicate the error.
*
****************************************************************************/
FAR struct if_nameindex *if_nameindex(void);
/****************************************************************************
* Name: if_freenameindex
*
* Description:
* The if_freenameindex() function free the data structure returned by
* if_nameindex().
*
* Input Parameters:
* ifn - The data structure to free
*
* Returned Value:
* None
*
****************************************************************************/
void if_freenameindex(FAR struct if_nameindex *ifn);
#undef EXTERN
#ifdef __cplusplus
}

View file

@ -33,6 +33,10 @@ ifeq ($(CONFIG_NET_LOOPBACK),y)
CSRCS += lib_loopback.c
endif
ifeq ($(CONFIG_NETDEV_IFINDEX),y)
CSRCS += lib_nameindex.c lib_freenameindex.c
endif
# Routing table support
ifeq ($(CONFIG_NET_ROUTE),y)

View file

@ -0,0 +1,51 @@
/****************************************************************************
* libs/libc/net/lib_freenameindex.c
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <net/if.h>
#include "libc.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: if_freenameindex
*
* Description:
* The if_freenameindex() function free the data structure returned by
* if_nameindex().
*
* Input Parameters:
* ifn - The data structure to free
*
* Returned Value:
* None
*
****************************************************************************/
void if_freenameindex(FAR struct if_nameindex *ifn)
{
lib_free(ifn);
}

View file

@ -0,0 +1,86 @@
/****************************************************************************
* libs/libc/net/lib_nameindex.c
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <net/if.h>
#include <errno.h>
#include "libc.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: if_nameindex
*
* Description:
* The if_nameindex() function returns an array of if_nameindex structures,
* each containing information about one of the network interfaces on the
* local system. The if_nameindex structure contains at least the following
* entries:
* unsigned int if_index;
* FAR char *if_name;
* The if_index field contains the interface index. The if_name field
* points to the null-terminated interface name. The end of the array
* is indicated by entry with if_index set to zero and if_name set to NULL.
*
* Input Parameters:
* None
*
* Returned Value:
* On success, if_nameindex() returns pointer to the array; on error, NULL
* is returned, and errno is set to indicate the error.
*
****************************************************************************/
FAR struct if_nameindex *if_nameindex(void)
{
FAR struct if_nameindex *ifn;
FAR char *buf;
int i = 0;
int j;
ifn = lib_malloc((sizeof(*ifn) + IF_NAMESIZE) * MAX_IFINDEX);
if (ifn == NULL)
{
set_errno(ENOBUFS);
return NULL;
}
buf = (FAR char *)(ifn + MAX_IFINDEX);
for (j = 1; j <= MAX_IFINDEX; j++)
{
ifn[i].if_name = if_indextoname(j, buf);
if (ifn[i].if_name)
{
ifn[i++].if_index = j;
buf += IF_NAMESIZE;
}
}
ifn[i].if_name = NULL;
ifn[i].if_index = 0;
return ifn;
}

View file

@ -36,16 +36,6 @@
# include <nuttx/wqueue.h>
#endif
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* If CONFIG_NETDEV_IFINDEX is enabled then there is limit to the number of
* devices that can be registered due to the nature of some static data.
*/
#define MAX_IFINDEX 32
/****************************************************************************
* Public Data
****************************************************************************/