wsutil/to_str: Deprecate ip_to_str() for endian-explicit versions

ip_to_str() forces the caller to cast the argument and it's not
obvious at all that the input should be in network-byte order
for IPv4 addresses.

Deprecated the function and add endian-explicit substitutes (number
vs address).
This commit is contained in:
João Valverde 2023-10-29 10:41:00 +00:00
parent 1c2cf8064a
commit ca392f6ddc
9 changed files with 71 additions and 17 deletions

View File

@ -1052,7 +1052,7 @@ fill_dummy_ip4(const guint addr, hashipv4_t* volatile tp)
gsize i;
host_addr = addr & (~subnet_entry.mask);
ip_to_str_buf((guint8 *)&host_addr, buffer, WS_INET_ADDRSTRLEN);
ip_addr_to_str_buf(&host_addr, buffer, WS_INET_ADDRSTRLEN);
paddr = buffer;
/* Skip to first octet that is not totally masked
@ -1073,7 +1073,7 @@ fill_dummy_ip4(const guint addr, hashipv4_t* volatile tp)
snprintf(tp->name, MAXNAMELEN, "%s%s", subnet_entry.name, paddr);
} else {
/* XXX: This means we end up printing "1.2.3.4 (1.2.3.4)" in many cases */
ip_to_str_buf((const guint8 *)&addr, tp->name, MAXNAMELEN);
ip_addr_to_str_buf(&addr, tp->name, MAXNAMELEN);
}
}
@ -1122,7 +1122,7 @@ new_ipv4(const guint addr)
tp->addr = addr;
tp->flags = 0;
tp->name[0] = '\0';
ip_to_str_buf((const guint8 *)&addr, tp->ip, sizeof(tp->ip));
ip_addr_to_str_buf(&addr, tp->ip, sizeof(tp->ip));
return tp;
}

View File

@ -210,7 +210,7 @@ int ether_name_resolution_len(void)
******************************************************************************/
static int ipv4_to_str(const address* addr, gchar *buf, int buf_len)
{
ip_to_str_buf((const guint8*)addr->data, buf, buf_len);
ip_addr_to_str_buf(addr->data, buf, buf_len);
return (int)(strlen(buf)+1);
}

View File

