diff options
Diffstat (limited to 'proto_lib')
-rw-r--r-- | proto_lib/api_protocol.h | 62 | ||||
-rw-r--r-- | proto_lib/lib.project | 105 | ||||
-rw-r--r-- | proto_lib/packet.cpp | 98 | ||||
-rw-r--r-- | proto_lib/packet.h | 38 | ||||
-rw-r--r-- | proto_lib/proto_lib.workspace | 12 | ||||
-rw-r--r-- | proto_lib/protocol.cpp | 64 |
6 files changed, 379 insertions, 0 deletions
diff --git a/proto_lib/api_protocol.h b/proto_lib/api_protocol.h new file mode 100644 index 0000000..8206854 --- /dev/null +++ b/proto_lib/api_protocol.h @@ -0,0 +1,62 @@ +// 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_PROTOCOL_H +#define API_PROTOCOL_H + + +struct service_s +{ + struct cmd + { + std::string command, description; + }; + std::string service; + std::list<cmd> cmds; +}; + +enum packet_type {AUTH_REPLY, AUTH_REQUEST, SERVICES_REPLY, SERVICES_REQUEST, COMMAND_REPLY, COMMAND_REQUEST, UNKNOWN}; + +class packet +{ +public: + packet(); + packet(std::vector<unsigned char>&); + const std::vector<unsigned char> &raw(); + packet_type get_type(); + bool is_good(); + bool is_server_packet(); + bool is_client_packet(); + bool assign(packet&); + bool assign(std::vector<unsigned char>&); +private: + std::vector<unsigned char> data; +}; + +//server functions +bool serv_validate_client_proto(packet&); + +//client functions + +packet cli_make_auth_packet(); //should be first packet to server +packet cli_make_command_packet(std::string &service, std::string &command); +packet cli_request_services(); +std::list<service_s> cli_extract_services(packet&); +bool cli_check_auth_reply(packet&); //false on fail +std::string cli_parse_command_reply(packet&); + + +#endif diff --git a/proto_lib/lib.project b/proto_lib/lib.project new file mode 100644 index 0000000..d8e56a4 --- /dev/null +++ b/proto_lib/lib.project @@ -0,0 +1,105 @@ +<?xml version="1.0" encoding="utf-8"?> +<CodeLite_Project Name="lib" InternalType="Console"> + <Plugins> + <Plugin Name="qmake"> + <![CDATA[00010001N0005Debug000000000000]]> + </Plugin> + </Plugins> + <Description/> + <Dependencies/> + <VirtualDirectory Name="src"> + <File Name="protocol.cpp"/> + <File Name="api_protocol.h"/> + <File Name="packet.cpp"/> + <File Name="packet.h"/> + </VirtualDirectory> + <Dependencies Name="Debug"/> + <Dependencies Name="Release"/> + <Settings Type="Static Library"> + <GlobalSettings> + <Compiler Options="" C_Options=""> + <IncludePath Value="."/> + </Compiler> + <Linker Options=""> + <LibraryPath Value="."/> + </Linker> + <ResourceCompiler Options=""/> + </GlobalSettings> + <Configuration Name="Debug" CompilerType="gnu g++" DebuggerType="GNU gdb debugger" Type="Static Library" BuildCmpWithGlobalSettings="append" BuildLnkWithGlobalSettings="append" BuildResWithGlobalSettings="append"> + <Compiler Options="-g;-O0;-Wall" C_Options="-g;-O0;-Wall" Required="yes" PreCompiledHeader="" PCHInCommandLine="no" UseDifferentPCHFlags="no" PCHFlags=""> + <IncludePath Value="."/> + </Compiler> + <Linker Options="" Required="yes"/> + <ResourceCompiler Options="" Required="no"/> + <General OutputFile="$(IntermediateDirectory)/libproto.a" IntermediateDirectory="./Debug" Command="./$(ProjectName)" CommandArguments="" UseSeparateDebugArgs="no" DebugArguments="" WorkingDirectory="$(IntermediateDirectory)" PauseExecWhenProcTerminates="yes"/> + <Environment EnvVarSetName="<Use Defaults>" DbgSetName="<Use Defaults>"> + <![CDATA[]]> + </Environment> + <Debugger IsRemote="no" RemoteHostName="" RemoteHostPort="" DebuggerPath=""> + <PostConnectCommands/> + <StartupCommands/> + </Debugger> + <PreBuild/> + <PostBuild/> + <CustomBuild Enabled="no"> + <RebuildCommand/> + <CleanCommand/> + <BuildCommand/> + <PreprocessFileCommand/> + <SingleFileCommand/> + <MakefileGenerationCommand/> + <ThirdPartyToolName>None</ThirdPartyToolName> + <WorkingDirectory/> + </CustomBuild> + <AdditionalRules> + <CustomPostBuild/> + <CustomPreBuild/> + </AdditionalRules> + <Completion EnableCpp11="yes"> + <ClangCmpFlagsC/> + <ClangCmpFlags/> + <ClangPP/> + <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="" BuildCmpWithGlobalSettings="append" BuildLnkWithGlobalSettings="append" BuildResWithGlobalSettings="append"> + <Compiler Options="-O2;-Wall" C_Options="-O2;-Wall" Required="yes" PreCompiledHeader="" PCHInCommandLine="no" UseDifferentPCHFlags="no" PCHFlags=""> + <IncludePath Value="."/> + </Compiler> + <Linker Options="" Required="yes"/> + <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[ + ]]> + </Environment> + <Debugger IsRemote="no" RemoteHostName="" RemoteHostPort="" DebuggerPath=""> + <PostConnectCommands/> + <StartupCommands/> + </Debugger> + <PreBuild/> + <PostBuild/> + <CustomBuild Enabled="no"> + <RebuildCommand/> + <CleanCommand/> + <BuildCommand/> + <PreprocessFileCommand/> + <SingleFileCommand/> + <MakefileGenerationCommand/> + <ThirdPartyToolName>None</ThirdPartyToolName> + <WorkingDirectory/> + </CustomBuild> + <AdditionalRules> + <CustomPostBuild/> + <CustomPreBuild/> + </AdditionalRules> + <Completion EnableCpp11="no"> + <ClangCmpFlagsC/> + <ClangCmpFlags/> + <ClangPP/> + <SearchPaths/> + </Completion> + </Configuration> + </Settings> +</CodeLite_Project> diff --git a/proto_lib/packet.cpp b/proto_lib/packet.cpp new file mode 100644 index 0000000..3e0d676 --- /dev/null +++ b/proto_lib/packet.cpp @@ -0,0 +1,98 @@ +// 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 <string> +#include <list> +#include <vector> +#include <algorithm> +#include "api_protocol.h" +#include "packet.h" + + +packet::packet() +{ +} + +packet::packet(std::vector<unsigned char>& new_data) +{ + data = new_data; +} + +bool packet::assign(packet& p) +{ + data = p.data; + return is_good(); +} + +bool packet::assign(std::vector<unsigned char>& v) +{ + data = v; + return is_good(); +} + +bool packet::is_good() +{ + if(data.empty()) + return false; + if(std::search(data.begin(), data.end(), proto_header, proto_header + sizeof(proto_header)) == data.end()) + return false; + if(std::search(data.begin(), data.end(), proto_footer, proto_footer + sizeof(proto_footer)) == data.end()) + return false; + return true; +} + +bool packet::is_client_packet() +{ + std::vector<unsigned char>::iterator i = data.begin(); + i+= sizeof(proto_header); + std::vector<unsigned char> cli; + cli.push_back(*i); + i++; + cli.push_back(*i); + if(std::search(cli.begin(), cli.end(), cli_packet, cli_packet + sizeof(cli_packet)) != cli.end()) + return true; + return false; +} + +bool packet::is_server_packet() +{ + std::vector<unsigned char>::iterator i = data.begin(); + i+= sizeof(proto_header); + std::vector<unsigned char> srv; + srv.push_back(*i); + i++; + srv.push_back(*i); + if(std::search(srv.begin(), srv.end(), serv_packet, serv_packet + sizeof(serv_packet)) != srv.end()) + return true; + return false; +} + +packet_type packet::get_type() +{ + std::vector<unsigned char>::iterator i = data.begin(); + i+= (sizeof(proto_header) + 2); + std::vector<unsigned char> type; + type.push_back(*i); + i++; + type.push_back(*i); + bool cli = is_client_packet(); + if(std::search(type.begin(), type.end(), type_auth, type_auth + sizeof(type_auth)) != type.end()) + return cli?AUTH_REQUEST:AUTH_REPLY; + if(std::search(type.begin(), type.end(), type_services, type_services + sizeof(type_services)) != type.end()) + return cli?SERVICES_REQUEST:SERVICES_REPLY; + if(std::search(type.begin(), type.end(), type_command, type_command + sizeof(type_command)) != type.end()) + return cli?COMMAND_REQUEST:COMMAND_REPLY; + return UNKNOWN; +} diff --git a/proto_lib/packet.h b/proto_lib/packet.h new file mode 100644 index 0000000..6ef9094 --- /dev/null +++ b/proto_lib/packet.h @@ -0,0 +1,38 @@ +// 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. + + + + +const unsigned char proto_header [] = { 0x06, 0x06, 0x06, 0x13}; +const unsigned char proto_footer [] = { 0x13, 0x06, 0x06, 0x06}; + +const unsigned char cli_packet [] = {0xF0, 0X01}; +const unsigned char serv_packet [] = {0xF0, 0X00}; + +const unsigned char proto_version = 0x03; + +const unsigned char delimiter = 0x01; + + + + +//each data type should be specified at begin and end of data block +//data types follow: + +const unsigned char type_auth [] = {0x01, 0x01}; +const unsigned char type_services [] = {0xA, 0x01}; +const unsigned char type_command [] = {0xA, 0x02}; diff --git a/proto_lib/proto_lib.workspace b/proto_lib/proto_lib.workspace new file mode 100644 index 0000000..bde095f --- /dev/null +++ b/proto_lib/proto_lib.workspace @@ -0,0 +1,12 @@ +<?xml version="1.0" encoding="utf-8"?> +<CodeLite_Workspace Name="proto_lib" Database="./proto_lib.tags"> + <Project Name="lib" Path="lib.project" Active="Yes"/> + <BuildMatrix> + <WorkspaceConfiguration Name="Debug" Selected="yes"> + <Project Name="lib" ConfigName="Debug"/> + </WorkspaceConfiguration> + <WorkspaceConfiguration Name="Release" Selected="yes"> + <Project Name="lib" ConfigName="Release"/> + </WorkspaceConfiguration> + </BuildMatrix> +</CodeLite_Workspace> diff --git a/proto_lib/protocol.cpp b/proto_lib/protocol.cpp new file mode 100644 index 0000000..e709415 --- /dev/null +++ b/proto_lib/protocol.cpp @@ -0,0 +1,64 @@ +// 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 <string> +#include <list> +#include <vector> +#include <algorithm> +#include "api_protocol.h" +#include "packet.h" + + + +bool serv_validate_client_proto(packet &p) +{ + const std::vector<unsigned char> &data = p.raw(); + if(data.empty()) + return false; + if(std::search(data.begin(), data.end(), proto_header, proto_header + sizeof(proto_header)) == data.end()) + return false; + if(std::search(data.begin(), data.end(), proto_footer, proto_footer + sizeof(proto_footer)) == data.end()) + return false; + std::vector<unsigned char>::const_iterator i = std::search(data.begin(), data.end(), cli_packet, cli_packet + sizeof(cli_packet)); + if(i == data.end()) + return false; + i += sizeof(cli_packet); + if(*i < proto_version) + return false; + return true; +} + +packet cli_make_auth_packet() +{ + std::vector<unsigned char> v; + int size = sizeof(proto_header); + for(int i = 0; i < size; i++) + v.push_back(proto_header[i]); + size = sizeof(cli_packet); + for(int i = 0; i < size; i++) + v.push_back(cli_packet[i]); + size = sizeof(type_auth); + for(int i = 0; i < size; i++) + v.push_back(type_auth[i]); + v.push_back(proto_version); + size = sizeof(proto_footer); + for(int i = 0; i < size; i++) + v.push_back(proto_footer[i]); + packet *p = new packet; + p->assign(v); + return *p; +} + |