Skip to content

Commit 81d3c39

Browse files
committed
Set network number to first IP in network
when looking up a network that is more specific than the network in the database using NetworksWithin. Previously, the network number would be set to the network number of the provided *net.IPNet. This changes it to the canonical form.
1 parent beb721f commit 81d3c39

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

traverse.go

+4
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ func (r *Reader) NetworksWithin(network *net.IPNet, options ...NetworksOption) *
9595
}
9696

9797
pointer, bit := r.traverseTree(ip, 0, uint(prefixLength))
98+
99+
if bit < prefixLength {
100+
ip = ip.Mask(net.CIDRMask(bit, len(ip)*8))
101+
}
98102
networks.nodes = []netNode{
99103
{
100104
ip: ip,

traverse_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,27 @@ var tests = []networkTest{
8585
"1.1.1.1/32",
8686
},
8787
},
88+
{
89+
Network: "1.1.1.2/32",
90+
Database: "ipv4",
91+
Expected: []string{
92+
"1.1.1.2/31",
93+
},
94+
},
95+
{
96+
Network: "1.1.1.3/32",
97+
Database: "ipv4",
98+
Expected: []string{
99+
"1.1.1.2/31",
100+
},
101+
},
102+
{
103+
Network: "1.1.1.19/32",
104+
Database: "ipv4",
105+
Expected: []string{
106+
"1.1.1.16/28",
107+
},
108+
},
88109
{
89110
Network: "255.255.255.0/24",
90111
Database: "ipv4",

0 commit comments

Comments
 (0)