Jump to content

Send a custom message over a TCP/IP Connection


cleverbum

Recommended Posts

I'm practicing some stuff on networking, and have found that bittorrent one of the more documented protocols out there, and it's pretty easy to get hold of some people to practice connecting to!!

While I understand it might be against the rules of the board, I'm a little bit stuck, and was hoping that someone could help out.

 

The protocol calls for a handshake that meets the following rules:

The handshake is a required message and must be the first message transmitted by the client. It is (49+len(pstr)) bytes long.

handshake: <pstrlen><pstr><reserved><info_hash><peer_id>

    * pstrlen: string length of <pstr>, as a single raw byte
    * pstr: string identifier of the protocol
    * reserved: eight ( reserved bytes. All current implementations use all zeroes. 
    * info_hash: 20-byte SHA1 hash of the info key in the metainfo file. This is the same info_hash
that is transmitted in tracker requests.
    * peer_id: 20-byte string used as a unique ID for the client. This is usually the same peer_id
that is transmitted in tracker requests (but not always e.g. an anonymity option in Azureus). 

In version 1.0 of the BitTorrent protocol, pstrlen = 19, and pstr = "BitTorrent protocol". 

 

I'm using the following code to try to create this message, but it never seems to work.

        $buffer="000".decbin(19);
$buffer.="BitTorrent protocol";
$buffer.="00000000";//pack("x8");
// the info hash is read in, and the userID apparently usually starts with -AZ2060-
$buffer.=$info_hash."-AZ2060-".substr(sha1("its martinklefas-stennett!!!"),0,20-strlen("-AZ2060-"));

I think I've fundamentally misunderstood how to make a custom binary string, so any help would be greatly appreciated.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.