1 // Package iplist handles the P2P Plaintext Format described by
2 // https://en.wikipedia.org/wiki/PeerGuardian#P2P_plaintext_format.
15 // An abstraction of IP list implementations.
16 type Ranger interface {
17 // Return a Range containing the IP.
18 Lookup(net.IP) (r Range, ok bool)
19 // If your ranges hurt, use this.
32 func (r Range) String() string {
33 return fmt.Sprintf("%s-%s: %s", r.First, r.Last, r.Description)
36 // Create a new IP list. The given ranges must already sorted by the lower
37 // bound IP in each range. Behaviour is undefined for lists of overlapping
39 func New(initSorted []Range) *IPList {
45 func (ipl *IPList) NumRanges() int {
49 return len(ipl.ranges)
52 // Return the range the given IP is in. ok if false if no range is found.
53 func (ipl *IPList) Lookup(ip net.IP) (r Range, ok bool) {
57 // TODO: Perhaps all addresses should be converted to IPv6, if the future
58 // of IP is to always be backwards compatible. But this will cost 4x the
59 // memory for IPv4 addresses?
62 r, ok = ipl.lookup(v4)
71 if v4 == nil && v6 == nil {
73 Description: "bad IP",
80 // Return a range that contains ip, or nil.
82 first func(i int) net.IP,
83 full func(i int) Range,
89 // Find the index of the first range for which the following range exceeds
91 i := sort.Search(n, func(i int) bool {
95 return bytes.Compare(ip, first(i+1)) < 0
101 ok = bytes.Compare(r.First, ip) <= 0 && bytes.Compare(ip, r.Last) <= 0
105 // Return the range the given IP is in. Returns nil if no range is found.
106 func (ipl *IPList) lookup(ip net.IP) (Range, bool) {
107 return lookup(func(i int) net.IP {
108 return ipl.ranges[i].First
109 }, func(i int) Range {
111 }, len(ipl.ranges), ip)
114 func minifyIP(ip *net.IP) {
117 *ip = append(make([]byte, 0, 4), v4...)
121 // Parse a line of the PeerGuardian Text Lists (P2P) Format. Returns !ok but
122 // no error if a line doesn't contain a range but isn't erroneous, such as
123 // comment and blank lines.
124 func ParseBlocklistP2PLine(l []byte) (r Range, ok bool, err error) {
125 l = bytes.TrimSpace(l)
126 if len(l) == 0 || bytes.HasPrefix(l, []byte("#")) {
129 // TODO: Check this when IPv6 blocklists are available.
130 colon := bytes.LastIndexAny(l, ":")
132 err = errors.New("missing colon")
135 hyphen := bytes.IndexByte(l[colon+1:], '-')
137 err = errors.New("missing hyphen")
141 r.Description = string(l[:colon])
142 r.First = net.ParseIP(string(l[colon+1 : hyphen]))
144 r.Last = net.ParseIP(string(l[hyphen+1:]))
146 if r.First == nil || r.Last == nil || len(r.First) != len(r.Last) {
147 err = errors.New("bad IP range")
154 // Creates an IPList from a line-delimited P2P Plaintext file.
155 func NewFromReader(f io.Reader) (ret *IPList, err error) {
157 // There's a lot of similar descriptions, so we maintain a pool and reuse
158 // them to reduce memory overhead.
159 uniqStrs := make(map[string]string)
160 scanner := bufio.NewScanner(f)
163 r, ok, lineErr := ParseBlocklistP2PLine(scanner.Bytes())
165 err = fmt.Errorf("error parsing line %d: %s", lineNum, lineErr)
172 if s, ok := uniqStrs[r.Description]; ok {
175 uniqStrs[r.Description] = r.Description
177 ranges = append(ranges, r)