Browse Source

tabify and use shared pointers

pull/628/head
Jeff Becker 8 years ago
parent
commit
8a29dfc3fa
No known key found for this signature in database
GPG Key ID: AB950234D6EA286B
  1. 19
      ClientContext.cpp
  2. 2
      ClientContext.h
  3. 15
      HTTPServer.cpp
  4. 321
      I2PTunnel.cpp
  5. 51
      I2PTunnel.h

19
ClientContext.cpp

@ -276,20 +276,27 @@ namespace client
return success; return success;
} }
std::vector<DatagramSessionInfo> ClientContext::GetForwardInfosFor(const i2p::data::IdentHash & destination) std::vector<std::shared_ptr<DatagramSessionInfo> > ClientContext::GetForwardInfosFor(const i2p::data::IdentHash & destination)
{ {
std::vector<std::shared_ptr<DatagramSessionInfo> > infos;
std::lock_guard<std::mutex> lock(m_ForwardsMutex); std::lock_guard<std::mutex> lock(m_ForwardsMutex);
for(auto & c : m_ClientForwards) for(const auto & c : m_ClientForwards)
{ {
if (c.second->IsLocalDestination(destination)) 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) 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<ClientDestination> ClientContext::CreateNewLocalDestination (bool isPublic, i2p::data::SigningKeyType sigType, std::shared_ptr<ClientDestination> ClientContext::CreateNewLocalDestination (bool isPublic, i2p::data::SigningKeyType sigType,

2
ClientContext.h

@ -67,7 +67,7 @@ namespace client
AddressBook& GetAddressBook () { return m_AddressBook; }; AddressBook& GetAddressBook () { return m_AddressBook; };
const SAMBridge * GetSAMBridge () const { return m_SamBridge; }; const SAMBridge * GetSAMBridge () const { return m_SamBridge; };
std::vector<DatagramSessionInfo> GetForwardInfosFor(const i2p::data::IdentHash & destination); std::vector<std::shared_ptr<DatagramSessionInfo> > GetForwardInfosFor(const i2p::data::IdentHash & destination);
private: private:

15
HTTPServer.cpp

@ -343,29 +343,26 @@ namespace http {
s << "<th>IBGW</th>"; s << "<th>IBGW</th>";
s << "<th>OBEP</th>"; s << "<th>OBEP</th>";
s << "<th>UDP Converstation</th>"; s << "<th>UDP Converstation</th>";
s << "<th>Idle Time</th>";
s << "</th>"; s << "</th>";
auto forward = i2p::client::context.GetForwardInfosFor(dest->GetIdentHash()); auto forward = i2p::client::context.GetForwardInfosFor(dest->GetIdentHash());
for (auto info : forward) for (auto info : forward)
{ {
s << "<tr>"; s << "<tr>";
s << "<td>" << info.RemoteIdent.ToBase32() << "</td>"; s << "<td>" << info->RemoteIdent->ToBase32() << "</td>";
s << "<td>"; s << "<td>";
if(info.CurrentIBGW) if(info->CurrentIBGW)
s << std::string(info.CurrentIBGW->ToBase64()); s << info->CurrentIBGW->ToBase64();
else else
s << "(none)"; s << "(none)";
s << "</td>"; s << "</td>";
s << "<td>"; s << "<td>";
if(info.CurrentOBEP) if(info->CurrentOBEP)
s << std::string(info.CurrentOBEP->ToBase64()); s << info->CurrentOBEP->ToBase64();
else else
s << "(none)"; s << "(none)";
s << "</td>"; s << "</td>";
s << "<td>" << info.LocalEndpoint << " &#8644; " << info.RemoteEndpoint << "</td>"; s << "<td>" << info->LocalEndpoint << " &#8644; " << info->RemoteEndpoint << "</td>";
auto sec = std::chrono::duration<float, std::ratio<1000, 1 > >( std::chrono::milliseconds(info.idle) );
s << "<td>" << sec.count() << " seconds </td>";
s << "</tr><br>\r\n"; s << "</tr><br>\r\n";
} }
s << "</table>\r\n"; s << "</table>\r\n";

321
I2PTunnel.cpp

@ -569,207 +569,200 @@ namespace client
uint16_t ourPort, uint16_t theirPort) : uint16_t ourPort, uint16_t theirPort) :
m_Destination(localDestination->GetDatagramDestination()), m_Destination(localDestination->GetDatagramDestination()),
m_Service(localDestination->GetService()), m_Service(localDestination->GetService()),
IPSocket(localDestination->GetService(), localEndpoint), IPSocket(localDestination->GetService(), localEndpoint),
Identity(to), Identity(to),
SendEndpoint(endpoint), SendEndpoint(endpoint),
LastActivity(i2p::util::GetMillisecondsSinceEpoch()), LastActivity(i2p::util::GetMillisecondsSinceEpoch()),
LocalPort(ourPort), LocalPort(ourPort),
RemotePort(theirPort) RemotePort(theirPort)
{ {
Receive(); Receive();
} }
void UDPSession::Receive() { void UDPSession::Receive() {
LogPrint(eLogDebug, "UDPSession: Receive"); LogPrint(eLogDebug, "UDPSession: Receive");
IPSocket.async_receive_from(boost::asio::buffer(m_Buffer, I2P_UDP_MAX_MTU), 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)); 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) void UDPSession::HandleReceived(const boost::system::error_code & ecode, std::size_t len)
{ {
if(!ecode) if(!ecode)
{ {
LogPrint(eLogDebug, "UDPSession: forward ", len, "B from ", FromEndpoint); LogPrint(eLogDebug, "UDPSession: forward ", len, "B from ", FromEndpoint);
LastActivity = i2p::util::GetMillisecondsSinceEpoch(); LastActivity = i2p::util::GetMillisecondsSinceEpoch();
uint8_t * data = new uint8_t[len]; uint8_t * data = new uint8_t[len];
memcpy(data, m_Buffer, len); memcpy(data, m_Buffer, len);
m_Service.post([&,len, data] () { m_Service.post([&,len, data] () {
m_Destination->SendDatagramTo(data, len, Identity, 0, 0); m_Destination->SendDatagramTo(data, len, Identity, 0, 0);
delete [] data; delete [] data;
}); });
Receive(); Receive();
} else { } else {
LogPrint(eLogError, "UDPSession: ", ecode.message()); LogPrint(eLogError, "UDPSession: ", ecode.message());
} }
} }
I2PUDPServerTunnel::I2PUDPServerTunnel(const std::string & name, std::shared_ptr<i2p::client::ClientDestination> localDestination, I2PUDPServerTunnel::I2PUDPServerTunnel(const std::string & name, std::shared_ptr<i2p::client::ClientDestination> localDestination,
const boost::asio::ip::address& localAddress, boost::asio::ip::udp::endpoint forwardTo, uint16_t port) : const boost::asio::ip::address& localAddress, boost::asio::ip::udp::endpoint forwardTo, uint16_t port) :
m_Name(name), m_Name(name),
LocalPort(port), LocalPort(port),
m_LocalAddress(localAddress), m_LocalAddress(localAddress),
m_RemoteEndpoint(forwardTo) m_RemoteEndpoint(forwardTo)
{ {
m_LocalDest = localDestination; m_LocalDest = localDestination;
m_LocalDest->Start(); m_LocalDest->Start();
auto dgram = m_LocalDest->CreateDatagramDestination(); 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)); dgram->SetReceiver(std::bind(&I2PUDPServerTunnel::HandleRecvFromI2P, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5));
} }
I2PUDPServerTunnel::~I2PUDPServerTunnel() I2PUDPServerTunnel::~I2PUDPServerTunnel()
{ {
auto dgram = m_LocalDest->GetDatagramDestination(); auto dgram = m_LocalDest->GetDatagramDestination();
if (dgram) dgram->ResetReceiver(); if (dgram) dgram->ResetReceiver();
LogPrint(eLogInfo, "UDPServer: done"); LogPrint(eLogInfo, "UDPServer: done");
} }
void I2PUDPServerTunnel::Start() { void I2PUDPServerTunnel::Start() {
m_LocalDest->Start(); m_LocalDest->Start();
} }
std::vector<DatagramSessionInfo> I2PUDPServerTunnel::GetSessions() std::vector<std::shared_ptr<DatagramSessionInfo> > I2PUDPServerTunnel::GetSessions()
{ {
std::vector<DatagramSessionInfo> sessions; std::vector<std::shared_ptr<DatagramSessionInfo> > sessions;
auto localident = m_LocalDest->GetIdentHash();
std::lock_guard<std::mutex> lock(m_SessionsMutex); std::lock_guard<std::mutex> lock(m_SessionsMutex);
for ( UDPSession * s : m_Sessions ) for ( UDPSession * s : m_Sessions )
{ {
if (!s->m_Destination) continue; if (!s->m_Destination) continue;
auto info = s->m_Destination->GetInfoForRemote(s->Identity); auto info = s->m_Destination->GetInfoForRemote(s->Identity);
if(!info) continue; if(!info) continue;
sessions.push_back(DatagramSessionInfo{
m_Name, auto sinfo = std::make_shared<DatagramSessionInfo>();
localident, sinfo->Name = m_Name;
s->Identity, sinfo->LocalIdent = std::make_shared<i2p::data::IdentHash>(m_LocalDest->GetIdentHash().data());
info->IBGW, sinfo->RemoteIdent = std::make_shared<i2p::data::IdentHash>(s->Identity.data());
info->OBEP, sinfo->CurrentIBGW = info->IBGW;
s->IPSocket.local_endpoint(), sinfo->CurrentOBEP = info->OBEP;
s->SendEndpoint, sessions.push_back(sinfo);
info->success
});
} }
return sessions; return sessions;
} }
I2PUDPClientTunnel::I2PUDPClientTunnel(const std::string & name, const std::string &remoteDest, I2PUDPClientTunnel::I2PUDPClientTunnel(const std::string & name, const std::string &remoteDest,
boost::asio::ip::udp::endpoint localEndpoint, boost::asio::ip::udp::endpoint localEndpoint,
std::shared_ptr<i2p::client::ClientDestination> localDestination, std::shared_ptr<i2p::client::ClientDestination> localDestination,
uint16_t remotePort) : uint16_t remotePort) :
m_Name(name), m_Name(name),
m_Session(nullptr), m_Session(nullptr),
m_RemoteDest(remoteDest), m_RemoteDest(remoteDest),
m_LocalDest(localDestination), m_LocalDest(localDestination),
m_LocalEndpoint(localEndpoint), m_LocalEndpoint(localEndpoint),
m_RemoteIdent(nullptr), m_RemoteIdent(nullptr),
m_ResolveThread(nullptr), m_ResolveThread(nullptr),
LocalPort(localEndpoint.port()), LocalPort(localEndpoint.port()),
RemotePort(remotePort), RemotePort(remotePort),
m_cancel_resolve(false) m_cancel_resolve(false)
{ {
auto dgram = m_LocalDest->CreateDatagramDestination(); auto dgram = m_LocalDest->CreateDatagramDestination();
dgram->SetReceiver(std::bind(&I2PUDPClientTunnel::HandleRecvFromI2P, this, dgram->SetReceiver(std::bind(&I2PUDPClientTunnel::HandleRecvFromI2P, this,
std::placeholders::_1, std::placeholders::_2, std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3, std::placeholders::_4, std::placeholders::_3, std::placeholders::_4,
std::placeholders::_5)); std::placeholders::_5));
} }
void I2PUDPClientTunnel::Start() { void I2PUDPClientTunnel::Start() {
m_LocalDest->Start(); m_LocalDest->Start();
if (m_ResolveThread == nullptr) if (m_ResolveThread == nullptr)
m_ResolveThread = new std::thread(std::bind(&I2PUDPClientTunnel::TryResolving, this)); m_ResolveThread = new std::thread(std::bind(&I2PUDPClientTunnel::TryResolving, this));
} }
std::vector<DatagramSessionInfo> I2PUDPClientTunnel::GetSessions() std::vector<std::shared_ptr<DatagramSessionInfo> > I2PUDPClientTunnel::GetSessions()
{ {
std::vector<DatagramSessionInfo> infos; std::vector<std::shared_ptr<DatagramSessionInfo> > infos;
if(m_Session && m_LocalDest) if(m_Session && m_LocalDest)
{ {
auto localident = m_LocalDest->GetIdentHash();
auto s = m_Session; auto s = m_Session;
if (s->m_Destination) if (s->m_Destination)
{ {
auto info = m_Session->m_Destination->GetInfoForRemote(s->Identity); auto info = m_Session->m_Destination->GetInfoForRemote(s->Identity);
if(info) if(info)
{ {
infos.push_back(DatagramSessionInfo{ auto sinfo = std::make_shared<DatagramSessionInfo>();
m_Name, sinfo->Name = m_Name;
localident, sinfo->LocalIdent = std::make_shared<i2p::data::IdentHash>(m_LocalDest->GetIdentHash().data());
s->Identity, sinfo->RemoteIdent = std::make_shared<i2p::data::IdentHash>(s->Identity.data());
info->IBGW, sinfo->CurrentIBGW = info->IBGW;
info->OBEP, sinfo->CurrentOBEP = info->OBEP;
s->IPSocket.local_endpoint(), infos.push_back(sinfo);
s->SendEndpoint,
info->success
});
} }
} }
} }
return infos; return infos;
} }
void I2PUDPClientTunnel::TryResolving() { void I2PUDPClientTunnel::TryResolving() {
LogPrint(eLogInfo, "UDP Tunnel: Trying to resolve ", m_RemoteDest); LogPrint(eLogInfo, "UDP Tunnel: Trying to resolve ", m_RemoteDest);
m_RemoteIdent = new i2p::data::IdentHash; m_RemoteIdent = new i2p::data::IdentHash;
m_RemoteIdent->Fill(0); m_RemoteIdent->Fill(0);
while(!context.GetAddressBook().GetIdentHash(m_RemoteDest, *m_RemoteIdent) && !m_cancel_resolve) while(!context.GetAddressBook().GetIdentHash(m_RemoteDest, *m_RemoteIdent) && !m_cancel_resolve)
{ {
LogPrint(eLogWarning, "UDP Tunnel: failed to lookup ", m_RemoteDest); LogPrint(eLogWarning, "UDP Tunnel: failed to lookup ", m_RemoteDest);
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));
} }
if(m_cancel_resolve) if(m_cancel_resolve)
{ {
LogPrint(eLogError, "UDP Tunnel: lookup of ", m_RemoteDest, " was cancelled"); LogPrint(eLogError, "UDP Tunnel: lookup of ", m_RemoteDest, " was cancelled");
return; return;
} }
LogPrint(eLogInfo, "UDP Tunnel: resolved ", m_RemoteDest, " to ", m_RemoteIdent->ToBase32()); LogPrint(eLogInfo, "UDP Tunnel: resolved ", m_RemoteDest, " to ", m_RemoteIdent->ToBase32());
// delete existing session // delete existing session
if(m_Session) delete m_Session; if(m_Session) delete m_Session;
boost::asio::ip::udp::endpoint ep(boost::asio::ip::address::from_string("127.0.0.1"), 0); 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); 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) 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) if(m_RemoteIdent && from.GetIdentHash() == *m_RemoteIdent)
{ {
// address match // address match
if(m_Session) if(m_Session)
{ {
// tell session // tell session
LogPrint(eLogDebug, "UDP Client: got ", len, "B from ", from.GetIdentHash().ToBase32()); LogPrint(eLogDebug, "UDP Client: got ", len, "B from ", from.GetIdentHash().ToBase32());
m_Session->IPSocket.send_to(boost::asio::buffer(buf, len), m_Session->FromEndpoint); m_Session->IPSocket.send_to(boost::asio::buffer(buf, len), m_Session->FromEndpoint);
} }
else else
LogPrint(eLogWarning, "UDP Client: no session"); LogPrint(eLogWarning, "UDP Client: no session");
} }
else else
LogPrint(eLogWarning, "UDP Client: unwarrented traffic from ", from.GetIdentHash().ToBase32()); LogPrint(eLogWarning, "UDP Client: unwarrented traffic from ", from.GetIdentHash().ToBase32());
} }
I2PUDPClientTunnel::~I2PUDPClientTunnel() { I2PUDPClientTunnel::~I2PUDPClientTunnel() {
auto dgram = m_LocalDest->GetDatagramDestination(); auto dgram = m_LocalDest->GetDatagramDestination();
if (dgram) dgram->ResetReceiver(); if (dgram) dgram->ResetReceiver();
if (m_Session) delete m_Session; if (m_Session) delete m_Session;
m_cancel_resolve = true; m_cancel_resolve = true;
if(m_ResolveThread) if(m_ResolveThread)
{ {
m_ResolveThread->join(); m_ResolveThread->join();
delete m_ResolveThread; delete m_ResolveThread;
m_ResolveThread = nullptr; m_ResolveThread = nullptr;
} }
if (m_RemoteIdent) delete m_RemoteIdent; if (m_RemoteIdent) delete m_RemoteIdent;
} }
} }
} }

