Skip to content

Commit

Permalink
Add helper function for mapping IP
Browse files Browse the repository at this point in the history
  • Loading branch information
oschwald committed Nov 12, 2024
1 parent b4d8464 commit f509ad1
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions traverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,8 @@ func (r *Reader) NetworksWithin(prefix netip.Prefix, options ...NetworksOption)
for {
if node.pointer == r.Metadata.NodeCount {
if n.includeEmptyNetworks {
ip := node.ip
if isInIPv4Subtree(ip) {
ip = v6ToV4(ip)
}

ok := yield(Result{
ip: ip,
ip: mappedIP(node.ip),
offset: notFound,
prefixLen: uint8(node.bit),
})
Expand All @@ -146,15 +141,10 @@ func (r *Reader) NetworksWithin(prefix netip.Prefix, options ...NetworksOption)
}

if node.pointer > r.Metadata.NodeCount {
ip := node.ip
if isInIPv4Subtree(ip) {
ip = v6ToV4(ip)
}

offset, err := r.resolveDataPointer(node.pointer)
ok := yield(Result{
decoder: r.decoder,
ip: ip,
ip: mappedIP(node.ip),
offset: uint(offset),
prefixLen: uint8(node.bit),
err: err,
Expand Down Expand Up @@ -202,6 +192,13 @@ func (r *Reader) NetworksWithin(prefix netip.Prefix, options ...NetworksOption)

var ipv4SubtreeBoundary = netip.MustParseAddr("::255.255.255.255").Next()

func mappedIP(ip netip.Addr) netip.Addr {
if isInIPv4Subtree(ip) {
return v6ToV4(ip)
}
return ip
}

// isInIPv4Subtree returns true if the IP is in the database's IPv4 subtree.
func isInIPv4Subtree(ip netip.Addr) bool {
return ip.Is4() || ip.Less(ipv4SubtreeBoundary)
Expand Down

0 comments on commit f509ad1

Please sign in to comment.