@ -190,7 +190,7 @@ static int dissect_nano_keepalive(tvbuff_t *tvb, packet_info *pinfo, proto_tree
if (!memcmp(&ip_addr, "\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0", 16)) {
proto_item_append_text(ti, ": (none)");
} else if (!memcmp(&ip_addr, "\x0\x0\x0\x0\x0\x0\x0\x0\x0\x0\xff\xff", 12)) {
ip_to_str_buf((gchar *) &ip_addr + 12, buf, sizeof(buf));
ip_addr_to_str_buf((ws_in4_addr *)((uint8_t *)&ip_addr + 12), buf, sizeof(buf));
proto_item_append_text(ti, ": %s:%d", buf, port);
peers++;
} else {

View File

@ -479,7 +479,7 @@ dissect_rpcap_ifaddr (tvbuff_t *tvb, packet_info *pinfo,
proto_tree *tree;
proto_item *ti;
guint16 af;
guint32 ipv4;
ws_in4_addr ipv4;
ws_in6_addr ipv6;
gchar ipaddr[MAX_ADDR_STR_LEN];
@ -499,7 +499,7 @@ dissect_rpcap_ifaddr (tvbuff_t *tvb, packet_info *pinfo,
offset += 2;
ipv4 = tvb_get_ipv4 (tvb, offset);
ip_to_str_buf((guint8 *)&ipv4, ipaddr, MAX_ADDR_STR_LEN);
ip_addr_to_str_buf(&ipv4, ipaddr, MAX_ADDR_STR_LEN);
proto_item_append_text (ti, ": %s", ipaddr);
if (parent_item) {
proto_item_append_text (parent_item, ": %s", ipaddr);

View File

@ -434,7 +434,7 @@ typedef struct wccp_address_table {
gint16 family;
gint16 version;
guint16 table_length;
guint32 *table_ipv4;
ws_in4_addr *table_ipv4;
ws_in6_addr *table_ipv6;
} wccp_address_table;
@ -553,7 +553,7 @@ static const gchar * decode_wccp_encoded_address(tvbuff_t *tvb, int offset, pack
/* 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);
ip_addr_to_str_buf(&host_addr, buffer, WS_INET_ADDRSTRLEN);
}
else
{
@ -589,7 +589,7 @@ static const gchar * decode_wccp_encoded_address(tvbuff_t *tvb, int offset, pack
/* 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);
ip_addr_to_str_buf(&addr_table->table_ipv4[addr_index-1], buffer, WS_INET_ADDRSTRLEN);
}
else {
buffer = wmem_strdup(wmem_packet_scope(), "INVALID IPv4 table empty!");

View File

@ -100,8 +100,7 @@ val_to_repr(wmem_allocator_t *scope, const fvalue_t *fv, ftrepr_t rtype _U_, int
char buf[WS_INET_ADDRSTRLEN];
char *repr;
uint32_t ipv4_net_order = g_htonl(fv->value.ipv4.addr);
ip_to_str_buf((uint8_t*)&ipv4_net_order, buf, sizeof(buf));
ip_num_to_str_buf(fv->value.ipv4.addr, buf, sizeof(buf));
if (fv->value.ipv4.nmask != 0 && fv->value.ipv4.nmask != 0xffffffff)
repr = wmem_strdup_printf(scope, "%s/%d", buf, ws_count_ones(fv->value.ipv4.nmask));

View File

@ -12,6 +12,7 @@
#include <glib.h>
#include <wsutil/utf8_entities.h>
#include <wsutil/time_util.h>
#include <wsutil/to_str.h>
#include "inet_addr.h"
@ -72,6 +73,19 @@ static void test_inet_ntop6_test1(void)
g_assert_cmpstr(result, ==, in6_test1.str);
}
static void test_ip_addr_to_str_test1(void)
{
char result[WS_INET_ADDRSTRLEN];
const char *expect;
ws_in4_addr addr;
addr = g_htonl(3325256904);
expect = "198.51.100.200";
ip_addr_to_str_buf(&addr, result, sizeof(result));
g_assert_cmpstr(result, ==, expect);
}
#include "str_util.h"
static void test_format_size(void)
@ -854,6 +868,7 @@ int main(int argc, char **argv)
g_test_add_func("/to_str/uint64_to_str_back_len", test_uint64_to_str_back_len);
g_test_add_func("/to_str/int_to_str_back", test_int_to_str_back);
g_test_add_func("/to_str/int64_to_str_back", test_int64_to_str_back);
g_test_add_func("/to_str/ip_addr_to_str_test1", test_ip_addr_to_str_test1);
g_test_add_func("/nstime/from_iso8601", test_nstime_from_iso8601);

View File

@ -547,8 +547,9 @@ guint64_to_str_buf(uint64_t u, char *buf, size_t buf_len)
XXX update the address_to_str stuff to use this function.
*/
void
ip_to_str_buf(const uint8_t *ad, char *buf, const int buf_len)
ip_addr_to_str_buf(const ws_in4_addr *_ad, char *buf, const int buf_len)
{
uint8_t *ad = (uint8_t *)_ad;
register char const *p;
register char *b=buf;
@ -583,15 +584,43 @@ ip_to_str_buf(const uint8_t *ad, char *buf, const int buf_len)
*b=0;
}
char *ip_to_str(wmem_allocator_t *scope, const uint8_t *ad)
char *
ip_addr_to_str(wmem_allocator_t *scope, const ws_in4_addr *ad)
{
char *buf = wmem_alloc(scope, WS_INET_ADDRSTRLEN * sizeof(char));
ip_to_str_buf(ad, buf, WS_INET_ADDRSTRLEN);
ip_addr_to_str_buf(ad, buf, WS_INET_ADDRSTRLEN);
return buf;
}
void
ip_num_to_str_buf(uint32_t ad, char *buf, const int buf_len)
{
ws_in4_addr addr = g_htonl(ad);
ip_addr_to_str_buf(&addr, buf, buf_len);
}
/* Host byte order */
char *
ip_num_to_str(wmem_allocator_t *scope, uint32_t ad)
{
ws_in4_addr addr = g_htonl(ad);
return ip_addr_to_str(scope, &addr);
}
void
ip_to_str_buf(const uint8_t *ad, char *buf, const int buf_len)
{
ip_addr_to_str_buf((const ws_in4_addr *)ad, buf, buf_len);
}
char *
ip_to_str(wmem_allocator_t *scope, const uint8_t *ad)
{
return ip_addr_to_str(scope, (const ws_in4_addr *)ad);
}
void
ip6_to_str_buf(const ws_in6_addr *addr, char *buf, size_t buf_size)
{

View File

@ -15,7 +15,7 @@
#include <wireshark.h>
#include <wsutil/wmem/wmem.h>
#include <wsutil/inet_ipv6.h>
#include <wsutil/inet_addr.h>
#include <wsutil/nstime.h>
#ifdef __cplusplus
@ -286,11 +286,22 @@ WS_DLL_PUBLIC void guint32_to_str_buf(uint32_t u, char *buf, size_t buf_len);
WS_DLL_PUBLIC void guint64_to_str_buf(uint64_t u, char *buf, size_t buf_len);
WS_DEPRECATED_X("Use ip_num_to_str_buf() or ip_addr_to_str() instead")
WS_DLL_PUBLIC void ip_to_str_buf(const uint8_t *ad, char *buf, const int buf_len);
WS_DEPRECATED_X("Use ip_num_to_str() or ip_addr_to_str() instead")
WS_DLL_PUBLIC char *ip_to_str(wmem_allocator_t *scope, const uint8_t *ad);
/* Returns length of the result. */
/* Host byte order */
WS_DLL_PUBLIC void ip_num_to_str_buf(uint32_t ad, char *buf, const int buf_len);
/* Host byte order */
WS_DLL_PUBLIC char *ip_num_to_str(wmem_allocator_t *scope, uint32_t ad);
WS_DLL_PUBLIC void ip_addr_to_str_buf(const ws_in4_addr *ad, char *buf, const int buf_len);
WS_DLL_PUBLIC char *ip_addr_to_str(wmem_allocator_t *scope, const ws_in4_addr *ad);
WS_DLL_PUBLIC void ip6_to_str_buf(const ws_in6_addr *ad, char *buf, size_t buf_size);
WS_DLL_PUBLIC char *ip6_to_str(wmem_allocator_t *scope, const ws_in6_addr *ad);