2013-09-29 06:11:24 +08:00
|
|
|
package peer_protocol
|
|
|
|
|
2013-09-30 19:51:08 +08:00
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2013-10-02 15:57:19 +08:00
|
|
|
"io/ioutil"
|
2016-09-12 15:10:11 +08:00
|
|
|
"sync"
|
2013-09-30 19:51:08 +08:00
|
|
|
)
|
|
|
|
|
2013-09-29 06:11:24 +08:00
|
|
|
type (
|
|
|
|
MessageType byte
|
|
|
|
Integer uint32
|
|
|
|
)
|
|
|
|
|
2013-09-30 19:51:08 +08:00
|
|
|
func (i *Integer) Read(r io.Reader) error {
|
|
|
|
return binary.Read(r, binary.BigEndian, i)
|
|
|
|
}
|
|
|
|
|
2016-02-21 14:25:26 +08:00
|
|
|
// It's perfectly fine to cast these to an int.
|
|
|
|
func (i Integer) Int() int {
|
|
|
|
return int(i)
|
|
|
|
}
|
|
|
|
|
2013-09-29 14:45:17 +08:00
|
|
|
const (
|
|
|
|
Protocol = "\x13BitTorrent protocol"
|
|
|
|
)
|
|
|
|
|
2013-09-29 06:11:24 +08:00
|
|
|
const (
|
2014-03-17 22:44:22 +08:00
|
|
|
Choke MessageType = iota
|
|
|
|
Unchoke // 1
|
|
|
|
Interested // 2
|
|
|
|
NotInterested // 3
|
|
|
|
Have // 4
|
|
|
|
Bitfield // 5
|
|
|
|
Request // 6
|
|
|
|
Piece // 7
|
|
|
|
Cancel // 8
|
2014-08-25 20:12:16 +08:00
|
|
|
Port // 9
|
2015-03-12 17:06:23 +08:00
|
|
|
|
|
|
|
// BEP 6
|
|
|
|
Suggest = 0xd // 13
|
|
|
|
HaveAll = 0xe // 14
|
|
|
|
HaveNone = 0xf // 15
|
|
|
|
Reject = 0x10 // 16
|
|
|
|
AllowedFast = 0x11 // 17
|
|
|
|
|
|
|
|
Extended = 20
|
2014-06-26 22:57:07 +08:00
|
|
|
|
|
|
|
HandshakeExtendedID = 0
|
2014-06-28 17:38:31 +08:00
|
|
|
|
|
|
|
RequestMetadataExtensionMsgType = 0
|
|
|
|
DataMetadataExtensionMsgType = 1
|
|
|
|
RejectMetadataExtensionMsgType = 2
|
2013-09-29 06:11:24 +08:00
|
|
|
)
|
|
|
|
|
2013-09-30 19:51:08 +08:00
|
|
|
type Message struct {
|
|
|
|
Keepalive bool
|
|
|
|
Type MessageType
|
2013-09-29 06:11:24 +08:00
|
|
|
Index, Begin, Length Integer
|
2013-09-30 19:51:08 +08:00
|
|
|
Piece []byte
|
|
|
|
Bitfield []bool
|
2014-06-26 22:57:07 +08:00
|
|
|
ExtendedID byte
|
|
|
|
ExtendedPayload []byte
|
2014-08-25 20:12:16 +08:00
|
|
|
Port uint16
|
2013-09-30 19:51:08 +08:00
|
|
|
}
|
|
|
|
|
2017-08-31 12:30:53 +08:00
|
|
|
func (msg Message) MustMarshalBinary() []byte {
|
|
|
|
b, err := msg.MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2013-09-30 19:51:08 +08:00
|
|
|
func (msg Message) MarshalBinary() (data []byte, err error) {
|
|
|
|
buf := &bytes.Buffer{}
|
2014-05-28 23:22:51 +08:00
|
|
|
if !msg.Keepalive {
|
|
|
|
err = buf.WriteByte(byte(msg.Type))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch msg.Type {
|
2015-03-12 17:06:23 +08:00
|
|
|
case Choke, Unchoke, Interested, NotInterested, HaveAll, HaveNone:
|
2014-05-28 23:22:51 +08:00
|
|
|
case Have:
|
|
|
|
err = binary.Write(buf, binary.BigEndian, msg.Index)
|
2015-03-12 17:06:23 +08:00
|
|
|
case Request, Cancel, Reject:
|
2014-05-28 23:22:51 +08:00
|
|
|
for _, i := range []Integer{msg.Index, msg.Begin, msg.Length} {
|
|
|
|
err = binary.Write(buf, binary.BigEndian, i)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case Bitfield:
|
|
|
|
_, err = buf.Write(marshalBitfield(msg.Bitfield))
|
|
|
|
case Piece:
|
|
|
|
for _, i := range []Integer{msg.Index, msg.Begin} {
|
|
|
|
err = binary.Write(buf, binary.BigEndian, i)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n, err := buf.Write(msg.Piece)
|
2013-09-30 19:51:08 +08:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2014-05-28 23:22:51 +08:00
|
|
|
if n != len(msg.Piece) {
|
|
|
|
panic(n)
|
2014-03-17 22:44:22 +08:00
|
|
|
}
|
2014-06-26 22:57:07 +08:00
|
|
|
case Extended:
|
|
|
|
err = buf.WriteByte(msg.ExtendedID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, err = buf.Write(msg.ExtendedPayload)
|
2014-08-25 20:12:16 +08:00
|
|
|
case Port:
|
|
|
|
err = binary.Write(buf, binary.BigEndian, msg.Port)
|
2014-05-28 23:22:51 +08:00
|
|
|
default:
|
2014-06-26 22:57:07 +08:00
|
|
|
err = fmt.Errorf("unknown message type: %v", msg.Type)
|
2014-03-17 22:44:22 +08:00
|
|
|
}
|
2013-09-30 19:51:08 +08:00
|
|
|
}
|
2013-10-01 16:43:18 +08:00
|
|
|
data = make([]byte, 4+buf.Len())
|
|
|
|
binary.BigEndian.PutUint32(data, uint32(buf.Len()))
|
|
|
|
if buf.Len() != copy(data[4:], buf.Bytes()) {
|
|
|
|
panic("bad copy")
|
|
|
|
}
|
2013-09-30 19:51:08 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type Decoder struct {
|
|
|
|
R *bufio.Reader
|
2016-09-12 15:10:11 +08:00
|
|
|
Pool *sync.Pool
|
2014-03-20 21:42:40 +08:00
|
|
|
MaxLength Integer // TODO: Should this include the length header or not?
|
2013-09-30 19:51:08 +08:00
|
|
|
}
|
|
|
|
|
2016-09-25 08:31:43 +08:00
|
|
|
func readByte(r io.Reader) (b byte, err error) {
|
|
|
|
var arr [1]byte
|
|
|
|
n, err := r.Read(arr[:])
|
|
|
|
b = arr[0]
|
|
|
|
if n == 1 {
|
|
|
|
err = nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-08-22 15:39:18 +08:00
|
|
|
// io.EOF is returned if the source terminates cleanly on a message boundary.
|
2013-09-30 19:51:08 +08:00
|
|
|
func (d *Decoder) Decode(msg *Message) (err error) {
|
|
|
|
var length Integer
|
|
|
|
err = binary.Read(d.R, binary.BigEndian, &length)
|
|
|
|
if err != nil {
|
2014-08-22 15:39:18 +08:00
|
|
|
if err != io.EOF {
|
|
|
|
err = fmt.Errorf("error reading message length: %s", err)
|
|
|
|
}
|
2013-09-30 19:51:08 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if length > d.MaxLength {
|
|
|
|
return errors.New("message too long")
|
|
|
|
}
|
2014-05-22 22:36:47 +08:00
|
|
|
if length == 0 {
|
|
|
|
msg.Keepalive = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
msg.Keepalive = false
|
2016-09-25 08:31:43 +08:00
|
|
|
r := &io.LimitedReader{d.R, int64(length)}
|
2014-07-17 14:01:48 +08:00
|
|
|
// Check that all of r was utilized.
|
2014-03-20 21:42:40 +08:00
|
|
|
defer func() {
|
2014-07-17 14:01:48 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-09-25 08:31:43 +08:00
|
|
|
if r.N != 0 {
|
|
|
|
err = fmt.Errorf("%d bytes unused in message type %d", r.N, msg.Type)
|
2014-03-20 21:42:40 +08:00
|
|
|
}
|
|
|
|
}()
|
2013-09-30 19:51:08 +08:00
|
|
|
msg.Keepalive = false
|
2016-09-25 08:31:43 +08:00
|
|
|
c, err := readByte(r)
|
2013-09-30 19:51:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
msg.Type = MessageType(c)
|
|
|
|
switch msg.Type {
|
2015-03-12 17:06:23 +08:00
|
|
|
case Choke, Unchoke, Interested, NotInterested, HaveAll, HaveNone:
|
2013-09-30 19:51:08 +08:00
|
|
|
return
|
|
|
|
case Have:
|
2013-10-02 15:57:19 +08:00
|
|
|
err = msg.Index.Read(r)
|
2015-03-12 17:06:23 +08:00
|
|
|
case Request, Cancel, Reject:
|
2014-03-17 22:44:22 +08:00
|
|
|
for _, data := range []*Integer{&msg.Index, &msg.Begin, &msg.Length} {
|
|
|
|
err = data.Read(r)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2013-09-30 19:51:08 +08:00
|
|
|
case Bitfield:
|
|
|
|
b := make([]byte, length-1)
|
2013-10-02 15:57:19 +08:00
|
|
|
_, err = io.ReadFull(r, b)
|
2013-09-30 19:51:08 +08:00
|
|
|
msg.Bitfield = unmarshalBitfield(b)
|
2013-10-02 15:57:19 +08:00
|
|
|
case Piece:
|
|
|
|
for _, pi := range []*Integer{&msg.Index, &msg.Begin} {
|
|
|
|
err = pi.Read(r)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2016-09-12 15:10:11 +08:00
|
|
|
//msg.Piece, err = ioutil.ReadAll(r)
|
2017-11-07 13:11:59 +08:00
|
|
|
b := *d.Pool.Get().(*[]byte)
|
2016-09-12 15:10:11 +08:00
|
|
|
n, err := io.ReadFull(r, b)
|
|
|
|
if err != nil {
|
|
|
|
if err != io.ErrUnexpectedEOF || n != int(length-9) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b = b[0:n]
|
|
|
|
}
|
|
|
|
msg.Piece = b
|
2014-06-26 22:57:07 +08:00
|
|
|
case Extended:
|
2016-09-25 08:31:43 +08:00
|
|
|
msg.ExtendedID, err = readByte(r)
|
2014-06-26 22:57:07 +08:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
msg.ExtendedPayload, err = ioutil.ReadAll(r)
|
2014-08-25 20:12:16 +08:00
|
|
|
case Port:
|
|
|
|
err = binary.Read(r, binary.BigEndian, &msg.Port)
|
2013-09-30 19:51:08 +08:00
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unknown message type %#v", c)
|
|
|
|
}
|
|
|
|
return
|
2013-09-29 06:11:24 +08:00
|
|
|
}
|
|
|
|
|
2013-09-29 14:45:17 +08:00
|
|
|
type Bytes []byte
|
|
|
|
|
2013-09-30 19:51:08 +08:00
|
|
|
func (b Bytes) MarshalBinary() ([]byte, error) {
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshalBitfield(b []byte) (bf []bool) {
|
|
|
|
for _, c := range b {
|
|
|
|
for i := 7; i >= 0; i-- {
|
|
|
|
bf = append(bf, (c>>uint(i))&1 == 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalBitfield(bf []bool) (b []byte) {
|
|
|
|
b = make([]byte, (len(bf)+7)/8)
|
|
|
|
for i, have := range bf {
|
|
|
|
if !have {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
c := b[i/8]
|
|
|
|
c |= 1 << uint(7-i%8)
|
|
|
|
b[i/8] = c
|
|
|
|
}
|
|
|
|
return
|
2013-09-29 06:11:24 +08:00
|
|
|
}
|
2017-09-18 11:42:42 +08:00
|
|
|
|
|
|
|
func MakeCancelMessage(piece, offset, length Integer) Message {
|
|
|
|
return Message{
|
|
|
|
Type: Cancel,
|
|
|
|
Index: piece,
|
|
|
|
Begin: offset,
|
|
|
|
Length: length,
|
|
|
|
}
|
|
|
|
}
|