diff options
author | Petri Savolainen <petri.savolainen@nokia.com> | 2016-02-09 14:25:08 +0200 |
---|---|---|
committer | Maxim Uvarov <maxim.uvarov@linaro.org> | 2016-03-03 22:32:04 +0300 |
commit | 96cef77a0aad6940f5b210277ffb0bccb6e5b3c1 (patch) | |
tree | 55001f97f8d7d94200e8d27ab035a9b5cca3ab97 /helper/ip.c | |
parent | cc03b63e667c3acf580f20765910dbb342d452b2 (diff) |
helper: ip: added ipv4 address parse
IPv4 address parse function is commonly needed by
test applications. A common parse function harmonizes the
definition IPv4 address as a command line parameter.
Signed-off-by: Petri Savolainen <petri.savolainen@nokia.com>
Reviewed-by: Juha-Matti Tilli <juha-matti.tilli@nokia.com>
Signed-off-by: Maxim Uvarov <maxim.uvarov@linaro.org>
Diffstat (limited to 'helper/ip.c')
-rw-r--r-- | helper/ip.c | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/helper/ip.c b/helper/ip.c new file mode 100644 index 000000000..eb73e5a08 --- /dev/null +++ b/helper/ip.c @@ -0,0 +1,30 @@ +/* Copyright (c) 2016, Linaro Limited + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include <odp/helper/ip.h> + +#include <stdio.h> +#include <string.h> + +int odph_ipv4_addr_parse(uint32_t *ip_addr, const char *str) +{ + unsigned byte[ODPH_IPV4ADDR_LEN]; + int i; + + memset(byte, 0, sizeof(byte)); + + if (sscanf(str, "%u.%u.%u.%u", + &byte[0], &byte[1], &byte[2], &byte[3]) != ODPH_IPV4ADDR_LEN) + return -1; + + for (i = 0; i < ODPH_IPV4ADDR_LEN; i++) + if (byte[i] > 255) + return -1; + + *ip_addr = byte[0] << 24 | byte[1] << 16 | byte[2] << 8 | byte[3]; + + return 0; +} |