You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ag...@apache.org on 2020/03/31 21:18:32 UTC

[incubator-nuttx-apps] branch master updated: ping/ping6: Support the dual stack host correctly

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 595c72b  ping/ping6: Support the dual stack host correctly
595c72b is described below

commit 595c72b38e10eb7aa5578484cfa1ed931c9d5452
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Wed Apr 1 05:18:26 2020 +0800

    ping/ping6: Support the dual stack host correctly
---
 netutils/ping/icmp_ping.c   | 49 ++++++++++++++++++++++++++-------------------
 netutils/ping/icmpv6_ping.c | 48 ++++++++++++++++++++++++--------------------
 2 files changed, 54 insertions(+), 43 deletions(-)

diff --git a/netutils/ping/icmp_ping.c b/netutils/ping/icmp_ping.c
index 44edd87..fffcc09 100644
--- a/netutils/ping/icmp_ping.c
+++ b/netutils/ping/icmp_ping.c
@@ -38,6 +38,10 @@
  *
  ****************************************************************************/
 
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
 #include <nuttx/config.h>
 
 #include <sys/socket.h>
@@ -49,7 +53,7 @@
 #include <string.h>
 #include <errno.h>
 
-#if defined(CONFIG_LIBC_NETDB) && defined(CONFIG_NETDB_DNSCLIENT)
+#ifdef CONFIG_LIBC_NETDB
 #  include <netdb.h>
 #endif
 
