forked from DNAProject/DNA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
netserver.go
701 lines (607 loc) · 18.5 KB
/
netserver.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
/*
* Copyright (C) 2018 The DNA Authors
* This file is part of The DNA library.
*
* The DNA is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The DNA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The DNA. If not, see <http://www.gnu.org/licenses/>.
*/
package netserver
import (
"errors"
"io/ioutil"
"math/rand"
"net"
"strings"
"sync"
"time"
"github.com/dnaproject2/DNA/common/config"
"github.com/dnaproject2/DNA/common/log"
"github.com/dnaproject2/DNA/core/ledger"
"github.com/dnaproject2/DNA/p2pserver/common"
msgpack "github.com/dnaproject2/DNA/p2pserver/message/msg_pack"
"github.com/dnaproject2/DNA/p2pserver/message/types"
p2p "github.com/dnaproject2/DNA/p2pserver/net/protocol"
"github.com/dnaproject2/DNA/p2pserver/peer"
)
//NewNetServer return the net object in p2p
func NewNetServer() p2p.P2P {
n := &NetServer{
NetChan: make(chan *types.MsgPayload, common.CHAN_CAPABILITY),
}
n.PeerAddrMap.PeerAddress = make(map[string]*peer.Peer)
n.init()
return n
}
//NetServer represent all the actions in net layer
type NetServer struct {
base peer.PeerCom
listener net.Listener
NetChan chan *types.MsgPayload
ConnectingNodes
PeerAddrMap
Np *peer.NbrPeers
connectLock sync.Mutex
inConnRecord InConnectionRecord
outConnRecord OutConnectionRecord
OwnAddress string //network`s own address(ip : sync port),which get from version check
Cert string //network's own certificate
Addr string //network's own account address in base58 format
}
//InConnectionRecord include all addr connected
type InConnectionRecord struct {
sync.RWMutex
InConnectingAddrs []string
}
//OutConnectionRecord include all addr accepted
type OutConnectionRecord struct {
sync.RWMutex
OutConnectingAddrs []string
}
//ConnectingNodes include all addr in connecting state
type ConnectingNodes struct {
sync.RWMutex
ConnectingAddrs []string
}
//PeerAddrMap include all addr-peer list
type PeerAddrMap struct {
sync.RWMutex
PeerAddress map[string]*peer.Peer
}
//init initializes attribute of network server
func (this *NetServer) init() error {
this.base.SetVersion(common.PROTOCOL_VERSION)
if config.DefConfig.Consensus.EnableConsensus {
this.base.SetServices(uint64(common.VERIFY_NODE))
} else {
this.base.SetServices(uint64(common.SERVICE_NODE))
}
if config.DefConfig.P2PNode.NodePort == 0 {
log.Error("[p2p]link port invalid")
return errors.New("[p2p]invalid link port")
}
this.base.SetPort(uint16(config.DefConfig.P2PNode.NodePort))
this.base.SetRelay(true)
rand.Seed(time.Now().UnixNano())
id := rand.Uint64()
this.base.SetID(id)
err := this.SetCert(config.DefConfig.P2PNode.CertPath)
if err != nil {
log.Errorf("[p2p]set certificate error, %s", err)
return errors.New("[p2p]set certificate error")
}
log.Infof("[p2p]init peer ID to %d", this.base.GetID())
this.Np = &peer.NbrPeers{}
this.Np.Init()
return nil
}
//InitListen start listening on the config port
func (this *NetServer) Start() {
this.startListening()
}
//GetVersion return self peer`s version
func (this *NetServer) GetVersion() uint32 {
return this.base.GetVersion()
}
//GetId return peer`s id
func (this *NetServer) GetID() uint64 {
return this.base.GetID()
}
func (this *NetServer) SetAddr(addr string) {
this.Addr = addr
}
//GetAddr returns self peer's account address
func (this *NetServer) GetAddr() string {
return this.Addr
}
// SetHeight sets the local's height
func (this *NetServer) SetHeight(height uint64) {
this.base.SetHeight(height)
}
// GetHeight return peer's heigh
func (this *NetServer) GetHeight() uint64 {
return this.base.GetHeight()
}
//GetTime return the last contact time of self peer
func (this *NetServer) GetTime() int64 {
t := time.Now()
return t.UnixNano()
}
//GetServices return the service state of self peer
func (this *NetServer) GetServices() uint64 {
return this.base.GetServices()
}
//GetPort return the sync port
func (this *NetServer) GetPort() uint16 {
return this.base.GetPort()
}
// SetCert set the PEM encoded certificate read from the file
func (this *NetServer) SetCert(file string) error {
data, err := ioutil.ReadFile(file)
if err != nil {
return err
}
this.Cert = string(data)
return nil
}
//GetCert return self peer's certificate
func (this *NetServer) GetCert() string {
return this.Cert
}
//GetHttpInfoPort return the port support info via http
func (this *NetServer) GetHttpInfoPort() uint16 {
return this.base.GetHttpInfoPort()
}
//GetRelay return whether net module can relay msg
func (this *NetServer) GetRelay() bool {
return this.base.GetRelay()
}
// GetPeer returns a peer with the peer id
func (this *NetServer) GetPeer(id uint64) *peer.Peer {
return this.Np.GetPeer(id)
}
//return nbr peers collection
func (this *NetServer) GetNp() *peer.NbrPeers {
return this.Np
}
//GetNeighborAddrs return all the nbr peer`s addr
func (this *NetServer) GetNeighborAddrs() []common.PeerAddr {
return this.Np.GetNeighborAddrs()
}
//GetConnectionCnt return the total number of valid connections
func (this *NetServer) GetConnectionCnt() uint32 {
return this.Np.GetNbrNodeCnt()
}
//AddNbrNode add peer to nbr peer list
func (this *NetServer) AddNbrNode(remotePeer *peer.Peer) {
this.Np.AddNbrNode(remotePeer)
}
//DelNbrNode delete nbr peer by id
func (this *NetServer) DelNbrNode(id uint64) (*peer.Peer, bool) {
return this.Np.DelNbrNode(id)
}
//GetNeighbors return all nbr peer
func (this *NetServer) GetNeighbors() []*peer.Peer {
return this.Np.GetNeighbors()
}
//NodeEstablished return whether a peer is establish with self according to id
func (this *NetServer) NodeEstablished(id uint64) bool {
return this.Np.NodeEstablished(id)
}
//Xmit called by actor, broadcast msg
func (this *NetServer) Xmit(msg types.Message) {
this.Np.Broadcast(msg)
}
//GetMsgChan return sync or consensus channel when msgrouter need msg input
func (this *NetServer) GetMsgChan() chan *types.MsgPayload {
return this.NetChan
}
//Tx send data buf to peer
func (this *NetServer) Send(p *peer.Peer, msg types.Message) error {
if p != nil {
return p.Send(msg)
}
log.Warn("[p2p]send to a invalid peer")
return errors.New("[p2p]send to a invalid peer")
}
//IsPeerEstablished return the establise state of given peer`s id
func (this *NetServer) IsPeerEstablished(p *peer.Peer) bool {
if p != nil {
return this.Np.NodeEstablished(p.GetID())
}
return false
}
//Connect used to connect net address under sync or cons mode
func (this *NetServer) Connect(addr string) error {
if this.IsAddrInOutConnRecord(addr) {
log.Debugf("[p2p]Address: %s is in OutConnectionRecord,", addr)
return nil
}
if this.IsOwnAddress(addr) {
return nil
}
if !this.AddrValid(addr) {
return nil
}
this.connectLock.Lock()
connCount := uint(this.GetOutConnRecordLen())
if connCount >= config.DefConfig.P2PNode.MaxConnOutBound {
log.Warnf("[p2p]Connect: out connections(%d) reach the max limit(%d)", connCount,
config.DefConfig.P2PNode.MaxConnOutBound)
this.connectLock.Unlock()
return errors.New("[p2p]connect: out connections reach the max limit")
}
this.connectLock.Unlock()
if this.IsNbrPeerAddr(addr) {
return nil
}
this.connectLock.Lock()
if added := this.AddOutConnectingList(addr); added == false {
log.Debug("[p2p]node exist in connecting list", addr)
}
this.connectLock.Unlock()
isTls := config.DefConfig.P2PNode.IsTLS
var conn net.Conn
var err error
var remotePeer *peer.Peer
if isTls {
conn, err = TLSDial(addr)
if err != nil {
this.RemoveFromConnectingList(addr)
log.Debugf("[p2p]connect %s failed:%s", addr, err.Error())
return err
}
} else {
conn, err = nonTLSDial(addr)
if err != nil {
this.RemoveFromConnectingList(addr)
log.Debugf("[p2p]connect %s failed:%s", addr, err.Error())
return err
}
}
addr = conn.RemoteAddr().String()
log.Debugf("[p2p]peer %s connect with %s with %s",
conn.LocalAddr().String(), conn.RemoteAddr().String(),
conn.RemoteAddr().Network())
this.AddOutConnRecord(addr)
remotePeer = peer.NewPeer()
this.AddPeerAddress(addr, remotePeer)
remotePeer.Link.SetAddr(addr)
remotePeer.Link.SetConn(conn)
remotePeer.AttachChan(this.NetChan)
go remotePeer.Link.Rx()
remotePeer.SetState(common.HAND)
version := msgpack.NewVersion(this, ledger.DefLedger.GetCurrentBlockHeight())
err = remotePeer.Send(version)
if err != nil {
this.RemoveFromOutConnRecord(addr)
log.Warn(err)
return err
}
return nil
}
//Halt stop all net layer logic
func (this *NetServer) Halt() {
peers := this.Np.GetNeighbors()
for _, p := range peers {
p.Close()
}
if this.listener != nil {
this.listener.Close()
}
}
//establishing the connection to remote peers and listening for inbound peers
func (this *NetServer) startListening() error {
var err error
syncPort := this.base.GetPort()
if syncPort == 0 {
log.Error("[p2p]sync port invalid")
return errors.New("[p2p]sync port invalid")
}
err = this.startNetListening(syncPort)
if err != nil {
log.Error("[p2p]start sync listening fail")
return err
}
return nil
}
// startNetListening starts a sync listener on the port for the inbound peer
func (this *NetServer) startNetListening(port uint16) error {
var err error
this.listener, err = createListener(port)
if err != nil {
log.Error("[p2p]failed to create sync listener")
return errors.New("[p2p]failed to create sync listener")
}
go this.startNetAccept(this.listener)
log.Infof("[p2p]start listen on sync port %d", port)
return nil
}
//startNetAccept accepts the sync connection from the inbound peer
func (this *NetServer) startNetAccept(listener net.Listener) {
for {
conn, err := listener.Accept()
if err != nil {
log.Error("[p2p]error accepting ", err.Error())
return
}
log.Debug("[p2p]remote sync node connect with ",
conn.RemoteAddr(), conn.LocalAddr())
if !this.AddrValid(conn.RemoteAddr().String()) {
log.Warnf("[p2p]remote %s not in reserved list, close it ", conn.RemoteAddr())
conn.Close()
continue
}
if this.IsAddrInInConnRecord(conn.RemoteAddr().String()) {
conn.Close()
continue
}
syncAddrCount := uint(this.GetInConnRecordLen())
if syncAddrCount >= config.DefConfig.P2PNode.MaxConnInBound {
log.Warnf("[p2p]SyncAccept: total connections(%d) reach the max limit(%d), conn closed",
syncAddrCount, config.DefConfig.P2PNode.MaxConnInBound)
conn.Close()
continue
}
remoteIp, err := common.ParseIPAddr(conn.RemoteAddr().String())
if err != nil {
log.Warn("[p2p]parse ip error ", err.Error())
conn.Close()
continue
}
connNum := this.GetIpCountInInConnRecord(remoteIp)
if connNum >= config.DefConfig.P2PNode.MaxConnInBoundForSingleIP {
log.Warnf("[p2p]SyncAccept: connections(%d) with ip(%s) has reach the max limit(%d), "+
"conn closed", connNum, remoteIp, config.DefConfig.P2PNode.MaxConnInBoundForSingleIP)
conn.Close()
continue
}
remotePeer := peer.NewPeer()
addr := conn.RemoteAddr().String()
this.AddInConnRecord(addr)
this.AddPeerAddress(addr, remotePeer)
remotePeer.Link.SetAddr(addr)
remotePeer.Link.SetConn(conn)
remotePeer.AttachChan(this.NetChan)
go remotePeer.Link.Rx()
}
}
//record the peer which is going to be dialed and sent version message but not in establish state
func (this *NetServer) AddOutConnectingList(addr string) (added bool) {
this.ConnectingNodes.Lock()
defer this.ConnectingNodes.Unlock()
for _, a := range this.ConnectingAddrs {
if strings.Compare(a, addr) == 0 {
return false
}
}
log.Trace("[p2p]add to out connecting list", addr)
this.ConnectingAddrs = append(this.ConnectingAddrs, addr)
return true
}
//Remove the peer from connecting list if the connection is established
func (this *NetServer) RemoveFromConnectingList(addr string) {
this.ConnectingNodes.Lock()
defer this.ConnectingNodes.Unlock()
addrs := this.ConnectingAddrs[:0]
for _, a := range this.ConnectingAddrs {
if a != addr {
addrs = append(addrs, a)
}
}
log.Trace("[p2p]remove from out connecting list", addr)
this.ConnectingAddrs = addrs
}
//record the peer which is going to be dialed and sent version message but not in establish state
func (this *NetServer) GetOutConnectingListLen() (count uint) {
this.ConnectingNodes.RLock()
defer this.ConnectingNodes.RUnlock()
return uint(len(this.ConnectingAddrs))
}
//check peer from connecting list
func (this *NetServer) IsAddrFromConnecting(addr string) bool {
this.ConnectingNodes.Lock()
defer this.ConnectingNodes.Unlock()
for _, a := range this.ConnectingAddrs {
if strings.Compare(a, addr) == 0 {
return true
}
}
return false
}
//find exist peer from addr map
func (this *NetServer) GetPeerFromAddr(addr string) *peer.Peer {
var p *peer.Peer
this.PeerAddrMap.RLock()
defer this.PeerAddrMap.RUnlock()
p, ok := this.PeerAddress[addr]
if ok {
return p
}
return nil
}
//IsNbrPeerAddr return result whether the address is under connecting
func (this *NetServer) IsNbrPeerAddr(addr string) bool {
var addrNew string
this.Np.RLock()
defer this.Np.RUnlock()
for _, p := range this.Np.List {
if p.GetState() == common.HAND || p.GetState() == common.HAND_SHAKE ||
p.GetState() == common.ESTABLISH {
addrNew = p.Link.GetAddr()
if strings.Compare(addrNew, addr) == 0 {
return true
}
}
}
return false
}
//AddPeerAddress add sync addr to peer-addr map
func (this *NetServer) AddPeerAddress(addr string, p *peer.Peer) {
this.PeerAddrMap.Lock()
defer this.PeerAddrMap.Unlock()
log.Debugf("[p2p]AddPeerAddress %s", addr)
this.PeerAddress[addr] = p
}
//RemovePeerAddress remove sync addr from peer-addr map
func (this *NetServer) RemovePeerAddress(addr string) {
this.PeerAddrMap.Lock()
defer this.PeerAddrMap.Unlock()
if _, ok := this.PeerAddress[addr]; ok {
delete(this.PeerAddress, addr)
log.Debugf("[p2p]delete Sync Address %s", addr)
}
}
//GetPeerAddressCount return length of cons addr from peer-addr map
func (this *NetServer) GetPeerAddressCount() (count uint) {
this.PeerAddrMap.RLock()
defer this.PeerAddrMap.RUnlock()
return uint(len(this.PeerAddress))
}
//AddInConnRecord add in connection to inConnRecord
func (this *NetServer) AddInConnRecord(addr string) {
this.inConnRecord.Lock()
defer this.inConnRecord.Unlock()
for _, a := range this.inConnRecord.InConnectingAddrs {
if strings.Compare(a, addr) == 0 {
return
}
}
this.inConnRecord.InConnectingAddrs = append(this.inConnRecord.InConnectingAddrs, addr)
log.Debugf("[p2p]add in record %s", addr)
}
//IsAddrInInConnRecord return result whether addr is in inConnRecordList
func (this *NetServer) IsAddrInInConnRecord(addr string) bool {
this.inConnRecord.RLock()
defer this.inConnRecord.RUnlock()
for _, a := range this.inConnRecord.InConnectingAddrs {
if strings.Compare(a, addr) == 0 {
return true
}
}
return false
}
//IsIPInInConnRecord return result whether the IP is in inConnRecordList
func (this *NetServer) IsIPInInConnRecord(ip string) bool {
this.inConnRecord.RLock()
defer this.inConnRecord.RUnlock()
var ipRecord string
for _, addr := range this.inConnRecord.InConnectingAddrs {
ipRecord, _ = common.ParseIPAddr(addr)
if 0 == strings.Compare(ipRecord, ip) {
return true
}
}
return false
}
//RemoveInConnRecord remove in connection from inConnRecordList
func (this *NetServer) RemoveFromInConnRecord(addr string) {
this.inConnRecord.Lock()
defer this.inConnRecord.Unlock()
addrs := []string{}
for _, a := range this.inConnRecord.InConnectingAddrs {
if strings.Compare(a, addr) != 0 {
addrs = append(addrs, a)
}
}
log.Debugf("[p2p]remove in record %s", addr)
this.inConnRecord.InConnectingAddrs = addrs
}
//GetInConnRecordLen return length of inConnRecordList
func (this *NetServer) GetInConnRecordLen() int {
this.inConnRecord.RLock()
defer this.inConnRecord.RUnlock()
return len(this.inConnRecord.InConnectingAddrs)
}
//GetIpCountInInConnRecord return count of in connections with single ip
func (this *NetServer) GetIpCountInInConnRecord(ip string) uint {
this.inConnRecord.RLock()
defer this.inConnRecord.RUnlock()
var count uint
var ipRecord string
for _, addr := range this.inConnRecord.InConnectingAddrs {
ipRecord, _ = common.ParseIPAddr(addr)
if 0 == strings.Compare(ipRecord, ip) {
count++
}
}
return count
}
//AddOutConnRecord add out connection to outConnRecord
func (this *NetServer) AddOutConnRecord(addr string) {
this.outConnRecord.Lock()
defer this.outConnRecord.Unlock()
for _, a := range this.outConnRecord.OutConnectingAddrs {
if strings.Compare(a, addr) == 0 {
return
}
}
this.outConnRecord.OutConnectingAddrs = append(this.outConnRecord.OutConnectingAddrs, addr)
log.Debugf("[p2p]add out record %s", addr)
}
//IsAddrInOutConnRecord return result whether addr is in outConnRecord
func (this *NetServer) IsAddrInOutConnRecord(addr string) bool {
this.outConnRecord.RLock()
defer this.outConnRecord.RUnlock()
for _, a := range this.outConnRecord.OutConnectingAddrs {
if strings.Compare(a, addr) == 0 {
return true
}
}
return false
}
//RemoveOutConnRecord remove out connection from outConnRecord
func (this *NetServer) RemoveFromOutConnRecord(addr string) {
this.outConnRecord.Lock()
defer this.outConnRecord.Unlock()
addrs := []string{}
for _, a := range this.outConnRecord.OutConnectingAddrs {
if strings.Compare(a, addr) != 0 {
addrs = append(addrs, a)
}
}
log.Debugf("[p2p]remove out record %s", addr)
this.outConnRecord.OutConnectingAddrs = addrs
}
//GetOutConnRecordLen return length of outConnRecord
func (this *NetServer) GetOutConnRecordLen() int {
this.outConnRecord.RLock()
defer this.outConnRecord.RUnlock()
return len(this.outConnRecord.OutConnectingAddrs)
}
//AddrValid whether the addr could be connect or accept
func (this *NetServer) AddrValid(addr string) bool {
if config.DefConfig.P2PNode.ReservedPeersOnly && len(config.DefConfig.P2PNode.ReservedCfg.ReservedPeers) > 0 {
for _, ip := range config.DefConfig.P2PNode.ReservedCfg.ReservedPeers {
if strings.HasPrefix(addr, ip) {
log.Info("[p2p]found reserved peer :", addr)
return true
}
}
return false
}
return true
}
//check own network address
func (this *NetServer) IsOwnAddress(addr string) bool {
if addr == this.OwnAddress {
return true
}
return false
}
//Set own network address
func (this *NetServer) SetOwnAddress(addr string) {
if addr != this.OwnAddress {
log.Infof("[p2p]set own address %s", addr)
this.OwnAddress = addr
}
}