-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPeers.go
87 lines (78 loc) · 2.1 KB
/
Peers.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
package main
import (
"fmt"
"github.com/osrg/gobgp/pkg/packet/mrt"
"math"
"strconv"
)
var peers []*mrt.Peer //DO NOT USE FOR ANYTHING ELSE BESIDES INITIALIZING "ourPeers". The peers (especially their IP addresses do not stay constant during initialization of trie.
var ourPeers []peer //stores the peers
var highestPeerId uint32
var highestPeerId100 uint32
type peer struct {
id uint16
ip string //this used to be of type net.IP. This led to a bug as sometimes the IP addresses did change without being intended to do so.
as uint32
}
func (p peer) toString() string {
result := ""
result = result + "Peer " + strconv.Itoa(int(p.id)) + ": " + p.ip + " (AS " + strconv.Itoa(int(p.as)) + ")\n"
return result
}
func ourPeersToString() string {
result := ""
for i := 0; i < len(ourPeers); i++ {
result = result + ourPeers[i].toString()
}
return result
}
var peermapByID map[uint16]*peer
var peermapByIP map[string]*peer
func insertPeers() {
ourPeers = make([]peer, len(peers), int(math.Max(2*float64(len(peers)), 100))) //leave some free capacity in case new peers appear during the updates
for i := 0; i < len(peers); i++ {
p := peer{
id: uint16(highestPeerId),
ip: peers[i].IpAddress.String(),
as: peers[i].AS,
}
if highestPeerId == 100*highestPeerId100 {
highestPeerId100++
fmt.Println(Yellow("peers added: ", highestPeerId))
}
if verbose {
fmt.Println(Yellow("new peer added: ", p.toString()))
}
highestPeerId++
peermapByID[p.id] = &p
peermapByIP[p.ip] = &p
ourPeers[i] = p
}
}
func findPeerIDbyIP(ip string, as uint32) uint16 {
v, ok := peermapByIP[ip]
if ok {
if v.as != as {
fmt.Println(Red("Found already existing peer with same ip, but for different as"))
}
return v.id
} else {
p := peer{
id: uint16(highestPeerId),
ip: ip,
as: as,
}
if verbose {
fmt.Println(Yellow("new peer added: ", p.toString()))
}
if highestPeerId == highestPeerId100*100 {
highestPeerId100++
fmt.Println(Yellow("peers added: ", highestPeerId))
}
highestPeerId++
peermapByID[p.id] = &p
peermapByIP[p.ip] = &p
ourPeers = append(ourPeers, p)
return p.id
}
}