-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremoteServer.c
More file actions
792 lines (566 loc) · 16.9 KB
/
remoteServer.c
File metadata and controls
792 lines (566 loc) · 16.9 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
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h> /* for sockaddr_in */
#include <arpa/inet.h> /* for hton * */
#include <signal.h>
#include <sys/wait.h>
#include <sys/time.h> //FD_SET, FD_ISSET, FD_ZERO
#include "remote.h"
#define max_clients 30
//Functions...
void create_TCP_SOCKET(int* server_fd_ret,struct sockaddr_in* server_addr_ret);
void server_function(int msg_size);
void child_function(int msg_size);
void signal_handler(int signum);
char** create_commnads_array(char* command,int length,int* commands_number);
int count_occurances(char* string,int length,char ch);
int valid_command(char* command);
FILE* execute(char* command);
char* create_udp_packet(char* command_result,int last,int command_code);
void close_reading_sockets();
void close_fds(int closed_pipe,int print_stderr);
void end_func();
void timeToStop_func();
void free_workers_array();
void send_stop_msg();
//...
//gloabl variables
int portNumber,numChildren,activeChildren;
pid_t* workers_array; //array with pids of children
pid_t parent;
/*
this variable will be changed to 1 for each child from father
*/
int timeToStop=0;
typedef struct{
char cmd[100];
int port;
int command_code;
}server_worker_msg;
typedef struct {
int socket;
int command_code;
int port;
} connectlist_node;
/* Array of connected sockets so we know who we are talking to */
connectlist_node connection_list[max_clients];
int server_fd;
int pipe_fds[2];
char* server_commands[5]={
"ls",
"cat",
"cut",
"grep",
"tr"
//end
//timeToStop
//end and timeToStop are executed in other way
};
struct sigaction action;
//...
int main(int argc,char *argv[]){
//first input => ./remoteServer
if(argc!=3){ perror("Invalid number of input"); exit(EXIT_FAILURE); }
portNumber = atoi(argv[1]);
numChildren = atoi(argv[2]);
activeChildren = numChildren;
if(numChildren < 1){ perror("At least one child is required"); exit(EXIT_FAILURE); }
//signals handlers....
action.sa_handler = signal_handler;
action.sa_flags = SA_RESTART; //<-- restart
sigemptyset (&action.sa_mask);
sigaddset(&action.sa_mask,SIGUSR1);
sigaddset(&action.sa_mask,SIGUSR2);
sigaddset(&action.sa_mask,SIGPIPE);
sigaction(SIGUSR1, &action, NULL);//command end
sigaction(SIGUSR2, &action, NULL);//command timeToStop
sigaction(SIGPIPE, &action, NULL);
//...
//create pipe...
if (pipe(pipe_fds) == -1){
perror("pipe call"); exit(EXIT_FAILURE);
}
int msg_size =sizeof(server_worker_msg);
pid_t temp;
/* allocate space for worikers_array*/
workers_array = (pid_t*)malloc(numChildren * sizeof(pid_t));
parent = getpid();
/* create numChildren child-process*/
for (int i = 0; i < numChildren; i++){
temp = fork();
if(temp < 0){
perror("Can't create process"); exit(EXIT_FAILURE);
}
/*child*/
else if( temp == 0 ){
child_function(msg_size); //never ends
}
/*parent*/
else{
workers_array[i] = temp;
}
}
//only father is here
server_function(msg_size);
}
/*
@brief create_TCP_SOCKET
return by refernce: server_fd,server_addr
*/
void create_TCP_SOCKET(int* server_fd_ret,struct sockaddr_in* server_addr_ret){
int server_fd;
struct sockaddr_in server_addr;
//create socket...
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket failed"); exit(EXIT_FAILURE);
}
server_addr.sin_family = AF_INET ;
server_addr.sin_addr.s_addr = htonl ( INADDR_ANY ); // receive connections from any address
server_addr.sin_port = htons ( portNumber );
/* to set the socket options- to reuse the given port multiple times */
int reuse=1;
if(setsockopt(server_fd,SOL_SOCKET,SO_REUSEPORT,(const char*)&reuse,sizeof(reuse))<0){
perror("setsockopt(SO_REUSEPORT) failed\n"); exit(EXIT_FAILURE);
}
/* bind the socket to known port */
if(bind(server_fd,(struct sockaddr*)&server_addr,sizeof(server_addr))<0){
perror("Error in binding"); exit(EXIT_FAILURE);
}
/* place the socket in passive mode and make the server ready to accept the requests and also
specify the max no. of connections
*/
if(listen(server_fd,20)<0){
perror("Can't create listener..."); exit(EXIT_FAILURE);
}
*server_fd_ret = server_fd;
*server_addr_ret = server_addr;
}
void server_function(int msg_size){
int new_socket;
struct sockaddr_in server_address,client_address;
int addrlen = sizeof(server_address);
char buffer[1024];
/*
call create_TCP_SOCKET to create a socket, bind it and place it in passive mode
once the call returns call accept on listening socket to accept the incoming requests
*/
create_TCP_SOCKET(&server_fd, &server_address);
int receivePort=-1;
fd_set socks; /* Socket file descriptors we want to wake up for, using select() */
int highsock; /* Highest file descriptor, needed for select() */
int readsocks;
int valread;
//initialize all fds to 0
for (int i = 0; i < max_clients; i++){
connection_list[i].socket = 0;
connection_list[i].command_code = -1;
}
/*main server loop runs forever*/
while(1){
//clear the set
FD_ZERO(&socks);
//add the server_fd to accept new connections
FD_SET(server_fd,&socks);
highsock = server_fd;
/* Loops through all the possible connections and adds those sockets to the fd_set */
for (int i = 0; i < max_clients; i++){
if(connection_list[i].socket!=0)
FD_SET(connection_list[i].socket,&socks);
if(connection_list[i].socket > highsock)
highsock = connection_list[i].socket;
}
/* select() returns the number of sockets that are readable */
readsocks = select(highsock+1, &socks,NULL,NULL,NULL);
if(readsocks < 0){
perror("select"); exit(EXIT_FAILURE);
}
/*read from sockets*/
/* If a client is trying to connect() to our listening
socket, select() will consider that as the socket
being 'readable'. Thus, if the listening socket is
part of the fd_set, we need to accept a new connection. */
if (FD_ISSET(server_fd,&socks)){
if ((new_socket = accept(server_fd, (struct sockaddr *)&client_address,(socklen_t*)&addrlen))<0) {
perror("accept"); exit(EXIT_FAILURE);
}
int accepted=0;
for (int i = 0; i < max_clients; i++){
if(connection_list[i].socket == 0 ){
connection_list[i].socket = new_socket;
accepted = 1;
break;
}
}
if(!accepted){
close(new_socket);
}
}
/* Now check connectlist for available data */
for (int i = 0; i < max_clients; i++){
if (FD_ISSET(connection_list[i].socket,&socks)){
valread=read( connection_list[i].socket , buffer, 1024);
if (valread <= 0){
close(connection_list[i].socket);
connection_list[i].socket = 0;
connection_list[i].command_code = -1;
continue;
}
//get the receivePort from client
char *ptr = strtok(buffer, ":");
if(strcmp(buffer,"receivePort")==0){
ptr = strtok(NULL, ":");
receivePort=atoi(ptr);
connection_list[i].port = receivePort;
continue;
}
if(strlen(buffer) > 100)
continue;
connection_list[i].command_code ++;
//create the message
server_worker_msg msg;
//copy the command
strcpy(msg.cmd,buffer);
msg.port = receivePort;
msg.command_code = connection_list[i].command_code;
//declare character buffer (byte array)
char buffer_msg[msg_size];
//serialize the struct
memcpy(buffer_msg,&msg,msg_size);
//write in pipe the serialized struct
write(pipe_fds[1],buffer_msg,msg_size);
//father waits for the child to exit
if(strcmp(buffer,END)==0)
wait(NULL);
}
}
}
}
void signal_handler(int signum){
switch(signum){
case SIGPIPE:
break; //do nothing
case SIGUSR1:
end_func();
break;
case SIGUSR2:
if(getpid() == parent){
timeToStop_func();
}
else{
//make each child variable timeToStop 1
timeToStop=1;
}
break;
}
}
void child_function(int msg_size){
close(pipe_fds[1]); // child doesn't write...
char task[msg_size];
int valdread,length;
int working = 0;
char invalid_command[8] = "";
//socket to send the command's result
int sockfd;
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror("socket creation failed"); exit(EXIT_FAILURE);
}
while(1){
//check if each child's varaible timeToStop is one
if(timeToStop){
//close pipe read-end
close(pipe_fds[0]);
//close socket for command's answerss
close(sockfd);
//print in stderr
fprintf( stderr, "\nChild process with pid %d is terminated.\n", getpid());
exit(EXIT_SUCCESS);
}
//wait for a task...
if(!working){
valdread=read(pipe_fds[0],task,msg_size);
if(valdread > 0)
working = 1;
}
//wait to read bytes
if(!working)
continue;
/*task assigned!*/
task[valdread]='\0';
//create the struct
server_worker_msg msg;
//desirialize and create the struct
memcpy(&msg ,task, msg_size);
//buffer has the command
char buffer [1024]={0};
strcpy(buffer,msg.cmd);
length = strlen(buffer);
char* udp_buf;
//address to send the command's result
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
// Filling server information
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(msg.port);
//...
if(strcmp(buffer,END)==0){
char end_cmd_res[100]={0};
snprintf(end_cmd_res, 100, "end command\nPid of child:%d", getpid());
udp_buf = create_udp_packet(end_cmd_res,1,msg.command_code);
sendto(sockfd, udp_buf, PACKET_SIZE , MSG_CONFIRM,
(struct sockaddr *) &servaddr,sizeof(servaddr));
sleep(0.005);
//inform father that this child is about to exit
kill(getppid(),SIGUSR1);
//close pipe read-end
close(pipe_fds[0]);
close(sockfd);
exit(EXIT_SUCCESS);
}
if(strcmp(buffer,TIME_TO_STOP)==0){
//print in stderr
fprintf( stderr, "\nChild process with pid %d is terminated.\n", getpid());
char timeToStop_cmd_res[100]="timeToStop command";
udp_buf = create_udp_packet(timeToStop_cmd_res,1,msg.command_code);
sendto(sockfd, udp_buf, PACKET_SIZE , MSG_CONFIRM,
(struct sockaddr *) &servaddr,sizeof(servaddr));
sleep(0.005);
kill(getppid(),SIGUSR2);
//close pipe read-end
close(pipe_fds[0]);
//close socket for command's answerss
close(sockfd);
exit(EXIT_SUCCESS);
}
int commands_number;
char** pipelined_commands = create_commnads_array(buffer,length,&commands_number);
int i=0;
int valid=0;
char command_name[100]="";
int inv_command=0;
//check if all commands are valid
for(i=0;i<commands_number;i++){
strcpy(command_name,pipelined_commands[i]);
valid = valid_command(command_name);
if(!valid){
if(i==0){
inv_command = 1;
break;
}
else{
pipelined_commands[i]= NULL; //invalid in pipe so don't include it in final_command
}
}
}
//invalid name
if(inv_command){
udp_buf = create_udp_packet(invalid_command,1,msg.command_code);
sendto(sockfd, udp_buf, PACKET_SIZE , MSG_CONFIRM,
(struct sockaddr *) &servaddr,sizeof(servaddr));
sleep(0.005);
}
else{
//concatenate the commands
char final_command[100];
strcpy(final_command,pipelined_commands[0]);
for(i=1;i<commands_number;i++){
if(pipelined_commands[i]!= NULL){
strcat(final_command , " | ");
strcat(final_command,pipelined_commands[i]);
}
}
//execute the command
FILE* fp = execute(final_command);
//command can't be executed because of arguments
if(fp == NULL){
udp_buf = create_udp_packet(invalid_command,1,msg.command_code);
sendto(sockfd, udp_buf, PACKET_SIZE , MSG_CONFIRM,
(struct sockaddr *) &servaddr,sizeof(servaddr));
sleep(0.005);
}
//command is executed
else{
char command_result[UPD_CMD_SIZE] = {0};
int c;
int n=0;
while (1){
//last packet
if((c = fgetc(fp)) == EOF){
command_result[n+1] = '\0';
//send the sruct
udp_buf = create_udp_packet(command_result,1,msg.command_code);
sendto(sockfd, udp_buf, PACKET_SIZE , MSG_CONFIRM,
(struct sockaddr *) &servaddr,sizeof(servaddr));
sleep(0.005);
break;
}
command_result[n++] = (char) c;
if(n == UPD_CMD_SIZE-1 ){
command_result[n+1] ='\0';
//send the struct
udp_buf = create_udp_packet(command_result,0,msg.command_code);
sendto(sockfd, udp_buf, PACKET_SIZE, MSG_CONFIRM,
(struct sockaddr *) &servaddr,sizeof(servaddr));
sleep(0.005);
n = 0;
for (int i = 0; i < 512; ++i){
command_result[i] = 0;
}
}
}
/* close */
pclose(fp);
}
}
/*task finished!*/
working= 0;
}
}
/* children's Functions */
char* create_udp_packet(char* command_result,int last,int command_code){
udp_msg msg;
strcpy(msg.command_result,command_result);
msg.last = last;
msg.command_num = command_code;
//declare character buffer (byte array)
static char buffer_msg[PACKET_SIZE];
//serialize the struct
memcpy(buffer_msg,&msg,PACKET_SIZE);
return buffer_msg;
}
/*
Given a pipelined command which may include ';' returns an array of all pipelined commands.
*/
char** create_commnads_array(char* command,int length,int* commands_number){
char** pipelined_commands= NULL;
int num_of_commands = 0;
// Extract the first token
char* token = strtok(command, "|");
/* split string and append tokens to pipelined_commands
* realloc function is used to resize a block of memory that was previously allocated
*/
while (token!=NULL) {
pipelined_commands = realloc (pipelined_commands, sizeof (char*) * ++num_of_commands);
if (pipelined_commands == NULL)
exit (-1); /* memory allocation failed */
pipelined_commands[num_of_commands-1] = token;
token = strtok (NULL, "|");
}
//for each pipelined command get only the first command which ends with ';' and trim it
for (int i = 0; i < num_of_commands; i++){
pipelined_commands[i] = strtok(pipelined_commands[i],";");
trim(pipelined_commands[i]);
}
*commands_number = num_of_commands;
return pipelined_commands;
}
/*
* check if a command is valid
*/
int valid_command(char* command){
//get the name of the command
char* command_name = strtok(command, " ");
for (int i = 0; i < 5; i++){
if(strcmp(command_name, server_commands[i])==0){
return 1;
}
}
return 0;
}
FILE* execute(char* command){
FILE *fp;
/* Open the command for reading. */
fp = popen(command, "r");
// if fp == NULL command can't be executed
return fp;
}
/* end of children's Functions */
/* signal handler functions */
void end_func(){
activeChildren--;
//terminate the server
if(activeChildren == 0 ){
free_workers_array();
close_fds(0,0);
}
}
void timeToStop_func(){
//father close pipe write-end because read is blocking I/O
close(pipe_fds[1]);
//inform all children that is time to stop
for (int i = 0; i < numChildren; i++){
kill(workers_array[i],SIGUSR2);
}
//wait all children to finish
int wpid;
while ((wpid = wait(NULL)) > 0); // this way, the father waits for all the child processes
free_workers_array();
//close all sockets infrom all clients and exit
close_fds(1,1);
}
/*
close reading sockets and server socket
terminate server
*/
void close_fds(int closed_pipe,int print_stderr){
if(!closed_pipe)
close(pipe_fds[1]);
//inform client that server is closing
send_stop_msg();
close_reading_sockets();
//close server
close(server_fd);
if(print_stderr)
fprintf( stderr, "\nParent process with pid %d is terminated.\n", getpid());
exit(EXIT_SUCCESS);
}
//close reading fds
void close_reading_sockets(){
for(int i = 0; i< max_clients ;i++){
if(connection_list[i].socket != -1){
close(connection_list[i].socket);
connection_list[i].socket = -1;
connection_list[i].command_code = -1;
connection_list[i].port = -1;
}
}
}
void free_workers_array(){
for(int i =0;i < numChildren ; i++){
workers_array[i] = -1;
}
free(workers_array);
}
/*
inform all clients that server is closed
*/
void send_stop_msg(){
int sockfd;
//address to send the command's result
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
// Filling server information
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
//...
char* udp_buf;
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror("socket creation failed"); exit(EXIT_FAILURE);
}
for (int i = 0; i < max_clients; i++)
{
if(connection_list[i].socket != -1){
//get each client's receive port
servaddr.sin_port = htons(connection_list[i].port);
udp_buf = create_udp_packet(SERVER_CLOSED,0,0);
sendto(sockfd, udp_buf, PACKET_SIZE, MSG_CONFIRM,
(struct sockaddr *) &servaddr,sizeof(servaddr));
sleep(0.005);
}
}
}
/*end of signal handler functions */