Blame SOURCES/glibc-rh677316-inet_pton.patch

fa3bfd
Backport of
fa3bfd
fa3bfd
inet/if_index.c
fa3bfd
inet/inet6_scopeid_pton.c
fa3bfd
inet/inet_mkadr.c
fa3bfd
inet/tst-inet6_scopeid_pton.c
fa3bfd
resolv/inet_pton.c
fa3bfd
resolv/tst-inet_pton.c
fa3bfd
fa3bfd
up to and including this upstream commit on the 2.26 branch:
fa3bfd
fa3bfd
commit 77db8772bd3f6f2bbad697dcf46861ce310f5b95
fa3bfd
Author: Florian Weimer <fweimer@redhat.com>
fa3bfd
Date:   Thu Aug 10 16:06:52 2017 +0200
fa3bfd
fa3bfd
    __inet6_scopeid_pton: Remove attribute_hidden, internal_function
fa3bfd
fa3bfd
This change makes the inet_pton parser more strict for IPv6 addresses
fa3bfd
(upstream bug 16637).  The stricter __inet6_scopeid_pton parser is not
fa3bfd
effective yet with this patch because it is not used by the library yet.
fa3bfd
fa3bfd
diff --git a/include/arpa/inet.h b/include/arpa/inet.h
fa3bfd
index 63ece7029aa1d417..c3f28f2baaa2ed66 100644
fa3bfd
--- a/include/arpa/inet.h
fa3bfd
+++ b/include/arpa/inet.h
fa3bfd
@@ -7,6 +7,9 @@ libc_hidden_proto (__inet_aton)
fa3bfd
 libc_hidden_proto (inet_aton)
fa3bfd
 libc_hidden_proto (inet_ntop)
fa3bfd
 libc_hidden_proto (inet_pton)
fa3bfd
-libc_hidden_proto (inet_makeaddr)
fa3bfd
+extern __typeof (inet_pton) __inet_pton;
fa3bfd
+libc_hidden_proto (__inet_pton)
fa3bfd
+extern __typeof (inet_makeaddr) __inet_makeaddr;
fa3bfd
+libc_hidden_proto (__inet_makeaddr)
fa3bfd
 libc_hidden_proto (inet_netof)
fa3bfd
 #endif
fa3bfd
diff --git a/include/net/if.h b/include/net/if.h
fa3bfd
index 1d862260c7e83977..6c4cbc96c3733a01 100644
fa3bfd
--- a/include/net/if.h
fa3bfd
+++ b/include/net/if.h
fa3bfd
@@ -4,9 +4,13 @@
fa3bfd
 
fa3bfd
 #ifndef _ISOMAC
fa3bfd
 libc_hidden_proto (if_nametoindex)
fa3bfd
+extern __typeof (if_nametoindex) __if_nametoindex;
fa3bfd
+libc_hidden_proto (__if_nametoindex)
fa3bfd
 libc_hidden_proto (if_indextoname)
fa3bfd
 libc_hidden_proto (if_nameindex)
fa3bfd
 libc_hidden_proto (if_freenameindex)
fa3bfd
+extern __typeof (if_freenameindex) __if_freenameindex;
fa3bfd
+libc_hidden_proto (__if_freenameindex)
fa3bfd
 #endif
fa3bfd
 
fa3bfd
 #endif
fa3bfd
diff --git a/inet/Makefile b/inet/Makefile
fa3bfd
index 768df21b92534a31..4abf2ddf6dfff327 100644
fa3bfd
--- a/inet/Makefile
fa3bfd
+++ b/inet/Makefile
fa3bfd
@@ -45,7 +45,7 @@ routines := htonl htons		\
fa3bfd
 	    in6_addr getnameinfo if_index ifaddrs inet6_option \
fa3bfd
 	    getipv4sourcefilter setipv4sourcefilter \
fa3bfd
 	    getsourcefilter setsourcefilter inet6_opt inet6_rth \
fa3bfd
-	    deadline
fa3bfd
+	    inet6_scopeid_pton deadline
fa3bfd
 
fa3bfd
 aux := check_pf check_native ifreq
fa3bfd
 
fa3bfd
@@ -57,6 +57,10 @@ tests := htontest test_ifindex tst-ntoa tst-ether_aton tst-network \
fa3bfd
 # internal functions.
fa3bfd
 tests-static += tst-deadline
fa3bfd
 
fa3bfd
+# tst-inet6_scopeid_pton also needs internal functions but does not
fa3bfd
+# need to be linked statically.
fa3bfd
+tests += tst-inet6_scopeid_pton
fa3bfd
+
fa3bfd
 include ../Rules
fa3bfd
 
fa3bfd
 ifeq ($(have-thread-library),yes)
fa3bfd
diff --git a/inet/Versions b/inet/Versions
fa3bfd
index 06507199a92b3c51..157559c44442259e 100644
fa3bfd
--- a/inet/Versions
fa3bfd
+++ b/inet/Versions
fa3bfd
@@ -88,5 +88,8 @@ libc {
fa3bfd
     # functions used in other libraries
fa3bfd
     __internal_endnetgrent; __internal_getnetgrent_r;
fa3bfd
     __internal_setnetgrent;
fa3bfd
+
fa3bfd
+   # Used from nscd.
fa3bfd
+    __inet6_scopeid_pton;
fa3bfd
   }
fa3bfd
 }
fa3bfd
diff --git a/inet/if_index.c b/inet/if_index.c
fa3bfd
index 3d706766be62b91b..4604e0a35b890471 100644
fa3bfd
--- a/inet/if_index.c
fa3bfd
+++ b/inet/if_index.c
fa3bfd
@@ -1,4 +1,4 @@
fa3bfd
-/* Copyright (C) 1997,98,99,2000,02 Free Software Foundation, Inc.
fa3bfd
+/* Copyright (C) 1997-2017 Free Software Foundation, Inc.
fa3bfd
    This file is part of the GNU C Library.
fa3bfd
 
fa3bfd
    The GNU C Library is free software; you can redistribute it and/or
fa3bfd
@@ -20,44 +20,41 @@
fa3bfd
 #include <stddef.h>
fa3bfd
 
fa3bfd
 unsigned int
fa3bfd
-if_nametoindex (const char *ifname)
fa3bfd
+__if_nametoindex (const char *ifname)
fa3bfd
 {
fa3bfd
   __set_errno (ENOSYS);
fa3bfd
   return 0;
fa3bfd
 }
fa3bfd
-libc_hidden_def (if_nametoindex)
fa3bfd
+libc_hidden_def (__if_nametoindex)
fa3bfd
+weak_alias (__if_nametoindex, if_nametoindex)
fa3bfd
+libc_hidden_weak (if_nametoindex)
fa3bfd
 stub_warning (if_nametoindex)
fa3bfd
 
fa3bfd
 char *
fa3bfd
-if_indextoname (unsigned int ifindex, char *ifname)
fa3bfd
+__if_indextoname (unsigned int ifindex, char *ifname)
fa3bfd
 {
fa3bfd
   __set_errno (ENOSYS);
fa3bfd
   return NULL;
fa3bfd
 }
fa3bfd
-libc_hidden_def (if_indextoname)
fa3bfd
+weak_alias (__if_indextoname, if_indextoname)
fa3bfd
+libc_hidden_weak (if_indextoname)
fa3bfd
 stub_warning (if_indextoname)
fa3bfd
 
fa3bfd
 void
fa3bfd
-if_freenameindex (struct if_nameindex *ifn)
fa3bfd
+__if_freenameindex (struct if_nameindex *ifn)
fa3bfd
 {
fa3bfd
 }
fa3bfd
+libc_hidden_def (__if_freenameindex)
fa3bfd
+weak_alias (__if_freenameindex, if_freenameindex)
fa3bfd
+libc_hidden_weak (if_freenameindex)
fa3bfd
 stub_warning (if_freenameindex)
fa3bfd
 
fa3bfd
 struct if_nameindex *
fa3bfd
-if_nameindex (void)
fa3bfd
+__if_nameindex (void)
fa3bfd
 {
fa3bfd
   __set_errno (ENOSYS);
fa3bfd
   return NULL;
fa3bfd
 }
fa3bfd
+weak_alias (__if_nameindex, if_nameindex)
fa3bfd
+libc_hidden_weak (if_nameindex)
fa3bfd
 stub_warning (if_nameindex)
fa3bfd
-
fa3bfd
-#if 0
fa3bfd
-void
fa3bfd
-internal_function
fa3bfd
-__protocol_available (int *have_inet, int *have_inet6)
fa3bfd
-{
fa3bfd
-  /* By default we assume that IPv4 is available, IPv6 not.  */
fa3bfd
-  *have_inet = 1;
fa3bfd
-  *have_inet6 = 0;
fa3bfd
-}
fa3bfd
-#endif
fa3bfd
diff --git a/inet/inet6_scopeid_pton.c b/inet/inet6_scopeid_pton.c
fa3bfd
new file mode 100644
fa3bfd
index 0000000000000000..cc8803fa108f7bcb
fa3bfd
--- /dev/null
fa3bfd
+++ b/inet/inet6_scopeid_pton.c
fa3bfd
@@ -0,0 +1,64 @@
fa3bfd
+/* Convert an IPv6 scope ID from text to the internal representation.
fa3bfd
+   Copyright (C) 2016-2017 Free Software Foundation, Inc.
fa3bfd
+   This file is part of the GNU C Library.
fa3bfd
+
fa3bfd
+   The GNU C Library is free software; you can redistribute it and/or
fa3bfd
+   modify it under the terms of the GNU Lesser General Public
fa3bfd
+   License as published by the Free Software Foundation; either
fa3bfd
+   version 2.1 of the License, or (at your option) any later version.
fa3bfd
+
fa3bfd
+   The GNU C Library is distributed in the hope that it will be useful,
fa3bfd
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
fa3bfd
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
fa3bfd
+   Lesser General Public License for more details.
fa3bfd
+
fa3bfd
+   You should have received a copy of the GNU Lesser General Public
fa3bfd
+   License along with the GNU C Library; if not, see
fa3bfd
+   <http://www.gnu.org/licenses/>.  */
fa3bfd
+
fa3bfd
+#include <net-internal.h>
fa3bfd
+
fa3bfd
+#include <ctype.h>
fa3bfd
+#include <errno.h>
fa3bfd
+#include <locale.h>
fa3bfd
+#include <net/if.h>
fa3bfd
+#include <stdbool.h>
fa3bfd
+#include <stdint.h>
fa3bfd
+#include <stdlib.h>
fa3bfd
+
fa3bfd
+/* Parse SOURCE as a scope ID for ADDRESS.  Return 0 on success and -1
fa3bfd
+   on error.  */
fa3bfd
+int
fa3bfd
+__inet6_scopeid_pton (const struct in6_addr *address, const char *scope,
fa3bfd
+                      uint32_t *result)
fa3bfd
+{
fa3bfd
+  if (IN6_IS_ADDR_LINKLOCAL (address)
fa3bfd
+      || IN6_IS_ADDR_MC_NODELOCAL (address)
fa3bfd
+      || IN6_IS_ADDR_MC_LINKLOCAL (address))
fa3bfd
+    {
fa3bfd
+      uint32_t number = __if_nametoindex (scope);
fa3bfd
+      if (number != 0)
fa3bfd
+        {
fa3bfd
+          *result = number;
fa3bfd
+          return 0;
fa3bfd
+        }
fa3bfd
+    }
fa3bfd
+
fa3bfd
+  if (isdigit_l (scope[0], _nl_C_locobj_ptr))
fa3bfd
+    {
fa3bfd
+      char *end;
fa3bfd
+      unsigned long long number
fa3bfd
+        = ____strtoull_l_internal (scope, &end, /*base */ 10, /* group */ 0,
fa3bfd
+                                   _nl_C_locobj_ptr);
fa3bfd
+      if (*end == '\0' && number <= UINT32_MAX)
fa3bfd
+        {
fa3bfd
+          *result = number;
fa3bfd
+          return 0;
fa3bfd
+        }
fa3bfd
+    }
fa3bfd
+
fa3bfd
+  __set_errno (EINVAL);
fa3bfd
+  return -1;
fa3bfd
+}
fa3bfd
+
fa3bfd
+libc_hidden_def (__inet6_scopeid_pton)
fa3bfd
diff --git a/inet/inet_mkadr.c b/inet/inet_mkadr.c
fa3bfd
index d8d92da768b2ede0..88faef74cf50b722 100644
fa3bfd
--- a/inet/inet_mkadr.c
fa3bfd
+++ b/inet/inet_mkadr.c
fa3bfd
@@ -27,10 +27,6 @@
fa3bfd
  * SUCH DAMAGE.
fa3bfd
  */
fa3bfd
 
fa3bfd
-#if defined(LIBC_SCCS) && !defined(lint)
fa3bfd
-static char sccsid[] = "@(#)inet_makeaddr.c	8.1 (Berkeley) 6/4/93";
fa3bfd
-#endif /* LIBC_SCCS and not lint */
fa3bfd
-
fa3bfd
 #include <sys/param.h>
fa3bfd
 #include <netinet/in.h>
fa3bfd
 #include <arpa/inet.h>
fa3bfd
@@ -40,8 +36,7 @@ static char sccsid[] = "@(#)inet_makeaddr.c	8.1 (Berkeley) 6/4/93";
fa3bfd
  * building addresses stored in the ifnet structure.
fa3bfd
  */
fa3bfd
 struct in_addr
fa3bfd
-inet_makeaddr(net, host)
fa3bfd
-	in_addr_t net, host;
fa3bfd
+__inet_makeaddr (in_addr_t net, in_addr_t host)
fa3bfd
 {
fa3bfd
 	struct in_addr in;
fa3bfd
 
fa3bfd
@@ -56,4 +51,5 @@ inet_makeaddr(net, host)
fa3bfd
 	in.s_addr = htonl(in.s_addr);
fa3bfd
 	return in;
fa3bfd
 }
fa3bfd
-libc_hidden_def (inet_makeaddr)
fa3bfd
+libc_hidden_def (__inet_makeaddr)
fa3bfd
+weak_alias (__inet_makeaddr, inet_makeaddr)
fa3bfd
diff --git a/inet/net-internal.h b/inet/net-internal.h
fa3bfd
index 58349f54ea126698..6c666752493811be 100644
fa3bfd
--- a/inet/net-internal.h
fa3bfd
+++ b/inet/net-internal.h
fa3bfd
@@ -19,10 +19,15 @@
fa3bfd
 #ifndef _NET_INTERNAL_H
fa3bfd
 #define _NET_INTERNAL_H 1
fa3bfd
 
fa3bfd
+#include <arpa/inet.h>
fa3bfd
 #include <stdbool.h>
fa3bfd
 #include <stdint.h>
fa3bfd
 #include <sys/time.h>
fa3bfd
 
