Linux下C++ libtorrent庫使用

大囚長發表於2019-01-15

下載libtorrent

https://github.com/arvidn/libtorrent/releases

編譯安裝

libtorrent目錄下
./configure
make
make install

寫碼

dumptorrent.cpp

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include "libtorrent/entry.hpp"
#include "libtorrent/bencode.hpp"
#include "libtorrent/torrent_info.hpp"
#include "libtorrent/announce_entry.hpp"
#include "libtorrent/bdecode.hpp"
#include "libtorrent/magnet_uri.hpp"
#include "libtorrent/hex.hpp"

#include <fstream>

std::vector<char> load_file(std::string const& filename)
{
  std::vector<char> ret;
  std::fstream in;
  in.exceptions(std::ifstream::failbit);
  in.open(filename.c_str(), std::ios_base::in | std::ios_base::binary);
  in.seekg(0, std::ios_base::end);
  size_t const size = in.tellg();
  in.seekg(0, std::ios_base::beg);
  ret.resize(size);
  in.read(ret.data(), ret.size());
  return ret;
}

int main(int argc, char* argv[]) try
{
  if (argc < 2 || argc > 4) {
    fputs("usage: dump_torrent torrent-file [total-items-limit] [recursion-limit]\n", stderr);
    return 1;
  }

  int item_limit = 1000000;
  int depth_limit = 1000;

  if (argc > 2) item_limit = atoi(argv[2]);
  if (argc > 3) depth_limit = atoi(argv[3]);

  std::vector<char> buf = load_file(argv[1]);
  lt::bdecode_node e;
  int pos = -1;
  lt::error_code ec;
  std::cout << "decoding. recursion limit: " << depth_limit
    << " total item count limit: " << item_limit << "\n";
  int const ret = lt::bdecode(&buf[0], &buf[0] + buf.size(), e, ec, &pos
    , depth_limit, item_limit);

  printf("\n\n----- raw info -----\n\n%s\n", print_entry(e).c_str());

  if (ret != 0) {
    std::cerr << "failed to decode: '" << ec.message() << "' at character: " << pos<< "\n";
    return 1;
  }

  lt::torrent_info const t(e);
  e.clear();
  std::vector<char>().swap(buf);

  // print info about torrent
  printf("\n\n----- torrent file info -----\n\n"
    "nodes:\n");

  typedef std::vector<std::pair<std::string, int> > node_vec;
  node_vec const& nodes = t.nodes();
  for (node_vec::const_iterator i = nodes.begin(), end(nodes.end());
    i != end; ++i)
  {
    printf("%s: %d\n", i->first.c_str(), i->second);
  }
  puts("trackers:\n");
  for (std::vector<lt::announce_entry>::const_iterator i = t.trackers().begin();
    i != t.trackers().end(); ++i)
  {
    printf("%2d: %s\n", i->tier, i->url.c_str());
  }

  char ih[41];
  lt::to_hex(t.info_hash().data(), 20, ih);
  printf("number of pieces: %d\n"
    "piece length: %d\n"
    "info hash: %s\n"
    "comment: %s\n"
    "created by: %s\n"
    "magnet link: %s\n"
    "name: %s\n"
    "number of files: %d\n"
    "files:\n"
    , t.num_pieces()
    , t.piece_length()
    , ih
    , t.comment().c_str()
    , t.creator().c_str()
    , make_magnet_uri(t).c_str()
    , t.name().c_str()
    , t.num_files());
  lt::file_storage const& st = t.files();
  for (int i = 0; i < st.num_files(); ++i)
  {
    int const first = st.map_file(i, 0, 0).piece;
    int const last = st.map_file(i, (std::max)(boost::int64_t(st.file_size(i))-1, boost::int64_t(0)), 0).piece;
    int const flags = st.file_flags(i);
    printf(" %8" PRIx64 " %11" PRId64 " %c%c%c%c [ %5d, %5d ] %7u %s %s %s%s\n"
      , st.file_offset(i)
      , st.file_size(i)
      , ((flags & lt::file_storage::flag_pad_file)?'p':'-')
      , ((flags & lt::file_storage::flag_executable)?'x':'-')
      , ((flags & lt::file_storage::flag_hidden)?'h':'-')
      , ((flags & lt::file_storage::flag_symlink)?'l':'-')
      , first, last
      , boost::uint32_t(st.mtime(i))
      , st.hash(i) != lt::sha1_hash(0) ? lt::to_hex(st.hash(i).to_string()).c_str() : ""
      , st.file_path(i).c_str()
      , (flags & lt::file_storage::flag_symlink) ? "-> " : ""
      , (flags & lt::file_storage::flag_symlink) ? st.symlink(i).c_str() : "");
  }

  return 0;
}
catch (std::exception const& e)
{
  std::cerr << "ERROR: " << e.what() << "\n";
}

庫軟連線

ln -s /usr/local/lib/libtorrent-rasterbar.so.10 /usr/lib/libtorrent-rasterbar.so.10

編譯

不使用boost庫

g++ -std=c++11 dumptorrent.cpp -o dumptorrent -ltorrent-rasterbar -pthread

其他

也可以在安裝好boost庫和libtorrent後在example資料夾直接make

make dump_torrent

相關文章