Discussion:
[libtorrent] sha1_hash and how to create torrent?
kyôn
2004-06-22 12:40:01 UTC
Permalink
helo, I want to know where is the definition of sha1_hash, I tried to use it in a program and i can't to compile it,
I also want to know if this methods are used to create .torrent files:
(torrent_info.hpp)
entry create_torrent() const;
void set_comment(char const* str);
void set_creator(char const* str);
void set_hash(int index, const sha1_hash& h);
void add_tracker(std::string const& url, int tier = 0);
void add_file(boost::filesystem::path file, size_type size);

and how to use them, thanks


______________________________________________________
Consigue tu propio Hosting y Dominio al mejor precio: http://webcenter.lycos.es/
Tristan King
2004-06-22 12:45:25 UTC
Permalink
sha1_hash is defined in "peer_id.hpp"

as for how to create a .torrent. i dunno if that can be done yet
Post by kyôn
helo, I want to know where is the definition of sha1_hash, I tried to use it in a program and i can't to compile it,
(torrent_info.hpp)
entry create_torrent() const;
void set_comment(char const* str);
void set_creator(char const* str);
void set_hash(int index, const sha1_hash& h);
void add_tracker(std::string const& url, int tier = 0);
void add_file(boost::filesystem::path file, size_type size);
and how to use them, thanks
______________________________________________________
Consigue tu propio Hosting y Dominio al mejor precio: http://webcenter.lycos.es/
Arvid Norberg
2004-06-23 00:40:20 UTC
Permalink
Post by kyôn
helo, I want to know where is the definition of sha1_hash, I tried to use it in a program and i can't to compile it,
(torrent_info.hpp)
entry create_torrent() const;
void set_comment(char const* str);
void set_creator(char const* str);
void set_hash(int index, const sha1_hash& h);
void add_tracker(std::string const& url, int tier = 0);
void add_file(boost::filesystem::path file, size_type size);
and how to use them, thanks
It is possible to create torrents with libtorrent, it's just that I
haven't had time to document it yet.

The basic usage, though, is this:

create a torrent_info object. To its constructor you supply piece size
and name (of file or the directory).

create an instance of storage. To its constructor you give a reference
to the previously constructed torrent_info and a path to where the
file/drectory is located.

if it's a multi file torrent, you call torrent_info::add_file() with the
filename and the file's size (in bytes). The file is expected to be
located in a directory with the same name as the one given to the
torrent_info constructor.

When all files has been added, you can add a tracker by calling
torrent_info::add_tracker(). It takes a tracker url and a tier (which is
explained in the backup-tracker extension and to some extent in the
libotrrent docs).

Then you have to calculate the hashes. You do this by asking the
torrent_info for how many pieces there are in the torrent
(torrent_info::num_pieces()) and then iterating over them, calling
storage::read() for each piece. You can know the size of each piece by
calling torrent_info::piece_size(index), where index is the piece index.
All pieces have the same size except the last piece which may be
shorter. Anyway, storage::read() will read the piece data into a buffer
you supply the pointer to. You can then use the hasher to calculate the
sha1_hash of that data buffer. When you have the sha1_hash, you call
torrent_info::set_hash(index, hash) to initialize the torrent.

When you have set the sha1 hashes for all pieces, you can set the
creator. Use torrent_info::set_creator() and supply whatever you want.
It's a string that is supposed to describe who or what created this torrent.

Then finally, you can call torrent_info::create_torrent(), which will
return an entry object that represents the torrent data. You can bencode
this data directly to disk with the following code:

std::ofstream f(path);
libtorrent::bencode(std::ostream_iterator<char>(f), e);

That's it, hope it helps. (set comment is self explanatory)
--
Arvid Norberg
kyôn
2004-06-24 18:35:24 UTC
Permalink
thanks for the
info. I made a
function to generate
a torrent file, if
someone whants to
use it:
//////
//////

#include
"libtorrent/torrent_info.hpp"
#include
"libtorrent/storage.hpp"
#include
"libtorrent/entry.hpp"
#include
"libtorrent/bencode.hpp"
#include
<boost/filesystem/path.hpp>
#include
"libtorrent/peer_id.hpp"
#include
"libtorrent/hasher.hpp"
#include <assert.h>
#include <string>
#include <iostream>
#include <fstream>

