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>
This commit is contained in:
João Valverde 2017-10-29 11:25:30 +00:00 committed by João Valverde
parent de1b26a3c6
commit 7507b11ec4
3 changed files with 46 additions and 26 deletions

View File

@ -247,8 +247,7 @@ static int ipv4_name_res_len(void)
******************************************************************************/
static int ipv6_to_str(const address* addr, gchar *buf, int buf_len)
{
ip6_to_str_buf((const ws_in6_addr *)addr->data, buf, buf_len);
return (int)(strlen(buf)+1);
return ip6_to_str_buf((const ws_in6_addr *)addr->data, buf, buf_len);
}
static int ipv6_str_len(const address* addr _U_)
@ -436,25 +435,19 @@ static int eui64_len(void)
* AT_IB
******************************************************************************/
static int
ib_addr_to_str( const address *addr, gchar *buf, int buf_len){
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))
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;
memcpy((void *)&lid_number, addr->data, sizeof lid_number);
g_snprintf(buf,buf_len,"LID: %u",lid_number);
return ip6_to_str_buf_with_pfx((const ws_in6_addr *)addr->data, buf, buf_len, "GID: ");
}
return sizeof(buf) + 1;
/* this is a LID (16 bits) */
guint16 lid_number;
memcpy((void *)&lid_number, addr->data, sizeof lid_number);
g_snprintf(buf,buf_len,"LID: %u",lid_number);
return sizeof(buf) + 1; // XXX this looks all kinds of wrong
}
static int ib_str_len(const address* addr _U_)

View File

@ -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;
ws_inet_ntop6(ad, buf, buf_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;
/* 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 *

View File

@ -90,7 +90,12 @@ WS_DLL_PUBLIC void address_to_str_buf(const address *addr, gchar *buf, int b
#define tvb_eui64_to_str(tvb, offset) tvb_address_to_str(wmem_packet_scope(), tvb, AT_EUI64, offset)
void ip_to_str_buf(const guint8 *ad, gchar *buf, const int buf_len);
void ip6_to_str_buf(const ws_in6_addr *, gchar *, int buf_len);
/* Returns length of the result. */
int ip6_to_str_buf(const ws_in6_addr *ad, gchar *buf, int buf_size);
/* Returns length of the result. Takes a prefix to be inserted before the address. */
int ip6_to_str_buf_with_pfx(const ws_in6_addr *ad, gchar *buf, int buf_size, const char *prefix);
extern gchar* ipxnet_to_str_punct(wmem_allocator_t *scope, const guint32 ad, const char punct);
WS_DLL_PUBLIC gchar* eui64_to_str(wmem_allocator_t *scope, const guint64 ad);