[Accel-ppp-users] [PATCH 1/3] utils: rework u_parse_ip4addr()
Status: Beta
Brought to you by:
xebd
From: Guillaume N. <g....@al...> - 2018-12-07 16:37:57
|
Redefine u_parse_ip4addr() to match the behaviour of other u_parse_*() functions: * Drop the err_msg parameter. * Return the number of bytes parsed instead of an error number. * Remove support for fancy IPv4 address notations. There is currently only one user of u_parse_ip4addr() (in iprange.c). Dropping the fancy IPv4 address representations is probably not going to harm anyone (quite the opposite as many users don't realise that leading 0 means octal and that plain integers can be considered IPv4 addresses). Signed-off-by: Guillaume Nault <g....@al...> --- accel-pppd/iprange.c | 3 +-- accel-pppd/utils.c | 51 ++++++++++++++++++++++---------------------- accel-pppd/utils.h | 4 ++-- 3 files changed, 28 insertions(+), 30 deletions(-) diff --git a/accel-pppd/iprange.c b/accel-pppd/iprange.c index f7b77a27..40c938e2 100644 --- a/accel-pppd/iprange.c +++ b/accel-pppd/iprange.c @@ -51,7 +51,6 @@ static int parse_iprange(const char *str, struct iprange_t **range) char ipstr[CIDR_MAXLEN + 1] = { 0 }; struct iprange_t *_range; struct in_addr addr; - const char *errmsg; char *suffix_str; uint32_t ipmin; uint32_t ipmax; @@ -88,7 +87,7 @@ static int parse_iprange(const char *str, struct iprange_t **range) *suffix_str = '\0'; ++suffix_str; - if (u_parse_ip4addr(ipstr, &addr, &errmsg)) { + if (!u_parse_ip4addr(ipstr, &addr)) { log_error("iprange: impossible to parse range \"%s\":" " invalid IPv4 address \"%s\"\n", str, ipstr); diff --git a/accel-pppd/utils.c b/accel-pppd/utils.c index 544c59e7..b1590544 100644 --- a/accel-pppd/utils.c +++ b/accel-pppd/utils.c @@ -1,7 +1,6 @@ #include <arpa/inet.h> #include <ctype.h> #include <errno.h> -#include <netdb.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> @@ -167,31 +166,6 @@ size_t __export u_parse_u32(const char *str, uint32_t *val) return endptr - str; } -int __export u_parse_ip4addr(const char *src, struct in_addr *addr, - const char **err_msg) -{ - struct addrinfo hint = { - .ai_flags = AI_NUMERICHOST, - .ai_family = AF_INET, - .ai_socktype = 0, - .ai_protocol = 0, - }; - struct addrinfo *ainfo; - int err; - - err = getaddrinfo(src, NULL, &hint, &ainfo); - if (err) { - *err_msg = gai_strerror(err); - return err; - } - - *addr = ((struct sockaddr_in *)ainfo->ai_addr)->sin_addr; - - freeaddrinfo(ainfo); - - return 0; -} - /* Parse an IPv6 address (for example "2001:db8::1"). * Returns the number of bytes parsed, or 0 if str doesn't start with an IPv6 * address. @@ -214,6 +188,31 @@ size_t __export u_parse_ip6addr(const char *str, struct in6_addr *addr) return len; } +/* Parse an IPv4 address in dotted-decimal format (for example "198.51.100.1"). + * Other formats (hex "0xc6.0x33.0x64.0x1", octal "0306.063.0144.01", mixed + * "0xc6.51.0144.1", non dotted-quad "198.51.25601"...) are rejected. + * + * Returns the number of bytes parsed, or 0 if str doesn't start with an IPv4 + * address. + */ +size_t __export u_parse_ip4addr(const char *str, struct in_addr *addr) +{ + char buf[INET_ADDRSTRLEN]; + size_t len; + + len = strspn(str, ".0123456789"); + if (!len || len >= sizeof(buf)) + return 0; + + memcpy(buf, str, len); + buf[len] = '\0'; + + if (inet_pton(AF_INET, buf, addr) != 1) + return 0; + + return len; +} + /* Parse an IPv6 network prefix in CIDR notation (for example "2001:db8::/32"). * Returns the number of bytes parsed, or 0 if str doesn't start with an IPv6 * network prefix. diff --git a/accel-pppd/utils.h b/accel-pppd/utils.h index d3e06083..a7c38973 100644 --- a/accel-pppd/utils.h +++ b/accel-pppd/utils.h @@ -16,9 +16,9 @@ size_t u_parse_u8(const char *str, uint8_t *val); size_t u_parse_u16(const char *str, uint16_t *val); size_t u_parse_u32(const char *str, uint32_t *val); -int u_parse_ip4addr(const char *src, struct in_addr *addr, - const char **err_msg); size_t u_parse_ip6addr(const char *str, struct in6_addr *addr); +size_t u_parse_ip4addr(const char *str, struct in_addr *addr); + size_t u_parse_ip6cidr(const char *str, struct in6_addr *netp, uint8_t *plen); int u_randbuf(void *buf, size_t buf_len, int *err); -- 2.20.0.rc1 |