Browse Source

fix ipv6 contains_addr function in wire

olinex 2 years ago
parent
commit
55366d4eec
1 changed files with 27 additions and 10 deletions
  1. 27 10
      src/wire/ipv6.rs

+ 27 - 10
src/wire/ipv6.rs

@@ -324,8 +324,7 @@ impl Cidr {
             return true;
         }
 
-        let shift = 128 - self.prefix_len;
-        self.address.mask(shift) == addr.mask(shift)
+        self.address.mask(self.prefix_len) == addr.mask(self.prefix_len)
     }
 
     /// Query whether the subnetwork described by this IPV6 CIDR block contains
@@ -875,21 +874,28 @@ mod test {
 
     #[test]
     fn test_cidr() {
-        let cidr = Cidr::new(LINK_LOCAL_ADDR, 64);
+        // fe80::1/56
+        // 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
+        // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
+        let cidr = Cidr::new(LINK_LOCAL_ADDR, 56);
 
         let inside_subnet = [
+            // fe80::2
             [
                 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                 0x00, 0x02,
             ],
+            // fe80::1122:3344:5566:7788
             [
                 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
                 0x77, 0x88,
             ],
+            // fe80::ff00:0:0:0
             [
                 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
                 0x00, 0x00,
             ],
+            // fe80::ff
             [
                 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                 0x00, 0xff,
@@ -897,18 +903,22 @@ mod test {
         ];
 
         let outside_subnet = [
+            // fe80:0:0:101::1
             [
-                0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                 0x00, 0x01,
             ],
+            // ::1
             [
                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                 0x00, 0x01,
             ],
+            // ff02::1
             [
                 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                 0x00, 0x01,
             ],
+            // ff02::2
             [
                 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                 0x00, 0x02,
@@ -916,6 +926,7 @@ mod test {
         ];
 
         let subnets = [
+            // fe80::ffff:ffff:ffff:ffff/65
             (
                 [
                     0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
@@ -923,6 +934,7 @@ mod test {
                 ],
                 65,
             ),
+            // fe80::1/128
             (
                 [
                     0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -930,6 +942,7 @@ mod test {
                 ],
                 128,
             ),
+            // fe80::1234:5678/96
             (
                 [
                     0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12,
@@ -940,27 +953,31 @@ mod test {
         ];
 
         let not_subnets = [
+            // fe80::101:ffff:ffff:ffff:ffff/55
             (
                 [
-                    0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
+                    0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff,
                     0xff, 0xff, 0xff,
                 ],
-                63,
+                55,
             ),
+            // fe80::101:ffff:ffff:ffff:ffff/56
             (
                 [
-                    0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff,
+                    0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff,
                     0xff, 0xff, 0xff,
                 ],
-                64,
+                56,
             ),
+            // fe80::101:ffff:ffff:ffff:ffff/57
             (
                 [
-                    0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff,
+                    0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff,
                     0xff, 0xff, 0xff,
                 ],
-                65,
+                57,
             ),
+            // ::1/128
             (
                 [
                     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,