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.
This commit is contained in:
+18
-25
@@ -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;
|
||||
unsigned t, j = i - 1, p = j * 8;
|
||||
|
||||
if (j <= start_byte) {
|
||||
unsigned t, d;
|
||||
if (p >= prefixlen) {
|
||||
u->in6.s6_addr[j] = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
d = delta & 0xFF;
|
||||
delta >>= 8;
|
||||
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;
|
||||
}
|
||||
|
||||
t = u->in6.s6_addr[j] + d + overflow;
|
||||
overflow = t > UINT8_MAX ? t - UINT8_MAX : 0;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user