Portable alternative to inet_ntop?

Stephen Hemminger stephen at networkplumber.org
Wed Jul 24 18:21:13 CEST 2024


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.

diff --git a/lib/net/rte_ip.h b/lib/net/rte_ip.h
index 0d103d4127..a9404b4b41 100644
--- a/lib/net/rte_ip.h
+++ b/lib/net/rte_ip.h
@@ -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);


More information about the dev mailing list