|
|
|
@ -569,207 +569,200 @@ namespace client
@@ -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<i2p::client::ClientDestination> 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<i2p::client::ClientDestination> 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<DatagramSessionInfo> I2PUDPServerTunnel::GetSessions() |
|
|
|
|
std::vector<std::shared_ptr<DatagramSessionInfo> > I2PUDPServerTunnel::GetSessions() |
|
|
|
|
{ |
|
|
|
|
std::vector<DatagramSessionInfo> sessions; |
|
|
|
|
auto localident = m_LocalDest->GetIdentHash(); |
|
|
|
|
std::vector<std::shared_ptr<DatagramSessionInfo> > sessions; |
|
|
|
|
std::lock_guard<std::mutex> 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<DatagramSessionInfo>(); |
|
|
|
|
sinfo->Name = m_Name; |
|
|
|
|
sinfo->LocalIdent = std::make_shared<i2p::data::IdentHash>(m_LocalDest->GetIdentHash().data()); |
|
|
|
|
sinfo->RemoteIdent = std::make_shared<i2p::data::IdentHash>(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<i2p::client::ClientDestination> 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<i2p::client::ClientDestination> 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<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) |
|
|
|
|
{ |
|
|
|
|
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<DatagramSessionInfo>(); |
|
|
|
|
sinfo->Name = m_Name; |
|
|
|
|
sinfo->LocalIdent = std::make_shared<i2p::data::IdentHash>(m_LocalDest->GetIdentHash().data()); |
|
|
|
|
sinfo->RemoteIdent = std::make_shared<i2p::data::IdentHash>(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; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|