Skip to content

Commit

Permalink
Set network number to first IP in network
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
oschwald committed Jun 28, 2024
1 parent beb721f commit 81d3c39
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions traverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ func (r *Reader) NetworksWithin(network *net.IPNet, options ...NetworksOption) *
}

pointer, bit := r.traverseTree(ip, 0, uint(prefixLength))

if bit < prefixLength {
ip = ip.Mask(net.CIDRMask(bit, len(ip)*8))
}
networks.nodes = []netNode{
{
ip: ip,
Expand Down
21 changes: 21 additions & 0 deletions traverse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,27 @@ var tests = []networkTest{
"1.1.1.1/32",
},
},
{
Network: "1.1.1.2/32",
Database: "ipv4",
Expected: []string{
"1.1.1.2/31",
},
},
{
Network: "1.1.1.3/32",
Database: "ipv4",
Expected: []string{
"1.1.1.2/31",
},
},
{
Network: "1.1.1.19/32",
Database: "ipv4",
Expected: []string{
"1.1.1.16/28",
},
},
{
Network: "255.255.255.0/24",
Database: "ipv4",
Expand Down

0 comments on commit 81d3c39

Please sign in to comment.