This function accepts an uint32_t representation of an IP address and
produces a string representation stored in a char * buffer. Realavent
unit tests also included.

Signed-off-by: Ronan Randles <ronan.rand...@intel.com>
---
 app/test/test_net.c | 26 ++++++++++++++++++++++++++
 lib/net/rte_ip.c    | 15 +++++++++++++++
 lib/net/rte_ip.h    | 20 ++++++++++++++++++++
 lib/net/version.map |  1 +
 4 files changed, 62 insertions(+)

diff --git a/app/test/test_net.c b/app/test/test_net.c
index 2cb7d3e1c9..75beeea671 100644
--- a/app/test/test_net.c
+++ b/app/test/test_net.c
@@ -46,7 +46,32 @@ test_rte_ip_parse_addr(void)
                                return -1;
                }
        }
+       return 0;
+}
+
+static int
+test_rte_ip_print_addr(void)
+{
+       printf("Running IP printing tests...\n");
+       char buffer[128];
 
+       struct ip_str_t {
+               uint32_t ip_addr;
+               const char *exp_output;
+       } ip_str_tests[] = {
+               { .ip_addr = 16909060, .exp_output = "1.2.3.4"},
+               { .ip_addr = 3232301055, . exp_output = "192.168.255.255"},
+               { .ip_addr = 2886729737, .exp_output = "172.16.0.9"}
+       };
+
+       uint32_t i;
+       for (i = 0; i < RTE_DIM(ip_str_tests); i++) {
+               int32_t err = rte_ip_print_addr(ip_str_tests[i].ip_addr,
+                                                               buffer, 128);
+
+               if (err || strcmp(buffer, ip_str_tests[i].exp_output))
+                       return -1;
+       }
 
        return 0;
 }
@@ -55,6 +80,7 @@ static int
 test_net_tests(void)
 {
        int ret = test_rte_ip_parse_addr();
+       ret += test_rte_ip_print_addr();
        return ret;
 }
 
diff --git a/lib/net/rte_ip.c b/lib/net/rte_ip.c
index b859dfb640..fbd9161317 100644
--- a/lib/net/rte_ip.c
+++ b/lib/net/rte_ip.c
@@ -41,3 +41,18 @@ rte_ip_parse_addr(const char *src_ip, uint32_t *output_addr)
        free(tok);
        return ret;
 }
+
+int32_t
+rte_ip_print_addr(uint32_t ip_addr, char *buffer, uint32_t buffer_size)
+{
+       if (buffer == NULL)
+               return -1;
+
+       snprintf(buffer, buffer_size, "%u.%u.%u.%u",
+               (ip_addr >> 24),
+               (ip_addr >> 16) & UINT8_MAX,
+               (ip_addr >> 8) & UINT8_MAX,
+                ip_addr & UINT8_MAX);
+
+       return 0;
+}
diff --git a/lib/net/rte_ip.h b/lib/net/rte_ip.h
index 188054fda4..e46f0b41ba 100644
--- a/lib/net/rte_ip.h
+++ b/lib/net/rte_ip.h
@@ -444,6 +444,26 @@ __rte_experimental
 int32_t
 rte_ip_parse_addr(const char *src_ip, uint32_t *output_addr);
 
+
+/**
+ * Print IP address from 32 bit int into char * buffer.
+ *
+ * @param ip_addr
+ *   ip address to be printed.
+ * @param buffer
+ *   The buffer the string will be saved into.
+ * @param buffer_size
+ *   size of buffer to be used.
+ *
+ * @retval 0
+ *   Success.
+ * @retval -1
+ *   Failure due to invalid input arguments.
+ */
+__rte_experimental
+int32_t
+rte_ip_print_addr(uint32_t ip_addr, char *buffer, uint32_t buffer_size);
+
 /**
  * IPv6 Header
  */
diff --git a/lib/net/version.map b/lib/net/version.map
index c530d1a4e4..7fe9892d03 100644
--- a/lib/net/version.map
+++ b/lib/net/version.map
@@ -17,4 +17,5 @@ EXPERIMENTAL {
        global:
 
        rte_ip_parse_addr;
+       rte_ip_print_addr;
 };
-- 
2.25.1

Reply via email to