This is an automated email from the ASF dual-hosted git repository. aguettouche pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git
The following commit(s) were added to refs/heads/master by this push: new 20f5a28 net: Add if_nameindex and if_freenameindex API 20f5a28 is described below commit 20f5a28e49c921d6ed3638d468248d0ea68b8a53 Author: Xiang Xiao <xiaoxi...@xiaomi.com> AuthorDate: Tue Jun 22 11:19:06 2021 +0800 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 <xiaoxi...@xiaomi.com> Change-Id: Ia5daed775bd11fa5978d54860632554da8f7b416 --- include/net/if.h | 54 ++++++++++++++++++++++++ libs/libc/net/Make.defs | 4 ++ libs/libc/net/lib_freenameindex.c | 51 +++++++++++++++++++++++ libs/libc/net/lib_nameindex.c | 86 +++++++++++++++++++++++++++++++++++++++ net/netdev/netdev.h | 10 ----- 5 files changed, 195 insertions(+), 10 deletions(-) diff --git a/include/net/if.h b/include/net/if.h index f68f9e9..28da8d7 100644 --- a/include/net/if.h +++ b/include/net/if.h @@ -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 } diff --git a/libs/libc/net/Make.defs b/libs/libc/net/Make.defs index c32eb10..1209791 100644 --- a/libs/libc/net/Make.defs +++ b/libs/libc/net/Make.defs @@ -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) diff --git a/libs/libc/net/lib_freenameindex.c b/libs/libc/net/lib_freenameindex.c new file mode 100644 index 0000000..d88c1f3 --- /dev/null +++ b/libs/libc/net/lib_freenameindex.c @@ -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); +} diff --git a/libs/libc/net/lib_nameindex.c b/libs/libc/net/lib_nameindex.c new file mode 100644 index 0000000..42a91c2 --- /dev/null +++ b/libs/libc/net/lib_nameindex.c @@ -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; +} diff --git a/net/netdev/netdev.h b/net/netdev/netdev.h index 8e803c7..aba147c 100644 --- a/net/netdev/netdev.h +++ b/net/netdev/netdev.h @@ -37,16 +37,6 @@ #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 ****************************************************************************/