added some precautionary checks in bdecoder
[libtorrent.git] / docs / examples.rst
blob1ad5aa012c1ecce1cc81847c723c2238492ce481
1 ===================
2 libtorrent Examples
3 ===================
5 :Author: Arvid Norberg, arvid@rasterbar.com
7 .. contents:: Table of contents
8   :depth: 2
9   :backlinks: none
11 examples
12 ========
14 Except for the example programs in this manual, there's also a bigger example
15 of a (little bit) more complete client, ``client_test``. There are separate
16 instructions for how to use it here__ if you'd like to try it. Note that building
17 ``client_test`` also requires boost.regex and boost.program_options library.
19 __ client_test.html
21 dump_torrent
22 ------------
24 This is an example of a program that will take a torrent-file as a parameter and
25 print information about it to std out::
27         #include <iostream>
28         #include <fstream>
29         #include <iterator>
30         #include <iomanip>
31         
32         #include "libtorrent/entry.hpp"
33         #include "libtorrent/bencode.hpp"
34         #include "libtorrent/torrent_info.hpp"
35         #include "libtorrent/lazy_entry.hpp"
36         #include <boost/filesystem/operations.hpp>
37         
38         
39         int main(int argc, char* argv[])
40         {
41                 using namespace libtorrent;
42                 using namespace boost::filesystem;
43         
44                 if (argc != 2)
45                 {
46                         std::cerr << "usage: dump_torrent torrent-file\n";
47                         return 1;
48                 }
49         #if BOOST_VERSION < 103400
50                 boost::filesystem::path::default_name_check(boost::filesystem::no_check);
51         #endif
52         
53         #ifndef BOOST_NO_EXCEPTIONS
54                 try
55                 {
56         #endif
57         
58                         int size = file_size(argv[1]);
59                         if (size > 10 * 1000000)
60                         {
61                                 std::cerr << "file too big (" << size << "), aborting\n";
62                                 return 1;
63                         }
64                         std::vector<char> buf(size);
65                         std::ifstream(argv[1], std::ios_base::binary).read(&buf[0], size);
66                         lazy_entry e;
67                         int ret = lazy_bdecode(&buf[0], &buf[0] + buf.size(), e);
68         
69                         if (ret != 0)
70                         {
71                                 std::cerr << "invalid bencoding: " << ret << std::endl;
72                                 return 1;
73                         }
74         
75                         std::cout << "\n\n----- raw info -----\n\n";
76                         std::cout << e << std::endl;
77                 
78                         torrent_info t(e);
79         
80                         // print info about torrent
81                         std::cout << "\n\n----- torrent file info -----\n\n";
82                         std::cout << "nodes:\n";
83                         typedef std::vector<std::pair<std::string, int> > node_vec;
84                         node_vec const& nodes = t.nodes();
85                         for (node_vec::const_iterator i = nodes.begin(), end(nodes.end());
86                                 i != end; ++i)
87                         {
88                                 std::cout << i->first << ":" << i->second << "\n";
89                         }
90                         std::cout << "trackers:\n";
91                         for (std::vector<announce_entry>::const_iterator i = t.trackers().begin();
92                                 i != t.trackers().end(); ++i)
93                         {
94                                 std::cout << i->tier << ": " << i->url << "\n";
95                         }
96         
97                         std::cout << "number of pieces: " << t.num_pieces() << "\n";
98                         std::cout << "piece length: " << t.piece_length() << "\n";
99                         std::cout << "info hash: " << t.info_hash() << "\n";
100                         std::cout << "comment: " << t.comment() << "\n";
101                         std::cout << "created by: " << t.creator() << "\n";
102                         std::cout << "files:\n";
103                         int index = 0;
104                         for (torrent_info::file_iterator i = t.begin_files();
105                                 i != t.end_files(); ++i, ++index)
106                         {
107                                 int first = t.map_file(index, 0, 1).piece;
108                                 int last = t.map_file(index, i->size - 1, 1).piece;
109                                 std::cout << "  " << std::setw(11) << i->size
110                                         << " " << i->path.string() << "[ " << first << ", "
111                                         << last << " ]\n";
112                         }
113         
114         #ifndef BOOST_NO_EXCEPTIONS
115                 }
116                 catch (std::exception& e)
117                 {
118                         std::cout << e.what() << "\n";
119                 }
120         #endif
121         
122                 return 0;
123         }
125 simple client
126 -------------
128 This is a simple client. It doesn't have much output to keep it simple::
130         int main(int argc, char* argv[])
131         {
132                 using namespace libtorrent;
133         #if BOOST_VERSION < 103400
134                 namespace fs = boost::filesystem;
135                 fs::path::default_name_check(fs::no_check);
136         #endif
137         
138         if (argc != 2)
139         {
140                 std::cerr << "usage: ./simple_client torrent-file\n"
141                         "to stop the client, press return.\n";
142                 return 1;
143         }
144         
145         #ifndef BOOST_NO_EXCEPTIONS
146                 try
147         #endif
148                 {
149                         session s;
150                         s.listen_on(std::make_pair(6881, 6889));
151                         add_torrent_params p;
152                         p.save_path = "./";
153                         p.ti = new torrent_info(argv[1]);
154                         s.add_torrent(p);
155         
156                         // wait for the user to end
157                         char a;
158                         std::cin.unsetf(std::ios_base::skipws);
159                         std::cin >> a;
160                 }
161         #ifndef BOOST_NO_EXCEPTIONS
162                 catch (std::exception& e)
163                 {
164                         std::cout << e.what() << "\n";
165                 }
166         #endif
167                 return 0;
168         }
170 make_torrent
171 ------------
173 Shows how to create a torrent from a directory tree::
175         #include <iostream>
176         #include <fstream>
177         #include <iterator>
178         #include <iomanip>
179         
180         #include "libtorrent/entry.hpp"
181         #include "libtorrent/bencode.hpp"
182         #include "libtorrent/torrent_info.hpp"
183         #include "libtorrent/file.hpp"
184         #include "libtorrent/storage.hpp"
185         #include "libtorrent/hasher.hpp"
186         #include "libtorrent/create_torrent.hpp"
187         
188         #include <boost/filesystem/operations.hpp>
189         #include <boost/filesystem/path.hpp>
190         #include <boost/filesystem/fstream.hpp>
191         #include <boost/bind.hpp>
192         
193         using namespace boost::filesystem;
194         using namespace libtorrent;
195         
196         // do not include files and folders whose
197         // name starts with a .
198         bool file_filter(boost::filesystem::path const& filename)
199         {
200                 if (filename.leaf()[0] == '.') return false;
201                 std::cerr << filename << std::endl;
202                 return true;
203         }
204         
205         void print_progress(int i, int num)
206         {
207                 std::cerr << "\r" << (i+1) << "/" << num;
208         }
209         
210         int main(int argc, char* argv[])
211         {
212                 using namespace libtorrent;
213                 using namespace boost::filesystem;
214         
215                 int piece_size = 256 * 1024;
216                 char const* creator_str = "libtorrent";
217         
218                 path::default_name_check(no_check);
219         
220                 if (argc != 4 && argc != 5)
221                 {
222                         std::cerr << "usage: make_torrent <output torrent-file> "
223                         "<announce url> <file or directory to create torrent from> "
224                         "[url-seed]\n";
225                 return 1;
226         }
227         
228         #ifndef BOOST_NO_EXCEPTIONS
229                 try
230                 {
231         #endif
232                         file_storage fs;
233                         file_pool fp;
234                         path full_path = complete(path(argv[3]));
235         
236                         add_files(fs, full_path, file_filter);
237         
238                         create_torrent t(fs, piece_size);
239                         t.add_tracker(argv[2]);
240                         set_piece_hashes(t, full_path.branch_path()
241                                 , boost::bind(&print_progress, _1, t.num_pieces()));
242                         std::cerr << std::endl;
243                         t.set_creator(creator_str);
244         
245                         if (argc == 5) t.add_url_seed(argv[4]);
246         
247                         // create the torrent and print it to out
248                         ofstream out(complete(path(argv[1])), std::ios_base::binary);
249                         bencode(std::ostream_iterator<char>(out), t.generate());
250         #ifndef BOOST_NO_EXCEPTIONS
251                 }
252                 catch (std::exception& e)
253                 {
254                         std::cerr << e.what() << "\n";
255                 }
256         #endif
257         
258                 return 0;
259         }
260