-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cpp
449 lines (414 loc) · 13.8 KB
/
server.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <assert.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include "threadpool.h"
using namespace std;
#define MAXBUF 1024
#define THREAD 32
#define QUEUE 256
#define MAXACCOUNT 512
typedef struct threadpool_t threadpool_t;
typedef struct ClientInfo
{
struct sockaddr_in addr;
int clientfd;
int isConn;
int index;
SSL *ssl;
} ClientInfo;
ClientInfo clients[THREAD];
int client_num = 0;
char client_account_name[MAXACCOUNT][1024];
char client_ip_address[MAXACCOUNT][1024];
int client_port_num[MAXACCOUNT];
int client_account_balance[MAXACCOUNT] = {0};
int client_online[MAXACCOUNT] = {0};
SSL_CTX *ctx;
pthread_mutex_t lock1;
void conversation(void* argv);
int main(int argc, char *argv[])
{
//init ssl
SSL_library_init();
OpenSSL_add_all_algorithms();
SSL_load_error_strings();
ctx = SSL_CTX_new(SSLv23_server_method());
char* temp;
char pwd[100];
getcwd(pwd,100);
if (strlen(pwd) == 1) { pwd[0]='\0'; }
if (SSL_CTX_use_certificate_file(ctx, temp=strcat(pwd,"/ssl/serverCert.pem"), SSL_FILETYPE_PEM) <= 0)
{
ERR_print_errors_fp(stdout);
exit(1);
}
getcwd(pwd,100);
if (strlen(pwd) == 1) { pwd[0]='\0'; }
if (SSL_CTX_use_PrivateKey_file(ctx, temp=strcat(pwd,"/ssl/serverKey.pem"), SSL_FILETYPE_PEM) <= 0)
{
ERR_print_errors_fp(stdout);
exit(1);
}
if (!SSL_CTX_check_private_key(ctx))
{
ERR_print_errors_fp(stdout);
exit(1);
}
for(int i = 0; i < THREAD; i++)
clients[i].isConn = 0;
//socket的建立
int sockfd = 0;
sockfd = socket(AF_INET , SOCK_STREAM , 0);
if (sockfd == -1)
{
cout << "Fail to create a socket.\n";
}
//socket的連線
struct sockaddr_in serverInfo;
bzero(&serverInfo,sizeof(serverInfo));
serverInfo.sin_family = PF_INET;
serverInfo.sin_addr.s_addr = INADDR_ANY;
serverInfo.sin_port = htons(8700);
bind(sockfd,(struct sockaddr *)&serverInfo,sizeof(serverInfo));
listen(sockfd,10);
//init thread pool
threadpool_t *pool;
pthread_mutex_init(&lock1, NULL);
assert((pool = threadpool_create(THREAD, QUEUE, 0)) != NULL);
//waiting for client connection
while(1){
int forClientSockfd = 0;
struct sockaddr_in clientInfo;
socklen_t addrlen = sizeof(clientInfo);
forClientSockfd = accept(sockfd,(struct sockaddr*) &clientInfo, &addrlen);
if(forClientSockfd == -1)
{
cout << "Fail to accept socket\n";
continue;
}
else
{
int i = 0;
while(i < THREAD)
{
if(clients[i].isConn == 0)
break;
else
i++;
}
cout << "Accept socket successfully\n";
clients[i].clientfd = forClientSockfd;
clients[i].addr = clientInfo;
clients[i].isConn = 1;
clients[i].index = i;
SSL *ssl;
ssl = SSL_new(ctx);
SSL_set_fd(ssl, forClientSockfd);
if (SSL_accept(ssl) == -1)
{
perror("accept");
close(forClientSockfd);
break;
}
clients[i].ssl = ssl;
threadpool_add(pool, &conversation, &clients[i], 0);
}
}
assert(threadpool_destroy(pool, 0) == 0);
return 0;
}
void conversation(void* argv)
{
cout << "Enter conversation\n";
pthread_mutex_lock(&lock1);
ClientInfo *client = (ClientInfo *)(argv);
int clientfd = client->clientfd;
struct sockaddr_in addr = client->addr;
pthread_mutex_unlock(&lock1);
cout << "ip: " << inet_ntoa(addr.sin_addr) << "\t port: " << ntohs(addr.sin_port) << endl;
SSL *ssl = client->ssl;
while (1){
char name[1024];
char msg_recieved[1024];
int handler = 0;
memset(msg_recieved , 0 , sizeof(msg_recieved));
handler = SSL_read(ssl, msg_recieved, MAXBUF);
if(handler > 0)
printf("Receive Complete !\n");
else
{
printf("Failure to receive message ! Error code is %d,Error messages are '%s'\n", errno, strerror(errno));
strcpy(msg_recieved, "Exit");
}
cout << "Recieve: " << msg_recieved << endl;
char msg_sent[1024];
char const delim[2] = "#";
char *token;
token = strtok(msg_recieved, delim);
if (strcmp(token, "REGISTER") == 0)
{
token = strtok(NULL, delim);
char *token2;
token2 = strtok(NULL, delim);
int port = atoi(token2);
int account_exist = 0;
for (int i = 0; i < client_num; ++i)
{
if (strcmp(token, client_account_name[i]) == 0)
{
strcpy(msg_sent, "210 FALL");
account_exist = 1;
break;
}
}
if (account_exist == 0)
{
strcpy(msg_sent, "100 OK");
strcpy(name, token);
pthread_mutex_lock(&lock1);
strcpy(client_account_name[client_num], token);
strcpy(client_ip_address[client_num], inet_ntoa(addr.sin_addr));
client_port_num[client_num] = port;
client_online[client_num] = 1;
client_num += 1;
pthread_mutex_unlock(&lock1);
}
}
else if (strcmp(token, "DEPOSIT") == 0)
{
token = strtok(NULL, delim);
int account_exist = 0;
int account_index = 0;
for (int i = 0; i < client_num; ++i)
{
if (strcmp(token, client_account_name[i]) == 0)
{
account_exist = 1;
account_index = i;
break;
}
}
if (account_exist == 0)
{
strcpy(msg_sent, "DEPOSIT_FALL");
}
else
{
token = strtok(NULL, delim);
pthread_mutex_lock(&lock1);
client_account_balance[account_index] = atoi(token);
pthread_mutex_unlock(&lock1);
strcpy(msg_sent, "DEPOSIT_OK");
}
}
else if (strcmp(token, "List") == 0)
{
int account_index = 0;
int client_online_num = 0;
for (int i = 0; i < client_num; ++i)
{
if (client_online[i] != 0)
{
client_online_num += 1;
}
}
pthread_mutex_lock(&lock1);
char list[2048];
strcpy(list, "Online Clients: ");
char client_online_num_char[1024];
sprintf(client_online_num_char, "%d", client_online_num);
strcat(list, client_online_num_char);
strcat(list, "\n");
strcat(list, "Online Clients Info: ");
strcat(list, "\n");
pthread_mutex_unlock(&lock1);
for (int i = 0; i < client_num; i++)
{
if (strcmp(client_account_name[i], name) == 0)
{
account_index = i;
}
if (client_online[i] == 1)
{
pthread_mutex_lock(&lock1);
strcat(list, client_account_name[i]);
strcat(list, "#");
strcat(list, client_ip_address[i]);
strcat(list, "#");
char client_port_num_char[1024];
sprintf(client_port_num_char, "%d", client_port_num[i]);
strcat(list, client_port_num_char);
strcat(list, "\n");
pthread_mutex_unlock(&lock1);
}
}
pthread_mutex_lock(&lock1);
strcpy(msg_sent, "Account Balance: ");
char client_account_balance_char[1024];
sprintf(client_account_balance_char, "%d", client_account_balance[account_index]);
strcat(msg_sent, client_account_balance_char);
strcat(msg_sent, "\n");
strcat(msg_sent, list);
pthread_mutex_unlock(&lock1);
}
else if (strcmp(token, "Exit") == 0)
{
for (int i = 0; i < MAXACCOUNT; i++)
{
if (strcmp(client_account_name[i], name) == 0)
{
pthread_mutex_lock(&lock1);
client_online[i] = 0;
pthread_mutex_unlock(&lock1);
break;
}
}
break;
}
else if (strcmp(token, "Pay") == 0)
{
token = strtok(NULL, delim);
int got = 0;
for (int i = 0; i < client_num; i++)
{
if(strcmp(client_account_name[i], token) == 0 && client_online[i] == 1)
{
got = 1;
strcpy(msg_sent, "On");
strcat(msg_sent, "#");
strcat(msg_sent, client_ip_address[i]);
strcat(msg_sent, "#");
char client_port_num_char[1024];
sprintf(client_port_num_char, "%d", client_port_num[i]);
strcat(msg_sent, client_port_num_char);
break;
}
}
if (got == 0)
{
strcpy(msg_sent, "Off");
}
}
else if (strcmp(token, "Paid") == 0)
{
char *payer;
int pay_amount;
char *payee;
payer = strtok(NULL, delim);
pay_amount = atoi(strtok(NULL, delim));
payee = strtok(NULL, delim);
int payer_i = -1;
int payee_i = -1;
for (int i = 0; i < client_num; i++)
{
if(strcmp(client_account_name[i], payer) == 0)
{
payer_i = i;
}
if(strcmp(client_account_name[i], payee) == 0)
{
payee_i = i;
}
}
if (payer_i == payee_i || client_account_balance[payer_i] < pay_amount || payer_i == -1 || payee_i == -1)
{
strcpy(msg_sent, "Fail");
}
else
{
client_account_balance[payer_i] -= pay_amount;
client_account_balance[payee_i] += pay_amount;
strcpy(msg_sent, "Successful");
}
}
else
{
int client_online_num = 0;
for (int i = 0; i < client_num; i++)
{
if (client_online[i] != 0)
{
client_online_num += 1;
}
}
pthread_mutex_lock(&lock1);
char list[2048];
strcpy(list, "Online Clients: ");
char client_online_num_char[1024];
sprintf(client_online_num_char, "%d", client_online_num+1);
strcat(list, client_online_num_char);
strcat(list, "\n");
strcat(list, "Online Clients Info: ");
strcat(list, "\n");
pthread_mutex_unlock(&lock1);
char *token2;
token2 = strtok(NULL, delim);
int port_num = atoi(token2);
int account_exist = 0;
int account_index = 0;
for (int i = 0; i < client_num; ++i)
{
if (strcmp(client_account_name[i], token) == 0)
{
client_port_num[i] = port_num;
account_index = i;
account_exist = 1;
strcpy(name, token);
pthread_mutex_lock(&lock1);
client_online[i] = 1;
pthread_mutex_unlock(&lock1);
}
if (client_online[i] == 1)
{
pthread_mutex_lock(&lock1);
strcat(list, client_account_name[i]);
strcat(list, "#");
strcat(list, client_ip_address[i]);
strcat(list, "#");
char client_port_num_char[1024];
sprintf(client_port_num_char, "%d", client_port_num[i]);
strcat(list, client_port_num_char);
strcat(list, "\n");
pthread_mutex_unlock(&lock1);
}
}
if (account_exist == 0)
{
strcpy(msg_sent, "220 AUTH_FALL");
cout << "Sent: \n" << msg_sent << endl;
send(clientfd,&msg_sent,sizeof(msg_sent),0);
return;
}
else
{
pthread_mutex_lock(&lock1);
strcpy(msg_sent, "Account Balance: ");
char client_account_balance_char[1024];
sprintf(client_account_balance_char, "%d", client_account_balance[account_index]);
strcat(msg_sent, client_account_balance_char);
strcat(msg_sent, "\n");
strcat(msg_sent, list);
pthread_mutex_unlock(&lock1);
}
}
cout << "Sent: \n" << msg_sent << endl;
SSL_write(ssl, msg_sent, strlen(msg_sent));
memset(msg_sent , 0 , sizeof(msg_sent));
}
char msg_sent[1024];
strcpy(msg_sent, "Bye");
cout << "Sent: \n" << msg_sent << endl;
SSL_write(ssl, msg_sent, strlen(msg_sent));
memset(msg_sent , 0 , sizeof(msg_sent));
return;
}