mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
5.3 KiB
139 lines
5.3 KiB
![]()
10 years ago
|
#ifndef I2PSERVICE_H__
|
||
|
#define I2PSERVICE_H__
|
||
|
|
||
|
#include <atomic>
|
||
|
#include <mutex>
|
||
|
#include <unordered_set>
|
||
|
#include <memory>
|
||
|
#include <boost/asio.hpp>
|
||
|
#include "Destination.h"
|
||
|
#include "Identity.h"
|
||
|
|
||
|
namespace i2p
|
||
|
{
|
||
|
namespace client
|
||
|
{
|
||
|
class I2PServiceHandler;
|
||
|
class I2PService
|
||
|
{
|
||
|
public:
|
||
![]()
10 years ago
|
I2PService (std::shared_ptr<ClientDestination> localDestination = nullptr);
|
||
![]()
10 years ago
|
I2PService (i2p::data::SigningKeyType kt);
|
||
![]()
10 years ago
|
virtual ~I2PService () { ClearHandlers (); }
|
||
|
|
||
![]()
10 years ago
|
inline void AddHandler (std::shared_ptr<I2PServiceHandler> conn)
|
||
|
{
|
||
![]()
10 years ago
|
std::unique_lock<std::mutex> l(m_HandlersMutex);
|
||
|
m_Handlers.insert(conn);
|
||
|
}
|
||
![]()
10 years ago
|
inline void RemoveHandler (std::shared_ptr<I2PServiceHandler> conn)
|
||
|
{
|
||
![]()
10 years ago
|
std::unique_lock<std::mutex> l(m_HandlersMutex);
|
||
|
m_Handlers.erase(conn);
|
||
|
}
|
||
![]()
10 years ago
|
inline void ClearHandlers ()
|
||
|
{
|
||
![]()
10 years ago
|
std::unique_lock<std::mutex> l(m_HandlersMutex);
|
||
|
m_Handlers.clear();
|
||
|
}
|
||
|
|
||
![]()
10 years ago
|
inline std::shared_ptr<ClientDestination> GetLocalDestination () { return m_LocalDestination; }
|
||
![]()
8 years ago
|
inline std::shared_ptr<const ClientDestination> GetLocalDestination () const { return m_LocalDestination; }
|
||
![]()
10 years ago
|
inline void SetLocalDestination (std::shared_ptr<ClientDestination> dest) { m_LocalDestination = dest; }
|
||
![]()
10 years ago
|
void CreateStream (StreamRequestComplete streamRequestComplete, const std::string& dest, int port = 0);
|
||
![]()
10 years ago
|
|
||
![]()
10 years ago
|
inline boost::asio::io_service& GetService () { return m_LocalDestination->GetService (); }
|
||
![]()
10 years ago
|
|
||
|
virtual void Start () = 0;
|
||
|
virtual void Stop () = 0;
|
||
|
|
||
![]()
10 years ago
|
virtual const char* GetName() { return "Generic I2P Service"; }
|
||
![]()
10 years ago
|
private:
|
||
|
|
||
![]()
10 years ago
|
std::shared_ptr<ClientDestination> m_LocalDestination;
|
||
![]()
10 years ago
|
std::unordered_set<std::shared_ptr<I2PServiceHandler> > m_Handlers;
|
||
|
std::mutex m_HandlersMutex;
|
||
|
};
|
||
|
|
||
|
/*Simple interface for I2PHandlers, allows detection of finalization amongst other things */
|
||
|
class I2PServiceHandler
|
||
|
{
|
||
|
public:
|
||
![]()
10 years ago
|
I2PServiceHandler(I2PService * parent) : m_Service(parent), m_Dead(false) { }
|
||
![]()
10 years ago
|
virtual ~I2PServiceHandler() { }
|
||
![]()
10 years ago
|
//If you override this make sure you call it from the children
|
||
|
virtual void Handle() {}; //Start handling the socket
|
||
![]()
10 years ago
|
protected:
|
||
|
// Call when terminating or handing over to avoid race conditions
|
||
![]()
10 years ago
|
inline bool Kill () { return m_Dead.exchange(true); }
|
||
![]()
10 years ago
|
// Call to know if the handler is dead
|
||
![]()
10 years ago
|
inline bool Dead () { return m_Dead; }
|
||
![]()
10 years ago
|
// Call when done to clean up (make sure Kill is called first)
|
||
![]()
10 years ago
|
inline void Done (std::shared_ptr<I2PServiceHandler> me) { if(m_Service) m_Service->RemoveHandler(me); }
|
||
![]()
10 years ago
|
// Call to talk with the owner
|
||
|
inline I2PService * GetOwner() { return m_Service; }
|
||
|
private:
|
||
|
I2PService *m_Service;
|
||
|
std::atomic<bool> m_Dead; //To avoid cleaning up multiple times
|
||
|
};
|
||
![]()
10 years ago
|
|
||
![]()
8 years ago
|
const size_t TCP_IP_PIPE_BUFFER_SIZE = 8192 * 8;
|
||
![]()
9 years ago
|
|
||
|
// bidirectional pipe for 2 tcp/ip sockets
|
||
|
class TCPIPPipe: public I2PServiceHandler, public std::enable_shared_from_this<TCPIPPipe> {
|
||
|
public:
|
||
|
TCPIPPipe(I2PService * owner, std::shared_ptr<boost::asio::ip::tcp::socket> upstream, std::shared_ptr<boost::asio::ip::tcp::socket> downstream);
|
||
|
~TCPIPPipe();
|
||
|
void Start();
|
||
|
protected:
|
||
|
void Terminate();
|
||
|
void AsyncReceiveUpstream();
|
||
|
void AsyncReceiveDownstream();
|
||
|
void HandleUpstreamReceived(const boost::system::error_code & ecode, std::size_t bytes_transferred);
|
||
|
void HandleDownstreamReceived(const boost::system::error_code & ecode, std::size_t bytes_transferred);
|
||
|
void HandleUpstreamWrite(const boost::system::error_code & ecode);
|
||
|
void HandleDownstreamWrite(const boost::system::error_code & ecode);
|
||
![]()
8 years ago
|
void UpstreamWrite(size_t len);
|
||
|
void DownstreamWrite(size_t len);
|
||
![]()
9 years ago
|
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_buf[TCP_IP_PIPE_BUFFER_SIZE], m_downstream_buf[TCP_IP_PIPE_BUFFER_SIZE];
|
||
|
std::shared_ptr<boost::asio::ip::tcp::socket> m_up, m_down;
|
||
|
};
|
||
|
|
||
![]()
10 years ago
|
/* TODO: support IPv6 too */
|
||
![]()
10 years ago
|
//This is a service that listens for connections on the IP network and interacts with I2P
|
||
|
class TCPIPAcceptor: public I2PService
|
||
|
{
|
||
|
public:
|
||
![]()
9 years ago
|
TCPIPAcceptor (const std::string& address, int port, std::shared_ptr<ClientDestination> localDestination = nullptr) :
|
||
![]()
10 years ago
|
I2PService(localDestination),
|
||
![]()
9 years ago
|
m_Acceptor (GetService (), boost::asio::ip::tcp::endpoint (boost::asio::ip::address::from_string(address), port)),
|
||
![]()
10 years ago
|
m_Timer (GetService ()) {}
|
||
![]()
9 years ago
|
TCPIPAcceptor (const std::string& address, int port, i2p::data::SigningKeyType kt) :
|
||
![]()
10 years ago
|
I2PService(kt),
|
||
![]()
9 years ago
|
m_Acceptor (GetService (), boost::asio::ip::tcp::endpoint (boost::asio::ip::address::from_string(address), port)),
|
||
![]()
10 years ago
|
m_Timer (GetService ()) {}
|
||
|
virtual ~TCPIPAcceptor () { TCPIPAcceptor::Stop(); }
|
||
|
//If you override this make sure you call it from the children
|
||
|
void Start ();
|
||
|
//If you override this make sure you call it from the children
|
||
|
void Stop ();
|
||
![]()
8 years ago
|
|
||
|
const boost::asio::ip::tcp::acceptor& GetAcceptor () const { return m_Acceptor; };
|
||
![]()
8 years ago
|
|
||
|
virtual const char* GetName() { return "Generic TCP/IP accepting daemon"; }
|
||
|
|
||
![]()
10 years ago
|
protected:
|
||
![]()
10 years ago
|
virtual std::shared_ptr<I2PServiceHandler> CreateHandler(std::shared_ptr<boost::asio::ip::tcp::socket> socket) = 0;
|
||
![]()
10 years ago
|
private:
|
||
|
void Accept();
|
||
![]()
10 years ago
|
void HandleAccept(const boost::system::error_code& ecode, std::shared_ptr<boost::asio::ip::tcp::socket> socket);
|
||
![]()
10 years ago
|
boost::asio::ip::tcp::acceptor m_Acceptor;
|
||
|
boost::asio::deadline_timer m_Timer;
|
||
|
};
|
||
![]()
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
#endif
|