summaryrefslogtreecommitdiff
path: root/proto_lib/protocol.cpp
blob: e709415c976dd3228b546c0a7672ff10a3d53b89 (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
// 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;
}