|
|
@ -210,42 +210,81 @@ namespace client |
|
|
|
return std::make_shared<SocketsPipe<SocketUpstream, SocketDownstream> >(owner, upstream, downstream); |
|
|
|
return std::make_shared<SocketsPipe<SocketUpstream, SocketDownstream> >(owner, upstream, downstream); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* TODO: support IPv6 too */ |
|
|
|
//This is a service that listens for connections on the IP network or local socket and interacts with I2P
|
|
|
|
//This is a service that listens for connections on the IP network and interacts with I2P
|
|
|
|
template<typename Protocol> |
|
|
|
class TCPIPAcceptor: public I2PService |
|
|
|
class ServiceAcceptor: public I2PService |
|
|
|
{ |
|
|
|
{ |
|
|
|
public: |
|
|
|
public: |
|
|
|
|
|
|
|
|
|
|
|
TCPIPAcceptor (const std::string& address, uint16_t port, std::shared_ptr<ClientDestination> localDestination = nullptr) : |
|
|
|
ServiceAcceptor (const typename Protocol::endpoint& localEndpoint, std::shared_ptr<ClientDestination> localDestination = nullptr) : |
|
|
|
I2PService(localDestination), |
|
|
|
I2PService(localDestination), m_LocalEndpoint (localEndpoint) {} |
|
|
|
m_LocalEndpoint (boost::asio::ip::address::from_string(address), port), |
|
|
|
|
|
|
|
m_Timer (GetService ()) {} |
|
|
|
virtual ~ServiceAcceptor () { Stop(); } |
|
|
|
TCPIPAcceptor (const std::string& address, uint16_t port, i2p::data::SigningKeyType kt) : |
|
|
|
void Start () override |
|
|
|
I2PService(kt), |
|
|
|
{ |
|
|
|
m_LocalEndpoint (boost::asio::ip::address::from_string(address), port), |
|
|
|
m_Acceptor.reset (new typename Protocol::acceptor (GetService (), m_LocalEndpoint)); |
|
|
|
m_Timer (GetService ()) {} |
|
|
|
// update the local end point in case port has been set zero and got updated now
|
|
|
|
virtual ~TCPIPAcceptor () { TCPIPAcceptor::Stop(); } |
|
|
|
m_LocalEndpoint = m_Acceptor->local_endpoint(); |
|
|
|
//If you override this make sure you call it from the children
|
|
|
|
m_Acceptor->listen (); |
|
|
|
void Start (); |
|
|
|
Accept (); |
|
|
|
//If you override this make sure you call it from the children
|
|
|
|
} |
|
|
|
void Stop (); |
|
|
|
void Stop () override |
|
|
|
|
|
|
|
{ |
|
|
|
const boost::asio::ip::tcp::endpoint& GetLocalEndpoint () const { return m_LocalEndpoint; }; |
|
|
|
if (m_Acceptor) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
m_Acceptor->close(); |
|
|
|
|
|
|
|
m_Acceptor.reset (nullptr); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ClearHandlers(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
const typename Protocol::endpoint& GetLocalEndpoint () const { return m_LocalEndpoint; }; |
|
|
|
|
|
|
|
|
|
|
|
virtual const char* GetName() { return "Generic TCP/IP accepting daemon"; } |
|
|
|
const char* GetName() override { return "Generic TCP/IP accepting daemon"; } |
|
|
|
|
|
|
|
|
|
|
|
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<typename Protocol::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); |
|
|
|
{ |
|
|
|
boost::asio::ip::tcp::endpoint m_LocalEndpoint; |
|
|
|
auto newSocket = std::make_shared<typename Protocol::socket> (GetService ()); |
|
|
|
std::unique_ptr<boost::asio::ip::tcp::acceptor> m_Acceptor; |
|
|
|
m_Acceptor->async_accept (*newSocket, |
|
|
|
boost::asio::deadline_timer m_Timer; |
|
|
|
[newSocket, this](const boost::system::error_code& ecode) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (ecode == boost::asio::error::operation_aborted) return; |
|
|
|
|
|
|
|
if (!ecode) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LogPrint(eLogDebug, "ServiceAcceptor: ", GetName(), " accepted"); |
|
|
|
|
|
|
|
auto handler = CreateHandler(newSocket); |
|
|
|
|
|
|
|
if (handler) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
AddHandler(handler); |
|
|
|
|
|
|
|
handler->Handle(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
newSocket->close(); |
|
|
|
|
|
|
|
Accept(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
LogPrint (eLogError, "ServiceAcceptor: ", GetName(), " closing socket on accept because: ", ecode.message ()); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
typename Protocol::endpoint m_LocalEndpoint; |
|
|
|
|
|
|
|
std::unique_ptr<typename Protocol::acceptor> m_Acceptor; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TCPIPAcceptor: public ServiceAcceptor<boost::asio::ip::tcp> |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
public: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TCPIPAcceptor (const std::string& address, uint16_t port, std::shared_ptr<ClientDestination> localDestination = nullptr) : |
|
|
|
|
|
|
|
ServiceAcceptor (boost::asio::ip::tcp::endpoint (boost::asio::ip::address::from_string(address), port), localDestination) {} |
|
|
|
|
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|