From 8a29dfc3fa433b5eac1fb133b2553fa1cc4ae239 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Sat, 3 Sep 2016 17:53:46 -0400 Subject: [PATCH] tabify and use shared pointers --- ClientContext.cpp | 19 ++- ClientContext.h | 2 +- HTTPServer.cpp | 15 +-- I2PTunnel.cpp | 321 +++++++++++++++++++++++----------------------- I2PTunnel.h | 51 ++++---- 5 files changed, 203 insertions(+), 205 deletions(-) diff --git a/ClientContext.cpp b/ClientContext.cpp index 6ef7ec60..3eee42b2 100644 --- a/ClientContext.cpp +++ b/ClientContext.cpp @@ -276,20 +276,27 @@ namespace client return success; } - std::vector ClientContext::GetForwardInfosFor(const i2p::data::IdentHash & destination) + std::vector > ClientContext::GetForwardInfosFor(const i2p::data::IdentHash & destination) { + std::vector > infos; std::lock_guard lock(m_ForwardsMutex); - for(auto & c : m_ClientForwards) + for(const auto & c : m_ClientForwards) { if (c.second->IsLocalDestination(destination)) - return c.second->GetSessions(); + { + for (auto & i : c.second->GetSessions()) infos.push_back(i); + break; + } } - for(auto & s : m_ServerForwards) + for(const auto & s : m_ServerForwards) { if(std::get<0>(s.first) == destination) - return s.second->GetSessions(); + { + for( auto & i : s.second->GetSessions()) infos.push_back(i); + break; + } } - return {}; + return infos; } std::shared_ptr ClientContext::CreateNewLocalDestination (bool isPublic, i2p::data::SigningKeyType sigType, diff --git a/ClientContext.h b/ClientContext.h index 7faf04ca..581ff466 100644 --- a/ClientContext.h +++ b/ClientContext.h @@ -67,7 +67,7 @@ namespace client AddressBook& GetAddressBook () { return m_AddressBook; }; const SAMBridge * GetSAMBridge () const { return m_SamBridge; }; - std::vector GetForwardInfosFor(const i2p::data::IdentHash & destination); + std::vector > GetForwardInfosFor(const i2p::data::IdentHash & destination); private: diff --git a/HTTPServer.cpp b/HTTPServer.cpp index 01338ff2..a8434bef 100644 --- a/HTTPServer.cpp +++ b/HTTPServer.cpp @@ -343,29 +343,26 @@ namespace http { s << "IBGW"; s << "OBEP"; s << "UDP Converstation"; - s << "Idle Time"; s << ""; auto forward = i2p::client::context.GetForwardInfosFor(dest->GetIdentHash()); for (auto info : forward) { s << ""; - s << "" << info.RemoteIdent.ToBase32() << ""; + s << "" << info->RemoteIdent->ToBase32() << ""; s << ""; - if(info.CurrentIBGW) - s << std::string(info.CurrentIBGW->ToBase64()); + if(info->CurrentIBGW) + s << info->CurrentIBGW->ToBase64(); else s << "(none)"; s << ""; s << ""; - if(info.CurrentOBEP) - s << std::string(info.CurrentOBEP->ToBase64()); + if(info->CurrentOBEP) + s << info->CurrentOBEP->ToBase64(); else s << "(none)"; s << ""; - s << "" << info.LocalEndpoint << " ⇄ " << info.RemoteEndpoint << ""; - auto sec = std::chrono::duration >( std::chrono::milliseconds(info.idle) ); - s << "" << sec.count() << " seconds "; + s << "" << info->LocalEndpoint << " ⇄ " << info->RemoteEndpoint << ""; s << "
\r\n"; } s << "\r\n"; diff --git a/I2PTunnel.cpp b/I2PTunnel.cpp index 688866c8..0c58ba9d 100644 --- a/I2PTunnel.cpp +++ b/I2PTunnel.cpp @@ -569,207 +569,200 @@ namespace client uint16_t ourPort, uint16_t theirPort) : m_Destination(localDestination->GetDatagramDestination()), m_Service(localDestination->GetService()), - IPSocket(localDestination->GetService(), localEndpoint), - Identity(to), - SendEndpoint(endpoint), - LastActivity(i2p::util::GetMillisecondsSinceEpoch()), - LocalPort(ourPort), - RemotePort(theirPort) - { - Receive(); - } + IPSocket(localDestination->GetService(), localEndpoint), + Identity(to), + SendEndpoint(endpoint), + LastActivity(i2p::util::GetMillisecondsSinceEpoch()), + LocalPort(ourPort), + RemotePort(theirPort) + { + Receive(); + } - void UDPSession::Receive() { - LogPrint(eLogDebug, "UDPSession: Receive"); - IPSocket.async_receive_from(boost::asio::buffer(m_Buffer, I2P_UDP_MAX_MTU), - FromEndpoint, std::bind(&UDPSession::HandleReceived, this, std::placeholders::_1, std::placeholders::_2)); - } - - void UDPSession::HandleReceived(const boost::system::error_code & ecode, std::size_t len) - { - if(!ecode) - { - LogPrint(eLogDebug, "UDPSession: forward ", len, "B from ", FromEndpoint); - LastActivity = i2p::util::GetMillisecondsSinceEpoch(); - uint8_t * data = new uint8_t[len]; - memcpy(data, m_Buffer, len); - m_Service.post([&,len, data] () { - m_Destination->SendDatagramTo(data, len, Identity, 0, 0); - delete [] data; - }); - - Receive(); - } else { - LogPrint(eLogError, "UDPSession: ", ecode.message()); - } - } + void UDPSession::Receive() { + LogPrint(eLogDebug, "UDPSession: Receive"); + IPSocket.async_receive_from(boost::asio::buffer(m_Buffer, I2P_UDP_MAX_MTU), + FromEndpoint, std::bind(&UDPSession::HandleReceived, this, std::placeholders::_1, std::placeholders::_2)); + } + + void UDPSession::HandleReceived(const boost::system::error_code & ecode, std::size_t len) + { + if(!ecode) + { + LogPrint(eLogDebug, "UDPSession: forward ", len, "B from ", FromEndpoint); + LastActivity = i2p::util::GetMillisecondsSinceEpoch(); + uint8_t * data = new uint8_t[len]; + memcpy(data, m_Buffer, len); + m_Service.post([&,len, data] () { + m_Destination->SendDatagramTo(data, len, Identity, 0, 0); + delete [] data; + }); + + Receive(); + } else { + LogPrint(eLogError, "UDPSession: ", ecode.message()); + } + } - - - I2PUDPServerTunnel::I2PUDPServerTunnel(const std::string & name, std::shared_ptr localDestination, - const boost::asio::ip::address& localAddress, boost::asio::ip::udp::endpoint forwardTo, uint16_t port) : - m_Name(name), - LocalPort(port), - m_LocalAddress(localAddress), - m_RemoteEndpoint(forwardTo) - { - m_LocalDest = localDestination; - m_LocalDest->Start(); - auto dgram = m_LocalDest->CreateDatagramDestination(); - dgram->SetReceiver(std::bind(&I2PUDPServerTunnel::HandleRecvFromI2P, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5)); - } + + + I2PUDPServerTunnel::I2PUDPServerTunnel(const std::string & name, std::shared_ptr localDestination, + const boost::asio::ip::address& localAddress, boost::asio::ip::udp::endpoint forwardTo, uint16_t port) : + m_Name(name), + LocalPort(port), + m_LocalAddress(localAddress), + m_RemoteEndpoint(forwardTo) + { + m_LocalDest = localDestination; + m_LocalDest->Start(); + auto dgram = m_LocalDest->CreateDatagramDestination(); + dgram->SetReceiver(std::bind(&I2PUDPServerTunnel::HandleRecvFromI2P, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5)); + } - I2PUDPServerTunnel::~I2PUDPServerTunnel() - { - auto dgram = m_LocalDest->GetDatagramDestination(); - if (dgram) dgram->ResetReceiver(); - - LogPrint(eLogInfo, "UDPServer: done"); - } + I2PUDPServerTunnel::~I2PUDPServerTunnel() + { + auto dgram = m_LocalDest->GetDatagramDestination(); + if (dgram) dgram->ResetReceiver(); + + LogPrint(eLogInfo, "UDPServer: done"); + } - void I2PUDPServerTunnel::Start() { - m_LocalDest->Start(); - } + void I2PUDPServerTunnel::Start() { + m_LocalDest->Start(); + } - std::vector I2PUDPServerTunnel::GetSessions() + std::vector > I2PUDPServerTunnel::GetSessions() { - std::vector sessions; - auto localident = m_LocalDest->GetIdentHash(); + std::vector > sessions; std::lock_guard lock(m_SessionsMutex); for ( UDPSession * s : m_Sessions ) { if (!s->m_Destination) continue; auto info = s->m_Destination->GetInfoForRemote(s->Identity); if(!info) continue; - sessions.push_back(DatagramSessionInfo{ - m_Name, - localident, - s->Identity, - info->IBGW, - info->OBEP, - s->IPSocket.local_endpoint(), - s->SendEndpoint, - info->success - }); + + auto sinfo = std::make_shared(); + sinfo->Name = m_Name; + sinfo->LocalIdent = std::make_shared(m_LocalDest->GetIdentHash().data()); + sinfo->RemoteIdent = std::make_shared(s->Identity.data()); + sinfo->CurrentIBGW = info->IBGW; + sinfo->CurrentOBEP = info->OBEP; + sessions.push_back(sinfo); } return sessions; } - - I2PUDPClientTunnel::I2PUDPClientTunnel(const std::string & name, const std::string &remoteDest, - boost::asio::ip::udp::endpoint localEndpoint, - std::shared_ptr localDestination, - uint16_t remotePort) : - m_Name(name), - m_Session(nullptr), - m_RemoteDest(remoteDest), - m_LocalDest(localDestination), - m_LocalEndpoint(localEndpoint), - m_RemoteIdent(nullptr), - m_ResolveThread(nullptr), - LocalPort(localEndpoint.port()), - RemotePort(remotePort), - m_cancel_resolve(false) - { - auto dgram = m_LocalDest->CreateDatagramDestination(); - dgram->SetReceiver(std::bind(&I2PUDPClientTunnel::HandleRecvFromI2P, this, - std::placeholders::_1, std::placeholders::_2, - std::placeholders::_3, std::placeholders::_4, - std::placeholders::_5)); - } + + I2PUDPClientTunnel::I2PUDPClientTunnel(const std::string & name, const std::string &remoteDest, + boost::asio::ip::udp::endpoint localEndpoint, + std::shared_ptr localDestination, + uint16_t remotePort) : + m_Name(name), + m_Session(nullptr), + m_RemoteDest(remoteDest), + m_LocalDest(localDestination), + m_LocalEndpoint(localEndpoint), + m_RemoteIdent(nullptr), + m_ResolveThread(nullptr), + LocalPort(localEndpoint.port()), + RemotePort(remotePort), + m_cancel_resolve(false) + { + auto dgram = m_LocalDest->CreateDatagramDestination(); + dgram->SetReceiver(std::bind(&I2PUDPClientTunnel::HandleRecvFromI2P, this, + std::placeholders::_1, std::placeholders::_2, + std::placeholders::_3, std::placeholders::_4, + std::placeholders::_5)); + } - - - void I2PUDPClientTunnel::Start() { - m_LocalDest->Start(); - if (m_ResolveThread == nullptr) - m_ResolveThread = new std::thread(std::bind(&I2PUDPClientTunnel::TryResolving, this)); - } + + + void I2PUDPClientTunnel::Start() { + m_LocalDest->Start(); + if (m_ResolveThread == nullptr) + m_ResolveThread = new std::thread(std::bind(&I2PUDPClientTunnel::TryResolving, this)); + } - std::vector I2PUDPClientTunnel::GetSessions() + std::vector > I2PUDPClientTunnel::GetSessions() { - std::vector infos; + std::vector > infos; if(m_Session && m_LocalDest) { - auto localident = m_LocalDest->GetIdentHash(); auto s = m_Session; if (s->m_Destination) { auto info = m_Session->m_Destination->GetInfoForRemote(s->Identity); if(info) { - infos.push_back(DatagramSessionInfo{ - m_Name, - localident, - s->Identity, - info->IBGW, - info->OBEP, - s->IPSocket.local_endpoint(), - s->SendEndpoint, - info->success - }); + auto sinfo = std::make_shared(); + sinfo->Name = m_Name; + sinfo->LocalIdent = std::make_shared(m_LocalDest->GetIdentHash().data()); + sinfo->RemoteIdent = std::make_shared(s->Identity.data()); + sinfo->CurrentIBGW = info->IBGW; + sinfo->CurrentOBEP = info->OBEP; + infos.push_back(sinfo); } } } return infos; } - void I2PUDPClientTunnel::TryResolving() { - LogPrint(eLogInfo, "UDP Tunnel: Trying to resolve ", m_RemoteDest); - m_RemoteIdent = new i2p::data::IdentHash; - m_RemoteIdent->Fill(0); + void I2PUDPClientTunnel::TryResolving() { + LogPrint(eLogInfo, "UDP Tunnel: Trying to resolve ", m_RemoteDest); + m_RemoteIdent = new i2p::data::IdentHash; + m_RemoteIdent->Fill(0); - while(!context.GetAddressBook().GetIdentHash(m_RemoteDest, *m_RemoteIdent) && !m_cancel_resolve) - { - LogPrint(eLogWarning, "UDP Tunnel: failed to lookup ", m_RemoteDest); - std::this_thread::sleep_for(std::chrono::seconds(1)); - } - if(m_cancel_resolve) - { - LogPrint(eLogError, "UDP Tunnel: lookup of ", m_RemoteDest, " was cancelled"); - return; - } - LogPrint(eLogInfo, "UDP Tunnel: resolved ", m_RemoteDest, " to ", m_RemoteIdent->ToBase32()); - // delete existing session - if(m_Session) delete m_Session; - - boost::asio::ip::udp::endpoint ep(boost::asio::ip::address::from_string("127.0.0.1"), 0); - m_Session = new UDPSession(m_LocalEndpoint, m_LocalDest, ep, *m_RemoteIdent, LocalPort, RemotePort); - } + while(!context.GetAddressBook().GetIdentHash(m_RemoteDest, *m_RemoteIdent) && !m_cancel_resolve) + { + LogPrint(eLogWarning, "UDP Tunnel: failed to lookup ", m_RemoteDest); + std::this_thread::sleep_for(std::chrono::seconds(1)); + } + if(m_cancel_resolve) + { + LogPrint(eLogError, "UDP Tunnel: lookup of ", m_RemoteDest, " was cancelled"); + return; + } + LogPrint(eLogInfo, "UDP Tunnel: resolved ", m_RemoteDest, " to ", m_RemoteIdent->ToBase32()); + // delete existing session + if(m_Session) delete m_Session; + + boost::asio::ip::udp::endpoint ep(boost::asio::ip::address::from_string("127.0.0.1"), 0); + m_Session = new UDPSession(m_LocalEndpoint, m_LocalDest, ep, *m_RemoteIdent, LocalPort, RemotePort); + } - void I2PUDPClientTunnel::HandleRecvFromI2P(const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len) - { - if(m_RemoteIdent && from.GetIdentHash() == *m_RemoteIdent) - { - // address match - if(m_Session) - { - // tell session - LogPrint(eLogDebug, "UDP Client: got ", len, "B from ", from.GetIdentHash().ToBase32()); - m_Session->IPSocket.send_to(boost::asio::buffer(buf, len), m_Session->FromEndpoint); - } - else - LogPrint(eLogWarning, "UDP Client: no session"); - } - else - LogPrint(eLogWarning, "UDP Client: unwarrented traffic from ", from.GetIdentHash().ToBase32()); - - } + void I2PUDPClientTunnel::HandleRecvFromI2P(const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len) + { + if(m_RemoteIdent && from.GetIdentHash() == *m_RemoteIdent) + { + // address match + if(m_Session) + { + // tell session + LogPrint(eLogDebug, "UDP Client: got ", len, "B from ", from.GetIdentHash().ToBase32()); + m_Session->IPSocket.send_to(boost::asio::buffer(buf, len), m_Session->FromEndpoint); + } + else + LogPrint(eLogWarning, "UDP Client: no session"); + } + else + LogPrint(eLogWarning, "UDP Client: unwarrented traffic from ", from.GetIdentHash().ToBase32()); + + } - I2PUDPClientTunnel::~I2PUDPClientTunnel() { - auto dgram = m_LocalDest->GetDatagramDestination(); - if (dgram) dgram->ResetReceiver(); + I2PUDPClientTunnel::~I2PUDPClientTunnel() { + auto dgram = m_LocalDest->GetDatagramDestination(); + if (dgram) dgram->ResetReceiver(); - if (m_Session) delete m_Session; - m_cancel_resolve = true; + if (m_Session) delete m_Session; + m_cancel_resolve = true; - if(m_ResolveThread) - { - m_ResolveThread->join(); - delete m_ResolveThread; - m_ResolveThread = nullptr; - } - if (m_RemoteIdent) delete m_RemoteIdent; - } + if(m_ResolveThread) + { + m_ResolveThread->join(); + delete m_ResolveThread; + m_ResolveThread = nullptr; + } + if (m_RemoteIdent) delete m_RemoteIdent; + } } } diff --git a/I2PTunnel.h b/I2PTunnel.h index 0d4d85a1..2418da30 100644 --- a/I2PTunnel.h +++ b/I2PTunnel.h @@ -138,27 +138,6 @@ namespace client /** max size for i2p udp */ const size_t I2P_UDP_MAX_MTU = i2p::datagram::MAX_DATAGRAM_SIZE; - /** read only info about a datagram session */ - struct DatagramSessionInfo - { - /** the name of this forward */ - const std::string Name; - /** ident hash of local destination */ - const i2p::data::IdentHash LocalIdent; - /** ident hash of remote destination */ - const i2p::data::IdentHash RemoteIdent; - /** ident hash of IBGW in use currently in this session or nullptr if none is set */ - std::shared_ptr CurrentIBGW; - /** ident hash of OBEP in use for this session or nullptr if none is set */ - std::shared_ptr CurrentOBEP; - /** i2p router's udp endpoint */ - const boost::asio::ip::udp::endpoint LocalEndpoint; - /** client's udp endpoint */ - const boost::asio::ip::udp::endpoint RemoteEndpoint; - /** how long has this converstation been idle in ms */ - const uint64_t idle; - }; - struct UDPSession { i2p::datagram::DatagramDestination * m_Destination; @@ -182,9 +161,31 @@ namespace client void Receive(); }; + + /** read only info about a datagram session */ + struct DatagramSessionInfo + { + /** the name of this forward */ + std::string Name; + /** ident hash of local destination */ + std::shared_ptr LocalIdent; + /** ident hash of remote destination */ + std::shared_ptr RemoteIdent; + /** ident hash of IBGW in use currently in this session or nullptr if none is set */ + std::shared_ptr CurrentIBGW; + /** ident hash of OBEP in use for this session or nullptr if none is set */ + std::shared_ptr CurrentOBEP; + /** i2p router's udp endpoint */ + boost::asio::ip::udp::endpoint LocalEndpoint; + /** client's udp endpoint */ + boost::asio::ip::udp::endpoint RemoteEndpoint; + /** how long has this converstation been idle in ms */ + uint64_t idle; + }; + /** server side udp tunnel, many i2p inbound to 1 ip outbound */ class I2PUDPServerTunnel - { + { public: I2PUDPServerTunnel(const std::string & name, std::shared_ptr localDestination, @@ -195,7 +196,7 @@ namespace client void ExpireStale(const uint64_t delta=I2P_UDP_SESSION_TIMEOUT); void Start(); const char * GetName() const { return m_Name.c_str(); } - std::vector GetSessions(); + std::vector > GetSessions(); private: void HandleRecvFromI2P(const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len); UDPSession * ObtainUDPSession(const i2p::data::IdentityEx& from, uint16_t localPort, uint16_t remotePort); @@ -209,7 +210,7 @@ namespace client std::shared_ptr m_LocalDest; }; - class I2PUDPClientTunnel + class I2PUDPClientTunnel { public: I2PUDPClientTunnel(const std::string & name, const std::string &remoteDest, @@ -218,7 +219,7 @@ namespace client ~I2PUDPClientTunnel(); void Start(); const char * GetName() const { return m_Name.c_str(); } - std::vector GetSessions(); + std::vector > GetSessions(); bool IsLocalDestination(const i2p::data::IdentHash & destination) const { return destination == m_LocalDest->GetIdentHash(); } private: void HandleRecvFromI2P(const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len);