2018-02-02 18:29:57 +08:00
|
|
|
package peer_protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"sync"
|
2018-07-14 09:50:43 +08:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2018-02-02 18:29:57 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Decoder struct {
|
|
|
|
R *bufio.Reader
|
|
|
|
Pool *sync.Pool
|
|
|
|
MaxLength Integer // TODO: Should this include the length header or not?
|
|
|
|
}
|
|
|
|
|
|
|
|
// io.EOF is returned if the source terminates cleanly on a message boundary.
|
|
|
|
func (d *Decoder) Decode(msg *Message) (err error) {
|
|
|
|
var length Integer
|
2021-09-30 08:24:03 +08:00
|
|
|
err = length.Read(d.R)
|
2018-02-02 18:29:57 +08:00
|
|
|
if err != nil {
|
2021-09-30 07:59:18 +08:00
|
|
|
return fmt.Errorf("reading message length: %w", err)
|
2018-02-02 18:29:57 +08:00
|
|
|
}
|
|
|
|
if length > d.MaxLength {
|
|
|
|
return errors.New("message too long")
|
|
|
|
}
|
|
|
|
if length == 0 {
|
|
|
|
msg.Keepalive = true
|
|
|
|
return
|
|
|
|
}
|
2021-09-30 09:05:01 +08:00
|
|
|
r := d.R
|
|
|
|
readByte := func() (byte, error) {
|
|
|
|
length--
|
|
|
|
return d.R.ReadByte()
|
|
|
|
}
|
|
|
|
c, err := readByte()
|
2018-02-02 18:29:57 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
msg.Type = MessageType(c)
|
|
|
|
switch msg.Type {
|
|
|
|
case Choke, Unchoke, Interested, NotInterested, HaveAll, HaveNone:
|
2018-02-04 21:18:38 +08:00
|
|
|
case Have, AllowedFast, Suggest:
|
2021-09-30 09:05:01 +08:00
|
|
|
length -= 4
|
2018-02-02 18:29:57 +08:00
|
|
|
err = msg.Index.Read(r)
|
|
|
|
case Request, Cancel, Reject:
|
|
|
|
for _, data := range []*Integer{&msg.Index, &msg.Begin, &msg.Length} {
|
|
|
|
err = data.Read(r)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-09-30 09:05:01 +08:00
|
|
|
length -= 12
|
2018-02-02 18:29:57 +08:00
|
|
|
case Bitfield:
|
2021-09-30 09:05:01 +08:00
|
|
|
b := make([]byte, length)
|
2018-02-02 18:29:57 +08:00
|
|
|
_, err = io.ReadFull(r, b)
|
2021-09-30 09:05:01 +08:00
|
|
|
length = 0
|
2018-02-02 18:29:57 +08:00
|
|
|
msg.Bitfield = unmarshalBitfield(b)
|
|
|
|
case Piece:
|
|
|
|
for _, pi := range []*Integer{&msg.Index, &msg.Begin} {
|
2018-07-14 09:50:43 +08:00
|
|
|
err := pi.Read(r)
|
2018-02-02 18:29:57 +08:00
|
|
|
if err != nil {
|
2018-07-14 09:50:43 +08:00
|
|
|
return err
|
2018-02-02 18:29:57 +08:00
|
|
|
}
|
|
|
|
}
|
2021-09-30 09:05:01 +08:00
|
|
|
length -= 8
|
|
|
|
dataLen := int64(length)
|
2021-10-06 15:43:15 +08:00
|
|
|
msg.Piece = *d.Pool.Get().(*[]byte)
|
2018-07-14 09:50:43 +08:00
|
|
|
if int64(cap(msg.Piece)) < dataLen {
|
|
|
|
return errors.New("piece data longer than expected")
|
2018-02-02 18:29:57 +08:00
|
|
|
}
|
2018-07-14 09:50:43 +08:00
|
|
|
msg.Piece = msg.Piece[:dataLen]
|
|
|
|
_, err := io.ReadFull(r, msg.Piece)
|
2018-02-02 18:29:57 +08:00
|
|
|
if err != nil {
|
2021-09-30 09:05:01 +08:00
|
|
|
return fmt.Errorf("reading piece data: %w", err)
|
2018-02-02 18:29:57 +08:00
|
|
|
}
|
2021-09-30 09:05:01 +08:00
|
|
|
length = 0
|
2018-02-02 18:29:57 +08:00
|
|
|
case Extended:
|
2020-02-20 08:09:57 +08:00
|
|
|
var b byte
|
2021-09-30 09:05:01 +08:00
|
|
|
b, err = readByte()
|
2018-02-02 18:29:57 +08:00
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
2018-07-10 10:20:36 +08:00
|
|
|
msg.ExtendedID = ExtensionNumber(b)
|
2021-09-30 09:05:01 +08:00
|
|
|
msg.ExtendedPayload = make([]byte, length)
|
|
|
|
_, err = io.ReadFull(r, msg.ExtendedPayload)
|
|
|
|
if err == io.EOF {
|
|
|
|
err = io.ErrUnexpectedEOF
|
|
|
|
}
|
|
|
|
length = 0
|
2018-02-02 18:29:57 +08:00
|
|
|
case Port:
|
|
|
|
err = binary.Read(r, binary.BigEndian, &msg.Port)
|
2021-09-30 09:05:01 +08:00
|
|
|
length -= 2
|
2018-02-02 18:29:57 +08:00
|
|
|
default:
|
|
|
|
err = fmt.Errorf("unknown message type %#v", c)
|
|
|
|
}
|
2021-09-30 09:05:01 +08:00
|
|
|
if err == nil && length != 0 {
|
|
|
|
err = fmt.Errorf("%v unused bytes in message type %v", length, msg.Type)
|
|
|
|
}
|
2018-02-02 18:29:57 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|