-
Notifications
You must be signed in to change notification settings - Fork 31
/
net_registry.go
162 lines (127 loc) · 3.43 KB
/
net_registry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// OpenRDAP
// Copyright 2017 Tom Harwood
// MIT License, see the LICENSE file.
package bootstrap
import (
"bytes"
"errors"
"fmt"
"net"
"net/url"
"sort"
"strings"
)
type NetRegistry struct {
// Map of netmask size (0-32 for IPv4, 0-128 for IPv6) to list of NetEntries.
networks map[int][]netEntry
numIPBytes int // Length in bytes of each IP address (4 for IPv4, 16 for IPv6).
file *File
}
// A netEntry is a network and its RDAP base URLs.
type netEntry struct {
Net *net.IPNet
URLs []*url.URL
}
type netEntrySorter []netEntry
func (a netEntrySorter) Len() int {
return len(a)
}
func (a netEntrySorter) Swap(i int, j int) {
a[i], a[j] = a[j], a[i]
}
func (a netEntrySorter) Less(i int, j int) bool {
return bytes.Compare(a[i].Net.IP, a[j].Net.IP) <= 0
}
// NewNetRegistry creates a NetRegistry from an IPv4 or IPv6 registry JSON document. ipVersion must be 4 or 6.
//
// The document formats are specified in https://tools.ietf.org/html/rfc7484#section-5.1 and https://tools.ietf.org/html/rfc7484#section-5.2.
func NewNetRegistry(json []byte, ipVersion int) (*NetRegistry, error) {
if ipVersion != 4 && ipVersion != 6 {
return nil, fmt.Errorf("Unknown IP version %d", ipVersion)
}
var registry *File
registry, err := NewFile(json)
if err != nil {
return nil, fmt.Errorf("Error parsing net registry file: %s", err)
}
n := &NetRegistry{
networks: map[int][]netEntry{},
numIPBytes: numIPBytesForVersion(ipVersion),
file: registry,
}
var cidr string
var urls []*url.URL
for cidr, urls = range registry.Entries {
_, ipNet, err := net.ParseCIDR(cidr)
if err != nil {
continue
} else if len(ipNet.IP) != n.numIPBytes {
continue
}
size, _ := ipNet.Mask.Size()
n.networks[size] = append(n.networks[size], netEntry{Net: ipNet, URLs: urls})
}
for _, nets := range n.networks {
sort.Sort(netEntrySorter(nets))
}
return n, nil
}
// Lookup returns the RDAP base URLs for the IP address or CIDR range question |Question|.
//
// Example queries are: "192.0.2.0", "192.0.2.0/25". "2001:db8::", "2001::db8::/62".
func (n *NetRegistry) Lookup(question *Question) (*Answer, error) {
input := question.Query
if !strings.ContainsAny(input, "/") {
// Convert IP address to CIDR format, with a /32 or /128 mask.
input = fmt.Sprintf("%s/%d", input, n.numIPBytes*8)
}
_, lookupNet, err := net.ParseCIDR(input)
if err != nil {
return nil, err
}
if len(lookupNet.IP) != n.numIPBytes {
return nil, errors.New("Lookup address has wrong IP protocol")
}
lookupMask, _ := lookupNet.Mask.Size()
var bestEntry string
var bestURLs []*url.URL
var bestMask int
var mask int
var nets []netEntry
for mask, nets = range n.networks {
if mask < bestMask || mask > lookupMask {
continue
}
index := sort.Search(len(nets), func(i int) bool {
net := nets[i].Net
return net.Contains(lookupNet.IP) || bytes.Compare(net.IP, lookupNet.IP) >= 0
})
if index == len(nets) || !nets[index].Net.Contains(lookupNet.IP) {
continue
}
bestEntry = nets[index].Net.String()
bestMask = mask
bestURLs = nets[index].URLs
}
return &Answer{
Query: input,
Entry: bestEntry,
URLs: bestURLs,
}, nil
}
func numIPBytesForVersion(ipVersion int) int {
len := 0
switch ipVersion {
case 4:
len = net.IPv4len
case 6:
len = net.IPv6len
default:
panic("Unknown IP version")
}
return len
}
// File returns a struct describing the registry's JSON document.
func (n *NetRegistry) File() *File {
return n.file
}