diff options
author | João Valverde <joao.valverde@tecnico.ulisboa.pt> | 2017-10-29 11:25:30 +0000 |
---|---|---|
committer | João Valverde <j@v6e.pt> | 2017-10-29 15:15:31 +0000 |
commit | 7507b11ec4440b8183a10113f5b33f56e6202c0e (patch) | |
tree | d3ab4591693625726d14f50dfe75f6d648985292 /epan/to_str.c | |
parent | de1b26a3c6fa27b29157e86a6da03f354586badb (diff) |
Improve our ip6_to_str_buf() implementation
Change-Id: I02b5d01797e526299a6dc5a031662cb78e4f8423
Reviewed-on: https://code.wireshark.org/review/24163
Reviewed-by: João Valverde <j@v6e.pt>
Diffstat (limited to 'epan/to_str.c')
-rw-r--r-- | epan/to_str.c | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/epan/to_str.c b/epan/to_str.c index 4c59ef9681..c6b30c6162 100644 --- a/epan/to_str.c +++ b/epan/to_str.c @@ -1043,15 +1043,37 @@ ip_to_str_buf(const guint8 *ad, gchar *buf, const int buf_len) *b=0; } -void -ip6_to_str_buf(const ws_in6_addr *ad, gchar *buf, int buf_len) +int +ip6_to_str_buf_with_pfx(const ws_in6_addr *addr, gchar *buf, int buf_size, const char *prefix) { - if (buf_len < WS_INET6_ADDRSTRLEN) { - g_strlcpy(buf, BUF_TOO_SMALL_ERR, buf_len); /* Let the unexpected value alert user */ - return; + int bytes; /* the number of bytes which would be produced if the buffer was large enough. */ + gchar addr_buf[WS_INET6_ADDRSTRLEN]; + int len; + + if (prefix == NULL) + prefix = ""; + bytes = g_snprintf(buf, buf_size, "%s%s", prefix, ws_inet_ntop6(addr, addr_buf, sizeof(addr_buf))); + len = bytes - 1; + + if (len > buf_size - 1) { /* size minus nul terminator */ + len = (int)g_strlcpy(buf, BUF_TOO_SMALL_ERR, buf_size); /* Let the unexpected value alert user */ } + return len; +} + +int +ip6_to_str_buf(const ws_in6_addr *addr, gchar *buf, int buf_size) +{ + gchar addr_buf[WS_INET6_ADDRSTRLEN]; + int len; - ws_inet_ntop6(ad, buf, buf_len); + /* slightly more efficient than ip6_to_str_buf_with_pfx(addr, buf, buf_size, NULL) */ + len = (int)g_strlcpy(buf, ws_inet_ntop6(addr, addr_buf, sizeof(addr_buf)), buf_size); /* this returns len = strlen(addr_buf) */ + + if (len > buf_size - 1) { /* size minus nul terminator */ + len = (int)g_strlcpy(buf, BUF_TOO_SMALL_ERR, buf_size); /* Let the unexpected value alert user */ + } + return len; } gchar * |