fa3bfd
+int __inet6_scopeid_pton (const struct in6_addr *address,
fa3bfd
+                          const char *scope, uint32_t *result);
fa3bfd
+libc_hidden_proto (__inet6_scopeid_pton)
fa3bfd
+
fa3bfd
 /* Deadline handling for enforcing timeouts.
fa3bfd
 
fa3bfd
    Code should call __deadline_current_time to obtain the current time
fa3bfd
diff --git a/inet/tst-inet6_scopeid_pton.c b/inet/tst-inet6_scopeid_pton.c
fa3bfd
new file mode 100644
fa3bfd
index 0000000000000000..8225b3b80abe6331
fa3bfd
--- /dev/null
fa3bfd
+++ b/inet/tst-inet6_scopeid_pton.c
fa3bfd
@@ -0,0 +1,230 @@
fa3bfd
+/* Tests for __inet6_scopeid_pton and IPv6 scopes in getaddrinfo.
fa3bfd
+   Copyright (C) 2016-2017 Free Software Foundation, Inc.
fa3bfd
+   This file is part of the GNU C Library.
fa3bfd
+
fa3bfd
+   The GNU C Library is free software; you can redistribute it and/or
fa3bfd
+   modify it under the terms of the GNU Lesser General Public
fa3bfd
+   License as published by the Free Software Foundation; either
fa3bfd
+   version 2.1 of the License, or (at your option) any later version.
fa3bfd
+
fa3bfd
+   The GNU C Library is distributed in the hope that it will be useful,
fa3bfd
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
fa3bfd
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
fa3bfd
+   Lesser General Public License for more details.
fa3bfd
+
fa3bfd
+   You should have received a copy of the GNU Lesser General Public
fa3bfd
+   License along with the GNU C Library; if not, see
fa3bfd
+   <http://www.gnu.org/licenses/>.  */
fa3bfd
+
fa3bfd
+#include <arpa/inet.h>
fa3bfd
+#include <inttypes.h>
fa3bfd
+#include <net-internal.h>
fa3bfd
+#include <net/if.h>
fa3bfd
+#include <netdb.h>
fa3bfd
+#include <stdio.h>
fa3bfd
+#include <stdlib.h>
fa3bfd
+#include <string.h>
fa3bfd
+#include <support/check.h>
fa3bfd
+#include <support/support.h>
fa3bfd
+#include <support/test-driver.h>
fa3bfd
+
fa3bfd
+/* An interface which is known to the system.  */
fa3bfd
+static const char *interface_name;
fa3bfd
+static uint32_t interface_index;
fa3bfd
+
fa3bfd
+/* Initiale the variables above.  */
fa3bfd
+static void
fa3bfd
+setup_interface (void)
fa3bfd
+{
fa3bfd
+  struct if_nameindex *list = if_nameindex ();
fa3bfd
+  if (list != NULL && list[0].if_index != 0 && list[0].if_name[0] != '\0')
fa3bfd
+    {
fa3bfd
+      interface_name = list[0].if_name;
fa3bfd
+      interface_index = list[0].if_index;
fa3bfd
+    }
fa3bfd
+}
fa3bfd
+
fa3bfd
+/* Convert ADDRESS to struct in6_addr.  */
fa3bfd
+static struct in6_addr
fa3bfd
+from_string (const char *address)
fa3bfd
+{
fa3bfd
+  struct in6_addr addr;
fa3bfd
+  if (inet_pton (AF_INET6, address, &addr) != 1)
fa3bfd
+    FAIL_EXIT1 ("inet_pton (\"%s\")", address);
fa3bfd
+  return addr;
fa3bfd
+}
fa3bfd
+
fa3bfd
+/* Invoke getaddrinfo to parse ADDRESS%SCOPE.  Return true if
fa3bfd
+   getaddrinfo was successful.  */
fa3bfd
+static bool
fa3bfd
+call_gai (int family, const char *address, const char *scope,
fa3bfd
+          struct sockaddr_in6 *result)
fa3bfd
+{
fa3bfd
+  struct addrinfo hints =
fa3bfd
+    {
fa3bfd
+      .ai_family = family,
fa3bfd
+      .ai_flags = AI_NUMERICHOST,
fa3bfd
+      .ai_socktype = SOCK_DGRAM,
fa3bfd
+      .ai_protocol = IPPROTO_UDP,
fa3bfd
+    };
fa3bfd
+  char *fulladdr = xasprintf ("%s%%%s", address, scope);
fa3bfd
+  struct addrinfo *ai = NULL;
fa3bfd
+  int ret = getaddrinfo (fulladdr, NULL, &hints, &ai;;
fa3bfd
+  if (ret == EAI_ADDRFAMILY || ret == EAI_NONAME)
fa3bfd
+    {
fa3bfd
+      if (test_verbose > 0)
fa3bfd
+        printf ("info: getaddrinfo (\"%s\"): %s (%d)\n",
fa3bfd
+                fulladdr, gai_strerror (ret), ret);
fa3bfd
+      free (fulladdr);
fa3bfd
+      return false;
fa3bfd
+    }
fa3bfd
+  if (ret != 0)
fa3bfd
+    FAIL_EXIT1 ("getaddrinfo (\"%s\"): %s (%d)\n",
fa3bfd
+                fulladdr, gai_strerror (ret), ret);
fa3bfd
+  TEST_VERIFY_EXIT (ai != NULL);
fa3bfd
+  TEST_VERIFY_EXIT (ai->ai_addrlen == sizeof (*result));
fa3bfd
+  TEST_VERIFY (ai->ai_family == AF_INET6);
fa3bfd
+  TEST_VERIFY (ai->ai_next == NULL);
fa3bfd
+  memcpy (result, ai->ai_addr, sizeof (*result));
fa3bfd
+  free (fulladdr);
fa3bfd
+  freeaddrinfo (ai);
fa3bfd
+  return true;
fa3bfd
+}
fa3bfd
+
fa3bfd
+/* Verify that a successful call to getaddrinfo returned the expected
fa3bfd
+   scope data.  */
fa3bfd
+static void
fa3bfd
+check_ai (const char *what, const char *addr_string, const char *scope_string,
fa3bfd
+          const struct sockaddr_in6 *sa,
fa3bfd
+          const struct in6_addr *addr, uint32_t scope)
fa3bfd
+{
fa3bfd
+  if (memcmp (addr, &sa->sin6_addr, sizeof (*addr)) != 0)
fa3bfd
+    {
fa3bfd
+      support_record_failure ();
fa3bfd
+      printf ("error: getaddrinfo %s address mismatch for %s%%%s\n",
fa3bfd
+              what, addr_string, scope_string);
fa3bfd
+    }
fa3bfd
+  if (sa->sin6_scope_id != scope)
fa3bfd
+    {
fa3bfd
+      support_record_failure ();
fa3bfd
+      printf ("error: getaddrinfo %s scope mismatch for %s%%%s\n"
fa3bfd
+              "  expected: %" PRIu32 "\n"
fa3bfd
+              "  actual:   %" PRIu32 "\n",
fa3bfd
+              what, addr_string, scope_string, scope, sa->sin6_scope_id);
fa3bfd
+    }
fa3bfd
+}
fa3bfd
+
fa3bfd
+/* Check a single address were we expected a failure.  */
fa3bfd
+static void
fa3bfd
+expect_failure (const char *address, const char *scope)
fa3bfd
+{
fa3bfd
+  if (test_verbose > 0)
fa3bfd
+    printf ("info: expecting failure for %s%%%s\n", address, scope);
fa3bfd
+  struct in6_addr addr = from_string (address);
fa3bfd
+  uint32_t result = 1234;
fa3bfd
+  if (__inet6_scopeid_pton (&addr, scope, &result) == 0)
fa3bfd
+    {
fa3bfd
+      support_record_failure ();
fa3bfd
+      printf ("error: unexpected success for %s%%%s\n",
fa3bfd
+              address, scope);
fa3bfd
+    }
fa3bfd
+  if (result != 1234)
fa3bfd
+    {
fa3bfd
+      support_record_failure ();
fa3bfd
+      printf ("error: unexpected result update for %s%%%s\n",
fa3bfd
+              address, scope);
fa3bfd
+    }
fa3bfd
+
fa3bfd
+  struct sockaddr_in6 sa;
fa3bfd
+  if (call_gai (AF_UNSPEC, address, scope, &sa))
fa3bfd
+    {
fa3bfd
+      support_record_failure ();
fa3bfd
+      printf ("error: unexpected getaddrinfo success for %s%%%s (AF_UNSPEC)\n",
fa3bfd
+              address, scope);
fa3bfd
+    }
fa3bfd
+  if (call_gai (AF_INET6, address, scope, &sa))
fa3bfd
+    {
fa3bfd
+      support_record_failure ();
fa3bfd
+      printf ("error: unexpected getaddrinfo success for %s%%%s (AF_INET6)\n",
fa3bfd
+              address, scope);
fa3bfd
+    }
fa3bfd
+}
fa3bfd
+
fa3bfd
+/* Check a single address were we expected a success.  */
fa3bfd
+static void
fa3bfd
+expect_success (const char *address, const char *scope, uint32_t expected)
fa3bfd
+{
fa3bfd
+  if (test_verbose > 0)
fa3bfd
+    printf ("info: expecting success for %s%%%s\n", address, scope);
fa3bfd
+  struct in6_addr addr = from_string (address);
fa3bfd
+  uint32_t actual = expected + 1;
fa3bfd
+  if (__inet6_scopeid_pton (&addr, scope, &actual) != 0)
fa3bfd
+    {
fa3bfd
+      support_record_failure ();
fa3bfd
+      printf ("error: unexpected failure for %s%%%s\n",
fa3bfd
+              address, scope);
fa3bfd
+    }
fa3bfd
+  if (actual != expected)
fa3bfd
+    {
fa3bfd
+      support_record_failure ();
fa3bfd
+      printf ("error: unexpected result for for %s%%%s\n",
fa3bfd
+              address, scope);
fa3bfd
+      printf ("  expected: %" PRIu32 "\n", expected);
fa3bfd
+      printf ("  actual:   %" PRIu32 "\n", actual);
fa3bfd
+    }
fa3bfd
+
fa3bfd
+  struct sockaddr_in6 sa;
fa3bfd
+  memset (&sa, 0xc0, sizeof (sa));
fa3bfd
+  if (call_gai (AF_UNSPEC, address, scope, &sa))
fa3bfd
+    check_ai ("AF_UNSPEC", address, scope, &sa, &addr, expected);
fa3bfd
+  else
fa3bfd
+    {
fa3bfd
+      support_record_failure ();
fa3bfd
+      printf ("error: unexpected getaddrinfo failure for %s%%%s (AF_UNSPEC)\n",
fa3bfd
+              address, scope);
fa3bfd
+    }
fa3bfd
+  memset (&sa, 0xc0, sizeof (sa));
fa3bfd
+  if (call_gai (AF_INET6, address, scope, &sa))
fa3bfd
+    check_ai ("AF_INET6", address, scope, &sa, &addr, expected);
fa3bfd
+  else
fa3bfd
+    {
fa3bfd
+      support_record_failure ();
fa3bfd
+      printf ("error: unexpected getaddrinfo failure for %s%%%s (AF_INET6)\n",
fa3bfd
+              address, scope);
fa3bfd
+    }
fa3bfd
+}
fa3bfd
+
fa3bfd
+static int
fa3bfd
+do_test (void)
fa3bfd
+{
fa3bfd
+  setup_interface ();
fa3bfd
+
fa3bfd
+  static const char *test_addresses[]
fa3bfd
+    = { "::", "::1", "2001:db8::1", NULL };
fa3bfd
+  for (int i = 0; test_addresses[i] != NULL; ++i)
fa3bfd
+    {
fa3bfd
+      expect_success (test_addresses[i], "0", 0);
fa3bfd
+      expect_success (test_addresses[i], "5555", 5555);
fa3bfd
+
fa3bfd
+      expect_failure (test_addresses[i], "");
fa3bfd
+      expect_failure (test_addresses[i], "-1");
fa3bfd
+      expect_failure (test_addresses[i], "-99");
fa3bfd
+      expect_failure (test_addresses[i], "037777777777");
fa3bfd
+      expect_failure (test_addresses[i], "0x");
fa3bfd
+      expect_failure (test_addresses[i], "0x1");
fa3bfd
+    }
fa3bfd
+
fa3bfd
+  if (interface_name != NULL)
fa3bfd
+    {
fa3bfd
+      expect_success ("fe80::1", interface_name, interface_index);
fa3bfd
+      expect_success ("ff02::1", interface_name, interface_index);
fa3bfd
+      expect_success ("ff01::1", interface_name, interface_index);
fa3bfd
+      expect_failure ("::", interface_name);
fa3bfd
+      expect_failure ("::1", interface_name);
fa3bfd
+      expect_failure ("2001:db8::1", interface_name);
fa3bfd
+    }
fa3bfd
+
fa3bfd
+  return 0;
fa3bfd
+}
fa3bfd
+
fa3bfd
+#include <support/test-driver.c>
fa3bfd
diff --git a/resolv/Makefile b/resolv/Makefile
fa3bfd
index a86646d4ef61b77d..3f525bcc75ee83ae 100644
fa3bfd
--- a/resolv/Makefile
fa3bfd
+++ b/resolv/Makefile
fa3bfd
@@ -29,7 +29,7 @@ headers	:= resolv.h \
fa3bfd
 routines := herror inet_addr inet_ntop inet_pton nsap_addr res_init \
fa3bfd
 	    res_hconf res_libc res-state
fa3bfd
 
fa3bfd
-tests = tst-aton tst-leaks tst-inet_ntop
fa3bfd
+tests = tst-aton tst-leaks tst-inet_ntop tst-inet_pton
fa3bfd
 xtests = tst-leaks2
fa3bfd
 
fa3bfd
 generate := mtrace-tst-leaks tst-leaks.mtrace tst-leaks2.mtrace
fa3bfd
diff --git a/resolv/Versions b/resolv/Versions
fa3bfd
index 152ef3f68f9a8b48..24b07ef770ed3915 100644
fa3bfd
--- a/resolv/Versions
fa3bfd
+++ b/resolv/Versions
fa3bfd
@@ -29,6 +29,7 @@ libc {
fa3bfd
     __h_errno; __resp;
fa3bfd
 
fa3bfd
     __res_maybe_init; __res_iclose;
fa3bfd
+    __inet_pton_length;
fa3bfd
   }
fa3bfd
 }
