Browse Source

fixes

pull/628/head
Jeff Becker 8 years ago
parent
commit
46d640cd86
No known key found for this signature in database
GPG Key ID: AB950234D6EA286B
  1. 7
      ClientContext.cpp
  2. 41
      I2PTunnel.cpp
  3. 4
      I2PTunnel.h

7
ClientContext.cpp

@ -42,7 +42,9 @@ namespace client
if ( m_ServiceThread == nullptr ) { if ( m_ServiceThread == nullptr ) {
m_ServiceThread = new std::thread([&] () { m_ServiceThread = new std::thread([&] () {
LogPrint(eLogInfo, "ClientContext: starting service");
m_Service.run(); m_Service.run();
LogPrint(eLogError, "ClientContext: service died");
}); });
ScheduleCleanupUDP(); ScheduleCleanupUDP();
} }
@ -394,7 +396,7 @@ namespace client
{ {
localDestination = FindLocalDestination (k.GetPublic ()->GetIdentHash ()); localDestination = FindLocalDestination (k.GetPublic ()->GetIdentHash ());
if (!localDestination) if (!localDestination)
localDestination = CreateNewLocalDestination (k, false, &options); localDestination = CreateNewLocalDestination (k, type == I2P_TUNNELS_SECTION_TYPE_UDPCLIENT, &options);
} }
} }
if (type == I2P_TUNNELS_SECTION_TYPE_UDPCLIENT) { if (type == I2P_TUNNELS_SECTION_TYPE_UDPCLIENT) {
@ -412,7 +414,6 @@ namespace client
clientTunnel->Start(); clientTunnel->Start();
} else { } else {
LogPrint(eLogError, "Clients: I2P Client forward for endpoint ", end, " already exists"); LogPrint(eLogError, "Clients: I2P Client forward for endpoint ", end, " already exists");
delete clientTunnel;
} }
} else { } else {
@ -454,7 +455,6 @@ namespace client
localDestination = FindLocalDestination (k.GetPublic ()->GetIdentHash ()); localDestination = FindLocalDestination (k.GetPublic ()->GetIdentHash ());
if (!localDestination) if (!localDestination)
localDestination = CreateNewLocalDestination (k, true, &options); localDestination = CreateNewLocalDestination (k, true, &options);
if (type == I2P_TUNNELS_SECTION_TYPE_UDPSERVER) { if (type == I2P_TUNNELS_SECTION_TYPE_UDPSERVER) {
// udp server tunnel // udp server tunnel
// TODO: ipv6 and hostnames // TODO: ipv6 and hostnames
@ -469,7 +469,6 @@ namespace client
LogPrint(eLogInfo, "Cleints: I2P Server Forward created for UDP Endpoint ", host, ":", port, " via ",localDestination->GetIdentHash().ToBase32()); LogPrint(eLogInfo, "Cleints: I2P Server Forward created for UDP Endpoint ", host, ":", port, " via ",localDestination->GetIdentHash().ToBase32());
} else { } else {
LogPrint(eLogError, "Clients: I2P Server Forward for destination/port ", m_AddressBook.ToAddress(localDestination->GetIdentHash()), "/", port, "already exists"); LogPrint(eLogError, "Clients: I2P Server Forward for destination/port ", m_AddressBook.ToAddress(localDestination->GetIdentHash()), "/", port, "already exists");
delete serverTunnel;
} }
continue; continue;
} }

41
I2PTunnel.cpp

