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
41 func (me peerExtensionBytes) String() string {
42 return hex.EncodeToString(me[:])
45 func newPeerExtensionBytes(bits ...ExtensionBit) (ret peerExtensionBytes) {
46 for _, b := range bits {
52 func (pex peerExtensionBytes) SupportsExtended() bool {
53 return pex.GetBit(ExtensionBitExtended)
56 func (pex peerExtensionBytes) SupportsDHT() bool {
57 return pex.GetBit(ExtensionBitDHT)
60 func (pex peerExtensionBytes) SupportsFast() bool {
61 return pex.GetBit(ExtensionBitFast)
64 func (pex *peerExtensionBytes) SetBit(bit ExtensionBit) {
65 pex[7-bit/8] |= 1 << (bit % 8)
68 func (pex peerExtensionBytes) GetBit(bit ExtensionBit) bool {
69 return pex[7-bit/8]&(1<<(bit%8)) != 0
72 type handshakeResult struct {
78 // ih is nil if we expect the peer to declare the InfoHash, such as when the
79 // peer initiated the connection. Returns ok if the handshake was successful,
80 // and err if there was an unexpected condition other than the peer simply
81 // abandoning the handshake.
82 func handshake(sock io.ReadWriter, ih *metainfo.Hash, peerID [20]byte, extensions peerExtensionBytes) (res handshakeResult, ok bool, err error) {
83 // Bytes to be sent to the peer. Should never block the sender.
84 postCh := make(chan []byte, 4)
85 // A single error value sent when the writer completes.
86 writeDone := make(chan error, 1)
87 // Performs writes to the socket and ensures posts don't block.
88 go handshakeWriter(sock, postCh, writeDone)
91 close(postCh) // Done writing.
98 // Wait until writes complete before returning from handshake.
101 err = fmt.Errorf("error writing: %s", err)
105 post := func(bb []byte) {
109 panic("mustn't block while posting")
113 post([]byte(pp.Protocol))
115 if ih != nil { // We already know what we want.
120 _, err = io.ReadFull(sock, b[:68])
125 if string(b[:20]) != pp.Protocol {
128 missinggo.CopyExact(&res.peerExtensionBytes, b[20:28])
129 missinggo.CopyExact(&res.Hash, b[28:48])
130 missinggo.CopyExact(&res.PeerID, b[48:68])
131 peerExtensions.Add(res.peerExtensionBytes.String(), 1)
133 // TODO: Maybe we can just drop peers here if we're not interested. This
134 // could prevent them trying to reconnect, falsely believing there was
136 if ih == nil { // We were waiting for the peer to tell us what they wanted.
145 // Wraps a raw connection and provides the interface we want for using the
146 // connection in the message loop.
147 type deadlineReader struct {
152 func (r deadlineReader) Read(b []byte) (int, error) {
153 // Keep-alives should be received every 2 mins. Give a bit of gracetime.
154 err := r.nc.SetReadDeadline(time.Now().Add(150 * time.Second))
156 return 0, fmt.Errorf("error setting read deadline: %s", err)
161 func handleEncryption(
163 skeys mse.SecretKeyIter,
164 policy EncryptionPolicy,
167 headerEncrypted bool,
168 cryptoMethod mse.CryptoMethod,
171 if !policy.ForceEncryption {
172 var protocol [len(pp.Protocol)]byte
173 _, err = io.ReadFull(rw, protocol[:])
181 io.MultiReader(bytes.NewReader(protocol[:]), rw),
184 if string(protocol[:]) == pp.Protocol {
189 headerEncrypted = true
190 ret, cryptoMethod, err = mse.ReceiveHandshake(rw, skeys, func(provides mse.CryptoMethod) mse.CryptoMethod {
192 case policy.ForceEncryption:
193 return mse.CryptoMethodRC4
194 case policy.DisableEncryption:
195 return mse.CryptoMethodPlaintext
196 case policy.PreferNoEncryption && provides&mse.CryptoMethodPlaintext != 0:
197 return mse.CryptoMethodPlaintext
199 return mse.DefaultCryptoSelector(provides)