]> Sergey Matveev's repositories - btrtrc.git/blob - iplist/iplist.go
c102733d60f6d1668405a7d9466d00bcc16d0c63
[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 range must already sorted by the lower IP
25 // in the range. Behaviour is undefined for lists of overlapping ranges.
26 func New(initSorted []Range) *IPList {
27         return &IPList{
28                 ranges: initSorted,
29         }
30 }
31
32 func (me *IPList) NumRanges() int {
33         if me == nil {
34                 return 0
35         }
36         return len(me.ranges)
37 }
38
39 // Return the range the given IP is in. Returns nil if no range is found.
40 func (me *IPList) Lookup(ip net.IP) (r *Range) {
41         if me == nil {
42                 return nil
43         }
44         // TODO: Perhaps all addresses should be converted to IPv6, if the future
45         // of IP is to always be backwards compatible. But this will cost 4x the
46         // memory for IPv4 addresses?
47         if v4 := ip.To4(); v4 != nil {
48                 r = me.lookup(v4)
49                 if r != nil {
50                         return
51                 }
52         }
53         if v6 := ip.To16(); v6 != nil {
54                 return me.lookup(v6)
55         }
56         return nil
57 }
58
59 // Return the range the given IP is in. Returns nil if no range is found.
60 func (me *IPList) lookup(ip net.IP) (r *Range) {
61         // Find the index of the first range for which the following range exceeds
62         // it.
63         i := sort.Search(len(me.ranges), func(i int) bool {
64                 if i+1 >= len(me.ranges) {
65                         return true
66                 }
67                 return bytes.Compare(ip, me.ranges[i+1].First) < 0
68         })
69         if i == len(me.ranges) {
70                 return
71         }
72         r = &me.ranges[i]
73         if bytes.Compare(ip, r.First) < 0 || bytes.Compare(ip, r.Last) > 0 {
74                 r = nil
75         }
76         return
77 }
78
79 // Parse a line of the PeerGuardian Text Lists (P2P) Format. Returns !ok but
80 // no error if a line doesn't contain a range but isn't erroneous, such as
81 // comment and blank lines.
82 func ParseBlocklistP2PLine(l []byte) (r Range, ok bool, err error) {
83         l = bytes.TrimSpace(l)
84         if len(l) == 0 || bytes.HasPrefix(l, []byte("#")) {
85                 return
86         }
87         colon := bytes.IndexByte(l, ':')
88         if colon == -1 {
89                 err = errors.New("missing colon")
90                 return
91         }
92         hyphen := bytes.IndexByte(l[colon+1:], '-')
93         if hyphen == -1 {
94                 err = errors.New("missing hyphen")
95                 return
96         }
97         hyphen += colon + 1
98         r.Description = string(l[:colon])
99         r.First = net.ParseIP(string(l[colon+1 : hyphen]))
100         r.Last = net.ParseIP(string(l[hyphen+1:]))
101         if r.First == nil || r.Last == nil {
102                 return
103         }
104         ok = true
105         return
106 }