diff --git a/include/net/if.h b/include/net/if.h index f68f9e9f3a9..28da8d73242 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 c32eb10e974..1209791e66e 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 00000000000..d88c1f3a463 --- /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 + +#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 00000000000..42a91c25c73 --- /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 +#include + +#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 8e803c78e87..aba147c8036 100644 --- a/net/netdev/netdev.h +++ b/net/netdev/netdev.h @@ -36,16 +36,6 @@ # include #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 ****************************************************************************/