-
Notifications
You must be signed in to change notification settings - Fork 0
/
clientFTP.c
494 lines (426 loc) · 20.3 KB
/
clientFTP.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
/*
* echoclient.c - An echo client
*/
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include <unistd.h>
#include "csapp.h"
#include <termios.h>
int clientfd;
void crush(int sig){
// Client forced to close, we cut properly the socket and send a BYE command.
Rio_writen(clientfd, "BYE\n", 4);
Close(clientfd);
exit(0);
}
// Used to split our command string in a char**
char** str_split(char* a_str, const char a_delim)
{
char** result = 0;
size_t count = 0;
char* tmp = a_str;
char* last_comma = 0;
char delim[2];
delim[0] = a_delim;
delim[1] = 0;
/* Count how many elements will be extracted. */
while (*tmp)
{
if (a_delim == *tmp)
{
count++;
last_comma = tmp;
}
tmp++;
}
/* Add space for trailing token. */
count += last_comma < (a_str + strlen(a_str) - 1);
/* Add space for terminating null string so caller
knows where the list of returned strings ends. */
count++;
result = malloc(sizeof(char*) * count);
if (result)
{
size_t idx = 0;
char* token = strtok(a_str, delim);
while (token)
{
assert(idx < count);
*(result + idx++) = strdup(token);
token = strtok(0, delim);
}
assert(idx == count - 1);
*(result + idx) = 0;
}
return result;
}
int main(int argc, char **argv)
{
int port;
char *host, buf[MAXLINE];
char reply[MAXLINE];
rio_t rio;
rio_t bufferRio;
int fdin;
int fdstat;
size_t n;
struct stat st;
size_t CHUNK_SIZE = 10000; // If edited, please make sure it is the same value in both server and client !
clock_t start, end;
double cpu_time_used;
ssize_t totalSize;
if (argc != 2) {
fprintf(stderr, "usage: %s <host>\n", argv[0]);
exit(0);
}
host = argv[1];
port = 2121;
/*
* Note that the 'host' can be a name or an IP address.
* If necessary, Open_clientfd will perform the name resolution
* to obtain the IP address.
*/
clientfd = Open_clientfd(host, port);
/*
* At this stage, the connection is established between the client
* and the server OS ... but it is possible that the server application
* has not yet called "Accept" for this connection
*/
printf("Connected to %s\n", host);
signal(SIGINT, crush);
Rio_readinitb(&rio, clientfd);
printf("ftp> ");
// Connected, display the prompt
do {
while (Fgets(buf, MAXLINE, stdin) != NULL) {
// Waits for a command, and breaks it into an array
char **cmd = str_split(buf, ' ');
// Checks which functionnality we are calling
if (strcmp(cmd[0], "get") == 0){
// Getting a file from remote server
printf("This is get\n");
Rio_writen(clientfd, "GET\n", 4);
// Get the filename
char okFileName[strlen(cmd[1])];
int i;
for (i = 0; i < strlen(cmd[1]); i++){
okFileName[i] = cmd[1][i];
}
okFileName[strcspn(okFileName,"\r\n")] = 0;
Rio_writen(clientfd, cmd[1], strlen(cmd[1]));
// Sends the filename to the server, and waits for a anwser
// Create our temp file - holds the name of the processed file for the recover command
fdstat = Open("status.tmp", O_RDWR | O_CREAT, 0644);
rio_writen(fdstat, cmd[1], strlen(cmd[1]));
Close(fdstat);
//end of section
char code[4];
totalSize = 0;
start = clock();
// Starts timer for status check
if ((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0){
//printf("%s\n", buf);
strncpy(code, buf, 3);
//printf("%s\n", code);
// We catch the reply code
if (strcmp(code, "100") == 0) {
// Server is sending ! Catch size, determine how many chunks we are going to fetch + the remaining data size
printf("Server replied, receiving file...\n");
// Opens the new file
fdin = Open(okFileName, O_WRONLY | O_CREAT, 0644);
size_t expectedSize;
if ((n = Rio_readlineb(&rio, &expectedSize, sizeof(size_t))) != 0){
printf("Awaiting %ld bytes !\n", expectedSize);
}
long int chunks = expectedSize / CHUNK_SIZE;
// Catch the data
while (chunks > 0) {
if ((n = Rio_readnb(&rio, buf, CHUNK_SIZE)) > 0) {
totalSize += rio_writen(fdin, buf, n);
chunks -= 1;
}
}
if ((n = Rio_readnb(&rio, buf, (expectedSize - totalSize))) > 0) {
totalSize += rio_writen(fdin, buf, n);
}
end = clock();
// Finished, print statistics and timing
Close(fdin);
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("Transfer succesfully complete.\n");
ssize_t bandwidth = (totalSize/1000) / cpu_time_used;
if (bandwidth > 0){
printf("%ld bytes received in %f seconds (%ld Kbytes/s)\n", totalSize, cpu_time_used, bandwidth);
} else {
printf("%ld bytes received in %f seconds (inf Kbytes/s)\n", totalSize, cpu_time_used);
}
// We nailed it, remove the now useless status.tmp
remove("status.tmp");
} else if (strcmp(code, "550") == 0){
// File does not exists, well, quit
printf("File does not exists remotely !\n");
remove("status.tmp");
} else {
// Something happened :(
printf("Unknown error.\n");
}
}
} else if (strcmp(cmd[0], "recover\n") == 0) {
// We are recovering a partially-downloaded file !
printf("Check for file lock...\n");
// Check if there is already a lock - if yes, a previous dl was interrupted !
fdstat = open("status.tmp", O_RDONLY, 0);
if (fdstat != -1) {
printf("File lock status.tmp found ! Recovering file...\n");
// There is a lock
rio_readinitb(&bufferRio, fdstat);
if ((n = Rio_readlineb(&bufferRio, buf, MAXLINE)) > 0) {
Rio_writen(clientfd, "REC\n", 4);
// Get the filename
char okFileName[n];
int i;
for (i = 0; i < n; i++){
okFileName[i] = buf[i];
}
okFileName[strcspn(okFileName,"\r\n")] = 0;
stat(okFileName, &st);
printf("already has %ld bytes of file %s\n", st.st_size, okFileName);
// File name and size
Rio_writen(clientfd, buf, strlen(buf));
char code[4];
if ((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0){
strncpy(code, buf, 3);
if (strcmp(code, "550") == 0){
// Does not exists, fake lock ? We kill it anyways
printf("File does not exists remotely ! Closing connection...\n");
remove("status.tmp");
} else if (strcmp(code, "100") == 0) {
// Exists, we catch up
printf("Server replied, receiving file...\n");
Rio_writen(clientfd, &(st.st_size), sizeof(size_t)-1);
fdin = Open(okFileName, O_WRONLY|O_APPEND, 0644);
size_t expectedSize;
if ((n = Rio_readlineb(&rio, &expectedSize, sizeof(size_t))) != 0){
printf("Awaiting %ld bytes !\n", expectedSize);
}
long int chunks = expectedSize / CHUNK_SIZE;
totalSize = 0;
while (chunks > 0) {
if ((n = Rio_readnb(&rio, buf, CHUNK_SIZE)) > 0) {
totalSize += rio_writen(fdin, buf, n);
chunks -= 1;
}
}
if ((n = Rio_readnb(&rio, buf, (expectedSize - totalSize))) > 0) {
totalSize += rio_writen(fdin, buf, n);
}
printf("Situation recovered ! %ld bytes downloaded to complete the file.\n", totalSize);
Close(fdin);
close(fdstat);
remove("status.tmp");
// Recovered, we can close the now complete file and remove the lock
} else {
// Oh well
printf("Unknown error...\n");
}
}
}
} else {
// Was status.tmp externally destroyed or something like that ? Anyways, there is no lock. Abort.
printf("Nothing to recover, or the file status.tmp has been deleted by another application.\n");
//break;
}
} else if (strcmp(cmd[0], "put") == 0) {
// We are putting a file, prepare the data and the commands
// Get the filename
char okFileName[strlen(cmd[1])];
int i;
for (i = 0; i < strlen(cmd[1]); i++){
okFileName[i] = cmd[1][i];
}
okFileName[strcspn(okFileName,"\r\n")] = 0;
printf("Putting file %s on remote server...\n", okFileName);
// Ouverture du fichier
fdin = open(okFileName, O_RDONLY, 0);
if (fdin == -1) {
// File ? What file ?
printf("file does not exists !!!\n");
} else {
// Nah jk, preepare it for sending
stat(okFileName, &st);
long int chunks = st.st_size / CHUNK_SIZE;
printf("file exists, sending %ld bytes in %ld chunks...\n", st.st_size, chunks);
Rio_writen(clientfd, "PUT\n", 4);
// Ask to PUT a file
char code[4];
if ((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0){
strncpy(code, buf, 3);
if (strcmp(code, "530") == 0){
// Not logged in, abort (server won't accept your file even if you force send it).
printf("Action not permitted, account needed !\n");
} else if (strcmp(code, "100") == 0) {
// All green on track 1
Rio_writen(clientfd, cmd[1], strlen(cmd[1]));
Rio_writen(clientfd, &st.st_size, sizeof(size_t)-1);
// Mise en place du buffer
rio_readinitb(&bufferRio, fdin);
size_t rSent = 0;
// Same send as in the server
while (chunks > 0) {
if ((n = Rio_readnb(&bufferRio, buf, CHUNK_SIZE)) > 0) {
Rio_writen(clientfd, buf, CHUNK_SIZE);
rSent += n;
chunks -= 1;
}
}
if ((n = Rio_readnb(&bufferRio, buf, CHUNK_SIZE)) > 0) {
Rio_writen(clientfd, buf, n);
rSent += n;
//printf("Sent remaining data\n");
}
close(fdin);
}
}
}
} else if (strcmp(cmd[0], "ls\n") == 0) {
// Extra command - ls - prints the nb of elements and their names (at least 2 : . and ..)
Rio_writen(clientfd, "CMD\n", 4);
Rio_writen(clientfd, "LS\n", 3);
int count;
if ((n = Rio_readnb(&rio, &count, sizeof(int))) > 0) {
printf("There are %d elements on the current folder\n", count);
while(count > 0){
if ((n = Rio_readnb(&rio, &reply, MAXLINE)) > 0) {
printf("\t%s ", reply);
}
printf("\n");
count -= 1;
}
}
} else if (strcmp(cmd[0], "pwd\n") == 0) {
// Extra command - pwd - prints wd
Rio_writen(clientfd, "CMD\n", 4);
Rio_writen(clientfd, "PWD\n", 4);
if ((n = Rio_readnb(&rio, &reply, MAXLINE)) > 0) {
printf("Working dir : %s\n", reply);
}
} else if (strcmp(cmd[0], "cd") == 0) {
// Extra command - cd - prints new wd or error
Rio_writen(clientfd, "CMD\n", 4);
Rio_writen(clientfd, "CD\n", 3);
Rio_writen(clientfd, cmd[1], strlen(cmd[1]));
if ((n = Rio_readnb(&rio, &reply, MAXLINE)) > 0) {
printf("New wd : %s\n", reply);
}
} else if (strcmp(cmd[0], "mkdir") == 0) {
// Extra command - mkdir - prints status - needs login
Rio_writen(clientfd, "CMD\n", 4);
Rio_writen(clientfd, "MKDIR\n", 6);
char code[4];
if ((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0){
strncpy(code, buf, 3);
if (strcmp(code, "530") == 0){
printf("Action not permitted, account needed !\n");
} else {
Rio_writen(clientfd, cmd[1], strlen(cmd[1]));
if ((n = Rio_readnb(&rio, &reply, MAXLINE)) > 0) {
printf("Reply : %s\n", reply);
}
}
}
} else if (strcmp(cmd[0], "rm") == 0) {
// Extra command - rm and rm -r - removes file/directory and prints status - needs login
Rio_writen(clientfd, "CMD\n", 4);
char code[4];
if (strcmp(cmd[1], "-r") == 0) {
// Remove dir mode
Rio_writen(clientfd, "RMDIR\n", 6);
if ((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0){
strncpy(code, buf, 3);
if (strcmp(code, "530") == 0){
printf("Action not permitted, account needed !\n");
} else {
Rio_writen(clientfd, cmd[2], strlen(cmd[2]));
if ((n = Rio_readnb(&rio, &reply, MAXLINE)) > 0) {
printf("Reply : %s\n", reply);
}
}
}
} else {
// Remove file mode
Rio_writen(clientfd, "RMFILE\n", 7);
if ((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0){
strncpy(code, buf, 3);
if (strcmp(code, "530") == 0){
printf("Action not permitted, account needed !\n");
} else {
Rio_writen(clientfd, cmd[1], strlen(cmd[1]));
if ((n = Rio_readnb(&rio, &reply, MAXLINE)) > 0) {
printf("Reply : %s\n", reply);
}
}
}
}
} else if (strcmp(cmd[0], "auth") == 0) {
// Login on the server - only if not yet login
Rio_writen(clientfd, "CMD\n", 4);
Rio_writen(clientfd, "AUTH\n", 5);
char code[4];
if ((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0){
strncpy(code, buf, 3);
if (strcmp(code, "230") == 0){
printf("You are already logged in !\n");
} else {
Rio_writen(clientfd, cmd[1], strlen(cmd[1]));
printf("PASSWORD> ");
if (Fgets(buf, MAXLINE, stdin) != NULL) {
Rio_writen(clientfd, buf, strlen(buf));
if ((n = Rio_readnb(&rio, &reply, MAXLINE)) > 0) {
printf("Reply : %s\n", reply);
}
} else {
printf("Please provide a password...");
}
}
}
} else if (strcmp(cmd[0], "addusr") == 0) {
// Create an account - login needed
Rio_writen(clientfd, "CMD\n", 4);
Rio_writen(clientfd, "ADDUSR\n", 7);
char code[4];
if ((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0){
strncpy(code, buf, 3);
if (strcmp(code, "530") == 0){
printf("Action not permitted, account needed !\n");
} else {
Rio_writen(clientfd, cmd[1], strlen(cmd[1]));
printf("PASSWORD FOR NEW USER> ");
if (Fgets(buf, MAXLINE, stdin) != NULL) {
Rio_writen(clientfd, buf, strlen(buf));
if ((n = Rio_readnb(&rio, &reply, MAXLINE)) > 0) {
printf("Reply : %s\n", reply);
}
} else {
printf("Please provide a password...");
}
}
}
} else if (strcmp(cmd[0], "bye\n") == 0) {
// Say bye kevin
printf("Bye.\n");
Rio_writen(clientfd, "BYE\n", 4);
Close(clientfd);
exit(0);
} else {
// Sorry not sorry
printf("Unknown command !\n");
}
// Reprompt after command
printf("ftp> ");
}
} while(1);
}