diff --git a/include/libtorrent/alert_types.hpp b/include/libtorrent/alert_types.hpp index 094be3428..3412cdd1c 100644 --- a/include/libtorrent/alert_types.hpp +++ b/include/libtorrent/alert_types.hpp @@ -114,9 +114,10 @@ namespace libtorrent virtual char const* what() const { return "read piece"; } virtual std::string message() const { - std::stringstream ret; - ret << torrent_alert::message() << ": piece " << (buffer ? "successful " : "failed ") << piece; - return ret.str(); + char msg[200]; + snprintf(msg, 200, "%s: piece %s %u", torrent_alert::message().c_str() + , buffer ? "successful" : "failed", piece); + return msg; } boost::shared_array buffer; @@ -141,10 +142,10 @@ namespace libtorrent virtual char const* what() const { return "file renamed"; } virtual std::string message() const { - std::stringstream ret; - ret << torrent_alert::message() << ": file " - << index << " renamed to " << name; - return ret.str(); + char msg[200]; + snprintf(msg, 200, "%s: file %d renamed to %s", torrent_alert::message().c_str() + , index, name.c_str()); + return msg; } std::string name; @@ -167,10 +168,10 @@ namespace libtorrent virtual char const* what() const { return "file rename failed"; } virtual std::string message() const { - std::stringstream ret; - ret << torrent_alert::message() << ": failed to rename file " - << index << ": " << msg; - return ret.str(); + char ret[200]; + snprintf(ret, 200, "%s: failed to rename file %d: %s" + , torrent_alert::message().c_str(), index, msg.c_str()); + return ret; } const static int static_category = alert::storage_notification; @@ -273,10 +274,10 @@ namespace libtorrent virtual char const* what() const { return "tracker error"; } virtual std::string message() const { - std::stringstream ret; - ret << tracker_alert::message() << " (" << status_code << ") " - << msg << " (" << times_in_row << ")"; - return ret.str(); + char ret[200]; + snprintf(ret, 200, "%s (%u) %s (%d)" + , torrent_alert::message().c_str(), status_code, msg.c_str(), times_in_row); + return ret; } int times_in_row; @@ -325,10 +326,10 @@ namespace libtorrent virtual char const* what() const { return "tracker scrape reply"; } virtual std::string message() const { - std::stringstream ret; - ret << tracker_alert::message() << " scrape reply: " << incomplete - << " " << complete; - return ret.str(); + char ret[200]; + snprintf(ret, 200, "%s scrape reply: %u %u" + , torrent_alert::message().c_str(), incomplete, complete); + return ret; } }; @@ -370,10 +371,10 @@ namespace libtorrent virtual char const* what() const { return "tracker reply"; } virtual std::string message() const { - std::stringstream ret; - ret << tracker_alert::message() << " received peers: " - << num_peers; - return ret.str(); + char ret[200]; + snprintf(ret, 200, "%s received peers: %u" + , torrent_alert::message().c_str(), num_peers); + return ret; } }; @@ -392,10 +393,10 @@ namespace libtorrent virtual char const* what() const { return "DHT reply"; } virtual std::string message() const { - std::stringstream ret; - ret << torrent_alert::message() << " received DHT peers: " - << num_peers; - return ret.str(); + char ret[200]; + snprintf(ret, 200, "%s received DHT peers: %u" + , torrent_alert::message().c_str(), num_peers); + return ret; } }; @@ -435,10 +436,10 @@ namespace libtorrent virtual int category() const { return static_category; } virtual std::string message() const { - std::stringstream ret; - ret << torrent_alert::message() << " hash for piece " - << piece_index << " failed"; - return ret.str(); + char ret[200]; + snprintf(ret, 200, "%s hash for piece %u failed" + , torrent_alert::message().c_str(), piece_index); + return ret; } int piece_index; @@ -567,11 +568,10 @@ namespace libtorrent virtual char const* what() const { return "invalid piece request"; } virtual std::string message() const { - std::stringstream ret; - ret << peer_alert::message() << " peer sent an invalid piece request " - "( piece: " << request.piece << " start: " << request.start - << " len: " << request.length << ")"; - return ret.str(); + char ret[200]; + snprintf(ret, 200, "%s peer sent an invalid piece request (piece: %u start: %u len: %u)" + , torrent_alert::message().c_str(), request.piece, request.start, request.length); + return ret; } peer_request request; @@ -613,10 +613,10 @@ namespace libtorrent virtual int category() const { return static_category; } virtual std::string message() const { - std::stringstream ret; - ret << torrent_alert::message() << " piece " << piece_index - << " finished downloading"; - return ret.str(); + char ret[200]; + snprintf(ret, 200, "%s piece: %u finished downloading" + , torrent_alert::message().c_str(), piece_index); + return ret; } }; @@ -640,10 +640,10 @@ namespace libtorrent virtual int category() const { return static_category; } virtual std::string message() const { - std::stringstream ret; - ret << peer_alert::message() << " peer dropped block ( piece: " - << piece_index << " block: " << block_index << ")"; - return ret.str(); + char ret[200]; + snprintf(ret, 200, "%s peer dropped block ( piece: %u block: %u)" + , torrent_alert::message().c_str(), piece_index, block_index); + return ret; } }; @@ -667,10 +667,10 @@ namespace libtorrent virtual int category() const { return static_category; } virtual std::string message() const { - std::stringstream ret; - ret << peer_alert::message() << " peer timed out request ( piece: " - << piece_index << " block: " << block_index << ")"; - return ret.str(); + char ret[200]; + snprintf(ret, 200, "%s peer timed out request ( piece: %u block: %u)" + , torrent_alert::message().c_str(), piece_index, block_index); + return ret; } }; @@ -693,10 +693,10 @@ namespace libtorrent virtual int category() const { return static_category; } virtual std::string message() const { - std::stringstream ret; - ret << peer_alert::message() << " block finished downloading ( piece: " - << piece_index << " block: " << block_index << ")"; - return ret.str(); + char ret[200]; + snprintf(ret, 200, "%s block finished downloading (piece: %u block: %u)" + , torrent_alert::message().c_str(), piece_index, block_index); + return ret; } }; @@ -721,10 +721,10 @@ namespace libtorrent virtual int category() const { return static_category; } virtual std::string message() const { - std::stringstream ret; - ret << peer_alert::message() << " requested block ( piece: " - << piece_index << " block: " << block_index << ") " << peer_speedmsg; - return ret.str(); + char ret[200]; + snprintf(ret, 200, "%s requested block (piece: %u block: %u) %s" + , torrent_alert::message().c_str(), piece_index, block_index, peer_speedmsg); + return ret; } }; @@ -745,10 +745,10 @@ namespace libtorrent virtual char const* what() const { return "unwanted block received"; } virtual std::string message() const { - std::stringstream ret; - ret << peer_alert::message() << " received block not in download queue ( piece: " - << piece_index << " block: " << block_index << ")"; - return ret.str(); + char ret[200]; + snprintf(ret, 200, "%s received block not in download queue (piece: %u block: %u)" + , torrent_alert::message().c_str(), piece_index, block_index); + return ret; } }; @@ -1059,11 +1059,10 @@ namespace libtorrent virtual int category() const { return static_category; } virtual std::string message() const { - error_code ec; - std::stringstream ret; - ret << "listening on " << endpoint - << " failed: " << error.message(); - return ret.str(); + char ret[200]; + snprintf(ret, 200, "listening on %s failed: %s" + , print_endpoint(endpoint).c_str(), error.message().c_str()); + return ret; } }; @@ -1082,10 +1081,9 @@ namespace libtorrent virtual int category() const { return static_category; } virtual std::string message() const { - error_code ec; - std::stringstream ret; - ret << "successfully listening on " << endpoint; - return ret.str(); + char ret[200]; + snprintf(ret, 200, "successfully listening on %s", print_endpoint(endpoint).c_str()); + return ret; } }; @@ -1131,10 +1129,10 @@ namespace libtorrent virtual std::string message() const { static char const* type_str[] = {"NAT-PMP", "UPnP"}; - std::stringstream ret; - ret << "successfully mapped port using " << type_str[type] - << ". external port: " << external_port; - return ret.str(); + char ret[200]; + snprintf(ret, 200, "successfully mapped port using %s. external port: %u" + , type_str[type], external_port); + return ret; } }; @@ -1155,9 +1153,9 @@ namespace libtorrent virtual std::string message() const { static char const* type_str[] = {"NAT-PMP", "UPnP"}; - std::stringstream ret; - ret << type_str[type] << ": " << msg; - return ret.str(); + char ret[200]; + snprintf(ret, 200, "%s: %s", type_str[type], msg.c_str()); + return ret; } }; @@ -1224,10 +1222,12 @@ namespace libtorrent virtual std::string message() const { error_code ec; - std::stringstream ret; - ret << "incoming dht annonce: " << ip.to_string(ec) << ":" - << port << " (" << info_hash << ")"; - return ret.str(); + char ih_hex[41]; + to_hex((const char*)&info_hash[0], 20, ih_hex); + char msg[200]; + snprintf(msg, 200, "incoming dht announce: %s:%u (%s)" + , ip.to_string(ec).c_str(), port, ih_hex); + return msg; } }; @@ -1246,10 +1246,11 @@ namespace libtorrent virtual int category() const { return static_category; } virtual std::string message() const { - error_code ec; - std::stringstream ret; - ret << "incoming dht get_peers: " << info_hash; - return ret.str(); + char ih_hex[41]; + to_hex((const char*)&info_hash[0], 20, ih_hex); + char msg[200]; + snprintf(msg, 200, "incoming dht get_peers: %s", ih_hex); + return msg; } }; } diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index b4c36e115..8eb72c58d 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -650,20 +650,24 @@ namespace libtorrent , int incomplete , address const& external_ip) { - std::stringstream s; - s << "TRACKER RESPONSE:\n" - "interval: " << interval << "\n" - "peers:\n"; + std::string s; + s = "TRACKER RESPONSE:\n"; + char tmp[200]; + snprintf(tmp, 200, "interval: %d\npeers:\n", interval); + s += tmp; for (std::vector::const_iterator i = peers.begin(); i != peers.end(); ++i) { - s << " " << std::setfill(' ') << std::setw(16) << i->ip - << " " << std::setw(5) << std::dec << i->port << " "; - if (!i->pid.is_all_zeros()) s << " " << i->pid; - s << "\n"; + char pid[41]; + to_hex((const char*)&i->pid[0], 20, pid); + if (i->pid.is_all_zeros()) pid[0] = 0; + + snprintf(tmp, 200, " %16s %5d %s\n", i->ip.c_str(), i->port, pid); + s += tmp; } - s << "external ip: " << external_ip << "\n"; - debug_log(s.str()); + snprintf(tmp, 200, "external ip: %s\n", print_address(external_ip).c_str()); + s += tmp; + debug_log(s); } void tracker_request_timed_out( diff --git a/include/libtorrent/config.hpp b/include/libtorrent/config.hpp index 6b3de8dbb..fe0fab07c 100644 --- a/include/libtorrent/config.hpp +++ b/include/libtorrent/config.hpp @@ -88,6 +88,7 @@ POSSIBILITY OF SUCH DAMAGE. #define TORRENT_USE_MLOCK 1 #define TORRENT_USE_READV 1 #define TORRENT_USE_WRITEV 1 +#define TORRENT_USE_IOSTREAM 1 // should wpath or path be used? #if defined UNICODE && !defined BOOST_FILESYSTEM_NARROW_ONLY \ diff --git a/include/libtorrent/debug.hpp b/include/libtorrent/debug.hpp index 0f2dad20b..37a6387dd 100644 --- a/include/libtorrent/debug.hpp +++ b/include/libtorrent/debug.hpp @@ -33,9 +33,15 @@ POSSIBILITY OF SUCH DAMAGE. #ifndef TORRENT_DEBUG_HPP_INCLUDED #define TORRENT_DEBUG_HPP_INCLUDED +#ifdef TORRENT_DEBUG + #include +#include "libtorrent/config.hpp" + +#if TORRENT_USE_IOSTREAM #include #include +#endif #ifdef _MSC_VER #pragma warning(push, 1) @@ -60,6 +66,8 @@ namespace libtorrent { logger(fs::path const& logpath, fs::path const& filename, int instance, bool append = true) { +#if TORRENT_USE_IOSTREAM + #ifndef BOOST_NO_EXCEPTIONS try { @@ -74,21 +82,27 @@ namespace libtorrent { std::cerr << "failed to create log '" << filename.string() << "': " << e.what() << std::endl; } +#endif #endif } template logger& operator<<(T const& v) { +#if TORRENT_USE_IOSTREAM m_file << v; m_file.flush(); +#endif return *this; } +#if TORRENT_USE_IOSTREAM std::ofstream m_file; +#endif }; } +#endif // TORRENT_DEBUG #endif // TORRENT_DEBUG_HPP_INCLUDED diff --git a/include/libtorrent/escape_string.hpp b/include/libtorrent/escape_string.hpp index 5a8ef0cb3..99c08b21c 100644 --- a/include/libtorrent/escape_string.hpp +++ b/include/libtorrent/escape_string.hpp @@ -61,6 +61,8 @@ namespace libtorrent std::string const& url, std::string argument); TORRENT_EXPORT std::string to_hex(std::string const& s); + TORRENT_EXPORT void to_hex(char const *in, int len, char* out); + TORRENT_EXPORT bool from_hex(char const *in, int len, char* out); #if TORRENT_USE_WPATH TORRENT_EXPORT std::wstring convert_to_wstring(std::string const& s); diff --git a/include/libtorrent/extensions/logger.hpp b/include/libtorrent/extensions/logger.hpp index 42e08fcf6..5e5e6cb3a 100644 --- a/include/libtorrent/extensions/logger.hpp +++ b/include/libtorrent/extensions/logger.hpp @@ -43,6 +43,10 @@ POSSIBILITY OF SUCH DAMAGE. #pragma warning(pop) #endif +#include "libtorrent/config.hpp" + +#if TORRENT_USE_IOSTREAM + namespace libtorrent { struct torrent_plugin; @@ -50,5 +54,7 @@ namespace libtorrent boost::shared_ptr create_logger_plugin(torrent*); } +#endif + #endif // TORRENT_LOGGER_HPP_INCLUDED diff --git a/include/libtorrent/ip_filter.hpp b/include/libtorrent/ip_filter.hpp index 1adb14551..c87afbacb 100644 --- a/include/libtorrent/ip_filter.hpp +++ b/include/libtorrent/ip_filter.hpp @@ -34,7 +34,6 @@ POSSIBILITY OF SUCH DAMAGE. #define TORRENT_IP_FILTER_HPP #include -#include #ifdef _MSC_VER #pragma warning(push, 1) diff --git a/include/libtorrent/kademlia/logging.hpp b/include/libtorrent/kademlia/logging.hpp index d55627f10..a84a0753c 100644 --- a/include/libtorrent/kademlia/logging.hpp +++ b/include/libtorrent/kademlia/logging.hpp @@ -33,6 +33,10 @@ POSSIBILITY OF SUCH DAMAGE. #ifndef TORRENT_LOGGING_HPP #define TORRENT_LOGGING_HPP +#include "libtorrent/config.hpp" + +#if TORRENT_USE_IOSTREAM + #include #include #include "libtorrent/time.hpp" @@ -143,5 +147,7 @@ public: if (libtorrent::dht::inverted_log_event event_object__ = name ## _log()); \ else static_cast(event_object__) +#endif // TORRENT_USE_IOSTREAM + #endif diff --git a/include/libtorrent/lazy_entry.hpp b/include/libtorrent/lazy_entry.hpp index 3745535ef..80187c4e1 100644 --- a/include/libtorrent/lazy_entry.hpp +++ b/include/libtorrent/lazy_entry.hpp @@ -35,12 +35,15 @@ POSSIBILITY OF SUCH DAMAGE. #include #include -#include #include #include "libtorrent/config.hpp" #include "libtorrent/assert.hpp" #include "libtorrent/size_type.hpp" +#if TORRENT_USE_IOSTREAM +#include +#endif + namespace libtorrent { struct lazy_entry; @@ -227,7 +230,9 @@ namespace libtorrent char const* m_end; }; +#if TORRENT_USE_IOSTREAM TORRENT_EXPORT std::ostream& operator<<(std::ostream& os, lazy_entry const& e); +#endif } diff --git a/include/libtorrent/peer_id.hpp b/include/libtorrent/peer_id.hpp index 67d07cc7a..50274eebe 100644 --- a/include/libtorrent/peer_id.hpp +++ b/include/libtorrent/peer_id.hpp @@ -33,8 +33,6 @@ POSSIBILITY OF SUCH DAMAGE. #ifndef TORRENT_PEER_ID_HPP_INCLUDED #define TORRENT_PEER_ID_HPP_INCLUDED -#include -#include #include #include #include @@ -44,6 +42,11 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/assert.hpp" #include "libtorrent/escape_string.hpp" +#if TORRENT_USE_IOSTREAM +#include +#include +#endif + namespace libtorrent { @@ -160,41 +163,23 @@ namespace libtorrent typedef big_number peer_id; typedef big_number sha1_hash; +#if TORRENT_USE_IOSTREAM inline std::ostream& operator<<(std::ostream& os, big_number const& peer) { - for (big_number::const_iterator i = peer.begin(); - i != peer.end(); ++i) - { - os << std::hex << std::setw(2) << std::setfill('0') - << static_cast(*i); - } - os << std::dec << std::setfill(' '); - return os; + char out[41]; + to_hex((char const*)&peer[0], big_number::size, out); + return os << out; } inline std::istream& operator>>(std::istream& is, big_number& peer) { - for (big_number::iterator i = peer.begin(); - i != peer.end(); ++i) - { - char c[2]; - is >> c[0] >> c[1]; - c[0] = tolower(c[0]); - c[1] = tolower(c[1]); - if ( - ((c[0] < '0' || c[0] > '9') && (c[0] < 'a' || c[0] > 'f')) - || ((c[1] < '0' || c[1] > '9') && (c[1] < 'a' || c[1] > 'f')) - || is.fail()) - { - is.setstate(std::ios_base::failbit); - return is; - } - *i = ((is_digit(c[0])?c[0]-'0':c[0]-'a'+10) << 4) - + (is_digit(c[1])?c[1]-'0':c[1]-'a'+10); - } + char hex[40]; + is.read(hex, 40); + if (from_hex(hex, 40, (char*)&peer[0]) == -1) + is.setstate(std::ios_base::failbit); return is; } - +#endif // TORRENT_USE_IOSTREAM } #endif // TORRENT_PEER_ID_HPP_INCLUDED diff --git a/include/libtorrent/socket.hpp b/include/libtorrent/socket.hpp index ed8daf9b9..a5ad72334 100644 --- a/include/libtorrent/socket.hpp +++ b/include/libtorrent/socket.hpp @@ -74,6 +74,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/io.hpp" #include "libtorrent/time.hpp" #include "libtorrent/error_code.hpp" +#include "libtorrent/escape_string.hpp" // for to_string #ifdef _MSC_VER #pragma warning(pop) @@ -114,6 +115,36 @@ namespace libtorrent typedef boost::asio::basic_deadline_timer deadline_timer; #endif + inline std::string print_address(address const& addr) + { + error_code ec; + return addr.to_string(ec); + } + + inline std::string print_endpoint(tcp::endpoint const& ep) + { + error_code ec; + std::string ret; + address const& addr = ep.address(); +#if TORRENT_USE_IPV6 + if (addr.is_v6()) + { + ret += '['; + ret += addr.to_string(ec); + ret += ']'; + ret += ':'; + ret += to_string(ep.port()).elems; + } + else +#endif + { + ret += addr.to_string(ec); + ret += ':'; + ret += to_string(ep.port()).elems; + } + return ret; + } +/* inline std::ostream& print_address(std::ostream& os, address const& addr) { error_code ec; @@ -137,7 +168,7 @@ namespace libtorrent os << ep.port(); return os; } - +*/ namespace detail { template diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 74afa9a73..2436fd0b8 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -37,7 +37,6 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include -#include #ifdef _MSC_VER #pragma warning(push, 1) @@ -204,9 +203,6 @@ namespace libtorrent void second_tick(stat& accumulator, float tick_interval); - // debug purpose only - void print(std::ostream& os) const; - std::string name() const; stat statistics() const { return m_stat; } diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index cba946548..f99caf0f7 100644 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -34,8 +34,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/pch.hpp" #include -#include -#include #include #include @@ -1013,11 +1011,10 @@ namespace libtorrent if (t->valid_metadata() && packet_size() - 1 != (t->torrent_file().num_pieces() + 7) / 8) { - std::stringstream msg; - msg << "got bitfield with invalid size: " << (packet_size() - 1) - << " bytes. expected: " << ((t->torrent_file().num_pieces() + 7) / 8) - << " bytes"; - disconnect(msg.str().c_str(), 2); + char msg[200]; + snprintf(msg, 200, "got bitfield with invalid size: %d bytes. expected: %d bytes" + , int(packet_size()-1), int((t->torrent_file().num_pieces() + 7) / 8)); + disconnect(msg, 2); return; } @@ -1433,9 +1430,9 @@ namespace libtorrent } #endif - std::stringstream msg; - msg << "unknown extended message id: " << extended_id; - disconnect(msg.str().c_str(), 2); + char msg[200]; + snprintf(msg, 200, "unknown extended message id: %d", extended_id); + disconnect(msg, 2); return; } @@ -1558,9 +1555,9 @@ namespace libtorrent #endif m_statistics.received_bytes(0, received); - std::stringstream msg; - msg << "unkown message id: " << packet_type << " size: " << packet_size(); - disconnect(msg.str().c_str(), 2); + char msg[200]; + snprintf(msg, 200, "unknown message message id: %d size: %d", packet_type, packet_size()); + disconnect(msg, 2); return packet_finished(); } @@ -1743,14 +1740,15 @@ namespace libtorrent #ifdef TORRENT_VERBOSE_LOGGING (*m_logger) << time_now_string() << " ==> BITFIELD "; - std::stringstream bitfield_string; + char bitfield_string[1000]; for (int k = 0; k < num_pieces; ++k) { - if (i.begin[k / 8] & (0x80 >> (k % 8))) bitfield_string << "1"; - else bitfield_string << "0"; + if (i.begin[k / 8] & (0x80 >> (k % 8))) bitfield_string[k] = '1'; + else bitfield_string[k] = '0'; } - bitfield_string << "\n"; - (*m_logger) << bitfield_string.str(); + bitfield_string[num_pieces] = '\n'; + bitfield_string[num_pieces + 1] = '\0'; + (*m_logger) << bitfield_string; #endif #ifdef TORRENT_DEBUG m_sent_bitfield = true; @@ -2728,18 +2726,18 @@ namespace libtorrent #ifdef TORRENT_VERBOSE_LOGGING { - peer_id tmp; - std::copy(recv_buffer.begin, recv_buffer.begin + 20, (char*)tmp.begin()); - std::stringstream s; - s << "received peer_id: " << tmp << " client: " << identify_client(tmp) << "\n"; - s << "as ascii: "; - for (peer_id::iterator i = tmp.begin(); i != tmp.end(); ++i) + char hex_pid[41]; + to_hex(recv_buffer.begin, 20, hex_pid); + char ascii_pid[21]; + for (int i = 0; i != 20; ++i) { - if (std::isprint(*i)) s << *i; - else s << "."; + if (isprint(recv_buffer.begin[i])) ascii_pid[i] = recv_buffer.begin[i]; + else ascii_pid[i] = '.'; } - s << "\n"; - (*m_logger) << s.str(); + char msg[200]; + snprintf(msg, 200, "received peer_id: %s client: %s\nas ascii: %s\n" + , hex_pid, identify_client(peer_id(recv_buffer.begin)).c_str(), ascii_pid); + (*m_logger) << msg; } #endif peer_id pid; @@ -2875,9 +2873,9 @@ namespace libtorrent { m_statistics.received_bytes(0, bytes_transferred); // packet too large - std::stringstream msg; - msg << "packet > 1 MB (" << (unsigned int)packet_size << " bytes)"; - disconnect(msg.str().c_str(), 2); + char msg[200]; + printf("packet > 1 MB (%u bytes)", (unsigned int)packet_size); + disconnect(msg, 2); return; } diff --git a/src/entry.cpp b/src/entry.cpp index f6c9a8137..0189f65fb 100644 --- a/src/entry.cpp +++ b/src/entry.cpp @@ -33,8 +33,10 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/pch.hpp" #include -#include +#if defined TORRENT_DEBUG && TORRENT_USE_IOSTREAM #include +#include +#endif #include #include "libtorrent/entry.hpp" #include "libtorrent/config.hpp" @@ -353,6 +355,7 @@ namespace libtorrent TORRENT_ASSERT(false); } +#if defined TORRENT_DEBUG && TORRENT_USE_IOSTREAM void entry::print(std::ostream& os, int indent) const { TORRENT_ASSERT(indent >= 0); @@ -415,5 +418,6 @@ namespace libtorrent os << "\n"; } } +#endif } diff --git a/src/escape_string.cpp b/src/escape_string.cpp index 19231fad7..cb5c104fd 100644 --- a/src/escape_string.cpp +++ b/src/escape_string.cpp @@ -33,12 +33,8 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/pch.hpp" #include -#include -#include -#include #include #include -#include #include #include @@ -54,7 +50,6 @@ POSSIBILITY OF SUCH DAMAGE. #include #endif -#include #include "libtorrent/utf8.hpp" #endif @@ -150,20 +145,21 @@ namespace libtorrent return ret; } + // http://www.ietf.org/rfc/rfc2396.txt + // section 2.3 + // some trackers seems to require that ' is escaped + //static const char unreserved_chars[] = "-_.!~*'()"; + static const char unreserved_chars[] = "/-_.!~*()" + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + "0123456789"; + static const char hex_chars[] = "0123456789abcdef"; + std::string escape_string(const char* str, int len) { TORRENT_ASSERT(str != 0); TORRENT_ASSERT(len >= 0); - // http://www.ietf.org/rfc/rfc2396.txt - // section 2.3 - // some trackers seems to require that ' is escaped -// static const char unreserved_chars[] = "-_.!~*'()"; - static const char unreserved_chars[] = "-_.!~*()" - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" - "0123456789"; - std::stringstream ret; - ret << std::hex << std::setfill('0'); + std::string ret; for (int i = 0; i < len; ++i) { if (std::count( @@ -171,29 +167,25 @@ namespace libtorrent , unreserved_chars+sizeof(unreserved_chars)-1 , *str)) { - ret << *str; + ret += *str; } else { - ret << '%' - << std::setw(2) - << (int)static_cast(*str); + ret += '%'; + ret += hex_chars[((unsigned int)*str) >> 4]; + ret += hex_chars[((unsigned int)*str) & 15]; } ++str; } - return ret.str(); + return ret; } std::string escape_path(const char* str, int len) { TORRENT_ASSERT(str != 0); TORRENT_ASSERT(len >= 0); - static const char unreserved_chars[] = "/-_.!~*()" - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" - "0123456789"; - std::stringstream ret; - ret << std::hex << std::setfill('0'); + std::string ret; for (int i = 0; i < len; ++i) { if (std::count( @@ -201,17 +193,17 @@ namespace libtorrent , unreserved_chars+sizeof(unreserved_chars)-1 , *str)) { - ret << *str; + ret += *str; } else { - ret << '%' - << std::setw(2) - << (int)static_cast(*str); + ret += '%'; + ret += hex_chars[((unsigned int)*str) >> 4]; + ret += hex_chars[((unsigned int)*str) & 15]; } ++str; } - return ret.str(); + return ret; } std::string base64encode(const std::string& s) @@ -400,15 +392,46 @@ namespace libtorrent TORRENT_EXPORT std::string to_hex(std::string const& s) { std::string ret; - char const* digits = "0123456789abcdef"; for (std::string::const_iterator i = s.begin(); i != s.end(); ++i) { - ret += digits[((unsigned char)*i) >> 4]; - ret += digits[((unsigned char)*i) & 0xf]; + ret += hex_chars[((unsigned char)*i) >> 4]; + ret += hex_chars[((unsigned char)*i) & 0xf]; } return ret; } + TORRENT_EXPORT void to_hex(char const *in, int len, char* out) + { + for (char const* end = in + len; in < end; ++in) + { + *out++ = hex_chars[((unsigned char)*in) >> 4]; + *out++ = hex_chars[((unsigned char)*in) & 0xf]; + } + *out = '\0'; + } + + int hex_to_int(char in) + { + if (in >= '0' && in <= '9') return int(in) - '0'; + if (in >= 'A' && in <= 'F') return int(in) - 'A' + 10; + if (in >= 'a' && in <= 'f') return int(in) - 'a' + 10; + return -1; + } + + TORRENT_EXPORT bool from_hex(char const *in, int len, char* out) + { + for (char const* end = in + len; in < end; ++in, ++out) + { + int t = hex_to_int(*in); + if (t == -1) return false; + *out = t << 4; + t = hex_to_int(*in); + if (t == -1) return false; + *out |= t & 15; + } + return true; + } + #if TORRENT_USE_WPATH std::wstring convert_to_wstring(std::string const& s) { diff --git a/src/file_pool.cpp b/src/file_pool.cpp index ae371a93c..916e3ce7e 100644 --- a/src/file_pool.cpp +++ b/src/file_pool.cpp @@ -35,8 +35,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/file_pool.hpp" #include "libtorrent/error_code.hpp" -#include - namespace libtorrent { using boost::multi_index::nth_index; diff --git a/src/http_seed_connection.cpp b/src/http_seed_connection.cpp index dbcdb0601..26f2f5dfd 100644 --- a/src/http_seed_connection.cpp +++ b/src/http_seed_connection.cpp @@ -33,11 +33,8 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/pch.hpp" #include -#include -#include #include #include -#include #include "libtorrent/http_seed_connection.hpp" #include "libtorrent/session.hpp" @@ -355,9 +352,9 @@ namespace libtorrent // add the redirected url and remove the current one t->add_web_seed(location, web_seed_entry::http_seed); t->remove_web_seed(m_url, web_seed_entry::http_seed); - std::stringstream msg; - msg << "redirecting to \"" << location << "\""; - disconnect(msg.str().c_str()); + char msg[200]; + snprintf(msg, 200, "redirecting to \"%s\"", location.c_str()); + disconnect(msg, 2); return; } diff --git a/src/http_tracker_connection.cpp b/src/http_tracker_connection.cpp index c5d0af598..1e435c0fb 100644 --- a/src/http_tracker_connection.cpp +++ b/src/http_tracker_connection.cpp @@ -33,10 +33,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/pch.hpp" #include -#include #include -#include -#include #include #include "libtorrent/config.hpp" @@ -148,9 +145,9 @@ namespace libtorrent } url += "&key="; - std::stringstream key_string; - key_string << std::hex << tracker_req().key; - url += key_string.str(); + char key[200]; + snprintf(key, 200, "%x", tracker_req().key); + url += key; url += "&compact=1"; diff --git a/src/ip_filter.cpp b/src/ip_filter.cpp index eb91de0d0..fa3b70f0e 100644 --- a/src/ip_filter.cpp +++ b/src/ip_filter.cpp @@ -34,7 +34,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/ip_filter.hpp" #include -//#include namespace libtorrent diff --git a/src/kademlia/dht_tracker.cpp b/src/kademlia/dht_tracker.cpp index e4b725be9..5f8fcac96 100644 --- a/src/kademlia/dht_tracker.cpp +++ b/src/kademlia/dht_tracker.cpp @@ -893,7 +893,9 @@ namespace libtorrent { namespace dht ret["nodes"] = nodes; } - ret["node-id"] = boost::lexical_cast(m_dht.nid()); + char node_id[41]; + to_hex((char*)&m_dht.nid()[0], 20, node_id); + ret["node-id"] = node_id; return ret; } diff --git a/src/lazy_bdecode.cpp b/src/lazy_bdecode.cpp index 292f83943..b9f692467 100644 --- a/src/lazy_bdecode.cpp +++ b/src/lazy_bdecode.cpp @@ -32,9 +32,12 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/lazy_entry.hpp" #include "libtorrent/escape_string.hpp" +#include + +#if TORRENT_USE_IOSTREAM #include #include -#include +#endif namespace { @@ -363,6 +366,7 @@ namespace libtorrent return return_t(m_begin, m_end - m_begin); } +#if TORRENT_USE_IOSTREAM std::ostream& operator<<(std::ostream& os, lazy_entry const& e) { switch (e.type()) @@ -431,6 +435,6 @@ namespace libtorrent } return os; } - +#endif // TORRENT_USE_IOSTREAM }; diff --git a/src/logger.cpp b/src/logger.cpp index 154415498..2f7456159 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -52,6 +52,8 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/peer_request.hpp" #include "libtorrent/peer_connection.hpp" +#if TORRENT_USE_IOSTREAM + namespace libtorrent { namespace fs = boost::filesystem; @@ -229,4 +231,5 @@ namespace libtorrent } +#endif diff --git a/src/lsd.cpp b/src/lsd.cpp index 5f5aa9e9e..cb9fe86fe 100644 --- a/src/lsd.cpp +++ b/src/lsd.cpp @@ -83,17 +83,19 @@ void lsd::announce(sha1_hash const& ih, int listen_port) { if (m_disabled) return; - std::stringstream btsearch; - btsearch << "BT-SEARCH * HTTP/1.1\r\n" + char ih_hex[41]; + to_hex((char const*)&ih[0], 20, ih_hex); + char msg[200]; + int msg_len = snprintf(msg, 200, + "BT-SEARCH * HTTP/1.1\r\n" "Host: 239.192.152.143:6771\r\n" - "Port: " << to_string(listen_port).elems << "\r\n" - "Infohash: " << ih << "\r\n" - "\r\n\r\n"; - std::string const& msg = btsearch.str(); + "Port: %d\r\n" + "Infohash: %s\r\n" + "\r\n\r\n", listen_port, ih_hex); m_retry_count = 1; error_code ec; - m_socket.send(msg.c_str(), int(msg.size()), ec); + m_socket.send(msg, msg_len, ec); if (ec) { m_disabled = true; @@ -101,8 +103,9 @@ void lsd::announce(sha1_hash const& ih, int listen_port) } #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) - m_log << time_now_string() - << " ==> announce: ih: " << ih << " port: " << to_string(listen_port).elems << std::endl; + snprintf(msg, 200, "%s ==> announce: ih: %s port: %u\n" + , time_now_string(), ih_hex, listen_port); + m_log << msg; #endif m_broadcast_timer.expires_from_now(milliseconds(250 * m_retry_count), ec); @@ -174,16 +177,16 @@ void lsd::on_announce(udp::endpoint const& from, char* buffer } sha1_hash ih(0); - std::istringstream ih_sstr(ih_str); - ih_sstr >> ih; + from_hex(ih_str.c_str(), 40, (char*)&ih[0]); int port = std::atoi(port_str.c_str()); if (!ih.is_all_zeros() && port != 0) { #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) - m_log << time_now_string() - << " *** incoming local announce " << from.address() - << ":" << to_string(port).elems << " ih: " << ih << std::endl; + char msg[200]; + snprintf(msg, 200, "%s *** incoming local announce %s:%d ih: %s\n" + , time_now_string(), print_address(from.address()).c_str() + , port, ih_str.c_str()); #endif // we got an announce, pass it on through the callback #ifndef BOOST_NO_EXCEPTIONS diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index f1175dd82..d7ad3a5ba 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -33,8 +33,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/pch.hpp" #include -#include -#include #include #include @@ -1395,11 +1393,10 @@ namespace libtorrent if (t->valid_metadata() && (bits.size() + 7) / 8 != (m_have_piece.size() + 7) / 8) { - std::stringstream msg; - msg << "got bitfield with invalid size: " << ((bits.size() + 7) / 8) - << "bytes. expected: " << ((m_have_piece.size() + 7) / 8) - << " bytes"; - disconnect(msg.str().c_str(), 2); + char msg[200]; + snprintf(msg, 200, "got bitfield with invalid size: %d bytes. expected: %d bytes" + , int((bits.size() + 7) / 8), int((m_have_piece.size() + 7) / 8)); + disconnect(msg, 2); return; } diff --git a/src/policy.cpp b/src/policy.cpp index 89d84dc21..73f15805a 100644 --- a/src/policy.cpp +++ b/src/policy.cpp @@ -32,8 +32,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/pch.hpp" -#include - #ifdef _MSC_VER #pragma warning(push, 1) #endif @@ -834,9 +832,12 @@ namespace libtorrent // it again. error_code ec; - m_torrent->debug_log("already connected to peer: " + remote.address().to_string(ec) + ":" - + boost::lexical_cast(remote.port()) + " " - + boost::lexical_cast(i->second.connection->pid())); + char hex_pid[41]; + to_hex((char*)&i->second.connection->pid()[0], 20, hex_pid); + char msg[200]; + snprintf(msg, 200, "already connected to peer: %s %s" + , print_endpoint(remote).c_str(), hex_pid); + m_torrent->debug_log(msg); TORRENT_ASSERT(i->second.connection->associated_torrent().lock().get() == m_torrent); } diff --git a/src/session.cpp b/src/session.cpp index 60b21f691..3dbd2b2cf 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -33,9 +33,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/pch.hpp" #include -#include -#include -#include #include #include #include diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 5984cc69a..22cd4ffde 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -672,10 +672,10 @@ namespace aux { if (m_alerts.should_post()) m_alerts.post_alert(listen_failed_alert(ep, ec)); #if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING) - std::stringstream msg; - msg << "cannot bind to interface '"; - print_endpoint(msg, ep) << "' " << ec.message(); - (*m_logger) << msg.str() << "\n"; + char msg[200]; + snprintf(msg, 200, "cannot bind to interface \"%s\": %s" + , print_endpoint(ep).c_str(), ec.message().c_str()); + (*m_logger) << msg << "\n"; #endif return listen_socket_t(); } @@ -686,10 +686,10 @@ namespace aux { if (m_alerts.should_post()) m_alerts.post_alert(listen_failed_alert(ep, ec)); #if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING) - std::stringstream msg; - msg << "cannot listen on interface '"; - print_endpoint(msg, ep) << "' " << ec.message(); - (*m_logger) << msg.str() << "\n"; + char msg[200]; + snprintf(msg, 200, "cannot listen on interface \"%s\": %s" + , print_endpoint(ep).c_str(), ec.message().c_str()); + (*m_logger) << msg << "\n"; #endif return listen_socket_t(); } diff --git a/src/storage.cpp b/src/storage.cpp index f9e57c95a..f7a0f0ffb 100644 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -45,8 +45,6 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include -#include -#include #include #include #include @@ -72,13 +70,15 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/disk_buffer_holder.hpp" #include "libtorrent/alloca.hpp" +#include + //#define TORRENT_PARTIAL_HASH_LOG -#ifdef TORRENT_DEBUG +#if TORRENT_USE_IOSTREAM +#include #include #include #include -#include #endif #if defined(__APPLE__) @@ -110,7 +110,7 @@ using boost::bind; using namespace ::boost::multi_index; using boost::multi_index::multi_index_container; -#if defined TORRENT_DEBUG && defined TORRENT_STORAGE_DEBUG +#if defined TORRENT_DEBUG && defined TORRENT_STORAGE_DEBUG && TORRENT_USE_IOSTREAM namespace { using namespace libtorrent; @@ -1644,7 +1644,7 @@ ret: // only save the partial hash if the write succeeds if (ret != size) return ret; -#ifdef TORRENT_PARTIAL_HASH_LOG +#if defined TORRENT_PARTIAL_HASH_LOG && TORRENT_USE_IOSTREAM std::ofstream out("partial_hash.log", std::ios::app); #endif @@ -1657,7 +1657,7 @@ ret: for (file::iovec_t* i = iov, *end(iov + num_bufs); i < end; ++i) ph.h.update((char const*)i->iov_base, i->iov_len); -#ifdef TORRENT_PARTIAL_HASH_LOG +#if defined TORRENT_PARTIAL_HASH_LOG && TORRENT_USE_IOSTREAM out << time_now_string() << " NEW [" " s: " << this << " p: " << piece_index @@ -2713,7 +2713,7 @@ ret: && m_slot_to_piece[piece_index] >= 0) { -#if defined TORRENT_DEBUG && defined TORRENT_STORAGE_DEBUG +#if defined TORRENT_DEBUG && defined TORRENT_STORAGE_DEBUG && TORRENT_USE_IOSTREAM std::stringstream s; s << "there is another piece at our slot, swapping.."; @@ -2953,7 +2953,7 @@ ret: } } -#ifdef TORRENT_STORAGE_DEBUG +#ifdef TORRENT_STORAGE_DEBUG && TORRENT_USE_IOSTREAM void piece_manager::debug_log() const { std::stringstream s; diff --git a/src/torrent.cpp b/src/torrent.cpp index d37c6ecb1..22ae75d27 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -33,8 +33,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/pch.hpp" #include -#include -#include #include #include #include @@ -79,6 +77,10 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/assert.hpp" #include "libtorrent/broadcast_socket.hpp" +#if TORRENT_USE_IOSTREAM +#include +#endif + using namespace libtorrent; using boost::tuples::tuple; using boost::tuples::get; @@ -1166,7 +1168,7 @@ namespace libtorrent if (complete >= 0 && incomplete >= 0) m_last_scrape = now; -#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING +#if (defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING) && TORRENT_USE_IOSTREAM std::stringstream s; s << "TRACKER RESPONSE:\n" "interval: " << interval << "\n" @@ -2783,10 +2785,10 @@ namespace libtorrent { if (m_ses.m_alerts.should_post()) { - std::stringstream msg; - msg << "HTTP seed hostname lookup failed: " << e.message(); + char msg[200]; + snprintf(msg, 200, "HTTP seed hostname lookup failed: %s", e.message().c_str()); m_ses.m_alerts.post_alert( - url_seed_alert(get_handle(), web.url, msg.str())); + url_seed_alert(get_handle(), web.url, msg)); } #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING (*m_ses.m_logger) << " ** HOSTNAME LOOKUP FAILED!**: " << web.url << "\n"; diff --git a/src/torrent_handle.cpp b/src/torrent_handle.cpp index c20495352..5f96095be 100644 --- a/src/torrent_handle.cpp +++ b/src/torrent_handle.cpp @@ -33,9 +33,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/pch.hpp" #include -#include -#include -#include #include #include #include diff --git a/src/torrent_info.cpp b/src/torrent_info.cpp index 8bf6ebb9f..b69efd046 100644 --- a/src/torrent_info.cpp +++ b/src/torrent_info.cpp @@ -33,9 +33,12 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/pch.hpp" #include + +#if !defined TORRENT_NO_DEPRECATE && TORRENT_USE_IOSTREAM #include -#include #include +#endif + #include #include #include @@ -903,7 +906,7 @@ namespace libtorrent < bind(&announce_entry::tier, _2)); } -#ifndef TORRENT_NO_DEPRECATE +#if !defined TORRENT_NO_DEPRECATE && TORRENT_USE_IOSTREAM // ------- start deprecation ------- void torrent_info::print(std::ostream& os) const diff --git a/src/tracker_manager.cpp b/src/tracker_manager.cpp index e4ddf4b81..318d2dbb7 100644 --- a/src/tracker_manager.cpp +++ b/src/tracker_manager.cpp @@ -33,10 +33,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/pch.hpp" #include -#include #include -#include -#include #include diff --git a/src/udp_tracker_connection.cpp b/src/udp_tracker_connection.cpp index 97ef15220..294fbd71b 100644 --- a/src/udp_tracker_connection.cpp +++ b/src/udp_tracker_connection.cpp @@ -33,10 +33,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/pch.hpp" #include -#include #include -#include -#include #include "zlib.h" @@ -46,11 +43,6 @@ POSSIBILITY OF SUCH DAMAGE. #include -#if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING -#include -using boost::lexical_cast; -#endif - #ifdef _MSC_VER #pragma warning(pop) #endif @@ -195,9 +187,9 @@ namespace libtorrent { #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING || defined TORRENT_ERROR_LOGGING boost::shared_ptr cb = requester(); - std::stringstream msg; - msg << "*** UDP_TRACKER [ timed out url: " << tracker_req().url << " ]"; - if (cb) cb->debug_log(msg.str().c_str()); + char msg[200]; + snprintf(msg, 200, "*** UDP_TRACKER [ timed out url: %s ]", tracker_req().url.c_str()); + if (cb) cb->debug_log(msg); #endif m_socket.close(); m_name_lookup.cancel(); @@ -230,9 +222,9 @@ namespace libtorrent boost::shared_ptr cb = requester(); if (cb) { - std::stringstream msg; - msg << "<== UDP_TRACKER_PACKET [ size: " << size << " ]"; - cb->debug_log(msg.str()); + char msg[200]; + snprintf(msg, 200, "<== UDP_TRACKER_PACKET [ size: %d ]", size); + cb->debug_log(msg); } #endif @@ -248,9 +240,9 @@ namespace libtorrent #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING if (cb) { - std::stringstream msg; - msg << "*** UDP_TRACKER_PACKET [ acton: " << action << " ]"; - cb->debug_log(msg.str()); + char msg[200]; + snprintf(msg, 200, "*** UDP_TRACKER_PACKET [ action: %d ]", action); + cb->debug_log(msg); } #endif @@ -269,9 +261,10 @@ namespace libtorrent #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING if (cb) { - std::stringstream msg; - msg << "*** UDP_TRACKER_RESPONSE [ cid: " << m_connection_id << " ]"; - cb->debug_log(msg.str()); + char msg[200]; + snprintf(msg, 200, "*** UDP_TRACKER_RESPONSE [ cid: %x%x ]" + , int(m_connection_id >> 32), int(m_connection_id & 0xffffffff)); + cb->debug_log(msg); } #endif @@ -315,8 +308,11 @@ namespace libtorrent boost::shared_ptr cb = requester(); if (cb) { - cb->debug_log("==> UDP_TRACKER_CONNECT [" - + lexical_cast(tracker_req().info_hash) + "]"); + char hex_ih[41]; + to_hex((char const*)&tracker_req().info_hash[0], 20, hex_ih); + char msg[200]; + snprintf(msg, 200, "==> UDP_TRACKER_CONNECT [%s]", hex_ih); + cb->debug_log(msg); } #endif if (!m_socket.is_open()) return; // the operation was aborted @@ -398,9 +394,9 @@ namespace libtorrent if (cb) { boost::shared_ptr cb = requester(); - std::stringstream msg; - msg << "<== UDP_ANNOUNCE_RESPONSE [ url: " << tracker_req().url << " ]"; - cb->debug_log(msg.str().c_str()); + char msg[200]; + snprintf(msg, 200, "<== UDP_TRACKER_RESPONSE [ url: %s ]", tracker_req().url.c_str()); + cb->debug_log(msg); } #endif @@ -415,12 +411,13 @@ namespace libtorrent { // TODO: don't use a string here peer_entry e; - std::stringstream s; - s << (int)detail::read_uint8(buf) << "."; - s << (int)detail::read_uint8(buf) << "."; - s << (int)detail::read_uint8(buf) << "."; - s << (int)detail::read_uint8(buf); - e.ip = s.str(); + char ip_string[100]; + unsigned int a = detail::read_uint8(buf); + unsigned int b = detail::read_uint8(buf); + unsigned int c = detail::read_uint8(buf); + unsigned int d = detail::read_uint8(buf); + snprintf(ip_string, 100, "%u.%u.%u.%u", a, b, c, d); + e.ip = ip_string; e.port = detail::read_uint16(buf); e.pid.clear(); peer_list.push_back(e); @@ -521,11 +518,11 @@ namespace libtorrent #if defined TORRENT_VERBOSE_LOGGING || defined TORRENT_LOGGING boost::shared_ptr cb = requester(); - if (cb) - { - cb->debug_log("==> UDP_TRACKER_ANNOUNCE [" - + lexical_cast(req.info_hash) + "]"); - } + char hex_ih[41]; + to_hex((char const*)&req.info_hash[0], 20, hex_ih); + char msg[200]; + snprintf(msg, 200, "==> UDP_TRACKER_ANNOUNCE [%s]", hex_ih); + cb->debug_log(msg); #endif error_code ec; diff --git a/src/web_peer_connection.cpp b/src/web_peer_connection.cpp index 4cb69f684..daed1690f 100644 --- a/src/web_peer_connection.cpp +++ b/src/web_peer_connection.cpp @@ -33,11 +33,8 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/pch.hpp" #include -#include -#include #include #include -#include #include #include "libtorrent/web_peer_connection.hpp" @@ -448,19 +445,19 @@ namespace libtorrent if (i == std::string::npos) { t->remove_web_seed(m_url, web_seed_entry::url_seed); - std::stringstream msg; - msg << "got invalid HTTP redirection location (\"" << location << "\") " - "expected it to end with: " << path; - disconnect(msg.str().c_str(), 2); + char msg[200]; + snprintf(msg, 200, "got invalid HTTP redirection location (\"%s\") " + "expected it to end with: %s", location.c_str(), path.c_str()); + disconnect(msg, 2); return; } location.resize(i); } t->add_web_seed(location, web_seed_entry::url_seed); t->remove_web_seed(m_url, web_seed_entry::url_seed); - std::stringstream msg; - msg << "redirecting to \"" << location << "\""; - disconnect(msg.str().c_str()); + char msg[200]; + snprintf(msg, 200, "redirecting to \"%s\"", location.c_str()); + disconnect(msg, 2); return; } @@ -488,17 +485,29 @@ namespace libtorrent size_type range_end; if (m_parser.status_code() == 206) { - std::stringstream range_str(m_parser.header("content-range")); - char dummy; - std::string bytes; - range_str >> bytes >> range_start >> dummy >> range_end; - if (!range_str) + std::string const& range_str = m_parser.header("content-range"); + + bool success = true; + char const* ptr = range_str.c_str(); + char* end; + range_start = strtoll(ptr, &end, 10); + if (end == ptr) success = false; + else if (*end != '-') success = false; + else + { + ptr = end + 1; + range_end = strtoll(ptr, &end, 10); + if (end == ptr) success = false; + } + + if (!success) { // we should not try this server again. t->remove_web_seed(m_url, web_seed_entry::url_seed); - std::stringstream msg; - msg << "invalid range in HTTP response: " << range_str.str(); - disconnect(msg.str().c_str(), 2); + char msg[200]; + snprintf(msg, 200, "invalid range in HTTP response: %s" + , range_str.c_str()); + disconnect(msg, 2); return; } // the http range is inclusive