diff options
author | Gluzskiy Alexandr <sss@sss.chaoslab.ru> | 2013-02-12 14:23:46 +0200 |
---|---|---|
committer | Gluzskiy Alexandr <sss@sss.chaoslab.ru> | 2013-02-12 14:23:46 +0200 |
commit | 1e4aaa2f93cc0a5c394a03d53c6707fe98c0c1c2 (patch) | |
tree | e727e815d5d74cc5387d7c0d567cf31849172ee2 /proto_lib | |
parent | b568c0cab255aefd400d48c05e2cabc6ca96c270 (diff) |
working with proto lib
Diffstat (limited to 'proto_lib')
-rw-r--r-- | proto_lib/api_protocol.h | 11 | ||||
-rw-r--r-- | proto_lib/lib.project | 2 | ||||
-rw-r--r-- | proto_lib/packet.cpp | 67 | ||||
-rw-r--r-- | proto_lib/packet.h | 5 | ||||
-rw-r--r-- | proto_lib/utilities.cpp | 87 | ||||
-rw-r--r-- | proto_lib/utilities.h | 32 |
6 files changed, 149 insertions, 55 deletions
diff --git a/proto_lib/api_protocol.h b/proto_lib/api_protocol.h index 452d6dc..af6d84b 100644 --- a/proto_lib/api_protocol.h +++ b/proto_lib/api_protocol.h @@ -34,12 +34,14 @@ enum status {failure = 0x00, success = 0x01}; class packet { public: - packet(std::vector<unsigned char>&); + explicit packet(std::vector<unsigned char>&); const std::vector<unsigned char> &raw(); packet_type get_type(); bool is_good(); + bool check_status(); //false on fail bool is_server_packet(); bool is_client_packet(); + bool is_status_packet(); bool assign(packet&); bool assign(std::vector<unsigned char>&); @@ -53,16 +55,13 @@ public: static std::string cli_parse_command_reply(packet&); //server functions - static bool serv_validate_client_proto(packet&); + static bool serv_validate_client_proto(packet&); //false on fail static packet *serv_make_services_packet(std::list<service_s>&); //generic - static packet *make_status_packet(const unsigned char* type, status s); - static bool check_status_packet(const unsigned char *type, packet&); //false on fail - + static packet *make_status_packet(packet_type type, status s); private: - packet(); std::vector<unsigned char> data; }; diff --git a/proto_lib/lib.project b/proto_lib/lib.project index a8ab117..39a0a37 100644 --- a/proto_lib/lib.project +++ b/proto_lib/lib.project @@ -11,6 +11,8 @@ <File Name="api_protocol.h"/> <File Name="packet.cpp"/> <File Name="packet.h"/> + <File Name="utilities.cpp"/> + <File Name="utilities.h"/> </VirtualDirectory> <Dependencies Name="Debug"/> <Dependencies Name="Release"/> diff --git a/proto_lib/packet.cpp b/proto_lib/packet.cpp index c52c24d..c9c3d6e 100644 --- a/proto_lib/packet.cpp +++ b/proto_lib/packet.cpp @@ -20,11 +20,9 @@ #include <string.h> #include "api_protocol.h" #include "packet.h" +#include "utilities.h" -packet::packet() -{ -} packet::packet(std::vector<unsigned char>& new_data) { @@ -98,50 +96,16 @@ packet_type packet::get_type() return UNKNOWN; } -void pack_cli_header(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]); -} - -void pack_serv_header(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(serv_packet); - for(int i = 0; i < size; i++) - v.push_back(serv_packet[i]); -} - - -void pack_buffer(const unsigned char *buf, size_t len, std::vector<unsigned char> &v) -{ - for(size_t i = 0; i < len; i++) - v.push_back(buf[i]); -} - -void pack_buffer(std::vector<unsigned char> &source, std::vector<unsigned char> &dest) +bool packet::is_status_packet() { - dest.insert(dest.end(), source.begin(), source.end()); + const unsigned char *type = to_internal_type(get_type()); + std::vector<unsigned char>::iterator i = std::search(data.begin(), data.end(), type, type + sizeof(type)); + i += sizeof(type); + if(*i == success || *i == failure) + return true; + return false; } -void pack_buffer(std::string &str, std::vector<unsigned char> &v) -{ - for(size_t i = 0; i < str.length(); i++) - v.push_back(str[i]); -} - -void pack_buffer(const char* str, std::vector<unsigned char> &v) -{ - size_t len = strlen(str); - for(size_t i = 0; i < len; i++) - v.push_back(str[i]); -} packet *packet::cli_make_auth_packet() { @@ -236,6 +200,11 @@ std::list<service_s> *packet::cli_extract_services(packet& p) return list; } +std::string packet::cli_parse_command_reply(packet&) +{ + return std::string(); +} + bool packet::serv_validate_client_proto(packet &p) { const std::vector<unsigned char> &data = p.raw(); @@ -280,8 +249,9 @@ packet *packet::serv_make_services_packet(std::list<service_s> &slist) return new packet(v); } -packet *packet::make_status_packet(const unsigned char* type, status s) +packet *packet::make_status_packet(packet_type t, status s) { + const unsigned char *type = to_internal_type(t); std::vector<unsigned char> v; pack_serv_header(v); pack_buffer(type, sizeof(type), v); @@ -290,10 +260,11 @@ packet *packet::make_status_packet(const unsigned char* type, status s) return new packet(v); } -bool packet::check_status_packet(const unsigned char *type, packet& p) +bool packet::check_status() { - std::vector<unsigned char>::const_iterator i = std::search(p.raw().begin(), p.raw().end(), type, type + sizeof(type)); - if(i != p.raw().end()) + const unsigned char *type = to_internal_type(get_type()); + std::vector<unsigned char>::const_iterator i = std::search(data.begin(), data.end(), type, type + sizeof(type)); + if(i != data.end()) { i += sizeof(type); if(*i == success) diff --git a/proto_lib/packet.h b/proto_lib/packet.h index aedb63f..58422cc 100644 --- a/proto_lib/packet.h +++ b/proto_lib/packet.h @@ -14,7 +14,8 @@ // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - +#ifndef PACKET_H +#define PACKET_H const unsigned char proto_header [] = { 0x06, 0x06, 0x06, 0x13}; @@ -36,3 +37,5 @@ const unsigned char block_end = 0x02; const unsigned char type_auth [] = {0x01, 0x01}; const unsigned char type_services [] = {0xA, 0x01}; const unsigned char type_command [] = {0xA, 0x02}; + +#endif diff --git a/proto_lib/utilities.cpp b/proto_lib/utilities.cpp new file mode 100644 index 0000000..1546ab6 --- /dev/null +++ b/proto_lib/utilities.cpp @@ -0,0 +1,87 @@ +// 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 <string.h> +#include "utilities.h" + +void pack_cli_header(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]); +} + +void pack_serv_header(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(serv_packet); + for(int i = 0; i < size; i++) + v.push_back(serv_packet[i]); +} + + +void pack_buffer(const unsigned char *buf, size_t len, std::vector<unsigned char> &v) +{ + for(size_t i = 0; i < len; i++) + v.push_back(buf[i]); +} + +void pack_buffer(std::vector<unsigned char> &source, std::vector<unsigned char> &dest) +{ + dest.insert(dest.end(), source.begin(), source.end()); +} + +void pack_buffer(std::string &str, std::vector<unsigned char> &v) +{ + for(size_t i = 0; i < str.length(); i++) + v.push_back(str[i]); +} + +void pack_buffer(const char* str, std::vector<unsigned char> &v) +{ + size_t len = strlen(str); + for(size_t i = 0; i < len; i++) + v.push_back(str[i]); +} + +const unsigned char *to_internal_type(packet_type t) +{ + const unsigned char *type = NULL; + switch(t) + { + case AUTH_REPLY: case AUTH_REQUEST: + type = type_auth; + break; + case SERVICES_REPLY: case SERVICES_REQUEST: + type = type_services; + break; + case COMMAND_REPLY: case COMMAND_REQUEST: + type = type_command; + break; + case UNKNOWN: default: + break; + }; + return type; +} diff --git a/proto_lib/utilities.h b/proto_lib/utilities.h new file mode 100644 index 0000000..7b8713b --- /dev/null +++ b/proto_lib/utilities.h @@ -0,0 +1,32 @@ +// 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 "packet.h" +#include "api_protocol.h" + +#ifndef UTILITIES_H +#define UTILITIES_H + +void pack_cli_header(std::vector<unsigned char> &v); +void pack_serv_header(std::vector<unsigned char> &v); +void pack_buffer(const unsigned char *buf, size_t len, std::vector<unsigned char> &v); +void pack_buffer(std::vector<unsigned char> &source, std::vector<unsigned char> &dest); +void pack_buffer(std::string &str, std::vector<unsigned char> &v); +void pack_buffer(const char* str, std::vector<unsigned char> &v); +const unsigned char *to_internal_type(packet_type t); + + +#endif |