-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.cpp
374 lines (342 loc) · 8.83 KB
/
client.cpp
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "client.h"
#include <string>
#include <iostream>
#include <sstream>
Client::Client(string login){
this->login = login;
stop = false;
incomingConnection = false;
activePartnerSocket = NULL;
try{
activeServerSocket = new SocketClient("127.0.0.1" , SERVER_DEFAULT_PORT);
}catch(...)
{
std::cout<<"[ERROR] Connection error.\n";
activeServerSocket = NULL;
}
}
int Client::registrationRequest(){
std::string toSend;
toSend = "REG";
toSend += ":";
toSend += login;
activeServerSocket->SendLine(toSend);
Sleep(100);
std::string r = activeServerSocket->ReceiveLine();
r = r.substr(0,r.size()-1);
vector<string> message = split(r,":");
if((message[0]=="OK")){
std::cout<<"Registration successfull.\n";
std::cout<<"Your password is: "<<message[1]<<std::endl;
return 0;
}else{
std::cout<<"[ERROR] Registration failed.\n";
return -1;
}
}
int Client::listRequest()
{
std::string toSend;
toSend = "LIST";
toSend += ":";
toSend += login;
activeServerSocket->SendLine(toSend);
Sleep(100);
std::string r = activeServerSocket->ReceiveLine();
r = r.substr(0,r.size()-1);
vector<string> message = split(r,":");
if (message[0]=="NOK") cout <<message[1]<<std::endl;
else
{
std::cout<<"Following users are online:\n";
for(int i =0;i<message.size();i++)
{
cout << message[i]<<endl;
}
}
return 0;
}
int Client::loginRequest(string password)
{
std::stringstream toSend;
toSend << "LOGIN";
toSend << ":";
toSend << login;
toSend << ":";
toSend << password;
cout << "Specify the port you will be available for incoming connections: ";
int p;
cin>> p;
port = p;
toSend << ":";
toSend << p;
activeServerSocket->SendLine(toSend.str());
Sleep(100);
std::string r = activeServerSocket->ReceiveLine();
r = r.substr(0,r.size()-1);
vector<string> message = split(r,":");
if((message[0]=="OK")){
std::cout<<"Login successful.\n";
SocketServer* server = new SocketServer(p , 1);
CWinThread* m_WaitingClient = new CWinThread();
m_WaitingClient = AfxBeginThread(clientWaiting , (LPVOID) server);
return 0;
}else{
std::cout<<"[ERROR] Login failed.\n";
return -1;
}
return 0;
}
int Client::communicationRequest(string partnerLogin)
{
string toSend = "";
toSend += "COMM";
toSend += ":";
toSend += login;
toSend += ":";
toSend += partnerLogin;
activeServerSocket->SendLine(toSend);
Sleep(100);
std::string r = activeServerSocket->ReceiveLine();
r = r.substr(0,r.size()-1);
vector<string> message = split(r,":");
if((message[0]=="OK")) cout << "Invitation send, wait for answer.\n";
else cout << "[ERROR] An error occured, communication won't be established.\n";
return 0;
}
int Client::logoutRequest(){
std::stringstream buff;
buff<<"LOGOUT:"<<login;
activeServerSocket->SendLine(buff.str());
Sleep(100);
std::string r = activeServerSocket->ReceiveLine();
r = r.substr(0,r.size()-1);
vector<string> message = split(r,":");
if((message[0]=="OK")) cout << "You have been disconnected.\n";
else cout << "[ERROR] An error occured, you are still connected.\n";
delete activeServerSocket;
activeServerSocket = NULL;
return 0;
}
int Client::cryptoSym(std::string key, unsigned char iv[16], std::string data, std::string& outData, int mode)
{
if(key.length() < 32)
key.append(32 - key.length(), '\0');
if(data.length() % 16 != 0)
data.append( 16 - (data.length() %16), '\0');
aes_context ctx;
outData = std::string("");
if(mode == AES_ENCRYPT)
{
aes_setkey_enc(&ctx, (unsigned char*)key.c_str(), 256);
for(int i = 0; i < 16; i++)
iv[i] = (unsigned char) rand();
}
else
aes_setkey_dec(&ctx, (unsigned char*)key.c_str(), 256);
std::string strIv((char*) iv, 16);
size_t length = ((data.length() / 16) + 1) * 16;
unsigned char *out = new unsigned char[length];
aes_crypt_cbc(&ctx, mode, data.size(), iv, (unsigned char*) data.c_str(), out);
outData = std::string((char*) out, length);
strcpy((char*) iv, strIv.c_str());
return true;
}
string Client::encipher(string text)
{
string cipherText;
cipherText.resize(text.size());
int i = 0;
bool done = false;
while(!done)
{
while (getPointerEnc != putPointerEnc)
{
if (i == text.size()) {done = true;break;}
cipherText[i] = text[i] ^ encBuffer[getPointerEnc];
cipherText[i]++;
i++;
getPointerEnc = (getPointerEnc + 1) % 1000;
}
}
return cipherText;
}
string Client::decipher(string text)
{
string plainText;
plainText.resize(text.size());
int i = 0;
bool done = false;
while(!done)
{
while (getPointerDec != putPointerDec)
{
if (i == text.size()) {done = true;break;}
text[i]--;
plainText[i] = text[i] ^ decBuffer[getPointerDec];
i++;
getPointerDec = (getPointerDec + 1) % 1000;
}
}
return plainText;
}
int Client::connectToPartner(std::string value){
if(!activePartnerSocket){
partnerName = value;
communicationRequest(value);
return 0;
}else{
std::cout<<"[INVALID COMMAND] You already have ongoing communication.\n";
return -1;
}
}
int Client::acceptComm(){
if(incomingConnection){
std::stringstream buff;
ctr_drbg_context ctr_drbg;
entropy_context entropy;
char *pers = "Rebus in adversis animum submittere noli: spem retine, spes una hominen nec morte relin quit.";
entropy_init( &entropy );
ctr_drbg_init( &ctr_drbg, entropy_func, &entropy , (unsigned char *) pers, strlen( pers ) );
ctr_drbg_random( &ctr_drbg , key , 16 );
std::string keyToSend;
for(int i = 0 ; i < 16 ; i++){
keyToSend.push_back(key[i]);
}
buff<<"YES:"<<port<<":"<<keyToSend;
Sleep(100);
activePartnerSocket->SendLine(buff.str());
std::cout<<"You are now connected to "<<partnerName<<"."<<std::endl;
stop = false;
CWinThread* crypt = new CWinThread();
crypt = AfxBeginThread(pre_generatingKeyEnc , (LPVOID) this);
return 0;
}else{
std::cout<<"[INVALID COMMAND] You have currently no incoming connections.\n";
return -1;
}
}
int Client::declineComm(){
if(incomingConnection){
std::string buff;
buff = "NO";
activePartnerSocket->SendLine(buff);
delete activePartnerSocket;
activePartnerSocket = NULL;
return 0;
}else{
std::cout<<"[INVALID COMMAND] You have currently no incoming connections.\n";
return -1;
}
}
int Client::endComm(){
if(activePartnerSocket){
std::string buff;
buff = "END";
activePartnerSocket->SendLine(buff);
delete activePartnerSocket;
activePartnerSocket = NULL;
stop = true;
return 0;
}else{
std::cout<<"[INVALID COMMAND] You have currently no ongoing communication.\n";
return -1;
}
}
int Client::disconnect(){
if(activeServerSocket){
logoutRequest();
return 0;
}else{
std::cout<<"[INVALID COMMAND] You are not connected at this moment.\n";
return -1;
}
}
int Client::quit(){
if(activePartnerSocket){
endComm();
}
if(activeServerSocket){
logoutRequest();
}
return 0;
}
int Client::sendMessage(std::string value){
if(activePartnerSocket){
std::string mess = "MESS:" + value;
mess = encipher(mess);
activePartnerSocket->SendLine(mess);
return 0;
}else{
std::cout<<"[INVALID COMMAND] Message not sent(no recipient connected)\n";
return -1;
}
}
bool Client::command(std::string cmd){
int action;
std::string value;
action = commandParse(value , cmd);
switch(action){
case 0:
help();
break;
case 1:
registrationRequest();
break;
case 2:
loginRequest(value);
break;
case 3:
listRequest();
break;
case 4:
connectToPartner(value);
break;
case 5:
acceptComm();
break;
case 6:
declineComm();
break;
case 7:
endComm();
break;
case 8:
disconnect();
break;
case 9:
quit();
std::cout<<"Have a nice day.\n";
return true;
}
return false;
}
int Client::commandParse(std::string& value , std::string cmd){
std::vector<string> command = split(cmd , ":");
if(command.size() > 1)
value = command[1];
if(command[0].compare("/help") == 0){return 0;}
if(command[0].compare("/register") == 0){return 1;}
if(command[0].compare("/login") == 0){return 2;}
if(command[0].compare("/online") == 0){return 3;}
if(command[0].compare("/connect") == 0){return 4;}
if(command[0].compare("/accept") == 0){return 5;}
if(command[0].compare("/decline") == 0){return 6;}
if(command[0].compare("/end") == 0){return 7;}
if(command[0].compare("/disconnect") == 0){return 8;}
if(command[0].compare("/quit") == 0){return 9;}
std::cout<<"Unknown command.\n";
return -1;
}
void Client::help(){
std::cout<<"Type:\n";
std::cout<<" \"/register\" to register on server\n";//1
std::cout<<" \"/login:[pwd]\" to log in on server, pwd is your password\n";//2
std::cout<<" \"/online\" to see who is online\n";//3
std::cout<<" \"/connect:[user]\" to connect to specific user\n";//4
std::cout<<" \"/accept\" to accept incoming connection\n";//5
std::cout<<" \"/decline\" to decline incoming connection\n";//6
std::cout<<" \"/end\" to end actual communication\n";//7
std::cout<<" \"/disconnect\" to disconnect from server\n";//8
std::cout<<" \"/quit\" to end program\n";//9
}