You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2022/09/06 17:53:17 UTC

[GitHub] [incubator-nuttx] xiaoxiang781216 commented on a diff in pull request #7020: net/netdev: simplify handling of netdev ifr ioctl()

xiaoxiang781216 commented on code in PR #7020:
URL: https://github.com/apache/incubator-nuttx/pull/7020#discussion_r964006952


##########
net/netdev/netdev_ioctl.c:
##########
@@ -636,235 +636,204 @@ static FAR struct net_driver_s *netdev_ifr_dev(FAR struct ifreq *req)
 static int netdev_ifr_ioctl(FAR struct socket *psock, int cmd,
                             FAR struct ifreq *req)
 {
-  FAR struct net_driver_s *dev;
-  int ret = -EINVAL;
+  FAR struct net_driver_s *dev = NULL;
+  FAR struct net_driver_s *tmpdev;
+  ssize_t arglen;
+  int ret;
 
   ninfo("cmd: %d\n", cmd);
 
   net_lock();
 
-  /* Execute the command */
+  /* Execute the command without ifr_name or lifr_name */
 
   switch (cmd)
     {
+      case SIOCGIFCOUNT:  /* Get number of devices */
+        {
+          req->ifr_count = netdev_count();
+          ret = OK;

Review Comment:
   let's set ret = OK at line 642 and remove this line



##########
net/netdev/netdev_ioctl.c:
##########
@@ -636,235 +636,204 @@ static FAR struct net_driver_s *netdev_ifr_dev(FAR struct ifreq *req)
 static int netdev_ifr_ioctl(FAR struct socket *psock, int cmd,
                             FAR struct ifreq *req)
 {
-  FAR struct net_driver_s *dev;
-  int ret = -EINVAL;
+  FAR struct net_driver_s *dev = NULL;
+  FAR struct net_driver_s *tmpdev;
+  ssize_t arglen;
+  int ret;
 
   ninfo("cmd: %d\n", cmd);
 
   net_lock();
 
-  /* Execute the command */
+  /* Execute the command without ifr_name or lifr_name */
 
   switch (cmd)
     {
+      case SIOCGIFCOUNT:  /* Get number of devices */
+        {
+          req->ifr_count = netdev_count();
+          ret = OK;
+        }
+        break;
+
 #ifdef CONFIG_NET_IPv4
-      case SIOCGIFADDR:  /* Get IP address */
+      case SIOCGIFCONF:  /* Return an interface list (IPv4) */
         {
-          dev = netdev_ifr_dev(req);
-          if (dev)
-            {
-              ioctl_get_ipv4addr(&req->ifr_addr, dev->d_ipaddr);
-              ret = OK;
-            }
+          ret = netdev_ipv4_ifconf((FAR struct ifconf *)req);
         }
         break;
 #endif
 
-#ifdef CONFIG_NET_IPv4
-      case SIOCSIFADDR:  /* Set IP address */
+#ifdef CONFIG_NET_IPv6
+      case SIOCGLIFCONF:  /* Return an interface list (IPv6) */
         {
-          dev = netdev_ifr_dev(req);
-          if (dev)
-            {
-              ioctl_set_ipv4addr(&dev->d_ipaddr, &req->ifr_addr);
-              ret = OK;
-            }
+          ret = netdev_ipv6_ifconf((FAR struct lifconf *)req);
         }
         break;
 #endif
 
-#ifdef CONFIG_NET_IPv4
-      case SIOCGIFDSTADDR:  /* Get P-to-P address */
+#ifdef CONFIG_NETDEV_IFINDEX
+      case SIOCGIFNAME:  /* Get interface name */
         {
-          dev = netdev_ifr_dev(req);
-          if (dev)
+          tmpdev = netdev_findbyindex(req->ifr_ifindex);
+          if (tmpdev != NULL)
             {
-              ioctl_get_ipv4addr(&req->ifr_dstaddr, dev->d_draddr);
+              strlcpy(req->ifr_name, tmpdev->d_ifname, IFNAMSIZ);
               ret = OK;

Review Comment:
   remove



##########
net/netdev/netdev_ioctl.c:
##########
@@ -636,235 +636,204 @@ static FAR struct net_driver_s *netdev_ifr_dev(FAR struct ifreq *req)
 static int netdev_ifr_ioctl(FAR struct socket *psock, int cmd,
                             FAR struct ifreq *req)
 {
-  FAR struct net_driver_s *dev;
-  int ret = -EINVAL;
+  FAR struct net_driver_s *dev = NULL;
+  FAR struct net_driver_s *tmpdev;
+  ssize_t arglen;
+  int ret;
 
   ninfo("cmd: %d\n", cmd);
 
   net_lock();
 
-  /* Execute the command */
+  /* Execute the command without ifr_name or lifr_name */
 
   switch (cmd)
     {
+      case SIOCGIFCOUNT:  /* Get number of devices */
+        {
+          req->ifr_count = netdev_count();
+          ret = OK;
+        }
+        break;
+
 #ifdef CONFIG_NET_IPv4
-      case SIOCGIFADDR:  /* Get IP address */
+      case SIOCGIFCONF:  /* Return an interface list (IPv4) */
         {
-          dev = netdev_ifr_dev(req);
-          if (dev)
-            {
-              ioctl_get_ipv4addr(&req->ifr_addr, dev->d_ipaddr);
-              ret = OK;
-            }
+          ret = netdev_ipv4_ifconf((FAR struct ifconf *)req);
         }
         break;
 #endif
 
-#ifdef CONFIG_NET_IPv4
-      case SIOCSIFADDR:  /* Set IP address */
+#ifdef CONFIG_NET_IPv6
+      case SIOCGLIFCONF:  /* Return an interface list (IPv6) */
         {
-          dev = netdev_ifr_dev(req);
-          if (dev)
-            {
-              ioctl_set_ipv4addr(&dev->d_ipaddr, &req->ifr_addr);
-              ret = OK;
-            }
+          ret = netdev_ipv6_ifconf((FAR struct lifconf *)req);
         }
         break;
 #endif
 
-#ifdef CONFIG_NET_IPv4
-      case SIOCGIFDSTADDR:  /* Get P-to-P address */
+#ifdef CONFIG_NETDEV_IFINDEX
+      case SIOCGIFNAME:  /* Get interface name */
         {
-          dev = netdev_ifr_dev(req);
-          if (dev)
+          tmpdev = netdev_findbyindex(req->ifr_ifindex);
+          if (tmpdev != NULL)
             {
-              ioctl_get_ipv4addr(&req->ifr_dstaddr, dev->d_draddr);
+              strlcpy(req->ifr_name, tmpdev->d_ifname, IFNAMSIZ);
               ret = OK;
             }
+          else
+            {
+              ret = -ENODEV;
+            }
         }
         break;
 #endif
-
-#ifdef CONFIG_NET_IPv4
-      case SIOCSIFDSTADDR:  /* Set P-to-P address */
+      default:
         {
-          dev = netdev_ifr_dev(req);
-          if (dev)
+          arglen = net_ioctl_arglen(cmd);
+
+          if (arglen == sizeof(struct ifreq) ||

Review Comment:
   why check the size of ifreq or lifreq? it isn't reliable.



##########
net/netdev/netdev_ioctl.c:
##########
@@ -636,235 +636,204 @@ static FAR struct net_driver_s *netdev_ifr_dev(FAR struct ifreq *req)
 static int netdev_ifr_ioctl(FAR struct socket *psock, int cmd,
                             FAR struct ifreq *req)
 {
-  FAR struct net_driver_s *dev;
-  int ret = -EINVAL;
+  FAR struct net_driver_s *dev = NULL;
+  FAR struct net_driver_s *tmpdev;
+  ssize_t arglen;
+  int ret;
 
   ninfo("cmd: %d\n", cmd);
 
   net_lock();
 
-  /* Execute the command */
+  /* Execute the command without ifr_name or lifr_name */
 
   switch (cmd)
     {
+      case SIOCGIFCOUNT:  /* Get number of devices */
+        {
+          req->ifr_count = netdev_count();
+          ret = OK;
+        }
+        break;
+
 #ifdef CONFIG_NET_IPv4
-      case SIOCGIFADDR:  /* Get IP address */
+      case SIOCGIFCONF:  /* Return an interface list (IPv4) */
         {
-          dev = netdev_ifr_dev(req);
-          if (dev)
-            {
-              ioctl_get_ipv4addr(&req->ifr_addr, dev->d_ipaddr);
-              ret = OK;
-            }
+          ret = netdev_ipv4_ifconf((FAR struct ifconf *)req);
         }
         break;
 #endif
 
-#ifdef CONFIG_NET_IPv4
-      case SIOCSIFADDR:  /* Set IP address */
+#ifdef CONFIG_NET_IPv6
+      case SIOCGLIFCONF:  /* Return an interface list (IPv6) */
         {
-          dev = netdev_ifr_dev(req);
-          if (dev)
-            {
-              ioctl_set_ipv4addr(&dev->d_ipaddr, &req->ifr_addr);
-              ret = OK;
-            }
+          ret = netdev_ipv6_ifconf((FAR struct lifconf *)req);
         }
         break;
 #endif
 
-#ifdef CONFIG_NET_IPv4
-      case SIOCGIFDSTADDR:  /* Get P-to-P address */
+#ifdef CONFIG_NETDEV_IFINDEX
+      case SIOCGIFNAME:  /* Get interface name */
         {
-          dev = netdev_ifr_dev(req);
-          if (dev)
+          tmpdev = netdev_findbyindex(req->ifr_ifindex);

Review Comment:
   why not use dev directly



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org