2
0
mirror of synced 2025-02-23 14:18:13 +00:00

iplist: Check for matches against IPv4 and IPv6 addresses

This commit is contained in:
Matt Joiner 2015-03-07 17:09:39 +11:00
parent 1c5bd1855a
commit 881458d079

View File

@ -41,6 +41,23 @@ func (me *IPList) Lookup(ip net.IP) (r *Range) {
if me == nil {
return nil
}
// TODO: Perhaps all addresses should be converted to IPv6, if the future
// of IP is to always be backwards compatible. But this will cost 4x the
// memory for IPv4 addresses?
if v4 := ip.To4(); v4 != nil {
r = me.lookup(v4)
if r != nil {
return
}
}
if v6 := ip.To16(); v6 != nil {
return me.lookup(v6)
}
return nil
}
// Return the range the given IP is in. Returns nil if no range is found.
func (me *IPList) lookup(ip net.IP) (r *Range) {
// Find the index of the first range for which the following range exceeds
// it.
i := sort.Search(len(me.ranges), func(i int) bool {