diff options
author | João Valverde <joao.valverde@tecnico.ulisboa.pt> | 2016-06-01 22:22:46 +0100 |
---|---|---|
committer | Michael Mann <mmann78@netscape.net> | 2016-06-06 22:00:41 +0000 |
commit | f1ff3208a055f462e69c9ac7479271aecf784d79 (patch) | |
tree | 51809fa94142090dd57fbd7d006c60d1016da4b2 | |
parent | 528edba87ae6a6ffd951bdd137421910ea88d964 (diff) |
Move ip6_to_str_buf() to to_str.c and make it take the buffer length.
Also make it use ws_inet_ntop6() (rather than implementing the string
conversion ourselves).
Remove ip6_to_str_buf_len().
Change-Id: I1eff3a8941e00987c2ff0c4dcfda13476af86191
Reviewed-on: https://code.wireshark.org/review/15692
Reviewed-by: João Valverde <j@v6e.pt>
Reviewed-by: Michael Mann <mmann78@netscape.net>
-rw-r--r-- | epan/addr_resolv.c | 2 | ||||
-rw-r--r-- | epan/address_types.c | 119 | ||||
-rw-r--r-- | epan/dissectors/packet-radius.c | 2 | ||||
-rw-r--r-- | epan/dissectors/packet-wccp.c | 2 | ||||
-rw-r--r-- | epan/ftypes/ftype-ipv6.c | 10 | ||||
-rw-r--r-- | epan/to_str.c | 11 | ||||
-rw-r--r-- | epan/to_str.h | 5 | ||||
-rw-r--r-- | wiretap/wtap.h | 2 | ||||
-rw-r--r-- | wsutil/inet_addr.h | 3 |
9 files changed, 26 insertions, 130 deletions
diff --git a/epan/addr_resolv.c b/epan/addr_resolv.c index 2d003a70f2..936c43d0e8 100644 --- a/epan/addr_resolv.c +++ b/epan/addr_resolv.c @@ -828,7 +828,7 @@ new_ipv6(const struct e_in6_addr *addr) memcpy(tp->addr, addr->bytes, sizeof tp->addr); tp->flags = 0; tp->name[0] = '\0'; - ip6_to_str_buf(addr, tp->ip6); + ip6_to_str_buf(addr, tp->ip6, sizeof(tp->ip6)); return tp; } diff --git a/epan/address_types.c b/epan/address_types.c index f7da4deeeb..52d2639430 100644 --- a/epan/address_types.c +++ b/epan/address_types.c @@ -230,126 +230,9 @@ static int ipv4_name_res_len(void) /****************************************************************************** * AT_IPv6 ******************************************************************************/ -/* const char * - * inet_ntop6(src, dst, size) - * convert IPv6 binary address into presentation (printable) format - * author: - * Paul Vixie, 1996. - */ -static void -ip6_to_str_buf_len(const guchar* src, char *buf, int buf_len) -{ - struct { int base, len; } best, cur; - guint words[8]; - int i; - - if (buf_len < MAX_IP6_STR_LEN) { /* buf_len < 40 */ - g_strlcpy(buf, BUF_TOO_SMALL_ERR, buf_len); /* Let the unexpected value alert user */ - return; - } - - /* - * Preprocess: - * Copy the input (bytewise) array into a wordwise array. - * Find the longest run of 0x00's in src[] for :: shorthanding. - */ - for (i = 0; i < 16; i += 2) { - words[i / 2] = (src[i+1] << 0); - words[i / 2] |= (src[i] << 8); - } - best.base = -1; best.len = 0; - cur.base = -1; cur.len = 0; - for (i = 0; i < 8; i++) { - if (words[i] == 0) { - if (cur.base == -1) { - cur.base = i; - cur.len = 1; - } else - cur.len++; - } else { - if (cur.base != -1) { - if (best.base == -1 || cur.len > best.len) - best = cur; - cur.base = -1; - } - } - } - if (cur.base != -1) { - if (best.base == -1 || cur.len > best.len) - best = cur; - } - if (best.base != -1 && best.len < 2) - best.base = -1; - - /* Is this address an encapsulated IPv4? */ - /* XXX, - * Orginal code dated 1996 uses ::/96 as a valid IPv4-compatible addresses - * but since Feb 2006 ::/96 is deprecated one. - * Quoting wikipedia [0]: - * > The 96-bit zero-value prefix ::/96, originally known as IPv4-compatible - * > addresses, was mentioned in 1995[35] but first described in 1998.[41] - * > This class of addresses was used to represent IPv4 addresses within - * > an IPv6 transition technology. Such an IPv6 address has its first - * > (most significant) 96 bits set to zero, while its last 32 bits are the - * > IPv4 address that is represented. - * > In February 2006 the Internet Engineering Task Force (IETF) has deprecated - * > the use of IPv4-compatible addresses.[1] The only remaining use of this address - * > format is to represent an IPv4 address in a table or database with fixed size - * > members that must also be able to store an IPv6 address. - * - * If needed it can be fixed by changing next line: - * if (best.base == 0 && (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) - * to: - * if (best.base == 0 && best.len == 5 && words[5] == 0xffff) - * - * [0] http://en.wikipedia.org/wiki/IPv6_address#Historical_notes - */ - - if (best.base == 0 && (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) - { - /* best.len == 6 -> ::IPv4; 5 -> ::ffff:IPv4 */ - buf = g_stpcpy(buf, "::"); - if (best.len == 5) - buf = g_stpcpy(buf, "ffff:"); - ip_to_str_buf(src + 12, buf, MAX_IP_STR_LEN); - /* max: 2 + 5 + 16 == 23 bytes */ - return; - } - - /* - * Format the result. - */ - for (i = 0; i < 8; i++) { - /* Are we inside the best run of 0x00's? */ - if (i == best.base) { - *buf++ = ':'; - i += best.len; - - /* Was it a trailing run of 0x00's? */ - if (i == 8) { - *buf++ = ':'; - break; - } - } - /* Are we following an initial run of 0x00s or any real hex? */ - if (i != 0) - *buf++ = ':'; - - buf = word_to_hex_npad(buf, words[i]); /* max: 4B */ - /* max: 8 * 4 + 7 == 39 bytes */ - } - *buf = '\0'; /* 40 byte */ -} - -void -ip6_to_str_buf(const struct e_in6_addr *ad, gchar *buf) -{ - ip6_to_str_buf_len((const guchar*)ad, buf, MAX_IP6_STR_LEN); -} - static int ipv6_to_str(const address* addr, gchar *buf, int buf_len) { - ip6_to_str_buf_len((const guchar*)addr->data, buf, buf_len); + ip6_to_str_buf((const struct e_in6_addr *)addr->data, buf, buf_len); return (int)(strlen(buf)+1); } diff --git a/epan/dissectors/packet-radius.c b/epan/dissectors/packet-radius.c index bf0671b14e..babba057c5 100644 --- a/epan/dissectors/packet-radius.c +++ b/epan/dissectors/packet-radius.c @@ -1139,7 +1139,7 @@ radius_ipv6prefix(radius_attr_info_t *a, proto_tree *tree, packet_info *pinfo _U /* cannot use tvb_get_ipv6() here, since the prefix most likely is truncated */ memset(&ipv6_buff, 0, sizeof ipv6_buff); tvb_memcpy(tvb, &ipv6_buff, offset + 2, len - 2); - ip6_to_str_buf(&ipv6_buff, txtbuf); + ip6_to_str_buf(&ipv6_buff, txtbuf, sizeof(txtbuf)); proto_item_append_text(avp_item, "%s/%u", txtbuf, n); } diff --git a/epan/dissectors/packet-wccp.c b/epan/dissectors/packet-wccp.c index 9c9eb00e29..81a1eea8d1 100644 --- a/epan/dissectors/packet-wccp.c +++ b/epan/dissectors/packet-wccp.c @@ -616,7 +616,7 @@ static void wccp_fmt_ipaddress(gchar *buffer, guint32 host_addr, wccp_address_ta /* ok get the IP */ if (addr_table->table_ipv6 != NULL) { - ip6_to_str_buf(&(addr_table->table_ipv6[addr_index-1]), buffer); + ip6_to_str_buf(&(addr_table->table_ipv6[addr_index-1]), buffer, ITEM_LABEL_LENGTH); return; } else { diff --git a/epan/ftypes/ftype-ipv6.c b/epan/ftypes/ftype-ipv6.c index 39b45578fc..432f3f0f41 100644 --- a/epan/ftypes/ftype-ipv6.c +++ b/epan/ftypes/ftype-ipv6.c @@ -25,6 +25,7 @@ #include <ftypes-int.h> #include <epan/ipv6.h> #include <epan/addr_resolv.h> +#include <epan/to_str.h> static void ipv6_fvalue_set(fvalue_t *fv, const guint8 *value) @@ -93,16 +94,13 @@ ipv6_from_unparsed(fvalue_t *fv, const char *s, gboolean allow_partial_value _U_ static int ipv6_repr_len(fvalue_t *fv _U_, ftrepr_t rtype _U_, int field_display _U_) { - /* - * 39 characters for "XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX". - */ - return 39; + return MAX_IP6_STR_LEN; } static void -ipv6_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, int field_display _U_, char *buf, unsigned int size _U_) +ipv6_to_repr(fvalue_t *fv, ftrepr_t rtype _U_, int field_display _U_, char *buf, unsigned int size) { - ip6_to_str_buf(&(fv->value.ipv6.addr), buf); + ip6_to_str_buf(&(fv->value.ipv6.addr), buf, size); } static gpointer diff --git a/epan/to_str.c b/epan/to_str.c index e70547c0f2..ca4fa907f9 100644 --- a/epan/to_str.c +++ b/epan/to_str.c @@ -1033,6 +1033,17 @@ ip_to_str_buf(const guint8 *ad, gchar *buf, const int buf_len) *b=0; } +void +ip6_to_str_buf(const struct e_in6_addr *ad, gchar *buf, int buf_len) +{ + if (buf_len < WS_INET6_ADDRSTRLEN) { + g_strlcpy(buf, BUF_TOO_SMALL_ERR, buf_len); /* Let the unexpected value alert user */ + return; + } + + ws_inet_ntop6(ad, buf, buf_len); +} + gchar * guid_to_str(wmem_allocator_t *scope, const e_guid_t *guid) { diff --git a/epan/to_str.h b/epan/to_str.h index 36849e0b84..2377dde425 100644 --- a/epan/to_str.h +++ b/epan/to_str.h @@ -26,6 +26,7 @@ #include <glib.h> #include "wsutil/nstime.h" +#include <wsutil/inet_addr.h> #include "time_fmt.h" #include <epan/packet_info.h> #include <epan/ipv6.h> @@ -34,7 +35,7 @@ #define GUID_STR_LEN 37 #define MAX_IP_STR_LEN 16 -#define MAX_IP6_STR_LEN 40 +#define MAX_IP6_STR_LEN WS_INET6_ADDRSTRLEN #define MAX_ADDR_STR_LEN 256 #define VINES_ADDR_LEN 6 #define EUI64_STR_LEN 24 @@ -91,8 +92,8 @@ 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 struct e_in6_addr *, gchar *, int buf_len); -void ip6_to_str_buf(const struct e_in6_addr *, gchar *); 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); diff --git a/wiretap/wtap.h b/wiretap/wtap.h index 2a4d3ddb0a..6d257ea002 100644 --- a/wiretap/wtap.h +++ b/wiretap/wtap.h @@ -1321,7 +1321,7 @@ typedef struct hashipv4 { typedef struct hashipv6 { guint8 addr[16]; guint8 flags; /* B0 dummy_entry, B1 resolve, B2 If the address is used in the trace */ - gchar ip6[40]; + gchar ip6[46]; gchar name[MAXNAMELEN]; } hashipv6_t; diff --git a/wsutil/inet_addr.h b/wsutil/inet_addr.h index 1c9d1ab5cc..f0e10db100 100644 --- a/wsutil/inet_addr.h +++ b/wsutil/inet_addr.h @@ -28,6 +28,9 @@ #include "inet_ipv6.h" +#define WS_INET6_ADDRSTRLEN 46 + + WS_DLL_PUBLIC const gchar * ws_inet_ntop4(gconstpointer src, gchar *dst, guint dst_size); |