diff mbox

[2/2] helper/ip: add ipv4/subnet parsing

Message ID 1469675708-28540-2-git-send-email-forrest.shi@linaro.org
State New
Headers show

Commit Message

Forrest Shi July 28, 2016, 3:15 a.m. UTC
From: Xuelin Shi <forrest.shi@linaro.org>


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 <forrest.shi@linaro.org>

---
 helper/include/odp/helper/ip.h | 19 +++++++++++++++++++
 helper/ip.c                    | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+)

-- 
2.1.0.27.g96db324

Comments

Forrest Shi Aug. 10, 2016, 10:34 a.m. UTC | #1
Hi,

This patch needs review.
Attach the gcov report.

Thanks,
Forrest

> -----Original Message-----

> From: forrest.shi@linaro.org [mailto:forrest.shi@linaro.org]

> Sent: Thursday, July 28, 2016 11:15

> To: lng-odp@lists.linaro.org

> Cc: Xuelin Shi <forrest.shi@linaro.org>

> Subject: [lng-odp][PATCH 2/2] helper/ip: add ipv4/subnet parsing

> 

> From: Xuelin Shi <forrest.shi@linaro.org>

> 

> 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 <forrest.shi@linaro.org>

> ---

>  helper/include/odp/helper/ip.h | 19 +++++++++++++++++++

>  helper/ip.c                    | 33 +++++++++++++++++++++++++++++++++

>  2 files changed, 52 insertions(+)

> 

> 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;

> +}

> --

> 2.1.0.27.g96db324
diff mbox

Patch

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;
+}