@@ -108,30 +112,30 @@ static inline uint16_t ping_newid(void)
 
 static int ping_gethostip(FAR const char *hostname, FAR struct in_addr *dest)
 {
-#if defined(CONFIG_LIBC_NETDB) && defined(CONFIG_NETDB_DNSCLIENT)
+#ifdef CONFIG_LIBC_NETDB
   /* Netdb DNS client support is enabled */
 
-  FAR struct hostent *he;
+  FAR struct addrinfo hint;
+  FAR struct addrinfo *info;
+  FAR struct sockaddr_in *addr;
 
-  he = gethostbyname(hostname);
-  if (he == NULL)
-    {
-      return -ENOENT;
-    }
-  else if (he->h_addrtype == AF_INET)
-    {
-       memcpy(dest, he->h_addr, sizeof(in_addr_t));
-    }
-  else
+  memset(&hint, 0, sizeof(hint));
+  hint.ai_family = AF_INET;
+
+  if (getaddrinfo(hostname, NULL, &hint, &info) != OK)
     {
-      return -ENOEXEC;
+      return ERROR;
     }
 
+  addr = (FAR struct sockaddr_in *)info->ai_addr;
+  memcpy(dest, &addr->sin_addr, sizeof(struct in_addr));
+
+  freeaddrinfo(info);
   return OK;
 
 #else /* CONFIG_LIBC_NETDB */
-
   /* No host name support */
+
   /* Convert strings to numeric IPv6 address */
 
   int ret = inet_pton(AF_INET, hostname, dest);
@@ -151,7 +155,8 @@ static int ping_gethostip(FAR const char *hostname, FAR struct in_addr *dest)
  * Name: icmp_callback
  ****************************************************************************/
 
-static void icmp_callback(FAR struct ping_result_s *result, int code, int extra)
+static void icmp_callback(FAR struct ping_result_s *result,
+                          int code, int extra)
 {
   result->code = code;
   result->extra = extra;
@@ -237,7 +242,7 @@ void icmp_ping(FAR const struct ping_info_s *info)
 
       memcpy(iobuffer, &outhdr, sizeof(struct icmp_hdr_s));
 
-     /* Add some easily verifiable payload data */
+      /* Add some easily verifiable payload data */
 
       ptr = &iobuffer[sizeof(struct icmp_hdr_s)];
       ch  = 0x20;
@@ -253,7 +258,7 @@ void icmp_ping(FAR const struct ping_info_s *info)
 
       start = clock();
       nsent = sendto(sockfd, iobuffer, result.outsize, 0,
-                     (FAR struct sockaddr*)&destaddr,
+                     (FAR struct sockaddr *)&destaddr,
                      sizeof(struct sockaddr_in));
       if (nsent < 0)
         {
@@ -302,7 +307,7 @@ void icmp_ping(FAR const struct ping_info_s *info)
           else if (nrecvd < sizeof(struct icmp_hdr_s))
             {
               icmp_callback(&result, ICMP_E_RECVSMALL, nrecvd);
-             goto done;
+              goto done;
             }
 
           elapsed = (unsigned int)TICK2MSEC(clock() - start);
@@ -317,7 +322,8 @@ void icmp_ping(FAR const struct ping_info_s *info)
                 }
               else if (ntohs(inhdr->seqno) > result.seqno)
                 {
-                  icmp_callback(&result, ICMP_W_SEQNOBIG, ntohs(inhdr->seqno));
+                  icmp_callback(&result, ICMP_W_SEQNOBIG,
+                                ntohs(inhdr->seqno));
                   retry = true;
                 }
               else
@@ -327,7 +333,8 @@ void icmp_ping(FAR const struct ping_info_s *info)
 
                   if (ntohs(inhdr->seqno) < result.seqno)
                     {
-                      icmp_callback(&result, ICMP_W_SEQNOSMALL, ntohs(inhdr->seqno));
+                      icmp_callback(&result, ICMP_W_SEQNOSMALL,
+                                    ntohs(inhdr->seqno));
                       pktdelay += info->delay;
                       retry     = true;
                     }
diff --git a/netutils/ping/icmpv6_ping.c b/netutils/ping/icmpv6_ping.c
index e0d084a..522d093 100644
--- a/netutils/ping/icmpv6_ping.c
+++ b/netutils/ping/icmpv6_ping.c
@@ -53,7 +53,7 @@
 #include <string.h>
 #include <errno.h>
 
-#if defined(CONFIG_LIBC_NETDB) && defined(CONFIG_NETDB_DNSCLIENT)
+#ifdef CONFIG_LIBC_NETDB
 #  include <netdb.h>
 #endif
 
@@ -109,32 +109,33 @@ static inline uint16_t ping6_newid(void)
  *
  ****************************************************************************/
 
-static int ping6_gethostip(FAR const char *hostname, FAR struct in6_addr *dest)
+static int ping6_gethostip(FAR const char *hostname,
+                           FAR struct in6_addr *dest)
 {
-#if defined(CONFIG_LIBC_NETDB) && defined(CONFIG_NETDB_DNSCLIENT)
+#ifdef CONFIG_LIBC_NETDB
   /* Netdb DNS client support is enabled */
 
-  FAR struct hostent *he;
+  FAR struct addrinfo hint;
+  FAR struct addrinfo *info;
+  FAR struct sockaddr_in6 *addr;
 
-  he = gethostbyname(hostname);
-  if (he == NULL)
-    {
-      return -ENOENT;
-    }
-  else if (he->h_addrtype == AF_INET6)
-    {
-      memcpy(dest, he->h_addr, sizeof(struct in6_addr));
-    }
-  else
+  memset(&hint, 0, sizeof(hint));
+  hint.ai_family = AF_INET6;
+
+  if (getaddrinfo(hostname, NULL, &hint, &info) != OK)
     {
-      return -ENOEXEC;
+      return ERROR;
     }
 
+  addr = (FAR struct sockaddr_in6 *)info->ai_addr;
+  memcpy(dest, &addr->sin6_addr, sizeof(struct in6_addr));
+
+  freeaddrinfo(info);
   return OK;
 
 #else /* CONFIG_LIBC_NETDB */
-
   /* No host name support */
+
   /* Convert strings to numeric IPv6 address */
 
   int ret = inet_pton(AF_INET6, hostname, dest->s6_addr16);
@@ -153,7 +154,8 @@ static int ping6_gethostip(FAR const char *hostname, FAR struct in6_addr *dest)
  * Name: icmp6_callback
  ****************************************************************************/
 
-static void icmp6_callback(FAR struct ping6_result_s *result, int code, int extra)
+static void icmp6_callback(FAR struct ping6_result_s *result,
+                           int code, int extra)
 {
   result->code = code;
   result->extra = extra;
@@ -239,7 +241,7 @@ void icmp6_ping(FAR const struct ping6_info_s *info)
 
       memcpy(iobuffer, &outhdr, SIZEOF_ICMPV6_ECHO_REQUEST_S(0));
 
-     /* Add some easily verifiable payload data */
+      /* Add some easily verifiable payload data */
 
       ptr = &iobuffer[SIZEOF_ICMPV6_ECHO_REQUEST_S(0)];
       ch  = 0x20;
@@ -255,7 +257,7 @@ void icmp6_ping(FAR const struct ping6_info_s *info)
 
       start = clock();
       nsent = sendto(sockfd, iobuffer, result.outsize, 0,
-                     (FAR struct sockaddr*)&destaddr,
+                     (FAR struct sockaddr *)&destaddr,
                      sizeof(struct sockaddr_in6));
       if (nsent < 0)
         {
@@ -304,7 +306,7 @@ void icmp6_ping(FAR const struct ping6_info_s *info)
           else if (nrecvd < SIZEOF_ICMPV6_ECHO_REPLY_S(0))
             {
               icmp6_callback(&result, ICMPv6_E_RECVSMALL, nrecvd);
-             goto done;
+              goto done;
             }
 
           elapsed = (unsigned int)TICK2MSEC(clock() - start);
@@ -319,7 +321,8 @@ void icmp6_ping(FAR const struct ping6_info_s *info)
                 }
               else if (ntohs(inhdr->seqno) > result.seqno)
                 {
-                  icmp6_callback(&result, ICMPv6_W_SEQNOBIG, ntohs(inhdr->seqno));
+                  icmp6_callback(&result, ICMPv6_W_SEQNOBIG,
+                                 ntohs(inhdr->seqno));
                   retry = true;
                 }
               else
@@ -329,7 +332,8 @@ void icmp6_ping(FAR const struct ping6_info_s *info)
 
                   if (ntohs(inhdr->seqno) < result.seqno)
                     {
-                      icmp6_callback(&result, ICMPv6_W_SEQNOSMALL, ntohs(inhdr->seqno));
+                      icmp6_callback(&result, ICMPv6_W_SEQNOSMALL,
+                                     ntohs(inhdr->seqno));
                       pktdelay += info->delay;
                       retry     = true;
                     }