2020-05-22 13:18:41 +00:00
|
|
|
/*
|
2024-02-13 23:52:18 +00:00
|
|
|
* Copyright (c) 2013-2024, The PurpleI2P Project
|
2020-05-22 13:18:41 +00:00
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*/
|
|
|
|
|
2015-01-07 18:09:59 +00:00
|
|
|
#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"
|
2019-03-28 13:57:34 +00:00
|
|
|
#include "AddressBook.h"
|
2015-01-07 18:09:59 +00:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace client
|
|
|
|
{
|
|
|
|
class I2PServiceHandler;
|
2018-01-12 10:04:12 +00:00
|
|
|
class I2PService : public std::enable_shared_from_this<I2PService>
|
2015-01-07 18:09:59 +00:00
|
|
|
{
|
2017-10-04 17:15:29 +00:00
|
|
|
public:
|
2020-03-01 10:25:50 +00:00
|
|
|
|
2017-10-04 17:15:29 +00:00
|
|
|
typedef std::function<void(const boost::system::error_code &)> ReadyCallback;
|
|
|
|
|
2015-01-07 18:09:59 +00:00
|
|
|
public:
|
2020-03-01 10:25:50 +00:00
|
|
|
|
|
|
|
I2PService (std::shared_ptr<ClientDestination> localDestination = nullptr);
|
2015-01-07 20:15:04 +00:00
|
|
|
I2PService (i2p::data::SigningKeyType kt);
|
2017-07-06 20:12:06 +00:00
|
|
|
virtual ~I2PService ();
|
2015-01-07 18:09:59 +00:00
|
|
|
|
2015-01-07 20:52:40 +00:00
|
|
|
inline void AddHandler (std::shared_ptr<I2PServiceHandler> conn)
|
|
|
|
{
|
2015-01-07 18:09:59 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_HandlersMutex);
|
|
|
|
m_Handlers.insert(conn);
|
|
|
|
}
|
2015-01-07 20:52:40 +00:00
|
|
|
inline void RemoveHandler (std::shared_ptr<I2PServiceHandler> conn)
|
|
|
|
{
|
2015-01-07 18:09:59 +00:00
|
|
|
std::unique_lock<std::mutex> l(m_HandlersMutex);
|
|
|
|
m_Handlers.erase(conn);
|
|
|
|
}
|
2017-08-11 00:29:35 +00:00
|
|
|
void ClearHandlers ();
|
2015-01-07 18:09:59 +00:00
|
|
|
|
2017-10-04 17:15:29 +00:00
|
|
|
void SetConnectTimeout(uint32_t timeout);
|
2017-08-31 16:08:22 +00:00
|
|
|
|
2017-10-04 17:15:29 +00:00
|
|
|
void AddReadyCallback(ReadyCallback cb);
|
2017-08-31 16:08:22 +00:00
|
|
|
|
2015-02-24 20:40:50 +00:00
|
|
|
inline std::shared_ptr<ClientDestination> GetLocalDestination () { return m_LocalDestination; }
|
2020-03-01 10:25:50 +00:00
|
|
|
inline std::shared_ptr<const ClientDestination> GetLocalDestination () const { return m_LocalDestination; }
|
2018-01-06 03:48:51 +00:00
|
|
|
inline void SetLocalDestination (std::shared_ptr<ClientDestination> dest)
|
|
|
|
{
|
2017-09-29 19:34:26 +00:00
|
|
|
if (m_LocalDestination) m_LocalDestination->Release ();
|
|
|
|
if (dest) dest->Acquire ();
|
2018-01-06 03:48:51 +00:00
|
|
|
m_LocalDestination = dest;
|
2017-09-29 19:34:26 +00:00
|
|
|
}
|
2023-06-12 02:10:32 +00:00
|
|
|
void CreateStream (StreamRequestComplete streamRequestComplete, const std::string& dest, uint16_t port = 0);
|
|
|
|
void CreateStream(StreamRequestComplete complete, std::shared_ptr<const Address> address, uint16_t port);
|
2015-01-07 20:52:40 +00:00
|
|
|
inline boost::asio::io_service& GetService () { return m_LocalDestination->GetService (); }
|
2015-01-07 18:09:59 +00:00
|
|
|
|
|
|
|
virtual void Start () = 0;
|
|
|
|
virtual void Stop () = 0;
|
|
|
|
|
2015-01-08 00:31:31 +00:00
|
|
|
virtual const char* GetName() { return "Generic I2P Service"; }
|
2017-08-31 16:08:22 +00:00
|
|
|
|
2015-01-07 18:09:59 +00:00
|
|
|
private:
|
2020-03-01 10:25:50 +00:00
|
|
|
|
2017-10-04 17:15:29 +00:00
|
|
|
void TriggerReadyCheckTimer();
|
|
|
|
void HandleReadyCheckTimer(const boost::system::error_code & ec);
|
2015-01-07 18:09:59 +00:00
|
|
|
|
2017-10-04 17:15:29 +00:00
|
|
|
private:
|
2020-03-01 10:25:50 +00:00
|
|
|
|
2015-02-24 20:40:50 +00:00
|
|
|
std::shared_ptr<ClientDestination> m_LocalDestination;
|
2015-01-07 18:09:59 +00:00
|
|
|
std::unordered_set<std::shared_ptr<I2PServiceHandler> > m_Handlers;
|
|
|
|
std::mutex m_HandlersMutex;
|
2017-10-04 17:15:29 +00:00
|
|
|
std::vector<std::pair<ReadyCallback, uint32_t> > m_ReadyCallbacks;
|
|
|
|
boost::asio::deadline_timer m_ReadyTimer;
|
2020-03-01 10:25:50 +00:00
|
|
|
bool m_ReadyTimerTriggered;
|
2017-10-04 17:15:29 +00:00
|
|
|
uint32_t m_ConnectTimeout;
|
2017-07-28 19:12:15 +00:00
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
const size_t NEVER_TIMES_OUT = 0;
|
|
|
|
|
2017-07-28 19:12:15 +00:00
|
|
|
public:
|
2020-03-01 10:25:50 +00:00
|
|
|
|
2017-08-31 16:08:22 +00:00
|
|
|
bool isUpdated; // transient, used during reload only
|
2015-01-07 18:09:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*Simple interface for I2PHandlers, allows detection of finalization amongst other things */
|
|
|
|
class I2PServiceHandler
|
|
|
|
{
|
|
|
|
public:
|
2020-03-01 10:25:50 +00:00
|
|
|
|
2015-01-07 20:15:04 +00:00
|
|
|
I2PServiceHandler(I2PService * parent) : m_Service(parent), m_Dead(false) { }
|
2015-01-07 18:09:59 +00:00
|
|
|
virtual ~I2PServiceHandler() { }
|
2015-01-08 00:31:31 +00:00
|
|
|
//If you override this make sure you call it from the children
|
|
|
|
virtual void Handle() {}; //Start handling the socket
|
2024-02-13 23:52:18 +00:00
|
|
|
virtual void Start () {};
|
2017-08-11 00:29:35 +00:00
|
|
|
|
|
|
|
void Terminate () { Kill (); };
|
2017-08-31 16:08:22 +00:00
|
|
|
|
2015-01-07 18:09:59 +00:00
|
|
|
protected:
|
2020-03-01 10:25:50 +00:00
|
|
|
|
2015-01-07 18:09:59 +00:00
|
|
|
// Call when terminating or handing over to avoid race conditions
|
2015-01-08 00:31:31 +00:00
|
|
|
inline bool Kill () { return m_Dead.exchange(true); }
|
2015-01-07 18:09:59 +00:00
|
|
|
// Call to know if the handler is dead
|
2015-01-08 00:31:31 +00:00
|
|
|
inline bool Dead () { return m_Dead; }
|
2015-01-07 18:09:59 +00:00
|
|
|
// Call when done to clean up (make sure Kill is called first)
|
2015-01-08 00:31:31 +00:00
|
|
|
inline void Done (std::shared_ptr<I2PServiceHandler> me) { if(m_Service) m_Service->RemoveHandler(me); }
|
2015-01-07 18:09:59 +00:00
|
|
|
// Call to talk with the owner
|
|
|
|
inline I2PService * GetOwner() { return m_Service; }
|
2017-10-04 17:15:29 +00:00
|
|
|
|
2015-01-07 18:09:59 +00:00
|
|
|
private:
|
2020-03-01 10:25:50 +00:00
|
|
|
|
2015-01-07 18:09:59 +00:00
|
|
|
I2PService *m_Service;
|
|
|
|
std::atomic<bool> m_Dead; //To avoid cleaning up multiple times
|
|
|
|
};
|
2015-01-08 00:31:31 +00:00
|
|
|
|
2024-02-13 23:52:18 +00:00
|
|
|
const size_t SOCKETS_PIPE_BUFFER_SIZE = 8192 * 8;
|
2016-02-26 22:06:11 +00:00
|
|
|
|
2024-02-13 23:52:18 +00:00
|
|
|
// bidirectional pipe for 2 stream sockets
|
2024-02-14 20:35:47 +00:00
|
|
|
template<typename SocketUpstream, typename SocketDownstream>
|
|
|
|
class SocketsPipe: public I2PServiceHandler,
|
|
|
|
public std::enable_shared_from_this<SocketsPipe<SocketUpstream, SocketDownstream> >
|
2017-10-04 17:15:29 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-03-01 10:25:50 +00:00
|
|
|
|
2024-02-14 20:35:47 +00:00
|
|
|
SocketsPipe(I2PService * owner, std::shared_ptr<SocketUpstream> upstream, std::shared_ptr<SocketDownstream> downstream):
|
|
|
|
I2PServiceHandler(owner), m_up(upstream), m_down(downstream)
|
|
|
|
{
|
|
|
|
boost::asio::socket_base::receive_buffer_size option(SOCKETS_PIPE_BUFFER_SIZE);
|
|
|
|
upstream->set_option(option);
|
|
|
|
downstream->set_option(option);
|
|
|
|
}
|
|
|
|
~SocketsPipe() { Terminate(); }
|
|
|
|
|
|
|
|
void Start() override
|
|
|
|
{
|
|
|
|
Transfer (m_up, m_down, m_upstream_to_down_buf, SOCKETS_PIPE_BUFFER_SIZE); // receive from upstream
|
|
|
|
Transfer (m_down, m_up, m_downstream_to_up_buf, SOCKETS_PIPE_BUFFER_SIZE); // receive from upstream
|
|
|
|
}
|
2020-03-01 10:25:50 +00:00
|
|
|
|
2024-02-14 20:35:47 +00:00
|
|
|
private:
|
2017-10-04 17:15:29 +00:00
|
|
|
|
2024-02-14 20:35:47 +00:00
|
|
|
void Terminate()
|
|
|
|
{
|
|
|
|
if(Kill()) return;
|
|
|
|
if (m_up)
|
|
|
|
{
|
|
|
|
if (m_up->is_open())
|
|
|
|
m_up->close();
|
|
|
|
m_up = nullptr;
|
|
|
|
}
|
|
|
|
if (m_down)
|
|
|
|
{
|
|
|
|
if (m_down->is_open())
|
|
|
|
m_down->close();
|
|
|
|
m_down = nullptr;
|
|
|
|
}
|
|
|
|
Done(SocketsPipe<SocketUpstream, SocketDownstream>::shared_from_this());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename From, typename To>
|
|
|
|
void Transfer (std::shared_ptr<From> from, std::shared_ptr<To> to, uint8_t * buf, size_t len)
|
|
|
|
{
|
|
|
|
if (!from || !to || !buf) return;
|
|
|
|
auto s = SocketsPipe<SocketUpstream, SocketDownstream>::shared_from_this ();
|
|
|
|
from->async_read_some(boost::asio::buffer(buf, len),
|
|
|
|
[from, to, s, buf, len](const boost::system::error_code& ecode, std::size_t transferred)
|
|
|
|
{
|
|
|
|
if (ecode == boost::asio::error::operation_aborted) return;
|
|
|
|
if (!ecode)
|
|
|
|
{
|
|
|
|
boost::asio::async_write(*to, boost::asio::buffer(buf, transferred), boost::asio::transfer_all(),
|
|
|
|
[from, to, s, buf, len](const boost::system::error_code& ecode, std::size_t transferred)
|
|
|
|
{
|
|
|
|
(void) transferred;
|
|
|
|
if (ecode == boost::asio::error::operation_aborted) return;
|
|
|
|
if (!ecode)
|
|
|
|
s->Transfer (from, to, buf, len);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint(eLogWarning, "SocketsPipe: Write error:" , ecode.message());
|
|
|
|
s->Terminate();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint(eLogWarning, "SocketsPipe: Read error:" , ecode.message());
|
|
|
|
s->Terminate();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-10-04 17:15:29 +00:00
|
|
|
private:
|
2020-03-01 10:25:50 +00:00
|
|
|
|
2024-02-14 20:35:47 +00:00
|
|
|
uint8_t m_upstream_to_down_buf[SOCKETS_PIPE_BUFFER_SIZE], m_downstream_to_up_buf[SOCKETS_PIPE_BUFFER_SIZE];
|
|
|
|
std::shared_ptr<SocketUpstream> m_up;
|
|
|
|
std::shared_ptr<SocketDownstream> m_down;
|
2016-02-26 22:06:11 +00:00
|
|
|
};
|
2017-08-31 16:08:22 +00:00
|
|
|
|
2024-02-14 20:35:47 +00:00
|
|
|
template<typename SocketUpstream, typename SocketDownstream>
|
|
|
|
std::shared_ptr<I2PServiceHandler> CreateSocketsPipe (I2PService * owner, std::shared_ptr<SocketUpstream> upstream, std::shared_ptr<SocketDownstream> downstream)
|
2024-02-13 23:52:18 +00:00
|
|
|
{
|
2024-02-14 20:35:47 +00:00
|
|
|
return std::make_shared<SocketsPipe<SocketUpstream, SocketDownstream> >(owner, upstream, downstream);
|
2024-02-13 23:52:18 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 02:49:35 +00:00
|
|
|
/* TODO: support IPv6 too */
|
2015-01-08 00:31:31 +00:00
|
|
|
//This is a service that listens for connections on the IP network and interacts with I2P
|
|
|
|
class TCPIPAcceptor: public I2PService
|
|
|
|
{
|
|
|
|
public:
|
2020-03-01 10:25:50 +00:00
|
|
|
|
2023-06-12 02:10:32 +00:00
|
|
|
TCPIPAcceptor (const std::string& address, uint16_t port, std::shared_ptr<ClientDestination> localDestination = nullptr) :
|
2015-01-08 00:31:31 +00:00
|
|
|
I2PService(localDestination),
|
2017-08-03 01:00:04 +00:00
|
|
|
m_LocalEndpoint (boost::asio::ip::address::from_string(address), port),
|
2015-01-08 00:31:31 +00:00
|
|
|
m_Timer (GetService ()) {}
|
2023-06-12 02:10:32 +00:00
|
|
|
TCPIPAcceptor (const std::string& address, uint16_t port, i2p::data::SigningKeyType kt) :
|
2015-01-08 00:31:31 +00:00
|
|
|
I2PService(kt),
|
2017-08-03 01:00:04 +00:00
|
|
|
m_LocalEndpoint (boost::asio::ip::address::from_string(address), port),
|
2015-01-08 00:31:31 +00:00
|
|
|
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 ();
|
2016-04-24 21:32:24 +00:00
|
|
|
|
2020-03-01 10:25:50 +00:00
|
|
|
const boost::asio::ip::tcp::endpoint& GetLocalEndpoint () const { return m_LocalEndpoint; };
|
2017-01-07 13:32:50 +00:00
|
|
|
|
2017-10-04 17:15:29 +00:00
|
|
|
virtual const char* GetName() { return "Generic TCP/IP accepting daemon"; }
|
2017-01-07 13:32:50 +00:00
|
|
|
|
2015-01-08 00:31:31 +00:00
|
|
|
protected:
|
2020-03-01 10:25:50 +00:00
|
|
|
|
2015-04-06 18:41:07 +00:00
|
|
|
virtual std::shared_ptr<I2PServiceHandler> CreateHandler(std::shared_ptr<boost::asio::ip::tcp::socket> socket) = 0;
|
2017-10-04 17:15:29 +00:00
|
|
|
|
2015-01-08 00:31:31 +00:00
|
|
|
private:
|
2020-03-01 10:25:50 +00:00
|
|
|
|
2015-01-08 00:31:31 +00:00
|
|
|
void Accept();
|
2015-04-06 18:41:07 +00:00
|
|
|
void HandleAccept(const boost::system::error_code& ecode, std::shared_ptr<boost::asio::ip::tcp::socket> socket);
|
2017-08-03 01:00:04 +00:00
|
|
|
boost::asio::ip::tcp::endpoint m_LocalEndpoint;
|
|
|
|
std::unique_ptr<boost::asio::ip::tcp::acceptor> m_Acceptor;
|
2015-01-08 00:31:31 +00:00
|
|
|
boost::asio::deadline_timer m_Timer;
|
|
|
|
};
|
2015-01-07 18:09:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|