]> sjero.net Git - wget/blobdiff - src/host.c
[svn] Use new macros xnew, xnew0, xnew_array, and xnew0_array in various places.
[wget] / src / host.c
index 937d72c800e3e35c0826124f1846294ceea0abfa..ab490421ae09866c2cf451db83629001719ed60d 100644 (file)
@@ -236,11 +236,9 @@ address_list_from_addrinfo (const struct addrinfo *ai)
   if (cnt == 0)
     return NULL;
 
-  al = xmalloc (sizeof (struct address_list));
-  al->addresses  = xmalloc (cnt * sizeof (ip_address));
+  al = xnew0 (struct address_list);
+  al->addresses  = xnew_array (ip_address, cnt);
   al->count      = cnt;
-  al->faulty     = 0;
-  al->from_cache = 0;
   al->refcount   = 1;
 
   ip = al->addresses;
@@ -268,23 +266,22 @@ address_list_from_addrinfo (const struct addrinfo *ai)
   return al;
 }
 #else
-/* Create an address_list out of a NULL-terminated vector of
-   addresses, as returned by gethostbyname.  */
+/* Create an address_list from a NULL-terminated vector of IPv4
+   addresses.  This kind of vector is returned by gethostbyname.  */
+
 static struct address_list *
-address_list_from_vector (char **h_addr_list)
+address_list_from_ipv4_addresses (char **h_addr_list)
 {
   int count, i;
-  struct address_list *al = xmalloc (sizeof (struct address_list));
+  struct address_list *al = xnew0 (struct address_list);
 
   count = 0;
   while (h_addr_list[count])
     ++count;
   assert (count > 0);
 
+  al->addresses  = xnew_array (ip_address, count);
   al->count      = count;
-  al->faulty     = 0;
-  al->addresses  = xmalloc (count * sizeof (ip_address));
-  al->from_cache = 0;
   al->refcount   = 1;
 
   for (i = 0; i < count; i++)
@@ -296,23 +293,6 @@ address_list_from_vector (char **h_addr_list)
 
   return al;
 }
-
-/* Like address_list_from_vector, but initialized with a single
-   address. */
-
-static struct address_list *
-address_list_from_single (const ip_address *addr)
-{
-  struct address_list *al = xmalloc (sizeof (struct address_list));
-  al->count      = 1;
-  al->faulty     = 0;
-  al->addresses  = xmalloc (sizeof (ip_address));
-  al->from_cache = 0;
-  al->refcount   = 1;
-  memcpy (al->addresses, addr, sizeof (ip_address));
-
-  return al;
-}
 #endif
 
 static void
@@ -512,7 +492,7 @@ lookup_host (const char *host, int flags)
      flag.  Without IPv6, we use inet_addr succeeds.  */
 
 #ifdef ENABLE_IPV6
-  memset (&hints, 0, sizeof (hints));
+  xzero (hints);
   hints.ai_family   = family;
   hints.ai_socktype = SOCK_STREAM;
   hints.ai_flags    = AI_NUMERICHOST;
@@ -536,10 +516,10 @@ lookup_host (const char *host, int flags)
       {
        /* The return value of inet_addr is in network byte order, so
           we can just copy it to IP.  */
-       ip_address ip;
-       ip.type = IPV4_ADDRESS;
-       memcpy (ADDRESS_IPV4_DATA (&ip), &addr_ipv4, 4);
-       return address_list_from_single (&ip);
+       char **vec[2];
+       vec[0] = (char *)&addr_ipv4;
+       vec[1] = NULL;
+       return address_list_from_ipv4_addresses (vec);
       }
   }
 #endif
@@ -565,7 +545,7 @@ lookup_host (const char *host, int flags)
 
 #ifdef ENABLE_IPV6
   {
-    memset (&hints, 0, sizeof (hints));
+    xzero (hints);
     hints.ai_family   = family;
     hints.ai_socktype = SOCK_STREAM;
     if (flags & LH_PASSIVE) 
@@ -598,9 +578,8 @@ lookup_host (const char *host, int flags)
        return NULL;
       }
     assert (hptr->h_length == 4);
-    /* Do all systems have h_addr_list, or is it a newer thing?  If
-       the latter, use address_list_from_single.  */
-    al = address_list_from_vector (hptr->h_addr_list);
+    /* Do older systems have h_addr_list?  */
+    al = address_list_from_ipv4_addresses (hptr->h_addr_list);
   }
 #endif