From patchwork Thu Jul 28 03:15:08 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Forrest Shi X-Patchwork-Id: 72926 Delivered-To: patch@linaro.org Received: by 10.140.29.52 with SMTP id a49csp626467qga; Wed, 27 Jul 2016 20:26:55 -0700 (PDT) X-Received: by 10.55.57.134 with SMTP id g128mr7662194qka.10.1469676415861; Wed, 27 Jul 2016 20:26:55 -0700 (PDT) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id d189si6576997qkf.300.2016.07.27.20.26.55; Wed, 27 Jul 2016 20:26:55 -0700 (PDT) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Authentication-Results: mx.google.com; spf=pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) smtp.mailfrom=lng-odp-bounces@lists.linaro.org; dmarc=pass (p=NONE dis=NONE) header.from=linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 77B58684A4; Thu, 28 Jul 2016 03:26:55 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252 X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAD_ENC_HEADER,BAYES_00, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from [127.0.0.1] (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id F0521684DF; Thu, 28 Jul 2016 03:26:47 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id 1540C684D4; Thu, 28 Jul 2016 03:26:43 +0000 (UTC) Received: from NAM01-BN3-obe.outbound.protection.outlook.com (mail-bn3nam01on0080.outbound.protection.outlook.com [104.47.33.80]) by lists.linaro.org (Postfix) with ESMTPS id A5C2F684A4 for ; Thu, 28 Jul 2016 03:26:19 +0000 (UTC) Received: from DM2PR03CA0036.namprd03.prod.outlook.com (10.141.96.35) by MWHPR03MB2494.namprd03.prod.outlook.com (10.169.201.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.549.15; Thu, 28 Jul 2016 03:26:17 +0000 Received: from BL2FFO11FD044.protection.gbl (2a01:111:f400:7c09::116) by DM2PR03CA0036.outlook.office365.com (2a01:111:e400:2428::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.549.15 via Frontend Transport; Thu, 28 Jul 2016 03:26:18 +0000 Received-SPF: SoftFail (protection.outlook.com: domain of transitioning linaro.org discourages use of 192.88.168.50 as permitted sender) Received: from tx30smr01.am.freescale.net (192.88.168.50) by BL2FFO11FD044.mail.protection.outlook.com (10.173.161.140) with Microsoft SMTP Server (TLS) id 15.1.539.16 via Frontend Transport; Thu, 28 Jul 2016 03:26:17 +0000 Received: from titan.ap.freescale.net ([10.192.208.233]) by tx30smr01.am.freescale.net (8.14.3/8.14.0) with ESMTP id u6S3QCut015653; Wed, 27 Jul 2016 20:26:16 -0700 From: To: Date: Thu, 28 Jul 2016 11:15:08 +0800 Message-ID: <1469675708-28540-2-git-send-email-forrest.shi@linaro.org> X-Mailer: git-send-email 2.1.0.27.g96db324 In-Reply-To: <1469675708-28540-1-git-send-email-forrest.shi@linaro.org> References: <1469675708-28540-1-git-send-email-forrest.shi@linaro.org> X-EOPAttributedMessage: 0 X-Matching-Connectors: 131141499778470568; (91ab9b29-cfa4-454e-5278-08d120cd25b8); () X-Forefront-Antispam-Report: CIP:192.88.168.50; IPV:NLI; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(6009001)(7916002)(2980300002)(189002)(199003)(50226002)(110136002)(77096005)(8936002)(6806005)(189998001)(97736004)(305945005)(2950100001)(7846002)(92566002)(356003)(81156014)(87936001)(47776003)(68736007)(104016004)(8676002)(81166006)(36756003)(86362001)(76176999)(19580405001)(5003940100001)(229853001)(586003)(50986999)(2876002)(2906002)(33646002)(19580395003)(4326007)(50466002)(48376002)(106466001)(86152002)(2351001)(105596002)(21314002); DIR:OUT; SFP:1101; SCL:1; SRVR:MWHPR03MB2494; H:tx30smr01.am.freescale.net; FPR:; SPF:SoftFail; PTR:InfoDomainNonexistent; MX:1; A:1; LANG:en; X-Microsoft-Exchange-Diagnostics: 1; BL2FFO11FD044; 1:e/aW0u7V/5TU5dL/8Qdfie1DGcArzRcMrTbSivXN1OKZtXaEcLL8kSOHTZ7t9CF4X1bPS+swvOwe3kN6RrUgGDj+d6xNgd5ErCPXa28kwlPgpVyGvRRFL7MgQQRmOL4shv2wDrnsHEgrb95oJfo5jnOFM5UCCHsNt9VthRErdKjfZXdsBl9wL9QOj2QyrKiy+1TCixvEZsZa9lira6+Nm7m8Lp4pcnj8D7D+abtWw9AKNo8D5zku8IAng5cRdT0MdljR0dqhb4cGJI98ftSrMZvOxt2IHFFmwaOwJrUXjMMlcpZJ/g2YR5A/13v0JvejzLnb7dhvcE6+hmXp0HelIofraxngcIIiZiEkvVFnIFNX0+PeqRXIjUdeyfPz0kXTCfpXJuxZ1PKqcswEqIMl8RHJQrRjSQa/eBfm6o4ND053sdRgBHcAP7XuUdQ/pI/hYmp4WiGroz0kxaAXE4o+m7Mg/VsAoNADBClRFAn/JBx29f/S9PNpQtCX2um3ITsV3ThD+TMM/++lHGvRj5Fdz1dMdcLrmZaWkcJuePSeaHpj1QvdQQOtdCE2xRcPQx0gnFQhzd66xi5zr3t535h+rFydni9ul92JQmRF42+kHQZdO6HqJurjjqJJ0SgcZLeuhsc5fLl65aycbd3xsp6lBA== MIME-Version: 1.0 X-MS-Office365-Filtering-Correlation-Id: 299b54de-195d-4f9a-3147-08d3b696f010 X-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/kDtVopgs+5D03Q1uoGwk3T6CM82t6TqaAnvCEpuE/XwB4C6vZ848Tv1olyLhxxtYvWJWA/G7jK5JamaJqudt6xFSl6l0VnfWpF11HANS8lNS0K3EaJJsdignJU1Lair+diOPUQMpF308WT53RDakHHhcujkFeeJ8AP04qt6vNaU7hmjVBeE++TvfTpw9oKWEr1bAJ; 31:GCNNIONf5rrDAg/ttI/iiBHqt4Ru4bLZ62kEyeoLriKrKbPq+JvoxTUF99V6CvO8AJHegaLc/S0WawR4Mom5ZDTKdjTSBWwezioRiEzVrtfit3TtDwuYrky65DlHzX+vhPcbruzNDKRY05zXLTayM+qQeAsaMOVbMwYEQgtxutD0Uj0H2wfE5AlaloKIMXG6mWMO9JjR/0EYb4bW8Z3JVQ== X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(131110393319338); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(601004)(2401047)(13023025)(13024025)(13015025)(13018025)(13017025)(5005006)(8121501046)(10201501046)(3002001)(6055026); SRVR:MWHPR03MB2494; BCL:0; PCL:0; RULEID:(400006); SRVR:MWHPR03MB2494; X-Microsoft-Exchange-Diagnostics: 1; MWHPR03MB2494; 4:7ppTi5LEH6TtfInPt/90EU8f4mJg+hfqvYtBRiCOE7i+37VVH3lBYzn9Y79+bFKe2yO6GjsEVK6fxakRfitrQ8rVNTgHW5m+o7WFRnXyXw0AZcHvQO7uqvKBak7895N5Knw51u1zpzdz30MjU0Ys3zpu5fyxzOhlj7Nnog5B+1fBNw9dt0EQVnxz8u5+hZnXDkKc2Mu8YgUuNwXWaoFLyCiQyIPGyCdhWxACkDK4n4eow7h/qKgtHHeN8wV/aVj3H9IAmVSAouJdfxcMXaSieL7PENHZrK3zyFOhZjcwbMsCm07sT7Imq5h/8vKwBWK2xEAkPvjlhGwbPEQFXm90D8BvCFoCe3WMpRDbb75B3f+Py6t8ZqKcvlUFZwBLZSQO6Zsbk8UdIFXZnBq4R6Esnu5zAk8plcP28x40GI5TR4hZkVZjVuLL0ZaWoEXinSDKtF1qp3otN6aBHUvYf4tWz4oXIXgSOx2wYln6RVKweOkoLZykiMEjNoqg/ZgF/l2JsA6nQHZtrFqSvLEYhxZBYA== X-Forefront-PRVS: 00179089FD X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; MWHPR03MB2494; 23:x9Ulf9TuBAaPOeD+B0uCvOjIkJMZPIjC4jNB+rWRF?= =?us-ascii?Q?cqR1hj+tTTPC8wUKQF9KioPc7A/LiXvBiqajyjtxh0GTnwZZNCcK1CyKUzJ2?= =?us-ascii?Q?Cz9B9B1lkyc3/V6wr3NdW3nGonaDSBmp0bykQNV1qD6T9C5Mi6YTDHEhLM4q?= =?us-ascii?Q?tjCipNvA4dDpHXt20W92LZIn1K0+WTdZ0bjU971n5NydWQgDEkVe6MAVoI2Z?= =?us-ascii?Q?cr2tc98mRSlZNUe9yP1NcI835SJdgDoVuXaTgASX8R3328ACOF4smKV1WZeL?= =?us-ascii?Q?2ZzAXrkM+ENjgC6trjRlAAgcqbLDEUwXn9EmVP+2xI8nTeFCzQDWvi6jLvXS?= =?us-ascii?Q?IVE1UlVSy1dtUJEfB0TfvOI4K+fJE3sgjX7BAO7bU98MC20dI/3NUaqt5STj?= =?us-ascii?Q?iXVwNesdgFIpMYXuH/LTeJGXYVWR1reIeKTIrtk7JvQdDm3UW5KJePc+BTcb?= =?us-ascii?Q?Ekf+0lpRtdFhMK4huAlz2amZqd8bCaMx8gzvDojpEM6TG5oa30GGILIGsL9i?= =?us-ascii?Q?gucC5uu549AGCJgqTJRTGsZoyulD79Z9+VzociExEwCUataSaguGxgR0ET4B?= =?us-ascii?Q?aHfqq8+W+AXmOvfVMyQU6Z8lzYP/tQgeFr4wr2BxBDlT607v93VpM+YcVd+f?= =?us-ascii?Q?y+vj66zCjZH8Mropvac84poanOlmL/7f/etXp57k62fJbnt5oih488UoF1ZP?= =?us-ascii?Q?/ALQnPETRDPnGbq2atnUoC5wO4c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U0=; 7:06J+gB1P1GYiexDq9CbJLmZm4c3+MHv6o/byQ/aRmdVZppdaG6OFIio3W0B9ugQ0eDgom/FXEOn8lppuAAlWV+qH6OHYQzPAQKcnLEFBeUwSVbCWxVpH4J+U18zKWGHj+KflYWb116z1cocBsulvNPAGerR7zrUW4TI2zFQkMMP3MSXlsUhz9mkG6CPbcEb+VBso/fNvV6HBSuTTmzrlX0wes0mh7PZNy7/TbyThwpWjvAc38jbxvbeB6Gz0ZSr5 SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Jul 2016 03:26:17.6130 (UTC) X-MS-Exchange-CrossTenant-Id: 5afe0b00-7697-4969-b663-5eab37d5f47e X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=5afe0b00-7697-4969-b663-5eab37d5f47e; Ip=[192.88.168.50]; Helo=[tx30smr01.am.freescale.net] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR03MB2494 X-Topics: patch Subject: [lng-odp] [PATCH 2/2] helper/ip: add ipv4/subnet parsing X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: "The OpenDataPlane \(ODP\) List" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" From: Xuelin Shi parse an ipv4/subnet string like "192.168.1.0/24" into 3 values: ipv4-addr, subnet bit width and subnet mask Signed-off-by: Xuelin Shi --- helper/include/odp/helper/ip.h | 19 +++++++++++++++++++ helper/ip.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) -- 2.1.0.27.g96db324 diff --git a/helper/include/odp/helper/ip.h b/helper/include/odp/helper/ip.h index 4cfc00f..dd538c9 100644 --- a/helper/include/odp/helper/ip.h +++ b/helper/include/odp/helper/ip.h @@ -233,6 +233,25 @@ typedef struct ODP_PACKED { int odph_ipv4_addr_parse(uint32_t *ip_addr, const char *str); /** + * Parse text string representing an IPv4 address or subnet + * + * String is of the format "XXX.XXX.XXX.XXX(/W)" where + * "XXX" is decimal value and "/W" is optional subnet length + * + * @param ip_net_str Pointer to IP address/subnet string to convert + * @param ip_addr Pointer to return IPv4 address, host endianness + * @param depth Pointer to subnet bit width + * @param subnet_mask Pointer to subnet mask + * + * @retval 0 on success of parsing both ip and subnet + * @retval 1 on success of parsing only ip + * @retval 2 on success of parsing only subnet + * @retval <0 on failure + */ +int odph_ipv4_subnet_parse(const char *ip_net_str, uint32_t *ip_addr, + uint32_t *depth, uint32_t *subnet_mask); + +/** * @} */ #ifdef __cplusplus diff --git a/helper/ip.c b/helper/ip.c index eb73e5a..5ce7ced 100644 --- a/helper/ip.c +++ b/helper/ip.c @@ -28,3 +28,36 @@ int odph_ipv4_addr_parse(uint32_t *ip_addr, const char *str) return 0; } + +int odph_ipv4_subnet_parse(const char *ip_net_str, uint32_t *ip_addr, + uint32_t *depth, uint32_t *subnet_mask) +{ + char *s; + int converted; + uint32_t qualifier = 32; + char ip_valid = 0; + char subnet_valid = 0; + + s = strchr(ip_net_str, '/'); + if (s) { + converted = sscanf(s, "/%u", &qualifier); + if (converted == 1 && qualifier && qualifier <= 32) { + *depth = qualifier; + *subnet_mask = ((1 << qualifier) - 1) << + (32 - qualifier); + subnet_valid = 1; + } + } + + if (!odph_ipv4_addr_parse(ip_addr, ip_net_str)) + ip_valid = 1; + + if (ip_valid && subnet_valid) + return 0; + else if (ip_valid) + return 1; + else if (subnet_valid) + return 2; + else + return -1; +}