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 23:29:55 UTC

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

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


##########
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 */

Review Comment:
   Maybe change comment to:
   
   `/* Execute commands that do not need ifr_name or lifr_name */`
   
   or something like that?



##########
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) ||
+              arglen == sizeof(struct lifreq))
             {
-              ioctl_set_ipv4addr(&dev->d_draddr, &req->ifr_dstaddr);
-              ret = OK;
+              dev = netdev_ifr_dev(req);
+              ret = (dev == NULL) ? -ENODEV : OK;
+            }
+          else
+            {
+              ret = -ENOTTY;
             }
         }
         break;
-#endif
+    }
 
+  if (dev == NULL)
+    {
+      return ret;
+    }
+
+  /* Execute the command with ifr_name or lifr_name */

Review Comment:
   If changing comment mentioned above, then I suggest to change this one also, for consistency, for example:
   
   `/* Execute commands that need ifr_name or lifr_name */`
   



##########
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:
   Because originally there was one `switch`, but now there are two.
   
   The first `switch` handles IOCTLs that do not require `dev`, and the `default` gets the `dev`.
   
   Then there is a check if `dev` is `NULL` then we return.
   
   Then, the second `switch` handles all the other IOCTLs.
   
   If use `dev` directly, then we will execute the second `switch` when we shouldn't.
   
   This is a nice refactoring because:
   
   1. Instead of many places getting `dev`, now there is only one. (Reduce code size.)
   2. A lot of `#ifdef` rash is reduced by grouping IPv4, IPv6, Ethernet, and 6LOWPAN IOCTLs.
   
   I think it is correct (just by code reading). The diff is hard to read, though, because diff algorithm is grouping things strangely.



-- 
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