11 "github.com/anacrolix/missinggo"
13 "github.com/anacrolix/torrent/metainfo"
14 "github.com/anacrolix/torrent/mse"
15 pp "github.com/anacrolix/torrent/peer_protocol"
18 type ExtensionBit uint
21 ExtensionBitDHT = 0 // http://www.bittorrent.org/beps/bep_0005.html
22 ExtensionBitExtended = 20 // http://www.bittorrent.org/beps/bep_0010.html
23 ExtensionBitFast = 2 // http://www.bittorrent.org/beps/bep_0006.html
26 func handshakeWriter(w io.Writer, bb <-chan []byte, done chan<- error) {
38 peerExtensionBytes [8]byte
42 func (pex peerExtensionBytes) SupportsExtended() bool {
43 return pex.GetBit(ExtensionBitExtended)
46 func (pex peerExtensionBytes) SupportsDHT() bool {
47 return pex.GetBit(ExtensionBitDHT)
50 func (pex peerExtensionBytes) SupportsFast() bool {
51 return pex.GetBit(ExtensionBitFast)
54 func (pex *peerExtensionBytes) SetBit(bit ExtensionBit) {
55 pex[7-bit/8] |= 1 << bit % 8
58 func (pex peerExtensionBytes) GetBit(bit ExtensionBit) bool {
59 return pex[7-bit/8]&(1<<(bit%8)) != 0
62 type handshakeResult struct {
68 // ih is nil if we expect the peer to declare the InfoHash, such as when the
69 // peer initiated the connection. Returns ok if the handshake was successful,
70 // and err if there was an unexpected condition other than the peer simply
71 // abandoning the handshake.
72 func handshake(sock io.ReadWriter, ih *metainfo.Hash, peerID [20]byte, extensions peerExtensionBytes) (res handshakeResult, ok bool, err error) {
73 // Bytes to be sent to the peer. Should never block the sender.
74 postCh := make(chan []byte, 4)
75 // A single error value sent when the writer completes.
76 writeDone := make(chan error, 1)
77 // Performs writes to the socket and ensures posts don't block.
78 go handshakeWriter(sock, postCh, writeDone)
81 close(postCh) // Done writing.
88 // Wait until writes complete before returning from handshake.
91 err = fmt.Errorf("error writing: %s", err)
95 post := func(bb []byte) {
99 panic("mustn't block while posting")
103 post([]byte(pp.Protocol))
105 if ih != nil { // We already know what we want.
110 _, err = io.ReadFull(sock, b[:68])
115 if string(b[:20]) != pp.Protocol {
118 missinggo.CopyExact(&res.peerExtensionBytes, b[20:28])
119 missinggo.CopyExact(&res.Hash, b[28:48])
120 missinggo.CopyExact(&res.peerID, b[48:68])
121 peerExtensions.Add(hex.EncodeToString(res.peerExtensionBytes[:]), 1)
123 // TODO: Maybe we can just drop peers here if we're not interested. This
124 // could prevent them trying to reconnect, falsely believing there was
126 if ih == nil { // We were waiting for the peer to tell us what they wanted.
135 // Wraps a raw connection and provides the interface we want for using the
136 // connection in the message loop.
137 type deadlineReader struct {
142 func (r deadlineReader) Read(b []byte) (int, error) {
143 // Keep-alives should be received every 2 mins. Give a bit of gracetime.
144 err := r.nc.SetReadDeadline(time.Now().Add(150 * time.Second))
146 return 0, fmt.Errorf("error setting read deadline: %s", err)
151 func handleEncryption(
153 skeys mse.SecretKeyIter,
154 policy EncryptionPolicy,
157 headerEncrypted bool,
161 if !policy.ForceEncryption {
162 var protocol [len(pp.Protocol)]byte
163 _, err = io.ReadFull(rw, protocol[:])
171 io.MultiReader(bytes.NewReader(protocol[:]), rw),
174 if string(protocol[:]) == pp.Protocol {
179 headerEncrypted = true
180 ret, err = mse.ReceiveHandshake(rw, skeys, func(provides uint32) uint32 {
181 cryptoMethod = func() uint32 {
183 case policy.ForceEncryption:
184 return mse.CryptoMethodRC4
185 case policy.DisableEncryption:
186 return mse.CryptoMethodPlaintext
187 case policy.PreferNoEncryption && provides&mse.CryptoMethodPlaintext != 0:
188 return mse.CryptoMethodPlaintext
190 return mse.DefaultCryptoSelector(provides)