2006-03-21 14:12:51 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
2006-09-21 15:31:24 +00:00
|
|
|
* aria2 - The high speed download utility
|
2006-03-21 14:12:51 +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-03-21 14:12:51 +00:00
|
|
|
*/
|
|
|
|
/* copyright --> */
|
|
|
|
#include "PeerListenCommand.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "DownloadEngine.h"
|
|
|
|
#include "Peer.h"
|
|
|
|
#include "RequestGroupMan.h"
|
2007-05-20 13:51:52 +00:00
|
|
|
#include "RecoverableException.h"
|
2007-05-20 15:22:50 +00:00
|
|
|
#include "CUIDCounter.h"
|
2007-07-20 17:06:21 +00:00
|
|
|
#include "message.h"
|
2007-10-11 16:58:24 +00:00
|
|
|
#include "PeerReceiveHandshakeCommand.h"
|
2006-03-21 14:12:51 +00:00
|
|
|
|
2007-11-04 12:26:12 +00:00
|
|
|
int32_t PeerListenCommand::__numInstance = 0;
|
|
|
|
|
|
|
|
PeerListenCommand* PeerListenCommand::__instance = 0;
|
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
PeerListenCommand::PeerListenCommand(int32_t cuid, DownloadEngine* e):
|
|
|
|
Command(cuid),
|
|
|
|
e(e),
|
2007-11-04 12:26:12 +00:00
|
|
|
_lowestSpeedLimit(20*1024)
|
|
|
|
{
|
|
|
|
++__numInstance;
|
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
|
2007-11-04 12:26:12 +00:00
|
|
|
PeerListenCommand::~PeerListenCommand()
|
|
|
|
{
|
|
|
|
--__numInstance;
|
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
|
2007-11-04 12:26:12 +00:00
|
|
|
int32_t PeerListenCommand::bindPort(int32_t portRangeStart,
|
|
|
|
int32_t portRangeEnd)
|
|
|
|
{
|
2006-03-21 14:12:51 +00:00
|
|
|
if(portRangeStart > portRangeEnd) {
|
|
|
|
return -1;
|
|
|
|
}
|
2007-07-21 08:56:16 +00:00
|
|
|
for(int32_t port = portRangeStart; port <= portRangeEnd; port++) {
|
2006-03-21 14:12:51 +00:00
|
|
|
try {
|
|
|
|
socket->beginListen(port);
|
2007-07-20 17:06:21 +00:00
|
|
|
logger->info(MSG_LISTENING_PORT,
|
2006-04-17 16:17:20 +00:00
|
|
|
cuid, port);
|
2006-03-21 14:12:51 +00:00
|
|
|
return port;
|
2007-01-11 16:32:31 +00:00
|
|
|
} catch(RecoverableException* ex) {
|
2007-07-20 17:06:21 +00:00
|
|
|
logger->error(MSG_BIND_FAILURE,
|
2006-04-17 16:17:20 +00:00
|
|
|
ex, cuid, port);
|
2006-07-19 17:07:45 +00:00
|
|
|
socket->closeConnection();
|
2006-03-27 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
* configure.in: Added gnutls support. Added several CPP macros.
* m4/openssl.m4: Added.
* src/SocketCore.{h,cc}: Drop const quarifier from writeData(),
readData(), peekData(). Added gnutls support.
* src/HttpDownloadCommand.cc: Removed SleepCommand.h
* src/TrackerWatcherCommand.{h,cc}: Added. This command creates
TrackerInitCommand periodicaly(TorrentMan::minInterval).
* src/TorrentMan.cc: Remove downloadedSize == 0 check from
save().
Instead, added a check for whether setup method has executed
successfully.
* src/TorrentMan.h: Added member vaiable setupComplete. Updated
DEFAULT_ANNOUNCE_INTERVAL and DEFAULT_ANNOUNCE_MIN_INTERVAL to
300.
* src/Makefile.am: Updated.
* src/messageDigest.h: Added. This is a macro calculating SHA1
digest
using whether OpenSSL or gcrypt, depending on the result of
configure
script.
* src/ShaVisitor.{h,cc}: Removed direct dependency on OpenSSL by
using
messageDigest.h.
* src/TorrentAutoSaveCommand.h: Removed unused variable cuid.
* src/PeerListenCommand.cc: Added log about port binded
successfully.
Fixed memory leak.
* src/main.cc: Added gnutls support. Replaced LIB_SSL with
ENABLE_BITTORRENT where they are not related to OpenSSL but
BitTorrent.
Removed instantiation of TrackerInitCommand. Instead,
TrackerWatcherCommand is instantiated and pushed to the command
queue.
* src/InitiateConnectionCommandFactory.cc: Replaced HAVE_LIBSSL
with
ENABLE_SSL.
* src/Request.cc: Replaced HAVE_LIBSSL with ENABLE_SSL.
* src/RequestSlotMan.cc:
(deleteCompletedRequestSlot)
If a piece is already acquired by another command, delete the
request
slots for the piece.
* src/TrackerUpdateCommand.cc:
(execute)
Changed log level of MSG_TRACKER_WARNING_MESSAGE from info to
warn.
Added a check whether peer list is null.
Fixed the bug that causes sending completed event to the tracker
several times.
* src/TrackerInitCommand.cc:
(execute)
Fixed the bug that causes sending completed event to the tracker
several times.
* src/AbstractDiskWriter.{h,cc}: Removed direct dependency on
OpenSSL
by using messageDigest.h.
2006-03-26 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
* PeerConnection.cc: Replaced log message "keep-alive" with
"keep alive".
* PeerInteractionCommand.{h,cc}: Close connection if peer is
choking
localhost long time.
* TorrentMan.cc: When adding new peer with duplicate = true, if
the
number of peer list is equal to or grater than MAX_PEER_LIST,
delete
at most 100 failure entry from the list. If with duplicate =
false,
MAX_PEER_LIST is not checked.
* PeerListenCommand.cc: Fixed the argument order of log message.
2006-03-25 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
* Logger.h: Moved enum LEVEL from SimpleLogger.h to here.
Added warn().
* SimpleLogger.h: Moved enum LEVEL to Logger.h.
Implemented warn().
Defined 2 macros(WRITE_LOG, WRITE_LOG_EX) to avoid duplicated
code.
2006-03-27 14:47:26 +00:00
|
|
|
delete ex;
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PeerListenCommand::execute() {
|
2007-10-11 16:58:24 +00:00
|
|
|
if(e->isHaltRequested() || e->_requestGroupMan->countRequestGroup() == 0) {
|
2006-05-24 15:18:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
2007-07-21 08:56:16 +00:00
|
|
|
for(int32_t i = 0; i < 3 && socket->isReadable(0); i++) {
|
2006-07-19 17:07:45 +00:00
|
|
|
SocketHandle peerSocket;
|
|
|
|
try {
|
|
|
|
peerSocket = socket->acceptConnection();
|
2007-07-21 08:56:16 +00:00
|
|
|
pair<string, int32_t> peerInfo;
|
2006-07-19 17:07:45 +00:00
|
|
|
peerSocket->getPeerInfo(peerInfo);
|
2007-07-21 08:56:16 +00:00
|
|
|
pair<string, int32_t> localInfo;
|
2006-07-19 17:07:45 +00:00
|
|
|
peerSocket->getAddrInfo(localInfo);
|
2007-06-04 12:33:29 +00:00
|
|
|
|
2007-10-11 16:58:24 +00:00
|
|
|
if(peerInfo.first == localInfo.first) {
|
|
|
|
continue;
|
2006-07-19 17:07:45 +00:00
|
|
|
}
|
2007-10-11 16:58:24 +00:00
|
|
|
PeerHandle peer = new Peer(peerInfo.first, peerInfo.second, 0, 0);
|
|
|
|
PeerReceiveHandshakeCommand* command =
|
|
|
|
new PeerReceiveHandshakeCommand(CUIDCounterSingletonHolder::instance()->newID(),
|
|
|
|
peer, e, peerSocket);
|
|
|
|
e->commands.push_back(command);
|
|
|
|
logger->debug("Accepted the connection from %s:%d.",
|
|
|
|
peer->ipaddr.c_str(),
|
|
|
|
peer->port);
|
|
|
|
logger->debug("Added CUID#%d to receive Bt handshake.",
|
|
|
|
command->getCuid());
|
|
|
|
|
2007-01-11 16:32:31 +00:00
|
|
|
} catch(RecoverableException* ex) {
|
2007-07-20 17:06:21 +00:00
|
|
|
logger->debug(MSG_ACCEPT_FAILURE, ex, cuid);
|
2006-07-19 17:07:45 +00:00
|
|
|
delete ex;
|
|
|
|
}
|
2006-03-21 14:12:51 +00:00
|
|
|
}
|
2006-05-09 15:54:14 +00:00
|
|
|
e->commands.push_back(this);
|
2006-03-21 14:12:51 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-11-04 12:26:12 +00:00
|
|
|
|
|
|
|
PeerListenCommand* PeerListenCommand::getInstance(DownloadEngine* e)
|
|
|
|
{
|
|
|
|
if(__numInstance == 0) {
|
|
|
|
__instance = new PeerListenCommand(CUIDCounterSingletonHolder::instance()->newID(), e);
|
|
|
|
}
|
|
|
|
return __instance;
|
|
|
|
}
|