3 * Wireshark - Network traffic analyzer
4 * By Gerald Combs <gerald@wireshark.org>
5 * Copyright 1998 Gerald Combs
7 * SPDX-License-Identifier: GPL-2.0-or-later
11 #include "inet_addr.h"
16 #ifdef HAVE_ARPA_INET_H
17 #include <arpa/inet.h>
20 #ifdef HAVE_NETINET_IN_H
21 #include <netinet/in.h>
24 #include <sys/types.h>
26 #ifdef HAVE_SYS_SOCKET_H
27 #include <sys/socket.h> /* needed to define AF_ values on UNIX */
31 #include <Ws2tcpip.h> /* indirectly defines AF_ values on Windows */
32 #define _NTOP_SRC_CAST_ (PVOID)
34 #define _NTOP_SRC_CAST_
38 * We assume and require an inet_pton/inet_ntop that supports AF_INET
42 static inline gboolean
43 _inet_pton(int af, const gchar *src, gpointer dst)
45 gint ret = inet_pton(af, src, dst);
46 if (G_UNLIKELY(ret < 0)) {
49 memset(dst, 0, sizeof(struct in_addr));
50 g_critical("ws_inet_pton4: EAFNOSUPPORT");
52 else if (af == AF_INET6) {
53 memset(dst, 0, sizeof(struct in6_addr));
54 g_critical("ws_inet_pton6: EAFNOSUPPORT");
64 static inline const gchar *
65 _inet_ntop(int af, gconstpointer src, gchar *dst, guint dst_size)
67 const gchar *ret = inet_ntop(af, _NTOP_SRC_CAST_ src, dst, dst_size);
68 if (G_UNLIKELY(ret == NULL)) {
69 int saved_errno = errno;
70 gchar *errmsg = "<<ERROR>>";
73 errmsg = "<<EAFNOSUPPORT>>";
74 g_critical("ws_inet_ntop: EAFNOSUPPORT");
77 errmsg = "<<ENOSPC>>";
82 /* set result to something that can't be confused with a valid conversion */
83 g_strlcpy(dst, errmsg, dst_size);
84 /* set errno for caller */
91 ws_inet_ntop4(gconstpointer src, gchar *dst, guint dst_size)
93 return _inet_ntop(AF_INET, src, dst, dst_size);
97 ws_inet_pton4(const gchar *src, guint32 *dst)
99 return _inet_pton(AF_INET, src, dst);
103 ws_inet_ntop6(gconstpointer src, gchar *dst, guint dst_size)
105 return _inet_ntop(AF_INET6, src, dst, dst_size);
109 ws_inet_pton6(const gchar *src, ws_in6_addr *dst)
111 return _inet_pton(AF_INET6, src, dst);