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 --- proto_lib/api_protocol.h | 62 +++++++++++++++++++++++++ proto_lib/lib.project | 105 ++++++++++++++++++++++++++++++++++++++++++ proto_lib/packet.cpp | 98 +++++++++++++++++++++++++++++++++++++++ proto_lib/packet.h | 38 +++++++++++++++ proto_lib/proto_lib.workspace | 12 +++++ proto_lib/protocol.cpp | 64 +++++++++++++++++++++++++ 6 files changed, 379 insertions(+) create mode 100644 proto_lib/api_protocol.h create mode 100644 proto_lib/lib.project create mode 100644 proto_lib/packet.cpp create mode 100644 proto_lib/packet.h create mode 100644 proto_lib/proto_lib.workspace create mode 100644 proto_lib/protocol.cpp (limited to 'proto_lib') 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 cmds; +}; + +enum packet_type {AUTH_REPLY, AUTH_REQUEST, SERVICES_REPLY, SERVICES_REQUEST, COMMAND_REPLY, COMMAND_REQUEST, UNKNOWN}; + +class packet +{ +public: + packet(); + packet(std::vector&); + const std::vector &raw(); + packet_type get_type(); + bool is_good(); + bool is_server_packet(); + bool is_client_packet(); + bool assign(packet&); + bool assign(std::vector&); +private: + std::vector 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 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 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + None + + + + + + + + + + + /usr/include +/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/include + + + + + + + + + + + + + + + + + + + + + + + + + + None + + + + + + + + + + + + + + + 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 +#include +#include +#include +#include "api_protocol.h" +#include "packet.h" + + +packet::packet() +{ +} + +packet::packet(std::vector& new_data) +{ + data = new_data; +} + +bool packet::assign(packet& p) +{ + data = p.data; + return is_good(); +} + +bool packet::assign(std::vector& 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::iterator i = data.begin(); + i+= sizeof(proto_header); + std::vector 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::iterator i = data.begin(); + i+= sizeof(proto_header); + std::vector 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::iterator i = data.begin(); + i+= (sizeof(proto_header) + 2); + std::vector 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 @@ + + + + + + + + + + + + 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 +#include +#include +#include +#include "api_protocol.h" +#include "packet.h" + + + +bool serv_validate_client_proto(packet &p) +{ + const std::vector &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::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 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; +} + -- cgit v1.2.3