fa3bfd
 
fa3bfd
diff --git a/resolv/inet_pton.c b/resolv/inet_pton.c
fa3bfd
index c507013e4ea9a90b..16ee33e0c0dfb015 100644
fa3bfd
--- a/resolv/inet_pton.c
fa3bfd
+++ b/resolv/inet_pton.c
fa3bfd
@@ -1,3 +1,20 @@
fa3bfd
+/* Copyright (C) 1996-2017 Free Software Foundation, Inc.
fa3bfd
+   This file is part of the GNU C Library.
fa3bfd
+
fa3bfd
+   The GNU C Library is free software; you can redistribute it and/or
fa3bfd
+   modify it under the terms of the GNU Lesser General Public
fa3bfd
+   License as published by the Free Software Foundation; either
fa3bfd
+   version 2.1 of the License, or (at your option) any later version.
fa3bfd
+
fa3bfd
+   The GNU C Library is distributed in the hope that it will be useful,
fa3bfd
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
fa3bfd
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
fa3bfd
+   Lesser General Public License for more details.
fa3bfd
+
fa3bfd
+   You should have received a copy of the GNU Lesser General Public
fa3bfd
+   License along with the GNU C Library; if not, see
fa3bfd
+   <http://www.gnu.org/licenses/>.  */
fa3bfd
+
fa3bfd
 /*
fa3bfd
  * Copyright (c) 1996,1999 by Internet Software Consortium.
fa3bfd
  *
fa3bfd
@@ -15,207 +32,203 @@
fa3bfd
  * SOFTWARE.
fa3bfd
  */
fa3bfd
 
fa3bfd
-#if defined(LIBC_SCCS) && !defined(lint)
fa3bfd
-static const char rcsid[] = "$BINDId: inet_pton.c,v 1.7 1999/10/13 16:39:28 vixie Exp $";
fa3bfd
-#endif /* LIBC_SCCS and not lint */
fa3bfd
-
fa3bfd
-#include <sys/param.h>
fa3bfd
-#include <sys/types.h>
fa3bfd
-#include <sys/socket.h>
fa3bfd
-#include <netinet/in.h>
fa3bfd
 #include <arpa/inet.h>
fa3bfd
 #include <arpa/nameser.h>
fa3bfd
 #include <ctype.h>
fa3bfd
-#include <string.h>
fa3bfd
 #include <errno.h>
fa3bfd
+#include <netinet/in.h>
fa3bfd
+#include <resolv/resolv-internal.h>
fa3bfd
+#include <string.h>
fa3bfd
+#include <sys/socket.h>
fa3bfd
+#include <sys/types.h>
fa3bfd
 
fa3bfd
-/*
fa3bfd
- * WARNING: Don't even consider trying to compile this on a system where
fa3bfd
- * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
fa3bfd
- */
fa3bfd
+static int inet_pton4 (const char *src, const char *src_end, u_char *dst);
fa3bfd
+static int inet_pton6 (const char *src, const char *src_end, u_char *dst);
fa3bfd
 
fa3bfd
-static int inet_pton4 (const char *src, u_char *dst) internal_function;
fa3bfd
-static int inet_pton6 (const char *src, u_char *dst) internal_function;
fa3bfd
-
fa3bfd
-/* int
fa3bfd
- * inet_pton(af, src, dst)
fa3bfd
- *	convert from presentation format (which usually means ASCII printable)
fa3bfd
- *	to network format (which is usually some kind of binary format).
fa3bfd
- * return:
fa3bfd
- *	1 if the address was valid for the specified address family
fa3bfd
- *	0 if the address wasn't valid (`dst' is untouched in this case)
fa3bfd
- *	-1 if some other error occurred (`dst' is untouched in this case, too)
fa3bfd
- * author:
fa3bfd
- *	Paul Vixie, 1996.
fa3bfd
- */
fa3bfd
 int
fa3bfd
-inet_pton(af, src, dst)
fa3bfd
-	int af;
fa3bfd
-	const char *src;
fa3bfd
-	void *dst;
fa3bfd
+__inet_pton_length (int af, const char *src, size_t srclen, void *dst)
fa3bfd
 {
fa3bfd
-	switch (af) {
fa3bfd
-	case AF_INET:
fa3bfd
-		return (inet_pton4(src, dst));
fa3bfd
-	case AF_INET6:
fa3bfd
-		return (inet_pton6(src, dst));
fa3bfd
-	default:
fa3bfd
-		__set_errno (EAFNOSUPPORT);
fa3bfd
-		return (-1);
fa3bfd
-	}
fa3bfd
-	/* NOTREACHED */
fa3bfd
+  switch (af)
fa3bfd
+    {
fa3bfd
+    case AF_INET:
fa3bfd
+      return inet_pton4 (src, src + srclen, dst);
fa3bfd
+    case AF_INET6:
fa3bfd
+      return inet_pton6 (src, src + srclen, dst);
fa3bfd
+    default:
fa3bfd
+      __set_errno (EAFNOSUPPORT);
fa3bfd
+      return -1;
fa3bfd
+    }
fa3bfd
 }
fa3bfd
-libc_hidden_def (inet_pton)
fa3bfd
-
fa3bfd
-/* int
fa3bfd
- * inet_pton4(src, dst)
fa3bfd
- *	like inet_aton() but without all the hexadecimal, octal (with the
fa3bfd
- *	exception of 0) and shorthand.
fa3bfd
- * return:
fa3bfd
- *	1 if `src' is a valid dotted quad, else 0.
fa3bfd
- * notice:
fa3bfd
- *	does not touch `dst' unless it's returning 1.
fa3bfd
- * author:
fa3bfd
- *	Paul Vixie, 1996.
fa3bfd
- */
fa3bfd
+libc_hidden_def (__inet_pton_length)
fa3bfd
+
fa3bfd
+/* Like __inet_pton_length, but use strlen (SRC) as the length of
fa3bfd
+   SRC.  */
fa3bfd
+int
fa3bfd
+__inet_pton (int af, const char *src, void *dst)
fa3bfd
+{
fa3bfd
+  return __inet_pton_length (af, src, strlen (src), dst);
fa3bfd
+}
fa3bfd
+libc_hidden_def (__inet_pton)
fa3bfd
+weak_alias (__inet_pton, inet_pton)
fa3bfd
+libc_hidden_weak (inet_pton)
fa3bfd
+
fa3bfd
+/* Like inet_aton but without all the hexadecimal, octal and shorthand
fa3bfd
+   (and trailing garbage is not ignored).  Return 1 if SRC is a valid
fa3bfd
+   dotted quad, else 0.  This function does not touch DST unless it's
fa3bfd
+   returning 1.
fa3bfd
+   Author: Paul Vixie, 1996.  */
fa3bfd
 static int