51
I2PTunnel.h

@ -138,27 +138,6 @@ namespace client
/** max size for i2p udp */ /** max size for i2p udp */
const size_t I2P_UDP_MAX_MTU = i2p::datagram::MAX_DATAGRAM_SIZE; 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<const i2p::data::IdentHash> CurrentIBGW;
/** ident hash of OBEP in use for this session or nullptr if none is set */
std::shared_ptr<const i2p::data::IdentHash> 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 struct UDPSession
{ {
i2p::datagram::DatagramDestination * m_Destination; i2p::datagram::DatagramDestination * m_Destination;
@ -182,9 +161,31 @@ namespace client
void Receive(); 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<const i2p::data::IdentHash> LocalIdent;
/** ident hash of remote destination */
std::shared_ptr<const i2p::data::IdentHash> RemoteIdent;
/** ident hash of IBGW in use currently in this session or nullptr if none is set */
std::shared_ptr<const i2p::data::IdentHash> CurrentIBGW;
/** ident hash of OBEP in use for this session or nullptr if none is set */
std::shared_ptr<const i2p::data::IdentHash> 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 */ /** server side udp tunnel, many i2p inbound to 1 ip outbound */
class I2PUDPServerTunnel class I2PUDPServerTunnel
{ {
public: public:
I2PUDPServerTunnel(const std::string & name, I2PUDPServerTunnel(const std::string & name,
std::shared_ptr<i2p::client::ClientDestination> localDestination, std::shared_ptr<i2p::client::ClientDestination> localDestination,
@ -195,7 +196,7 @@ namespace client
void ExpireStale(const uint64_t delta=I2P_UDP_SESSION_TIMEOUT); void ExpireStale(const uint64_t delta=I2P_UDP_SESSION_TIMEOUT);
void Start(); void Start();
const char * GetName() const { return m_Name.c_str(); } const char * GetName() const { return m_Name.c_str(); }
std::vector<DatagramSessionInfo> GetSessions(); std::vector<std::shared_ptr<DatagramSessionInfo> > GetSessions();
private: private:
void HandleRecvFromI2P(const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len); 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); UDPSession * ObtainUDPSession(const i2p::data::IdentityEx& from, uint16_t localPort, uint16_t remotePort);
@ -209,7 +210,7 @@ namespace client
std::shared_ptr<i2p::client::ClientDestination> m_LocalDest; std::shared_ptr<i2p::client::ClientDestination> m_LocalDest;
}; };
class I2PUDPClientTunnel class I2PUDPClientTunnel
{ {
public: public:
I2PUDPClientTunnel(const std::string & name, const std::string &remoteDest, I2PUDPClientTunnel(const std::string & name, const std::string &remoteDest,
@ -218,7 +219,7 @@ namespace client
~I2PUDPClientTunnel(); ~I2PUDPClientTunnel();
void Start(); void Start();
const char * GetName() const { return m_Name.c_str(); } const char * GetName() const { return m_Name.c_str(); }
std::vector<DatagramSessionInfo> GetSessions(); std::vector<std::shared_ptr<DatagramSessionInfo> > GetSessions();
bool IsLocalDestination(const i2p::data::IdentHash & destination) const { return destination == m_LocalDest->GetIdentHash(); } bool IsLocalDestination(const i2p::data::IdentHash & destination) const { return destination == m_LocalDest->GetIdentHash(); }
private: private:
void HandleRecvFromI2P(const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len); void HandleRecvFromI2P(const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len);

Loading…
Cancel
Save