-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
495 lines (361 loc) · 11.7 KB
/
client.c
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
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include "message_handler.h"
#include "declarations.h"
#include "conference_handler.h"
#include "communication_handler.h"
peer_struct server;
fd_set read_fds;
fd_set write_fds;
fd_set except_fds;
char client_name[256];
char password[256];
/* DISCALIMER:
* You should be careful when using this function in multithread program.
* Ensure that server is thread-safe. */
void shutdown_client(int code){
delete_peer(&server);
printf("shutdown client client.\n");
exit(code);
}
/*MAGIC CONTROL
DESCRIPTION
The sigaction() system call is used to change the action taken by a
process on receipt of a specific signal.
signum specifies the signal and can be any valid signal except
SIGKILL and SIGSTOP.
If act is non-NULL, the new action for signal signum is installed
from act. If oldact is non-NULL, the previous action is saved in
oldact.
The sigaction structure is defined as something like:
struct sigaction {
void (*sa_handler)(int);
void (*sa_sigaction)(int, siginfo_t *, void *);
sigset_t sa_mask;
int sa_flags;
void (*sa_restorer)(void);
};
*/
void handle_signal_action(int sig_number){
if (sig_number == SIGINT) {
shutdown_client(EXIT_SUCCESS);
}
else if (sig_number == SIGPIPE) {
shutdown_client(EXIT_SUCCESS);
}
}
int setup_signals(){
struct sigaction sa;
sa.sa_handler = handle_signal_action;
if (sigaction(SIGPIPE, &sa, 0) != 0) {
perror("sigaction()");
return -1;
}
else if (sigaction(SIGINT, &sa, 0) != 0) {
perror("sigaction()");
return -1;
}
return 0;
}
//Function to obtain client name
int obtain_client_name(int argc, char **argv, char *client_name){
strcpy(client_name, argv[1]);
return 0;
}
//Function to obtain client password
int obtain_client_password(int argc, char **argv, char *password){
strcpy(password, argv[2]);
return 0;
}
// Function to connect client to server (Referred from Beej's guide)
int connect_server(peer_struct *server, char *client_id){
FILE *client_name_temp_store_file= fopen("client_name_temp.txt", "w");
fprintf(client_name_temp_store_file, "%s", client_id);
fclose(client_name_temp_store_file);
// create socket
server->socket = socket(AF_INET, SOCK_STREAM, 0);
if (server->socket < 0) {
perror("socket()");
return -1;
}
// set up address
struct sockaddr_in server_addr, client_addr;
int len = sizeof(client_addr);
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(STATION_SERVER_ADDRESS_IPV4);
server_addr.sin_port = htons(LISTENER_PORT);
server->address = server_addr;
if (connect(server->socket, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) != 0) {
perror("connect()");
return -1;
}
printf("Connected to %s:%d.\n", STATION_SERVER_ADDRESS_IPV4, LISTENER_PORT);
return 0;
}
//Function to extract message provided by client
void get_msg_field_by_delimiter(char *message, int field_no, char *return_str, char delimiter){
//field_no 1: message_type UNLESS default message??
//field_no 2: session_id (for CREATE/JOIN/LEAVE sessions)
int str_length = strlen(message);
int semi_counter = 0;
int buff_char_count = 0;
char curr_char;
for(int i = 0; i < str_length; i++){
curr_char = message[i];
if(curr_char == delimiter){
semi_counter++;
if(semi_counter == field_no){
return;
}else{
memset(return_str, 0, strlen(return_str));
buff_char_count = 0;
}
}else{
return_str[buff_char_count] = message[i];
buff_char_count++;
}
if (i == str_length-1){
return;
}
}
memset(return_str, 0, sizeof(return_str));
}
// Helper function to help concatanate strings as per protocol
void new_concat_str(char *str1, char* str2, char* output_str){
char *txt = ":";
strcpy(output_str, str1);
strcat(output_str, txt);
strcat(output_str, str2);
strcat(output_str, txt);
}
//Function to obtain message through STDIN
//Through this function we classify the packet requests into:
/*
1. /create
2. /join
3. /leave
4. /swap
5. /listsessions
6. /listclients
7. /swap
8. /current
9. /exit
*/
//NOTE: logout and login are taken care directly through server
int handle_read_from_stdin(peer_struct *server, char *client_name){
char read_buffer[DATA_SIZE_MAX]; // buffer for stdin
if (read_from_stdin(read_buffer, DATA_SIZE_MAX) != 0)
return -1;
// Create new message and enqueue it.
message_data new_message;
//
char msg_type[DATA_SIZE_MAX];
memset(msg_type, 0, sizeof(msg_type)); //THIS IS IMPORTANT!!!
char delimiter;
delimiter = ' ';
get_msg_field_by_delimiter(read_buffer,1,msg_type,delimiter);
char outbound_msg[DATA_SIZE_MAX];
memset(outbound_msg, 0, sizeof(outbound_msg));
if (strcmp(msg_type,"/create") == 0){
delimiter = ' ';
char session_name[DATA_SIZE_MAX];
memset(session_name,0,sizeof(session_name));
get_msg_field_by_delimiter(read_buffer,2,session_name,delimiter);
new_concat_str(msg_type, session_name, outbound_msg);
}
else if (strcmp(msg_type,"/join") == 0){
delimiter = ' ';
char session_name[DATA_SIZE_MAX];
memset(session_name,0,sizeof(session_name));
get_msg_field_by_delimiter(read_buffer,2,session_name,delimiter);
new_concat_str(msg_type, session_name, outbound_msg);
}
else if (strcmp(msg_type, "/leave") == 0){
delimiter = ' ';
char session_name[DATA_SIZE_MAX];
memset(session_name,0,sizeof(session_name));
get_msg_field_by_delimiter(read_buffer,2,session_name,delimiter);
new_concat_str(msg_type, session_name, outbound_msg);
}else if(strcmp(msg_type, "/listsessions") == 0){
list_sessions();
return 0;
}
else if(strcmp(msg_type, "/listclients") == 0){
delimiter = ' ';
char session_name[DATA_SIZE_MAX];
memset(session_name,0,sizeof(session_name));
get_msg_field_by_delimiter(read_buffer,2,session_name,delimiter);
list_session_clients(session_name);
return 0;
}
else if(strcmp(msg_type, "/swap") == 0){
delimiter = ' ';
char session_name[DATA_SIZE_MAX];
memset(session_name,0,sizeof(session_name));
get_msg_field_by_delimiter(read_buffer,2,session_name,delimiter);
new_concat_str(msg_type, session_name, outbound_msg);
}
else if(strcmp(msg_type, "/current") == 0){
delimiter = ' ';
char session_name[DATA_SIZE_MAX];
memset(session_name,0,sizeof(session_name));
strcat(msg_type, ":");
strcpy(outbound_msg, msg_type);
}
else if(strcmp(msg_type, "/exit") == 0){
strcat(msg_type, ":");
strcpy(outbound_msg, msg_type);
prepare_message(client_name, outbound_msg, &new_message);
peer_add_to_send(server, &new_message);
shutdown_client(EXIT_SUCCESS);
}
else{
prepare_message(client_name, read_buffer, &new_message);
if (peer_add_to_send(server, &new_message) != 0) {
return 0;
}
print_message(&new_message);
return 0;
}
prepare_message(client_name, outbound_msg, &new_message);
print_message(&new_message);
if (peer_add_to_send(server, &new_message) != 0) {
return 0;
}
return 0;
}
int handle_received_message(message_data *message){
print_message(message);
return 0;
}
// Function to check authentacity of the user
int check_user(char client_id[BUF_SIZE], char password[BUF_SIZE]){
char buffer[BUF_SIZE];
char *msg;
int ret;
char * line = NULL;
size_t len = 0;
ssize_t read;
int line_counter = 0;
//open the file to read
FILE *fp = fopen("client_database.txt", "r");
if (fp == NULL)
exit(1);
while ((read = getline(&line, &len, fp)) != -1) {
char* packet_str = line;
char* line_user = (char*)malloc(100 * sizeof(char));
char* line_pwd = (char*)malloc(100 * sizeof(char));
//Separate line into strings ...
int start = 0;
//Read value till first colon into total_frag
int index = 0;
while(packet_str[start + index] != ':'){
line_user[index] = packet_str[start+index];
index += 1;
}
start += index;
start += 1; //Skip the ';' encountered when starting next read!
printf(line_user);
printf("\n");
//Read value till second colon into frag_no
index = 0;
while(packet_str[start + index] != ':'){
line_pwd[index] = packet_str[start+index];
index += 1;
}
start += index;
start += 1; //Skip the ';' encountered when starting next read!
///////////Compare!!!!/////////
int cmp_user = strcmp(client_id, line_user);
int cmp_pwd = strcmp(password, line_pwd);
if (cmp_user == 0){
if (cmp_pwd == 0){
return 0; //Found username and password!
}
else{
return 1; //Password doesn't match :(!
}
}
line_counter++;
}
if (line)
free(line);
return 2; //No matching entry in DB!
}
//MAIN starts
int main(int argc, char **argv){
if (setup_signals() > 0)
exit(EXIT_FAILURE);
if(argc!=3){
printf("%s\n", "usage: client <userid> <password>");
exit(EXIT_FAILURE);
}
obtain_client_name(argc, argv, client_name);
obtain_client_password(argc, argv, password);
if(check_user(client_name, password) == 0){
//
}else{
printf("%s\n", "Incorrect Credentials");
exit(1);
}
create_peer(&server);
if (connect_server(&server, client_name) != 0)
shutdown_client(EXIT_FAILURE);
/* Set nonblock for stdin. */
int flag = fcntl(STDIN_FILENO, F_GETFL, 0);
flag |= O_NONBLOCK;
fcntl(STDIN_FILENO, F_SETFL, flag);
// server socket always will be greater then STDIN_FILENO
int maxfd = server.socket;
printf("Waiting for server message or stdin input. Please create and join a session for text conferencing\n");
for(;;) {
// Select() updates fd_set's, so we need to build fd_set's before each select()call.
FD_ZERO(&read_fds);
FD_SET(STDIN_FILENO, &read_fds);
FD_SET(server.socket, &read_fds);
FD_ZERO(&write_fds);
// smth to send, set up write_fd for server socket
if (server.send_buffer.current > 0)
FD_SET(server.socket, &write_fds);
FD_ZERO(&except_fds);
FD_SET(STDIN_FILENO, &except_fds);
FD_SET(server.socket, &except_fds);
int activity = select(maxfd + 1, &read_fds, &write_fds, &except_fds, NULL);
if(activity != -1 && activity != 0){
/* All fd_set's should be checked. */
if (FD_ISSET(STDIN_FILENO, &read_fds)) {
if (handle_read_from_stdin(&server, client_name) != 0)
shutdown_client(EXIT_FAILURE);
}
if (FD_ISSET(STDIN_FILENO, &except_fds)) {
shutdown_client(EXIT_FAILURE);
}
if (FD_ISSET(server.socket, &read_fds)) {
if (receive_from_peer(&server, &handle_received_message) != 0)
shutdown_client(EXIT_FAILURE);
}
if (FD_ISSET(server.socket, &write_fds)) {
if (send_to_peer(&server) != 0)
shutdown_client(EXIT_FAILURE);
}
if (FD_ISSET(server.socket, &except_fds)) {
shutdown_client(EXIT_FAILURE);
}
}else if(activity == -1){
perror("select()");
shutdown_client(EXIT_FAILURE);
}else{
printf("select() returns 0.\n");
shutdown_client(EXIT_FAILURE);
}
}
return 0;
}