Browse Source

store Address instead IdentHash for UDP tunnel

pull/1804/head
orignal 2 years ago
parent
commit
b8ce0b0838
  1. 38
      libi2pd_client/UDPTunnel.cpp
  2. 11
      libi2pd_client/UDPTunnel.h

38
libi2pd_client/UDPTunnel.cpp

@ -85,24 +85,23 @@ namespace client
else else
addr = m_LocalAddress; addr = m_LocalAddress;
boost::asio::ip::udp::endpoint ep(addr, 0); boost::asio::ip::udp::endpoint ep(addr, 0);
m_Sessions.push_back(std::make_shared<UDPSession>(ep, m_LocalDest, m_RemoteEndpoint, &ih, localPort, remotePort)); m_Sessions.push_back(std::make_shared<UDPSession>(ep, m_LocalDest, m_RemoteEndpoint, ih, localPort, remotePort));
auto & back = m_Sessions.back(); auto & back = m_Sessions.back();
return back; return back;
} }
UDPSession::UDPSession(boost::asio::ip::udp::endpoint localEndpoint, UDPSession::UDPSession(boost::asio::ip::udp::endpoint localEndpoint,
const std::shared_ptr<i2p::client::ClientDestination> & localDestination, const std::shared_ptr<i2p::client::ClientDestination> & localDestination,
boost::asio::ip::udp::endpoint endpoint, const i2p::data::IdentHash * to, const boost::asio::ip::udp::endpoint& endpoint, const i2p::data::IdentHash& to,
uint16_t ourPort, uint16_t theirPort) : uint16_t ourPort, uint16_t theirPort) :
m_Destination(localDestination->GetDatagramDestination()), m_Destination(localDestination->GetDatagramDestination()),
IPSocket(localDestination->GetService(), localEndpoint), IPSocket(localDestination->GetService(), localEndpoint),
SendEndpoint(endpoint), Identity (to), SendEndpoint(endpoint),
LastActivity(i2p::util::GetMillisecondsSinceEpoch()), LastActivity(i2p::util::GetMillisecondsSinceEpoch()),
LocalPort(ourPort), LocalPort(ourPort),
RemotePort(theirPort) RemotePort(theirPort)
{ {
IPSocket.set_option (boost::asio::socket_base::receive_buffer_size (I2P_UDP_MAX_MTU )); IPSocket.set_option (boost::asio::socket_base::receive_buffer_size (I2P_UDP_MAX_MTU ));
memcpy(Identity, to->data(), 32);
Receive(); Receive();
} }
@ -145,7 +144,7 @@ namespace client
} }
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,
boost::asio::ip::address localAddress, boost::asio::ip::udp::endpoint forwardTo, uint16_t port, bool gzip) : const boost::asio::ip::address& localAddress, const boost::asio::ip::udp::endpoint& forwardTo, uint16_t port, bool gzip) :
m_IsUniqueLocal (true), m_Name (name), m_LocalAddress (localAddress), m_IsUniqueLocal (true), m_Name (name), m_LocalAddress (localAddress),
m_RemoteEndpoint (forwardTo), m_LocalDest (localDestination), m_Gzip (gzip) m_RemoteEndpoint (forwardTo), m_LocalDest (localDestination), m_Gzip (gzip)
{ {
@ -194,11 +193,11 @@ namespace client
} }
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, const boost::asio::ip::udp::endpoint& localEndpoint,
std::shared_ptr<i2p::client::ClientDestination> localDestination, std::shared_ptr<i2p::client::ClientDestination> localDestination,
uint16_t remotePort, bool gzip) : uint16_t remotePort, bool gzip) :
m_Name (name), m_RemoteDest (remoteDest), m_LocalDest (localDestination), m_LocalEndpoint (localEndpoint), m_Name (name), m_RemoteDest (remoteDest), m_LocalDest (localDestination), m_LocalEndpoint (localEndpoint),
m_RemoteIdent (nullptr), m_ResolveThread (nullptr), m_LocalSocket (nullptr), RemotePort (remotePort), m_ResolveThread (nullptr), m_LocalSocket (nullptr), RemotePort (remotePort),
m_LastPort (0), m_cancel_resolve (false), m_Gzip (gzip) m_LastPort (0), m_cancel_resolve (false), m_Gzip (gzip)
{ {
} }
@ -248,11 +247,7 @@ namespace client
delete m_ResolveThread; delete m_ResolveThread;
m_ResolveThread = nullptr; m_ResolveThread = nullptr;
} }
if (m_RemoteIdent) m_RemoteAddr = nullptr;
{
delete m_RemoteIdent;
m_RemoteIdent = nullptr;
}
} }
void I2PUDPClientTunnel::RecvFromLocal () void I2PUDPClientTunnel::RecvFromLocal ()
@ -272,7 +267,8 @@ namespace client
RecvFromLocal (); // Restart listener and continue work RecvFromLocal (); // Restart listener and continue work
return; return;
} }
if (!m_RemoteIdent) { if (!m_RemoteAddr || !m_RemoteAddr->IsIdentHash ()) // TODO: handle B33
{
LogPrint (eLogWarning, "UDP Client: Remote endpoint not resolved yet"); LogPrint (eLogWarning, "UDP Client: Remote endpoint not resolved yet");
RecvFromLocal (); RecvFromLocal ();
return; // drop, remote not resolved return; // drop, remote not resolved
@ -292,8 +288,8 @@ namespace client
} }
// send off to remote i2p destination // send off to remote i2p destination
auto ts = i2p::util::GetMillisecondsSinceEpoch (); auto ts = i2p::util::GetMillisecondsSinceEpoch ();
LogPrint (eLogDebug, "UDP Client: Send ", transferred, " to ", m_RemoteIdent->ToBase32 (), ":", RemotePort); LogPrint (eLogDebug, "UDP Client: Send ", transferred, " to ", m_RemoteAddr->identHash.ToBase32 (), ":", RemotePort);
auto session = m_LocalDest->GetDatagramDestination ()->GetSession (*m_RemoteIdent); auto session = m_LocalDest->GetDatagramDestination ()->GetSession (m_RemoteAddr->identHash);
if (ts > m_LastSession->second + I2P_UDP_REPLIABLE_DATAGRAM_INTERVAL) if (ts > m_LastSession->second + I2P_UDP_REPLIABLE_DATAGRAM_INTERVAL)
m_LocalDest->GetDatagramDestination ()->SendDatagram (session, m_RecvBuff, transferred, remotePort, RemotePort); m_LocalDest->GetDatagramDestination ()->SendDatagram (session, m_RecvBuff, transferred, remotePort, RemotePort);
else else
@ -311,7 +307,7 @@ namespace client
numPackets++; numPackets++;
} }
if (numPackets) if (numPackets)
LogPrint (eLogDebug, "UDP Client: Sent ", numPackets, " more packets to ", m_RemoteIdent->ToBase32 ()); LogPrint (eLogDebug, "UDP Client: Sent ", numPackets, " more packets to ", m_RemoteAddr->identHash.ToBase32 ());
m_LocalDest->GetDatagramDestination ()->FlushSendQueue (session); m_LocalDest->GetDatagramDestination ()->FlushSendQueue (session);
// mark convo as active // mark convo as active
@ -343,19 +339,17 @@ namespace client
LogPrint(eLogError, "UDP Tunnel: Lookup of ", m_RemoteDest, " was cancelled"); LogPrint(eLogError, "UDP Tunnel: Lookup of ", m_RemoteDest, " was cancelled");
return; return;
} }
if (!addr || !addr->IsIdentHash ()) if (!addr)
{ {
LogPrint (eLogError, "UDP Tunnel: ", m_RemoteDest, " not found"); LogPrint (eLogError, "UDP Tunnel: ", m_RemoteDest, " not found");
return; return;
} }
m_RemoteIdent = new i2p::data::IdentHash; LogPrint(eLogInfo, "UDP Tunnel: Resolved ", m_RemoteDest, " to ", m_RemoteAddr->identHash.ToBase32 ());
*m_RemoteIdent = addr->identHash;
LogPrint(eLogInfo, "UDP Tunnel: Resolved ", m_RemoteDest, " to ", m_RemoteIdent->ToBase32 ());
} }
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_RemoteAddr && from.GetIdentHash() == m_RemoteAddr->identHash)
HandleRecvFromI2PRaw (fromPort, toPort, buf, len); HandleRecvFromI2PRaw (fromPort, toPort, buf, len);
else else
LogPrint(eLogWarning, "UDP Client: Unwarranted traffic from ", from.GetIdentHash().ToBase32 ()); LogPrint(eLogWarning, "UDP Client: Unwarranted traffic from ", from.GetIdentHash().ToBase32 ());
@ -370,7 +364,7 @@ namespace client
// found convo // found convo
if (len > 0) if (len > 0)
{ {
LogPrint (eLogDebug, "UDP Client: Got ", len, "B from ", m_RemoteIdent ? m_RemoteIdent->ToBase32 () : ""); LogPrint (eLogDebug, "UDP Client: Got ", len, "B from ", m_RemoteAddr ? m_RemoteAddr->identHash.ToBase32 () : "");
m_LocalSocket->send_to (boost::asio::buffer (buf, len), itr->second->first); m_LocalSocket->send_to (boost::asio::buffer (buf, len), itr->second->first);
// mark convo as active // mark convo as active
itr->second->second = i2p::util::GetMillisecondsSinceEpoch (); itr->second->second = i2p::util::GetMillisecondsSinceEpoch ();

11
libi2pd_client/UDPTunnel.h

@ -19,6 +19,7 @@
#include "Identity.h" #include "Identity.h"
#include "Destination.h" #include "Destination.h"
#include "Datagram.h" #include "Datagram.h"
#include "AddressBook.h"
namespace i2p namespace i2p
{ {
@ -47,7 +48,7 @@ namespace client
UDPSession(boost::asio::ip::udp::endpoint localEndpoint, UDPSession(boost::asio::ip::udp::endpoint localEndpoint,
const std::shared_ptr<i2p::client::ClientDestination> & localDestination, const std::shared_ptr<i2p::client::ClientDestination> & localDestination,
boost::asio::ip::udp::endpoint remote, const i2p::data::IdentHash * ident, const boost::asio::ip::udp::endpoint& remote, const i2p::data::IdentHash& ident,
uint16_t ourPort, uint16_t theirPort); uint16_t ourPort, uint16_t theirPort);
void HandleReceived(const boost::system::error_code & ecode, std::size_t len); void HandleReceived(const boost::system::error_code & ecode, std::size_t len);
void Receive(); void Receive();
@ -84,8 +85,8 @@ namespace client
I2PUDPServerTunnel (const std::string & name, I2PUDPServerTunnel (const std::string & name,
std::shared_ptr<i2p::client::ClientDestination> localDestination, std::shared_ptr<i2p::client::ClientDestination> localDestination,
boost::asio::ip::address localAddress, const boost::asio::ip::address& localAddress,
boost::asio::ip::udp::endpoint forwardTo, uint16_t port, bool gzip); const boost::asio::ip::udp::endpoint& forwardTo, uint16_t port, bool gzip);
~I2PUDPServerTunnel (); ~I2PUDPServerTunnel ();
/** expire stale udp conversations */ /** expire stale udp conversations */
@ -126,7 +127,7 @@ namespace client
public: public:
I2PUDPClientTunnel (const std::string & name, const std::string &remoteDest, I2PUDPClientTunnel (const std::string & name, const std::string &remoteDest,
boost::asio::ip::udp::endpoint localEndpoint, std::shared_ptr<i2p::client::ClientDestination> localDestination, const boost::asio::ip::udp::endpoint& localEndpoint, std::shared_ptr<i2p::client::ClientDestination> localDestination,
uint16_t remotePort, bool gzip); uint16_t remotePort, bool gzip);
~I2PUDPClientTunnel (); ~I2PUDPClientTunnel ();
@ -164,7 +165,7 @@ namespace client
const std::string m_RemoteDest; const std::string m_RemoteDest;
std::shared_ptr<i2p::client::ClientDestination> m_LocalDest; std::shared_ptr<i2p::client::ClientDestination> m_LocalDest;
const boost::asio::ip::udp::endpoint m_LocalEndpoint; const boost::asio::ip::udp::endpoint m_LocalEndpoint;
i2p::data::IdentHash * m_RemoteIdent; std::shared_ptr<const Address> m_RemoteAddr;
std::thread * m_ResolveThread; std::thread * m_ResolveThread;
std::unique_ptr<boost::asio::ip::udp::socket> m_LocalSocket; std::unique_ptr<boost::asio::ip::udp::socket> m_LocalSocket;
boost::asio::ip::udp::endpoint m_RecvEndpoint; boost::asio::ip::udp::endpoint m_RecvEndpoint;

Loading…
Cancel
Save