-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
151 lines (131 loc) · 2.94 KB
/
reader.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
package main
import (
"bufio"
"io"
"log"
"regexp"
"runtime"
"strconv"
"strings"
)
func readPersonalPreference(reader io.Reader) {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
as := strings.Fields(scanner.Text())
if len(as) != 2 {
continue
}
id, err := strconv.Atoi(as[0])
if err != nil {
continue
}
value, err := strconv.Atoi(as[1])
if err != nil {
continue
}
personalValue[id] = value
}
}
func readRoutes(reader io.Reader) (uint32, map[int]int) {
var seenPrefixBefore = make(map[string]bool)
var asRoutes = make(map[int]int)
var routesSeen uint32
/* Regular expression helper */
var aspathReg = regexp.MustCompile(`(\d+)[^\d]*$`)
var ipAddressReg = regexp.MustCompile(`^\d{1,7}\s+([\d\.\/abcdef:]+)|^\d{7}([\d\.\/abcdef:]+)`)
var skipNextLine = false
var reset = false
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
if skipNextLine {
skipNextLine = false
continue
}
brocadeIPmatcher := ipAddressReg.FindStringSubmatch(scanner.Text())
if len(brocadeIPmatcher) == 3 {
var prefix string
if brocadeIPmatcher[2] != "" {
prefix = brocadeIPmatcher[2]
} else {
prefix = brocadeIPmatcher[1]
}
if seenPrefixBefore[prefix] {
skipNextLine = true
continue
}
routesSeen++
seenPrefixBefore[prefix] = true
reset = false
continue
}
if strings.Contains(scanner.Text(), "AS_PATH:") && reset == false {
reset = true
subMatches := aspathReg.FindStringSubmatch(scanner.Text())
if len(subMatches) != 2 {
continue
}
asn, err := strconv.Atoi(subMatches[1])
if err != nil {
continue
}
asRoutes[asn]++
continue
}
}
return routesSeen, asRoutes
}
func readCountryList(reader io.Reader) {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
c := strings.Fields(scanner.Text())
value, err := strconv.Atoi(c[1])
if err != nil {
continue
}
countries[c[0]] = value
}
}
func readAsList(reader io.Reader) {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
as := strings.Fields(scanner.Text())
/* cut out some things */
asId := strings.Trim(as[0], "AS")
id, err := strconv.Atoi(asId)
if err != nil {
continue
}
as = as[1:]
country := as[len(as)-1]
as = as[:len(as)-1]
asInfo := strings.Join(as, "")
asNumber[id] = asInfo
var value int
if val, ok := countries[country]; !ok {
value = *bgpDefaultValue
} else {
value += val
}
if _, ok := personalValue[id]; ok {
value = personalValue[id]
}
bgpASlist = append(bgpASlist, bgpAS{
value: value,
routesNumber: asNumberRoutes[id],
asNumber: id,
country: country,
name: asId,
})
}
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}
func PrintMemUsage() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
log.Printf("Alloc = %v MiB", bToMb(m.Alloc))
log.Printf("TotalAlloc = %v MiB", bToMb(m.TotalAlloc))
log.Printf("Sys = %v MiB", bToMb(m.Sys))
log.Printf("NumGC = %v\n", m.NumGC)
}