From 1d41574c6e8e7bbf3705645feb429df6281ccb83 Mon Sep 17 00:00:00 2001 From: Gluzskiy Alexandr Date: Tue, 12 Feb 2013 20:36:08 +0200 Subject: working on service support --- server/api_service.h | 16 +++-- server/headers.h | 2 + server/modules.cpp | 3 +- server/restarter_server.project | 7 ++- server/server.cpp | 127 +++++++++------------------------------- server/server.h | 89 +++------------------------- server/service.cpp | 18 ++++++ 7 files changed, 74 insertions(+), 188 deletions(-) (limited to 'server') diff --git a/server/api_service.h b/server/api_service.h index 9d6a656..4f5a183 100644 --- a/server/api_service.h +++ b/server/api_service.h @@ -17,23 +17,29 @@ #ifndef API_SERVICES_H #define API_SERVICES_H +namespace service + +{ + enum service_return {RET_NONE = 2, RET_VOID_PTR = 4, RET_CHAR_PTR = 8, RET_STD_STRING = 16, RET_INT, RET_FLOAT = 32}; enum service_accept {ACC_NONE = 2, ACC_VOID_PTR = 4, ACC_CHAR_PTR = 8, ACC_STD_STRING = 16, ACC_INT, ACC_FLOAT = 32}; -struct command -{ - std::string command, description; -}; //service may have predefined command set struct service_info { + struct command + { + std::string command, description; + bool operator==(const std::string &command); + }; std::string name, description; service_return ret; service_accept acc; void * (*exec)(void *); std::list predefined; + bool operator==(const std::string &name); }; #define services(x) extern "C" void __attribute__((__visibility__("default"))) x(std::list &s) @@ -42,7 +48,7 @@ struct service_info #define init_func(x) extern "C" void __attribute__((__visibility__("default"))) x() //service may export "init" function which will be called on service loading - +}; #endif diff --git a/server/headers.h b/server/headers.h index c7119b6..0f0587a 100644 --- a/server/headers.h +++ b/server/headers.h @@ -24,3 +24,5 @@ #include "get_function.h" #include "modules.h" #include "server.h" +#include "session.h" +#include "service.h" diff --git a/server/modules.cpp b/server/modules.cpp index 1f10b90..f042d82 100644 --- a/server/modules.cpp +++ b/server/modules.cpp @@ -16,7 +16,8 @@ #include "headers.h" -std::list installed_services; +using namespace service; + void load_modules(const char* path) { diff --git a/server/restarter_server.project b/server/restarter_server.project index d2e0e61..f3bd51b 100644 --- a/server/restarter_server.project +++ b/server/restarter_server.project @@ -20,6 +20,9 @@ + + + @@ -50,7 +53,8 @@ - + @@ -94,6 +98,7 @@ diff --git a/server/server.cpp b/server/server.cpp index bdd2cfb..7a13fe6 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -17,113 +17,42 @@ #include "headers.h" -void session::handle_handshake(const boost::system::error_code& error) + + +server::server(boost::asio::io_service& io_service, short port) : +io_service_(io_service), acceptor_(io_service, tcp::endpoint(boost::asio::ip::address_v4(), port)), context_(boost::asio::ssl::context::sslv23) +{ + context_.set_options(boost::asio::ssl::context::default_workarounds | boost::asio::ssl::context::no_sslv2); + context_.set_password_callback(boost::bind(&server::get_password, this)); + context_.use_certificate_chain_file("/etc/restarter_server/serv.crt"); + context_.use_rsa_private_key_file("/etc/restarter_server/serv.key", boost::asio::ssl::context::pem); + context_.load_verify_file("/etc/restarter_server/ca.crt"); + context_.set_verify_mode(boost::asio::ssl::verify_peer | boost::asio::ssl::verify_client_once); + start_accept(); +} + + +void server::start_accept() +{ + session* new_session = new session(io_service_, context_); + acceptor_.async_accept(new_session->socket(), boost::bind(&server::handle_accept, this, new_session, boost::asio::placeholders::error)); +} + + +void server::handle_accept(session* new_session, const boost::system::error_code& error) { if (!error) { - socket_.async_read_some(boost::asio::buffer(data_, max_length), boost::bind(&session::handle_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); + new_session->start(); } else { - delete this; + delete new_session; } + start_accept(); } -void session::handle_read(const boost::system::error_code& error, size_t bytes_transferred) +std::string server::get_password() const //TODO: add key password support { - if (!error) - { -// std::cout<<"recieved: "< ssl_socket; -class session -{ -public: - session(boost::asio::io_service& io_service, boost::asio::ssl::context& context) : socket_(io_service, context) - {} - - ssl_socket::lowest_layer_type& socket() - { - return socket_.lowest_layer(); - } - void handle_handshake(const boost::system::error_code& error); - - void start() - { - socket_.async_handshake(boost::asio::ssl::stream_base::server, boost::bind(&session::handle_handshake, this, boost::asio::placeholders::error)); - } - -private: - void handle_read(const boost::system::error_code& error, size_t bytes_transferred); - -/* void handle_write(const boost::system::error_code& error) - { - if (!error) - { - socket_.async_read_some(boost::asio::buffer(data_, max_length), - boost::bind(&session::handle_read, this, - boost::asio::placeholders::error, - boost::asio::placeholders::bytes_transferred)); - } - else - { - delete this; - } - }*/ - - ssl_socket socket_; - enum { max_length = 128 }; - char data_[max_length]; -}; class server { public: - server(boost::asio::io_service& io_service, short port) - : io_service_(io_service), - acceptor_(io_service, tcp::endpoint(boost::asio::ip::address_v4(), port)), - context_(boost::asio::ssl::context::sslv23) - { - context_.set_options( - boost::asio::ssl::context::default_workarounds - | boost::asio::ssl::context::no_sslv2); - context_.set_password_callback(boost::bind(&server::get_password, this)); - context_.use_certificate_chain_file("/etc/restarter_server/serv.crt"); - context_.use_rsa_private_key_file("/etc/restarter_server/serv.key", boost::asio::ssl::context::pem); - context_.load_verify_file("/etc/restarter_server/ca.crt"); - context_.set_verify_mode(boost::asio::ssl::verify_peer | boost::asio::ssl::verify_client_once); - start_accept(); - } + server(boost::asio::io_service& io_service, short port); private: - void start_accept() - { - session* new_session = new session(io_service_, context_); - acceptor_.async_accept(new_session->socket(), - boost::bind(&server::handle_accept, this, new_session, - boost::asio::placeholders::error)); - } - std::string get_password() const - { - return ""; - } - - void handle_accept(session* new_session, - const boost::system::error_code& error) - { - if (!error) - { - new_session->start(); - } - else - { - delete new_session; - } + void start_accept(); + std::string get_password() const; - start_accept(); - } + void handle_accept(session* new_session, const boost::system::error_code& error); - boost::asio::io_service& io_service_; - boost::asio::ip::tcp::acceptor acceptor_; - boost::asio::ssl::context context_; + boost::asio::io_service& io_service_; + boost::asio::ip::tcp::acceptor acceptor_; + boost::asio::ssl::context context_; }; #endif diff --git a/server/service.cpp b/server/service.cpp index ab9d7a1..004def3 100644 --- a/server/service.cpp +++ b/server/service.cpp @@ -14,3 +14,21 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +#include "headers.h" + +namespace service +{ + + std::list installed_services; + +bool service_info::operator==(const std::string &name) +{ + return name == this->name; +} + +bool service_info::command::operator==(const std::string &command) +{ + return command == this->command; +} + +}; -- cgit v1.2.3