-
Notifications
You must be signed in to change notification settings - Fork 45
/
dht_connection.go
183 lines (171 loc) · 4.96 KB
/
dht_connection.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"errors"
"fmt"
"net"
"sync"
"github.com/golang/protobuf/proto"
ptp "github.com/subutai-io/p2p/lib"
"github.com/subutai-io/p2p/protocol"
)
// DHT Errors
var (
ErrorNoRouters = errors.New("Routers wasn't specified")
ErrorBadRouterAddress = errors.New("Bad router address")
)
// DHTConnection to a DHT bootstrap node
type DHTConnection struct {
routers []*DHTRouter // Bootstrap nodes
routersList map[int]string // List of bootstrap nodes received from SRV lookup
lock sync.Mutex // Mutex for register/unregister
instances map[string]*P2PInstance // Instances
registered []string // List of registered swarm IDs
incoming chan *protocol.DHTPacket // Packets received by routers
ip string // Our outbound IP
isActive bool // Whether DHT connection is active or not
}
func (dht *DHTConnection) init(target string) error {
ptp.Log(ptp.Debug, "Initializing connection to a bootstrap nodes")
dht.incoming = make(chan *protocol.DHTPacket)
var err error
dht.routersList, err = ptp.SrvLookup(target, "tcp", "subutai.io")
if err != nil {
ptp.Log(ptp.Debug, "Failed to get bootstrap nodes: %s", err.Error())
dht.routersList = make(map[int]string)
}
if len(dht.routersList) == 0 {
return ErrorNoRouters
}
for _, r := range dht.routersList {
if r == "" {
continue
}
addr, err := net.ResolveTCPAddr("tcp4", r)
if err != nil {
ptp.Log(ptp.Error, "Bad router address provided [%s]: %s", r, err)
return ErrorBadRouterAddress
}
router := new(DHTRouter)
router.addr = addr
router.router = r
router.data = dht.incoming
dht.routers = append(dht.routers, router)
}
dht.instances = make(map[string]*P2PInstance)
return nil
}
func (dht *DHTConnection) registerInstance(hash string, inst *P2PInstance) error {
dht.lock.Lock()
defer dht.lock.Unlock()
ptp.Log(ptp.Debug, "Registering instance %s on bootstrap", hash)
exists := false
for ihash, _ := range dht.instances {
if hash == ihash {
exists = true
break
}
}
for _, ihash := range dht.registered {
if ihash == hash {
exists = true
break
}
}
if exists {
return fmt.Errorf("Hash already registered on bootstrap")
}
dht.instances[hash] = inst
dht.registered = append(dht.registered, hash)
inst.PTP.Dht.IncomingData = make(chan *protocol.DHTPacket)
inst.PTP.Dht.OutgoingData = make(chan *protocol.DHTPacket)
go func() {
for {
packet := <-inst.PTP.Dht.OutgoingData
if packet == nil {
break
}
dht.send(packet)
}
}()
ptp.Log(ptp.Debug, "Instance was registered with bootstrap client")
return nil
}
func (dht *DHTConnection) send(packet *protocol.DHTPacket) {
if packet == nil {
return
}
ptp.Log(ptp.Trace, "Sending DHT packet %+v", packet)
data, err := proto.Marshal(packet)
if err != nil {
ptp.Log(ptp.Error, "Failed to marshal DHT Packet: %s", err)
}
ptp.Log(ptp.Trace, "Sending marshaled DHT Packet of size [%d]", len(data))
for i, router := range dht.routers {
if router.running && router.handshaked {
n, err := router.sendRaw(data)
if err != nil {
ptp.Log(ptp.Error, "Failed to send data to %s", router.addr.String())
continue
}
if n >= 0 {
dht.routers[i].tx += uint64(n)
}
}
}
}
func (dht *DHTConnection) run() {
for {
packet := <-dht.incoming
if packet == nil {
continue
}
ptp.Log(ptp.Trace, "Routing DHT Packet %+v", packet)
// Ping should always provide us with outbound IP value
if packet.Type == protocol.DHTPacketType_Ping && packet.Data != "" {
ptp.Log(ptp.Info, "Received outbound IP: %s", packet.Data)
dht.ip = packet.Data
if packet.Extra != "" && packet.Query == "handshaked" {
// Resend our IP
for _, inst := range dht.instances {
if inst == nil || inst.PTP == nil {
continue
}
inst.PTP.ReportIP(inst.PTP.Interface.GetIP().String(), inst.PTP.Interface.GetHardwareAddress().String(), inst.PTP.Interface.GetName())
}
}
continue
}
if packet.Infohash == "" {
continue
}
i, e := dht.instances[packet.Infohash]
if e && i != nil && i.PTP != nil && !i.PTP.Shutdown && i.PTP.Dht != nil && i.PTP.Dht.IncomingData != nil {
i.PTP.Dht.IncomingData <- packet
} else {
ptp.Log(ptp.Debug, "DHT received data for unknown instance %s: %+v", packet.Infohash, packet)
}
}
}
func (dht *DHTConnection) unregisterInstance(hash string) error {
dht.lock.Lock()
defer dht.lock.Unlock()
ptp.Log(ptp.Debug, "Unregistering instance %s from bootstrap", hash)
inst, e := dht.instances[hash]
if !e {
return fmt.Errorf("Can't unregister hash %s: Instance doesn't exists", hash)
}
if inst != nil && inst.PTP != nil && inst.PTP.Dht != nil {
err := inst.PTP.Dht.Close()
if err != nil {
ptp.Log(ptp.Error, "Failed to stop DHT on instance %s", hash)
}
}
delete(dht.instances, hash)
for i, ihash := range dht.registered {
if ihash == hash {
dht.registered = append(dht.registered[:i], dht.registered[i+1:]...)
break
}
}
return nil
}