aboutsummaryrefslogtreecommitdiffstats
path: root/wsutil
diff options
context:
space:
mode:
authorJoão Valverde <joao.valverde@tecnico.ulisboa.pt>2017-10-26 08:51:55 +0100
committerJoão Valverde <j@v6e.pt>2017-10-26 14:39:00 +0000
commitac804b59e242c57e225c390eb8d6bec359a7003f (patch)
tree914f047f0e08d253c284380b6a4960cabd2e2c69 /wsutil
parent296a36698b9ff2ab1beb3c21dc8409c3a276001c (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>
Diffstat (limited to 'wsutil')
-rw-r--r--wsutil/inet_addr.c22
-rw-r--r--wsutil/inet_addr.h10
-rw-r--r--wsutil/interface.c4
3 files changed, 25 insertions, 11 deletions
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: