-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.go
93 lines (83 loc) · 1.59 KB
/
node.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
package l2
import (
"encoding/binary"
"math/rand"
"net"
)
type Node struct {
LanIP net.IP
WanHost string // reachable from anywhere
PrivateIP net.IP // reachable from local lan
BridgeNames []string
ID int // to allow multiple nodes in single host
wanIP net.IP
lanIPStr string
}
func (n *Node) Init() {
// defaults
if len(n.BridgeNames) == 0 {
n.BridgeNames = []string{BridgeTCP}
}
// lookup host
if n.WanHost != "" {
addrs, err := net.LookupHost(n.WanHost)
ce(err)
for _, addr := range addrs {
ip := net.ParseIP(addr)
if len(ip) > 0 {
n.wanIP = ip
break
}
}
}
// ip string
n.lanIPStr = n.LanIP.String()
}
func (n *Node) HasBridge(name string) bool {
for _, bridge := range n.BridgeNames {
if bridge == name {
return true
}
}
return false
}
func (Network) LocalNode(
selectNode SelectNode,
ipnet net.IPNet,
initNodes InitNodes,
) *Node {
if selectNode != nil {
if n := selectNode(); n != nil {
return n
}
}
// random ip
ones, _ := ipnet.Mask.Size()
random_ip:
ip := ipnet.IP.Mask(ipnet.Mask)
bs := make(net.IP, len(ip))
if len(bs) == 8 {
num := uint64(rand.Int63()) & (^uint64(0) >> ones)
if num == 0 || (^num) == 0 {
goto random_ip
}
binary.BigEndian.PutUint64(bs, uint64(num))
} else {
num := uint32(rand.Int63()) & (^uint32(0) >> ones)
if num == 0 || (^num) == 0 {
goto random_ip
}
binary.BigEndian.PutUint32(bs, uint32(num))
}
for i, b := range ip {
ip[i] = b | bs[i]
}
for _, node := range initNodes {
if node.LanIP.Equal(ip) {
goto random_ip
}
}
return &Node{
LanIP: ip,
}
}