Skip to contentSkip to conversation

Portable alternative to inet_ntop?

PATCH Stephen Hemminger 3 participants 4 messages Jul 24, 2024 – Aug 7, 2024

lore
Share & tools
lore
#0 lore raw

The function inet_ntop is useful to make printable addresses for debugging. It is available on Linux and FreeBSD but not on Windows.

There are some alternatives:
- add yet another OS shim in lib/eal/windows/include. Win32 has similar InetNtoP but it uses wide characters.

- copy/paste code from FreeBSD into some new functions.

Hate duplicating code, but portability is a problem here.

lib/net/rte_ip.h +19 -0
index 0d103d4127..a9404b4b41 100644
@@ -839,6 +839,27 @@ rte_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len)
        return next_proto;
 }
 
+
+#define RTE_IPV4_ADDR_FMT_SIZE 16
+#define RTE_IPV6_ADDR_FMT_SIZE 46
+
+__rte_experimental
+int
+rte_ipv4_format_addr(char *buf, uint16_t size, const void *addr);
+
+__rte_experimental
+void
+rte_ipv4_unformat_addr(const char *str, void *addr);
+
+__rte_experimental
+void
+rte_ipv6_format_addr(char *buf, uint16_t size, const void *addr);
+
+__rte_experimental
+void
+rte_ipv4_unformat_addr(const char *str, void *addr);
#1 lore raw
Quoted 7 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Wednesday, 24 July 2024 18.21

The function inet_ntop is useful to make printable addresses for debugging.
It is available on Linux and FreeBSD but not on Windows.

Quoted 7 lines

There are some alternatives:
- add yet another OS shim in lib/eal/windows/include. Win32 has similar InetNtoP but it uses wide characters.

- copy/paste code from FreeBSD into some new functions.

Hate duplicating code, but portability is a problem here.

+1 for "duplicating code" in this case.

Quoted 7 lines
lib/net/rte_ip.h +0 -0
index 0d103d4127..a9404b4b41 100644
@@ -839,6 +839,27 @@ rte_ipv6_get_next_ext(const uint8_t *p, int proto,
 size_t *ext_len)

lib/net/rte_ip.h rte_ipv6_get_next_ext(const uint8_t *p, int proto, · line 849

Quoted 10 lines
lib/net/rte_ip.h +7 -0
        return next_proto;
 }
 
+
+#define RTE_IPV4_ADDR_FMT_SIZE 16
+#define RTE_IPV6_ADDR_FMT_SIZE 46
+
+__rte_experimental
+int
+rte_ipv4_format_addr(char *buf, uint16_t size, const void *addr);

This resembles rte_ether_format_addr() [1], so I agree to passing the address by reference instead of by value.

[1]: https://elixir.bootlin.com/dpdk/v24.07-rc3/source/lib/net/rte_ether.h#L264

I consider rte_be32_t the "official" type for an IPv4 address in network byte order. This is the type used in the IPv4 header [2].

[2]: https://elixir.bootlin.com/dpdk/v24.07-rc3/source/lib/net/rte_ip.h#L41

With this in mind, please change the address parameter's type from "const void *" to "const rte_be32_t *".
I speculate that you used "void *" to be compatible with both the types unsigned char[4] and rte_be32_t, and avoid alignment issues with the latter.
I fear this could set a very bad precedent; using "void *" instead of the proper type would make APIs difficult to read, because the actual types are omitted. We don't want to start using "void *" for API parameters to avoid type casting.

(C++ would allow providing the same function with a variety of parameter types, but we are stuck with C.)

lib/net/rte_ip.h rte_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len) · line 852

Quoted 4 lines
lib/net/rte_ip.h +4 -0
+
+__rte_experimental
+void
+rte_ipv4_unformat_addr(const char *str, void *addr);

Same as above; please change output type from void* to rte_be32_t*.

lib/net/rte_ip.h rte_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len) · line 856

Quoted 4 lines
lib/net/rte_ip.h +4 -0
+
+__rte_experimental
+void
+rte_ipv6_format_addr(char *buf, uint16_t size, const void *addr);

I suppose this outputs the IPv6 address string in packed format (using "::" if possible), as I suppose the IPv4 address string is output without leading zeroes (%u, not %03u). Alternatively, consider adding a formatting flags parameter to specify the output format (leading zeroes or not, and "::" or not).

Same as above; addr parameter should be "const uint8_t addr[16]", reflecting the "official" IPv6 address type. This will be updated to "const struct rte_ipv6_addr *addr" with Robin's coming 24.11 patch series.

lib/net/rte_ip.h rte_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len) · line 852

Quoted 4 lines
lib/net/rte_ip.h +4 -0
+
+__rte_experimental
+void
+rte_ipv4_unformat_addr(const char *str, void *addr);

Same as above; addr parameter type should reflect the "official" IPv6 address type.

And a typo in the function name: ipv4 -> ipv6

#2 lore raw
In reply to Stephen Hemminger
Quoted 2 lines · Stephen Hemminger

The function inet_ntop is useful to make printable addresses for debugging. It is available on Linux and FreeBSD but not on Windows.

i know my reply is late. but the following wasn't usable?
https://learn.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-inet_ntop
it isn't wide (PSTR).

i know trying to use winsock headers is a mess though so if it is hard duplicating seems fine. i would not be unhappy for more generalized apis to be built up within dpdk to avoid the platform coupling.

ty

Quoted 36 lines

There are some alternatives:
- add yet another OS shim in lib/eal/windows/include. Win32 has similar InetNtoP but it uses wide characters.

- copy/paste code from FreeBSD into some new functions.

Hate duplicating code, but portability is a problem here.

lib/net/rte_ip.h +19 -0
index 0d103d4127..a9404b4b41 100644
@@ -839,6 +839,27 @@ rte_ipv6_get_next_ext(const uint8_t *p, int proto, size_t *ext_len)
        return next_proto;
 }
 
+
+#define RTE_IPV4_ADDR_FMT_SIZE 16
+#define RTE_IPV6_ADDR_FMT_SIZE 46
+
+__rte_experimental
+int
+rte_ipv4_format_addr(char *buf, uint16_t size, const void *addr);
+
+__rte_experimental
+void
+rte_ipv4_unformat_addr(const char *str, void *addr);
+
+__rte_experimental
+void
+rte_ipv6_format_addr(char *buf, uint16_t size, const void *addr);
+
+__rte_experimental
+void
+rte_ipv4_unformat_addr(const char *str, void *addr);
#3 lore raw
Quoted 8 lines · On Mon, 5 Aug 2024 22:35:27 -0700 Tyler Retzlaff…
Quoted 2 lines · Stephen Hemminger

The function inet_ntop is useful to make printable addresses for debugging. It is available on Linux and FreeBSD but not on Windows.

i know my reply is late. but the following wasn't usable?
https://learn.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-inet_ntop
it isn't wide (PSTR).

Quoted 5 lines

i know trying to use winsock headers is a mess though so if it is hard duplicating seems fine. i would not be unhappy for more generalized apis to be built up within dpdk to avoid the platform coupling.

ty

I got inet_ntop to work on windows (as long as the right includes are used).