/
ftserver.c
667 lines (600 loc) · 18.6 KB
/
ftserver.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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
/**************************************************************
* * Filename: server.c
* * Coded by: Kevin To
* * Purpose - [CS372 Project 2] Acts as a server to process FTP
* * commands.
* *
* * Sample command:
* * server <port>
* *
* * port = the port for the client to connect to.
* *
* ***************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <signal.h>
#include <ctype.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <time.h>
#include <dirent.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <fcntl.h>
#define LISTEN_QUEUE 5
#define LENGTH 1024
#define NUMBER_ALLOWED_CHARS 27 // Represents the number of different types of allowed characters
#define BUFFERLENGTH 50 // Max allowed length in a string buffer
#define FILELISTLENGTH 1024 // Max allowed length for directory list
// Global variable to track number of children
int number_children = 0;
void handleRequest(int commandSocket, char *clientHostName);
void SendFileToClient(int socket, int filePointer);
void ReceiveClientCommand(int socket, char *clientCommand, char *transferFileName, int *dataPort, char *clientHostName);
void SendFtClientFileStatus(int socket, char *serverResponse);
int GetNumCommas(char *strValue, int strLen);
int GetCommaIdx(char *strValue, int strLen, int occurance);
void OutputServerReqMsg(char *clientCommand, char *transferFileName, int dataPort);
void SendFileListToServer(int sockfd);
void SendFileToServer(int sockfd, char *transferFileName);
void GetFileList(char *returnArr);
int HostnameToIp(char *hostname, char *ip);
int startup (int argc, char *argv[]);
/**************************************************************
* * Entry:
* * sig - the signal event
* *
* * Exit:
* * N/a
* *
* * Purpose:
* * Signal handler to clean up zombie processes
* *
* * Reference: http://stackoverflow.com/questions/19461744/make-parent-wait-for-all-child-processes
* ***************************************************************/
static void wait_for_child(int sig)
{
while (waitpid(-1, NULL, WNOHANG) > 0);
number_children--;
}
/**************************************************************
* * Entry:
* * argc - the number of arguments passed into this program
* * argv - a pointer to the char array of all the arguments
* * passed into this program
* *
* * Exit:
* * N/a
* *
* * Purpose:
* * This is the entry point into the program.
* *
* ***************************************************************/
int main (int argc, char *argv[])
{
return startup(argc, argv);
}
/**************************************************************
* * Entry:
* * argc - the number of arguments passed into this program
* * argv - a pointer to the char array of all the arguments
* * passed into this program
* *
* * Exit:
* * N/a
* *
* * Purpose:
* * Starts up the server code.
* *
* ***************************************************************/
int startup (int argc, char *argv[])
{
if (argc < 2)
{
printf("usage: ftserver <port>\n");
exit(1);
}
// --------------- Section to setup socket -----------------------
// Reference: http://www.tutorialspoint.com/unix_sockets/socket_server_example.htm
int sockfd, newsockfd, sin_size, pid;
struct sockaddr_in addr_local; // client addr
struct sockaddr_in addr_remote; // server addr
int portNumber = atoi(argv[1]);
struct sigaction sa;
char cHostName[BUFFERLENGTH];
bzero(cHostName, BUFFERLENGTH);
// Get the socket file descriptor
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
{
fprintf(stderr, "ERROR: Failed to obtain Socket Descriptor. (errno = %d)\n", errno);
exit(1);
}
// Fill the client socket address struct
addr_local.sin_family = AF_INET; // Protocol Family
addr_local.sin_port = htons(portNumber); // Port number
addr_local.sin_addr.s_addr = INADDR_ANY; // AutoFill local address
bzero(&(addr_local.sin_zero), 8); // Flush the rest of struct
// Bind a port
if ( bind(sockfd, (struct sockaddr*)&addr_local, sizeof(struct sockaddr)) == -1 )
{
fprintf(stderr, "ERROR: Failed to bind Port. Please select another port. (errno = %d)\n", errno);
exit(1);
}
// Listen to port
if (listen(sockfd, LISTEN_QUEUE) == -1)
{
fprintf(stderr, "ERROR: Failed to listen Port. (errno = %d)\n", errno);
exit(1);
}
else
{
printf ("Server open on %d\n", portNumber);
}
// Set up the signal handler to clean up zombie children
// Reference: http://www.thegeekstuff.com/2012/03/catch-signals-sample-c-code/
sa.sa_handler = wait_for_child;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
perror("sigaction");
return 1;
}
// Main loop that accepts and processes client connections
while (1)
{
sin_size = sizeof(struct sockaddr_in);
// Wait for any connections from the client
if ((newsockfd = accept(sockfd, (struct sockaddr *)&addr_remote, &sin_size)) == -1)
{
fprintf(stderr, "ERROR: Obtaining new socket descriptor. (errno = %d)\n", errno);
exit(1);
}
else
{
// Taken from here: http://beej.us/guide/bgnet/output/html/multipage/gethostbynameman.html
struct hostent *he;
struct in_addr ipv4addr;
inet_pton(AF_INET, inet_ntoa(addr_remote.sin_addr), &ipv4addr);
he = gethostbyaddr(&ipv4addr, sizeof ipv4addr, AF_INET);
strncpy(cHostName, he->h_name, BUFFERLENGTH);
printf("Connection from %s\n", cHostName);
}
// Create child process to handle processing multiple connections
number_children++; // Keep track of number of open children
pid = fork();
if (pid < 0)
{
perror("ERROR on fork");
exit(1);
}
if (pid == 0)
{
// This is the client process
close(sockfd);
handleRequest(newsockfd, cHostName);
exit(0);
}
else
{
close(newsockfd);
}
}
}
/**************************************************************
* * Entry:
* * socket - the socket file descriptor
* * cHostName - the actual client host name
* *
* * Exit:
* * N/a
* *
* * Purpose:
* * This code gets run in the forked process to handle the actual
* * server processing of the client request.
* *
* ***************************************************************/
void handleRequest(int commandSocket, char *cHostName)
{
int dataPort = -1;
char clientCommand[BUFFERLENGTH];
bzero(clientCommand, BUFFERLENGTH);
char transferFileName[BUFFERLENGTH];
bzero(transferFileName, BUFFERLENGTH);
char clientHostName[BUFFERLENGTH]; // Note: this variable is the ftserver hostname
bzero(clientHostName, BUFFERLENGTH);
ReceiveClientCommand(commandSocket, clientCommand, transferFileName, &dataPort, clientHostName);
OutputServerReqMsg(clientCommand, transferFileName, dataPort);
close(commandSocket);
// Get IP address from ftclient host name
char IP[100];
HostnameToIp(cHostName, IP);
int dataSocket;
struct sockaddr_in remote_addr;
// Get the Socket file descriptor
if ((dataSocket = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
printf("Error: Failed to obtain socket descriptor.\n");
exit(2);
}
// Fill the socket address struct
remote_addr.sin_family = AF_INET;
remote_addr.sin_port = htons(dataPort);
inet_pton(AF_INET, IP, &remote_addr.sin_addr);
bzero(&(remote_addr.sin_zero), 8);
// Try to connect the client data port
if (connect(dataSocket, (struct sockaddr *)&remote_addr, sizeof(struct sockaddr)) == -1)
{
printf("Error: could not contact client on port %d\n", dataPort);
exit(2);
}
if (strcmp(clientCommand, "-l") == 0)
{
printf("Sending directory contents to %s:%d.\n", clientHostName, dataPort);
SendFileListToServer(dataSocket);
}
else if (strcmp(clientCommand, "-g") == 0)
{
// Reference: http://stackoverflow.com/questions/230062/whats-the-best-way-to-check-if-a-file-exists-in-c-cross-platform
if(access(transferFileName, F_OK) != -1)
{
// File exists
SendFtClientFileStatus(dataSocket, "s");
printf("Sending \"%s\" to %s:%d\n", transferFileName, cHostName, dataPort);
SendFileToServer(dataSocket, transferFileName);
} else {
// File doesnt exist
printf("File not found. Sending error message to %s:%d\n", cHostName, dataPort);
SendFtClientFileStatus(dataSocket, "e");
}
}
close(dataSocket);
exit(0); // Exiting the child process.
}
/**************************************************************
* * Entry:
* * hostname - the host name
* * ip - char array that will hold the ip address
* *
* * Exit:
* * n/a
* *
* * Purpose:
* * Gets the IP from the hostname.
* *
* * Reference:
* * http://www.binarytides.com/hostname-to-ip-address-c-sockets-linux/
* ***************************************************************/
int HostnameToIp(char *hostname , char *ip)
{
struct hostent *he;
struct in_addr **addr_list;
int i;
if ((he = gethostbyname(hostname)) == NULL)
{
herror("gethostbyname");
return 1;
}
addr_list = (struct in_addr **) he->h_addr_list;
// Iterate through the network info struct to get the IP.
for(i = 0; addr_list[i] != NULL; i++)
{
//Return the first one;
strcpy(ip ,inet_ntoa(*addr_list[i]));
return 0;
}
return 1;
}
/**************************************************************
* * Entry:
* * sockfd - the socket to send the file list to.
* *
* * Exit:
* * n/a
* *
* * Purpose:
* * Sends the server's directory list to the client
* *
* ***************************************************************/
void SendFileListToServer(int sockfd)
{
char fileList[FILELISTLENGTH];
bzero(fileList, FILELISTLENGTH);
GetFileList(fileList);
int sendSize = FILELISTLENGTH;
if (send(sockfd, fileList, sendSize, 0) < 0)
{
printf("Error: Failed to send directory list.\n");
}
}
/**************************************************************
* * Entry:
* * dirList - output for comma delimited list of file names
* *
* * Exit:
* * n/a
* *
* * Purpose:
* * Generates a comma delimited string of all the file
* * names in the server's directory
* *
* * Reference: http://stackoverflow.com/questions/4204666/how-to-list-files-in-a-directory-in-a-c-program
* *
* ***************************************************************/
void GetFileList(char *dirList)
{
// Get host name of server to add to the beginning of the list
char name[50];
gethostname(name, 50);
strcat(dirList, name);
strcat(dirList, ",");
DIR *directoryHandle;
struct dirent *directoryStruct;
// Loop through all the files and add them to the list
directoryHandle = opendir(".");
if (directoryHandle)
{
int currDirLen = 0;
int currDirListLen = 0;
while ((directoryStruct = readdir(directoryHandle)) != NULL)
{
if (strcmp(directoryStruct->d_name, ".") != 0 && strcmp(directoryStruct->d_name, "..") != 0)
{
// Make sure we dont overflow our send buffer
currDirLen = strnlen(directoryStruct->d_name, NAME_MAX);
currDirListLen = strnlen(dirList, FILELISTLENGTH);
if ((currDirListLen + currDirLen) > FILELISTLENGTH)
{
break;
}
// Add the directory to the comma delimited list
strcat(dirList, directoryStruct->d_name);
strcat(dirList, ",");
}
}
closedir(directoryHandle);
}
// Remove the last comma
int lastCommaIdx = strnlen(dirList, FILELISTLENGTH) - 1;
dirList[lastCommaIdx] = 0;
}
/**************************************************************
* * Entry:
* * sockfd - the socket to send the file list to.
* *
* * Exit:
* * n/a
* *
* * Purpose:
* * Sends the requested file to the server
* *
* ***************************************************************/
void SendFileToServer(int sockfd, char *transferFileName)
{
FILE * transferFilePointer;
transferFilePointer = fopen(transferFileName, "r+");
int fd = fileno(transferFilePointer);
SendFileToClient(sockfd, fd);
fclose(transferFilePointer);
}
/**************************************************************
* * Entry:
* * clientCommand - char array that holds the client command
* * transferFileName - char array that holds the transfer file name
* * dataPort - int passed by ref to hold port number
* *
* * Exit:
* * N/A
* *
* * Purpose:
* * Outputs the server request on to the console
* *
* ***************************************************************/
void OutputServerReqMsg(char *clientCommand, char *transferFileName, int dataPort)
{
if (strcmp(clientCommand, "-l") == 0)
{
printf("List directory requested on port %d.\n", dataPort);
}
if (strcmp(clientCommand, "-g") == 0)
{
printf("File \"%s\" requested on port %d.\n", transferFileName, dataPort);
}
}
/**************************************************************
* * Entry:
* * socket - the socket to send the hankdshake acknoledgement from.
* * serverResponse - The single letter response from the server.
* *
* * Exit:
* * n/a
* *
* * Purpose:
* * Sends the file status to the client.
* *
* ***************************************************************/
void SendFtClientFileStatus(int socket, char *serverResponse)
{
char sendBuffer[2]; // Send buffer
bzero(sendBuffer, 2);
strncpy(sendBuffer, serverResponse, 1);
if (send(socket, sendBuffer, 1, 0) < 0)
{
printf("ERROR: Failed to send client the following message: %s", serverResponse);
exit(1);
}
}
/**************************************************************
* * Entry:
* * socket - the socket to receive the client command,
* * dataport, and tranfer file name (if applicable)
* * clientCommand - char array that holds the client command
* * transferFileName - char array that holds the transfer file name
* * dataPort - int passed by ref to hold port number
* * clientHostName - char array that holds the client host name
* *
* * Exit:
* * N/A
* *
* * Purpose:
* * Receives the command message from the client.
* *
* ***************************************************************/
void ReceiveClientCommand(int socket, char *clientCommand, char *transferFileName, int *dataPort, char *clientHostName)
{
// Wait for client to send message
char receiveBuffer[BUFFERLENGTH];
bzero(receiveBuffer, BUFFERLENGTH);
recv(socket, receiveBuffer, LENGTH, 0);
// Get the comma instance in the command string
int numCommas = GetNumCommas(receiveBuffer, BUFFERLENGTH);
int commaIdx = GetCommaIdx(receiveBuffer, BUFFERLENGTH, 1);
// Get the client command from the sent string
strncpy(clientCommand, receiveBuffer, commaIdx);
// Get the data port from the sent string
char dataPortBuffer[BUFFERLENGTH];
bzero(dataPortBuffer, BUFFERLENGTH);
strncpy(dataPortBuffer, receiveBuffer + commaIdx + 1, BUFFERLENGTH);
(*dataPort) = atoi(dataPortBuffer);
if (numCommas == 2)
{
int commaIdx = GetCommaIdx(receiveBuffer, BUFFERLENGTH, 2);
strncpy(clientHostName, receiveBuffer + commaIdx + 1, BUFFERLENGTH);
}
// Get file name if a third param is passed through
if (numCommas == 3)
{
// Example = "-l, 3333, filename, hostname".
// This code block finds the second and third comma and gets the value
// in between
int commaIdxStart = GetCommaIdx(receiveBuffer, BUFFERLENGTH, 2);
int commaIdxEnd = GetCommaIdx(receiveBuffer, BUFFERLENGTH, 3);
strncpy(transferFileName, receiveBuffer + commaIdxStart + 1, commaIdxEnd - commaIdxStart - 1);
strncpy(clientHostName, receiveBuffer + commaIdxEnd + 1, BUFFERLENGTH);
}
// Clean the newline character from the client host name
int i = 0;
char currChar = ' ';
while(currChar != 0)
{
if (i == BUFFERLENGTH)
{
break;
}
currChar = clientHostName[i];
if (currChar == '\n')
{
clientHostName[i] = 0;
break;
}
i++;
}
}
/**************************************************************
* * Entry:
* * strValue - the string value you want to find a comma in.
* * strLen - the length of the string.
* * occurance - the comma occurance you want the index to.
* *
* * Exit:
* * Returns the index of the comma.
* *
* * Purpose:
* * Gets the index of the specified comma occurance.
* *
* ***************************************************************/
int GetCommaIdx(char *strValue, int strLen, int occurance)
{
int i = 0;
int commaIdx = -1;
int numCommas = 0;
char currChar = ' ';
while(currChar != '\n')
{
currChar = strValue[i++];
if (currChar == ',')
{
numCommas++;
}
if (numCommas == occurance)
{
commaIdx = i - 1;
break;
}
// Exit loop if we are trying to access an out of bounds index
if (i >= strLen)
{
break;
}
}
return commaIdx;
}
/**************************************************************
* * Entry:
* * strValue - the string value you want to find a comma in.
* * strLen - the length of the string
* *
* * Exit:
* * Returns the number of commas found.
* *
* * Purpose:
* * Gets the number of commas in a string.
* *
* ***************************************************************/
int GetNumCommas(char *strValue, int strLen)
{
int i = 0;
int numCommas = 0;
char currChar = ' ';
while(currChar != '\n')
{
currChar = strValue[i++];
if (currChar == ',')
{
numCommas++;
}
if (i >= strLen)
{
break;
}
}
return numCommas;
}
/**************************************************************
* * Entry:
* * socket - the file desc for the socket
* * filePointer - the file desc for the transferring file
* *
* * Exit:
* * n/a
* *
* * Purpose:
* * Sends a file to the client.
* *
* ***************************************************************/
void SendFileToClient(int socket, int filePointer)
{
char sendBuffer[LENGTH]; // Send buffer
if (filePointer == 0)
{
fprintf(stderr, "ERROR: File not found on server.");
exit(1);
}
bzero(sendBuffer, LENGTH);
int readSize;
while ((readSize = read(filePointer, sendBuffer, LENGTH)) > 0)
{
if (send(socket, sendBuffer, readSize, 0) < 0)
{
fprintf(stderr, "ERROR: Failed to send file.");
exit(1);
}
bzero(sendBuffer, LENGTH);
}
}