]> Sergey Matveev's repositories - btrtrc.git/commitdiff
iplist: Check for matches against IPv4 and IPv6 addresses
authorMatt Joiner <anacrolix@gmail.com>
Sat, 7 Mar 2015 06:09:39 +0000 (17:09 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Sat, 7 Mar 2015 06:09:39 +0000 (17:09 +1100)
iplist/iplist.go

index 2145b297fc5a1dc9fafabe3d8527fbd9628d40c3..c102733d60f6d1668405a7d9466d00bcc16d0c63 100644 (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 {