2016-02-10 09:11:12 +00:00
|
|
|
/* inet_addr.c
|
|
|
|
*
|
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
2018-02-07 11:26:45 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
2016-02-10 09:11:12 +00:00
|
|
|
*/
|
|
|
|
|
2016-12-02 10:32:39 +00:00
|
|
|
#include "config.h"
|
2016-02-10 09:11:12 +00:00
|
|
|
#include "inet_addr.h"
|
|
|
|
|
2017-10-29 19:22:39 +00:00
|
|
|
#include <errno.h>
|
2017-10-30 08:42:26 +00:00
|
|
|
#include <string.h>
|
2017-10-29 19:22:39 +00:00
|
|
|
|
2017-10-24 23:29:14 +00:00
|
|
|
#ifdef HAVE_ARPA_INET_H
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
|
2018-05-25 17:50:35 +00:00
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
|
|
|
|
2017-10-24 23:29:14 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
|
|
|
#include <sys/socket.h> /* needed to define AF_ values on UNIX */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Ws2tcpip.h> /* indirectly defines AF_ values on Windows */
|
|
|
|
#define _NTOP_SRC_CAST_ (PVOID)
|
|
|
|
#else
|
|
|
|
#define _NTOP_SRC_CAST_
|
|
|
|
#endif
|
2016-02-10 09:11:12 +00:00
|
|
|
|
2021-05-23 23:46:43 +00:00
|
|
|
#include <wsutil/ws_assert.h>
|
|
|
|
|
2017-10-26 07:51:55 +00:00
|
|
|
/*
|
2017-10-30 08:42:26 +00:00
|
|
|
* We assume and require an inet_pton/inet_ntop that supports AF_INET
|
|
|
|
* and AF_INET6.
|
2017-10-26 07:51:55 +00:00
|
|
|
*/
|
|
|
|
|
2016-02-10 09:11:12 +00:00
|
|
|
static inline gboolean
|
|
|
|
_inet_pton(int af, const gchar *src, gpointer dst)
|
|
|
|
{
|
2017-10-26 07:51:55 +00:00
|
|
|
gint ret = inet_pton(af, src, dst);
|
2017-10-30 08:42:26 +00:00
|
|
|
if (G_UNLIKELY(ret < 0)) {
|
|
|
|
/* EAFNOSUPPORT */
|
|
|
|
if (af == AF_INET) {
|
|
|
|
memset(dst, 0, sizeof(struct in_addr));
|
|
|
|
g_critical("ws_inet_pton4: EAFNOSUPPORT");
|
|
|
|
}
|
|
|
|
else if (af == AF_INET6) {
|
|
|
|
memset(dst, 0, sizeof(struct in6_addr));
|
|
|
|
g_critical("ws_inet_pton6: EAFNOSUPPORT");
|
|
|
|
}
|
|
|
|
else {
|
2021-05-23 23:46:43 +00:00
|
|
|
ws_assert_not_reached();
|
2017-10-30 08:42:26 +00:00
|
|
|
}
|
|
|
|
errno = EAFNOSUPPORT;
|
|
|
|
}
|
2016-02-10 09:11:12 +00:00
|
|
|
return ret == 1;
|
|
|
|
}
|
|
|
|
|
2017-10-26 07:51:55 +00:00
|
|
|
static inline const gchar *
|
|
|
|
_inet_ntop(int af, gconstpointer src, gchar *dst, guint dst_size)
|
|
|
|
{
|
|
|
|
const gchar *ret = inet_ntop(af, _NTOP_SRC_CAST_ src, dst, dst_size);
|
2017-10-30 08:42:26 +00:00
|
|
|
if (G_UNLIKELY(ret == NULL)) {
|
|
|
|
int saved_errno = errno;
|
|
|
|
gchar *errmsg = "<<ERROR>>";
|
|
|
|
switch (errno) {
|
|
|
|
case EAFNOSUPPORT:
|
|
|
|
errmsg = "<<EAFNOSUPPORT>>";
|
|
|
|
g_critical("ws_inet_ntop: EAFNOSUPPORT");
|
|
|
|
break;
|
|
|
|
case ENOSPC:
|
|
|
|
errmsg = "<<ENOSPC>>";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2017-10-29 19:22:39 +00:00
|
|
|
/* set result to something that can't be confused with a valid conversion */
|
2021-04-30 10:18:25 +00:00
|
|
|
(void) g_strlcpy(dst, errmsg, dst_size);
|
2017-10-29 19:22:39 +00:00
|
|
|
/* set errno for caller */
|
2017-10-30 08:42:26 +00:00
|
|
|
errno = saved_errno;
|
2017-10-29 19:22:39 +00:00
|
|
|
}
|
|
|
|
return dst;
|
2017-10-26 07:51:55 +00:00
|
|
|
}
|
|
|
|
|
2016-02-10 09:11:12 +00:00
|
|
|
const gchar *
|
2016-02-19 16:57:39 +00:00
|
|
|
ws_inet_ntop4(gconstpointer src, gchar *dst, guint dst_size)
|
2016-02-10 09:11:12 +00:00
|
|
|
{
|
2017-10-26 07:51:55 +00:00
|
|
|
return _inet_ntop(AF_INET, src, dst, dst_size);
|
2016-02-10 09:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
ws_inet_pton4(const gchar *src, guint32 *dst)
|
|
|
|
{
|
|
|
|
return _inet_pton(AF_INET, src, dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
const gchar *
|
2016-02-19 16:57:39 +00:00
|
|
|
ws_inet_ntop6(gconstpointer src, gchar *dst, guint dst_size)
|
2016-02-10 09:11:12 +00:00
|
|
|
{
|
2017-10-26 07:51:55 +00:00
|
|
|
return _inet_ntop(AF_INET6, src, dst, dst_size);
|
2016-02-10 09:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
2017-10-26 08:50:00 +00:00
|
|
|
ws_inet_pton6(const gchar *src, ws_in6_addr *dst)
|
2016-02-10 09:11:12 +00:00
|
|
|
{
|
|
|
|
return _inet_pton(AF_INET6, src, dst);
|
|
|
|
}
|