diff options
author | Gluzskiy Alexandr <sss@sss.chaoslab.ru> | 2013-02-11 02:12:12 +0200 |
---|---|---|
committer | Gluzskiy Alexandr <sss@sss.chaoslab.ru> | 2013-02-11 02:12:12 +0200 |
commit | 5a81b0e272a6e456e8c29ce2e755ac4e1c8b5546 (patch) | |
tree | fe3f315cf3ebf102ca5d6b8529fc172ab55c9352 /server | |
parent | 94a11e7736df09143fd7cf1a75282dc9b408fbd9 (diff) |
started separated proto lib implementation
started modules support implementation
Diffstat (limited to 'server')
-rw-r--r-- | server/api_service.h | 48 | ||||
-rw-r--r-- | server/config.cpp | 15 | ||||
-rw-r--r-- | server/config.h | 21 | ||||
-rw-r--r-- | server/get_function.cpp | 46 | ||||
-rw-r--r-- | server/get_function.h | 23 | ||||
-rw-r--r-- | server/headers.h | 26 | ||||
-rw-r--r-- | server/main.cpp | 239 | ||||
-rw-r--r-- | server/modules.cpp | 50 | ||||
-rw-r--r-- | server/modules.h | 6 | ||||
-rw-r--r-- | server/restarter_server.project | 29 | ||||
-rw-r--r-- | server/server.cpp | 129 | ||||
-rw-r--r-- | server/server.h | 118 | ||||
-rw-r--r-- | server/service.cpp | 16 |
13 files changed, 533 insertions, 233 deletions
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<command> predefined; +}; + +#define services(x) extern "C" void __attribute__((__visibility__("default"))) x(std::list<service_info> &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 <windows.h> +#else +#include <dlfcn.h> +#endif +#include <stdlib.h> + +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 <cstdlib> +#include <stdio.h> + +//os +#include <signal.h> +#include <unistd.h> + +//c++ +#include <iostream> +#include <fstream> + +//boost +#include <boost/bind.hpp> +#include <boost/asio.hpp> +#include <boost/asio/ssl.hpp> +#include <boost/filesystem.hpp> + +//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 <cstdlib> +#include "headers.h" -#include <signal.h> -#include <unistd.h> -#include <stdio.h> -#include <iostream> -#include <fstream> -#include <boost/bind.hpp> -#include <boost/asio.hpp> -#include <boost/asio/ssl.hpp> -using boost::asio::ip::tcp; -typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> 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: "<<data_<<"\n"; - if(strstr(data_, "restart vbox")) - { - FILE *f = popen("/sbin/runscript /etc/init.d/vbox_headles restart --nodeps","r"); - if(f != NULL) - { - char buf[128]; - while(fgets(buf, 128, f) != NULL) - ; //TODO: do something with output - int s = pclose(f); // TODO: handle exit status - } - else - ; //TODO: handle fail - } - else if(strstr(data_, "reboot now")) - { - FILE *f = popen("reboot","r"); - if(f != NULL) - { - char buf[128]; - while(fgets(buf, 128, f) != NULL) - ; //TODO: do something with output - int s = pclose(f); // TODO: handle exit status - } - else - ; //TODO: handle fail - } - else if(strstr(data_, "halt now")) - { - FILE *f = popen("halt","r"); - if(f != NULL) - { - char buf[128]; - while(fgets(buf, 128, f) != NULL) - ; //TODO: do something with output - int s = pclose(f); // TODO: handle exit status - } - else - ; //TODO: handle fail - } - else if(strstr(data_, "restart cups")) - { - FILE *f = popen("/sbin/runscript /etc/init.d/cupsd restart --nodeps","r"); - if(f != NULL) - { - char buf[128]; - while(fgets(buf, 128, f) != NULL) - ; //TODO: do something with output - int s = pclose(f); // TODO: handle exit status - } - else - ; //TODO: handle fail - } - else if(strstr(data_, "restart ppp")) - { - FILE *f = popen("/sbin/runscript /etc/init.d/net.ppp0 stop --nodeps","r"); - char buf[128]; - int s = 0; - if(f != NULL) - { - while(fgets(buf, 128, f) != NULL) - ; //TODO: do something with output - s = pclose(f); // TODO: handle exit status - } - else - ; //TODO: handle fail - sleep(3); - f = popen("killall pppd","r"); - if(f == NULL) - { - while(fgets(buf, 128, f) != NULL) - ; //TODO: do something with output - s = pclose(f); // TODO: handle exit status - } - else - ; //TODO: handle fail - sleep(1); - f = popen("/sbin/runscript /etc/init.d/net.ppp0 start --nodeps","r"); - if(f == NULL) - { - while(fgets(buf, 128, f) != NULL) - ; //TODO: do something with output - s = pclose(f); // TODO: handle exit status - } - else - ; //TODO: handle fail - } -/* boost::asio::async_write(socket_, - boost::asio::buffer(data_, bytes_transferred), - boost::bind(&session::handle_write, this, - boost::asio::placeholders::error)); */ - } - delete this; - } - -/* 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 = 32 }; - 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::from_string("192.168.0.1"), port)), - context_(boost::asio::ssl::context::sslv23) */ - 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_; -}; 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<<pid<<"\n"; out.close(); exit(EXIT_SUCCESS); - } + } */ signal (SIGTERM, handle_term); + load_modules(); + try { @@ -310,4 +91,4 @@ int main(int argc, char* argv[]) } return 0; -}
\ No newline at end of file +} diff --git a/server/modules.cpp b/server/modules.cpp new file mode 100644 index 0000000..1f10b90 --- /dev/null +++ b/server/modules.cpp @@ -0,0 +1,50 @@ +// 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" + +std::list<service_info> installed_services; + +void load_modules(const char* path) +{ + boost::filesystem::path p (path); + typedef void (*svcs_ptr)(std::list<service_info>&); + typedef void (*init_ptr)(); + std::list<service_info> 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<init_ptr>(get_function(boost::filesystem::absolute(i->path()).generic_string().c_str(), "init")); + if(f) + f(); + local_services.clear(); + svcs_ptr svcs = reinterpret_cast<svcs_ptr>(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 @@ <Dependencies/> <VirtualDirectory Name="src"> <File Name="main.cpp"/> + <File Name="api_service.h"/> + <File Name="service.cpp"/> + <File Name="headers.h"/> + <File Name="get_function.cpp"/> + <File Name="get_function.h"/> + <File Name="config.cpp"/> + <File Name="config.h"/> + <File Name="modules.cpp"/> + <File Name="modules.h"/> + <File Name="server.cpp"/> + <File Name="server.h"/> </VirtualDirectory> + <Dependencies Name="Debug"/> + <Dependencies Name="Release"/> <Settings Type="Executable"> <GlobalSettings> <Compiler Options="" C_Options=""> @@ -23,12 +36,16 @@ <Configuration Name="Debug" CompilerType="gnu g++" DebuggerType="GNU gdb debugger" Type="Executable" BuildCmpWithGlobalSettings="append" BuildLnkWithGlobalSettings="append" BuildResWithGlobalSettings="append"> <Compiler Options="-g" C_Options="-g" Required="yes" PreCompiledHeader="" PCHInCommandLine="no" UseDifferentPCHFlags="no" PCHFlags=""> <IncludePath Value="."/> + <IncludePath Value="../proto_lib"/> </Compiler> <Linker Options="" Required="yes"> + <LibraryPath Value="../proto_lib/Debug"/> <Library Value="boost_system"/> <Library Value="pthread"/> <Library Value="ssl"/> <Library Value="crypto"/> + <Library Value="boost_filesystem"/> + <Library Value="proto"/> </Linker> <ResourceCompiler Options="" Required="no"/> <General OutputFile="$(IntermediateDirectory)/$(ProjectName)" IntermediateDirectory="./Debug" Command="./$(ProjectName)" CommandArguments="" UseSeparateDebugArgs="no" DebugArguments="" WorkingDirectory="$(IntermediateDirectory)" PauseExecWhenProcTerminates="yes"/> @@ -55,10 +72,12 @@ <CustomPostBuild/> <CustomPreBuild/> </AdditionalRules> - <Completion> + <Completion EnableCpp11="yes"> + <ClangCmpFlagsC/> <ClangCmpFlags/> <ClangPP/> - <SearchPaths/> + <SearchPaths>/usr/include +/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/include</SearchPaths> </Completion> </Configuration> <Configuration Name="Release" CompilerType="gnu g++" DebuggerType="GNU gdb debugger" Type="Executable" BuildCmpWithGlobalSettings="append" BuildLnkWithGlobalSettings="append" BuildResWithGlobalSettings="append"> @@ -74,7 +93,8 @@ <ResourceCompiler Options="" Required="no"/> <General OutputFile="$(IntermediateDirectory)/$(ProjectName)" IntermediateDirectory="./Release" Command="./$(ProjectName)" CommandArguments="" UseSeparateDebugArgs="no" DebugArguments="" WorkingDirectory="$(IntermediateDirectory)" PauseExecWhenProcTerminates="yes"/> <Environment EnvVarSetName="<Use Defaults>" DbgSetName="<Use Defaults>"> - <![CDATA[]]> + <![CDATA[ + ]]> </Environment> <Debugger IsRemote="no" RemoteHostName="" RemoteHostPort="" DebuggerPath=""> <PostConnectCommands/> @@ -96,7 +116,8 @@ <CustomPostBuild/> <CustomPreBuild/> </AdditionalRules> - <Completion> + <Completion EnableCpp11="no"> + <ClangCmpFlagsC/> <ClangCmpFlags/> <ClangPP/> <SearchPaths/> 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: "<<data_<<"\n"; + if(strstr(data_, "restart vbox")) + { + FILE *f = popen("/sbin/runscript /etc/init.d/vbox_headles restart --nodeps","r"); + if(f != NULL) + { + char buf[128]; + while(fgets(buf, 128, f) != NULL) + ; //TODO: do something with output + int s = pclose(f); // TODO: handle exit status + } + else + ; //TODO: handle fail + } + else if(strstr(data_, "reboot now")) + { + FILE *f = popen("reboot","r"); + if(f != NULL) + { + char buf[128]; + while(fgets(buf, 128, f) != NULL) + ; //TODO: do something with output + int s = pclose(f); // TODO: handle exit status + } + else + ; //TODO: handle fail + } + else if(strstr(data_, "halt now")) + { + FILE *f = popen("halt","r"); + if(f != NULL) + { + char buf[128]; + while(fgets(buf, 128, f) != NULL) + ; //TODO: do something with output + int s = pclose(f); // TODO: handle exit status + } + else + ; //TODO: handle fail + } + else if(strstr(data_, "restart cups")) + { + FILE *f = popen("/sbin/runscript /etc/init.d/cupsd restart --nodeps","r"); + if(f != NULL) + { + char buf[128]; + while(fgets(buf, 128, f) != NULL) + ; //TODO: do something with output + int s = pclose(f); // TODO: handle exit status + } + else + ; //TODO: handle fail + } + else if(strstr(data_, "restart ppp")) + { + FILE *f = popen("/sbin/runscript /etc/init.d/net.ppp0 stop --nodeps","r"); + char buf[128]; + int s = 0; + if(f != NULL) + { + while(fgets(buf, 128, f) != NULL) + ; //TODO: do something with output + s = pclose(f); // TODO: handle exit status + } + else + ; //TODO: handle fail + sleep(3); + f = popen("killall pppd","r"); + if(f == NULL) + { + while(fgets(buf, 128, f) != NULL) + ; //TODO: do something with output + s = pclose(f); // TODO: handle exit status + } + else + ; //TODO: handle fail + sleep(1); + f = popen("/sbin/runscript /etc/init.d/net.ppp0 start --nodeps","r"); + if(f == NULL) + { + while(fgets(buf, 128, f) != NULL) + ; //TODO: do something with output + s = pclose(f); // TODO: handle exit status + } + else + ; //TODO: handle fail + } +/* boost::asio::async_write(socket_, + boost::asio::buffer(data_, bytes_transferred), + boost::bind(&session::handle_write, this, + boost::asio::placeholders::error)); */ + } + delete this; +} diff --git a/server/server.h b/server/server.h new file mode 100644 index 0000000..c9028d3 --- /dev/null +++ b/server/server.h @@ -0,0 +1,118 @@ +// 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 SERVER_H +#define SERVER_H + +#include "headers.h" + +using boost::asio::ip::tcp; +typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> 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. + |