-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFtpClient.sc
216 lines (173 loc) · 6.88 KB
/
FtpClient.sc
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
import "mysocket.sc"
[const]
HELP_STR: string := "Command provided: get/put/pwd/dir/cd/quit/?\n"
class FtpClient
[static, const]
CMD_BUF_LEN := 128
sock: TcpSocket
pwd: string
cmd_buf: char[CMD_BUF_LEN]
cmd_args: string[3]
cmd_argc: int
void parse(cmd: string)
cmd_argc = 0
st := 0
for i <- 0 to cmd.length(), cmd[i]==' ' or cmd[i]=='\n'
cmd_args[cmd_argc++] = cmd.substr(st,i-st)
st = i + 1
if cmd_argc==3
return
if cmd[cmd.length()-1] != '\n'
cmd_args[cmd_argc++] = cmd.substr(st,cmd.length()-st)
string readResponse(res_code: int*) = sock.readLine(res_code)
int sendCommand(cmd: const char*)
printf("Command: %s", cmd)
return sock.send(cmd, strlen(cmd))
TcpSocket enterPassiveMode()
sendCommand("PASV\r\n")
res_code: int
res := readResponse(&res_code);
printf("%s", res.c_str())
if res_code!=-1 and res[0]=='2'
st := res.find('(') + 1
ed := st
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
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);
sock: TcpSocket
sock.connectToServ(ip.c_str(),port)
return sock
else
printf("Error!")
exit(0)
[public]
int connectToHost(ip: const char*, port: int, username: const char*, password: const char*)
if sock.connectToServ(ip, port) < 0
perror("Can't connect to ftp server\n")
return -1
res_code: int
response := readResponse(&res_code)
sprintf(cmd_buf, "USER %s\r\n", username)
sendCommand(cmd_buf)
response = readResponse(&res_code)
if response[0] != '3'
return -1
sprintf(cmd_buf, "PASS %s\r\n", password)
sendCommand(cmd_buf)
response = readResponse(&res_code)
if response[0] != '2'
printf("Login failed\n")
return -1
return 0
[public]
void processInput()
loop
printf("FTP> ")
line: string
if not getline(cin, line) then break
parse(line)
res_code: int
switch
when cmd_args[0] == "pwd"
sendCommand("PWD\r\n")
res := readResponse(&res_code)
printf("%s", res.c_str())
when cmd_args[0] == "dir"
tmpSocket := @enterPassiveMode()
sendCommand("LIST\r\n")
res := readResponse(&res_code)
printf("%s", res.c_str())
loop
res = tmpSocket.readLine(&res_code);
printf("%s", res.c_str())
if res_code < 0 then break
res = readResponse(&res_code)
printf("\n%s", res.c_str())
when cmd_args[0] == "cd"
sprintf(cmd_buf,"CWD %s\r\n",cmd_args[1].c_str())
sendCommand(cmd_buf)
res := readResponse(&res_code)
printf("Response: %s", res.c_str())
when cmd_args[0] == "delete"
sprintf(cmd_buf,"DELE %s\r\n",cmd_args[1].c_str())
sendCommand(cmd_buf)
printf("Response: %s", readResponse(&res_code).c_str())
when cmd_args[0] == "mkdir"
sprintf(cmd_buf,"MKD %s\r\n",cmd_args[1].c_str())
sendCommand(cmd_buf)
printf("Response: %s", readResponse(&res_code).c_str())
when cmd_args[0] == "rename"
sprintf(cmd_buf,"RNFR %s\r\n",cmd_args[1].c_str())
sendCommand(cmd_buf)
res := readResponse(&res_code)
printf("%s", res.c_str())
if res[0] == '3'
sprintf(cmd_buf,"RNTO %s\r\n",cmd_args[2].c_str());
sendCommand(cmd_buf)
printf("%s", readResponse(&res_code).c_str())
when cmd_args[0] == "get"
tmpSocket := enterPassiveMode()
file_name := cmd_args[1].c_str();
sprintf(cmd_buf,"RETR %s\r\n", file_name)
sendCommand(cmd_buf)
res := readResponse(&res_code)
printf("%s", res.c_str())
if res[0] == '1'
printf("Receving file %s\n",file_name)
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)
printf("%s", res.c_str())
tmpSocket.close()
when cmd_args[0] == "put"
tmpSocket := @enterPassiveMode()
file_name := cmd_args[1].c_str()
remote_file_name := cmd_args[2].c_str()
sprintf(cmd_buf,"STOR %s\r\n", remote_file_name)
sendCommand(cmd_buf)
res := readResponse(&res_code)
printf("%s", res.c_str())
if res[0] == '1'
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)
printf("%s", res.c_str())
when cmd_args[0]=="quit"
sock.sendString("QUIT\r\n")
sock.close()
exit(0)
when cmd_args[0]=="?"
printf("%s", HELP_STR.c_str())
else
printf("wrong command\n")
int main(argc: int, argv: char**)
if argc < 4
printf("usage: exe [port] [username] [password]\n")
return 0
client: FtpClient
if client.connectToHost("127.0.0.1",atoi(argv[1]),argv[2],argv[3]) < 0
return 0
client.processInput()