]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
Add inet_sockaddr2str() to wrap inet_ntop(3)
authorAlejandro Colomar <alx@kernel.org>
Thu, 22 Dec 2022 14:26:01 +0000 (15:26 +0100)
committerIker Pedrosa <ikerpedrosam@gmail.com>
Mon, 2 Jan 2023 07:20:43 +0000 (08:20 +0100)
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/prototypes.h
libmisc/Makefile.am
libmisc/inet_sockaddr2str.c [new file with mode: 0644]

index d2314dcd6fe2a10b2f1f611b406d8c670ba1e2e4..066252c6e705e6816399dfafaa4da346030e7972 100644 (file)
@@ -21,6 +21,7 @@
 
 #include <config.h>
 
+#include <sys/socket.h>
 #include <sys/stat.h>
 #include <utmp.h>
 #include <sys/types.h>
@@ -193,6 +194,9 @@ extern bool gr_append_member (struct group *grp, char *member);
 /* hushed.c */
 extern bool hushed (const char *username);
 
+/* inet_sockaddr2str.c */
+extern const char *inet_sockaddr2str(const struct sockaddr *sa);
+
 /* audit_help.c */
 #ifdef WITH_AUDIT
 extern int audit_fd;
index c2277a02343e34820d6ff68f3a1ad2dee20ff14e..5484c8a5d3a41d893cc3d1a16276f8cd69355e19 100644 (file)
@@ -38,6 +38,7 @@ libmisc_la_SOURCES = \
        hushed.c \
        idmapping.h \
        idmapping.c \
+       inet_sockaddr2str.c \
        isexpired.c \
        limits.c \
        list.c log.c \
diff --git a/libmisc/inet_sockaddr2str.c b/libmisc/inet_sockaddr2str.c
new file mode 100644 (file)
index 0000000..ce7466c
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * SPDX-FileCopyrightText: 2022, Alejandro Colomar <alx@kernel.org>
+ *
+ * SPDX-License-Identifier:  BSD-3-Clause
+ */
+
+#include <config.h>
+
+#include <arpa/inet.h>
+#include <errno.h>
+#include <netinet/in.h>
+#include <sys/param.h>
+#include <sys/socket.h>
+
+#ident "$Id$"
+
+#include "defines.h"
+#include "prototypes.h"
+
+
+#if !defined(INET_ADDRSTRLENMAX)
+#define INET_ADDRSTRLENMAX  MAX(INET_ADDRSTRLEN, INET6_ADDRSTRLEN)
+#endif
+
+
+/*
+ * SYNOPSIS
+ *     const char *inet_sockaddr2str(const struct sockaddr *sa);
+ *
+ * DESCRIPTION
+ *     This function is similar to inet_ntop(3).  It transforms an address
+ *     in 'struct in_addr' or 'struct in6_addr' form into a human-readable
+ *     string.
+ *
+ *     It receives a sockaddr structure, which is simpler to pass after
+ *     receiving it from getaddrinfo(3).  However, this function is not
+ *     reentrant, and like inet_ntoa(3), it uses an internal buffer, for
+ *     simplicity; anyway we're not in a multithreaded program, and it
+ *     doesn't contain any sensitive data, so it's fine to use static
+ *     storage here.
+ *
+ * RETURN VALUE
+ *     This function returns a pointer to a statically allocated buffer,
+ *     which subsequent calls will overwrite.
+ *
+ *     On error, it returns NULL.
+ *
+ * ERRORS
+ *     EAFNOSUPPORT
+ *             The address family in sa->sa_family is not AF_INET or AF_INET6.
+ *
+ * CAVEATS
+ *     This function is not reentrant.
+ */
+
+
+const char *
+inet_sockaddr2str(const struct sockaddr *sa)
+{
+       struct sockaddr_in   *sin;
+       struct sockaddr_in6  *sin6;
+
+       static char          buf[INET_ADDRSTRLENMAX];
+
+       switch (sa->sa_family) {
+       case AF_INET:
+               sin = (struct sockaddr_in *) sa;
+               inet_ntop(AF_INET, &sin->sin_addr, buf, NITEMS(buf));
+               return buf;
+       case AF_INET6:
+               sin6 = (struct sockaddr_in6 *) sa;
+               inet_ntop(AF_INET6, &sin6->sin6_addr, buf, NITEMS(buf));
+               return buf;
+       default:
+               errno = EAFNOSUPPORT;
+               return NULL;
+       }
+}