summaryrefslogtreecommitdiffstats
path: root/src/basic/in-addr-util.c
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2021-02-17 14:55:01 +0100
committerYu Watanabe <watanabe.yu+github@gmail.com>2021-02-17 14:57:37 +0100
commit1534c5791ae8b581df4cc8fc321cc6a757998be0 (patch)
tree5131841c3e518edb0cc886a07da62de04afa36f7 /src/basic/in-addr-util.c
parentin-addr-util: make in_addr_prefix_nth() always return valid prefix (diff)
downloadsystemd-1534c5791ae8b581df4cc8fc321cc6a757998be0.tar.xz
systemd-1534c5791ae8b581df4cc8fc321cc6a757998be0.zip
in-addr-util: introduce in_addr_prefix_range()
This will replace nft_in6addr_to_range() in later commit.
Diffstat (limited to 'src/basic/in-addr-util.c')
-rw-r--r--src/basic/in-addr-util.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/basic/in-addr-util.c b/src/basic/in-addr-util.c
index aa681b7bb7..641c5de7a3 100644
--- a/src/basic/in-addr-util.c
+++ b/src/basic/in-addr-util.c
@@ -350,6 +350,43 @@ int in_addr_random_prefix(
return -EAFNOSUPPORT;
}
+int in_addr_prefix_range(
+ int family,
+ const union in_addr_union *in,
+ unsigned prefixlen,
+ union in_addr_union *ret_start,
+ union in_addr_union *ret_end) {
+
+ union in_addr_union start, end;
+ int r;
+
+ assert(in);
+
+ if (!IN_SET(family, AF_INET, AF_INET6))
+ return -EAFNOSUPPORT;
+
+ if (ret_start) {
+ start = *in;
+ r = in_addr_prefix_nth(family, &start, prefixlen, 0);
+ if (r < 0)
+ return r;
+ }
+
+ if (ret_end) {
+ end = *in;
+ r = in_addr_prefix_nth(family, &end, prefixlen, 1);
+ if (r < 0)
+ return r;
+ }
+
+ if (ret_start)
+ *ret_start = start;
+ if (ret_end)
+ *ret_end = end;
+
+ return 0;
+}
+
int in_addr_to_string(int family, const union in_addr_union *u, char **ret) {
_cleanup_free_ char *x = NULL;
size_t l;