From d1d879c27c96cdca3a6433734327e60c09d52423 Mon Sep 17 00:00:00 2001 From: Naim A Date: Fri, 5 Feb 2016 03:56:49 +0200 Subject: [PATCH] WIP: Added some logging to tracker --- src/multiplatform.h | 1 + src/tracker.cpp | 25 ++++--- src/tracker.hpp | 6 +- src/udpTracker.cpp | 170 +++++++++++++++++++++++--------------------- src/udpTracker.hpp | 13 +++- 5 files changed, 122 insertions(+), 93 deletions(-) diff --git a/src/multiplatform.h b/src/multiplatform.h index d837c9f..ab254b2 100644 --- a/src/multiplatform.h +++ b/src/multiplatform.h @@ -32,6 +32,7 @@ #ifdef WIN32 #include +#include #include #elif defined (linux) #include diff --git a/src/tracker.cpp b/src/tracker.cpp index 0aa7014..f0d9abe 100644 --- a/src/tracker.cpp +++ b/src/tracker.cpp @@ -20,18 +20,19 @@ namespace UDPT { - Tracker::Tracker() + Tracker::Tracker() : m_logger(boost::log::keywords::channel = "TRACKER") { - + BOOST_LOG_SEV(m_logger, boost::log::trivial::debug) << "Initialized Tracker"; } Tracker::~Tracker() { - + BOOST_LOG_SEV(m_logger, boost::log::trivial::debug) << "Destroying Tracker..."; } void Tracker::stop() - { + { + BOOST_LOG_SEV(m_logger, boost::log::trivial::info) << "Requesting Tracker to terminate."; m_udpTracker->stop(); wait(); @@ -41,22 +42,26 @@ namespace UDPT } void Tracker::wait() - { - m_udpTracker->wait(); + { + m_udpTracker->wait(); + BOOST_LOG_SEV(m_logger, boost::log::trivial::info) << "Tracker terminated."; } void Tracker::start(const boost::program_options::variables_map& conf) - { - m_udpTracker = std::shared_ptr(new UDPTracker(conf)); + { + BOOST_LOG_SEV(m_logger, boost::log::trivial::debug) << "Starting Tracker..."; + m_udpTracker = std::shared_ptr(new UDPTracker(conf)); if (conf["apiserver.enable"].as()) - { + { + BOOST_LOG_SEV(m_logger, boost::log::trivial::info) << "Initializing and deploying WebAPI..."; m_apiSrv = std::shared_ptr(new UDPT::Server::HTTPServer(conf)); m_webApp = std::shared_ptr(new UDPT::Server::WebApp(m_apiSrv, m_udpTracker->m_conn.get(), conf)); m_webApp->deploy(); } - m_udpTracker->start(); + m_udpTracker->start(); + BOOST_LOG_SEV(m_logger, boost::log::trivial::info) << "Tracker started."; } Tracker& Tracker::getInstance() diff --git a/src/tracker.hpp b/src/tracker.hpp index 4178477..2ff169f 100644 --- a/src/tracker.hpp +++ b/src/tracker.hpp @@ -21,6 +21,9 @@ #include #include +#include +#include + #include "multiplatform.h" #include "udpTracker.hpp" #include "http/httpserver.hpp" @@ -46,7 +49,8 @@ namespace UDPT std::shared_ptr m_udpTracker; std::shared_ptr m_apiSrv; std::shared_ptr m_webApp; - + boost::log::sources::severity_channel_logger_mt<> m_logger; + Tracker(); }; } diff --git a/src/udpTracker.cpp b/src/udpTracker.cpp index 650e456..2d6c976 100644 --- a/src/udpTracker.cpp +++ b/src/udpTracker.cpp @@ -17,15 +17,7 @@ * along with UDPT. If not, see . */ -#include // atoi -#include -#include -#include -#include -#include #include "udpTracker.hpp" -#include "tools.h" -#include "multiplatform.h" using namespace UDPT::Data; @@ -34,7 +26,7 @@ using namespace UDPT::Data; namespace UDPT { - UDPTracker::UDPTracker(const boost::program_options::variables_map& conf) : m_conf(conf) + UDPTracker::UDPTracker(const boost::program_options::variables_map& conf) : m_conf(conf), m_logger(boost::log::keywords::channel = "UDPTracker") { this->m_allowRemotes = conf["tracker.allow_remotes"].as(); this->m_allowIANA_IPs = conf["tracker.allow_iana_ips"].as(); @@ -67,7 +59,6 @@ namespace UDPT void UDPTracker::start() { - std::stringstream ss; SOCKET sock; int r, // saves results i, // loop index @@ -105,15 +96,12 @@ namespace UDPT ::closesocket(sock); #elif defined (linux) ::close(sock); - #endif +#endif throw UDPT::UDPTException("Failed to bind socket."); } this->m_sock = sock; - ss.str(""); - ss << "Starting maintenance thread (1/" << ((int)this->m_threadCount) << ")"; - // create maintainer thread. m_threads.push_back(boost::thread(UDPTracker::_maintainance_start, this)); @@ -132,6 +120,7 @@ namespace UDPT ::closesocket(m_sock); #endif + BOOST_LOG_SEV(m_logger, boost::log::trivial::warning) << "Interrupting workers..."; for (std::vector::iterator it = m_threads.begin(); it != m_threads.end(); ++it) { it->interrupt(); @@ -142,6 +131,8 @@ namespace UDPT void UDPTracker::wait() { + BOOST_LOG_SEV(m_logger, boost::log::trivial::warning) << "Waiting for threads to terminate..."; + for (std::vector::iterator it = m_threads.begin(); it != m_threads.end(); ++it) { it->join(); @@ -193,38 +184,43 @@ namespace UDPT ::sendto(usi->m_sock, (char*)&resp, sizeof(ConnectionResponse), 0, (SOCKADDR*)remote, sizeof(SOCKADDR_IN)); + { + char buffer[INET_ADDRSTRLEN]; + BOOST_LOG_SEV(usi->m_logger, boost::log::trivial::debug) << "Connection Request from " << ::inet_ntop(AF_INET, &remote->sin_addr, buffer, sizeof(buffer)) << "; cId=" << resp.connection_id << "; tId=" << resp.transaction_id; + } + + return 0; } - int UDPTracker::handleAnnounce (UDPTracker *usi, SOCKADDR_IN *remote, char *data) + int UDPTracker::handleAnnounce(UDPTracker *usi, SOCKADDR_IN *remote, char *data) { AnnounceRequest *req; AnnounceResponse *resp; int q, // peer counts bSize, // message size i; // loop index - DatabaseDriver::PeerEntry *peers; DatabaseDriver::TorrentEntry tE; - uint8_t buff [1028]; // Reasonable buffer size. (header+168 peers) + uint8_t buff[1028]; // Reasonable buffer size. (header+168 peers) req = (AnnounceRequest*)data; if (!usi->m_conn->verifyConnectionId(req->connection_id, - m_hton32(remote->sin_addr.s_addr), - m_hton16(remote->sin_port))) + m_hton32(remote->sin_addr.s_addr), + m_hton16(remote->sin_port))) { return 1; } // change byte order: - req->port = m_hton16 (req->port); - req->ip_address = m_hton32 (req->ip_address); - req->downloaded = m_hton64 (req->downloaded); - req->event = m_hton32 (req->event); // doesn't really matter for this tracker - req->uploaded = m_hton64 (req->uploaded); - req->num_want = m_hton32 (req->num_want); - req->left = m_hton64 (req->left); + req->port = m_hton16(req->port); + req->ip_address = m_hton32(req->ip_address); + req->downloaded = m_hton64(req->downloaded); + req->event = m_hton32(req->event); // doesn't really matter for this tracker + req->uploaded = m_hton64(req->uploaded); + req->num_want = m_hton32(req->num_want); + req->left = m_hton64(req->left); if (!usi->m_allowRemotes && req->ip_address != 0) { @@ -243,62 +239,64 @@ namespace UDPT if (req->num_want >= 1) q = std::min(q, req->num_want); - peers = new DatabaseDriver::PeerEntry [q]; - - DatabaseDriver::TrackerEvents event; - switch (req->event) + { - case 1: - event = DatabaseDriver::EVENT_COMPLETE; - break; - case 2: - event = DatabaseDriver::EVENT_START; - break; - case 3: - event = DatabaseDriver::EVENT_STOP; - break; - default: - event = DatabaseDriver::EVENT_UNSPEC; - break; + std::shared_ptr peersSptr = std::shared_ptr(new DatabaseDriver::PeerEntry[q]); + DatabaseDriver::PeerEntry *peers = peersSptr.get(); + + switch (req->event) + { + case 1: + event = DatabaseDriver::EVENT_COMPLETE; + break; + case 2: + event = DatabaseDriver::EVENT_START; + break; + case 3: + event = DatabaseDriver::EVENT_STOP; + break; + default: + event = DatabaseDriver::EVENT_UNSPEC; + break; + } + + if (event == DatabaseDriver::EVENT_STOP) + q = 0; // no need for peers when stopping. + + if (q > 0) + usi->m_conn->getPeers(req->info_hash, &q, peers); + + bSize = 20; // header is 20 bytes + bSize += (6 * q); // + 6 bytes per peer. + + tE.info_hash = req->info_hash; + usi->m_conn->getTorrentInfo(&tE); + + resp = (AnnounceResponse*)buff; + resp->action = m_hton32(1); + resp->interval = m_hton32(usi->m_announceInterval); + resp->leechers = m_hton32(tE.leechers); + resp->seeders = m_hton32(tE.seeders); + resp->transaction_id = req->transaction_id; + + for (i = 0; i < q; i++) + { + int x = i * 6; + // network byte order!!! + + // IP + buff[20 + x] = ((peers[i].ip & (0xff << 24)) >> 24); + buff[21 + x] = ((peers[i].ip & (0xff << 16)) >> 16); + buff[22 + x] = ((peers[i].ip & (0xff << 8)) >> 8); + buff[23 + x] = (peers[i].ip & 0xff); + + // port + buff[24 + x] = ((peers[i].port & (0xff << 8)) >> 8); + buff[25 + x] = (peers[i].port & 0xff); + + } } - - if (event == DatabaseDriver::EVENT_STOP) - q = 0; // no need for peers when stopping. - - if (q > 0) - usi->m_conn->getPeers(req->info_hash, &q, peers); - - bSize = 20; // header is 20 bytes - bSize += (6 * q); // + 6 bytes per peer. - - tE.info_hash = req->info_hash; - usi->m_conn->getTorrentInfo(&tE); - - resp = (AnnounceResponse*)buff; - resp->action = m_hton32(1); - resp->interval = m_hton32 ( usi->m_announceInterval ); - resp->leechers = m_hton32(tE.leechers); - resp->seeders = m_hton32 (tE.seeders); - resp->transaction_id = req->transaction_id; - - for (i = 0;i < q;i++) - { - int x = i * 6; - // network byte order!!! - - // IP - buff[20 + x] = ((peers[i].ip & (0xff << 24)) >> 24); - buff[21 + x] = ((peers[i].ip & (0xff << 16)) >> 16); - buff[22 + x] = ((peers[i].ip & (0xff << 8)) >> 8); - buff[23 + x] = (peers[i].ip & 0xff); - - // port - buff[24 + x] = ((peers[i].port & (0xff << 8)) >> 8); - buff[25 + x] = (peers[i].port & 0xff); - - } - delete[] peers; ::sendto(usi->m_sock, (char*)buff, bSize, 0, (SOCKADDR*)remote, sizeof(SOCKADDR_IN)); // update DB. @@ -396,10 +394,18 @@ namespace UDPT { if (isIANAIP(remote->sin_addr.s_addr)) { + char buffer[INET_ADDRSTRLEN]; + BOOST_LOG_SEV(usi->m_logger, boost::log::trivial::warning) << "Client ignored (IANA IP): " << ::inet_ntop(AF_INET, &remote->sin_addr, buffer, sizeof(buffer)); return 0; // Access Denied: IANA reserved IP. } } + { + + char buffer[INET_ADDRSTRLEN]; + BOOST_LOG_SEV(usi->m_logger, boost::log::trivial::debug) << "Client request from " << ::inet_ntop(AF_INET, &remote->sin_addr, buffer, sizeof(buffer)); + } + if (action == 0 && r >= 16) return UDPTracker::handleConnection(usi, remote, data); else if (action == 1 && r >= 98) @@ -417,6 +423,7 @@ namespace UDPT void UDPTracker::_thread_start(UDPTracker *usi) { + BOOST_LOG_SEV(usi->m_logger, boost::log::trivial::info) << "Worker thread started with PID=" << boost::this_thread::get_id() << "."; SOCKADDR_IN remoteAddr; char tmpBuff[UDP_BUFFER_SIZE]; @@ -441,17 +448,20 @@ namespace UDPT { boost::this_thread::disable_interruption di; - r = UDPTracker::resolveRequest(usi, &remoteAddr, tmpBuff, r); + + UDPTracker::resolveRequest(usi, &remoteAddr, tmpBuff, r); } } } void UDPTracker::_maintainance_start(UDPTracker* usi) { + BOOST_LOG_SEV(usi->m_logger, boost::log::trivial::info) << "Maintenance thread started with PID=" << boost::this_thread::get_id() << "."; while (true) { { boost::this_thread::disable_interruption di; + BOOST_LOG_SEV(usi->m_logger, boost::log::trivial::info) << "Running cleanup..."; usi->m_conn->cleanup(); } diff --git a/src/udpTracker.hpp b/src/udpTracker.hpp index 5e7cf4d..c0441c8 100644 --- a/src/udpTracker.hpp +++ b/src/udpTracker.hpp @@ -22,11 +22,19 @@ #include -#include #include #include -#include #include +#include +#include +#include + +#include +#include +#include +#include + +#include "tools.h" #include "exceptions.h" #include "multiplatform.h" #include "db/driver_sqlite.hpp" @@ -155,6 +163,7 @@ namespace UDPT std::vector m_threads; uint32_t m_announceInterval; uint32_t m_cleanupInterval; + boost::log::sources::severity_channel_logger_mt<> m_logger; const boost::program_options::variables_map& m_conf;