2015-01-07 18:26:44 +00:00
|
|
|
#ifndef I2P_CONTROL_H__
|
|
|
|
#define I2P_CONTROL_H__
|
|
|
|
|
2015-01-07 21:09:32 +00:00
|
|
|
#include <inttypes.h>
|
2015-01-07 18:26:44 +00:00
|
|
|
#include <thread>
|
2015-01-07 21:09:32 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <array>
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
2015-01-07 18:26:44 +00:00
|
|
|
#include <boost/asio.hpp>
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace client
|
|
|
|
{
|
2015-01-07 21:09:32 +00:00
|
|
|
const size_t I2P_CONTROL_MAX_REQUEST_SIZE = 1024;
|
|
|
|
typedef std::array<char, I2P_CONTROL_MAX_REQUEST_SIZE> I2PControlBuffer;
|
|
|
|
|
|
|
|
const char I2P_CONTROL_PROPERTY_ID[] = "id";
|
|
|
|
const char I2P_CONTROL_PROPERTY_METHOD[] = "method";
|
|
|
|
const char I2P_CONTROL_PROPERTY_PARAMS[] = "params";
|
2015-01-08 18:28:51 +00:00
|
|
|
const char I2P_CONTROL_PROPERTY_RESULT[] = "result";
|
|
|
|
|
|
|
|
// methods
|
2015-01-08 21:11:40 +00:00
|
|
|
const char I2P_CONTROL_METHOD_AUTHENTICATE[] = "Authenticate";
|
|
|
|
const char I2P_CONTROL_METHOD_ECHO[] = "Echo";
|
2015-01-08 18:28:51 +00:00
|
|
|
const char I2P_CONTROL_METHOD_ROUTER_INFO[] = "RouterInfo";
|
2015-01-10 03:27:52 +00:00
|
|
|
const char I2P_CONTROL_METHOD_ROUTER_MANAGER[] = "RouterManager";
|
2015-01-12 19:31:45 +00:00
|
|
|
const char I2P_CONTROL_METHOD_NETWORK_SETTING[] = "NetworkSetting";
|
|
|
|
|
2015-01-08 21:11:40 +00:00
|
|
|
// params
|
|
|
|
const char I2P_CONTROL_PARAM_API[] = "API";
|
|
|
|
const char I2P_CONTROL_PARAM_PASSWORD[] = "Password";
|
|
|
|
const char I2P_CONTROL_PARAM_TOKEN[] = "Token";
|
|
|
|
const char I2P_CONTROL_PARAM_ECHO[] = "Echo";
|
|
|
|
const char I2P_CONTROL_PARAM_RESULT[] = "Result";
|
|
|
|
|
2015-01-12 18:38:16 +00:00
|
|
|
// RouterInfo requests
|
|
|
|
const char I2P_CONTROL_ROUTER_INFO_NETDB_KNOWNPEERS[] = "i2p.router.netdb.knownpeers";
|
2015-01-12 19:03:20 +00:00
|
|
|
const char I2P_CONTROL_ROUTER_INFO_TUNNELS_PARTICIPATING[] = "i2p.router.net.tunnels.participating";
|
|
|
|
|
2015-01-12 18:38:16 +00:00
|
|
|
// RouterManager requests
|
|
|
|
const char I2P_CONTROL_ROUTER_MANAGER_SHUTDOWN[] = "Shutdown";
|
|
|
|
const char I2P_CONTROL_ROUTER_MANAGER_SHUTDOWN_GRACEFUL[] = "ShutdownGraceful";
|
2015-01-10 03:27:52 +00:00
|
|
|
|
2015-01-07 18:26:44 +00:00
|
|
|
class I2PControlService
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
I2PControlService (int port);
|
2015-01-07 21:09:32 +00:00
|
|
|
~I2PControlService ();
|
|
|
|
|
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void Run ();
|
|
|
|
void Accept ();
|
|
|
|
void HandleAccept(const boost::system::error_code& ecode, std::shared_ptr<boost::asio::ip::tcp::socket> socket);
|
|
|
|
void ReadRequest (std::shared_ptr<boost::asio::ip::tcp::socket> socket);
|
|
|
|
void HandleRequestReceived (const boost::system::error_code& ecode, size_t bytes_transferred,
|
|
|
|
std::shared_ptr<boost::asio::ip::tcp::socket> socket, std::shared_ptr<I2PControlBuffer> buf);
|
2015-01-08 18:28:51 +00:00
|
|
|
void SendResponse (std::shared_ptr<boost::asio::ip::tcp::socket> socket,
|
|
|
|
std::shared_ptr<I2PControlBuffer> buf, const std::string& id,
|
|
|
|
const std::map<std::string, std::string>& results);
|
|
|
|
void HandleResponseSent (const boost::system::error_code& ecode, std::size_t bytes_transferred,
|
|
|
|
std::shared_ptr<boost::asio::ip::tcp::socket> socket, std::shared_ptr<I2PControlBuffer> buf);
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2015-01-12 18:38:16 +00:00
|
|
|
// methods
|
|
|
|
typedef void (I2PControlService::*MethodHandler)(const std::map<std::string, std::string>& params, std::map<std::string, std::string>& results);
|
|
|
|
|
2015-01-09 15:28:16 +00:00
|
|
|
void AuthenticateHandler (const std::map<std::string, std::string>& params, std::map<std::string, std::string>& results);
|
|
|
|
void EchoHandler (const std::map<std::string, std::string>& params, std::map<std::string, std::string>& results);
|
|
|
|
void RouterInfoHandler (const std::map<std::string, std::string>& params, std::map<std::string, std::string>& results);
|
2015-01-10 03:27:52 +00:00
|
|
|
void RouterManagerHandler (const std::map<std::string, std::string>& params, std::map<std::string, std::string>& results);
|
2015-01-12 19:31:45 +00:00
|
|
|
void NetworkSettingHandler (const std::map<std::string, std::string>& params, std::map<std::string, std::string>& results);
|
|
|
|
|
2015-01-12 18:38:16 +00:00
|
|
|
// RouterInfo
|
|
|
|
typedef void (I2PControlService::*RouterInfoRequestHandler)(std::map<std::string, std::string>& results);
|
|
|
|
void NetDbKnownPeersHandler (std::map<std::string, std::string>& results);
|
2015-01-12 19:03:20 +00:00
|
|
|
void TunnelsParticipatingHandler (std::map<std::string, std::string>& results);
|
2015-01-12 18:38:16 +00:00
|
|
|
|
|
|
|
// RouterManager
|
|
|
|
typedef void (I2PControlService::*RouterManagerRequestHandler)(std::map<std::string, std::string>& results);
|
|
|
|
void ShutdownHandler (std::map<std::string, std::string>& results);
|
|
|
|
void ShutdownGracefulHandler (std::map<std::string, std::string>& results);
|
|
|
|
|
2015-01-12 19:31:45 +00:00
|
|
|
// NetworkSetting
|
|
|
|
typedef void (I2PControlService::*NetworkSettingRequestHandler)(const std::string& value, std::map<std::string, std::string>& results);
|
|
|
|
|
2015-01-07 18:26:44 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
bool m_IsRunning;
|
|
|
|
std::thread * m_Thread;
|
|
|
|
|
|
|
|
boost::asio::io_service m_Service;
|
2015-01-10 03:27:52 +00:00
|
|
|
boost::asio::ip::tcp::acceptor m_Acceptor;
|
|
|
|
boost::asio::deadline_timer m_ShutdownTimer;
|
2015-01-12 18:38:16 +00:00
|
|
|
|
|
|
|
std::map<std::string, MethodHandler> m_MethodHandlers;
|
|
|
|
std::map<std::string, RouterInfoRequestHandler> m_RouterInfoHandlers;
|
|
|
|
std::map<std::string, RouterManagerRequestHandler> m_RouterManagerHandlers;
|
2015-01-12 19:31:45 +00:00
|
|
|
std::map<std::string, NetworkSettingRequestHandler> m_NetworkSettingHandlers;
|
2015-01-07 18:26:44 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|