@ -515,7 +515,6 @@ namespace client
{ {
std::lock_guard<std::mutex> lock(m_SessionsMutex); std::lock_guard<std::mutex> lock(m_SessionsMutex);
auto & session = ObtainUDPSession(from, toPort, fromPort); auto & session = ObtainUDPSession(from, toPort, fromPort);
session.IPSocket.send_to(boost::asio::buffer(buf, len), m_Endpoint); session.IPSocket.send_to(boost::asio::buffer(buf, len), m_Endpoint);
session.LastActivity = i2p::util::GetMillisecondsSinceEpoch(); session.LastActivity = i2p::util::GetMillisecondsSinceEpoch();
@ -535,19 +534,21 @@ namespace client
for ( UDPSession & s : m_Sessions ) { for ( UDPSession & s : m_Sessions ) {
if ( s.Identity == ih) { if ( s.Identity == ih) {
/** found existing */ /** found existing */
LogPrint(eLogDebug, "UDP Server: found ", s.SendEndpoint);
return s; return s;
} }
} }
/** create new */ /** create new */
m_Sessions.push_back(UDPSession(m_Service, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), 0), m_Destination, m_Endpoint, ih, localPort, remotePort)); boost::asio::ip::udp::endpoint ep(boost::asio::ip::address::from_string("127.0.0.1"), 0);
m_Sessions.push_back(UDPSession(m_Service, ep, m_Destination, ep, ih, localPort, remotePort));
return m_Sessions.back(); return m_Sessions.back();
} }
UDPSession::UDPSession(boost::asio::io_service & ios, boost::asio::ip::udp::endpoint localEndpoint, std::shared_ptr<i2p::client::ClientDestination> localDestination, boost::asio::ip::udp::endpoint endpoint, const i2p::data::IdentHash from, uint16_t ourPort, uint16_t theirPort) : UDPSession::UDPSession(boost::asio::io_service & ios, boost::asio::ip::udp::endpoint localEndpoint, std::shared_ptr<i2p::client::ClientDestination> localDestination, boost::asio::ip::udp::endpoint endpoint, const i2p::data::IdentHash to, uint16_t ourPort, uint16_t theirPort) :
Destination(localDestination), Destination(localDestination),
IPSocket(ios, localEndpoint), IPSocket(ios, localEndpoint),
Identity(from), Identity(to),
ExpectedEndpoint(endpoint), SendEndpoint(endpoint),
LastActivity(i2p::util::GetMillisecondsSinceEpoch()), LastActivity(i2p::util::GetMillisecondsSinceEpoch()),
LocalPort(ourPort), LocalPort(ourPort),
RemotePort(theirPort) RemotePort(theirPort)
@ -558,18 +559,24 @@ namespace client
void UDPSession::Receive() { void UDPSession::Receive() {
LogPrint(eLogDebug, "UDPSesssion: Recveive");
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)); 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) void UDPSession::HandleReceived(const boost::system::error_code & ecode, std::size_t len)
{ {
LogPrint(eLogDebug, "UDPSesssion: HandleRecveived");
if(!ecode) { if(!ecode) {
LogPrint(eLogDebug, "UDPSession: forward ", len, "B from ", FromEndpoint); LogPrint(eLogDebug, "UDPSession: forward ", len, "B from ", FromEndpoint);
i2p::datagram::DatagramDestination * dgram = Destination->GetDatagramDestination(); if (Destination) {
if(dgram && FromEndpoint == ExpectedEndpoint) { auto dgram = Destination->CreateDatagramDestination();
LastActivity = i2p::util::GetMillisecondsSinceEpoch(); if(dgram) {
dgram->SendDatagramTo(m_Buffer, len, Identity, LocalPort, RemotePort); LastActivity = i2p::util::GetMillisecondsSinceEpoch();
LogPrint(eLogDebug, "UDPSession: forward to ", Identity.ToBase32()); dgram->SendDatagramTo(m_Buffer, len, Identity, RemotePort, LocalPort);
LogPrint(eLogDebug, "UDPSession: forward ", len, "B to ", Identity.ToBase32());
}
} else {
LogPrint(eLogWarning, "UDPSession: no Local Destination");
} }
Receive(); Receive();
} }
@ -583,7 +590,7 @@ namespace client
{ {
i2p::datagram::DatagramDestination * dgram = m_Destination->CreateDatagramDestination(); i2p::datagram::DatagramDestination * dgram = m_Destination->CreateDatagramDestination();
if(dgram) if(dgram)
dgram->SetReceiver(std::bind(&I2PUDPServerTunnel::HandleRecvFromI2P, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5), LocalPort); 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()
@ -632,11 +639,12 @@ namespace client
auto dgram = m_LocalDest->CreateDatagramDestination(); auto dgram = m_LocalDest->CreateDatagramDestination();
// delete existing session // delete existing session
if(m_Session) delete m_Session; if(m_Session) delete m_Session;
m_Session = new UDPSession(m_Service, m_LocalEndpoint, m_LocalDest, m_LocalEndpoint, *m_RemoteIdent, LocalPort, RemotePort); boost::asio::ip::udp::endpoint ep(boost::asio::ip::address::from_string("127.0.0.1"), 0);
m_Session = new UDPSession(m_Service, m_LocalEndpoint, m_LocalDest, ep, *m_RemoteIdent, LocalPort, RemotePort);
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), LocalPort); std::placeholders::_5));
} }
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)
@ -645,8 +653,13 @@ namespace client
// address match // address match
if(m_Session) { if(m_Session) {
// tell session // tell session
m_Session->IPSocket.send_to(boost::asio::buffer(buf, len), m_LocalEndpoint); LogPrint(eLogDebug, "UDP Client: got ", len, "B from ", from.GetIdentHash().ToBase32(), " via ", m_Session->SendEndpoint);
m_Session->IPSocket.send_to(boost::asio::buffer(buf, len), m_Session->SendEndpoint);
} else {
LogPrint(eLogWarning, "UDP Client: no session");
} }
} else {
LogPrint(eLogWarning, "UDP Client: unwarrented traffic from ", from.GetIdentHash().ToBase32());
} }
} }

4
I2PTunnel.h

@ -143,8 +143,8 @@ namespace client
std::shared_ptr<i2p::client::ClientDestination> Destination; std::shared_ptr<i2p::client::ClientDestination> Destination;
boost::asio::ip::udp::socket IPSocket; boost::asio::ip::udp::socket IPSocket;
i2p::data::IdentHash Identity; i2p::data::IdentHash Identity;
boost::asio::ip::udp::endpoint ExpectedEndpoint;
boost::asio::ip::udp::endpoint FromEndpoint; boost::asio::ip::udp::endpoint FromEndpoint;
boost::asio::ip::udp::endpoint SendEndpoint;
uint64_t LastActivity; uint64_t LastActivity;
uint16_t LocalPort; uint16_t LocalPort;
@ -193,8 +193,8 @@ namespace client
UDPSession * m_Session; UDPSession * m_Session;
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;
i2p::data::IdentHash * m_RemoteIdent;
const boost::asio::ip::udp::endpoint m_LocalEndpoint; const boost::asio::ip::udp::endpoint m_LocalEndpoint;
i2p::data::IdentHash * m_RemoteIdent;
std::thread * m_ResolveThread; std::thread * m_ResolveThread;
boost::asio::io_service & m_Service; boost::asio::io_service & m_Service;
uint16_t LocalPort; uint16_t LocalPort;

Loading…
Cancel
Save