|
|
@ -796,7 +796,8 @@ namespace client |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void I2PUDPServerTunnel::ExpireStale(const uint64_t delta) { |
|
|
|
void I2PUDPServerTunnel::ExpireStale(const uint64_t delta) |
|
|
|
|
|
|
|
{ |
|
|
|
std::lock_guard<std::mutex> lock(m_SessionsMutex); |
|
|
|
std::lock_guard<std::mutex> lock(m_SessionsMutex); |
|
|
|
uint64_t now = i2p::util::GetMillisecondsSinceEpoch(); |
|
|
|
uint64_t now = i2p::util::GetMillisecondsSinceEpoch(); |
|
|
|
auto itr = m_Sessions.begin(); |
|
|
|
auto itr = m_Sessions.begin(); |
|
|
@ -808,7 +809,8 @@ namespace client |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void I2PUDPClientTunnel::ExpireStale(const uint64_t delta) { |
|
|
|
void I2PUDPClientTunnel::ExpireStale(const uint64_t delta) |
|
|
|
|
|
|
|
{ |
|
|
|
std::lock_guard<std::mutex> lock(m_SessionsMutex); |
|
|
|
std::lock_guard<std::mutex> lock(m_SessionsMutex); |
|
|
|
uint64_t now = i2p::util::GetMillisecondsSinceEpoch(); |
|
|
|
uint64_t now = i2p::util::GetMillisecondsSinceEpoch(); |
|
|
|
std::vector<uint16_t> removePorts; |
|
|
|
std::vector<uint16_t> removePorts; |
|
|
@ -864,7 +866,8 @@ namespace client |
|
|
|
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)); |
|
|
@ -903,29 +906,28 @@ 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) : |
|
|
|
boost::asio::ip::address localAddress, boost::asio::ip::udp::endpoint forwardTo, uint16_t port, bool gzip) : |
|
|
|
m_IsUniqueLocal(true), |
|
|
|
m_IsUniqueLocal (true), m_Name (name), m_LocalAddress (localAddress), m_LocalDest (localDestination), m_RemoteEndpoint (forwardTo), m_Gzip (gzip) |
|
|
|
m_Name(name), |
|
|
|
|
|
|
|
m_LocalAddress(localAddress), |
|
|
|
|
|
|
|
m_RemoteEndpoint(forwardTo) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
m_LocalDest = localDestination; |
|
|
|
|
|
|
|
m_LocalDest->Start(); |
|
|
|
|
|
|
|
auto dgram = m_LocalDest->CreateDatagramDestination(gzip); |
|
|
|
|
|
|
|
dgram->SetReceiver(std::bind(&I2PUDPServerTunnel::HandleRecvFromI2P, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5)); |
|
|
|
|
|
|
|
dgram->SetRawReceiver(std::bind(&I2PUDPServerTunnel::HandleRecvFromI2PRaw, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
I2PUDPServerTunnel::~I2PUDPServerTunnel () |
|
|
|
I2PUDPServerTunnel::~I2PUDPServerTunnel () |
|
|
|
{ |
|
|
|
{ |
|
|
|
auto dgram = m_LocalDest->GetDatagramDestination(); |
|
|
|
Stop (); |
|
|
|
if (dgram) dgram->ResetReceiver(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
LogPrint(eLogInfo, "UDPServer: Done"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void I2PUDPServerTunnel::Start () |
|
|
|
void I2PUDPServerTunnel::Start () |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_LocalDest->Start (); |
|
|
|
m_LocalDest->Start (); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
auto dgram = m_LocalDest->CreateDatagramDestination (m_Gzip); |
|
|
|
|
|
|
|
dgram->SetReceiver (std::bind (&I2PUDPServerTunnel::HandleRecvFromI2P, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5)); |
|
|
|
|
|
|
|
dgram->SetRawReceiver (std::bind (&I2PUDPServerTunnel::HandleRecvFromI2PRaw, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void I2PUDPServerTunnel::Stop () |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
auto dgram = m_LocalDest->GetDatagramDestination (); |
|
|
|
|
|
|
|
if (dgram) dgram->ResetReceiver (); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::vector<std::shared_ptr<DatagramSessionInfo> > I2PUDPServerTunnel::GetSessions () |
|
|
|
std::vector<std::shared_ptr<DatagramSessionInfo> > I2PUDPServerTunnel::GetSessions () |
|
|
@ -954,38 +956,67 @@ namespace client |
|
|
|
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, bool gzip) : |
|
|
|
uint16_t remotePort, bool gzip) : |
|
|
|
m_Name(name), |
|
|
|
m_Name (name), m_RemoteDest (remoteDest), m_LocalDest (localDestination), m_LocalEndpoint (localEndpoint), |
|
|
|
m_RemoteDest(remoteDest), |
|
|
|
m_RemoteIdent (nullptr), m_ResolveThread (nullptr), m_LocalSocket (nullptr), RemotePort (remotePort), |
|
|
|
m_LocalDest(localDestination), |
|
|
|
m_LastPort (0), m_cancel_resolve (false), m_Gzip (gzip) |
|
|
|
m_LocalEndpoint(localEndpoint), |
|
|
|
{ |
|
|
|
m_RemoteIdent(nullptr), |
|
|
|
} |
|
|
|
m_ResolveThread(nullptr), |
|
|
|
|
|
|
|
m_LocalSocket(localDestination->GetService(), localEndpoint), |
|
|
|
I2PUDPClientTunnel::~I2PUDPClientTunnel () |
|
|
|
RemotePort(remotePort), m_LastPort (0), |
|
|
|
{ |
|
|
|
m_cancel_resolve(false) |
|
|
|
Stop (); |
|
|
|
{ |
|
|
|
} |
|
|
|
m_LocalSocket.set_option (boost::asio::socket_base::receive_buffer_size (I2P_UDP_MAX_MTU)); |
|
|
|
|
|
|
|
m_LocalSocket.set_option (boost::asio::socket_base::reuse_address (true)); |
|
|
|
void I2PUDPClientTunnel::Start () |
|
|
|
|
|
|
|
{ |
|
|
|
auto dgram = m_LocalDest->CreateDatagramDestination(gzip); |
|
|
|
// Reset flag in case of tunnel reload
|
|
|
|
|
|
|
|
if (m_cancel_resolve) m_cancel_resolve = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m_LocalSocket.reset (new boost::asio::ip::udp::socket (m_LocalDest->GetService (), m_LocalEndpoint)); |
|
|
|
|
|
|
|
m_LocalSocket->set_option (boost::asio::socket_base::receive_buffer_size (I2P_UDP_MAX_MTU)); |
|
|
|
|
|
|
|
m_LocalSocket->set_option (boost::asio::socket_base::reuse_address (true)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
auto dgram = m_LocalDest->CreateDatagramDestination (m_Gzip); |
|
|
|
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)); |
|
|
|
dgram->SetRawReceiver (std::bind (&I2PUDPClientTunnel::HandleRecvFromI2PRaw, this, |
|
|
|
dgram->SetRawReceiver (std::bind (&I2PUDPClientTunnel::HandleRecvFromI2PRaw, this, |
|
|
|
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)); |
|
|
|
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4)); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)); |
|
|
|
RecvFromLocal (); |
|
|
|
RecvFromLocal (); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void I2PUDPClientTunnel::Stop () |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
auto dgram = m_LocalDest->GetDatagramDestination (); |
|
|
|
|
|
|
|
if (dgram) dgram->ResetReceiver (); |
|
|
|
|
|
|
|
m_cancel_resolve = true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m_Sessions.clear(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(m_LocalSocket && m_LocalSocket->is_open ()) |
|
|
|
|
|
|
|
m_LocalSocket->close (); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(m_ResolveThread) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
m_ResolveThread->join (); |
|
|
|
|
|
|
|
delete m_ResolveThread; |
|
|
|
|
|
|
|
m_ResolveThread = nullptr; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (m_RemoteIdent) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
delete m_RemoteIdent; |
|
|
|
|
|
|
|
m_RemoteIdent = nullptr; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void I2PUDPClientTunnel::RecvFromLocal () |
|
|
|
void I2PUDPClientTunnel::RecvFromLocal () |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_LocalSocket.async_receive_from(boost::asio::buffer(m_RecvBuff, I2P_UDP_MAX_MTU), |
|
|
|
m_LocalSocket->async_receive_from (boost::asio::buffer (m_RecvBuff, I2P_UDP_MAX_MTU), |
|
|
|
m_RecvEndpoint, std::bind (&I2PUDPClientTunnel::HandleRecvFromLocal, this, std::placeholders::_1, std::placeholders::_2)); |
|
|
|
m_RecvEndpoint, std::bind (&I2PUDPClientTunnel::HandleRecvFromLocal, this, std::placeholders::_1, std::placeholders::_2)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -1030,9 +1061,9 @@ namespace client |
|
|
|
while (numPackets < i2p::datagram::DATAGRAM_SEND_QUEUE_MAX_SIZE) |
|
|
|
while (numPackets < i2p::datagram::DATAGRAM_SEND_QUEUE_MAX_SIZE) |
|
|
|
{ |
|
|
|
{ |
|
|
|
boost::system::error_code ec; |
|
|
|
boost::system::error_code ec; |
|
|
|
size_t moreBytes = m_LocalSocket.available(ec); |
|
|
|
size_t moreBytes = m_LocalSocket->available (ec); |
|
|
|
if (ec || !moreBytes) break; |
|
|
|
if (ec || !moreBytes) break; |
|
|
|
transferred = m_LocalSocket.receive_from (boost::asio::buffer (m_RecvBuff, I2P_UDP_MAX_MTU), m_RecvEndpoint, 0, ec); |
|
|
|
transferred = m_LocalSocket->receive_from (boost::asio::buffer (m_RecvBuff, I2P_UDP_MAX_MTU), m_RecvEndpoint, 0, ec); |
|
|
|
remotePort = m_RecvEndpoint.port (); |
|
|
|
remotePort = m_RecvEndpoint.port (); |
|
|
|
// TODO: check remotePort
|
|
|
|
// TODO: check remotePort
|
|
|
|
m_LocalDest->GetDatagramDestination ()->SendRawDatagram (session, m_RecvBuff, transferred, remotePort, RemotePort); |
|
|
|
m_LocalDest->GetDatagramDestination ()->SendRawDatagram (session, m_RecvBuff, transferred, remotePort, RemotePort); |
|
|
@ -1055,7 +1086,8 @@ namespace client |
|
|
|
return infos; |
|
|
|
return infos; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void I2PUDPClientTunnel::TryResolving() { |
|
|
|
void I2PUDPClientTunnel::TryResolving () |
|
|
|
|
|
|
|
{ |
|
|
|
i2p::util::SetThreadName ("UDP Resolver"); |
|
|
|
i2p::util::SetThreadName ("UDP Resolver"); |
|
|
|
LogPrint (eLogInfo, "UDP Tunnel: Trying to resolve ", m_RemoteDest); |
|
|
|
LogPrint (eLogInfo, "UDP Tunnel: Trying to resolve ", m_RemoteDest); |
|
|
|
|
|
|
|
|
|
|
@ -1098,7 +1130,7 @@ namespace client |
|
|
|
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_RemoteIdent ? m_RemoteIdent->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 (); |
|
|
|
} |
|
|
|
} |
|
|
@ -1106,25 +1138,5 @@ namespace client |
|
|
|
else |
|
|
|
else |
|
|
|
LogPrint (eLogWarning, "UDP Client: Not tracking udp session using port ", (int) toPort); |
|
|
|
LogPrint (eLogWarning, "UDP Client: Not tracking udp session using port ", (int) toPort); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
I2PUDPClientTunnel::~I2PUDPClientTunnel() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
auto dgram = m_LocalDest->GetDatagramDestination(); |
|
|
|
|
|
|
|
if (dgram) dgram->ResetReceiver(); |
|
|
|
|
|
|
|
m_cancel_resolve = true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m_Sessions.clear(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(m_LocalSocket.is_open()) |
|
|
|
|
|
|
|
m_LocalSocket.close(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(m_ResolveThread) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
m_ResolveThread->join(); |
|
|
|
|
|
|
|
delete m_ResolveThread; |
|
|
|
|
|
|
|
m_ResolveThread = nullptr; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (m_RemoteIdent) delete m_RemoteIdent; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|