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/nuttx-apps.git

commit 2ad05e3062e2d462acd85d51318a92e821a70bbf
Author: Zhe Weng <[email protected]>
AuthorDate: Wed Dec 28 18:38:42 2022 +0800

    ifconfig: Support prefixlen/CIDR for IPv6
    
    Linux:
    ifconfig eth0 [inet6] add 2022::2/96
    
    FreeBSD:
    ifconfig eth0 inet6 2022::2/96
    ifconfig eth0 inet6 2022::2 prefixlen 96
    
    NuttX newly supported:
    ifconfig eth0 inet6 [add] 2022::2/96
    ifconfig eth0 inet6 [add] 2022::2 prefixlen 96
    
    Ref:
    https://man7.org/linux/man-pages/man8/ifconfig.8.html
    https://www.freebsd.org/cgi/man.cgi?ifconfig
    
    Signed-off-by: Zhe Weng <[email protected]>
---
 netutils/netlib/netlib_prefix2ipv6netmask.c |  6 +++---
 nshlib/README.md                            |  2 +-
 nshlib/nsh_netcmds.c                        | 30 +++++++++++++++++++++++++++++
 3 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/netutils/netlib/netlib_prefix2ipv6netmask.c 
b/netutils/netlib/netlib_prefix2ipv6netmask.c
index d72117641..c8f7a71d9 100644
--- a/netutils/netlib/netlib_prefix2ipv6netmask.c
+++ b/netutils/netlib/netlib_prefix2ipv6netmask.c
@@ -69,7 +69,7 @@ void netlib_prefix2ipv6netmask(uint8_t preflen, FAR struct 
in6_addr *netmask)
    *              1..6 7..2 3..8 9..4  5..0 1..6 7..2 3..8
    */
 
-  for (i = 0; i < 7; i++)
+  for (i = 0; i < 8; i++)
     {
       /* bit = {0, 16, 32, 48, 64, 80, 96, 112} */
 
@@ -88,12 +88,12 @@ void netlib_prefix2ipv6netmask(uint8_t preflen, FAR struct 
in6_addr *netmask)
           else
             {
               /* Eg. preflen = 38, bit = {32}
-               *     bit - preflen = 6
+               *     preflen - bit = 6
                *     make = 0xffff << (16-6)
                *          = 0xfc00
                */
 
-              mask[i]  = 0xffff << (16 - (bit - preflen));
+              mask[i]  = htons(0xffff << (16 - (preflen - bit)));
             }
         }
       else
diff --git a/nshlib/README.md b/nshlib/README.md
index 7968414dd..b95aeefe4 100644
--- a/nshlib/README.md
+++ b/nshlib/README.md
@@ -670,7 +670,7 @@ system image.
 
   Dump data in hexadecimal format from a file or character device.
 
-- `ifconfig [nic_name [address_family] [<ip-address>|dhcp]] [dr|gw|gateway 
<dr-address>] [netmask <net-mask>] [dns <dns-address>] [hw <hw-mac>]`
+- `ifconfig [nic_name [address_family] [<ip-address>|dhcp]] [dr|gw|gateway 
<dr-address>] [netmask <net-mask>] [prefixlen <len>] [dns <dns-address>] [hw 
<hw-mac>]`
 
   Show the current configuration of the network, for example:
 
diff --git a/nshlib/nsh_netcmds.c b/nshlib/nsh_netcmds.c
index 2c2cbd858..ca7655b90 100644
--- a/nshlib/nsh_netcmds.c
+++ b/nshlib/nsh_netcmds.c
@@ -559,6 +559,7 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR 
char **argv)
 #ifdef CONFIG_NET_IPv6
   struct in6_addr addr6;
   struct in6_addr gip6 = IN6ADDR_ANY_INIT;
+  FAR char *preflen = NULL;
 #endif
   int i;
   FAR char *ifname = NULL;
@@ -665,6 +666,21 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, 
FAR char **argv)
 #endif
                 }
 
+#ifdef CONFIG_NET_IPv6
+              else if (!strcmp(tmp, "prefixlen"))
+                {
+                  if (argc - 1 >= i + 1)
+                    {
+                      preflen = argv[i + 1];
+                      i++;
+                    }
+                  else
+                    {
+                      badarg = true;
+                    }
+                }
+#endif
+
 #ifdef HAVE_HWADDR
               /* REVISIT: How will we handle Ethernet and SLIP together? */
 
@@ -753,6 +769,15 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, 
FAR char **argv)
         {
           /* REVISIT: Should DHCPC check be used here too? */
 
+          if ((tmp = strchr(hostip, '/')) != NULL)
+            {
+              *tmp = 0;
+              if (preflen == NULL)
+                {
+                  preflen = tmp + 1;
+                }
+            }
+
           ninfo("Host IP: %s\n", hostip);
           inet_pton(AF_INET6, hostip, &addr6);
         }
@@ -856,6 +881,11 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, 
FAR char **argv)
           ninfo("Netmask: %s\n", mask);
           inet_pton(AF_INET6, mask, &addr6);
         }
+      else if (preflen != NULL)
+        {
+          ninfo("Prefixlen: %s\n", preflen);
+          netlib_prefix2ipv6netmask(atoi(preflen), &addr6);
+        }
       else
         {
           ninfo("Netmask: Default\n");

Reply via email to