Skip to content

Commit a36f346

Browse files
Add IPv4 only annotator (#64)
* Add IPv4 only annotator * Update coveralls to travis-pro
1 parent cb531cc commit a36f346

File tree

3 files changed

+68
-10
lines changed

3 files changed

+68
-10
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ script:
1414
- go build ./...
1515
- go test ./... -cover=1 -coverprofile=_c.cov
1616
- go test ./... -race
17-
- $GOPATH/bin/goveralls -service=travis-ci -coverprofile=_c.cov
17+
- $GOPATH/bin/goveralls -service=travis-pro -coverprofile=_c.cov

asnannotator/asn.go

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ type asnAnnotator struct {
3636
asnames ipinfo.ASNames
3737
}
3838

39+
// NewIPv4 makes a new IPv4-only Annotator that uses IP addresses to lookup ASN metadata for
40+
// that IP based on the current copy of RouteViews data stored in the given providers.
41+
func NewIPv4(ctx context.Context, as4 content.Provider) ASNAnnotator {
42+
a := &asnAnnotator{
43+
as4: as4,
44+
}
45+
var err error
46+
a.asn4, err = load(ctx, as4, nil)
47+
rtx.Must(err, "Could not load Routeviews IPv4 ASN db")
48+
return a
49+
}
50+
3951
// New makes a new Annotator that uses IP addresses to lookup ASN metadata for
4052
// that IP based on the current copy of RouteViews data stored in the given providers.
4153
func New(ctx context.Context, as4 content.Provider, as6 content.Provider, asnamedata content.Provider, localIPs []net.IP) ASNAnnotator {
@@ -97,6 +109,10 @@ func (a *asnAnnotator) annotateIPHoldingLock(src string) *annotator.Network {
97109
metrics.ASNSearches.WithLabelValues("ipv4-success").Inc()
98110
return ann
99111
}
112+
if a.asn6 == nil {
113+
ann.Missing = true
114+
return ann
115+
}
100116

101117
ipnet, err = a.asn6.Search(src)
102118
if err != nil {
@@ -126,15 +142,19 @@ func (a *asnAnnotator) Reload(ctx context.Context) {
126142
log.Println("Could not reload v4 routeviews:", err)
127143
return
128144
}
129-
new6, err := load(ctx, a.as6, a.asn6)
130-
if err != nil {
131-
log.Println("Could not reload v6 routeviews:", err)
132-
return
133-
}
134-
newnames, err := loadNames(ctx, a.asnamedata, a.asnames)
135-
if err != nil {
136-
log.Println("Could not reload asnames from ipinfo:", err)
137-
return
145+
var new6 routeview.Index
146+
var newnames ipinfo.ASNames
147+
if a.as6 != nil {
148+
new6, err = load(ctx, a.as6, a.asn6)
149+
if err != nil {
150+
log.Println("Could not reload v6 routeviews:", err)
151+
return
152+
}
153+
newnames, err = loadNames(ctx, a.asnamedata, a.asnames)
154+
if err != nil {
155+
log.Println("Could not reload asnames from ipinfo:", err)
156+
return
157+
}
138158
}
139159
// Don't acquire the lock until after the data is in RAM.
140160
a.m.Lock()

asnannotator/asn_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,41 @@ func TestNewFake(t *testing.T) {
305305
t.Error("Should have had a missing return value, not", n3)
306306
}
307307
}
308+
309+
func Test_IPv4Annotator_AnnotateIP(t *testing.T) {
310+
setUp()
311+
tests := []struct {
312+
name string
313+
addr string
314+
want annotator.Network
315+
}{
316+
{
317+
name: "success-ipv4",
318+
addr: "1.0.0.1",
319+
want: annotator.Network{
320+
CIDR: "1.0.0.0/24",
321+
ASNumber: 13335,
322+
Systems: []annotator.System{
323+
{ASNs: []uint32{13335}},
324+
},
325+
},
326+
},
327+
{
328+
name: "success-ipv6",
329+
addr: "2001:200::1",
330+
want: annotator.Network{
331+
Missing: true,
332+
},
333+
},
334+
}
335+
ctx := context.Background()
336+
a := NewIPv4(ctx, local4Rawfile)
337+
for _, tt := range tests {
338+
t.Run(tt.name, func(t *testing.T) {
339+
got := a.AnnotateIP(tt.addr)
340+
if diff := deep.Equal(*got, tt.want); diff != nil {
341+
t.Error("AnnotateIP() wrong value; got!=want", diff)
342+
}
343+
})
344+
}
345+
}

0 commit comments

Comments
 (0)