-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
971 lines (843 loc) · 25.6 KB
/
server.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
package p2p
import (
"bytes"
"errors"
"fmt"
"math/rand"
"net"
"runtime"
"sort"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/elastos/Elastos.ELA/dpos/p2p/addrmgr"
"github.com/elastos/Elastos.ELA/dpos/p2p/connmgr"
"github.com/elastos/Elastos.ELA/dpos/p2p/hub"
"github.com/elastos/Elastos.ELA/dpos/p2p/msg"
"github.com/elastos/Elastos.ELA/dpos/p2p/peer"
"github.com/elastos/Elastos.ELA/p2p"
)
const (
// Max number of inbound and outbound peers.
maxPeers = 72 * 2
// connectionRetryInterval is the base amount of time to wait in between
// retries when connecting to persistent peers. It is adjusted by the
// number of retries such that there is a retry backoff.
connectionRetryInterval = time.Second * 5
)
var (
// ErrSendMessageFailed is the error to reply when send message to peer
// failed.
ErrSendMessageFailed = errors.New("send message failed")
)
// simpleAddr implements the net.Addr interface with two struct fields
type simpleAddr struct {
net, addr string
}
// String returns the address.
//
// This is part of the net.Addr interface.
func (a simpleAddr) String() string {
return a.addr
}
// Network returns the network.
//
// This is part of the net.Addr interface.
func (a simpleAddr) Network() string {
return a.net
}
// Ensure simpleAddr implements the net.Addr interface.
var _ net.Addr = simpleAddr{}
// newPeerMsg represent a new connected peer.
type newPeerMsg struct {
sp *serverPeer
reply chan struct{}
}
// donePeerMsg represent a disconnected peer.
type donePeerMsg struct {
sp *serverPeer
reply chan struct{}
}
// broadcastMsg provides the ability to house a message to be broadcast
// to all connected peers except specified excluded peers.
type broadcastMsg struct {
message p2p.Message
excludePeers []peer.PID
}
// peerState maintains state of inbound, persistent, outbound peers as well
// as banned peers and outbound groups.
type peerState struct {
connectPeers map[peer.PID]struct{}
inboundPeers map[uint64]*serverPeer
outboundPeers map[uint64]*serverPeer
}
// havePeer returns how many connected peers matches to given PID, if no matches
// return 0
func (ps *peerState) havePeer(pid peer.PID) int {
var matches int
ps.forAllPeers(func(sp *serverPeer) {
if sp.PID().Equal(pid) {
matches++
}
})
return matches
}
// Count returns the count of all known peers.
func (ps *peerState) Count() int {
return len(ps.inboundPeers) + len(ps.outboundPeers)
}
// forAllOutboundPeers is a helper function that runs closure on all outbound
// peers known to peerState.
func (ps *peerState) forAllOutboundPeers(closure func(sp *serverPeer)) {
for _, e := range ps.outboundPeers {
closure(e)
}
}
// forAllPeers is a helper function that runs closure on all peers known to
// peerState.
func (ps *peerState) forAllPeers(closure func(sp *serverPeer)) {
for _, e := range ps.inboundPeers {
closure(e)
}
ps.forAllOutboundPeers(closure)
}
// server provides a server for handling communications to and from peers.
type server struct {
// The following variables must only be used atomically.
// Putting the uint64s first makes them 64-bit aligned for 32-bit systems.
started int32
shutdown int32
shutdownSched int32
startupTime int64
cfg Config
hubService *hub.Hub
addrManager *addrmgr.AddrManager
connManager *connmgr.ConnManager
peerQueue chan interface{}
query chan interface{}
broadcast chan broadcastMsg
wg sync.WaitGroup
quit chan struct{}
}
// IPeer extends the peer to maintain state shared by the server.
type serverPeer struct {
*peer.Peer
connReq *connmgr.ConnReq
server *server
sentAddrs bool
knownAddresses map[string]struct{}
quit chan struct{}
}
// newServerPeer returns a new IPeer instance. The peer needs to be set by
// the caller.
func newServerPeer(s *server) *serverPeer {
return &serverPeer{
server: s,
knownAddresses: make(map[string]struct{}),
quit: make(chan struct{}),
}
}
// OnVersion is invoked when a peer receives a version message and is
// used to negotiate the protocol version details as well as kick start
// the communications.
func (sp *serverPeer) OnVersion(_ *peer.Peer, v *msg.Version) {
// Check if received PID matches PeerAddr PID.
if sp.connReq != nil && !bytes.Equal(v.PID[:], sp.connReq.PID[:]) {
log.Infof("Disconnecting peer %v - version PID not match", sp)
sp.Disconnect()
return
}
// Advertise the local address when the server accepts outbound incoming
// connections.
if !sp.Inbound() {
addr := msg.NewAddr(sp.server.cfg.Localhost, sp.server.cfg.DefaultPort)
sp.QueueMessage(addr, nil)
}
// Add the remote peer time as a sample for creating an offset against
// the local clock to keep the network time in sync.
sp.server.cfg.TimeSource.AddTimeSample(sp.Addr(), v.Timestamp)
// Add valid peer to the server.
sp.server.AddPeer(sp)
}
// OnAddr is invoked when a peer receives an addr message and is used to notify
// the server about advertised address.
func (sp *serverPeer) OnAddr(_ *peer.Peer, msg *msg.Addr) {
addr := normalizeAddress(msg.Host, fmt.Sprint(msg.Port))
sp.server.addrManager.AddAddress(sp.PID(), simpleAddr{net: "tcp",
addr: addr})
}
// PID returns the peer's public key id.
func (sp *serverPeer) PID() peer.PID {
return sp.Peer.PID()
}
// ToPeer returns the underlying peer instance.
//
// This function is safe for concurrent access and is part of the IPeer
// interface implementation.
func (sp *serverPeer) ToPeer() *peer.Peer {
if sp == nil {
return nil
}
return sp.Peer
}
// handleAddPeerMsg deals with adding new peers. It is invoked from the
// peerHandler goroutine.
func (s *server) handleAddPeerMsg(state *peerState, sp *serverPeer) bool {
if sp == nil {
return false
}
// Ignore new peers if we're shutting down.
if atomic.LoadInt32(&s.shutdown) != 0 {
log.Infof("New peer %s ignored - server is shutting down", sp)
sp.Disconnect()
return false
}
// Ignore peers not exist in connect list.
if _, ok := state.connectPeers[sp.PID()]; !ok {
log.Infof("New peer %s ignored - not in connect list", sp)
sp.Disconnect()
return false
}
// Add the new peer and start it.
log.Debugf("New peer %s", sp)
if sp.Inbound() {
state.inboundPeers[sp.ID()] = sp
} else {
state.outboundPeers[sp.ID()] = sp
}
// Add message handler for the new peer.
if sp.server.cfg.HandleMessage != nil {
sp.Peer.AddMessageFunc(func(peer *peer.Peer, msg p2p.Message) {
sp.server.cfg.HandleMessage(peer.PID(), msg)
})
}
// Notify peer state change for new peer added.
if s.cfg.StateNotifier != nil {
s.cfg.StateNotifier.OnNewPeer(sp.PID())
}
return true
}
// handleDonePeerMsg deals with peers that have signalled they are done. It is
// invoked from the peerHandler goroutine.
func (s *server) handleDonePeerMsg(state *peerState, sp *serverPeer) {
var list map[uint64]*serverPeer
if sp.Inbound() {
list = state.inboundPeers
} else {
list = state.outboundPeers
}
if _, ok := list[sp.ID()]; ok {
delete(list, sp.ID())
log.Debugf("Removed peer %s", sp)
}
// Remove connection request if peer not in connect list.
if sp.connReq != nil {
s.connManager.Disconnect(sp.connReq.PID)
}
// Notify peer state change for done peer removed.
if s.cfg.StateNotifier != nil {
s.cfg.StateNotifier.OnDonePeer(sp.PID())
}
}
// handleBroadcastMsg deals with broadcasting messages to peers. It is invoked
// from the peerHandler goroutine.
func (s *server) handleBroadcastMsg(state *peerState, bmsg *broadcastMsg) {
// Group peers by PID, so each peer only get one broadcast message.
groups := make(map[peer.PID]*serverPeer)
state.forAllPeers(func(sp *serverPeer) {
if !sp.Connected() {
return
}
for _, ep := range bmsg.excludePeers {
if sp.PID().Equal(ep) {
return
}
}
groups[sp.PID()] = sp
})
for _, sp := range groups {
sp.QueueMessage(bmsg.message, nil)
}
}
type connectPeersMsg struct {
peers []peer.PID
reply chan struct{}
}
type sendToPeerMsg struct {
pid peer.PID
msg p2p.Message
reply chan error
}
type getConnCountMsg struct {
reply chan int32
}
type getPeersMsg struct {
reply chan []*serverPeer
}
type dumpPeersInfoMsg struct {
reply chan []*PeerInfo
}
// handleQuery is the central handler for all queries and commands from other
// goroutines related to peer state.
func (s *server) handleQuery(state *peerState, querymsg interface{}) {
switch msg := querymsg.(type) {
case connectPeersMsg:
// A connectPeers message will make server connect to the peers
// in connect list, and disconnect peers not in connect list.
// connectPeers saves the new received connect peer addresses.
connectPeers := make(map[peer.PID]struct{})
// Loop through the new received connect peer addresses.
for _, pid := range msg.peers {
// Do not create connection to self.
if pid.Equal(s.cfg.PID) {
continue
}
// Add address to connectPeers
connectPeers[pid] = struct{}{}
// Peers in previous connect list already.
if _, ok := state.connectPeers[pid]; ok {
continue
}
// Connect the peer.
go s.connManager.Connect(pid)
}
// disconnectPeers saves the peers need to be disconnected.
disconnectPeers := make(map[peer.PID]struct{})
// Peers not in new received connect list need to be disconnected.
for pid := range state.connectPeers {
if _, ok := connectPeers[pid]; !ok {
disconnectPeers[pid] = struct{}{}
}
}
// Disconnect peers in disconnect list.
for pid := range disconnectPeers {
s.connManager.Remove(pid)
}
state.forAllPeers(func(sp *serverPeer) {
if _, ok := disconnectPeers[sp.PID()]; ok {
sp.Disconnect()
}
})
// Set new connect list into state.
state.connectPeers = connectPeers
// Notify peer state change for connect list changed.
if s.cfg.StateNotifier != nil {
s.cfg.StateNotifier.OnConnectPeers(msg.peers)
}
msg.reply <- struct{}{}
case sendToPeerMsg:
// SendToPeer message send a message to the connected peer by PID.
// There may be multiple connected peers matches the given PID,
// we just pick the first matched peer to send the message, if
// something goes wrong, we try next. If all attempts are failed
// return an send message failed error.
sent := false
done := make(chan error, 1)
state.forAllPeers(func(sp *serverPeer) {
if !sent && sp.PID().Equal(msg.pid) {
sp.QueueMessage(msg.msg, done)
if err := <-done; err == nil {
sent = true
}
}
})
if !sent {
msg.reply <- ErrSendMessageFailed
} else {
msg.reply <- nil
}
case getConnCountMsg:
connected := int32(0)
state.forAllPeers(func(sp *serverPeer) {
if sp.Connected() {
connected++
}
})
msg.reply <- connected
case getPeersMsg:
peers := make([]*serverPeer, 0, state.Count())
state.forAllPeers(func(sp *serverPeer) {
if !sp.Connected() {
return
}
peers = append(peers, sp)
})
msg.reply <- peers
case dumpPeersInfoMsg:
// DumpPeesInfo returns the peers info in connect peers list. The peers
// in connect list can be in 4 states.
// 1. NoneConnection, no outbound or inbound connection.
// 2. 2WayConnection, have both outbound and inbound connections.
// 3. OutboundOnly, has one outbound connection.
// 4. InboundOnly, has one inbound connection.
// To get the actual state of a peer in connect list, we need to loop
// through outbound peers list, inbound peers list and connect peers
// list, these are high cost operations. So this method should not be
// called frequently.
peers := make(map[peer.PID]*PeerInfo)
for _, sp := range state.outboundPeers {
peers[sp.PID()] = &PeerInfo{
PID: sp.PID(),
Addr: sp.Addr(),
State: CSOutboundOnly,
}
}
for _, sp := range state.inboundPeers {
if pi, ok := peers[sp.PID()]; ok {
pi.State = CS2WayConnection
continue
}
peers[sp.PID()] = &PeerInfo{
PID: sp.PID(),
Addr: sp.Addr(),
State: CSInboundOnly,
}
}
for pid := range state.connectPeers {
if _, ok := peers[pid]; ok {
continue
}
addr := "unknown"
na := s.addrManager.GetAddress(pid)
if na != nil {
addr = na.String()
}
peers[pid] = &PeerInfo{
PID: pid,
Addr: addr,
State: CSNoneConnection,
}
}
msg.reply <- sortPeersInfo(peers)
}
}
// sortPeersInfo returns an ordered PeerInfo slice by peer's PID in asc.
func sortPeersInfo(peers map[peer.PID]*PeerInfo) []*PeerInfo {
list := make([]*PeerInfo, 0, len(peers))
for _, pi := range peers {
list = append(list, pi)
}
sort.Slice(list, func(i, j int) bool {
return bytes.Compare(list[i].PID[:], list[j].PID[:]) < 0
})
return list
}
func (s *server) pingNonce(pid peer.PID) uint64 {
if s.cfg.PingNonce == nil {
return 0
}
return s.cfg.PingNonce(pid)
}
func (s *server) pongNonce(pid peer.PID) uint64 {
if s.cfg.PongNonce == nil {
return 0
}
return s.cfg.PongNonce(pid)
}
// newPeerConfig returns the configuration for the given serverPeer.
func newPeerConfig(sp *serverPeer) *peer.Config {
return &peer.Config{
PID: sp.server.cfg.PID,
Magic: sp.server.cfg.MagicNumber,
Port: sp.server.cfg.DefaultPort,
PingInterval: sp.server.cfg.PingInterval,
Sign: sp.server.cfg.Sign,
PingNonce: sp.server.pingNonce,
PongNonce: sp.server.pongNonce,
MakeEmptyMessage: sp.server.cfg.MakeEmptyMessage,
MessageFunc: func(peer *peer.Peer, m p2p.Message) {
switch m := m.(type) {
case *msg.Version:
sp.OnVersion(peer, m)
case *msg.Addr:
sp.OnAddr(peer, m)
}
},
}
}
// inboundPeerConnected is invoked by the connection manager when a new inbound
// connection is established. It initializes a new inbound server peer
// instance, associates it with the connection, and starts a goroutine to wait
// for disconnection.
func (s *server) inboundPeerConnected(conn net.Conn) {
// If hub service is enabled, intercept the connection.
if s.hubService != nil {
conn = s.hubService.Intercept(conn)
}
// If the connection has been intercepted, do nothing.
if conn == nil {
return
}
sp := newServerPeer(s)
sp.Peer = peer.NewInboundPeer(newPeerConfig(sp))
sp.AssociateConnection(conn)
go s.peerDoneHandler(sp)
}
// outboundPeerConnected is invoked by the connection manager when a new
// outbound connection is established. It initializes a new outbound server
// peer instance, associates it with the relevant state such as the connection
// request instance and the connection itself, and finally notifies the address
// manager of the attempt.
func (s *server) outboundPeerConnected(c *connmgr.ConnReq, conn net.Conn) {
sp := newServerPeer(s)
cfg := newPeerConfig(sp)
cfg.Target = hub.PIDTo16(c.PID)
p, err := peer.NewOutboundPeer(cfg, c.Addr.String())
if err != nil {
log.Debugf("Cannot create outbound peer %s: %v", c.Addr, err)
s.connManager.Disconnect(c.PID)
}
sp.Peer = p
sp.connReq = c
sp.AssociateConnection(conn)
go s.peerDoneHandler(sp)
}
// peerDoneHandler handles peer disconnects by notifiying the server that it's
// done along with other performing other desirable cleanup.
func (s *server) peerDoneHandler(sp *serverPeer) {
sp.WaitForDisconnect()
reply := make(chan struct{})
s.peerQueue <- donePeerMsg{sp: sp, reply: reply}
<-reply
close(sp.quit)
}
// peerHandler is used to handle peer operations such as adding and removing
// peers to and from the server, banning peers, and broadcasting messages to
// peers. It must be run in a goroutine.
func (s *server) peerHandler() {
s.addrManager.Start()
state := &peerState{
inboundPeers: make(map[uint64]*serverPeer),
outboundPeers: make(map[uint64]*serverPeer),
}
go s.connManager.Start()
out:
for {
select {
// Deal with peer messages.
case pmsg := <-s.peerQueue:
s.handlePeerMsg(state, pmsg)
// Message to broadcast to all connected peers except those
// which are excluded by the message.
case bmsg := <-s.broadcast:
s.handleBroadcastMsg(state, &bmsg)
case qmsg := <-s.query:
s.handleQuery(state, qmsg)
case <-s.quit:
// Disconnect all peers on server shutdown.
state.forAllPeers(func(sp *serverPeer) {
sp.Disconnect()
})
break out
}
}
s.connManager.Stop()
s.addrManager.Stop()
// Drain channels before exiting so nothing is left waiting around
// to send.
cleanup:
for {
select {
case <-s.peerQueue:
case <-s.broadcast:
case <-s.query:
default:
break cleanup
}
}
s.wg.Done()
}
// handlePeerMsg deals with adding/removing and ban peer message.
func (s *server) handlePeerMsg(state *peerState, msg interface{}) {
switch msg := msg.(type) {
case newPeerMsg:
s.handleAddPeerMsg(state, msg.sp)
msg.reply <- struct{}{}
case donePeerMsg:
s.handleDonePeerMsg(state, msg.sp)
msg.reply <- struct{}{}
}
}
// AddAddr adds an arbiter address into AddrManager.
func (s *server) AddAddr(pid peer.PID, addr string) {
addr = normalizeAddress(addr, fmt.Sprint(s.cfg.DefaultPort))
s.addrManager.AddAddress(pid, &simpleAddr{net: "tcp", addr: addr})
}
// AddPeer adds a new peer that has already been connected to the server.
func (s *server) AddPeer(sp *serverPeer) {
reply := make(chan struct{})
s.peerQueue <- newPeerMsg{sp: sp, reply: reply}
<-reply
}
// BroadcastMessage sends msg to all peers currently connected to the server
// except those in the passed peers to exclude.
func (s *server) BroadcastMessage(msg p2p.Message, exclPeers ...peer.PID) {
// XXX: Need to determine if this is an alert that has already been
// broadcast and refrain from broadcasting again.
bmsg := broadcastMsg{message: msg, excludePeers: exclPeers}
s.broadcast <- bmsg
}
// ConnectedCount returns the number of currently connected peers.
func (s *server) ConnectedCount() int32 {
replyChan := make(chan int32)
s.query <- getConnCountMsg{reply: replyChan}
return <-replyChan
}
// ConnectPeers let server connect the peers in the given peers, and
// disconnect peers that not in the peers.
func (s *server) ConnectPeers(peers []peer.PID) {
reply := make(chan struct{})
s.query <- connectPeersMsg{peers: peers, reply: reply}
<-reply
}
// SendMessageToPeer send a message to the peer with the given id, error
// will be returned if there is no matches, or fail to send the message.
func (s *server) SendMessageToPeer(id peer.PID, msg p2p.Message) error {
reply := make(chan error)
s.query <- sendToPeerMsg{pid: id, msg: msg, reply: reply}
return <-reply
}
// ConnectedPeers returns an array consisting of all connected peers.
//
// This function is safe for concurrent access and is part of the
// IServer interface implementation.
func (s *server) ConnectedPeers() []Peer {
replyChan := make(chan []*serverPeer)
s.query <- getPeersMsg{reply: replyChan}
serverPeers := <-replyChan
peers := make([]Peer, 0, len(serverPeers))
for _, sp := range serverPeers {
peers = append(peers, (Peer)(sp))
}
return peers
}
// DumpPeersInfo returns an array consisting of all peers state in connect list.
//
// This function is safe for concurrent access and is part of the
// IServer interface implementation.
func (s *server) DumpPeersInfo() []*PeerInfo {
replyChan := make(chan []*PeerInfo)
s.query <- dumpPeersInfoMsg{reply: replyChan}
return <-replyChan
}
// Start begins accepting connections from peers.
func (s *server) Start() {
// Already started?
if atomic.AddInt32(&s.started, 1) != 1 {
return
}
// server startup time. Used for the uptime command for uptime calculation.
s.startupTime = time.Now().Unix()
// Start the peer handler which in turn starts the address and block
// managers.
s.wg.Add(1)
go s.peerHandler()
}
// Stop gracefully shuts down the server by stopping and disconnecting all
// peers and the main listener.
func (s *server) Stop() error {
// Make sure this only happens once.
if atomic.AddInt32(&s.shutdown, 1) != 1 {
log.Infof("server is already in the process of shutting down")
return nil
}
log.Warnf("server shutting down")
// Signal the remaining goroutines to quit.
close(s.quit)
return nil
}
// WaitForShutdown blocks until the main listener and peer handlers are stopped.
func (s *server) WaitForShutdown() {
s.wg.Wait()
}
// ScheduleShutdown schedules a server shutdown after the specified duration.
// It also dynamically adjusts how often to warn the server is going down based
// on remaining duration.
func (s *server) ScheduleShutdown(duration time.Duration) {
// Don't schedule shutdown more than once.
if atomic.AddInt32(&s.shutdownSched, 1) != 1 {
return
}
log.Warnf("Server shutdown in %v", duration)
go func() {
remaining := duration
tickDuration := dynamicTickDuration(remaining)
done := time.After(remaining)
ticker := time.NewTicker(tickDuration)
out:
for {
select {
case <-done:
ticker.Stop()
s.Stop()
break out
case <-ticker.C:
remaining = remaining - tickDuration
if remaining < time.Second {
continue
}
// Change tick duration dynamically based on remaining time.
newDuration := dynamicTickDuration(remaining)
if tickDuration != newDuration {
tickDuration = newDuration
ticker.Stop()
ticker = time.NewTicker(tickDuration)
}
log.Warnf("Server shutdown in %v", remaining)
}
}
}()
}
func (s *server) dialTimeout(addr net.Addr) (net.Conn, error) {
log.Debugf("Server dial addr %s", addr)
addr, err := addrStringToNetAddr(addr.String())
if err != nil {
return nil, err
}
return net.DialTimeout(addr.Network(), addr.String(), s.cfg.ConnectTimeout)
}
// parseListeners determines whether each listen address is IPv4 and IPv6 and
// returns a slice of appropriate net.Addrs to listen on with TCP. It also
// properly detects addresses which apply to "all interfaces" and adds the
// address as both IPv4 and IPv6.
func parseListeners(addr string) ([]net.Addr, error) {
netAddrs := make([]net.Addr, 0, 2)
host, _, err := net.SplitHostPort(addr)
if err != nil {
// Shouldn't happen due to already being normalized.
return nil, err
}
// Empty host or host of * on plan9 is both IPv4 and IPv6.
if host == "" || (host == "*" && runtime.GOOS == "plan9") {
netAddrs = append(netAddrs, simpleAddr{net: "tcp4", addr: addr})
netAddrs = append(netAddrs, simpleAddr{net: "tcp6", addr: addr})
}
return netAddrs, nil
}
// NewServer returns a new server instance by the given config.
// Use start to begin accepting connections from peers.
func NewServer(origCfg *Config) (*server, error) {
cfg := *origCfg // Copy to avoid mutating caller.
if cfg.ConnectTimeout <= 0 {
cfg.ConnectTimeout = defaultConnectTimeout
}
if cfg.PingInterval <= 0 {
cfg.PingInterval = defaultPingInterval
}
listeners, err := initListeners(cfg)
if err != nil {
return nil, err
}
admgr := addrmgr.New(cfg.DataDir)
var hubService *hub.Hub
if cfg.EnableHub {
hubService = hub.New(cfg.MagicNumber, cfg.PID, admgr)
}
s := server{
cfg: cfg,
hubService: hubService,
addrManager: admgr,
peerQueue: make(chan interface{}, maxPeers),
query: make(chan interface{}, maxPeers),
broadcast: make(chan broadcastMsg, maxPeers),
quit: make(chan struct{}),
}
cmgr, err := connmgr.New(&connmgr.Config{
Listeners: listeners,
OnAccept: s.inboundPeerConnected,
RetryDuration: connectionRetryInterval,
Dial: s.dialTimeout,
OnConnection: s.outboundPeerConnected,
GetAddr: func(pid [33]byte) (net.Addr, error) {
na := s.addrManager.GetAddress(pid)
if na == nil {
return nil, fmt.Errorf("can not find network"+
" address for %s", peer.PID(pid))
}
return na, nil
},
})
if err != nil {
return nil, err
}
s.connManager = cmgr
return &s, nil
}
// initListeners initializes the configured net listeners and adds any bound
// addresses to the address manager. Returns the listeners and a NAT interface,
// which is non-nil if UPnP is in use.
func initListeners(cfg Config) ([]net.Listener, error) {
// Listen for TCP connections at the configured addresses
netAddrs, err := parseListeners(fmt.Sprintf(":%d", cfg.DefaultPort))
if err != nil {
return nil, err
}
listeners := make([]net.Listener, 0, len(netAddrs))
for _, addr := range netAddrs {
listener, err := net.Listen(addr.Network(), addr.String())
if err != nil {
log.Warnf("Can't listen on %s: %v", addr, err)
continue
}
listeners = append(listeners, listener)
}
return listeners, nil
}
// addrStringToNetAddr takes an address in the form of 'host:port' and returns
// a net.Addr which maps to the original address with any host names resolved
// to IP addresses. It also handles tor addresses properly by returning a
// net.Addr that encapsulates the address.
func addrStringToNetAddr(addr string) (net.Addr, error) {
host, strPort, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
port, err := strconv.Atoi(strPort)
if err != nil {
return nil, err
}
// Skip if host is already an IP address.
if ip := net.ParseIP(host); ip != nil {
return &net.TCPAddr{
IP: ip,
Port: port,
}, nil
}
// Attempt to look up an IP address associated with the parsed host.
ips, err := net.LookupIP(host)
if err != nil {
return nil, err
}
if len(ips) == 0 {
return nil, fmt.Errorf("no addresses found for %s", host)
}
return &net.TCPAddr{
IP: ips[0],
Port: port,
}, nil
}
// dynamicTickDuration is a convenience function used to dynamically choose a
// tick duration based on remaining time. It is primarily used during
// server shutdown to make shutdown warnings more frequent as the shutdown time
// approaches.
func dynamicTickDuration(remaining time.Duration) time.Duration {
switch {
case remaining <= time.Second*5:
return time.Second
case remaining <= time.Second*15:
return time.Second * 5
case remaining <= time.Minute:
return time.Second * 15
case remaining <= time.Minute*5:
return time.Minute
case remaining <= time.Minute*15:
return time.Minute * 5
case remaining <= time.Hour:
return time.Minute * 15
}
return time.Hour
}
func init() {
rand.Seed(time.Now().UnixNano())
}