diff options
author | João Valverde <joao.valverde@tecnico.ulisboa.pt> | 2017-10-26 08:51:55 +0100 |
---|---|---|
committer | João Valverde <j@v6e.pt> | 2017-10-26 14:39:00 +0000 |
commit | ac804b59e242c57e225c390eb8d6bec359a7003f (patch) | |
tree | 914f047f0e08d253c284380b6a4960cabd2e2c69 | |
parent | 296a36698b9ff2ab1beb3c21dc8409c3a276001c (diff) |
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>
-rw-r--r-- | dumpcap.c | 17 | ||||
-rw-r--r-- | epan/address_types.c | 11 | ||||
-rw-r--r-- | ws_attributes.h | 8 | ||||
-rw-r--r-- | wsutil/inet_addr.c | 22 | ||||
-rw-r--r-- | wsutil/inet_addr.h | 10 | ||||
-rw-r--r-- | wsutil/interface.c | 4 |
6 files changed, 43 insertions, 29 deletions
@@ -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); diff --git a/epan/address_types.c b/epan/address_types.c index d6be4e63cf..8b43fa7469 100644 --- a/epan/address_types.c +++ b/epan/address_types.c @@ -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; diff --git a/ws_attributes.h b/ws_attributes.h index d07ae75324..488a9fabab 100644 --- a/ws_attributes.h +++ b/ws_attributes.h @@ -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 */ diff --git a/wsutil/inet_addr.c b/wsutil/inet_addr.c index 505d029c01..b667c6d17a 100644 --- a/wsutil/inet_addr.c +++ b/wsutil/inet_addr.c @@ -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 diff --git a/wsutil/inet_addr.h b/wsutil/inet_addr.h index 3fced32126..078acc7afa 100644 --- a/wsutil/inet_addr.h +++ b/wsutil/inet_addr.h @@ -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 diff --git a/wsutil/interface.c b/wsutil/interface.c index 45921ccb52..57f79c2bdf 100644 --- a/wsutil/interface.c +++ b/wsutil/interface.c @@ -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: |