]> Sergey Matveev's repositories - btrtrc.git/blob - iplist/iplist.go
iplist: Fail invalid IPs, they were always passing
[btrtrc.git] / iplist / iplist.go
1 package iplist
2
3 import (
4         "bytes"
5         "errors"
6         "fmt"
7         "net"
8         "sort"
9 )
10
11 type IPList struct {
12         ranges []Range
13 }
14
15 type Range struct {
16         First, Last net.IP
17         Description string
18 }
19
20 func (r *Range) String() string {
21         return fmt.Sprintf("%s-%s (%s)", r.First, r.Last, r.Description)
22 }
23
24 // Create a new IP list. The given ranges must already sorted by the lower
25 // bound IP in each range. Behaviour is undefined for lists of overlapping
26 // ranges.
27 func New(initSorted []Range) *IPList {
28         return &IPList{
29                 ranges: initSorted,
30         }
31 }
32
33 func (me *IPList) NumRanges() int {
34         if me == nil {
35                 return 0
36         }
37         return len(me.ranges)
38 }
39
40 // Return the range the given IP is in. Returns nil if no range is found.
41 func (me *IPList) Lookup(ip net.IP) (r *Range) {
42         if me == nil {
43                 return nil
44         }
45         // TODO: Perhaps all addresses should be converted to IPv6, if the future
46         // of IP is to always be backwards compatible. But this will cost 4x the
47         // memory for IPv4 addresses?
48         v4 := ip.To4()
49         if v4 != nil {
50                 r = me.lookup(v4)
51                 if r != nil {
52                         return
53                 }
54         }
55         v6 := ip.To16()
56         if v6 != nil {
57                 return me.lookup(v6)
58         }
59         if v4 == nil && v6 == nil {
60                 return &Range{
61                         Description: fmt.Sprintf("unsupported IP: %s", ip),
62                 }
63         }
64         return nil
65 }
66
67 // Return the range the given IP is in. Returns nil if no range is found.
68 func (me *IPList) lookup(ip net.IP) (r *Range) {
69         // Find the index of the first range for which the following range exceeds
70         // it.
71         i := sort.Search(len(me.ranges), func(i int) bool {
72                 if i+1 >= len(me.ranges) {
73                         return true
74                 }
75                 return bytes.Compare(ip, me.ranges[i+1].First) < 0
76         })
77         if i == len(me.ranges) {
78                 return
79         }
80         r = &me.ranges[i]
81         if bytes.Compare(ip, r.First) < 0 || bytes.Compare(ip, r.Last) > 0 {
82                 r = nil
83         }
84         return
85 }
86
87 func minifyIP(ip *net.IP) {
88         v4 := ip.To4()
89         if v4 != nil {
90                 *ip = append(make([]byte, 0, 4), v4...)
91         }
92 }
93
94 // Parse a line of the PeerGuardian Text Lists (P2P) Format. Returns !ok but
95 // no error if a line doesn't contain a range but isn't erroneous, such as
96 // comment and blank lines.
97 func ParseBlocklistP2PLine(l []byte) (r Range, ok bool, err error) {
98         l = bytes.TrimSpace(l)
99         if len(l) == 0 || bytes.HasPrefix(l, []byte("#")) {
100                 return
101         }
102         // TODO: Check this when IPv6 blocklists are available.
103         colon := bytes.LastIndexAny(l, ":")
104         if colon == -1 {
105                 err = errors.New("missing colon")
106                 return
107         }
108         hyphen := bytes.IndexByte(l[colon+1:], '-')
109         if hyphen == -1 {
110                 err = errors.New("missing hyphen")
111                 return
112         }
113         hyphen += colon + 1
114         r.Description = string(l[:colon])
115         r.First = net.ParseIP(string(l[colon+1 : hyphen]))
116         minifyIP(&r.First)
117         r.Last = net.ParseIP(string(l[hyphen+1:]))
118         minifyIP(&r.Last)
119         if r.First == nil || r.Last == nil || len(r.First) != len(r.Last) {
120                 err = errors.New("bad IP range")
121                 return
122         }
123         ok = true
124         return
125 }