Repository navigation
Home
Welcome to the cnt5106c wiki!
The handshake consists of three parts: handshake header, zero bits, and peer ID. The length of the handshake message is 32 bytes. The handshake header is 18-byte string ‘P2PFILESHARINGPROJ’, which is followed by 10-byte zero bits, which is followed by 4-byte peer ID which is the integer representation of the peer ID. handshake header zero bits peer ID
After handshaking, each peer can send a stream of actual messages. An actual message consists of
- 4-byte message length field
- 1-byte message type
- a message payload with variable size
The 4-byte message length specifies the message length in bytes. It does not include the length of the message length field itself.
The 1-byte message type field specifies the type of the message. There are eight types of messages
| Message Type | Value |
|---|---|
| Choke | 0 |
| unchoke | 1 |
| interested | 2 |
| not interested | 3 |
| have | 4 |
| bitfield | 5 |
| request | 6 |
| piece | 7 |
No Payload
have messages have a payload that contains a 4-byte piece index field
bitfield messages in only sent as the first message right after handshaking is done when a connection is established. bitfield messages have a bitfield as its payload. Each bit in the bitfield payload represents whether the peer has the corresponding piece or not. the first byte of the bitfield corresponds to the piece indices 0 - 7 from high bit to low bit, respectively. The net one corresponds to piece indices 8 - 15, etc. Spare bits at the end are set to zero. Peers that don't have anything yet may skip a bitfield message.
request message have a payload which consists of a 4-byte piece index field. Note that request message payload defined here is different from that of BitTorrent. We don't divide a into smaller subpieces.
piece messages have a payload which consists of a 4-byte piece index field and the content of the piece.