Search code examples
bittorrentbit-fields

Confusion around Bitfield Torrent


I'm a bit confuse about the bitfield message in bittorrent. I have noted the confusion in form of question below.

  1. Optional vs Required

Bitfield to be sent immediately after the handshaking sequence is completed

I'm assuming this is compulsory i.e after handshake there must follow a bitfield message. Correct?

  1. When to expect bitfield?

The bitfield message may only be sent immediately after the handshaking sequence is completed, and before any other messages are sent

assuming I read this clear although be optional message. peer can still broadcast the bitfield message prior to any message (like request, choke, uncoke etc). correct ?

  1. The high bit in the first byte corresponds to piece index 0

If I'm correct bitfield represent the state i.e whether or not the peer has a given piece with it.

Assuming that my bitfield is [1,1,1,1,1,1,1,1,1,1 ..]. I establish the fact that the peer has 10th piece missing and if the bitfield look like this [1,1,0,1,1,1,1,1,1,1 ..] the peer has a 3rd piece missing. Then what is the high bit in the first byte corresponds to piece index 0 means.

  1. Spare bits

Spare bits at the end are set to zero

What does this mean ? I mean if have a bit at end as 0 does it not means that peers has that as missing piece. why is the spare bit used.

  1. The most important of all what is the purpose of the bitfield.

My hunch on this is that bitfield make it easier to find the right peer for a piece knowing available with the peer but am i correct on this?

@Encombe

here how my bitfield payload looks like

\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE


Solution

  • I'm assuming this is compulsory i.e after handshake there must follow a bitfield message. Correct?

    No, the bitfield message is optional, but if a client sends it, it MUST be the first message after the handshake.

    Also, both peers must have sent their complete handshakes, (ie the handshaking sequence is completed), before anyone of them starts to send any type of regular messages including the bitfield message.

    assuming I read this clear although be optional message. peer can still broadcast the bitfield message prior to any message (like request, choke, uncoke etc). correct ?

    Yes, see above. If a client sends a bitfield message anywhere else the connection must be closed.

    Assuming that my bitfield is [1,1,1,1,1,1,1,1,1,1 ..]. I establish the fact that the peer has 10th piece missing

    No. It's unclear to me if your numbers is bits (0b1111111111) or bytes (0x01010101010101010101).

    If it's bits (0b11111111): It means have pieces 0 to 9

    If it's bytes (0x01010101010101010101): It means have pieces 7, 15, 23, 31, 39, 47, 55, 63, 71 and 79

    if the bitfield look like this [1,1,0,1,1,1,1,1,1,1 ..] the peer has a 3rd piece missing.

    No, pieces are zero indexed. 0b1101111111: means piece 2 is missing.

    Then what is the high bit in the first byte corresponds to piece index 0 means.

    It means that the piece with index 0 is represented by the leftmost bit. (Most significant bit in bigendian.)
    . eight bits = one byte
    . 0b10000000 = 0x80
    . ^ high bit set meaning that the client have piece 0

    . 0b00000001 = 0x01
    . ^ low bit set meaning that the client have piece 7

    why is the spare bit used

    If the number of pieces in the torrent is not evenly divisible by eight; there will be bits over, that don't represent any pieces, in the last byte of the bitfield. Those bits must be set to zero.

    The size of the bitfield in bytes can be calculated this way:
    size_bitfield = math.ceil( number_of_pieces / 8 )
    and the number of spare bits is:
    spare_bits = 8 * size_bitfield - number_of_pieces

    what is the purpose of the bitfield

    The purpose is to tell what pieces the client has, so the other peer know what pieces it can request.