2018-07-07 09:31:29 +08:00
|
|
|
package peer_protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
2021-09-04 14:30:14 +08:00
|
|
|
"errors"
|
2018-07-07 09:31:29 +08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-09-04 14:30:14 +08:00
|
|
|
"strconv"
|
2018-07-07 09:31:29 +08:00
|
|
|
|
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ExtensionBit uint
|
|
|
|
|
|
|
|
const (
|
|
|
|
ExtensionBitDHT = 0 // http://www.bittorrent.org/beps/bep_0005.html
|
|
|
|
ExtensionBitExtended = 20 // http://www.bittorrent.org/beps/bep_0010.html
|
|
|
|
ExtensionBitFast = 2 // http://www.bittorrent.org/beps/bep_0006.html
|
|
|
|
)
|
|
|
|
|
|
|
|
func handshakeWriter(w io.Writer, bb <-chan []byte, done chan<- error) {
|
|
|
|
var err error
|
|
|
|
for b := range bb {
|
|
|
|
_, err = w.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done <- err
|
|
|
|
}
|
|
|
|
|
|
|
|
type (
|
|
|
|
PeerExtensionBits [8]byte
|
|
|
|
)
|
|
|
|
|
2021-09-04 14:30:14 +08:00
|
|
|
func (pex PeerExtensionBits) String() string {
|
|
|
|
return hex.EncodeToString(pex[:])
|
2018-07-07 09:31:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewPeerExtensionBytes(bits ...ExtensionBit) (ret PeerExtensionBits) {
|
|
|
|
for _, b := range bits {
|
2020-04-23 11:03:35 +08:00
|
|
|
ret.SetBit(b, true)
|
2018-07-07 09:31:29 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pex PeerExtensionBits) SupportsExtended() bool {
|
|
|
|
return pex.GetBit(ExtensionBitExtended)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pex PeerExtensionBits) SupportsDHT() bool {
|
|
|
|
return pex.GetBit(ExtensionBitDHT)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pex PeerExtensionBits) SupportsFast() bool {
|
|
|
|
return pex.GetBit(ExtensionBitFast)
|
|
|
|
}
|
|
|
|
|
2020-04-23 11:03:35 +08:00
|
|
|
func (pex *PeerExtensionBits) SetBit(bit ExtensionBit, on bool) {
|
|
|
|
if on {
|
|
|
|
pex[7-bit/8] |= 1 << (bit % 8)
|
|
|
|
} else {
|
|
|
|
pex[7-bit/8] &^= 1 << (bit % 8)
|
|
|
|
}
|
2018-07-07 09:31:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pex PeerExtensionBits) GetBit(bit ExtensionBit) bool {
|
|
|
|
return pex[7-bit/8]&(1<<(bit%8)) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
type HandshakeResult struct {
|
|
|
|
PeerExtensionBits
|
|
|
|
PeerID [20]byte
|
|
|
|
metainfo.Hash
|
|
|
|
}
|
|
|
|
|
2019-07-19 12:56:29 +08:00
|
|
|
// ih is nil if we expect the peer to declare the InfoHash, such as when the peer initiated the
|
|
|
|
// connection. Returns ok if the Handshake was successful, and err if there was an unexpected
|
|
|
|
// condition other than the peer simply abandoning the Handshake.
|
|
|
|
func Handshake(
|
|
|
|
sock io.ReadWriter, ih *metainfo.Hash, peerID [20]byte, extensions PeerExtensionBits,
|
|
|
|
) (
|
2019-07-19 14:15:46 +08:00
|
|
|
res HandshakeResult, err error,
|
2019-07-19 12:56:29 +08:00
|
|
|
) {
|
2018-07-07 09:31:29 +08:00
|
|
|
// Bytes to be sent to the peer. Should never block the sender.
|
|
|
|
postCh := make(chan []byte, 4)
|
|
|
|
// A single error value sent when the writer completes.
|
|
|
|
writeDone := make(chan error, 1)
|
|
|
|
// Performs writes to the socket and ensures posts don't block.
|
|
|
|
go handshakeWriter(sock, postCh, writeDone)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
close(postCh) // Done writing.
|
|
|
|
if err != nil {
|
2019-07-19 14:15:46 +08:00
|
|
|
return
|
2018-07-07 09:31:29 +08:00
|
|
|
}
|
|
|
|
// Wait until writes complete before returning from handshake.
|
|
|
|
err = <-writeDone
|
|
|
|
if err != nil {
|
2021-09-04 14:30:14 +08:00
|
|
|
err = fmt.Errorf("error writing: %w", err)
|
2018-07-07 09:31:29 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
post := func(bb []byte) {
|
|
|
|
select {
|
|
|
|
case postCh <- bb:
|
|
|
|
default:
|
|
|
|
panic("mustn't block while posting")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
post([]byte(Protocol))
|
|
|
|
post(extensions[:])
|
|
|
|
if ih != nil { // We already know what we want.
|
|
|
|
post(ih[:])
|
|
|
|
post(peerID[:])
|
|
|
|
}
|
|
|
|
var b [68]byte
|
|
|
|
_, err = io.ReadFull(sock, b[:68])
|
|
|
|
if err != nil {
|
2021-09-04 14:30:14 +08:00
|
|
|
return res, fmt.Errorf("while reading: %w", err)
|
2018-07-07 09:31:29 +08:00
|
|
|
}
|
|
|
|
if string(b[:20]) != Protocol {
|
2021-09-04 14:30:14 +08:00
|
|
|
return res, errors.New("unexpected protocol string")
|
|
|
|
}
|
|
|
|
|
|
|
|
copyExact := func(dst []byte, src []byte) {
|
|
|
|
if dstLen, srcLen := uint64(len(dst)), uint64(len(src)); dstLen != srcLen {
|
2021-09-09 20:51:24 +08:00
|
|
|
panic("dst len " + strconv.FormatUint(dstLen, 10) + " != src len " + strconv.FormatUint(srcLen, 10))
|
2021-09-04 14:30:14 +08:00
|
|
|
}
|
|
|
|
copy(dst, src)
|
2018-07-07 09:31:29 +08:00
|
|
|
}
|
2021-09-04 14:30:14 +08:00
|
|
|
copyExact(res.PeerExtensionBits[:], b[20:28])
|
|
|
|
copyExact(res.Hash[:], b[28:48])
|
|
|
|
copyExact(res.PeerID[:], b[48:68])
|
2018-11-02 20:12:01 +08:00
|
|
|
// peerExtensions.Add(res.PeerExtensionBits.String(), 1)
|
2018-07-07 09:31:29 +08:00
|
|
|
|
|
|
|
// TODO: Maybe we can just drop peers here if we're not interested. This
|
|
|
|
// could prevent them trying to reconnect, falsely believing there was
|
|
|
|
// just a problem.
|
|
|
|
if ih == nil { // We were waiting for the peer to tell us what they wanted.
|
|
|
|
post(res.Hash[:])
|
|
|
|
post(peerID[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|