-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeer_connection.go
443 lines (386 loc) · 13.1 KB
/
peer_connection.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
package main
import (
"bytes"
"compress/zlib"
"fmt"
"io"
"log"
"net"
"github.com/a-pavlov/ged2k/data"
"github.com/a-pavlov/ged2k/proto"
)
const (
PEER_SPEED_SLOW = iota
PEER_SPEED_MEDIUM
PEER_SPEED_FAST
)
type PendingBlock struct {
block proto.PieceBlock
data []byte
region data.Region
}
func Min(a uint64, b uint64) uint64 {
if a < b {
return a
}
return b
}
func MakePendingBlock(b proto.PieceBlock, size uint64) PendingBlock {
begin := b.Start()
end := Min(b.Start()+uint64(proto.BLOCK_SIZE), size)
return PendingBlock{block: b, region: data.MakeRegion(data.Range{Begin: begin, End: end}), data: make([]byte, end-begin)}
}
func RemovePendingBlock(s []*PendingBlock, i int) []*PendingBlock {
s[i] = s[len(s)-1]
return s[:len(s)-1]
}
func (pb *PendingBlock) Receive(reader io.Reader, begin uint64, end uint64) (int, error) {
inBlockOffset := proto.InBlockOffset(begin)
chunkLen := int(end - begin)
n, err := io.ReadFull(reader, pb.data[inBlockOffset:inBlockOffset+chunkLen])
log.Printf("pending block [%d:%d] receive %d bytes offset %d\n", pb.block.PieceIndex, pb.block.BlockIndex, chunkLen, inBlockOffset)
if err == nil {
if n != chunkLen {
return n, fmt.Errorf("not enough bytes")
}
pb.region.Sub(data.Range{Begin: begin, End: end})
}
return n, err
}
func (pb *PendingBlock) ReceiveToEof(reader io.Reader, begin uint64) (int, error) {
inBlockOffset := proto.InBlockOffset(begin)
n, err := io.ReadFull(reader, pb.data[inBlockOffset:])
if err == nil {
pb.region.Sub(data.Range{Begin: begin, End: begin + uint64(n)})
}
return n, err
}
type AbortPendingBlock struct {
pendingBlock *PendingBlock
peer *Peer
}
type PeerConnectionPacket struct {
Connection *PeerConnection
Error error
}
type StatPacket struct {
Connection *PeerConnection
Counter int
}
type PeerConnection struct {
connection net.Conn
transfer *Transfer
peer *Peer
Endpoint proto.Endpoint
Connected bool
DisconnectLater bool
Stat Statistics
Speed int
requestedBlocks []*PendingBlock
closedByRequest bool
}
func NewPeerConnection(e proto.Endpoint, transfer *Transfer, p *Peer) *PeerConnection {
return &PeerConnection{Endpoint: e, transfer: transfer, peer: p, Stat: MakeStatistics(), requestedBlocks: make([]*PendingBlock, 0)}
}
func (peerConnection *PeerConnection) Start(s *Session) {
log.Println("Peer connection start", peerConnection.peer.endpoint.ToString())
if peerConnection.connection == nil {
conn, err := net.Dial("tcp", peerConnection.peer.endpoint.ToString())
if err != nil {
log.Println("Can not connect", err)
peerConnection.unregister(s, err)
return
}
hello := proto.Hello{Answer: s.CreateHelloAnswer(), HashLength: byte(proto.HASH_LEN)}
peerConnection.connection = conn
s.registerPeerConnection <- peerConnection
peerConnection.SendPacket(s, proto.OP_EDONKEYPROT, proto.OP_HELLO, &hello)
}
pc := proto.PacketCombiner{}
var lastError error = nil
for {
ph, packetBytes, err := pc.Read(peerConnection.connection)
if err != nil {
log.Printf("Peer connection read error %v\n", err)
lastError = err
break
}
peerConnection.recvStat(s, len(packetBytes)+ph.Size()-1)
sb := proto.StateBuffer{Data: packetBytes}
switch {
case ph.Packet == proto.OP_HELLO:
log.Println("Peer connection: HELLO")
hello := proto.Hello{}
sb.Read(&hello)
if sb.Error() != nil {
lastError = sb.Error()
break
}
// obtain peer information
helloAnswer := s.CreateHelloAnswer()
peerConnection.SendPacket(s, proto.OP_EDONKEYPROT, proto.OP_HELLOANSWER, &helloAnswer)
case ph.Packet == proto.OP_HELLOANSWER:
log.Println("Peer connection: HELLO_ANSWER")
helloAnswer := proto.HelloAnswer{}
sb.Read(&helloAnswer)
if sb.Error() != nil {
lastError = sb.Error()
break
}
peerConnection.SendPacket(s, proto.OP_EDONKEYPROT, proto.OP_REQUESTFILENAME, &peerConnection.transfer.Hash)
case ph.Packet == proto.OP_PUBLICIP_REQ && ph.Protocol == proto.OP_EMULEPROT:
log.Println("Public IP request has been received")
ep, err := proto.FromString("192.168.111.11:9999")
if err == nil {
ip := proto.IP(ep.Ip)
peerConnection.SendPacket(s, proto.OP_EMULEPROT, proto.OP_PUBLICIP_ANSWER, &ip)
}
case ph.Packet == proto.OP_REQUESTFILENAME:
case ph.Packet == proto.OP_REQFILENAMEANSWER:
fa := proto.FileAnswer{}
sb.Read(&fa)
if sb.Error() != nil {
lastError = sb.Error()
break
}
log.Println("Received filename answer", fa.Name.ToString())
peerConnection.SendPacket(s, proto.OP_EDONKEYPROT, proto.OP_SETREQFILEID, &peerConnection.transfer.Hash)
case ph.Packet == proto.OP_CANCELTRANSFER:
// cancel transfer received
// sent OP_REQUESTFILENAME
case ph.Packet == proto.OP_SETREQFILEID:
// got file status request
case ph.Packet == proto.OP_FILESTATUS:
fs := proto.FileStatusAnswer{}
sb.Read(&fs)
if sb.Error() != nil {
lastError = sb.Error()
log.Println("Error on file status answer", sb.Error())
break
}
log.Println("File status received, bits:", fs.BF.Bits(), "count", fs.BF.Count())
if peerConnection.transfer.Size >= proto.PIECE_SIZE_UINT64 {
peerConnection.SendPacket(s, proto.OP_EDONKEYPROT, proto.OP_HASHSETREQUEST, &peerConnection.transfer.Hash)
} else {
hs := proto.HashSet{Hash: peerConnection.transfer.Hash, PieceHashes: []proto.ED2KHash{peerConnection.transfer.Hash}}
peerConnection.transfer.hashSetChan <- &hs
peerConnection.SendPacket(s, proto.OP_EDONKEYPROT, proto.OP_STARTUPLOADREQ, &hs.Hash)
}
case ph.Packet == proto.OP_FILEREQANSNOFIL:
// no file status received
lastError = fmt.Errorf("no file answer received")
case ph.Packet == proto.OP_HASHSETREQUEST:
// hash set request received
case ph.Packet == proto.OP_HASHSETANSWER:
// got hash set answer
hs := proto.HashSet{}
sb.Read(&hs)
if sb.Error() != nil {
lastError = sb.Error()
log.Println("Can not read hash set answer")
break
} else {
log.Println("Received hash set answer")
}
peerConnection.transfer.hashSetChan <- &hs
peerConnection.SendPacket(s, proto.OP_EDONKEYPROT, proto.OP_STARTUPLOADREQ, &hs.Hash)
case ph.Packet == proto.OP_STARTUPLOADREQ:
// receive start upload request
case ph.Packet == proto.OP_ACCEPTUPLOADREQ:
log.Println("received accept uploadow req")
peerConnection.transfer.peerConnChan <- peerConnection
// got accept upload
case ph.Packet == proto.OP_QUEUERANKING:
lastError = fmt.Errorf("queue ranked: %d", sb.ReadUint16())
case ph.Packet == proto.OP_OUTOFPARTREQS:
lastError = fmt.Errorf("out of parts")
case ph.Packet == proto.OP_REQUESTPARTS:
// got 32 request parts request
case ph.Packet == proto.OP_REQUESTPARTS_I64:
// got 64 request parts request
case ph.Packet == proto.OP_SENDINGPART || ph.Packet == proto.OP_SENDINGPART_I64:
sp := proto.SendingPart{Extended: ph.Packet == proto.OP_SENDINGPART_I64}
sb.Read(&sp)
if sb.Error() != nil {
lastError = sb.Error()
break
}
log.Println("Peer connection sending part", sp.Begin, sp.End)
block := proto.FromOffset(sp.Begin)
reqBlockIndex := -1
for i, x := range peerConnection.requestedBlocks {
if x.block == block {
reqBlockIndex = i
if x.data != nil {
recvBytes, err := x.Receive(peerConnection.connection, sp.Begin, sp.End)
if err != nil {
log.Printf("Peer connection error on receive data: %v\n", err)
lastError = err
break
} else {
peerConnection.recvStat(s, recvBytes)
}
if x.region.IsEmpty() {
peerConnection.transfer.dataChan <- x
peerConnection.requestedBlocks = RemovePendingBlock(peerConnection.requestedBlocks, i)
if len(peerConnection.requestedBlocks) == 0 {
// all blocks completed
peerConnection.transfer.peerConnChan <- peerConnection
}
}
}
break
}
}
if reqBlockIndex == -1 {
lastError = fmt.Errorf("incoming block %s has not corresponding index", block.ToString())
}
// got 32 sending part response
// got 64 sending part response
case ph.Packet == proto.OP_COMPRESSEDPART || ph.Packet == proto.OP_COMPRESSEDPART_I64:
// got 32 compressed part response
// got 64 compressed part response
cp := proto.CompressedPart{Extended: ph.Packet == proto.OP_COMPRESSEDPART_I64}
sb.Read(&cp)
if sb.Error() != nil {
lastError = sb.Error()
break
}
fmt.Printf("Recv compressed part, offset: %d, compressed data length %d\n", cp.Offset, cp.CompressedDataLength)
block := proto.FromOffset(cp.Offset)
reqBlockIndex := -1
for i, x := range peerConnection.requestedBlocks {
if x.block == block {
reqBlockIndex = i
compressedData := make([]byte, cp.CompressedDataLength)
recvBytes, err := io.ReadFull(peerConnection.connection, compressedData)
log.Printf("compressed recv bytes %d\n", recvBytes)
if err != nil {
lastError = err
break
} else {
peerConnection.recvStat(s, recvBytes)
}
b := bytes.NewReader(compressedData)
z, err := zlib.NewReader(b)
if err != nil {
lastError = err
break
}
defer z.Close()
// unpack data
recvBytes, err = x.ReceiveToEof(z, cp.Offset)
if err != nil {
lastError = err
break
} else {
log.Printf("receive %d uncompressed bytes\n", recvBytes)
// already taken in account above
}
if x.region.IsEmpty() {
peerConnection.transfer.dataChan <- x
peerConnection.requestedBlocks = RemovePendingBlock(peerConnection.requestedBlocks, i)
if len(peerConnection.requestedBlocks) == 0 {
peerConnection.transfer.peerConnChan <- peerConnection
}
}
break
}
}
if reqBlockIndex == -1 {
lastError = fmt.Errorf("incoming block %s has not corresponding index", block.ToString())
}
case ph.Packet == proto.OP_CANCELTRANSFER:
lastError = fmt.Errorf("cancel transfer")
case ph.Packet == proto.OP_END_OF_DOWNLOAD:
// got end of download response
lastError = fmt.Errorf("end of download")
default:
log.Printf("Receive unknown protocol:%x packet: %x packetBytes: %d\n", ph.Protocol, ph.Packet, ph.Bytes)
}
if lastError != nil {
log.Printf("stop peer connection by error %v\n", lastError)
break
}
}
peerConnection.unregister(s, lastError)
}
func (connection *PeerConnection) SendPacket(s *Session, protocol byte, packet byte, data proto.Serializable) {
if data == nil {
b := make([]byte, proto.HEADER_SIZE)
ph := proto.PacketHeader{Protocol: protocol, Packet: packet, Bytes: 1}
ph.Write(b)
n, err := connection.connection.Write(b[:proto.HEADER_SIZE])
if err != nil {
connection.Close(false)
} else {
connection.sendStat(s, n)
}
}
sz := proto.DataSize(data)
log.Printf("Packet size calculated: %d\n", sz)
b := make([]byte, sz+proto.HEADER_SIZE)
stateBuffer := proto.StateBuffer{Data: b[proto.HEADER_SIZE:]}
data.Put(&stateBuffer)
if stateBuffer.Error() != nil {
log.Printf("Send error %v for %d bytes\n", stateBuffer.Error(), sz)
connection.Close(false)
} else {
log.Println("Wrote", stateBuffer.Offset(), "bytes")
}
bytesCount := uint32(stateBuffer.Offset() + 1)
ph := proto.PacketHeader{Protocol: protocol, Packet: packet, Bytes: bytesCount}
ph.Write(b)
log.Printf("Bytes: %x\n", b)
n, err := connection.connection.Write(b[:stateBuffer.Offset()+proto.HEADER_SIZE])
if err != nil {
log.Printf("peer connection can not write packet %v\n", err)
connection.Close(false)
} else {
connection.sendStat(s, n)
}
}
func (peerConnection *PeerConnection) Close(byRequest bool) {
if peerConnection.Connected {
fmt.Println("close connection")
if peerConnection.connection == nil {
panic("connection is nil")
}
err := peerConnection.connection.Close()
if err != nil {
log.Printf("unable to close peed connection %v", err)
}
} else {
fmt.Println("no connection - set disconnect later")
peerConnection.DisconnectLater = true
}
}
func (peerConnection *PeerConnection) recvStat(s *Session, n int) {
s.statReceiveChan <- StatPacket{Connection: peerConnection, Counter: n}
}
func (peerConnection *PeerConnection) sendStat(s *Session, n int) {
s.statSendChan <- StatPacket{Connection: peerConnection, Counter: n}
}
func (peerConnection *PeerConnection) unregister(s *Session, err error) {
for _, pb := range peerConnection.requestedBlocks {
peerConnection.transfer.abortPendingBlockChan <- AbortPendingBlock{pendingBlock: pb, peer: peerConnection.peer}
}
s.unregisterPeerConnection <- PeerConnectionPacket{Connection: peerConnection, Error: err}
}
func (peerConnection *PeerConnection) RefreshSpeed() int {
if peerConnection.transfer != nil {
peerDRate := peerConnection.Stat.DownloadRate()
transferDRate := peerConnection.transfer.Stat.DownloadRate()
switch {
case peerDRate > 512 && peerDRate > transferDRate/16:
peerConnection.Speed = PEER_SPEED_FAST
case peerDRate > 4096 && peerDRate > transferDRate/64:
peerConnection.Speed = PEER_SPEED_MEDIUM
case peerDRate < transferDRate/15 && peerConnection.Speed == PEER_SPEED_FAST:
peerConnection.Speed = PEER_SPEED_MEDIUM
default:
peerConnection.Speed = PEER_SPEED_SLOW
}
}
return peerConnection.Speed
}