-
Notifications
You must be signed in to change notification settings - Fork 17
/
routing.go
119 lines (101 loc) · 2.78 KB
/
routing.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
/*
* ZAnnotate Copyright 2018 Regents of the University of Michigan
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package zannotate
import (
"errors"
"flag"
"net"
"os"
log "github.com/sirupsen/logrus"
"github.com/zmap/zannotate/zrouting"
)
type RoutingAnnotatorFactory struct {
BasePluginConf
RoutingTablePath string
ASNamesPath string
ASDataPath string
rlt *zrouting.RoutingLookupTree
}
type RoutingAnnotator struct {
Factory *RoutingAnnotatorFactory
rlt *zrouting.RoutingLookupTree
Id int
}
// Routing Annotator Factory (Global)
func (a *RoutingAnnotatorFactory) AddFlags(flags *flag.FlagSet) {
flags.BoolVar(&a.Enabled, "routing", false, "annotate with origin AS lookup")
flags.StringVar(&a.RoutingTablePath, "routing-mrt-file", "",
"path to MRT TABLE_DUMPv2 file")
flags.StringVar(&a.ASNamesPath, "routing-as-names", "", "path to as names file")
flags.IntVar(&a.Threads, "routing-threads", 5,
"how many routing processing threads to use")
}
func (a *RoutingAnnotatorFactory) IsEnabled() bool {
return a.Enabled
}
func (a *RoutingAnnotatorFactory) GetWorkers() int {
return a.Threads
}
func (a *RoutingAnnotatorFactory) Initialize(conf *GlobalConf) error {
if a.RoutingTablePath == "" {
return errors.New("no routing file (MRT TABLE_DUMPv2) provided")
}
log.Debug("will add routing using ", a.RoutingTablePath)
// Routing Lookup Trees are thread-safe
a.rlt = new(zrouting.RoutingLookupTree)
f, err := os.Open(a.RoutingTablePath)
if err != nil {
return err
}
if a.ASNamesPath != "" {
f, err := os.Open(a.ASNamesPath)
if err != nil {
return err
}
a.rlt.PopulateASnames(f)
}
a.rlt.PopulateFromMRT(f)
return nil
}
func (a *RoutingAnnotatorFactory) MakeAnnotator(i int) Annotator {
var v RoutingAnnotator
v.Factory = a
v.rlt = a.rlt
v.Id = i
return &v
}
func (a *RoutingAnnotatorFactory) Close() error {
return nil
}
// Routing Annotator (Per-Worker)
func (a *RoutingAnnotator) Initialize() error {
return nil
}
func (a *RoutingAnnotator) GetFieldName() string {
return "routing"
}
func (a *RoutingAnnotator) Annotate(ip net.IP) interface{} {
ret, err := a.rlt.Get(ip)
if err != nil {
return nil
}
return ret
}
func (a *RoutingAnnotator) Close() error {
return nil
}
func init() {
s := new(RoutingAnnotatorFactory)
RegisterAnnotator(s)
}