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 (pex peerExtensionBytes) SupportsExtended() bool {
42 return pex.GetBit(ExtensionBitExtended)
45 func (pex peerExtensionBytes) SupportsDHT() bool {
46 return pex.GetBit(ExtensionBitDHT)
49 func (pex peerExtensionBytes) SupportsFast() bool {
50 return pex.GetBit(ExtensionBitFast)
53 func (pex *peerExtensionBytes) SetBit(bit ExtensionBit) {
54 pex[7-bit/8] |= 1 << bit % 8
57 func (pex peerExtensionBytes) GetBit(bit ExtensionBit) bool {
58 return pex[7-bit/8]&(1<<(bit%8)) != 0
61 type handshakeResult struct {
67 // ih is nil if we expect the peer to declare the InfoHash, such as when the
68 // peer initiated the connection. Returns ok if the handshake was successful,
69 // and err if there was an unexpected condition other than the peer simply
70 // abandoning the handshake.
71 func handshake(sock io.ReadWriter, ih *metainfo.Hash, peerID [20]byte, extensions peerExtensionBytes) (res handshakeResult, ok bool, err error) {
72 // Bytes to be sent to the peer. Should never block the sender.
73 postCh := make(chan []byte, 4)
74 // A single error value sent when the writer completes.
75 writeDone := make(chan error, 1)
76 // Performs writes to the socket and ensures posts don't block.
77 go handshakeWriter(sock, postCh, writeDone)
80 close(postCh) // Done writing.
87 // Wait until writes complete before returning from handshake.
90 err = fmt.Errorf("error writing: %s", err)
94 post := func(bb []byte) {
98 panic("mustn't block while posting")
102 post([]byte(pp.Protocol))
104 if ih != nil { // We already know what we want.
109 _, err = io.ReadFull(sock, b[:68])
114 if string(b[:20]) != pp.Protocol {
117 missinggo.CopyExact(&res.peerExtensionBytes, b[20:28])
118 missinggo.CopyExact(&res.Hash, b[28:48])
119 missinggo.CopyExact(&res.PeerID, b[48:68])
120 peerExtensions.Add(hex.EncodeToString(res.peerExtensionBytes[:]), 1)
122 // TODO: Maybe we can just drop peers here if we're not interested. This
123 // could prevent them trying to reconnect, falsely believing there was
125 if ih == nil { // We were waiting for the peer to tell us what they wanted.
134 // Wraps a raw connection and provides the interface we want for using the
135 // connection in the message loop.
136 type deadlineReader struct {
141 func (r deadlineReader) Read(b []byte) (int, error) {
142 // Keep-alives should be received every 2 mins. Give a bit of gracetime.
143 err := r.nc.SetReadDeadline(time.Now().Add(150 * time.Second))
145 return 0, fmt.Errorf("error setting read deadline: %s", err)
150 func handleEncryption(
152 skeys mse.SecretKeyIter,
153 policy EncryptionPolicy,
156 headerEncrypted bool,
160 if !policy.ForceEncryption {
161 var protocol [len(pp.Protocol)]byte
162 _, err = io.ReadFull(rw, protocol[:])
170 io.MultiReader(bytes.NewReader(protocol[:]), rw),
173 if string(protocol[:]) == pp.Protocol {
178 headerEncrypted = true
179 ret, err = mse.ReceiveHandshake(rw, skeys, func(provides uint32) uint32 {
180 cryptoMethod = func() uint32 {
182 case policy.ForceEncryption:
183 return mse.CryptoMethodRC4
184 case policy.DisableEncryption:
185 return mse.CryptoMethodPlaintext
186 case policy.PreferNoEncryption && provides&mse.CryptoMethodPlaintext != 0:
187 return mse.CryptoMethodPlaintext
189 return mse.DefaultCryptoSelector(provides)