Message ID | 1467965654-34734-3-git-send-email-forrest.shi@linaro.org |
---|---|
State | Superseded |
Headers | show |
On 8 July 2016 at 04:14, <forrest.shi@linaro.org> wrote: > 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); > This new api is not tested by the validation suite To see this in LCOV use check-odp and point at your repo and branch setting LCOV GIT_URL=~/git/odp GIT_BRANCH=forrest LCOV=1 ./build.sh firefox publishing/forrest/linux-generic-helper-lcov-html/helper/ip.c.gcov.html > + > +/** > * @} > */ > #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; > +} > -- > 2.1.0.27.g96db324 > > -- Mike Holmes Technical Manager - Linaro Networking Group Linaro.org <http://www.linaro.org/> *│ *Open source software for ARM SoCs "Work should be fun and collaborative, the rest follows"
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; +}