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 006AEA034E; Fri, 21 Jan 2022 11:31:44 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id B299842751; Fri, 21 Jan 2022 11:31:34 +0100 (CET) Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) by mails.dpdk.org (Postfix) with ESMTP id C5B1940042 for ; Fri, 21 Jan 2022 11:31:32 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1642761093; x=1674297093; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=5+UYx+hbjVJM/7fvf5oUftHOMI30Hf333p11TZUG+qc=; b=NZXQl9GwZUHmrcftss8MFlL+7PkofJjBugv0peroS61toDC+ipoU0fXQ Y48dYl8adt/VjlZXkJ6z9a4Xy4EYeszUN4ArE/vx6nqN2wbXbHH3fnh9v DliJVag70ZDGYLiAVlUvyWBJBwn81Ay+uEY1bs/MUFXj8MUWY9FjZZaOS rDTf0IaEXb95q6yquWeAYtMvbOQVI0K7AjnSCuuW0YgpyfVK8BCR8vOjT FBAOUdgafiqZMnxm61i+XzC728dvHNqcJ1HLOibgQK7gzMe8JY8VUsU0y NCX9RwAO91CwZLyjBIXdzmwjVRxBKGPCO5BKwFOAFB9w603C53lCFcsbC A==; X-IronPort-AV: E=McAfee;i="6200,9189,10233"; a="270045070" X-IronPort-AV: E=Sophos;i="5.88,304,1635231600"; d="scan'208";a="270045070" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Jan 2022 02:31:32 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.88,304,1635231600"; d="scan'208";a="533222716" Received: from silpixa00401120.ir.intel.com ([10.55.128.255]) by orsmga008.jf.intel.com with ESMTP; 21 Jan 2022 02:31:31 -0800 From: Ronan Randles To: dev@dpdk.org Cc: Ronan Randles Subject: [PATCH v2 02/15] net: add function to pretty print IPv4 Date: Fri, 21 Jan 2022 10:31:09 +0000 Message-Id: <20220121103122.2926856-3-ronan.randles@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220121103122.2926856-1-ronan.randles@intel.com> References: <20211214141242.3383831-1-ronan.randles@intel.com> <20220121103122.2926856-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 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