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