summaryrefslogtreecommitdiff
path: root/proto_lib/packet.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'proto_lib/packet.cpp')
-rw-r--r--proto_lib/packet.cpp98
1 files changed, 98 insertions, 0 deletions
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;
+}