| 1 | BEP: 15 |
|---|
| 2 | Title: UDP Tracker Protocol for BitTorrent |
|---|
| 3 | Version: $Revision$ |
|---|
| 4 | Last-Modified: $Date$ |
|---|
| 5 | Author: Olaf van der Spek <olafvdspek@gmail.com> |
|---|
| 6 | Status: Draft |
|---|
| 7 | Type: Standards Track |
|---|
| 8 | Created: 13-Feb-2008 |
|---|
| 9 | Post-History: |
|---|
| 10 | |
|---|
| 11 | Introduction |
|---|
| 12 | ============ |
|---|
| 13 | |
|---|
| 14 | To discover other peers in a swarm a client announces it's existance |
|---|
| 15 | to a tracker. The HTTP protocol is used and a typical request |
|---|
| 16 | contains the following parameters: info_hash, key, peer_id, port, |
|---|
| 17 | downloaded, left, uploaded and compact. A response contains a list of |
|---|
| 18 | peers (host and port) and some other information. The request and |
|---|
| 19 | response are both quite short. Since TCP is used, a connection has to |
|---|
| 20 | be opened and closed, introducing additional overhead. |
|---|
| 21 | |
|---|
| 22 | Overhead |
|---|
| 23 | ======== |
|---|
| 24 | |
|---|
| 25 | Using HTTP introduces significant overhead. There's overhead at the |
|---|
| 26 | ethernet layer (14 bytes per packet), at the IP layer (20 bytes per |
|---|
| 27 | packet), at the TCP layer (20 bytes per packet) and at the HTTP layer. |
|---|
| 28 | About 10 packets are used for a request plus response containing 50 |
|---|
| 29 | peers and the total number of bytes used is about 1206 [1]. This |
|---|
| 30 | overhead can be reduced significantly by using a UDP based |
|---|
| 31 | protocol. The protocol proposed here uses 4 packets and about 618 |
|---|
| 32 | bytes, reducing traffic by 50%. For a client, saving 1 kbyte every |
|---|
| 33 | hour isn't significant, but for a tracker serving a million peers, |
|---|
| 34 | reducing traffic by 50% matters a lot. An additional advantage is |
|---|
| 35 | that a UDP based binary protocol doesn't require a complex parser and |
|---|
| 36 | no connection handling, reducing the complexity of tracker code and |
|---|
| 37 | increasing it's performance. |
|---|
| 38 | |
|---|
| 39 | UDP connections / spoofing |
|---|
| 40 | ========================== |
|---|
| 41 | |
|---|
| 42 | In the ideal case, only 2 packets would be necessary. However, it is |
|---|
| 43 | possible to spoof the source address of a UDP packet. The tracker has |
|---|
| 44 | to ensure this doesn't occur, so it calculates a value (connection_id) |
|---|
| 45 | and sends it to the client. If the client spoofed it's source |
|---|
| 46 | address, it won't receive this value (unless it's sniffing the |
|---|
| 47 | network). The connection_id will then be send to the tracker again in |
|---|
| 48 | packet 3. The tracker verifies the connection_id and ignores the |
|---|
| 49 | request if it doesn't match. Connection IDs should not be guessable |
|---|
| 50 | by the client. This is comparable to a TCP handshake and a syn cookie |
|---|
| 51 | like approach can be used to storing the connection IDs on the tracker |
|---|
| 52 | side. A connection ID can be used for multiple requests. A client can |
|---|
| 53 | use a connection ID until one minute after it has received |
|---|
| 54 | it. Trackers should accept the connection ID until two minutes after |
|---|
| 55 | it has been send. |
|---|
| 56 | |
|---|
| 57 | Time outs |
|---|
| 58 | ========= |
|---|
| 59 | |
|---|
| 60 | UDP is an 'unreliable' protocol. This means it doesn't retransmit lost |
|---|
| 61 | packets itself. The application is responsible for this. If a |
|---|
| 62 | response is not received after 15 * 2 ^ n seconds, the client should |
|---|
| 63 | retransmit the request, where n starts at 0 and is increased up to 8 |
|---|
| 64 | (3840 seconds) after every retransmission. Note that it is necessary |
|---|
| 65 | to rerequest a connection ID when it has expired. |
|---|
| 66 | |
|---|
| 67 | Examples |
|---|
| 68 | ======== |
|---|
| 69 | |
|---|
| 70 | Normal announce:: |
|---|
| 71 | |
|---|
| 72 | t = 0: connect request |
|---|
| 73 | t = 1: connect response |
|---|
| 74 | t = 2: announce request |
|---|
| 75 | t = 3: annonce response |
|---|
| 76 | |
|---|
| 77 | Connect 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 | |
|---|
| 85 | Announce 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 | |
|---|
| 97 | Multiple 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 | |
|---|
| 118 | UDP tracker protocol |
|---|
| 119 | ==================== |
|---|
| 120 | |
|---|
| 121 | All values are send in network byte order (big endian). Do not expect |
|---|
| 122 | packets to be exactly of a certain size. Future extensions could |
|---|
| 123 | increase the size of packets. |
|---|
| 124 | |
|---|
| 125 | Before announcing or scraping, you have to obtain a connection ID. |
|---|
| 126 | |
|---|
| 127 | 1. Choose a random transaction ID. |
|---|
| 128 | 2. Fill the connect request structure. |
|---|
| 129 | 3. Send the packet. |
|---|
| 130 | |
|---|
| 131 | connect 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 | |
|---|
| 139 | 1. Receive the packet. |
|---|
| 140 | 2. Check whether the packet is at least 16 bytes. |
|---|
| 141 | 3. Check whether the transaction ID is equal to the one you chose. |
|---|
| 142 | 4. Check whether the action is connect. |
|---|
| 143 | 5. Store the connection ID for future use. |
|---|
| 144 | |
|---|
| 145 | connect 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 | |
|---|
| 153 | 1. Choose a random transaction ID. |
|---|
| 154 | 2. Fill the announce request structure. |
|---|
| 155 | 3. Send the packet. |
|---|
| 156 | |
|---|
| 157 | announce 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 | |
|---|
| 175 | 1. Receive the packet. |
|---|
| 176 | 2. Check whether the packet is at least 20 bytes. |
|---|
| 177 | 3. Check whether the transaction ID is equal to the one you chose. |
|---|
| 178 | 4. Check whether the action is announce. |
|---|
| 179 | 5. Do not announce again until interval seconds have passed or an event has occurred. |
|---|
| 180 | |
|---|
| 181 | announce 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 | |
|---|
| 193 | Up to about 74 torrents can be scraped at once. A full scrape can't be done with this protocol. |
|---|
| 194 | |
|---|
| 195 | 1. Choose a random transaction ID. |
|---|
| 196 | 2. Fill the scrape request structure. |
|---|
| 197 | 3. Send the packet. |
|---|
| 198 | |
|---|
| 199 | scrape 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 | |
|---|
| 208 | 1. Receive the packet. |
|---|
| 209 | 2. Check whether the packet is at least 8 bytes. |
|---|
| 210 | 3. Check whether the transaction ID is equal to the one you chose. |
|---|
| 211 | 4. Check whether the action is scrape. |
|---|
| 212 | |
|---|
| 213 | scrape 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 | |
|---|
| 223 | If the tracker encounters an error, it might send an error packet. |
|---|
| 224 | |
|---|
| 225 | 1. Receive the packet. |
|---|
| 226 | 2. Check whether the packet is at least 8 bytes. |
|---|
| 227 | 3. Check whether the transaction ID is equal to the one you chose. |
|---|
| 228 | |
|---|
| 229 | error 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 | |
|---|
| 236 | Existing implementations |
|---|
| 237 | ======================== |
|---|
| 238 | |
|---|
| 239 | Azureus, libtorrent [2], opentracker [3], XBT Client and XBT Tracker |
|---|
| 240 | support this protocol. |
|---|
| 241 | |
|---|
| 242 | IPv6 |
|---|
| 243 | ==== |
|---|
| 244 | |
|---|
| 245 | IPv6 is not supported at the moment. A simple way to support IPv6 |
|---|
| 246 | would be to increase the size of all IP addresses to 128 bits when the |
|---|
| 247 | request is done over IPv6. However, I think more experience with IPv6 |
|---|
| 248 | and discussion is needed before including it. |
|---|
| 249 | |
|---|
| 250 | Extensions |
|---|
| 251 | ========== |
|---|
| 252 | |
|---|
| 253 | Extension bits or a version field are not included. Clients and |
|---|
| 254 | trackers should not assume packets to be of a certain size. This way, |
|---|
| 255 | additional fields can be added without breaking compatibility. |
|---|
| 256 | |
|---|
| 257 | References 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: |
|---|