forked from spacemeshos/go-spacemesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swarmhandlers.go
464 lines (354 loc) · 12.8 KB
/
swarmhandlers.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package p2p
import (
"encoding/hex"
"fmt"
"time"
"github.com/gogo/protobuf/proto"
"github.com/spacemeshos/go-spacemesh/crypto"
"github.com/spacemeshos/go-spacemesh/p2p/net"
"github.com/spacemeshos/go-spacemesh/p2p/node"
"github.com/spacemeshos/go-spacemesh/p2p/pb"
"github.com/spacemeshos/go-spacemesh/p2p/timesync"
)
// This source file contain swarm internal handlers
// These should only be called from the swarms event processing main loop
// or by other internal handlers but not from a random type or go routine
// Handles a local request to register a remote node in the swarm.
// Register adds info about this node but doesn't attempt to connect to it.
func (s *swarmImpl) onRegisterNodeRequest(n node.RemoteNodeData) {
if s.peers[n.ID()] != nil {
s.localNode.Info("Already connected to %s", n.Pretty())
return
}
rn, err := NewRemoteNode(n.ID(), n.IP())
if err != nil { // invalid id
s.localNode.Error("invalid remote node id: %s", n.ID())
return
}
s.peers[n.ID()] = rn
// update the routing table with the nde node info
s.routingTable.Update(n)
s.sendNodeEvent(n.ID(), Registered)
}
// Handles a local request to connect to a remote node.
func (s *swarmImpl) onConnectionRequest(req node.RemoteNodeData) {
s.localNode.Info("Local request to connect to node %s", req.Pretty())
var err error
// check for existing session with that node
peer := s.peers[req.ID()]
if peer == nil {
s.onRegisterNodeRequest(req)
}
peer = s.peers[req.ID()]
if peer == nil {
s.localNode.Error("unexpected null peer")
return
}
conn := peer.GetActiveConnection()
session := peer.GetAuthenticatedSession()
if conn != nil && session != nil {
s.localNode.Info("Already got an active session and connection with: %s", req.Pretty())
return
}
if conn == nil {
s.sendNodeEvent(req.ID(), Connecting)
// Dial the other node using the node's network config values
localNodeConfig := s.localNode.Config()
addressableNodeConfig := &localNodeConfig
conn, err = s.network.DialTCP(req.IP(), addressableNodeConfig.DialTimeout.Duration(), addressableNodeConfig.ConnKeepAlive.Duration())
if err != nil {
s.sendNodeEvent(req.ID(), Disconnected)
s.localNode.Error("failed to connect to remote node %s on advertised ip %s", req.Pretty(), req.IP())
return
}
s.sendNodeEvent(req.ID(), Connected)
// update the routing table
s.routingTable.Update(req)
id := conn.ID()
// update state with new connection
s.peersByConnection[id] = peer
s.connections[id] = conn
// update remote node connections
peer.GetConnections()[id] = conn
}
// todo: we need to handle the case that there's a non-authenticated session with the remote node
// we need to decide if to wait for it to auth, kill it, etc....
if session == nil || !session.IsAuthenticated() {
// start handshake protocol
s.sendNodeEvent(req.ID(), HandshakeStarted)
s.handshakeProtocol.CreateSession(peer)
}
}
// callback from handshake protocol when session state changes.
func (s *swarmImpl) onNewSession(data HandshakeData) {
if err := data.GetError(); err != nil {
s.localNode.Error("Session creation error %s", err)
return
}
if data.Session().IsAuthenticated() {
s.localNode.Info("Established new session with %s", data.Peer().TCPAddress())
s.sendNodeEvent(data.Peer().String(), SessionEstablished)
// store the session
s.allSessions[data.Session().String()] = data.Session()
// send all messages queued for the remote node we now have a session with
for key, msg := range s.messagesPendingSession {
if msg.PeerID == data.Peer().String() {
s.localNode.Info("Sending queued message %s to remote node", hex.EncodeToString(msg.ReqID))
delete(s.messagesPendingSession, key)
go s.SendMessage(msg)
}
}
}
}
// Local request to disconnect from a node
func (s *swarmImpl) onDisconnectionRequest(req node.RemoteNodeData) {
// todo: disconnect all connections with node
s.sendNodeEvent(req.ID(), Disconnected)
}
// Local request to send a message to a remote node
func (s *swarmImpl) onSendHandshakeMessage(r SendMessageReq) {
// check for existing remote node and session
remoteNode := s.peers[r.PeerID]
if remoteNode == nil {
// for now we assume messages are sent only to nodes we already know their ip address
return
}
conn := remoteNode.GetActiveConnection()
if conn == nil {
s.localNode.Error("Expected to have a connection with remote node")
return
}
conn.Send(r.Payload, r.ReqID)
}
// Local request to send a message to a remote node
func (s *swarmImpl) onSendMessageRequest(r SendMessageReq) {
// check for existing remote node and session
peer := s.peers[r.PeerID]
if peer == nil {
s.localNode.Info("No contact info to target peer - attempting to find it on the network...")
callback := make(chan node.RemoteNodeData)
// attempt to find the peer
s.findNode(r.PeerID, callback)
go func() {
select {
case n := <-callback:
if n != nil { // we found it - now we can send the message to it
s.localNode.Info("Peer %s found.... - sending message", r.PeerID)
s.onSendMessageRequest(r)
} else {
s.localNode.Info("Peer %s not found.... - can't send message", r.PeerID)
}
}
}()
return
}
session := peer.GetAuthenticatedSession()
conn := peer.GetActiveConnection()
if session == nil || conn == nil {
s.localNode.Warning("queuing protocol request until session is established...")
// save the message for later sending and try to connect to the node
s.messagesPendingSession[hex.EncodeToString(r.ReqID)] = r
// try to connect to remote node and send the message once connected
// todo: callback listener if connection fails (possibly after retries)
s.onConnectionRequest(node.NewRemoteNodeData(peer.String(), peer.TCPAddress()))
return
}
encPayload, err := session.Encrypt(r.Payload)
if err != nil {
e := fmt.Errorf("aborting send - failed to encrypt payload: %v", err)
go func() {
if r.Callback != nil {
r.Callback <- SendError{r.ReqID, e}
}
}()
return
}
msg := &pb.CommonMessageData{
SessionId: session.ID(),
Payload: encPayload,
Timestamp: time.Now().Unix(),
}
data, err := proto.Marshal(msg)
if err != nil {
e := fmt.Errorf("aborting send - invalid msg format %v", err)
go func() {
if r.Callback != nil {
r.Callback <- SendError{r.ReqID, e}
}
}()
return
}
// store callback by reqId for this connection so we can call back in case of msg timeout or other send failure
if r.Callback != nil {
callbacks := s.outgoingSendsCallbacks[conn.ID()]
if callbacks == nil {
s.outgoingSendsCallbacks[conn.ID()] = make(map[string]chan SendError)
}
s.outgoingSendsCallbacks[conn.ID()][hex.EncodeToString(r.ReqID)] = r.Callback
}
// finally - send it away!
s.localNode.Info("Sending protocol message down the connection...")
conn.Send(data, r.ReqID)
}
func (s *swarmImpl) onConnectionClosed(c net.Connection) {
// forget about this connection
id := c.ID()
peer := s.peersByConnection[id]
if peer != nil {
peer.GetConnections()[id] = nil
}
delete(s.connections, id)
delete(s.peersByConnection, id)
s.sendNodeEvent(peer.String(), Disconnected)
}
func (s *swarmImpl) onRemoteClientConnected(c net.Connection) {
// nop - a remote client connected - this is handled w message
s.localNode.Info("Remote client connected. %s", c.ID())
peer := s.peersByConnection[c.ID()]
if peer != nil {
s.sendNodeEvent(peer.String(), Connected)
}
}
// Processes an incoming handshake protocol message.
func (s *swarmImpl) onRemoteClientHandshakeMessage(msg net.IncomingMessage) {
data := &pb.HandshakeData{}
err := proto.Unmarshal(msg.Message, data)
if err != nil {
s.localNode.Warning("Unexpected handshake message format: %v", err)
return
}
connID := msg.Connection.ID()
// check if we already know about the remote node of this connection
sender := s.peersByConnection[connID]
if sender == nil {
// authenticate sender before registration
err := authenticateSenderNode(data)
if err != nil {
s.localNode.Error("Dropping incoming message - failed to authenticate message sender: %v", err)
return
}
nodeKey, err := crypto.NewPublicKey(data.NodePubKey)
if err != nil {
return
}
sender, err = NewRemoteNode(nodeKey.String(), data.TcpAddress)
if err != nil {
return
}
// register this remote node and the new connection
sender.GetConnections()[connID] = msg.Connection
s.peers[sender.String()] = sender
s.peersByConnection[connID] = sender
}
// update the routing table - we just heard from this node
s.routingTable.Update(sender.GetRemoteNodeData())
// Let the demuxer route the message to its registered protocol handler
s.demuxer.RouteIncomingMessage(NewIncomingMessage(sender, data.Protocol, msg.Message))
}
// Pre-process a protocol message from a remote client handling decryption and authentication.
// Authenticated messages are forwarded to the demuxer for demuxing to protocol handlers.
func (s *swarmImpl) onRemoteClientProtocolMessage(msg net.IncomingMessage, c *pb.CommonMessageData) {
// Locate the session
session := s.allSessions[hex.EncodeToString(c.SessionId)]
if session == nil || !session.IsAuthenticated() {
s.localNode.Warning("Dropping incoming protocol message - expected to have an authenticated session with this node")
return
}
remoteNode := s.peers[session.RemoteNodeID()]
if remoteNode == nil {
s.localNode.Warning("Dropping incoming protocol message - expected to have data about this node for an established session")
return
}
decPayload, err := session.Decrypt(c.Payload)
if err != nil {
s.localNode.Warning("Dropping incoming protocol message - can't decrypt message payload with session key. %v", err)
return
}
pm := &pb.ProtocolMessage{}
err = proto.Unmarshal(decPayload, pm)
if err != nil {
s.localNode.Warning("Dropping incoming protocol message - Failed to get protocol message from payload. %v", err)
return
}
// authenticate message author - we already authenticated the sender via the shared session key secret
err = pm.AuthenticateAuthor()
if err != nil {
s.localNode.Warning("Dropping incoming protocol message - Failed to verify author. %v", err)
return
}
s.localNode.Info("Message %s author authenticated.", hex.EncodeToString(pm.GetMetadata().ReqId), pm.GetMetadata().Protocol)
// update the routing table - we just heard from this authenticated node
s.routingTable.Update(remoteNode.GetRemoteNodeData())
// todo: remove send error channels for this connection if this is a response to a locally sent request
// route authenticated message to the reigstered protocol
s.demuxer.RouteIncomingMessage(NewIncomingMessage(remoteNode, pm.Metadata.Protocol, decPayload))
}
// Main incoming network messages handler
// c: connection we got this message on
// msg: binary protobufs encoded data
//
// not go safe - called from event processing main loop
func (s *swarmImpl) onRemoteClientMessage(msg net.IncomingMessage) {
c := &pb.CommonMessageData{}
err := proto.Unmarshal(msg.Message, c)
if err != nil {
s.localNode.Warning("Bad request - closing connection...")
msg.Connection.Close()
return
}
// check that the message was send within a reasonable time
if ok := timesync.CheckMessageDrift(c.Timestamp); !ok {
s.localNode.Error("Received out of sync msg from %s dropping .. ", msg.Connection.RemoteAddr())
return
}
// route messages based on msg payload length
if len(c.Payload) == 0 {
// handshake messages have no enc payload
s.onRemoteClientHandshakeMessage(msg)
} else {
// protocol messages are encrypted in payload
s.onRemoteClientProtocolMessage(msg, c)
}
}
// not go safe - called from event processing main loop
func (s *swarmImpl) onMessageSentEvent(evt net.MessageSentEvent) {
// message was written to a connection without an error
callbacks := s.outgoingSendsCallbacks[evt.Connection.ID()]
if callbacks == nil {
return
}
reqID := hex.EncodeToString(evt.ID)
callback := callbacks[reqID]
if callback == nil {
return
}
// stop tracking this outgoing message - it was sent
delete(callbacks, reqID)
}
// not go safe - called from event processing main loop
func (s *swarmImpl) onConnectionError(ce net.ConnectionError) {
s.sendMessageSendError(ce)
}
// not go safe - called from event processing main loop
func (s *swarmImpl) onMessageSendError(mse net.MessageSendError) {
s.sendMessageSendError(net.ConnectionError{Connection: mse.Connection, Err: mse.Err, ID: mse.ID})
}
// send a msg send error back to the callback registered by reqID
func (s *swarmImpl) sendMessageSendError(cr net.ConnectionError) {
c := cr.Connection
callbacks := s.outgoingSendsCallbacks[c.ID()]
if callbacks == nil {
return
}
reqID := hex.EncodeToString(cr.ID)
callback := callbacks[reqID]
if callback == nil {
return
}
// there will be no more callbacks for this reqID
delete(callbacks, reqID)
go func() {
// send the error to the callback channel
callback <- SendError{cr.ID, cr.Err}
}()
}