From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id C3539A00C3; Tue, 14 Dec 2021 15:13:04 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 9984641151; Tue, 14 Dec 2021 15:12:52 +0100 (CET) Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by mails.dpdk.org (Postfix) with ESMTP id 5B4AE410E4 for ; Tue, 14 Dec 2021 15:12:50 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1639491170; x=1671027170; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=wOqgJavZuJODwkZCtBBRtyUQznJztXAEEPL7JpEuFs8=; b=Y4cxdItgomr8u3ylS5M7y8igl9/lLrgm7p5NhqP1m0ViNhZH83vH4kl3 cYqIe0/GJeHPcI9O1CqMtc7eyfRmbT+9gImKkBB5kdJsSMdrFoYAJxT+/ QGCduada53VaX+9zW194gTtyAh86Cx9PXxMd3Hv35LcfxISR7So8i4aAQ 6Ux/QEdq7BDhHp5am/tg6dXKQd2zkjM9jSYfxh3MC/GHgh7ToCUBHNLIN 1uC/+z4ciax7jTm3yXcSUACf36SgPRpiLU/7Q9JJ9Mh035uxeNojAyplx 7b5dzvmS5dF+ScT8PCIjTkv3KWHEGxd5tHHUMByXqnC5oEUaDBP9+x910 Q==; X-IronPort-AV: E=McAfee;i="6200,9189,10197"; a="302362304" X-IronPort-AV: E=Sophos;i="5.88,205,1635231600"; d="scan'208";a="302362304" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Dec 2021 06:12:49 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.88,205,1635231600"; d="scan'208";a="465104094" Received: from silpixa00401120.ir.intel.com ([10.55.129.95]) by orsmga006.jf.intel.com with ESMTP; 14 Dec 2021 06:12:48 -0800 From: Ronan Randles To: dev@dpdk.org Cc: harry.van.haaren@intel.com, Ronan Randles Subject: [PATCH 02/12] net: add function to pretty print IPv4 Date: Tue, 14 Dec 2021 14:12:32 +0000 Message-Id: <20211214141242.3383831-3-ronan.randles@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211214141242.3383831-1-ronan.randles@intel.com> References: <20211214141242.3383831-1-ronan.randles@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org 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 --- 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 3e5a307e48..25028f28ff 100644 --- a/lib/net/version.map +++ b/lib/net/version.map @@ -19,4 +19,5 @@ EXPERIMENTAL { rte_net_skip_ip6_ext; rte_ether_unformat_addr; rte_ip_parse_addr; + rte_ip_print_addr; }; -- 2.25.1