提交 0f2f775d 编写于 作者: G Guillaume Nault 提交者: Dmitry Kozlov

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: NGuillaume Nault <g.nault@alphalink.fr>
上级 7bbe2a6d
......@@ -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);
......
#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.
......
......@@ -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);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册