@@ -55,33 +55,31 @@ func getConfigFromNAD(
55
55
func GetRangeFromCIDR (
56
56
cidr netip.Prefix ,
57
57
) (start netip.Addr , end netip.Addr ) {
58
- // For IPv6, a /64 is expected, if the CIDR is aaaa:bbbb:cccc:dddd::/64,
59
- // the range is aaaa:bbbb:cccc:dddd::5 - aaaa:bbbb:cccc:dddd:ffff:ffff:ffff:fffe
60
- // For IPv4, a /16 is expected, if the CIDR is a.b.0.0/16
61
- // the range is a.b.0.5 - a.b.255.254
62
- // IPs from from 1 to 5 are reserved for later user
63
- addr := cidr .Addr ()
64
- if addr .Is6 () {
65
- addrBytes := addr .As16 ()
66
- for i := 8 ; i < 15 ; i ++ {
67
- addrBytes [i ] = 0
68
- }
69
- addrBytes [15 ] = 5
70
- start = netip .AddrFrom16 (addrBytes )
71
- for i := 8 ; i < 15 ; i ++ {
72
- addrBytes [i ] = 0xff
73
- }
74
- addrBytes [15 ] = 0xfe
75
- end = netip .AddrFrom16 (addrBytes )
76
- } else {
77
- addrBytes := addr .As4 ()
78
- addrBytes [2 ] = 0
79
- addrBytes [3 ] = 5
80
- start = netip .AddrFrom4 (addrBytes )
81
- addrBytes [2 ] = 0xff
82
- addrBytes [3 ] = 0xfe
83
- end = netip .AddrFrom4 (addrBytes )
58
+ // start is the 5th address of the Cidr
59
+ start = cidr .Masked ().Addr ()
60
+ for i := 0 ; i < 5 ; i ++ {
61
+ start = start .Next ()
62
+ }
63
+
64
+ bits := cidr .Bits ()
65
+ if start .Is4 () {
66
+ // Padding for ipv4 addresses in a [16]bytes table
67
+ bits += 96
68
+ }
69
+ // convert it to a [16]bytes table, set the remaining bits to 1
70
+ addrBytes := start .As16 ()
71
+ for b := bits ; b < 128 ; b ++ {
72
+ addrBytes [b / 8 ] |= 1 << uint (7 - (b % 8 ))
84
73
}
74
+ // convert the table to an ip address to get the last IP
75
+ // in case of IPv4, the address should be unmapped
76
+ last := netip .AddrFrom16 (addrBytes )
77
+ if start .Is4 () {
78
+ last = last .Unmap ()
79
+ }
80
+ // end is the 2nd last
81
+ end = last .Prev ()
82
+
85
83
return
86
84
}
87
85
0 commit comments