aboutsummaryrefslogtreecommitdiffstats
path: root/device/allowedips_rand_test.go
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2022-03-16 19:34:42 -0600
committerJason A. Donenfeld <Jason@zx2c4.com>2022-03-16 19:45:10 -0600
commitf3aff443a6e829519c4144b8c523d1335a8e66ef (patch)
tree437fe987b6f95965009ab6d876670777246a07be /device/allowedips_rand_test.go
parenttun/netstack: bump mod (diff)
downloadwireguard-go-f3aff443a6e829519c4144b8c523d1335a8e66ef.tar.xz
wireguard-go-f3aff443a6e829519c4144b8c523d1335a8e66ef.zip
device: make allowedips genericjd/generic-aip
The implementation of commonBits uses a horrific unsafe.Slice trick. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'device/allowedips_rand_test.go')
-rw-r--r--device/allowedips_rand_test.go13
1 files changed, 11 insertions, 2 deletions
diff --git a/device/allowedips_rand_test.go b/device/allowedips_rand_test.go
index 0d3eecb..8c17d02 100644
--- a/device/allowedips_rand_test.go
+++ b/device/allowedips_rand_test.go
@@ -40,9 +40,18 @@ func (r SlowRouter) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}
+func commonBitsSlice(addr1, addr2 []byte) uint8 {
+ if len(addr1) == 4 {
+ return commonBits4(*(*[4]byte)(addr1), *(*[4]byte)(addr2))
+ } else if len(addr1) == 16 {
+ return commonBits16(*(*[16]byte)(addr1), *(*[16]byte)(addr2))
+ }
+ return 0
+}
+
func (r SlowRouter) Insert(addr []byte, cidr uint8, peer *Peer) SlowRouter {
for _, t := range r {
- if t.cidr == cidr && commonBits(t.bits, addr) >= cidr {
+ if t.cidr == cidr && commonBitsSlice(t.bits, addr) >= cidr {
t.peer = peer
t.bits = addr
return r
@@ -59,7 +68,7 @@ func (r SlowRouter) Insert(addr []byte, cidr uint8, peer *Peer) SlowRouter {
func (r SlowRouter) Lookup(addr []byte) *Peer {
for _, t := range r {
- common := commonBits(t.bits, addr)
+ common := commonBitsSlice(t.bits, addr)
if common >= t.cidr {
return t.peer
}