13 "github.com/anacrolix/torrent/metainfo"
16 type ExtensionBit uint
18 // https://www.bittorrent.org/beps/bep_0004.html
19 // https://wiki.theory.org/BitTorrentSpecification.html#Reserved_Bytes
21 ExtensionBitDht = 0 // http://www.bittorrent.org/beps/bep_0005.html
22 ExtensionBitFast = 2 // http://www.bittorrent.org/beps/bep_0006.html
23 // A peer connection initiator can set this when sending a v1 infohash during handshake if they
24 // allow the receiving end to upgrade to v2 by responding with the corresponding v2 infohash.
26 ExtensionBitV2Upgrade = 4
27 ExtensionBitAzureusExtensionNegotiation1 = 16
28 ExtensionBitAzureusExtensionNegotiation2 = 17
29 // LibTorrent Extension Protocol, http://www.bittorrent.org/beps/bep_0010.html
31 // https://wiki.theory.org/BitTorrent_Location-aware_Protocol_1
32 ExtensionBitLocationAwareProtocol = 43
33 ExtensionBitAzureusMessagingProtocol = 63 // https://www.bittorrent.org/beps/bep_0004.html
37 func handshakeWriter(w io.Writer, bb <-chan []byte, done chan<- error) {
49 PeerExtensionBits [8]byte
52 var bitTags = []struct {
56 // Ordered by their bit position left to right.
57 {ExtensionBitAzureusMessagingProtocol, "amp"},
58 {ExtensionBitLocationAwareProtocol, "loc"},
59 {ExtensionBitLtep, "ltep"},
60 {ExtensionBitAzureusExtensionNegotiation2, "azen2"},
61 {ExtensionBitAzureusExtensionNegotiation1, "azen1"},
62 {ExtensionBitV2Upgrade, "v2"},
63 {ExtensionBitFast, "fast"},
64 {ExtensionBitDht, "dht"},
67 func (pex PeerExtensionBits) String() string {
68 pexHex := hex.EncodeToString(pex[:])
69 tags := make([]string, 0, len(bitTags)+1)
70 for _, bitTag := range bitTags {
71 if pex.GetBit(bitTag.bit) {
72 tags = append(tags, bitTag.tag)
73 pex.SetBit(bitTag.bit, false)
76 unknownCount := bits.OnesCount64(*(*uint64)((unsafe.Pointer(&pex[0]))))
77 if unknownCount != 0 {
78 tags = append(tags, fmt.Sprintf("%v unknown", unknownCount))
80 return fmt.Sprintf("%v (%s)", pexHex, strings.Join(tags, ", "))
84 func NewPeerExtensionBytes(bits ...ExtensionBit) (ret PeerExtensionBits) {
85 for _, b := range bits {
91 func (pex PeerExtensionBits) SupportsExtended() bool {
92 return pex.GetBit(ExtensionBitLtep)
95 func (pex PeerExtensionBits) SupportsDHT() bool {
96 return pex.GetBit(ExtensionBitDht)
99 func (pex PeerExtensionBits) SupportsFast() bool {
100 return pex.GetBit(ExtensionBitFast)
103 func (pex *PeerExtensionBits) SetBit(bit ExtensionBit, on bool) {
105 pex[7-bit/8] |= 1 << (bit % 8)
107 pex[7-bit/8] &^= 1 << (bit % 8)
111 func (pex PeerExtensionBits) GetBit(bit ExtensionBit) bool {
112 return pex[7-bit/8]&(1<<(bit%8)) != 0
115 type HandshakeResult struct {
121 // ih is nil if we expect the peer to declare the InfoHash, such as when the peer initiated the
122 // connection. Returns ok if the Handshake was successful, and err if there was an unexpected
123 // condition other than the peer simply abandoning the Handshake.
125 sock io.ReadWriter, ih *metainfo.Hash, peerID [20]byte, extensions PeerExtensionBits,
127 res HandshakeResult, err error,
129 // Bytes to be sent to the peer. Should never block the sender.
130 postCh := make(chan []byte, 4)
131 // A single error value sent when the writer completes.
132 writeDone := make(chan error, 1)
133 // Performs writes to the socket and ensures posts don't block.
134 go handshakeWriter(sock, postCh, writeDone)
137 close(postCh) // Done writing.
141 // Wait until writes complete before returning from handshake.
144 err = fmt.Errorf("error writing: %w", err)
148 post := func(bb []byte) {
152 panic("mustn't block while posting")
156 post([]byte(Protocol))
158 if ih != nil { // We already know what we want.
163 _, err = io.ReadFull(sock, b[:68])
165 return res, fmt.Errorf("while reading: %w", err)
167 if string(b[:20]) != Protocol {
168 return res, errors.New("unexpected protocol string")
171 copyExact := func(dst, src []byte) {
172 if dstLen, srcLen := uint64(len(dst)), uint64(len(src)); dstLen != srcLen {
173 panic("dst len " + strconv.FormatUint(dstLen, 10) + " != src len " + strconv.FormatUint(srcLen, 10))
177 copyExact(res.PeerExtensionBits[:], b[20:28])
178 copyExact(res.Hash[:], b[28:48])
179 copyExact(res.PeerID[:], b[48:68])
180 // peerExtensions.Add(res.PeerExtensionBits.String(), 1)
182 // TODO: Maybe we can just drop peers here if we're not interested. This
183 // could prevent them trying to reconnect, falsely believing there was
185 if ih == nil { // We were waiting for the peer to tell us what they wanted.