in-addr-util: drop unused in_addr_prefix_nth()
This commit is contained in:
@@ -264,84 +264,6 @@ int in_addr_prefix_intersect(
|
||||
return -EAFNOSUPPORT;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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 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.
|
||||
*
|
||||
* Examples:
|
||||
* - in_addr_prefix_nth(AF_INET, 192.168.0.0, 24, 2), returns 0, writes 192.168.2.0 to u
|
||||
* - in_addr_prefix_nth(AF_INET, 192.168.0.0, 24, 0), returns 0, no data written
|
||||
* - in_addr_prefix_nth(AF_INET, 255.255.255.0, 24, 1), returns -ERANGE, no data written
|
||||
* - in_addr_prefix_nth(AF_INET, 255.255.255.0, 0, 1), returns -ERANGE, no data written
|
||||
* - in_addr_prefix_nth(AF_INET6, 2001:db8, 64, 0xff00) returns 0, writes 2001:0db8:0000:ff00:: to u
|
||||
*/
|
||||
int in_addr_prefix_nth(int family, union in_addr_union *u, unsigned prefixlen, uint64_t nth) {
|
||||
assert(u);
|
||||
|
||||
if (prefixlen <= 0)
|
||||
return -ERANGE;
|
||||
|
||||
if (family == AF_INET) {
|
||||
uint32_t c, n, t;
|
||||
|
||||
if (prefixlen > 32)
|
||||
return -ERANGE;
|
||||
|
||||
c = be32toh(u->in.s_addr);
|
||||
|
||||
t = nth << (32 - prefixlen);
|
||||
|
||||
/* Check for wrap */
|
||||
if (c > UINT32_MAX - t)
|
||||
return -ERANGE;
|
||||
|
||||
n = c + t;
|
||||
|
||||
n &= UINT32_C(0xFFFFFFFF) << (32 - prefixlen);
|
||||
u->in.s_addr = htobe32(n);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (family == AF_INET6) {
|
||||
bool overflow = false;
|
||||
|
||||
if (prefixlen > 128)
|
||||
return -ERANGE;
|
||||
|
||||
for (unsigned i = 16; i > 0; i--) {
|
||||
unsigned t, j = i - 1, p = j * 8;
|
||||
|
||||
if (p >= prefixlen) {
|
||||
u->in6.s6_addr[j] = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
overflow = t > UINT8_MAX;
|
||||
u->in6.s6_addr[j] = (uint8_t) (t & 0xff);
|
||||
}
|
||||
|
||||
if (overflow || nth != 0)
|
||||
return -ERANGE;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -EAFNOSUPPORT;
|
||||
}
|
||||
|
||||
int in_addr_random_prefix(
|
||||
int family,
|
||||
union in_addr_union *u,
|
||||
|
||||
@@ -73,7 +73,6 @@ int in_addr_prefix_intersect(
|
||||
unsigned aprefixlen,
|
||||
const union in_addr_union *b,
|
||||
unsigned bprefixlen) _pure_;
|
||||
int in_addr_prefix_nth(int family, union in_addr_union *u, unsigned prefixlen, uint64_t nth);
|
||||
int in_addr_random_prefix(int family, union in_addr_union *u, unsigned prefixlen_fixed_part, unsigned prefixlen);
|
||||
int in_addr_prefix_range(
|
||||
int family,
|
||||
|
||||
@@ -213,47 +213,6 @@ TEST(in_addr_prefix_intersect) {
|
||||
test_in_addr_prefix_intersect_one(AF_INET6, "1::1", 121, "1::00ff", 121, 0);
|
||||
}
|
||||
|
||||
static void test_in_addr_prefix_nth_one(unsigned f, const char *before, unsigned pl, uint64_t nth, const char *after) {
|
||||
union in_addr_union ubefore, uafter, t;
|
||||
|
||||
log_debug("/* %s(%s, prefixlen=%u, nth=%"PRIu64") */", __func__, before, pl, nth);
|
||||
|
||||
assert_se(in_addr_from_string(f, before, &ubefore) >= 0);
|
||||
|
||||
t = ubefore;
|
||||
assert_se((in_addr_prefix_nth(f, &t, pl, nth) >= 0) == !!after);
|
||||
|
||||
if (after) {
|
||||
assert_se(in_addr_from_string(f, after, &uafter) >= 0);
|
||||
assert_se(in_addr_equal(f, &t, &uafter) > 0);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(in_addr_prefix_nth) {
|
||||
test_in_addr_prefix_nth_one(AF_INET, "192.168.0.0", 24, 0, "192.168.0.0");
|
||||
test_in_addr_prefix_nth_one(AF_INET, "192.168.0.123", 24, 0, "192.168.0.0");
|
||||
test_in_addr_prefix_nth_one(AF_INET, "192.168.0.123", 24, 1, "192.168.1.0");
|
||||
test_in_addr_prefix_nth_one(AF_INET, "192.168.0.0", 24, 4, "192.168.4.0");
|
||||
test_in_addr_prefix_nth_one(AF_INET, "192.168.0.0", 25, 1, "192.168.0.128");
|
||||
test_in_addr_prefix_nth_one(AF_INET, "192.168.255.0", 25, 1, "192.168.255.128");
|
||||
test_in_addr_prefix_nth_one(AF_INET, "192.168.255.0", 24, 0, "192.168.255.0");
|
||||
test_in_addr_prefix_nth_one(AF_INET, "255.255.255.255", 32, 1, NULL);
|
||||
test_in_addr_prefix_nth_one(AF_INET, "255.255.255.255", 0, 1, NULL);
|
||||
|
||||
test_in_addr_prefix_nth_one(AF_INET6, "4400::", 8, 1, "4500::");
|
||||
test_in_addr_prefix_nth_one(AF_INET6, "4400::", 7, 1, "4600::");
|
||||
test_in_addr_prefix_nth_one(AF_INET6, "4400::", 64, 1, "4400:0:0:1::");
|
||||
test_in_addr_prefix_nth_one(AF_INET6, "4400::", 64, 2, "4400:0:0:2::");
|
||||
test_in_addr_prefix_nth_one(AF_INET6, "4400::", 64, 0xbad, "4400:0:0:0bad::");
|
||||
test_in_addr_prefix_nth_one(AF_INET6, "4400:0:0:ffff::", 64, 1, "4400:0:1::");
|
||||
test_in_addr_prefix_nth_one(AF_INET6, "4400::", 56, ((uint64_t)1<<48) -1, "44ff:ffff:ffff:ff00::");
|
||||
test_in_addr_prefix_nth_one(AF_INET6, "0000::", 8, 255, "ff00::");
|
||||
test_in_addr_prefix_nth_one(AF_INET6, "0000::", 8, 256, NULL);
|
||||
test_in_addr_prefix_nth_one(AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 128, 1, NULL);
|
||||
test_in_addr_prefix_nth_one(AF_INET6, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 0, 1, NULL);
|
||||
test_in_addr_prefix_nth_one(AF_INET6, "1234:5678:90ab:cdef:1234:5678:90ab:cdef", 12, 1, "1240::");
|
||||
}
|
||||
|
||||
static void test_in_addr_prefix_range_one(
|
||||
int family,
|
||||
const char *in,
|
||||
|
||||
Reference in New Issue
Block a user