merged local service discovery logging from RC_0_16

This commit is contained in:
Arvid Norberg 2012-10-09 04:16:37 +00:00
parent 9ed60479ce
commit 971aea19e7
7 changed files with 76 additions and 45 deletions

View File

@ -66,10 +66,12 @@ namespace libtorrent
class TORRENT_EXTRA_EXPORT broadcast_socket class TORRENT_EXTRA_EXPORT broadcast_socket
{ {
public: public:
broadcast_socket(io_service& ios, udp::endpoint const& multicast_endpoint broadcast_socket(udp::endpoint const& multicast_endpoint
, receive_handler_t const& handler, bool loopback = true); , receive_handler_t const& handler);
~broadcast_socket() { close(); } ~broadcast_socket() { close(); }
void open(io_service& ios, error_code& ec, bool loopback = true);
enum flags_t { broadcast = 1 }; enum flags_t { broadcast = 1 };
void send(char const* buffer, int size, error_code& ec, int flags = 0); void send(char const* buffer, int size, error_code& ec, int flags = 0);

View File

@ -86,7 +86,7 @@ private:
bool m_disabled; bool m_disabled;
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
std::ofstream m_log; FILE* m_log;
#endif #endif
}; };

View File

@ -48,8 +48,8 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/debug.hpp" #include "libtorrent/debug.hpp"
#endif #endif
#ifndef NDEBUG #ifdef TORRENT_DEBUG
//#include "libtorrent/socket_io.hpp" #include "libtorrent/socket_io.hpp"
#endif #endif
#if BOOST_VERSION < 103500 #if BOOST_VERSION < 103500
@ -216,10 +216,9 @@ namespace libtorrent
#endif #endif
} }
broadcast_socket::broadcast_socket(io_service& ios broadcast_socket::broadcast_socket(
, udp::endpoint const& multicast_endpoint udp::endpoint const& multicast_endpoint
, receive_handler_t const& handler , receive_handler_t const& handler)
, bool loopback)
: m_multicast_endpoint(multicast_endpoint) : m_multicast_endpoint(multicast_endpoint)
, m_on_receive(handler) , m_on_receive(handler)
, m_outstanding_operations(0) , m_outstanding_operations(0)
@ -228,12 +227,14 @@ namespace libtorrent
TORRENT_ASSERT(is_multicast(m_multicast_endpoint.address())); TORRENT_ASSERT(is_multicast(m_multicast_endpoint.address()));
using namespace asio::ip::multicast; using namespace asio::ip::multicast;
}
error_code ec;
void broadcast_socket::open(io_service& ios, error_code& ec, bool loopback)
{
std::vector<ip_interface> interfaces = enum_net_interfaces(ios, ec); std::vector<ip_interface> interfaces = enum_net_interfaces(ios, ec);
#if TORRENT_USE_IPV6 #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); open_multicast_socket(ios, address_v6::any(), loopback, ec);
else else
#endif #endif
@ -243,16 +244,16 @@ namespace libtorrent
, end(interfaces.end()); i != end; ++i) , end(interfaces.end()); i != end; ++i)
{ {
// only multicast on compatible networks // 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 // ignore any loopback interface
if (!loopback && is_loopback(i->interface_address)) continue; if (!loopback && is_loopback(i->interface_address)) continue;
ec = error_code(); ec = error_code();
open_multicast_socket(ios, i->interface_address, loopback, ec); 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" // fprintf(stderr, "broadcast socket [ if: %s group: %s mask: %s ] %s\n"
// , i->interface_address.to_string().c_str() // , 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() // , i->netmask.to_string().c_str()
// , ec.message().c_str()); // , ec.message().c_str());
#endif #endif
@ -331,13 +332,13 @@ namespace libtorrent
i->socket->send_to(asio::buffer(buffer, size) i->socket->send_to(asio::buffer(buffer, size)
, udp::endpoint(i->broadcast_address(), m_multicast_endpoint.port()), 0, e); , 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() // 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()); // , print_endpoint(m_multicast_endpoint).c_str());
#endif #endif
if (e) if (e)
{ {
#ifndef NDEBUG #ifdef TORRENT_DEBUG
// fprintf(stderr, " ERROR: %s\n", e.message().c_str()); // fprintf(stderr, " ERROR: %s\n", e.message().c_str());
#endif #endif
i->socket->close(e); i->socket->close(e);
@ -351,7 +352,7 @@ namespace libtorrent
if (!i->socket) continue; if (!i->socket) continue;
error_code e; error_code e;
i->socket->send_to(asio::buffer(buffer, size), m_multicast_endpoint, 0, 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_address(address const& addr);
// extern std::string print_endpoint(udp::endpoint const& ep); // 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() // 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 #endif
if (e) if (e)
{ {
#ifndef NDEBUG #ifdef TORRENT_DEBUG
// fprintf(stderr, " ERROR: %s\n", e.message().c_str()); // fprintf(stderr, " ERROR: %s\n", e.message().c_str());
#endif #endif
i->socket->close(e); i->socket->close(e);

View File

@ -70,17 +70,38 @@ lsd::lsd(io_service& ios, address const& listen_interface
, peer_callback_t const& cb) , peer_callback_t const& cb)
: m_callback(cb) : m_callback(cb)
, m_retry_count(1) , 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)) , boost::bind(&lsd::on_announce, self(), _1, _2, _3))
, m_broadcast_timer(ios) , m_broadcast_timer(ios)
, m_disabled(false) , m_disabled(false)
{ {
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) #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 #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) 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" "Infohash: %s\r\n"
"\r\n\r\n", listen_port, ih_hex); "\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; m_retry_count = 1;
error_code ec; error_code ec;
m_socket.send(msg, msg_len, ec, broadcast ? broadcast_socket::broadcast : 0); m_socket.send(msg, msg_len, ec, broadcast ? broadcast_socket::broadcast : 0);
if (ec) if (ec)
{ {
m_disabled = true; 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; 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 #if defined TORRENT_ASIO_DEBUGGING
add_outstanding_async("lsd::resend_announce"); add_outstanding_async("lsd::resend_announce");
#endif #endif
@ -156,8 +182,7 @@ void lsd::on_announce(udp::endpoint const& from, char* buffer
if (!p.header_finished() || error) if (!p.header_finished() || error)
{ {
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
m_log << time_now_string() if (m_log) fprintf(m_log, "%s <== announce: incomplete HTTP message\n", time_now_string());
<< " <== announce: incomplete HTTP message" << std::endl;
#endif #endif
return; return;
} }
@ -165,8 +190,8 @@ void lsd::on_announce(udp::endpoint const& from, char* buffer
if (p.method() != "bt-search") if (p.method() != "bt-search")
{ {
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
m_log << time_now_string() if (m_log) fprintf(m_log, "%s <== announce: invalid HTTP method: %s\n"
<< " <== announce: invalid HTTP method: " << p.method() << std::endl; , time_now_string(), p.method().c_str());
#endif #endif
return; return;
} }
@ -175,8 +200,8 @@ void lsd::on_announce(udp::endpoint const& from, char* buffer
if (port_str.empty()) if (port_str.empty())
{ {
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
m_log << time_now_string() if (m_log) fprintf(m_log, "%s <== announce: invalid BT-SEARCH, missing port\n"
<< " <== announce: invalid BT-SEARCH, missing port" << std::endl; , time_now_string());
#endif #endif
return; return;
} }
@ -194,9 +219,8 @@ void lsd::on_announce(udp::endpoint const& from, char* buffer
if (ih_str.size() != 40) if (ih_str.size() != 40)
{ {
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
m_log << time_now_string() if (m_log) fprintf(m_log, "%s <== announce: invalid BT-SEARCH, invalid infohash: %s\n"
<< " <== announce: invalid BT-SEARCH, invalid infohash: " , time_now_string(), ih_str.c_str());
<< ih_str << std::endl;
#endif #endif
continue; continue;
} }
@ -207,8 +231,7 @@ void lsd::on_announce(udp::endpoint const& from, char* buffer
if (!ih.is_all_zeros() && port != 0) if (!ih.is_all_zeros() && port != 0)
{ {
#if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
char msg[200]; if (m_log) fprintf(m_log, "%s *** incoming local announce %s:%d ih: %s\n"
snprintf(msg, 200, "%s *** incoming local announce %s:%d ih: %s\n"
, time_now_string(), print_address(from.address()).c_str() , time_now_string(), print_address(from.address()).c_str()
, port, ih_str.c_str()); , port, ih_str.c_str());
#endif #endif

View File

@ -5239,7 +5239,7 @@ retry:
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING) #if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
(*m_logger) << time_now_string() (*m_logger) << time_now_string()
<< ": added peer from local discovery: " << peer << "\n"; << ": added peer from local discovery: " << print_endpoint(peer) << "\n";
#endif #endif
t->get_policy().add_peer(peer, peer_id(0), peer_info::lsd, 0); t->get_policy().add_peer(peer, peer_id(0), peer_info::lsd, 0);
if (m_alerts.should_post<lsd_peer_alert>()) if (m_alerts.should_post<lsd_peer_alert>())

View File

@ -71,7 +71,7 @@ upnp::upnp(io_service& ios, connection_queue& cc
, m_log_callback(lcb) , m_log_callback(lcb)
, m_retry_count(0) , m_retry_count(0)
, m_io_service(ios) , 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)) , boost::bind(&upnp::on_reply, self(), _1, _2, _3))
, m_broadcast_timer(ios) , m_broadcast_timer(ios)
, m_refresh_timer(ios) , m_refresh_timer(ios)
@ -82,6 +82,9 @@ upnp::upnp(io_service& ios, connection_queue& cc
{ {
TORRENT_ASSERT(cb); TORRENT_ASSERT(cb);
error_code ec;
m_socket.open(ios, ec);
if (state) if (state)
{ {
upnp_state_t* s = (upnp_state_t*)state; upnp_state_t* s = (upnp_state_t*)state;

View File

@ -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.write(soap_add_response, sizeof(soap_add_response)-1);
xml.close(); 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); , &incoming_msearch);
sock->open(ios, ec);
std::string user_agent = "test agent"; std::string user_agent = "test agent";
connection_queue cc(ios); connection_queue cc(ios);