Mask IPs when showing from trie

Otherwise intermediate nodes that get collapsed to real nodes will
display the wrong value.
This commit is contained in:
Jason A. Donenfeld 2018-05-14 17:57:58 +02:00
parent a62c770a99
commit 7a1a537f43

View file

@ -16,7 +16,7 @@ import (
type trieEntry struct { type trieEntry struct {
cidr uint cidr uint
child [2]*trieEntry child [2]*trieEntry
bits []byte bits net.IP
peer *Peer peer *Peer
// index of "branching" bit // index of "branching" bit
@ -181,21 +181,11 @@ func (node *trieEntry) entriesForPeer(p *Peer, results []net.IPNet) []net.IPNet
return results return results
} }
if node.peer == p { if node.peer == p {
var mask net.IPNet mask := net.CIDRMask(int(node.cidr), len(node.bits)*8)
mask.Mask = net.CIDRMask(int(node.cidr), len(node.bits)*8) results = append(results, net.IPNet{
if len(node.bits) == net.IPv4len { Mask: mask,
mask.IP = net.IPv4( IP: node.bits.Mask(mask),
node.bits[0], })
node.bits[1],
node.bits[2],
node.bits[3],
)
} else if len(node.bits) == net.IPv6len {
mask.IP = node.bits
} else {
panic(errors.New("unexpected address length"))
}
results = append(results, mask)
} }
results = node.child[0].entriesForPeer(p, results) results = node.child[0].entriesForPeer(p, results)
results = node.child[1].entriesForPeer(p, results) results = node.child[1].entriesForPeer(p, results)