-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
417 lines (359 loc) · 10.1 KB
/
client.cpp
File metadata and controls
417 lines (359 loc) · 10.1 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
#include <bits/stdc++.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
using namespace std;
#define PORT 8080
#define BUFFER_SIZE 1024
#define CONTENT_SIZE 100000
struct thread_data {
string user;
int sock;
};
void error(string err,int ex = 1) {
cout<<"\t ERR: \t";
cout<<err<<"\n";
if(ex) exit(-1);
}
void invalidInput(string hint = "") {
cout<<"----> Invalid input format. Try again!!!\n";
if(hint != "") {
cout<<"----> "<<hint<<"\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;
}
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) {
close(sock);
pthread_exit(NULL);
}
void refresh_buffer(char buffer[], int ¤tIndex,int idx) {
memmove(buffer, buffer + idx + 1, currentIndex - idx - 1);
currentIndex = currentIndex - idx - 1;
buffer[currentIndex] = '\0';
}
int wrap_send(int connection_socket, string response) {
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){
error("Reading failed",0);
return -2;
}
st = currentIndex;
currentIndex += read_res;
}
return -1;
}
int create_connection(struct sockaddr_in server) {
int sock;
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock == 0) {
error("Socket creation failed");
}
int connect_res = connect(sock, (struct sockaddr *)&server, sizeof(server));
if(connect_res < 0) {
error("Connection failed");
}
return sock;
}
int recv_connection(string user, struct sockaddr_in server) {
int sock = create_connection(server);
string reg = "REGISTER TORECV " + user + "\n\n";
if(wrap_send(sock, reg)) {
close(sock);
error("Server Disconnected");
}
char buffer[BUFFER_SIZE+1];
int currentIndex = 0;
int idx = wrap_read(sock, buffer, currentIndex);
if(idx == -2) {
close(sock);
error("Server Disconnected... RECV Registration failed");
}
if(idx == -1) {
close(sock);
error("Server behaviour undetermined... RECV Registration failed");
}
buffer[idx] = '\0';
string resp(buffer);
if(resp == "REGISTERED TORECV " + user + "\n") {
cout<<resp;
return sock;
} else if(resp == "ERROR 100 Malformed username\n") {
cout<<"** Server: "<<resp;
close(sock);
error("Invalid Username... RECV Registration failed");
} else {
close(sock);
error("Server behaviour undetermined... RECV Registration failed");
}
return sock;
}
int send_connection(string user, struct sockaddr_in server) {
int sock = create_connection(server);
string reg = "REGISTER TOSEND " + user + "\n\n";
if(wrap_send(sock, reg)) {
close(sock);
error("Server Disconnected");
}
char buffer[BUFFER_SIZE+1];
int currentIndex = 0;
int idx = wrap_read(sock, buffer, currentIndex);
if(idx == -2) {
close(sock);
error("Server Disconnected... SEND Registration failed");
}
if(idx == -1) {
close(sock);
error("Server behaviour undetermined... SEND Registration failed");
}
buffer[idx] = '\0';
string resp(buffer);
if(resp == "REGISTERED TOSEND " + user + "\n") {
cout<<resp;
return sock;
} else if(resp == "ERROR 100 Malformed username \n") {
cout<<"** Server: "<<resp;
close(sock);
error("Invalid Username... SEND Registration failed");
} else {
close(sock);
error("Server behaviour undetermined... SEND Registration failed");
}
return sock;
}
void * thf_send(void *td) {
struct thread_data data = *((struct thread_data *)td);
string user = data.user;
int sock = data.sock;
while(true) {
string str;
getline(cin,str);
if(str[0] != '@') {
invalidInput();
continue;
}
int user_idxe = str.find(' ');
if(user_idxe == string::npos) {
invalidInput();
continue;
}
string to_send = str.substr(1,user_idxe-1);
if(to_send==user || (!valid_username(to_send) && to_send!="ALL")) {
invalidInput("Invalid username");
continue;
}
string content = str.substr(user_idxe+1);
int content_len = content.size();
if(content_len == 0) {
invalidInput("Content is missing");
continue;
}
string msg_send = "SEND " + to_send +
"\nContent-length: " + to_string(content_len) + "\n\n" + content;
if(wrap_send(sock, msg_send)) break;
char buffer[BUFFER_SIZE+1];
int currentIndex = 0;
int idx = wrap_read(sock, buffer, currentIndex);
if(idx == -2) break;
if(idx == -1) break;
buffer[idx] = '\0';
string resp(buffer);
if(resp == "SEND " + to_send + "\n") {
cout<<"** Server: "<<resp+"("+str+")\n";
} else if(resp == "ERROR 102 Unable to send\n") {
cout<<"** Server: "<<resp+"("+str+")\n";
} else if(resp == "ERROR 103 Header incomplete\n") {
cout<<"** Server: "<<resp+"("+str+")\n";
} else {
break;
}
}
close(sock);
cout<<"Server Disconnected or Behaviour Undetermined... SEND Connection closed\n";
pthread_exit(NULL);
}
void * thf_recv(void *td) {
struct thread_data data = *((struct thread_data *)td);
string user = data.user;
int sock = data.sock;
char buffer[BUFFER_SIZE+1];
int currentIndex = 0;
while(true) {
// read header
int idx = wrap_read(sock, buffer, currentIndex);
if(idx == -2) break;
if(idx == -1) break;
buffer[idx] = '\0';
string msg(buffer);
int split = msg.find('\n');
if(split == string::npos) {
string response = "ERROR 103 Header incomplete\n\n";
if(wrap_send(sock, response)) break;
refresh_buffer(buffer, currentIndex, idx);
break;
}
string sendto = msg.substr(0,split);
if(!check(sendto, "FORWARD", 0)) {
string response = "ERROR 103 Header incomplete\n\n";
if(wrap_send(sock, response)) break;
refresh_buffer(buffer, currentIndex, idx);
break;
}
sendto = sendto.substr(8);
if(!valid_username(sendto)) {
string response = "ERROR 103 Header incomplete\n\n";
if(wrap_send(sock, response)) break;
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(sock, response)) break;
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(sock, response)) break;
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(sock, response)) exit_thread(sock);
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(sock, response)) exit_thread(sock);
break;
}*/
while(chatIndex != len) {
int length = recv(sock, chat + chatIndex, len-chatIndex, 0);
if(length <= 0){
error("Reading failed", 0);
break;
}
chatIndex += length;
}
chat[chatIndex] = '\0';
string sendmsg(chat);
cout<<"\t\t\t @" + sendto + " " + sendmsg + "\n";
string resp = "RECEIVED " + sendto + "\n\n";
if(wrap_send(sock, resp)) break;
}
close(sock);
cout<<"Server Disconnected or Behaviour Undetermined... RECV Connection closed\n";
pthread_exit(NULL);
}
int main(int argc, char **argv) {
string user = "default";
string IP = "127.0.0.1";
if(argc != 3) {
error("Wrong command line arguements");
}
user = argv[1];
IP = argv[2];
const char *ip = IP.c_str();
if(!valid_username(user)) {
error("Invalid username!!!");
}
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(PORT);
int inet_pton_res = inet_pton(AF_INET, ip, &server.sin_addr);
if(inet_pton_res <= 0) {
error("Invalid address");
}
int recv_socket = recv_connection(user, server);
int send_socket = send_connection(user, server);
pthread_t th_send,th_recv;
struct thread_data td_send,td_recv;
td_send.user = user;
td_recv.user = user;
td_send.sock = send_socket;
td_recv.sock = recv_socket;
pthread_create(&th_send, NULL, thf_send, (void*)&td_send);
pthread_detach(th_send);
pthread_create(&th_recv, NULL, thf_recv, (void*)&td_recv);
pthread_detach(th_recv);
pthread_exit(NULL);
return 0;
}