]> Sergey Matveev's repositories - btrtrc.git/blob - peer_protocol/handshake.go
Tidy up function signature
[btrtrc.git] / peer_protocol / handshake.go
1 package peer_protocol
2
3 import (
4         "encoding/hex"
5         "fmt"
6         "io"
7
8         "github.com/anacrolix/missinggo"
9         "github.com/anacrolix/torrent/metainfo"
10 )
11
12 type ExtensionBit uint
13
14 const (
15         ExtensionBitDHT      = 0  // http://www.bittorrent.org/beps/bep_0005.html
16         ExtensionBitExtended = 20 // http://www.bittorrent.org/beps/bep_0010.html
17         ExtensionBitFast     = 2  // http://www.bittorrent.org/beps/bep_0006.html
18 )
19
20 func handshakeWriter(w io.Writer, bb <-chan []byte, done chan<- error) {
21         var err error
22         for b := range bb {
23                 _, err = w.Write(b)
24                 if err != nil {
25                         break
26                 }
27         }
28         done <- err
29 }
30
31 type (
32         PeerExtensionBits [8]byte
33 )
34
35 func (me PeerExtensionBits) String() string {
36         return hex.EncodeToString(me[:])
37 }
38
39 func NewPeerExtensionBytes(bits ...ExtensionBit) (ret PeerExtensionBits) {
40         for _, b := range bits {
41                 ret.SetBit(b)
42         }
43         return
44 }
45
46 func (pex PeerExtensionBits) SupportsExtended() bool {
47         return pex.GetBit(ExtensionBitExtended)
48 }
49
50 func (pex PeerExtensionBits) SupportsDHT() bool {
51         return pex.GetBit(ExtensionBitDHT)
52 }
53
54 func (pex PeerExtensionBits) SupportsFast() bool {
55         return pex.GetBit(ExtensionBitFast)
56 }
57
58 func (pex *PeerExtensionBits) SetBit(bit ExtensionBit) {
59         pex[7-bit/8] |= 1 << (bit % 8)
60 }
61
62 func (pex PeerExtensionBits) GetBit(bit ExtensionBit) bool {
63         return pex[7-bit/8]&(1<<(bit%8)) != 0
64 }
65
66 type HandshakeResult struct {
67         PeerExtensionBits
68         PeerID [20]byte
69         metainfo.Hash
70 }
71
72 // ih is nil if we expect the peer to declare the InfoHash, such as when the peer initiated the
73 // connection. Returns ok if the Handshake was successful, and err if there was an unexpected
74 // condition other than the peer simply abandoning the Handshake.
75 func Handshake(
76         sock io.ReadWriter, ih *metainfo.Hash, peerID [20]byte, extensions PeerExtensionBits,
77 ) (
78         res HandshakeResult, ok bool, err error,
79 ) {
80         // Bytes to be sent to the peer. Should never block the sender.
81         postCh := make(chan []byte, 4)
82         // A single error value sent when the writer completes.
83         writeDone := make(chan error, 1)
84         // Performs writes to the socket and ensures posts don't block.
85         go handshakeWriter(sock, postCh, writeDone)
86
87         defer func() {
88                 close(postCh) // Done writing.
89                 if !ok {
90                         return
91                 }
92                 if err != nil {
93                         panic(err)
94                 }
95                 // Wait until writes complete before returning from handshake.
96                 err = <-writeDone
97                 if err != nil {
98                         err = fmt.Errorf("error writing: %s", err)
99                 }
100         }()
101
102         post := func(bb []byte) {
103                 select {
104                 case postCh <- bb:
105                 default:
106                         panic("mustn't block while posting")
107                 }
108         }
109
110         post([]byte(Protocol))
111         post(extensions[:])
112         if ih != nil { // We already know what we want.
113                 post(ih[:])
114                 post(peerID[:])
115         }
116         var b [68]byte
117         _, err = io.ReadFull(sock, b[:68])
118         if err != nil {
119                 err = nil
120                 return
121         }
122         if string(b[:20]) != Protocol {
123                 return
124         }
125         missinggo.CopyExact(&res.PeerExtensionBits, b[20:28])
126         missinggo.CopyExact(&res.Hash, b[28:48])
127         missinggo.CopyExact(&res.PeerID, b[48:68])
128         // peerExtensions.Add(res.PeerExtensionBits.String(), 1)
129
130         // TODO: Maybe we can just drop peers here if we're not interested. This
131         // could prevent them trying to reconnect, falsely believing there was
132         // just a problem.
133         if ih == nil { // We were waiting for the peer to tell us what they wanted.
134                 post(res.Hash[:])
135                 post(peerID[:])
136         }
137
138         ok = true
139         return
140 }