-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.c
More file actions
542 lines (489 loc) · 13.3 KB
/
Copy pathclient.c
File metadata and controls
542 lines (489 loc) · 13.3 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <dirent.h>
//#define DATASIZE 1024
#define DATASIZE 1000000
#define BUFSIZE 1000000
//#define BUFSIZE 1024
#define PORT 10000
void quit();
void cd();
void pwd(int num, char *com[]);
void dir(int num, char *com[]);
void lpwd();
void lcd();
void ldir(int num, char *com[]);
void get(int num, char *com[]);
void put(int argc, char *argv[]);
void send_filepath(int num, char *com[]);
int sd;
FILE *fp;
int file_close = 1;
struct command_table {
char *cmd;
void (*func)();
} cmd_tbl[] = {
{"quit", quit},
{"pwd", pwd},
{"cd", cd},
{"dir", dir},
{"lpwd", lpwd},
{"lcd", lcd},
{"ldir", ldir},
{"get", get},
{"put", send_filepath},
{NULL, NULL}
};
struct ftp_header {
uint8_t type;
uint8_t code;
uint16_t length;
};
void lcd(int num, char *com[])
{
if (chdir(com[1]) < 0) {
perror("chdir");
}
}
/* send file content */
void put(int num, char *com[])
{
FILE *fp;
char buf[BUFSIZE];
char *p;
int i, k, n = 0, len = 0, time;
uint16_t length;
int flag = 0;
struct ftp_header *send_header;
uint8_t *send_pay;
uint8_t *data;
int wtime = 0;
if ((fp = fopen(com[1], "r")) == NULL) {
perror("fopen");
exit(EXIT_FAILURE);
} else {
/* content of the file */
n = fread(buf, sizeof(char), BUFSIZE, fp);
len = n;
if (n < BUFSIZE) { /// filesize is under BUFSIZE
data = (uint8_t *)malloc(sizeof(struct ftp_header) + sizeof(uint8_t) * n);
send_header = (struct ftp_header *)data;
send_pay = data + sizeof(struct ftp_header);
memcpy(send_pay, buf, n);
/* send file content */
send_header->type = 0x20;
send_header->code = 0x00;
send_header->length = htons(len);
if (send(sd, data, sizeof(struct ftp_header) + len, 0) < 0) {
perror("send");
exit(EXIT_FAILURE);
}
free(data);
} else { /// filesize if over BUFSIZE
while (1) {
data = (uint8_t *)malloc(sizeof(struct ftp_header) + sizeof(uint8_t) * n);
send_header = (struct ftp_header *)data;
send_pay = data + sizeof(struct ftp_header);
memcpy(send_pay, buf, n);
/* send file content */
send_header->type = 0x20;
send_header->code = 0x01;
send_header->length = htons(n);
if (send(sd, data, sizeof(struct ftp_header) + n, 0) < 0) {
perror("send");
exit(EXIT_FAILURE);
}
free(data);
memset(buf, 0, BUFSIZE);
n = fread(buf, sizeof(char), BUFSIZE, fp);
len += n;
if (n < BUFSIZE) {
flag++;
break;
}
wtime++;
}
/* send the rest of the file */
data = (uint8_t *)malloc(sizeof(struct ftp_header) + sizeof(uint8_t) * n);
send_header = (struct ftp_header *)data;
send_pay = data + sizeof(struct ftp_header);
memcpy(send_pay, buf, n);
/* send file content */
send_header->type = 0x20;
send_header->code = 0x00;
send_header->length = htons(n);
printf("going to send last data");
if (send(sd, data, sizeof(struct ftp_header) + n, 0) < 0) {
perror("send");
exit(EXIT_FAILURE);
}
free(data);
}
}
}
/* send file path */
void send_filepath(int num, char *com[])
{
char pkt_data[sizeof(struct ftp_header) + DATASIZE];
struct ftp_header *send_header = (struct ftp_header *)pkt_data;
char *data = pkt_data + sizeof(struct ftp_header);
int len;
struct ftp_header recv_header;
uint16_t data_len = 0;
uint8_t type, code;
len = strlen(com[1]);
strcpy(data, com[1]);
//printf("file path = %s, length = %d\n", data, len);
send_header->type = 0x06;
send_header->code = 0x00;
send_header->length = htons(len - 1);
if (send(sd, pkt_data, sizeof(struct ftp_header) + len, 0) < 0) {
perror("send");
exit(EXIT_FAILURE);
}
/* receive only the header part */
if (recv(sd, &recv_header, sizeof(struct ftp_header), 0) < 0) {
perror("recv");
exit(EXIT_FAILURE);
}
/* save the header info */
data_len = ntohs(recv_header.length);
type = recv_header.type;
code = recv_header.code;
if (type == 0x10) {
put(num, com); ////// call put function to send file content
}
}
void get(int num, char *com[])
{
char pkt_data[sizeof(struct ftp_header) + DATASIZE];
struct ftp_header *send_header = (struct ftp_header *)pkt_data;
char *data = pkt_data + sizeof(struct ftp_header);
int len, data_len, n, size;
struct ftp_header recv_header;
struct ftp_header recv_header_length;
char file_data[DATASIZE];
uint8_t code;
len = strlen(com[1]);
strcpy(data, com[1]);
//printf("file path = %s, length = %d\n", data, len);
// send file path to the server
send_header->type = 0x05;
send_header->code = 0x00;
send_header->length = htons(len);
if (send(sd, pkt_data, sizeof(struct ftp_header) + len, 0) < 0) {
perror("send");
exit(EXIT_FAILURE);
}
/* receive ack from server */
if (recv(sd, &recv_header, sizeof(struct ftp_header), 0) < 0) {
perror("recv");
exit(EXIT_FAILURE);
}
/* receive ftp_header and get data length */
if (recv(sd, &recv_header_length, sizeof(struct ftp_header), 0) < 0) {
perror("recv");
exit(EXIT_FAILURE);
}
data_len = (int)ntohs(recv_header_length.length);
code = recv_header_length.code;
if (file_close == 1) { /// if file pointer is not open yet
fp = fopen(data, "w");
file_close = 0; /// file pointer is set
}
if (code == 0x00) {
while (data_len > 0) {
if ((n = read(sd, file_data, DATASIZE)) > 0) {
file_data[n] = '\0';
size = fwrite(file_data, sizeof(char), n, fp);
if (size < n) {
if (!feof(fp)) {
perror("fwrite");
}
break;
}
data_len = data_len - size;
}
}
fclose(fp);
file_close = 1;
} else {
while (code != 0x00) {
// receive the first part of the file
while (data_len > 0) {
if ((n = read(sd, file_data, DATASIZE)) > 0) {
file_data[n] = '\0';
size = fwrite(file_data, sizeof(char), n, fp);
if (size < n) {
if (!feof(fp)) {
perror("fwrite");
}
break;
}
data_len = data_len - size;
}
}
/* receive ftp_header and get data length */
if (recv(sd, &recv_header_length, sizeof(struct ftp_header), 0) < 0) {
perror("recv");
exit(EXIT_FAILURE);
}
data_len = (int)ntohs(recv_header_length.length);
code = recv_header_length.code;
}
// receive the rest of the file
while (data_len > 0) {
if ((n = read(sd, file_data, DATASIZE)) > 0) {
file_data[n] = '\0';
size = fwrite(file_data, sizeof(char), n, fp);
if (size < n) {
if (!feof(fp)) {
perror("fwrite");
}
break;
}
data_len = data_len - size;
}
}
fclose(fp);
file_close = 1;
}
}
void pwd(int num, char *com[])
{
char pkt_data[sizeof(struct ftp_header) + DATASIZE];
struct ftp_header *send_header = (struct ftp_header *)pkt_data;
char *data = pkt_data + sizeof(struct ftp_header);
int len;
struct ftp_header recv_header;
uint16_t data_len = 0;
uint8_t type, code;
char dir[DATASIZE];
int n;
send_header->type = 0x02;
send_header->code = 0x00;
send_header->length = 0;
if (send(sd, send_header, sizeof(struct ftp_header) + 0, 0) < 0) {
perror("send");
exit(EXIT_FAILURE);
}
if (recv(sd, &recv_header, sizeof(struct ftp_header), 0) < 0) {
perror("recv");
exit(EXIT_FAILURE);
}
data_len = ntohs(recv_header.length);
type = recv_header.type;
code =recv_header.code;
if ((n = read(sd, dir, data_len)) > 0) {
dir[n] = '\n';
dir[n + 1] = '\0';
//fwrite(data, sizeof(char), n, stdout);
fputs(dir, stdout);
}
}
void lpwd()
{
char data[DATASIZE];
FILE *fp;
fp = popen("pwd", "r");
while (fgets(data, DATASIZE - 1, fp) != NULL) {
fputs(data, stdout);
}
fclose(fp);
}
void ldir(int num, char *com[])
{
char info[DATASIZE];
int result, dir_len, k;
struct stat st;
DIR *dir;
struct dirent *dp;
char *p;
char data[DATASIZE];
if (num == 0) {
info[0] = '.';
info[1] = '\0';
} else {
strcpy(info, com[1]);
}
result = stat(info, &st);
if (result != 0) {
fprintf(stderr, "%s is not found.\n", info);
exit(EXIT_FAILURE);
}
if ((st.st_mode & S_IFMT) == S_IFDIR) {
//printf("%s is directory.\n", info);
if ((dir = opendir(info)) == NULL) {
perror("opendir");
exit(EXIT_FAILURE);
}
p = data;
for (dp = readdir(dir); dp != NULL; dp = readdir(dir)) {
printf("%s\n", dp->d_name);
}
} else {
printf("%s is not directory.\n", info);
}
}
void quit()
{
struct ftp_header send_header;
send_header.type = 0x01;
send_header.code = 0x00;
send_header.length = 0;
if (send(sd, &send_header, sizeof(struct ftp_header), 0) < 0) {
perror("send");
exit(EXIT_FAILURE);
}
close(sd);
exit(0);
}
void dir(int num, char *com[])
{
char pkt_data[sizeof(struct ftp_header) + DATASIZE];
struct ftp_header *send_header = (struct ftp_header *)pkt_data;
char *data = pkt_data + sizeof(struct ftp_header);
struct ftp_header recv_header;
uint16_t data_len = 0;
uint8_t type, code;
char dir[DATASIZE];
int n, len;
//printf("num == %d\n", num);
if (num == 1) {
len = strlen(com[1]);
strcpy(data, com[1]);
//printf("file path = %s, length = %d\n", data, len);
send_header->type = 0x04;
send_header->code = 0x00;
send_header->length = htons(len);
} else {
len = 0;
send_header->type = 0x04;
send_header->code = 0x00;
send_header->length = htons(0);
}
if (send(sd, pkt_data, sizeof(struct ftp_header) + len, 0) < 0) {
perror("send");
exit(EXIT_FAILURE);
}
if (recv(sd, &recv_header, sizeof(struct ftp_header), 0) < 0) {
perror("recv");
exit(EXIT_FAILURE);
}
data_len = ntohs(recv_header.length);
type = recv_header.type;
code =recv_header.code;
if ((n = read(sd, dir, data_len)) > 0) {
dir[n] = '\n';
dir[n + 1] = '\0';
//fwrite(data, sizeof(char), n, stdout);
fputs(dir, stdout);
}
}
void cd(int num, char *com[])
{
char pkt_data[sizeof(struct ftp_header) + DATASIZE];
struct ftp_header *send_header = (struct ftp_header *)pkt_data;
char *data = pkt_data + sizeof(struct ftp_header);
struct ftp_header recv_header;
uint16_t data_len = 0;
uint8_t type, code;
char dir[DATASIZE];
int n, len;
//len = strlen(com[1]);
strcpy(data, com[1]);
len = strlen(data);
//printf("data = %s, length = %d\n", data, len);
send_header->type = 0x03;
send_header->code = 0x00;
send_header->length = htons(len);
if (send(sd, pkt_data, sizeof(struct ftp_header) + len, 0) < 0) {
perror("send");
exit(EXIT_FAILURE);
}
if (recv(sd, &recv_header, sizeof(struct ftp_header), 0) < 0) {
perror("recv");
exit(EXIT_FAILURE);
}
if (recv_header.type == 0x10) {
puts("succesfully moved the directory in server");
}
}
void getargs(char *cp, char *argv[], int *argc)
{
char *p;
int i = 1, len, k;
argv[0] = cp;
len = strlen(cp);
*argc = 0;
for (p = cp + 1, k = 1; k < len; p++, k++) {
if ((*p != ' ') && (*(p - 1) == '\0')) {
argv[i] = p;
i++;
(*argc)++;
} else if ((*p != ' ') && (*(p - 1) != ' ')) {
continue;
} else if (*p == ' '){
*p = '\0';
}
}
}
int main (int argc, char *argv[])
{
char buf[100];
int num, n;
char *com[5];
struct sockaddr_in s_addr;
char *server_ip;
struct command_table *p;
fd_set readfds;
server_ip = argv[1];
/* create client socket */
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
exit(EXIT_FAILURE);
}
memset(&s_addr, 0, sizeof(struct sockaddr_in));
s_addr.sin_family = AF_INET;
s_addr.sin_port = htons(PORT);
s_addr.sin_addr.s_addr = inet_addr(server_ip);
/* connect to the server */
if (connect(sd, (struct sockaddr *)&s_addr, sizeof(s_addr)) < 0) {
perror("connect");
exit(EXIT_FAILURE);
}
for (;;) {
fprintf(stdout, "FTP$ ");
if (fgets(buf, sizeof(buf) - 1, stdin) == NULL) {
fprintf(stdout, "\n");
continue;
}
buf[strlen(buf) - 1] = '\0';
if (*buf == '\0')
continue;
getargs(buf, com, &num);
//printf("com[0] = %s\n", com[0]);
if (strncmp(com[0], "lcd", 3) == 0) {
chdir(com[1]);
continue;
}
for (p = cmd_tbl; p->cmd != NULL; p++) {
if (strcmp(com[0], p->cmd) == 0) {
(*p->func)(num, com);
break;
}
}
if (p->cmd == NULL)
fprintf(stderr, "nuknown command\n");
}
}