2006-02-21 12:28:42 +00:00
|
|
|
/* <!-- 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"
|
2006-02-21 14:00:58 +00:00
|
|
|
#include "DlRetryEx.h"
|
2006-02-21 12:28:42 +00:00
|
|
|
#include "message.h"
|
2006-02-21 14:00:58 +00:00
|
|
|
#include "prefs.h"
|
2006-02-21 12:28:42 +00:00
|
|
|
|
2006-07-19 17:07:45 +00:00
|
|
|
FtpNegotiationCommand::FtpNegotiationCommand(int cuid, Request* req,
|
|
|
|
DownloadEngine* e,
|
|
|
|
const SocketHandle& s):
|
|
|
|
AbstractCommand(cuid, req, e, s), sequence(SEQ_RECV_GREETING)
|
2006-02-21 12:28:42 +00:00
|
|
|
{
|
2006-04-17 16:17:20 +00:00
|
|
|
ftp = new FtpConnection(cuid, socket, req, e->option);
|
2006-07-19 17:07:45 +00:00
|
|
|
disableReadCheckSocket();
|
2006-02-21 12:28:42 +00:00
|
|
|
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) {
|
2006-07-19 17:07:45 +00:00
|
|
|
FtpDownloadCommand* command =
|
|
|
|
new FtpDownloadCommand(cuid, req, e, dataSocket, socket);
|
2006-05-09 15:54:14 +00:00
|
|
|
e->commands.push_back(command);
|
2006-02-21 12:28:42 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
2006-05-09 15:54:14 +00:00
|
|
|
e->commands.push_back(this);
|
2006-02-21 12:28:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtpNegotiationCommand::recvGreeting() {
|
|
|
|
socket->setBlockingMode();
|
|
|
|
int status = ftp->receiveResponse();
|
|
|
|
if(status == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(status != 220) {
|
2006-02-21 14:00:58 +00:00
|
|
|
throw new DlRetryEx(EX_CONNECTION_FAILED);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
sequence = SEQ_SEND_USER;
|
|
|
|
|
|
|
|
setReadCheckSocket(socket);
|
2006-07-19 17:07:45 +00:00
|
|
|
disableWriteCheckSocket();
|
2006-02-21 12:28:42 +00:00
|
|
|
|
|
|
|
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:
|
2006-02-21 14:00:58 +00:00
|
|
|
throw new DlRetryEx(EX_BAD_STATUS, status);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
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) {
|
2006-02-21 14:00:58 +00:00
|
|
|
throw new DlRetryEx(EX_BAD_STATUS, status);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
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) {
|
2006-02-21 14:00:58 +00:00
|
|
|
throw new DlRetryEx(EX_BAD_STATUS, status);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
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) {
|
2006-02-21 14:00:58 +00:00
|
|
|
throw new DlRetryEx(EX_BAD_STATUS, status);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
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) {
|
2006-02-21 14:00:58 +00:00
|
|
|
throw new DlRetryEx(EX_BAD_STATUS, status);
|
|
|
|
}
|
|
|
|
if(size == LONG_LONG_MAX || size < 0) {
|
|
|
|
throw new DlAbortEx(EX_TOO_LARGE_FILE, size);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
2006-02-21 14:00:58 +00:00
|
|
|
if(e->option->get(PREF_FTP_PASV_ENABLED) == V_TRUE) {
|
2006-02-21 12:28:42 +00:00
|
|
|
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) {
|
2006-02-21 14:00:58 +00:00
|
|
|
throw new DlRetryEx(EX_BAD_STATUS, status);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
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) {
|
2006-02-21 14:00:58 +00:00
|
|
|
throw new DlRetryEx(EX_BAD_STATUS, status);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
// make a data connection to the server.
|
2006-04-17 16:17:20 +00:00
|
|
|
logger->info(MSG_CONNECTING_TO_SERVER, cuid,
|
|
|
|
dest.first.c_str(),
|
|
|
|
dest.second);
|
2006-02-21 12:28:42 +00:00
|
|
|
dataSocket->establishConnection(dest.first, dest.second);
|
|
|
|
|
2006-07-19 17:07:45 +00:00
|
|
|
disableReadCheckSocket();
|
2006-02-21 12:28:42 +00:00
|
|
|
setWriteCheckSocket(dataSocket);
|
|
|
|
|
|
|
|
sequence = SEQ_SEND_REST_PASV;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtpNegotiationCommand::sendRestPasv(const Segment& segment) {
|
|
|
|
dataSocket->setBlockingMode();
|
|
|
|
setReadCheckSocket(socket);
|
2006-07-19 17:07:45 +00:00
|
|
|
disableWriteCheckSocket();
|
2006-02-21 12:28:42 +00:00
|
|
|
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) {
|
2006-02-21 14:00:58 +00:00
|
|
|
throw new DlRetryEx(EX_BAD_STATUS, status);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
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) {
|
2006-02-21 14:00:58 +00:00
|
|
|
throw new DlRetryEx(EX_BAD_STATUS, status);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
2006-02-21 14:00:58 +00:00
|
|
|
if(e->option->get(PREF_FTP_PASV_ENABLED) != V_TRUE) {
|
2006-07-19 17:07:45 +00:00
|
|
|
assert(serverSocket->getSockfd() != -1);
|
2006-02-21 12:28:42 +00:00
|
|
|
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;
|
|
|
|
}
|