-
Notifications
You must be signed in to change notification settings - Fork 3
/
peer.go
429 lines (370 loc) · 10.4 KB
/
peer.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
/*
* Copyright (c) 2016 Mark Samman <https://github.com/marksamman/gotorrent>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package main
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"log"
"math"
"net"
"sync"
"sync/atomic"
"time"
)
const (
Choke = iota
Unchoke
Interested
Uninterested
Have
Bitfield
Request
PieceBlock
Cancel
Port
)
type Peer struct {
ip net.IP
port uint16
torrent *Torrent
pieces map[uint32]struct{}
queue []*PeerPiece
connection net.Conn
remoteChoked bool
remoteInterested bool
localChoked bool
localInterested bool
closed uint32
requestPieceChannel chan uint32
sendPieceBlockChannel chan *BlockMessage
sendHaveChannel chan uint32
done chan struct{}
queueLock sync.Mutex
id string
}
type PeerPiece struct {
index uint32
blocks [][]byte
writes int
reqWrites int
}
type BlockMessage struct {
index uint32
begin uint32
block []byte
}
func NewPeer(torrent *Torrent) *Peer {
peer := Peer{}
peer.torrent = torrent
peer.remoteChoked = true
peer.localChoked = true
return &peer
}
func (peer *Peer) readN(n int) ([]byte, error) {
buf := make([]byte, n)
_, err := io.ReadFull(peer.connection, buf)
return buf, err
}
func (peer *Peer) close() {
if atomic.CompareAndSwapUint32(&peer.closed, 0, 1) {
peer.connection.Close()
}
}
func (peer *Peer) connect() {
defer func() {
peer.torrent.decrementPeerCount <- struct{}{}
}()
var addr string
if ip := peer.ip.To4(); ip != nil {
addr = fmt.Sprintf("%s:%d", ip.String(), peer.port)
} else {
addr = fmt.Sprintf("[%s]:%d", peer.ip.String(), peer.port)
}
var err error
if peer.connection, err = net.DialTimeout("tcp", addr, time.Second*5); err != nil {
return
}
defer peer.close()
// Send handshake
if _, err := peer.connection.Write(peer.torrent.handshake); err != nil {
log.Printf("failed to send handshake to peer: %s\n", err)
return
}
// Receive handshake
if handshake, err := peer.readN(68); err != nil {
log.Printf("failed to read handshake from peer: %s\n", err)
return
} else if !bytes.Equal(handshake[0:20], []byte("\x13BitTorrent protocol")) {
log.Printf("bad protocol from peer: %s\n", addr)
return
} else if !bytes.Equal(handshake[28:48], peer.torrent.getInfoHash()) {
log.Printf("info hash mismatch from peer: %s\n", addr)
return
} else if len(peer.id) != 0 {
if !bytes.Equal(handshake[48:68], []byte(peer.id)) {
log.Printf("peer id mismatch from peer: %s\n", addr)
return
}
} else {
peer.id = string(handshake[48:68])
}
peer.requestPieceChannel = make(chan uint32)
peer.sendPieceBlockChannel = make(chan *BlockMessage)
peer.sendHaveChannel = make(chan uint32)
peer.done = make(chan struct{})
peer.pieces = make(map[uint32]struct{})
peer.torrent.addPeerChannel <- peer
errorChannel := make(chan struct{})
go peer.receiver(errorChannel)
for {
select {
case pieceIndex := <-peer.requestPieceChannel:
peer.sendPieceRequest(pieceIndex)
case blockMessage := <-peer.sendPieceBlockChannel:
peer.sendPieceBlockMessage(blockMessage)
case pieceIndex := <-peer.sendHaveChannel:
peer.sendHaveMessage(pieceIndex)
case <-errorChannel:
go func() {
peer.torrent.removePeerChannel <- peer
}()
for {
select {
// Until we receive another message over "done" to
// confirm the removal of the peer, it's possible that
// Torrent can send messages over other channels.
case <-peer.sendHaveChannel:
case <-peer.sendPieceBlockChannel:
case <-peer.requestPieceChannel:
case <-peer.done:
return
}
}
case <-peer.done:
peer.close()
}
}
}
func (peer *Peer) receiver(errorChannel chan struct{}) {
for {
lengthHeader, err := peer.readN(4)
if err != nil {
errorChannel <- struct{}{}
break
}
length := binary.BigEndian.Uint32(lengthHeader)
if length == 0 {
// keep-alive
continue
}
data, err := peer.readN(int(length))
if err != nil {
errorChannel <- struct{}{}
break
}
if err := peer.processMessage(length, data[0], data[1:]); err != nil {
log.Print(err)
errorChannel <- struct{}{}
break
}
}
}
func (peer *Peer) processMessage(length uint32, messageType byte, payload []byte) error {
switch messageType {
case Choke:
if length != 1 {
return errors.New("length of choke packet must be 1")
}
peer.remoteChoked = true
case Unchoke:
if length != 1 {
return errors.New("length of unchoke packet must be 1")
}
peer.remoteChoked = false
peer.queueLock.Lock()
for _, piece := range peer.queue {
peer.requestPiece(piece)
}
peer.queueLock.Unlock()
case Interested:
if length != 1 {
return errors.New("length of interested packet must be 1")
}
peer.remoteInterested = true
peer.sendUnchokeMessage()
case Uninterested:
if length != 1 {
return errors.New("length of not interested packet must be 1")
}
peer.remoteInterested = false
case Have:
if length != 5 {
return errors.New("length of have packet must be 5")
}
index := binary.BigEndian.Uint32(payload)
peer.torrent.havePieceChannel <- &HavePieceMessage{peer, index}
case Bitfield:
if length < 2 {
return errors.New("length of bitfield packet must be at least 2")
}
peer.torrent.bitfieldChannel <- &BitfieldMessage{peer, payload}
case Request:
if length != 13 {
return errors.New("length of request packet must be 13")
}
if !peer.remoteInterested {
return errors.New("peer sent request without showing interest")
}
if peer.localChoked {
return errors.New("peer sent request while choked")
}
index := binary.BigEndian.Uint32(payload)
begin := binary.BigEndian.Uint32(payload[4:])
blockLength := binary.BigEndian.Uint32(payload[8:])
if blockLength > 32768 {
return errors.New("peer requested length over 32KB")
}
peer.torrent.blockRequestChannel <- &BlockRequestMessage{peer, index, begin, blockLength}
case PieceBlock:
if length < 10 {
return errors.New("length of piece packet must be at least 10")
}
blockLength := length - 9
if blockLength > 16384 {
return errors.New("received block over 16KB")
}
index := binary.BigEndian.Uint32(payload)
peer.queueLock.Lock()
defer peer.queueLock.Unlock()
idx := 0
var piece *PeerPiece
for k, v := range peer.queue {
if v.index == index {
idx = k
piece = v
break
}
}
if piece == nil {
return errors.New("received index we didn't ask for")
}
begin := binary.BigEndian.Uint32(payload[4:])
blockIndex := begin / 16384
if int(blockIndex) >= len(piece.blocks) {
return errors.New("received too big block index")
}
piece.blocks[blockIndex] = payload[8:]
piece.writes++
if piece.writes == piece.reqWrites {
// Glue all blocks together into a piece
pieceData := []byte{}
for k := range piece.blocks {
pieceData = append(pieceData, piece.blocks[k]...)
}
// Send piece to Torrent
peer.torrent.pieceChannel <- &PieceMessage{peer, index, pieceData}
// Remove piece from peer
peer.queue = append(peer.queue[:idx], peer.queue[idx+1:]...)
}
case Cancel:
if length != 13 {
return errors.New("length of cancel packet must be 13")
}
// TODO: Handle cancel
/*
index := binary.BigEndian.Uint32(payload)
begin := binary.BigEndian.Uint32(payload[4:])
length := binary.BigEndian.Uint32(payload[8:])
*/
case Port:
if length != 3 {
return errors.New("length of port packet must be 3")
}
// port := binary.BigEndian.Uint16(payload)
// Peer has a DHT node on port
}
return nil
}
func (peer *Peer) sendInterested() {
if peer.localInterested {
return
}
peer.connection.Write([]byte{0, 0, 0, 1, Interested})
peer.localInterested = true
}
func (peer *Peer) sendRequest(index, begin, length uint32) {
packet := make([]byte, 17)
binary.BigEndian.PutUint32(packet, 13) // Length
packet[4] = Request
binary.BigEndian.PutUint32(packet[5:], index)
binary.BigEndian.PutUint32(packet[9:], begin)
binary.BigEndian.PutUint32(packet[13:], length)
peer.connection.Write(packet)
}
func (peer *Peer) sendPieceRequest(index uint32) {
peer.sendInterested()
pieceLength := peer.torrent.getPieceLength(index)
blocks := int(math.Ceil(float64(pieceLength) / 16384))
piece := &PeerPiece{index, make([][]byte, blocks), 0, blocks}
peer.queueLock.Lock()
peer.queue = append(peer.queue, piece)
peer.queueLock.Unlock()
if !peer.remoteChoked {
peer.requestPiece(piece)
}
}
func (peer *Peer) requestPiece(piece *PeerPiece) {
var pos uint32
pieceLength := peer.torrent.getPieceLength(piece.index)
for pieceLength > 16384 {
peer.sendRequest(piece.index, pos, 16384)
pieceLength -= 16384
pos += 16384
}
peer.sendRequest(piece.index, pos, uint32(pieceLength))
}
func (peer *Peer) sendPieceBlockMessage(blockMessage *BlockMessage) {
packet := make([]byte, 13)
binary.BigEndian.PutUint32(packet, uint32(9+len(blockMessage.block)))
packet[4] = PieceBlock
binary.BigEndian.PutUint32(packet[5:], blockMessage.index)
binary.BigEndian.PutUint32(packet[9:], blockMessage.begin)
peer.connection.Write(packet)
peer.connection.Write(blockMessage.block)
}
func (peer *Peer) sendHaveMessage(pieceIndex uint32) {
packet := make([]byte, 9)
packet[3] = 5 // Length
packet[4] = Have
binary.BigEndian.PutUint32(packet[5:], pieceIndex)
peer.connection.Write(packet)
}
func (peer *Peer) sendUnchokeMessage() {
if !peer.localChoked {
return
}
peer.connection.Write([]byte{0, 0, 0, 1, Unchoke})
peer.localChoked = false
}