From: Matt Joiner Date: Sat, 7 Mar 2015 06:09:39 +0000 (+1100) Subject: iplist: Check for matches against IPv4 and IPv6 addresses X-Git-Tag: v1.0.0~1299 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=881458d079ca6bbeb3ff1614e64af78998edc1e0;p=btrtrc.git iplist: Check for matches against IPv4 and IPv6 addresses --- diff --git a/iplist/iplist.go b/iplist/iplist.go index 2145b297..c102733d 100644 --- a/iplist/iplist.go +++ b/iplist/iplist.go @@ -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 {