summaryrefslogtreecommitdiff
path: root/accel-pppd
diff options
context:
space:
mode:
authorGuillaume Nault <g.nault@alphalink.fr>2018-12-07 17:37:42 +0100
committerDmitry Kozlov <xeb@mail.ru>2018-12-08 07:51:34 +0300
commit0f2f775d644c4cd9007b5816c2cfa9e8d933ef6d (patch)
treec487c625a8ce15b18c04c7936bfabd16efaa3f06 /accel-pppd
parent7bbe2a6d695dfffd52cae09b03af246329f96458 (diff)
downloadaccel-ppp-0f2f775d644c4cd9007b5816c2cfa9e8d933ef6d.tar.gz
accel-ppp-0f2f775d644c4cd9007b5816c2cfa9e8d933ef6d.zip
utils: rework u_parse_ip4addr()
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.nault@alphalink.fr>
Diffstat (limited to 'accel-pppd')
-rw-r--r--accel-pppd/iprange.c3
-rw-r--r--accel-pppd/utils.c51
-rw-r--r--accel-pppd/utils.h4
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 0295934f..bf58c145 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);