@@ -939,7 +939,7 @@ bfd_forwarding__(struct bfd *bfd) OVS_REQUIRES(mutex)
static bool
bfd_lookup_ip(const char *host_name, struct in_addr *addr)
{
- if (!inet_pton(AF_INET, host_name, addr)) {
+ if (!ip_parse(host_name, &addr->s_addr)) {
VLOG_ERR_RL(&rl, "\"%s\" is not a valid IP address", host_name);
return false;
}
@@ -1426,18 +1426,16 @@ netdev_dummy_ip4addr(struct unixctl_conn *conn, int argc OVS_UNUSED,
struct netdev *netdev = netdev_from_name(argv[1]);
if (netdev && is_dummy_class(netdev->netdev_class)) {
- struct in_addr ip;
- uint16_t plen;
+ struct in_addr ip, mask;
+ char *error;
- if (ovs_scan(argv[2], IP_SCAN_FMT"/%"SCNi16,
- IP_SCAN_ARGS(&ip.s_addr), &plen)) {
- struct in_addr mask;
-
- mask.s_addr = be32_prefix_mask(plen);
+ error = ip_parse_masked(argv[2], &ip.s_addr, &mask.s_addr);
+ if (!error) {
netdev_dummy_set_in4(netdev, ip, mask);
unixctl_command_reply(conn, "OK");
} else {
- unixctl_command_reply_error(conn, "Invalid parameters");
+ unixctl_command_reply_error(conn, error);
+ free(error);
}
} else {
unixctl_command_reply_error(conn, "Unknown Dummy Interface");
@@ -199,79 +199,48 @@ rt_entry_delete(uint8_t priority, const struct in6_addr *ip6_dst, uint8_t plen)
static bool
scan_ipv6_route(const char *s, struct in6_addr *addr, unsigned int *plen)
{
- struct in6_addr mask;
- char *error;
-
- error = ipv6_parse_masked(s, addr, &mask);
+ char *error = ipv6_parse_cidr(s, addr, plen);
if (error) {
free(error);
return false;
}
-
- if (!ipv6_is_cidr(&mask)) {
- return false;
- }
-
- *plen = ipv6_count_cidr_bits(&mask);
-
return true;
}
static bool
scan_ipv4_route(const char *s, ovs_be32 *addr, unsigned int *plen)
{
- int len, max_plen, n;
- int slen = strlen(s);
- uint8_t *ip = (uint8_t *)addr;
-
- *addr = htonl(0);
- if (!ovs_scan(s, "%"SCNu8"%n", &ip[0], &n)) {
+ char *error = ip_parse_cidr(s, addr, plen);
+ if (error) {
+ free(error);
return false;
}
- len = n;
- max_plen = 8;
- for (int i = 1; i < 4; i++) {
- if (ovs_scan(s + len, ".%"SCNu8"%n", &ip[i], &n)) {
- len += n;
- max_plen += 8;
- } else {
- break;
- }
- }
- if (len == slen && max_plen == 32) {
- *plen = 32;
- return true;
- }
- if (ovs_scan(s + len, "/%u%n", plen, &n)
- && len + n == slen && *plen <= max_plen) {
- return true;
- }
- return false;
+ return true;
}
static void
ovs_router_add(struct unixctl_conn *conn, int argc,
const char *argv[], void *aux OVS_UNUSED)
{
- ovs_be32 ip, gw;
+ ovs_be32 ip;
unsigned int plen;
struct in6_addr ip6;
struct in6_addr gw6;
if (scan_ipv4_route(argv[1], &ip, &plen)) {
- if (argc > 3) {
- inet_pton(AF_INET, argv[3], (struct in_addr *)&gw);
- } else {
- gw = 0;
+ ovs_be32 gw = 0;
+ if (argc > 3 && !ip_parse(argv[3], &gw)) {
+ unixctl_command_reply_error(conn, "Invalid gateway");
+ return;
}
in6_addr_set_mapped_ipv4(&ip6, ip);
in6_addr_set_mapped_ipv4(&gw6, gw);
plen += 96;
} else if (scan_ipv6_route(argv[1], &ip6, &plen)) {
- if (argc > 3) {
- inet_pton(AF_INET6, argv[3], &gw6);
- } else {
- gw6 = in6addr_any;
+ gw6 = in6addr_exact;
+ if (argc > 3 && !ipv6_parse(argv[3], &gw6)) {
+ unixctl_command_reply_error(conn, "Invalid IPv6 gateway");
+ return;
}
} else {
unixctl_command_reply_error(conn, "Invalid parameters");
@@ -447,7 +447,7 @@ ip_parse_masked(const char *s, ovs_be32 *ip, ovs_be32 *mask)
"32", s);
}
*mask = be32_prefix_mask(prefix);
- } else if (ovs_scan(s, IP_SCAN_FMT"%n", IP_SCAN_ARGS(ip), &n) && !s[n]) {
+ } else if (ip_parse(s, ip)) {
*mask = OVS_BE32_MAX;
} else {
return xasprintf("%s: invalid IP address", s);
@@ -151,7 +151,7 @@ addr_is_ipv6(const char *host_name)
int
lookup_ip(const char *host_name, struct in_addr *addr)
{
- if (!inet_pton(AF_INET, host_name, addr)) {
+ if (!ip_parse(host_name, &addr->s_addr)) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
VLOG_ERR_RL(&rl, "\"%s\" is not a valid IP address", host_name);
return ENOENT;
@@ -165,7 +165,7 @@ lookup_ip(const char *host_name, struct in_addr *addr)
int
lookup_ipv6(const char *host_name, struct in6_addr *addr)
{
- if (inet_pton(AF_INET6, host_name, addr) != 1) {
+ if (!ipv6_parse(host_name, addr)) {
static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
VLOG_ERR_RL(&rl, "\"%s\" is not a valid IPv6 address", host_name);
return ENOENT;
@@ -188,7 +188,7 @@ lookup_hostname(const char *host_name, struct in_addr *addr)
struct addrinfo *result;
struct addrinfo hints;
- if (inet_pton(AF_INET, host_name, addr)) {
+ if (ip_parse(host_name, &addr->s_addr)) {
return 0;
}
@@ -371,14 +371,14 @@ parse_sockaddr_components(struct sockaddr_storage *ss,
sin6->sin6_family = AF_INET6;
sin6->sin6_port = htons(port);
- if (!inet_pton(AF_INET6, host_s, sin6->sin6_addr.s6_addr)) {
+ if (!ipv6_parse(host_s, &sin6->sin6_addr)) {
VLOG_ERR("%s: bad IPv6 address \"%s\"", s, host_s);
goto exit;
}
} else {
sin->sin_family = AF_INET;
sin->sin_port = htons(port);
- if (!inet_pton(AF_INET, host_s, &sin->sin_addr.s_addr)) {
+ if (!ip_parse(host_s, &sin->sin_addr.s_addr)) {
VLOG_ERR("%s: bad IPv4 address \"%s\"", s, host_s);
goto exit;
}
@@ -21,6 +21,7 @@
#include <stdarg.h>
#include "dynamic-string.h"
#include "json.h"
+#include "packets.h"
#include "util.h"
/* Returns a string that represents 'format'. */
@@ -338,13 +339,9 @@ lex_parse_integer__(const char *p, struct lex_token *token)
memcpy(copy, p, len);
copy[len] = '\0';
- struct in_addr ipv4;
- struct in6_addr ipv6;
- if (inet_pton(AF_INET, copy, &ipv4) == 1) {
- token->value.ipv4 = ipv4.s_addr;
+ if (ip_parse(copy, &token->value.ipv4)) {
token->format = LEX_F_IPV4;
- } else if (inet_pton(AF_INET6, copy, &ipv6) == 1) {
- token->value.ipv6 = ipv6;
+ } else if (ipv6_parse(copy, &token->value.ipv6)) {
token->format = LEX_F_IPV6;
} else {
lex_error(token, "Invalid numeric constant.");
@@ -3439,8 +3439,7 @@ bridge_configure_local_iface_netdev(struct bridge *br,
/* If there's no local interface or no IP address, give up. */
local_iface = iface_from_ofp_port(br, OFPP_LOCAL);
- if (!local_iface || !c->local_ip
- || !inet_pton(AF_INET, c->local_ip, &ip)) {
+ if (!local_iface || !c->local_ip || !ip_parse(c->local_ip, &ip.s_addr)) {
return;
}
@@ -3450,7 +3449,7 @@ bridge_configure_local_iface_netdev(struct bridge *br,
/* Configure the IP address and netmask. */
if (!c->local_netmask
- || !inet_pton(AF_INET, c->local_netmask, &mask)
+ || !ip_parse(c->local_netmask, &mask.s_addr)
|| !mask.s_addr) {
mask.s_addr = guess_netmask(ip.s_addr);
}
@@ -3461,7 +3460,7 @@ bridge_configure_local_iface_netdev(struct bridge *br,
/* Configure the default gateway. */
if (c->local_gateway
- && inet_pton(AF_INET, c->local_gateway, &gateway)
+ && ip_parse(c->local_gateway, &gateway.s_addr)
&& gateway.s_addr) {
if (!netdev_add_router(netdev, gateway)) {
VLOG_INFO("bridge %s: configured gateway "IP_FMT,
This saves some code and improves clarity, in my opinion. Some of these changes just change an inet_pton() call into a similar ip_parse() or ipv6_parse() call. In those cases the benefit is better type safety, since inet_pton()'s output parameter is type "void *". Signed-off-by: Ben Pfaff <blp@ovn.org> --- lib/bfd.c | 2 +- lib/netdev-dummy.c | 14 ++++++------- lib/ovs-router.c | 59 +++++++++++++----------------------------------------- lib/packets.c | 2 +- lib/socket-util.c | 10 ++++----- ovn/lib/lex.c | 9 +++------ vswitchd/bridge.c | 7 +++---- 7 files changed, 33 insertions(+), 70 deletions(-)