BitTorrent protocol analysis Six __bittorrent protocol

Source: Internet
Author: User

u l int process_handshake_msg (Peer *peer,unsigned char *buff,int len)

Function: Handles a received handshake message.

Parameters: Receive this handshake message from peer; Buff point to handshake message; Len is the length of buff. The Code for the function implementation is as follows:

int process_handshake_msg (Peer *peer,unsigned char *buff,int len)

{

if (Peer==null | | buff==null) return-1;

if (memcmp (info_hash,buff+28,20)!= 0) {//Close the connection if Info_hash inconsistent

Peer->state = CLOSING;

Discard data from the send buffer

Discard_send_buffer (peer);

Clear_btcache_before_peer_close (peer);

Close (Peer->socket);

return-1;

}

Save the peer_id of the peer

memcpy (peer->id,buff+48,20);

(Peer->id) [20] = ' n ';

If you are currently in a initial state, send a handshake message to peer

if (peer->state = = INITIAL) {

Create_handshake_msg (Info_hash,peer_id,peer);

Peer->state = handshaked;

}

If the handshake message has been sent, the state is converted to a handshake state

if (peer->state = = halfshaked) peer->state = handshaked;

Record when the peer message was recently received

If no messages from the peer are received within a certain period of time (for example, two minutes), close the connection

Peer->start_timestamp = time (NULL);

return 0;

}

u l int process_keep_alive_msg (Peer *peer,unsigned char *buff,int len)

Function: Process the keepv_alive message from peer that you just received. The Code for the function implementation is as follows:

int process_keep_alive_msg (Peer *peer,unsigned char *buff,int len)

{

if (Peer==null | | buff==null) return-1;

Record when the peer message was recently received

If no messages from the peer are received within a certain period of time (such as 2min), close the connection

Peer->start_timestamp = time (NULL);

return 0;

}

u l int process_choke_msg (Peer *peer,unsigned char *buff,int len)

Function: Process the received choke message. The Code for the function implementation is as follows:

int process_choke_msg (Peer *peer,unsigned char *buff,int len)

{

if (Peer==null | | buff==null) return-1;

If you are in a unchoke state, the value of some variables in peer is updated after you receive the message

if (peer->state!=closing && peer->peer_choking==0) {

peer->peer_choking = 1;

Peer->last_down_timestamp = 0; Zero time to receive data from the peer recently

Peer->down_count = 0; Zero the number of bytes recently downloaded from the peer

peer->down_rate = 0; To peer the most recent download of data from this

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.