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:09 UTC

[incubator-nuttx] branch master updated (0896d06 -> c562263)

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git.


    from 0896d06  eventfd: Change mode_semaphore from uint8_t to bool
     new 882bc86  net: Add NET_SOCK_[FAMILY|TYPE|PROTOCOL] defintion to netconfig.h
     new c562263  net: Move if_nametoindex and if_indextoname to libc

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 include/nuttx/net/netconfig.h                      |  95 +++++++++++++++-
 include/sys/syscall_lookup.h                       |   4 -
 libs/libc/net/Make.defs                            |   1 +
 .../libc/net/lib_indextoname.c                     |  49 +++++----
 .../libc/net/lib_nametoindex.c                     |  47 ++++----
 net/netdev/Make.defs                               |   4 -
 net/netdev/netdev.h                                |  42 --------
 net/netdev/netdev_indextoname.c                    | 119 ---------------------
 net/netdev/netdev_nametoindex.c                    | 109 -------------------
 syscall/syscall.csv                                |   2 -
 10 files changed, 136 insertions(+), 336 deletions(-)
 copy net/netdev/netdev_findbyname.c => libs/libc/net/lib_indextoname.c (61%)
 copy net/netdev/netdev_findbyname.c => libs/libc/net/lib_nametoindex.c (66%)
 delete mode 100644 net/netdev/netdev_indextoname.c
 delete mode 100644 net/netdev/netdev_nametoindex.c

[incubator-nuttx] 02/02: net: Move if_nametoindex and if_indextoname to libc

Posted by xi...@apache.org.
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"

[incubator-nuttx] 01/02: net: Add NET_SOCK_[FAMILY|TYPE|PROTOCOL] defintion to netconfig.h

Posted by xi...@apache.org.
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 882bc8639cdb7c0fa8c5f6f7c889616fdc8976f6
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Sun Dec 19 16:03:14 2021 +0800

    net: Add NET_SOCK_[FAMILY|TYPE|PROTOCOL] defintion to netconfig.h
    
    to help create a general sock for ioctl operation
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 include/nuttx/net/netconfig.h | 95 ++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 90 insertions(+), 5 deletions(-)

diff --git a/include/nuttx/net/netconfig.h b/include/nuttx/net/netconfig.h
index 18d5d29..0486a73 100644
--- a/include/nuttx/net/netconfig.h
+++ b/include/nuttx/net/netconfig.h
@@ -57,11 +57,7 @@
 #include <nuttx/net/ethernet.h>
 
 /****************************************************************************
- * Public Definitions
- ****************************************************************************/
-
-/****************************************************************************
- * Public Type Definitions
+ * Pre-processor Definitions
  ****************************************************************************/
 
 #ifndef MAX
@@ -72,6 +68,95 @@
 #  define MIN(a,b) ((a) < (b) ? (a) : (b))
 #endif
 
+/* Using the following definitions, the following socket() arguments should
+ * provide a valid socket in all configurations:
+ *
+ *   ret = socket(NET_SOCK_FAMILY, NET_SOCK_TYPE,
+ *                NET_SOCK_PROTOCOL);
+ */
+
+/* The address family that we used to create the socket really does not
+ * matter.  It should, however, be valid in the current configuration.
+ */
+
+#if defined(CONFIG_NET_IPv4)
+#  define NET_SOCK_FAMILY  AF_INET
+#elif defined(CONFIG_NET_IPv6)
+#  define NET_SOCK_FAMILY  AF_INET6
+#elif defined(CONFIG_NET_LOCAL)
+#  define NET_SOCK_FAMILY  AF_LOCAL
+#elif defined(CONFIG_NET_PKT)
+#  define NET_SOCK_FAMILY  AF_PACKET
+#elif defined(CONFIG_NET_CAN)
+#  define NET_SOCK_FAMILY  AF_CAN
+#elif defined(CONFIG_NET_IEEE802154)
+#  define NET_SOCK_FAMILY  AF_IEEE802154
+#elif defined(CONFIG_WIRELESS_PKTRADIO)
+#  define NET_SOCK_FAMILY  AF_PKTRADIO
+#elif defined(CONFIG_NET_BLUETOOTH)
+#  define NET_SOCK_FAMILY  AF_BLUETOOTH
+#elif defined(CONFIG_NET_USRSOCK)
+#  define NET_SOCK_FAMILY  AF_INET
+#elif defined(CONFIG_NET_NETLINK)
+#  define NET_SOCK_FAMILY  AF_NETLINK
+#elif defined(CONFIG_NET_RPMSG)
+#  define NET_SOCK_FAMILY  AF_RPMSG
+#else
+#  define NET_SOCK_FAMILY  AF_UNSPEC
+#endif
+
+/* Socket protocol of zero normally works */
+
+#define NET_SOCK_PROTOCOL  0
+
+/* SOCK_DGRAM is the preferred socket type to use when we just want a
+ * socket for performing driver ioctls.  However, we can't use SOCK_DRAM
+ * if UDP is disabled.
+ *
+ * Pick a socket type (and perhaps protocol) compatible with the currently
+ * selected address family.
+ */
+
+#if NET_SOCK_FAMILY == AF_INET
+#  if defined(CONFIG_NET_UDP)
+#    define NET_SOCK_TYPE SOCK_DGRAM
+#  elif defined(CONFIG_NET_TCP)
+#   define NET_SOCK_TYPE SOCK_STREAM
+#  elif defined(CONFIG_NET_ICMP_SOCKET)
+#   define NET_SOCK_TYPE SOCK_DGRAM
+#   undef NET_SOCK_PROTOCOL
+#   define NET_SOCK_PROTOCOL IPPROTO_ICMP
+#  endif
+#elif NET_SOCK_FAMILY == AF_INET6
+#  if defined(CONFIG_NET_UDP)
+#    define NET_SOCK_TYPE SOCK_DGRAM
+#  elif defined(CONFIG_NET_TCP)
+#   define NET_SOCK_TYPE SOCK_STREAM
+#  elif defined(CONFIG_NET_ICMPv6_SOCKET)
+#   define NET_SOCK_TYPE SOCK_DGRAM
+#   undef NET_SOCK_PROTOCOL
+#   define NET_SOCK_PROTOCOL IPPROTO_ICMP6
+#  endif
+#elif NET_SOCK_FAMILY == AF_LOCAL
+#  if defined(CONFIG_NET_LOCAL_DGRAM)
+#    define NET_SOCK_TYPE SOCK_DGRAM
+#  elif defined(CONFIG_NET_LOCAL_STREAM)
+#     define NET_SOCK_TYPE SOCK_STREAM
+#  endif
+#elif NET_SOCK_FAMILY == AF_PACKET
+#  define NET_SOCK_TYPE SOCK_RAW
+#elif NET_SOCK_FAMILY == AF_CAN
+#  define NET_SOCK_TYPE SOCK_RAW
+#elif NET_SOCK_FAMILY == AF_IEEE802154
+#  define NET_SOCK_TYPE SOCK_DGRAM
+#elif NET_SOCK_FAMILY == AF_BLUETOOTH
+#  define NET_SOCK_TYPE SOCK_RAW
+#elif NET_SOCK_FAMILY == AF_NETLINK
+#  define NET_SOCK_TYPE SOCK_DGRAM
+#elif NET_SOCK_FAMILY == AF_RPMSG
+#  define NET_SOCK_TYPE SOCK_STREAM
+#endif
+
 /* Eliminate dependencies on other header files.  This should not harm
  * portability because these are well-known constants.
  */