+}
+
+/* The following two functions were adapted from glibc's
+ implementation of inet_pton, written by Paul Vixie. */
+
+static bool
+is_valid_ipv4_address (const char *str, const char *end)
+{
+ bool saw_digit = false;
+ int octets = 0;
+ int val = 0;
+
+ while (str < end)
+ {
+ int ch = *str++;
+
+ if (ch >= '0' && ch <= '9')
+ {
+ val = val * 10 + (ch - '0');
+
+ if (val > 255)
+ return false;
+ if (!saw_digit)
+ {
+ if (++octets > 4)
+ return false;
+ saw_digit = true;
+ }
+ }
+ else if (ch == '.' && saw_digit)
+ {
+ if (octets == 4)
+ return false;
+ val = 0;
+ saw_digit = false;
+ }
+ else
+ return false;
+ }
+ if (octets < 4)
+ return false;
+
+ return true;
+}
+
+bool
+is_valid_ipv6_address (const char *str, const char *end)
+{
+ /* Use lower-case for these to avoid clash with system headers. */
+ enum {
+ ns_inaddrsz = 4,
+ ns_in6addrsz = 16,
+ ns_int16sz = 2
+ };
+
+ const char *curtok;
+ int tp;
+ const char *colonp;
+ bool saw_xdigit;
+ unsigned int val;
+
+ tp = 0;
+ colonp = NULL;
+
+ if (str == end)
+ return false;
+
+ /* Leading :: requires some special handling. */
+ if (*str == ':')
+ {
+ ++str;
+ if (str == end || *str != ':')
+ return false;
+ }
+
+ curtok = str;
+ saw_xdigit = false;
+ val = 0;
+
+ while (str < end)
+ {
+ int ch = *str++;
+
+ /* if ch is a number, add it to val. */
+ if (c_isxdigit (ch))
+ {
+ val <<= 4;
+ val |= XDIGIT_TO_NUM (ch);
+ if (val > 0xffff)
+ return false;
+ saw_xdigit = true;
+ continue;
+ }
+
+ /* if ch is a colon ... */
+ if (ch == ':')
+ {
+ curtok = str;
+ if (!saw_xdigit)
+ {
+ if (colonp != NULL)
+ return false;
+ colonp = str + tp;
+ continue;
+ }
+ else if (str == end)
+ return false;
+ if (tp > ns_in6addrsz - ns_int16sz)
+ return false;
+ tp += ns_int16sz;
+ saw_xdigit = false;
+ val = 0;
+ continue;
+ }
+
+ /* if ch is a dot ... */
+ if (ch == '.' && (tp <= ns_in6addrsz - ns_inaddrsz)
+ && is_valid_ipv4_address (curtok, end) == 1)
+ {
+ tp += ns_inaddrsz;
+ saw_xdigit = false;
+ break;
+ }
+
+ return false;
+ }
+
+ if (saw_xdigit)
+ {
+ if (tp > ns_in6addrsz - ns_int16sz)
+ return false;
+ tp += ns_int16sz;
+ }
+
+ if (colonp != NULL)
+ {
+ if (tp == ns_in6addrsz)
+ return false;
+ tp = ns_in6addrsz;
+ }
+
+ if (tp != ns_in6addrsz)
+ return false;
+
+ return true;
+}
+\f
+/* Simple host cache, used by lookup_host to speed up resolving. The
+ cache doesn't handle TTL because Wget is a fairly short-lived
+ application. Refreshing is attempted when connect fails, though --
+ see connect_to_host. */
+
+/* Mapping between known hosts and to lists of their addresses. */
+static struct hash_table *host_name_addresses_map;
+
+
+/* Return the host's resolved addresses from the cache, if
+ available. */
+
+static struct address_list *
+cache_query (const char *host)
+{
+ struct address_list *al;
+ if (!host_name_addresses_map)
+ return NULL;
+ al = hash_table_get (host_name_addresses_map, host);
+ if (al)
+ {
+ DEBUGP (("Found %s in host_name_addresses_map (%p)\n", host, al));
+ ++al->refcount;
+ return al;