]> Sergey Matveev's repositories - btrtrc.git/blob - iplist/iplist.go
iplist: Make range descriptions copies of substring, saves 8MB heap on level1 blocklist!
[btrtrc.git] / iplist / iplist.go
1 package iplist
2
3 import (
4         "bytes"
5         "fmt"
6         "net"
7         "sort"
8 )
9
10 type IPList struct {
11         ranges []Range
12 }
13
14 type Range struct {
15         First, Last net.IP
16         Description string
17 }
18
19 func (r *Range) String() string {
20         return fmt.Sprintf("%s-%s (%s)", r.First, r.Last, r.Description)
21 }
22
23 // Create a new IP list. The given range must already sorted by the lower IP
24 // in the range. Behaviour is undefined for lists of overlapping ranges.
25 func New(initSorted []Range) *IPList {
26         return &IPList{
27                 ranges: initSorted,
28         }
29 }
30
31 // Return the range the given IP is in. Returns nil if no range is found.
32 func (me *IPList) Lookup(ip net.IP) (r *Range) {
33         // Find the index of the first range for which the following range exceeds
34         // it.
35         i := sort.Search(len(me.ranges), func(i int) bool {
36                 if i+1 >= len(me.ranges) {
37                         return true
38                 }
39                 return bytes.Compare(ip, me.ranges[i+1].First) < 0
40         })
41         if i == len(me.ranges) {
42                 return
43         }
44         r = &me.ranges[i]
45         if bytes.Compare(ip, r.First) < 0 || bytes.Compare(ip, r.Last) > 0 {
46                 r = nil
47         }
48         return
49 }
50
51 // Parse a line of the PeerGuardian Text Lists (P2P) Format. Returns !ok but
52 // no error if a line doesn't contain a range but isn't erroneous, such as
53 // comment and blank lines.
54 func ParseBlocklistP2PLine(l []byte) (r Range, ok bool, err error) {
55         l = bytes.TrimSpace(l)
56         if len(l) == 0 || bytes.HasPrefix(l, []byte("#")) {
57                 return
58         }
59         colon := bytes.IndexByte(l, ':')
60         hyphen := bytes.IndexByte(l[colon+1:], '-') + colon + 1
61         r.Description = string(l[:colon])
62         r.First = net.ParseIP(string(l[colon+1 : hyphen]))
63         r.Last = net.ParseIP(string(l[hyphen+1:]))
64         if r.First == nil || r.Last == nil {
65                 return
66         }
67         ok = true
68         return
69 }