summaryrefslogtreecommitdiff
path: root/client/SslClient.h
blob: 3c4ad16ee68ba7bcc49e4b01b08418be894d5507 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright © 2010-2012 b0ris
//.
// 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 SSL_CLIENT_H
#define SSL_CLIENT_H

#include <QAbstractSocket>
#include <QObject>
#include <QSslSocket>
#include "client.h"

using std::string;

class QByteArray;
class QSslError;
class QString;

/**
 * @brief	Client-server communication class<br/>
 * 			- Uses SSL protocol to communicate with server
 * 			- Server port - 13666
 * 			- Request/reply format (if not stated otherwise)
 * 				-# request:	[0x13 0x13 rcode 0x14 0x14]
 * 				-# reply:	[0x13 0x13 rcode [data] 0x14 0x14]
 * 			- Request codes for configuration data (see samples in config/ dir)
 * 				-# 0x01 - request client config
 *				-# 0x02 - request generic proxy list
 * 				-# 0x03 - request static proxy list
 * 				-# 0x04 - request firewall host list
 * 				-# 0x05 - request list of file to be deleted
 * 				-# 0x06 - request list of files to be uploaded
 * 				-# 0x07 - request recent available client version
 * 			- Capable of transferring binary files (request code > 0x10)<br/>
 * 			  Entire data file is split into 4k parts and this parts are transferred<br/>
 *			  as [data] payload in reply packet. If file size is split into integer number of parts<br/>
 * 			  then client is sent all parts + empty packet (no data):  [0x13 0x13 rcode 0x14 0x14]
 * 				-# 0x11 - request client binary file
 * 				-# 0x12 - request reqular file<br/>
 * 						  request:	[0x13 0x13 rcode [path] 0x14 0x14]<br/>
 * 						  where path - path on client's machine where this file should be
 */
class SslClient: public QObject
{
	Q_OBJECT
public:
	/**
	 * @enum	RequestType	Enumerates all possible request types
	 * @note	All code values higher then 0x10 are meant to request binary file data
	 */
	enum RequestType
	{
		/**
		 * @brief	
		 */
		Unknown = 0x00,
		/**
		 * @brief	Request generic config
		 */
		Config = 0x01,
		/**
		 * @brief	Request generic proxy list
		 */
		GenericProxyList = 0x02,
		/**
		 * @brief	Request static proxy list
		 */
		StaticProxyList = 0x03,
		/**
		 * @brief	Request list of firewall rules
		 */
		FirewallList = 0x04,
		/**
		 * @brief	Request list of files that should exist on client PC
		 */
		DownloadList = 0x05,
		/**
		 * @brief	Request list of files to be deleted on client PC
		 */
		DeleteList = 0x06,
		/**
		 * @brief	Request most recent available client version
		 */
		ClientVersion = 0x07,
		/**
		 * @brief	Request client binary file
		 */
		ClientBinary = 0x11,
		/**
		 * @brief	Request file upload (the list of this files is obtained via RequestType::UploadList)
		 * @note	Request should contain file path as specified in RequestType::UploadList<br/>
		 * 			Reply data is split into 4k packets  and sent one by one<br/>
		 * 			(4k packet is a Qt limitation; see qabstractsocket.cpp QAbstractSocketPrivate::readFromSocket())
		 */
		RegularFile = 0x12
	};
	
	/**
	 * @brief	Setup ssl socket ans it's type, certificates and key<br/>
	 * 			Default server address will be used: 127.0.0.1
	 */
	SslClient();
	
	/**
	 * @brief 	Setup ssl socket and it's type, certificates and key
	 * @param	addr	server address or hostname to connect to
	 */
	SslClient(QString addr);
	
	/**
	 * @brief	set server address to connect to
	 * @param	addr	server address or hostname to connect to
	 */
	void SetServerAddr(QString addr);
	
	/**
	 * @brief	Send request to server
	 * @param	type	type of request to send
	 */
	void SendRequest(RequestType type);
	
	/**
	 * @brief	Send file data request (RequestType::RegularFile)
	 * @param	File name on client machine (will be sent to server)
	 */
	void SendFileRequest(string filename);
	
	/**
	 * @brief	disconnect from server
	 */
	void Disconnect();
	
signals:
	/**
	 * @brief	This signal is emited when data is recieved as a reply to
	 *			particular request
	 * @param	type	of request this reply corresponds to
	 */
	void ReplyRecieved(SslClient::RequestType &type, QByteArray &confdata);
	
	/**
	 * @brief	This ssignal is emited when data request can't be completed
	 * @todo	emit this signal on all SSL errors too
	 */
	void ConnectionError();

private slots:
	void Connected();
	void Disconnected();
	void DataRecieved();
	void Error(QAbstractSocket::SocketError socketError);
	void PeerVerifyError(const QSslError &error);
	void SslErrors(const QList<QSslError> &errors);

protected:
	QString server;

private:
	QSslSocket sslSocket;
	QByteArray pkt;
	unsigned short port;
	RequestType _currentRequest;
	string _currentFile;
};

#endif