]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
in-addr-util: make in_addr_prefix_nth() always return valid prefix
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 17 Feb 2021 11:01:26 +0000 (20:01 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 17 Feb 2021 13:57:37 +0000 (22:57 +0900)
Previously, e.g. in_addr_prefix_nth(2400::1, prefixlen=32, nth=1)
does not return 2400:1:: but does 2400:1::1.

src/basic/in-addr-util.c
src/test/test-in-addr-util.c

index 343f62c5dc16e323f009ddd6ab0d4e2b4f54ed8b..aa681b7bb79528f8f968809fe33c5c41d00f3e8e 100644 (file)
@@ -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;
         }
 
index beab1caf850ff2885ce8d0f9cb2d042a2ca7c634..c2cf26d7206046d33319ce569df61ad9bfe3c03c 100644 (file)
@@ -238,6 +238,8 @@ static void test_in_addr_prefix_intersect(void) {
 static void test_in_addr_prefix_next_one(unsigned f, const char *before, unsigned pl, const char *after) {
         union in_addr_union ubefore, uafter, t;
 
+        log_info("/* %s(%s, prefixlen=%u) */", __func__, before, pl);
+
         assert_se(in_addr_from_string(f, before, &ubefore) >= 0);
 
         t = ubefore;
@@ -250,13 +252,12 @@ static void test_in_addr_prefix_next_one(unsigned f, const char *before, unsigne
 }
 
 static void test_in_addr_prefix_next(void) {
-        log_info("/* %s */", __func__);
-
         test_in_addr_prefix_next_one(AF_INET, "192.168.0.0", 24, "192.168.1.0");
         test_in_addr_prefix_next_one(AF_INET, "192.168.0.0", 16, "192.169.0.0");
         test_in_addr_prefix_next_one(AF_INET, "192.168.0.0", 20, "192.168.16.0");
 
         test_in_addr_prefix_next_one(AF_INET, "0.0.0.0", 32, "0.0.0.1");
+        test_in_addr_prefix_next_one(AF_INET, "255.255.255.254", 32, "255.255.255.255");
         test_in_addr_prefix_next_one(AF_INET, "255.255.255.255", 32, NULL);
         test_in_addr_prefix_next_one(AF_INET, "255.255.255.0", 24, NULL);
 
@@ -275,6 +276,8 @@ static void test_in_addr_prefix_next(void) {
 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_info("/* %s(%s, prefixlen=%u, nth=%"PRIu64") */", __func__, before, pl, nth);
+
         assert_se(in_addr_from_string(f, before, &ubefore) >= 0);
 
         t = ubefore;
@@ -287,10 +290,9 @@ static void test_in_addr_prefix_nth_one(unsigned f, const char *before, unsigned
 }
 
 static void test_in_addr_prefix_nth(void) {
-        log_info("/* %s */", __func__);
-
         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.0", 24, 1, "192.168.1.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");
@@ -309,6 +311,7 @@ static void test_in_addr_prefix_nth(void) {
         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_to_string_one(int f, const char *addr) {