From 5a81b0e272a6e456e8c29ce2e755ac4e1c8b5546 Mon Sep 17 00:00:00 2001 From: Gluzskiy Alexandr Date: Mon, 11 Feb 2013 02:12:12 +0200 Subject: started separated proto lib implementation started modules support implementation --- server/api_service.h | 48 ++++++++ server/config.cpp | 15 +++ server/config.h | 21 ++++ server/get_function.cpp | 46 ++++++++ server/get_function.h | 23 ++++ server/headers.h | 26 +++++ server/main.cpp | 239 ++-------------------------------------- server/modules.cpp | 50 +++++++++ server/modules.h | 6 + server/restarter_server.project | 29 ++++- server/server.cpp | 129 ++++++++++++++++++++++ server/server.h | 118 ++++++++++++++++++++ server/service.cpp | 16 +++ 13 files changed, 533 insertions(+), 233 deletions(-) create mode 100644 server/api_service.h create mode 100644 server/config.cpp create mode 100644 server/config.h create mode 100644 server/get_function.cpp create mode 100644 server/get_function.h create mode 100644 server/headers.h create mode 100644 server/modules.cpp create mode 100644 server/modules.h create mode 100644 server/server.cpp create mode 100644 server/server.h create mode 100644 server/service.cpp (limited to 'server') diff --git a/server/api_service.h b/server/api_service.h new file mode 100644 index 0000000..9d6a656 --- /dev/null +++ b/server/api_service.h @@ -0,0 +1,48 @@ +// Copyright © 2013 sss +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef API_SERVICES_H +#define API_SERVICES_H + +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 +{ + std::string name, description; + service_return ret; + service_accept acc; + void * (*exec)(void *); + std::list predefined; +}; + +#define services(x) extern "C" void __attribute__((__visibility__("default"))) x(std::list &s) +//service should export "get_service" function which fill list of available services in this service library + +#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/config.cpp b/server/config.cpp new file mode 100644 index 0000000..265971b --- /dev/null +++ b/server/config.cpp @@ -0,0 +1,15 @@ +// Copyright © 2013 sss +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. diff --git a/server/config.h b/server/config.h new file mode 100644 index 0000000..bdea2d1 --- /dev/null +++ b/server/config.h @@ -0,0 +1,21 @@ +// Copyright © 2013 sss +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef CONFIG_H +#define CONFIG_H + + +#endif diff --git a/server/get_function.cpp b/server/get_function.cpp new file mode 100644 index 0000000..00aa569 --- /dev/null +++ b/server/get_function.cpp @@ -0,0 +1,46 @@ +// Copyright © 2013 sss +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +#ifdef _WIN32 +#include +#else +#include +#endif +#include + +void *load_lib(const char *path) +{ +#ifdef _WIN32 + return (void*)LoadLibraryA(path); +#else + return dlopen(path, RTLD_LAZY); +#endif +} + +void *get_function(const char *path, const char *func) +{ + void *lib = load_lib(path); + if(lib) + { +#ifdef _WIN32 + return GetProcAddressA((HANDLE)lib, func); +#else + return dlsym(lib, func); +#endif + } + return NULL; +} diff --git a/server/get_function.h b/server/get_function.h new file mode 100644 index 0000000..1156afe --- /dev/null +++ b/server/get_function.h @@ -0,0 +1,23 @@ +// Copyright © 2013 sss +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#ifndef GET_FUNCTION_H +#define GET_FUNCTION_H + + +void *get_function(const char *path, const char *func); + +#endif diff --git a/server/headers.h b/server/headers.h new file mode 100644 index 0000000..c7119b6 --- /dev/null +++ b/server/headers.h @@ -0,0 +1,26 @@ + +//c +#include +#include + +//os +#include +#include + +//c++ +#include +#include + +//boost +#include +#include +#include +#include + +//our api + +#include "api_service.h" +#include "api_protocol.h" +#include "get_function.h" +#include "modules.h" +#include "server.h" diff --git a/server/main.cpp b/server/main.cpp index c92244f..eae3b1e 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -1,4 +1,4 @@ -// Copyright © 2010-2012 sss +// Copyright © 2010-2013 sss // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License @@ -16,232 +16,10 @@ //based on async_tcp_echo_server.cpp code by Christopher M. Kohlhoff (chris at kohlhoff dot com) -#include +#include "headers.h" -#include -#include -#include -#include -#include -#include -#include -#include -using boost::asio::ip::tcp; -typedef boost::asio::ssl::stream 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) - { - 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; - } - } - - 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) - { - if (!error) - { -// std::cout<<"recieved: "<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; - } - - start_accept(); - } - - boost::asio::io_service& io_service_; - boost::asio::ip::tcp::acceptor acceptor_; - boost::asio::ssl::context context_; -}; extern "C" void handle_term(int i) { @@ -249,13 +27,14 @@ extern "C" void handle_term(int i) exit(1); } + int main(int argc, char* argv[]) { - if(geteuid()) +/* if(geteuid()) //temporary disabled for debug { std::cout<<"Program must be runned with root privilegies\n"; exit(EXIT_FAILURE); - } + } */ { std::ifstream in_pid; in_pid.open("/var/run/restarter_server.pid"); @@ -275,7 +54,7 @@ int main(int argc, char* argv[]) } } } - pid_t pid; +/* pid_t pid; //temporary disabled for debug pid = fork(); if(pid < 0) { @@ -292,9 +71,11 @@ int main(int argc, char* argv[]) out< installed_services; + +void load_modules(const char* path) +{ + boost::filesystem::path p (path); + typedef void (*svcs_ptr)(std::list&); + typedef void (*init_ptr)(); + std::list local_services; + for(boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator(p), end = boost::filesystem::directory_iterator(); i != end; ++i) + { + if(boost::filesystem::is_regular_file(i->path())) + { + init_ptr f = reinterpret_cast(get_function(boost::filesystem::absolute(i->path()).generic_string().c_str(), "init")); + if(f) + f(); + local_services.clear(); + svcs_ptr svcs = reinterpret_cast(get_function(boost::filesystem::absolute(i->path()).generic_string().c_str(), "get_services")); + if(svcs) + { + svcs(local_services); + if(!local_services.empty()) + installed_services.insert(installed_services.end(), local_services.begin(), local_services.end()); + } + } + } +} + +void load_modules() +{ + if(boost::filesystem::exists("./modules") && boost::filesystem::is_directory("./modules")) + load_modules("./modules"); +} diff --git a/server/modules.h b/server/modules.h new file mode 100644 index 0000000..e73415c --- /dev/null +++ b/server/modules.h @@ -0,0 +1,6 @@ +#ifndef MODULES_H +#define MODULES_H + +void load_modules(); + +#endif diff --git a/server/restarter_server.project b/server/restarter_server.project index f48e175..aa09d0c 100644 --- a/server/restarter_server.project +++ b/server/restarter_server.project @@ -9,7 +9,20 @@ + + + + + + + + + + + + + @@ -23,12 +36,16 @@ + + + + @@ -55,10 +72,12 @@ - + + - + /usr/include +/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/include @@ -74,7 +93,8 @@ - + @@ -96,7 +116,8 @@ - + + diff --git a/server/server.cpp b/server/server.cpp new file mode 100644 index 0000000..bdd2cfb --- /dev/null +++ b/server/server.cpp @@ -0,0 +1,129 @@ +// Copyright © 2013 sss +//. +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +//. +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +//. +// You should have received a copy of the GNU General Public License +// 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" + + +void session::handle_handshake(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; + } +} + +void session::handle_read(const boost::system::error_code& error, size_t bytes_transferred) +{ + 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(); + } + +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; + } + + start_accept(); + } + + 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 new file mode 100644 index 0000000..ab9d7a1 --- /dev/null +++ b/server/service.cpp @@ -0,0 +1,16 @@ +// Copyright © 2013 sss +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + -- cgit v1.2.3