[v2,02/15] net: add function to pretty print IPv4
Checks
Commit Message
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.randles@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(+)
Comments
On Fri, 21 Jan 2022 10:31:09 +0000
Ronan Randles <ronan.randles@intel.com> wrote:
> +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;
NAK
Broken on big endian machines.
Once again, why does DPDK need to reinvent everything?
@@ -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;
}
@@ -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;
+}
@@ -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
*/
@@ -17,4 +17,5 @@ EXPERIMENTAL {
global:
rte_ip_parse_addr;
+ rte_ip_print_addr;
};