]> Sergey Matveev's repositories - btrtrc.git/blob - iplist/iplist.go
2145b297fc5a1dc9fafabe3d8527fbd9628d40c3
[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         // Find the index of the first range for which the following range exceeds
45         // it.
46         i := sort.Search(len(me.ranges), func(i int) bool {
47                 if i+1 >= len(me.ranges) {
48                         return true
49                 }
50                 return bytes.Compare(ip, me.ranges[i+1].First) < 0
51         })
52         if i == len(me.ranges) {
53                 return
54         }
55         r = &me.ranges[i]
56         if bytes.Compare(ip, r.First) < 0 || bytes.Compare(ip, r.Last) > 0 {
57                 r = nil
58         }
59         return
60 }
61
62 // Parse a line of the PeerGuardian Text Lists (P2P) Format. Returns !ok but
63 // no error if a line doesn't contain a range but isn't erroneous, such as
64 // comment and blank lines.
65 func ParseBlocklistP2PLine(l []byte) (r Range, ok bool, err error) {
66         l = bytes.TrimSpace(l)
67         if len(l) == 0 || bytes.HasPrefix(l, []byte("#")) {
68                 return
69         }
70         colon := bytes.IndexByte(l, ':')
71         if colon == -1 {
72                 err = errors.New("missing colon")
73                 return
74         }
75         hyphen := bytes.IndexByte(l[colon+1:], '-')
76         if hyphen == -1 {
77                 err = errors.New("missing hyphen")
78                 return
79         }
80         hyphen += colon + 1
81         r.Description = string(l[:colon])
82         r.First = net.ParseIP(string(l[colon+1 : hyphen]))
83         r.Last = net.ParseIP(string(l[hyphen+1:]))
84         if r.First == nil || r.Last == nil {
85                 return
86         }
87         ok = true
88         return
89 }