-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFtpClient.cpp
203 lines (186 loc) · 5.64 KB
/
FtpClient.cpp
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
#include "FtpClient.h"
string FtpClient::readResponse(int* res_code){
return sock.readLine(res_code);
}
int FtpClient::sendCommand(const char* cmd){
printf("Command:%s",cmd);
return sock.send(cmd,strlen(cmd));
}
int FtpClient::connectToHost(const char* ip,int port,const char* uname,const char* passwd){
string response;
int res_code;
if(sock.connectToServ(ip,port)<0){
perror("Can't connect to ftp server\n");
return -1;
}
response = readResponse(&res_code);
sprintf(cmd_buf,"USER %s\r\n",uname);
sendCommand(cmd_buf);
response = readResponse(&res_code);
if(response[0] != '3'){
return -1;
}
sprintf(cmd_buf,"PASS %s\r\n",passwd);
sendCommand(cmd_buf);
response = readResponse(&res_code);
if(response[0] != '2' ){
printf("Login failed\n");
return -1;
}
return 0;
}
const char* helpStr = "Command provided:"
"get\n"
"put\n"
"pwd\n"
"dir\n"
"cd\n"
"?"
"quit";
int FtpClient::processInput(){
string line;
cout<<"FTP< ";
while(getline(cin,line)){
string res;
int res_code;
parse(line);
if(cmd_args[0]=="pwd"){
sendCommand("PWD\r\n");
res = readResponse(&res_code);
cout<<res;
} else if(cmd_args[0]=="dir"){
TcpSocket tmpSock = enterPassiveMode();
sendCommand("LIST\r\n");
res = readResponse(&res_code);
cout<<res<<endl;
while(true){
res = tmpSock.readLine(&res_code);
cout<<res;
if(res_code<0)
break;
}
res = readResponse(&res_code);
cout<<endl<<res;
} else if(cmd_args[0]=="cd"){
sprintf(cmd_buf,"CWD %s\r\n",cmd_args[1].c_str());
sendCommand(cmd_buf);
res = readResponse(&res_code);
cout<<res;
} else if(cmd_args[0]=="delete"){
sprintf(cmd_buf,"DELE %s\r\n",cmd_args[1].c_str());
sendCommand(cmd_buf);
cout<<readResponse(&res_code);
} else if(cmd_args[0]=="mkdir"){
sprintf(cmd_buf,"MKD %s\r\n",cmd_args[1].c_str());
sendCommand(cmd_buf);
cout<<readResponse(&res_code);
} else if(cmd_args[0]=="rename"){
sprintf(cmd_buf,"RNFR %s\r\n",cmd_args[1].c_str());
sendCommand(cmd_buf);
res = readResponse(&res_code);
cout<<res;
if(res[0]=='3'){
sprintf(cmd_buf,"RNTO %s\r\n",cmd_args[2].c_str());
sendCommand(cmd_buf);
cout<<readResponse(&res_code);
}
} else if(cmd_args[0]=="get"){
TcpSocket tmpSocket = enterPassiveMode();
const char* file_name = cmd_args[1].c_str();
sprintf(cmd_buf,"RETR %s\r\n",file_name);
sendCommand(cmd_buf);
res = readResponse(&res_code);
cout<<res;
if(res[0]=='1'){
printf("Receving file %s\n",file_name);
int total_size = tmpSocket.recvFile(file_name);
if(total_size>=0){
printf("Receive %d bytes\n",total_size);
} else {
printf("Can't open file %s for writing!\n",file_name);
}
res = readResponse(&res_code);
cout<<res;
}
tmpSocket.close();
} else if(cmd_args[0]=="put"){
TcpSocket tmpSocket = enterPassiveMode();
const char* file_name = cmd_args[1].c_str();
sprintf(cmd_buf,"STOR %s\r\n",file_name);
sendCommand(cmd_buf);
res = readResponse(&res_code);
cout<<res;
if(res[0]=='1'){
int total_size = tmpSocket.sendFile(file_name);
if(total_size>=0){
printf("Transfer %d bytes\n",total_size);
} else {
printf("Can't open file %s for reading!\n",file_name);
}
tmpSocket.close();
res = readResponse(&res_code);
cout<<res;
}
} else if(cmd_args[0]=="quit"){
sock.sendString("QUIT\r\n");
sock.close();
exit(0);
} else if(cmd_args[0]=="?"){
printf(helpStr);
} else {
cout<<"wrong command"<<endl;
}
cout<<endl<<"FTP< ";
}
return 0;
}
TcpSocket FtpClient::enterPassiveMode(){
sendCommand("PASV\r\n");
string ip;
int port;
string res;
int res_code;
res = readResponse(&res_code);
cout<<res;
if(res_code!=-1 && res[0]=='2'){
int st = res.find('(')+1;
int ed = st;
int dotMeeted=0;
while(dotMeeted<4){
++ed;
if(res[ed]==','){
res[ed]='.';
++dotMeeted;
}
}
++ed;
ip = res.substr(st,ed-st-1);
port = 0;
while(res[ed]!=','){
port = (port) * 10 + res[ed]-'0';
++ed;
}
++ed;
int lowpart = 0;
while(res[ed]!=')'){
lowpart = lowpart * 10 + res[ed]-'0';
++ed;
}
port = (port<<8)+lowpart;
printf("passive ip:%s port:%d\n",ip.c_str(),port);
TcpSocket sock;
sock.connectToServ(ip.c_str(),port);
return sock;
}
}
int main(int argc,char* argv[]){
if(argc<4){
printf("usage: exe [port] [username] [password]\n");
return 0;
}
FtpClient c;
if(c.connectToHost("127.0.0.1",atoi(argv[1]),argv[2],argv[3])<0){
return 0;
}
c.processInput();
}