Improve our inet_ntop() wrapper

Also fix buffer length define, as it is not guaranteed to be 46 on
Windows (it never was guaranteed anyway for the libc implementation,
but the likelyhood of being greater was small).

Change-Id: I2db705d86f825765ed32ec70b8d22058b5d629e8
Reviewed-on: https://code.wireshark.org/review/24074
Reviewed-by: João Valverde <j@v6e.pt>
This commit is contained in:
João Valverde 2017-10-26 08:51:55 +01:00 committed by João Valverde
parent 296a36698b
commit ac804b59e2
6 changed files with 43 additions and 29 deletions

View File

@ -818,7 +818,6 @@ capture_interface_list(int *err, char **err_str, void(*update_cb)(void) _U_)
return get_interface_list(err, err_str);
}
#define ADDRSTRLEN 46 /* Covers IPv4 & IPv6 */
/*
* Output a machine readable list of the interfaces
* This list is retrieved by the sync_interface_list_open() function
@ -832,7 +831,7 @@ print_machine_readable_interfaces(GList *if_list)
if_info_t *if_info;
GSList *addr;
if_addr_t *if_addr;
char addr_str[ADDRSTRLEN];
char addr_str[WS_INET6_ADDRSTRLEN];
if (capture_child) {
/* Let our parent know we succeeded. */
@ -872,20 +871,10 @@ print_machine_readable_interfaces(GList *if_list)
if_addr = (if_addr_t *)addr->data;
switch(if_addr->ifat_type) {
case IF_AT_IPv4:
if (ws_inet_ntop4(&if_addr->addr.ip4_addr, addr_str,
ADDRSTRLEN)) {
printf("%s", addr_str);
} else {
printf("<unknown IPv4>");
}
printf("%s", ws_inet_ntop4(&if_addr->addr.ip4_addr, addr_str, sizeof(addr_str)));
break;
case IF_AT_IPv6:
if (ws_inet_ntop6(&if_addr->addr.ip6_addr,
addr_str, ADDRSTRLEN)) {
printf("%s", addr_str);
} else {
printf("<unknown IPv6>");
}
printf("%s", ws_inet_ntop6(&if_addr->addr.ip6_addr, addr_str, sizeof(addr_str)));
break;
default:
printf("<type unknown %i>", if_addr->ifat_type);

View File

@ -439,11 +439,14 @@ static int
ib_addr_to_str( const address *addr, gchar *buf, int buf_len){
if (addr->len >= 16) { /* GID is 128bits */
#define PREAMBLE_STR_LEN ((int)(sizeof("GID: ") - 1))
g_strlcpy(buf, "GID: ", buf_len);
if (buf_len < PREAMBLE_STR_LEN ||
ws_inet_ntop6(addr->data, buf + PREAMBLE_STR_LEN,
buf_len - PREAMBLE_STR_LEN) == NULL ) /* Returns NULL if no space and does not touch buf */
gchar addr_buf[WS_INET6_ADDRSTRLEN];
ws_inet_ntop6(addr->data, addr_buf, sizeof(addr_buf));
if (buf_len < PREAMBLE_STR_LEN + (int)strlen(addr_buf) + 1) {
g_strlcpy(buf, BUF_TOO_SMALL_ERR, buf_len); /* Let the unexpected value alert user */
} else {
g_snprintf(buf, buf_len, "GID: %s", addr_buf);
}
} else { /* this is a LID (16 bits) */
guint16 lid_number;

View File

@ -52,6 +52,14 @@ extern "C" {
#define WS_NORETURN
#endif
/* Hint to the compiler that the function returns a non-null value */
#if defined(__GNUC__)
/* This includes clang */
#define WS_RETNONNULL __attribute__((returns_nonnull))
#else
#define WS_RETNONNULL
#endif
#ifdef __cplusplus
}
#endif /* __cplusplus */

View File

@ -42,20 +42,32 @@
#define _NTOP_SRC_CAST_
#endif
/*
* We only employ and require AF_INET/AF_INET6, so we can
* have some stronger checks for correctness and convenience. It is a
* programming error to pass a too-small buffer to inet_ntop.
*/
static inline gboolean
_inet_pton(int af, const gchar *src, gpointer dst)
{
gint ret;
ret = inet_pton(af, src, dst);
gint ret = inet_pton(af, src, dst);
g_assert(ret >= 0);
return ret == 1;
}
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);
g_assert(ret != NULL);
return ret;
}
const gchar *
ws_inet_ntop4(gconstpointer src, gchar *dst, guint dst_size)
{
return inet_ntop(AF_INET, _NTOP_SRC_CAST_ src, dst, dst_size);
return _inet_ntop(AF_INET, src, dst, dst_size);
}
gboolean
@ -67,7 +79,7 @@ ws_inet_pton4(const gchar *src, guint32 *dst)
const gchar *
ws_inet_ntop6(gconstpointer src, gchar *dst, guint dst_size)
{
return inet_ntop(AF_INET6, _NTOP_SRC_CAST_ src, dst, dst_size);
return _inet_ntop(AF_INET6, src, dst, dst_size);
}
gboolean

View File

@ -23,21 +23,23 @@
#define __WS_INET_ADDR_H__
#include "ws_symbol_export.h"
#include "ws_attributes.h"
#include <glib.h>
#include "inet_ipv6.h"
#define WS_INET6_ADDRSTRLEN 46
/* Choose a buffer size big enough for all implementations */
#define WS_INET_ADDRSTRLEN 30
#define WS_INET6_ADDRSTRLEN 80
WS_DLL_PUBLIC const gchar *
WS_DLL_PUBLIC WS_RETNONNULL const gchar *
ws_inet_ntop4(gconstpointer src, gchar *dst, guint dst_size);
WS_DLL_PUBLIC gboolean
ws_inet_pton4(const gchar *src, guint32 *dst);
WS_DLL_PUBLIC const gchar *
WS_DLL_PUBLIC WS_RETNONNULL const gchar *
ws_inet_ntop6(gconstpointer src, gchar *dst, guint dst_size);
WS_DLL_PUBLIC gboolean

View File

@ -64,7 +64,7 @@ static GSList* local_interfaces_to_list_nix(void)
struct ifaddrs *ifap;
struct ifaddrs *ifa;
int family;
char ip[INET6_ADDRSTRLEN];
char ip[WS_INET6_ADDRSTRLEN];
if (getifaddrs(&ifap)) {
goto end;
@ -76,7 +76,7 @@ static GSList* local_interfaces_to_list_nix(void)
family = ifa->ifa_addr->sa_family;
memset(ip, 0x0, INET6_ADDRSTRLEN);
memset(ip, 0x0, WS_INET6_ADDRSTRLEN);
switch (family) {
case AF_INET: