root / dotorg / v4 / html / beps / bep_0015.rst

Revision 10759, 8.5 kB (checked in by dave, 9 months ago)

rstify bep 0015 from olaf. Remove references to BitTorrent?, Inc. in favor of bittorrent.org.
Update bep 0000 to refer to bep 0015.

  • Property svn:executable set to *
Line 
1BEP: 15
2Title: UDP Tracker Protocol for BitTorrent
3Version: $Revision$
4Last-Modified: $Date$
5Author:  Olaf van der Spek <olafvdspek@gmail.com>
6Status:  Draft
7Type:    Standards Track
8Created: 13-Feb-2008
9Post-History:
10
11Introduction
12============
13
14To discover other peers in a swarm a client announces it's existance
15to a tracker.  The HTTP protocol is used and a typical request
16contains the following parameters: info_hash, key, peer_id, port,
17downloaded, left, uploaded and compact.  A response contains a list of
18peers (host and port) and some other information.  The request and
19response are both quite short.  Since TCP is used, a connection has to
20be opened and closed, introducing additional overhead.
21
22Overhead
23========
24
25Using HTTP introduces significant overhead. There's overhead at the
26ethernet layer (14 bytes per packet), at the IP layer (20 bytes per
27packet), at the TCP layer (20 bytes per packet) and at the HTTP layer.
28About 10 packets are used for a request plus response containing 50
29peers and the total number of bytes used is about 1206 [1].  This
30overhead can be reduced significantly by using a UDP based
31protocol. The protocol proposed here uses 4 packets and about 618
32bytes, reducing traffic by 50%.  For a client, saving 1 kbyte every
33hour isn't significant, but for a tracker serving a million peers,
34reducing traffic by 50% matters a lot.  An additional advantage is
35that a UDP based binary protocol doesn't require a complex parser and
36no connection handling, reducing the complexity of tracker code and
37increasing it's performance.
38
39UDP connections / spoofing
40==========================
41
42In the ideal case, only 2 packets would be necessary. However, it is
43possible to spoof the source address of a UDP packet.  The tracker has
44to ensure this doesn't occur, so it calculates a value (connection_id)
45and sends it to the client.  If the client spoofed it's source
46address, it won't receive this value (unless it's sniffing the
47network).  The connection_id will then be send to the tracker again in
48packet 3. The tracker verifies the connection_id and ignores the
49request if it doesn't match.  Connection IDs should not be guessable
50by the client. This is comparable to a TCP handshake and a syn cookie
51like approach can be used to storing the connection IDs on the tracker
52side.  A connection ID can be used for multiple requests. A client can
53use a connection ID until one minute after it has received
54it. Trackers should accept the connection ID until two minutes after
55it has been send.
56
57Time outs
58=========
59
60UDP is an 'unreliable' protocol. This means it doesn't retransmit lost
61packets itself. The application is responsible for this.  If a
62response is not received after 15 * 2 ^ n seconds, the client should
63retransmit the request, where n starts at 0 and is increased up to 8
64(3840 seconds) after every retransmission.  Note that it is necessary
65to rerequest a connection ID when it has expired.
66
67Examples
68========
69
70Normal announce::
71
72  t = 0: connect request
73  t = 1: connect response
74  t = 2: announce request
75  t = 3: annonce response
76
77Connect times out::
78
79  t = 0: connect request
80  t = 15: connect request
81  t = 45: connect request
82  t = 105: connect request
83  etc
84
85Announce times out::
86
87  t = 0:
88  t = 0: connect request
89  t = 1: connect response
90  t = 2: announce request
91  t = 17: announce request
92  t = 47: announce request
93  t = 107: connect request (because connection ID expired)
94  t = 227: connect request
95  etc
96
97Multiple requests::
98
99  t = 0: connect request
100  t = 1: connect response
101  t = 2: announce request
102  t = 3: annonce response
103  t = 4: announce request
104  t = 5: annonce response
105  t = 60: announce request
106  t = 61: annonce response
107  t = 62: connect request
108  t = 63: connect response
109  t = 64: announce request
110  t = 64: scrape request
111  t = 64: scrape request
112  t = 64: announce request
113  t = 65: announce response
114  t = 66: announce response
115  t = 67: scrape response
116  t = 68: scrape response
117
118UDP tracker protocol
119====================
120
121All values are send in network byte order (big endian). Do not expect
122packets to be exactly of a certain size. Future extensions could
123increase the size of packets.
124
125Before announcing or scraping, you have to obtain a connection ID.
126
1271. Choose a random transaction ID.
1282. Fill the connect request structure.
1293. Send the packet.
130
131connect request::
132
133  Offset  Size            Name            Value
134  0       64-bit integer  connection_id   0x41727101980
135  8       32-bit integer  action          0 // connect
136  12      32-bit integer  transaction_id
137  16
138
1391. Receive the packet.
1402. Check whether the packet is at least 16 bytes.
1413. Check whether the transaction ID is equal to the one you chose.
1424. Check whether the action is connect.
1435. Store the connection ID for future use.
144
145connect response::
146
147  Offset  Size            Name            Value
148  0       32-bit integer  action          0 // connect
149  4       32-bit integer  transaction_id
150  8       64-bit integer  connection_id
151  16
152
1531. Choose a random transaction ID.
1542. Fill the announce request structure.
1553. Send the packet.
156
157announce request::
158
159  Offset  Size    Name    Value
160  0       64-bit integer  connection_id
161  8       32-bit integer  action          1 // announce
162  12      32-bit integer  transaction_id
163  16      20-byte string  info_hash
164  36      20-byte string  peer_id
165  56      64-bit integer  downloaded
166  64      64-bit integer  left
167  72      64-bit integer  uploaded
168  80      32-bit integer  event           0 // 0: none; 1: completed; 2: started; 3: stopped
169  84      32-bit integer  IP address      0 // default
170  88      32-bit integer  key
171  92      32-bit integer  num_want        -1 // default
172  96      16-bit integer  port
173  98
174
1751. Receive the packet.
1762. Check whether the packet is at least 20 bytes.
1773. Check whether the transaction ID is equal to the one you chose.
1784. Check whether the action is announce.
1795. Do not announce again until interval seconds have passed or an event has occurred.
180
181announce response::
182
183  Offset      Size            Name            Value
184  0           32-bit integer  action          1 // announce
185  4           32-bit integer  transaction_id
186  8           32-bit integer  interval
187  12          32-bit integer  leechers
188  16          32-bit integer  seeders
189  20 + 6 * n  32-bit integer  IP address
190  24 + 6 * n  16-bit integer  TCP port
191  20 + 6 * N
192
193Up to about 74 torrents can be scraped at once. A full scrape can't be done with this protocol.
194
1951. Choose a random transaction ID.
1962. Fill the scrape request structure.
1973. Send the packet.
198
199scrape request::
200
201  Offset          Size            Name            Value
202  0               64-bit integer  connection_id
203  8               32-bit integer  action          2 // scrape
204  12              32-bit integer  transaction_id
205  16 + 20 * n     20-byte string  info_hash
206  16 + 20 * N
207
2081. Receive the packet.
2092. Check whether the packet is at least 8 bytes.
2103. Check whether the transaction ID is equal to the one you chose.
2114. Check whether the action is scrape.
212
213scrape response::
214
215  Offset      Size            Name            Value
216  0           32-bit integer  action          2 // scrape
217  4           32-bit integer  transaction_id
218  8 + 12 * n  32-bit integer  seeders
219  12 + 12 * n 32-bit integer  completed
220  16 + 12 * n 32-bit integer  leechers
221  8 + 12 * N
222
223If the tracker encounters an error, it might send an error packet.
224
2251. Receive the packet.
2262. Check whether the packet is at least 8 bytes.
2273. Check whether the transaction ID is equal to the one you chose.
228
229error response::
230
231  Offset  Size            Name            Value
232  0       32-bit integer  action          3 // error
233  4       32-bit integer  transaction_id
234  8       string  message
235
236Existing implementations
237========================
238
239Azureus, libtorrent [2], opentracker [3], XBT Client and XBT Tracker
240support this protocol.
241
242IPv6
243====
244
245IPv6 is not supported at the moment. A simple way to support IPv6
246would be to increase the size of all IP addresses to 128 bits when the
247request is done over IPv6.  However, I think more experience with IPv6
248and discussion is needed before including it.
249
250Extensions
251==========
252
253Extension bits or a version field are not included. Clients and
254trackers should not assume packets to be of a certain size. This way,
255additional fields can be added without breaking compatibility.
256
257References and Footnotes
258========================
259
260.. [1] http://xbtt.sourceforge.net/udp_tracker_protocol.html
261.. [2] http://www.rasterbar.com/products/libtorrent/udp_tracker_protocol.html
262.. [3] http://opentracker.blog.h3q.com/
263
264
265..
266   Local Variables:
267   mode: indented-text
268   indent-tabs-mode: nil
269   sentence-end-double-space: t
270   fill-column: 70
271   coding: utf-8
272   End:
Note: See TracBrowser for help on using the browser.