diff --git a/inet/Makefile b/inet/Makefile
index cf4cf5cf..3b0d69be 100644
--- a/inet/Makefile
+++ b/inet/Makefile
@@ -56,7 +56,7 @@ aux := check_pf check_native ifreq
tests := htontest test_ifindex tst-ntoa tst-ether_aton tst-network \
tst-gethnm test-ifaddrs bug-if1 test-inet6_opt tst-ether_line \
tst-getni1 tst-getni2 tst-inet6_rth tst-checks tst-checks-posix \
- tst-sockaddr test-hnto-types tst-if_index-long
+ tst-sockaddr test-hnto-types tst-if_index-long tst-getni-local
# tst-deadline must be linked statically so that we can access
# internal functions.
diff --git a/inet/getnameinfo.c b/inet/getnameinfo.c
index 8380d857..58d138d3 100644
--- a/inet/getnameinfo.c
+++ b/inet/getnameinfo.c
@@ -396,7 +396,7 @@ gni_host_local (struct scratch_buffer *tmpbuf,
return checked_copy (host, hostlen, "localhost");
}
-/* Convert the host part of an AF_LOCAK socket address. */
+/* Convert the host part of an AF_LOCAL socket address. */
static int
gni_host (struct scratch_buffer *tmpbuf,
const struct sockaddr *sa, socklen_t addrlen,
@@ -459,6 +459,15 @@ gni_serv_local (struct scratch_buffer *tmpbuf,
const struct sockaddr *sa, socklen_t addrlen,
char *serv, socklen_t servlen, int flags)
{
+ if (((const struct sockaddr_un *) sa)->sun_path[0] == '\0')
+ {
+ /* Abstract socket */
+ socklen_t len = addrlen - offsetof (struct sockaddr_un, sun_path);
+ if (len > servlen)
+ return EAI_OVERFLOW;
+ memcpy (serv, ((const struct sockaddr_un *) sa)->sun_path, len);
+ return 0;
+ }
return checked_copy
(serv, servlen, ((const struct sockaddr_un *) sa)->sun_path);
}
diff --git a/inet/tst-getni-local.c b/inet/tst-getni-local.c
new file mode 100644
index 00000000..8c39fb27
--- /dev/null
+++ b/inet/tst-getni-local.c
@@ -0,0 +1,63 @@
+/* Test for getnameinfo AF_LOCAL/UNIX sockets
+ Copyright (C) 2021 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ . */
+
+#include
+#include
+#include
+#include
+#include
+
+#include
+
+#define TEST_SOCK1 "my funky socket"
+static int
+do_test (void)
+{
+ struct sockaddr_un s;
+ char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
+ const char abstract[] = { 0, 'a', 'b', 's', 't', 'r', 'a', 'c', 't', 0,
+ '!' };
+ int len = sizeof(abstract) + offsetof (struct sockaddr_un, sun_path);
+
+ s.sun_family = AF_UNIX;
+ strcpy (s.sun_path, TEST_SOCK1);
+
+ TEST_VERIFY (getnameinfo ((struct sockaddr *) &s, sizeof (s), hbuf,
+ sizeof (hbuf), sbuf, sizeof (sbuf),
+ NI_NUMERICHOST | NI_NUMERICSERV) == 0);
+
+ TEST_VERIFY (strncmp ("localhost", hbuf, NI_MAXHOST) == 0);
+ TEST_VERIFY (strncmp (TEST_SOCK1, sbuf, NI_MAXSERV) == 0);
+
+ memset ( hbuf, 0, NI_MAXHOST );
+ memset ( sbuf, 0, NI_MAXSERV );
+
+ memcpy ( s.sun_path, abstract, sizeof (abstract) );
+
+ TEST_VERIFY (getnameinfo ((struct sockaddr *) &s, len, hbuf, sizeof (hbuf),
+ sbuf, sizeof (sbuf),
+ NI_NUMERICHOST | NI_NUMERICSERV) == 0);
+
+ TEST_VERIFY (strncmp ("localhost", hbuf, NI_MAXHOST) == 0);
+ TEST_VERIFY (memcmp (abstract, sbuf, sizeof (abstract)) == 0);
+
+ return 0;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"