Jump to content

Recommended Posts

I'm fiddling around with the bittorrent protocol as a mere proof-of-concept and I'm stuck at a certain point. See, when you request peers to connect to with bittorrent, the tracker returns a "compact format" which consists of a large string of ips and ports. The IP is stored as 4 bytes and the port as 2. Basically i would need to break this string (it's needs to utilize multibyte functions) up by 6 bytes and then break it up into it's ip and port to use this information, but so far I'm not having any luck.

 

Any ideas?

 

I know I'm missing something here, but I can't seem to figure out just how to break up a string by bytes in php...

 

I also threw together (haven't optimized the code, or made sure it worked properly) a quick function to convert the 32-bit ip address it returns into a real IP address I can use:

function dec2dot($dec){
$bin=decbin($dec);
while (strlen($bin)<32){$bin="0".$bin;}
$chunks=chunk_split($bin,8,"|");
$chunksA=explode("|",$chunks);
$final="";$first=true;
foreach ($chunksA as $dd){
	if ($dd!=""){
		if ($first){$final=bindec($dd);$first=false;}
		else{$final.=".".bindec($dd);}
	}
}
return $final;
}

 

Again, It's not optimized, but it seems to do the trick...

 

Thanks in advance for any help, and if you need more information just ask. The official specification for the compact lists is located here:

http://www.bittorrent.org/beps/bep_0023.html

Link to comment
https://forums.phpfreaks.com/topic/153051-solved-working-with-bytes-in-php/
Share on other sites

U may learn a lot more if u already look at some php tracker projects.

As they have to generate those compact codes, it's just a prime learning spot.

 

I started learning php programming from one of them php trackers.

The biggest support forum for a php tracker is for TBDev torrent trackers.

TBDev Portal

Just sign up, look around, and download the code.

The code you will be interested in is in announce.php

 

Good luck :)

You know it crossed my mind, but I never actually did it. Thanks for pointing out the file, I'll download it when I locate where I can download it (the site isn't working I guess).

 

Edit: Hmm... That tracker doesn't support the compact format, which would have made it easy for me to obtain the info from them, but most popular trackers ONLY support compact.

Thanks alot! I found another tracker, and I found out how to unpack the data. Now I support all I need to do is break it apart by 6 chrs, but I'll figure that out.

 

I would call this solved for now at least, I can always bug later if I need more help =).

 

Thanks again laffin.

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.