-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
508 lines (449 loc) · 14 KB
/
server.cpp
File metadata and controls
508 lines (449 loc) · 14 KB
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#include <bits/stdc++.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
using namespace std;
#define PORT 8080
#define BACKLOG 10
#define BUFFER_SIZE 1024
#define CONTENT_SIZE 100000
pthread_mutex_t mutex_lock;
unordered_map<string,int> mp[3];
struct broadcast_data {
int status;
int connection_socket;
int recv_socket;
string response;
string recp;
string sender;
};
void error(string err, int ex = 1) {
cout<<err<<"\n";
if(ex) exit(-1);
}
void server_log(string res, string user) {
if(user == ""){
user = "UNREGISTERED_CONNECTION";
}
cout<<"Server TO " + user + " :\n";
cout<<res;
cout<<"\n------------------------------\n";
}
void client_log(string res, string user) {
if(user == ""){
user = "UNREGISTERED_CONNECTION";
}
cout<<"From client: " + user + " :\n";
cout<<res;
cout<<"\n------------------------------\n";
}
bool valid_username(string user) {
if(user.size() == 0){
return false;
}
for(auto ch:user) {
if(!isalnum(ch)) {
return false;
}
}
if(user == "ALL") {
return false;
}
return true;
}
void refresh_buffer(char buffer[], int ¤tIndex,int idx) {
memmove(buffer, buffer + idx + 1, currentIndex - idx - 1);
currentIndex = currentIndex - idx - 1;
buffer[currentIndex] = '\0';
}
bool check(string msg, string val, int idx){
if(msg.size() < val.size()+idx) {
return false;
}
if(val == msg.substr(idx,val.size())) {
return true;
}
return false;
}
void exit_thread(int sock, int ex = 1, string username = "") {
close(sock);
if(ex) {
if(username != "") {
pthread_mutex_lock(&mutex_lock);
mp[2][username] = 0;
close(mp[0][username]);
mp[0].erase(username);
mp[1].erase(username);
pthread_mutex_unlock(&mutex_lock);
}
pthread_exit(NULL);
} else if(username != "") {
mp[2][username] = 0;
close(mp[0][username]);
mp[0].erase(username);
mp[1].erase(username);
}
}
int wrap_send(int connection_socket, string response, string username) {
server_log(response, username);
const char* data = response.c_str();
int tosend = strlen(data);
int sent = 0;
while(tosend>0) {
int length = send(connection_socket, data + sent, tosend, 0);
if(length <= 0){
error("Sending failed", 0);
return 1;
}
sent += length;
tosend -= length;
}
return 0;
}
int read_buffer(char buffer[], int sz, int st){
for(int i=st; i<sz-1; i++) {
if(buffer[i]=='\n' && buffer[i+1]=='\n'){
return i+1;
}
}
return -1;
}
int wrap_read(int connection_socket, char buffer[], int ¤tIndex) {
int st = 0;
while(true){
int idx = read_buffer(buffer, currentIndex, st);
if(idx != -1){
return idx;
}
if(currentIndex == BUFFER_SIZE){
break;
}
int read_res = recv(connection_socket, buffer + currentIndex, BUFFER_SIZE - currentIndex, 0);
if(read_res <= 0){
return -2;
}
st = currentIndex;
currentIndex += read_res;
}
return -1;
}
void inform_sender(int connection_socket, string sender, string recp, int status) {
if(status == 0) {
string resp = "SEND " + recp + "\n\n";
if(wrap_send(connection_socket, resp, sender)) {
exit_thread(connection_socket, 1, sender);
}
} else if(status == 102) {
string resp = "ERROR 102 Unable to send\n\n";
if(wrap_send(connection_socket, resp, sender)) {
exit_thread(connection_socket, 1, sender);
}
}
else if(status == 103) {
string resp = "ERROR 103 Header incomplete\n\n";
if(wrap_send(connection_socket, resp, sender)) {
exit_thread(connection_socket, 1, sender);
}
exit_thread(connection_socket, 1, sender);
}
}
int forward(int connection_socket, string recp, string response, string sender, int recv_socket) {
if(wrap_send(recv_socket, response, recp)) {
exit_thread(recv_socket, 0, recp);
return 102;
}
char recv_buffer[BUFFER_SIZE+1];
int recvIndex = 0;
int idx = wrap_read(recv_socket, recv_buffer, recvIndex);
if(idx == -2) {
exit_thread(recv_socket, 0, recp);
return 103;
}
if(idx == -1) {
// invalid response from receptor client
client_log("Some unwanted data", recp);
return 103;
}
recv_buffer[idx] = '\0';
string msg(recv_buffer);
client_log(msg, recp);
if(msg == "RECEIVED " + sender + "\n") {
return 0;
} else if(msg == "ERROR 103 Header incomplete\n") {
return 103;
} else {
return 102;
}
}
void * broadcast(void *th_arg) {
struct broadcast_data *data = (struct broadcast_data*)th_arg;
data->status = forward(data->connection_socket, data->recp, data->response, data->sender, data->recv_socket);
pthread_exit(NULL);
}
void * handle_connection(void *th_arg) {
int connection_socket = *((int *)th_arg);
bool registered = false;
int currentIndex = 0;
char buffer[BUFFER_SIZE+1];
int type = -1;
string username;
// registration
while(true){
int idx = wrap_read(connection_socket, buffer, currentIndex);
if(idx == -2){
exit_thread(connection_socket);
}
if(idx == -1){
client_log("Some unwanted data",username);
string response = "ERROR 101 No user registered\n\n";
if(wrap_send(connection_socket, response, username)) exit_thread(connection_socket);
buffer[0] = '\0';
currentIndex = 0;
continue;
}
buffer[idx] = '\0';
string msg(buffer);
client_log(msg, username);
if(!check(msg, "REGISTER", 0)) {
string response = "ERROR 101 No user registered\n\n";
if(wrap_send(connection_socket, response, username)) exit_thread(connection_socket);
refresh_buffer(buffer, currentIndex, idx);
continue;
}
if(!check(msg, "TOSEND", 9) && !check(msg, "TORECV", 9)) {
string response = "ERROR 101 No user registered\n\n";
if(wrap_send(connection_socket, response, username)) exit_thread(connection_socket);
refresh_buffer(buffer, currentIndex, idx);
continue;
}
username = msg.substr(16, idx-17);
if(!valid_username(username)) {
string response = "ERROR 100 Malformed username\n\n";
if(wrap_send(connection_socket, response, username)) exit_thread(connection_socket);
refresh_buffer(buffer, currentIndex, idx);
continue;
}
type = msg.substr(9,6) == "TOSEND" ? 1 : 0;
pthread_mutex_lock(&mutex_lock);
bool already_registered = mp[type].find(username)!=mp[type].end();
pthread_mutex_unlock(&mutex_lock);
if(already_registered) {
string response = "ERROR 100 Malformed username\n\n";
if(wrap_send(connection_socket, response, username)) exit_thread(connection_socket);
refresh_buffer(buffer, currentIndex, idx);
continue;
}
string tmp = type==0 ?"TORECV ":"TOSEND ";
string response = "REGISTERED " + tmp + username + "\n\n";
if(wrap_send(connection_socket, response, username)) exit_thread(connection_socket);
pthread_mutex_lock(&mutex_lock);
mp[type][username] = connection_socket;
mp[2][username]++;
pthread_mutex_unlock(&mutex_lock);
refresh_buffer(buffer, currentIndex, idx);
break;
}
// close if receiving thread
if(!type) {
pthread_exit(NULL);
}
// chat
while(true) {
// read header
int idx = wrap_read(connection_socket, buffer, currentIndex);
if(idx == -2) {
exit_thread(connection_socket, 1, username);
}
if(!registered) {
pthread_mutex_lock(&mutex_lock);
bool check = mp[2][username] == 2;
pthread_mutex_unlock(&mutex_lock);
if(check){
registered = true;
}
else{
string response = "ERROR 101 No user registered\n\n";
if(wrap_send(connection_socket, response, username)) exit_thread(connection_socket, 1, username);
continue;
}
}
if(idx == -1){
client_log("Some unwanted data", username);
string response = "ERROR 103 Header incomplete\n\n";
if(wrap_send(connection_socket, response, username)) exit_thread(connection_socket, 1, username);
buffer[0] = '\0';
break;
}
buffer[idx] = '\0';
string msg(buffer);
client_log(msg, username);
int split = msg.find('\n');
if(split == string::npos) {
string response = "ERROR 103 Header incomplete\n\n";
if(wrap_send(connection_socket, response, username)) exit_thread(connection_socket, 1, username);
refresh_buffer(buffer, currentIndex, idx);
break;
}
string sendto = msg.substr(0,split);
if(!check(sendto, "SEND", 0)) {
string response = "ERROR 103 Header incomplete\n\n";
if(wrap_send(connection_socket, response, username)) exit_thread(connection_socket, 1, username);
refresh_buffer(buffer, currentIndex, idx);
break;
}
sendto = sendto.substr(5);
if(!valid_username(sendto) && sendto != "ALL") {
string response = "ERROR 103 Header incomplete\n\n";
if(wrap_send(connection_socket, response, username)) exit_thread(connection_socket, 1, username);
refresh_buffer(buffer, currentIndex, idx);
break;
}
string con_len = msg.substr(split+1);
if(!check(con_len, "Content-length:", 0)) {
string response = "ERROR 103 Header incomplete\n\n";
if(wrap_send(connection_socket, response, username)) exit_thread(connection_socket, 1, username);
refresh_buffer(buffer, currentIndex, idx);
break;
}
con_len = con_len.substr(16,con_len.size()-17);
int len = -1;
try {
len = stoi(con_len);
if(len <= 0 || len > CONTENT_SIZE) throw -1;
}
catch(...) {
len = 0;
}
if(!len){
string response = "ERROR 103 Header incomplete\n\n";
if(wrap_send(connection_socket, response, username)) exit_thread(connection_socket, 1, username);
refresh_buffer(buffer, currentIndex, idx);
break;
}
refresh_buffer(buffer, currentIndex, idx);
// read len characters from buffer+socket.
char chat[len+1];
int chatIndex = 0;
while(chatIndex<currentIndex && chatIndex<len) {
chat[chatIndex] = buffer[chatIndex];
chatIndex++;
}
refresh_buffer(buffer, currentIndex, chatIndex-1);
// Uncomment below code for assumption that message is not defragmented and
// to get errors when there is mismatch in actual messsage length and one mentioned in header.
/*
if(chatIndex < len) {
// ERROR 103 because message length is less than what is mentioned in header.
string response = "ERROR 103 Header incomplete\n\n";
if(wrap_send(connection_socket, response, username)) exit_thread(connection_socket, 1, username);
break;
}
if(currentIndex != 0) {
// ERROR 103 because message length is more than what is mentioned in header.
string response = "ERROR 103 Header incomplete\n\n";
if(wrap_send(connection_socket, response, username)) exit_thread(connection_socket, 1, username);
break;
}
*/
while(chatIndex != len) {
int length = recv(connection_socket, chat + chatIndex, len-chatIndex, 0);
if(length <= 0){
error("Reading failed", 0);
exit_thread(connection_socket, 1, username);
}
chatIndex += length;
}
chat[chatIndex] = '\0';
string sendmsg(chat);
client_log(sendmsg, username);
pthread_mutex_lock(&mutex_lock);
bool avail_user = mp[2][sendto] == 2;
pthread_mutex_unlock(&mutex_lock);
if(!avail_user || sendto==username) {
string response = "ERROR 102 Unable to send\n\n";
if(wrap_send(connection_socket, response, username)) exit_thread(connection_socket, 1, username);
continue;
}
// forward message
string response = "FORWARD " + username + "\nContent-length: " + to_string(sendmsg.size()) + "\n\n" + sendmsg;
vector<pair<string,int>> list;
pthread_mutex_lock(&mutex_lock);
if(sendto == "ALL") {
for(auto x:mp[2]) {
if(x.first!="ALL" && x.first!=username && x.second == 2){
list.push_back({x.first,mp[0][x.first]});
}
}
int num = list.size();
if(num==0) {
inform_sender(connection_socket, username, sendto, 0);
}
int flag = 0;
pthread_t tid[num];
struct broadcast_data th_data[num];
for(int i=0;i<num;i++) {
th_data[i].connection_socket = connection_socket;
th_data[i].response = response;
th_data[i].recp = list[i].first;
th_data[i].recv_socket = list[i].second;
th_data[i].sender = username;
pthread_create(&tid[i], NULL, broadcast, (void*)&th_data[i]);
}
for(int i=0;i<num;i++) {
pthread_join(tid[i], NULL);
flag=max(flag,th_data[i].status);
}
inform_sender(connection_socket, username, sendto, flag);
}
else {
int status = forward(connection_socket, sendto, response, username, mp[0][sendto]);
inform_sender(connection_socket, username, sendto, status);
}
pthread_mutex_unlock(&mutex_lock);
}
exit_thread(connection_socket, 1, username);
pthread_exit(NULL);
}
int main() {
if (pthread_mutex_init(&mutex_lock, NULL) != 0) {
error("Mutex not created");
}
mp[2]["ALL"] = 2;
int server_fd;
struct sockaddr_in server;
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if(server_fd == 0){
error("Socket creation failed");
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(PORT);
int reuse=1;
int set_res = setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &reuse, sizeof(int));
if(set_res < 0){
error("reuse failed");
}
int bind_res = bind(server_fd, (struct sockaddr *)&server, sizeof(server));
if(bind_res < 0){
error("Bind failed");
}
int listen_res = listen(server_fd, BACKLOG);
if(listen_res < 0){
error("Listen failed");
}
cout<<"Server started\n\n---------------------\n";
while(true) {
int server_size = sizeof(sockaddr_in);
int connection_socket = accept(server_fd, (struct sockaddr *)&server, (socklen_t *)&server_size);
if(connection_socket < 0){
error("Connection Failed");
}
pthread_t th;
int *th_arg = &connection_socket;
pthread_create(&th, NULL, handle_connection, th_arg);
pthread_detach(th);
}
pthread_exit(NULL);
pthread_mutex_destroy(&mutex_lock);
return 0;
}