diff --git a/include/libtorrent/broadcast_socket.hpp b/include/libtorrent/broadcast_socket.hpp index fb3e1d238..ce7fe23e5 100644 --- a/include/libtorrent/broadcast_socket.hpp +++ b/include/libtorrent/broadcast_socket.hpp @@ -66,10 +66,12 @@ namespace libtorrent class TORRENT_EXTRA_EXPORT broadcast_socket { public: - broadcast_socket(io_service& ios, udp::endpoint const& multicast_endpoint - , receive_handler_t const& handler, bool loopback = true); + broadcast_socket(udp::endpoint const& multicast_endpoint + , receive_handler_t const& handler); ~broadcast_socket() { close(); } + void open(io_service& ios, error_code& ec, bool loopback = true); + enum flags_t { broadcast = 1 }; void send(char const* buffer, int size, error_code& ec, int flags = 0); diff --git a/include/libtorrent/lsd.hpp b/include/libtorrent/lsd.hpp index bfdbb68ea..b2ebd9361 100644 --- a/include/libtorrent/lsd.hpp +++ b/include/libtorrent/lsd.hpp @@ -86,7 +86,7 @@ private: bool m_disabled; #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) - std::ofstream m_log; + FILE* m_log; #endif }; diff --git a/src/broadcast_socket.cpp b/src/broadcast_socket.cpp index 140fc7513..c23fa949d 100644 --- a/src/broadcast_socket.cpp +++ b/src/broadcast_socket.cpp @@ -48,8 +48,8 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/debug.hpp" #endif -#ifndef NDEBUG -//#include "libtorrent/socket_io.hpp" +#ifdef TORRENT_DEBUG +#include "libtorrent/socket_io.hpp" #endif #if BOOST_VERSION < 103500 @@ -216,10 +216,9 @@ namespace libtorrent #endif } - broadcast_socket::broadcast_socket(io_service& ios - , udp::endpoint const& multicast_endpoint - , receive_handler_t const& handler - , bool loopback) + broadcast_socket::broadcast_socket( + udp::endpoint const& multicast_endpoint + , receive_handler_t const& handler) : m_multicast_endpoint(multicast_endpoint) , m_on_receive(handler) , m_outstanding_operations(0) @@ -228,12 +227,14 @@ namespace libtorrent TORRENT_ASSERT(is_multicast(m_multicast_endpoint.address())); using namespace asio::ip::multicast; - - error_code ec; + } + + void broadcast_socket::open(io_service& ios, error_code& ec, bool loopback) + { std::vector interfaces = enum_net_interfaces(ios, ec); #if TORRENT_USE_IPV6 - if (multicast_endpoint.address().is_v6()) + if (m_multicast_endpoint.address().is_v6()) open_multicast_socket(ios, address_v6::any(), loopback, ec); else #endif @@ -243,16 +244,16 @@ namespace libtorrent , end(interfaces.end()); i != end; ++i) { // only multicast on compatible networks - if (i->interface_address.is_v4() != multicast_endpoint.address().is_v4()) continue; + if (i->interface_address.is_v4() != m_multicast_endpoint.address().is_v4()) continue; // ignore any loopback interface if (!loopback && is_loopback(i->interface_address)) continue; ec = error_code(); open_multicast_socket(ios, i->interface_address, loopback, ec); -#ifndef NDEBUG +#ifdef TORRENT_DEBUG // fprintf(stderr, "broadcast socket [ if: %s group: %s mask: %s ] %s\n" // , i->interface_address.to_string().c_str() -// , multicast_endpoint.address().to_string().c_str() +// , m_multicast_endpoint.address().to_string().c_str() // , i->netmask.to_string().c_str() // , ec.message().c_str()); #endif @@ -331,13 +332,13 @@ namespace libtorrent i->socket->send_to(asio::buffer(buffer, size) , udp::endpoint(i->broadcast_address(), m_multicast_endpoint.port()), 0, e); -#ifndef NDEBUG +#ifdef TORRENT_DEBUG // fprintf(stderr, " sending on unicast %s to: %s\n", print_address(i->socket->local_endpoint().address()).c_str() // , print_endpoint(m_multicast_endpoint).c_str()); #endif if (e) { -#ifndef NDEBUG +#ifdef TORRENT_DEBUG // fprintf(stderr, " ERROR: %s\n", e.message().c_str()); #endif i->socket->close(e); @@ -351,7 +352,7 @@ namespace libtorrent if (!i->socket) continue; error_code e; i->socket->send_to(asio::buffer(buffer, size), m_multicast_endpoint, 0, e); -#ifndef NDEBUG +#ifdef TORRENT_DEBUG // extern std::string print_address(address const& addr); // extern std::string print_endpoint(udp::endpoint const& ep); // fprintf(stderr, " sending on multicast %s to: %s\n", print_address(i->socket->local_endpoint().address()).c_str() @@ -359,7 +360,7 @@ namespace libtorrent #endif if (e) { -#ifndef NDEBUG +#ifdef TORRENT_DEBUG // fprintf(stderr, " ERROR: %s\n", e.message().c_str()); #endif i->socket->close(e); diff --git a/src/lsd.cpp b/src/lsd.cpp index af4966ae9..d2751d53c 100644 --- a/src/lsd.cpp +++ b/src/lsd.cpp @@ -70,17 +70,38 @@ lsd::lsd(io_service& ios, address const& listen_interface , peer_callback_t const& cb) : m_callback(cb) , m_retry_count(1) - , m_socket(ios, udp::endpoint(address_v4::from_string("239.192.152.143", ec), 6771) + , m_socket(udp::endpoint(address_v4::from_string("239.192.152.143", ec), 6771) , boost::bind(&lsd::on_announce, self(), _1, _2, _3)) , m_broadcast_timer(ios) , m_disabled(false) { #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) - m_log.open("lsd.log", std::ios::in | std::ios::out | std::ios::trunc); + m_log = fopen("lsd.log", "w+"); + if (m_log == NULL) + { + fprintf(stderr, "failed to open 'lsd.log': (%d) %s" + , errno, strerror(errno)); + } +#endif + + error_code ec; + m_socket.open(ios, ec); + +#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) + if (ec) + { + if (m_log) fprintf(m_log, "FAILED TO OPEN SOCKET: (%d) %s\n" + , ec.value(), ec.message().c_str()); + } #endif } -lsd::~lsd() {} +lsd::~lsd() +{ +#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) + if (m_log) fclose(m_log); +#endif +} void lsd::announce(sha1_hash const& ih, int listen_port, bool broadcast) { @@ -96,24 +117,29 @@ void lsd::announce(sha1_hash const& ih, int listen_port, bool broadcast) "Infohash: %s\r\n" "\r\n\r\n", listen_port, ih_hex); +#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) + { + if (m_log) fprintf(m_log, "%s ==> announce: ih: %s port: %u\n" + , time_now_string(), ih_hex, listen_port); + } +#endif + m_retry_count = 1; error_code ec; m_socket.send(msg, msg_len, ec, broadcast ? broadcast_socket::broadcast : 0); if (ec) { m_disabled = true; +#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) + { + if (m_log) fprintf(m_log, "%s failed to send message: (%d) %s" + , time_now_string(), ec.value(), ec.message().c_str()); + } +#endif + return; } -#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) - { - char msg[200]; - snprintf(msg, sizeof(msg), "%s ==> announce: ih: %s port: %u" - , time_now_string(), ih_hex, listen_port); - m_log << msg << std::endl; - } -#endif - #if defined TORRENT_ASIO_DEBUGGING add_outstanding_async("lsd::resend_announce"); #endif @@ -156,8 +182,7 @@ void lsd::on_announce(udp::endpoint const& from, char* buffer if (!p.header_finished() || error) { #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) - m_log << time_now_string() - << " <== announce: incomplete HTTP message" << std::endl; + if (m_log) fprintf(m_log, "%s <== announce: incomplete HTTP message\n", time_now_string()); #endif return; } @@ -165,8 +190,8 @@ void lsd::on_announce(udp::endpoint const& from, char* buffer if (p.method() != "bt-search") { #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) - m_log << time_now_string() - << " <== announce: invalid HTTP method: " << p.method() << std::endl; + if (m_log) fprintf(m_log, "%s <== announce: invalid HTTP method: %s\n" + , time_now_string(), p.method().c_str()); #endif return; } @@ -175,8 +200,8 @@ void lsd::on_announce(udp::endpoint const& from, char* buffer if (port_str.empty()) { #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) - m_log << time_now_string() - << " <== announce: invalid BT-SEARCH, missing port" << std::endl; + if (m_log) fprintf(m_log, "%s <== announce: invalid BT-SEARCH, missing port\n" + , time_now_string()); #endif return; } @@ -194,9 +219,8 @@ void lsd::on_announce(udp::endpoint const& from, char* buffer if (ih_str.size() != 40) { #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) - m_log << time_now_string() - << " <== announce: invalid BT-SEARCH, invalid infohash: " - << ih_str << std::endl; + if (m_log) fprintf(m_log, "%s <== announce: invalid BT-SEARCH, invalid infohash: %s\n" + , time_now_string(), ih_str.c_str()); #endif continue; } @@ -207,8 +231,7 @@ void lsd::on_announce(udp::endpoint const& from, char* buffer if (!ih.is_all_zeros() && port != 0) { #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) - char msg[200]; - snprintf(msg, 200, "%s *** incoming local announce %s:%d ih: %s\n" + if (m_log) fprintf(m_log, "%s *** incoming local announce %s:%d ih: %s\n" , time_now_string(), print_address(from.address()).c_str() , port, ih_str.c_str()); #endif diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 88a7df04a..e720f9425 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -5239,7 +5239,7 @@ retry: #if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING) (*m_logger) << time_now_string() - << ": added peer from local discovery: " << peer << "\n"; + << ": added peer from local discovery: " << print_endpoint(peer) << "\n"; #endif t->get_policy().add_peer(peer, peer_id(0), peer_info::lsd, 0); if (m_alerts.should_post()) diff --git a/src/upnp.cpp b/src/upnp.cpp index 7099d2241..c9f3da9a6 100644 --- a/src/upnp.cpp +++ b/src/upnp.cpp @@ -71,7 +71,7 @@ upnp::upnp(io_service& ios, connection_queue& cc , m_log_callback(lcb) , m_retry_count(0) , m_io_service(ios) - , m_socket(ios, udp::endpoint(address_v4::from_string("239.255.255.250", ec), 1900) + , m_socket(udp::endpoint(address_v4::from_string("239.255.255.250", ec), 1900) , boost::bind(&upnp::on_reply, self(), _1, _2, _3)) , m_broadcast_timer(ios) , m_refresh_timer(ios) @@ -82,6 +82,9 @@ upnp::upnp(io_service& ios, connection_queue& cc { TORRENT_ASSERT(cb); + error_code ec; + m_socket.open(ios, ec); + if (state) { upnp_state_t* s = (upnp_state_t*)state; diff --git a/test/test_upnp.cpp b/test/test_upnp.cpp index 255030857..5c273cb73 100644 --- a/test/test_upnp.cpp +++ b/test/test_upnp.cpp @@ -140,9 +140,11 @@ int run_upnp_test(char const* root_filename, char const* router_model, char cons xml.write(soap_add_response, sizeof(soap_add_response)-1); xml.close(); - sock = new broadcast_socket(ios, udp::endpoint(address_v4::from_string("239.255.255.250"), 1900) + sock = new broadcast_socket(udp::endpoint(address_v4::from_string("239.255.255.250"), 1900) , &incoming_msearch); + sock->open(ios, ec); + std::string user_agent = "test agent"; connection_queue cc(ios);