Skip to content

Commit

Permalink
device: simplify allowedips lookup signature
Browse files Browse the repository at this point in the history
The inliner should handle this for us.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
  • Loading branch information
zx2c4 committed Jun 3, 2021
1 parent 79a0dd6 commit 9252f58
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 17 deletions.
17 changes: 9 additions & 8 deletions device/allowedips.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
defer table.mutex.RUnlock()
return table.IPv4.lookup(address)
}

func (table *AllowedIPs) LookupIPv6(address []byte) *Peer {
table.mutex.RLock()
defer table.mutex.RUnlock()
return table.IPv6.lookup(address)
switch len(address) {
case net.IPv6len:
return table.IPv6.lookup(address)
case net.IPv4len:
return table.IPv4.lookup(address)
default:
panic(errors.New("looking up unknown address type"))
}
}
4 changes: 2 additions & 2 deletions device/allowedips_rand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ func TestTrieRandom(t *testing.T) {
var addr4 [4]byte
rand.Read(addr4[:])
peer1 := slow4.Lookup(addr4[:])
peer2 := allowedIPs.LookupIPv4(addr4[:])
peer2 := allowedIPs.Lookup(addr4[:])
if peer1 != peer2 {
t.Errorf("Trie did not match naive implementation, for %v: want %p, got %p", net.IP(addr4[:]), peer1, peer2)
}

var addr6 [16]byte
rand.Read(addr6[:])
peer1 = slow6.Lookup(addr6[:])
peer2 = allowedIPs.LookupIPv6(addr6[:])
peer2 = allowedIPs.Lookup(addr6[:])
if peer1 != peer2 {
t.Errorf("Trie did not match naive implementation, for %v: want %p, got %p", net.IP(addr6[:]), peer1, peer2)
}
Expand Down
6 changes: 3 additions & 3 deletions device/allowedips_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ func TestTrieIPv4(t *testing.T) {
}

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 {
t.Error("Assert EQ failed")
}
}

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 {
t.Error("Assert NEQ failed")
}
Expand Down Expand Up @@ -208,7 +208,7 @@ func TestTrieIPv6(t *testing.T) {
addr = append(addr, expand(b)...)
addr = append(addr, expand(c)...)
addr = append(addr, expand(d)...)
p := allowedIPs.LookupIPv6(addr)
p := allowedIPs.Lookup(addr)
if p != peer {
t.Error("Assert EQ failed")
}
Expand Down
4 changes: 2 additions & 2 deletions device/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ func (peer *Peer) RoutineSequentialReceiver() {
}
elem.packet = elem.packet[:length]
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)
goto skip
}
Expand All @@ -464,7 +464,7 @@ func (peer *Peer) RoutineSequentialReceiver() {
}
elem.packet = elem.packet[:length]
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)
goto skip
}
Expand Down
4 changes: 2 additions & 2 deletions device/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,14 @@ func (device *Device) RoutineReadFromTUN() {
continue
}
dst := elem.packet[IPv4offsetDst : IPv4offsetDst+net.IPv4len]
peer = device.allowedips.LookupIPv4(dst)
peer = device.allowedips.Lookup(dst)

case ipv6.Version:
if len(elem.packet) < ipv6.HeaderLen {
continue
}
dst := elem.packet[IPv6offsetDst : IPv6offsetDst+net.IPv6len]
peer = device.allowedips.LookupIPv6(dst)
peer = device.allowedips.Lookup(dst)

default:
device.log.Verbosef("Received packet with unknown IP version")
Expand Down

0 comments on commit 9252f58

Please sign in to comment.