diff options
author | Guy Harris <guy@alum.mit.edu> | 2017-10-29 01:25:24 -0700 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2017-10-29 08:26:11 +0000 |
commit | a25af02ef7c3dee6d83f5d61fdb62ec8ebec971c (patch) | |
tree | 63895b4273b887abce138c0ea5b7de36376e2d69 | |
parent | 930f6345157659fca89d922612bfbb69d8a8b53e (diff) |
Clean up processing of encoded addresses.
Allocate the buffer at the point we fill it in, and pick the appropriate
size or let the wmem_strXXX routine do it for us.
If we aren't using an address table, just fetch the value as an IPv4
address and hand it to ip_to_str_buf() - don't fetch it in host byte
order and then *fix* it by byte-swapping (hint: on a big-endian machine,
host byte order *is* network byte order and you don't want to swap it;
not all the world's an x86).
Change-Id: I966b107271ba166ff76a5600fbc4922808e7ead1
Reviewed-on: https://code.wireshark.org/review/24159
Reviewed-by: Guy Harris <guy@alum.mit.edu>
-rw-r--r-- | epan/dissectors/packet-wccp.c | 147 |
1 files changed, 70 insertions, 77 deletions
diff --git a/epan/dissectors/packet-wccp.c b/epan/dissectors/packet-wccp.c index d4f59ba526..d031196c00 100644 --- a/epan/dissectors/packet-wccp.c +++ b/epan/dissectors/packet-wccp.c @@ -549,86 +549,92 @@ find_wccp_address_table(tvbuff_t *tvb, int offset, we need to fix that */ -static void wccp_fmt_ipaddress(gchar *buffer, guint32 host_addr, wccp_address_table* addr_table) +static const gchar * decode_wccp_encoded_address(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, + proto_tree *info_tree _U_, wccp_address_table* addr_table) { + guint32 host_addr; + gchar *buffer; + /* are we using an address table? */ if (!addr_table->in_use) { - /* no return the IPv4 IP */ - /* first fix the byte order */ - guint addr= GUINT32_SWAP_LE_BE(host_addr); - - ip_to_str_buf( (guint8 *) &addr, buffer, ITEM_LABEL_LENGTH); - return; + /* no; return the IPv4 IP */ + host_addr = tvb_get_ipv4(tvb,offset); + buffer = (char *) wmem_alloc(wmem_packet_scope(), WS_INET_ADDRSTRLEN); + ip_to_str_buf( (guint8 *) &host_addr, buffer, WS_INET_ADDRSTRLEN); } else { - /* we need to decode the encoded address: */ - guint16 reserv = (host_addr & 0xFFFF0000) >> 16; - guint16 addr_index = (host_addr & 0x0000FFFF); + /* yes; we need to decode the encoded address */ + guint16 reserv; + guint16 addr_index; + + host_addr = tvb_get_ntohl(tvb,offset); + reserv = (host_addr & 0xFFFF0000) >> 16; + addr_index = (host_addr & 0x0000FFFF); if (reserv != 0) { - g_snprintf(buffer, ITEM_LABEL_LENGTH, "INVALID: reserved part non zero"); - return; + buffer = wmem_strdup(wmem_packet_scope(), "INVALID: reserved part non zero"); } + else { + /* now check if it's IPv4 or IPv6 we need to print */ + switch (addr_table->family) { + case 1: + /* IPv4 */ + + /* special case: index 0 -> undefined IP */ + if (addr_index == 0) { + buffer = wmem_strdup(wmem_packet_scope(), "0.0.0.0"); + break; + } + /* are we be beyond the end of the table? */ + if (addr_index > addr_table->table_length) { + buffer = wmem_strdup_printf(wmem_packet_scope(), "INVALID IPv4 index: %d > %d", + addr_index, addr_table->table_length); + break; + } - /* now check if it's IPv4 or IPv6 we need to print */ - switch (addr_table->family) { - case 1: - /* IPv4 */ - - /* special case: index 0 -> undefined IP */ - if (addr_index == 0) { - g_snprintf(buffer, ITEM_LABEL_LENGTH, "0.0.0.0"); - return; - } - /* are we be beyond the end of the table? */ - if (addr_index > addr_table->table_length) { - g_snprintf(buffer, ITEM_LABEL_LENGTH, "INVALID IPv4 index: %d > %d", - addr_index, addr_table->table_length); - return; - } - - /* ok get the IP */ - if (addr_table->table_ipv4 != NULL) { - ip_to_str_buf( (guint8 *) &(addr_table->table_ipv4[addr_index-1]), buffer, ITEM_LABEL_LENGTH); - return; - } - else { - g_snprintf(buffer, ITEM_LABEL_LENGTH, "INVALID IPv4 table empty!"); - return; - } - break; - case 2: - /* IPv6 */ - /* special case: index 0 -> undefined IP */ - if (addr_index == 0) { - g_snprintf(buffer, ITEM_LABEL_LENGTH, "::"); - return; - } + /* ok get the IP */ + if (addr_table->table_ipv4 != NULL) { + buffer = (char *) wmem_alloc(wmem_packet_scope(), WS_INET_ADDRSTRLEN); + ip_to_str_buf( (guint8 *) &(addr_table->table_ipv4[addr_index-1]), buffer, WS_INET_ADDRSTRLEN); + } + else { + buffer = wmem_strdup(wmem_packet_scope(), "INVALID IPv4 table empty!"); + } + break; + case 2: + /* IPv6 */ + /* special case: index 0 -> undefined IP */ + if (addr_index == 0) { + buffer = wmem_strdup(wmem_packet_scope(), "::"); + break; + } - /* are we be beyond the end of the table? */ - if (addr_index > addr_table->table_length) { - g_snprintf(buffer, ITEM_LABEL_LENGTH, "INVALID IPv6 index: %d > %d", - addr_index, addr_table->table_length); - return; - } + /* are we be beyond the end of the table? */ + if (addr_index > addr_table->table_length) { + buffer = wmem_strdup_printf(wmem_packet_scope(), "INVALID IPv6 index: %d > %d", + addr_index, addr_table->table_length); + break; + } - /* ok get the IP */ - if (addr_table->table_ipv6 != NULL) { - ip6_to_str_buf(&(addr_table->table_ipv6[addr_index-1]), buffer, ITEM_LABEL_LENGTH); - return; - } - else { - g_snprintf(buffer, ITEM_LABEL_LENGTH, "INVALID IPv6 table empty!"); - return; + /* ok get the IP */ + if (addr_table->table_ipv6 != NULL) { + buffer = (char *) wmem_alloc(wmem_packet_scope(), WS_INET6_ADDRSTRLEN); + ip6_to_str_buf(&(addr_table->table_ipv6[addr_index-1]), buffer, WS_INET6_ADDRSTRLEN); + } + else { + buffer = wmem_strdup(wmem_packet_scope(), "INVALID IPv6 table empty!"); + } + break; + default: + buffer = wmem_strdup(wmem_packet_scope(), "INVALID IP family"); + break; } - break; - default: - g_snprintf(buffer, ITEM_LABEL_LENGTH, "INVALID IP family"); - return; } } + + return buffer; } static proto_item* wccp_add_ipaddress_item(proto_tree* tree, int hf_index, int hf_ipv4, int hf_ipv6, tvbuff_t *tvb, @@ -703,19 +709,6 @@ static proto_item* wccp_add_ipaddress_item(proto_tree* tree, int hf_index, int h #define WCCP_IP_MAX_LENGTH (WS_INET_ADDRSTRLEN > 46 ? WS_INET_ADDRSTRLEN : 46) -static const gchar * decode_wccp_encoded_address(tvbuff_t *tvb, int offset, packet_info *pinfo _U_, - proto_tree *info_tree _U_, wccp_address_table* addr_table) -{ - gchar *buffer; - guint32 host_addr; - - buffer= (char *) wmem_alloc(wmem_packet_scope(), WCCP_IP_MAX_LENGTH+1); - host_addr = tvb_get_ntohl(tvb,offset); - - wccp_fmt_ipaddress(buffer, host_addr, addr_table); - return buffer; -} - static guint dissect_hash_data(tvbuff_t *tvb, int offset, proto_tree *wccp_tree) { |