-
Notifications
You must be signed in to change notification settings - Fork 41
/
handshake.go
141 lines (125 loc) · 4.13 KB
/
handshake.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
/**
* @file
* @copyright defined in aergo/LICENSE.txt
*/
package p2p
import (
"bufio"
"context"
"fmt"
"github.com/aergoio/aergo/p2p/v030"
"io"
"time"
"github.com/aergoio/aergo-lib/log"
"github.com/aergoio/aergo/p2p/p2pcommon"
"github.com/aergoio/aergo/types"
)
// LegacyInboundHSHandler handshake handler for legacy version
type LegacyInboundHSHandler struct {
*LegacyWireHandshaker
}
func (ih *LegacyInboundHSHandler) Handle(r io.Reader, w io.Writer, ttl time.Duration) (p2pcommon.MsgReadWriter, *types.Status, error) {
ctx, cancel := context.WithTimeout(context.Background(), ttl)
defer cancel()
return ih.handshakeInboundPeer(ctx, r, w)
}
// LegacyOutboundHSHandler handshake handler for legacy version
type LegacyOutboundHSHandler struct {
*LegacyWireHandshaker
}
func (oh *LegacyOutboundHSHandler) Handle(r io.Reader, w io.Writer, ttl time.Duration) (p2pcommon.MsgReadWriter, *types.Status, error) {
ctx, cancel := context.WithTimeout(context.Background(), ttl)
defer cancel()
return oh.handshakeOutboundPeer(ctx, r, w)
}
// LegacyWireHandshaker works to handshake to just connected peer, it detect chain networks
// and protocol versions, and then select InnerHandshaker for that protocol version.
type LegacyWireHandshaker struct {
pm p2pcommon.PeerManager
actorServ p2pcommon.ActorService
logger *log.Logger
peerID types.PeerID
// check if is it adhoc
localChainID *types.ChainID
remoteStatus *types.Status
}
func newHandshaker(pm p2pcommon.PeerManager, actor p2pcommon.ActorService, log *log.Logger, chainID *types.ChainID, peerID types.PeerID) *LegacyWireHandshaker {
return &LegacyWireHandshaker{pm: pm, actorServ: actor, logger: log, localChainID: chainID, peerID: peerID}
}
func (h *LegacyWireHandshaker) handshakeOutboundPeer(ctx context.Context, r io.Reader, w io.Writer) (p2pcommon.MsgReadWriter, *types.Status, error) {
bufReader, bufWriter := bufio.NewReader(r), bufio.NewWriter(w)
// send initial hsmessage
hsHeader := p2pcommon.HSHeader{Magic: p2pcommon.MAGICTest, Version: p2pcommon.P2PVersion030}
sent, err := bufWriter.Write(hsHeader.Marshal())
if err != nil {
return nil, nil, err
}
select {
case <-ctx.Done():
return nil, nil, ctx.Err()
default:
// go on
}
if sent != len(hsHeader.Marshal()) {
return nil, nil, fmt.Errorf("transport error")
}
// continue to handshake with VersionedHandshaker
innerHS, err := h.selectProtocolVersion(hsHeader.Version, bufReader, bufWriter)
if err != nil {
return nil, nil, err
}
status, err := innerHS.DoForOutbound(ctx)
h.remoteStatus = status
return innerHS.GetMsgRW(), status, err
}
func (h *LegacyWireHandshaker) handshakeInboundPeer(ctx context.Context, r io.Reader, w io.Writer) (p2pcommon.MsgReadWriter, *types.Status, error) {
var hsHeader p2pcommon.HSHeader
bufReader, bufWriter := bufio.NewReader(r), bufio.NewWriter(w)
// wait initial hsmessage
headBuf := make([]byte, p2pcommon.V030HSHeaderLength)
read, err := h.readToLen(bufReader, headBuf, 8)
if err != nil {
return nil, nil, err
}
select {
case <-ctx.Done():
return nil, nil, ctx.Err()
default:
// go on
}
if read != p2pcommon.V030HSHeaderLength {
return nil, nil, fmt.Errorf("transport error")
}
hsHeader.Unmarshal(headBuf)
// continue to handshake with VersionedHandshaker
innerHS, err := h.selectProtocolVersion(hsHeader.Version, bufReader, bufWriter)
if err != nil {
return nil, nil, err
}
status, err := innerHS.DoForInbound(ctx)
// send hsresponse
h.remoteStatus = status
return innerHS.GetMsgRW(), status, err
}
func (h *LegacyWireHandshaker) readToLen(rd io.Reader, bf []byte, max int) (int, error) {
remain := max
offset := 0
for remain > 0 {
read, err := rd.Read(bf[offset:])
if err != nil {
return offset, err
}
remain -= read
offset += read
}
return offset, nil
}
func (h *LegacyWireHandshaker) selectProtocolVersion(version p2pcommon.P2PVersion, r *bufio.Reader, w *bufio.Writer) (p2pcommon.VersionedHandshaker, error) {
switch version {
case p2pcommon.P2PVersion030:
v030hs := v030.NewV030StateHS(h.pm, h.actorServ, h.logger, h.localChainID, h.peerID, r, w)
return v030hs, nil
default:
return nil, fmt.Errorf("not supported version")
}
}