void
make_torrent(std::string
path_orig,
std::string
path_dest,
std::string url)
{
//path_orig is the
name of the origin
file in absolute or
relative mode
//idem to path_dest
//url is the url of
the tracker
long tam_f;
ifstream fin;
fin.open(path_orig.c_str(),
ifstream::binary);
filebuf* pbuf;
pbuf = fin.rdbuf();
tam_f =
pbuf->pubseekoff(0,ios_base::end);//calculate
file size
fin.close();

torrent_info
t(32768,
"");//create the
torrent_info
boost::filesystem::path
p(path_orig);
t.add_file(p,
tam_f);//the
constructor of
torrent_info takes a
name, but does not
use it, ¿?
//I have to
add the file whith
this function
boost::filesystem::path
p1("");
storage sto(t, p1);
t.add_tracker(url);//add
a tracker
int num =
t.num_pieces();
hasher h;
char buf[32768];
for(int i=0; i<num;
i++)//calculate sha1
numbers of all
pieces
{
memset(buf, 0,
32768);
sto.read(buf, i,
0, t.piece_size(i));
h.reset();
h.update(buf,
t.piece_size(i));
t.set_hash(i,
h.final());
}
t.set_creator("kyon");//set
the creator
entry e =
t.create_torrent();//write
torrent to a file
ofstream
f(path_dest.c_str());
libtorrent::bencode(std::ostream_iterator<char>(f),
e);
}

////////
////////
I know i am the
worst c programer of
th history, XD.
I have more
questions, ;), I
noticed that the
torrent file is ok
if I calculate the
sha1 of the origin
file and put in the
torrent_info
constructor or if I
don't do this. What
is the minimun size
of the pieces
defined in the
bittorrent
protocol?, whit 1024
it does not work.
Another thing: you
don't use the name
parametre of the
torrent_info
constructor.
In the documentation
you say that the
extension "metadata
from peers" is
experimental, works
corretly?
for use it, I must
use this function?:
torrent_handle
session::add_torrent(
char const*
tracker_url
, sha1_hash
const& info_hash
,
boost::filesystem::path
const& save_path
, entry const&
resume_data =
entry());

thanks
kyon

_________________________________________________________
Envia tus postales desde Lycos Postales. Envía la tuya desde http://postales.lycos.es
Arvid Norberg
2004-06-28 13:38:33 UTC
Permalink
[...]
I have more questions, ;), I noticed that the torrent file is ok if
I calculate the sha1 of the origin file and put in the torrent_info
constructor or if I don't do this. What is the minimun size of the
pieces defined in the bittorrent protocol?, whit 1024 it does not
work.
I don't think such a limit is defined in the protocol. What happens when
you have a piece-size of 1024?

I had a look at your source, it looks correct (I haven't tested it
though). The torrent-making facilities of libtorrent hasn't been very
tested either (that's the biggest reason it hasn't been documented yet).
Another thing: you don't use the name parametre of the torrent_info
constructor.
You're right, it is supposed to fill the 'name' field in the .torrent. I
will commit a fix for this soon.
In the documentation you say that the extension
"metadata from peers" is experimental, works corretly? for use it, I
must use this function?: torrent_handle session::add_torrent( char
const* tracker_url , sha1_hash const& info_hash ,
boost::filesystem::path const& save_path , entry const& resume_data =
entry());
Yes. I consider it experimental partly because it hasn't been tested in
any large scale, and partly because the current implementation isn't
very optimized (the metadata is requested from all peers until it has
been retrieved).
thanks kyon
_________________________________________________________ Envia tus
postales desde Lycos Postales. Envía la tuya desde
http://postales.lycos.es
--
Arvid Norberg
kyôn
2004-06-25 09:32:25 UTC
Permalink
i hate this stupid webmail, the previus message it's view very bad, here is the program as an attach file
kyon

_________________________________________________________
Envia tus postales desde Lycos Postales. Envía la tuya desde http://postales.lycos.es
Loading...