Browse Source

windows warnings, tabulation workaround

pull/972/head
R4SAS 7 years ago
parent
commit
057d6ca05b
  1. 3
      Win32/DaemonWin32.cpp
  2. 51
      Win32/Win32Service.cpp
  3. 16
      Win32/Win32Service.h
  4. 71
      libi2pd_client/I2PService.cpp
  5. 9
      libi2pd_client/I2PService.h
  6. 6
      libi2pd_client/I2PTunnel.cpp
  7. 27
      libi2pd_client/I2PTunnel.h

3
Win32/DaemonWin32.cpp

@ -6,7 +6,6 @@
#include "Log.h" #include "Log.h"
#ifdef _WIN32 #ifdef _WIN32
#include "Win32/Win32Service.h" #include "Win32/Win32Service.h"
#ifdef WIN32_APP #ifdef WIN32_APP
#include "Win32/Win32App.h" #include "Win32/Win32App.h"
@ -109,4 +108,4 @@ namespace util
} }
} }
} }
#endif #endif //_WIN32

51
Win32/Win32Service.cpp

@ -15,7 +15,6 @@ I2PService *I2PService::s_service = NULL;
BOOL I2PService::isService() BOOL I2PService::isService()
{ {
BOOL bIsService = FALSE; BOOL bIsService = FALSE;
HWINSTA hWinStation = GetProcessWindowStation(); HWINSTA hWinStation = GetProcessWindowStation();
if (hWinStation != NULL) if (hWinStation != NULL)
{ {
@ -31,28 +30,23 @@ BOOL I2PService::isService()
BOOL I2PService::Run(I2PService &service) BOOL I2PService::Run(I2PService &service)
{ {
s_service = &service; s_service = &service;
SERVICE_TABLE_ENTRY serviceTable[] = SERVICE_TABLE_ENTRY serviceTable[] =
{ {
{ service.m_name, ServiceMain }, { service.m_name, ServiceMain },
{ NULL, NULL } { NULL, NULL }
}; };
return StartServiceCtrlDispatcher(serviceTable); return StartServiceCtrlDispatcher(serviceTable);
} }
void WINAPI I2PService::ServiceMain(DWORD dwArgc, PSTR *pszArgv) void WINAPI I2PService::ServiceMain(DWORD dwArgc, PSTR *pszArgv)
{ {
assert(s_service != NULL); assert(s_service != NULL);
s_service->m_statusHandle = RegisterServiceCtrlHandler( s_service->m_statusHandle = RegisterServiceCtrlHandler(
s_service->m_name, ServiceCtrlHandler); s_service->m_name, ServiceCtrlHandler);
if (s_service->m_statusHandle == NULL) if (s_service->m_statusHandle == NULL)
{ {
throw GetLastError(); throw GetLastError();
} }
s_service->Start(dwArgc, pszArgv); s_service->Start(dwArgc, pszArgv);
} }
@ -76,11 +70,8 @@ I2PService::I2PService(PSTR pszServiceName,
BOOL fCanPauseContinue) BOOL fCanPauseContinue)
{ {
m_name = (pszServiceName == NULL) ? (PSTR)"" : pszServiceName; m_name = (pszServiceName == NULL) ? (PSTR)"" : pszServiceName;
m_statusHandle = NULL; m_statusHandle = NULL;
m_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS; m_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
m_status.dwCurrentState = SERVICE_START_PENDING; m_status.dwCurrentState = SERVICE_START_PENDING;
DWORD dwControlsAccepted = 0; DWORD dwControlsAccepted = 0;
@ -90,15 +81,13 @@ I2PService::I2PService(PSTR pszServiceName,
dwControlsAccepted |= SERVICE_ACCEPT_SHUTDOWN; dwControlsAccepted |= SERVICE_ACCEPT_SHUTDOWN;
if (fCanPauseContinue) if (fCanPauseContinue)
dwControlsAccepted |= SERVICE_ACCEPT_PAUSE_CONTINUE; dwControlsAccepted |= SERVICE_ACCEPT_PAUSE_CONTINUE;
m_status.dwControlsAccepted = dwControlsAccepted;
m_status.dwControlsAccepted = dwControlsAccepted;
m_status.dwWin32ExitCode = NO_ERROR; m_status.dwWin32ExitCode = NO_ERROR;
m_status.dwServiceSpecificExitCode = 0; m_status.dwServiceSpecificExitCode = 0;
m_status.dwCheckPoint = 0; m_status.dwCheckPoint = 0;
m_status.dwWaitHint = 0; m_status.dwWaitHint = 0;
m_fStopping = FALSE; m_fStopping = FALSE;
// Create a manual-reset event that is not signaled at first to indicate // Create a manual-reset event that is not signaled at first to indicate
// the stopped signal of the service. // the stopped signal of the service.
m_hStoppedEvent = CreateEvent(NULL, TRUE, FALSE, NULL); m_hStoppedEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
@ -108,7 +97,6 @@ I2PService::I2PService(PSTR pszServiceName,
} }
} }
I2PService::~I2PService(void) I2PService::~I2PService(void)
{ {
if (m_hStoppedEvent) if (m_hStoppedEvent)
@ -118,92 +106,73 @@ I2PService::~I2PService(void)
} }
} }
void I2PService::Start(DWORD dwArgc, PSTR *pszArgv) void I2PService::Start(DWORD dwArgc, PSTR *pszArgv)
{ {
try try
{ {
SetServiceStatus(SERVICE_START_PENDING); SetServiceStatus(SERVICE_START_PENDING);
OnStart(dwArgc, pszArgv); OnStart(dwArgc, pszArgv);
SetServiceStatus(SERVICE_RUNNING); SetServiceStatus(SERVICE_RUNNING);
} }
catch (DWORD dwError) catch (DWORD dwError)
{ {
LogPrint(eLogError, "Win32Service Start", dwError); LogPrint(eLogError, "Win32Service Start", dwError);
SetServiceStatus(SERVICE_STOPPED, dwError); SetServiceStatus(SERVICE_STOPPED, dwError);
} }
catch (...) catch (...)
{ {
LogPrint(eLogError, "Win32Service failed to start.", EVENTLOG_ERROR_TYPE); LogPrint(eLogError, "Win32Service failed to start.", EVENTLOG_ERROR_TYPE);
SetServiceStatus(SERVICE_STOPPED); SetServiceStatus(SERVICE_STOPPED);
} }
} }
void I2PService::OnStart(DWORD dwArgc, PSTR *pszArgv) void I2PService::OnStart(DWORD dwArgc, PSTR *pszArgv)
{ {
LogPrint(eLogInfo, "Win32Service in OnStart", EVENTLOG_INFORMATION_TYPE); LogPrint(eLogInfo, "Win32Service in OnStart", EVENTLOG_INFORMATION_TYPE);
Daemon.start(); Daemon.start();
//i2p::util::config::OptionParser(dwArgc, pszArgv); //i2p::util::config::OptionParser(dwArgc, pszArgv);
//i2p::util::filesystem::ReadConfigFile(i2p::util::config::mapArgs, i2p::util::config::mapMultiArgs); //i2p::util::filesystem::ReadConfigFile(i2p::util::config::mapArgs, i2p::util::config::mapMultiArgs);
//i2p::context.OverrideNTCPAddress(i2p::util::config::GetCharArg("-host", "127.0.0.1"), //i2p::context.OverrideNTCPAddress(i2p::util::config::GetCharArg("-host", "127.0.0.1"),
// i2p::util::config::GetArg("-port", 17070)); // i2p::util::config::GetArg("-port", 17070));
_worker = new std::thread(std::bind(&I2PService::WorkerThread, this)); _worker = new std::thread(std::bind(&I2PService::WorkerThread, this));
} }
void I2PService::WorkerThread() void I2PService::WorkerThread()
{ {
while (!m_fStopping) while (!m_fStopping)
{ {
::Sleep(1000); // Simulate some lengthy operations. ::Sleep(1000); // Simulate some lengthy operations.
} }
// Signal the stopped event. // Signal the stopped event.
SetEvent(m_hStoppedEvent); SetEvent(m_hStoppedEvent);
} }
void I2PService::Stop() void I2PService::Stop()
{ {
DWORD dwOriginalState = m_status.dwCurrentState; DWORD dwOriginalState = m_status.dwCurrentState;
try try
{ {
SetServiceStatus(SERVICE_STOP_PENDING); SetServiceStatus(SERVICE_STOP_PENDING);
OnStop(); OnStop();
SetServiceStatus(SERVICE_STOPPED); SetServiceStatus(SERVICE_STOPPED);
} }
catch (DWORD dwError) catch (DWORD dwError)
{ {
LogPrint(eLogInfo, "Win32Service Stop", dwError); LogPrint(eLogInfo, "Win32Service Stop", dwError);
SetServiceStatus(dwOriginalState); SetServiceStatus(dwOriginalState);
} }
catch (...) catch (...)
{ {
LogPrint(eLogError, "Win32Service failed to stop.", EVENTLOG_ERROR_TYPE); LogPrint(eLogError, "Win32Service failed to stop.", EVENTLOG_ERROR_TYPE);
SetServiceStatus(dwOriginalState); SetServiceStatus(dwOriginalState);
} }
} }
void I2PService::OnStop() void I2PService::OnStop()
{ {
// Log a service stop message to the Application log. // Log a service stop message to the Application log.
LogPrint(eLogInfo, "Win32Service in OnStop", EVENTLOG_INFORMATION_TYPE); LogPrint(eLogInfo, "Win32Service in OnStop", EVENTLOG_INFORMATION_TYPE);
Daemon.stop(); Daemon.stop();
m_fStopping = TRUE; m_fStopping = TRUE;
if (WaitForSingleObject(m_hStoppedEvent, INFINITE) != WAIT_OBJECT_0) if (WaitForSingleObject(m_hStoppedEvent, INFINITE) != WAIT_OBJECT_0)
{ {
@ -213,57 +182,46 @@ void I2PService::OnStop()
delete _worker; delete _worker;
} }
void I2PService::Pause() void I2PService::Pause()
{ {
try try
{ {
SetServiceStatus(SERVICE_PAUSE_PENDING); SetServiceStatus(SERVICE_PAUSE_PENDING);
OnPause(); OnPause();
SetServiceStatus(SERVICE_PAUSED); SetServiceStatus(SERVICE_PAUSED);
} }
catch (DWORD dwError) catch (DWORD dwError)
{ {
LogPrint(eLogError, "Win32Service Pause", dwError); LogPrint(eLogError, "Win32Service Pause", dwError);
SetServiceStatus(SERVICE_RUNNING); SetServiceStatus(SERVICE_RUNNING);
} }
catch (...) catch (...)
{ {
LogPrint(eLogError, "Win32Service failed to pause.", EVENTLOG_ERROR_TYPE); LogPrint(eLogError, "Win32Service failed to pause.", EVENTLOG_ERROR_TYPE);
SetServiceStatus(SERVICE_RUNNING); SetServiceStatus(SERVICE_RUNNING);
} }
} }
void I2PService::OnPause() void I2PService::OnPause()
{ {
} }
void I2PService::Continue() void I2PService::Continue()
{ {
try try
{ {
SetServiceStatus(SERVICE_CONTINUE_PENDING); SetServiceStatus(SERVICE_CONTINUE_PENDING);
OnContinue(); OnContinue();
SetServiceStatus(SERVICE_RUNNING); SetServiceStatus(SERVICE_RUNNING);
} }
catch (DWORD dwError) catch (DWORD dwError)
{ {
LogPrint(eLogError, "Win32Service Continue", dwError); LogPrint(eLogError, "Win32Service Continue", dwError);
SetServiceStatus(SERVICE_PAUSED); SetServiceStatus(SERVICE_PAUSED);
} }
catch (...) catch (...)
{ {
LogPrint(eLogError, "Win32Service failed to resume.", EVENTLOG_ERROR_TYPE); LogPrint(eLogError, "Win32Service failed to resume.", EVENTLOG_ERROR_TYPE);
SetServiceStatus(SERVICE_PAUSED); SetServiceStatus(SERVICE_PAUSED);
} }
} }
@ -277,7 +235,6 @@ void I2PService::Shutdown()
try try
{ {
OnShutdown(); OnShutdown();
SetServiceStatus(SERVICE_STOPPED); SetServiceStatus(SERVICE_STOPPED);
} }
catch (DWORD dwError) catch (DWORD dwError)
@ -299,11 +256,9 @@ void I2PService::SetServiceStatus(DWORD dwCurrentState,
DWORD dwWaitHint) DWORD dwWaitHint)
{ {
static DWORD dwCheckPoint = 1; static DWORD dwCheckPoint = 1;
m_status.dwCurrentState = dwCurrentState; m_status.dwCurrentState = dwCurrentState;
m_status.dwWin32ExitCode = dwWin32ExitCode; m_status.dwWin32ExitCode = dwWin32ExitCode;
m_status.dwWaitHint = dwWaitHint; m_status.dwWaitHint = dwWaitHint;
m_status.dwCheckPoint = m_status.dwCheckPoint =
((dwCurrentState == SERVICE_RUNNING) || ((dwCurrentState == SERVICE_RUNNING) ||
(dwCurrentState == SERVICE_STOPPED)) ? (dwCurrentState == SERVICE_STOPPED)) ?
@ -328,7 +283,7 @@ void FreeHandles(SC_HANDLE schSCManager, SC_HANDLE schService)
} }
} }
void InstallService(PSTR pszServiceName, PSTR pszDisplayName, DWORD dwStartType, PSTR pszDependencies, PSTR pszAccount, PSTR pszPassword) void InstallService(PCSTR pszServiceName, PCSTR pszDisplayName, DWORD dwStartType, PCSTR pszDependencies, PCSTR pszAccount, PCSTR pszPassword)
{ {
printf("Try to install Win32Service (%s).\n", pszServiceName); printf("Try to install Win32Service (%s).\n", pszServiceName);
@ -383,7 +338,7 @@ void InstallService(PSTR pszServiceName, PSTR pszDisplayName, DWORD dwStartType,
FreeHandles(schSCManager, schService); FreeHandles(schSCManager, schService);
} }
void UninstallService(PSTR pszServiceName) void UninstallService(PCSTR pszServiceName)
{ {
printf("Try to uninstall Win32Service (%s).\n", pszServiceName); printf("Try to uninstall Win32Service (%s).\n", pszServiceName);

16
Win32/Win32Service.h

@ -4,7 +4,6 @@
#include <thread> #include <thread>
#include <windows.h> #include <windows.h>
#ifdef _WIN32 #ifdef _WIN32
// Internal name of the service // Internal name of the service
#define SERVICE_NAME "i2pdService" #define SERVICE_NAME "i2pdService"
@ -25,7 +24,6 @@
#define SERVICE_PASSWORD NULL #define SERVICE_PASSWORD NULL
#endif #endif
class I2PService class I2PService
{ {
public: public:
@ -72,13 +70,15 @@ private:
std::thread* _worker; std::thread* _worker;
}; };
void InstallService(PSTR pszServiceName, void InstallService(
PSTR pszDisplayName, PCSTR pszServiceName,
PCSTR pszDisplayName,
DWORD dwStartType, DWORD dwStartType,
PSTR pszDependencies, PCSTR pszDependencies,
PSTR pszAccount, PCSTR pszAccount,
PSTR pszPassword); PCSTR pszPassword
);
void UninstallService(PSTR pszServiceName); void UninstallService(PCSTR pszServiceName);
#endif // WIN_32_SERVICE_H__ #endif // WIN_32_SERVICE_H__

71
libi2pd_client/I2PService.cpp

@ -104,9 +104,7 @@ namespace client
assert(streamRequestComplete); assert(streamRequestComplete);
i2p::data::IdentHash identHash; i2p::data::IdentHash identHash;
if (i2p::client::context.GetAddressBook ().GetIdentHash (dest, identHash)) if (i2p::client::context.GetAddressBook ().GetIdentHash (dest, identHash))
{
CreateStream(streamRequestComplete, identHash, port); CreateStream(streamRequestComplete, identHash, port);
}
else else
{ {
LogPrint (eLogWarning, "I2PService: Remote destination not found: ", dest); LogPrint (eLogWarning, "I2PService: Remote destination not found: ", dest);
@ -158,16 +156,16 @@ namespace client
void TCPIPPipe::Terminate() void TCPIPPipe::Terminate()
{ {
if(Kill()) return; if(Kill()) return;
if (m_up) { if (m_up)
if (m_up->is_open()) { {
if (m_up->is_open())
m_up->close(); m_up->close();
}
m_up = nullptr; m_up = nullptr;
} }
if (m_down) { if (m_down)
if (m_down->is_open()) { {
if (m_down->is_open())
m_down->close(); m_down->close();
}
m_down = nullptr; m_down = nullptr;
} }
Done(shared_from_this()); Done(shared_from_this());
@ -175,13 +173,14 @@ namespace client
void TCPIPPipe::AsyncReceiveUpstream() void TCPIPPipe::AsyncReceiveUpstream()
{ {
if (m_up) { if (m_up)
{
m_up->async_read_some(boost::asio::buffer(m_upstream_to_down_buf, TCP_IP_PIPE_BUFFER_SIZE), m_up->async_read_some(boost::asio::buffer(m_upstream_to_down_buf, TCP_IP_PIPE_BUFFER_SIZE),
std::bind(&TCPIPPipe::HandleUpstreamReceived, shared_from_this(), std::bind(&TCPIPPipe::HandleUpstreamReceived, shared_from_this(),
std::placeholders::_1, std::placeholders::_2)); std::placeholders::_1, std::placeholders::_2));
} else {
LogPrint(eLogError, "TCPIPPipe: upstream receive: no socket");
} }
else
LogPrint(eLogError, "TCPIPPipe: upstream receive: no socket");
} }
void TCPIPPipe::AsyncReceiveDownstream() void TCPIPPipe::AsyncReceiveDownstream()
@ -190,88 +189,90 @@ namespace client
m_down->async_read_some(boost::asio::buffer(m_downstream_to_up_buf, TCP_IP_PIPE_BUFFER_SIZE), m_down->async_read_some(boost::asio::buffer(m_downstream_to_up_buf, TCP_IP_PIPE_BUFFER_SIZE),
std::bind(&TCPIPPipe::HandleDownstreamReceived, shared_from_this(), std::bind(&TCPIPPipe::HandleDownstreamReceived, shared_from_this(),
std::placeholders::_1, std::placeholders::_2)); std::placeholders::_1, std::placeholders::_2));
} else {
LogPrint(eLogError, "TCPIPPipe: downstream receive: no socket");
} }
else
LogPrint(eLogError, "TCPIPPipe: downstream receive: no socket");
} }
void TCPIPPipe::UpstreamWrite(size_t len) void TCPIPPipe::UpstreamWrite(size_t len)
{ {
if (m_up) { if (m_up)
{
LogPrint(eLogDebug, "TCPIPPipe: upstream: ", (int) len, " bytes written"); LogPrint(eLogDebug, "TCPIPPipe: upstream: ", (int) len, " bytes written");
boost::asio::async_write(*m_up, boost::asio::buffer(m_upstream_buf, len), boost::asio::async_write(*m_up, boost::asio::buffer(m_upstream_buf, len),
boost::asio::transfer_all(), boost::asio::transfer_all(),
std::bind(&TCPIPPipe::HandleUpstreamWrite, std::bind(&TCPIPPipe::HandleUpstreamWrite,
shared_from_this(), shared_from_this(),
std::placeholders::_1) std::placeholders::_1));
);
} else {
LogPrint(eLogError, "TCPIPPipe: upstream write: no socket");
} }
else
LogPrint(eLogError, "TCPIPPipe: upstream write: no socket");
} }
void TCPIPPipe::DownstreamWrite(size_t len) void TCPIPPipe::DownstreamWrite(size_t len)
{ {
if (m_down) { if (m_down)
{
LogPrint(eLogDebug, "TCPIPPipe: downstream: ", (int) len, " bytes written"); LogPrint(eLogDebug, "TCPIPPipe: downstream: ", (int) len, " bytes written");
boost::asio::async_write(*m_down, boost::asio::buffer(m_downstream_buf, len), boost::asio::async_write(*m_down, boost::asio::buffer(m_downstream_buf, len),
boost::asio::transfer_all(), boost::asio::transfer_all(),
std::bind(&TCPIPPipe::HandleDownstreamWrite, std::bind(&TCPIPPipe::HandleDownstreamWrite,
shared_from_this(), shared_from_this(),
std::placeholders::_1) std::placeholders::_1));
);
} else {
LogPrint(eLogError, "TCPIPPipe: downstream write: no socket");
} }
else
LogPrint(eLogError, "TCPIPPipe: downstream write: no socket");
} }
void TCPIPPipe::HandleDownstreamReceived(const boost::system::error_code & ecode, std::size_t bytes_transfered) void TCPIPPipe::HandleDownstreamReceived(const boost::system::error_code & ecode, std::size_t bytes_transfered)
{ {
LogPrint(eLogDebug, "TCPIPPipe: downstream: ", (int) bytes_transfered, " bytes received"); LogPrint(eLogDebug, "TCPIPPipe: downstream: ", (int) bytes_transfered, " bytes received");
if (ecode) { if (ecode)
{
LogPrint(eLogError, "TCPIPPipe: downstream read error:" , ecode.message()); LogPrint(eLogError, "TCPIPPipe: downstream read error:" , ecode.message());
if (ecode != boost::asio::error::operation_aborted) if (ecode != boost::asio::error::operation_aborted)
Terminate(); Terminate();
} else { } else {
if (bytes_transfered > 0 ) { if (bytes_transfered > 0 )
memcpy(m_upstream_buf, m_downstream_to_up_buf, bytes_transfered); memcpy(m_upstream_buf, m_downstream_to_up_buf, bytes_transfered);
}
UpstreamWrite(bytes_transfered); UpstreamWrite(bytes_transfered);
} }
} }
void TCPIPPipe::HandleDownstreamWrite(const boost::system::error_code & ecode) { void TCPIPPipe::HandleDownstreamWrite(const boost::system::error_code & ecode) {
if (ecode) { if (ecode)
{
LogPrint(eLogError, "TCPIPPipe: downstream write error:" , ecode.message()); LogPrint(eLogError, "TCPIPPipe: downstream write error:" , ecode.message());
if (ecode != boost::asio::error::operation_aborted) if (ecode != boost::asio::error::operation_aborted)
Terminate(); Terminate();
} else {
AsyncReceiveUpstream();
} }
else
AsyncReceiveUpstream();
} }
void TCPIPPipe::HandleUpstreamWrite(const boost::system::error_code & ecode) { void TCPIPPipe::HandleUpstreamWrite(const boost::system::error_code & ecode) {
if (ecode) { if (ecode)
{
LogPrint(eLogError, "TCPIPPipe: upstream write error:" , ecode.message()); LogPrint(eLogError, "TCPIPPipe: upstream write error:" , ecode.message());
if (ecode != boost::asio::error::operation_aborted) if (ecode != boost::asio::error::operation_aborted)
Terminate(); Terminate();
} else {
AsyncReceiveDownstream();
} }
else
AsyncReceiveDownstream();
} }
void TCPIPPipe::HandleUpstreamReceived(const boost::system::error_code & ecode, std::size_t bytes_transfered) void TCPIPPipe::HandleUpstreamReceived(const boost::system::error_code & ecode, std::size_t bytes_transfered)
{ {
LogPrint(eLogDebug, "TCPIPPipe: upstream ", (int)bytes_transfered, " bytes received"); LogPrint(eLogDebug, "TCPIPPipe: upstream ", (int)bytes_transfered, " bytes received");
if (ecode) { if (ecode)
{
LogPrint(eLogError, "TCPIPPipe: upstream read error:" , ecode.message()); LogPrint(eLogError, "TCPIPPipe: upstream read error:" , ecode.message());
if (ecode != boost::asio::error::operation_aborted) if (ecode != boost::asio::error::operation_aborted)
Terminate(); Terminate();
} else { } else {
if (bytes_transfered > 0 ) { if (bytes_transfered > 0 )
memcpy(m_downstream_buf, m_upstream_to_down_buf, bytes_transfered); memcpy(m_downstream_buf, m_upstream_to_down_buf, bytes_transfered);
}
DownstreamWrite(bytes_transfered); DownstreamWrite(bytes_transfered);
} }
} }

9
libi2pd_client/I2PService.h

@ -18,6 +18,7 @@ namespace client
{ {
public: public:
typedef std::function<void(const boost::system::error_code &)> ReadyCallback; typedef std::function<void(const boost::system::error_code &)> ReadyCallback;
public: public:
I2PService (std::shared_ptr<ClientDestination> localDestination = nullptr); I2PService (std::shared_ptr<ClientDestination> localDestination = nullptr);
I2PService (i2p::data::SigningKeyType kt); I2PService (i2p::data::SigningKeyType kt);
@ -61,7 +62,6 @@ namespace client
void HandleReadyCheckTimer(const boost::system::error_code & ec); void HandleReadyCheckTimer(const boost::system::error_code & ec);
private: private:
std::shared_ptr<ClientDestination> m_LocalDestination; std::shared_ptr<ClientDestination> m_LocalDestination;
std::unordered_set<std::shared_ptr<I2PServiceHandler> > m_Handlers; std::unordered_set<std::shared_ptr<I2PServiceHandler> > m_Handlers;
std::mutex m_HandlersMutex; std::mutex m_HandlersMutex;
@ -93,6 +93,7 @@ namespace client
inline void Done (std::shared_ptr<I2PServiceHandler> me) { if(m_Service) m_Service->RemoveHandler(me); } inline void Done (std::shared_ptr<I2PServiceHandler> me) { if(m_Service) m_Service->RemoveHandler(me); }
// Call to talk with the owner // Call to talk with the owner
inline I2PService * GetOwner() { return m_Service; } inline I2PService * GetOwner() { return m_Service; }
private: private:
I2PService *m_Service; I2PService *m_Service;
std::atomic<bool> m_Dead; //To avoid cleaning up multiple times std::atomic<bool> m_Dead; //To avoid cleaning up multiple times
@ -101,11 +102,13 @@ namespace client
const size_t TCP_IP_PIPE_BUFFER_SIZE = 8192 * 8; const size_t TCP_IP_PIPE_BUFFER_SIZE = 8192 * 8;
// bidirectional pipe for 2 tcp/ip sockets // bidirectional pipe for 2 tcp/ip sockets
class TCPIPPipe: public I2PServiceHandler, public std::enable_shared_from_this<TCPIPPipe> { class TCPIPPipe: public I2PServiceHandler, public std::enable_shared_from_this<TCPIPPipe>
{
public: public:
TCPIPPipe(I2PService * owner, std::shared_ptr<boost::asio::ip::tcp::socket> upstream, std::shared_ptr<boost::asio::ip::tcp::socket> downstream); TCPIPPipe(I2PService * owner, std::shared_ptr<boost::asio::ip::tcp::socket> upstream, std::shared_ptr<boost::asio::ip::tcp::socket> downstream);
~TCPIPPipe(); ~TCPIPPipe();
void Start(); void Start();
protected: protected:
void Terminate(); void Terminate();
void AsyncReceiveUpstream(); void AsyncReceiveUpstream();
@ -116,6 +119,7 @@ namespace client
void HandleDownstreamWrite(const boost::system::error_code & ecode); void HandleDownstreamWrite(const boost::system::error_code & ecode);
void UpstreamWrite(size_t len); void UpstreamWrite(size_t len);
void DownstreamWrite(size_t len); void DownstreamWrite(size_t len);
private: private:
uint8_t m_upstream_to_down_buf[TCP_IP_PIPE_BUFFER_SIZE], m_downstream_to_up_buf[TCP_IP_PIPE_BUFFER_SIZE]; uint8_t m_upstream_to_down_buf[TCP_IP_PIPE_BUFFER_SIZE], m_downstream_to_up_buf[TCP_IP_PIPE_BUFFER_SIZE];
uint8_t m_upstream_buf[TCP_IP_PIPE_BUFFER_SIZE], m_downstream_buf[TCP_IP_PIPE_BUFFER_SIZE]; uint8_t m_upstream_buf[TCP_IP_PIPE_BUFFER_SIZE], m_downstream_buf[TCP_IP_PIPE_BUFFER_SIZE];
@ -147,6 +151,7 @@ namespace client
protected: protected:
virtual std::shared_ptr<I2PServiceHandler> CreateHandler(std::shared_ptr<boost::asio::ip::tcp::socket> socket) = 0; virtual std::shared_ptr<I2PServiceHandler> CreateHandler(std::shared_ptr<boost::asio::ip::tcp::socket> socket) = 0;
private: private:
void Accept(); void Accept();
void HandleAccept(const boost::system::error_code& ecode, std::shared_ptr<boost::asio::ip::tcp::socket> socket); void HandleAccept(const boost::system::error_code& ecode, std::shared_ptr<boost::asio::ip::tcp::socket> socket);

6
libi2pd_client/I2PTunnel.cpp

@ -674,7 +674,6 @@ 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),
@ -694,8 +693,6 @@ 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) : boost::asio::ip::address localAddress, boost::asio::ip::udp::endpoint forwardTo, uint16_t port) :
m_IsUniqueLocal(true), m_IsUniqueLocal(true),
@ -764,8 +761,6 @@ namespace client
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)
@ -850,7 +845,6 @@ namespace client
} }
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() {

27
libi2pd_client/I2PTunnel.h

@ -29,7 +29,6 @@ namespace client
class I2PTunnelConnection: public I2PServiceHandler, public std::enable_shared_from_this<I2PTunnelConnection> class I2PTunnelConnection: public I2PServiceHandler, public std::enable_shared_from_this<I2PTunnelConnection>
{ {
public: public:
I2PTunnelConnection (I2PService * owner, std::shared_ptr<boost::asio::ip::tcp::socket> socket, I2PTunnelConnection (I2PService * owner, std::shared_ptr<boost::asio::ip::tcp::socket> socket,
std::shared_ptr<const i2p::data::LeaseSet> leaseSet, int port = 0); // to I2P std::shared_ptr<const i2p::data::LeaseSet> leaseSet, int port = 0); // to I2P
I2PTunnelConnection (I2PService * owner, std::shared_ptr<boost::asio::ip::tcp::socket> socket, I2PTunnelConnection (I2PService * owner, std::shared_ptr<boost::asio::ip::tcp::socket> socket,
@ -41,7 +40,6 @@ namespace client
void Connect (bool isUniqueLocal = true); void Connect (bool isUniqueLocal = true);
protected: protected:
void Terminate (); void Terminate ();
void Receive (); void Receive ();
@ -56,7 +54,6 @@ namespace client
std::shared_ptr<const boost::asio::ip::tcp::socket> GetSocket () const { return m_Socket; }; std::shared_ptr<const boost::asio::ip::tcp::socket> GetSocket () const { return m_Socket; };
private: private:
uint8_t m_Buffer[I2P_TUNNEL_CONNECTION_BUFFER_SIZE], m_StreamBuffer[I2P_TUNNEL_CONNECTION_BUFFER_SIZE]; uint8_t m_Buffer[I2P_TUNNEL_CONNECTION_BUFFER_SIZE], m_StreamBuffer[I2P_TUNNEL_CONNECTION_BUFFER_SIZE];
std::shared_ptr<boost::asio::ip::tcp::socket> m_Socket; std::shared_ptr<boost::asio::ip::tcp::socket> m_Socket;
std::shared_ptr<i2p::stream::Stream> m_Stream; std::shared_ptr<i2p::stream::Stream> m_Stream;
@ -67,18 +64,15 @@ namespace client
class I2PClientTunnelConnectionHTTP: public I2PTunnelConnection class I2PClientTunnelConnectionHTTP: public I2PTunnelConnection
{ {
public: public:
I2PClientTunnelConnectionHTTP (I2PService * owner, std::shared_ptr<boost::asio::ip::tcp::socket> socket, I2PClientTunnelConnectionHTTP (I2PService * owner, std::shared_ptr<boost::asio::ip::tcp::socket> socket,
std::shared_ptr<i2p::stream::Stream> stream): std::shared_ptr<i2p::stream::Stream> stream):
I2PTunnelConnection (owner, socket, stream), m_HeaderSent (false), I2PTunnelConnection (owner, socket, stream), m_HeaderSent (false),
m_ConnectionSent (false), m_ProxyConnectionSent (false) {}; m_ConnectionSent (false), m_ProxyConnectionSent (false) {};
protected: protected:
void Write (const uint8_t * buf, size_t len); void Write (const uint8_t * buf, size_t len);
private: private:
std::stringstream m_InHeader, m_OutHeader; std::stringstream m_InHeader, m_OutHeader;
bool m_HeaderSent, m_ConnectionSent, m_ProxyConnectionSent; bool m_HeaderSent, m_ConnectionSent, m_ProxyConnectionSent;
}; };
@ -86,17 +80,14 @@ namespace client
class I2PServerTunnelConnectionHTTP: public I2PTunnelConnection class I2PServerTunnelConnectionHTTP: public I2PTunnelConnection
{ {
public: public:
I2PServerTunnelConnectionHTTP (I2PService * owner, std::shared_ptr<i2p::stream::Stream> stream, I2PServerTunnelConnectionHTTP (I2PService * owner, std::shared_ptr<i2p::stream::Stream> stream,
std::shared_ptr<boost::asio::ip::tcp::socket> socket, std::shared_ptr<boost::asio::ip::tcp::socket> socket,
const boost::asio::ip::tcp::endpoint& target, const std::string& host); const boost::asio::ip::tcp::endpoint& target, const std::string& host);
protected: protected:
void Write (const uint8_t * buf, size_t len); void Write (const uint8_t * buf, size_t len);
private: private:
std::string m_Host; std::string m_Host;
std::stringstream m_InHeader, m_OutHeader; std::stringstream m_InHeader, m_OutHeader;
bool m_HeaderSent; bool m_HeaderSent;
@ -106,17 +97,14 @@ namespace client
class I2PTunnelConnectionIRC: public I2PTunnelConnection class I2PTunnelConnectionIRC: public I2PTunnelConnection
{ {
public: public:
I2PTunnelConnectionIRC (I2PService * owner, std::shared_ptr<i2p::stream::Stream> stream, I2PTunnelConnectionIRC (I2PService * owner, std::shared_ptr<i2p::stream::Stream> stream,
std::shared_ptr<boost::asio::ip::tcp::socket> socket, std::shared_ptr<boost::asio::ip::tcp::socket> socket,
const boost::asio::ip::tcp::endpoint& target, const std::string& m_WebircPass); const boost::asio::ip::tcp::endpoint& target, const std::string& m_WebircPass);
protected: protected:
void Write (const uint8_t * buf, size_t len); void Write (const uint8_t * buf, size_t len);
private: private:
std::shared_ptr<const i2p::data::IdentityEx> m_From; std::shared_ptr<const i2p::data::IdentityEx> m_From;
std::stringstream m_OutPacket, m_InPacket; std::stringstream m_OutPacket, m_InPacket;
bool m_NeedsWebIrc; bool m_NeedsWebIrc;
@ -127,12 +115,10 @@ namespace client
class I2PClientTunnel: public TCPIPAcceptor class I2PClientTunnel: public TCPIPAcceptor
{ {
protected: protected:
// Implements TCPIPAcceptor // Implements TCPIPAcceptor
std::shared_ptr<I2PServiceHandler> CreateHandler(std::shared_ptr<boost::asio::ip::tcp::socket> socket); std::shared_ptr<I2PServiceHandler> CreateHandler(std::shared_ptr<boost::asio::ip::tcp::socket> socket);
public: public:
I2PClientTunnel (const std::string& name, const std::string& destination, I2PClientTunnel (const std::string& name, const std::string& destination,
const std::string& address, int port, std::shared_ptr<ClientDestination> localDestination, int destinationPort = 0); const std::string& address, int port, std::shared_ptr<ClientDestination> localDestination, int destinationPort = 0);
~I2PClientTunnel () {} ~I2PClientTunnel () {}
@ -143,11 +129,9 @@ namespace client
const char* GetName() { return m_Name.c_str (); } const char* GetName() { return m_Name.c_str (); }
private: private:
const i2p::data::IdentHash * GetIdentHash (); const i2p::data::IdentHash * GetIdentHash ();
private: private:
std::string m_Name, m_Destination; std::string m_Name, m_Destination;
const i2p::data::IdentHash * m_DestinationIdentHash; const i2p::data::IdentHash * m_DestinationIdentHash;
int m_DestinationPort; int m_DestinationPort;
@ -225,7 +209,6 @@ namespace client
void SetUniqueLocal(bool isUniqueLocal = true) { m_IsUniqueLocal = isUniqueLocal; } void SetUniqueLocal(bool isUniqueLocal = true) { m_IsUniqueLocal = isUniqueLocal; }
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);
UDPSessionPtr ObtainUDPSession(const i2p::data::IdentityEx& from, uint16_t localPort, uint16_t remotePort); UDPSessionPtr ObtainUDPSession(const i2p::data::IdentityEx& from, uint16_t localPort, uint16_t remotePort);
@ -279,7 +262,6 @@ namespace client
class I2PServerTunnel: public I2PService class I2PServerTunnel: public I2PService
{ {
public: public:
I2PServerTunnel (const std::string& name, const std::string& address, int port, I2PServerTunnel (const std::string& name, const std::string& address, int port,
std::shared_ptr<ClientDestination> localDestination, int inport = 0, bool gzip = true); std::shared_ptr<ClientDestination> localDestination, int inport = 0, bool gzip = true);
@ -301,7 +283,6 @@ namespace client
void SetMaxConnsPerMinute(const uint32_t conns) { m_PortDestination->SetMaxConnsPerMinute(conns); } void SetMaxConnsPerMinute(const uint32_t conns) { m_PortDestination->SetMaxConnsPerMinute(conns); }
private: private:
void HandleResolve (const boost::system::error_code& ecode, boost::asio::ip::tcp::resolver::iterator it, void HandleResolve (const boost::system::error_code& ecode, boost::asio::ip::tcp::resolver::iterator it,
std::shared_ptr<boost::asio::ip::tcp::resolver> resolver); std::shared_ptr<boost::asio::ip::tcp::resolver> resolver);
@ -310,7 +291,6 @@ namespace client
virtual std::shared_ptr<I2PTunnelConnection> CreateI2PConnection (std::shared_ptr<i2p::stream::Stream> stream); virtual std::shared_ptr<I2PTunnelConnection> CreateI2PConnection (std::shared_ptr<i2p::stream::Stream> stream);
private: private:
bool m_IsUniqueLocal; bool m_IsUniqueLocal;
std::string m_Name, m_Address; std::string m_Name, m_Address;
int m_Port; int m_Port;
@ -323,37 +303,30 @@ namespace client
class I2PServerTunnelHTTP: public I2PServerTunnel class I2PServerTunnelHTTP: public I2PServerTunnel
{ {
public: public:
I2PServerTunnelHTTP (const std::string& name, const std::string& address, int port, I2PServerTunnelHTTP (const std::string& name, const std::string& address, int port,
std::shared_ptr<ClientDestination> localDestination, const std::string& host, std::shared_ptr<ClientDestination> localDestination, const std::string& host,
int inport = 0, bool gzip = true); int inport = 0, bool gzip = true);
private: private:
std::shared_ptr<I2PTunnelConnection> CreateI2PConnection (std::shared_ptr<i2p::stream::Stream> stream); std::shared_ptr<I2PTunnelConnection> CreateI2PConnection (std::shared_ptr<i2p::stream::Stream> stream);
private: private:
std::string m_Host; std::string m_Host;
}; };
class I2PServerTunnelIRC: public I2PServerTunnel class I2PServerTunnelIRC: public I2PServerTunnel
{ {
public: public:
I2PServerTunnelIRC (const std::string& name, const std::string& address, int port, I2PServerTunnelIRC (const std::string& name, const std::string& address, int port,
std::shared_ptr<ClientDestination> localDestination, const std::string& webircpass, std::shared_ptr<ClientDestination> localDestination, const std::string& webircpass,
int inport = 0, bool gzip = true); int inport = 0, bool gzip = true);
private: private:
std::shared_ptr<I2PTunnelConnection> CreateI2PConnection (std::shared_ptr<i2p::stream::Stream> stream); std::shared_ptr<I2PTunnelConnection> CreateI2PConnection (std::shared_ptr<i2p::stream::Stream> stream);
private: private:
std::string m_WebircPass; std::string m_WebircPass;
}; };
} }
} }

Loading…
Cancel
Save