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-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"
|
|
|
|
|
|
|
|
namespace aria2 {
|
2006-02-21 12:28:42 +00:00
|
|
|
|
2007-07-21 08:56:16 +00:00
|
|
|
FtpConnection::FtpConnection(int32_t cuid, const SocketHandle& socket,
|
2008-02-08 15:53:45 +00:00
|
|
|
const RequestHandle& req, const Option* op):
|
|
|
|
cuid(cuid), socket(socket), req(req), option(op),
|
|
|
|
logger(LogFactory::getInstance()) {}
|
2006-02-21 12:28:42 +00:00
|
|
|
|
|
|
|
FtpConnection::~FtpConnection() {}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void FtpConnection::sendUser() const
|
|
|
|
{
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string request = "USER "+AuthConfigFactorySingleton::instance()->createAuthConfig(req)->getUser()+"\r\n";
|
2006-02-21 15:01:05 +00:00
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
|
2006-02-21 12:28:42 +00:00
|
|
|
socket->writeData(request);
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void FtpConnection::sendPass() const
|
|
|
|
{
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string request = "PASS "+AuthConfigFactorySingleton::instance()->createAuthConfig(req)->getPassword()+"\r\n";
|
2006-02-21 15:01:05 +00:00
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, "PASS ********");
|
2006-02-21 12:28:42 +00:00
|
|
|
socket->writeData(request);
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void FtpConnection::sendType() const
|
|
|
|
{
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string type;
|
2006-02-21 14:00:58 +00:00
|
|
|
if(option->get(PREF_FTP_TYPE) == V_ASCII) {
|
|
|
|
type = "A";
|
|
|
|
} else {
|
|
|
|
type = "I";
|
|
|
|
}
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string request = "TYPE "+type+"\r\n";
|
2006-02-21 15:01:05 +00:00
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
|
2006-02-21 12:28:42 +00:00
|
|
|
socket->writeData(request);
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void FtpConnection::sendCwd() const
|
|
|
|
{
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string request = "CWD "+Util::urldecode(req->getDir())+"\r\n";
|
2006-02-21 15:01:05 +00:00
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
|
2006-02-21 12:28:42 +00:00
|
|
|
socket->writeData(request);
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void FtpConnection::sendSize() const
|
|
|
|
{
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string request = "SIZE "+Util::urldecode(req->getFile())+"\r\n";
|
2006-02-21 15:01:05 +00:00
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
|
2006-02-21 12:28:42 +00:00
|
|
|
socket->writeData(request);
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void FtpConnection::sendPasv() const
|
|
|
|
{
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string request = "PASV\r\n";
|
2006-02-21 15:01:05 +00:00
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
|
2006-02-21 12:28:42 +00:00
|
|
|
socket->writeData(request);
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
SocketHandle FtpConnection::sendPort() const
|
|
|
|
{
|
2008-04-20 00:50:22 +00:00
|
|
|
SocketHandle 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();
|
|
|
|
|
2008-03-09 12:24:01 +00:00
|
|
|
std::pair<std::string, uint16_t> addrinfo;
|
2006-07-19 17:07:45 +00:00
|
|
|
socket->getAddrInfo(addrinfo);
|
2008-03-09 12:24:01 +00:00
|
|
|
unsigned int ipaddr[4];
|
|
|
|
sscanf(addrinfo.first.c_str(), "%u.%u.%u.%u",
|
2006-07-19 17:07:45 +00:00
|
|
|
&ipaddr[0], &ipaddr[1], &ipaddr[2], &ipaddr[3]);
|
|
|
|
serverSocket->getAddrInfo(addrinfo);
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string request = "PORT "+
|
2008-03-09 12:24:01 +00:00
|
|
|
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";
|
2006-07-19 17:07:45 +00:00
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
|
|
|
|
socket->writeData(request);
|
2006-02-21 12:28:42 +00:00
|
|
|
return serverSocket;
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void FtpConnection::sendRest(const SegmentHandle& segment) const
|
|
|
|
{
|
2008-02-24 09:43:31 +00:00
|
|
|
std::string request = "REST "+Util::itos(segment->getPositionToWrite())+"\r\n";
|
2006-02-21 15:01:05 +00:00
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
|
2006-02-21 12:28:42 +00:00
|
|
|
socket->writeData(request);
|
|
|
|
}
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
void FtpConnection::sendRetr() const
|
|
|
|
{
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string request = "RETR "+Util::urldecode(req->getFile())+"\r\n";
|
2006-02-21 15:01:05 +00:00
|
|
|
logger->info(MSG_SENDING_REQUEST, cuid, request.c_str());
|
2006-02-21 12:28:42 +00:00
|
|
|
socket->writeData(request);
|
|
|
|
}
|
|
|
|
|
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-03-09 12:24:01 +00:00
|
|
|
bool FtpConnection::isEndOfResponse(unsigned int status, const std::string& response) const
|
2007-10-11 16:58:24 +00:00
|
|
|
{
|
2006-02-21 12:28:42 +00:00
|
|
|
if(response.size() <= 4) {
|
|
|
|
return false;
|
|
|
|
}
|
2006-03-01 15:28:03 +00:00
|
|
|
// if 4th character of buf is '-', then multi line response is expected.
|
2006-02-21 12:28:42 +00:00
|
|
|
if(response.at(3) == '-') {
|
|
|
|
// multi line response
|
2008-02-08 15:53:45 +00:00
|
|
|
std::string::size_type p;
|
2008-03-09 12:24:01 +00:00
|
|
|
p = response.find("\r\n"+Util::uitos(status)+" ");
|
2008-02-08 15:53:45 +00:00
|
|
|
if(p == std::string::npos) {
|
2006-02-21 12:28:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(Util::endsWith(response, "\r\n")) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-03-09 12:24:01 +00:00
|
|
|
size_t size = sizeof(buf)-1;
|
2006-02-21 12:28:42 +00:00
|
|
|
socket->readData(buf, size);
|
2006-11-08 16:25:38 +00:00
|
|
|
if(size == 0) {
|
|
|
|
throw new DlRetryEx(EX_GOT_EOF);
|
|
|
|
}
|
2006-02-21 12:28:42 +00:00
|
|
|
buf[size] = '\0';
|
|
|
|
strbuf += buf;
|
|
|
|
}
|
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) {
|
2007-12-04 11:38:06 +00:00
|
|
|
throw new DlAbortEx(EX_INVALID_RESPONSE);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(isEndOfResponse(status, strbuf)) {
|
|
|
|
logger->info(MSG_RECEIVE_RESPONSE, cuid, strbuf.c_str());
|
|
|
|
response.first = status;
|
|
|
|
response.second = strbuf;
|
|
|
|
strbuf.erase();
|
|
|
|
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-03-09 12:24:01 +00:00
|
|
|
sscanf(response.second.c_str(), "%*u " LONGLONG_SCANF, &size);
|
2006-02-21 12:28:42 +00:00
|
|
|
}
|
|
|
|
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 {
|
2006-02-21 14:00:58 +00:00
|
|
|
throw new 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
|
|
|
|
|
|
|
} // namespace aria2
|