-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprotocolV9.go
261 lines (222 loc) · 5.77 KB
/
protocolV9.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
package p2p
import (
"encoding/binary"
"encoding/gob"
"encoding/json"
"fmt"
"hash/crc32"
"time"
)
var _ Protocol = (*ProtocolV9)(nil)
// ProtocolV9 is the legacy format of the old p2p package which sends Parcels
// over the wire using gob. The V9Msg struct is equivalent to the old package's
// "Parcel" and "ParcelHeader" structure
type ProtocolV9 struct {
network NetworkID
nodeID uint32
port string
decoder *gob.Decoder
encoder *gob.Encoder
}
func newProtocolV9(netw NetworkID, nodeID uint32, listenPort string, decoder *gob.Decoder, encoder *gob.Encoder) *ProtocolV9 {
v9 := new(ProtocolV9)
v9.network = netw
v9.nodeID = nodeID
v9.port = listenPort
v9.decoder = decoder
v9.encoder = encoder
return v9
}
func v9SendHandshake(encoder *gob.Encoder, h *Handshake) error {
var payload []byte
if len(h.Alternatives) > 0 {
if data, err := json.Marshal(h.Alternatives); err != nil {
return err
} else {
payload = data
}
} else {
payload = make([]byte, 8)
binary.LittleEndian.PutUint64(payload, h.Loopback)
}
var msg V9Msg
msg.Header.Network = h.Network
msg.Header.Version = h.Version // can be 9 or 10
msg.Header.Type = h.Type
msg.Header.TargetPeer = ""
msg.Header.NodeID = uint64(h.NodeID)
msg.Header.PeerAddress = ""
msg.Header.PeerPort = h.ListenPort
msg.Header.AppHash = "NetworkMessage"
msg.Header.AppType = "Network"
msg.Payload = payload
msg.Header.Crc32 = crc32.Checksum(msg.Payload, crcTable)
msg.Header.Length = uint32(len(msg.Payload))
return encoder.Encode(msg)
}
// SendHandshake sends out a v9 structured handshake
// transform handshake into peer request
func (v9 *ProtocolV9) SendHandshake(h *Handshake) error {
if h.Type == TypeHandshake {
h.Type = TypePeerRequest
}
return v9SendHandshake(v9.encoder, h)
}
func (v9 *ProtocolV9) ReadHandshake() (*Handshake, error) {
msg, err := v9.read()
if err != nil {
return nil, err
}
hs := new(Handshake)
hs.Type = msg.Header.Type
if msg.Header.Type == TypeRejectAlternative {
var alternatives []Endpoint
if err = json.Unmarshal(msg.Payload, &alternatives); err != nil {
return nil, err
}
hs.Alternatives = alternatives
} else if len(msg.Payload) == 8 {
hs.Loopback = binary.LittleEndian.Uint64(msg.Payload)
}
hs.ListenPort = msg.Header.PeerPort
hs.Network = msg.Header.Network
hs.NodeID = uint32(msg.Header.NodeID)
hs.Version = msg.Header.Version
return hs, nil
}
// Send a parcel over the connection
func (v9 *ProtocolV9) Send(p *Parcel) error {
var msg V9Msg
msg.Header.Network = v9.network
msg.Header.Version = 9 // hardcoded
msg.Header.Type = p.ptype
msg.Header.TargetPeer = p.Address
msg.Header.NodeID = uint64(v9.nodeID)
msg.Header.PeerAddress = ""
msg.Header.PeerPort = v9.port
msg.Header.AppHash = "NetworkMessage"
msg.Header.AppType = "Network"
msg.Payload = p.Payload
msg.Header.Crc32 = crc32.Checksum(p.Payload, crcTable)
msg.Header.Length = uint32(len(p.Payload))
return v9.encoder.Encode(msg)
}
func (v9 *ProtocolV9) read() (*V9Msg, error) {
var msg V9Msg
err := v9.decoder.Decode(&msg)
if err != nil {
return nil, err
}
return &msg, nil
}
// Receive a parcel from the network. Blocking.
func (v9 *ProtocolV9) Receive() (*Parcel, error) {
msg, err := v9.read()
if err != nil {
return nil, err
}
if err = msg.Valid(); err != nil {
return nil, err
}
p := new(Parcel)
p.Address = msg.Header.TargetPeer
p.Payload = msg.Payload
p.ptype = msg.Header.Type
return p, nil
}
// Version of the protocol
func (v9 *ProtocolV9) Version() uint16 {
return 9
}
func (v9 *ProtocolV9) String() string { return "9" }
// V9Msg is the legacy format of protocol 9
type V9Msg struct {
Header V9Header
Payload []byte
}
// V9Header carries meta information about the parcel
type V9Header struct {
Network NetworkID
Version uint16
Type ParcelType
Length uint32
TargetPeer string
Crc32 uint32
PartNo uint16
PartsTotal uint16
NodeID uint64
PeerAddress string
PeerPort string
AppHash string
AppType string
}
// Valid checks header for inconsistencies
func (msg V9Msg) Valid() error {
if msg.Header.Version != 9 {
return fmt.Errorf("invalid version %v", msg.Header)
}
if len(msg.Payload) == 0 {
return fmt.Errorf("zero-length payload")
}
if msg.Header.Length != uint32(len(msg.Payload)) {
return fmt.Errorf("length in header does not match payload")
}
csum := crc32.Checksum(msg.Payload, crcTable)
if csum != msg.Header.Crc32 {
return fmt.Errorf("invalid checksum")
}
return nil
}
// V9Share is the legacy code's "Peer" struct. Resets QualityScore and Source list when
// decoding, filters out wrong Networks
type V9Share struct {
QualityScore int32
Address string
Port string
NodeID uint64
Hash string
Location uint32
Network NetworkID
Type uint8
Connections int
LastContact time.Time
Source map[string]time.Time
}
// MakePeerShare serializes the given endpoints to a V9Share encoded in json
func (v9 *ProtocolV9) MakePeerShare(ps []Endpoint) ([]byte, error) {
var conv []V9Share
src := make(map[string]time.Time)
for _, ep := range ps {
loc := IP2LocationQuick(ep.IP)
conv = append(conv, V9Share{
Address: ep.IP,
Port: ep.Port,
QualityScore: 20,
NodeID: 1,
Hash: ep.IP,
Location: loc,
Network: v9.network,
Type: 0,
Connections: 1,
LastContact: time.Time{},
Source: src,
})
}
return json.Marshal(conv)
}
// ParsePeerShare unserializes the json V9Share
func (v9 *ProtocolV9) ParsePeerShare(payload []byte) ([]Endpoint, error) {
var list []V9Share
err := json.Unmarshal(payload, &list)
if err != nil {
return nil, err
}
var conv []Endpoint
for _, s := range list {
conv = append(conv, Endpoint{
IP: s.Address,
Port: s.Port,
})
}
return conv, nil
}