summaryrefslogtreecommitdiff
path: root/proto_lib/packet.cpp
blob: 3e0d67649f359f475121660dda2522decc7e15b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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;
}