net: Move if_nametoindex and if_indextoname to libc

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao
2021-12-19 15:49:28 +08:00
committed by Xiang Xiao
parent 882bc8639c
commit c562263205
7 changed files with 36 additions and 173 deletions
-4
View File
@@ -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
+1
View File
@@ -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
@@ -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,65 +22,17 @@
* 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
*
@@ -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 */
@@ -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,58 +22,17 @@
* 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
*
@@ -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 */
-4
View File
@@ -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
-42
View File
@@ -287,48 +287,6 @@ FAR struct net_driver_s *netdev_findbyindex(int ifindex);
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
*
-2
View File
@@ -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"
1 _exit unistd.h noreturn int
46 getsockopt sys/socket.h defined(CONFIG_NET) int int
47 gettid unistd.h pid_t
48 getuid unistd.h defined(CONFIG_SCHED_USER_IDENTITY) uid_t
if_indextoname net/if.h defined(CONFIG_NETDEV_IFINDEX) FAR char * unsigned int
if_nametoindex net/if.h defined(CONFIG_NETDEV_IFINDEX) unsigned int FAR const char *
49 insmod nuttx/module.h defined(CONFIG_MODULE) FAR void * FAR const char *
50 ioctl sys/ioctl.h int int
51 kill signal.h int pid_t