-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftpclient_v1.c
executable file
·415 lines (351 loc) · 7.42 KB
/
ftpclient_v1.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
/*FTP Client*/
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <dirent.h>
/*for getting file size using stat()*/
#include<sys/stat.h>
/*for sendfile()*/
#include<sys/sendfile.h>
/*for O_RDONLY*/
#include<fcntl.h>
#define cmdPort 21
struct sockaddr_in cmdAddr, dataAddr;
int cmdSock, dataSock;
char mode[4];
char buf[BUFSIZ+1];
char ip[16];
char localAddress[1024];
struct sockaddr_storage serverStorage;
socklen_t addr_size = sizeof serverStorage;
int listenSock;
int status;
void replylogcode(int code)
{
switch(code){
case 200:
printf("Command okay");
break;
case 500:
printf("Syntax error, command unrecognized.");
printf("This may include errors such as command line too long.");
break;
case 501:
printf("Syntax error in parameters or arguments.");
break;
case 202:
printf("Command not implemented, superfluous at this site.");
break;
case 502:
printf("Command not implemented.");
break;
case 503:
printf("Bad sequence of commands.");
break;
case 530:
printf("Not logged in.");
break;
}
printf("\n");
}
void ModePassive()
{
//Passive mode
char s1[5], s2[5];
int x1, x2, dataPort;
char* temp;
memset(buf, 0, sizeof buf);
sprintf(buf,"PASV\r\n");
send(cmdSock, buf, strlen(buf), 0);
memset(buf, 0, sizeof buf);
recv(cmdSock, buf, BUFSIZ, 0);
printf("%s", buf);
//Calc port
temp = strtok(buf,"(");
/*(int i=0;i<5;i++){
temp = strtok(NULL,",");
if (i == 4)
strcpy(s1,temp);
}*/
int i=0;
do {
temp = strtok(NULL,",");
if (i == 4)
strcpy(s1,temp);
i++;
}while(i<5);
temp = strtok(NULL,")");
strcpy(s2,temp);
x1 = atoi(s1);
x2 = atoi(s2);
dataPort = x1 * 256 + x2;
dataAddr.sin_port=htons(dataPort);
dataSock = socket(AF_INET, SOCK_STREAM, 0);
if(dataSock == -1)
{
puts("Socket creation failed");
exit(1);
}
if( connect(dataSock, (struct sockaddr *) &dataAddr, sizeof(struct sockaddr)) == -1 )
{
puts("Connect Error");
exit(1);
}
}
void ModeActive()
{
int x1, x2, dataPort;
char temp[16];
x1 = rand() % 200 + 1;
x2 = rand() % 200 + 1;
/*for(int i=0; i<16; i++)
if(ip[i] != '.')
temp[i] = ip[i];
else
temp[i] = ',';*/
int i=0;
do {
if(ip[i] != '.')
temp[i] = ip[i];
else
temp[i] = ',';
i++;
}while(i<16);
memset(buf, 0, sizeof buf);
sprintf(buf,"PORT %s,%d,%d\r\n",temp,x1,x2);
send(cmdSock, buf, strlen(buf), 0);
memset(buf, 0, sizeof buf);
recv(cmdSock, buf, BUFSIZ, 0);
puts(buf);
dataPort = x1 * 256 + x2;
printf("%d\n",dataPort);
dataAddr.sin_port=htons(dataPort);
listenSock = socket(AF_INET, SOCK_STREAM, 0);
bind(listenSock, (struct sockaddr *) &dataAddr, sizeof(struct sockaddr));
listen(listenSock,5);
}
void CheckMode()
{
if (strcmp(mode,"on") == 0)
ModePassive();
else
ModeActive();
}
void Accept()
{
if (strcmp(mode,"off") == 0)
dataSock = accept(listenSock, (struct sockaddr *) &serverStorage, &addr_size);
}
/*
char* GetValue(char* cmdLine)
{
char* value = strstr(cmdLine, " ");
if (value != NULL)
{
printf("ok\n");
value += 1;
}
printf("%s\n", cmdLine);
return value;
}
*/
void CmdLineHandle(char* cmdLine)
{
cmdLine[strlen(cmdLine) - 1] = '\0';
char* temp = strtok(cmdLine," ");
//passive
if (strcmp(cmdLine,"passive") == 0)
{
if (strcmp(mode,"off") == 0)
{
strcpy(mode,"on");
puts("Passive mode on!");
}
else {
strcpy(mode,"off");
puts("Passive mode off!");
}
}
//ls
else if (strcmp(cmdLine,"ls") == 0)
{
CheckMode();
memset(buf, 0, sizeof buf);
sprintf(buf,"LIST\r\n");
send(cmdSock, buf, strlen(buf), 0);
Accept();
memset(buf, 0, sizeof buf);
recv(cmdSock, buf, BUFSIZ, 0);
puts(buf);
memset(buf, 0, sizeof buf);
recv(cmdSock, buf, BUFSIZ, 0);
puts(buf);
memset(buf, 0, sizeof buf);
recv(dataSock, buf, BUFSIZ, 0);
puts(buf);
}
//exit
else if (strcmp(cmdLine,"exit") == 0)
{
status = 1;
memset(buf, 0, sizeof buf);
sprintf(buf,"QUIT\r\n");
send(cmdSock, buf, strlen(buf), 0);
recv(cmdSock, buf, BUFSIZ, 0);
puts(buf);
}
//cd
else if (strcmp(cmdLine, "cd") == 0)
{
memset(buf, 0, sizeof buf);
char* value = strtok(NULL, " ");
if (value != NULL)
{
sprintf(buf,"CWD %s\r\n", value);
send(cmdSock, buf, strlen(buf), 0);
recv(cmdSock, buf, BUFSIZ, 0);
puts(buf);
}
}
//delete
else if (strcmp(cmdLine, "delete") == 0)
{
memset(buf, 0, sizeof buf);
char* value = strtok(NULL, " ");
if (value != NULL)
{
sprintf(buf,"DELE %s\r\n", value);
send(cmdSock, buf, strlen(buf), 0);
recv(cmdSock, buf, BUFSIZ, 0);
puts(buf);
}
}
//mkdir
else if (strcmp(cmdLine, "mkdir") == 0)
{
memset(buf, 0, sizeof buf);
char* value = strtok(NULL, " ");
if (value != NULL)
{
sprintf(buf,"MKD %s\r\n", value);
send(cmdSock, buf, strlen(buf), 0);
recv(cmdSock, buf, BUFSIZ, 0);
puts(buf);
}
}
//lcd
else if (strcmp(cmdLine, "lcd") == 0)
{
char* value = strtok(NULL, " ");
if (value != NULL)
{
DIR* dir = opendir(value);
if (dir)
{
memset(localAddress, 0, sizeof localAddress);
strcpy(localAddress, value);
printf("Local directory: %s\n\n", localAddress);
}
else
{
printf("Failed to change local directory\n\n");
}
}
}
}
int main(int argc,char *argv[])
{
int tmpres;
char cmdLine[1024];
status = 0;
//get current directory
getcwd(localAddress, sizeof localAddress);
//Create socket
strcpy(ip,argv[1]);
cmdAddr.sin_family = AF_INET;
cmdAddr.sin_addr.s_addr=inet_addr(ip);
cmdAddr.sin_port = htons(cmdPort);
memset(cmdAddr.sin_zero, '\0', sizeof cmdAddr.sin_zero);
dataAddr.sin_family=AF_INET;
dataAddr.sin_addr.s_addr=inet_addr(ip);
memset(dataAddr.sin_zero, '\0', sizeof dataAddr.sin_zero);
cmdSock = socket(AF_INET, SOCK_STREAM, 0);
if(cmdSock == -1)
{
printf("Socket creation failed");
exit(1);
}
tmpres = connect(cmdSock,(struct sockaddr*)&cmdAddr, sizeof(struct sockaddr));
if(tmpres == -1)
{
printf("Connect Error");
exit(1);
}
char * str;
int codeftp;
printf("Connection established, waiting for welcome message...\n");
while((tmpres = recv(cmdSock, buf, BUFSIZ, 0)) > 0){
sscanf(buf,"%d", &codeftp);
printf("%s", buf);
if(codeftp != 220)
{
replylogcode(codeftp);
exit(1);
}
str = strstr(buf, "\r\n");
if(str != NULL){
break;
}
memset(buf, 0, tmpres);
}
while(true) {
//Send Username
char info[50];
printf("Name (%s): ", ip);
memset(buf, 0, sizeof buf);
fgets (info, sizeof info, stdin);
sprintf(buf,"USER %s\r\n",info);
tmpres = send(cmdSock, buf, strlen(buf), 0);
memset(buf, 0, sizeof buf);
tmpres = recv(cmdSock, buf, BUFSIZ, 0);
sscanf(buf,"%d", &codeftp);
if(codeftp != 331)
{
replylogcode(codeftp);
continue;
}
printf("%s", buf);
//Send Password
memset(info, 0, sizeof info);
printf("Password: ");
memset(buf, 0, sizeof buf);
fgets (info, sizeof info, stdin);
sprintf(buf,"PASS %s\r\n",info);
tmpres = send(cmdSock, buf, strlen(buf), 0);
memset(buf, 0, sizeof buf);
tmpres = recv(cmdSock, buf, BUFSIZ, 0);
sscanf(buf,"%d", &codeftp);
if(codeftp != 230)
{
replylogcode(codeftp);
continue;
}
printf("%s", buf);
break;
}
strcpy(mode,"off");
do {
memset(cmdLine, 0, sizeof cmdLine);
printf("ftp>");
fflush(stdin);
fgets (cmdLine, 1024, stdin);
CmdLineHandle(cmdLine);
}while(status == 0);
/*flush(stdin);
scanf("%s",cmdLine);
CmdLineHandle(cmdLine);*/
}