You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2021/12/19 16:09:11 UTC
[incubator-nuttx] 02/02: net: Move if_nametoindex and if_indextoname to libc
This is an automated email from the ASF dual-hosted git repository.
xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git
commit c56226320527e097bb41111914da3ad3cb3beaff
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Sun Dec 19 15:49:28 2021 +0800
net: Move if_nametoindex and if_indextoname to libc
Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
include/sys/syscall_lookup.h | 4 --
libs/libc/net/Make.defs | 1 +
.../libc/net/lib_indextoname.c | 82 +++++-----------------
.../libc/net/lib_nametoindex.c | 74 +++++--------------
net/netdev/Make.defs | 4 --
net/netdev/netdev.h | 42 -----------
syscall/syscall.csv | 2 -
7 files changed, 36 insertions(+), 173 deletions(-)
diff --git a/include/sys/syscall_lookup.h b/include/sys/syscall_lookup.h
index 022c8d3..ebd5e0e 100644
--- a/include/sys/syscall_lookup.h
+++ b/include/sys/syscall_lookup.h
@@ -218,10 +218,6 @@ SYSCALL_LOOKUP(pwrite, 4)
#ifdef CONFIG_EVENT_FD
SYSCALL_LOOKUP(eventfd, 2)
#endif
-#ifdef CONFIG_NETDEV_IFINDEX
- SYSCALL_LOOKUP(if_indextoname, 2)
- SYSCALL_LOOKUP(if_nametoindex, 1)
-#endif
#ifdef CONFIG_SERIAL_TERMIOS
SYSCALL_LOOKUP(tcdrain, 1)
#endif
diff --git a/libs/libc/net/Make.defs b/libs/libc/net/Make.defs
index 0c434f2..10aed94 100644
--- a/libs/libc/net/Make.defs
+++ b/libs/libc/net/Make.defs
@@ -34,6 +34,7 @@ CSRCS += lib_loopback.c
endif
ifeq ($(CONFIG_NETDEV_IFINDEX),y)
+CSRCS += lib_indextoname.c lib_nametoindex.c
CSRCS += lib_nameindex.c lib_freenameindex.c
endif
diff --git a/net/netdev/netdev_indextoname.c b/libs/libc/net/lib_indextoname.c
similarity index 58%
rename from net/netdev/netdev_indextoname.c
rename to libs/libc/net/lib_indextoname.c
index 46f147d..48cc003 100644
--- a/net/netdev/netdev_indextoname.c
+++ b/libs/libc/net/lib_indextoname.c
@@ -1,5 +1,5 @@
/****************************************************************************
- * net/netdev/netdev_indextoname.c
+ * libs/libc/net/lib_indextoname.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@@ -22,66 +22,18 @@
* Included Files
****************************************************************************/
-#include <nuttx/config.h>
-
-#include <string.h>
-#include <assert.h>
-#include <errno.h>
-
#include <net/if.h>
+#include <sys/ioctl.h>
+#include <string.h>
+#include <unistd.h>
-#include "nuttx/net/net.h"
-#include "nuttx/net/netdev.h"
-
-#include "netdev/netdev.h"
-
-#ifdef CONFIG_NETDEV_IFINDEX
+#include <nuttx/net/netconfig.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
- * Name: netdev_indextoname
- *
- * Description:
- * The if_indextoname() function maps an interface index to its
- * corresponding name.
- *
- * Input Parameters:
- * ifname - Points to a buffer of at least IF_NAMESIZE bytes.
- * if_indextoname() will place in this buffer the name of the
- * interface with index ifindex.
- *
- * Returned Value:
- * If ifindex is an interface index, then the function will return zero
- * (OK). Otherwise, the function returns a negated errno value;
- *
- ****************************************************************************/
-
-int netdev_indextoname(unsigned int ifindex, FAR char *ifname)
-{
- FAR struct net_driver_s *dev;
- int ret = -ENODEV;
-
- DEBUGASSERT(ifindex > 0 && ifindex <= MAX_IFINDEX);
- DEBUGASSERT(ifname != NULL);
-
- /* Find the driver with this name */
-
- net_lock();
- dev = netdev_findbyindex(ifindex);
- if (dev != NULL)
- {
- memcpy(ifname, dev->d_ifname, IF_NAMESIZE);
- ret = OK;
- }
-
- net_unlock();
- return ret;
-}
-
-/****************************************************************************
* Name: if_indextoname
*
* Description:
@@ -102,18 +54,20 @@ int netdev_indextoname(unsigned int ifindex, FAR char *ifname)
FAR char *if_indextoname(unsigned int ifindex, FAR char *ifname)
{
- int ret;
-
- /* Let netdev_indextoname to the work */
-
- ret = netdev_indextoname(ifindex, ifname);
- if (ret < 0)
+ int sockfd = socket(NET_SOCK_FAMILY, NET_SOCK_TYPE, NET_SOCK_PROTOCOL);
+ if (sockfd >= 0)
{
- set_errno(-ret);
- return NULL;
+ struct ifreq req;
+ req.ifr_ifindex = ifindex;
+ if (ioctl(sockfd, SIOCGIFNAME, (unsigned long)&req) >= 0)
+ {
+ strncpy(ifname, req.ifr_name, IF_NAMESIZE);
+ close(sockfd);
+ return ifname;
+ }
+
+ close(sockfd);
}
- return ifname;
+ return NULL;
}
-
-#endif /* CONFIG_NETDEV_IFINDEX */
diff --git a/net/netdev/netdev_nametoindex.c b/libs/libc/net/lib_nametoindex.c
similarity index 61%
rename from net/netdev/netdev_nametoindex.c
rename to libs/libc/net/lib_nametoindex.c
index 7e76d3b..1dcc028 100644
--- a/net/netdev/netdev_nametoindex.c
+++ b/libs/libc/net/lib_nametoindex.c
@@ -1,5 +1,5 @@
/****************************************************************************
- * net/netdev/netdev_nametoindex.c
+ * libs/libc/net/lib_nametoindex.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@@ -22,59 +22,18 @@
* Included Files
****************************************************************************/
-#include <nuttx/config.h>
-
-#include <assert.h>
-#include <errno.h>
-
#include <net/if.h>
+#include <sys/ioctl.h>
+#include <string.h>
+#include <unistd.h>
-#include "nuttx/net/net.h"
-#include "nuttx/net/netdev.h"
-
-#include "netdev/netdev.h"
-
-#ifdef CONFIG_NETDEV_IFINDEX
+#include <nuttx/net/netconfig.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
- * Name: netdev_nametoindex
- *
- * Description:
- * The if_nametoindex() function returns the interface index corresponding
- * to name ifname.
- *
- * Input Parameters:
- * ifname - The interface name
- *
- * Returned Value:
- * The corresponding index if ifname is the name of an interface;
- * otherwise, a negated errno value is returned.
- *
- ****************************************************************************/
-
-unsigned int netdev_nametoindex(FAR const char *ifname)
-{
- FAR struct net_driver_s *dev;
- unsigned int ifindex = -ENODEV;
-
- /* Find the driver with this name */
-
- net_lock();
- dev = netdev_findbyname(ifname);
- if (dev != NULL)
- {
- ifindex = dev->d_ifindex;
- }
-
- net_unlock();
- return ifindex;
-}
-
-/****************************************************************************
* Name: if_nametoindex
*
* Description:
@@ -92,18 +51,19 @@ unsigned int netdev_nametoindex(FAR const char *ifname)
unsigned int if_nametoindex(FAR const char *ifname)
{
- int ret;
-
- /* Let netdev_nametoindex to the work */
-
- ret = netdev_nametoindex(ifname);
- if (ret < 0)
+ int sockfd = socket(NET_SOCK_FAMILY, NET_SOCK_TYPE, NET_SOCK_PROTOCOL);
+ if (sockfd >= 0)
{
- set_errno(-ret);
- return 0;
+ struct ifreq req;
+ strncpy(req.ifr_name, ifname, IF_NAMESIZE);
+ if (ioctl(sockfd, SIOCGIFINDEX, (unsigned long)&req) >= 0)
+ {
+ close(sockfd);
+ return req.ifr_ifindex;
+ }
+
+ close(sockfd);
}
- return ret;
+ return 0;
}
-
-#endif /* CONFIG_NETDEV_IFINDEX */
diff --git a/net/netdev/Make.defs b/net/netdev/Make.defs
index 29738b1..31c3c78 100644
--- a/net/netdev/Make.defs
+++ b/net/netdev/Make.defs
@@ -26,10 +26,6 @@ NETDEV_CSRCS += netdev_count.c netdev_ifconf.c netdev_foreach.c
NETDEV_CSRCS += netdev_unregister.c netdev_carrier.c netdev_default.c
NETDEV_CSRCS += netdev_verify.c netdev_lladdrsize.c
-ifeq ($(CONFIG_NETDEV_IFINDEX),y)
-NETDEV_CSRCS += netdev_indextoname.c netdev_nametoindex.c
-endif
-
ifeq ($(CONFIG_NETDOWN_NOTIFIER),y)
SOCK_CSRCS += netdown_notifier.c
endif
diff --git a/net/netdev/netdev.h b/net/netdev/netdev.h
index aba147c..bc47544 100644
--- a/net/netdev/netdev.h
+++ b/net/netdev/netdev.h
@@ -288,48 +288,6 @@ int netdev_nextindex(int ifindex);
#endif
/****************************************************************************
- * Name: netdev_indextoname
- *
- * Description:
- * The if_indextoname() function maps an interface index to its
- * corresponding name.
- *
- * Input Parameters:
- * ifname - Points to a buffer of at least IF_NAMESIZE bytes.
- * if_indextoname() will place in this buffer the name of the
- * interface with index ifindex.
- *
- * Returned Value:
- * If ifindex is an interface index, then the function will return zero
- * (OK). Otherwise, the function returns a negated errno value;
- *
- ****************************************************************************/
-
-#ifdef CONFIG_NETDEV_IFINDEX
-int netdev_indextoname(unsigned int ifindex, FAR char *ifname);
-#endif
-
-/****************************************************************************
- * Name: netdev_nametoindex
- *
- * Description:
- * The if_nametoindex() function returns the interface index corresponding
- * to name ifname.
- *
- * Input Parameters:
- * ifname - The interface name
- *
- * Returned Value:
- * The corresponding index if ifname is the name of an interface;
- * otherwise, a negated errno value is returned.
- *
- ****************************************************************************/
-
-#ifdef CONFIG_NETDEV_IFINDEX
-unsigned int netdev_nametoindex(FAR const char *ifname);
-#endif
-
-/****************************************************************************
* Name: netdev_default
*
* Description:
diff --git a/syscall/syscall.csv b/syscall/syscall.csv
index 282f6ae..3c8daf7 100644
--- a/syscall/syscall.csv
+++ b/syscall/syscall.csv
@@ -46,8 +46,6 @@
"getsockopt","sys/socket.h","defined(CONFIG_NET)","int","int","int","int","FAR void *","FAR socklen_t *"
"gettid","unistd.h","","pid_t"
"getuid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","uid_t"
-"if_indextoname","net/if.h","defined(CONFIG_NETDEV_IFINDEX)","FAR char *","unsigned int","FAR char *"
-"if_nametoindex","net/if.h","defined(CONFIG_NETDEV_IFINDEX)","unsigned int","FAR const char *"
"insmod","nuttx/module.h","defined(CONFIG_MODULE)","FAR void *","FAR const char *","FAR const char *"
"ioctl","sys/ioctl.h","","int","int","int","...","unsigned long"
"kill","signal.h","","int","pid_t","int"