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
|
/* <!-- copyright */
/*
* aria2 - a simple utility for downloading files faster
*
* Copyright (C) 2006 Tatsuhiro Tsujikawa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* copyright --> */
#include "FtpNegotiationCommand.h"
#include "FtpDownloadCommand.h"
#include "DlAbortEx.h"
#include "DlRetryEx.h"
#include "message.h"
#include "prefs.h"
FtpNegotiationCommand::FtpNegotiationCommand(int cuid, Request* req,
DownloadEngine* e,
const SocketHandle& s):
AbstractCommand(cuid, req, e, s), sequence(SEQ_RECV_GREETING)
{
ftp = new FtpConnection(cuid, socket, req, e->option);
disableReadCheckSocket();
setWriteCheckSocket(socket);
}
FtpNegotiationCommand::~FtpNegotiationCommand() {
delete ftp;
}
bool FtpNegotiationCommand::executeInternal(Segment segment) {
while(processSequence(segment));
if(sequence == SEQ_RETRY) {
return prepareForRetry(0);
} else if(sequence == SEQ_NEGOTIATION_COMPLETED) {
FtpDownloadCommand* command =
new FtpDownloadCommand(cuid, req, e, dataSocket, socket);
e->commands.push_back(command);
return true;
} else {
e->commands.push_back(this);
return false;
}
}
bool FtpNegotiationCommand::recvGreeting() {
socket->setBlockingMode();
int status = ftp->receiveResponse();
if(status == 0) {
return false;
}
if(status != 220) {
throw new DlRetryEx(EX_CONNECTION_FAILED);
}
sequence = SEQ_SEND_USER;
setReadCheckSocket(socket);
disableWriteCheckSocket();
return true;
}
bool FtpNegotiationCommand::sendUser() {
ftp->sendUser();
sequence = SEQ_RECV_USER;
return false;
}
bool FtpNegotiationCommand::recvUser() {
int status = ftp->receiveResponse();
switch(status) {
case 0:
return false;
case 230:
sequence = SEQ_SEND_TYPE;
break;
case 331:
sequence = SEQ_SEND_PASS;
break;
default:
throw new DlRetryEx(EX_BAD_STATUS, status);
}
return true;
}
bool FtpNegotiationCommand::sendPass() {
ftp->sendPass();
sequence = SEQ_RECV_PASS;
return false;
}
bool FtpNegotiationCommand::recvPass() {
int status = ftp->receiveResponse();
if(status == 0) {
return false;
}
if(status != 230) {
throw new DlRetryEx(EX_BAD_STATUS, status);
}
sequence = SEQ_SEND_TYPE;
return true;
}
bool FtpNegotiationCommand::sendType() {
ftp->sendType();
sequence = SEQ_RECV_TYPE;
return false;
}
bool FtpNegotiationCommand::recvType() {
int status = ftp->receiveResponse();
if(status == 0) {
return false;
}
if(status != 200) {
throw new DlRetryEx(EX_BAD_STATUS, status);
}
sequence = SEQ_SEND_CWD;
return true;
}
bool FtpNegotiationCommand::sendCwd() {
ftp->sendCwd();
sequence = SEQ_RECV_CWD;
return false;
}
bool FtpNegotiationCommand::recvCwd() {
int status = ftp->receiveResponse();
if(status == 0) {
return false;
}
if(status != 250) {
throw new DlRetryEx(EX_BAD_STATUS, status);
}
sequence = SEQ_SEND_SIZE;
return true;
}
bool FtpNegotiationCommand::sendSize() {
ftp->sendSize();
sequence = SEQ_RECV_SIZE;
return false;
}
bool FtpNegotiationCommand::recvSize() {
long long int size = 0;
int status = ftp->receiveSizeResponse(size);
if(status == 0) {
return false;
}
if(status != 213) {
throw new DlRetryEx(EX_BAD_STATUS, status);
}
if(size == LONG_LONG_MAX || size < 0) {
throw new DlAbortEx(EX_TOO_LARGE_FILE, size);
}
if(!e->segmentMan->downloadStarted) {
e->segmentMan->downloadStarted = true;
e->segmentMan->totalSize = size;
} else if(e->segmentMan->totalSize != size) {
throw new DlAbortEx(EX_SIZE_MISMATCH, e->segmentMan->totalSize, size);
}
if(e->option->get(PREF_FTP_PASV_ENABLED) == V_TRUE) {
sequence = SEQ_SEND_PASV;
} else {
sequence = SEQ_SEND_PORT;
}
return true;
}
bool FtpNegotiationCommand::sendPort() {
serverSocket = ftp->sendPort();
sequence = SEQ_RECV_PORT;
return false;
}
bool FtpNegotiationCommand::recvPort() {
int status = ftp->receiveResponse();
if(status == 0) {
return false;
}
if(status != 200) {
throw new DlRetryEx(EX_BAD_STATUS, status);
}
sequence = SEQ_SEND_REST;
return true;
}
bool FtpNegotiationCommand::sendPasv() {
ftp->sendPasv();
sequence = SEQ_RECV_PASV;
return false;
}
bool FtpNegotiationCommand::recvPasv() {
pair<string, int> dest;
int status = ftp->receivePasvResponse(dest);
if(status == 0) {
return false;
}
if(status != 227) {
throw new DlRetryEx(EX_BAD_STATUS, status);
}
// make a data connection to the server.
logger->info(MSG_CONNECTING_TO_SERVER, cuid,
dest.first.c_str(),
dest.second);
dataSocket->establishConnection(dest.first, dest.second);
disableReadCheckSocket();
setWriteCheckSocket(dataSocket);
sequence = SEQ_SEND_REST_PASV;
return false;
}
bool FtpNegotiationCommand::sendRestPasv(const Segment& segment) {
dataSocket->setBlockingMode();
setReadCheckSocket(socket);
disableWriteCheckSocket();
return sendRest(segment);
}
bool FtpNegotiationCommand::sendRest(const Segment& segment) {
ftp->sendRest(segment);
sequence = SEQ_RECV_REST;
return false;
}
bool FtpNegotiationCommand::recvRest() {
int status = ftp->receiveResponse();
if(status == 0) {
return false;
}
// TODO if we recieve negative response, then we set e->segmentMan->splittable = false, and continue.
if(status != 350) {
throw new DlRetryEx(EX_BAD_STATUS, status);
}
sequence = SEQ_SEND_RETR;
return true;
}
bool FtpNegotiationCommand::sendRetr() {
ftp->sendRetr();
sequence = SEQ_RECV_RETR;
return false;
}
bool FtpNegotiationCommand::recvRetr() {
int status = ftp->receiveResponse();
if(status == 0) {
return false;
}
if(status != 150) {
throw new DlRetryEx(EX_BAD_STATUS, status);
}
if(e->option->get(PREF_FTP_PASV_ENABLED) != V_TRUE) {
assert(serverSocket->getSockfd() != -1);
dataSocket = serverSocket->acceptConnection();
}
sequence = SEQ_NEGOTIATION_COMPLETED;
return false;
}
bool FtpNegotiationCommand::processSequence(const Segment& segment) {
bool doNextSequence = true;
switch(sequence) {
case SEQ_RECV_GREETING:
return recvGreeting();
case SEQ_SEND_USER:
return sendUser();
case SEQ_RECV_USER:
return recvUser();
case SEQ_SEND_PASS:
return sendPass();
case SEQ_RECV_PASS:
return recvPass();
case SEQ_SEND_TYPE:
return sendType();
case SEQ_RECV_TYPE:
return recvType();
case SEQ_SEND_CWD:
return sendCwd();
case SEQ_RECV_CWD:
return recvCwd();
case SEQ_SEND_SIZE:
return sendSize();
case SEQ_RECV_SIZE:
return recvSize();
case SEQ_SEND_PORT:
return sendPort();
case SEQ_RECV_PORT:
return recvPort();
case SEQ_SEND_PASV:
return sendPasv();
case SEQ_RECV_PASV:
return recvPasv();
case SEQ_SEND_REST_PASV:
return sendRestPasv(segment);
case SEQ_SEND_REST:
return sendRest(segment);
case SEQ_RECV_REST:
return recvRest();
case SEQ_SEND_RETR:
return sendRetr();
case SEQ_RECV_RETR:
return recvRetr();
default:
abort();
}
return doNextSequence;
}
|