summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2018-11-05 08:18:17 +0100
committerYu Watanabe <watanabe.yu+github@gmail.com>2018-11-05 08:46:26 +0100
commita4798d4e6d88c2be2625ce62d8326eaa9b80f2ee (patch)
treed5f1f6594f37fdcd1f28d61b03c03c0194b7ad75
parentutil: use strndup() instead of strndupa() (diff)
downloadsystemd-a4798d4e6d88c2be2625ce62d8326eaa9b80f2ee.tar.xz
systemd-a4798d4e6d88c2be2625ce62d8326eaa9b80f2ee.zip
util: introduce in_addr_default_prefix_from_string() and friends
It is similar to in_addr_prefix_from_string() but it determines the prefix length from address if it is not specified.
-rw-r--r--src/basic/in-addr-util.c14
-rw-r--r--src/basic/in-addr-util.h16
2 files changed, 26 insertions, 4 deletions
diff --git a/src/basic/in-addr-util.c b/src/basic/in-addr-util.c
index 9a6b1b3d61..4cf6ab6974 100644
--- a/src/basic/in-addr-util.c
+++ b/src/basic/in-addr-util.c
@@ -495,8 +495,9 @@ int in_addr_parse_prefixlen(int family, const char *p, unsigned char *ret) {
return 0;
}
-int in_addr_prefix_from_string(
+int in_addr_prefix_from_string_internal(
const char *p,
+ bool use_default_prefixlen,
int family,
union in_addr_union *ret_prefix,
unsigned char *ret_prefixlen) {
@@ -530,6 +531,10 @@ int in_addr_prefix_from_string(
r = in_addr_parse_prefixlen(family, e+1, &k);
if (r < 0)
return r;
+ } else if (family == AF_INET && use_default_prefixlen) {
+ r = in4_addr_default_prefixlen(&buffer.in, &k);
+ if (r < 0)
+ return r;
} else
k = FAMILY_ADDRESS_SIZE(family) * 8;
@@ -541,8 +546,9 @@ int in_addr_prefix_from_string(
return 0;
}
-int in_addr_prefix_from_string_auto(
+int in_addr_prefix_from_string_auto_internal(
const char *p,
+ bool use_default_prefixlen,
int *ret_family,
union in_addr_union *ret_prefix,
unsigned char *ret_prefixlen) {
@@ -573,6 +579,10 @@ int in_addr_prefix_from_string_auto(
r = in_addr_parse_prefixlen(family, e+1, &k);
if (r < 0)
return r;
+ } else if (family == AF_INET && use_default_prefixlen) {
+ r = in4_addr_default_prefixlen(&buffer.in, &k);
+ if (r < 0)
+ return r;
} else
k = FAMILY_ADDRESS_SIZE(family) * 8;
diff --git a/src/basic/in-addr-util.h b/src/basic/in-addr-util.h
index e4be30dc0d..2dafa3c084 100644
--- a/src/basic/in-addr-util.h
+++ b/src/basic/in-addr-util.h
@@ -45,8 +45,20 @@ int in4_addr_default_subnet_mask(const struct in_addr *addr, struct in_addr *mas
int in_addr_mask(int family, union in_addr_union *addr, unsigned char prefixlen);
int in_addr_prefix_covers(int family, const union in_addr_union *prefix, unsigned char prefixlen, const union in_addr_union *address);
int in_addr_parse_prefixlen(int family, const char *p, unsigned char *ret);
-int in_addr_prefix_from_string(const char *p, int family, union in_addr_union *ret_prefix, unsigned char *ret_prefixlen);
-int in_addr_prefix_from_string_auto(const char *p, int *ret_family, union in_addr_union *ret_prefix, unsigned char *ret_prefixlen);
+int in_addr_prefix_from_string_internal(const char *p, bool use_default_prefixlen, int family, union in_addr_union *ret_prefix, unsigned char *ret_prefixlen);
+int in_addr_prefix_from_string_auto_internal(const char *p, bool use_default_prefixlen, int *ret_family, union in_addr_union *ret_prefix, unsigned char *ret_prefixlen);
+static inline int in_addr_prefix_from_string(const char *p, int family, union in_addr_union *ret_prefix, unsigned char *ret_prefixlen) {
+ return in_addr_prefix_from_string_internal(p, false, family, ret_prefix, ret_prefixlen);
+}
+static inline int in_addr_prefix_from_string_auto(const char *p, int *ret_family, union in_addr_union *ret_prefix, unsigned char *ret_prefixlen) {
+ return in_addr_prefix_from_string_auto_internal(p, false, ret_family, ret_prefix, ret_prefixlen);
+}
+static inline int in_addr_default_prefix_from_string(const char *p, int family, union in_addr_union *ret_prefix, unsigned char *ret_prefixlen) {
+ return in_addr_prefix_from_string_internal(p, true, family, ret_prefix, ret_prefixlen);
+}
+static inline int in_addr_default_prefix_from_string_auto(const char *p, int *ret_family, union in_addr_union *ret_prefix, unsigned char *ret_prefixlen) {
+ return in_addr_prefix_from_string_auto_internal(p, true, ret_family, ret_prefix, ret_prefixlen);
+}
static inline size_t FAMILY_ADDRESS_SIZE(int family) {
assert(IN_SET(family, AF_INET, AF_INET6));