fa3bfd
-internal_function
fa3bfd
-inet_pton4(src, dst)
fa3bfd
-	const char *src;
fa3bfd
-	u_char *dst;
fa3bfd
+inet_pton4 (const char *src, const char *end, unsigned char *dst)
fa3bfd
 {
fa3bfd
-	int saw_digit, octets, ch;
fa3bfd
-	u_char tmp[NS_INADDRSZ], *tp;
fa3bfd
-
fa3bfd
-	saw_digit = 0;
fa3bfd
-	octets = 0;
fa3bfd
-	*(tp = tmp) = 0;
fa3bfd
-	while ((ch = *src++) != '\0') {
fa3bfd
-
fa3bfd
-		if (ch >= '0' && ch <= '9') {
fa3bfd
-			u_int new = *tp * 10 + (ch - '0');
fa3bfd
-
fa3bfd
-			if (saw_digit && *tp == 0)
fa3bfd
-				return (0);
fa3bfd
-			if (new > 255)
fa3bfd
-				return (0);
fa3bfd
-			*tp = new;
fa3bfd
-			if (! saw_digit) {
fa3bfd
-				if (++octets > 4)
fa3bfd
-					return (0);
fa3bfd
-				saw_digit = 1;
fa3bfd
-			}
fa3bfd
-		} else if (ch == '.' && saw_digit) {
fa3bfd
-			if (octets == 4)
fa3bfd
-				return (0);
fa3bfd
-			*++tp = 0;
fa3bfd
-			saw_digit = 0;
fa3bfd
-		} else
fa3bfd
-			return (0);
fa3bfd
-	}
fa3bfd
-	if (octets < 4)
fa3bfd
-		return (0);
fa3bfd
-	memcpy(dst, tmp, NS_INADDRSZ);
fa3bfd
-	return (1);
fa3bfd
+  int saw_digit, octets, ch;
fa3bfd
+  unsigned char tmp[NS_INADDRSZ], *tp;
fa3bfd
+
fa3bfd
+  saw_digit = 0;
fa3bfd
+  octets = 0;
fa3bfd
+  *(tp = tmp) = 0;
fa3bfd
+  while (src < end)
fa3bfd
+    {
fa3bfd
+      ch = *src++;
fa3bfd
+      if (ch >= '0' && ch <= '9')
fa3bfd
+        {
fa3bfd
+          unsigned int new = *tp * 10 + (ch - '0');
fa3bfd
+
fa3bfd
+          if (saw_digit && *tp == 0)
fa3bfd
+            return 0;
fa3bfd
+          if (new > 255)
fa3bfd
+            return 0;
fa3bfd
+          *tp = new;
fa3bfd
+          if (! saw_digit)
fa3bfd
+            {
fa3bfd
+              if (++octets > 4)
fa3bfd
+                return 0;
fa3bfd
+              saw_digit = 1;
fa3bfd
+            }
fa3bfd
+        }
fa3bfd
+      else if (ch == '.' && saw_digit)
fa3bfd
+        {
fa3bfd
+          if (octets == 4)
fa3bfd
+            return 0;
fa3bfd
+          *++tp = 0;
fa3bfd
+          saw_digit = 0;
fa3bfd
+        }
fa3bfd
+      else
fa3bfd
+        return 0;
fa3bfd
+    }
fa3bfd
+  if (octets < 4)
fa3bfd
+    return 0;
fa3bfd
+  memcpy (dst, tmp, NS_INADDRSZ);
fa3bfd
+  return 1;
fa3bfd
 }
fa3bfd
 
fa3bfd
-/* int
fa3bfd
- * inet_pton6(src, dst)
fa3bfd
- *	convert presentation level address to network order binary form.
fa3bfd
- * return:
fa3bfd
- *	1 if `src' is a valid [RFC1884 2.2] address, else 0.
fa3bfd
- * notice:
fa3bfd
- *	(1) does not touch `dst' unless it's returning 1.
fa3bfd
- *	(2) :: in a full address is silently ignored.
fa3bfd
- * credit:
fa3bfd
- *	inspired by Mark Andrews.
fa3bfd
- * author:
fa3bfd
- *	Paul Vixie, 1996.
fa3bfd
- */
fa3bfd
+/* Return the value of CH as a hexademical digit, or -1 if it is a
fa3bfd
+   different type of character.  */
fa3bfd
+static int
fa3bfd
+hex_digit_value (char ch)
fa3bfd
+{
fa3bfd
+  if ('0' <= ch && ch <= '9')
fa3bfd
+    return ch - '0';
fa3bfd
+  if ('a' <= ch && ch <= 'f')
fa3bfd
+    return ch - 'a' + 10;
fa3bfd
+  if ('A' <= ch && ch <= 'F')
fa3bfd
+    return ch - 'A' + 10;
fa3bfd
+  return -1;
fa3bfd
+}
fa3bfd
+
fa3bfd
+/* Convert presentation-level IPv6 address to network order binary
fa3bfd
+   form.  Return 1 if SRC is a valid [RFC1884 2.2] address, else 0.
fa3bfd
+   This function does not touch DST unless it's returning 1.
fa3bfd
+   Author: Paul Vixie, 1996.  Inspired by Mark Andrews.  */
fa3bfd
 static int
