2006-02-21 12:28:42 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
2006-09-21 15:31:24 +00:00
|
|
|
* aria2 - The high speed download utility
|
2006-02-21 12:28:42 +00:00
|
|
|
*
|
|
|
|
* 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
|
2006-09-21 15:31:24 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give
|
|
|
|
* permission to link the code of portions of this program with the
|
|
|
|
* OpenSSL library under certain conditions as described in each
|
|
|
|
* individual source file, and distribute linked combinations
|
|
|
|
* including the two.
|
|
|
|
* You must obey the GNU General Public License in all respects
|
|
|
|
* for all of the code used other than OpenSSL. If you modify
|
|
|
|
* file(s) with this exception, you may extend this exception to your
|
|
|
|
* version of the file(s), but you are not obligated to do so. If you
|
|
|
|
* do not wish to do so, delete this exception statement from your
|
|
|
|
* version. If you delete this exception statement from all source
|
|
|
|
* files in the program, then also delete it here.
|
2006-02-21 12:28:42 +00:00
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "FtpConnection.h"
|
2008-11-03 10:06:25 +00:00
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cassert>
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Request.h"
|
|
|
|
#include "Segment.h"
|
|
|
|
#include "Option.h"
|
2006-02-21 12:28:42 +00:00
|
|
|
#include "Util.h"
|
|
|
|
#include "message.h"
|
2006-02-21 14:00:58 +00:00
|
|
|
#include "prefs.h"
|
2006-04-17 16:17:20 +00:00
|
|
|
#include "LogFactory.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Logger.h"
|
2007-10-30 12:48:01 +00:00
|
|
|
#include "AuthConfigFactory.h"
|
|
|
|
#include "AuthConfig.h"
|
2007-11-10 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Don't connect server before checking file integrity at startup,
if
filesize and output file path are known.
* src/AbstractCommand.cc
* src/StreamFileAllocationEntry.cc
* src/Metalink2RequestGroup.cc
* src/RequestGroup.{h, cc}
* src/HttpResponseCommand.cc
* src/FtpNegotiationCommand.cc
Added DownloadFailureException. If it is thrown, RequestGroup
should
halt.
* src/AbstractCommand.cc
* src/DownloadFailureException.h
* src/RequestGroup.cc
Catch RecoverableException, instead of DlAbortEx.
* src/RequestGroupMan.cc
* src/FillRequestGroupCommand.cc
* src/MetaFileUtil.cc
* src/IteratableChunkChecksumValidator.cc
Now first parameter of MSG_DOWNLOAD_ABORTED is
gid(RequestGroup::
getGID())
* src/CheckIntegrityCommand.cc
* src/message.h
Print gid instead of idx.
* src/RequestGroupMan.cc
Removed exception throwers declaration.
* src/DirectDiskAdaptor.{h, cc}
* src/SocketCore.{h, cc}
* src/MultiDiskAdaptor.{h, cc}
* src/HttpConnection.{h, cc}
* src/HttpResponse.{h, cc}
* src/DiskAdaptor.{h, cc}
* src/CopyDiskAdaptor.{h, cc}
* src/MultiDiskAdaptor.{h, cc}
* src/HttpHeaderProcessor.{h, cc}
* src/AbstractSingleDiskAdaptor.{h, cc}
* src/Util.{h, cc}
* test/UtilTest.cc
* src/DefaultDiskWriter.{h, cc}
* src/FtpConnection.{h, cc}
* src/AbstractDiskWriter.{h, cc}
Removed duplicate code.
* src/StreamCheckIntegrityEntry.cc
Removed unnecessary include.
* src/DiskWriter.h
Included Exception.h
* src/option_processing.cc
Included 2 files and added doc
* src/TrackerWatcherCommand.cc
* src/SocketCore.cc (writeData): Fixed send error with GnuTLS.
2007-11-09 18:01:12 +00:00
|
|
|
#include "DlRetryEx.h"
|
2007-12-04 11:38:06 +00:00
|
|
|
#include "DlAbortEx.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
#include "Socket.h"
|
2008-05-14 13:52:47 +00:00
|
|
|
#include "A2STR.h"
|
2008-09-14 12:51:30 +00:00
|
|
|
#include "StringFormat.h"
|
2008-11-03 10:06:25 +00:00
|
|
|
#include "AuthConfig.h"
|
2008-02-08 15:53:45 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
2006-02-21 12:28:42 +00:00
|
|
|
|
2008-05-14 13:52:47 +00:00
|
|
|
const std::string FtpConnection::A("A");
|
|
|
|
|
|
|
|
const std::string FtpConnection::I("I");
|
|
|
|
|
2007-07-21 08:56:16 +00:00
|
|
|
FtpConnection::FtpConnection(int32_t cuid, const SocketHandle& socket,
|
2008-11-03 10:06:25 +00:00
|
|
|
const RequestHandle& req,
|
|
|
|
const SharedHandle<AuthConfig>& authConfig,
|
|
|
|
const Option* op):
|
|
|
|
cuid(cuid), socket(socket), req(req),
|
|
|
|
_authConfig(authConfig), option(op),
|
2008-09-13 16:32:47 +00:00
|
|
|
logger(LogFactory::getInstance()),
|
2008-09-24 17:01:57 +00:00
|
|
|
_socketBuffer(socket),
|
|
|
|
_baseWorkingDir("/") {}
|
2006-02-21 12:28:42 +00:00
|
|
|
|
|
|
|
FtpConnection::~FtpConnection() {}
|
|
|
|
|
2008-09-13 16:32:47 +00:00
|
|
|
bool FtpConnection::sendUser()
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2008-09-13 16:32:47 +00:00
|
|
|
if(_socketBuffer.sendBufferIsEmpty()) {
|
2008-11-03 10:06:25 +00:00
|
|
|
std::string request = "USER "+_authConfig->getUser()+"\r\n";
|
2008-09-13 16:32:47 +00:00
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, "USER ********");
|
|
|
|
_socketBuffer.feedSendBuffer(request);
|
|
|
|
}
|
|
|
|
_socketBuffer.send();
|
|
|
|
return _socketBuffer.sendBufferIsEmpty();
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
|
2008-09-13 16:32:47 +00:00
|
|
|
bool FtpConnection::sendPass()
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2008-09-13 16:32:47 +00:00
|
|
|
if(_socketBuffer.sendBufferIsEmpty()) {
|
2008-11-03 10:06:25 +00:00
|
|
|
std::string request = "PASS "+_authConfig->getPassword()+"\r\n";
|
2008-09-13 16:32:47 +00:00
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, "PASS ********");
|
|
|
|
_socketBuffer.feedSendBuffer(request);
|
|
|
|
}
|
|
|
|
_socketBuffer.send();
|
|
|
|
return _socketBuffer.sendBufferIsEmpty();
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
|
2008-09-13 16:32:47 +00:00
|
|
|
bool FtpConnection::sendType()
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2008-09-13 16:32:47 +00:00
|
|
|
if(_socketBuffer.sendBufferIsEmpty()) {
|
|
|
|
std::string type;
|
|
|
|
if(option->get(PREF_FTP_TYPE) == V_ASCII) {
|
|
|
|
type = FtpConnection::A;
|
|
|
|
} else {
|
|
|
|
type = FtpConnection::I;
|
|
|
|
}
|
|
|
|
std::string request = "TYPE "+type+"\r\n";
|
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
|
|
|
|
_socketBuffer.feedSendBuffer(request);
|
2006-02-21 14:00:58 +00:00
|
|
|
}
|
2008-09-13 16:32:47 +00:00
|
|
|
_socketBuffer.send();
|
|
|
|
return _socketBuffer.sendBufferIsEmpty();
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
|
2008-09-24 17:01:57 +00:00
|
|
|
bool FtpConnection::sendPwd()
|
|
|
|
{
|
|
|
|
if(_socketBuffer.sendBufferIsEmpty()) {
|
|
|
|
std::string request = "PWD\r\n";
|
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
|
|
|
|
_socketBuffer.feedSendBuffer(request);
|
|
|
|
}
|
|
|
|
_socketBuffer.send();
|
|
|
|
return _socketBuffer.sendBufferIsEmpty();
|
|
|
|
}
|
|
|
|
|
2008-09-13 16:32:47 +00:00
|
|
|
bool FtpConnection::sendCwd()
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2008-09-13 16:32:47 +00:00
|
|
|
if(_socketBuffer.sendBufferIsEmpty()) {
|
2008-09-24 17:01:57 +00:00
|
|
|
logger->info("CUID#%d - Using base working directory '%s'",
|
|
|
|
cuid, _baseWorkingDir.c_str());
|
|
|
|
std::string request = "CWD "+
|
|
|
|
(_baseWorkingDir == "/" ? "" : _baseWorkingDir)+
|
|
|
|
Util::urldecode(req->getDir())+"\r\n";
|
2008-09-13 16:32:47 +00:00
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
|
|
|
|
_socketBuffer.feedSendBuffer(request);
|
|
|
|
}
|
|
|
|
_socketBuffer.send();
|
|
|
|
return _socketBuffer.sendBufferIsEmpty();
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
|
2008-09-13 16:32:47 +00:00
|
|
|
bool FtpConnection::sendMdtm()
|
2008-09-08 13:06:44 +00:00
|
|
|
{
|
2008-09-13 16:32:47 +00:00
|
|
|
if(_socketBuffer.sendBufferIsEmpty()) {
|
|
|
|
std::string request = "MDTM "+Util::urlencode(req->getFile())+"\r\n";
|
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
|
|
|
|
_socketBuffer.feedSendBuffer(request);
|
|
|
|
}
|
|
|
|
_socketBuffer.send();
|
|
|
|
return _socketBuffer.sendBufferIsEmpty();
|
2008-09-08 13:06:44 +00:00
|
|
|
}
|
|
|
|
|
2008-09-13 16:32:47 +00:00
|
|
|
bool FtpConnection::sendSize()
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2008-09-13 16:32:47 +00:00
|
|
|
if(_socketBuffer.sendBufferIsEmpty()) {
|
|
|
|
std::string request = "SIZE "+Util::urldecode(req->getFile())+"\r\n";
|
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
|
|
|
|
_socketBuffer.feedSendBuffer(request);
|
|
|
|
}
|
|
|
|
_socketBuffer.send();
|
|
|
|
return _socketBuffer.sendBufferIsEmpty();
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
|
2008-09-13 16:32:47 +00:00
|
|
|
bool FtpConnection::sendPasv()
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2008-09-13 16:32:47 +00:00
|
|
|
if(_socketBuffer.sendBufferIsEmpty()) {
|
|
|
|
static const std::string request("PASV\r\n");
|
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
|
|
|
|
_socketBuffer.feedSendBuffer(request);
|
|
|
|
}
|
|
|
|
_socketBuffer.send();
|
|
|
|
return _socketBuffer.sendBufferIsEmpty();
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
|
2008-09-13 16:32:47 +00:00
|
|
|
SharedHandle<SocketCore> FtpConnection::createServerSocket()
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2008-09-13 16:32:47 +00:00
|
|
|
SharedHandle<SocketCore> serverSocket(new SocketCore());
|
2008-02-01 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added DHT functionality, compatible with mainline.
DHT is disabled by default. To enable it, give --enable-dht to
aria2c.
You may need to specify entry point to DHT network using
--dht-entry-point. DHT uses UDP port to listen incoming message.
Use --dht-listen-port to specify port number. Make sure that
your
firewall configuration can pass through UDP traffic to the port.
The routing table is saved in $HOME/.aria2/dht.dat.
* src/DHT*
* src/BNode.{h, cc}
* src/PeerInteractionCommand.cc: enable DHT functionality for a
particular torrent.
* src/Data.cc: Rewritten ctor.
* src/OptionHandlerFactory.cc: Added --enable-dht,
--dht-listen-port,
--dht-entry-point.
* src/DefaultBtInteractive.cc: Send port message if dht is
enabled.
* src/RequestGroup.cc: Initialize DHT functionality. When
download
ends, remove BtContext from DHTPeerAnnounceStorage.
* src/BtPortMessage.{h, cc}: Rewritten.
* src/message.h
* src/OptionHandlerImpl.cc
* src/option_processing.cc: Added --enable-dht,
--dht-listen-port,
--dht-entry-point.
* src/Dictionary.{h, cc} (remove): New function.
* src/prefs.h
* src/DefaultBtMessageFactory.h
* src/BtHandshakeMessage.cc
* src/ActivePeerConnectionCommand.cc
* src/SocketCore.{h, cc}: Added datagram socket support.
* src/DefaultBtMessageFactory.cc
* src/BtSetup.cc: Add BtContext to DHTPeerAnnounceStorage here.
Create DHT commands.
* src/BtMessageFactory.h
* src/PeerMessageUtil.{h, cc}
2008-02-01 17:36:33 +00:00
|
|
|
serverSocket->bind(0);
|
2006-07-19 17:07:45 +00:00
|
|
|
serverSocket->beginListen();
|
2008-02-19 13:36:39 +00:00
|
|
|
serverSocket->setNonBlockingMode();
|
2006-02-21 12:28:42 +00:00
|
|
|
return serverSocket;
|
|
|
|
}
|
|
|
|
|
2008-09-13 16:32:47 +00:00
|
|
|
bool FtpConnection::sendPort(const SharedHandle<SocketCore>& serverSocket)
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2008-09-13 16:32:47 +00:00
|
|
|
if(_socketBuffer.sendBufferIsEmpty()) {
|
|
|
|
std::pair<std::string, uint16_t> addrinfo;
|
|
|
|
socket->getAddrInfo(addrinfo);
|
|
|
|
unsigned int ipaddr[4];
|
|
|
|
sscanf(addrinfo.first.c_str(), "%u.%u.%u.%u",
|
|
|
|
&ipaddr[0], &ipaddr[1], &ipaddr[2], &ipaddr[3]);
|
|
|
|
serverSocket->getAddrInfo(addrinfo);
|
|
|
|
std::string request = "PORT "+
|
|
|
|
Util::uitos(ipaddr[0])+","+Util::uitos(ipaddr[1])+","+
|
|
|
|
Util::uitos(ipaddr[2])+","+Util::uitos(ipaddr[3])+","+
|
|
|
|
Util::uitos(addrinfo.second/256)+","+
|
|
|
|
Util::uitos(addrinfo.second%256)+"\r\n";
|
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
|
|
|
|
_socketBuffer.feedSendBuffer(request);
|
2008-06-24 14:31:23 +00:00
|
|
|
}
|
2008-09-13 16:32:47 +00:00
|
|
|
_socketBuffer.send();
|
|
|
|
return _socketBuffer.sendBufferIsEmpty();
|
|
|
|
}
|
2008-06-24 14:31:23 +00:00
|
|
|
|
2008-09-13 16:32:47 +00:00
|
|
|
bool FtpConnection::sendRest(const SegmentHandle& segment)
|
|
|
|
{
|
|
|
|
if(_socketBuffer.sendBufferIsEmpty()) {
|
|
|
|
std::string request = "REST ";
|
|
|
|
if(segment.isNull()) {
|
|
|
|
request += "0";
|
|
|
|
} else {
|
|
|
|
request += Util::itos(segment->getPositionToWrite());
|
|
|
|
}
|
|
|
|
request += "\r\n";
|
|
|
|
|
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
|
|
|
|
_socketBuffer.feedSendBuffer(request);
|
|
|
|
}
|
|
|
|
_socketBuffer.send();
|
|
|
|
return _socketBuffer.sendBufferIsEmpty();
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
|
2008-09-13 16:32:47 +00:00
|
|
|
bool FtpConnection::sendRetr()
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2008-09-13 16:32:47 +00:00
|
|
|
if(_socketBuffer.sendBufferIsEmpty()) {
|
|
|
|
std::string request = "RETR "+Util::urldecode(req->getFile())+"\r\n";
|
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
|
|
|
|
_socketBuffer.feedSendBuffer(request);
|
|
|
|
}
|
|
|
|
_socketBuffer.send();
|
|
|
|
return _socketBuffer.sendBufferIsEmpty();
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
unsigned int FtpConnection::getStatus(const std::string& response) const
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2008-03-09 12:24:01 +00:00
|
|
|
unsigned int status;
|
|
|
|
// When the response is not like "%u %*s",
|
2006-02-22 11:18:47 +00:00
|
|
|
// we return 0.
|
|
|
|
if(response.find_first_not_of("0123456789") != 3
|
2006-02-22 14:30:47 +00:00
|
|
|
|| !(response.find(" ") == 3 || response.find("-") == 3)) {
|
2006-02-22 11:18:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2008-03-09 12:24:01 +00:00
|
|
|
if(sscanf(response.c_str(), "%u %*s", &status) == 1) {
|
2006-02-21 12:28:42 +00:00
|
|
|
return status;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-09 15:22:32 +00:00
|
|
|
// Returns the length of the reponse if the whole response has been received.
|
|
|
|
// The length includes \r\n.
|
|
|
|
// If the whole response has not been recieved, then returns std::string::npos.
|
|
|
|
std::string::size_type
|
|
|
|
FtpConnection::findEndOfResponse(unsigned int status,
|
|
|
|
const std::string& buf) const
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2008-09-09 15:22:32 +00:00
|
|
|
if(buf.size() <= 4) {
|
|
|
|
return std::string::npos;
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
2006-03-01 15:28:03 +00:00
|
|
|
// if 4th character of buf is '-', then multi line response is expected.
|
2008-09-09 15:22:32 +00:00
|
|
|
if(buf.at(3) == '-') {
|
2006-02-21 12:28:42 +00:00
|
|
|
// multi line response
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string::size_type p;
|
2008-09-09 15:22:32 +00:00
|
|
|
p = buf.find(A2STR::CRLF+Util::uitos(status)+" ");
|
2008-02-08 15:53:45 +00:00
|
|
|
if(p == std::string::npos) {
|
2008-09-09 15:22:32 +00:00
|
|
|
return std::string::npos;
|
|
|
|
}
|
|
|
|
p = buf.find(A2STR::CRLF, p+6);
|
|
|
|
if(p == std::string::npos) {
|
|
|
|
return std::string::npos;
|
|
|
|
} else {
|
|
|
|
return p+2;
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
} else {
|
2008-09-09 15:22:32 +00:00
|
|
|
// single line response
|
|
|
|
std::string::size_type p = buf.find(A2STR::CRLF);
|
|
|
|
if(p == std::string::npos) {
|
|
|
|
return std::string::npos;
|
|
|
|
} else {
|
|
|
|
return p+2;
|
|
|
|
}
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
bool FtpConnection::bulkReceiveResponse(std::pair<unsigned int, std::string>& response)
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2006-02-21 12:28:42 +00:00
|
|
|
char buf[1024];
|
|
|
|
while(socket->isReadable(0)) {
|
2008-09-14 12:51:30 +00:00
|
|
|
size_t size = sizeof(buf);
|
2006-02-21 12:28:42 +00:00
|
|
|
socket->readData(buf, size);
|
2006-11-08 16:25:38 +00:00
|
|
|
if(size == 0) {
|
2008-09-27 16:06:34 +00:00
|
|
|
if(socket->wantRead() || socket->wantWrite()) {
|
|
|
|
return false;
|
|
|
|
}
|
2008-04-27 02:22:14 +00:00
|
|
|
throw DlRetryEx(EX_GOT_EOF);
|
2006-11-08 16:25:38 +00:00
|
|
|
}
|
2008-09-14 12:51:30 +00:00
|
|
|
if(strbuf.size()+size > MAX_RECV_BUFFER) {
|
2008-10-01 15:16:36 +00:00
|
|
|
throw DlRetryEx
|
|
|
|
(StringFormat("Max FTP recv buffer reached. length=%lu",
|
|
|
|
static_cast<unsigned long>(strbuf.size()+size)).str());
|
2008-09-14 12:51:30 +00:00
|
|
|
}
|
|
|
|
strbuf.append(&buf[0], &buf[size]);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
2008-03-09 12:24:01 +00:00
|
|
|
unsigned int status;
|
2006-02-21 12:28:42 +00:00
|
|
|
if(strbuf.size() >= 4) {
|
|
|
|
status = getStatus(strbuf);
|
|
|
|
if(status == 0) {
|
2008-04-27 02:22:14 +00:00
|
|
|
throw DlAbortEx(EX_INVALID_RESPONSE);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2008-09-09 15:22:32 +00:00
|
|
|
std::string::size_type length;
|
|
|
|
if((length = findEndOfResponse(status, strbuf)) != std::string::npos) {
|
2006-02-21 12:28:42 +00:00
|
|
|
response.first = status;
|
2008-09-09 15:22:32 +00:00
|
|
|
response.second = strbuf.substr(0, length);
|
|
|
|
logger->info(MSG_RECEIVE_RESPONSE, cuid, response.second.c_str());
|
|
|
|
strbuf.erase(0, length);
|
2006-02-21 12:28:42 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
// didn't receive response fully.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
unsigned int FtpConnection::receiveResponse()
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2008-03-09 12:24:01 +00:00
|
|
|
std::pair<unsigned int, std::string> response;
|
2006-02-21 12:28:42 +00:00
|
|
|
if(bulkReceiveResponse(response)) {
|
|
|
|
return response.first;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
# define LONGLONG_PRINTF "%I64d"
|
|
|
|
# define ULONGLONG_PRINTF "%I64u"
|
|
|
|
# define LONGLONG_SCANF "%I64d"
|
|
|
|
# define ULONGLONG_SCANF "%I64u"
|
|
|
|
#else
|
|
|
|
# define LONGLONG_PRINTF "%lld"
|
|
|
|
# define ULONGLONG_PRINTF "%llu"
|
|
|
|
# define LONGLONG_SCANF "%Ld"
|
|
|
|
# define ULONGLONG_SCANF "%Lu"
|
|
|
|
#endif // __MINGW32__
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
unsigned int FtpConnection::receiveSizeResponse(uint64_t& size)
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2008-03-09 12:24:01 +00:00
|
|
|
std::pair<unsigned int, std::string> response;
|
2006-02-21 12:28:42 +00:00
|
|
|
if(bulkReceiveResponse(response)) {
|
|
|
|
if(response.first == 213) {
|
2008-10-12 16:09:12 +00:00
|
|
|
sscanf(response.second.c_str(), "%*u " ULONGLONG_SCANF,
|
|
|
|
reinterpret_cast<long long unsigned int*>(&size));
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
return response.first;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-08 13:06:44 +00:00
|
|
|
unsigned int FtpConnection::receiveMdtmResponse(Time& time)
|
|
|
|
{
|
|
|
|
// MDTM command, specified in RFC3659.
|
|
|
|
std::pair<unsigned int, std::string> response;
|
|
|
|
if(bulkReceiveResponse(response)) {
|
|
|
|
if(response.first == 213) {
|
|
|
|
char buf[15]; // YYYYMMDDhhmmss+\0, milli second part is dropped.
|
|
|
|
sscanf(response.second.c_str(), "%*u %14s", buf);
|
|
|
|
if(strlen(buf) == 14) {
|
|
|
|
time = Time::parse(buf, "%Y%m%d%H%M%S");
|
|
|
|
} else {
|
|
|
|
time.setTimeInSec(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return response.first;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
unsigned int FtpConnection::receivePasvResponse(std::pair<std::string, uint16_t>& dest)
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2008-03-09 12:24:01 +00:00
|
|
|
std::pair<unsigned int, std::string> response;
|
2006-02-21 12:28:42 +00:00
|
|
|
if(bulkReceiveResponse(response)) {
|
|
|
|
if(response.first == 227) {
|
|
|
|
// we assume the format of response is "227 Entering Passive Mode (h1,h2,h3,h4,p1,p2)."
|
2008-03-09 12:24:01 +00:00
|
|
|
unsigned int h1, h2, h3, h4, p1, p2;
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string::size_type p = response.second.find("(");
|
2006-02-21 12:28:42 +00:00
|
|
|
if(p >= 4) {
|
|
|
|
sscanf(response.second.substr(response.second.find("(")).c_str(),
|
2008-03-09 12:24:01 +00:00
|
|
|
"(%u,%u,%u,%u,%u,%u).",
|
2006-02-21 12:28:42 +00:00
|
|
|
&h1, &h2, &h3, &h4, &p1, &p2);
|
|
|
|
// ip address
|
2008-03-09 12:24:01 +00:00
|
|
|
dest.first = Util::uitos(h1)+"."+Util::uitos(h2)+"."+Util::uitos(h3)+"."+Util::uitos(h4);
|
2006-02-21 12:28:42 +00:00
|
|
|
// port number
|
|
|
|
dest.second = 256*p1+p2;
|
|
|
|
} else {
|
2008-04-27 02:22:14 +00:00
|
|
|
throw DlRetryEx(EX_INVALID_RESPONSE);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return response.first;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
|
2008-09-24 17:01:57 +00:00
|
|
|
unsigned int FtpConnection::receivePwdResponse(std::string& pwd)
|
|
|
|
{
|
|
|
|
std::pair<unsigned int, std::string> response;
|
|
|
|
if(bulkReceiveResponse(response)) {
|
|
|
|
if(response.first == 257) {
|
|
|
|
std::string::size_type first;
|
|
|
|
std::string::size_type last;
|
|
|
|
|
|
|
|
if((first = response.second.find("\"")) != std::string::npos &&
|
|
|
|
(last = response.second.find("\"", ++first)) != std::string::npos) {
|
|
|
|
pwd = response.second.substr(first, last-first);
|
|
|
|
} else {
|
|
|
|
throw DlAbortEx(EX_INVALID_RESPONSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return response.first;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FtpConnection::setBaseWorkingDir(const std::string& baseWorkingDir)
|
|
|
|
{
|
|
|
|
_baseWorkingDir = baseWorkingDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& FtpConnection::getBaseWorkingDir() const
|
|
|
|
{
|
|
|
|
return _baseWorkingDir;
|
|
|
|
}
|
|
|
|
|
2008-02-08 15:53:45 +00:00
|
|
|
} // namespace aria2
|