-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcli_func.c
643 lines (584 loc) · 19.7 KB
/
cli_func.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/*------------------------------------------------------
*
* cli_func.c
*
* this file contains many functions that used in lyrebird client side
*
* Project : LyreBird
* Name : Chong Guo
* Student ID : 301295753
* SFU username : armourg
* Lecture section : D1
* Instructor : Brain G.Booth
* TA : Scott Kristjanson
*
* Created by Armour on 25/09/2015
* Copyright (c) 2015 Armour. All rights reserved.
*
*------------------------------------------------------
*/
#include "cli_func.h"
#include "time.h"
#include "memwatch.h"
/*
* Function: Get_time
* -------------------
* This function is used to get current time in specify format
*
* Parameters:
* no parameters
*
* Returns:
* void
*/
void get_time(void) {
time_t raw_time; /* Used to generate output time */
struct tm *tmp_time; /* Used to generate output time */
time(&raw_time);
tmp_time = localtime(&raw_time);
strftime(out_time, TIME_MAXLENGTH, "%a %b %d %H:%M:%S %Y", tmp_time); /* Format time */
if (out_time[8] == '0')
out_time[8] = ' ';
}
/*
* Function: Send_socket_msg
* -------------------
* This function is used to send a message through socket with its length send first
*
* Parameters:
* socket: the socket file descriptor
* msg: the message that need to send
*
* Returns:
* void
*/
void send_socket_msg(int socket, char *msg) {
msg_len = htonl(strlen(msg));
send(socket, &msg_len, sizeof(uint32_t), 0); /* Send the message length first */
send(socket, msg, ntohl(msg_len), 0); /* Send the message content */
}
/*
* Function: Recv_socket_msg
* -------------------
* This function is used to receive a message through socket with its length at first
*
* Parameters:
* socket: the socket file descriptor
* msg: the message that need to receive
*
* Returns:
* void
*/
void recv_socket_msg(int socket, char *msg) {
recv(socket, &msg_len, sizeof(uint32_t), 0); /* Recv the message length first */
recv(socket, msg, ntohl(msg_len), 0); /* Recv the message content */
msg[ntohl(msg_len)] = '\0';
}
/*
* Function: Write_pipe_msg
* -------------------
* This function is used to write a message through pipe with its length write first
*
* Parameters:
* pipe: the pipe file descriptor
* msg: the message that need to send
*
* Returns:
* void
*/
void write_pipe_msg(int pipe, char *msg) {
msg_len = htonl(strlen(msg));
write(pipe, &msg_len, sizeof(uint32_t)); /* Write the message length first */
write(pipe, msg, ntohl(msg_len)); /* Write the message content */
}
/*
* Function: Read_pipe_msg
* -------------------
* This function is used to read a message through pipe with its length at first
*
* Parameters:
* pipe: the pipe file descriptor
* msg: the message that need to receive
*
* Returns:
* void
*/
int read_pipe_msg(int pipe, char *msg) {
int ret;
if ((ret = read(pipe, &msg_len, sizeof(uint32_t))) == 0) return 0; /* Read the message length first */
if ((ret = read(pipe, msg, ntohl(msg_len))) == 0) return 0; /* Read the message content */
msg[ntohl(msg_len)] = '\0';
return ret;
}
/*
* Function: Init
* -------------------
* This function is used to init some variables,
* like malloc timestamp string, encrypt text string, etc.
*
* Parameters:
* no parameters
*
* Returns:
* void
*/
void init(void) {
signal(SIGINT, signal_handler);
signal(SIGQUIT, signal_handler);
signal(SIGHUP, signal_handler);
addr_len = sizeof(struct sockaddr_in);
out_time = (char *)malloc(sizeof(char) * TIME_MAXLENGTH);
enc_txt = (char *)malloc(sizeof(char) * FILE_MAXLENGTH);
dec_txt = (char *)malloc(sizeof(char) * FILE_MAXLENGTH);
}
/*
* Function: Check_par
* -------------------
* This function is used to check the parameters in command
*
* Parameters:
* argc: the count of arguments
* argv: the command arguments
*
* Returns:
* void
*/
void check_par(int argc, char *argv[]) {
if (argc != 3) { /* Check if the arguments number is right or not */
get_time();
printf("[%s] (Process ID #%d) Arguments number is not right! Usage: %s <ip address> <port number>\n", out_time, getpid(), argv[0]);
clean_up(CLEAN_TO_TIME);
exit(EXIT_FAILURE);
}
}
/*
* Function: Get_port_number
* -------------------
* This function is used to get the port number
*
* Parameters:
* argv: the command arguments
*
* Returns:
* void
*/
void get_port_number(char *argv[]) {
if ((port_number = atoi(argv[2])) == 0) { /* Get the server port number */
get_time();
printf("[%s] (Process ID #%d) ERROR: Can not use this port number for TCP connection!\n", out_time, getpid());
clean_up(CLEAN_TO_TIME);
exit(EXIT_FAILURE);
}
}
/*
* Function: Create_socket
* -------------------
* This function is used to create a socket in client side
*
* Parameters:
* no parameters
*
* Returns:
* void
*/
void create_socket(void) {
int opt = TRUE; /* Used when set socket options */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { /* Create socket to connect server */
get_time();
printf("[%s] (Process ID #%d) ERROR: Create socket failed!\n", out_time, getpid());
clean_up(CLEAN_TO_TIME);
exit(EXIT_FAILURE);
}
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt)) < 0) { /* Set socket option to reuseable address (not necessary but good) */
get_time();
printf("[%s] (Process ID #%d) ERROR: Can not set socket option!\n", out_time, getpid());
clean_up(CLEAN_TO_SOCKET);
exit(EXIT_FAILURE);
}
}
/*
* Function: Connect_socket
* -------------------
* This function is used to connect to server side
*
* Parameters:
* argv: the command arguments
*
* Returns:
* void
*/
void connect_socket(char *argv[]) {
memset(&serv_addr, 0, addr_len); /* Initialize server address config */
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port_number);
if (inet_pton(AF_INET, argv[1], &serv_addr.sin_addr) <= 0) { /* Check IP address */
get_time();
printf("[%s] (Process ID #%d) ERROR: Server host in inet_pton function is not a valid IP address!\n", out_time, getpid());
clean_up(CLEAN_TO_SOCKET);
exit(EXIT_FAILURE);
}
if (connect(sockfd, (struct sockaddr *)&serv_addr, addr_len) < 0) {
get_time();
printf("[%s] (Process ID #%d) ERROR: Connect to server %s:%d failed!\n", out_time, getpid(), argv[1], port_number);
clean_up(CLEAN_TO_SOCKET);
exit(EXIT_FAILURE);
}
}
/*
* Function: Print_connect_info
* -------------------
* This function is used to print the info of connection
*
* Parameters:
* argv: the command arguments
*
* Returns:
* void
*/
void print_connect_info(char *argv[]) {
get_time();
printf("[%s] lyrebird client: PID %d connected to server %s on port %d.\n", out_time, getpid(), argv[1], port_number);
}
/*
* Function: Send_connect_msg
* -------------------
* This function is used to send the connection message to server
*
* Parameters:
* no parameters
*
* Returns:
* void
*/
void send_connect_msg(void) {
strcpy(send_mark, CONNECT_MSG); /* Send connect message to server */
send_socket_msg(sockfd, send_mark);
}
/*
* Function: Init_pipe
* -------------------
* This function is used to initialize the max number of pipe descriptor
*
* Parameters:
* no parameters
*
* Returns:
* void
*/
void init_pipe(void) {
int i;
process_number_limit = PROCESS_MAX_NUMBER; /* Max number of processes for this machine */
parent_to_child = (int *)malloc(sizeof(int) * process_number_limit * 2);
if (parent_to_child == NULL) { /* Create parent to child pipes and check whether failed or not */
get_time();
printf("[%s] (Process ID #%d) ERROR: Malloc parent_to_child failed!\n", out_time, getpid());
clean_up(CLEAN_TO_SOCKET);
exit(EXIT_FAILURE);
}
child_to_parent = (int *)malloc(sizeof(int) * process_number_limit * 2);
if (child_to_parent == NULL) { /* Create child to parent pipes and check whether failed or not */
get_time();
printf("[%s] (Process ID #%d) ERROR: Malloc child_to_parent failed!\n", out_time, getpid());
clean_up(CLEAN_TO_SOCKET);
free(parent_to_child);
exit(EXIT_FAILURE);
}
pid_array = (int *)malloc(sizeof(int) * process_number_limit);
if (pid_array == NULL) { /* Create child pid array and check whether failed or not */
get_time();
printf("[%s] (Process ID #%d) ERROR: Malloc pid_array failed!\n", out_time, getpid());
clean_up(CLEAN_TO_SOCKET);
free(parent_to_child);
free(child_to_parent);
exit(EXIT_FAILURE);
}
is_free = (int *)malloc(sizeof(int) * process_number_limit);
if (is_free == NULL) { /* Create child pid array and check whether failed or not */
get_time();
printf("[%s] (Process ID #%d) ERROR: Malloc is_free failed!\n", out_time, getpid());
clean_up(CLEAN_TO_SOCKET);
free(parent_to_child);
free(child_to_parent);
free(pid_array);
exit(EXIT_FAILURE);
}
memset(pid_array, 0, sizeof(int) * process_number_limit);
memset(is_free, 0, sizeof(int) * process_number_limit);
for (i = 0; i < process_number_limit; ++i) {
if (pipe(parent_to_child + i * 2)) { /* Create pipe from parent to child */
get_time();
printf("[%s] (Process ID #%d) ERROR: Create parent to child pipe number %d failed!\n", out_time, getpid(), i);
clean_up(CLEAN_TO_SOCKET);
free(parent_to_child);
free(child_to_parent);
free(pid_array);
free(is_free);
exit(EXIT_FAILURE); /* If create pipe failed */
}
if (pipe(child_to_parent + i * 2)) { /* Create pipe from child to parent */
get_time();
printf("[%s] (Process ID #%d) ERROR: Create child to parent pipe number %d failed!\n", out_time, getpid(), i);
clean_up(CLEAN_TO_SOCKET);
free(parent_to_child);
free(child_to_parent);
free(pid_array);
free(is_free);
exit(EXIT_FAILURE); /* If create pipe failed */
}
}
}
/*
* Function: Init_select
* -------------------
* This function is used to prepare select function for pipe in different processes
*
* Parameters:
* no parameters
*
* Returns:
* void
*/
void init_select(void) {
int i;
int max = 0;
FD_ZERO(&rfds); /* First initialize set to empty */
for (i = 0; i < process_number_limit; ++i) {
FD_SET(child_to_parent[i * 2], &rfds); /* Add file descriptor to set */
if (child_to_parent[i * 2] > max)
max = child_to_parent[i * 2]; /* Calculate max file descriptor */
}
max_descriptor = max;
}
/*
* Function: Init_select_with_sockfd
* -------------------
* This function is used to prepare select function for pipe in different processes (with socket)
*
* Parameters:
* no parameters
*
* Returns:
* void
*/
void init_select_with_socket(int sockfd) {
int i;
int max = 0;
FD_ZERO(&rfds); /* First initialize set to empty */
for (i = 0; i < process_number_limit; ++i) {
FD_SET(child_to_parent[i * 2], &rfds); /* Add file descriptor to set */
if (child_to_parent[i * 2] > max)
max = child_to_parent[i * 2]; /* Calculate max file descriptor */
}
FD_SET(sockfd, &rfds); /* Add file descriptor to set */
if (sockfd > max)
max = sockfd;
max_descriptor = max;
}
/*
* Function: Close_ptc_pipes_except
* -------------------
* This function is used to close the pipes from parent to child except process p's pipe
*
* Parameters:
* p: the number of the process which we do not want to close its pipe
*
* Returns:
* void
*/
void close_ptc_pipes_except(int p) {
int i;
for (i = 0; i < process_number_limit * 2; ++i) { /* Close each pipe from parent to child except process p */
if (i/2 != p)
close(parent_to_child[i]);
}
}
/*
* Function: Close_ctp_pipes_except
* -------------------
* This function is used to close the pipes from child to parent except process p's pipe
*
* Parameters:
* p: the number of the process which we do not want to close its pipe
*
* Returns:
* void
*/
void close_ctp_pipes_except(int p) {
int i;
for (i = 0; i < process_number_limit * 2; ++i) { /* Close each pipe from child to parent except process p */
if (i/2 != p)
close(child_to_parent[i]);
}
}
/*
* Function: Close_all_ptc_pipes
* -------------------
* This function is used to close all the pipes from parent to child process
*
* Parameters:
* no parameters
*
* Returns:
* void
*/
void close_all_ptc_pipes(void) {
int i;
for (i = 0; i < process_number_limit * 2; ++i) { /* Close all the pipes from parent to child */
close(parent_to_child[i]);
}
}
/*
* Function: Close_ctp_pipe_with_pid
* -------------------
* This function is used to close pipe that belong to child process with specific pid
*
* Parameters:
* pid: the pid number that we want to match
*
* Returns:
* void
*/
void close_ctp_pipe_with_pid(int pid) {
int i;
for (i = 0; i < process_number_limit; i++) {
if (pid_array[i] == pid) { /* Close pipes with specific pid */
close(child_to_parent[2 * i]);
break;
}
}
}
/*
* Function: Read_rmng_msg
* -------------------
* This function is used to read all remaining messages in each child process
*
* Parameters:
* no parameters
*
* Returns:
* void
*/
void read_rmng_msg(void) {
int i;
for (i = 0; i < process_number_limit; i++) { /* Read all remaining message in child processes */
while (read_pipe_msg(child_to_parent[i * 2], read_mark)) {
if (strcmp(read_mark, CHILD_PROCESS_INIT) == 0) { /* If messge is init message */
strcpy(send_mark, DISPATCH_MSG);
send_socket_msg(sockfd, send_mark);
} else if (strcmp(read_mark, CHILD_PROCESS_SUCCESS) == 0) { /* If message is success messge */
is_free[i] = TRUE;
read_pipe_msg(child_to_parent[i * 2], fcfs_file_buf); /* Read successful decrypted file name */
strcpy(send_mark, SUCCESS_MSG);
sprintf(fcfs_pid_buf, "%d", pid_array[i]);
send_socket_msg(sockfd, send_mark); /* Send message to server side about this successful decryption */
send_socket_msg(sockfd, fcfs_file_buf);
send_socket_msg(sockfd, fcfs_pid_buf);
} else if (strcmp(read_mark, CHILD_PROCESS_WARNING) == 0 || strcmp(read_mark, CHILD_PROCESS_FAILURE) == 0) { /* If meet warning or failure */
if (strcmp(read_mark, CHILD_PROCESS_WARNING) == 0) { /* If message is warning message */
is_free[i] = TRUE; /* Set this child process state to FREE */
}
read_pipe_msg(child_to_parent[i * 2], fcfs_err_buf); /* Read error messge from child process */
strcpy(send_mark, FAILURE_MSG);
send_socket_msg(sockfd, send_mark); /* Send error message to server side */
send_socket_msg(sockfd, fcfs_err_buf);
} else { /* If message can not be identify */
get_time();
printf("[%s] (Process ID #%d) ERROR: Wrong message has been read from pipe after select.\n", out_time, getpid());
main_flag = EXIT_FAILURE;
}
}
}
}
/*
* Function: Wait_all_child
* -------------------
* This function is used to wait all child processes quit
*
* Parameters:
* no parameters
*
* Returns:
* void
*/
void wait_all_child(void) {
int i;
for (i = 0; i < process_number_limit; i++) { /* Parent process wait for all child processes before exit */
int state;
pid_t pid = wait(&state); /* Wait until found one child process finished */
close_ctp_pipe_with_pid(pid); /* Close the pipe that uses to read messages from exited child process */
}
}
/*
* Function: Check_client_exit_state
* -------------------
* This function is used to check client's exit state
* and send it to server before exit
*
* Parameters:
* no parameters
*
* Returns:
* void
*/
void check_client_exit_state(void) {
if (main_flag == EXIT_SUCCESS) { /* If client exit with state EXIT_SUCCESS */
strcpy(send_mark, DISCONNECT_SUCC_MSG);
send_socket_msg(sockfd, send_mark);
get_time();
printf("[%s] lyrebird client: PID %d completed its tasks and is exiting successfully.\n", out_time, getpid());
} else { /* If client exit with other state */
strcpy(send_mark, DISCONNECT_FAIL_MSG);
send_socket_msg(sockfd, send_mark);
get_time();
printf("[%s] lyrebird client: PID %d completed its tasks and is exiting successfully.\n", out_time, getpid());
}
}
/*
* Function: Clean_up
* -------------------
* This function is used to free memory and close file pointer before program exit
*
* Parameters:
* no parameters
*
* Returns:
* void
*/
void clean_up(int step) {
if (step >= CLEAN_TO_TIME){
free(enc_txt);
free(dec_txt);
free(out_time);
}
if (step >= CLEAN_TO_SOCKET) {
free(parent_to_child);
free(child_to_parent);
free(pid_array);
free(is_free);
close(sockfd);
}
}
/*
* Function: Signal_handler
* -------------------
* This function is used to handle signal
*
* Parameters:
* sig_num: the number of the signal
*
* Returns:
* void
*/
void signal_handler(int sig_num) {
switch (sig_num) {
case SIGINT:
printf("Please do not interrupt (signal #%d) this process, because this is a VERY IMPORTANT task! :D\n", sig_num);
break;
case SIGQUIT:
printf("Please do not quit (signal #%d) this process, because this is a VERY IMPORTANT task! :D\n", sig_num);
break;
case SIGHUP:
printf("Please do not exit (signal #%d) this process, because this is a VERY IMPORTANT task! :D\n", sig_num);
break;
}
}