⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 protocol.txt

📁 libbt-1.04
💻 TXT
📖 第 1 页 / 共 2 页
字号:
*  resp = {*     failure reason => 'error text'*       - or -*     interval => 12345*     peers => {*         peer id => 'identifier'*         ip => 'ip-address' -or- 'hostname'*         port => 12345*     }*  }Tracker responses are bencoded dictionaries. If a tracker response has akey 'failure reason', then that maps to a human readable string whichexplains why the query failed, and no other keys are required.Otherwise, it must have two keys - 'interval', which maps to the numberof seconds the downloader should wait between regular rerequests, and'peers'. 'peers' maps to a list of dictionaries corresponding to peers,each of which contains the keys 'peer id', 'ip', and 'port', which mapto the peer's self-selected id, ip address or dns name as a string, andport number, respectively. Note that downloaders may rerequest onnonscheduled times if an event happens or they need more peers.If you want to make any extensions to metainfo files or tracker queries,please coordinate with Bram Cohen <mailto:bram@bitconjurer.org> to makesure that all extensions are done compatibly.* BitTorrent's peer protocol operates over TCP**   peer = {*   }*BitTorrent's peer protocol operates over TCP. It performs efficientlywithout setting any socket options.Peer connections are symmetrical. Messages sent in both directions lookthe same, and data can flow in either direction.The peer protocol refers to pieces of the file by index as described inthe metainfo file, starting at zero. When a peer finishes downloading apiece and checks that the hash matches, it announces that it has thatpiece to all of its peers.Connections contain two bits of state on either end - choked or not, andinterested or not. Choking is a notification that no data will be sentuntil unchoking happens. The reasoning and common techniques behindchoking are explained later in this document.Data transfer takes place whenever one side is interested and the otherside is not choking. Interest state must be kept up to date at all times- whenever a downloader doesn't have something they currently would aska peer for in unchoked, they must express lack of interest, despitebeing choked. Implementing this properly is tricky, but makes itpossible for downloaders to know which peers will start downloadingimmediately if unchoked.Connections start out choked and not interested.When data is being transferred, downloaders should keep several piecerequests queued up at once in order to get good TCP performance (this iscalled 'pipelining'.) On the other side, requests which can't be writtenout to the TCP buffer immediately should be queued up in memory ratherthan kept in an application-level network buffer, so they can all bethrown out when a choke happens.*    Handshake:*    \x13BitTorrent protocol\0\0\0\0\0\0\0\0<sha1 info hash><20byte peerid>**    Keep alive:*    \x0000000**    0 - choke:*    \x0000001\00*    *    1 - unchoke:*    \x0000001\01**    2 - interested:*    \x0000001\02* *    3 - not interested:*    \x0000001\03**    4 - have:*    \x0000005\04<index>**    5 - bitfield:*    \x00000??\05<bitfield-data>**    6 - request:*    \x000000d\06<index><begin><length>*    length must be less than 2^17, recommended 2^15, regardless of pieces size**    7 - piece:*    \x???????\07<index><begin><data>**    8 - cancel:*    \x000000d\08<index><begin><length>The peer wire protocol consists of a handshake followed by anever-ending stream of length-prefixed messages. The handshake startswith character ninteen followed by the string 'BitTorrent protocol'. Theleading character is a length prefix, put there in the hope that othernew protocols may do the same and thus be trivially distinguishable fromeach other.All later integers sent in the protocol are encoded as four bytesbig-endian.After the fixed headers come eight reserved bytes, which are all zero inall current implementations. If you wish to extend the protocol usingthese bytes, please coordinate with Bram Cohen<mailto:bram@bitconjurer.org> to make sure all extensions are donecompatibly.Next comes the 20 byte sha1 hash of the bencoded form of the 'info'value from the metainfo file. (This is the same value which is announcedas info_hash to the tracker, only here it's raw instead of quoted here).If both sides don't send the same value, they sever the connection. Theone possible exception is if a downloader wants to do multiple downloadsover a single port, they may wait for incoming connections to give adownload hash first, and respond with the same one if it's in their list.After the download hash comes the 20-byte peer id which is reported intracker requests and contained in peer lists in tracker responses. Ifthe receiving side's peer id doesn't match the one the initiating sideexpects, it severs the connection.That's it for handshaking, next comes an alternating stream of lengthprefixes and messages. Messages of length zero are keepalives, andignored. Keepalives are generally sent once every two minutes, but notethat timeouts can be done much more quickly when data is expected.All non-keepalive messages start with a single byte which gives theirtype. The possible values are -    * 0 - choke    * 1 - unchoke    * 2 - interested    * 3 - not interested    * 4 - have    * 5 - bitfield    * 6 - request    * 7 - piece    * 8 - cancelChoke, unchoke, interested, and not interested have no payload.Bitfield is only ever sent as the first message. Its payload is abitfield with each index that downloader has sent set to one and therest set to zero. Downloaders which don't have anything yet may skip thebitfield message. The first byte of the bitfield corresponds to indices0-7 from high bit to low bit, respectively. The next one 8-15, etc.Spare bits at the end are set to zero.The have message's payload is a single number, the index which thatdownloader just completed and checked the hash of.Request messages contain an index, begin, and length. The last two arebyte offsets. Length is generally a power of two unless it getstruncated by the end of the file. All current implementations use 2^15 ,and close connections which request an amount greater than 2^17 .Cancel messages have the same payload as request messages. They aregenerally only sent towards the end of a download, during what's called'endgame mode'. When a download is almost complete, there's a tendencyfor the last few pieces to all be downloaded off a single hosed modemline, taking a very long time. To make sure the last few pieces come inquickly, once requests for all pieces a given downloader doesn't haveyet are currently pending, it sends requests for everything to everyoneit's downloading from. To keep this from becoming horribly inefficient,it sends cancels to everyone else every time a piece arrives.Piece messages contain an index, begin, and piece. Note that they arecorrelated with request messages implicitly. It's possible for anunexpected piece to arrive if choke and unchoke messages are sent inquick succession and/or transfer is going very slowly.Downloaders generally download pieces in random order, which does areasonably good job of keeping them from having a strict subset orsuperset of the pieces of any of their peers.Choking is done for several reasons. TCP congestion control behaves verypoorly when sending over many connections at once. Also, choking letseach peer use a tit-for-tat-ish algorithm to ensure that they get aconsistent download rate.The choking algorithm described below is the currently deployed one. Itis very important that all new algorithms work well both in a networkconsisting entirely of themselves and in a network consisting mostly ofthis one.There are several criteria a good choking algorithm should meet. Itshould cap the number of simultaneous uploads for good TCP performance.It should avoid choking and unchoking quickly, known as fibrillation. Itshould reciprocate to peers who let it download. Finally, it should tryout unused connections once in a while to find out if they might bebetter than the currently used ones, known as optimistic unchoking.The currently deployed choking algorithm avoids fibrillation by onlychanging who's choked once every ten seconds. It does reciprocation andnumber of uploads capping by unchoking the four peers which it has thebest download rates from and are interested. Peers which have a betterupload rate but aren't interested get unchoked and if they becomeinterested the worst uploader gets choked. If a downloader has acomplete file, it uses its upload rate rather than its download rate todecide who to unchoke.For optimistic unchoking, at any one time there is a single peer whichis unchoked regardless of it's upload rate (if interested, it counts asone of the four allowed downloaders.) Which peer is optimisticallyunchoked rotates every 30 seconds. To give them a decent chance ofgetting a complete piece to upload, new connections are three times aslikely to start as the current optimistic unchoke as anywhere else inthe rotation.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -