13 "github.com/anacrolix/torrent/metainfo"
16 type ExtensionBit uint
19 ExtensionBitDHT = 0 // http://www.bittorrent.org/beps/bep_0005.html
20 ExtensionBitExtended = 20 // http://www.bittorrent.org/beps/bep_0010.html
21 ExtensionBitFast = 2 // http://www.bittorrent.org/beps/bep_0006.html
24 func handshakeWriter(w io.Writer, bb <-chan []byte, done chan<- error) {
36 PeerExtensionBits [8]byte
39 var bitTags = []struct {
43 // Ordered by their base protocol type values (PORT, fast.., EXTENDED)
44 {ExtensionBitDHT, "dht"},
45 {ExtensionBitFast, "fast"},
46 {ExtensionBitExtended, "ext"},
49 func (pex PeerExtensionBits) String() string {
50 pexHex := hex.EncodeToString(pex[:])
51 tags := make([]string, 0, len(bitTags)+1)
52 for _, bitTag := range bitTags {
53 if pex.GetBit(bitTag.bit) {
54 tags = append(tags, bitTag.tag)
55 pex.SetBit(bitTag.bit, false)
58 unknownCount := bits.OnesCount64(*(*uint64)((unsafe.Pointer(unsafe.SliceData(pex[:])))))
59 if unknownCount != 0 {
60 tags = append(tags, fmt.Sprintf("%v unknown", unknownCount))
62 return fmt.Sprintf("%v (%s)", pexHex, strings.Join(tags, ", "))
66 func NewPeerExtensionBytes(bits ...ExtensionBit) (ret PeerExtensionBits) {
67 for _, b := range bits {
73 func (pex PeerExtensionBits) SupportsExtended() bool {
74 return pex.GetBit(ExtensionBitExtended)
77 func (pex PeerExtensionBits) SupportsDHT() bool {
78 return pex.GetBit(ExtensionBitDHT)
81 func (pex PeerExtensionBits) SupportsFast() bool {
82 return pex.GetBit(ExtensionBitFast)
85 func (pex *PeerExtensionBits) SetBit(bit ExtensionBit, on bool) {
87 pex[7-bit/8] |= 1 << (bit % 8)
89 pex[7-bit/8] &^= 1 << (bit % 8)
93 func (pex PeerExtensionBits) GetBit(bit ExtensionBit) bool {
94 return pex[7-bit/8]&(1<<(bit%8)) != 0
97 type HandshakeResult struct {
103 // ih is nil if we expect the peer to declare the InfoHash, such as when the peer initiated the
104 // connection. Returns ok if the Handshake was successful, and err if there was an unexpected
105 // condition other than the peer simply abandoning the Handshake.
107 sock io.ReadWriter, ih *metainfo.Hash, peerID [20]byte, extensions PeerExtensionBits,
109 res HandshakeResult, err error,
111 // Bytes to be sent to the peer. Should never block the sender.
112 postCh := make(chan []byte, 4)
113 // A single error value sent when the writer completes.
114 writeDone := make(chan error, 1)
115 // Performs writes to the socket and ensures posts don't block.
116 go handshakeWriter(sock, postCh, writeDone)
119 close(postCh) // Done writing.
123 // Wait until writes complete before returning from handshake.
126 err = fmt.Errorf("error writing: %w", err)
130 post := func(bb []byte) {
134 panic("mustn't block while posting")
138 post([]byte(Protocol))
140 if ih != nil { // We already know what we want.
145 _, err = io.ReadFull(sock, b[:68])
147 return res, fmt.Errorf("while reading: %w", err)
149 if string(b[:20]) != Protocol {
150 return res, errors.New("unexpected protocol string")
153 copyExact := func(dst, src []byte) {
154 if dstLen, srcLen := uint64(len(dst)), uint64(len(src)); dstLen != srcLen {
155 panic("dst len " + strconv.FormatUint(dstLen, 10) + " != src len " + strconv.FormatUint(srcLen, 10))
159 copyExact(res.PeerExtensionBits[:], b[20:28])
160 copyExact(res.Hash[:], b[28:48])
161 copyExact(res.PeerID[:], b[48:68])
162 // peerExtensions.Add(res.PeerExtensionBits.String(), 1)
164 // TODO: Maybe we can just drop peers here if we're not interested. This
165 // could prevent them trying to reconnect, falsely believing there was
167 if ih == nil { // We were waiting for the peer to tell us what they wanted.