From f7bf1abef9b4db7ffe18a6cf84db2aaef7d908d3 Mon Sep 17 00:00:00 2001 From: Susant Sahani Date: Sat, 6 May 2017 00:04:07 +0000 Subject: socket-util: add parse_ip_prefix (#5867) networkd: replace parse prefix with generic in_addr_prefix_from_string --- src/basic/in-addr-util.c | 42 ++++++++++++++++++++++++++++++++++++ src/basic/in-addr-util.h | 1 + src/network/networkd-address-label.c | 42 ++++-------------------------------- src/network/networkd-address-label.h | 1 - 4 files changed, 47 insertions(+), 39 deletions(-) diff --git a/src/basic/in-addr-util.c b/src/basic/in-addr-util.c index 3927df2955..d52fdad3ac 100644 --- a/src/basic/in-addr-util.c +++ b/src/basic/in-addr-util.c @@ -464,3 +464,45 @@ int in_addr_mask(int family, union in_addr_union *addr, unsigned char prefixlen) return -EAFNOSUPPORT; } + +int in_addr_prefix_from_string(const char *p, int family, union in_addr_union *ret_prefix, uint8_t *ret_prefixlen) { + union in_addr_union buffer; + const char *e, *l; + uint8_t k; + int r; + + assert(p); + + if (!IN_SET(family, AF_INET, AF_INET6)) + return -EAFNOSUPPORT; + + e = strchr(p, '/'); + if (e) + l = strndupa(p, e - p); + else + l = p; + + r = in_addr_from_string(family, l, &buffer); + if (r < 0) + return r; + + k = FAMILY_ADDRESS_SIZE(family) * 8; + + if (e) { + uint8_t n; + + r = safe_atou8(e + 1, &n); + if (r < 0) + return r; + + if (n > k) + return -ERANGE; + + k = n; + } + + *ret_prefix = buffer; + *ret_prefixlen = k; + + return 0; +} diff --git a/src/basic/in-addr-util.h b/src/basic/in-addr-util.h index 51a5aa67e4..14e27246b5 100644 --- a/src/basic/in-addr-util.h +++ b/src/basic/in-addr-util.h @@ -60,6 +60,7 @@ struct in_addr* in_addr_prefixlen_to_netmask(struct in_addr *addr, unsigned char int in_addr_default_prefixlen(const struct in_addr *addr, unsigned char *prefixlen); int in_addr_default_subnet_mask(const struct in_addr *addr, struct in_addr *mask); int in_addr_mask(int family, union in_addr_union *addr, unsigned char prefixlen); +int in_addr_prefix_from_string(const char *p, int family, union in_addr_union *ret_prefix, uint8_t *ret_prefixlen); static inline size_t FAMILY_ADDRESS_SIZE(int family) { assert(family == AF_INET || family == AF_INET6); diff --git a/src/network/networkd-address-label.c b/src/network/networkd-address-label.c index 1248719cf3..b89995ec44 100644 --- a/src/network/networkd-address-label.c +++ b/src/network/networkd-address-label.c @@ -117,7 +117,7 @@ int address_label_configure( assert(link->manager->rtnl); r = sd_rtnl_message_new_addrlabel(link->manager->rtnl, &req, RTM_NEWADDRLABEL, - link->ifindex, label->family); + link->ifindex, AF_INET6); if (r < 0) return log_error_errno(r, "Could not allocate RTM_NEWADDR message: %m"); @@ -155,9 +155,7 @@ int config_parse_address_label_prefix(const char *unit, _cleanup_address_label_free_ AddressLabel *n = NULL; Network *network = userdata; - const char *prefix, *e; - union in_addr_union buffer; - int r, f; + int r; assert(filename); assert(section); @@ -169,44 +167,12 @@ int config_parse_address_label_prefix(const char *unit, if (r < 0) return r; - /* AddressLabel=prefix/prefixlen */ - - /* prefixlen */ - e = strchr(rvalue, '/'); - if (e) { - unsigned i; - - r = safe_atou(e + 1, &i); - if (r < 0) { - log_syntax(unit, LOG_ERR, filename, line, r, "Prefix length is invalid, ignoring assignment: %s", e + 1); - return 0; - } - - if (i > 128) { - log_syntax(unit, LOG_ERR, filename, line, r, "Prefix length is out of range, ignoring assignment: %s", e + 1); - return 0; - } - - n->prefixlen = (unsigned char) i; - - prefix = strndupa(rvalue, e - rvalue); - } else - prefix = rvalue; - - r = in_addr_from_string_auto(prefix, &f, &buffer); + r = in_addr_prefix_from_string(rvalue, AF_INET6, &n->in_addr, &n->prefixlen); if (r < 0) { - log_syntax(unit, LOG_ERR, filename, line, r, "Address label is invalid, ignoring assignment: %s", prefix); - return 0; - } - - if (f != AF_INET6) { - log_syntax(unit, LOG_ERR, filename, line, 0, "Address label family is not IPv6, ignoring assignment: %s", prefix); + log_syntax(unit, LOG_ERR, filename, line, r, "Address label is invalid, ignoring assignment: %s", rvalue); return 0; } - n->family = f; - n->in_addr = buffer; - n = NULL; return 0; diff --git a/src/network/networkd-address-label.h b/src/network/networkd-address-label.h index 05bb24924c..8724ea8cb5 100644 --- a/src/network/networkd-address-label.h +++ b/src/network/networkd-address-label.h @@ -38,7 +38,6 @@ struct AddressLabel { Link *link; NetworkConfigSection *section; - int family; unsigned char prefixlen; uint32_t label; -- cgit v1.2.3-54-g00ecf