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"
|
2021-06-14 23:06:02 +00:00
|
|
|
#define WS_LOG_DOMAIN LOG_DOMAIN_WSUTIL
|
2021-12-27 13:28:29 +00:00
|
|
|
#include "inet_addr.h"
|
2016-02-10 09:11:12 +00:00
|
|
|
|
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-12-27 13:28:29 +00:00
|
|
|
#include "str_util.h"
|
2021-05-23 23:46:43 +00:00
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
2021-12-27 13:28:29 +00:00
|
|
|
static inline bool
|
|
|
|
inet_pton_internal(int af, const char *src, void *dst, size_t dst_size,
|
|
|
|
const char *af_str)
|
2016-02-10 09:11:12 +00:00
|
|
|
{
|
2021-12-27 13:28:29 +00:00
|
|
|
int ret = inet_pton(af, src, dst);
|
|
|
|
if (ret < 0) {
|
|
|
|
int err = errno;
|
|
|
|
ws_log(WS_LOG_DOMAIN, LOG_LEVEL_CRITICAL, "inet_pton: %s (%d): %s", af_str, af, g_strerror(err));
|
|
|
|
memset(dst, 0, dst_size);
|
|
|
|
errno = err;
|
|
|
|
return false;
|
2017-10-30 08:42:26 +00:00
|
|
|
}
|
2021-12-27 13:28:29 +00:00
|
|
|
/* ret == 0 invalid src representation, ret == 1 success. */
|
2016-02-10 09:11:12 +00:00
|
|
|
return ret == 1;
|
|
|
|
}
|
|
|
|
|
2021-12-27 13:28:29 +00:00
|
|
|
static inline const char *
|
|
|
|
inet_ntop_internal(int af, const void *src, char *dst, size_t dst_size,
|
|
|
|
const char *af_str)
|
2017-10-26 07:51:55 +00:00
|
|
|
{
|
2021-12-27 21:37:56 +00:00
|
|
|
/* Add a cast to ignore 64-to-32 bit narrowing warnings with some
|
|
|
|
* compilers (POSIX uses socklen_t instead of size_t). */
|
|
|
|
const char *ret = inet_ntop(af, _NTOP_SRC_CAST_ src, dst, (unsigned int)dst_size);
|
2021-12-27 13:28:29 +00:00
|
|
|
if (ret == NULL) {
|
|
|
|
int err = errno;
|
|
|
|
char errbuf[16];
|
|
|
|
ws_log(WS_LOG_DOMAIN, LOG_LEVEL_CRITICAL, "inet_ntop: %s (%d): %s", af_str, af, g_strerror(err));
|
2017-10-29 19:22:39 +00:00
|
|
|
/* set result to something that can't be confused with a valid conversion */
|
2021-12-27 13:28:29 +00:00
|
|
|
(void)g_strlcpy(dst, ws_strerrorname_r(err, errbuf, sizeof(errbuf)), dst_size);
|
|
|
|
errno = err;
|
|
|
|
return dst;
|
2017-10-29 19:22:39 +00:00
|
|
|
}
|
|
|
|
return dst;
|
2017-10-26 07:51:55 +00:00
|
|
|
}
|
|
|
|
|
2021-12-27 13:28:29 +00:00
|
|
|
const char *
|
|
|
|
ws_inet_ntop4(const void *src, char *dst, size_t dst_size)
|
2016-02-10 09:11:12 +00:00
|
|
|
{
|
2021-12-27 13:28:29 +00:00
|
|
|
return inet_ntop_internal(AF_INET, src, dst, dst_size, "AF_INET");
|
2016-02-10 09:11:12 +00:00
|
|
|
}
|
|
|
|
|
2021-12-27 13:28:29 +00:00
|
|
|
bool
|
|
|
|
ws_inet_pton4(const char *src, ws_in4_addr *dst)
|
2016-02-10 09:11:12 +00:00
|
|
|
{
|
2021-12-27 13:28:29 +00:00
|
|
|
return inet_pton_internal(AF_INET, src, dst, sizeof(*dst), "AF_INET");
|
2016-02-10 09:11:12 +00:00
|
|
|
}
|
|
|
|
|
2021-12-27 13:28:29 +00:00
|
|
|
const char *
|
|
|
|
ws_inet_ntop6(const void *src, char *dst, size_t dst_size)
|
2016-02-10 09:11:12 +00:00
|
|
|
{
|
2021-12-27 13:28:29 +00:00
|
|
|
return inet_ntop_internal(AF_INET6, src, dst, dst_size, "AF_INET6");
|
2016-02-10 09:11:12 +00:00
|
|
|
}
|
|
|
|
|
2021-12-27 13:28:29 +00:00
|
|
|
bool
|
|
|
|
ws_inet_pton6(const char *src, ws_in6_addr *dst)
|
2016-02-10 09:11:12 +00:00
|
|
|
{
|
2021-12-27 13:28:29 +00:00
|
|
|
return inet_pton_internal(AF_INET6, src, dst, sizeof(*dst), "AF_INET6");
|
2016-02-10 09:11:12 +00:00
|
|
|
}
|