summaryrefslogtreecommitdiffstats
path: root/src/basic/in-addr-util.c
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2021-02-17 12:01:26 +0100
committerYu Watanabe <watanabe.yu+github@gmail.com>2021-02-17 14:57:37 +0100
commit9164338b2e6943ded69f623f2c39a46cb94d35dd (patch)
treec252b09aab380c1954325bf9028a325bb79dc7c5 /src/basic/in-addr-util.c
parentin-addr-util: make in_addr_prefix_nth() refuse prefixlen larger than maximum ... (diff)
downloadsystemd-9164338b2e6943ded69f623f2c39a46cb94d35dd.tar.xz
systemd-9164338b2e6943ded69f623f2c39a46cb94d35dd.zip
in-addr-util: make in_addr_prefix_nth() always return valid prefix
Previously, e.g. in_addr_prefix_nth(2400::1, prefixlen=32, nth=1) does not return 2400:1:: but does 2400:1::1.
Diffstat (limited to 'src/basic/in-addr-util.c')
-rw-r--r--src/basic/in-addr-util.c43
1 files changed, 18 insertions, 25 deletions
diff --git a/src/basic/in-addr-util.c b/src/basic/in-addr-util.c
index 343f62c5dc..aa681b7bb7 100644
--- a/src/basic/in-addr-util.c
+++ b/src/basic/in-addr-util.c
@@ -199,8 +199,7 @@ int in_addr_prefix_next(int family, union in_addr_union *u, unsigned prefixlen)
* Calculates the nth prefix of size prefixlen starting from the address denoted by u.
*
* On success 0 will be returned and the calculated prefix will be available in
- * u. In the case nth == 0 the input will be left unchanged and 0 will be returned.
- * In case the calculation cannot be performed (invalid prefix length,
+ * u. In case the calculation cannot be performed (invalid prefix length,
* overflows would occur) -ERANGE is returned. If the address family given isn't
* supported -EAFNOSUPPORT will be returned.
*
@@ -217,9 +216,6 @@ int in_addr_prefix_nth(int family, union in_addr_union *u, unsigned prefixlen, u
if (prefixlen <= 0)
return -ERANGE;
- if (nth == 0)
- return 0;
-
if (family == AF_INET) {
uint32_t c, n, t;
@@ -242,38 +238,35 @@ int in_addr_prefix_nth(int family, union in_addr_union *u, unsigned prefixlen, u
}
if (family == AF_INET6) {
- struct in6_addr result = {};
- uint8_t overflow = 0;
- uint64_t delta; /* this assumes that we only ever have to up to 1<<64 subnets */
- unsigned start_byte = (prefixlen - 1) / 8;
+ bool overflow = false;
if (prefixlen > 128)
return -ERANGE;
- /* First calculate what we have to add */
- delta = nth << ((128 - prefixlen) % 8);
-
for (unsigned i = 16; i > 0; i--) {
- unsigned j = i - 1;
-
- if (j <= start_byte) {
- unsigned t, d;
+ unsigned t, j = i - 1, p = j * 8;
- d = delta & 0xFF;
- delta >>= 8;
+ if (p >= prefixlen) {
+ u->in6.s6_addr[j] = 0;
+ continue;
+ }
- t = u->in6.s6_addr[j] + d + overflow;
- overflow = t > UINT8_MAX ? t - UINT8_MAX : 0;
+ if (prefixlen - p < 8) {
+ u->in6.s6_addr[j] &= 0xff << (8 - (prefixlen - p));
+ t = u->in6.s6_addr[j] + ((nth & 0xff) << (8 - (prefixlen - p)));
+ nth >>= prefixlen - p;
+ } else {
+ t = u->in6.s6_addr[j] + (nth & 0xff) + overflow;
+ nth >>= 8;
+ }
- result.s6_addr[j] = (uint8_t) t;
- } else
- result.s6_addr[j] = u->in6.s6_addr[j];
+ overflow = t > UINT8_MAX;
+ u->in6.s6_addr[j] = (uint8_t) (t & 0xff);
}
- if (overflow || delta != 0)
+ if (overflow || nth != 0)
return -ERANGE;
- u->in6 = result;
return 0;
}