LIBTEXT_API inline std::string to_hex(const char *buf, unsigned bufl) { return to_hex(reinterpret_cast<const unsigned char *>(buf), bufl); }
void entry::to_string_impl(std::string& out, int indent) const { TORRENT_ASSERT(indent >= 0); for (int i = 0; i < indent; ++i) out += " "; switch (m_type) { case int_t: out += libtorrent::to_string(integer()).elems; out += "\n"; break; case string_t: { bool binary_string = false; for (std::string::const_iterator i = string().begin(); i != string().end(); ++i) { if (!is_print(static_cast<unsigned char>(*i))) { binary_string = true; break; } } if (binary_string) { out += to_hex(string()); out += "\n"; } else { out += string(); out += "\n"; } } break; case list_t: { out += "list\n"; for (list_type::const_iterator i = list().begin(); i != list().end(); ++i) { i->to_string_impl(out, indent+1); } } break; case dictionary_t: { out += "dictionary\n"; for (dictionary_type::const_iterator i = dict().begin(); i != dict().end(); ++i) { bool binary_string = false; for (std::string::const_iterator k = i->first.begin(); k != i->first.end(); ++k) { if (!is_print(static_cast<unsigned char>(*k))) { binary_string = true; break; } } for (int j = 0; j < indent+1; ++j) out += " "; out += "["; if (binary_string) out += to_hex(i->first); else out += i->first; out += "]"; if (i->second.type() != entry::string_t && i->second.type() != entry::int_t) out += "\n"; else out += " "; i->second.to_string_impl(out, indent+2); } } break; case undefined_t: default: out += "<uninitialized>\n"; } }
void entry::print(std::ostream& os, int indent) const { TORRENT_ASSERT(indent >= 0); for (int i = 0; i < indent; ++i) os << " "; switch (m_type) { case int_t: os << integer() << "\n"; break; case string_t: { bool binary_string = false; for (std::string::const_iterator i = string().begin(); i != string().end(); ++i) { if (!is_print(static_cast<unsigned char>(*i))) { binary_string = true; break; } } if (binary_string) os << to_hex(string()) << "\n"; else os << string() << "\n"; } break; case list_t: { os << "list\n"; for (list_type::const_iterator i = list().begin(); i != list().end(); ++i) { i->print(os, indent+1); } } break; case dictionary_t: { os << "dictionary\n"; for (dictionary_type::const_iterator i = dict().begin(); i != dict().end(); ++i) { bool binary_string = false; for (std::string::const_iterator k = i->first.begin(); k != i->first.end(); ++k) { if (!is_print(static_cast<unsigned char>(*k))) { binary_string = true; break; } } for (int j = 0; j < indent+1; ++j) os << " "; os << "["; if (binary_string) os << to_hex(i->first); else os << i->first; os << "]"; if (i->second.type() != entry::string_t && i->second.type() != entry::int_t) os << "\n"; else os << " "; i->second.print(os, indent+2); } } break; default: os << "<uninitialized>\n"; } }
void to_variant( const std::vector<char>& var, variant& vo ) { if( var.size() ) vo = variant(to_hex(var.data(),var.size())); else vo = ""; }
static int t64_bit(int verbose) { uint8_t buf[9]; size_t i; int errors = 0; for (i = 0; i < NELE(tests_64_bit); i++) { uint64_t u64; int64_t i64; if (verbose) { fprintf(stderr, "%s %20"PRId64" %20"PRIu64"\n", to_hex(tests_64_bit[i].u8, 8), tests_64_bit[i].i64, tests_64_bit[i].u64); } u64 = le_to_u64(tests_64_bit[i].u8); if (u64 != tests_64_bit[i].u64) { fprintf(stderr, "Failed %s => %"PRIu64"; expected %"PRIu64"\n", to_hex(tests_64_bit[i].u8, 8), u64, tests_64_bit[i].u64); errors++; } i64 = le_to_i64(tests_64_bit[i].u8); if (i64 != tests_64_bit[i].i64) { fprintf(stderr, "Failed %s => %"PRId64"; expected %"PRId64"\n", to_hex(tests_64_bit[i].u8, 8), i64, tests_64_bit[i].i64); errors++; } u64 = le_to_u64(tests_64_bit[i].u8_unaligned + 1); if (u64 != tests_64_bit[i].u64) { fprintf(stderr, "Failed unaligned %s => %"PRIu64"; expected %"PRIu64"\n", to_hex(tests_64_bit[i].u8_unaligned + 1, 8), u64, tests_64_bit[i].u64); errors++; } i64 = le_to_i64(tests_64_bit[i].u8_unaligned + 1); if (i64 != tests_64_bit[i].i64) { fprintf(stderr, "Failed unaligned %s => %"PRId64"; expected %"PRId64"\n", to_hex(tests_64_bit[i].u8_unaligned + 1, 8), i64, tests_64_bit[i].i64); errors++; } u64_to_le(tests_64_bit[i].u64, buf); if (memcmp(buf, tests_64_bit[i].u8, 8) != 0) { fprintf(stderr, "Failed %"PRIu64" => %s; expected %s\n", tests_64_bit[i].u64, to_hex(buf, 8), to_hex(tests_64_bit[i].u8, 8)); errors++; } i64_to_le(tests_64_bit[i].i64, buf); if (memcmp(buf, tests_64_bit[i].u8, 8) != 0) { fprintf(stderr, "Failed %"PRId64" => %s; expected %s\n", tests_64_bit[i].i64, to_hex(buf, 8), to_hex(tests_64_bit[i].u8, 8)); errors++; } u64_to_le(tests_64_bit[i].u64, buf + 1); if (memcmp(buf + 1, tests_64_bit[i].u8, 8) != 0) { fprintf(stderr, "Failed unaligned %"PRIu64" => %s; expected %s\n", tests_64_bit[i].u64, to_hex(buf + 1, 8), to_hex(tests_64_bit[i].u8, 8)); errors++; } i64_to_le(tests_64_bit[i].i64, buf + 1); if (memcmp(buf + 1, tests_64_bit[i].u8, 8) != 0) { fprintf(stderr, "Failed unaligned %"PRId64" => %s; expected %s\n", tests_64_bit[i].i64, to_hex(buf + 1, 8), to_hex(tests_64_bit[i].u8, 8)); errors++; } } return errors; }
static inline bool is_hex(int c) { return to_hex(c) >= 0; }
static int t16_bit(int verbose) { uint8_t buf[9]; size_t i; int errors = 0; for (i = 0; i < NELE(tests_16_bit); i++) { uint16_t u16; int16_t i16; if (verbose) { fprintf(stderr, "%s %6"PRId16" %6"PRId16"\n", to_hex(tests_16_bit[i].u8, 2), tests_16_bit[i].i16, tests_16_bit[i].u16); } u16 = le_to_u16(tests_16_bit[i].u8); if (u16 != tests_16_bit[i].u16) { fprintf(stderr, "Failed %s => %"PRIu16"; expected %"PRIu16"\n", to_hex(tests_16_bit[i].u8, 2), u16, tests_16_bit[i].u16); errors++; } i16 = le_to_i16(tests_16_bit[i].u8); if (i16 != tests_16_bit[i].i16) { fprintf(stderr, "Failed %s => %"PRId16"; expected %"PRId16"\n", to_hex(tests_16_bit[i].u8, 2), i16, tests_16_bit[i].i16); errors++; } u16 = le_to_u16(tests_16_bit[i].u8_unaligned + 1); if (u16 != tests_16_bit[i].u16) { fprintf(stderr, "Failed unaligned %s => %"PRIu16"; expected %"PRIu16"\n", to_hex(tests_16_bit[i].u8_unaligned + 1, 2), u16, tests_16_bit[i].u16); errors++; } i16 = le_to_i16(tests_16_bit[i].u8_unaligned + 1); if (i16 != tests_16_bit[i].i16) { fprintf(stderr, "Failed unaligned %s => %"PRId16"; expected %"PRId16"\n", to_hex(tests_16_bit[i].u8_unaligned + 1, 2), i16, tests_16_bit[i].i16); errors++; } u16_to_le(tests_16_bit[i].u16, buf); if (memcmp(buf, tests_16_bit[i].u8, 2) != 0) { fprintf(stderr, "Failed %"PRIu16" => %s; expected %s\n", tests_16_bit[i].u16, to_hex(buf, 2), to_hex(tests_16_bit[i].u8, 2)); errors++; } i16_to_le(tests_16_bit[i].i16, buf); if (memcmp(buf, tests_16_bit[i].u8, 2) != 0) { fprintf(stderr, "Failed %"PRId16" => %s; expected %s\n", tests_16_bit[i].i16, to_hex(buf, 2), to_hex(tests_16_bit[i].u8, 2)); errors++; } u16_to_le(tests_16_bit[i].u16, buf + 1); if (memcmp(buf + 1, tests_16_bit[i].u8, 2) != 0) { fprintf(stderr, "Failed unaligned %"PRIu16" => %s; expected %s\n", tests_16_bit[i].u16, to_hex(buf + 1, 2), to_hex(tests_16_bit[i].u8, 2)); errors++; } i16_to_le(tests_16_bit[i].i16, buf + 1); if (memcmp(buf + 1, tests_16_bit[i].u8, 2) != 0) { fprintf(stderr, "Failed unaligned %"PRId16" => %s; expected %s\n", tests_16_bit[i].i16, to_hex(buf + 1, 2), to_hex(tests_16_bit[i].u8, 2)); errors++; } } return errors; }
static int t32_bit(int verbose) { uint8_t buf[9]; size_t i; int errors = 0; for (i = 0; i < NELE(tests_32_bit); i++) { uint32_t u32; int32_t i32; if (verbose) { fprintf(stderr, "%s %11"PRId32" %11"PRIu32"\n", to_hex(tests_32_bit[i].u8, 4), tests_32_bit[i].i32, tests_32_bit[i].u32); } u32 = le_to_u32(tests_32_bit[i].u8); if (u32 != tests_32_bit[i].u32) { fprintf(stderr, "Failed %s => %"PRIu32"; expected %"PRIu32"\n", to_hex(tests_32_bit[i].u8, 4), u32, tests_32_bit[i].u32); errors++; } i32 = le_to_i32(tests_32_bit[i].u8); if (i32 != tests_32_bit[i].i32) { fprintf(stderr, "Failed %s => %"PRId32"; expected %"PRId32"\n", to_hex(tests_32_bit[i].u8, 4), i32, tests_32_bit[i].i32); errors++; } u32 = le_to_u32(tests_32_bit[i].u8_unaligned + 1); if (u32 != tests_32_bit[i].u32) { fprintf(stderr, "Failed unaligned %s => %"PRIu32"; expected %"PRIu32"\n", to_hex(tests_32_bit[i].u8_unaligned + 1, 4), u32, tests_32_bit[i].u32); errors++; } i32 = le_to_i32(tests_32_bit[i].u8_unaligned + 1); if (i32 != tests_32_bit[i].i32) { fprintf(stderr, "Failed unaligned %s => %"PRId32"; expected %"PRId32"\n", to_hex(tests_32_bit[i].u8_unaligned + 1, 4), i32, tests_32_bit[i].i32); errors++; } u32_to_le(tests_32_bit[i].u32, buf); if (memcmp(buf, tests_32_bit[i].u8, 4) != 0) { fprintf(stderr, "Failed %"PRIu32" => %s; expected %s\n", tests_32_bit[i].u32, to_hex(buf, 4), to_hex(tests_32_bit[i].u8, 4)); errors++; } i32_to_le(tests_32_bit[i].i32, buf); if (memcmp(buf, tests_32_bit[i].u8, 4) != 0) { fprintf(stderr, "Failed %"PRId32" => %s; expected %s\n", tests_32_bit[i].i32, to_hex(buf, 4), to_hex(tests_32_bit[i].u8, 4)); errors++; } u32_to_le(tests_32_bit[i].u32, buf + 1); if (memcmp(buf + 1, tests_32_bit[i].u8, 4) != 0) { fprintf(stderr, "Failed unaligned %"PRIu32" => %s; expected %s\n", tests_32_bit[i].u32, to_hex(buf + 1, 4), to_hex(tests_32_bit[i].u8, 4)); errors++; } i32_to_le(tests_32_bit[i].i32, buf + 1); if (memcmp(buf + 1, tests_32_bit[i].u8, 4) != 0) { fprintf(stderr, "Failed unaligned %"PRId32" => %s; expected %s\n", tests_32_bit[i].i32, to_hex(buf + 1, 4), to_hex(tests_32_bit[i].u8, 4)); errors++; } } return errors; }
char xor_chars(char a, char b) { return to_char(to_hex(a) ^ to_hex(b)); }
void stats_issue_event( ot_status_event event, PROTO_FLAG proto, uintptr_t event_data ) { switch( event ) { case EVENT_ACCEPT: if( proto == FLAG_TCP ) ot_overall_tcp_connections++; else ot_overall_udp_connections++; #ifdef WANT_LOG_NETWORKS stat_increase_network_count( &stats_network_counters_root, 0, event_data ); #endif break; case EVENT_ANNOUNCE: if( proto == FLAG_TCP ) ot_overall_tcp_successfulannounces++; else ot_overall_udp_successfulannounces++; break; case EVENT_CONNECT: if( proto == FLAG_TCP ) ot_overall_tcp_connects++; else ot_overall_udp_connects++; break; case EVENT_COMPLETED: #ifdef WANT_SYSLOGS if( event_data) { struct ot_workstruct *ws = (struct ot_workstruct *)event_data; char timestring[64]; char hash_hex[42], peerid_hex[42], ip_readable[64]; struct tm time_now; time_t ttt; time( &ttt ); localtime_r( &ttt, &time_now ); strftime( timestring, sizeof( timestring ), "%FT%T%z", &time_now ); to_hex( hash_hex, *ws->hash ); if( ws->peer_id ) to_hex( peerid_hex, (uint8_t*)ws->peer_id ); else { *peerid_hex=0; } #ifdef WANT_V6 ip_readable[ fmt_ip6c( ip_readable, (char*)&ws->peer ) ] = 0; #else ip_readable[ fmt_ip4( ip_readable, (char*)&ws->peer ) ] = 0; #endif syslog( LOG_INFO, "time=%s event=completed info_hash=%s peer_id=%s ip=%s", timestring, hash_hex, peerid_hex, ip_readable ); } #endif ot_overall_completed++; break; case EVENT_SCRAPE: if( proto == FLAG_TCP ) ot_overall_tcp_successfulscrapes++; else ot_overall_udp_successfulscrapes++; break; case EVENT_FULLSCRAPE: ot_full_scrape_count++; ot_full_scrape_size += event_data; break; case EVENT_FULLSCRAPE_REQUEST: { ot_ip6 *ip = (ot_ip6*)event_data; /* ugly hack to transfer ip to stats */ char _debug[512]; int off = snprintf( _debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)(g_now_seconds - ot_start_time)/60 ); off += fmt_ip6c( _debug+off, *ip ); off += snprintf( _debug+off, sizeof(_debug)-off, " - FULL SCRAPE\n" ); write( 2, _debug, off ); ot_full_scrape_request_count++; } break; case EVENT_FULLSCRAPE_REQUEST_GZIP: { ot_ip6 *ip = (ot_ip6*)event_data; /* ugly hack to transfer ip to stats */ char _debug[512]; int off = snprintf( _debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)(g_now_seconds - ot_start_time)/60 ); off += fmt_ip6c(_debug+off, *ip ); off += snprintf( _debug+off, sizeof(_debug)-off, " - FULL SCRAPE\n" ); write( 2, _debug, off ); ot_full_scrape_request_count++; } break; case EVENT_FAILED: ot_failed_request_counts[event_data]++; break; case EVENT_RENEW: ot_renewed[event_data]++; break; case EVENT_SYNC: ot_overall_sync_count+=event_data; break; case EVENT_BUCKET_LOCKED: ot_overall_stall_count++; break; #ifdef WANT_SPOT_WOODPECKER case EVENT_WOODPECKER: pthread_mutex_lock( &g_woodpeckers_mutex ); stat_increase_network_count( &stats_woodpeckers_tree, 0, event_data ); pthread_mutex_unlock( &g_woodpeckers_mutex ); break; #endif case EVENT_CONNID_MISSMATCH: ++ot_overall_udp_connectionidmissmatches; default: break; } }
std::ostream& operator<<(std::ostream& lhs, const BinarySequence& rhs) { to_hex(lhs, rhs.begin(), rhs.end()); return lhs; }
void http_tracker_connection::start() { // TODO: authentication std::string url = tracker_req().url; if (tracker_req().kind == tracker_request::scrape_request) { // find and replace "announce" with "scrape" // in request std::size_t pos = url.find("announce"); if (pos == std::string::npos) { m_ios.post(boost::bind(&http_tracker_connection::fail_disp, self() , error_code(errors::scrape_not_available))); return; } url.replace(pos, 8, "scrape"); } #if TORRENT_USE_I2P bool i2p = is_i2p_url(url); #else static const bool i2p = false; #endif session_settings const& settings = m_ses.settings(); // if request-string already contains // some parameters, append an ampersand instead // of a question mark size_t arguments_start = url.find('?'); if (arguments_start != std::string::npos) url += "&"; else url += "?"; url += "info_hash="; url += escape_string((const char*)&tracker_req().info_hash[0], 20); if (tracker_req().kind == tracker_request::announce_request) { char str[1024]; const bool stats = tracker_req().send_stats; snprintf(str, sizeof(str), "&peer_id=%s&port=%d&uploaded=%"PRId64 "&downloaded=%"PRId64"&left=%"PRId64"&corrupt=%"PRId64"&redundant=%"PRId64 "&compact=1&numwant=%d&key=%x&no_peer_id=1" , escape_string((const char*)&tracker_req().pid[0], 20).c_str() // the i2p tracker seems to verify that the port is not 0, // even though it ignores it otherwise , i2p ? 1 : tracker_req().listen_port , stats ? tracker_req().uploaded : 0 , stats ? tracker_req().downloaded : 0 , stats ? tracker_req().left : 0 , stats ? tracker_req().corrupt : 0 , stats ? tracker_req().redundant: 0 , tracker_req().num_want , tracker_req().key); url += str; #ifndef TORRENT_DISABLE_ENCRYPTION if (m_ses.get_pe_settings().in_enc_policy != pe_settings::disabled) url += "&supportcrypto=1"; #endif if (!tracker_req().trackerid.empty()) { std::string id = tracker_req().trackerid; url += "&trackerid="; url += escape_string(id.c_str(), id.length()); } if (tracker_req().event != tracker_request::none) { const char* event_string[] = {"completed", "started", "stopped", "paused"}; url += "&event="; url += event_string[tracker_req().event - 1]; } #if TORRENT_USE_I2P if (i2p) { url += "&ip="; url += escape_string(m_i2p_conn->local_endpoint().c_str() , m_i2p_conn->local_endpoint().size()); url += ".i2p"; } else #endif if (!m_ses.settings().anonymous_mode) { if (!settings.announce_ip.empty()) { url += "&ip=" + escape_string( settings.announce_ip.c_str(), settings.announce_ip.size()); } else if (m_ses.settings().announce_double_nat && is_local(m_ses.listen_address())) { // only use the global external listen address here // if it turned out to be on a local network // since otherwise the tracker should use our // source IP to determine our origin url += "&ip=" + print_address(m_ses.listen_address()); } if (!tracker_req().ipv6.empty() && !i2p) { url += "&ipv6="; url += tracker_req().ipv6; } if (!tracker_req().ipv4.empty() && !i2p) { url += "&ipv4="; url += tracker_req().ipv4; } } } m_tracker_connection.reset(new http_connection(m_ios, m_cc , boost::bind(&http_tracker_connection::on_response, self(), _1, _2, _3, _4) , true , boost::bind(&http_tracker_connection::on_connect, self(), _1) , boost::bind(&http_tracker_connection::on_filter, self(), _1, _2) #ifdef TORRENT_USE_OPENSSL , tracker_req().ssl_ctx #endif )); int timeout = tracker_req().event==tracker_request::stopped ?settings.stop_tracker_timeout :settings.tracker_completion_timeout; m_tracker_connection->get(url, seconds(timeout) , tracker_req().event == tracker_request::stopped ? 2 : 1 , &m_ps, 5, settings.anonymous_mode ? "" : settings.user_agent , bind_interface() #if TORRENT_USE_I2P , m_i2p_conn #endif ); // the url + 100 estimated header size sent_bytes(url.size() + 100); #if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING) || defined(TORRENT_MINIMAL_LOGGING) boost::shared_ptr<request_callback> cb = requester(); if (cb) { std::string logURL = url; size_t start = logURL.find("?info_hash="); if (start != std::string::npos) { size_t end = logURL.find('&', start+1); if (start != std::string::npos) { logURL.erase (start, end - start - 1); } } cb->debug_log("==> TRACKER_REQUEST [ url: " + logURL + " ] info-hash: " + to_hex (tracker_req().info_hash.to_string())); } #endif }
inline std::string to_hex(const char* input,size_t length) { return to_hex(reinterpret_cast<const uint8_t*>(input),length); }