From patchwork Fri Jul 8 08:14:14 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Forrest Shi X-Patchwork-Id: 71654 Delivered-To: patch@linaro.org Received: by 10.140.28.4 with SMTP id 4csp327032qgy; Fri, 8 Jul 2016 01:26:34 -0700 (PDT) X-Received: by 10.200.35.78 with SMTP id b14mr6764419qtb.41.1467966394162; Fri, 08 Jul 2016 01:26:34 -0700 (PDT) Return-Path: Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id c62si1358862qkd.267.2016.07.08.01.26.33; Fri, 08 Jul 2016 01:26:34 -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 AAFBC68671; Fri, 8 Jul 2016 08:26:33 +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 DCF6A68673; Fri, 8 Jul 2016 08:26:03 +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 4726268673; Fri, 8 Jul 2016 08:26:01 +0000 (UTC) Received: from NAM01-BY2-obe.outbound.protection.outlook.com (mail-by2nam01on0082.outbound.protection.outlook.com [104.47.34.82]) by lists.linaro.org (Postfix) with ESMTPS id E523868672 for ; Fri, 8 Jul 2016 08:25:12 +0000 (UTC) Received: from BLUPR0301CA0014.namprd03.prod.outlook.com (10.162.113.152) by BN3PR03MB1496.namprd03.prod.outlook.com (10.163.35.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.534.14; Fri, 8 Jul 2016 08:25:11 +0000 Received: from BL2FFO11FD048.protection.gbl (2a01:111:f400:7c09::114) by BLUPR0301CA0014.outlook.office365.com (2a01:111:e400:5259::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.534.14 via Frontend Transport; Fri, 8 Jul 2016 08:25:11 +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 BL2FFO11FD048.mail.protection.outlook.com (10.173.161.210) with Microsoft SMTP Server (TLS) id 15.1.523.9 via Frontend Transport; Fri, 8 Jul 2016 08:25:11 +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 u688OwIJ024315; Fri, 8 Jul 2016 01:25:09 -0700 From: To: Date: Fri, 8 Jul 2016 16:14:14 +0800 Message-ID: <1467965654-34734-3-git-send-email-forrest.shi@linaro.org> X-Mailer: git-send-email 2.1.0.27.g96db324 In-Reply-To: <1467965654-34734-1-git-send-email-forrest.shi@linaro.org> References: <1467965654-34734-1-git-send-email-forrest.shi@linaro.org> X-EOPAttributedMessage: 0 X-Matching-Connectors: 131124399114388212; (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)(36756003)(77096005)(87936001)(6806005)(356003)(68736007)(229853001)(2950100001)(2351001)(92566002)(97736004)(110136002)(4326007)(86152002)(7846002)(48376002)(50466002)(305945005)(189998001)(11100500001)(2906002)(47776003)(5003940100001)(104016004)(586003)(50226002)(81166006)(76176999)(50986999)(86362001)(2876002)(19580405001)(105596002)(106466001)(19580395003)(8936002)(33646002)(8676002)(81156014)(21314002); DIR:OUT; SFP:1101; SCL:1; SRVR:BN3PR03MB1496; H:tx30smr01.am.freescale.net; FPR:; SPF:SoftFail; PTR:InfoDomainNonexistent; MX:1; A:1; LANG:en; X-Microsoft-Exchange-Diagnostics: 1; BL2FFO11FD048; 1:KgROX2wh9786n/NxR0pup4n+krD31NuOKQ5VK54M0vTPjhOZFsz0m4kcBFc0YT1KWju+v23txhkngYCrF9sdeYdQW1Omcv2yjJ5yy/5BcUU84zzROxdvUHVR4RiYIifIuir7H77RmYkyHVIssiSLfRyXVv49h+JwwAGFV6C3e6zs0fwC+K9p7vpspiQQhrx6hVVb+yq5/J47m4YVh4cgInQHNfAjIiBvZnStb1+zr/K+fvrQ1X4PboZLZRBCci0XcYO+Noqza5QGzZFgknJgBKP7oEaARO70rUdzIVoYwyAt2hDHqL5H0KOFWI4Hgr608OQFfwcqjY1jCATOelCkhaiTiFfJcaZwXC5cRPybWLQiPDZn7HltTZVX8mK8uaAzHmEoJL+eWcJYQ2ZVlrUFgeTOlrx7vOnNK2Ckgq8UHH4gTlqDcpFuGqZqlt92lXUYeZ/AfvvOfi042q6fHyOBp+77fhq+fbuQB8hwZjxidbVJry0Y1VgBVZfZTDGIM6jhAQ+B/wGokKGkTSRZeM9lon/YYeYvhEDuW5YDr/+/LxgWzH/vIB+J/fYRyXVoCtcnzbRZWYteHyUuztgpqPlOGjS2SOURbJsf/7K1ugeA3Mk= MIME-Version: 1.0 X-MS-Office365-Filtering-Correlation-Id: a2a7a15b-5d03-4b67-2197-08d3a709610e X-Microsoft-Exchange-Diagnostics: 1; BN3PR03MB1496; 2:5sOi85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uEZFs5JTfti+JRef2fDvSRxWbFgu4lY5YLyPYECvXsoJyjts1brGbFWK+AFfr4+shhDOn3ezRYdXfqVWk0OeCitIcz43b/KzXh74ZZ+Z1ZD+ugpjrqAKr1DPpcS273QLSNlwyO4oBc1JxOzk; 31:QEpT4JKKd3Y5HToCxZLjaAoJTq7FuxC3WcBzqsOUXplCV1Krb5TT8ZCO3T7zV8qooMzeeVf7/JaKBnNLoeXxLwbwDnNF+ss47jrJMU9LyDUf5+DUbMoX3j3zJUgnJG21XroMBjI2QNa73K8hwlM/4S6q90dyD3BykAdY6fidPvxpqYdM4N4Odwt+V9aJNziCWqa6Y78h8hUI0izw0KtDaA== 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)(8121501046)(13017025)(5005006)(3002001)(10201501046)(6055026); SRVR:BN3PR03MB1496; BCL:0; PCL:0; RULEID:(400006); SRVR:BN3PR03MB1496; X-Microsoft-Exchange-Diagnostics: 1; BN3PR03MB1496; 4:wsQvYklmN2Z5QftK1m27cAICSyaN007zNo3zqLut1ZSr7FU8bpq0rZ4EdCObA3nEkqbb202yMDwOTK94UNdxCiQAdOVxZ7MNeEFrhPRrdsKYcjlzEqIVICokuF7c3JwlYs6j1a2rRMb/F+a99H0JPT+nMkTxguoFwhsDzVmyx87o78n/I2EWwAIFpl+zxNzmuHycM2UhYQexXfIuK/B0X2Y22q0CBwv11kx9gGp4cKBGGjTdHHfp6nBWuDDdPQl4WsdDwUsUA3w9sb1U2Yg1+/fyBLnI8b9PT5szOVhg/tRjzNtW3McTTe0bAvzc/E1SOPlBB4JBuPtCCuuifg1YZALxl+2ABsBaz326TdUKy7HVU3wZ8GD0JbNI5fnnTCTu8bmNLoSGVLDjiK6Z3xCs5ZrHPjw69YPrWdIaLkwbVruUFhFd5YOwfYXE7UEjSKmkHvlAas6tOmUpSQ3lUCa6ilBwXkzbohaBvAG/kN76NDRVcpPaIS6K/DCNQb3JTKGaiYz4A2oHRbrqoiADrsF2ZA== X-Forefront-PRVS: 0997523C40 X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; BN3PR03MB1496; 23:DgIzgYAhS4oSuLW2zm7b+WNT54HFt1/6VwXgmGXH9?= =?us-ascii?Q?nlqITm0KEaNw42pcZ1mUnQ11HSI+cMDIs9SiVllV9kNO3oc+SMs+h+jaX+Yk?= =?us-ascii?Q?YjqW+XqD9e+51qXKbncVI3tZwsGCC15qi7ai2PD0dy0FvpUlGyzK3Y8781ZH?= =?us-ascii?Q?eQVxXbb6wS7rlIuwTnA3lR1S3+s0sqHbr7/gpBnXb8YPp+T/r2lVf36n5kgs?= =?us-ascii?Q?89m3B4Kf0jHWet7G+GlgcDppXI53x8pjR1xKp089dIcrX8RtBAyv18N2lSZY?= =?us-ascii?Q?jDI3idM/ty6hYtzhUB31UnUKGUykBI8z3Xswl+bobPo59D8u5imdKDdJODeK?= =?us-ascii?Q?iot54qHPTryMH7hyLTEoKbbTwV+aOibnAFRUE20ORz8jmBHHhH+3aNBtioVu?= =?us-ascii?Q?nIt4Pg+qs1kUuOA+yAy0d5CiMoLIIUU/94sMavdNBIxeQf+6R7BtEyEoWu41?= =?us-ascii?Q?6h2NzyAccK1k0t5Yexoq2dRDxyrznt7hSXnfYvxTpY7ne9ycg3ASUT66OJYW?= =?us-ascii?Q?pU6FUhtvs2WZE2oiKa5V+CcMYhv/XZiWG+SMQHsL6OvvA2St/fFSwI+nbC3E?= =?us-ascii?Q?5Qwv+9DHae2dh69CfA6LlAcvElzS07R5j7ulxHwVz+zIuQw7heR539Fh3BZR?= =?us-ascii?Q?rpnDlLf7EIhVwENaZN433+RBlxy8B1xxbGcObxw7RcbELFSBBZZvJUh7ayj/?= =?us-ascii?Q?4TIsCwdsNZRAvGph7DOn67VtGLWX8k2ydzXl7wfcUPj/V9VFWrcEuzY9bAww?= =?us-ascii?Q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GtOL5mLfmSkwEkC75mlFapEQbxd2KSfaLYsTOAdwOAUxK9dWjZ2hqasGIrD05Fvzyldo+4augUGE/7zTIVIplvC/6plg38KDzvdZhRbV7rt2F9oRaNDb9GYcNpdEjpA4x4XVvVvLlKIitHUYWhP9qje7J/fLXTcTldswly6W+n4FdxjnVeO8VjsoHHSCi+SJthTra7tx2x6KG/ffjugGBlt0bvZFeQ2s5ON3C3XhTH SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Jul 2016 08:25:11.2672 (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: BN3PR03MB1496 X-Topics: patch Cc: lng-odp@lists.linaro.org Subject: [lng-odp] [PATCH/API-NEXT 3/3] 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 e211001..d2ca21e 100644 --- a/helper/ip.c +++ b/helper/ip.c @@ -30,3 +30,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; +}