fa3bfd
-internal_function
fa3bfd
-inet_pton6(src, dst)
fa3bfd
-	const char *src;
fa3bfd
-	u_char *dst;
fa3bfd
+inet_pton6 (const char *src, const char *src_endp, unsigned char *dst)
fa3bfd
 {
fa3bfd
-	static const char xdigits[] = "0123456789abcdef";
fa3bfd
-	u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
fa3bfd
-	const char *curtok;
fa3bfd
-	int ch, saw_xdigit;
fa3bfd
-	u_int val;
fa3bfd
-
fa3bfd
-	tp = memset(tmp, '\0', NS_IN6ADDRSZ);
fa3bfd
-	endp = tp + NS_IN6ADDRSZ;
fa3bfd
-	colonp = NULL;
fa3bfd
-	/* Leading :: requires some special handling. */
fa3bfd
-	if (*src == ':')
fa3bfd
-		if (*++src != ':')
fa3bfd
-			return (0);
fa3bfd
-	curtok = src;
fa3bfd
-	saw_xdigit = 0;
fa3bfd
-	val = 0;
fa3bfd
-	while ((ch = tolower (*src++)) != '\0') {
fa3bfd
-		const char *pch;
fa3bfd
-
fa3bfd
-		pch = strchr(xdigits, ch);
fa3bfd
-		if (pch != NULL) {
fa3bfd
-			val <<= 4;
fa3bfd
-			val |= (pch - xdigits);
fa3bfd
-			if (val > 0xffff)
fa3bfd
-				return (0);
fa3bfd
-			saw_xdigit = 1;
fa3bfd
-			continue;
fa3bfd
-		}
fa3bfd
-		if (ch == ':') {
fa3bfd
-			curtok = src;
fa3bfd
-			if (!saw_xdigit) {
fa3bfd
-				if (colonp)
fa3bfd
-					return (0);
fa3bfd
-				colonp = tp;
fa3bfd
-				continue;
fa3bfd
-			} else if (*src == '\0') {
fa3bfd
-				return (0);
fa3bfd
-			}
fa3bfd
-			if (tp + NS_INT16SZ > endp)
fa3bfd
-				return (0);
fa3bfd
-			*tp++ = (u_char) (val >> 8) & 0xff;
fa3bfd
-			*tp++ = (u_char) val & 0xff;
fa3bfd
-			saw_xdigit = 0;
fa3bfd
-			val = 0;
fa3bfd
-			continue;
fa3bfd
-		}
fa3bfd
-		if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
fa3bfd
-		    inet_pton4(curtok, tp) > 0) {
fa3bfd
-			tp += NS_INADDRSZ;
fa3bfd
-			saw_xdigit = 0;
fa3bfd
-			break;	/* '\0' was seen by inet_pton4(). */
fa3bfd
-		}
fa3bfd
-		return (0);
fa3bfd
+  unsigned char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
fa3bfd
+  const char *curtok;
fa3bfd
+  int ch;
fa3bfd
+  size_t xdigits_seen;	/* Number of hex digits since colon.  */
fa3bfd
+  unsigned int val;
fa3bfd
+
fa3bfd
+  tp = memset (tmp, '\0', NS_IN6ADDRSZ);
fa3bfd
+  endp = tp + NS_IN6ADDRSZ;
fa3bfd
+  colonp = NULL;
fa3bfd
+
fa3bfd
+  /* Leading :: requires some special handling.  */
fa3bfd
+  if (src == src_endp)
fa3bfd
+    return 0;
fa3bfd
+  if (*src == ':')
fa3bfd
+    {
fa3bfd
+      ++src;
fa3bfd
+      if (src == src_endp || *src != ':')
fa3bfd
+        return 0;
fa3bfd
+    }
fa3bfd
+
fa3bfd
+  curtok = src;
fa3bfd
+  xdigits_seen = 0;
fa3bfd
+  val = 0;
fa3bfd
+  while (src < src_endp)
fa3bfd
+    {
fa3bfd
+      ch = *src++;
fa3bfd
+      int digit = hex_digit_value (ch);
fa3bfd
+      if (digit >= 0)
fa3bfd
+	{
fa3bfd
+	  if (xdigits_seen == 4)
fa3bfd
+	    return 0;
fa3bfd
+	  val <<= 4;
fa3bfd
+	  val |= digit;
fa3bfd
+	  if (val > 0xffff)
fa3bfd
+	    return 0;
fa3bfd
+	  ++xdigits_seen;
fa3bfd
+	  continue;
fa3bfd
 	}
fa3bfd
-	if (saw_xdigit) {
fa3bfd
-		if (tp + NS_INT16SZ > endp)
fa3bfd
-			return (0);
fa3bfd
-		*tp++ = (u_char) (val >> 8) & 0xff;
fa3bfd
-		*tp++ = (u_char) val & 0xff;
fa3bfd
+      if (ch == ':')
fa3bfd
+	{
fa3bfd
+	  curtok = src;
fa3bfd
+	  if (xdigits_seen == 0)
fa3bfd
+	    {
fa3bfd
+	      if (colonp)
fa3bfd
+		return 0;
fa3bfd
+	      colonp = tp;
fa3bfd
+	      continue;
fa3bfd
+	    }
fa3bfd
+	  else if (src == src_endp)
fa3bfd
+            return 0;
fa3bfd
+	  if (tp + NS_INT16SZ > endp)
fa3bfd
+	    return 0;
fa3bfd
+	  *tp++ = (unsigned char) (val >> 8) & 0xff;
fa3bfd
+	  *tp++ = (unsigned char) val & 0xff;
fa3bfd
+	  xdigits_seen = 0;
fa3bfd
+	  val = 0;
fa3bfd
+	  continue;
fa3bfd
 	}
fa3bfd
-	if (colonp != NULL) {
fa3bfd
-		/*
fa3bfd
-		 * Since some memmove()'s erroneously fail to handle
fa3bfd
-		 * overlapping regions, we'll do the shift by hand.
fa3bfd
-		 */
fa3bfd
-		const int n = tp - colonp;
fa3bfd
-		int i;
fa3bfd
-
fa3bfd
-		if (tp == endp)
fa3bfd
-			return (0);
fa3bfd
-		for (i = 1; i <= n; i++) {
fa3bfd
-			endp[- i] = colonp[n - i];
fa3bfd
-			colonp[n - i] = 0;
fa3bfd
-		}
fa3bfd
-		tp = endp;
fa3bfd
+      if (ch == '.' && ((tp + NS_INADDRSZ) <= endp)
fa3bfd
+          && inet_pton4 (curtok, src_endp, tp) > 0)
fa3bfd
+	{
fa3bfd
+	  tp += NS_INADDRSZ;
fa3bfd
+	  xdigits_seen = 0;
fa3bfd
+	  break;  /* '\0' was seen by inet_pton4.  */
fa3bfd
 	}
fa3bfd
-	if (tp != endp)
fa3bfd
-		return (0);
fa3bfd
-	memcpy(dst, tmp, NS_IN6ADDRSZ);
fa3bfd
-	return (1);
fa3bfd
+      return 0;
fa3bfd
+    }
fa3bfd
+  if (xdigits_seen > 0)
fa3bfd
+    {
fa3bfd
+      if (tp + NS_INT16SZ > endp)
fa3bfd
+	return 0;
fa3bfd
+      *tp++ = (unsigned char) (val >> 8) & 0xff;
fa3bfd
+      *tp++ = (unsigned char) val & 0xff;
fa3bfd
+    }
fa3bfd
+  if (colonp != NULL)
fa3bfd
+    {
fa3bfd
+      /* Replace :: with zeros.  */
fa3bfd
+      if (tp == endp)
fa3bfd
+        /* :: would expand to a zero-width field.  */
fa3bfd
+        return 0;
fa3bfd
+      size_t n = tp - colonp;
fa3bfd
+      memmove (endp - n, colonp, n);
fa3bfd
+      memset (colonp, 0, endp - n - colonp);
fa3bfd
+      tp = endp;
fa3bfd
+    }
fa3bfd
+  if (tp != endp)
fa3bfd
+    return 0;
fa3bfd
+  memcpy (dst, tmp, NS_IN6ADDRSZ);
fa3bfd
+  return 1;
fa3bfd
 }
fa3bfd
diff --git a/resolv/resolv-internal.h b/resolv/resolv-internal.h
fa3bfd
index 269758c416544cb4..a9f5659dbbacb477 100644
fa3bfd
--- a/resolv/resolv-internal.h
fa3bfd
+++ b/resolv/resolv-internal.h
fa3bfd
@@ -32,4 +32,13 @@ res_use_inet6 (void)
fa3bfd
   return _res.options & DEPRECATED_RES_USE_INET6;
fa3bfd
 }
fa3bfd
 
fa3bfd
+/* Convert from presentation format (which usually means ASCII
fa3bfd
+   printable) to network format (which is usually some kind of binary
fa3bfd
+   format).  The input is in the range [SRC, SRC + SRCLEN).  The
fa3bfd
+   output is written to DST (which has to be 4 or 16 bytes long,
fa3bfd
+   depending on AF).  Return 0 for invalid input, 1 for success, -1
fa3bfd
+   for an invalid address family.  */
fa3bfd
+int __inet_pton_length (int af, const char *src, size_t srclen, void *);
fa3bfd
+libc_hidden_proto (__inet_pton_length)
fa3bfd
+
fa3bfd
 #endif  /* _RESOLV_INTERNAL_H */
fa3bfd
diff --git a/resolv/tst-inet_pton.c b/resolv/tst-inet_pton.c
fa3bfd
new file mode 100644
fa3bfd
index 0000000000000000..4bb9f8119378b467
fa3bfd
--- /dev/null
fa3bfd
+++ b/resolv/tst-inet_pton.c
fa3bfd
@@ -0,0 +1,537 @@
fa3bfd
+/* Test inet_pton functions.
fa3bfd
+   Copyright (C) 2017 Free Software Foundation, Inc.
fa3bfd
+   This file is part of the GNU C Library.
fa3bfd
+
fa3bfd
+   The GNU C Library is free software; you can redistribute it and/or
fa3bfd
+   modify it under the terms of the GNU Lesser General Public
fa3bfd
+   License as published by the Free Software Foundation; either
fa3bfd
+   version 2.1 of the License, or (at your option) any later version.
fa3bfd
+
fa3bfd
+   The GNU C Library is distributed in the hope that it will be useful,
fa3bfd
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
fa3bfd
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
fa3bfd
+   Lesser General Public License for more details.
fa3bfd
+
fa3bfd
+   You should have received a copy of the GNU Lesser General Public
fa3bfd
+   License along with the GNU C Library; if not, see
fa3bfd
+   <http://www.gnu.org/licenses/>.  */
fa3bfd
+
fa3bfd
+#include <arpa/inet.h>
fa3bfd
+#include <resolv/resolv-internal.h>
fa3bfd
+#include <stdbool.h>
fa3bfd
+#include <stdio.h>
fa3bfd
+#include <string.h>
fa3bfd
+#include <support/check.h>
fa3bfd
+#include <support/xunistd.h>
fa3bfd
+#include <sys/mman.h>
fa3bfd
+#include <sys/param.h>
fa3bfd
+#include <unistd.h>
fa3bfd
+
fa3bfd
+/* Memory region created by next_to_fault_allocate.  */
fa3bfd
+struct next_to_fault
fa3bfd
+{
fa3bfd
+  /* The user data.  */
fa3bfd
+  char *buffer;
fa3bfd
+  size_t length;
fa3bfd
+
fa3bfd
+  /* The entire allocated region.  */
fa3bfd
+  void *region_start;
fa3bfd
+  size_t region_size;
fa3bfd
+};
fa3bfd
+
fa3bfd
+/* Allocate a buffer of SIZE bytes just before a page which is mapped
fa3bfd
+   with PROT_NONE (so that overrunning the buffer will cause a
fa3bfd
+   fault).  */
fa3bfd
+static struct next_to_fault
fa3bfd
+next_to_fault_allocate (size_t size)
fa3bfd
+{
fa3bfd
+  long page_size = sysconf (_SC_PAGE_SIZE);
fa3bfd
+  TEST_VERIFY_EXIT (page_size > 0);
fa3bfd
+  struct next_to_fault result;
fa3bfd
+  result.region_size = roundup (size, page_size) + page_size;
fa3bfd
+  TEST_VERIFY_EXIT (size + page_size > size);
fa3bfd
+  TEST_VERIFY_EXIT (result.region_size > size);
fa3bfd
+  result.region_start
fa3bfd
+    = xmmap (NULL, result.region_size, PROT_READ | PROT_WRITE,
fa3bfd
+             MAP_PRIVATE | MAP_ANONYMOUS, -1);
fa3bfd
+  /* Unmap the page after the allocation.  */
fa3bfd
+  xmprotect (result.region_start + (result.region_size - page_size),
fa3bfd
+             page_size, PROT_NONE);
fa3bfd
+  /* Align the allocation within the region so that it ends just
fa3bfd
+     before the PROT_NONE page.  */
fa3bfd
+  result.buffer = result.region_start + result.region_size - page_size - size;
fa3bfd
+  result.length = size;
fa3bfd
+  return result;
fa3bfd
+}
fa3bfd
+
fa3bfd
+/* Deallocate the memory region allocated by
fa3bfd
+   next_to_fault_allocate.  */
fa3bfd
+static void
fa3bfd
+next_to_fault_free (struct next_to_fault *ntf)
fa3bfd
+{
fa3bfd
+  xmunmap (ntf->region_start, ntf->region_size);
fa3bfd
+  *ntf = (struct next_to_fault) { NULL, };
fa3bfd
+}
fa3bfd
+
fa3bfd
+struct test_case
fa3bfd
+{
fa3bfd
+  /* The input data.  */
fa3bfd
+  const char *input;
fa3bfd
+
fa3bfd
+  /* True if AF_INET parses successfully.  */
fa3bfd
+  bool ipv4_ok;
fa3bfd
+
fa3bfd
+  /* True if AF_INET6 parses successfully.  */
fa3bfd
+  bool ipv6_ok;
fa3bfd
+
fa3bfd
+  /* Expected result for AF_INET.  */
fa3bfd
+  unsigned char ipv4_expected[4];
fa3bfd
+
fa3bfd
+  /* Expected result for AF_INET6.  */
fa3bfd
+  unsigned char ipv6_expected[16];
fa3bfd
+};
fa3bfd
+
fa3bfd
+static void
fa3bfd
+check_result (const char *what, const struct test_case *t, int family,
fa3bfd
+              void *result_buffer, int inet_ret)
fa3bfd
+{
fa3bfd
+  TEST_VERIFY_EXIT (inet_ret >= -1);
fa3bfd
+  TEST_VERIFY_EXIT (inet_ret <= 1);
fa3bfd
+
fa3bfd
+  int ok;
fa3bfd
+  const unsigned char *expected;
fa3bfd
+  size_t result_size;
fa3bfd
+  switch (family)
fa3bfd
+    {
fa3bfd
+    case AF_INET:
fa3bfd
+      ok = t->ipv4_ok;
fa3bfd
+      expected = t->ipv4_expected;
fa3bfd
+      result_size = 4;
fa3bfd
+      break;
fa3bfd
+    case AF_INET6:
fa3bfd
+      ok = t->ipv6_ok;
fa3bfd
+      expected = t->ipv6_expected;
fa3bfd
+      result_size = 16;
fa3bfd
+      break;
fa3bfd
+    default:
fa3bfd
+      FAIL_EXIT1 ("invalid address family %d", family);
fa3bfd
+    }
fa3bfd
+
fa3bfd
+  if (inet_ret != ok)
fa3bfd
+    {
fa3bfd
+      support_record_failure ();
fa3bfd
+      printf ("error: %s return value mismatch for [[%s]], family %d\n"
fa3bfd
+              "  expected: %d\n"
fa3bfd
+              "  actual: %d\n",
fa3bfd
+              what, t->input, family, ok, inet_ret);
fa3bfd
+      return;
fa3bfd
+    }
fa3bfd
+  if (memcmp (result_buffer, expected, result_size) != 0)
fa3bfd
+    {
fa3bfd
+      support_record_failure ();
fa3bfd
+      printf ("error: %s result mismatch for [[%s]], family %d\n",
fa3bfd
+              what, t->input, family);
fa3bfd
+    }
fa3bfd
+}
fa3bfd
+
fa3bfd
+static void
fa3bfd
+run_one_test (const struct test_case *t)
fa3bfd
+{
fa3bfd
+  size_t test_len = strlen (t->input);
fa3bfd
+
fa3bfd
+  struct next_to_fault ntf_out4 = next_to_fault_allocate (4);
fa3bfd
+  struct next_to_fault ntf_out6 = next_to_fault_allocate (16);
fa3bfd
+
fa3bfd
+  /* inet_pton requires NUL termination.  */
fa3bfd
+  {
fa3bfd
+    struct next_to_fault ntf_in = next_to_fault_allocate (test_len + 1);
fa3bfd
+    memcpy (ntf_in.buffer, t->input, test_len + 1);
fa3bfd
+    memset (ntf_out4.buffer, 0, 4);
fa3bfd
+    check_result ("inet_pton", t, AF_INET, ntf_out4.buffer,
fa3bfd
+                  inet_pton (AF_INET, ntf_in.buffer, ntf_out4.buffer));
fa3bfd
+    memset (ntf_out6.buffer, 0, 16);
fa3bfd
+    check_result ("inet_pton", t, AF_INET6, ntf_out6.buffer,
fa3bfd
+                  inet_pton (AF_INET6, ntf_in.buffer, ntf_out6.buffer));
fa3bfd
+    next_to_fault_free (&ntf_in);
fa3bfd
+  }
fa3bfd
+
fa3bfd
+  /* __inet_pton_length does not require NUL termination.  */
fa3bfd
+  {
fa3bfd
+    struct next_to_fault ntf_in = next_to_fault_allocate (test_len);
fa3bfd
+    memcpy (ntf_in.buffer, t->input, test_len);
fa3bfd
+    memset (ntf_out4.buffer, 0, 4);
fa3bfd
+    check_result ("__inet_pton_length", t, AF_INET, ntf_out4.buffer,
fa3bfd
+                  __inet_pton_length (AF_INET, ntf_in.buffer, ntf_in.length,
fa3bfd
+                                      ntf_out4.buffer));
fa3bfd
+    memset (ntf_out6.buffer, 0, 16);
fa3bfd
+    check_result ("__inet_pton_length", t, AF_INET6, ntf_out6.buffer,
fa3bfd
+                  __inet_pton_length (AF_INET6, ntf_in.buffer, ntf_in.length,
fa3bfd
+                                      ntf_out6.buffer));
fa3bfd
+    next_to_fault_free (&ntf_in);
fa3bfd
+  }
fa3bfd
+
fa3bfd
+  next_to_fault_free (&ntf_out4);
fa3bfd
+  next_to_fault_free (&ntf_out6);
fa3bfd
+}
fa3bfd
+
fa3bfd
+/* The test cases were manually crafted and the set enhanced with
fa3bfd
+   American Fuzzy Lop.  */
fa3bfd
+const struct test_case test_cases[] =
fa3bfd
+  {
fa3bfd
+    {.input = ".:", },
fa3bfd
+    {.input = "0.0.0.0",
fa3bfd
+     .ipv4_ok = true,
fa3bfd
+     .ipv4_expected = {0, 0, 0, 0},
fa3bfd
+    },
fa3bfd
+    {.input = "0.:", },
fa3bfd
+    {.input = "00", },
fa3bfd
+    {.input = "0000000", },
fa3bfd
+    {.input = "00000000000000000", },
fa3bfd
+    {.input = "092.", },
fa3bfd
+    {.input = "10.0.301.2", },
fa3bfd
+    {.input = "127.0.0.1",
fa3bfd
+     .ipv4_ok = true,
fa3bfd
+     .ipv4_expected = {127, 0, 0, 1},
fa3bfd
+    },
fa3bfd
+    {.input = "19..", },
fa3bfd
+    {.input = "192.0.2.-1", },
fa3bfd
+    {.input = "192.0.2.01", },
fa3bfd
+    {.input = "192.0.2.1.", },
fa3bfd
+    {.input = "192.0.2.1192.", },
fa3bfd
+    {.input = "192.0.2.192.\377..", },
fa3bfd
+    {.input = "192.0.2.256", },
fa3bfd
+    {.input = "192.0.2.27",
fa3bfd
+     .ipv4_ok = true,
fa3bfd
+     .ipv4_expected = {192, 0, 2, 27},
fa3bfd
+    },
fa3bfd
+    {.input = "192.0.201.", },
fa3bfd
+    {.input = "192.0.261.", },
fa3bfd
+    {.input = "192.0.2\256", },
fa3bfd
+    {.input = "192.0.\262.", },
fa3bfd
+    {.input = "192.062.", },
fa3bfd
+    {.input = "192.092.\256", },
fa3bfd
+    {.input = "192.0\2562.", },
fa3bfd
+    {.input = "192.192.0.2661\031", },
fa3bfd
+    {.input = "192.192.00n2.1.", },
fa3bfd
+    {.input = "192.192.2.190.", },
fa3bfd
+    {.input = "192.255.255.2555", },
fa3bfd
+    {.input = "192.92.219\023.", },
fa3bfd
+    {.input = "192.\260.2.", },
fa3bfd
+    {.input = "1:1::1:1",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x0, 0x1, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x1
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2", },
fa3bfd
+    {.input = "2.", },
fa3bfd
+    {.input = "2001:db8:00001::f", },
fa3bfd
+    {.input = "2001:db8:10000::f", },
fa3bfd
+    {.input = "2001:db8:1234:5678:abcd:ef01:2345:67",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x20, 0x1, 0xd, 0xb8, 0x12, 0x34, 0x56, 0x78,
fa3bfd
+       0xab, 0xcd, 0xef, 0x1, 0x23, 0x45, 0x0, 0x67
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2001:db8:1234:5678:abcd:ef01:2345:6789:1", },
fa3bfd
+    {.input = "2001:db8:1234:5678:abcd:ef01:2345::6789", },
fa3bfd
+    {.input = "2001:db8::0",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2001:db8::00",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2001:db8::1",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2001:db8::10",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x10
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2001:db8::19",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x19
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2001:db8::1::\012", },
fa3bfd
+    {.input = "2001:db8::1::2\012", },
fa3bfd
+    {.input = "2001:db8::2",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2001:db8::3",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2001:db8::4",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x4
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2001:db8::5",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2001:db8::6",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2001:db8::7",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2001:db8::8",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x8
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2001:db8::9",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x9
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2001:db8::A",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2001:db8::B",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xb
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2001:db8::C",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2001:db8::D",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xd
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2001:db8::E",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xe
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2001:db8::F",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2001:db8::a",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2001:db8::b",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xb
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2001:db8::c",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xc
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2001:db8::d",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xd
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2001:db8::e",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xe
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2001:db8::f",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xf
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2001:db8::ff",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x20, 0x1, 0xd, 0xb8, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "2001:db8::ffff:2\012", },
fa3bfd
+    {.input = "22", },
fa3bfd
+    {.input = "2222@", },
fa3bfd
+    {.input = "255.255.255.255",
fa3bfd
+     .ipv4_ok = true,
fa3bfd
+     .ipv4_expected = {255, 255, 255, 255},
fa3bfd
+    },
fa3bfd
+    {.input = "255.255.255.255\001", },
fa3bfd
+    {.input = "255.255.255.25555", },
fa3bfd
+    {.input = "2:", },
fa3bfd
+    {.input = "2:a:8:EEEE::EEEE:F:EEE8:EEEE\034*:", },
fa3bfd
+    {.input = "2:ff:1:1:7:ff:1:1:7.", },
fa3bfd
+    {.input = "2f:0000000000000000000000000000000000000000000000000000000000"
fa3bfd
+     "0000000000000000000000000000000000000000000000000000000000000000000000"
fa3bfd
+     "0G01",
fa3bfd
+    },
fa3bfd
+    {.input = "429495", },
fa3bfd
+    {.input = "5::5::", },
fa3bfd
+    {.input = "6.6.", },
fa3bfd
+    {.input = "992.", },
fa3bfd
+    {.input = "::",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "::00001", },
fa3bfd
+    {.input = "::1",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "::10000", },
fa3bfd
+    {.input = "::1:1",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x1
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "::ff:1:1:7.0.0.1",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff,
fa3bfd
+       0x0, 0x1, 0x0, 0x1, 0x7, 0x0, 0x0, 0x1
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "::ff:1:1:7:ff:1:1:7.", },
fa3bfd
+    {.input = "::ff:1:1:7ff:1:8:7.0.0.1", },
fa3bfd
+    {.input = "::ff:1:1:7ff:1:8f:1:1:71", },
fa3bfd
+    {.input = "::ffff:02fff:127.0.S1", },
fa3bfd
+    {.input = "::ffff:127.0.0.1",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0xff, 0xff, 0x7f, 0x0, 0x0, 0x1
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "::ffff:1:7.0.0.1",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0xff, 0xff, 0x0, 0x1, 0x7, 0x0, 0x0, 0x1
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = ":\272", },
fa3bfd
+    {.input = "A:f:ff:1:1:D:ff:1:1::7.", },
fa3bfd
+    {.input = "AAAAA.", },
fa3bfd
+    {.input = "D:::", },
fa3bfd
+    {.input = "DF8F", },
fa3bfd
+    {.input = "F::",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x0, 0xf, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "F:A:8:EEEE:8:EEEE\034*:", },
fa3bfd
+    {.input = "F:a:8:EEEE:8:EEEE\034*:", },
fa3bfd
+    {.input = "F:ff:100:7ff:1:8:7.0.10.1",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0x0, 0xf, 0x0, 0xff, 0x1, 0x0, 0x7, 0xff,
fa3bfd
+       0x0, 0x1, 0x0, 0x8, 0x7, 0x0, 0xa, 0x1
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "d92.", },
fa3bfd
+    {.input = "ff:00000000000000000000000000000000000000000000000000000000000"
fa3bfd
+     "00000000000000000000000000000000000000000000000000000000000000000001",
fa3bfd
+    },
fa3bfd
+    {.input = "fff2:2::ff2:2:f7",
fa3bfd
+     .ipv6_ok = true,
fa3bfd
+     .ipv6_expected = {
fa3bfd
+       0xff, 0xf2, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0,
fa3bfd
+       0x0, 0x0, 0xf, 0xf2, 0x0, 0x2, 0x0, 0xf7
fa3bfd
+     },
fa3bfd
+    },
fa3bfd
+    {.input = "ffff:ff:ff:fff:ff:ff:ff:", },
fa3bfd
+    {.input = "\272:", },
fa3bfd
+    {NULL}
fa3bfd
+  };
fa3bfd
+
fa3bfd
+static int
fa3bfd
+do_test (void)
fa3bfd
+{
fa3bfd
+  for (size_t i = 0; test_cases[i].input != NULL; ++i)
fa3bfd
+    run_one_test (test_cases + i);
fa3bfd
+  return 0;
fa3bfd
+}
fa3bfd
+
fa3bfd
+#include <support/test-driver.c>
fa3bfd
diff --git a/sysdeps/unix/sysv/linux/if_index.c b/sysdeps/unix/sysv/linux/if_index.c
fa3bfd
index cf336839259b24b6..8ba5eae7818b49cd 100644
fa3bfd
--- a/sysdeps/unix/sysv/linux/if_index.c
fa3bfd
+++ b/sysdeps/unix/sysv/linux/if_index.c
fa3bfd
@@ -1,4 +1,4 @@
fa3bfd
-/* Copyright (C) 1997-2012 Free Software Foundation, Inc.
fa3bfd
+/* Copyright (C) 1997-2017 Free Software Foundation, Inc.
fa3bfd
    This file is part of the GNU C Library.
fa3bfd
 
fa3bfd
    The GNU C Library is free software; you can redistribute it and/or
fa3bfd
@@ -24,15 +24,14 @@
fa3bfd
 #include <net/if.h>
fa3bfd
 #include <sys/socket.h>
fa3bfd
 #include <sys/ioctl.h>
fa3bfd
-#include <bits/libc-lock.h>
fa3bfd
+#include <libc-lock.h>
fa3bfd
 #include <not-cancel.h>
fa3bfd
-#include <kernel-features.h>
fa3bfd
 
fa3bfd
 #include "netlinkaccess.h"
fa3bfd
 
fa3bfd
 
fa3bfd
 unsigned int
fa3bfd
-if_nametoindex (const char *ifname)
fa3bfd
+__if_nametoindex (const char *ifname)
fa3bfd
 {
fa3bfd
 #ifndef SIOCGIFINDEX
fa3bfd
   __set_errno (ENOSYS);
fa3bfd
@@ -57,11 +56,13 @@ if_nametoindex (const char *ifname)
fa3bfd
   return ifr.ifr_ifindex;
fa3bfd
 #endif
fa3bfd
 }
fa3bfd
-libc_hidden_def (if_nametoindex)
fa3bfd
+libc_hidden_def (__if_nametoindex)
fa3bfd
+weak_alias (__if_nametoindex, if_nametoindex)
fa3bfd
+libc_hidden_weak (if_nametoindex)
fa3bfd
 
fa3bfd
 
fa3bfd
 void
fa3bfd
-if_freenameindex (struct if_nameindex *ifn)
fa3bfd
+__if_freenameindex (struct if_nameindex *ifn)
fa3bfd
 {
fa3bfd
   struct if_nameindex *ptr = ifn;
fa3bfd
   while (ptr->if_name || ptr->if_index)
fa3bfd
@@ -71,7 +72,9 @@ if_freenameindex (struct if_nameindex *ifn)
fa3bfd
     }
fa3bfd
   free (ifn);
fa3bfd
 }
fa3bfd
-libc_hidden_def (if_freenameindex)
fa3bfd
+libc_hidden_def (__if_freenameindex)
fa3bfd
+weak_alias (__if_freenameindex, if_freenameindex)
fa3bfd
+libc_hidden_weak (if_freenameindex)
fa3bfd
 
fa3bfd
 
fa3bfd
 static struct if_nameindex *
fa3bfd
@@ -163,7 +166,7 @@ if_nameindex_netlink (void)
fa3bfd
 		      if (idx[nifs].if_name == NULL)
fa3bfd
 			{
fa3bfd
 			  idx[nifs].if_index = 0;
fa3bfd
-			  if_freenameindex (idx);
fa3bfd
+			  __if_freenameindex (idx);
fa3bfd
 			  idx = NULL;
fa3bfd
 			  goto nomem;
fa3bfd
 			}
fa3bfd
@@ -190,7 +193,7 @@ if_nameindex_netlink (void)
fa3bfd
 
fa3bfd
 
fa3bfd
 struct if_nameindex *
fa3bfd
-if_nameindex (void)
fa3bfd
+__if_nameindex (void)
fa3bfd
 {
fa3bfd
 #ifndef SIOCGIFINDEX
fa3bfd
   __set_errno (ENOSYS);
fa3bfd
@@ -200,11 +203,12 @@ if_nameindex (void)
fa3bfd
   return result;
fa3bfd
 #endif
fa3bfd
 }
fa3bfd
-libc_hidden_def (if_nameindex)
fa3bfd
+weak_alias (__if_nameindex, if_nameindex)
fa3bfd
+libc_hidden_weak (if_nameindex)
fa3bfd
 
fa3bfd
 
fa3bfd
 char *
fa3bfd
-if_indextoname (unsigned int ifindex, char *ifname)
fa3bfd
+__if_indextoname (unsigned int ifindex, char *ifname)
fa3bfd
 {
fa3bfd
   /* We may be able to do the conversion directly, rather than searching a
fa3bfd
      list.  This ioctl is not present in kernels before version 2.1.50.  */
fa3bfd
@@ -233,4 +237,5 @@ if_indextoname (unsigned int ifindex, char *ifname)
fa3bfd
   else
fa3bfd
     return strncpy (ifname, ifr.ifr_name, IFNAMSIZ);
fa3bfd
 }
fa3bfd
-libc_hidden_def (if_indextoname)
fa3bfd
+weak_alias (__if_indextoname, if_indextoname)
fa3bfd
+libc_hidden_weak (if_indextoname)