device: simplify allowedips lookup signature

The inliner should handle this for us.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2021-06-03 16:12:29 +02:00
parent c382222eab
commit 841756e328
5 changed files with 18 additions and 17 deletions

View file

@ -285,14 +285,15 @@ func (table *AllowedIPs) Insert(ip net.IP, cidr uint8, peer *Peer) {
} }
} }
func (table *AllowedIPs) LookupIPv4(address []byte) *Peer { func (table *AllowedIPs) Lookup(address []byte) *Peer {
table.mutex.RLock() table.mutex.RLock()
defer table.mutex.RUnlock() defer table.mutex.RUnlock()
return table.IPv4.lookup(address) switch len(address) {
} case net.IPv6len:
return table.IPv6.lookup(address)
func (table *AllowedIPs) LookupIPv6(address []byte) *Peer { case net.IPv4len:
table.mutex.RLock() return table.IPv4.lookup(address)
defer table.mutex.RUnlock() default:
return table.IPv6.lookup(address) panic(errors.New("looking up unknown address type"))
}
} }

View file

@ -108,7 +108,7 @@ func TestTrieRandom(t *testing.T) {
var addr4 [4]byte var addr4 [4]byte
rand.Read(addr4[:]) rand.Read(addr4[:])
peer1 := slow4.Lookup(addr4[:]) peer1 := slow4.Lookup(addr4[:])
peer2 := allowedIPs.LookupIPv4(addr4[:]) peer2 := allowedIPs.Lookup(addr4[:])
if peer1 != peer2 { if peer1 != peer2 {
t.Errorf("Trie did not match naive implementation, for %v: want %p, got %p", net.IP(addr4[:]), peer1, peer2) t.Errorf("Trie did not match naive implementation, for %v: want %p, got %p", net.IP(addr4[:]), peer1, peer2)
} }
@ -116,7 +116,7 @@ func TestTrieRandom(t *testing.T) {
var addr6 [16]byte var addr6 [16]byte
rand.Read(addr6[:]) rand.Read(addr6[:])
peer1 = slow6.Lookup(addr6[:]) peer1 = slow6.Lookup(addr6[:])
peer2 = allowedIPs.LookupIPv6(addr6[:]) peer2 = allowedIPs.Lookup(addr6[:])
if peer1 != peer2 { if peer1 != peer2 {
t.Errorf("Trie did not match naive implementation, for %v: want %p, got %p", net.IP(addr6[:]), peer1, peer2) t.Errorf("Trie did not match naive implementation, for %v: want %p, got %p", net.IP(addr6[:]), peer1, peer2)
} }

View file

@ -102,14 +102,14 @@ func TestTrieIPv4(t *testing.T) {
} }
assertEQ := func(peer *Peer, a, b, c, d byte) { assertEQ := func(peer *Peer, a, b, c, d byte) {
p := allowedIPs.LookupIPv4([]byte{a, b, c, d}) p := allowedIPs.Lookup([]byte{a, b, c, d})
if p != peer { if p != peer {
t.Error("Assert EQ failed") t.Error("Assert EQ failed")
} }
} }
assertNEQ := func(peer *Peer, a, b, c, d byte) { assertNEQ := func(peer *Peer, a, b, c, d byte) {
p := allowedIPs.LookupIPv4([]byte{a, b, c, d}) p := allowedIPs.Lookup([]byte{a, b, c, d})
if p == peer { if p == peer {
t.Error("Assert NEQ failed") t.Error("Assert NEQ failed")
} }
@ -208,7 +208,7 @@ func TestTrieIPv6(t *testing.T) {
addr = append(addr, expand(b)...) addr = append(addr, expand(b)...)
addr = append(addr, expand(c)...) addr = append(addr, expand(c)...)
addr = append(addr, expand(d)...) addr = append(addr, expand(d)...)
p := allowedIPs.LookupIPv6(addr) p := allowedIPs.Lookup(addr)
if p != peer { if p != peer {
t.Error("Assert EQ failed") t.Error("Assert EQ failed")
} }

View file

@ -447,7 +447,7 @@ func (peer *Peer) RoutineSequentialReceiver() {
} }
elem.packet = elem.packet[:length] elem.packet = elem.packet[:length]
src := elem.packet[IPv4offsetSrc : IPv4offsetSrc+net.IPv4len] src := elem.packet[IPv4offsetSrc : IPv4offsetSrc+net.IPv4len]
if device.allowedips.LookupIPv4(src) != peer { if device.allowedips.Lookup(src) != peer {
device.log.Verbosef("IPv4 packet with disallowed source address from %v", peer) device.log.Verbosef("IPv4 packet with disallowed source address from %v", peer)
goto skip goto skip
} }
@ -464,7 +464,7 @@ func (peer *Peer) RoutineSequentialReceiver() {
} }
elem.packet = elem.packet[:length] elem.packet = elem.packet[:length]
src := elem.packet[IPv6offsetSrc : IPv6offsetSrc+net.IPv6len] src := elem.packet[IPv6offsetSrc : IPv6offsetSrc+net.IPv6len]
if device.allowedips.LookupIPv6(src) != peer { if device.allowedips.Lookup(src) != peer {
device.log.Verbosef("IPv6 packet with disallowed source address from %v", peer) device.log.Verbosef("IPv6 packet with disallowed source address from %v", peer)
goto skip goto skip
} }

View file

@ -254,14 +254,14 @@ func (device *Device) RoutineReadFromTUN() {
continue continue
} }
dst := elem.packet[IPv4offsetDst : IPv4offsetDst+net.IPv4len] dst := elem.packet[IPv4offsetDst : IPv4offsetDst+net.IPv4len]
peer = device.allowedips.LookupIPv4(dst) peer = device.allowedips.Lookup(dst)
case ipv6.Version: case ipv6.Version:
if len(elem.packet) < ipv6.HeaderLen { if len(elem.packet) < ipv6.HeaderLen {
continue continue
} }
dst := elem.packet[IPv6offsetDst : IPv6offsetDst+net.IPv6len] dst := elem.packet[IPv6offsetDst : IPv6offsetDst+net.IPv6len]
peer = device.allowedips.LookupIPv6(dst) peer = device.allowedips.Lookup(dst)
default: default:
device.log.Verbosef("Received packet with unknown IP version") device.log.Verbosef("Received packet with unknown IP version")