diff options
author | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-01-20 02:37:40 +0000 |
---|---|---|
committer | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-01-20 02:37:40 +0000 |
commit | 9ad5c7b5e23b4940e7a3ea3ca3a6fb77e6a8fab0 (patch) | |
tree | d088b5210e77d9fa91d954d8550e00e372b47378 /libktorrent/torrent | |
download | ktorrent-9ad5c7b5e23b4940e7a3ea3ca3a6fb77e6a8fab0.tar.gz ktorrent-9ad5c7b5e23b4940e7a3ea3ca3a6fb77e6a8fab0.zip |
Updated to final KDE3 ktorrent release (2.2.6)
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/ktorrent@1077377 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'libktorrent/torrent')
119 files changed, 24425 insertions, 0 deletions
diff --git a/libktorrent/torrent/Makefile.am b/libktorrent/torrent/Makefile.am new file mode 100644 index 0000000..d546228 --- /dev/null +++ b/libktorrent/torrent/Makefile.am @@ -0,0 +1,33 @@ +INCLUDES = -I$(top_builddir)/ktorrent/libktorrent -I$(top_builddir)/libktorrent \ + -I$(srcdir)/.. $(all_includes) + +METASOURCES = AUTO + +noinst_LTLIBRARIES = libtorrent.la +libtorrent_la_LDFLAGS = $(all_libraries) +noinst_HEADERS = advancedchokealgorithm.h announcelist.h authenticate.h \ + authenticatebase.h authenticationmonitor.h bdecoder.h bencoder.h bnode.h cache.h \ + cachefile.h cap.h choker.h chunk.h chunkcounter.h chunkdownload.h chunkmanager.h \ + chunkselector.h dndfile.h downloadcap.h downloader.h globals.h httptracker.h \ + ipblocklist.h movedatafilesjob.h multifilecache.h newchokealgorithm.h \ + oldchokealgorithm.h packet.h packetreader.h packetwriter.h peer.h peerdownloader.h peerid.h \ + peermanager.h peersourcemanager.h peeruploader.h piece.h preallocationthread.h \ + queuemanager.h request.h server.h serverauthenticate.h singlefilecache.h \ + speedestimater.h statsfile.h timeestimator.h torrent.h torrentcontrol.h torrentcreator.h \ + torrentfile.h tracker.h udptracker.h udptrackersocket.h uploadcap.h uploader.h \ + upspeedestimater.h utpex.h value.h + +libtorrent_la_SOURCES = advancedchokealgorithm.cpp announcelist.cpp \ + authenticate.cpp authenticatebase.cpp authenticationmonitor.cpp bdecoder.cpp \ + bencoder.cpp bnode.cpp cache.cpp cachefile.cpp cap.cpp choker.cpp chunk.cpp \ + chunkcounter.cpp chunkdownload.cpp chunkmanager.cpp chunkselector.cpp dndfile.cpp \ + downloadcap.cpp downloader.cpp globals.cpp httptracker.cpp ipblocklist.cpp \ + movedatafilesjob.cpp multifilecache.cpp newchokealgorithm.cpp packet.cpp packetreader.cpp \ + packetwriter.cpp peer.cpp peerdownloader.cpp peerid.cpp peermanager.cpp \ + peersourcemanager.cpp peeruploader.cpp piece.cpp preallocationthread.cpp queuemanager.cpp \ + request.cpp server.cpp serverauthenticate.cpp singlefilecache.cpp \ + speedestimater.cpp statsfile.cpp timeestimator.cpp torrent.cpp torrentcontrol.cpp \ + torrentcreator.cpp torrentfile.cpp tracker.cpp udptracker.cpp udptrackersocket.cpp \ + uploadcap.cpp uploader.cpp upspeedestimater.cpp utpex.cpp value.cpp + +KDE_CXXFLAGS = $(USE_EXCEPTIONS) $(USE_RTTI) diff --git a/libktorrent/torrent/advancedchokealgorithm.cpp b/libktorrent/torrent/advancedchokealgorithm.cpp new file mode 100644 index 0000000..7ca0578 --- /dev/null +++ b/libktorrent/torrent/advancedchokealgorithm.cpp @@ -0,0 +1,259 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <stdlib.h> +#include <util/functions.h> +#include <interfaces/torrentinterface.h> +#include "chunkmanager.h" +#include "peer.h" +#include "peermanager.h" +#include "packetwriter.h" +#include "advancedchokealgorithm.h" + +using namespace kt; + +namespace bt +{ + + + const Uint32 OPT_SEL_INTERVAL = 30*1000; // we switch optimistic peer each 30 seconds + const double NEWBIE_BONUS = 1.0; + const double SNUB_PENALTY = 10.0; + const double ONE_MB = 1024*1024; + + + AdvancedChokeAlgorithm::AdvancedChokeAlgorithm() + : ChokeAlgorithm() + { + last_opt_sel_time = 0; + } + + + AdvancedChokeAlgorithm::~AdvancedChokeAlgorithm() + {} + + bool AdvancedChokeAlgorithm::calcACAScore(Peer* p,ChunkManager & cman,const kt::TorrentStats & stats) + { + const PeerInterface::Stats & s = p->getStats(); + if (p->isSeeder()) + { + /* + double bd = 0; + if (stats.trk_bytes_downloaded > 0) + bd = s.bytes_downloaded / stats.trk_bytes_downloaded; + double ds = 0; + if (stats.download_rate > 0) + ds = s.download_rate/ stats.download_rate; + p->setACAScore(5*bd + 5*ds); + */ + p->setACAScore(0.0); + return false; + } + + bool should_be_interested = false; + bool should_we_be_interested = false; + // before we start calculating first check if we have piece that the peer doesn't have + const BitSet & ours = cman.getBitSet(); + const BitSet & theirs = p->getBitSet(); + for (Uint32 i = 0;i < ours.getNumBits();i++) + { + if (ours.get(i) && !theirs.get(i)) + { + should_be_interested = true; + break; + } + } + + if (!should_be_interested || !p->isInterested()) + { + // not interseted so it doesn't make sense to unchoke it + p->setACAScore(-50.0); + return false; + } + + + + double nb = 0.0; // newbie bonus + double cp = 0.0; // choke penalty + double sp = 0.0; // snubbing penalty + double lb = s.local ? 10.0 : 0.0; // local peers get a bonus of 10 + double bd = s.bytes_downloaded; // bytes downloaded + double tbd = stats.trk_bytes_downloaded; // total bytes downloaded + double ds = s.download_rate; // current download rate + double tds = stats.download_rate; // total download speed + + // if the peer has less than 1 MB or 0.5 % of the torrent it is a newbie + if (p->percentAvailable() < 0.5 && stats.total_bytes * p->percentAvailable() < 1024*1024) + { + nb = NEWBIE_BONUS; + } + + if (p->isChoked()) + { + cp = NEWBIE_BONUS; // cp cancels out newbie bonus + } + + // if the evil bit is on (!choked, snubbed and requests have timed out) + if (s.evil) + { + sp = SNUB_PENALTY; + } + + // NB + K * (BD/TBD) - CP - SP + L * (DS / TDS) + double K = 5.0; + double L = 5.0; + double aca = lb + nb + (tbd > 0 ? K * (bd/tbd) : 0.0) + (tds > 0 ? L* (ds / tds) : 0.0) - cp - sp; + + p->setACAScore(aca); + return true; + } + + static int ACACmp(Peer* a,Peer* b) + { + if (a->getStats().aca_score < b->getStats().aca_score) + return 1; + else if (a->getStats().aca_score > b->getStats().aca_score) + return -1; + else + return 0; + } + + + void AdvancedChokeAlgorithm::doChokingLeechingState(PeerManager & pman,ChunkManager & cman,const kt::TorrentStats & stats) + { + PeerPtrList ppl; + Uint32 np = pman.getNumConnectedPeers(); + // add all non seeders + for (Uint32 i = 0;i < np;i++) + { + Peer* p = pman.getPeer(i); + if (p) + { + if (calcACAScore(p,cman,stats)) + ppl.append(p); + else + // choke seeders they do not want to download from us anyway + p->choke(); + } + } + + // sort list by ACA score + ppl.setCompareFunc(ACACmp); + ppl.sort(); + + doUnchoking(ppl,updateOptimisticPeer(pman,ppl)); + } + + void AdvancedChokeAlgorithm::doUnchoking(PeerPtrList & ppl,Peer* poup) + { + // Get the number of upload slots + Uint32 num_slots = Choker::getNumUploadSlots(); + // Do the choking and unchoking + Uint32 num_unchoked = 0; + for (Uint32 i = 0;i < ppl.count();i++) + { + Peer* p = ppl.at(i); + if (!poup && num_unchoked < num_slots) + { + p->getPacketWriter().sendUnchoke(); + num_unchoked++; + } + else if (num_unchoked < num_slots -1 || p == poup) + { + p->getPacketWriter().sendUnchoke(); + if (p != poup) + num_unchoked++; + } + else + { + p->choke(); + } + } + } + + static int UpRateCmp(Peer* a,Peer* b) + { + if (a->getStats().upload_rate < b->getStats().upload_rate) + return -1; + else if (a->getStats().upload_rate > b->getStats().upload_rate) + return 1; + else + return 0; + } + + void AdvancedChokeAlgorithm::doChokingSeedingState(PeerManager & pman,ChunkManager & cman,const kt::TorrentStats & stats) + { + PeerPtrList ppl; + Uint32 np = pman.getNumConnectedPeers(); + // add all non seeders + for (Uint32 i = 0;i < np;i++) + { + Peer* p = pman.getPeer(i); + if (p) + { + // update the ACA score in the process + if (calcACAScore(p,cman,stats)) + ppl.append(p); + else + // choke seeders they do not want to download from us anyway + p->choke(); + } + } + + ppl.setCompareFunc(UpRateCmp); + ppl.sort(); + + doUnchoking(ppl,updateOptimisticPeer(pman,ppl)); + } + + static Uint32 FindPlannedOptimisticUnchokedPeer(PeerManager& pman,const PeerPtrList & ppl) + { + Uint32 num_peers = pman.getNumConnectedPeers(); + if (num_peers == 0) + return UNDEFINED_ID; + + // find a random peer that is choked and interested + Uint32 start = rand() % num_peers; + Uint32 i = (start + 1) % num_peers; + while (i != start) + { + Peer* p = pman.getPeer(i); + if (p && p->isChoked() && p->isInterested() && !p->isSeeder() && ppl.contains(p)) + return p->getID(); + i = (i + 1) % num_peers; + } + + // we do not expect to have 4 billion peers + return UNDEFINED_ID; + } + + Peer* AdvancedChokeAlgorithm::updateOptimisticPeer(PeerManager & pman,const PeerPtrList & ppl) + { + // get the planned optimistic unchoked peer and change it if necessary + Peer* poup = pman.findPeer(opt_unchoked_peer_id); + TimeStamp now = GetCurrentTime(); + if (now - last_opt_sel_time > OPT_SEL_INTERVAL || !poup) + { + opt_unchoked_peer_id = FindPlannedOptimisticUnchokedPeer(pman,ppl); + last_opt_sel_time = now; + poup = pman.findPeer(opt_unchoked_peer_id); + } + return poup; + } +} diff --git a/libktorrent/torrent/advancedchokealgorithm.h b/libktorrent/torrent/advancedchokealgorithm.h new file mode 100644 index 0000000..f8a1086 --- /dev/null +++ b/libktorrent/torrent/advancedchokealgorithm.h @@ -0,0 +1,52 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTADVANCEDCHOKEALGORITHM_H +#define BTADVANCEDCHOKEALGORITHM_H + +#include "choker.h" + +namespace bt +{ + class Peer; + class PeerPtrList; + + + /** + @author Joris Guisson <joris.guisson@gmail.com> + */ + class AdvancedChokeAlgorithm : public ChokeAlgorithm + { + TimeStamp last_opt_sel_time; // last time we updated the optimistic unchoked peer + public: + AdvancedChokeAlgorithm(); + virtual ~AdvancedChokeAlgorithm(); + + virtual void doChokingLeechingState(PeerManager & pman,ChunkManager & cman,const kt::TorrentStats & stats); + virtual void doChokingSeedingState(PeerManager & pman,ChunkManager & cman,const kt::TorrentStats & stats); + + private: + bool calcACAScore(Peer* p,ChunkManager & cman,const kt::TorrentStats & stats); + Peer* updateOptimisticPeer(PeerManager & pman,const PeerPtrList & ppl); + void doUnchoking(PeerPtrList & ppl,Peer* poup); + }; + +} + +#endif diff --git a/libktorrent/torrent/announcelist.cpp b/libktorrent/torrent/announcelist.cpp new file mode 100644 index 0000000..74b3397 --- /dev/null +++ b/libktorrent/torrent/announcelist.cpp @@ -0,0 +1,195 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#if 0 +#include "announcelist.h" +#include "bnode.h" +#include <util/error.h> +#include "globals.h" +#include <util/log.h> + +#include <klocale.h> +#include <qstringlist.h> +#include <qfile.h> +#include <qtextstream.h> + +namespace bt +{ + + AnnounceList::AnnounceList() + :m_datadir(QString::null) + { + curr = 0; + } + + + AnnounceList::~AnnounceList() + { + saveTrackers(); + } + + void AnnounceList::load(BNode* node) + { + BListNode* ml = dynamic_cast<BListNode*>(node); + if (!ml) + return; + + //ml->printDebugInfo(); + for (Uint32 i = 0;i < ml->getNumChildren();i++) + { + BListNode* url = dynamic_cast<BListNode*>(ml->getChild(i)); + if (!url) + throw Error(i18n("Parse Error")); + + for (Uint32 j = 0;j < url->getNumChildren();j++) + { + BValueNode* vn = dynamic_cast<BValueNode*>(url->getChild(j)); + if (!vn) + throw Error(i18n("Parse Error")); + + KURL url(vn->data().toString().stripWhiteSpace()); + trackers.append(url); + //Out() << "Added tracker " << url << endl; + } + } + } + + const KURL::List AnnounceList::getTrackerURLs() + { + KURL::List complete(trackers); + complete += custom_trackers; + return complete; + } + + void AnnounceList::addTracker(KURL url, bool custom) + { + if(custom) + custom_trackers.append(url); + else + trackers.append(url); + } + + bool AnnounceList::removeTracker(KURL url) + { + KURL::List::iterator i = custom_trackers.find(url); + if(i != custom_trackers.end()) + { + custom_trackers.remove(i); + return true; + } + else + return false; + } + + KURL AnnounceList::getTrackerURL(bool last_was_succesfull) const + { + int defaults = trackers.count(); + int customs = custom_trackers.count(); + int total = defaults + customs; + + if (total == 0) + return KURL(); // return invalid url is there are no trackers + + if (last_was_succesfull) + return curr < defaults ? *trackers.at(curr) : *custom_trackers.at(curr % customs); + + curr = (curr + 1) % total; + return curr < defaults ? *trackers.at(curr) : *custom_trackers.at(curr % customs); + } + + void AnnounceList::debugPrintURLList() + { + Out() << "Announce List : " << endl; + for (KURL::List::iterator i = trackers.begin();i != trackers.end();i++) + Out() << "URL : " << *i << endl; + } + + void AnnounceList::saveTrackers() + { + QFile file(m_datadir + "trackers"); + if(!file.open(IO_WriteOnly)) + return; + + QTextStream stream(&file); + for (KURL::List::iterator i = custom_trackers.begin();i != custom_trackers.end();i++) + stream << (*i).prettyURL() << ::endl; + file.close(); + } + + void AnnounceList::loadTrackers() + { + QFile file(m_datadir + "trackers"); + if(!file.open(IO_ReadOnly)) + return; + + QTextStream stream(&file); + while (!stream.atEnd()) + { + KURL url(stream.readLine().stripWhiteSpace()); + custom_trackers.append(url); + } + + file.close(); + } + + void AnnounceList::setDatadir(const QString& theValue) + { + m_datadir = theValue; + loadTrackers(); + } + + void AnnounceList::setTracker(KURL url) + { + int defaults = trackers.count(); + int customs = custom_trackers.count(); + int total = defaults + customs; + + int backup = curr; + + for(curr=0; curr<defaults; ++curr) + { + if( *trackers.at(curr) == url ) + return; + } + + for( ; curr<total; ++curr) + { + if( *custom_trackers.at(curr % customs) == url ) + return; + } + + curr = backup; + } + + void AnnounceList::restoreDefault() + { + curr = 0; + } + + void AnnounceList::merge(const AnnounceList* al) + { + for (Uint32 i = 0;i < al->getNumTrackerURLs();i++) + { + KURL url = *al->trackers.at(i); + if (!trackers.contains(url) && !custom_trackers.contains(url)) + custom_trackers.append(url); + } + } +} +#endif diff --git a/libktorrent/torrent/announcelist.h b/libktorrent/torrent/announcelist.h new file mode 100644 index 0000000..38f9e72 --- /dev/null +++ b/libktorrent/torrent/announcelist.h @@ -0,0 +1,107 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTANNOUNCELIST_H +#define BTANNOUNCELIST_H + +#if 0 +#include <kurl.h> +#include <qstring.h> +#include <interfaces/trackerslist.h> + +namespace bt +{ + class BNode; + + /** + * @author Joris Guisson + * @brief Keep track of a list of trackers + * + * This class keeps track of a list of tracker URL. + */ + class AnnounceList : public kt::TrackersList + { + KURL::List trackers; + KURL::List custom_trackers; + + public: + AnnounceList(); + virtual ~AnnounceList(); + + /** + * Load the list from a bencoded list of lists. + * @param node The BNode + */ + void load(BNode* node); + + /** + * Get a new tracker url. + * @param last_was_succesfull Wether or not the last url was succesfull + * @return An URL + */ + KURL getTrackerURL(bool last_was_succesfull) const; + + + ///Gets a list of trackers (URLs) + const KURL::List getTrackerURLs(); + + ///Adds new tracker URL to the list + void addTracker(KURL url, bool custom = true); + + /** + * Removes a tracker from the list + * @param url Tracker URL to remove from custom trackers list. + * @returns TRUE if URL is in custom list and it is removed or FALSE if it could not be removed or it's a default tracker + */ + bool removeTracker(KURL url); + + ///Changes current tracker + void setTracker(KURL url); + + ///Restores the default torrent tracker + void restoreDefault(); + + /// Get the number of tracker URLs + unsigned int getNumTrackerURLs() const {return trackers.count();} + + void debugPrintURLList(); + + ///Saves custom trackers in a file + void saveTrackers(); + + ///Loads custom trackers from a file + void loadTrackers(); + + void setDatadir(const QString& theValue); + + /** + * Merge an other announce list to this one. + * @param al The AnnounceList + */ + void merge(const AnnounceList* al); + + private: + QString m_datadir; + + }; + +} +#endif + +#endif diff --git a/libktorrent/torrent/authenticate.cpp b/libktorrent/torrent/authenticate.cpp new file mode 100644 index 0000000..14e34ea --- /dev/null +++ b/libktorrent/torrent/authenticate.cpp @@ -0,0 +1,156 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <util/log.h> +#include <mse/streamsocket.h> +#include "authenticate.h" +#include "ipblocklist.h" +#include "peermanager.h" + +namespace bt +{ + + Authenticate::Authenticate(const QString & ip,Uint16 port, + const SHA1Hash & info_hash,const PeerID & peer_id,PeerManager* pman) + : info_hash(info_hash),our_peer_id(peer_id),pman(pman) + { + finished = succes = false; + sock = new mse::StreamSocket(); + host = ip; + this->port = port; + + Out(SYS_CON|LOG_NOTICE) << "Initiating connection to " << host << endl; + + if (sock->connectTo(host,port)) + { + connected(); + } + else if (sock->connecting()) + { + // do nothing the monitor will notify us when we are connected + } + else + { + onFinish(false); + } + } + + Authenticate::~Authenticate() + { + } + + void Authenticate::onReadyWrite() + { +// Out() << "Authenticate::onReadyWrite()" << endl; + if (sock->connectSuccesFull()) + { + connected(); + } + else + { + onFinish(false); + } + } + + void Authenticate::connected() + { + sendHandshake(info_hash,our_peer_id); + } + + void Authenticate::onFinish(bool succes) + { + Out(SYS_CON|LOG_NOTICE) << "Authentication to " << host << " : " << (succes ? "ok" : "failure") << endl; + finished = true; + this->succes = succes; + + if (!succes) + { + sock->deleteLater(); + sock = 0; + } + timer.stop(); + if (pman) + pman->peerAuthenticated(this,succes); + } + + void Authenticate::handshakeRecieved(bool full) + { + const Uint8* hs = handshake; + // Out() << "Authenticate::handshakeRecieved" << endl; + IPBlocklist& ipfilter = IPBlocklist::instance(); + //Out() << "Dodo " << pp.ip << endl; + if (ipfilter.isBlocked(host)) + { + onFinish(false); + return; + } + + SHA1Hash rh(hs+28); + if (rh != info_hash) + { + Out() << "Wrong info_hash : " << rh.toString() << endl; + onFinish(false); + return; + } + + char tmp[21]; + tmp[20] = '\0'; + memcpy(tmp,hs+48,20); + peer_id = PeerID(tmp); + + if (our_peer_id == peer_id /*|| peer_id.startsWith("Yoda")*/) + { + Out(SYS_CON|LOG_DEBUG) << "Lets not connect to our selves " << endl; + onFinish(false); + return; + } + + // check if we aren't already connected to the client + if (pman->connectedTo(peer_id)) + { + Out(SYS_CON|LOG_NOTICE) << "Already connected to " << peer_id.toString() << endl; + onFinish(false); + return; + } + + // only finish when the handshake was fully received + if (full) + onFinish(true); + } + + + mse::StreamSocket* Authenticate::takeSocket() + { + mse::StreamSocket* s = sock; + sock = 0; + return s; + } + + void Authenticate::onPeerManagerDestroyed() + { + // Out(SYS_CON|LOG_NOTICE) << "Authenticate::onPeerManagerDestroyed()" << endl; + pman = 0; + if (finished) + return; + + onFinish(false); + } + +} +#include "authenticate.moc" diff --git a/libktorrent/torrent/authenticate.h b/libktorrent/torrent/authenticate.h new file mode 100644 index 0000000..03c8d75 --- /dev/null +++ b/libktorrent/torrent/authenticate.h @@ -0,0 +1,98 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTAUTHENTICATE_H +#define BTAUTHENTICATE_H + + +#include <util/sha1hash.h> +#include "authenticatebase.h" +#include "globals.h" +#include "peerid.h" + + +namespace bt +{ + + + class PeerManager; + + + /** + * @author Joris Guisson + * @brief Authenicate a peer + * + * After we connect to a peer, + * we need to authenticate the peer. This class handles this. + */ + class Authenticate : public AuthenticateBase + { + Q_OBJECT + public: + + /** + * Connect to a remote host first and authenicate it. + * @param ip IP-address of host + * @param port Port of host + * @param info_hash Info hash + * @param peer_id Peer ID + * @param pman PeerManager + */ + Authenticate(const QString & ip,Uint16 port, + const SHA1Hash & info_hash,const PeerID & peer_id, + PeerManager* pman); + + virtual ~Authenticate(); + + /** + * Get a pointer to the socket, and set it internally + * to NULL. After a succesfull authentication, this is used + * to transfer ownership to a Peer object. + * @return The socket + */ + mse::StreamSocket* takeSocket(); + + const PeerID & getPeerID() const {return peer_id;} + + /// See if the authentication is succesfull + bool isSuccesfull() const {return succes;} + + const QString & getIP() const {return host;} + Uint16 getPort() const {return port;} + + protected slots: + void onReadyWrite(); + void onPeerManagerDestroyed(); + + protected: + void onFinish(bool succes); + void handshakeRecieved(bool full); + virtual void connected(); + + protected: + SHA1Hash info_hash; + PeerID our_peer_id,peer_id; + QString host; + Uint16 port; + bool succes; + PeerManager* pman; + }; +} + +#endif diff --git a/libktorrent/torrent/authenticatebase.cpp b/libktorrent/torrent/authenticatebase.cpp new file mode 100644 index 0000000..9ee2ad7 --- /dev/null +++ b/libktorrent/torrent/authenticatebase.cpp @@ -0,0 +1,159 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <mse/streamsocket.h> +#include <util/sha1hash.h> +#include <util/log.h> +#include <kademlia/dhtbase.h> +#include "globals.h" +#include "peerid.h" +#include "authenticatebase.h" + +namespace bt +{ + + + + AuthenticateBase::AuthenticateBase(mse::StreamSocket* s) : sock(s),finished(false),local(false) + { + connect(&timer,SIGNAL(timeout()),this,SLOT(onTimeout())); + timer.start(20000,true); + memset(handshake,0x00,68); + bytes_of_handshake_recieved = 0; + ext_support = 0; + poll_index = -1; + } + + + AuthenticateBase::~AuthenticateBase() + { + if (sock) + sock->deleteLater(); + } + + void AuthenticateBase::sendHandshake(const SHA1Hash & info_hash,const PeerID & our_peer_id) + { + // Out() << "AuthenticateBase::sendHandshake" << endl; + if (!sock) return; + + Uint8 hs[68]; + makeHandshake(hs,info_hash,our_peer_id); + sock->sendData(hs,68); + } + + void AuthenticateBase::makeHandshake(Uint8* hs,const SHA1Hash & info_hash,const PeerID & our_peer_id) + { + const char* pstr = "BitTorrent protocol"; + hs[0] = 19; + memcpy(hs+1,pstr,19); + memset(hs+20,0x00,8); + if (Globals::instance().getDHT().isRunning()) + hs[27] |= 0x01; // DHT support + + hs[25] |= 0x10; // extension protocol + hs[27] |= 0x04; // fast extensions + memcpy(hs+28,info_hash.getData(),20); + memcpy(hs+48,our_peer_id.data(),20); + } + + void AuthenticateBase::onReadyRead() + { + Uint32 ba = sock->bytesAvailable(); + // Out() << "AuthenticateBase::onReadyRead " << ba << endl; + if (ba == 0) + { + onFinish(false); + return; + } + + if (!sock || finished || ba < 48) + return; + + // first see if we already have some bytes from the handshake + if (bytes_of_handshake_recieved == 0) + { + if (ba < 68) + { + // read partial + sock->readData(handshake,ba); + bytes_of_handshake_recieved += ba; + if (ba >= 27 && handshake[27] & 0x01) + ext_support |= bt::DHT_SUPPORT; + // tell subclasses of a partial handshake + handshakeRecieved(false); + return; + } + else + { + // read full handshake + sock->readData(handshake,68); + } + } + else + { + // read remaining part + Uint32 to_read = 68 - bytes_of_handshake_recieved; + sock->readData(handshake + bytes_of_handshake_recieved,to_read); + } + + if (handshake[0] != 19) + { + onFinish(false); + return; + } + + const char* pstr = "BitTorrent protocol"; + if (memcmp(pstr,handshake+1,19) != 0) + { + onFinish(false); + return; + } + + if (Globals::instance().getDHT().isRunning() && (handshake[27] & 0x01)) + ext_support |= bt::DHT_SUPPORT; + + if (handshake[27] & 0x04) + ext_support |= bt::FAST_EXT_SUPPORT; + + if (handshake[25] & 0x10) + ext_support |= bt::EXT_PROT_SUPPORT; + + handshakeRecieved(true); + } + + void AuthenticateBase::onError(int) + { + if (finished) + return; + onFinish(false); + } + + void AuthenticateBase::onTimeout() + { + if (finished) + return; + + Out(SYS_CON|LOG_DEBUG) << "Timeout occurred" << endl; + onFinish(false); + } + + void AuthenticateBase::onReadyWrite() + {} +} +#include "authenticatebase.moc" diff --git a/libktorrent/torrent/authenticatebase.h b/libktorrent/torrent/authenticatebase.h new file mode 100644 index 0000000..fdab158 --- /dev/null +++ b/libktorrent/torrent/authenticatebase.h @@ -0,0 +1,125 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTAUTHENTICATEBASE_H +#define BTAUTHENTICATEBASE_H + +#include <qobject.h> +#include <qsocket.h> +#include <qtimer.h> +#include <util/constants.h> + + +namespace mse +{ + class StreamSocket; +} + + +namespace bt +{ + class SHA1Hash; + class PeerID; + + /** + * @author Joris Guisson + * + * Base class for authentication classes. This class just groups + * some common stuff between Authenticate and ServerAuthentciate. + * It has a socket, handles the timing out, provides a function to send + * the handshake. + */ + class AuthenticateBase : public QObject + { + Q_OBJECT + public: + AuthenticateBase(mse::StreamSocket* s = 0); + virtual ~AuthenticateBase(); + + /// Set wether this is a local peer + void setLocal(bool loc) {local = loc;} + + /// Is this a local peer + bool isLocal() const {return local;} + + /// See if the authentication is finished + bool isFinished() const {return finished;} + + /// Flags indicating which extensions are supported + Uint32 supportedExtensions() const {return ext_support;} + + /// get teh socket + const mse::StreamSocket* getSocket() const {return sock;} + + /// We can read from the socket + virtual void onReadyRead(); + + /// We can write to the socket (used to detect a succesfull connection) + virtual void onReadyWrite(); + + int getPollIndex() const {return poll_index;} + void setPollIndex(int pi) {poll_index = pi;} + + protected: + /** + * Send a handshake + * @param info_hash The info_hash to include + * @param our_peer_id Our PeerID + */ + void sendHandshake(const SHA1Hash & info_hash,const PeerID & our_peer_id); + + /** + * Authentication finished. + * @param succes Succes or not + */ + virtual void onFinish(bool succes) = 0; + + /** + * The other side send a handshake. The first 20 bytes + * of the handshake will already have been checked. + * @param full Indicates wether we have a full handshake + * if this is not full, we should just send our own + */ + virtual void handshakeRecieved(bool full) = 0; + + /** + * Fill in the handshake in a buffer. + */ + void makeHandshake(bt::Uint8* buf,const SHA1Hash & info_hash,const PeerID & our_peer_id); + + + + protected slots: + void onTimeout(); + void onError(int err); + + protected: + mse::StreamSocket* sock; + QTimer timer; + bool finished; + Uint8 handshake[68]; + Uint32 bytes_of_handshake_recieved; + Uint32 ext_support; + bool local; + int poll_index; + }; + +} + +#endif diff --git a/libktorrent/torrent/authenticationmonitor.cpp b/libktorrent/torrent/authenticationmonitor.cpp new file mode 100644 index 0000000..08215d0 --- /dev/null +++ b/libktorrent/torrent/authenticationmonitor.cpp @@ -0,0 +1,149 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <math.h> +#include <unistd.h> +#include <sys/poll.h> +#include <util/functions.h> +#include <util/log.h> +#include <mse/streamsocket.h> +#include "authenticationmonitor.h" +#include "authenticatebase.h" + +#include <util/profiler.h> + + +namespace bt +{ + AuthenticationMonitor AuthenticationMonitor::self; + + AuthenticationMonitor::AuthenticationMonitor() + {} + + + AuthenticationMonitor::~AuthenticationMonitor() + { + + } + + void AuthenticationMonitor::clear() + { + std::list<AuthenticateBase*>::iterator itr = auths.begin(); + while (itr != auths.end()) + { + AuthenticateBase* ab = *itr; + if (ab) + ab->deleteLater(); + itr++; + } + auths.clear(); + } + + + void AuthenticationMonitor::add(AuthenticateBase* s) + { + auths.push_back(s); + } + + void AuthenticationMonitor::remove(AuthenticateBase* s) + { + auths.remove(s); + } + + void AuthenticationMonitor::update() + { + if (auths.size() == 0) + return; + + int i = 0; + + std::list<AuthenticateBase*>::iterator itr = auths.begin(); + while (itr != auths.end()) + { + AuthenticateBase* ab = *itr; + if (!ab || ab->isFinished()) + { + if (ab) + ab->deleteLater(); + + itr = auths.erase(itr); + } + else + { + ab->setPollIndex(-1); + if (ab->getSocket() && ab->getSocket()->fd() >= 0) + { + int fd = ab->getSocket()->fd(); + if (i >= fd_vec.size()) + { + struct pollfd pfd = {-1,0,0}; + fd_vec.push_back(pfd); + } + + struct pollfd & pfd = fd_vec[i]; + pfd.fd = fd; + pfd.revents = 0; + if (!ab->getSocket()->connecting()) + pfd.events = POLLIN; + else + pfd.events = POLLOUT; + ab->setPollIndex(i); + i++; + } + itr++; + } + } + + if (poll(&fd_vec[0],i,1) > 0) + { + handleData(); + } + } + + void AuthenticationMonitor::handleData() + { + std::list<AuthenticateBase*>::iterator itr = auths.begin(); + while (itr != auths.end()) + { + AuthenticateBase* ab = *itr; + if (ab && ab->getSocket() && ab->getSocket()->fd() >= 0 && ab->getPollIndex() >= 0) + { + int pi = ab->getPollIndex(); + if (fd_vec[pi].revents & POLLIN) + { + ab->onReadyRead(); + } + else if (fd_vec[pi].revents & POLLOUT) + { + ab->onReadyWrite(); + } + } + + if (!ab || ab->isFinished()) + { + if (ab) + ab->deleteLater(); + itr = auths.erase(itr); + } + else + itr++; + } + } + +} diff --git a/libktorrent/torrent/authenticationmonitor.h b/libktorrent/torrent/authenticationmonitor.h new file mode 100644 index 0000000..43a4ebb --- /dev/null +++ b/libktorrent/torrent/authenticationmonitor.h @@ -0,0 +1,80 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTAUTHENTICATIONMONITOR_H +#define BTAUTHENTICATIONMONITOR_H + +#include <list> +#include <vector> + +struct pollfd; + +namespace bt +{ + class AuthenticateBase; + + /** + @author Joris Guisson <joris.guisson@gmail.com> + + Monitors ongoing authentication attempts. This class is a singleton. + */ + class AuthenticationMonitor + { + std::list<AuthenticateBase*> auths; + std::vector<struct pollfd> fd_vec; + + static AuthenticationMonitor self; + + AuthenticationMonitor(); + public: + + virtual ~AuthenticationMonitor(); + + + /** + * Add a new AuthenticateBase object. + * @param s + */ + void add(AuthenticateBase* s); + + /** + * Remove an AuthenticateBase object + * @param s + */ + void remove(AuthenticateBase* s); + + /** + * Check all AuthenticateBase objects. + */ + void update(); + + /** + * Clear all AuthenticateBase objects, also delets them + */ + void clear(); + + static AuthenticationMonitor & instance() {return self;} + + private: + void handleData(); + }; + +} + +#endif diff --git a/libktorrent/torrent/bdecoder.cpp b/libktorrent/torrent/bdecoder.cpp new file mode 100644 index 0000000..6c5a179 --- /dev/null +++ b/libktorrent/torrent/bdecoder.cpp @@ -0,0 +1,224 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <util/log.h> +#include <util/error.h> +#include <klocale.h> +#include "bdecoder.h" +#include "bnode.h" +#include "globals.h" + +namespace bt +{ + + BDecoder::BDecoder(const QByteArray & data,bool verbose,Uint32 off) + : data(data),pos(off),verbose(verbose) + { + } + + + BDecoder::~BDecoder() + {} + + BNode* BDecoder::decode() + { + if (pos >= data.size()) + return 0; + + if (data[pos] == 'd') + { + return parseDict(); + } + else if (data[pos] == 'l') + { + return parseList(); + } + else if (data[pos] == 'i') + { + return parseInt(); + } + else if (data[pos] >= '0' && data[pos] <= '9') + { + return parseString(); + } + else + { + throw Error(i18n("Illegal token: %1").arg(data[pos])); + } + } + + BDictNode* BDecoder::parseDict() + { + Uint32 off = pos; + // we're now entering a dictionary + BDictNode* curr = new BDictNode(off); + pos++; + if (verbose) Out() << "DICT" << endl; + try + { + while (pos < data.size() && data[pos] != 'e') + { + if (verbose) Out() << "Key : " << endl; + BNode* kn = decode(); + BValueNode* k = dynamic_cast<BValueNode*>(kn); + if (!k || k->data().getType() != Value::STRING) + { + delete kn; + throw Error(i18n("Decode error")); + } + + QByteArray key = k->data().toByteArray(); + delete kn; + + BNode* data = decode(); + curr->insert(key,data); + } + pos++; + } + catch (...) + { + delete curr; + throw; + } + if (verbose) Out() << "END" << endl; + curr->setLength(pos - off); + return curr; + } + + BListNode* BDecoder::parseList() + { + Uint32 off = pos; + if (verbose) Out() << "LIST" << endl; + BListNode* curr = new BListNode(off); + pos++; + try + { + while (pos < data.size() && data[pos] != 'e') + { + BNode* n = decode(); + curr->append(n); + } + pos++; + } + catch (...) + { + delete curr; + throw; + } + if (verbose) Out() << "END" << endl; + curr->setLength(pos - off); + return curr; + } + + BValueNode* BDecoder::parseInt() + { + Uint32 off = pos; + pos++; + QString n; + // look for e and add everything between i and e to n + while (pos < data.size() && data[pos] != 'e') + { + n += data[pos]; + pos++; + } + + // check if we aren't at the end of the data + if (pos >= data.size()) + { + throw Error(i18n("Unexpected end of input")); + } + + // try to decode the int + bool ok = true; + int val = 0; + val = n.toInt(&ok); + if (ok) + { + pos++; + if (verbose) Out() << "INT = " << val << endl; + BValueNode* vn = new BValueNode(Value(val),off); + vn->setLength(pos - off); + return vn; + } + else + { + Int64 bi = 0LL; + bi = n.toLongLong(&ok); + if (!ok) + throw Error(i18n("Cannot convert %1 to an int").arg(n)); + + pos++; + if (verbose) Out() << "INT64 = " << n << endl; + BValueNode* vn = new BValueNode(Value(bi),off); + vn->setLength(pos - off); + return vn; + } + } + + BValueNode* BDecoder::parseString() + { + Uint32 off = pos; + // string are encoded 4:spam (length:string) + + // first get length by looking for the : + QString n; + while (pos < data.size() && data[pos] != ':') + { + n += data[pos]; + pos++; + } + // check if we aren't at the end of the data + if (pos >= data.size()) + { + throw Error(i18n("Unexpected end of input")); + } + + // try to decode length + bool ok = true; + int len = 0; + len = n.toInt(&ok); + if (!ok) + { + throw Error(i18n("Cannot convert %1 to an int").arg(n)); + } + // move pos to the first part of the string + pos++; + if (pos + len > data.size()) + throw Error(i18n("Torrent is incomplete!")); + + QByteArray arr(len); + for (unsigned int i = pos;i < pos + len;i++) + arr.at(i-pos) = data[i]; + pos += len; + // read the string into n + + // pos should be positioned right after the string + BValueNode* vn = new BValueNode(Value(arr),off); + vn->setLength(pos - off); + if (verbose) + { + if (arr.size() < 200) + Out() << "STRING " << QString(arr) << endl; + else + Out() << "STRING " << "really long string" << endl; + } + return vn; + } +} + diff --git a/libktorrent/torrent/bdecoder.h b/libktorrent/torrent/bdecoder.h new file mode 100644 index 0000000..dfffce5 --- /dev/null +++ b/libktorrent/torrent/bdecoder.h @@ -0,0 +1,70 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTBDECODER_H +#define BTBDECODER_H + +#include <qstring.h> +#include <util/constants.h> + +namespace bt +{ + + class BNode; + class BListNode; + class BDictNode; + class BValueNode; + + /** + * @author Joris Guisson + * @brief Decodes b-encoded data + * + * Class to decode b-encoded data. + */ + class BDecoder + { + const QByteArray & data; + Uint32 pos; + bool verbose; + public: + /** + * Constructor, passes in the data to decode. + * @param data The data + * @param verbose Verbose output to the log + * @param off Offset to start parsing + */ + BDecoder(const QByteArray & data,bool verbose,Uint32 off = 0); + virtual ~BDecoder(); + + /** + * Decode the data, the root node gets + * returned. (Note that the caller must delete this node) + * @return The root node + */ + BNode* decode(); + private: + BDictNode* parseDict(); + BListNode* parseList(); + BValueNode* parseInt(); + BValueNode* parseString(); + }; + +} + +#endif diff --git a/libktorrent/torrent/bencoder.cpp b/libktorrent/torrent/bencoder.cpp new file mode 100644 index 0000000..e4a80a0 --- /dev/null +++ b/libktorrent/torrent/bencoder.cpp @@ -0,0 +1,137 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include "bencoder.h" +#include <util/file.h> + +namespace bt +{ + + + BEncoderFileOutput::BEncoderFileOutput(File* fptr) : fptr(fptr) + { + } + + void BEncoderFileOutput::write(const char* str,Uint32 len) + { + if (!fptr) + return; + + fptr->write(str,len); + } + + //////////////////////////////////// + + BEncoderBufferOutput::BEncoderBufferOutput(QByteArray & data) : data(data),ptr(0) + { + } + + void BEncoderBufferOutput::write(const char* str,Uint32 len) + { + if (ptr + len > data.size()) + data.resize(ptr + len); + + for (Uint32 i = 0;i < len;i++) + data[ptr++] = str[i]; + } + + //////////////////////////////////// + + BEncoder::BEncoder(File* fptr) : out(0),del(true) + { + out = new BEncoderFileOutput(fptr); + } + + BEncoder::BEncoder(BEncoderOutput* out) : out(out),del(true) + { + } + + + BEncoder::~BEncoder() + { + if (del) + delete out; + } + + void BEncoder::beginDict() + { + if (!out) return; + + out->write("d",1); + } + + void BEncoder::beginList() + { + if (!out) return; + + out->write("l",1); + } + + void BEncoder::write(Uint32 val) + { + if (!out) return; + + QCString s = QString("i%1e").arg(val).utf8(); + out->write(s,s.length()); + } + + void BEncoder::write(Uint64 val) + { + if (!out) return; + + QCString s = QString("i%1e").arg(val).utf8(); + out->write(s,s.length()); + } + + void BEncoder::write(const QString & str) + { + if (!out) return; + + QCString u = str.utf8(); + QCString s = QString("%1:").arg(u.length()).utf8(); + out->write(s,s.length()); + out->write(u,u.length()); + } + + void BEncoder::write(const QByteArray & data) + { + if (!out) return; + + QCString s = QString::number(data.size()).utf8(); + out->write(s,s.length()); + out->write(":",1); + out->write(data.data(),data.size()); + } + + void BEncoder::write(const Uint8* data,Uint32 size) + { + if (!out) return; + + QCString s = QString("%1:").arg(size).utf8(); + out->write(s,s.length()); + out->write((const char*)data,size); + } + + void BEncoder::end() + { + if (!out) return; + + out->write("e",1); + } +} diff --git a/libktorrent/torrent/bencoder.h b/libktorrent/torrent/bencoder.h new file mode 100644 index 0000000..8760d14 --- /dev/null +++ b/libktorrent/torrent/bencoder.h @@ -0,0 +1,150 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTBENCODER_H +#define BTBENCODER_H + + +#include <util/file.h> + + +namespace bt +{ + class File; + + /** + * @author Joris Guisson + * + * Interface for classes which wish to receive the output from a BEncoder. + */ + class BEncoderOutput + { + public: + virtual ~BEncoderOutput() {} + /** + * Write a string of characters. + * @param str The string + * @param len The length of the string + */ + virtual void write(const char* str,Uint32 len) = 0; + }; + + /** + * Writes the output of a bencoder to a file + */ + class BEncoderFileOutput : public BEncoderOutput + { + File* fptr; + public: + BEncoderFileOutput(File* fptr); + + void write(const char* str,Uint32 len); + }; + + /** + * Write the output of a BEncoder to a QByteArray + */ + class BEncoderBufferOutput : public BEncoderOutput + { + QByteArray & data; + Uint32 ptr; + public: + BEncoderBufferOutput(QByteArray & data); + + void write(const char* str,Uint32 len); + }; + + + /** + * @author Joris Guisson + * @brief Helper class to b-encode stuff. + * + * This class b-encodes data. For more details about b-encoding, see + * the BitTorrent protocol docs. The data gets written to a BEncoderOutput + * thing. + */ + class BEncoder + { + BEncoderOutput* out; + bool del; + public: + /** + * Constructor, output gets written to a file. + * @param fptr The File to write to + */ + BEncoder(File* fptr); + + + /** + * Constructor, output gets written to a BEncoderOutput object. + * @param out The BEncoderOutput + */ + BEncoder(BEncoderOutput* out); + virtual ~BEncoder(); + + /** + * Begin a dictionary.Should have a corresponding end call. + */ + void beginDict(); + + /** + * Begin a list. Should have a corresponding end call. + */ + void beginList(); + + /** + * Write an int + * @param val + */ + void write(Uint32 val); + + /** + * Write an int64 + * @param val + */ + void write(Uint64 val); + + /** + * Write a string + * @param str + */ + void write(const QString & str); + + /** + * Write a QByteArray + * @param data + */ + void write(const QByteArray & data); + + /** + * Write a data array + * @param data + * @param size of data + */ + void write(const Uint8* data,Uint32 size); + + /** + * End a beginDict or beginList call. + */ + void end(); + }; + +} + +#endif diff --git a/libktorrent/torrent/bnode.cpp b/libktorrent/torrent/bnode.cpp new file mode 100644 index 0000000..e76dcf3 --- /dev/null +++ b/libktorrent/torrent/bnode.cpp @@ -0,0 +1,177 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <util/log.h> +#include "bnode.h" +#include "globals.h" + +namespace bt +{ + + BNode::BNode(Type type,Uint32 off) : type(type),off(off),len(0) + { + } + + + BNode::~BNode() + {} + + //////////////////////////////////////////////// + + BValueNode::BValueNode(const Value & v,Uint32 off) : BNode(VALUE,off),v(v) + {} + + BValueNode::~BValueNode() + {} + + void BValueNode::printDebugInfo() + { + if (v.getType() == Value::INT) + Out() << "Value = " << v.toInt() << endl; + else + Out() << "Value = " << v.toString() << endl; + } + + //////////////////////////////////////////////// + + BDictNode::BDictNode(Uint32 off) : BNode(DICT,off) + { + } + + BDictNode::~BDictNode() + { + QValueList<DictEntry>::iterator i = children.begin(); + while (i != children.end()) + { + DictEntry & e = *i; + delete e.node; + i++; + } + } + + void BDictNode::insert(const QByteArray & key,BNode* node) + { + DictEntry entry; + entry.key = key; + entry.node = node; + children.append(entry); + } + + BNode* BDictNode::getData(const QString & key) + { + QValueList<DictEntry>::iterator i = children.begin(); + while (i != children.end()) + { + DictEntry & e = *i; + if (QString(e.key) == key) + return e.node; + i++; + } + return 0; + } + + BDictNode* BDictNode::getDict(const QByteArray & key) + { + QValueList<DictEntry>::iterator i = children.begin(); + while (i != children.end()) + { + DictEntry & e = *i; + if (e.key == key) + return dynamic_cast<BDictNode*>(e.node); + i++; + } + return 0; + } + + BListNode* BDictNode::getList(const QString & key) + { + BNode* n = getData(key); + return dynamic_cast<BListNode*>(n); + } + + BDictNode* BDictNode::getDict(const QString & key) + { + BNode* n = getData(key); + return dynamic_cast<BDictNode*>(n); + } + + BValueNode* BDictNode::getValue(const QString & key) + { + BNode* n = getData(key); + return dynamic_cast<BValueNode*>(n); + } + + void BDictNode::printDebugInfo() + { + Out() << "DICT" << endl; + QValueList<DictEntry>::iterator i = children.begin(); + while (i != children.end()) + { + DictEntry & e = *i; + Out() << QString(e.key) << ": " << endl; + e.node->printDebugInfo(); + i++; + } + Out() << "END" << endl; + } + + //////////////////////////////////////////////// + + BListNode::BListNode(Uint32 off) : BNode(LIST,off) + { + children.setAutoDelete(true); + } + + + BListNode::~BListNode() + {} + + + void BListNode::append(BNode* node) + { + children.append(node); + } + + BListNode* BListNode::getList(Uint32 idx) + { + return dynamic_cast<BListNode*>(getChild(idx)); + } + + BDictNode* BListNode::getDict(Uint32 idx) + { + return dynamic_cast<BDictNode*>(getChild(idx)); + } + + BValueNode* BListNode::getValue(Uint32 idx) + { + return dynamic_cast<BValueNode*>(getChild(idx)); + } + + void BListNode::printDebugInfo() + { + Out() << "LIST " << children.count() << endl; + for (Uint32 i = 0;i < children.count();i++) + { + BNode* n = children.at(i); + n->printDebugInfo(); + } + Out() << "END" << endl; + } +} + diff --git a/libktorrent/torrent/bnode.h b/libktorrent/torrent/bnode.h new file mode 100644 index 0000000..685291c --- /dev/null +++ b/libktorrent/torrent/bnode.h @@ -0,0 +1,210 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTBNODE_H +#define BTBNODE_H + +#include <qptrlist.h> +#include <qvaluelist.h> +#include <util/constants.h> +#include "value.h" + + +namespace bt +{ + class BListNode; + + /** + * @author Joris Guisson + * @brief Base class for a node in a b-encoded piece of data + * + * There are 3 possible pieces of data in b-encoded piece of data. + * This is the base class for all those 3 things. + */ + class BNode + { + public: + enum Type + { + VALUE,DICT,LIST + }; + + /** + * Constructor, sets the Type, and the offset into + * the data. + * @param type Type of node + * @param off The offset into the data + */ + BNode(Type type,Uint32 off); + virtual ~BNode(); + + /// Get the type of node + Type getType() const {return type;} + + /// Get the offset in the bytearray where this node starts. + Uint32 getOffset() const {return off;} + + /// Get the length this node takes up in the bytearray. + Uint32 getLength() const {return len;} + + /// Set the length + void setLength(Uint32 l) {len = l;} + + /// Print some debugging info + virtual void printDebugInfo() = 0; + private: + Type type; + Uint32 off,len; + }; + + /** + * @author Joris Guisson + * @brief Represents a value (string,bytearray or int) in bencoded data + * + * @todo Use QVariant + */ + class BValueNode : public BNode + { + Value v; + public: + BValueNode(const Value & v,Uint32 off); + virtual ~BValueNode(); + + const Value & data() const {return v;} + void printDebugInfo(); + }; + + /** + * @author Joris Guisson + * @brief Represents a dictionary in bencoded data + * + */ + class BDictNode : public BNode + { + struct DictEntry + { + QByteArray key; + BNode* node; + }; + QValueList<DictEntry> children; + public: + BDictNode(Uint32 off); + virtual ~BDictNode(); + + /** + * Insert a BNode in the dictionary. + * @param key The key + * @param node The node + */ + void insert(const QByteArray & key,BNode* node); + + /** + * Get a BNode. + * @param key The key + * @return The node or 0 if there is no node with has key @a key + */ + BNode* getData(const QString & key); + + /** + * Get a BListNode. + * @param key The key + * @return The node or 0 if there is no list node with has key @a key + */ + BListNode* getList(const QString & key); + + /** + * Get a BDictNode. + * @param key The key + * @return The node or 0 if there is no dict node with has key @a key + */ + BDictNode* getDict(const QString & key); + + /** + * Get a BDictNode. + * @param key The key + * @return The node or 0 if there is no dict node with has key @a key + */ + BDictNode* getDict(const QByteArray & key); + + /** + * Get a BValueNode. + * @param key The key + * @return The node or 0 if there is no value node with has key @a key + */ + BValueNode* getValue(const QString & key); + + void printDebugInfo(); + }; + + /** + * @author Joris Guisson + * @brief Represents a list in bencoded data + * + */ + class BListNode : public BNode + { + QPtrList<BNode> children; + public: + BListNode(Uint32 off); + virtual ~BListNode(); + + /** + * Append a node to the list. + * @param node The node + */ + void append(BNode* node); + void printDebugInfo(); + + /// Get the number of nodes in the list. + Uint32 getNumChildren() const {return children.count();} + + /** + * Get a node from the list + * @param idx The index + * @return The node or 0 if idx is out of bounds + */ + BNode* getChild(Uint32 idx) {return children.at(idx);} + + /** + * Get a BListNode. + * @param idx The index + * @return The node or 0 if the index is out of bounds or the element + * at postion @a idx isn't a BListNode. + */ + BListNode* getList(Uint32 idx); + + /** + * Get a BDictNode. + * @param idx The index + * @return The node or 0 if the index is out of bounds or the element + * at postion @a idx isn't a BDictNode. + */ + BDictNode* getDict(Uint32 idx); + + /** + * Get a BValueNode. + * @param idx The index + * @return The node or 0 if the index is out of bounds or the element + * at postion @a idx isn't a BValueNode. + */ + BValueNode* getValue(Uint32 idx); + }; +} + +#endif diff --git a/libktorrent/torrent/cache.cpp b/libktorrent/torrent/cache.cpp new file mode 100644 index 0000000..dcf9a77 --- /dev/null +++ b/libktorrent/torrent/cache.cpp @@ -0,0 +1,55 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include "torrent.h" +#include "chunk.h" +#include "cache.h" +#include "peermanager.h" +#include <util/functions.h> + +namespace bt +{ + + Cache::Cache(Torrent & tor,const QString & tmpdir,const QString & datadir) + : tor(tor),tmpdir(tmpdir),datadir(datadir),mmap_failures(0) + { + if (!datadir.endsWith(bt::DirSeparator())) + this->datadir += bt::DirSeparator(); + + if (!tmpdir.endsWith(bt::DirSeparator())) + this->tmpdir += bt::DirSeparator(); + + preexisting_files = false; + } + + + Cache::~Cache() + {} + + + void Cache::changeTmpDir(const QString & ndir) + { + tmpdir = ndir; + } + + bool Cache::mappedModeAllowed() + { + return MaxOpenFiles() - bt::PeerManager::getTotalConnections() < 100; + } +} diff --git a/libktorrent/torrent/cache.h b/libktorrent/torrent/cache.h new file mode 100644 index 0000000..4c373ee --- /dev/null +++ b/libktorrent/torrent/cache.h @@ -0,0 +1,165 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTCACHE_H +#define BTCACHE_H + +#include <kio/job.h> + +class QStringList; + +namespace bt +{ + class Torrent; + class TorrentFile; + class Chunk; + class PreallocationThread; + + + /** + * @author Joris Guisson + * @brief Manages the temporary data + * + * Interface for a class which manages downloaded data. + * Subclasses should implement the load and save methods. + */ + class Cache + { + protected: + Torrent & tor; + QString tmpdir; + QString datadir; + bool preexisting_files; + Uint32 mmap_failures; + public: + Cache(Torrent & tor,const QString & tmpdir,const QString & datadir); + virtual ~Cache(); + + /// Get the datadir + QString getDataDir() const {return datadir;} + + /** + * Get the actual output path. + * @return The output path + */ + virtual QString getOutputPath() const = 0; + + /** + * Changes the tmp dir. All data files should already been moved. + * This just modifies the tmpdir variable. + * @param ndir The new tmpdir + */ + virtual void changeTmpDir(const QString & ndir); + + /** + * Move the data files to a new directory. + * @param ndir The directory + * @return The KIO::Job doing the move + */ + virtual KIO::Job* moveDataFiles(const QString & ndir) = 0; + + /** + * The move data files job is done. + * @param job The job that did it + */ + virtual void moveDataFilesCompleted(KIO::Job* job) = 0; + + /** + * Changes output path. All data files should already been moved. + * This just modifies the datadir variable. + * @param outputpath New output path + */ + virtual void changeOutputPath(const QString & outputpath) = 0; + + /** + * Load a chunk into memory. If something goes wrong, + * an Error should be thrown. + * @param c The Chunk + */ + virtual void load(Chunk* c) = 0; + + /** + * Save a chunk to disk. If something goes wrong, + * an Error should be thrown. + * @param c The Chunk + */ + virtual void save(Chunk* c) = 0; + + /** + * Prepare a chunk for downloading. + * @param c The Chunk + * @return true if ok, false otherwise + */ + virtual bool prep(Chunk* c) = 0; + + /** + * Create all the data files to store the data. + */ + virtual void create() = 0; + + /** + * Close the cache file(s). + */ + virtual void close() = 0; + + /** + * Open the cache file(s) + */ + virtual void open() = 0; + + /// Does nothing, can be overridden to be alerted of download status changes of a TorrentFile + virtual void downloadStatusChanged(TorrentFile*, bool) {}; + + /** + * Preallocate diskspace for all files + * @param prealloc The thread doing the preallocation + */ + virtual void preallocateDiskSpace(PreallocationThread* prealloc) = 0; + + /// See if the download has existing files + bool hasExistingFiles() const {return preexisting_files;} + + + /** + * Test all files and see if they are not missing. + * If so put them in a list + */ + virtual bool hasMissingFiles(QStringList & sl) = 0; + + /** + * Delete all data files, in case of multi file torrents + * empty directories should also be deleted. + */ + virtual void deleteDataFiles() = 0; + + /** + * See if we are allowed to use mmap, when loading chunks. + * This will return false if we are close to system limits. + */ + static bool mappedModeAllowed(); + + /** + * Get the number of bytes all the files of this torrent are currently using on disk. + * */ + virtual Uint64 diskUsage() = 0; + }; + +} + +#endif diff --git a/libktorrent/torrent/cachefile.cpp b/libktorrent/torrent/cachefile.cpp new file mode 100644 index 0000000..6367b7f --- /dev/null +++ b/libktorrent/torrent/cachefile.cpp @@ -0,0 +1,507 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + + +#include <fcntl.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/mman.h> +#include <unistd.h> +#include <errno.h> +#include <qfile.h> +#include <kio/netaccess.h> +#include <klocale.h> +#include <kfileitem.h> +#include <util/array.h> +#include <util/fileops.h> +#include <torrent/globals.h> +#include <interfaces/functions.h> +#include <kapplication.h> +#include <util/log.h> +#include <util/error.h> +#include "cachefile.h" +#include "preallocationthread.h" +#include "settings.h" + + +// Not all systems have an O_LARGEFILE - Solaris depending +// on command-line defines, FreeBSD never - so in those cases, +// make it a zero bitmask. As long as it's only OR'ed into +// open(2) flags, that's fine. +// +#ifndef O_LARGEFILE +#define O_LARGEFILE (0) +#endif + + + + +namespace bt +{ + + CacheFile::CacheFile() : fd(-1),max_size(0),file_size(0),mutex(true) + { + read_only = false; + } + + + CacheFile::~CacheFile() + { + if (fd != -1) + close(); + } + + void CacheFile::changePath(const QString & npath) + { + path = npath; + } + + void CacheFile::openFile(Mode mode) + { + int flags = O_LARGEFILE; + + // by default allways try read write + fd = ::open(QFile::encodeName(path),flags | O_RDWR); + if (fd < 0 && mode == READ) + { + // in case RDWR fails, try readonly if possible + fd = ::open(QFile::encodeName(path),flags | O_RDONLY); + if (fd >= 0) + read_only = true; + } + + if (fd < 0) + { + throw Error(i18n("Cannot open %1 : %2").arg(path).arg(strerror(errno))); + } + + file_size = FileSize(fd); + } + + void CacheFile::open(const QString & path,Uint64 size) + { + QMutexLocker lock(&mutex); + // only set the path and the max size, we only open the file when it is needed + this->path = path; + max_size = size; + } + + void* CacheFile::map(MMappeable* thing,Uint64 off,Uint32 size,Mode mode) + { + QMutexLocker lock(&mutex); + // reopen the file if necessary + if (fd == -1) + { + // Out() << "Reopening " << path << endl; + openFile(mode); + } + + if (read_only && mode != READ) + { + throw Error(i18n("Cannot open %1 for writing : readonly filesystem").arg(path)); + } + + if (off + size > max_size) + { + Out() << "Warning : writing past the end of " << path << endl; + Out() << (off + size) << " " << max_size << endl; + return 0; + } + + int mmap_flag = 0; + switch (mode) + { + case READ: + mmap_flag = PROT_READ; + break; + case WRITE: + mmap_flag = PROT_WRITE; + break; + case RW: + mmap_flag = PROT_READ|PROT_WRITE; + break; + } + + if (off + size > file_size) + { + Uint64 to_write = (off + size) - file_size; + // Out() << "Growing file with " << to_write << " bytes" << endl; + growFile(to_write); + } + + Uint32 page_size = sysconf(_SC_PAGESIZE); + if (off % page_size > 0) + { + // off is not a multiple of the page_size + // so we play around a bit + Uint32 diff = (off % page_size); + Uint64 noff = off - diff; + // Out() << "Offsetted mmap : " << diff << endl; +#if HAVE_MMAP64 + char* ptr = (char*)mmap64(0, size + diff, mmap_flag, MAP_SHARED, fd, noff); +#else + char* ptr = (char*)mmap(0, size + diff, mmap_flag, MAP_SHARED, fd, noff); +#endif + if (ptr == MAP_FAILED) + { + Out() << "mmap failed : " << QString(strerror(errno)) << endl; + return 0; + } + else + { + CacheFile::Entry e; + e.thing = thing; + e.offset = off; + e.diff = diff; + e.ptr = ptr; + e.size = size + diff; + e.mode = mode; + mappings.insert((void*)(ptr + diff),e); + return ptr + diff; + } + } + else + { +#if HAVE_MMAP64 + void* ptr = mmap64(0, size, mmap_flag, MAP_SHARED, fd, off); +#else + void* ptr = mmap(0, size, mmap_flag, MAP_SHARED, fd, off); +#endif + if (ptr == MAP_FAILED) + { + Out() << "mmap failed : " << QString(strerror(errno)) << endl; + return 0; + } + else + { + CacheFile::Entry e; + e.thing = thing; + e.offset = off; + e.ptr = ptr; + e.diff = 0; + e.size = size; + e.mode = mode; + mappings.insert(ptr,e); + return ptr; + } + } + } + + void CacheFile::growFile(Uint64 to_write) + { + // reopen the file if necessary + if (fd == -1) + { + // Out() << "Reopening " << path << endl; + openFile(RW); + } + + if (read_only) + throw Error(i18n("Cannot open %1 for writing : readonly filesystem").arg(path)); + + // jump to the end of the file + SeekFile(fd,0,SEEK_END); + + if (file_size + to_write > max_size) + { + Out() << "Warning : writing past the end of " << path << endl; + Out() << (file_size + to_write) << " " << max_size << endl; + } + + Uint8 buf[1024]; + memset(buf,0,1024); + Uint64 num = to_write; + // write data until to_write is 0 + while (to_write > 0) + { + int nb = to_write > 1024 ? 1024 : to_write; + int ret = ::write(fd,buf,nb); + if (ret < 0) + throw Error(i18n("Cannot expand file %1 : %2").arg(path).arg(strerror(errno))); + else if (ret != nb) + throw Error(i18n("Cannot expand file %1 : incomplete write").arg(path)); + to_write -= nb; + } + file_size += num; +// + // Out() << QString("growing %1 = %2").arg(path).arg(kt::BytesToString(file_size)) << endl; + + if (file_size != FileSize(fd)) + { +// Out() << QString("Homer Simpson %1 %2").arg(file_size).arg(sb.st_size) << endl; + fsync(fd); + if (file_size != FileSize(fd)) + { + throw Error(i18n("Cannot expand file %1").arg(path)); + } + } + } + + void CacheFile::unmap(void* ptr,Uint32 size) + { + int ret = 0; + QMutexLocker lock(&mutex); + // see if it wasn't an offsetted mapping + if (mappings.contains(ptr)) + { + CacheFile::Entry & e = mappings[ptr]; +#if HAVE_MUNMAP64 + if (e.diff > 0) + ret = munmap64((char*)ptr - e.diff,e.size); + else + ret = munmap64(ptr,e.size); +#else + if (e.diff > 0) + ret = munmap((char*)ptr - e.diff,e.size); + else + ret = munmap(ptr,e.size); +#endif + mappings.erase(ptr); + // no mappings, close temporary + if (mappings.count() == 0) + closeTemporary(); + } + else + { +#if HAVE_MUNMAP64 + ret = munmap64(ptr,size); +#else + ret = munmap(ptr,size); +#endif + } + + if (ret < 0) + { + Out(SYS_DIO|LOG_IMPORTANT) << QString("Munmap failed with error %1 : %2").arg(errno).arg(strerror(errno)) << endl; + } + } + + void CacheFile::close() + { + QMutexLocker lock(&mutex); + + if (fd == -1) + return; + + QMap<void*,Entry>::iterator i = mappings.begin(); + while (i != mappings.end()) + { + int ret = 0; + CacheFile::Entry & e = i.data(); +#if HAVE_MUNMAP64 + if (e.diff > 0) + ret = munmap64((char*)e.ptr - e.diff,e.size); + else + ret = munmap64(e.ptr,e.size); +#else + if (e.diff > 0) + ret = munmap((char*)e.ptr - e.diff,e.size); + else + ret = munmap(e.ptr,e.size); +#endif + e.thing->unmapped(); + + i++; + mappings.erase(e.ptr); + + if (ret < 0) + { + Out(SYS_DIO|LOG_IMPORTANT) << QString("Munmap failed with error %1 : %2").arg(errno).arg(strerror(errno)) << endl; + } + } + ::close(fd); + fd = -1; + } + + void CacheFile::read(Uint8* buf,Uint32 size,Uint64 off) + { + QMutexLocker lock(&mutex); + bool close_again = false; + + // reopen the file if necessary + if (fd == -1) + { + // Out() << "Reopening " << path << endl; + openFile(READ); + close_again = true; + } + + if (off >= file_size || off >= max_size) + { + throw Error(i18n("Error : Reading past the end of the file %1").arg(path)); + } + + // jump to right position + SeekFile(fd,(Int64)off,SEEK_SET); + if ((Uint32)::read(fd,buf,size) != size) + { + if (close_again) + closeTemporary(); + + throw Error(i18n("Error reading from %1").arg(path)); + } + + if (close_again) + closeTemporary(); + } + + void CacheFile::write(const Uint8* buf,Uint32 size,Uint64 off) + { + QMutexLocker lock(&mutex); + bool close_again = false; + + // reopen the file if necessary + if (fd == -1) + { + // Out() << "Reopening " << path << endl; + openFile(RW); + close_again = true; + } + + if (read_only) + throw Error(i18n("Cannot open %1 for writing : readonly filesystem").arg(path)); + + if (off + size > max_size) + { + Out() << "Warning : writing past the end of " << path << endl; + Out() << (off + size) << " " << max_size << endl; + } + + if (file_size < off) + { + //Out() << QString("Writing %1 bytes at %2").arg(size).arg(off) << endl; + growFile(off - file_size); + } + + // jump to right position + SeekFile(fd,(Int64)off,SEEK_SET); + int ret = ::write(fd,buf,size); + if (close_again) + closeTemporary(); + + if (ret == -1) + throw Error(i18n("Error writing to %1 : %2").arg(path).arg(strerror(errno))); + else if ((Uint32)ret != size) + { + Out() << QString("Incomplete write of %1 bytes, should be %2").arg(ret).arg(size) << endl; + throw Error(i18n("Error writing to %1").arg(path)); + } + + if (off + size > file_size) + file_size = off + size; + } + + void CacheFile::closeTemporary() + { + if (fd == -1 || mappings.count() > 0) + return; + + ::close(fd); + fd = -1; + } + + + + void CacheFile::preallocate(PreallocationThread* prealloc) + { + QMutexLocker lock(&mutex); + + if (FileSize(path) == max_size) + { + Out(SYS_GEN|LOG_NOTICE) << "File " << path << " already big enough" << endl; + return; + } + + Out(SYS_GEN|LOG_NOTICE) << "Preallocating file " << path << " (" << max_size << " bytes)" << endl; + bool close_again = false; + if (fd == -1) + { + openFile(RW); + close_again = true; + } + + if (read_only) + { + if (close_again) + closeTemporary(); + + throw Error(i18n("Cannot open %1 for writing : readonly filesystem").arg(path)); + } + + try + { + bool res = false; + + #ifdef HAVE_XFS_XFS_H + if( (! res) && Settings::fullDiskPrealloc() && (Settings::fullDiskPreallocMethod() == 1) ) + { + res = XfsPreallocate(fd, max_size); + } + #endif + + if(! res) + { + bt::TruncateFile(fd,max_size,!Settings::fullDiskPrealloc()); + } + } + catch (bt::Error & e) + { + // first attempt failed, must be fat so try that + if (!FatPreallocate(fd,max_size)) + { + if (close_again) + closeTemporary(); + + throw Error(i18n("Cannot preallocate diskspace : %1").arg(strerror(errno))); + } + } + + file_size = FileSize(fd); + Out(SYS_GEN|LOG_DEBUG) << "file_size = " << file_size << endl; + if (close_again) + closeTemporary(); + } + + Uint64 CacheFile::diskUsage() + { + Uint64 ret = 0; + bool close_again = false; + if (fd == -1) + { + openFile(READ); + close_again = true; + } + + struct stat sb; + if (fstat(fd,&sb) == 0) + { + ret = (Uint64)sb.st_blocks * 512; + } + + // Out(SYS_GEN|LOG_NOTICE) << "CF: " << path << " is taking up " << ret << " bytes" << endl; + if (close_again) + closeTemporary(); + + return ret; + } +} diff --git a/libktorrent/torrent/cachefile.h b/libktorrent/torrent/cachefile.h new file mode 100644 index 0000000..9c4ebc6 --- /dev/null +++ b/libktorrent/torrent/cachefile.h @@ -0,0 +1,149 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTCACHEFILE_H +#define BTCACHEFILE_H + +#include <qmap.h> +#include <qmutex.h> +#include <qstring.h> +#include <util/constants.h> + +namespace bt +{ + class PreallocationThread; + + + /** + * Interface which classes must implement to be able to map something from a CacheFile + * It will also be used to notify when things get unmapped or remapped + */ + class MMappeable + { + public: + virtual ~MMappeable() {} + + /** + * When a CacheFile is closed, this will be called on all existing mappings. + */ + virtual void unmapped() = 0; + }; + + /** + @author Joris Guisson <joris.guisson@gmail.com> + + Used by Single and MultiFileCache to write to disk. + */ + class CacheFile + { + public: + CacheFile(); + virtual ~CacheFile(); + + enum Mode + { + READ,WRITE,RW + }; + + + /** + * Open the file. + * @param path Path of the file + * @param size Max size of the file + * @throw Error when something goes wrong + */ + void open(const QString & path,Uint64 size); + + /// Change the path of the file + void changePath(const QString & npath); + + /** + * Map a part of the file into memory, will expand the file + * if it is to small, but will not go past the limit set in open. + * @param thing The thing that wishes to map the mmapping + * @param off Offset into the file + * @param size Size of the region to map + * @param mode How the region will be mapped + * @return A ptr to the mmaped region, or 0 if something goes wrong + */ + void* map(MMappeable* thing,Uint64 off,Uint32 size,Mode mode); + + /** + * Unmap a previously mapped region. + * @param ptr Ptr to the region + * @param size Size of the region + */ + void unmap(void* ptr,Uint32 size); + + /** + * Close the file, everything will be unmapped. + * @param to_be_reopened Indicates if the close is temporarely (i.e. it will be reopened) + */ + void close(); + + /** + * Read from the file. + * @param buf Buffer to store data + * @param size Size to read + * @param off Offset to read from in file + */ + void read(Uint8* buf,Uint32 size,Uint64 off); + + /** + * Write to the file. + * @param buf Buffer to write + * @param size Size to read + * @param off Offset to read from in file + */ + void write(const Uint8* buf,Uint32 size,Uint64 off); + + /** + * Preallocate disk space + */ + void preallocate(PreallocationThread* prealloc); + + /// Get the number of bytes this cache file is taking up + Uint64 diskUsage(); + + private: + void growFile(Uint64 to_write); + void closeTemporary(); + void openFile(Mode mode); + + private: + int fd; + bool read_only; + Uint64 max_size,file_size; + QString path; + struct Entry + { + MMappeable* thing; + void* ptr; + Uint32 size; + Uint64 offset; + Uint32 diff; + Mode mode; + }; + QMap<void*,Entry> mappings; // mappings where offset wasn't a multiple of 4K + mutable QMutex mutex; + }; + +} + +#endif diff --git a/libktorrent/torrent/cap.cpp b/libktorrent/torrent/cap.cpp new file mode 100644 index 0000000..a785520 --- /dev/null +++ b/libktorrent/torrent/cap.cpp @@ -0,0 +1,123 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#if 0 +#include <math.h> +#include "cap.h" + +namespace bt +{ + typedef QValueList<Cap::Entry>::iterator CapItr; + + Cap::Cap(bool percentage_check) : max_bytes_per_sec(0),leftover(0),current_speed(0),percentage_check(percentage_check) + { + timer.update(); + } + + + Cap::~Cap() + {} + + void Cap::setMaxSpeed(Uint32 max) + { + max_bytes_per_sec = max; + // tell everybody to go wild + if (max_bytes_per_sec == 0) + { + CapItr i = entries.begin(); + while (i != entries.end()) + { + Cap::Entry & e = *i; + e.obj->proceed(0); + i++; + } + entries.clear(); + leftover = 0; + } + } + + bool Cap::allow(Cappable* pd,Uint32 bytes) + { + if (max_bytes_per_sec == 0 || (percentage_check && (double)current_speed / (double)max_bytes_per_sec < 0.75)) + { + timer.update(); + return true; + } + + // append pd to queue + entries.append(Cap::Entry(pd,bytes)); + return false; + } + + void Cap::killed(Cappable* pd) + { + CapItr i = entries.begin(); + while (i != entries.end()) + { + Cap::Entry & e = *i; + if (e.obj == pd) + i = entries.erase(i); + else + i++; + } + } + + void Cap::update() + { + if (entries.count() == 0) + { + timer.update(); + return; + } + + // first calculate the time since the last update + double el = timer.getElapsedSinceUpdate(); + + // calculate the number of bytes we can send, including those leftover from the last time + Uint32 nb = (Uint32)round((el / 1000.0) * max_bytes_per_sec) + leftover; + leftover = 0; + // Out() << "nb = " << nb << endl; + + while (entries.count() > 0 && nb > 0) + { + // get the first + Cap::Entry & e = entries.first(); + + if (e.num_bytes <= nb) + { + nb -= e.num_bytes; + // we can send all remaining bytes of the packet + e.obj->proceed(e.num_bytes); + entries.pop_front(); + } + else + { + // sent nb bytes of the packets + e.obj->proceed(nb); + e.num_bytes -= nb; + nb = 0; + } + } + + leftover = nb; + timer.update(); + } + +} +#endif diff --git a/libktorrent/torrent/cap.h b/libktorrent/torrent/cap.h new file mode 100644 index 0000000..a3a365e --- /dev/null +++ b/libktorrent/torrent/cap.h @@ -0,0 +1,113 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTCAP_H +#define BTCAP_H + +#if 0 +#include <qvaluelist.h> +#include <util/timer.h> +#include <util/constants.h> + +namespace bt +{ + /** + * Base class for all cappable objects. + */ + class Cappable + { + public: + /** + * Proceed with doing some bytes + * @param bytes The number of bytes it can do (0 = no limit) + * @return true if finished, false otherwise + */ + virtual void proceed(Uint32 bytes) = 0; + }; + + /** + * @author Joris Guisson <joris.guisson@gmail.com> + * + * A Cap is something which caps something. + */ + class Cap + { + public: + Cap(bool percentage_check); + virtual ~Cap(); + + struct Entry + { + Cappable* obj; + Uint32 num_bytes; + + Entry() : obj(0),num_bytes(0) {} + Entry(Cappable* obj,Uint32 nb) : obj(obj),num_bytes(nb) {} + }; + + /** + * Set the speed cap in bytes per second. 0 indicates + * no limit. + * @param max Maximum number of bytes per second. + */ + void setMaxSpeed(Uint32 max); + + /// Get max bytes/sec + Uint32 getMaxSpeed() const {return max_bytes_per_sec;} + + /// Set the current speed + void setCurrentSpeed(Uint32 cs) {current_speed = cs;} + + /// Get the current speed + Uint32 getCurrrentSpeed() const {return current_speed;} + + /** + * Allow or disallow somebody from proceeding. If somebody + * is disallowed they will be stored in a queue, and will be notified + * when there turn is up. + * @param pd Thing which is doing the request + * @param bytes Bytes it wants to send + * @return true if the piece is allowed or not + */ + bool allow(Cappable* pd,Uint32 bytes); + + /** + * A thing in the queue should call this when it get destroyed. To + * remove them from the queue. + * @param pd The Cappable thing + */ + void killed(Cappable* pd); + + /** + * Update the downloadcap. + */ + void update(); + + private: + QValueList<Entry> entries; + Uint32 max_bytes_per_sec; + Timer timer; + Uint32 leftover; + Uint32 current_speed; + bool percentage_check; + }; + +} +#endif +#endif diff --git a/libktorrent/torrent/choker.cpp b/libktorrent/torrent/choker.cpp new file mode 100644 index 0000000..0cb08e9 --- /dev/null +++ b/libktorrent/torrent/choker.cpp @@ -0,0 +1,86 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + + +#include <qptrlist.h> +#include <interfaces/functions.h> +#include "choker.h" +#include "peermanager.h" +#include "newchokealgorithm.h" +#include "advancedchokealgorithm.h" + +using namespace kt; + +namespace bt +{ + + PeerPtrList::PeerPtrList(PeerCompareFunc pcmp) : pcmp(pcmp) + {} + + PeerPtrList::~PeerPtrList() + {} + + int PeerPtrList::compareItems(QPtrCollection::Item a, QPtrCollection::Item b) + { + if (pcmp) + return pcmp((Peer*)a,(Peer*)b); + else + return CompareVal(a,b); + } + + //////////////////////////////////////////// + + ChokeAlgorithm::ChokeAlgorithm() : opt_unchoked_peer_id(0) + { + } + + ChokeAlgorithm::~ChokeAlgorithm() + { + } + + + ///////////////////////////////// + + Uint32 Choker::num_upload_slots = 2; + + Choker::Choker(PeerManager & pman,ChunkManager & cman) : pman(pman),cman(cman) + { +#ifdef USE_OLD_CHOKE + choke = new NewChokeAlgorithm(); +#else + choke = new AdvancedChokeAlgorithm(); +#endif + } + + + Choker::~Choker() + { + delete choke; + } + + void Choker::update(bool have_all,const kt::TorrentStats & stats) + { + if (have_all) + choke->doChokingSeedingState(pman,cman,stats); + else + choke->doChokingLeechingState(pman,cman,stats); + } + +} diff --git a/libktorrent/torrent/choker.h b/libktorrent/torrent/choker.h new file mode 100644 index 0000000..ba78f3c --- /dev/null +++ b/libktorrent/torrent/choker.h @@ -0,0 +1,123 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTCHOKER_H +#define BTCHOKER_H + +#include <qptrlist.h> +#include <util/constants.h> +#include "peer.h" + +namespace kt +{ + struct TorrentStats; +} + +namespace bt +{ + const Uint32 UNDEFINED_ID = 0xFFFFFFFF; + + class PeerManager; + class ChunkManager; + + + typedef int (*PeerCompareFunc)(Peer* a,Peer* b); + + class PeerPtrList : public QPtrList<Peer> + { + PeerCompareFunc pcmp; + public: + PeerPtrList(PeerCompareFunc pcmp = NULL); + virtual ~PeerPtrList(); + + void setCompareFunc(PeerCompareFunc p) {pcmp = p;} + + virtual int compareItems(QPtrCollection::Item a, QPtrCollection::Item b); + }; + + /** + * Base class for all choke algorithms. + */ + class ChokeAlgorithm + { + protected: + Uint32 opt_unchoked_peer_id; + public: + ChokeAlgorithm(); + virtual ~ChokeAlgorithm(); + + /** + * Do the actual choking when we are still downloading. + * @param pman The PeerManager + * @param cman The ChunkManager + * @param stats The torrent stats + */ + virtual void doChokingLeechingState(PeerManager & pman,ChunkManager & cman,const kt::TorrentStats & stats) = 0; + + /** + * Do the actual choking when we are seeding + * @param pman The PeerManager + * @param cman The ChunkManager + * @param stats The torrent stats + */ + virtual void doChokingSeedingState(PeerManager & pman,ChunkManager & cman,const kt::TorrentStats & stats) = 0; + + /// Get the optimisticly unchoked peer ID + Uint32 getOptimisticlyUnchokedPeerID() const {return opt_unchoked_peer_id;} + }; + + + + /** + * @author Joris Guisson + * @brief Handles the choking + * + * This class handles the choking and unchoking of Peer's. + * This class needs to be updated every 10 seconds. + */ + class Choker + { + ChokeAlgorithm* choke; + PeerManager & pman; + ChunkManager & cman; + static Uint32 num_upload_slots; + public: + Choker(PeerManager & pman,ChunkManager & cman); + virtual ~Choker(); + + /** + * Update which peers are choked or not. + * @param have_all Indicates wether we have the entire file + * @param stats Statistic of the torrent + */ + void update(bool have_all,const kt::TorrentStats & stats); + + /// Get the PeerID of the optimisticly unchoked peer. + Uint32 getOptimisticlyUnchokedPeerID() const {return choke->getOptimisticlyUnchokedPeerID();} + + /// Set the number of upload slots + static void setNumUploadSlots(Uint32 n) {num_upload_slots = n;} + + /// Get the number of upload slots + static Uint32 getNumUploadSlots() {return num_upload_slots;} + }; + +} + +#endif diff --git a/libktorrent/torrent/chunk.cpp b/libktorrent/torrent/chunk.cpp new file mode 100644 index 0000000..6873713 --- /dev/null +++ b/libktorrent/torrent/chunk.cpp @@ -0,0 +1,81 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <util/sha1hash.h> +#include "chunk.h" +#include "globals.h" + + +namespace bt +{ + + Chunk::Chunk(unsigned int index,Uint32 size) + : status(Chunk::NOT_DOWNLOADED),index(index), + data(0),size(size),ref_count(0),priority(NORMAL_PRIORITY) + { + } + + + Chunk::~Chunk() + { + clear(); + } + + void Chunk::setData(Uint8* d,Status nstatus) + { + clear(); + status = nstatus; + data = d; + } + + void Chunk::allocate() + { + clear(); + status = BUFFERED; + data = new Uint8[size]; + } + + void Chunk::clear() + { + if (data) + { + if (status == BUFFERED) + delete [] data; + data = 0; + } + } + + void Chunk::unmapped() + { + setData(0,Chunk::ON_DISK); + } + + bool Chunk::checkHash(const SHA1Hash & h) const + { + if (status != BUFFERED && status != MMAPPED) + { + return false; + } + else + { + return SHA1Hash::generate(data,size) == h; + } + } + +} diff --git a/libktorrent/torrent/chunk.h b/libktorrent/torrent/chunk.h new file mode 100644 index 0000000..0896e96 --- /dev/null +++ b/libktorrent/torrent/chunk.h @@ -0,0 +1,165 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTCHUNK_H +#define BTCHUNK_H + +#include <util/constants.h> +#include "cachefile.h" + +namespace bt +{ + class SHA1Hash; + + /** + * @author Joris Guisson + * @brief Keep track of a piece of the file + * + * Keeps track of a piece of the file. The Chunk has 3 possible states : + * - MMAPPED : It is memory mapped + * - BUFFERED : It is in a buffer in dynamically allocated memory + * (because the chunk is located in 2 or more separate files, so we cannot just set a pointer + * to a region of mmapped memory) + * - ON_DISK : On disk + * - NOT_DOWNLOADED : It hasn't been dowloaded yet, and there is no buffer allocated + */ + class Chunk : public MMappeable + { + public: + Chunk(unsigned int index,Uint32 size); + ~Chunk(); + + enum Status + { + MMAPPED, + BUFFERED, + ON_DISK, + NOT_DOWNLOADED + }; + + /// Get the chunks status. + Status getStatus() const; + + /** + * Set the chunks status + * @param s + */ + void setStatus(Status s); + + /// Get the data + const Uint8* getData() const; + + /// Get the data + Uint8* getData(); + + /// Set the data and the new status + void setData(Uint8* d,Status nstatus); + + /// Clear the chunk (delete data depending on the mode) + void clear(); + + /// Get the chunk's index + Uint32 getIndex() const; + + /// Get the chunk's size + Uint32 getSize() const; + + /// Add one to the reference counter + void ref(); + + /// --reference counter + void unref(); + + /// reference coun > 0 + bool taken() const; + + /// allocate data if not already done, sets the status to buffered + void allocate(); + + /// get chunk priority + Priority getPriority() const; + + /// set chunk priority + void setPriority(Priority newpriority = NORMAL_PRIORITY); + + /// Is chunk excluded + bool isExcluded() const; + + /// Is this a seed only chunk + bool isExcludedForDownloading() const; + + /// In/Exclude chunk + void setExclude(bool yes); + + /** + * Check wehter the chunk matches it's hash. + * @param h The hash + * @return true if the data matches the hash + */ + bool checkHash(const SHA1Hash & h) const; + + private: + virtual void unmapped(); + + private: + Status status; + Uint32 index; + Uint8* data; + Uint32 size; + int ref_count; + Priority priority; + }; + + inline Chunk::Status Chunk::getStatus() const + { + return status; + } + + inline void Chunk::setStatus(Chunk::Status s) + { + status = s; + } + + inline const Uint8* Chunk::getData() const {return data;} + inline Uint8* Chunk::getData() {return data;} + + inline Uint32 Chunk::getIndex() const {return index;} + inline Uint32 Chunk::getSize() const {return size;} + + inline void Chunk::ref() {ref_count++;} + inline void Chunk::unref() {ref_count--;} + inline bool Chunk::taken() const {return ref_count > 0;} + + inline Priority Chunk::getPriority() const {return priority;} + inline void Chunk::setPriority(Priority newpriority) {priority = newpriority;} + inline bool Chunk::isExcluded() const + { + return priority == EXCLUDED; + } + + inline bool Chunk::isExcludedForDownloading() const + { + return priority == ONLY_SEED_PRIORITY; + } + + inline void Chunk::setExclude(bool yes) + {if(yes) priority = EXCLUDED; else priority = NORMAL_PRIORITY;} +} + +#endif diff --git a/libktorrent/torrent/chunkcounter.cpp b/libktorrent/torrent/chunkcounter.cpp new file mode 100644 index 0000000..95b7535 --- /dev/null +++ b/libktorrent/torrent/chunkcounter.cpp @@ -0,0 +1,80 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <util/bitset.h> +#include "chunkcounter.h" + +namespace bt +{ + + ChunkCounter::ChunkCounter(Uint32 num_chunks) : cnt(num_chunks) + { + // fill with 0 + cnt.fill(0); + } + + + ChunkCounter::~ChunkCounter() + { + } + + void ChunkCounter::reset() + { + cnt.fill(0); + } + + void ChunkCounter::incBitSet(const BitSet & bs) + { + for (Uint32 i = 0;i < cnt.size();i++) + { + if(bs.get(i)) + cnt[i]++; + } + } + + void ChunkCounter::decBitSet(const BitSet & bs) + { + for (Uint32 i = 0;i < cnt.size();i++) + { + if(bs.get(i)) + dec(i); + } + } + + void ChunkCounter::inc(Uint32 idx) + { + if (idx < cnt.size()) + cnt[idx]++; + } + + void ChunkCounter::dec(Uint32 idx) + { + if (idx < cnt.size() && cnt[idx] > 0) + cnt[idx]--; + } + + Uint32 ChunkCounter::get(Uint32 idx) const + { + if (idx < cnt.size()) + return cnt[idx]; + else + return 0; + } + +} diff --git a/libktorrent/torrent/chunkcounter.h b/libktorrent/torrent/chunkcounter.h new file mode 100644 index 0000000..ac2ec49 --- /dev/null +++ b/libktorrent/torrent/chunkcounter.h @@ -0,0 +1,83 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTCHUNKCOUNTER_H +#define BTCHUNKCOUNTER_H + +#include <util/constants.h> +#include <util/array.h> + +namespace bt +{ + class BitSet; + + /** + * @author Joris Guisson + * + * Class to keep track of how many peers have a chunk. + */ + class ChunkCounter + { + Array<Uint32> cnt; + public: + ChunkCounter(Uint32 num_chunks); + virtual ~ChunkCounter(); + + /** + * If a bit in the bitset is one, increment the corresponding counter. + * @param bs The BitSet + */ + void incBitSet(const BitSet & bs); + + + /** + * If a bit in the bitset is one, decrement the corresponding counter. + * @param bs The BitSet + */ + void decBitSet(const BitSet & bs); + + /** + * Increment the counter for the idx'th chunk + * @param idx Index of the chunk + */ + void inc(Uint32 idx); + + + /** + * Decrement the counter for the idx'th chunk + * @param idx Index of the chunk + */ + void dec(Uint32 idx); + + + /** + * Get the counter for the idx'th chunk + * @param idx Index of the chunk + */ + Uint32 get(Uint32 idx) const; + + /** + * Reset all values to 0 + */ + void reset(); + }; + +} + +#endif diff --git a/libktorrent/torrent/chunkdownload.cpp b/libktorrent/torrent/chunkdownload.cpp new file mode 100644 index 0000000..51e9db9 --- /dev/null +++ b/libktorrent/torrent/chunkdownload.cpp @@ -0,0 +1,484 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include <algorithm> +#include <util/file.h> +#include <util/log.h> +#include <util/array.h> +#include "chunkdownload.h" +#include "downloader.h" +#include "chunk.h" +#include "peer.h" +#include "peermanager.h" +#include "piece.h" +#include "peerdownloader.h" + +#include <klocale.h> + +namespace bt +{ + + class DownloadStatus : public std::set<Uint32> + { + public: + // typedef std::set<Uint32>::iterator iterator; + + DownloadStatus() + { + + } + + ~DownloadStatus() + { + } + + void add(Uint32 p) + { + insert(p); + } + + void remove(Uint32 p) + { + erase(p); + } + + bool contains(Uint32 p) + { + return count(p) > 0; + } + }; + + ChunkDownload::ChunkDownload(Chunk* chunk) : chunk(chunk) + { + num = num_downloaded = 0; + + num = chunk->getSize() / MAX_PIECE_LEN; + + if (chunk->getSize() % MAX_PIECE_LEN != 0) + { + last_size = chunk->getSize() % MAX_PIECE_LEN; + num++; + } + else + { + last_size = MAX_PIECE_LEN; + } + + pieces = BitSet(num); + pieces.clear(); + + for (Uint32 i = 0;i < num;i++) + piece_queue.append(i); + + dstatus.setAutoDelete(true); + chunk->ref(); + + num_pieces_in_hash = 0; + if (usingContinuousHashing()) + hash_gen.start(); + + } + + ChunkDownload::~ChunkDownload() + { + chunk->unref(); + } + + bool ChunkDownload::piece(const Piece & p,bool & ok) + { + ok = false; + timer.update(); + + Uint32 pp = p.getOffset() / MAX_PIECE_LEN; + if (pieces.get(pp)) + return false; + + + DownloadStatus* ds = dstatus.find(p.getPeer()); + if (ds) + ds->remove(pp); + + Uint8* buf = chunk->getData(); + if (buf) + { + ok = true; + memcpy(buf + p.getOffset(),p.getData(),p.getLength()); + pieces.set(pp,true); + piece_queue.remove(pp); + piece_providers.insert(p.getPeer()); + num_downloaded++; + if (pdown.count() > 1) + { + endgameCancel(p); + } + + if (usingContinuousHashing()) + updateHash(); + + if (num_downloaded >= num) + { + // finalize hash + if (usingContinuousHashing()) + hash_gen.end(); + + releaseAllPDs(); + return true; + } + } + + for (QPtrList<PeerDownloader>::iterator i = pdown.begin();i != pdown.end();++i) + sendRequests(*i); + + return false; + } + + void ChunkDownload::releaseAllPDs() + { + for (Uint32 i = 0;i < pdown.count();i++) + { + PeerDownloader* pd = pdown.at(i); + pd->release(); + disconnect(pd,SIGNAL(timedout(const Request& )),this,SLOT(onTimeout(const Request& ))); + disconnect(pd,SIGNAL(rejected( const Request& )),this,SLOT(onRejected( const Request& ))); + } + dstatus.clear(); + pdown.clear(); + } + + bool ChunkDownload::assignPeer(PeerDownloader* pd) + { + if (!pd || pdown.contains(pd)) + return false; + + pd->grab(); + pdown.append(pd); + dstatus.insert(pd->getPeer()->getID(),new DownloadStatus()); + sendRequests(pd); + connect(pd,SIGNAL(timedout(const Request& )),this,SLOT(onTimeout(const Request& ))); + connect(pd,SIGNAL(rejected( const Request& )),this,SLOT(onRejected( const Request& ))); + return true; + } + + void ChunkDownload::notDownloaded(const Request & r,bool reject) + { + // find the peer + DownloadStatus* ds = dstatus.find(r.getPeer()); + if (ds) + { + // Out() << "ds != 0" << endl; + Uint32 p = r.getOffset() / MAX_PIECE_LEN; + ds->remove(p); + } + + // go over all PD's and do requets again + for (QPtrList<PeerDownloader>::iterator i = pdown.begin();i != pdown.end();++i) + sendRequests(*i); + } + + void ChunkDownload::onRejected(const Request & r) + { + if (chunk->getIndex() == r.getIndex()) + { +// Out(SYS_CON|LOG_DEBUG) << QString("Request rejected %1 %2 %3 %4").arg(r.getIndex()).arg(r.getOffset()).arg(r.getLength()).arg(r.getPeer()) << endl; + + notDownloaded(r,true); + } + } + + void ChunkDownload::onTimeout(const Request & r) + { + // see if we are dealing with a piece of ours + if (chunk->getIndex() == r.getIndex()) + { + Out(SYS_CON|LOG_DEBUG) << QString("Request timed out %1 %2 %3 %4").arg(r.getIndex()).arg(r.getOffset()).arg(r.getLength()).arg(r.getPeer()) << endl; + + notDownloaded(r,false); + } + } + + void ChunkDownload::sendRequests(PeerDownloader* pd) + { + timer.update(); + DownloadStatus* ds = dstatus.find(pd->getPeer()->getID()); + if (!ds) + return; + + // if the peer is choked and we are not downloading an allowed fast chunk + if (pd->isChoked()) + return; + + Uint32 num_visited = 0; + while (num_visited < piece_queue.count() && pd->canAddRequest()) + { + // get the first one in the queue + Uint32 i = piece_queue.first(); + if (!ds->contains(i)) + { + // send request + pd->download( + Request( + chunk->getIndex(), + i*MAX_PIECE_LEN, + i+1<num ? MAX_PIECE_LEN : last_size, + pd->getPeer()->getID())); + ds->add(i); + } + // move to the back so that it will take a while before it's turn is up + piece_queue.pop_front(); + piece_queue.append(i); + num_visited++; + } + + if (piece_queue.count() < 2 && piece_queue.count() > 0) + pd->setNearlyDone(true); + } + + + + void ChunkDownload::update() + { + // go over all PD's and do requets again + for (QPtrList<PeerDownloader>::iterator i = pdown.begin();i != pdown.end();++i) + sendRequests(*i); + } + + + void ChunkDownload::sendCancels(PeerDownloader* pd) + { + DownloadStatus* ds = dstatus.find(pd->getPeer()->getID()); + if (!ds) + return; + + DownloadStatus::iterator itr = ds->begin(); + while (itr != ds->end()) + { + Uint32 i = *itr; + pd->cancel( + Request( + chunk->getIndex(), + i*MAX_PIECE_LEN, + i+1<num ? MAX_PIECE_LEN : last_size,0)); + itr++; + } + ds->clear(); + timer.update(); + } + + void ChunkDownload::endgameCancel(const Piece & p) + { + QPtrList<PeerDownloader>::iterator i = pdown.begin(); + while (i != pdown.end()) + { + PeerDownloader* pd = *i; + DownloadStatus* ds = dstatus.find(pd->getPeer()->getID()); + Uint32 pp = p.getOffset() / MAX_PIECE_LEN; + if (ds && ds->contains(pp)) + { + pd->cancel(Request(p)); + ds->remove(pp); + } + i++; + } + } + + void ChunkDownload::peerKilled(PeerDownloader* pd) + { + if (!pdown.contains(pd)) + return; + + dstatus.erase(pd->getPeer()->getID()); + pdown.remove(pd); + disconnect(pd,SIGNAL(timedout(const Request& )),this,SLOT(onTimeout(const Request& ))); + disconnect(pd,SIGNAL(rejected( const Request& )),this,SLOT(onRejected( const Request& ))); + } + + + const Peer* ChunkDownload::getCurrentPeer() const + { + if (pdown.count() == 0) + return 0; + else + return pdown.getFirst()->getPeer(); + } + + Uint32 ChunkDownload::getChunkIndex() const + { + return chunk->getIndex(); + } + + QString ChunkDownload::getCurrentPeerID() const + { + if (pdown.count() == 0) + { + return QString::null; + } + else if (pdown.count() == 1) + { + const Peer* p = pdown.getFirst()->getPeer(); + return p->getPeerID().identifyClient(); + } + else + { + return i18n("1 peer","%n peers",pdown.count()); + } + } + + Uint32 ChunkDownload::getDownloadSpeed() const + { + Uint32 r = 0; + QPtrList<PeerDownloader>::const_iterator i = pdown.begin(); + while (i != pdown.end()) + { + const PeerDownloader* pd = *i; + r += pd->getPeer()->getDownloadRate(); + i++; + } + return r; + } + + + + void ChunkDownload::save(File & file) + { + ChunkDownloadHeader hdr; + hdr.index = chunk->getIndex(); + hdr.num_bits = pieces.getNumBits(); + hdr.buffered = chunk->getStatus() == Chunk::BUFFERED ? 1 : 0; + // save the chunk header + file.write(&hdr,sizeof(ChunkDownloadHeader)); + // save the bitset + file.write(pieces.getData(),pieces.getNumBytes()); + if (hdr.buffered) + { + // if it's a buffered chunk, save the contents to + file.write(chunk->getData(),chunk->getSize()); + chunk->clear(); + chunk->setStatus(Chunk::ON_DISK); + } + } + + bool ChunkDownload::load(File & file,ChunkDownloadHeader & hdr) + { + // read pieces + if (hdr.num_bits != num) + return false; + + pieces = BitSet(hdr.num_bits); + Array<Uint8> data(pieces.getNumBytes()); + file.read(data,pieces.getNumBytes()); + pieces = BitSet(data,hdr.num_bits); + num_downloaded = pieces.numOnBits(); + if (hdr.buffered) + { + // if it's a buffered chunk, load the data to + if (file.read(chunk->getData(),chunk->getSize()) != chunk->getSize()) + return false; + } + + for (Uint32 i = 0;i < pieces.getNumBits();i++) + if (pieces.get(i)) + piece_queue.remove(i); + + updateHash(); + return true; + } + + Uint32 ChunkDownload::bytesDownloaded() const + { + Uint32 num_bytes = 0; + for (Uint32 i = 0;i < num;i++) + { + if (pieces.get(i)) + { + num_bytes += i == num-1 ? last_size : MAX_PIECE_LEN; + } + } + return num_bytes; + } + + void ChunkDownload::cancelAll() + { + QPtrList<PeerDownloader>::iterator i = pdown.begin(); + while (i != pdown.end()) + { + sendCancels(*i); + i++; + } + } + + bool ChunkDownload::getOnlyDownloader(Uint32 & pid) + { + if (piece_providers.size() == 1) + { + pid = *piece_providers.begin(); + return true; + } + else + { + return false; + } + } + + void ChunkDownload::getStats(Stats & s) + { + s.chunk_index = chunk->getIndex(); + s.current_peer_id = getCurrentPeerID(); + s.download_speed = getDownloadSpeed(); + s.num_downloaders = getNumDownloaders(); + s.pieces_downloaded = num_downloaded; + s.total_pieces = num; + } + + bool ChunkDownload::isChoked() const + { + QPtrList<PeerDownloader>::const_iterator i = pdown.begin(); + while (i != pdown.end()) + { + const PeerDownloader* pd = *i; + // if there is one which isn't choked + if (!pd->isChoked()) + return false; + i++; + } + return true; + } + + void ChunkDownload::updateHash() + { + // update the hash until where we can + Uint32 nn = num_pieces_in_hash; + while (pieces.get(nn) && nn < num) + nn++; + + for (Uint32 i = num_pieces_in_hash;i < nn;i++) + { + const Uint8* data = chunk->getData() + i * MAX_PIECE_LEN; + hash_gen.update(data,i == num - 1 ? last_size : MAX_PIECE_LEN); + } + num_pieces_in_hash = nn; + } + + bool ChunkDownload::usingContinuousHashing() const + { + // if the pieces are larger then 1 MB we will be using the continuous hashing feature + return pieces.getNumBits() > 64; + } +} +#include "chunkdownload.moc" diff --git a/libktorrent/torrent/chunkdownload.h b/libktorrent/torrent/chunkdownload.h new file mode 100644 index 0000000..4119a5b --- /dev/null +++ b/libktorrent/torrent/chunkdownload.h @@ -0,0 +1,207 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTCHUNKDOWNLOAD_H +#define BTCHUNKDOWNLOAD_H + +#include <set> +#include <qobject.h> +#include <qptrlist.h> +#include <util/timer.h> +#include <util/ptrmap.h> +#include <util/sha1hashgen.h> +#include <interfaces/chunkdownloadinterface.h> +#include <util/bitset.h> +#include "globals.h" +#include "peerid.h" + + +namespace bt +{ + + class File; + class Chunk; + class Piece; + class Peer; + class Request; + class PeerDownloader; + class DownloadStatus; + + struct ChunkDownloadHeader + { + Uint32 index; + Uint32 num_bits; + Uint32 buffered; + }; + + + + + /** + * @author Joris Guisson + * @brief Handles the download off one Chunk off a Peer + * + * This class handles the download of one Chunk. + */ + class ChunkDownload : public QObject,public kt::ChunkDownloadInterface + { + Q_OBJECT + public: + /** + * Constructor, set the chunk and the PeerManager. + * @param chunk The Chunk + */ + ChunkDownload(Chunk* chunk); + + virtual ~ChunkDownload(); + + /// Get the chunk + Chunk* getChunk() {return chunk;} + + /// Get the total number of pieces + Uint32 getTotalPieces() const {return num;} + + /// Get the number of pieces downloaded + Uint32 getPiecesDownloaded() const {return num_downloaded;} + + /// Get the number of bytes downloaded. + Uint32 bytesDownloaded() const; + + /// Get the index of the chunk + Uint32 getChunkIndex() const; + + /// Get the current peer + const Peer* getCurrentPeer() const; + + /// Get the PeerID of the current peer + QString getCurrentPeerID() const; + + /// Get the download speed + Uint32 getDownloadSpeed() const; + + /// Get download stats + void getStats(Stats & s); + + /// See if a chunkdownload is idle (i.e. has no downloaders) + bool isIdle() const {return pdown.count() == 0;} + + /** + * A Piece has arived. + * @param p The Piece + * @param ok Wether or not the piece was needed + * @return true If Chunk is complete + */ + bool piece(const Piece & p,bool & ok); + + /** + * Assign the downloader to download from. + * @param pd The downloader + * @return true if the peer was asigned, false if not + */ + bool assignPeer(PeerDownloader* pd); + + Uint32 getNumDownloaders() const {return pdown.count();} + + /** + * A Peer has been killed. We need to remove it's + * PeerDownloader. + * @param pd The PeerDownloader + */ + void peerKilled(PeerDownloader* pd); + + /** + * Save to a File + * @param file The File + */ + void save(File & file); + + /** + * Load from a File + * @param file The File + */ + bool load(File & file,ChunkDownloadHeader & hdr); + + /** + * Cancel all requests. + */ + void cancelAll(); + + /** + * When a Chunk is downloaded, this function checks if all + * pieces are delivered by the same peer and if so sets + * that peers' ID. + * @param pid The peers' ID (!= PeerID) + * @return true if there is only one downloader + */ + bool getOnlyDownloader(Uint32 & pid); + + /// See if a PeerDownloader is assigned to this chunk + bool containsPeer(PeerDownloader *pd) {return pdown.contains(pd);} + + /// See if the download is choked (i.e. all downloaders are choked) + bool isChoked() const; + + /// Release all PD's and clear the requested chunks + void releaseAllPDs(); + + /// Send requests to peers + void update(); + + /// See if this CD hasn't been active in the last update + bool needsToBeUpdated() const {return timer.getElapsedSinceUpdate() > 60 * 1000;} + + /// Get the SHA1 hash of the downloaded chunk + SHA1Hash getHash() const {return hash_gen.get();} + + /// Are we using the continous hashing feature for this chunk + bool usingContinuousHashing() const; + + private slots: + void sendRequests(PeerDownloader* pd); + void sendCancels(PeerDownloader* pd); + void endgameCancel(const Piece & p); + void onTimeout(const Request & r); + void onRejected(const Request & r); + + private: + void notDownloaded(const Request & r,bool reject); + void updateHash(); + + private: + BitSet pieces; + QValueList<Uint32> piece_queue; + Chunk* chunk; + Uint32 num; + Uint32 num_downloaded; + Uint32 last_size; + Timer timer; + QPtrList<PeerDownloader> pdown; + PtrMap<Uint32,DownloadStatus> dstatus; + std::set<Uint32> piece_providers; + + + SHA1HashGen hash_gen; + Uint32 num_pieces_in_hash; + + friend File & operator << (File & out,const ChunkDownload & cd); + friend File & operator >> (File & in,ChunkDownload & cd); + }; +} + +#endif diff --git a/libktorrent/torrent/chunkmanager.cpp b/libktorrent/torrent/chunkmanager.cpp new file mode 100644 index 0000000..08aac97 --- /dev/null +++ b/libktorrent/torrent/chunkmanager.cpp @@ -0,0 +1,1157 @@ +/*************************************************************************** + * Copyright (C) 2005 by * + * Joris Guisson <joris.guisson@gmail.com> * + * Ivan Vasic <ivasic@gmail.com> * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <algorithm> +#include <util/file.h> +#include <util/array.h> +#include <qstringlist.h> +#include "chunkmanager.h" +#include "torrent.h" +#include <util/error.h> +#include <util/bitset.h> +#include <util/fileops.h> +#include "singlefilecache.h" +#include "multifilecache.h" +#include <util/log.h> +#include <util/functions.h> +#include "globals.h" + +#include <klocale.h> + +namespace bt +{ + + Uint32 ChunkManager::max_chunk_size_for_data_check = 0; + + + ChunkManager::ChunkManager( + Torrent & tor, + const QString & tmpdir, + const QString & datadir, + bool custom_output_name) + : tor(tor),chunks(tor.getNumChunks()), + bitset(tor.getNumChunks()),excluded_chunks(tor.getNumChunks()),only_seed_chunks(tor.getNumChunks()),todo(tor.getNumChunks()) + { + during_load = false; + only_seed_chunks.setAll(false); + todo.setAll(true); + if (tor.isMultiFile()) + cache = new MultiFileCache(tor,tmpdir,datadir,custom_output_name); + else + cache = new SingleFileCache(tor,tmpdir,datadir); + + index_file = tmpdir + "index"; + file_info_file = tmpdir + "file_info"; + file_priority_file = tmpdir + "file_priority"; + Uint64 tsize = tor.getFileLength(); // total size + Uint64 csize = tor.getChunkSize(); // chunk size + Uint64 lsize = tsize - (csize * (tor.getNumChunks() - 1)); // size of last chunk + + for (Uint32 i = 0;i < tor.getNumChunks();i++) + { + if (i + 1 < tor.getNumChunks()) + chunks.insert(i,new Chunk(i,csize)); + else + chunks.insert(i,new Chunk(i,lsize)); + } + chunks.setAutoDelete(true); + chunks_left = 0; + recalc_chunks_left = true; + corrupted_count = recheck_counter = 0; + + for (Uint32 i = 0;i < tor.getNumFiles();i++) + { + TorrentFile & tf = tor.getFile(i); + connect(&tf,SIGNAL(downloadPriorityChanged(TorrentFile*, Priority, Priority )), + this,SLOT(downloadPriorityChanged(TorrentFile*, Priority, Priority ))); + + if (tf.getPriority() != NORMAL_PRIORITY) + { + downloadPriorityChanged(&tf,tf.getPriority(),tf.getOldPriority()); + } + } + + if(tor.isMultiFile()) + { + for(Uint32 i=0; i<tor.getNumFiles(); ++i) + { + bt::TorrentFile & file = tor.getFile(i); + if (!file.isMultimedia() || file.getPriority() == bt::ONLY_SEED_PRIORITY) + continue; + + if (file.getFirstChunk() == file.getLastChunk()) + { + // prioritise whole file + prioritise(file.getFirstChunk(),file.getLastChunk(),PREVIEW_PRIORITY); + } + else + { + Uint32 chunkOffset; + chunkOffset = ((file.getLastChunk() - file.getFirstChunk()) / 100) + 1; + prioritise(file.getFirstChunk(), file.getFirstChunk()+chunkOffset, PREVIEW_PRIORITY); + if (file.getLastChunk() - file.getFirstChunk() > chunkOffset) + { + prioritise(file.getLastChunk() - chunkOffset, file.getLastChunk(), PREVIEW_PRIORITY); + } + } + } + } + else + { + if(tor.isMultimedia()) + { + Uint32 chunkOffset; + chunkOffset = (tor.getNumChunks() / 100) + 1; + + prioritise(0,chunkOffset,PREVIEW_PRIORITY); + if (tor.getNumChunks() > chunkOffset) + { + prioritise(tor.getNumChunks() - chunkOffset, tor.getNumChunks() - 1,PREVIEW_PRIORITY); + } + } + } + } + + + ChunkManager::~ChunkManager() + { + delete cache; + } + + QString ChunkManager::getDataDir() const + { + return cache->getDataDir(); + } + + void ChunkManager::changeDataDir(const QString & data_dir) + { + cache->changeTmpDir(data_dir); + index_file = data_dir + "index"; + file_info_file = data_dir + "file_info"; + file_priority_file = data_dir + "file_priority"; + } + + KIO::Job* ChunkManager::moveDataFiles(const QString & ndir) + { + return cache->moveDataFiles(ndir); + } + + void ChunkManager::moveDataFilesCompleted(KIO::Job* job) + { + cache->moveDataFilesCompleted(job); + } + + void ChunkManager::changeOutputPath(const QString & output_path) + { + cache->changeOutputPath(output_path); + } + + void ChunkManager::loadIndexFile() + { + during_load = true; + loadPriorityInfo(); + + File fptr; + if (!fptr.open(index_file,"rb")) + { + // no index file, so assume it's empty + bt::Touch(index_file,true); + Out(SYS_DIO|LOG_IMPORTANT) << "Can't open index file : " << fptr.errorString() << endl; + during_load = false; + return; + } + + if (fptr.seek(File::END,0) != 0) + { + fptr.seek(File::BEGIN,0); + + while (!fptr.eof()) + { + NewChunkHeader hdr; + fptr.read(&hdr,sizeof(NewChunkHeader)); + Chunk* c = getChunk(hdr.index); + if (c) + { + c->setStatus(Chunk::ON_DISK); + bitset.set(hdr.index,true); + todo.set(hdr.index,false); + recalc_chunks_left = true; + } + } + } + tor.updateFilePercentage(bitset); + during_load = false; + } + + void ChunkManager::saveIndexFile() + { + File fptr; + if (!fptr.open(index_file,"wb")) + throw Error(i18n("Cannot open index file %1 : %2").arg(index_file).arg(fptr.errorString())); + + for (unsigned int i = 0;i < tor.getNumChunks();i++) + { + Chunk* c = getChunk(i); + if (c->getStatus() != Chunk::NOT_DOWNLOADED) + { + NewChunkHeader hdr; + hdr.index = i; + fptr.write(&hdr,sizeof(NewChunkHeader)); + } + } + savePriorityInfo(); + } + + void ChunkManager::createFiles(bool check_priority) + { + if (!bt::Exists(index_file)) + { + File fptr; + fptr.open(index_file,"wb"); + } + cache->create(); + if (check_priority) + { + for (Uint32 i = 0;i < tor.getNumFiles();i++) + { + TorrentFile & tf = tor.getFile(i); + connect(&tf,SIGNAL(downloadPriorityChanged(TorrentFile*, Priority, Priority )), + this,SLOT(downloadPriorityChanged(TorrentFile*, Priority, Priority ))); + + if (tf.getPriority() != NORMAL_PRIORITY) + { + downloadPriorityChanged(&tf,tf.getPriority(),tf.getOldPriority()); + } + } + } + } + + bool ChunkManager::hasMissingFiles(QStringList & sl) + { + return cache->hasMissingFiles(sl); + } + + Chunk* ChunkManager::getChunk(unsigned int i) + { + if (i >= chunks.count()) + return 0; + else + return chunks[i]; + } + + void ChunkManager::start() + { + cache->open(); + } + + void ChunkManager::stop() + { + // unmmap all chunks which can + for (Uint32 i = 0;i < bitset.getNumBits();i++) + { + Chunk* c = chunks[i]; + if (c->getStatus() == Chunk::MMAPPED) + { + cache->save(c); + c->clear(); + c->setStatus(Chunk::ON_DISK); + } + else if (c->getStatus() == Chunk::BUFFERED) + { + c->clear(); + c->setStatus(Chunk::ON_DISK); + } + } + cache->close(); + } + + Chunk* ChunkManager::grabChunk(unsigned int i) + { + if (i >= chunks.size()) + return 0; + + Chunk* c = chunks[i]; + if (c->getStatus() == Chunk::NOT_DOWNLOADED || c->isExcluded()) + { + return 0; + } + else if (c->getStatus() == Chunk::ON_DISK) + { + // load the chunk if it is on disk + cache->load(c); + loaded.insert(i,bt::GetCurrentTime()); + bool check_allowed = (max_chunk_size_for_data_check == 0 || tor.getChunkSize() <= max_chunk_size_for_data_check); + + // when no corruptions have been found, only check once every 5 chunks + if (check_allowed && recheck_counter < 5 && corrupted_count == 0) + check_allowed = false; + + if (c->getData() && check_allowed) + { + recheck_counter = 0; + if (!c->checkHash(tor.getHash(i))) + { + Out(SYS_DIO|LOG_IMPORTANT) << "Chunk " << i + << " has been found invalid, redownloading" << endl; + + resetChunk(i); + tor.updateFilePercentage(i,bitset); + saveIndexFile(); + recalc_chunks_left = true; + corrupted_count++; + corrupted(i); + return 0; + } + } + else + { + recheck_counter++; + } + } + + loaded.insert(i,bt::GetCurrentTime()); + return c; + } + + void ChunkManager::releaseChunk(unsigned int i) + { + if (i >= chunks.size()) + return; + + Chunk* c = chunks[i]; + if (!c->taken()) + { + if (c->getStatus() == Chunk::MMAPPED) + cache->save(c); + c->clear(); + c->setStatus(Chunk::ON_DISK); + loaded.remove(i); + } + } + + void ChunkManager::resetChunk(unsigned int i) + { + if (i >= chunks.size()) + return; + + Chunk* c = chunks[i]; + if (c->getStatus() == Chunk::MMAPPED) + cache->save(c); + c->clear(); + c->setStatus(Chunk::NOT_DOWNLOADED); + bitset.set(i,false); + todo.set(i,!excluded_chunks.get(i) && !only_seed_chunks.get(i)); + loaded.remove(i); + tor.updateFilePercentage(i,bitset); + } + + void ChunkManager::checkMemoryUsage() + { + Uint32 num_removed = 0; + QMap<Uint32,TimeStamp>::iterator i = loaded.begin(); + while (i != loaded.end()) + { + Chunk* c = chunks[i.key()]; + // get rid of chunk if nobody asked for it in the last 5 seconds + if (!c->taken() && bt::GetCurrentTime() - i.data() > 5000) + { + if (c->getStatus() == Chunk::MMAPPED) + cache->save(c); + c->clear(); + c->setStatus(Chunk::ON_DISK); + QMap<Uint32,TimeStamp>::iterator j = i; + i++; + loaded.erase(j); + num_removed++; + } + else + { + i++; + } + } + // Uint32 num_in_mem = loaded.count(); + // Out() << QString("Cleaned %1 chunks, %2 still in memory").arg(num_removed).arg(num_in_mem) << endl; + } + + void ChunkManager::saveChunk(unsigned int i,bool update_index) + { + if (i >= chunks.size()) + return; + + Chunk* c = chunks[i]; + if (!c->isExcluded()) + { + cache->save(c); + + // update the index file + if (update_index) + { + bitset.set(i,true); + todo.set(i,false); + recalc_chunks_left = true; + writeIndexFileEntry(c); + tor.updateFilePercentage(i,bitset); + } + } + else + { + c->clear(); + c->setStatus(Chunk::NOT_DOWNLOADED); + Out(SYS_DIO|LOG_IMPORTANT) << "Warning: attempted to save a chunk which was excluded" << endl; + } + } + + void ChunkManager::writeIndexFileEntry(Chunk* c) + { + File fptr; + if (!fptr.open(index_file,"r+b")) + { + // no index file, so assume it's empty + bt::Touch(index_file,true); + Out(SYS_DIO|LOG_IMPORTANT) << "Can't open index file : " << fptr.errorString() << endl; + // try again + if (!fptr.open(index_file,"r+b")) + // panick if it failes + throw Error(i18n("Cannot open index file %1 : %2").arg(index_file).arg(fptr.errorString())); + } + + + fptr.seek(File::END,0); + NewChunkHeader hdr; + hdr.index = c->getIndex(); + fptr.write(&hdr,sizeof(NewChunkHeader)); + } + + Uint32 ChunkManager::onlySeedChunks() const + { + return only_seed_chunks.numOnBits(); + } + + bool ChunkManager::completed() const + { + return todo.numOnBits() == 0 && bitset.numOnBits() > 0; + } + + Uint64 ChunkManager::bytesLeft() const + { + Uint32 num_left = bitset.getNumBits() - bitset.numOnBits(); + Uint32 last = chunks.size() - 1; + if (last < chunks.size() && !bitset.get(last)) + { + Chunk* c = chunks[last]; + if (c) + return (num_left - 1)*tor.getChunkSize() + c->getSize(); + else + return num_left*tor.getChunkSize(); + } + else + { + return num_left*tor.getChunkSize(); + } + } + + Uint64 ChunkManager::bytesLeftToDownload() const + { + Uint32 num_left = todo.numOnBits(); + Uint32 last = chunks.size() - 1; + if (last < chunks.size() && todo.get(last)) + { + Chunk* c = chunks[last]; + if (c) + return (num_left - 1)*tor.getChunkSize() + c->getSize(); + else + return num_left*tor.getChunkSize(); + } + else + { + return num_left*tor.getChunkSize(); + } + } + + Uint32 ChunkManager::chunksLeft() const + { + if (!recalc_chunks_left) + return chunks_left; + + Uint32 num = 0; + Uint32 tot = chunks.size(); + for (Uint32 i = 0;i < tot;i++) + { + const Chunk* c = chunks[i]; + if (!bitset.get(i) && !c->isExcluded()) + num++; + } + chunks_left = num; + recalc_chunks_left = false; + return num; + } + + bool ChunkManager::haveAllChunks() const + { + return bitset.numOnBits() == bitset.getNumBits(); + } + + Uint64 ChunkManager::bytesExcluded() const + { + Uint64 excl = 0; + if (excluded_chunks.get(tor.getNumChunks() - 1)) + { + Chunk* c = chunks[tor.getNumChunks() - 1]; + Uint32 num = excluded_chunks.numOnBits() - 1; + excl = tor.getChunkSize() * num + c->getSize(); + } + else + { + excl = tor.getChunkSize() * excluded_chunks.numOnBits(); + } + + if (only_seed_chunks.get(tor.getNumChunks() - 1)) + { + Chunk* c = chunks[tor.getNumChunks() - 1]; + Uint32 num = only_seed_chunks.numOnBits() - 1; + excl += tor.getChunkSize() * num + c->getSize(); + } + else + { + excl += tor.getChunkSize() * only_seed_chunks.numOnBits(); + } + return excl; + } + + Uint32 ChunkManager::chunksExcluded() const + { + return excluded_chunks.numOnBits() + only_seed_chunks.numOnBits(); + } + + Uint32 ChunkManager::chunksDownloaded() const + { + return bitset.numOnBits(); + } + + void ChunkManager::debugPrintMemUsage() + { + Out(SYS_DIO|LOG_DEBUG) << "Active Chunks : " << loaded.count()<< endl; + } + + void ChunkManager::prioritise(Uint32 from,Uint32 to,Priority priority) + { + if (from > to) + std::swap(from,to); + + Uint32 i = from; + while (i <= to && i < chunks.count()) + { + Chunk* c = chunks[i]; + c->setPriority(priority); + + if (priority == ONLY_SEED_PRIORITY) + { + only_seed_chunks.set(i,true); + todo.set(i,false); + } + else if (priority == EXCLUDED) + { + only_seed_chunks.set(i,false); + todo.set(i,false); + } + else + { + only_seed_chunks.set(i,false); + todo.set(i,!bitset.get(i)); + } + + i++; + } + updateStats(); + } + + void ChunkManager::exclude(Uint32 from,Uint32 to) + { + if (from > to) + std::swap(from,to); + + Uint32 i = from; + while (i <= to && i < chunks.count()) + { + Chunk* c = chunks[i]; + c->setExclude(true); + excluded_chunks.set(i,true); + only_seed_chunks.set(i,false); + todo.set(i,false); + bitset.set(i,false); + i++; + } + recalc_chunks_left = true; + excluded(from,to); + updateStats(); + } + + void ChunkManager::include(Uint32 from,Uint32 to) + { + if (from > to) + std::swap(from,to); + + Uint32 i = from; + while (i <= to && i < chunks.count()) + { + Chunk* c = chunks[i]; + c->setExclude(false); + excluded_chunks.set(i,false); + if (!bitset.get(i)) + todo.set(i,true); + i++; + } + recalc_chunks_left = true; + updateStats(); + included(from,to); + } + + void ChunkManager::saveFileInfo() + { + // saves which TorrentFiles do not need to be downloaded + File fptr; + if (!fptr.open(file_info_file,"wb")) + { + Out(SYS_DIO|LOG_IMPORTANT) << "Warning : Can't save chunk_info file : " << fptr.errorString() << endl; + return; + } + + // first write the number of excluded ones + // don't know this yet, so write 0 for the time being + Uint32 tmp = 0; + fptr.write(&tmp,sizeof(Uint32)); + + Uint32 i = 0; + Uint32 cnt = 0; + while (i < tor.getNumFiles()) + { + if (tor.getFile(i).doNotDownload()) + { + fptr.write(&i,sizeof(Uint32)); + cnt++; + } + i++; + } + + // go back to the beginning and write the number of files + fptr.seek(File::BEGIN,0); + fptr.write(&cnt,sizeof(Uint32)); + fptr.flush(); + } + + void ChunkManager::loadFileInfo() + { + if (during_load) + return; + + File fptr; + if (!fptr.open(file_info_file,"rb")) + return; + + Uint32 num = 0,tmp = 0; + // first read the number of dnd files + if (fptr.read(&num,sizeof(Uint32)) != sizeof(Uint32)) + { + Out(SYS_DIO|LOG_IMPORTANT) << "Warning : error reading chunk_info file" << endl; + return; + } + + for (Uint32 i = 0;i < num;i++) + { + if (fptr.read(&tmp,sizeof(Uint32)) != sizeof(Uint32)) + { + Out(SYS_DIO|LOG_IMPORTANT) << "Warning : error reading chunk_info file" << endl; + return; + } + + bt::TorrentFile & tf = tor.getFile(tmp); + if (!tf.isNull()) + { + Out(SYS_DIO|LOG_DEBUG) << "Excluding : " << tf.getPath() << endl; + tf.setDoNotDownload(true); + } + } + } + + void ChunkManager::savePriorityInfo() + { + if (during_load) + return; + + //save priority info and call saveFileInfo + saveFileInfo(); + File fptr; + if (!fptr.open(file_priority_file,"wb")) + { + Out(SYS_DIO|LOG_IMPORTANT) << "Warning : Can't save chunk_info file : " << fptr.errorString() << endl; + return; + } + + try + { + // first write the number of excluded ones + // don't know this yet, so write 0 for the time being + Uint32 tmp = 0; + fptr.write(&tmp,sizeof(Uint32)); + + Uint32 i = 0; + Uint32 cnt = 0; + while (i < tor.getNumFiles()) + { + const TorrentFile & tf = tor.getFile(i); + if (tf.getPriority() != NORMAL_PRIORITY) + { + tmp = tf.getPriority(); + fptr.write(&i,sizeof(Uint32)); + fptr.write(&tmp,sizeof(Uint32)); + cnt+=2; + } + i++; + } + + // go back to the beginning and write the number of items + fptr.seek(File::BEGIN,0); + fptr.write(&cnt,sizeof(Uint32)); + fptr.flush(); + } + catch (bt::Error & err) + { + Out(SYS_DIO|LOG_IMPORTANT) << "Failed to save priority file " << err.toString() << endl; + bt::Delete(file_priority_file,true); + } + } + + void ChunkManager::loadPriorityInfo() + { + //load priority info and if that fails load file info + File fptr; + if (!fptr.open(file_priority_file,"rb")) + { + loadFileInfo(); + return; + } + + Uint32 num = 0; + // first read the number of lines + if (fptr.read(&num,sizeof(Uint32)) != sizeof(Uint32) || num > 2*tor.getNumFiles()) + { + Out(SYS_DIO|LOG_IMPORTANT) << "Warning : error reading chunk_info file" << endl; + loadFileInfo(); + return; + } + + Array<Uint32> buf(num); + if (fptr.read(buf,sizeof(Uint32)*num) != sizeof(Uint32)*num) + { + Out(SYS_DIO|LOG_IMPORTANT) << "Warning : error reading chunk_info file" << endl; + loadFileInfo(); + return; + } + + fptr.close(); + + for (Uint32 i = 0;i < num;i += 2) + { + Uint32 idx = buf[i]; + if (idx >= tor.getNumFiles()) + { + Out(SYS_DIO|LOG_IMPORTANT) << "Warning : error reading chunk_info file" << endl; + loadFileInfo(); + return; + } + + bt::TorrentFile & tf = tor.getFile(idx); + + if (!tf.isNull()) + { + // numbers are to be compatible with old chunk info files + switch(buf[i+1]) + { + case FIRST_PRIORITY: + case 3: + tf.setPriority(FIRST_PRIORITY); + break; + case NORMAL_PRIORITY: + case 2: + tf.setPriority(NORMAL_PRIORITY); + break; + case EXCLUDED: + case 0: + //tf.setDoNotDownload(true); + tf.setPriority(EXCLUDED); + break; + case ONLY_SEED_PRIORITY: + case -1: + tf.setPriority(ONLY_SEED_PRIORITY); + break; + default: + tf.setPriority(LAST_PRIORITY); + break; + } + } + } + } + + void ChunkManager::downloadStatusChanged(TorrentFile* tf,bool download) + { + Uint32 first = tf->getFirstChunk(); + Uint32 last = tf->getLastChunk(); + if (download) + { + // include the chunks + include(first,last); + + // if it is a multimedia file, prioritise first and last chunks of file + if (tf->isMultimedia()) + { + Uint32 chunkOffset; + chunkOffset = ((last - first) / 100) + 1; + + prioritise(first,first+chunkOffset,PREVIEW_PRIORITY); + if (last - first > 2) + { + prioritise(last - chunkOffset, last, PREVIEW_PRIORITY); + //prioritise(last -1,last, PREVIEW_PRIORITY); + } + } + } + else + { + // Out(SYS_DIO|LOG_DEBUG) << "Excluding chunks " << first << " to " << last << endl; + // first and last chunk may be part of multiple files + // so we can't just exclude them + QValueList<Uint32> files,last_files; + + // get list of files where first chunk lies in + tor.calcChunkPos(first,files); + tor.calcChunkPos(last,last_files); + // check for exceptional case which causes very long loops + if (first == last && files.count() > 1) + { + cache->downloadStatusChanged(tf,download); + savePriorityInfo(); + return; + } + + // go over all chunks from first to last and mark them as not downloaded + // (first and last not included) + for (Uint32 i = first + 1;i < last;i++) + resetChunk(i); + + // if the first chunk only lies in one file, reset it + if (files.count() == 1 && first != 0) + { + // Out(SYS_DIO|LOG_DEBUG) << "Resetting first " << first << endl; + resetChunk(first); + } + + // if the last chunk only lies in one file reset it + if (last != first && last_files.count() == 1) + { + // Out(SYS_DIO|LOG_DEBUG) << "Resetting last " << last << endl; + resetChunk(last); + } + + Priority maxp = ONLY_SEED_PRIORITY; + bool reprioritise_border_chunk = false; + bool modified = false; + + // if one file in the list needs to be downloaded,increment first + for (QValueList<Uint32>::iterator i = files.begin();i != files.end();i++) + { + if (*i == tf->getIndex()) + continue; + + const TorrentFile & other = tor.getFile(*i); + if (!other.doNotDownload()) + { + if (first != last && !modified) + { + first++; + reprioritise_border_chunk = true; + modified = true; + } + + if (other.getPriority() > maxp) + maxp = other.getPriority(); + } + } + + // in case we have incremented first, we better reprioritise the border chunk + if (reprioritise_border_chunk) + prioritise(first-1,first-1,maxp); + + maxp = ONLY_SEED_PRIORITY; + reprioritise_border_chunk = false; + modified = false; + + // if one file in the list needs to be downloaded,decrement last + for (QValueList<Uint32>::iterator i = last_files.begin();i != last_files.end();i++) + { + if (*i == tf->getIndex()) + continue; + + const TorrentFile & other = tor.getFile(*i); + if (!other.doNotDownload()) + { + if (first != last && last > 0 && !modified) + { + last--; + reprioritise_border_chunk = true; + modified = true; + } + + if (other.getPriority() > maxp) + maxp = other.getPriority(); + } + } + + if (reprioritise_border_chunk) + prioritise(last+1,last+1,maxp); + + // last smaller then first is not normal, so just return + if (last < first) + { + cache->downloadStatusChanged(tf,download); + savePriorityInfo(); + return; + } + + // Out(SYS_DIO|LOG_DEBUG) << "exclude " << first << " to " << last << endl; + exclude(first,last); + } + // alert the cache but first put things in critical operation mode + cache->downloadStatusChanged(tf,download); + savePriorityInfo(); + } + + void ChunkManager::downloadPriorityChanged(TorrentFile* tf,Priority newpriority,Priority oldpriority) + { + if (newpriority == EXCLUDED) + { + downloadStatusChanged(tf, false); + return; + } + if (oldpriority == EXCLUDED) + { + downloadStatusChanged(tf, true); + return; + } + + savePriorityInfo(); + + Uint32 first = tf->getFirstChunk(); + Uint32 last = tf->getLastChunk(); + + // first and last chunk may be part of multiple files + // so we can't just exclude them + QValueList<Uint32> files; + + // get list of files where first chunk lies in + tor.calcChunkPos(first,files); + + Chunk* c = chunks[first]; + // if one file in the list needs to be downloaded,increment first + for (QValueList<Uint32>::iterator i = files.begin();i != files.end();i++) + { + Priority np = tor.getFile(*i).getPriority(); + if (np > newpriority && *i != tf->getIndex()) + { + // make sure we don't go past last + if (first == last) + return; + + first++; + break; + } + } + + files.clear(); + // get list of files where last chunk lies in + tor.calcChunkPos(last,files); + c = chunks[last]; + // if one file in the list needs to be downloaded,decrement last + for (QValueList<Uint32>::iterator i = files.begin();i != files.end();i++) + { + Priority np = tor.getFile(*i).getPriority(); + if (np > newpriority && *i != tf->getIndex()) + { + // make sure we don't wrap around + if (last == 0 || last == first) + return; + + last--; + break; + } + } + + // last smaller then first is not normal, so just return + if (last < first) + { + return; + } + + + prioritise(first,last,newpriority); + if (newpriority == ONLY_SEED_PRIORITY) + excluded(first,last); + } + + bool ChunkManager::prepareChunk(Chunk* c,bool allways) + { + if (!allways && c->getStatus() != Chunk::NOT_DOWNLOADED) + return false; + + return cache->prep(c); + } + + QString ChunkManager::getOutputPath() const + { + return cache->getOutputPath(); + } + + void ChunkManager::preallocateDiskSpace(PreallocationThread* prealloc) + { + cache->preallocateDiskSpace(prealloc); + } + + void ChunkManager::dataChecked(const BitSet & ok_chunks) + { + // go over all chunks at check each of them + for (Uint32 i = 0;i < chunks.count();i++) + { + Chunk* c = chunks[i]; + if (ok_chunks.get(i) && !bitset.get(i)) + { + // We think we do not hae a chunk, but we do have it + bitset.set(i,true); + todo.set(i,false); + // the chunk must be on disk + c->setStatus(Chunk::ON_DISK); + tor.updateFilePercentage(i,bitset); + } + else if (!ok_chunks.get(i) && bitset.get(i)) + { + Out(SYS_DIO|LOG_IMPORTANT) << "Previously OK chunk " << i << " is corrupt !!!!!" << endl; + // We think we have a chunk, but we don't + bitset.set(i,false); + todo.set(i,!only_seed_chunks.get(i) && !excluded_chunks.get(i)); + if (c->getStatus() == Chunk::ON_DISK) + { + c->setStatus(Chunk::NOT_DOWNLOADED); + tor.updateFilePercentage(i,bitset); + } + else if (c->getStatus() == Chunk::MMAPPED || c->getStatus() == Chunk::BUFFERED) + { + resetChunk(i); + } + else + { + tor.updateFilePercentage(i,bitset); + } + } + } + recalc_chunks_left = true; + try + { + saveIndexFile(); + } + catch (bt::Error & err) + { + Out(SYS_DIO|LOG_DEBUG) << "Failed to save index file : " << err.toString() << endl; + } + catch (...) + { + Out(SYS_DIO|LOG_DEBUG) << "Failed to save index file : unkown exception" << endl; + } + chunksLeft(); + corrupted_count = 0; + } + + bool ChunkManager::hasExistingFiles() const + { + return cache->hasExistingFiles(); + } + + + void ChunkManager::recreateMissingFiles() + { + createFiles(); + if (tor.isMultiFile()) + { + // loop over all files and mark all chunks of all missing files as + // not downloaded + for (Uint32 i = 0;i < tor.getNumFiles();i++) + { + TorrentFile & tf = tor.getFile(i); + if (!tf.isMissing()) + continue; + + for (Uint32 j = tf.getFirstChunk(); j <= tf.getLastChunk();j++) + resetChunk(j); + tf.setMissing(false); + } + } + else + { + // reset all chunks in case of single file torrent + for (Uint32 j = 0; j < tor.getNumChunks();j++) + resetChunk(j); + } + saveIndexFile(); + recalc_chunks_left = true; + chunksLeft(); + } + + void ChunkManager::dndMissingFiles() + { + // createFiles(); // create them again + // loop over all files and mark all chunks of all missing files as + // not downloaded + for (Uint32 i = 0;i < tor.getNumFiles();i++) + { + TorrentFile & tf = tor.getFile(i); + if (!tf.isMissing()) + continue; + + for (Uint32 j = tf.getFirstChunk(); j <= tf.getLastChunk();j++) + resetChunk(j); + tf.setMissing(false); + tf.setDoNotDownload(true); // set do not download + } + savePriorityInfo(); + saveIndexFile(); + recalc_chunks_left = true; + chunksLeft(); + } + + void ChunkManager::deleteDataFiles() + { + cache->deleteDataFiles(); + } + + Uint64 ChunkManager::diskUsage() + { + return cache->diskUsage(); + } + +} + +#include "chunkmanager.moc" diff --git a/libktorrent/torrent/chunkmanager.h b/libktorrent/torrent/chunkmanager.h new file mode 100644 index 0000000..daa2300 --- /dev/null +++ b/libktorrent/torrent/chunkmanager.h @@ -0,0 +1,366 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTCHUNKMANAGER_H +#define BTCHUNKMANAGER_H + +#include <qmap.h> +#include <qstring.h> +#include <qobject.h> +#include <qptrvector.h> +#include <util/bitset.h> +#include "chunk.h" +#include "globals.h" + +class QStringList; + +namespace KIO +{ + class Job; +} + +namespace bt +{ + class Torrent; + class Cache; + class TorrentFile; + class PreallocationThread; + + struct NewChunkHeader + { + unsigned int index; // the Chunks index + unsigned int deprecated; // offset in cache file + }; + + /** + * @author Joris Guisson + * + * Manages all Chunk's and the cache file, where all the chunk's are stored. + * It also manages a separate index file, where the position of each piece + * in the cache file is stored. + * + * The chunks are stored in the cache file in the correct order. Eliminating + * the need for a file reconstruction algorithm for single files. + */ + class ChunkManager : public QObject + { + Q_OBJECT + + Torrent & tor; + QString index_file,file_info_file,file_priority_file; + QPtrVector<Chunk> chunks; + Cache* cache; + QMap<Uint32,TimeStamp> loaded; // loaded chunks and when they were loaded + BitSet bitset; + BitSet excluded_chunks; + BitSet only_seed_chunks; + BitSet todo; + mutable Uint32 chunks_left; + mutable bool recalc_chunks_left; + Uint32 corrupted_count; + Uint32 recheck_counter; + bool during_load; + public: + ChunkManager(Torrent & tor, + const QString & tmpdir, + const QString & datadir, + bool custom_output_name); + virtual ~ChunkManager(); + + /// Get the torrent + const Torrent & getTorrent() const {return tor;} + + /// Get the data dir + QString getDataDir() const; + + /// Get the actual output path + QString getOutputPath() const; + + void changeOutputPath(const QString& output_path); + + /// Remove obsolete chunks + void checkMemoryUsage(); + + /** + * Change the data dir. + * @param data_dir + */ + void changeDataDir(const QString & data_dir); + + /** + * Move the data files of the torrent. + * @param ndir The new directory + * @return The job doing the move + */ + KIO::Job* moveDataFiles(const QString & ndir); + + /** + * The move data files job has finished + * @param job The move job + */ + void moveDataFilesCompleted(KIO::Job* job); + + /** + * Loads the index file. + * @throw Error When it can be loaded + */ + void loadIndexFile(); + + /** + * Create the cache file, and index files. + * @param check_priority Make sure chunk priorities and dnd status of files match + * @throw Error When it can be created + */ + void createFiles(bool check_priority = false); + + /** + * Test all files and see if they are not missing. + * If so put them in a list + */ + bool hasMissingFiles(QStringList & sl); + + /** + * Preallocate diskspace for all files + * @param prealloc The thread doing the preallocation + */ + void preallocateDiskSpace(PreallocationThread* prealloc); + + /** + * Open the necessary files when the download gets started. + */ + void start(); + + /** + * Closes files when the download gets stopped. + */ + void stop(); + + /** + * Get's the i'th Chunk. + * @param i The Chunk's index + * @return The Chunk, or 0 when i is out of bounds + */ + Chunk* getChunk(unsigned int i); + + /** + * Get's the i'th Chunk. Makes sure that the Chunk's data + * is in memory. If the Chunk hasn't been downloaded yet 0 + * is returned. Whenever the Chunk needs to be uploaded, call + * this function. This changes the status to MMAPPED or BUFFERED. + * @param i The Chunk's index + * @return The Chunk, or 0 when i is out of bounds + */ + Chunk* grabChunk(unsigned int i); + + /** + * Prepare a chunk for downloading + * @param c The Chunk + * @param allways Always do this, even if the chunk is not NOT_DOWNLOADED + * @return true if ok, false if the chunk is not NOT_DOWNLOADED + */ + bool prepareChunk(Chunk* c,bool allways = false); + + /** + * The upload is done, and the Chunk is no longer needed. + * The Chunk's data might be cleared, if we are using up to much + * memory. + * @param i The Chunk's index + */ + void releaseChunk(unsigned int i); + + /** + * Reset a chunk as if it were never downloaded. + * @param i The chunk + */ + void resetChunk(unsigned int i); + + /** + * Save the i'th Chunk to the cache_file. + * Also changes the Chunk's status to ON_DISK. + * The Chunk's data is immediately cleared. + * @param i The Chunk's index + * @param update_index Update the index or not + */ + void saveChunk(unsigned int i,bool update_index = true); + + /** + * Calculates the number of bytes left for the tracker. Does include + * excluded chunks (this should be used for the tracker). + * @return The number of bytes to download + the number of bytes excluded + */ + Uint64 bytesLeft() const; + + /** + * Calculates the number of bytes left to download. + */ + Uint64 bytesLeftToDownload() const; + + /** + * Calculates the number of bytes which have been excluded. + * @return The number of bytes excluded + */ + Uint64 bytesExcluded() const; + + /** + * Calculates the number of chunks left to download. + * Does not include excluded chunks. + * @return The number of chunks to download + */ + Uint32 chunksLeft() const; + + /** + * Check if we have all chunks, this is not the same as + * chunksLeft() == 0, it does not look at excluded chunks. + * @return true if all chunks have been downloaded + */ + bool haveAllChunks() const; + + /** + * Get the number of chunks which have been excluded. + * @return The number of excluded chunks + */ + Uint32 chunksExcluded() const; + + /** + * Get the number of downloaded chunks + * @return + */ + Uint32 chunksDownloaded() const; + + /** + * Get the number of only seed chunks. + */ + Uint32 onlySeedChunks() const; + + /** + * Get a BitSet of the status of all Chunks + */ + const BitSet & getBitSet() const {return bitset;} + + /** + * Get the excluded bitset + */ + const BitSet & getExcludedBitSet() const {return excluded_chunks;} + + /** + * Get the only seed bitset. + */ + const BitSet & getOnlySeedBitSet() const {return only_seed_chunks;} + + /// Get the number of chunks into the file. + Uint32 getNumChunks() const {return chunks.count();} + + /// Print memory usage to log file + void debugPrintMemUsage(); + + /** + * Make sure that a range will get priority over other chunks. + * @param from First chunk in range + * @param to Last chunk in range + */ + void prioritise(Uint32 from,Uint32 to, Priority priority); + + /** + * Make sure that a range will not be downloaded. + * @param from First chunk in range + * @param to Last chunk in range + */ + void exclude(Uint32 from,Uint32 to); + + /** + * Make sure that a range will be downloaded. + * Does the opposite of exclude. + * @param from First chunk in range + * @param to Last chunk in range + */ + void include(Uint32 from,Uint32 to); + + + /** + * Data has been checked, and these chunks are OK. + * The ChunkManager will update it's internal structures + * @param ok_chunks The ok_chunks + */ + void dataChecked(const BitSet & ok_chunks); + + /// Test if the torrent has existing files, only works the first time a torrent is loaded + bool hasExistingFiles() const; + + /// Recreates missing files + void recreateMissingFiles(); + + /// Set missing files as do not download + void dndMissingFiles(); + + /// Delete all data files + void deleteDataFiles(); + + /// Are all not deselected chunks downloaded. + bool completed() const; + + /// Set the maximum chunk size for a data check, 0 means alllways check + static void setMaxChunkSizeForDataCheck(Uint32 mcs) {max_chunk_size_for_data_check = mcs;} + + /// Get the current disk usage of all the files in this torrent + Uint64 diskUsage(); + signals: + /** + * Emitted when a range of chunks has been excluded + * @param from First chunk in range + * @param to Last chunk in range + */ + void excluded(Uint32 from,Uint32 to); + + /** + * Emitted when a range of chunks has been included back. + * @param from First chunk in range + * @param to Last chunk in range + */ + void included(Uint32 from,Uint32 to); + + /** + * Emitted when chunks get excluded or included, so + * that the statistics can be updated. + */ + void updateStats(); + + /** + * A corrupted chunk has been found during uploading. + * @param chunk The chunk + */ + void corrupted(Uint32 chunk); + + private: + void saveIndexFile(); + void writeIndexFileEntry(Chunk* c); + void saveFileInfo(); + void loadFileInfo(); + void savePriorityInfo(); + void loadPriorityInfo(); + + private slots: + void downloadStatusChanged(TorrentFile* tf,bool download); + void downloadPriorityChanged(TorrentFile* tf,Priority newpriority,Priority oldpriority); + + static Uint32 max_chunk_size_for_data_check; + }; + +} + +#endif diff --git a/libktorrent/torrent/chunkselector.cpp b/libktorrent/torrent/chunkselector.cpp new file mode 100644 index 0000000..b1c42fa --- /dev/null +++ b/libktorrent/torrent/chunkselector.cpp @@ -0,0 +1,185 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <stdlib.h> +#include <vector> +#include <algorithm> +#include <util/log.h> +#include <util/bitset.h> +#include "chunkcounter.h" +#include "chunkselector.h" +#include "chunkmanager.h" +#include "downloader.h" +#include "peerdownloader.h" +#include "globals.h" +#include "peer.h" +#include "peermanager.h" + +namespace bt +{ + struct RareCmp + { + ChunkManager & cman; + ChunkCounter & cc; + bool warmup; + + RareCmp(ChunkManager & cman,ChunkCounter & cc,bool warmup) : cman(cman),cc(cc),warmup(warmup) {} + + bool operator()(Uint32 a,Uint32 b) + { + // do some sanity checks + if (a >= cman.getNumChunks() || b >= cman.getNumChunks()) + return false; + + // the sorting is done on two criteria, priority and rareness + Priority pa = cman.getChunk(a)->getPriority(); + Priority pb = cman.getChunk(b)->getPriority(); + if (pa == pb) + return normalCmp(a,b); // if both have same priority compare on rareness + else if (pa > pb) // pa has priority over pb, so select pa + return true; + else // pb has priority over pa, so select pb + return false; + } + + bool normalCmp(Uint32 a,Uint32 b) + { + // during warmup mode choose most common chunks + if (!warmup) + return cc.get(a) < cc.get(b); + else + return cc.get(a) > cc.get(b); + } + }; + + ChunkSelector::ChunkSelector(ChunkManager & cman,Downloader & downer,PeerManager & pman) + : cman(cman),downer(downer),pman(pman) + { + std::vector<Uint32> tmp; + for (Uint32 i = 0;i < cman.getNumChunks();i++) + { + if (!cman.getBitSet().get(i)) + { + tmp.push_back(i); + } + } + std::random_shuffle(tmp.begin(),tmp.end()); + // std::list does not support random_shuffle so we use a vector as a temporary storage + // for the random_shuffle + chunks.insert(chunks.begin(),tmp.begin(),tmp.end()); + sort_timer.update(); + } + + + ChunkSelector::~ChunkSelector() + {} + + + bool ChunkSelector::select(PeerDownloader* pd,Uint32 & chunk) + { + const BitSet & bs = cman.getBitSet(); + + + // sort the chunks every 2 seconds + if (sort_timer.getElapsedSinceUpdate() > 2000) + { + bool warmup = cman.getNumChunks() - cman.chunksLeft() <= 4; +// dataChecked(bs); + chunks.sort(RareCmp(cman,pman.getChunkCounter(),warmup)); + sort_timer.update(); + } + + std::list<Uint32>::iterator itr = chunks.begin(); + while (itr != chunks.end()) + { + Uint32 i = *itr; + Chunk* c = cman.getChunk(*itr); + + // if we have the chunk remove it from the list + if (bs.get(i)) + { + std::list<Uint32>::iterator tmp = itr; + itr++; + chunks.erase(tmp); + } + else + { + // pd has to have the selected chunk and it needs to be not excluded + if (pd->hasChunk(i) && !downer.areWeDownloading(i) && + !c->isExcluded() && !c->isExcludedForDownloading()) + { + // we have a chunk + chunk = i; + return true; + } + itr++; + } + } + + return false; + } + + void ChunkSelector::dataChecked(const BitSet & ok_chunks) + { + for (Uint32 i = 0;i < ok_chunks.getNumBits();i++) + { + bool in_chunks = std::find(chunks.begin(),chunks.end(),i) != chunks.end(); + if (in_chunks && ok_chunks.get(i)) + { + // if we have the chunk, remove it from the chunks list + chunks.remove(i); + } + else if (!in_chunks && !ok_chunks.get(i)) + { + // if we don't have the chunk, add it to the list if it wasn't allrready in there + chunks.push_back(i); + } + } + } + + void ChunkSelector::reincluded(Uint32 from, Uint32 to) + { + // lets do a safety check first + if (from >= cman.getNumChunks() || to >= cman.getNumChunks()) + { + Out(SYS_DIO|LOG_NOTICE) << "Internal error in chunkselector" << endl; + return; + } + + for (Uint32 i = from;i <= to;i++) + { + bool in_chunks = std::find(chunks.begin(),chunks.end(),i) != chunks.end(); + if (!in_chunks && cman.getChunk(i)->getStatus() != Chunk::ON_DISK) + { + // Out(SYS_DIO|LOG_DEBUG) << "ChunkSelector::reIncluded " << i << endl; + chunks.push_back(i); + } + } + } + + void ChunkSelector::reinsert(Uint32 chunk) + { + bool in_chunks = std::find(chunks.begin(),chunks.end(),chunk) != chunks.end(); + if (!in_chunks) + chunks.push_back(chunk); + } + + +} + diff --git a/libktorrent/torrent/chunkselector.h b/libktorrent/torrent/chunkselector.h new file mode 100644 index 0000000..3ba2f8a --- /dev/null +++ b/libktorrent/torrent/chunkselector.h @@ -0,0 +1,80 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTCHUNKSELECTOR_H +#define BTCHUNKSELECTOR_H + +#include <list> +#include <util/timer.h> + +namespace bt +{ + class BitSet; + class PeerDownloader; + class ChunkManager; + class Downloader; + class PeerManager; + /** + * @author Joris Guisson + * + * Selects which Chunks to download. + */ + class ChunkSelector + { + ChunkManager & cman; + Downloader & downer; + PeerManager & pman; + std::list<Uint32> chunks; + Timer sort_timer; + public: + ChunkSelector(ChunkManager & cman,Downloader & downer,PeerManager &pman); + virtual ~ChunkSelector(); + + /** + * Select which chunk to download for a PeerDownloader. + * @param pd The PeerDownloader + * @param chunk Index of chunk gets stored here + * @return true upon succes, false otherwise + */ + bool select(PeerDownloader* pd,Uint32 & chunk); + + /** + * Data has been checked, and these chunks are OK. + * @param ok_chunks The ok_chunks + */ + void dataChecked(const BitSet & ok_chunks); + + /** + * A range of chunks has been reincluded. + * @param from The first chunk + * @param to The last chunk + */ + void reincluded(Uint32 from, Uint32 to); + + /** + * Reinsert a chunk. + * @param chunk The chunk + */ + void reinsert(Uint32 chunk); + }; + +} + +#endif + diff --git a/libktorrent/torrent/dndfile.cpp b/libktorrent/torrent/dndfile.cpp new file mode 100644 index 0000000..deace69 --- /dev/null +++ b/libktorrent/torrent/dndfile.cpp @@ -0,0 +1,268 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <klocale.h> +#include <util/file.h> +#include <util/error.h> +#include <util/fileops.h> +#include <util/sha1hash.h> +#include "dndfile.h" + +namespace bt +{ + const Uint32 DND_FILE_HDR_MAGIC = 0xD1234567; + + struct DNDFileHeader + { + Uint32 magic; + Uint32 first_size; + Uint32 last_size; + Uint8 data_sha1[20]; + }; + + DNDFile::DNDFile(const QString & path) : path(path) + {} + + + DNDFile::~DNDFile() + {} + + void DNDFile::changePath(const QString & npath) + { + path = npath; + } + + void DNDFile::checkIntegrity() + { + File fptr; + if (!fptr.open(path,"rb")) + { + create(); + return; + } + + DNDFileHeader hdr; + if (fptr.read(&hdr,sizeof(DNDFileHeader)) != sizeof(DNDFileHeader)) + { + create(); + return; + } + + if (hdr.magic != DND_FILE_HDR_MAGIC && bt::FileSize(path) != sizeof(DNDFileHeader) + hdr.first_size + hdr.last_size) + { + create(); + return; + } + +#if 0 + if (hdr.first_size > 0 || hdr.last_size > 0) + { + // check hash + Uint32 data_size = hdr.first_size + hdr.last_size; + Uint8* buf = new Uint8[data_size]; + if (fptr.read(buf,data_size) != data_size) + { + delete [] buf; + create(); + return; + } + + if (SHA1Hash::generate(buf,data_size) != SHA1Hash(hdr.data_sha1)) + { + delete [] buf; + create(); + return; + } + + delete [] buf; + } +#endif + } + + void DNDFile::create() + { + DNDFileHeader hdr; + hdr.magic = DND_FILE_HDR_MAGIC; + hdr.first_size = 0; + hdr.last_size = 0; + memset(hdr.data_sha1,0,20); + + File fptr; + if (!fptr.open(path,"wb")) + throw Error(i18n("Cannot create file %1 : %2").arg(path).arg(fptr.errorString())); + + fptr.write(&hdr,sizeof(DNDFileHeader)); + fptr.close(); + } + + + + Uint32 DNDFile::readFirstChunk(Uint8* buf,Uint32 off,Uint32 buf_size) + { + File fptr; + if (!fptr.open(path,"rb")) + { + create(); + return 0; + } + + DNDFileHeader hdr; + if (fptr.read(&hdr,sizeof(DNDFileHeader)) != sizeof(DNDFileHeader)) + { + create(); + return 0; + } + + if (hdr.first_size == 0) + return 0; + + if (hdr.first_size + off > buf_size) + return 0; + + return fptr.read(buf + off,hdr.first_size); + } + + Uint32 DNDFile::readLastChunk(Uint8* buf,Uint32 off,Uint32 buf_size) + { + File fptr; + if (!fptr.open(path,"rb")) + { + create(); + return 0; + } + + DNDFileHeader hdr; + if (fptr.read(&hdr,sizeof(DNDFileHeader)) != sizeof(DNDFileHeader)) + { + create(); + return 0; + } + + if (hdr.last_size == 0) + return 0; + + if (hdr.last_size + off > buf_size) + return 0; + + fptr.seek(File::BEGIN,sizeof(DNDFileHeader) + hdr.first_size); + return fptr.read(buf + off,hdr.last_size); + } + + void DNDFile::writeFirstChunk(const Uint8* buf,Uint32 fc_size) + { + File fptr; + if (!fptr.open(path,"r+b")) + { + create(); + if (!fptr.open(path,"r+b")) + { + throw Error(i18n("Failed to write first chunk to DND file : %1").arg(fptr.errorString())); + } + } + + DNDFileHeader hdr; + fptr.read(&hdr,sizeof(DNDFileHeader)); + if (hdr.last_size == 0) + { + hdr.first_size = fc_size; + fptr.seek(File::BEGIN,0); + // update hash first + // SHA1Hash h = SHA1Hash::generate(buf,fc_size); + // memcpy(hdr.data_sha1,h.getData(),20); + // write header + fptr.write(&hdr,sizeof(DNDFileHeader)); + // write data + fptr.write(buf,fc_size); + } + else + { + hdr.first_size = fc_size; + Uint8* tmp = new Uint8[hdr.first_size + hdr.last_size]; + try + { + + // put everything in tmp buf + memcpy(tmp,buf,hdr.first_size); + fptr.seek(File::BEGIN,sizeof(DNDFileHeader) + hdr.first_size); + fptr.read(tmp + hdr.first_size,hdr.last_size); + + // update the hash of the header + // SHA1Hash h = SHA1Hash::generate(tmp,hdr.first_size + hdr.last_size); + // memcpy(hdr.data_sha1,h.getData(),20); + + // write header + data + fptr.seek(File::BEGIN,0); + fptr.write(&hdr,sizeof(DNDFileHeader)); + fptr.write(tmp,hdr.first_size + hdr.last_size); + delete [] tmp; + + } + catch (...) + { + delete [] tmp; + throw; + } + } + } + + + void DNDFile::writeLastChunk(const Uint8* buf,Uint32 lc_size) + { + File fptr; + if (!fptr.open(path,"r+b")) + { + create(); + if (!fptr.open(path,"r+b")) + { + throw Error(i18n("Failed to write last chunk to DND file : %1").arg(fptr.errorString())); + } + } + + DNDFileHeader hdr; + fptr.read(&hdr,sizeof(DNDFileHeader)); + hdr.last_size = lc_size; + Uint8* tmp = new Uint8[hdr.first_size + hdr.last_size]; + try + { + // put everything in tmp buf + memcpy(tmp + hdr.first_size,buf,lc_size); + if (hdr.first_size > 0) + { + fptr.seek(File::BEGIN,sizeof(DNDFileHeader)); + fptr.read(tmp,hdr.first_size); + } + + // update the hash of the header + // SHA1Hash h = SHA1Hash::generate(tmp,hdr.first_size + hdr.last_size); + // memcpy(hdr.data_sha1,h.getData(),20); + + // write header + data + fptr.seek(File::BEGIN,0); + fptr.write(&hdr,sizeof(DNDFileHeader)); + fptr.write(tmp,hdr.first_size + hdr.last_size); + delete [] tmp; + } + catch (...) + { + delete [] tmp; + throw; + } + } + +} diff --git a/libktorrent/torrent/dndfile.h b/libktorrent/torrent/dndfile.h new file mode 100644 index 0000000..a7a7e7b --- /dev/null +++ b/libktorrent/torrent/dndfile.h @@ -0,0 +1,89 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTDNDFILE_H +#define BTDNDFILE_H + +#include <qstring.h> +#include <util/constants.h> + +namespace bt +{ + + + /** + * @author Joris Guisson <joris.guisson@gmail.com> + * + * Special file where we keep the first and last chunk of a file which is marked as do not download. + * THe first and last chunk of a file will most certainly be partial chunks. + */ + class DNDFile + { + public: + DNDFile(const QString & path); + virtual ~DNDFile(); + + /// Change the path of the file + void changePath(const QString & npath); + + /** + * CHeck integrity of the file, create it if it doesn't exist. + */ + void checkIntegrity(); + + /** + * Read the (partial)first chunk into a buffer. + * @param buf The buffer + * @param off OFfset into the buffer + * @param buf_size Size of the buffer + */ + Uint32 readFirstChunk(Uint8* buf,Uint32 off,Uint32 buf_size); + + /** + * Read the (partial)last chunk into a buffer. + * @param buf The buffer + * @param off OFfset into the buffer + * @param buf_size Size of the buffer + */ + Uint32 readLastChunk(Uint8* buf,Uint32 off,Uint32 buf_size); + + /** + * Write the partial first chunk. + * @param buf The buffer + * @param fc_size Size to write + */ + void writeFirstChunk(const Uint8* buf,Uint32 fc_size); + + /** + * Write the partial last chunk. + * @param buf The buffer + * @param lc_size Size to write + */ + void writeLastChunk(const Uint8* buf,Uint32 lc_size); + + private: + void create(); + + private: + QString path; + }; + +} + +#endif diff --git a/libktorrent/torrent/downloadcap.cpp b/libktorrent/torrent/downloadcap.cpp new file mode 100644 index 0000000..73e0cbb --- /dev/null +++ b/libktorrent/torrent/downloadcap.cpp @@ -0,0 +1,43 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#if 0 +#include <math.h> +#include <util/log.h> +#include "downloadcap.h" +#include "globals.h" + +namespace bt +{ + DownloadCap DownloadCap::self; + + const Uint32 SLOT_SIZE = 5*1024; + + DownloadCap::DownloadCap() : Cap(true) + { + } + + DownloadCap::~ DownloadCap() + { + } + + + +} +#endif diff --git a/libktorrent/torrent/downloadcap.h b/libktorrent/torrent/downloadcap.h new file mode 100644 index 0000000..2bda73c --- /dev/null +++ b/libktorrent/torrent/downloadcap.h @@ -0,0 +1,48 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTDOWNLOADCAP_H +#define BTDOWNLOADCAP_H + +#if 0 +#include <qvaluelist.h> +#include <util/timer.h> +#include "globals.h" +#include "cap.h" + +namespace bt +{ + + /** + * @author Joris Guisson + */ + class DownloadCap : public Cap + { + static DownloadCap self; + + DownloadCap(); + public: + ~DownloadCap(); + + static DownloadCap & instance() {return self;} + }; + +} +#endif +#endif diff --git a/libktorrent/torrent/downloader.cpp b/libktorrent/torrent/downloader.cpp new file mode 100644 index 0000000..b8acdc7 --- /dev/null +++ b/libktorrent/torrent/downloader.cpp @@ -0,0 +1,688 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <util/file.h> +#include <util/log.h> +#include "downloader.h" +#include "chunkmanager.h" +#include "torrent.h" +#include "peermanager.h" +#include <util/error.h> +#include "chunkdownload.h" +#include <util/sha1hash.h> +#include <util/array.h> +#include "peer.h" +#include "piece.h" +#include "peerdownloader.h" +#include <interfaces/functions.h> +#include <interfaces/monitorinterface.h> +#include "packetwriter.h" +#include "chunkselector.h" +#include "ipblocklist.h" +#include "ktversion.h" + +namespace bt +{ + + + + Downloader::Downloader(Torrent & tor,PeerManager & pman,ChunkManager & cman) + : tor(tor),pman(pman),cman(cman),downloaded(0),tmon(0) + { + chunk_selector = new ChunkSelector(cman,*this,pman); + Uint64 total = tor.getFileLength(); + downloaded = (total - cman.bytesLeft()); + curr_chunks_downloaded = 0; + unnecessary_data = 0; + + current_chunks.setAutoDelete(true); + connect(&pman,SIGNAL(newPeer(Peer* )),this,SLOT(onNewPeer(Peer* ))); + connect(&pman,SIGNAL(peerKilled(Peer* )),this,SLOT(onPeerKilled(Peer*))); + } + + + Downloader::~Downloader() + { + delete chunk_selector; + } + + void Downloader::pieceRecieved(const Piece & p) + { + if (cman.completed()) + return; + + ChunkDownload* cd = 0; + + for (CurChunkItr j = current_chunks.begin();j != current_chunks.end();++j) + { + if (p.getIndex() != j->first) + continue; + + cd = j->second; + break; + } + + if (!cd) + { + unnecessary_data += p.getLength(); + Out(SYS_DIO|LOG_DEBUG) << + "Unnecessary piece, total unnecessary data : " << kt::BytesToString(unnecessary_data) << endl; + return; + } + + // if the chunk is not in memory, reload it + if (cd->getChunk()->getStatus() == Chunk::ON_DISK) + { + cman.prepareChunk(cd->getChunk(),true); + } + + bool ok = false; + + if (cd->piece(p,ok)) + { + if (tmon) + tmon->downloadRemoved(cd); + + if (ok) + downloaded += p.getLength(); + + if (!finished(cd)) + { + // if the chunk fails don't count the bytes downloaded + if (cd->getChunk()->getSize() > downloaded) + downloaded = 0; + else + downloaded -= cd->getChunk()->getSize(); + } + current_chunks.erase(p.getIndex()); + update(); // run an update to assign new pieces + } + else + { + if (ok) + downloaded += p.getLength(); + + // save to disk again, if it is idle + if (cd->isIdle() && cd->getChunk()->getStatus() == Chunk::MMAPPED) + { + cman.saveChunk(cd->getChunk()->getIndex(),false); + } + } + + if (!ok) + { + unnecessary_data += p.getLength(); + Out(SYS_DIO|LOG_DEBUG) << + "Unnecessary piece, total unnecessary data : " << kt::BytesToString(unnecessary_data) << endl; + } + } + + void Downloader::update() + { + if (cman.completed()) + return; + + /* + Normal update should now handle all modes properly. + */ + normalUpdate(); + + // now see if there aren't any timed out pieces + for (Uint32 i = 0;i < pman.getNumConnectedPeers();i++) + { + Peer* p = pman.getPeer(i); + p->getPeerDownloader()->checkTimeouts(); + } + } + + + void Downloader::normalUpdate() + { + for (CurChunkItr j = current_chunks.begin();j != current_chunks.end();++j) + { + ChunkDownload* cd = j->second; + if (cd->isIdle()) // idle chunks do not need to be in memory + { + Chunk* c = cd->getChunk(); + if (c->getStatus() == Chunk::MMAPPED) + { + cman.saveChunk(cd->getChunk()->getIndex(),false); + } + } + else if (cd->isChoked()) + { + cd->releaseAllPDs(); + Chunk* c = cd->getChunk(); + if (c->getStatus() == Chunk::MMAPPED) + { + cman.saveChunk(cd->getChunk()->getIndex(),false); + } + } + else if (cd->needsToBeUpdated()) + { + cd->update(); + } + } + + for (Uint32 i = 0; i < pman.getNumConnectedPeers();++i) + { + PeerDownloader* pd = pman.getPeer(i)->getPeerDownloader(); + + if (pd->isNull()) + continue; + + bool ok = + (pd->getNumGrabbed() < pd->getMaxChunkDownloads() || + pd->isNearlyDone()) && + pd->canAddRequest(); + + + if (ok) + { + if (!pd->isChoked()) + downloadFrom(pd); + + pd->setNearlyDone(false); + } + } + } + + Uint32 Downloader::maxMemoryUsage() + { + Uint32 max = 1024 * 1024; + switch (mem_usage) + { + case 1: // Medium + max *= 60; // 60 MB + break; + case 2: // High + max *= 80; // 90 MB + break; + case 0: // LOW + default: + max *= 40; // 30 MB + break; + } + return max; + } + + Uint32 Downloader::numNonIdle() + { + Uint32 num_non_idle = 0; + for (CurChunkItr j = current_chunks.begin();j != current_chunks.end();++j) + { + ChunkDownload* cd = j->second; + if (!cd->isIdle()) + num_non_idle++; + } + return num_non_idle; + } + + ChunkDownload* Downloader::selectCD(PeerDownloader* pd,Uint32 num) + { + ChunkDownload* sel = 0; + Uint32 sel_left = 0xFFFFFFFF; + + for (CurChunkItr j = current_chunks.begin();j != current_chunks.end();++j) + { + ChunkDownload* cd = j->second; + if (pd->isChoked() || !pd->hasChunk(cd->getChunk()->getIndex())) + continue; + + if (cd->getNumDownloaders() == num) + { + // lets favor the ones which are nearly finished + if (!sel || cd->getTotalPieces() - cd->getPiecesDownloaded() < sel_left) + { + sel = cd; + sel_left = sel->getTotalPieces() - sel->getPiecesDownloaded(); + } + } + } + return sel; + } + + bool Downloader::findDownloadForPD(PeerDownloader* pd,bool warmup) + { + ChunkDownload* sel = 0; + + // first see if there are ChunkDownload's which need a PeerDownloader + sel = selectCD(pd,0); + + if (!sel && warmup) + { + // if we couldn't find one, try to select another + // which only has one downloader + // so that during warmup, there are at the most 2 downloaders + // assigned to one peer + sel = selectCD(pd,1); + } + + if (sel) + { + // if it is on disk, reload it + if (sel->getChunk()->getStatus() == Chunk::ON_DISK) + cman.prepareChunk(sel->getChunk(),true); + + sel->assignPeer(pd); + return true; + } + + return false; + } + + ChunkDownload* Downloader::selectWorst(PeerDownloader* pd) + { + ChunkDownload* cdmin = NULL; + for (CurChunkItr j = current_chunks.begin();j != current_chunks.end();++j) + { + ChunkDownload* cd = j->second; + if (!pd->hasChunk(cd->getChunk()->getIndex()) || cd->containsPeer(pd)) + continue; + + if (!cdmin) + cdmin = cd; + else if (cd->getDownloadSpeed() < cdmin->getDownloadSpeed()) + cdmin = cd; + else if (cd->getNumDownloaders() < cdmin->getNumDownloaders()) + cdmin = cd; + } + + return cdmin; + } + + void Downloader::downloadFrom(PeerDownloader* pd) + { + // calculate the max memory usage + Uint32 max = maxMemoryUsage(); + // calculate number of non idle chunks + Uint32 num_non_idle = numNonIdle(); + + // first see if we can use an existing dowload + if (findDownloadForPD(pd,cman.getNumChunks() - cman.chunksLeft() <= 4)) + return; + + bool limit_exceeded = num_non_idle * tor.getChunkSize() >= max; + + Uint32 chunk = 0; + if (!limit_exceeded && chunk_selector->select(pd,chunk)) + { + Chunk* c = cman.getChunk(chunk); + if (cman.prepareChunk(c)) + { + ChunkDownload* cd = new ChunkDownload(c); + current_chunks.insert(chunk,cd); + cd->assignPeer(pd); + if (tmon) + tmon->downloadStarted(cd); + } + } + else if (pd->getNumGrabbed() == 0) + { + // If the peer hasn't got a chunk we want, + ChunkDownload *cdmin = selectWorst(pd); + + if (cdmin) + { + // if it is on disk, reload it + if (cdmin->getChunk()->getStatus() == Chunk::ON_DISK) + { + cman.prepareChunk(cdmin->getChunk(),true); + } + + cdmin->assignPeer(pd); + } + } + } + + + bool Downloader::areWeDownloading(Uint32 chunk) const + { + return current_chunks.find(chunk) != 0; + } + + void Downloader::onNewPeer(Peer* peer) + { + PeerDownloader* pd = peer->getPeerDownloader(); + connect(pd,SIGNAL(downloaded(const Piece& )), + this,SLOT(pieceRecieved(const Piece& ))); + } + + void Downloader::onPeerKilled(Peer* peer) + { + PeerDownloader* pd = peer->getPeerDownloader(); + if (pd) + { + for (CurChunkItr i = current_chunks.begin();i != current_chunks.end();++i) + { + ChunkDownload* cd = i->second; + cd->peerKilled(pd); + } + } + } + + bool Downloader::finished(ChunkDownload* cd) + { + Chunk* c = cd->getChunk(); + // verify the data + SHA1Hash h; + if (cd->usingContinuousHashing()) + h = cd->getHash(); + else + h = SHA1Hash::generate(c->getData(),c->getSize()); + + if (tor.verifyHash(h,c->getIndex())) + { + // hash ok so save it + try + { + cman.saveChunk(c->getIndex()); + Out(SYS_GEN|LOG_NOTICE) << "Chunk " << c->getIndex() << " downloaded " << endl; + // tell everybody we have the Chunk + for (Uint32 i = 0;i < pman.getNumConnectedPeers();i++) + { + pman.getPeer(i)->getPacketWriter().sendHave(c->getIndex()); + } + } + catch (Error & e) + { + Out(SYS_DIO|LOG_IMPORTANT) << "Error " << e.toString() << endl; + emit ioError(e.toString()); + return false; + } + } + else + { + Out(SYS_GEN|LOG_IMPORTANT) << "Hash verification error on chunk " << c->getIndex() << endl; + Out(SYS_GEN|LOG_IMPORTANT) << "Is : " << h << endl; + Out(SYS_GEN|LOG_IMPORTANT) << "Should be : " << tor.getHash(c->getIndex()) << endl; + + cman.resetChunk(c->getIndex()); + chunk_selector->reinsert(c->getIndex()); + Uint32 pid; + if (cd->getOnlyDownloader(pid)) + { + Peer* p = pman.findPeer(pid); + if (!p) + return false; + QString IP(p->getIPAddresss()); + Out(SYS_GEN|LOG_NOTICE) << "Peer " << IP << " sent bad data" << endl; + IPBlocklist & ipfilter = IPBlocklist::instance(); + ipfilter.insert( IP ); + p->kill(); + } + return false; + } + return true; + } + + void Downloader::clearDownloads() + { + for (CurChunkItr i = current_chunks.begin();i != current_chunks.end();++i) + { + Uint32 ch = i->first; + Chunk* c = i->second->getChunk(); + if (c->getStatus() == Chunk::MMAPPED) + cman.saveChunk(ch,false); + + c->setStatus(Chunk::NOT_DOWNLOADED); + } + current_chunks.clear(); + } + + Uint32 Downloader::downloadRate() const + { + // sum of the download rate of each peer + Uint32 rate = 0; + for (Uint32 i = 0;i < pman.getNumConnectedPeers();i++) + { + Peer* p = pman.getPeer(i); + rate += p->getDownloadRate(); + } + return rate; + } + + void Downloader::setMonitor(kt::MonitorInterface* tmo) + { + tmon = tmo; + if (!tmon) + return; + + for (CurChunkItr i = current_chunks.begin();i != current_chunks.end();++i) + { + ChunkDownload* cd = i->second; + tmon->downloadStarted(cd); + } + } + + + + void Downloader::saveDownloads(const QString & file) + { + File fptr; + if (!fptr.open(file,"wb")) + return; + + // Save all the current downloads to a file + CurrentChunksHeader hdr; + hdr.magic = CURRENT_CHUNK_MAGIC; + hdr.major = kt::MAJOR; + hdr.minor = kt::MINOR; + hdr.num_chunks = current_chunks.count(); + fptr.write(&hdr,sizeof(CurrentChunksHeader)); + +// Out() << "sizeof(CurrentChunksHeader)" << sizeof(CurrentChunksHeader) << endl; + Out() << "Saving " << current_chunks.count() << " chunk downloads" << endl; + for (CurChunkItr i = current_chunks.begin();i != current_chunks.end();++i) + { + ChunkDownload* cd = i->second; + cd->save(fptr); + } + } + + void Downloader::loadDownloads(const QString & file) + { + // don't load stuff if download is finished + if (cman.completed()) + return; + + // Load all partial downloads + File fptr; + if (!fptr.open(file,"rb")) + return; + + // recalculate downloaded bytes + downloaded = (tor.getFileLength() - cman.bytesLeft()); + + CurrentChunksHeader chdr; + fptr.read(&chdr,sizeof(CurrentChunksHeader)); + if (chdr.magic != CURRENT_CHUNK_MAGIC) + { + Out() << "Warning : current_chunks file corrupted" << endl; + return; + } + + Out() << "Loading " << chdr.num_chunks << " active chunk downloads" << endl; + for (Uint32 i = 0;i < chdr.num_chunks;i++) + { + ChunkDownloadHeader hdr; + // first read header + fptr.read(&hdr,sizeof(ChunkDownloadHeader)); + Out() << "Loading chunk " << hdr.index << endl; + if (hdr.index >= tor.getNumChunks()) + { + Out() << "Warning : current_chunks file corrupted, invalid index " << hdr.index << endl; + return; + } + + if (!cman.getChunk(hdr.index) || current_chunks.contains(hdr.index)) + { + Out() << "Illegal chunk " << hdr.index << endl; + return; + } + Chunk* c = cman.getChunk(hdr.index); + if (!c->isExcluded() && cman.prepareChunk(c)) + { + ChunkDownload* cd = new ChunkDownload(c); + bool ret = false; + try + { + ret = cd->load(fptr,hdr); + } + catch (...) + { + ret = false; + } + + if (!ret) + { + delete cd; + } + else + { + current_chunks.insert(hdr.index,cd); + downloaded += cd->bytesDownloaded(); + + if (tmon) + tmon->downloadStarted(cd); + } + } + } + + // reset curr_chunks_downloaded to 0 + curr_chunks_downloaded = 0; + } + + Uint32 Downloader::getDownloadedBytesOfCurrentChunksFile(const QString & file) + { + // Load all partial downloads + File fptr; + if (!fptr.open(file,"rb")) + return 0; + + // read the number of chunks + CurrentChunksHeader chdr; + fptr.read(&chdr,sizeof(CurrentChunksHeader)); + if (chdr.magic != CURRENT_CHUNK_MAGIC) + { + Out() << "Warning : current_chunks file corrupted" << endl; + return 0; + } + Uint32 num_bytes = 0; + + // load all chunks and calculate how much is downloaded + for (Uint32 i = 0;i < chdr.num_chunks;i++) + { + // read the chunkdownload header + ChunkDownloadHeader hdr; + fptr.read(&hdr,sizeof(ChunkDownloadHeader)); + + Chunk* c = cman.getChunk(hdr.index); + if (!c) + return num_bytes; + + Uint32 last_size = c->getSize() % MAX_PIECE_LEN; + if (last_size == 0) + last_size = MAX_PIECE_LEN; + + // create the bitset and read it + BitSet bs(hdr.num_bits); + fptr.read(bs.getData(),bs.getNumBytes()); + + for (Uint32 j = 0;j < hdr.num_bits;j++) + { + if (bs.get(j)) + num_bytes += j == hdr.num_bits - 1 ? + last_size : MAX_PIECE_LEN; + } + + if (hdr.buffered) + fptr.seek(File::CURRENT,c->getSize()); + } + curr_chunks_downloaded = num_bytes; + return num_bytes; + } + + bool Downloader::isFinished() const + { + return cman.completed(); + } + + void Downloader::onExcluded(Uint32 from,Uint32 to) + { + for (Uint32 i = from;i <= to;i++) + { + ChunkDownload* cd = current_chunks.find(i); + // let only seed chunks finish + if (!cd || cman.getChunk(i)->getPriority() == ONLY_SEED_PRIORITY) + continue; + + cd->cancelAll(); + cd->releaseAllPDs(); + if (tmon) + tmon->downloadRemoved(cd); + current_chunks.erase(i); + cman.resetChunk(i); // reset chunk it is not fully downloaded yet + } + } + + void Downloader::onIncluded(Uint32 from,Uint32 to) + { + chunk_selector->reincluded(from,to); + } + + void Downloader::corrupted(Uint32 chunk) + { + chunk_selector->reinsert(chunk); + } + + Uint32 Downloader::mem_usage = 0; + + void Downloader::setMemoryUsage(Uint32 m) + { + mem_usage = m; +// PeerDownloader::setMemoryUsage(m); + } + + void Downloader::dataChecked(const BitSet & ok_chunks) + { + for (Uint32 i = 0;i < ok_chunks.getNumBits();i++) + { + ChunkDownload* cd = current_chunks.find(i); + if (ok_chunks.get(i) && cd) + { + // we have a chunk and we are downloading it so kill it + cd->releaseAllPDs(); + if (tmon) + tmon->downloadRemoved(cd); + + current_chunks.erase(i); + } + } + chunk_selector->dataChecked(ok_chunks); + } + + void Downloader::recalcDownloaded() + { + Uint64 total = tor.getFileLength(); + downloaded = (total - cman.bytesLeft()); + } +} + +#include "downloader.moc" diff --git a/libktorrent/torrent/downloader.h b/libktorrent/torrent/downloader.h new file mode 100644 index 0000000..5b39eeb --- /dev/null +++ b/libktorrent/torrent/downloader.h @@ -0,0 +1,221 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTDOWNLOADER_H +#define BTDOWNLOADER_H + +#include <qobject.h> +#include <util/ptrmap.h> +#include "globals.h" + +namespace kt +{ + class MonitorInterface; +} + + +namespace bt +{ + class BitSet; + class Torrent; + class ChunkManager; + class PeerManager; + class Peer; + class Chunk; + class ChunkDownload; + class PeerDownloader; + class Piece; + class Request; + class ChunkSelector; + + typedef PtrMap<Uint32,ChunkDownload>::iterator CurChunkItr; + typedef PtrMap<Uint32,ChunkDownload>::const_iterator CurChunkCItr; + + #define CURRENT_CHUNK_MAGIC 0xABCDEF00 + + struct CurrentChunksHeader + { + Uint32 magic; // CURRENT_CHUNK_MAGIC + Uint32 major; + Uint32 minor; + Uint32 num_chunks; + }; + + /** + * @author Joris Guisson + * @brief Manages the downloading + * + * This class manages the downloading of the file. It should + * regurarly be updated. + */ + class Downloader : public QObject + { + Q_OBJECT + + public: + /** + * Constructor. + * @param tor The Torrent + * @param pman The PeerManager + * @param cman The ChunkManager + */ + Downloader(Torrent & tor,PeerManager & pman,ChunkManager & cman); + virtual ~Downloader(); + + /// Get the number of bytes we have downloaded + Uint64 bytesDownloaded() const {return downloaded + curr_chunks_downloaded;} + + /// Get the current dowload rate + Uint32 downloadRate() const; + + /// Get the number of chunks we are dowloading + Uint32 numActiveDownloads() const {return current_chunks.count();} + + /// See if the download is finished. + bool isFinished() const; + + /** + * Clear all downloads. Deletes all active downloads. + */ + void clearDownloads(); + + CurChunkCItr beginDownloads() const {return current_chunks.begin();} + CurChunkCItr endDownloads() const {return current_chunks.end();} + + /** + * See if we are downloading a Chunk + * @param chunk ID of Chunk + * @return true if we are, false if not + */ + bool areWeDownloading(Uint32 chunk) const; + + /** + * Save the current downloads. + * @param file The file to save to + */ + void saveDownloads(const QString & file); + + /** + * Load the current downloads. + * @param file The file to load from + */ + void loadDownloads(const QString & file); + + /** + * Get the number of bytes already downloaded in the current_chunks file. + * @param file The path of the current_chunks file + * @return The bytes already downloading + */ + Uint32 getDownloadedBytesOfCurrentChunksFile(const QString & file); + + /** + * A corrupted chunk has been detected, make sure we redownload it. + * @param chunk The chunk + */ + void corrupted(Uint32 chunk); + public slots: + /** + * Update the downloader. + */ + void update(); + + /** + * We got a new connection. + * @param peer The Peer + */ + void onNewPeer(Peer* peer); + + /** + * A Peer has disconnected. + * @param peer The Peer + */ + void onPeerKilled(Peer* peer); + + /** + * Set the TorrentMonitor. + * @param tmo + */ + void setMonitor(kt::MonitorInterface* tmo); + + static void setMemoryUsage(Uint32 m); + + /** + * Data has been checked, and these chunks are OK. + * @param ok_chunks The ok_chunks + */ + void dataChecked(const BitSet & ok_chunks); + + /** + * Recalculate the number of bytes downloaded. + */ + void recalcDownloaded(); + + private slots: + void pieceRecieved(const Piece & p); + bool finished(ChunkDownload* c); + + /** + * Kill all ChunkDownload's which have been excluded. + * @param from First chunk of range + * @param to Last chunk of range + */ + void onExcluded(Uint32 from,Uint32 to); + + /** + * Make sure chunk selector is back OK, when chunks are included back again. + * @param from First chunk + * @param to Last chunk + */ + void onIncluded(Uint32 from,Uint32 to); + + signals: + /** + * An error occurred while we we're writing or reading from disk. + * @param msg Message + */ + void ioError(const QString & msg); + + private: + void downloadFrom(PeerDownloader* pd); + void normalUpdate(); + Uint32 maxMemoryUsage(); + Uint32 numNonIdle(); + bool findDownloadForPD(PeerDownloader* pd,bool warmup); + ChunkDownload* selectCD(PeerDownloader* pd,Uint32 num); + ChunkDownload* selectWorst(PeerDownloader* pd); + + private: + Torrent & tor; + PeerManager & pman; + ChunkManager & cman; + Uint64 downloaded; + Uint64 curr_chunks_downloaded; + Uint64 unnecessary_data; + PtrMap<Uint32,ChunkDownload> current_chunks; + ChunkSelector* chunk_selector; + + kt::MonitorInterface* tmon; + static Uint32 mem_usage; + }; + + + +} + +#endif diff --git a/libktorrent/torrent/globals.cpp b/libktorrent/torrent/globals.cpp new file mode 100644 index 0000000..0221c17 --- /dev/null +++ b/libktorrent/torrent/globals.cpp @@ -0,0 +1,97 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include <util/log.h> +#include <util/error.h> +#include <net/portlist.h> +#include <kademlia/dht.h> + +#include "globals.h" +#include "server.h" + +namespace bt +{ + + Globals* Globals::inst = 0; + + Globals::Globals() + { + plist = new net::PortList(); + debug_mode = false; + log = new Log(); + server = 0; + dh_table = new dht::DHT(); + } + + Globals::~ Globals() + { + delete server; + delete log; + delete dh_table; + delete plist; + } + + Globals & Globals::instance() + { + if (!inst) + inst = new Globals(); + return *inst; + } + + void Globals::cleanup() + { + delete inst; + inst = 0; + } + + void Globals::initLog(const QString & file) + { + log->setOutputFile(file); + log->setOutputToConsole(debug_mode); + } + + void Globals::initServer(Uint16 port) + { + if (server) + { + delete server; + server = 0; + } + + server = new Server(port); + } + + void Globals::shutdownServer() + { + if (server) + { + server->close(); + } + } + + Log& Globals::getLog(unsigned int arg) + { + log->setFilter(arg); + return *log; + } + + +} + diff --git a/libktorrent/torrent/globals.h b/libktorrent/torrent/globals.h new file mode 100644 index 0000000..7cfe3f5 --- /dev/null +++ b/libktorrent/torrent/globals.h @@ -0,0 +1,78 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTGLOBALS_H +#define BTGLOBALS_H + +#include <util/constants.h> + +class QString; + +namespace net +{ + class PortList; +} + +namespace dht +{ + class DHTBase; +} + +namespace bt +{ + class Log; + class Server; + + + + class Globals + { + public: + virtual ~Globals(); + + void initLog(const QString & file); + void initServer(Uint16 port); + void setDebugMode(bool on) {debug_mode = on;} + bool isDebugModeSet() const {return debug_mode;} + void shutdownServer(); + + Log & getLog(unsigned int arg); + Server & getServer() {return *server;} + dht::DHTBase & getDHT() {return *dh_table;} + net::PortList & getPortList() {return *plist;} + + static Globals & instance(); + static void cleanup(); + private: + Globals(); + + bool debug_mode; + Log* log; + Server* server; + dht::DHTBase* dh_table; + net::PortList* plist; + + friend Log& Out(unsigned int arg); + + static Globals* inst; + + }; +} + +#endif diff --git a/libktorrent/torrent/httptracker.cpp b/libktorrent/torrent/httptracker.cpp new file mode 100644 index 0000000..b220bc0 --- /dev/null +++ b/libktorrent/torrent/httptracker.cpp @@ -0,0 +1,462 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <config.h> + +#include <kurl.h> +#include <klocale.h> +#include <qhostaddress.h> +#include <util/log.h> +#include <util/functions.h> +#include <util/error.h> +#include <util/waitjob.h> +#include <interfaces/exitoperation.h> +#include <kio/job.h> +#include <kio/netaccess.h> +#include <kio/scheduler.h> +#include "bnode.h" +#include "httptracker.h" +#include "torrentcontrol.h" +#include "bdecoder.h" +#include "peermanager.h" +#include "server.h" +#include "globals.h" +#include "settings.h" + + +using namespace kt; + +namespace bt +{ + + HTTPTracker::HTTPTracker(const KURL & url,kt::TorrentInterface* tor,const PeerID & id,int tier) + : Tracker(url,tor,id,tier) + { + active_job = 0; + + interval = 5 * 60; // default interval 5 minutes + failures = 0; + seeders = leechers = 0; + } + + + HTTPTracker::~HTTPTracker() + { + } + + void HTTPTracker::start() + { + event = "started"; + doRequest(); + } + + void HTTPTracker::stop(WaitJob* wjob) + { + if (!started) + return; + + event = "stopped"; + doRequest(wjob); + started = false; + } + + void HTTPTracker::completed() + { + event = "completed"; + doRequest(); + event = QString::null; + } + + void HTTPTracker::manualUpdate() + { + if (!started) + event = "started"; + doRequest(); + } + + void HTTPTracker::scrape() + { + if (!url.isValid()) + { + Out(SYS_TRK|LOG_NOTICE) << "Invalid tracker url, canceling scrape" << endl; + return; + } + + if (!url.fileName(false).startsWith("announce")) + { + Out(SYS_TRK|LOG_NOTICE) << "Tracker " << url << " does not support scraping" << endl; + return; + } + + KURL scrape_url = url; + scrape_url.setFileName(url.fileName(false).replace("announce","scrape")); + + QString epq = scrape_url.encodedPathAndQuery(); + const SHA1Hash & info_hash = tor->getInfoHash(); + if (scrape_url.queryItems().count() > 0) + epq += "&info_hash=" + info_hash.toURLString(); + else + epq += "?info_hash=" + info_hash.toURLString(); + scrape_url.setEncodedPathAndQuery(epq); + + Out(SYS_TRK|LOG_NOTICE) << "Doing scrape request to url : " << scrape_url.prettyURL() << endl; + KIO::MetaData md; + setupMetaData(md); + + KIO::StoredTransferJob* j = KIO::storedGet(scrape_url,false,false); + // set the meta data + j->setMetaData(md); + KIO::Scheduler::scheduleJob(j); + + connect(j,SIGNAL(result(KIO::Job* )),this,SLOT(onScrapeResult( KIO::Job* ))); + } + + void HTTPTracker::onScrapeResult(KIO::Job* j) + { + if (j->error()) + { + Out(SYS_TRK|LOG_IMPORTANT) << "Scrape failed : " << j->errorString() << endl; + return; + } + + KIO::StoredTransferJob* st = (KIO::StoredTransferJob*)j; + BDecoder dec(st->data(),false,0); + BNode* n = 0; + + try + { + n = dec.decode(); + } + catch (bt::Error & err) + { + Out(SYS_TRK|LOG_IMPORTANT) << "Invalid scrape data " << err.toString() << endl; + return; + } + + if (n && n->getType() == BNode::DICT) + { + BDictNode* d = (BDictNode*)n; + d = d->getDict("files"); + if (d) + { + d = d->getDict(tor->getInfoHash().toByteArray()); + if (d) + { + BValueNode* vn = d->getValue("complete"); + if (vn && vn->data().getType() == Value::INT) + { + seeders = vn->data().toInt(); + } + + + vn = d->getValue("incomplete"); + if (vn && vn->data().getType() == Value::INT) + { + leechers = vn->data().toInt(); + } + + Out(SYS_TRK|LOG_DEBUG) << "Scrape : leechers = " << leechers + << ", seeders = " << seeders << endl; + } + } + } + + delete n; + } + + void HTTPTracker::doRequest(WaitJob* wjob) + { + const TorrentStats & s = tor->getStats(); + + KURL u = url; + if (!url.isValid()) + { + requestPending(); + QTimer::singleShot(500,this,SLOT(emitInvalidURLFailure())); + return; + } + + Uint16 port = Globals::instance().getServer().getPortInUse();; + + u.addQueryItem("peer_id",peer_id.toString()); + u.addQueryItem("port",QString::number(port)); + u.addQueryItem("uploaded",QString::number(s.trk_bytes_uploaded)); + u.addQueryItem("downloaded",QString::number(s.trk_bytes_downloaded)); + + if (event == "completed") + u.addQueryItem("left","0"); // need to send 0 when we are completed + else + u.addQueryItem("left",QString::number(s.bytes_left)); + + u.addQueryItem("compact","1"); + if (event != "stopped") + u.addQueryItem("numwant","100"); + else + u.addQueryItem("numwant","0"); + + u.addQueryItem("key",QString::number(key)); + QString cip = Tracker::getCustomIP(); + if (!cip.isNull()) + u.addQueryItem("ip",cip); + + if (event != QString::null) + u.addQueryItem("event",event); + QString epq = u.encodedPathAndQuery(); + const SHA1Hash & info_hash = tor->getInfoHash(); + epq += "&info_hash=" + info_hash.toURLString(); + + + u.setEncodedPathAndQuery(epq); + + if (active_job) + { + announce_queue.append(u); + Out(SYS_TRK|LOG_NOTICE) << "Announce ongoing, queueing announce" << endl; + } + else + { + doAnnounce(u); + // if there is a wait job, add this job to the waitjob + if (wjob) + wjob->addExitOperation(new kt::ExitJobOperation(active_job)); + } + } + + bool HTTPTracker::updateData(const QByteArray & data) + { +//#define DEBUG_PRINT_RESPONSE +#ifdef DEBUG_PRINT_RESPONSE + Out() << "Data : " << endl; + Out() << QString(data) << endl; +#endif + // search for dictionary, there might be random garbage infront of the data + Uint32 i = 0; + while (i < data.size()) + { + if (data[i] == 'd') + break; + i++; + } + + if (i == data.size()) + { + failures++; + requestFailed(i18n("Invalid response from tracker")); + return false; + } + + BDecoder dec(data,false,i); + BNode* n = 0; + try + { + n = dec.decode(); + } + catch (...) + { + failures++; + requestFailed(i18n("Invalid data from tracker")); + return false; + } + + if (!n || n->getType() != BNode::DICT) + { + failures++; + requestFailed(i18n("Invalid response from tracker")); + return false; + } + + BDictNode* dict = (BDictNode*)n; + if (dict->getData("failure reason")) + { + BValueNode* vn = dict->getValue("failure reason"); + QString msg = vn->data().toString(); + delete n; + failures++; + requestFailed(msg); + return false; + } + + BValueNode* vn = dict->getValue("interval"); + + // if no interval is specified, use 5 minutes + if (vn) + interval = vn->data().toInt(); + else + interval = 5 * 60; + + vn = dict->getValue("incomplete"); + if (vn) + leechers = vn->data().toInt(); + + vn = dict->getValue("complete"); + if (vn) + seeders = vn->data().toInt(); + + BListNode* ln = dict->getList("peers"); + if (!ln) + { + // no list, it might however be a compact response + vn = dict->getValue("peers"); + if (!vn) + { + delete n; + failures++; + requestFailed(i18n("Invalid response from tracker")); + return false; + } + + QByteArray arr = vn->data().toByteArray(); + for (Uint32 i = 0;i < arr.size();i+=6) + { + Uint8 buf[6]; + for (int j = 0;j < 6;j++) + buf[j] = arr[i + j]; + + addPeer(QHostAddress(ReadUint32(buf,0)).toString(),ReadUint16(buf,4)); + } + } + else + { + for (Uint32 i = 0;i < ln->getNumChildren();i++) + { + BDictNode* dict = dynamic_cast<BDictNode*>(ln->getChild(i)); + + if (!dict) + continue; + + BValueNode* ip_node = dict->getValue("ip"); + BValueNode* port_node = dict->getValue("port"); + + if (!ip_node || !port_node) + continue; + + addPeer(ip_node->data().toString(),port_node->data().toInt()); + } + } + + delete n; + return true; + } + + + void HTTPTracker::onAnnounceResult(KIO::Job* j) + { + if (j->error()) + { + KIO::StoredTransferJob* st = (KIO::StoredTransferJob*)j; + KURL u = st->url(); + active_job = 0; + + Out(SYS_TRK|LOG_IMPORTANT) << "Error : " << st->errorString() << endl; + if (u.queryItem("event") != "stopped") + { + failures++; + requestFailed(j->errorString()); + } + else + { + stopDone(); + } + } + else + { + KIO::StoredTransferJob* st = (KIO::StoredTransferJob*)j; + KURL u = st->url(); + active_job = 0; + + if (u.queryItem("event") != "stopped") + { + try + { + if (updateData(st->data())) + { + failures = 0; + peersReady(this); + requestOK(); + if (u.queryItem("event") == "started") + started = true; + } + } + catch (bt::Error & err) + { + failures++; + requestFailed(i18n("Invalid response from tracker")); + } + event = QString::null; + } + else + { + failures = 0; + stopDone(); + } + } + doAnnounceQueue(); + } + + void HTTPTracker::emitInvalidURLFailure() + { + failures++; + requestFailed(i18n("Invalid tracker URL")); + } + + void HTTPTracker::setupMetaData(KIO::MetaData & md) + { + md["UserAgent"] = "ktorrent/" VERSION; + md["SendLanguageSettings"] = "false"; + md["Cookies"] = "none"; + // md["accept"] = "text/plain"; + md["accept"] = "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"; + if (Settings::doNotUseKDEProxy()) + { + // set the proxy if the doNotUseKDEProxy ix enabled (URL must be valid to) + KURL url = KURL::fromPathOrURL(Settings::httpTrackerProxy()); + if (url.isValid()) + md["UseProxy"] = url.pathOrURL(); + else + md["UseProxy"] = QString::null; + } + } + + void HTTPTracker::doAnnounceQueue() + { + if (announce_queue.empty()) + return; + + KURL u = announce_queue.front(); + announce_queue.pop_front(); + doAnnounce(u); + } + + void HTTPTracker::doAnnounce(const KURL & u) + { + Out(SYS_TRK|LOG_NOTICE) << "Doing tracker request to url : " << u.prettyURL() << endl; + KIO::MetaData md; + setupMetaData(md); + KIO::StoredTransferJob* j = KIO::storedGet(u,false,false); + // set the meta data + j->setMetaData(md); + KIO::Scheduler::scheduleJob(j); + + connect(j,SIGNAL(result(KIO::Job* )),this,SLOT(onAnnounceResult( KIO::Job* ))); + + active_job = j; + requestPending(); + } +} +#include "httptracker.moc" diff --git a/libktorrent/torrent/httptracker.h b/libktorrent/torrent/httptracker.h new file mode 100644 index 0000000..8ac7e69 --- /dev/null +++ b/libktorrent/torrent/httptracker.h @@ -0,0 +1,77 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTHTTPTRACKER_H +#define BTHTTPTRACKER_H + +#include <qtimer.h> +#include "tracker.h" + +namespace KIO +{ + class Job; + class MetaData; +} + +namespace bt +{ + + + /** + * @author Joris Guisson + * @brief Communicates with the tracker + * + * This class uses the HTTP protocol to communicate with the tracker. + */ + class HTTPTracker : public Tracker + { + Q_OBJECT + public: + HTTPTracker(const KURL & url,kt::TorrentInterface* tor,const PeerID & id,int tier); + virtual ~HTTPTracker(); + + virtual void start(); + virtual void stop(WaitJob* wjob = 0); + virtual void completed(); + virtual void manualUpdate(); + virtual Uint32 failureCount() const {return failures;} + virtual void scrape(); + + private slots: + void onAnnounceResult(KIO::Job* j); + void onScrapeResult(KIO::Job* j); + void emitInvalidURLFailure(); + + private: + void doRequest(WaitJob* wjob = 0); + bool updateData(const QByteArray & data); + void setupMetaData(KIO::MetaData & md); + void doAnnounceQueue(); + void doAnnounce(const KURL & u); + + private: + KIO::Job* active_job; + KURL::List announce_queue; + QString event; + Uint32 failures; + }; + +} + +#endif diff --git a/libktorrent/torrent/ipblocklist.cpp b/libktorrent/torrent/ipblocklist.cpp new file mode 100644 index 0000000..de30968 --- /dev/null +++ b/libktorrent/torrent/ipblocklist.cpp @@ -0,0 +1,400 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * ivasic@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "ipblocklist.h" +#include <qmap.h> +#include <qstring.h> +#include <qstringlist.h> +#include <util/constants.h> +#include <util/log.h> +#include "globals.h" +#include <interfaces/ipblockinginterface.h> + + +namespace bt +{ + Uint32 toUint32(const QString& ip, bool* ok) + { + bool test; + *ok = true; + + Uint32 ret = ip.section('.',0,0).toULongLong(&test); + if(!test) *ok=false; + ret <<= 8; + ret |= ip.section('.',1,1).toULong(&test); + if(!test) *ok=false; + ret <<= 8; + ret |= ip.section('.',2,2).toULong(&test); + if(!test) *ok=false; + ret <<= 8; + ret |= ip.section('.',3,3).toULong(&test); + if(!test) *ok=false; + + if(*ok) + { + // Out() << "IP: " << ip << " parsed: " << ret << endl; + return ret; + } + else + { + // Out() << "Could not parse IP " << ip << ". IP blocklist might not be working." << endl; + return 0; + } + } + + IPBlocklist::IPBlocklist() + { + this->pluginInterface = 0; + insert("0.0.0.0",3); + addRange("3.*.*.*"); + } + + IPBlocklist::IPBlocklist(const IPBlocklist & ) {} + + void IPBlocklist::insert( QString ip, int state ) + { + bool ok; + Uint32 ipi = toUint32(ip, &ok); + if(!ok) + return; + IPKey key(ipi,0xFFFFFFFF); //-- you can test ranges here. Just specify your mask. + insertRangeIP(key, state); + Out(SYS_IPF|LOG_NOTICE) << "IP " << ip << " banned." << endl; + } + + void IPBlocklist::addRange(QString ip) + { + bool ok; + int tmp = 0; + Uint32 addr = 0; + Uint32 mask = 0xFFFFFFFF; + + tmp = ip.section('.',0,0).toInt(&ok); + if(!ok) + { + if(ip.section('.',0,0) == "*") + mask &= 0x00FFFFFF; + else return; //illegal character + } + else + addr = tmp; + + tmp = ip.section('.',1,1).toInt(&ok); + if(!ok) + { + addr <<= 8; + if(ip.section('.',1,1) == "*") + mask &= 0xFF00FFFF; + else return; //illegal character + } + else + { + addr <<= 8; + addr |= tmp; + } + + tmp = ip.section('.',2,2).toInt(&ok); + if(!ok) + { + addr <<= 8; + if(ip.section('.',2,2) == "*") + mask &= 0xFFFF00FF; + else return; //illegal character + } + else + { + addr <<= 8; + addr |= tmp; + } + + tmp = ip.section('.',3,3).toInt(&ok); + if(!ok) + { + addr <<= 8; + if(ip.section('.',3,3) == "*") + mask &=0xFFFFFF00; + else return; //illegal character + } + else + { + addr <<= 8; + addr |= tmp; + } + + IPKey key(addr, mask); + this->insertRangeIP(key); + } + + void IPBlocklist::insertRangeIP(IPKey& key, int state) + { +// Out() << "Blocked range: " << key.m_ip << " - " << key.m_mask << endl; + QMap<IPKey, int>::iterator it; + if ((it = m_peers.find(key)) != m_peers.end()) + { + + if(it.key().m_mask != key.m_mask) + { + int st = it.data(); + IPKey key1(key.m_ip, it.key().m_mask | key.m_mask); + m_peers.insert(key1, state+st); + return; + } + m_peers[key]+= state; + } + else + m_peers.insert(key,state); + } + + void IPBlocklist::removeRange(QString ip) + { + bool ok; + int tmp = 0; + Uint32 addr = 0; + Uint32 mask = 0xFFFFFFFF; + + tmp = ip.section('.',0,0).toInt(&ok); + if(!ok) + { + if(ip.section('.',0,0) == "*") + mask &= 0x00FFFFFF; + else return; //illegal character + } + else + addr = tmp; + + tmp = ip.section('.',1,1).toInt(&ok); + if(!ok) + { + addr <<= 8; + if(ip.section('.',1,1) == "*") + mask &= 0xFF00FFFF; + else return; //illegal character + } + else + { + addr <<= 8; + addr |= tmp; + } + + tmp = ip.section('.',2,2).toInt(&ok); + if(!ok) + { + addr <<= 8; + if(ip.section('.',2,2) == "*") + mask &= 0xFFFF00FF; + else return; //illegal character + } + else + { + addr <<= 8; + addr |= tmp; + } + + tmp = ip.section('.',3,3).toInt(&ok); + if(!ok) + { + addr <<= 8; + if(ip.section('.',3,3) == "*") + mask &=0xFFFFFF00; + else return; //illegal character + } + else + { + addr <<= 8; + addr |= tmp; + } + + IPKey key(addr, mask); + + QMap<IPKey, int>::iterator it = m_peers.find(key); + if (it == m_peers.end()) + return; + + m_peers.remove(key); + } + + void IPBlocklist::setPluginInterfacePtr( kt::IPBlockingInterface* ptr ) + { + this->pluginInterface = ptr; + } + + bool IPBlocklist::isBlocked(const QString& ip ) + { + //First check local filter list + if(isBlockedLocal(ip)) + { + Out(SYS_IPF|LOG_NOTICE) << "IP " << ip << " is blacklisted. Connection denied." << endl; + return true; + } + + //Then we ask plugin + if(isBlockedPlugin(ip)) + { + Out(SYS_IPF|LOG_NOTICE) << "IP " << ip << " is blacklisted. Connection denied." << endl; + return true; + } + + return false; + } + + bool IPBlocklist::isBlockedLocal(const QString& ip ) + { + bool ok; + Uint32 ipi = toUint32(ip,&ok); + if (!ok) + return false; + IPKey key(ipi); + + QMap<IPKey, int>::iterator it; + it = m_peers.find(key); + if (it==m_peers.end()) + return false; + + return m_peers[key] >= 3; + } + + bool IPBlocklist::isBlockedPlugin(const QString& ip ) + { + if (pluginInterface == 0) //the plugin is not loaded + return false; + else + return pluginInterface->isBlockedIP(ip); + } + + QStringList* IPBlocklist::getBlocklist() + { + QStringList* ret = new QStringList(); + QMap<IPKey,int>::iterator it = m_peers.begin(); + for( ;it!=m_peers.end();++it) + { + IPKey key = it.key(); + *ret << key.toString(); + } + + return ret; + } + + void IPBlocklist::setBlocklist(QStringList* list) + { + m_peers.clear(); + for (QStringList::Iterator it = list->begin(); it != list->end(); ++it ) + addRange(*it); + } + + /*** IPKey *****************************************************************************************************************/ + + IPKey::IPKey() + { + m_ip = 0; + m_mask = 0xFFFFFFFF; + } + + IPKey::IPKey(QString& ip, Uint32 mask) + : m_mask(mask) + { + bool ok; + this->m_ip = toUint32(ip, &ok); + } + + IPKey::IPKey(const IPKey& ip) + { + m_ip = ip.m_ip; + m_mask = ip.m_mask; + } + + IPKey::IPKey(Uint32 ip, Uint32 mask) + : m_ip(ip), m_mask(mask) + {} + + QString IPKey::toString() + { + Uint32 tmp, tmpmask; + Uint32 ip = m_ip; + Uint32 mask = m_mask; + QString out; + + tmp = ip; + tmpmask = mask; + tmp &= 0x000000FF; + tmpmask &= 0x000000FF; + if(tmpmask == 0) + out.prepend("*"); + else + out.prepend(QString("%1").arg(tmp)); + ip >>= 8; + mask >>= 8; + tmp = ip; + tmpmask = mask; + tmp &= 0x000000FF; + tmpmask &= 0x000000FF; + if(tmpmask == 0) + out.prepend("*."); + else + out.prepend(QString("%1.").arg(tmp)); + ip >>= 8; + mask >>= 8; + tmp = ip; + tmpmask = mask; + tmp &= 0x000000FF; + tmpmask &= 0x000000FF; + if(tmpmask == 0) + out.prepend("*."); + else + out.prepend(QString("%1.").arg(tmp)); + ip >>= 8; + mask >>= 8; + tmp = ip; + tmpmask = mask; + tmp &= 0x000000FF; + tmpmask &= 0x000000FF; + if(tmpmask == 0) + out.prepend("*."); + else + out.prepend(QString("%1.").arg(tmp)); + + return out; + } + + bool IPKey::operator ==(const IPKey& ip) const + { + return (m_ip & m_mask) == m_mask & ip.m_ip; + } + + bool IPKey::operator !=(const IPKey& ip) const + { + return (m_ip & m_mask) != m_mask & ip.m_ip; + } + + bool IPKey::operator < (const IPKey& ip) const + { + return (m_ip & m_mask) < (m_mask & ip.m_ip); + } + + IPKey& IPKey::operator =(const IPKey& ip) + { + m_ip = ip.m_ip; + m_mask = ip.m_mask; + return *this; + } + + IPKey::~ IPKey() + {} +} diff --git a/libktorrent/torrent/ipblocklist.h b/libktorrent/torrent/ipblocklist.h new file mode 100644 index 0000000..b30a856 --- /dev/null +++ b/libktorrent/torrent/ipblocklist.h @@ -0,0 +1,175 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * ivasic@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef IPBLOCKLIST_H +#define IPBLOCKLIST_H + +#include <interfaces/ipblockinginterface.h> + +#include <qmap.h> +#include <qstringlist.h> +#include <util/constants.h> + +class QString; + +namespace bt +{ + class IPKey + { + public: + IPKey(); + IPKey(QString& ip, Uint32 mask = 0xFFFFFFFF); + IPKey(Uint32 ip, Uint32 mask = 0xFFFFFFFF); + IPKey(const IPKey& ip); + ~IPKey(); + + bool operator== (const IPKey& ip) const; + bool operator!= (const IPKey& ip) const; + bool operator < (const IPKey & ip) const; + IPKey& operator= (const IPKey& ip); + + QString toString(); + + Uint32 m_ip; + Uint32 m_mask; + }; + + /** + * @author Ivan Vasic <ivasic@gmail.com> + * @brief Keeps track of blocked peers + * + * This class is used for keeping the IP addresses list of peers that + * have sent bad chunks. + * + * Peers that have sent >= 3 bad chunks are blocked. + */ + class IPBlocklist + { + IPBlocklist(); + IPBlocklist(const IPBlocklist & ); + const IPBlocklist& operator=(const IPBlocklist&); + + public: + + inline static IPBlocklist & instance() + { + static IPBlocklist singleton; + return singleton; + } + + /** + * @brief Adds ip address to the list. + * It also increases the number of times this IP appeared in the list. + * @param ip QString containing the peer IP address + * @param state int number of bad chunks client from ip sent. Basically this parameter + * is used only to permanently block some IP (by setting this param to 3) + */ + void insert(QString ip, int state=1); + + /** + * @brief Adds IP range to the list + * It is used for blocking plugin. For single IP use insert() instead. + * @param ip QString peer IP address. Uses ''*" for ranges. + **/ + void addRange(QString ip); + + + /** + * @brief Removes IP range from list + * It is used for blocking plugin. + * @param ip QString peer IP address. Uses ''*" for ranges. + **/ + void removeRange(QString ip); + + /** + * Checks if IP is in the blocking list + * @param ip - IP address to check + * @returns true if IP is blocked + */ + bool isBlocked(const QString& ip); + + /** + * @brief Sets the pointer to the IPBlockingInterface (IPBlocking plugin) + * Call this function from IPBlocking plugin when it gets loaded. + * @arg ptr - pointer to be set + */ + void setPluginInterfacePtr(kt::IPBlockingInterface* ptr); + + /** + * @brief Unsets the interface pointer + * Call this when IPBlockingPlugin gets unloaded or deleted + */ + void unsetPluginInterfacePtr() { pluginInterface = 0; } + + + /** + * @brief This function will fill QStringList with all banned peer IP addresses. + * @return QStringList filled with blacklisted peers. + * It will create a new QStringList object so don't forget to delete it after using. + */ + QStringList* getBlocklist(); + + + /** + * @brief This function will load blacklisted peers to IPFilter. + * @param list QStringList containing all banned peers. + * @note This function will remove current peers from blocklist before setting new list!!! + */ + void setBlocklist(QStringList* list); + + private: + + /** + * Pointer to the IPBlocking plugin which implements IPBlockingInterface + * Used to provide a way to use this plugin functions from within this class + */ + kt::IPBlockingInterface* pluginInterface; + + /** + * @param IPKey - Key: Peer IP address and bit mask if it is a range + * @param int - Number of bad chunks sent. + **/ + QMap<IPKey, int> m_peers; + + /** + * @brief Adds IP range to the list. + * @param key IPKey that represents this IP range + * @param state int Number of 'warnings' for the range. + * Default is 3 - that means range is blocked permanently. + */ + void insertRangeIP(IPKey& key, int state=3); + + + /** + * Checks if IP is listed in local database (IPBlocklist::m_peers) + * @return TRUE if IP is to be blocked + */ + bool isBlockedLocal(const QString& ip); + + /** + * Checks if IP is listed in plugins antip2p file + * @return TRUE if IP is to be blocked + */ + bool isBlockedPlugin(const QString& ip); + }; +} + +#endif + diff --git a/libktorrent/torrent/movedatafilesjob.cpp b/libktorrent/torrent/movedatafilesjob.cpp new file mode 100644 index 0000000..c0c24e7 --- /dev/null +++ b/libktorrent/torrent/movedatafilesjob.cpp @@ -0,0 +1,103 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <util/log.h> +#include "movedatafilesjob.h" + +namespace bt +{ + + MoveDataFilesJob::MoveDataFilesJob() : KIO::Job(false),err(false),active_job(0) + {} + + + MoveDataFilesJob::~MoveDataFilesJob() + {} + + void MoveDataFilesJob::addMove(const QString & src,const QString & dst) + { + todo.insert(src,dst); + } + + void MoveDataFilesJob::onJobDone(KIO::Job* j) + { + if (j->error() || err) + { + if (!err) + m_error = KIO::ERR_INTERNAL; + + active_job = 0; + if (j->error()) + j->showErrorDialog(); + + // shit happened cancel all previous moves + err = true; + recover(); + } + else + { + success.insert(active_src,active_dst); + active_src = active_dst = QString::null; + active_job = 0; + startMoving(); + } + } + + void MoveDataFilesJob::onCanceled(KIO::Job* j) + { + m_error = KIO::ERR_USER_CANCELED; + active_job = 0; + err = true; + recover(); + } + + void MoveDataFilesJob::startMoving() + { + if (todo.isEmpty()) + { + m_error = 0; + emitResult(); + return; + } + + QMap<QString,QString>::iterator i = todo.begin(); + active_job = KIO::move(KURL::fromPathOrURL(i.key()),KURL::fromPathOrURL(i.data()),false); + active_src = i.key(); + active_dst = i.data(); + Out(SYS_GEN|LOG_DEBUG) << "Moving " << active_src << " -> " << active_dst << endl; + connect(active_job,SIGNAL(result(KIO::Job*)),this,SLOT(onJobDone(KIO::Job*))); + connect(active_job,SIGNAL(canceled(KIO::Job*)),this,SLOT(onCanceled(KIO::Job*))); + todo.erase(i); + } + + void MoveDataFilesJob::recover() + { + if (success.isEmpty()) + { + emitResult(); + return; + } + QMap<QString,QString>::iterator i = success.begin(); + active_job = KIO::move(KURL::fromPathOrURL(i.data()),KURL::fromPathOrURL(i.key()),false); + connect(active_job,SIGNAL(result(KIO::Job*)),this,SLOT(onJobDone(KIO::Job*))); + connect(active_job,SIGNAL(canceled(KIO::Job*)),this,SLOT(onCanceled(KIO::Job*))); + success.erase(i); + } +} +#include "movedatafilesjob.moc" diff --git a/libktorrent/torrent/movedatafilesjob.h b/libktorrent/torrent/movedatafilesjob.h new file mode 100644 index 0000000..b0002d9 --- /dev/null +++ b/libktorrent/torrent/movedatafilesjob.h @@ -0,0 +1,68 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTMOVEDATAFILESJOB_H +#define BTMOVEDATAFILESJOB_H + +#include <kio/job.h> + +namespace bt +{ + + /** + * @author Joris Guisson <joris.guisson@gmail.com> + * KIO::Job to move all the files of a torrent. + */ + class MoveDataFilesJob : public KIO::Job + { + Q_OBJECT + public: + MoveDataFilesJob(); + virtual ~MoveDataFilesJob(); + + /** + * Add a move to the todo list. + * @param src File to move + * @param dst Where to move it to + */ + void addMove(const QString & src,const QString & dst); + + /** + * Start moving the files. + */ + void startMoving(); + + private slots: + void onJobDone(KIO::Job* j); + void onCanceled(KIO::Job* j); + + private: + void recover(); + + private: + bool err; + KIO::Job* active_job; + QString active_src,active_dst; + QMap<QString,QString> todo; + QMap<QString,QString> success; + }; + +} + +#endif diff --git a/libktorrent/torrent/multifilecache.cpp b/libktorrent/torrent/multifilecache.cpp new file mode 100644 index 0000000..c6af92c --- /dev/null +++ b/libktorrent/torrent/multifilecache.cpp @@ -0,0 +1,867 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <errno.h> +#include <qdir.h> +#include <qstringlist.h> +#include <qfileinfo.h> +#include <klocale.h> +#include <kio/netaccess.h> +#include <util/file.h> +#include <util/fileops.h> +#include <util/functions.h> +#include <util/error.h> +#include <util/log.h> +#include "torrent.h" +#include "cache.h" +#include "multifilecache.h" +#include "globals.h" +#include "chunk.h" +#include "cachefile.h" +#include "dndfile.h" +#include "preallocationthread.h" +#include "movedatafilesjob.h" + + + +namespace bt +{ + static Uint64 FileOffset(Chunk* c,const TorrentFile & f,Uint64 chunk_size); + static Uint64 FileOffset(Uint32 cindex,const TorrentFile & f,Uint64 chunk_size); + static void DeleteEmptyDirs(const QString & output_dir,const QString & fpath); + + + MultiFileCache::MultiFileCache(Torrent& tor,const QString & tmpdir,const QString & datadir,bool custom_output_name) : Cache(tor, tmpdir,datadir) + { + cache_dir = tmpdir + "cache" + bt::DirSeparator(); + if (datadir.length() == 0) + this->datadir = guessDataDir(); + if (!custom_output_name) + output_dir = this->datadir + tor.getNameSuggestion() + bt::DirSeparator(); + else + output_dir = this->datadir; + files.setAutoDelete(true); + } + + + MultiFileCache::~MultiFileCache() + {} + + QString MultiFileCache::guessDataDir() + { + for (Uint32 i = 0;i < tor.getNumFiles();i++) + { + TorrentFile & tf = tor.getFile(i); + if (tf.doNotDownload()) + continue; + + QString p = cache_dir + tf.getPath(); + QFileInfo fi(p); + if (!fi.isSymLink()) + continue; + + QString dst = fi.readLink(); + QString tmp = tor.getNameSuggestion() + bt::DirSeparator() + tf.getPath(); + dst = dst.left(dst.length() - tmp.length()); + if (dst.length() == 0) + continue; + + if (!dst.endsWith(bt::DirSeparator())) + dst += bt::DirSeparator(); + Out() << "Guessed outputdir to be " << dst << endl; + return dst; + } + + return QString::null; + } + + QString MultiFileCache::getOutputPath() const + { + return output_dir; + } + + void MultiFileCache::close() + { + files.clear(); + } + + void MultiFileCache::open() + { + QString dnd_dir = tmpdir + "dnd" + bt::DirSeparator(); + // open all files + for (Uint32 i = 0;i < tor.getNumFiles();i++) + { + TorrentFile & tf = tor.getFile(i); + CacheFile* fd = 0; + DNDFile* dfd = 0; + try + { + if (!tf.doNotDownload()) + { + if (files.contains(i)) + files.erase(i); + + fd = new CacheFile(); + fd->open(cache_dir + tf.getPath(),tf.getSize()); + files.insert(i,fd); + } + else + { + if (dnd_files.contains(i)) + dnd_files.erase(i); + + dfd = new DNDFile(dnd_dir + tf.getPath() + ".dnd"); + dfd->checkIntegrity(); + dnd_files.insert(i,dfd); + } + } + catch (...) + { + delete fd; + fd = 0; + delete dfd; + dfd = 0; + throw; + } + } + } + + void MultiFileCache::changeTmpDir(const QString& ndir) + { + Cache::changeTmpDir(ndir); + cache_dir = tmpdir + "cache/"; + QString dnd_dir = tmpdir + "dnd" + bt::DirSeparator(); + + // change paths for individual files, it should not + // be a problem to move these files when they are open + for (Uint32 i = 0;i < tor.getNumFiles();i++) + { + TorrentFile & tf = tor.getFile(i); + if (tf.doNotDownload()) + { + DNDFile* dfd = dnd_files.find(i); + if (dfd) + dfd->changePath(dnd_dir + tf.getPath() + ".dnd"); + } + else + { + CacheFile* fd = files.find(i); + if (fd) + fd->changePath(cache_dir + tf.getPath()); + } + } + } + + void MultiFileCache::changeOutputPath(const QString & outputpath) + { + output_dir = outputpath; + if (!output_dir.endsWith(bt::DirSeparator())) + output_dir += bt::DirSeparator(); + + datadir = output_dir; + + if (!bt::Exists(cache_dir)) + MakeDir(cache_dir); + + for (Uint32 i = 0;i < tor.getNumFiles();i++) + { + TorrentFile & tf = tor.getFile(i); + if (!tf.doNotDownload()) + { + QString fpath = tf.getPath(); + if (bt::Exists(output_dir + fpath)) + { + bt::Delete(cache_dir + fpath,true); // delete any existing symlinks + // create new one + bt::SymLink(output_dir + fpath,cache_dir + fpath,true); + } + } + } + } + + KIO::Job* MultiFileCache::moveDataFiles(const QString & ndir) + { + if (!bt::Exists(ndir)) + bt::MakeDir(ndir); + + QString nd = ndir; + if (!nd.endsWith(bt::DirSeparator())) + nd += bt::DirSeparator(); + + try + { + MoveDataFilesJob* mvd = new MoveDataFilesJob(); + for (Uint32 i = 0;i < tor.getNumFiles();i++) + { + TorrentFile & tf = tor.getFile(i); + if (tf.doNotDownload()) + continue; + + // check if every directory along the path exists, and if it doesn't + // create it + QStringList sl = QStringList::split(bt::DirSeparator(),nd + tf.getPath()); + QString odir = bt::DirSeparator(); + for (Uint32 i = 0;i < sl.count() - 1;i++) + { + odir += sl[i] + bt::DirSeparator(); + if (!bt::Exists(odir)) + { + bt::MakeDir(odir); + } + } + + mvd->addMove(output_dir + tf.getPath(),nd + tf.getPath()); + } + + mvd->startMoving(); + return mvd; + } + catch (bt::Error & err) + { + throw; // rethrow error + } + return 0; + } + + void MultiFileCache::moveDataFilesCompleted(KIO::Job* job) + { + if (!job->error()) + { + for (Uint32 i = 0;i < tor.getNumFiles();i++) + { + TorrentFile & tf = tor.getFile(i); + // check for empty directories and delete them + DeleteEmptyDirs(output_dir,tf.getPath()); + } + } + } + + void MultiFileCache::create() + { + if (!bt::Exists(cache_dir)) + MakeDir(cache_dir); + if (!bt::Exists(output_dir)) + MakeDir(output_dir); + if (!bt::Exists(tmpdir + "dnd")) + bt::MakeDir(tmpdir + "dnd"); + + // update symlinks + for (Uint32 i = 0;i < tor.getNumFiles();i++) + { + TorrentFile & tf = tor.getFile(i); + touch(tf); + } + } + + void MultiFileCache::touch(TorrentFile & tf) + { + QString fpath = tf.getPath(); + bool dnd = tf.doNotDownload(); + // first split fpath by / separator + QStringList sl = QStringList::split(bt::DirSeparator(),fpath); + // create all necessary subdirs + QString ctmp = cache_dir; + QString otmp = output_dir; + QString dtmp = tmpdir + "dnd" + bt::DirSeparator(); + for (Uint32 i = 0;i < sl.count() - 1;i++) + { + otmp += sl[i]; + ctmp += sl[i]; + dtmp += sl[i]; + // we need to make the same directory structure in the cache, + // the output_dir and the dnd directory + if (!bt::Exists(ctmp)) + MakeDir(ctmp); + if (!bt::Exists(otmp)) + MakeDir(otmp); + if (!bt::Exists(dtmp)) + MakeDir(dtmp); + otmp += bt::DirSeparator(); + ctmp += bt::DirSeparator(); + dtmp += bt::DirSeparator(); + } + + + bt::Delete(cache_dir + fpath,true); // delete any existing symlinks + + // then make the file + QString tmp = dnd ? tmpdir + "dnd" + bt::DirSeparator() : output_dir; + if (dnd) + { + // only symlink, when we open the files a default dnd file will be made if the file is corrupt or doesn't exist + bt::SymLink(tmp + fpath + ".dnd",cache_dir + fpath); + } + else + { + if (!bt::Exists(tmp + fpath)) + { + bt::Touch(tmp + fpath); + } + else + { + preexisting_files = true; + tf.setPreExisting(true); // mark the file as preexisting + } + + bt::SymLink(tmp + fpath,cache_dir + fpath); + } + } + + void MultiFileCache::load(Chunk* c) + { + QValueList<Uint32> tflist; + tor.calcChunkPos(c->getIndex(),tflist); + + // one file is simple, just mmap it + if (tflist.count() == 1) + { + const TorrentFile & f = tor.getFile(tflist.first()); + CacheFile* fd = files.find(tflist.first()); + if (!fd) + return; + + if (Cache::mappedModeAllowed() && mmap_failures < 3) + { + Uint64 off = FileOffset(c,f,tor.getChunkSize()); + Uint8* buf = (Uint8*)fd->map(c,off,c->getSize(),CacheFile::READ); + if (buf) + { + c->setData(buf,Chunk::MMAPPED); + // only return when the mapping is OK + // if mmap fails we will just load it buffered + return; + } + else + mmap_failures++; + } + } + + Uint8* data = new Uint8[c->getSize()]; + Uint64 read = 0; // number of bytes read + for (Uint32 i = 0;i < tflist.count();i++) + { + const TorrentFile & f = tor.getFile(tflist[i]); + CacheFile* fd = files.find(tflist[i]); + DNDFile* dfd = dnd_files.find(tflist[i]); + + // first calculate offset into file + // only the first file can have an offset + // the following files will start at the beginning + Uint64 off = 0; + if (i == 0) + off = FileOffset(c,f,tor.getChunkSize()); + + Uint32 to_read = 0; + // then the amount of data we can read from this file + if (tflist.count() == 1) + to_read = c->getSize(); + else if (i == 0) + to_read = f.getLastChunkSize(); + else if (i == tflist.count() - 1) + to_read = c->getSize() - read; + else + to_read = f.getSize(); + + + // read part of data + if (fd) + fd->read(data + read,to_read,off); + else if (dfd) + { + Uint32 ret = 0; + if (i == 0) + ret = dfd->readLastChunk(data,read,c->getSize()); + else if (i == tflist.count() - 1) + ret = dfd->readFirstChunk(data,read,c->getSize()); + else + ret = dfd->readFirstChunk(data,read,c->getSize()); + + if (ret > 0 && ret != to_read) + Out() << "Warning : MultiFileCache::load ret != to_read" << endl; + } + read += to_read; + } + c->setData(data,Chunk::BUFFERED); + } + + + bool MultiFileCache::prep(Chunk* c) + { + // find out in which files a chunk lies + QValueList<Uint32> tflist; + tor.calcChunkPos(c->getIndex(),tflist); + +// Out() << "Prep " << c->getIndex() << endl; + if (tflist.count() == 1) + { + // in one so just mmap it + Uint64 off = FileOffset(c,tor.getFile(tflist.first()),tor.getChunkSize()); + CacheFile* fd = files.find(tflist.first()); + Uint8* buf = 0; + if (fd && Cache::mappedModeAllowed() && mmap_failures < 3) + { + buf = (Uint8*)fd->map(c,off,c->getSize(),CacheFile::RW); + if (!buf) + mmap_failures++; + } + + if (!buf) + { + // if mmap fails or is not possible use buffered mode + c->allocate(); + c->setStatus(Chunk::BUFFERED); + } + else + { + c->setData(buf,Chunk::MMAPPED); + } + } + else + { + // just allocate it + c->allocate(); + c->setStatus(Chunk::BUFFERED); + } + return true; + } + + void MultiFileCache::save(Chunk* c) + { + QValueList<Uint32> tflist; + tor.calcChunkPos(c->getIndex(),tflist); + + if (c->getStatus() == Chunk::MMAPPED) + { + // mapped chunks are easy + CacheFile* fd = files.find(tflist[0]); + if (!fd) + return; + + fd->unmap(c->getData(),c->getSize()); + c->clear(); + c->setStatus(Chunk::ON_DISK); + return; + } + + // Out() << "Writing to " << tflist.count() << " files " << endl; + Uint64 written = 0; // number of bytes written + for (Uint32 i = 0;i < tflist.count();i++) + { + const TorrentFile & f = tor.getFile(tflist[i]); + CacheFile* fd = files.find(tflist[i]); + DNDFile* dfd = dnd_files.find(tflist[i]); + + // first calculate offset into file + // only the first file can have an offset + // the following files will start at the beginning + Uint64 off = 0; + Uint32 to_write = 0; + if (i == 0) + { + off = FileOffset(c,f,tor.getChunkSize()); + } + + // the amount of data we can write to this file + if (tflist.count() == 1) + to_write = c->getSize(); + else if (i == 0) + to_write = f.getLastChunkSize(); + else if (i == tflist.count() - 1) + to_write = c->getSize() - written; + else + to_write = f.getSize(); + + // Out() << "to_write " << to_write << endl; + // write the data + if (fd) + fd->write(c->getData() + written,to_write,off); + else if (dfd) + { + if (i == 0) + dfd->writeLastChunk(c->getData() + written,to_write); + else if (i == tflist.count() - 1) + dfd->writeFirstChunk(c->getData() + written,to_write); + else + dfd->writeFirstChunk(c->getData() + written,to_write); + } + + written += to_write; + } + + // set the chunk to on disk and clear it + c->clear(); + c->setStatus(Chunk::ON_DISK); + } + + void MultiFileCache::downloadStatusChanged(TorrentFile* tf, bool download) + { + bool dnd = !download; + QString dnd_dir = tmpdir + "dnd" + bt::DirSeparator(); + // if it is dnd and it is already in the dnd tree do nothing + if (dnd && bt::Exists(dnd_dir + tf->getPath() + ".dnd")) + return; + + // if it is !dnd and it is already in the output_dir tree do nothing + if (!dnd && bt::Exists(output_dir + tf->getPath())) + return; + + + DNDFile* dfd = 0; + CacheFile* fd = 0; + try + { + + if (dnd && bt::Exists(dnd_dir + tf->getPath())) + { + // old download, we need to convert it + // save first and last chunk of the file + saveFirstAndLastChunk(tf,dnd_dir + tf->getPath(),dnd_dir + tf->getPath() + ".dnd"); + // delete symlink + bt::Delete(cache_dir + tf->getPath()); + bt::Delete(dnd_dir + tf->getPath()); // delete old dnd file + // recreate it + bt::SymLink(dnd_dir + tf->getPath() + ".dnd",cache_dir + tf->getPath()); + + files.erase(tf->getIndex()); + dfd = new DNDFile(dnd_dir + tf->getPath() + ".dnd"); + dfd->checkIntegrity(); + dnd_files.insert(tf->getIndex(),dfd); + } + else if (dnd) + { + // save first and last chunk of the file + if (bt::Exists(output_dir + tf->getPath())) + saveFirstAndLastChunk(tf,output_dir + tf->getPath(),dnd_dir + tf->getPath() + ".dnd"); + + // delete symlink + bt::Delete(cache_dir + tf->getPath()); + // delete data file + bt::Delete(output_dir + tf->getPath(),true); + // recreate it + bt::SymLink(dnd_dir + tf->getPath() + ".dnd",cache_dir + tf->getPath()); + + files.erase(tf->getIndex()); + dfd = new DNDFile(dnd_dir + tf->getPath() + ".dnd"); + dfd->checkIntegrity(); + dnd_files.insert(tf->getIndex(),dfd); + } + else + { + // recreate the file + recreateFile(tf,dnd_dir + tf->getPath() + ".dnd",output_dir + tf->getPath()); + // delete symlink and dnd file + bt::Delete(cache_dir + tf->getPath()); + bt::Delete(dnd_dir + tf->getPath() + ".dnd"); + // recreate it + bt::SymLink(output_dir + tf->getPath(),cache_dir + tf->getPath()); + dnd_files.erase(tf->getIndex()); + + fd = new CacheFile(); + fd->open(output_dir + tf->getPath(),tf->getSize()); + files.insert(tf->getIndex(),fd); + } + } + catch (bt::Error & err) + { + delete fd; + delete dfd; + Out() << err.toString() << endl; + } + } + + + + void MultiFileCache::saveFirstAndLastChunk(TorrentFile* tf,const QString & src_file,const QString & dst_file) + { + DNDFile out(dst_file); + File fptr; + if (!fptr.open(src_file,"rb")) + throw Error(i18n("Cannot open file %1 : %2").arg(src_file).arg(fptr.errorString())); + + Uint32 cs = 0; + if (tf->getFirstChunk() == tor.getNumChunks() - 1) + { + cs = tor.getFileLength() % tor.getChunkSize(); + if (cs == 0) + cs = tor.getChunkSize(); + } + else + cs = tor.getChunkSize(); + + Uint8* tmp = new Uint8[tor.getChunkSize()]; + try + { + fptr.read(tmp,cs - tf->getFirstChunkOffset()); + out.writeFirstChunk(tmp,cs - tf->getFirstChunkOffset()); + + if (tf->getFirstChunk() != tf->getLastChunk()) + { + Uint64 off = FileOffset(tf->getLastChunk(),*tf,tor.getChunkSize()); + fptr.seek(File::BEGIN,off); + fptr.read(tmp,tf->getLastChunkSize()); + out.writeLastChunk(tmp,tf->getLastChunkSize()); + } + delete [] tmp; + } + catch (...) + { + delete [] tmp; + throw; + } + } + + void MultiFileCache::recreateFile(TorrentFile* tf,const QString & dnd_file,const QString & output_file) + { + DNDFile dnd(dnd_file); + + // create the output file + bt::Touch(output_file); + // truncate it + try + { + bool res = false; + + #ifdef HAVE_XFS_XFS_H + if( (! res) && (Settings::fullDiskPreallocMethod() == 1) ) + { + res = XfsPreallocate(output_file, tf->getSize()); + } + #endif + + if(! res) + { + bt::TruncateFile(output_file,tf->getSize()); + } + } + catch (bt::Error & e) + { + // first attempt failed, must be fat so try that + if (!FatPreallocate(output_file,tf->getSize())) + { + throw Error(i18n("Cannot preallocate diskspace : %1").arg(strerror(errno))); + } + } + + Uint32 cs = 0; + if (tf->getFirstChunk() == tor.getNumChunks() - 1) + { + cs = tor.getFileLength() % tor.getChunkSize(); + if (cs == 0) + cs = tor.getChunkSize(); + } + else + cs = tor.getChunkSize(); + + File fptr; + if (!fptr.open(output_file,"r+b")) + throw Error(i18n("Cannot open file %1 : %2").arg(output_file).arg(fptr.errorString())); + + + Uint32 ts = cs - tf->getFirstChunkOffset() > tf->getLastChunkSize() ? + cs - tf->getFirstChunkOffset() : tf->getLastChunkSize(); + Uint8* tmp = new Uint8[ts]; + + try + { + dnd.readFirstChunk(tmp,0,cs - tf->getFirstChunkOffset()); + fptr.write(tmp,cs - tf->getFirstChunkOffset()); + + if (tf->getFirstChunk() != tf->getLastChunk()) + { + Uint64 off = FileOffset(tf->getLastChunk(),*tf,tor.getChunkSize()); + fptr.seek(File::BEGIN,off); + dnd.readLastChunk(tmp,0,tf->getLastChunkSize()); + fptr.write(tmp,tf->getLastChunkSize()); + } + delete [] tmp; + } + catch (...) + { + delete [] tmp; + throw; + } + } + + void MultiFileCache::preallocateDiskSpace(PreallocationThread* prealloc) + { + Out() << "MultiFileCache::preallocateDiskSpace" << endl; + PtrMap<Uint32,CacheFile>::iterator i = files.begin(); + while (i != files.end()) + { + CacheFile* cf = i->second; + if (!prealloc->isStopped()) + { + cf->preallocate(prealloc); + } + else + { + // we got interrupted tell the thread we are not finished and return + prealloc->setNotFinished(); + return; + } + i++; + } + } + + bool MultiFileCache::hasMissingFiles(QStringList & sl) + { + bool ret = false; + for (Uint32 i = 0;i < tor.getNumFiles();i++) + { + TorrentFile & tf = tor.getFile(i); + if (tf.doNotDownload()) + continue; + + QString p = cache_dir + tf.getPath(); + QFileInfo fi(p); + // always use symlink first, file might have been moved + if (!fi.exists()) + { + ret = true; + p = fi.readLink(); + if (p.isNull()) + p = output_dir + tf.getPath(); + sl.append(p); + tf.setMissing(true); + } + else + { + p = output_dir + tf.getPath(); + // no symlink so try the actual file + if (!bt::Exists(p)) + { + ret = true; + sl.append(p); + tf.setMissing(true); + } + } + } + return ret; + } + + static void DeleteEmptyDirs(const QString & output_dir,const QString & fpath) + { + QStringList sl = QStringList::split(bt::DirSeparator(),fpath); + // remove the last, which is just the filename + sl.pop_back(); + + while (sl.count() > 0) + { + QString path = output_dir; + // reassemble the full directory path + for (QStringList::iterator itr = sl.begin(); itr != sl.end();itr++) + path += *itr + bt::DirSeparator(); + + QDir dir(path); + QStringList el = dir.entryList(QDir::All|QDir::System|QDir::Hidden); + el.remove("."); + el.remove(".."); + if (el.count() == 0) + { + // no childern so delete the directory + Out(SYS_GEN|LOG_IMPORTANT) << "Deleting empty directory : " << path << endl; + bt::Delete(path,true); + sl.pop_back(); // remove the last so we can go one higher + } + else + { + + // children, so we cannot delete any more directories higher up + return; + } + } + + // now the output_dir itself + QDir dir(output_dir); + QStringList el = dir.entryList(QDir::All|QDir::System|QDir::Hidden); + el.remove("."); + el.remove(".."); + if (el.count() == 0) + { + Out(SYS_GEN|LOG_IMPORTANT) << "Deleting empty directory : " << output_dir << endl; + bt::Delete(output_dir,true); + } + } + + void MultiFileCache::deleteDataFiles() + { + for (Uint32 i = 0;i < tor.getNumFiles();i++) + { + TorrentFile & tf = tor.getFile(i); + QString fpath = tf.getPath(); + if (!tf.doNotDownload()) + { + // first delete the file + bt::Delete(output_dir + fpath); + } + + // check for subdirectories + DeleteEmptyDirs(output_dir,fpath); + } + } + + Uint64 MultiFileCache::diskUsage() + { + Uint64 sum = 0; + + for (Uint32 i = 0;i < tor.getNumFiles();i++) + { + TorrentFile & tf = tor.getFile(i); + if (tf.doNotDownload()) + continue; + + try + { + CacheFile* cf = files.find(i); + if (cf) + { + sum += cf->diskUsage(); + } + else + { + // doesn't exist yet, must be before open is called + // so create one and delete it right after + cf = new CacheFile(); + cf->open(cache_dir + tf.getPath(),tf.getSize()); + sum += cf->diskUsage(); + delete cf; + } + } + catch (bt::Error & err) // make sure we catch any exceptions + { + Out(SYS_DIO|LOG_DEBUG) << "Error: " << err.toString() << endl; + } + } + + return sum; + } + + /////////////////////////////// + + Uint64 FileOffset(Chunk* c,const TorrentFile & f,Uint64 chunk_size) + { + return FileOffset(c->getIndex(),f,chunk_size); + } + + Uint64 FileOffset(Uint32 cindex,const TorrentFile & f,Uint64 chunk_size) + { + return f.fileOffset(cindex,chunk_size); + } + +} diff --git a/libktorrent/torrent/multifilecache.h b/libktorrent/torrent/multifilecache.h new file mode 100644 index 0000000..9c1280e --- /dev/null +++ b/libktorrent/torrent/multifilecache.h @@ -0,0 +1,74 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTMULTIFILECACHE_H +#define BTMULTIFILECACHE_H + + +#include <util/ptrmap.h> +#include "cache.h" +#include "settings.h" + +namespace bt +{ + class DNDFile; + class CacheFile; + + /** + * @author Joris Guisson + * @brief Cache for multi file torrents + * + * This class manages a multi file torrent cache. Everything gets stored in the + * correct files immediately. + */ + class MultiFileCache : public Cache + { + QString cache_dir,output_dir; + PtrMap<Uint32,CacheFile> files; + PtrMap<Uint32,DNDFile> dnd_files; + public: + MultiFileCache(Torrent& tor,const QString & tmpdir,const QString & datadir,bool custom_output_name); + virtual ~MultiFileCache(); + + virtual void changeTmpDir(const QString& ndir); + virtual void create(); + virtual void load(Chunk* c); + virtual void save(Chunk* c); + virtual bool prep(Chunk* c); + virtual void close(); + virtual void open(); + virtual QString getOutputPath() const; + virtual void changeOutputPath(const QString & outputpath); + virtual KIO::Job* moveDataFiles(const QString & ndir); + virtual void moveDataFilesCompleted(KIO::Job* job); + virtual void preallocateDiskSpace(PreallocationThread* prealloc); + virtual bool hasMissingFiles(QStringList & sl); + virtual void deleteDataFiles(); + virtual Uint64 diskUsage(); + private: + void touch(TorrentFile & tf); + virtual void downloadStatusChanged(TorrentFile*, bool); + QString guessDataDir(); + void saveFirstAndLastChunk(TorrentFile* tf,const QString & src_file,const QString & dst_file); + void recreateFile(TorrentFile* tf,const QString & dnd_file,const QString & output_file); + }; + +} + +#endif diff --git a/libktorrent/torrent/newchokealgorithm.cpp b/libktorrent/torrent/newchokealgorithm.cpp new file mode 100644 index 0000000..875f356 --- /dev/null +++ b/libktorrent/torrent/newchokealgorithm.cpp @@ -0,0 +1,345 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#if 0 +#include <util/log.h> +#include <util/timer.h> +#include <util/functions.h> +#include <torrent/globals.h> +#include <interfaces/functions.h> +#include "newchokealgorithm.h" +#include "peermanager.h" +#include "peer.h" +#include "packetwriter.h" +#include "peeruploader.h" + + +using namespace kt; + +namespace bt +{ + + + + NewChokeAlgorithm::NewChokeAlgorithm(): ChokeAlgorithm() + { + round_state = 1; + } + + + NewChokeAlgorithm::~NewChokeAlgorithm() + {} + + int RevDownloadRateCmp(Peer* a,Peer* b) + { + if (b->getDownloadRate() > a->getDownloadRate()) + return 1; + else if (a->getDownloadRate() > b->getDownloadRate()) + return -1; + else + return 0; + } + + void NewChokeAlgorithm::doChokingLeechingState(PeerManager & pman,ChunkManager & cman,const kt::TorrentStats & stats) + { + Uint32 num_peers = pman.getNumConnectedPeers(); + if (num_peers == 0) + return; + + Uint32 now = GetCurrentTime(); + Peer* poup = pman.findPeer(opt_unchoked_peer_id); + Peer* unchokers[] = {0,0,0,0}; + + // first find the planned optimistic unchoked peer if we are in the correct round + if (round_state == 1 || poup == 0) + { + opt_unchoked_peer_id = findPlannedOptimisticUnchokedPeer(pman); + poup = pman.findPeer(opt_unchoked_peer_id); + } + + PeerPtrList peers,other; + // now get all the peers who are interested and have sent us a piece in the + // last 30 seconds + for (Uint32 i = 0;i < num_peers;i++) + { + Peer* p = pman.getPeer(i); + if (!p) + continue; + + if (!p->isSeeder()) + { + if (p->isInterested() && now - p->getTimeSinceLastPiece() <= 30000) + peers.append(p); + else + other.append(p); + } + else + { + p->choke(); + } + } + + // sort them using a reverse download rate compare + // so that the fastest downloaders are in front + peers.setCompareFunc(RevDownloadRateCmp); + peers.sort(); + other.setCompareFunc(RevDownloadRateCmp); + other.sort(); + + // get the first tree and punt them in the unchokers + for (Uint32 i = 0;i < 3;i++) + { + if (i < peers.count()) + { + unchokers[i] = peers.at(i); + } + } + + // see if poup if part of the first 3 + // and if necessary replace it + bool poup_in_unchokers = false; + Uint32 attempts = 0; + do + { + poup_in_unchokers = false; + for (Uint32 i = 0;i < 3;i++) + { + if (unchokers[i] != poup) + continue; + + opt_unchoked_peer_id = findPlannedOptimisticUnchokedPeer(pman); + poup = pman.findPeer(opt_unchoked_peer_id); + poup_in_unchokers = true; + break; + } + // we don't want to keep trying this forever, so limit it to 5 atttempts + attempts++; + }while (poup_in_unchokers && attempts < 5); + + unchokers[3] = poup; + + Uint32 other_idx = 0; + Uint32 peers_idx = 3; + // unchoke the 4 unchokers + for (Uint32 i = 0;i < 4;i++) + { + if (!unchokers[i]) + { + // pick some other peer to unchoke + unchokers[i] = peers.at(peers_idx++); + if (unchokers[i] == poup) // it must not be equal to the poup + unchokers[i] = peers.at(peers_idx++); + + // nobody in the peers list, try the others list + if (!unchokers[i]) + unchokers[i] = other.at(other_idx++); + } + + if (unchokers[i]) + unchokers[i]->getPacketWriter().sendUnchoke(); + } + + // choke the rest + for (Uint32 i = 0;i < num_peers;i++) + { + Peer* p = pman.getPeer(i); + if (p == unchokers[0] || p == unchokers[1] || p == unchokers[2] || p == unchokers[3]) + continue; + if (p) + p->choke(); + } + + round_state++; + if (round_state > 3) + round_state = 1; + } + + Uint32 NewChokeAlgorithm::findPlannedOptimisticUnchokedPeer(PeerManager& pman) + { + Uint32 num_peers = pman.getNumConnectedPeers(); + if (num_peers == 0) + return UNDEFINED_ID; + + // find a random peer that is choked and interested + Uint32 start = rand() % num_peers; + Uint32 i = (start + 1) % num_peers; + while (i != start) + { + Peer* p = pman.getPeer(i); + if (p && p->isChoked() && p->isInterested() && !p->isSeeder()) + return p->getID(); + i = (i + 1) % num_peers; + } + + // we do not expect to have 4 billion peers + return 0xFFFFFFFF; + } + + ////////////////////////////////////////////// + + int NChokeCmp(Peer* a,Peer* b) + { + Uint32 now = GetCurrentTime(); + // if they have pending upload requests or they were unchoked in the last 20 seconds, + // they are category 1 + bool a_first_class = a->getPeerUploader()->getNumRequests() > 0 || + (now - a->getUnchokeTime() <= 20000); + bool b_first_class = b->getPeerUploader()->getNumRequests() > 0 || + (now - b->getUnchokeTime() <= 20000); + + if (a_first_class && !b_first_class) + { + // category 1 come first + return -1; + } + else if (!a_first_class && b_first_class) + { + // category 1 come first + return 1; + } + else + { + // use upload rate to differentiate peers of the same class + if (a->getUploadRate() > b->getUploadRate()) + return -1; + else if (b->getUploadRate() > a->getUploadRate()) + return 1; + else + return 0; + } + } + + + void NewChokeAlgorithm::doChokingSeedingState(PeerManager & pman,ChunkManager & cman,const kt::TorrentStats & stats) + { + Uint32 num_peers = pman.getNumConnectedPeers(); + if (num_peers == 0) + return; + + // first get all unchoked and interested peers + PeerPtrList peers,others; + for (Uint32 i = 0;i < num_peers;i++) + { + Peer* p = pman.getPeer(i); + if (!p) + continue; + + if (!p->isSeeder()) + { + if (!p->isChoked() && p->isInterested()) + peers.append(p); + else + others.append(p); + } + else + { + p->choke(); + } + } + + // sort them + peers.setCompareFunc(NChokeCmp); + peers.sort(); + others.setCompareFunc(NChokeCmp); + others.sort(); + + // first round so take the 4 first peers + if (round_state == 1) + { + Uint32 num_unchoked = 0; + for (Uint32 i = 0;i < peers.count();i++) + { + Peer* p = peers.at(i); + if (!p) + continue; + + if (num_unchoked < 4) + { + p->getPacketWriter().sendUnchoke(); + num_unchoked++; + } + else + p->choke(); + } + // go over the other peers and unchoke, if we do not have enough + for (Uint32 i = 0;i < others.count();i++) + { + Peer* p = others.at(i); + if (!p) + continue; + + if (num_unchoked < 4) + { + p->getPacketWriter().sendUnchoke(); + num_unchoked++; + } + else + p->choke(); + } + } + else + { + Uint32 rnd = 0; + if (peers.count() > 3) + rnd = 3 + rand() % (peers.count() - 3); + + Uint32 num_unchoked = 0; + // take the first 3 and a random one + for (Uint32 i = 0;i < peers.count();i++) + { + Peer* p = peers.at(i); + if (!p) + continue; + + if (num_unchoked < 4 || i == rnd) + { + p->getPacketWriter().sendUnchoke(); + num_unchoked++; + } + else + p->choke(); + } + + // go over the other peers and unchoke, if we do not have enough + for (Uint32 i = 0;i < others.count();i++) + { + Peer* p = others.at(i); + if (!p) + continue; + + if (num_unchoked < 4 || i == rnd) + { + p->getPacketWriter().sendUnchoke(); + num_unchoked++; + } + else + p->choke(); + } + } + + round_state++; + if (round_state > 3) + round_state = 1; + } + + + +} +#endif + diff --git a/libktorrent/torrent/newchokealgorithm.h b/libktorrent/torrent/newchokealgorithm.h new file mode 100644 index 0000000..9a0738a --- /dev/null +++ b/libktorrent/torrent/newchokealgorithm.h @@ -0,0 +1,54 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#if 0 +#ifndef BTNEWCHOKEALGORITHM_H +#define BTNEWCHOKEALGORITHM_H + +#include <choker.h> + +namespace bt +{ + + /** + * @author Joris Guisson + * + * The new choking algorithm. + */ + class NewChokeAlgorithm : public ChokeAlgorithm + { + Uint32 round_state; + public: + NewChokeAlgorithm(); + virtual ~NewChokeAlgorithm(); + + virtual void doChokingLeechingState(PeerManager & pman,ChunkManager & cman,const kt::TorrentStats & stats); + virtual void doChokingSeedingState(PeerManager & pman,ChunkManager & cman,const kt::TorrentStats & stats); + private: + void doChokingLeecherState(PeerManager& pman); + void doChokingSeederState(PeerManager& pman); + + Uint32 findPlannedOptimisticUnchokedPeer(PeerManager& pman); + }; + +} + +#endif +#endif + diff --git a/libktorrent/torrent/oldchokealgorithm.cpp b/libktorrent/torrent/oldchokealgorithm.cpp new file mode 100644 index 0000000..e24d63a --- /dev/null +++ b/libktorrent/torrent/oldchokealgorithm.cpp @@ -0,0 +1,223 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <interfaces/functions.h> +#include "oldchokealgorithm.h" +#include "peer.h" +#include "packetwriter.h" +#include "peermanager.h" + + +using namespace kt; + +namespace bt +{ + int UploadRateCmp(Peer* pa,Peer* pb) + { + return CompareVal(pa->getUploadRate(),pb->getUploadRate()); + } + + int DownloadRateCmp(Peer* pa,Peer* pb) + { + return CompareVal(pa->getDownloadRate(),pb->getDownloadRate()); + } + + + OldChokeAlgorithm::OldChokeAlgorithm(): ChokeAlgorithm() + { + opt_unchoke_index = 0; + opt_unchoke = 1; + } + + + OldChokeAlgorithm::~OldChokeAlgorithm() + {} + + + void OldChokeAlgorithm::doChoking(PeerManager& pman, bool have_all) + { + if (pman.getNumConnectedPeers() == 0) + return; + + downloaders.clear(); + interested.clear(); + not_interested.clear(); + + // first alert everybody that we're interested or not + sendInterested(pman,have_all); + // get who is interested and not + updateInterested(pman); + // them sort them; + if (have_all) + { + interested.setCompareFunc(DownloadRateCmp); + interested.sort(); + not_interested.setCompareFunc(DownloadRateCmp); + not_interested.sort(); + } + else + { + interested.setCompareFunc(UploadRateCmp); + interested.sort(); + not_interested.setCompareFunc(UploadRateCmp); + not_interested.sort(); + } + // determine the downloaders + updateDownloaders(); + // unchoke the not_interested peers + // which have a faster upload rate then the downloaders + sendUnchokes(have_all); + // optimisticly unchoke somebody + optimisticUnchoke(pman); + } + + void OldChokeAlgorithm::updateInterested(PeerManager& pman) + { + for (Uint32 i = 0;i < pman.getNumConnectedPeers();i++) + { + Peer* p = pman.getPeer(i); + + if (p->getID() == opt_unchoked_peer_id) + continue; + + if (p->isInterested()) + { + interested.append(p); + } + else + { + not_interested.append(p); + } + } + } + + void OldChokeAlgorithm::updateDownloaders() + { + QPtrList<Peer>::iterator itr = interested.begin(); + int num = 0; + // send all downloaders an unchoke + for (;itr != interested.end();itr++) + { + Peer* p = *itr; + + if (p->getID() == opt_unchoked_peer_id) + continue; + + if (num < 4) + { + p->choke(); + downloaders.append(p); + num++; + } + else + { + p->choke(); + } + } + } + + void OldChokeAlgorithm::sendInterested(PeerManager& pman,bool have_all) + { + for (Uint32 i = 0;i < pman.getNumConnectedPeers();i++) + { + Peer* p = pman.getPeer(i); + PacketWriter & pout = p->getPacketWriter(); + // if we don't have the entire file, send an intereseted message, + // else we're not intereseted + if (have_all && p->areWeInterested()) + pout.sendNotInterested(); + else if (!have_all && !p->areWeInterested()) + pout.sendInterested(); + } + } + + void OldChokeAlgorithm::sendUnchokes(bool have_all) + { + if (downloaders.count() == 0) + return; + + QPtrList<Peer>::iterator itr = not_interested.begin(); + // fd = fastest_downloader + Peer* fd = downloaders.first(); + // send all downloaders an unchoke + for (;itr != not_interested.end();itr++) + { + Peer* p = *itr; + if (p->getID() == opt_unchoked_peer_id) + continue; + + if ((have_all && p->getDownloadRate() > fd->getDownloadRate()) || + (!have_all && p->getUploadRate() > fd->getUploadRate())) + { + p->getPacketWriter().sendUnchoke(); + } + else + { + p->getPacketWriter().sendChoke(); + } + } + } + + void OldChokeAlgorithm::optimisticUnchoke(PeerManager& pman) + { + if (pman.getNumConnectedPeers() == 0) + return; + + // only switch optimistic unchoked peer every 30 seconds + // (update interval of choker is 10 seconds) + if (opt_unchoke != 3) + { + opt_unchoke++; + return; + } + + // Get current time + QTime now = QTime::currentTime(); + QPtrList<Peer> peers; // list to store peers to select from + + // recently connected peers == peers connected in the last 5 minutes + const int RECENTLY_CONNECT_THRESH = 5*60; + + for (Uint32 i = 0;i < pman.getNumConnectedPeers();i++) + { + Peer* p = pman.getPeer(i); + if (p->getConnectTime().secsTo(now) < RECENTLY_CONNECT_THRESH) + { + // we favor recently connected peers 3 times over other peers + // so we add them 3 times to the list + peers.append(p); + peers.append(p); + peers.append(p); + } + else + { + // not recent, so just add one time + peers.append(p); + } + } + + // draw a random one from the list and send it an unchoke + opt_unchoke_index = rand() % peers.count(); + Peer* lucky_one = peers.at(opt_unchoke_index); + lucky_one->getPacketWriter().sendUnchoke(); + opt_unchoked_peer_id = lucky_one->getID(); + opt_unchoke = 1; + } + +} diff --git a/libktorrent/torrent/oldchokealgorithm.h b/libktorrent/torrent/oldchokealgorithm.h new file mode 100644 index 0000000..bc813a8 --- /dev/null +++ b/libktorrent/torrent/oldchokealgorithm.h @@ -0,0 +1,54 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTOLDCHOKEALGORITHM_H +#define BTOLDCHOKEALGORITHM_H + +#include <choker.h> + +namespace bt +{ + + /** + * @author Joris Guisson + * + * The old choking algorithm as it is described on wiki.theory.org. + */ + class OldChokeAlgorithm : public ChokeAlgorithm + { + int opt_unchoke_index; + int opt_unchoke; + + PeerPtrList downloaders,interested,not_interested; + public: + OldChokeAlgorithm(); + virtual ~OldChokeAlgorithm(); + + virtual void doChoking(PeerManager& pman, bool have_all); + private: + void updateInterested(PeerManager& pman); + void updateDownloaders(); + void sendInterested(PeerManager& pman,bool have_all); + void sendUnchokes(bool have_all); + void optimisticUnchoke(PeerManager& pman); + }; + +} + +#endif diff --git a/libktorrent/torrent/packet.cpp b/libktorrent/torrent/packet.cpp new file mode 100644 index 0000000..febede2 --- /dev/null +++ b/libktorrent/torrent/packet.cpp @@ -0,0 +1,175 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <qstring.h> +#include <string.h> +#include <util/log.h> +#include <util/bitset.h> +#include <util/functions.h> +#include <torrent/globals.h> +#include "packet.h" +#include "request.h" +#include "chunk.h" +#include "peer.h" + +namespace bt +{ + + static Uint8* AllocPacket(Uint32 size,Uint8 type) + { + Uint8* data = new Uint8[size]; + WriteUint32(data,0,size - 4); + data[4] = type; + return data; + } + + + Packet::Packet(Uint8 type) : data(0),size(0),written(0) + { + size = 5; + data = AllocPacket(size,type); + } + + Packet::Packet(Uint16 port) : data(0),size(0),written(0) + { + size = 7; + data = AllocPacket(size,PORT); + WriteUint16(data,5,port); + + } + + Packet::Packet(Uint32 chunk,Uint8 type) : data(0),size(0),written(0) + { + size = 9; + data = AllocPacket(size,type); + WriteUint32(data,5,chunk); + } + + Packet::Packet(const BitSet & bs) : data(0),size(0),written(0) + { + size = 5 + bs.getNumBytes(); + data = AllocPacket(size,BITFIELD); + memcpy(data+5,bs.getData(),bs.getNumBytes()); + } + + Packet::Packet(const Request & r,Uint8 type) : data(0),size(0),written(0) + { + size = 17; + data = AllocPacket(size,type); + WriteUint32(data,5,r.getIndex()); + WriteUint32(data,9,r.getOffset()); + WriteUint32(data,13,r.getLength()); + } + + Packet::Packet(Uint32 index,Uint32 begin,Uint32 len,Chunk* ch) : data(0),size(0),written(0) + { + size = 13 + len; + data = AllocPacket(size,PIECE); + WriteUint32(data,5,index); + WriteUint32(data,9,begin); + memcpy(data+13,ch->getData() + begin,len); + } + + Packet::Packet(Uint8 ext_id,const QByteArray & ext_data) : data(0),size(0),written(0) + { + size = 6 + ext_data.size(); + data = AllocPacket(size,EXTENDED); + data[5] = ext_id; + memcpy(data + 6,ext_data.data(),ext_data.size()); + } + + Packet::~Packet() + { + delete [] data; + } + + bool Packet::isPiece(const Request & req) const + { + if (data[4] == PIECE) + { + if (ReadUint32(data,5) != req.getIndex()) + return false; + + if (ReadUint32(data,9) != req.getOffset()) + return false; + + if (ReadUint32(data,13) != req.getLength()) + return false; + + return true; + } + return false; + } + + Packet* Packet::makeRejectOfPiece() + { + if (getType() != PIECE) + return 0; + + Uint32 idx = bt::ReadUint32(data,5); + Uint32 off = bt::ReadUint32(data,9); + Uint32 len = size - 13; + + // Out(SYS_CON|LOG_DEBUG) << "Packet::makeRejectOfPiece " << idx << " " << off << " " << len << endl; + return new Packet(Request(idx,off,len,0),bt::REJECT_REQUEST); + } + + /* + QString Packet::debugString() const + { + if (!data) + return QString::null; + + switch (data[4]) + { + case CHOKE : return QString("CHOKE %1 %2").arg(hdr_length).arg(data_length); + case UNCHOKE : return QString("UNCHOKE %1 %2").arg(hdr_length).arg(data_length); + case INTERESTED : return QString("INTERESTED %1 %2").arg(hdr_length).arg(data_length); + case NOT_INTERESTED : return QString("NOT_INTERESTED %1 %2").arg(hdr_length).arg(data_length); + case HAVE : return QString("HAVE %1 %2").arg(hdr_length).arg(data_length); + case BITFIELD : return QString("BITFIELD %1 %2").arg(hdr_length).arg(data_length); + case PIECE : return QString("PIECE %1 %2").arg(hdr_length).arg(data_length); + case REQUEST : return QString("REQUEST %1 %2").arg(hdr_length).arg(data_length); + case CANCEL : return QString("CANCEL %1 %2").arg(hdr_length).arg(data_length); + default: return QString("UNKNOWN %1 %2").arg(hdr_length).arg(data_length); + } + } + */ + bool Packet::isOK() const + { + if (!data) + return false; + + return true; + } + + Uint32 Packet::putInOutputBuffer(Uint8* buf,Uint32 max_to_put,bool & piece) + { + piece = data[4] == PIECE; + Uint32 bw = size - written; + if (!bw) // nothing to write + return 0; + + if (bw > max_to_put) + bw = max_to_put; + memcpy(buf,data + written,bw); + written += bw; + return bw; + } +} diff --git a/libktorrent/torrent/packet.h b/libktorrent/torrent/packet.h new file mode 100644 index 0000000..9259d31 --- /dev/null +++ b/libktorrent/torrent/packet.h @@ -0,0 +1,91 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTPACKET_H +#define BTPACKET_H + +#include "globals.h" + +class QString; + +namespace bt +{ + class BitSet; + class Request; + class Chunk; + class Peer; + + /** + * @author Joris Guisson + * + * Packet off data, which gets sent to a Peer + */ + class Packet + { + Uint8* data; + Uint32 size; + Uint32 written; + public: + Packet(Uint8 type); + Packet(Uint16 port); + Packet(Uint32 chunk,Uint8 type); + Packet(const BitSet & bs); + Packet(const Request & req,Uint8 type); + Packet(Uint32 index,Uint32 begin,Uint32 len,Chunk* ch); + Packet(Uint8 ext_id,const QByteArray & ext_data); // extension protocol packet + virtual ~Packet(); + + Uint8 getType() const {return data ? data[4] : 0;} + + bool isOK() const; + + const Uint8* getData() const {return data;} + Uint32 getDataLength() const {return size;} + + Uint32 isSent() const {return written == size;} + + /** + * If this packet is a piece, make a reject for it. + * @return The newly created Packet, 0 if this is not a piece + */ + Packet* makeRejectOfPiece(); + + /// Are we sending this packet ? + bool sending() const {return written > 0;} + + /** + * Is this a piece packet which matches a request + * @param req The request + * @return If this is a piece in response of this request + */ + bool isPiece(const Request & req) const; + + /** + * Put the packet in an output buffer. + * @param buf The buffer + * @param max_to_put Maximum bytes to put + * @param piece Set to true if this is a piece + * @return The number of bytes put in the buffer + */ + Uint32 putInOutputBuffer(Uint8* buf,Uint32 max_to_put,bool & piece); + }; + +} + +#endif diff --git a/libktorrent/torrent/packetreader.cpp b/libktorrent/torrent/packetreader.cpp new file mode 100644 index 0000000..8df348b --- /dev/null +++ b/libktorrent/torrent/packetreader.cpp @@ -0,0 +1,247 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +//#define LOG_PACKET +#ifdef LOG_PACKET +#include <sys/types.h> +#include <unistd.h> +#endif + +#include <util/log.h> +#include <util/file.h> +#include <util/functions.h> +#include "packetreader.h" +#include "peer.h" + + +namespace bt +{ +#ifdef LOG_PACKET + static void LogPacket(const Uint8* data,Uint32 size,Uint32 len) + { + QString file = QString("/tmp/kt-packetreader-%1.log").arg(getpid()); + File fptr; + if (!fptr.open(file,"a")) + return; + + + QString tmp = QString("PACKET len = %1, type = %2\nDATA: \n").arg(len).arg(data[0]); + + fptr.write(tmp.ascii(),tmp.length()); + + Uint32 j = 0; + if (size <= 40) + { + for (Uint32 i = 0;i < size;i++) + { + tmp = QString("0x%1 ").arg(data[i],0,16); + fptr.write(tmp.ascii(),tmp.length()); + j++; + if (j > 10) + { + fptr.write("\n",1); + j = 0; + } + } + } + else + { + for (Uint32 i = 0;i < 20;i++) + { + tmp = QString("0x%1 ").arg(data[i],0,16); + fptr.write(tmp.ascii(),tmp.length()); + j++; + if (j > 10) + { + fptr.write("\n",1); + j = 0; + } + } + tmp = QString("\n ... \n"); + fptr.write(tmp.ascii(),tmp.length()); + for (Uint32 i = size - 20;i < size;i++) + { + tmp = QString("0x%1 ").arg(data[i],0,16); + fptr.write(tmp.ascii(),tmp.length()); + j++; + if (j > 10) + { + fptr.write("\n",1); + j = 0; + } + } + } + fptr.write("\n",1); + } +#endif + + IncomingPacket::IncomingPacket(Uint32 size) : data(0),size(size),read(0) + { + data = new Uint8[size]; + } + + IncomingPacket::~IncomingPacket() + { + delete [] data; + } + + PacketReader::PacketReader(Peer* peer) + : peer(peer),error(false) + { + packet_queue.setAutoDelete(true); + len_received = -1; + } + + + PacketReader::~PacketReader() + { + } + + + void PacketReader::update() + { + if (error) + return; + + mutex.lock(); + // pass packets to peer + while (packet_queue.count() > 0) + { + IncomingPacket* pck = packet_queue.first(); + if (pck->read == pck->size) + { + // full packet is read pass it to peer + peer->packetReady(pck->data,pck->size); + packet_queue.removeFirst(); + } + else + { + // packet is not yet full, break out of loop + break; + } + } + mutex.unlock(); + } + + Uint32 PacketReader::newPacket(Uint8* buf,Uint32 size) + { + Uint32 packet_length = 0; + Uint32 am_of_len_read = 0; + if (len_received > 0) + { + if (size < 4 - len_received) + { + memcpy(len + len_received,buf,size); + len_received += size; + return size; + } + else + { + memcpy(len + len_received,buf,4 - len_received); + am_of_len_read = 4 - len_received; + len_received = 0; + packet_length = ReadUint32(len,0); + + } + } + else if (size < 4) + { + memcpy(len,buf,size); + len_received = size; + return size; + } + else + { + packet_length = ReadUint32(buf,0); + am_of_len_read = 4; + } + + if (packet_length == 0) + return am_of_len_read; + + if (packet_length > MAX_PIECE_LEN + 13) + { + Out(SYS_CON|LOG_DEBUG) << " packet_length too large " << packet_length << endl; + + error = true; + return size; + } + + IncomingPacket* pck = new IncomingPacket(packet_length); + packet_queue.append(pck); + return am_of_len_read + readPacket(buf + am_of_len_read,size - am_of_len_read); + } + + Uint32 PacketReader::readPacket(Uint8* buf,Uint32 size) + { + if (!size) + return 0; + + IncomingPacket* pck = packet_queue.last(); + if (pck->read + size >= pck->size) + { + // we can read the full packet + Uint32 tr = pck->size - pck->read; + memcpy(pck->data + pck->read,buf,tr); + pck->read += tr; + return tr; + } + else + { + // we can do a partial read + Uint32 tr = size; + memcpy(pck->data + pck->read,buf,tr); + pck->read += tr; + return tr; + } + } + + + void PacketReader::onDataReady(Uint8* buf,Uint32 size) + { + if (error) + return; + + mutex.lock(); + if (packet_queue.count() == 0) + { + Uint32 ret = 0; + while (ret < size && !error) + { + ret += newPacket(buf + ret,size - ret); + } + } + else + { + Uint32 ret = 0; + IncomingPacket* pck = packet_queue.last(); + if (pck->read == pck->size) // last packet in queue is fully read + ret = newPacket(buf,size); + else + ret = readPacket(buf,size); + + while (ret < size && !error) + { + ret += newPacket(buf + ret,size - ret); + } + } + mutex.unlock(); + } +} diff --git a/libktorrent/torrent/packetreader.h b/libktorrent/torrent/packetreader.h new file mode 100644 index 0000000..da1e03e --- /dev/null +++ b/libktorrent/torrent/packetreader.h @@ -0,0 +1,68 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTPACKETREADER_H +#define BTPACKETREADER_H + +#include <qmutex.h> +#include <qptrlist.h> +#include <net/bufferedsocket.h> +#include "globals.h" + +namespace bt +{ + class Peer; + + struct IncomingPacket + { + Uint8* data; + Uint32 size; + Uint32 read; + + IncomingPacket(Uint32 size); + virtual ~IncomingPacket(); + }; + + /** + @author Joris Guisson + */ + class PacketReader : public net::SocketReader + { + Peer* peer; + bool error; + QPtrList<IncomingPacket> packet_queue; + QMutex mutex; + Uint8 len[4]; + int len_received; + public: + PacketReader(Peer* peer); + virtual ~PacketReader(); + + void update(); + bool ok() const {return !error;} + private: + Uint32 newPacket(Uint8* buf,Uint32 size); + Uint32 readPacket(Uint8* buf,Uint32 size); + virtual void onDataReady(Uint8* buf,Uint32 size); + + }; + +} + +#endif diff --git a/libktorrent/torrent/packetwriter.cpp b/libktorrent/torrent/packetwriter.cpp new file mode 100644 index 0000000..888d23d --- /dev/null +++ b/libktorrent/torrent/packetwriter.cpp @@ -0,0 +1,399 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +//#define LOG_PACKET + +#include <util/log.h> +#include <util/file.h> +#include <util/functions.h> +#include <net/socketmonitor.h> +#include <ktversion.h> +#include "packetwriter.h" +#include "peer.h" +#include "request.h" +#include "chunk.h" +#include <util/bitset.h> +#include "packet.h" +#include "uploadcap.h" +#include <util/log.h> +#include "globals.h" +#include "bencoder.h" + + + +namespace bt +{ + + + PacketWriter::PacketWriter(Peer* peer) : peer(peer),mutex(true) // this is a recursive mutex + { + uploaded = 0; + uploaded_non_data = 0; + curr_packet = 0; + ctrl_packets_sent = 0; + } + + + PacketWriter::~PacketWriter() + { + std::list<Packet*>::iterator i = data_packets.begin(); + while (i != data_packets.end()) + { + Packet* p = *i; + delete p; + i++; + } + + i = control_packets.begin(); + while (i != control_packets.end()) + { + Packet* p = *i; + delete p; + i++; + } + } + + void PacketWriter::queuePacket(Packet* p) + { + QMutexLocker locker(&mutex); + if (p->getType() == PIECE) + data_packets.push_back(p); + else + control_packets.push_back(p); + // tell upload thread we have data ready should it be sleeping + net::SocketMonitor::instance().signalPacketReady(); + } + + + + void PacketWriter::sendChoke() + { + if (peer->am_choked == true) + return; + + queuePacket(new Packet(CHOKE)); + peer->am_choked = true; + peer->stats.has_upload_slot = false; + } + + void PacketWriter::sendUnchoke() + { + if (peer->am_choked == false) + return; + + queuePacket(new Packet(UNCHOKE)); + peer->am_choked = false; + peer->stats.has_upload_slot = true; + } + + void PacketWriter::sendEvilUnchoke() + { + queuePacket(new Packet(UNCHOKE)); + peer->am_choked = true; + peer->stats.has_upload_slot = false; + } + + void PacketWriter::sendInterested() + { + if (peer->am_interested == true) + return; + + queuePacket(new Packet(INTERESTED)); + peer->am_interested = true; + } + + void PacketWriter::sendNotInterested() + { + if (peer->am_interested == false) + return; + + queuePacket(new Packet(NOT_INTERESTED)); + peer->am_interested = false; + } + + void PacketWriter::sendRequest(const Request & r) + { + queuePacket(new Packet(r,bt::REQUEST)); + } + + void PacketWriter::sendCancel(const Request & r) + { + queuePacket(new Packet(r,bt::CANCEL)); + } + + void PacketWriter::sendReject(const Request & r) + { + queuePacket(new Packet(r,bt::REJECT_REQUEST)); + } + + void PacketWriter::sendHave(Uint32 index) + { + queuePacket(new Packet(index,bt::HAVE)); + } + + void PacketWriter::sendPort(Uint16 port) + { + queuePacket(new Packet(port)); + } + + void PacketWriter::sendBitSet(const BitSet & bs) + { + queuePacket(new Packet(bs)); + } + + void PacketWriter::sendHaveAll() + { + queuePacket(new Packet(bt::HAVE_ALL)); + } + + void PacketWriter::sendHaveNone() + { + queuePacket(new Packet(bt::HAVE_NONE)); + } + + void PacketWriter::sendSuggestPiece(Uint32 index) + { + queuePacket(new Packet(index,bt::SUGGEST_PIECE)); + } + + void PacketWriter::sendAllowedFast(Uint32 index) + { + queuePacket(new Packet(index,bt::ALLOWED_FAST)); + } + + bool PacketWriter::sendChunk(Uint32 index,Uint32 begin,Uint32 len,Chunk * ch) + { +// Out() << "sendChunk " << index << " " << begin << " " << len << endl; + if (begin >= ch->getSize() || begin + len > ch->getSize()) + { + Out(SYS_CON|LOG_NOTICE) << "Warning : Illegal piece request" << endl; + Out(SYS_CON|LOG_NOTICE) << "\tChunk : index " << index << " size = " << ch->getSize() << endl; + Out(SYS_CON|LOG_NOTICE) << "\tPiece : begin = " << begin << " len = " << len << endl; + return false; + } + else if (!ch || ch->getData() == 0) + { + Out(SYS_CON|LOG_NOTICE) << "Warning : attempted to upload an invalid chunk" << endl; + return false; + } + else + { + /* Out(SYS_CON|LOG_DEBUG) << QString("Uploading %1 %2 %3 %4 %5") + .arg(index).arg(begin).arg(len).arg((Q_ULLONG)ch,0,16).arg((Q_ULLONG)ch->getData(),0,16) + << endl;; + */ + queuePacket(new Packet(index,begin,len,ch)); + return true; + } + } + + void PacketWriter::sendExtProtHandshake(Uint16 port,bool pex_on) + { + QByteArray arr; + BEncoder enc(new BEncoderBufferOutput(arr)); + enc.beginDict(); + enc.write("m"); + // supported messages + enc.beginDict(); + enc.write("ut_pex");enc.write((Uint32)(pex_on ? 1 : 0)); + enc.end(); + if (port > 0) + { + enc.write("p"); + enc.write((Uint32)port); + } + enc.write("v"); enc.write(QString("KTorrent %1").arg(kt::VERSION_STRING)); + enc.end(); + sendExtProtMsg(0,arr); + } + + void PacketWriter::sendExtProtMsg(Uint8 id,const QByteArray & data) + { + queuePacket(new Packet(id,data)); + } + + Packet* PacketWriter::selectPacket() + { + Packet* ret = 0; + // this function should ensure that between + // each data packet at least 3 control packets are sent + // so requests can get through + + if (ctrl_packets_sent < 3) + { + // try to send another control packet + if (control_packets.size() > 0) + ret = control_packets.front(); + else if (data_packets.size() > 0) + ret = data_packets.front(); + } + else + { + if (data_packets.size() > 0) + { + ctrl_packets_sent = 0; + ret = data_packets.front(); + } + else if (control_packets.size() > 0) + ret = control_packets.front(); + } + + return ret; + } + + Uint32 PacketWriter::onReadyToWrite(Uint8* data,Uint32 max_to_write) + { + QMutexLocker locker(&mutex); + + if (!curr_packet) + curr_packet = selectPacket(); + + Uint32 written = 0; + while (curr_packet && written < max_to_write) + { + Packet* p = curr_packet; + bool count_as_data = false; + Uint32 ret = p->putInOutputBuffer(data + written,max_to_write - written,count_as_data); + written += ret; + if (count_as_data) + uploaded += ret; + else + uploaded_non_data += ret; + + if (p->isSent()) + { + // packet sent, so remove it + if (p->getType() == PIECE) + { + // remove data packet + data_packets.pop_front(); + delete p; + // reset ctrl_packets_sent so the next packet should be a ctrl packet + ctrl_packets_sent = 0; + curr_packet = selectPacket(); + } + else + { + // remove control packet and select another one to send + control_packets.pop_front(); + delete p; + ctrl_packets_sent++; + curr_packet = selectPacket(); + } + } + else + { + // we can't send it fully, so break out of loop + break; + } + } + + return written; + } + + bool PacketWriter::hasBytesToWrite() const + { + return getNumPacketsToWrite() > 0; + } + + Uint32 PacketWriter::getUploadedDataBytes() const + { + QMutexLocker locker(&mutex); + Uint32 ret = uploaded; + uploaded = 0; + return ret; + } + + Uint32 PacketWriter::getUploadedNonDataBytes() const + { + QMutexLocker locker(&mutex); + Uint32 ret = uploaded_non_data; + uploaded_non_data = 0; + return ret; + } + + Uint32 PacketWriter::getNumPacketsToWrite() const + { + QMutexLocker locker(&mutex); + return data_packets.size() + control_packets.size(); + } + + Uint32 PacketWriter::getNumDataPacketsToWrite() const + { + QMutexLocker locker(&mutex); + return data_packets.size(); + } + + void PacketWriter::doNotSendPiece(const Request & req,bool reject) + { + QMutexLocker locker(&mutex); + std::list<Packet*>::iterator i = data_packets.begin(); + while (i != data_packets.end()) + { + Packet* p = *i; + if (p->isPiece(req) && !p->sending()) + { + // remove current item + if (curr_packet == p) + curr_packet = 0; + + i = data_packets.erase(i); + if (reject) + { + // queue a reject packet + sendReject(req); + } + delete p; + } + else + { + i++; + } + } + } + + void PacketWriter::clearPieces(bool reject) + { + QMutexLocker locker(&mutex); + + std::list<Packet*>::iterator i = data_packets.begin(); + while (i != data_packets.end()) + { + Packet* p = *i; + if (p->getType() == bt::PIECE && !p->sending()) + { + // remove current item + if (curr_packet == p) + curr_packet = 0; + + if (reject) + { + queuePacket(p->makeRejectOfPiece()); + } + + i = data_packets.erase(i); + delete p; + } + else + { + i++; + } + } + } +} diff --git a/libktorrent/torrent/packetwriter.h b/libktorrent/torrent/packetwriter.h new file mode 100644 index 0000000..9b77731 --- /dev/null +++ b/libktorrent/torrent/packetwriter.h @@ -0,0 +1,185 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTPACKETWRITER_H +#define BTPACKETWRITER_H + +#include <list> +#include <qmutex.h> +#include <net/bufferedsocket.h> +#include "globals.h" + +namespace bt +{ + class Peer; + class Request; + class Chunk; + class BitSet; + class Packet; + + /** + @author Joris Guisson + */ + class PacketWriter : public net::SocketWriter + { + Peer* peer; + std::list<Packet*> control_packets; + std::list<Packet*> data_packets; + Packet* curr_packet; + Uint32 ctrl_packets_sent; + mutable Uint32 uploaded; + mutable Uint32 uploaded_non_data; + mutable QMutex mutex; + public: + PacketWriter(Peer* peer); + virtual ~PacketWriter(); + + /** + * Send a choke packet. + */ + void sendChoke(); + + /** + * Send an unchoke packet. + */ + void sendUnchoke(); + + /** + * Sends an unchoke message but doesn't update the am_choked field so KT still thinks + * it is choked (and will not upload to it), this is to punish snubbers. + */ + void sendEvilUnchoke(); + + /** + * Send an interested packet. + */ + void sendInterested(); + + /** + * Send a not interested packet. + */ + void sendNotInterested(); + + /** + * Send a request for data. + * @param req The Request + */ + void sendRequest(const Request & r); + + /** + * Cancel a request. + * @param req The Request + */ + void sendCancel(const Request & r); + + + /** + * Send a reject for a request + * @param req The Request + */ + void sendReject(const Request & r); + + /** + * Send a have packet. + * @param index + */ + void sendHave(Uint32 index); + + /** + * Send an allowed fast packet + * @param index + */ + void sendAllowedFast(Uint32 index); + + /** + * Send a chunk of data. + * @param index Index of chunk + * @param begin Offset into chunk + * @param len Length of data + * @param ch The Chunk + * @return true If we satisfy the request, false otherwise + */ + bool sendChunk(Uint32 index,Uint32 begin,Uint32 len,Chunk * ch); + + /** + * Send a BitSet. The BitSet indicates which chunks we have. + * @param bs The BitSet + */ + void sendBitSet(const BitSet & bs); + + /** + * Send a port message + * @param port The port + */ + void sendPort(Uint16 port); + + /// Send a have all message + void sendHaveAll(); + + /// Send a have none message + void sendHaveNone(); + + /** + * Send a suggest piece packet + * @param index Index of the chunk + */ + void sendSuggestPiece(Uint32 index); + + /// Send the extension protocol handshake + void sendExtProtHandshake(Uint16 port,bool pex_on = true); + + /// Send an extended protocol message + void sendExtProtMsg(Uint8 id,const QByteArray & data); + + /// Get the number of packets which need to be written + Uint32 getNumPacketsToWrite() const; + + /// Get the number of data packets to write + Uint32 getNumDataPacketsToWrite() const; + + /// Get the number of data bytes uploaded + Uint32 getUploadedDataBytes() const; + + /// Get the number of bytes uploaded + Uint32 getUploadedNonDataBytes() const; + + /** + * Do not send a piece which matches this request. + * But only if we are not allready sending the piece. + * @param req The request + * @param reject Wether we can send a reject instead + */ + void doNotSendPiece(const Request & req,bool reject); + + /** + * Clear all pieces we are not in the progress of sending. + * @param reject Send a reject packet + */ + void clearPieces(bool reject); + + private: + void queuePacket(Packet* p); + Packet* selectPacket(); + virtual Uint32 onReadyToWrite(Uint8* data,Uint32 max_to_write); + virtual bool hasBytesToWrite() const; + }; + +} + +#endif diff --git a/libktorrent/torrent/peer.cpp b/libktorrent/torrent/peer.cpp new file mode 100644 index 0000000..7a5727b --- /dev/null +++ b/libktorrent/torrent/peer.cpp @@ -0,0 +1,593 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <math.h> +#include <util/log.h> +#include <util/functions.h> +#include <net/address.h> +#include <mse/streamsocket.h> + +#include "peer.h" +#include "chunk.h" +#include "piece.h" +#include "request.h" +#include "packetreader.h" +#include "packetwriter.h" +#include "peerdownloader.h" +#include "peeruploader.h" +#include "bdecoder.h" +#include "bnode.h" +#include "utpex.h" +#include "server.h" + +using namespace net; + +namespace bt +{ + + + + static Uint32 peer_id_counter = 1; + + + Peer::Peer(mse::StreamSocket* sock,const PeerID & peer_id, + Uint32 num_chunks,Uint32 chunk_size,Uint32 support,bool local) + : sock(sock),pieces(num_chunks),peer_id(peer_id) + { + id = peer_id_counter; + peer_id_counter++; + + ut_pex = 0; + preader = new PacketReader(this); + choked = am_choked = true; + interested = am_interested = false; + killed = false; + downloader = new PeerDownloader(this,chunk_size); + uploader = new PeerUploader(this); + + + pwriter = new PacketWriter(this); + time_choked = GetCurrentTime(); + time_unchoked = 0; + + connect_time = QTime::currentTime(); + //sock->attachPeer(this); + stats.client = peer_id.identifyClient(); + stats.ip_address = getIPAddresss(); + stats.choked = true; + stats.download_rate = 0; + stats.upload_rate = 0; + stats.perc_of_file = 0; + stats.snubbed = false; + stats.dht_support = support & DHT_SUPPORT; + stats.fast_extensions = support & FAST_EXT_SUPPORT; + stats.extension_protocol = support & EXT_PROT_SUPPORT; + stats.bytes_downloaded = stats.bytes_uploaded = 0; + stats.aca_score = 0.0; + stats.evil = false; + stats.has_upload_slot = false; + stats.num_up_requests = stats.num_down_requests = 0; + stats.encrypted = sock->encrypted(); + stats.local = local; + if (stats.ip_address == "0.0.0.0") + { + Out(SYS_CON|LOG_DEBUG) << "No more 0.0.0.0" << endl; + kill(); + } + else + { + sock->startMonitoring(preader,pwriter); + } + pex_allowed = stats.extension_protocol; + utorrent_pex_id = 0; + } + + + Peer::~Peer() + { + delete ut_pex; + delete uploader; + delete downloader; + delete sock; + delete pwriter; + delete preader; + } + + void Peer::closeConnection() + { + sock->close(); + } + + + void Peer::kill() + { + sock->close(); + killed = true; + } + + + + + void Peer::packetReady(const Uint8* packet,Uint32 len) + { + if (killed) return; + + if (len == 0) + return; + const Uint8* tmp_buf = packet; + //Out() << "Got packet : " << len << " type = " << type << endl; + Uint8 type = tmp_buf[0]; + switch (type) + { + case CHOKE: + if (len != 1) + { + Out() << "len err CHOKE" << endl; + kill(); + return; + } + + if (!choked) + { + time_choked = GetCurrentTime(); + } + choked = true; + downloader->choked(); + break; + case UNCHOKE: + if (len != 1) + { + Out() << "len err UNCHOKE" << endl; + kill(); + return; + } + + if (choked) + time_unchoked = GetCurrentTime(); + choked = false; + break; + case INTERESTED: + if (len != 1) + { + Out() << "len err INTERESTED" << endl; + kill(); + return; + } + if (!interested) + { + interested = true; + rerunChoker(); + } + break; + case NOT_INTERESTED: + if (len != 1) + { + Out() << "len err NOT_INTERESTED" << endl; + kill(); + return; + } + if (interested) + { + interested = false; + rerunChoker(); + } + break; + case HAVE: + if (len != 5) + { + Out() << "len err HAVE" << endl; + kill(); + } + else + { + Uint32 ch = ReadUint32(tmp_buf,1); + if (ch < pieces.getNumBits()) + { + haveChunk(this,ch); + pieces.set(ch,true); + } + else + { + Out(SYS_CON|LOG_NOTICE) << "Received invalid have value, kicking peer" << endl; + kill(); + } + } + break; + case BITFIELD: + if (len != 1 + pieces.getNumBytes()) + { + Out() << "len err BITFIELD" << endl; + kill(); + return; + } + + pieces = BitSet(tmp_buf+1,pieces.getNumBits()); + bitSetRecieved(pieces); + break; + case REQUEST: + if (len != 13) + { + Out() << "len err REQUEST" << endl; + kill(); + return; + } + + { + Request r( + ReadUint32(tmp_buf,1), + ReadUint32(tmp_buf,5), + ReadUint32(tmp_buf,9), + id); + + if (!am_choked) + uploader->addRequest(r); + else if (stats.fast_extensions) + pwriter->sendReject(r); + // Out() << "REQUEST " << r.getIndex() << " " << r.getOffset() << endl; + } + break; + case PIECE: + if (len < 9) + { + Out() << "len err PIECE" << endl; + kill(); + return; + } + + snub_timer.update(); + + { + stats.bytes_downloaded += (len - 9); + // turn on evil bit + if (stats.evil) + stats.evil = false; + Piece p(ReadUint32(tmp_buf,1), + ReadUint32(tmp_buf,5), + len - 9,id,tmp_buf+9); + piece(p); + } + break; + case CANCEL: + if (len != 13) + { + Out() << "len err CANCEL" << endl; + kill(); + return; + } + + { + Request r(ReadUint32(tmp_buf,1), + ReadUint32(tmp_buf,5), + ReadUint32(tmp_buf,9), + id); + uploader->removeRequest(r); + } + break; + case REJECT_REQUEST: + if (len != 13) + { + Out() << "len err REJECT_REQUEST" << endl; + kill(); + return; + } + + { + Request r(ReadUint32(tmp_buf,1), + ReadUint32(tmp_buf,5), + ReadUint32(tmp_buf,9), + id); + downloader->onRejected(r); + } + break; + case PORT: + if (len != 3) + { + Out() << "len err PORT" << endl; + kill(); + return; + } + + { + Uint16 port = ReadUint16(tmp_buf,1); + // Out() << "Got PORT packet : " << port << endl; + gotPortPacket(getIPAddresss(),port); + } + break; + case HAVE_ALL: + if (len != 1) + { + Out() << "len err HAVE_ALL" << endl; + kill(); + return; + } + pieces.setAll(true); + bitSetRecieved(pieces); + break; + case HAVE_NONE: + if (len != 1) + { + Out() << "len err HAVE_NONE" << endl; + kill(); + return; + } + pieces.setAll(false); + bitSetRecieved(pieces); + break; + case SUGGEST_PIECE: + // ignore suggestions for the moment + break; + case ALLOWED_FAST: + // we no longer support this, so do nothing + break; + case EXTENDED: + handleExtendedPacket(packet,len); + break; + } + } + + void Peer::handleExtendedPacket(const Uint8* packet,Uint32 size) + { + if (size <= 2 || packet[1] > 1) + return; + + if (packet[1] == 1) + { + if (ut_pex) + ut_pex->handlePexPacket(packet,size); + return; + } + + QByteArray tmp; + tmp.setRawData((const char*)packet,size); + BNode* node = 0; + try + { + BDecoder dec(tmp,false,2); + node = dec.decode(); + if (node && node->getType() == BNode::DICT) + { + BDictNode* dict = (BDictNode*)node; + + // handshake packet, so just check if the peer supports ut_pex + dict = dict->getDict("m"); + BValueNode* val = 0; + if (dict && (val = dict->getValue("ut_pex"))) + { + utorrent_pex_id = val->data().toInt(); + if (ut_pex) + { + if (utorrent_pex_id > 0) + ut_pex->changeID(utorrent_pex_id); + else + { + // id 0 means disabled + delete ut_pex; + ut_pex = 0; + } + } + else if (!ut_pex && utorrent_pex_id != 0 && pex_allowed) + { + // Don't create it when the id is 0 + ut_pex = new UTPex(this,utorrent_pex_id); + } + } + } + } + catch (...) + { + // just ignore invalid packets + Out(SYS_CON|LOG_DEBUG) << "Invalid extended packet" << endl; + } + delete node; + tmp.resetRawData((const char*)packet,size); + } + + Uint32 Peer::sendData(const Uint8* data,Uint32 len) + { + if (killed) return 0; + + Uint32 ret = sock->sendData(data,len); + if (!sock->ok()) + kill(); + + return ret; + } + + Uint32 Peer::readData(Uint8* buf,Uint32 len) + { + if (killed) return 0; + + Uint32 ret = sock->readData(buf,len); + + if (!sock->ok()) + kill(); + + return ret; + } + + Uint32 Peer::bytesAvailable() const + { + return sock->bytesAvailable(); + } + + void Peer::dataWritten(int ) + { + // Out() << "dataWritten " << bytes << endl; + + } + + Uint32 Peer::getUploadRate() const + { + if (sock) + return (Uint32)ceil(sock->getUploadRate()); + else + return 0; + } + + Uint32 Peer::getDownloadRate() const + { + if (sock) + return (Uint32)ceil(sock->getDownloadRate()); + else + return 0; + } + + bool Peer::readyToSend() const + { + return true; + } + + void Peer::update(PeerManager* pman) + { + if (killed) + return; + + if (!sock->ok() || !preader->ok()) + { + Out(SYS_CON|LOG_DEBUG) << "Connection closed" << endl; + kill(); + return; + } + + preader->update(); + + Uint32 data_bytes = pwriter->getUploadedDataBytes(); + + if (data_bytes > 0) + { + stats.bytes_uploaded += data_bytes; + uploader->addUploadedBytes(data_bytes); + } + + if (ut_pex && ut_pex->needsUpdate()) + ut_pex->update(pman); + } + + bool Peer::isSnubbed() const + { + // 4 minutes + return snub_timer.getElapsedSinceUpdate() >= 2*60*1000 && stats.num_down_requests > 0; + } + + bool Peer::isSeeder() const + { + return pieces.allOn(); + } + + QString Peer::getIPAddresss() const + { + if (sock) + return sock->getRemoteIPAddress(); + else + return QString::null; + } + + Uint16 Peer::getPort() const + { + if (!sock) + return 0; + else + return sock->getRemotePort(); + } + + net::Address Peer::getAddress() const + { + if (!sock) + return net::Address(); + else + return sock->getRemoteAddress(); + } + + Uint32 Peer::getTimeSinceLastPiece() const + { + return snub_timer.getElapsedSinceUpdate(); + } + + float Peer::percentAvailable() const + { + return (float)pieces.numOnBits() / (float)pieces.getNumBits() * 100.0; + } + + const kt::PeerInterface::Stats & Peer::getStats() const + { + stats.choked = this->isChoked(); + stats.download_rate = this->getDownloadRate(); + stats.upload_rate = this->getUploadRate(); + stats.perc_of_file = this->percentAvailable(); + stats.snubbed = this->isSnubbed(); + stats.num_up_requests = uploader->getNumRequests(); + stats.num_down_requests = downloader->getNumRequests(); + return stats; + } + + void Peer::setACAScore(double s) + { + stats.aca_score = s; + } + + void Peer::choke() + { + if (am_choked) + return; + + pwriter->sendChoke(); + uploader->clearAllRequests(); + } + + void Peer::emitPortPacket() + { + gotPortPacket(sock->getRemoteIPAddress(),sock->getRemotePort()); + } + + void Peer::emitPex(const QByteArray & data) + { + pex(data); + } + + void Peer::setPexEnabled(bool on) + { + if (!stats.extension_protocol) + return; + + // send extension protocol handshake + bt::Uint16 port = Globals::instance().getServer().getPortInUse(); + + if (ut_pex && !on) + { + delete ut_pex; + ut_pex = 0; + } + else if (!ut_pex && on && utorrent_pex_id > 0) + { + // if the other side has enabled it to, create a new UTPex object + ut_pex = new UTPex(this,utorrent_pex_id); + } + + pwriter->sendExtProtHandshake(port,on); + + pex_allowed = on; + } + + void Peer::setGroupIDs(Uint32 up_gid,Uint32 down_gid) + { + sock->setGroupIDs(up_gid,down_gid); + } +} + +#include "peer.moc" diff --git a/libktorrent/torrent/peer.h b/libktorrent/torrent/peer.h new file mode 100644 index 0000000..68dfecc --- /dev/null +++ b/libktorrent/torrent/peer.h @@ -0,0 +1,324 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTPEER_H +#define BTPEER_H + +#include <qobject.h> +#include <qdatetime.h> +#include <util/timer.h> +#include <interfaces/peerinterface.h> +#include <util/bitset.h> +#include "globals.h" +#include "peerid.h" + +namespace net +{ + class Address; +} + + +namespace mse +{ + class RC4Encryptor; + class StreamSocket; +} + +namespace bt +{ + class Chunk; + class Peer; + class Request; + class Piece; + class PacketReader; + class PacketWriter; + class PeerDownloader; + class PeerUploader; + class PeerManager; + class UTPex; + + + + + /** + * @author Joris Guisson + * @brief Manages the connection with a peer + * + * This class manages a connection with a peer in the P2P network. + * It provides functions for sending packets. Packets it receives + * get relayed to the outside world using a bunch of signals. + */ + class Peer : public QObject, public kt::PeerInterface + //,public Object + { + Q_OBJECT + public: + /** + * Constructor, set the socket. + * The socket is already opened. + * @param sock The socket + * @param peer_id The Peer's BitTorrent ID + * @param num_chunks The number of chunks in the file + * @param chunk_size Size of each chunk + * @param support Which extensions the peer supports + * @param local Wether or not it is a local peer + */ + Peer(mse::StreamSocket* sock, + const PeerID & peer_id, + Uint32 num_chunks, + Uint32 chunk_size, + Uint32 support, + bool local); + + virtual ~Peer(); + + /// Get the peer's unique ID. + Uint32 getID() const {return id;} + + /// Get the IP address of the Peer. + QString getIPAddresss() const; + + /// Get the port of the Peer + Uint16 getPort() const; + + /// Get the address of the peer + net::Address getAddress() const; + + /// See if the peer has been killed. + bool isKilled() const {return killed;} + + /// Get the PacketWriter + PacketWriter & getPacketWriter() {return *pwriter;} + + /// Is the Peer choked + bool isChoked() const {return choked;} + + /// Is the Peer interested + bool isInterested() const {return interested;} + + /// Are we interested in the Peer + bool areWeInterested() const {return am_interested;} + + /// Are we choked for the Peer + bool areWeChoked() const {return am_choked;} + + /// Are we being snubbed by the Peer + bool isSnubbed() const; + + /// Get the upload rate in bytes per sec + Uint32 getUploadRate() const; + + /// Get the download rate in bytes per sec + Uint32 getDownloadRate() const; + + /// Get the Peer's BitSet + const BitSet & getBitSet() const {return pieces;} + + /// Get the Peer's ID + const PeerID & getPeerID() const {return peer_id;} + + /// Update the up- and down- speed and handle incoming packets + void update(PeerManager* pman); + + /// Get the PeerDownloader. + PeerDownloader* getPeerDownloader() {return downloader;} + + /// Get the PeerUploader. + PeerUploader* getPeerUploader() {return uploader;} + + /** + * Send a chunk of data. + * @param data The data + * @param len The length + * @param proto Indicates wether the packed is data or a protocol message + * @return Number of bytes written + */ + Uint32 sendData(const Uint8* data,Uint32 len); + + /** + * Reads data from the peer. + * @param buf The buffer to store the data + * @param len The maximum number of bytes to read + * @return The number of bytes read + */ + Uint32 readData(Uint8* buf,Uint32 len); + + /// Get the number of bytes available to read. + Uint32 bytesAvailable() const; + + /** + * See if all previously written data, has been sent. + */ + bool readyToSend() const; + + + /** + * Close the peers connection. + */ + void closeConnection(); + + /** + * Kill the Peer. + */ + void kill(); + + /** + * Get the time when this Peer was choked. + */ + TimeStamp getChokeTime() const {return time_choked;} + + /** + * Get the time when this Peer was unchoked. + */ + TimeStamp getUnchokeTime() const {return time_unchoked;} + + /** + * See if the peer is a seeder. + */ + bool isSeeder() const; + + /// Get the time in milliseconds since the last time a piece was received. + Uint32 getTimeSinceLastPiece() const; + + /// Get the time the peer connection was established. + const QTime & getConnectTime() const {return connect_time;} + + /** + * Get the percentual amount of data available from peer. + */ + float percentAvailable() const; + + /// See if the peer supports DHT + bool isDHTSupported() const {return stats.dht_support;} + + /// Set the ACA score + void setACAScore(double s); + + /// Get the stats of the peer + virtual const Stats & getStats() const; + + /// Choke the peer + void choke(); + + /** + * Emit the port packet signal. + */ + void emitPortPacket(); + + /** + * Emit the pex signal + */ + void emitPex(const QByteArray & data); + + /// Disable or enable pex + void setPexEnabled(bool on); + + /** + * Set the peer's group IDs for traffic + * @param up_gid The upload gid + * @param down_gid The download gid + */ + void setGroupIDs(Uint32 up_gid,Uint32 down_gid); + + private slots: + void dataWritten(int bytes); + + signals: + /** + * The Peer has a Chunk. + * @param p The Peer + * @param index Index of Chunk + */ + void haveChunk(Peer* p,Uint32 index); + + /** + * The Peer sent a request. + * @param req The Request + */ + void request(const Request & req); + + /** + * The Peer sent a cancel. + * @param req The Request + */ + void canceled(const Request & req); + + /** + * The Peer sent a piece of a Chunk. + * @param p The Piece + */ + void piece(const Piece & p); + + /** + * Recieved a BitSet + * @param bs The BitSet + */ + void bitSetRecieved(const BitSet & bs); + + /** + * Emitted when the peer is unchoked and interested changes value. + */ + void rerunChoker(); + + /** + * Got a port packet from this peer. + * @param ip The IP + * @param port The port + */ + void gotPortPacket(const QString & ip,Uint16 port); + + /** + * A Peer Exchange has been received, the QByteArray contains the data. + */ + void pex(const QByteArray & data); + + private: + void packetReady(const Uint8* packet,Uint32 size); + void handleExtendedPacket(const Uint8* packet,Uint32 size); + + private: + mse::StreamSocket* sock; + bool choked; + bool interested; + bool am_choked; + bool am_interested; + bool killed; + TimeStamp time_choked; + TimeStamp time_unchoked; + Uint32 id; + BitSet pieces; + PeerID peer_id; + Timer snub_timer; + PacketReader* preader; + PacketWriter* pwriter; + PeerDownloader* downloader; + PeerUploader* uploader; + mutable kt::PeerInterface::Stats stats; + QTime connect_time; + UTPex* ut_pex; + bool pex_allowed; + Uint32 utorrent_pex_id; + + friend class PacketWriter; + friend class PacketReader; + friend class PeerDownloader; + }; +} + +#endif diff --git a/libktorrent/torrent/peerdownloader.cpp b/libktorrent/torrent/peerdownloader.cpp new file mode 100644 index 0000000..0c6cdd8 --- /dev/null +++ b/libktorrent/torrent/peerdownloader.cpp @@ -0,0 +1,311 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <math.h> +#include <util/functions.h> +#include <util/log.h> +#include "globals.h" +#include "peerdownloader.h" +#include "peer.h" +#include "piece.h" +#include "packetwriter.h" + + +namespace bt +{ + TimeStampedRequest::TimeStampedRequest() + { + time_stamp = bt::GetCurrentTime(); + } + + TimeStampedRequest::TimeStampedRequest(const Request & r) : req(r) + { + time_stamp = bt::GetCurrentTime(); + } + + TimeStampedRequest::TimeStampedRequest(const TimeStampedRequest & t) + : req(t.req),time_stamp(t.time_stamp) + { + } + + bool TimeStampedRequest::operator == (const Request & r) + { + return r == req; + } + + bool TimeStampedRequest::operator == (const TimeStampedRequest & r) + { + return r.req == req; + } + + TimeStampedRequest & TimeStampedRequest::operator = (const Request & r) + { + time_stamp = bt::GetCurrentTime(); + req = r; + return *this; + } + + TimeStampedRequest & TimeStampedRequest::operator = (const TimeStampedRequest & r) + { + time_stamp = r.time_stamp; + req = r.req; + return *this; + } + + PeerDownloader::PeerDownloader(Peer* peer,Uint32 chunk_size) : peer(peer),grabbed(0),chunk_size(chunk_size / MAX_PIECE_LEN) + { + connect(peer,SIGNAL(piece(const Piece& )),this,SLOT(piece(const Piece& ))); + connect(peer,SIGNAL(destroyed()),this,SLOT(peerDestroyed())); + nearly_done = false; + max_wait_queue_size = 25; + } + + + PeerDownloader::~PeerDownloader() + { + } +#if 0 + void PeerDownloader::retransmitRequests() + { + for (QValueList<Request>::iterator i = reqs.begin();i != reqs.end();i++) + peer->getPacketWriter().sendRequest(*i); + + } +#endif + + bool PeerDownloader::canAddRequest() const + { + return wait_queue.count() < max_wait_queue_size; + } + + Uint32 PeerDownloader::getNumRequests() const + { + return reqs.count() /*+ wait_queue.count() */; + } + + int PeerDownloader::grab() + { + grabbed++; + return grabbed; + } + + void PeerDownloader::release() + { + grabbed--; + if (grabbed < 0) + grabbed = 0; + } + + void PeerDownloader::download(const Request & req) + { + if (!peer) + return; + + wait_queue.append(req); + update(); + } + + void PeerDownloader::cancel(const Request & req) + { + if (!peer) + return; + + if (wait_queue.contains(req)) + { + wait_queue.remove(req); + } + else if (reqs.contains(req)) + { + reqs.remove(req); + peer->getPacketWriter().sendCancel(req); + } + } + + void PeerDownloader::onRejected(const Request & req) + { + if (!peer) + return; + +// Out(SYS_CON|LOG_DEBUG) << "Rejected : " << req.getIndex() << " " +// << req.getOffset() << " " << req.getLength() << endl; + if (reqs.contains(req)) + { + reqs.remove(req); + rejected(req); + } + } + + void PeerDownloader::cancelAll() + { + if (peer) + { + QValueList<TimeStampedRequest>::iterator i = reqs.begin(); + while (i != reqs.end()) + { + TimeStampedRequest & tr = *i; + peer->getPacketWriter().sendCancel(tr.req); + i++; + } + } + + wait_queue.clear(); + reqs.clear(); + } + + void PeerDownloader::piece(const Piece & p) + { + Request r(p); + if (wait_queue.contains(r)) + wait_queue.remove(r); + else if (reqs.contains(r)) + reqs.remove(r); + + downloaded(p); + update(); + } + + void PeerDownloader::peerDestroyed() + { + peer = 0; + } + + bool PeerDownloader::isChoked() const + { + if (peer) + return peer->isChoked(); + else + return true; + } + + bool PeerDownloader::hasChunk(Uint32 idx) const + { + if (peer) + return peer->getBitSet().get(idx); + else + return false; + } + + Uint32 PeerDownloader::getDownloadRate() const + { + if (peer) + return peer->getDownloadRate(); + else + return 0; + } + + void PeerDownloader::checkTimeouts() + { + TimeStamp now = bt::GetCurrentTime(); + // we use a 60 second interval + const Uint32 MAX_INTERVAL = 60 * 1000; + QValueList<TimeStampedRequest>::iterator i = reqs.begin(); + while (i != reqs.end()) + { + TimeStampedRequest & tr = *i; + if (now - tr.time_stamp > MAX_INTERVAL) + { + // cancel it + TimeStampedRequest r = tr; + peer->getPacketWriter().sendCancel(r.req); + + // retransmit it + peer->getPacketWriter().sendRequest(r.req); + r.time_stamp = now; + + // reappend it at the end of the list + i = reqs.erase(i); + reqs.append(r); + Out(SYS_CON|LOG_DEBUG) << "Retransmitting " << r.req.getIndex() << ":" << r.req.getOffset() << endl; + } + else + { + // new requests get appended so once we have found one + // which hasn't timed out all the following will also not have timed out + break; + } + } + } + + + Uint32 PeerDownloader::getMaxChunkDownloads() const + { + // get the download rate in KB/sec + Uint32 rate_kbs = peer->getDownloadRate(); + rate_kbs = rate_kbs / 1024; + Uint32 num_extra = rate_kbs / 50; + + if (chunk_size >= 16) + { + return 1 + 16 * num_extra / chunk_size; + } + else + { + return 1 + (16 / chunk_size) * num_extra; + } + } + + void PeerDownloader::choked() + { + // choke doesn't mean reject when fast extensions are enabled + if (peer->getStats().fast_extensions) + return; + + QValueList<TimeStampedRequest>::iterator i = reqs.begin(); + while (i != reqs.end()) + { + TimeStampedRequest & tr = *i; + rejected(tr.req); + i++; + } + reqs.clear(); + + QValueList<Request>::iterator j = wait_queue.begin(); + while (j != wait_queue.end()) + { + Request & req = *j; + rejected(req); + j++; + } + wait_queue.clear(); + } + + void PeerDownloader::update() + { + // modify the interval if necessary + double pieces_per_sec = (double)peer->getDownloadRate() / MAX_PIECE_LEN; + + Uint32 max_reqs = 1 + (Uint32)ceil(10*pieces_per_sec); + + while (wait_queue.count() > 0 && reqs.count() < max_reqs) + { + // get a request from the wait queue and send that + Request req = wait_queue.front(); + wait_queue.pop_front(); + TimeStampedRequest r = TimeStampedRequest(req); + reqs.append(r); + peer->getPacketWriter().sendRequest(req); + } + + max_wait_queue_size = 2*max_reqs; + if (max_wait_queue_size < 10) + max_wait_queue_size = 10; + } +} + +#include "peerdownloader.moc" diff --git a/libktorrent/torrent/peerdownloader.h b/libktorrent/torrent/peerdownloader.h new file mode 100644 index 0000000..4eb37d2 --- /dev/null +++ b/libktorrent/torrent/peerdownloader.h @@ -0,0 +1,231 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTPEERDOWNLOADER_H +#define BTPEERDOWNLOADER_H + +#include <set> +#include <qvaluelist.h> +#include <qobject.h> +#include "globals.h" +#include "request.h" + +namespace bt +{ + class Peer; + class Request; + class Piece; + + typedef std::set<Uint32> AllowedFastSet; + /** + * Request with a timestamp. + */ + struct TimeStampedRequest + { + Request req; + TimeStamp time_stamp; + + TimeStampedRequest(); + + /** + * Constructor, set the request and calculate the timestamp. + * @param r The Request + */ + TimeStampedRequest(const Request & r); + + /** + * Copy constructor, copy the request and the timestamp + * @param r The Request + */ + TimeStampedRequest(const TimeStampedRequest & t); + + /** + * Equality operator, compares requests only. + * @param r The Request + * @return true if equal + */ + bool operator == (const Request & r); + + /** + * Equality operator, compares requests only. + * @param r The Request + * @return true if equal + */ + bool operator == (const TimeStampedRequest & r); + + /** + * Assignment operator. + * @param r The Request to copy + * @return *this + */ + TimeStampedRequest & operator = (const Request & r); + + /** + * Assignment operator. + * @param r The TimeStampedRequest to copy + * @return *this + */ + TimeStampedRequest & operator = (const TimeStampedRequest & r); + }; + + + /** + * @author Joris Guisson + * @brief Class which downloads pieces from a Peer + * + * This class downloads Piece's from a Peer. + */ + class PeerDownloader : public QObject + { + Q_OBJECT + public: + /** + * Constructor, set the Peer + * @param peer The Peer + * @param chunk_size Size of a chunk in bytes + */ + PeerDownloader(Peer* peer,Uint32 chunk_size); + virtual ~PeerDownloader(); + + /// See if we can add a request to the wait_queue + bool canAddRequest() const; + + /// Get the number of active requests + Uint32 getNumRequests() const; + + /// Is the Peer choked. + bool isChoked() const; + + /// Is NULL (is the Peer set) + bool isNull() const {return peer == 0;} + + /** + * See if the Peer has a Chunk + * @param idx The Chunk's index + */ + bool hasChunk(Uint32 idx) const; + + /// See if this PeerDownloader has nearly finished a chunk + bool isNearlyDone() const {return grabbed == 1 && nearly_done;} + + /// Set the nearly done status of the PeerDownloader + void setNearlyDone(bool nd) {nearly_done = nd;} + + /** + * Grab the Peer, indicates how many ChunkDownload's + * are using this PeerDownloader. + * @return The number of times this PeerDownloader was grabbed + */ + int grab(); + + /** + * When a ChunkDownload is ready with this PeerDownloader, + * it will release it, so that others can use it. + */ + void release(); + + /// Get the number of times this PeerDownloader was grabbed. + int getNumGrabbed() const {return grabbed;} + + /// Get the Peer + const Peer* getPeer() const {return peer;} + + /// Get the current download rate + Uint32 getDownloadRate() const; + + /** + * Check for timed out requests. + */ + void checkTimeouts(); + + /// Get the maximum number of chunk downloads + Uint32 getMaxChunkDownloads() const; + + /** + * The peer has been choked, all pending requests are rejected. + * (except for allowed fast ones) + */ + void choked(); + + public slots: + /** + * Send a Request. Note that the DownloadCap + * may not allow this. (In which case it will + * be stored temporarely in the unsent_reqs list) + * @param req The Request + */ + void download(const Request & req); + + /** + * Cancel a Request. + * @param req The Request + */ + void cancel(const Request & req); + + /** + * Cancel all Requests + */ + void cancelAll(); + + /** + * Handles a rejected request. + * @param req + */ + void onRejected(const Request & req); + + private slots: + void piece(const Piece & p); + void peerDestroyed(); + void update(); + + signals: + /** + * Emited when a Piece has been downloaded. + * @param p The Piece + */ + void downloaded(const Piece & p); + + /** + * Emitted when a request takes longer then 60 seconds to download. + * The sender of the request will have to request it again. This does not apply for + * unsent requests. Their timestamps will be updated when they get transmitted. + * @param r The request + */ + void timedout(const Request & r); + + /** + * A request was rejected. + * @param req The Request + */ + void rejected(const Request & req); + + + private: + Peer* peer; + QValueList<TimeStampedRequest> reqs; + QValueList<Request> wait_queue; + Uint32 max_wait_queue_size; + int grabbed; + Uint32 chunk_size; + bool nearly_done; + }; + +} + +#endif diff --git a/libktorrent/torrent/peerid.cpp b/libktorrent/torrent/peerid.cpp new file mode 100644 index 0000000..5f314b3 --- /dev/null +++ b/libktorrent/torrent/peerid.cpp @@ -0,0 +1,253 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <time.h> +#include <stdlib.h> +#include <qmap.h> +#include <klocale.h> +#include "peerid.h" +#include "ktversion.h" + +namespace bt +{ + char RandomLetterOrNumber() + { + int i = rand() % 62; + if (i < 26) + return 'a' + i; + else if (i < 52) + return 'A' + (i - 26); + else + return '0' + (i - 52); + } + + + PeerID::PeerID() + { + srand(time(0)); + memcpy(id,kt::PEER_ID,8); + for (int i = 8;i < 20;i++) + id[i] = RandomLetterOrNumber(); + client_name = identifyClient(); + } + + PeerID::PeerID(const char* pid) + { + if (pid) + memcpy(id,pid,20); + else + memset(id,0,20); + client_name = identifyClient(); + } + + PeerID::PeerID(const PeerID & pid) + { + memcpy(id,pid.id,20); + client_name = pid.client_name; + } + + PeerID::~PeerID() + {} + + + + PeerID & PeerID::operator = (const PeerID & pid) + { + memcpy(id,pid.id,20); + client_name = pid.client_name; + return *this; + } + + bool operator == (const PeerID & a,const PeerID & b) + { + for (int i = 0;i < 20;i++) + if (a.id[i] != b.id[i]) + return false; + + return true; + } + + bool operator != (const PeerID & a,const PeerID & b) + { + return ! operator == (a,b); + } + + bool operator < (const PeerID & a,const PeerID & b) + { + for (int i = 0;i < 20;i++) + if (a.id[i] < b.id[i]) + return true; + + return false; + } + + QString PeerID::toString() const + { + QString r; + for (int i = 0;i < 20;i++) + r += id[i] == 0 ? ' ' : id[i]; + return r; + } + + QString PeerID::identifyClient() const + { + if (!client_name.isNull()) + return client_name; + + QString peer_id = toString(); + // we only need to create this map once + // so make it static + static QMap<QString, QString> Map; + static bool first = true; + + if (first) + { + // Keep things a bit alphabetic to make it easier add new ones + //AZUREUS STYLE + Map["AG"] = "Ares"; + Map["A~"] = "Ares"; + Map["AV"] = "Avicora"; + Map["AX"] = "BitPump"; + Map["AR"] = "Arctic"; + Map["AZ"] = "Azureus"; + Map["BB"] = "BitBuddy"; + Map["BC"] = "BitComet"; + Map["BF"] = "Bitflu"; + Map["BG"] = "BTGetit"; + Map["BM"] = "BitMagnet"; + Map["BO"] = "BitsOnWheels"; + Map["BR"] = "BitRocket"; + Map["BS"] = "BTSlave"; + Map["BX"] = "BitTorrent X"; + Map["CD"] = "Enhanced CTorrent"; + Map["CT"] = "CTorrent"; + Map["DE"] = "DelugeTorrent"; + Map["DP"] = "Propagate Data Client"; + Map["EB"] = "EBit"; + Map["ES"] = "electric sheep"; + Map["FT"] = "FoxTorrent"; + Map["GS"] = "GSTorrent"; + Map["G3"] = "G3 Torrent"; + Map["HL"] = "Halite"; + Map["HN"] = "Hydranode"; + Map["KG"] = "KGet"; + Map["KT"] = "KTorrent"; // lets not forget our own client + Map["LH"] = "LH-ABC"; + Map["lt"] = "libTorrent"; + Map["LT"] = "libtorrent"; + Map["LP"] = "Lphant"; + Map["LW"] = "LimeWire"; + Map["ML"] = "MLDonkey"; + Map["MO"] = "MonoTorrent"; + Map["MP"] = "MooPolice"; + Map["MT"] = "MoonLight"; + Map["PD"] = "Pando"; + Map["qB"] = "qBittorrent"; + Map["QD"] = "QQDownload"; + Map["QT"] = "Qt 4 Torrent example"; + Map["RS"] = "Rufus"; + Map["RT"] = "Retriever"; + Map["S~"] = "Shareaza alpha/beta"; + Map["SB"] = "Swiftbit"; + Map["SS"] = "SwarmScope"; + Map["ST"] = "SymTorrent"; + Map["st"] = "sharktorrent"; + Map["SZ"] = "Shareaza"; + Map["TN"] = "Torrent .NET"; + Map["TR"] = "Transmission"; + Map["TS"] = "Torrent Storm"; + Map["TT"] = "TuoTu"; + Map["UL"] = "uLeecher!"; + Map["UT"] = QString("%1Torrent").arg(QChar(0x00B5)); // µTorrent, 0x00B5 is unicode for µ + Map["WT"] = "BitLet"; + Map["WY"] = "FireTorrent"; + Map["XL"] = "Xunlei"; + Map["XT"] = "Xan Torrent"; + Map["XX"] = "Xtorrent"; + Map["ZT"] = "Zip Torrent"; + + //SHADOWS STYLE + Map["A"] = "ABC"; + Map["O"] = "Osprey Permaseed"; + Map["Q"] = "BTQueue"; + Map["R"] = "Tribler"; + Map["S"] = "Shadow's"; + Map["T"] = "BitTornado"; + Map["U"] = "UPnP NAT BitTorrent"; + //OTHER + Map["Plus"] = "Plus! II"; + Map["OP"] = "Opera"; + Map["BOW"] = "Bits on Wheels"; + Map["M"] = "BitTorrent"; + Map["exbc"] = "BitComet"; + Map["Mbrst"] = "Burst!"; + first = false; + } + + QString name = i18n("Unknown client"); + if (peer_id.at(0) == '-' && + peer_id.at(1).isLetter() && + peer_id.at(2).isLetter() ) //AZ style + { + QString ID(peer_id.mid(1,2)); + if (Map.contains(ID)) + name = Map[ID] + " " + peer_id.at(3) + "." + peer_id.at(4) + "." + + peer_id.at(5) + "." + peer_id.at(6); + } + else if (peer_id.at(0).isLetter() && + peer_id.at(1).isDigit() && + peer_id.at(2).isDigit() ) //Shadow's style + { + QString ID = QString(peer_id.at(0)); + if (Map.contains(ID)) + name = Map[ID] + " " + peer_id.at(1) + "." + + peer_id.at(2) + "." + peer_id.at(3); + } + else if (peer_id.at(0) == 'M' && peer_id.at(2) == '-' && (peer_id.at(4) == '-' || peer_id.at(5) == '-')) + { + name = Map["M"] + " " + peer_id.at(1) + "." + peer_id.at(3); + if(peer_id.at(4) == '-') + name += "." + peer_id.at(5); + else + name += peer_id.at(4) + "." + peer_id.at(6); + } + else if (peer_id.startsWith("OP")) + { + name = Map["OP"]; + } + else if ( peer_id.startsWith("exbc") ) + { + name = Map["exbc"]; + } + else if ( peer_id.mid(1,3) == "BOW") + { + name = Map["BOW"]; + } + else if ( peer_id.startsWith("Plus")) + { + name = Map["Plus"]; + } + else if ( peer_id.startsWith("Mbrst")) + { + name = Map["Mbrst"] + " " + peer_id.at(5) + "." + peer_id.at(7); + } + + return name; + } +} diff --git a/libktorrent/torrent/peerid.h b/libktorrent/torrent/peerid.h new file mode 100644 index 0000000..90ba439 --- /dev/null +++ b/libktorrent/torrent/peerid.h @@ -0,0 +1,61 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTPEERID_H +#define BTPEERID_H + +#include <qstring.h> + +namespace bt +{ + + /** + @author Joris Guisson + */ + class PeerID + { + char id[20]; + QString client_name; + public: + PeerID(); + PeerID(const char* pid); + PeerID(const PeerID & pid); + virtual ~PeerID(); + + PeerID & operator = (const PeerID & pid); + + const char* data() const {return id;} + + QString toString() const; + + /** + * Interprets the PeerID to figure out which client it is. + * @author Ivan + Joris + * @return The name of the client + */ + QString identifyClient() const; + + friend bool operator == (const PeerID & a,const PeerID & b); + friend bool operator != (const PeerID & a,const PeerID & b); + friend bool operator < (const PeerID & a,const PeerID & b); + }; + +} + +#endif diff --git a/libktorrent/torrent/peermanager.cpp b/libktorrent/torrent/peermanager.cpp new file mode 100644 index 0000000..d3744fc --- /dev/null +++ b/libktorrent/torrent/peermanager.cpp @@ -0,0 +1,607 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <util/log.h> +#include <util/file.h> +#include <util/error.h> +#include <net/address.h> +#include "peermanager.h" +#include "peer.h" +#include "bnode.h" +#include "globals.h" +#include "server.h" +#include "authenticate.h" +#include "torrent.h" +#include "uploader.h" +#include "downloader.h" +#include <util/functions.h> +#include <qhostaddress.h> +#include <mse/streamsocket.h> +#include <mse/encryptedauthenticate.h> +#include <klocale.h> +#include "ipblocklist.h" +#include "chunkcounter.h" +#include "authenticationmonitor.h" +#include <qdatetime.h> + +using namespace kt; + +namespace bt +{ + Uint32 PeerManager::max_connections = 0; + Uint32 PeerManager::max_total_connections = 0; + Uint32 PeerManager::total_connections = 0; + + PeerManager::PeerManager(Torrent & tor) + : tor(tor),available_chunks(tor.getNumChunks()) + { + killed.setAutoDelete(true); + started = false; + + cnt = new ChunkCounter(tor.getNumChunks()); + num_pending = 0; + pex_on = !tor.isPrivate(); + } + + + PeerManager::~PeerManager() + { + delete cnt; + Globals::instance().getServer().removePeerManager(this); + + if (peer_list.count() <= total_connections) + total_connections -= peer_list.count(); + else + total_connections = 0; + + peer_list.setAutoDelete(true); + peer_list.clear(); + } + + void PeerManager::update() + { + if (!started) + return; + + // update the speed of each peer, + // and get ridd of some killed peers + QPtrList<Peer>::iterator i = peer_list.begin(); + while (i != peer_list.end()) + { + Peer* p = *i; + if (p->isKilled()) + { + cnt->decBitSet(p->getBitSet()); + updateAvailableChunks(); + i = peer_list.erase(i); + killed.append(p); + peer_map.erase(p->getID()); + if (total_connections > 0) + total_connections--; + peerKilled(p); + } + else + { + p->update(this); + i++; + } + } + + // connect to some new peers + connectToPeers(); + } + + void PeerManager::killChokedPeers(Uint32 older_then) + { + Out() << "Getting rid of peers which have been choked for a long time" << endl; + TimeStamp now = bt::GetCurrentTime(); + QPtrList<Peer>::iterator i = peer_list.begin(); + Uint32 num_killed = 0; + while (i != peer_list.end() && num_killed < 20) + { + Peer* p = *i; + if (p->isChoked() && (now - p->getChokeTime()) > older_then) + { + p->kill(); + num_killed++; + } + + i++; + } + } + + void PeerManager::setMaxConnections(Uint32 max) + { + max_connections = max; + } + + void PeerManager::setMaxTotalConnections(Uint32 max) + { + Uint32 sys_max = bt::MaxOpenFiles() - 50; // leave about 50 free for regular files + max_total_connections = max; + if (max == 0 || max_total_connections > sys_max) + max_total_connections = sys_max; + } + + void PeerManager::addPotentialPeer(const PotentialPeer & pp) + { + if (potential_peers.size() > 150) + return; + + // avoid duplicates in the potential_peers map + std::pair<PPItr,PPItr> r = potential_peers.equal_range(pp.ip); + for (PPItr i = r.first;i != r.second;i++) + { + if (i->second.port == pp.port) // port and IP are the same so return + return; + } + + potential_peers.insert(std::make_pair(pp.ip,pp)); + } + + void PeerManager::killSeeders() + { + QPtrList<Peer>::iterator i = peer_list.begin(); + while (i != peer_list.end()) + { + Peer* p = *i; + if ( p->isSeeder() ) + p->kill(); + i++; + } + } + + void PeerManager::killUninterested() + { + QPtrList<Peer>::iterator i = peer_list.begin(); + while (i != peer_list.end()) + { + Peer* p = *i; + if ( !p->isInterested() && (p->getConnectTime().secsTo(QTime::currentTime()) > 30) ) + p->kill(); + i++; + } + } + + void PeerManager::onHave(Peer*,Uint32 index) + { + available_chunks.set(index,true); + cnt->inc(index); + } + + void PeerManager::onBitSetRecieved(const BitSet & bs) + { + for (Uint32 i = 0;i < bs.getNumBits();i++) + { + if (bs.get(i)) + { + available_chunks.set(i,true); + cnt->inc(i); + } + } + } + + + void PeerManager::newConnection(mse::StreamSocket* sock,const PeerID & peer_id,Uint32 support) + { + Uint32 total = peer_list.count() + num_pending; + bool local_not_ok = (max_connections > 0 && total >= max_connections); + bool global_not_ok = (max_total_connections > 0 && total_connections >= max_total_connections); + + if (!started || local_not_ok || global_not_ok) + { + // get rid of bad peer and replace it by another one + if (!killBadPeer()) + { + // we failed to find a bad peer, so just delete this one + delete sock; + return; + } + } + + createPeer(sock,peer_id,support,false); + } + + void PeerManager::peerAuthenticated(Authenticate* auth,bool ok) + { + if (!started) + return; + + if (total_connections > 0) + total_connections--; + + num_pending--; + if (!ok) + { + mse::EncryptedAuthenticate* a = dynamic_cast<mse::EncryptedAuthenticate*>(auth); + if (a && Globals::instance().getServer().unencryptedConnectionsAllowed()) + { + // if possible try unencrypted + QString ip = a->getIP(); + Uint16 port = a->getPort(); + Authenticate* st = new Authenticate(ip,port,tor.getInfoHash(),tor.getPeerID(),this); + if (auth->isLocal()) + st->setLocal(true); + + connect(this,SIGNAL(stopped()),st,SLOT(onPeerManagerDestroyed())); + AuthenticationMonitor::instance().add(st); + num_pending++; + total_connections++; + } + return; + } + + if (connectedTo(auth->getPeerID())) + { + return; + } + + createPeer(auth->takeSocket(),auth->getPeerID(),auth->supportedExtensions(),auth->isLocal()); + } + + void PeerManager::createPeer(mse::StreamSocket* sock,const PeerID & peer_id,Uint32 support,bool local) + { + Peer* peer = new Peer(sock,peer_id,tor.getNumChunks(),tor.getChunkSize(),support,local); + + connect(peer,SIGNAL(haveChunk(Peer*, Uint32 )),this,SLOT(onHave(Peer*, Uint32 ))); + connect(peer,SIGNAL(bitSetRecieved(const BitSet& )), + this,SLOT(onBitSetRecieved(const BitSet& ))); + connect(peer,SIGNAL(rerunChoker()),this,SLOT(onRerunChoker())); + connect(peer,SIGNAL(pex( const QByteArray& )),this,SLOT(pex( const QByteArray& ))); + + peer_list.append(peer); + peer_map.insert(peer->getID(),peer); + total_connections++; + newPeer(peer); + peer->setPexEnabled(pex_on); + } + + bool PeerManager::connectedTo(const PeerID & peer_id) + { + if (!started) + return false; + + for (Uint32 j = 0;j < peer_list.count();j++) + { + Peer* p = peer_list.at(j); + if (p->getPeerID() == peer_id) + { + return true; + } + } + return false; + } + + bool PeerManager::connectedTo(const QString & ip,Uint16 port) const + { + PtrMap<Uint32,Peer>::const_iterator i = peer_map.begin(); + while (i != peer_map.end()) + { + const Peer* p = i->second; + if (p->getPort() == port && p->getStats().ip_address == ip) + return true; + i++; + } + return false; + } + + void PeerManager::connectToPeers() + { + if (potential_peers.size() == 0) + return; + + if (peer_list.count() + num_pending >= max_connections && max_connections > 0) + return; + + if (total_connections >= max_total_connections && max_total_connections > 0) + return; + + if (num_pending > MAX_SIMULTANIOUS_AUTHS) + return; + + if (!mse::StreamSocket::canInitiateNewConnection()) + return; // to many sockets in SYN_SENT state + + Uint32 num = 0; + if (max_connections > 0) + { + Uint32 available = max_connections - (peer_list.count() + num_pending); + num = available >= potential_peers.size() ? + potential_peers.size() : available; + } + else + { + num = potential_peers.size(); + } + + if (num + total_connections >= max_total_connections && max_total_connections > 0) + num = max_total_connections - total_connections; + + for (Uint32 i = 0;i < num;i++) + { + if (num_pending > MAX_SIMULTANIOUS_AUTHS) + return; + + PPItr itr = potential_peers.begin(); + + IPBlocklist& ipfilter = IPBlocklist::instance(); + + if (!ipfilter.isBlocked(itr->first) && !connectedTo(itr->first,itr->second.port)) + { + // Out() << "EncryptedAuthenticate : " << pp.ip << ":" << pp.port << endl; + Authenticate* auth = 0; + const PotentialPeer & pp = itr->second; + + if (Globals::instance().getServer().isEncryptionEnabled()) + auth = new mse::EncryptedAuthenticate(pp.ip,pp.port,tor.getInfoHash(),tor.getPeerID(),this); + else + auth = new Authenticate(pp.ip,pp.port,tor.getInfoHash(),tor.getPeerID(),this); + + if (pp.local) + auth->setLocal(true); + + connect(this,SIGNAL(stopped()),auth,SLOT(onPeerManagerDestroyed())); + + AuthenticationMonitor::instance().add(auth); + num_pending++; + total_connections++; + } + potential_peers.erase(itr); + } + } + + + + Uint32 PeerManager::clearDeadPeers() + { + Uint32 num = killed.count(); + killed.clear(); + return num; + } + + void PeerManager::closeAllConnections() + { + killed.clear(); + + if (peer_list.count() <= total_connections) + total_connections -= peer_list.count(); + else + total_connections = 0; + + peer_map.clear(); + peer_list.setAutoDelete(true); + peer_list.clear(); + peer_list.setAutoDelete(false); + } + + // pick a random magic number + const Uint32 PEER_LIST_HDR_MAGIC = 0xEF12AB34; + + struct PeerListHeader + { + Uint32 magic; + Uint32 num_peers; + Uint32 ip_version; // 4 or 6, 6 is for future purposes only (when we support IPv6) + }; + + struct PeerListEntry + { + Uint32 ip; + Uint16 port; + }; + + void PeerManager::savePeerList(const QString & file) + { + bt::File fptr; + if (!fptr.open(file,"wb")) + return; + + try + { + PeerListHeader hdr; + hdr.magic = PEER_LIST_HDR_MAGIC; + // we will save both the active and potential peers + hdr.num_peers = peer_list.count() + potential_peers.size(); + hdr.ip_version = 4; + + fptr.write(&hdr,sizeof(PeerListHeader)); + + Out(SYS_GEN|LOG_DEBUG) << "Saving list of peers to " << file << endl; + // first the active peers + for (QPtrList<Peer>::iterator itr = peer_list.begin(); itr != peer_list.end();itr++) + { + Peer* p = *itr; + PeerListEntry e; + net::Address addr = p->getAddress(); + e.ip = addr.ip(); + e.port = addr.port(); + fptr.write(&e,sizeof(PeerListEntry)); + } + + // now the potential_peers + PPItr i = potential_peers.begin(); + while (i != potential_peers.end()) + { + net::Address addr(i->first,i->second.port); + PeerListEntry e; + e.ip = addr.ip(); + e.port = addr.port(); + fptr.write(&e,sizeof(PeerListEntry)); + i++; + } + } + catch (bt::Error & err) + { + Out(SYS_GEN|LOG_DEBUG) << "Error happened during saving of peer list : " << err.toString() << endl; + } + } + + void PeerManager::loadPeerList(const QString & file) + { + bt::File fptr; + if (!fptr.open(file,"rb")) + return; + + try + { + PeerListHeader hdr; + fptr.read(&hdr,sizeof(PeerListHeader)); + if (hdr.magic != PEER_LIST_HDR_MAGIC || hdr.ip_version != 4) + throw Error("Peer list file corrupted"); + + Out(SYS_GEN|LOG_DEBUG) << "Loading list of peers from " << file << " (num_peers = " << hdr.num_peers << ")" << endl; + + for (Uint32 i = 0;i < hdr.num_peers && !fptr.eof();i++) + { + PeerListEntry e; + fptr.read(&e,sizeof(PeerListEntry)); + PotentialPeer pp; + + // convert IP address to string + pp.ip = QString("%1.%2.%3.%4") + .arg((e.ip & 0xFF000000) >> 24) + .arg((e.ip & 0x00FF0000) >> 16) + .arg((e.ip & 0x0000FF00) >> 8) + .arg( e.ip & 0x000000FF); + pp.port = e.port; + addPotentialPeer(pp); + } + + } + catch (bt::Error & err) + { + Out(SYS_GEN|LOG_DEBUG) << "Error happened during saving of peer list : " << err.toString() << endl; + } + } + + void PeerManager::start() + { + started = true; + Globals::instance().getServer().addPeerManager(this); + } + + + void PeerManager::stop() + { + cnt->reset(); + available_chunks.clear(); + started = false; + Globals::instance().getServer().removePeerManager(this); + stopped(); + num_pending = 0; + } + + Peer* PeerManager::findPeer(Uint32 peer_id) + { + return peer_map.find(peer_id); + } + + void PeerManager::onRerunChoker() + { + // append a 0 ptr to killed + // so that the next update in TorrentControl + // will be forced to do the choking + killed.append(0); + } + + void PeerManager::updateAvailableChunks() + { + for (Uint32 i = 0;i < available_chunks.getNumBits();i++) + { + available_chunks.set(i,cnt->get(i) > 0); + } + } + + void PeerManager::peerSourceReady(kt::PeerSource* ps) + { + PotentialPeer pp; + while (ps->takePotentialPeer(pp)) + addPotentialPeer(pp); + } + + bool PeerManager::killBadPeer() + { + for (PtrMap<Uint32,Peer>::iterator i = peer_map.begin();i != peer_map.end();i++) + { + Peer* p = i->second; + if (p->getStats().aca_score <= -5.0 && p->getStats().aca_score > -50.0) + { + Out(SYS_GEN|LOG_DEBUG) << "Killing bad peer, to make room for other peers" << endl; + p->kill(); + return true; + } + } + return false; + } + + void PeerManager::pex(const QByteArray & arr) + { + if (!pex_on) + return; + + Out(SYS_CON|LOG_NOTICE) << "PEX: found " << (arr.size() / 6) << " peers" << endl; + for (Uint32 i = 0;i+6 <= arr.size();i+=6) + { + Uint8 tmp[6]; + memcpy(tmp,arr.data() + i,6); + PotentialPeer pp; + pp.port = ReadUint16(tmp,4); + Uint32 ip = ReadUint32(tmp,0); + pp.ip = QString("%1.%2.%3.%4") + .arg((ip & 0xFF000000) >> 24) + .arg((ip & 0x00FF0000) >> 16) + .arg((ip & 0x0000FF00) >> 8) + .arg( ip & 0x000000FF); + pp.local = false; + + addPotentialPeer(pp); + } + } + + + void PeerManager::setPexEnabled(bool on) + { + if (on && tor.isPrivate()) + return; + + if (pex_on == on) + return; + + QPtrList<Peer>::iterator i = peer_list.begin(); + while (i != peer_list.end()) + { + Peer* p = *i; + if (!p->isKilled()) + p->setPexEnabled(on); + i++; + } + pex_on = on; + } + + void PeerManager::setGroupIDs(Uint32 up,Uint32 down) + { + for (PtrMap<Uint32,Peer>::iterator i = peer_map.begin();i != peer_map.end();i++) + { + Peer* p = i->second; + p->setGroupIDs(up,down); + } + } +} + +#include "peermanager.moc" diff --git a/libktorrent/torrent/peermanager.h b/libktorrent/torrent/peermanager.h new file mode 100644 index 0000000..d5fdb9f --- /dev/null +++ b/libktorrent/torrent/peermanager.h @@ -0,0 +1,251 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTPEERMANAGER_H +#define BTPEERMANAGER_H + +#include <map> +#include <qobject.h> +#include <qptrlist.h> +#include <util/ptrmap.h> +#include "globals.h" +#include "peerid.h" +#include <util/bitset.h> +#include <interfaces/peersource.h> + +namespace mse +{ + class StreamSocket; +} + +namespace bt +{ + class Peer; + class ChunkManager; + class Torrent; + class Authenticate; + class ChunkCounter; + + + + const Uint32 MAX_SIMULTANIOUS_AUTHS = 20; + + /** + * @author Joris Guisson + * @brief Manages all the Peers + * + * This class manages all Peer objects. + * It can also open connections to other peers. + */ + class PeerManager : public QObject + { + Q_OBJECT + public: + /** + * Constructor. + * @param tor The Torrent + */ + PeerManager(Torrent & tor); + virtual ~PeerManager(); + + + /** + * Check for new connections, update down and upload speed of each Peer. + * Initiate new connections. + */ + void update(); + + /** + * Remove dead peers. + * @return The number of dead ones removed + */ + Uint32 clearDeadPeers(); + + /** + * Get the i'th Peer. + * @param index + * @return Peer or 0 if out of range + */ + Peer* getPeer(Uint32 index) {return peer_list.at(index);} + + /** + * Find a Peer based on it's ID + * @param peer_id The ID + * @return A Peer or 0, if nothing could be found + */ + Peer* findPeer(Uint32 peer_id); + + /** + * Try to connect to some peers + */ + void connectToPeers(); + + /** + * Close all Peer connections. + */ + void closeAllConnections(); + + /** + * Start listening to incoming requests. + */ + void start(); + + /** + * Stop listening to incoming requests. + */ + void stop(); + + /** + * Kill all peers who have been choked longer then @a older_then time. + * @param older_then Time in milliseconds + */ + void killChokedPeers(Uint32 older_then); + + Uint32 getNumConnectedPeers() const {return peer_list.count();} + Uint32 getNumPending() const {return num_pending;} + + static void setMaxConnections(Uint32 max); + static Uint32 getMaxConnections() {return max_connections;} + + static void setMaxTotalConnections(Uint32 max); + static Uint32 getMaxTotalConnections() {return max_total_connections;} + + static Uint32 getTotalConnections() {return total_connections;} + + /// Is the peer manager started + bool isStarted() const {return started;} + + /// Get the Torrent + Torrent & getTorrent() {return tor;} + + /** + * A new connection is ready for this PeerManager. + * @param sock The socket + * @param peer_id The Peer's ID + * @param support What extensions the peer supports + */ + void newConnection(mse::StreamSocket* sock,const PeerID & peer_id,Uint32 support); + + /** + * Add a potential peer + * @param pp The PotentialPeer + */ + void addPotentialPeer(const kt::PotentialPeer & pp); + + /** + * Kills all connections to seeders. + * This is used when torrent download gets finished + * and we should drop all connections to seeders + */ + void killSeeders(); + + /** + * Kills all peers that are not interested for a long time. + * This should be used when torrent is seeding ONLY. + */ + void killUninterested(); + + /// Get a BitSet of all available chunks + const BitSet & getAvailableChunksBitSet() const {return available_chunks;} + + /// Get the chunk counter. + ChunkCounter & getChunkCounter() {return *cnt;}; + + /// Are we connected to a Peer given it's PeerID ? + bool connectedTo(const PeerID & peer_id); + + /** + * A peer has authenticated. + * @param auth The Authenticate object + * @param ok Wether or not the attempt was succesfull + */ + void peerAuthenticated(Authenticate* auth,bool ok); + + /** + * Save the IP's and port numbers of all peers. + */ + void savePeerList(const QString & file); + + /** + * Load the peer list again and add them to the potential peers + */ + void loadPeerList(const QString & file); + + typedef QPtrList<Peer>::const_iterator CItr; + + CItr beginPeerList() const {return peer_list.begin();} + CItr endPeerList() const {return peer_list.end();} + + /// Is PEX eanbled + bool isPexEnabled() const {return pex_on;} + + /// Enable or disable PEX + void setPexEnabled(bool on); + + /// Set the group IDs of each peer + void setGroupIDs(Uint32 up,Uint32 down); + + public slots: + /** + * A PeerSource, has new potential peers. + * @param ps The PeerSource + */ + void peerSourceReady(kt::PeerSource* ps); + + private: + void updateAvailableChunks(); + bool killBadPeer(); + void createPeer(mse::StreamSocket* sock,const PeerID & peer_id,Uint32 support,bool local); + bool connectedTo(const QString & ip,Uint16 port) const; + + private slots: + void onHave(Peer* p,Uint32 index); + void onBitSetRecieved(const BitSet & bs); + void onRerunChoker(); + void pex(const QByteArray & arr); + + + signals: + void newPeer(Peer* p); + void peerKilled(Peer* p); + void stopped(); + + private: + PtrMap<Uint32,Peer> peer_map; + QPtrList<Peer> peer_list; + QPtrList<Peer> killed; + Torrent & tor; + bool started; + BitSet available_chunks; + ChunkCounter* cnt; + Uint32 num_pending; + bool pex_on; + + static Uint32 max_connections; + static Uint32 max_total_connections; + static Uint32 total_connections; + + std::multimap<QString,kt::PotentialPeer> potential_peers; + + typedef std::multimap<QString,kt::PotentialPeer>::iterator PPItr; + }; + +} + +#endif diff --git a/libktorrent/torrent/peersourcemanager.cpp b/libktorrent/torrent/peersourcemanager.cpp new file mode 100644 index 0000000..fef55b5 --- /dev/null +++ b/libktorrent/torrent/peersourcemanager.cpp @@ -0,0 +1,556 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <qfile.h> +#include <klocale.h> +#include <functions.h> +#include <util/log.h> +#include <torrent/globals.h> +#include <kademlia/dhtbase.h> +#include <kademlia/dhttrackerbackend.h> +#include "tracker.h" +#include "udptracker.h" +#include "httptracker.h" +#include "torrentcontrol.h" +#include "torrent.h" +#include "peermanager.h" +#include "peersourcemanager.h" + +namespace bt +{ + const Uint32 INITIAL_WAIT_TIME = 30; + const Uint32 LONGER_WAIT_TIME = 300; + const Uint32 FINAL_WAIT_TIME = 1800; + + PeerSourceManager::PeerSourceManager(TorrentControl* tor,PeerManager* pman) + : tor(tor),pman(pman),curr(0),m_dht(0),started(false),pending(false) + { + failures = 0; + trackers.setAutoDelete(true); + no_save_custom_trackers = false; + + const TrackerTier* t = tor->getTorrent().getTrackerList(); + int tier = 1; + while (t) + { + // add all standard trackers + const KURL::List & tr = t->urls; + KURL::List::const_iterator i = tr.begin(); + while (i != tr.end()) + { + addTracker(*i,false,tier); + i++; + } + + tier++; + t = t->next; + } + + //load custom trackers + loadCustomURLs(); + + connect(&timer,SIGNAL(timeout()),this,SLOT(updateCurrentManually())); + } + + PeerSourceManager::~PeerSourceManager() + { + saveCustomURLs(); + additional.setAutoDelete(true); + QPtrList<kt::PeerSource>::iterator itr = additional.begin(); + while (itr != additional.end()) + { + kt::PeerSource* ps = *itr; + ps->aboutToBeDestroyed(); + itr++; + } + additional.clear(); + } + + void PeerSourceManager::addTracker(Tracker* trk) + { + trackers.insert(trk->trackerURL(),trk); + connect(trk,SIGNAL(peersReady( kt::PeerSource* )), + pman,SLOT(peerSourceReady( kt::PeerSource* ))); + } + + void PeerSourceManager::addPeerSource(kt::PeerSource* ps) + { + additional.append(ps); + connect(ps,SIGNAL(peersReady( kt::PeerSource* )), + pman,SLOT(peerSourceReady( kt::PeerSource* ))); + } + + void PeerSourceManager::removePeerSource(kt::PeerSource* ps) + { + disconnect(ps,SIGNAL(peersReady( kt::PeerSource* )), + pman,SLOT(peerSourceReady( kt::PeerSource* ))); + additional.remove(ps); + } + + void PeerSourceManager::start() + { + if (started) + return; + + started = true; + QPtrList<kt::PeerSource>::iterator i = additional.begin(); + while (i != additional.end()) + { + (*i)->start(); + i++; + } + + if (!curr) + { + if (trackers.count() > 0) + { + switchTracker(selectTracker()); + tor->resetTrackerStats(); + curr->start(); + } + } + else + { + tor->resetTrackerStats(); + curr->start(); + } + } + + void PeerSourceManager::stop(WaitJob* wjob) + { + if (!started) + return; + + started = false; + QPtrList<kt::PeerSource>::iterator i = additional.begin(); + while (i != additional.end()) + { + (*i)->stop(); + i++; + } + + if (curr) + curr->stop(wjob); + + timer.stop(); + statusChanged(i18n("Stopped")); + } + + void PeerSourceManager::completed() + { + QPtrList<kt::PeerSource>::iterator i = additional.begin(); + while (i != additional.end()) + { + (*i)->completed(); + i++; + } + + if (curr) + curr->completed(); + } + + void PeerSourceManager::manualUpdate() + { + QPtrList<kt::PeerSource>::iterator i = additional.begin(); + while (i != additional.end()) + { + (*i)->manualUpdate(); + i++; + } + + if (curr) + { + timer.stop(); + curr->manualUpdate(); + } + } + + + + KURL PeerSourceManager::getTrackerURL() const + { + if (curr) + return curr->trackerURL(); + else + return KURL(); + } + + KURL::List PeerSourceManager::getTrackerURLs() + { + KURL::List urls; + const TrackerTier* t = tor->getTorrent().getTrackerList(); + while (t) + { + urls += t->urls; + t = t->next; + } + + urls += custom_trackers; + return urls; + } + + void PeerSourceManager::addTracker(KURL url, bool custom,int tier) + { + if (trackers.contains(url)) + return; + + Tracker* trk = 0; + if (url.protocol() == "udp") + trk = new UDPTracker(url,tor,tor->getTorrent().getPeerID(),tier); + else + trk = new HTTPTracker(url,tor,tor->getTorrent().getPeerID(),tier); + + addTracker(trk); + if (custom) + { + custom_trackers.append(url); + if (!no_save_custom_trackers) + saveCustomURLs(); + } + } + + bool PeerSourceManager::removeTracker(KURL url) + { + if (!custom_trackers.contains(url)) + return false; + + custom_trackers.remove(url); + Tracker* trk = trackers.find(url); + if (curr == trk) + { + // do a timed delete on the tracker, so the stop signal + // has plenty of time to reach it + trk->stop(); + trk->timedDelete(10 * 1000); + trackers.setAutoDelete(false); + trackers.erase(url); + trackers.setAutoDelete(true); + + if (trackers.count() > 0) + { + switchTracker(selectTracker()); + tor->resetTrackerStats(); + curr->start(); + } + } + else + { + // just delete if not the current one + trackers.erase(url); + } + saveCustomURLs(); + return true; + } + + void PeerSourceManager::setTracker(KURL url) + { + Tracker* trk = trackers.find(url); + if (!trk) + return; + + if (curr != trk) + { + if (curr) + curr->stop(); + switchTracker(trk); + tor->resetTrackerStats(); + trk->start(); + } + } + + void PeerSourceManager::restoreDefault() + { + KURL::List::iterator i = custom_trackers.begin(); + while (i != custom_trackers.end()) + { + Tracker* t = trackers.find(*i); + if (t) + { + if (curr == t) + { + if (t->isStarted()) + t->stop(); + + curr = 0; + trackers.erase(*i); + if (trackers.count() > 0) + { + switchTracker(trackers.begin()->second); + if (started) + { + tor->resetTrackerStats(); + curr->start(); + } + } + } + else + { + trackers.erase(*i); + } + } + i++; + } + + custom_trackers.clear(); + saveCustomURLs(); + } + + void PeerSourceManager::saveCustomURLs() + { + QString trackers_file = tor->getTorDir() + "trackers"; + QFile file(trackers_file); + if(!file.open(IO_WriteOnly)) + return; + + QTextStream stream(&file); + for (KURL::List::iterator i = custom_trackers.begin();i != custom_trackers.end();i++) + stream << (*i).prettyURL() << ::endl; + } + + void PeerSourceManager::loadCustomURLs() + { + QString trackers_file = tor->getTorDir() + "trackers"; + QFile file(trackers_file); + if(!file.open(IO_ReadOnly)) + return; + + no_save_custom_trackers = true; + QTextStream stream(&file); + while (!stream.atEnd()) + { + KURL url = stream.readLine(); + addTracker(url,true); + } + no_save_custom_trackers = false; + } + + Tracker* PeerSourceManager::selectTracker() + { + Tracker* n = 0; + PtrMap<KURL,Tracker>::iterator i = trackers.begin(); + while (i != trackers.end()) + { + Tracker* t = i->second; + if (!n) + n = t; + else if (t->failureCount() < n->failureCount()) + n = t; + else if (t->failureCount() == n->failureCount()) + n = t->getTier() < n->getTier() ? t : n; + i++; + } + + if (n) + { + Out(SYS_TRK|LOG_DEBUG) << "Selected tracker " << n->trackerURL().prettyURL() + << " (tier = " << n->getTier() << ")" << endl; + } + + return n; + } + + void PeerSourceManager::onTrackerError(const QString & err) + { + failures++; + pending = false; + if (started) + statusChanged(err); + + if (!started) + return; + + // select an other tracker + Tracker* trk = selectTracker(); + + if (!trk) + { + if (curr->failureCount() > 5) + { + // we failed to contact the only tracker 5 times in a row, so try again in + // 30 minutes + curr->setInterval(FINAL_WAIT_TIME); + timer.start(FINAL_WAIT_TIME * 1000,true); + request_time = QDateTime::currentDateTime(); + } + else if (curr->failureCount() > 2) + { + // we failed to contact the only tracker 3 times in a row, so try again in + // a minute or 5, no need for hammering every 30 seconds + curr->setInterval(LONGER_WAIT_TIME); + timer.start(LONGER_WAIT_TIME * 1000,true); + request_time = QDateTime::currentDateTime(); + } + else + { + // lets not hammer and wait 30 seconds + curr->setInterval(INITIAL_WAIT_TIME); + timer.start(INITIAL_WAIT_TIME * 1000,true); + request_time = QDateTime::currentDateTime(); + } + } + else + { + curr->stop(); + // switch to another one + switchTracker(trk); + if (trk->failureCount() == 0) + { + tor->resetTrackerStats(); + curr->start(); + } + else if (trk->failureCount() > 5) + { + curr->setInterval(FINAL_WAIT_TIME); + timer.start(FINAL_WAIT_TIME * 1000,true); + request_time = QDateTime::currentDateTime(); + } + else if (trk->failureCount() > 2) + { + // we tried everybody 3 times and it didn't work + // wait 5 minutes and try again + curr->setInterval(LONGER_WAIT_TIME); + timer.start(LONGER_WAIT_TIME * 1000,true); + request_time = QDateTime::currentDateTime(); + } + else + { + // wait 30 seconds and try again + curr->setInterval(INITIAL_WAIT_TIME); + timer.start(INITIAL_WAIT_TIME * 1000,true); + request_time = QDateTime::currentDateTime(); + } + } + } + + void PeerSourceManager::onTrackerOK() + { + failures = 0; + if (started) + { + timer.start(curr->getInterval() * 1000,true); + curr->scrape(); + } + pending = false; + if (started) + statusChanged(i18n("OK")); + request_time = QDateTime::currentDateTime(); + } + + void PeerSourceManager::onTrackerRequestPending() + { + if (started) + statusChanged(i18n("Announcing")); + pending = true; + } + + void PeerSourceManager::updateCurrentManually() + { + if (!curr) + return; + + if (!curr->isStarted()) + tor->resetTrackerStats(); + + curr->manualUpdate(); + } + + void PeerSourceManager::switchTracker(Tracker* trk) + { + if (curr == trk) + return; + + if (curr) + { + disconnect(curr,SIGNAL(requestFailed( const QString& )), + this,SLOT(onTrackerError( const QString& ))); + disconnect(curr,SIGNAL(requestOK()),this,SLOT(onTrackerOK())); + disconnect(curr,SIGNAL(requestPending()),this,SLOT(onTrackerRequestPending())); + curr = 0; + } + + curr = trk; + if (curr) + { + Out(SYS_TRK|LOG_NOTICE) << "Switching to tracker " << trk->trackerURL() << endl; + QObject::connect(curr,SIGNAL(requestFailed( const QString& )), + this,SLOT(onTrackerError( const QString& ))); + + QObject::connect(curr,SIGNAL(requestOK()), + this,SLOT(onTrackerOK())); + + QObject::connect(curr,SIGNAL(requestPending()), + this,SLOT(onTrackerRequestPending())); + } + } + + Uint32 PeerSourceManager::getTimeToNextUpdate() const + { + if (pending || !started || !curr) + return 0; + + return curr->getInterval() - request_time.secsTo(QDateTime::currentDateTime()); + } + + Uint32 PeerSourceManager::getNumSeeders() const + { + return curr ? curr->getNumSeeders() : 0; + } + + + Uint32 PeerSourceManager::getNumLeechers() const + { + return curr ? curr->getNumLeechers() : 0; + } + + void PeerSourceManager::addDHT() + { + if(m_dht) + { + removePeerSource(m_dht); + delete m_dht; + } + + m_dht = new dht::DHTTrackerBackend(Globals::instance().getDHT(),tor); + + // add the DHT source + addPeerSource(m_dht); + } + + void PeerSourceManager::removeDHT() + { + if(m_dht == 0) + { + removePeerSource(m_dht); + return; + } + + removePeerSource(m_dht); + delete m_dht; + m_dht = 0; + } + + bool PeerSourceManager::dhtStarted() + { + return m_dht != 0; + } + + +} + +#include "peersourcemanager.moc" diff --git a/libktorrent/torrent/peersourcemanager.h b/libktorrent/torrent/peersourcemanager.h new file mode 100644 index 0000000..cdace4e --- /dev/null +++ b/libktorrent/torrent/peersourcemanager.h @@ -0,0 +1,182 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTPEERSOURCEMANAGER_H +#define BTPEERSOURCEMANAGER_H + +#include <qtimer.h> +#include <qdatetime.h> +#include <qptrlist.h> +#include <util/ptrmap.h> +#include <interfaces/trackerslist.h> + +namespace kt +{ + class PeerSource; +} + +namespace dht +{ + class DHTTrackerBackend; +} + +namespace bt +{ + class Tracker; + class PeerManager; + class Torrent; + class TorrentControl; + + /** + * @author Joris Guisson <joris.guisson@gmail.com> + * + * This class manages all PeerSources. + */ + class PeerSourceManager : public QObject, public kt::TrackersList + { + Q_OBJECT + + TorrentControl* tor; + PeerManager* pman; + PtrMap<KURL,Tracker> trackers; + QPtrList<kt::PeerSource> additional; + Tracker* curr; + dht::DHTTrackerBackend* m_dht; + bool started; + bool pending; + KURL::List custom_trackers; + QDateTime request_time; + QTimer timer; + Uint32 failures; + bool no_save_custom_trackers; + public: + PeerSourceManager(TorrentControl* tor,PeerManager* pman); + virtual ~PeerSourceManager(); + + + /** + * Add a PeerSource, the difference between PeerSource and Tracker + * is that only one Tracker can be used at the same time, + * PeerSource can always be used. + * @param ps The PeerSource + */ + void addPeerSource(kt::PeerSource* ps); + + /** + * See if the PeerSourceManager has been started + */ + bool isStarted() const {return started;} + + /** + * Start gathering peers + */ + void start(); + + /** + * Stop gathering peers + * @param wjob WaitJob to wait at exit for the completion of stopped events to the trackers + */ + void stop(WaitJob* wjob = 0); + + /** + * Notify peersources and trackrs that the download is complete. + */ + void completed(); + + /** + * Do a manual update on all peer sources and trackers. + */ + void manualUpdate(); + + /** + * Remove a Tracker or PeerSource. + * @param ps + */ + void removePeerSource(kt::PeerSource* ps); + + virtual KURL getTrackerURL() const; + virtual KURL::List getTrackerURLs(); + virtual void addTracker(KURL url, bool custom = true,int tier = 1); + virtual bool removeTracker(KURL url); + virtual void setTracker(KURL url); + virtual void restoreDefault(); + + /** + * Get the time to the next tracker update. + * @return The time in seconds + */ + Uint32 getTimeToNextUpdate() const; + + /// Get the number of potential seeders + Uint32 getNumSeeders() const; + + /// Get the number of potential leechers + Uint32 getNumLeechers() const; + + /// Get the number of failures + Uint32 getNumFailures() const {return failures;} + + ///Adds DHT as PeerSource for this torrent + void addDHT(); + ///Removes DHT from PeerSourceManager for this torrent. + void removeDHT(); + ///Checks if DHT is enabled + bool dhtStarted(); + + private slots: + /** + * The an error happened contacting the tracker. + * @param err The error + */ + void onTrackerError(const QString & err); + + /** + * Tracker update was OK. + * @param + */ + void onTrackerOK(); + + /** + * Tracker is doing a request. + */ + void onTrackerRequestPending(); + + /** + * Update the current tracker manually + */ + void updateCurrentManually(); + + signals: + /** + * Status has changed of the tracker. + * @param ns The new status + */ + void statusChanged(const QString & ns); + + private: + void saveCustomURLs(); + void loadCustomURLs(); + void addTracker(Tracker* trk); + void switchTracker(Tracker* trk); + Tracker* selectTracker(); + }; + +} + +#endif diff --git a/libktorrent/torrent/peeruploader.cpp b/libktorrent/torrent/peeruploader.cpp new file mode 100644 index 0000000..1e0dbca --- /dev/null +++ b/libktorrent/torrent/peeruploader.cpp @@ -0,0 +1,130 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <set> +#include <ksocketaddress.h> +#include <util/log.h> +#include <util/functions.h> +#include <util/sha1hash.h> +#include "peeruploader.h" +#include "peer.h" +#include "chunkmanager.h" +#include "packetwriter.h" +#include "torrent.h" + +using namespace KNetwork; + +namespace bt +{ + + PeerUploader::PeerUploader(Peer* peer) : peer(peer) + { + uploaded = 0; + } + + + PeerUploader::~PeerUploader() + {} + + void PeerUploader::addRequest(const Request & r) + { + // Out(SYS_CON|LOG_DEBUG) << + // QString("PeerUploader::addRequest %1 %2 %3\n").arg(r.getIndex()).arg(r.getOffset()).arg(r.getLength()) << endl; + + // allowed fast chunks go to the front of the queue + requests.append(r); + } + + void PeerUploader::removeRequest(const Request & r) + { + // Out(SYS_CON|LOG_DEBUG) << + // QString("PeerUploader::removeRequest %1 %2 %3\n").arg(r.getIndex()).arg(r.getOffset()).arg(r.getLength()) << endl; + requests.remove(r); + peer->getPacketWriter().doNotSendPiece(r,peer->getStats().fast_extensions); + } + + Uint32 PeerUploader::update(ChunkManager & cman,Uint32 opt_unchoked) + { + Uint32 ret = uploaded; + uploaded = 0; + + PacketWriter & pw = peer->getPacketWriter(); + + // if we have choked the peer do not upload + if (peer->areWeChoked()) + return ret; + + if (peer->isSnubbed() && !peer->areWeChoked() && + !cman.completed() && peer->getID() != opt_unchoked) + return ret; + + + while (requests.count() > 0) + { + Request r = requests.front(); + + Chunk* c = cman.grabChunk(r.getIndex()); + if (c && c->getData()) + { + if (!pw.sendChunk(r.getIndex(),r.getOffset(),r.getLength(),c)) + { + if (peer->getStats().fast_extensions) + pw.sendReject(r); + } + requests.pop_front(); + } + else + { + // remove requests we can't satisfy + Out(SYS_CON|LOG_DEBUG) << "Cannot satisfy request" << endl; + if (peer->getStats().fast_extensions) + pw.sendReject(r); + requests.pop_front(); + } + } + + return ret; + } + + void PeerUploader::clearAllRequests() + { + bool fast_ext = peer->getStats().fast_extensions; + PacketWriter & pw = peer->getPacketWriter(); + pw.clearPieces(fast_ext); + + if (fast_ext) + { + // reject all requests + // if the peer supports fast extensions, + // choke doesn't mean reject all + QValueList<Request>::iterator i = requests.begin(); + while (i != requests.end()) + { + pw.sendReject(*i); + i++; + } + } + requests.clear(); + } + + Uint32 PeerUploader::getNumRequests() const + { + return requests.count() + peer->getPacketWriter().getNumDataPacketsToWrite(); + } +} diff --git a/libktorrent/torrent/peeruploader.h b/libktorrent/torrent/peeruploader.h new file mode 100644 index 0000000..94aea74 --- /dev/null +++ b/libktorrent/torrent/peeruploader.h @@ -0,0 +1,93 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTPEERUPLOADER_H +#define BTPEERUPLOADER_H + +#include <set> +#include <qvaluelist.h> +#include "request.h" + + + +namespace bt +{ + class Peer; + class ChunkManager; + + const Uint32 ALLOWED_FAST_SIZE = 8; + + /** + * @author Joris Guisson + * @brief Uploads pieces to a Peer + * + * This class handles the uploading of pieces to a Peer. It keeps + * track of a list of Request objects. All these Requests where sent + * by the Peer. It will upload the pieces to the Peer, making sure + * that the maximum upload rate isn't surpassed. + */ + class PeerUploader + { + Peer* peer; + QValueList<Request> requests; + Uint32 uploaded; + public: + /** + * Constructor. Set the Peer. + * @param peer The Peer + */ + PeerUploader(Peer* peer); + virtual ~PeerUploader(); + + /** + * Add a Request to the list of Requests. + * @param r The Request + */ + void addRequest(const Request & r); + + /** + * Remove a Request from the list of Requests. + * @param r The Request + */ + void removeRequest(const Request & r); + + /** + * Update the PeerUploader. This will check if there are Request, and + * will try to handle them. + * @param cman The ChunkManager + * @param opt_unchoked ID of optimisticly unchoked peer + * @return The number of bytes uploaded + */ + Uint32 update(ChunkManager & cman,Uint32 opt_unchoked); + + /// Get the number of requests + Uint32 getNumRequests() const; + + + void addUploadedBytes(Uint32 bytes) {uploaded += bytes;} + + /** + * Clear all pending requests. + */ + void clearAllRequests(); + }; + +} + +#endif diff --git a/libktorrent/torrent/piece.cpp b/libktorrent/torrent/piece.cpp new file mode 100644 index 0000000..0fff862 --- /dev/null +++ b/libktorrent/torrent/piece.cpp @@ -0,0 +1,34 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include "piece.h" + +namespace bt +{ + + Piece::Piece(Uint32 index, Uint32 off, Uint32 len, Uint32 peer,const Uint8* data) + : Request(index, off, len, peer),data(data) + {} + + + Piece::~Piece() + {} + + +} diff --git a/libktorrent/torrent/piece.h b/libktorrent/torrent/piece.h new file mode 100644 index 0000000..9e749db --- /dev/null +++ b/libktorrent/torrent/piece.h @@ -0,0 +1,44 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTPIECE_H +#define BTPIECE_H + +#include "request.h" + +namespace bt +{ + + /** + @author Joris Guisson + */ + class Piece : public Request + { + public: + Piece(Uint32 index, Uint32 off, Uint32 len, Uint32 peer,const Uint8* data); + virtual ~Piece(); + + const Uint8* getData() const {return data;} + private: + const Uint8* data; + }; + +} + +#endif diff --git a/libktorrent/torrent/preallocationthread.cpp b/libktorrent/torrent/preallocationthread.cpp new file mode 100644 index 0000000..6fb2100 --- /dev/null +++ b/libktorrent/torrent/preallocationthread.cpp @@ -0,0 +1,134 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <errno.h> +#include <unistd.h> +#include <fcntl.h> +#include <sys/types.h> +#include <sys/stat.h> + +#include <util/log.h> +#include <util/error.h> +#include <qfile.h> +#include <klocale.h> +#include "preallocationthread.h" +#include "chunkmanager.h" +#include "globals.h" + +#ifndef O_LARGEFILE +# define O_LARGEFILE 0 +#endif + +namespace bt +{ + + PreallocationThread::PreallocationThread(ChunkManager* cman) : cman(cman),stopped(false),not_finished(false),done(false) + { + bytes_written = 0; + } + + + PreallocationThread::~PreallocationThread() + {} + + void PreallocationThread::run() + { + try + { + cman->preallocateDiskSpace(this); + } + catch (Error & err) + { + setErrorMsg(err.toString()); + } + + mutex.lock(); + done = true; + mutex.unlock(); + Out(SYS_GEN|LOG_NOTICE) << "PreallocationThread has finished" << endl; + } + + void PreallocationThread::stop() + { + mutex.lock(); + stopped = true; + mutex.unlock(); + } + + void PreallocationThread::setErrorMsg(const QString & msg) + { + mutex.lock(); + error_msg = msg; stopped = true; + mutex.unlock(); + } + + bool PreallocationThread::isStopped() const + { + mutex.lock(); + bool tmp = stopped; + mutex.unlock(); + return tmp; + } + + bool PreallocationThread::errorHappened() const + { + mutex.lock(); + bool ret = !error_msg.isNull(); + mutex.unlock(); + return ret; + } + + void PreallocationThread::written(Uint64 nb) + { + mutex.lock(); + bytes_written += nb; + mutex.unlock(); + } + + Uint64 PreallocationThread::bytesWritten() + { + mutex.lock(); + Uint64 tmp = bytes_written; + mutex.unlock(); + return tmp; + } + + bool PreallocationThread::isDone() const + { + mutex.lock(); + bool tmp = done; + mutex.unlock(); + return tmp; + } + + bool PreallocationThread::isNotFinished() const + { + mutex.lock(); + bool tmp = not_finished; + mutex.unlock(); + return tmp; + } + + void PreallocationThread::setNotFinished() + { + mutex.lock(); + not_finished = true; + mutex.unlock(); + } +} diff --git a/libktorrent/torrent/preallocationthread.h b/libktorrent/torrent/preallocationthread.h new file mode 100644 index 0000000..31bd668 --- /dev/null +++ b/libktorrent/torrent/preallocationthread.h @@ -0,0 +1,94 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTPREALLOCATIONTHREAD_H +#define BTPREALLOCATIONTHREAD_H + +#include <qstring.h> +#include <qthread.h> +#include <qmap.h> +#include <qmutex.h> +#include <util/constants.h> + + + +namespace bt +{ + class ChunkManager; + + /** + * @author Joris Guisson <joris.guisson@gmail.com> + * + * Thread to preallocate diskspace + */ + class PreallocationThread : public QThread + { + ChunkManager* cman; + bool stopped,not_finished,done; + QString error_msg; + Uint64 bytes_written; + mutable QMutex mutex; + public: + PreallocationThread(ChunkManager* cman); + virtual ~PreallocationThread(); + + virtual void run(); + + + /** + * Stop the thread. + */ + void stop(); + + /** + * Set an error message, also calls stop + * @param msg The message + */ + void setErrorMsg(const QString & msg); + + /// See if the thread has been stopped + bool isStopped() const; + + /// Did an error occur during the preallocation ? + bool errorHappened() const; + + /// Get the error_msg + const QString & errorMessage() const {return error_msg;} + + /// nb Number of bytes have been written + void written(Uint64 nb); + + /// Get the number of bytes written + Uint64 bytesWritten(); + + /// Allocation was aborted, so the next time the torrent is started it needs to be started again + void setNotFinished(); + + /// See if the allocation hasn't completed yet + bool isNotFinished() const; + + /// See if the thread was done + bool isDone() const; + private: + bool expand(const QString & path,Uint64 max_size); + }; + +} + +#endif diff --git a/libktorrent/torrent/queuemanager.cpp b/libktorrent/torrent/queuemanager.cpp new file mode 100644 index 0000000..4135f8f --- /dev/null +++ b/libktorrent/torrent/queuemanager.cpp @@ -0,0 +1,811 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * ivasic@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include "queuemanager.h" + +#include <qstring.h> +#include <kmessagebox.h> +#include <klocale.h> +#include <util/log.h> +#include <util/error.h> +#include <util/sha1hash.h> +#include <util/waitjob.h> +#include <util/fileops.h> +#include <torrent/globals.h> +#include <torrent/torrent.h> +#include <torrent/torrentcontrol.h> +#include <interfaces/torrentinterface.h> +#include <interfaces/trackerslist.h> +#include <settings.h> + + +using namespace kt; + +namespace bt +{ + + QueueManager::QueueManager() : QObject(),exiting(false) + { + downloads.setAutoDelete(true); + max_downloads = 0; + max_seeds = 0; //for testing. Needs to be added to Settings:: + + keep_seeding = true; //test. Will be passed from Core + paused_state = false; + } + + + QueueManager::~QueueManager() + {} + + void QueueManager::append(kt::TorrentInterface* tc) + { + downloads.append(tc); + downloads.sort(); + + connect(tc, SIGNAL(diskSpaceLow(kt::TorrentInterface*, bool)), this, SLOT(onLowDiskSpace(kt::TorrentInterface*, bool))); + connect(tc, SIGNAL(torrentStopped(kt::TorrentInterface*)), this, SLOT(torrentStopped(kt::TorrentInterface*))); + } + + void QueueManager::remove(kt::TorrentInterface* tc) + { + paused_torrents.erase(tc); + + int index = downloads.findRef(tc); + + if (index != -1) + downloads.remove(index); + else + Out(SYS_GEN | LOG_IMPORTANT) << "Could not delete removed torrent control." << endl; + } + + void QueueManager::clear() + { + Uint32 nd = downloads.count(); + + paused_torrents.clear(); + downloads.clear(); + + // wait for a second to allow all http jobs to send the stopped event + if (nd > 0) + SynchronousWait(1000); + } + + kt::TorrentStartResponse QueueManager::start(kt::TorrentInterface* tc, bool user) + { + const TorrentStats & s = tc->getStats(); + + bool start_tc = user; + + bool check_done = false; + + if (tc->isCheckingData(check_done) && !check_done) + return kt::BUSY_WITH_DATA_CHECK; + + if (!user) + { + if (s.completed) + start_tc = (max_seeds == 0 || getNumRunning(false, true) < max_seeds); + else + start_tc = (max_downloads == 0 || getNumRunning(true) < max_downloads); + } + else + { + //User started this torrent so make it user controlled + tc->setPriority(0); + } + + if (start_tc) + { + + if (!s.completed) //no need to check diskspace for seeding torrents + { + //check diskspace + bool shortDiskSpace = !tc->checkDiskSpace(false); + + if (shortDiskSpace) + { + //we're short! + + switch (Settings::startDownloadsOnLowDiskSpace()) + { + + case 0: //don't start! + tc->setPriority(0); + return kt::NOT_ENOUGH_DISKSPACE; + + case 1: //ask user + if (KMessageBox::questionYesNo(0, i18n("You don't have enough disk space to download this torrent. Are you sure you want to continue?"), i18n("Insufficient disk space for %1").arg(s.torrent_name)) == KMessageBox::No) + { + tc->setPriority(0); + return kt::USER_CANCELED; + } + else + break; + + case 2: //force start + break; + } + } + } + + Out(SYS_GEN | LOG_NOTICE) << "Starting download" << endl; + + float ratio = kt::ShareRatio(s); + + float max_ratio = tc->getMaxShareRatio(); + + if (s.completed && max_ratio > 0 && ratio >= max_ratio) + { + if (KMessageBox::questionYesNo(0, i18n("Torrent \"%1\" has reached its maximum share ratio. Ignore the limit and start seeding anyway?").arg(s.torrent_name), i18n("Maximum share ratio limit reached.")) == KMessageBox::Yes) + { + tc->setMaxShareRatio(0.00f); + startSafely(tc); + } + else + return kt::USER_CANCELED; + } + else + startSafely(tc); + } + else + { + return kt::QM_LIMITS_REACHED; + } + + return kt::START_OK; + } + + void QueueManager::stop(kt::TorrentInterface* tc, bool user) + { + bool check_done = false; + + if (tc->isCheckingData(check_done) && !check_done) + return; + + const TorrentStats & s = tc->getStats(); + + if (s.running) + { + stopSafely(tc, user); + } + + if (user) //dequeue it + tc->setPriority(0); + } + + void QueueManager::startall(int type) + { + QPtrList<kt::TorrentInterface>::iterator i = downloads.begin(); + + while (i != downloads.end()) + { + kt::TorrentInterface* tc = *i; + + if (type >= 3) + start(tc, true); + else + { + if ((tc->getStats().completed && type == 2) || (!tc->getStats().completed && type == 1) || (type == 3)) + start(tc, true); + } + + i++; + } + } + + void QueueManager::stopall(int type) + { + QPtrList<kt::TorrentInterface>::iterator i = downloads.begin(); + + while (i != downloads.end()) + { + kt::TorrentInterface* tc = *i; + + const TorrentStats & s = tc->getStats(); + + if (tc->getStats().running) + { + try + { + if (type >= 3) + stopSafely(tc, true); + else if ((s.completed && type == 2) || (!s.completed && type == 1)) + stopSafely(tc, true); + } + catch (bt::Error & err) + { + QString msg = + i18n("Error stopping torrent %1 : %2") + .arg(s.torrent_name).arg(err.toString()); + KMessageBox::error(0, msg, i18n("Error")); + } + } + else //if torrent is not running but it is queued we need to make it user controlled + if ((s.completed && type == 2) || (!s.completed && type == 1) || (type == 3)) + tc->setPriority(0); + + i++; + } + } + + void QueueManager::onExit(WaitJob* wjob) + { + exiting = true; + QPtrList<kt::TorrentInterface>::iterator i = downloads.begin(); + + while (i != downloads.end()) + { + kt::TorrentInterface* tc = *i; + + if (tc->getStats().running) + { + stopSafely(tc, false, wjob); + } + + i++; + } + } + + void QueueManager::startNext() + { + orderQueue(); + } + + int QueueManager::countDownloads() + { + int nr = 0; + QPtrList<TorrentInterface>::const_iterator i = downloads.begin(); + + while (i != downloads.end()) + { + if (!(*i)->getStats().completed) + ++nr; + + ++i; + } + + return nr; + } + + int QueueManager::countSeeds() + { + int nr = 0; + QPtrList<TorrentInterface>::const_iterator i = downloads.begin(); + + while (i != downloads.end()) + { + if ((*i)->getStats().completed) + ++nr; + + ++i; + } + + return nr; + } + + int QueueManager::getNumRunning(bool onlyDownload, bool onlySeed) + { + int nr = 0; + // int test = 1; + QPtrList<TorrentInterface>::const_iterator i = downloads.begin(); + + while (i != downloads.end()) + { + const TorrentInterface* tc = *i; + + const TorrentStats & s = tc->getStats(); + + //Out() << "Torrent " << test++ << s.torrent_name << " priority: " << tc->getPriority() << endl; + if (s.running) + { + if (onlyDownload) + { + if (!s.completed) nr++; + } + else + { + if (onlySeed) + { + if (s.completed) nr++; + } + else + nr++; + } + } + + i++; + } + + // Out() << endl; + return nr; + } + + int QueueManager::getNumRunning(bool userControlled, bool onlyDownloads, bool onlySeeds) + { + int nr = 0; + // int test = 1; + QPtrList<TorrentInterface>::const_iterator i = downloads.begin(); + + while (i != downloads.end()) + { + const TorrentInterface* tc = *i; + + const TorrentStats & s = tc->getStats(); + + //Out() << "Torrent " << test++ << s.torrent_name << " priority: " << tc->getPriority() << endl; + if (s.running) + { + if (onlyDownloads) + { + if (!s.completed && (userControlled && s.user_controlled)) nr++; + } + else + { + if (onlySeeds) + { + if (s.completed && (userControlled && s.user_controlled)) nr++; + } + else + if (userControlled && s.user_controlled) nr++; + } + } + + i++; + } + + // Out() << endl; + return nr; + } + + QPtrList<kt::TorrentInterface>::iterator QueueManager::begin() + { + return downloads.begin(); + } + + QPtrList<kt::TorrentInterface>::iterator QueueManager::end() + { + return downloads.end(); + } + + void QueueManager::setMaxDownloads(int m) + { + max_downloads = m; + } + + void QueueManager::setMaxSeeds(int m) + { + max_seeds = m; + } + + void QueueManager::setKeepSeeding(bool ks) + { + keep_seeding = ks; + } + + bool QueueManager::allreadyLoaded(const SHA1Hash & ih) const + { + QPtrList<kt::TorrentInterface>::const_iterator itr = downloads.begin(); + + while (itr != downloads.end()) + { + const TorrentControl* tor = (const TorrentControl*)(*itr); + + if (tor->getTorrent().getInfoHash() == ih) + return true; + + itr++; + } + + return false; + } + + void QueueManager::mergeAnnounceList(const SHA1Hash & ih, const TrackerTier* trk) + + { + QPtrList<kt::TorrentInterface>::iterator itr = downloads.begin(); + + while (itr != downloads.end()) + { + TorrentControl* tor = (TorrentControl*)(*itr); + + if (tor->getTorrent().getInfoHash() == ih) + { + TrackersList* ta = tor->getTrackersList(); + ta->merge(trk); + return; + } + + itr++; + } + } + + void QueueManager::orderQueue() + { + if (!downloads.count()) + return; + + if (paused_state || exiting) + return; + + downloads.sort(); + + QPtrList<TorrentInterface>::const_iterator it = downloads.begin(); + QPtrList<TorrentInterface>::const_iterator its = downloads.end(); + + + if (max_downloads != 0 || max_seeds != 0) + { + bt::QueuePtrList download_queue; + bt::QueuePtrList seed_queue; + + int user_downloading = 0; + int user_seeding = 0; + + for (; it != downloads.end(); ++it) + { + TorrentInterface* tc = *it; + + const TorrentStats & s = tc->getStats(); + + if (s.running && s.user_controlled) + { + if (!s.completed) + ++user_downloading; + else + ++user_seeding; + } + + if (!s.user_controlled && !tc->isMovingFiles() && !s.stopped_by_error) + { + if (s.completed) + seed_queue.append(tc); + else + download_queue.append(tc); + } + } + + int max_qm_downloads = max_downloads - user_downloading; + + int max_qm_seeds = max_seeds - user_seeding; + + //stop all QM started torrents + + for (Uint32 i = max_qm_downloads; i < download_queue.count() && max_downloads; ++i) + { + TorrentInterface* tc = download_queue.at(i); + + const TorrentStats & s = tc->getStats(); + + if (s.running && !s.user_controlled && !s.completed) + { + Out(SYS_GEN | LOG_DEBUG) << "QM Stopping: " << s.torrent_name << endl; + stop(tc); + } + } + + //stop all QM started torrents + for (Uint32 i = max_qm_seeds; i < seed_queue.count() && max_seeds; ++i) + { + TorrentInterface* tc = seed_queue.at(i); + + const TorrentStats & s = tc->getStats(); + + if (s.running && !s.user_controlled && s.completed) + { + Out(SYS_GEN | LOG_NOTICE) << "QM Stopping: " << s.torrent_name << endl; + stop(tc); + } + } + + //Now start all needed torrents + if (max_downloads == 0) + max_qm_downloads = download_queue.count(); + + if (max_seeds == 0) + max_qm_seeds = seed_queue.count(); + + int counter = 0; + + for (Uint32 i = 0; counter < max_qm_downloads && i < download_queue.count(); ++i) + { + TorrentInterface* tc = download_queue.at(i); + + const TorrentStats & s = tc->getStats(); + + if (!s.running && !s.completed && !s.user_controlled) + { + start(tc, false); + + if (tc->getStats().stopped_by_error) + { + tc->setPriority(0); + continue; + } + } + + ++counter; + } + + counter = 0; + + for (Uint32 i = 0; counter < max_qm_seeds && i < seed_queue.count(); ++i) + { + TorrentInterface* tc = seed_queue.at(i); + + const TorrentStats & s = tc->getStats(); + + if (!s.running && s.completed && !s.user_controlled) + { + start(tc, false); + + if (tc->getStats().stopped_by_error) + { + tc->setPriority(0); + continue; + } + } + + ++counter; + } + } + else + { + //no limits at all + + for (it = downloads.begin(); it != downloads.end(); ++it) + { + TorrentInterface* tc = *it; + + const TorrentStats & s = tc->getStats(); + + if (!s.running && !s.user_controlled && !s.stopped_by_error && !tc->isMovingFiles()) + { + start(tc, false); + if (tc->getStats().stopped_by_error) + tc->setPriority(0); + } + } + } + + } + + void QueueManager::torrentFinished(kt::TorrentInterface* tc) + { + //dequeue this tc + tc->setPriority(0); + //make sure the max_seeds is not reached +// if(max_seeds !=0 && max_seeds < getNumRunning(false,true)) +// tc->stop(true); + + if (keep_seeding) + torrentAdded(tc, false, false); + else + stop(tc,true); + + orderQueue(); + } + + void QueueManager::torrentAdded(kt::TorrentInterface* tc, bool user, bool start_torrent) + { + if (!user) + { + QPtrList<TorrentInterface>::const_iterator it = downloads.begin(); + + while (it != downloads.end()) + { + TorrentInterface* _tc = *it; + int p = _tc->getPriority(); + + if (p == 0) + break; + else + _tc->setPriority(++p); + + ++it; + } + + tc->setPriority(1); + } + else + { + tc->setPriority(0); + if(start_torrent) + start(tc, true); + } + + orderQueue(); + } + + void QueueManager::torrentRemoved(kt::TorrentInterface* tc) + { + remove(tc); + + orderQueue(); + } + + void QueueManager::setPausedState(bool pause) + { + paused_state = pause; + if (!pause) + { + std::set<kt::TorrentInterface*>::iterator it = paused_torrents.begin(); + while (it != paused_torrents.end()) + { + TorrentInterface* tc = *it; + startSafely(tc); + it++; + } + + paused_torrents.clear(); + orderQueue(); + } + else + { + QPtrList<TorrentInterface>::const_iterator it = downloads.begin(); + for (; it != downloads.end(); it++) + { + TorrentInterface* tc = *it; + + const TorrentStats & s = tc->getStats(); + + if (s.running) + { + paused_torrents.insert(tc); + stopSafely(tc, false); + } + } + } + } + + void QueueManager::enqueue(kt::TorrentInterface* tc) + { + //if a seeding torrent reached its maximum share ratio or maximum seed time don't enqueue it... + if (tc->getStats().completed && (tc->overMaxRatio() || tc->overMaxSeedTime())) + { + Out(SYS_GEN | LOG_IMPORTANT) << "Torrent has reached max share ratio or max seed time and cannot be started automatically." << endl; + emit queuingNotPossible(tc); + return; + } + + torrentAdded(tc, false, false); + } + + void QueueManager::dequeue(kt::TorrentInterface* tc) + { + int tp = tc->getPriority(); + bool completed = tc->getStats().completed; + QPtrList<TorrentInterface>::const_iterator it = downloads.begin(); + + while (it != downloads.end()) + { + TorrentInterface* _tc = *it; + bool _completed = _tc->getStats().completed; + + if (tc == _tc || (_completed != completed)) + { + ++it; + continue; + } + + int p = _tc->getPriority(); + + if (p < tp) + break; + else + _tc->setPriority(--p); + + ++it; + } + + tc->setPriority(0); + + orderQueue(); + } + + void QueueManager::queue(kt::TorrentInterface* tc) + { + if (tc->getPriority() == 0) + enqueue(tc); + else + dequeue(tc); + } + + void QueueManager::startSafely(kt::TorrentInterface* tc) + { + try + { + tc->start(); + } + catch (bt::Error & err) + { + const TorrentStats & s = tc->getStats(); + + QString msg = + i18n("Error starting torrent %1 : %2") + .arg(s.torrent_name).arg(err.toString()); + + KMessageBox::error(0, msg, i18n("Error")); + } + } + + void QueueManager::stopSafely(kt::TorrentInterface* tc, bool user, WaitJob* wjob) + { + try + { + tc->stop(user, wjob); + } + catch (bt::Error & err) + { + const TorrentStats & s = tc->getStats(); + + QString msg = + i18n("Error stopping torrent %1 : %2") + .arg(s.torrent_name).arg(err.toString()); + + KMessageBox::error(0, msg, i18n("Error")); + } + } + + void QueueManager::onLowDiskSpace(kt::TorrentInterface* tc, bool toStop) + { + if(toStop) + { + stop(tc, false); + } + + //then emit the signal to inform trayicon to show passive popup + emit lowDiskSpace(tc, toStop); + } + + void QueueManager::torrentStopped(kt::TorrentInterface* tc ) + { + orderQueue(); + } + + ///////////////////////////////////////////////////////////////////////////////////////////// + + + QueuePtrList::QueuePtrList() : QPtrList<kt::TorrentInterface>() + {} + + QueuePtrList::~QueuePtrList() + {} + + int QueuePtrList::compareItems(QPtrCollection::Item item1, QPtrCollection::Item item2) + { + kt::TorrentInterface* tc1 = (kt::TorrentInterface*) item1; + kt::TorrentInterface* tc2 = (kt::TorrentInterface*) item2; + + if (tc1->getPriority() == tc2->getPriority()) + return 0; + + if (tc1->getPriority() == 0 && tc2->getPriority() != 0) + return 1; + else if (tc1->getPriority() != 0 && tc2->getPriority() == 0) + return -1; + + return tc1->getPriority() > tc2->getPriority() ? -1 : 1; + + return 0; + } +} + +#include "queuemanager.moc" diff --git a/libktorrent/torrent/queuemanager.h b/libktorrent/torrent/queuemanager.h new file mode 100644 index 0000000..658c252 --- /dev/null +++ b/libktorrent/torrent/queuemanager.h @@ -0,0 +1,173 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * ivasic@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef QUEUEMANAGER_H +#define QUEUEMANAGER_H + +#include <set> +#include <qobject.h> +#include <qptrlist.h> + +#include <interfaces/torrentinterface.h> + +namespace kt +{ + class TrackersList; +} + +namespace bt +{ + class SHA1Hash; + class AnnounceList; + struct TrackerTier; + class WaitJob; + + class QueuePtrList : public QPtrList<kt::TorrentInterface> + { + public: + QueuePtrList(); + virtual ~QueuePtrList(); + + protected: + int compareItems(QPtrCollection::Item item1, QPtrCollection::Item item2); + }; + + /** + * @author Ivan Vasic + * @brief This class contains list of all TorrentControls and is responsible for starting/stopping them + */ + class QueueManager : public QObject + { + Q_OBJECT + + public: + QueueManager(); + virtual ~QueueManager(); + + void append(kt::TorrentInterface* tc); + void remove(kt::TorrentInterface* tc); + void clear(); + + kt::TorrentStartResponse start(kt::TorrentInterface* tc, bool user = true); + void stop(kt::TorrentInterface* tc, bool user = false); + + void stopall(int type); + void startall(int type); + + /** + * Stop all running torrents + * @param wjob WaitJob which waits for stopped events to reach the tracker + */ + void onExit(WaitJob* wjob); + + /** + * Enqueue/Dequeue function. Places a torrent in queue. + * If the torrent is already in queue this will remove it from queue. + * @param tc TorrentControl pointer. + */ + void queue(kt::TorrentInterface* tc); + + int count() { return downloads.count(); } + int countDownloads(); + int countSeeds(); + + int getNumRunning(bool onlyDownload = false, bool onlySeed = false); + int getNumRunning(bool userControlled, bool onlyDownloads, bool onlySeeds); + + void startNext(); + + typedef QPtrList<kt::TorrentInterface>::iterator iterator; + + iterator begin(); + iterator end(); + + /** + * See if we already loaded a torrent. + * @param ih The info hash of a torrent + * @return true if we do, false if we don't + */ + bool allreadyLoaded(const SHA1Hash & ih) const; + + + /** + * Merge announce lists to a torrent + * @param ih The info_hash of the torrent to merge to + * @param trk First tier of trackers + */ + void mergeAnnounceList(const SHA1Hash & ih,const TrackerTier* trk); + + void setMaxDownloads(int m); + void setMaxSeeds(int m); + + void setKeepSeeding(bool ks); + + /** + * Sets global paused state for QueueManager and stopps all running torrents. + * No torrents will be automatically started/stopped with QM. + */ + void setPausedState(bool pause); + + /** + * Places all torrents from downloads in the right order in queue. + * Use this when torrent priorities get changed + */ + void orderQueue(); + + signals: + /** + * User tried to enqueue a torrent that has reached max share ratio. It's not possible. + * Signal should be connected to SysTray slot which shows appropriate KPassivePopup info. + * @param tc The torrent in question. + */ + void queuingNotPossible(kt::TorrentInterface* tc); + + /** + * Diskspace is running low. + * Signal should be connected to SysTray slot which shows appropriate KPassivePopup info. + * @param tc The torrent in question. + */ + void lowDiskSpace(kt::TorrentInterface* tc, bool stopped); + + public slots: + void torrentFinished(kt::TorrentInterface* tc); + void torrentAdded(kt::TorrentInterface* tc, bool user, bool start_torrent); + void torrentRemoved(kt::TorrentInterface* tc); + void torrentStopped(kt::TorrentInterface* tc); + void onLowDiskSpace(kt::TorrentInterface* tc, bool toStop); + + private: + void enqueue(kt::TorrentInterface* tc); + void dequeue(kt::TorrentInterface* tc); + void startSafely(kt::TorrentInterface* tc); + void stopSafely(kt::TorrentInterface* tc,bool user,WaitJob* wjob = 0); + + bt::QueuePtrList downloads; + std::set<kt::TorrentInterface*> paused_torrents; + + int max_downloads; + int max_seeds; + + + bool paused_state; + bool keep_seeding; + bool exiting; + }; +} +#endif diff --git a/libktorrent/torrent/request.cpp b/libktorrent/torrent/request.cpp new file mode 100644 index 0000000..413eb11 --- /dev/null +++ b/libktorrent/torrent/request.cpp @@ -0,0 +1,52 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include "request.h" + +namespace bt +{ + Request::Request() : index(0),off(0),len(0),peer(0) + {} + + Request::Request(Uint32 index,Uint32 off,Uint32 len,Uint32 peer) + : index(index),off(off),len(len),peer(peer) + {} + + Request::Request(const Request & r) + : index(r.index),off(r.off),len(r.len),peer(r.peer) + {} + + Request::~Request() + {} + + + Request & Request::operator = (const Request & r) + { + index = r.index; + off = r.off; + len = r.len; + peer = r.peer; + return *this; + } + + bool operator == (const Request & a,const Request & b) + { + return a.index == b.index && a.len == b.len && a.off == b.off; + } +} diff --git a/libktorrent/torrent/request.h b/libktorrent/torrent/request.h new file mode 100644 index 0000000..aeeff78 --- /dev/null +++ b/libktorrent/torrent/request.h @@ -0,0 +1,96 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTREQUEST_H +#define BTREQUEST_H + +#include "globals.h" + + +namespace bt +{ + + /** + * @author Joris Guisson + * @brief Request of a piece sent to other peers + * + * This class keeps track of a request of a piece. + * The Request consists of an index (the index of the chunk), + * offset into the chunk and the length of a piece. + * + * The PeerID of the Peer who sent the request is also kept. + */ + class Request + { + public: + /** + * Constructor, set everything to 0. + */ + Request(); + + /** + * Constructor, set the index, offset,length and peer + * @param index The index of the chunk + * @param off The offset into the chunk + * @param len The length of the piece + * @param peer The ID of the Peer who sent the request + */ + Request(Uint32 index,Uint32 off,Uint32 len,Uint32 peer); + + /** + * Copy constructor. + * @param r Request to copy + */ + Request(const Request & r); + virtual ~Request(); + + /// Get the index of the chunk + Uint32 getIndex() const {return index;} + + /// Get the offset into the chunk + Uint32 getOffset() const {return off;} + + /// Get the length of a the piece + Uint32 getLength() const {return len;} + + /// Get the sending Peer + Uint32 getPeer() const {return peer;} + + /** + * Assignmenth operator. + * @param r The Request to copy + */ + Request & operator = (const Request & r); + + /** + * Compare two requests. Return true if they are the same. + * This only compares the index,offset and length. + * @param a The first request + * @param b The second request + * @return true if they are equal + */ + friend bool operator == (const Request & a,const Request & b); + private: + Uint32 index,off,len; + Uint32 peer; + }; + +} + +#endif diff --git a/libktorrent/torrent/server.cpp b/libktorrent/torrent/server.cpp new file mode 100644 index 0000000..e3d00ae --- /dev/null +++ b/libktorrent/torrent/server.cpp @@ -0,0 +1,200 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include <qserversocket.h> +#include <mse/streamsocket.h> +#include <util/sha1hash.h> +#include <util/log.h> +#include <net/portlist.h> +#include <mse/encryptedserverauthenticate.h> +#include "globals.h" +#include "torrent.h" +#include "server.h" +#include "peermanager.h" +#include "serverauthenticate.h" +#include "ipblocklist.h" +#include "authenticationmonitor.h" + + +namespace bt +{ + + class ServerSocket : public QServerSocket + { + Server* srv; + public: + ServerSocket(Server* srv,Uint16 port) : QServerSocket(port),srv(srv) + { + QSocketDevice* sd = socketDevice(); + if (sd) + sd->setAddressReusable(true); + } + + virtual ~ServerSocket() + {} + + virtual void newConnection(int socket) + { + srv->newConnection(socket); + } + }; + + + + + Server::Server(Uint16 port) : sock(0),port(0) + { + changePort(port); + encryption = false; + allow_unencrypted = true; + } + + + Server::~Server() + { + delete sock; + } + + bool Server::isOK() const + { + return sock->ok(); + } + + void Server::changePort(Uint16 p) + { + if (p == port) + return; + + + if (sock && sock->ok()) + Globals::instance().getPortList().removePort(port,net::TCP); + + port = p; + delete sock; + sock = new ServerSocket(this,port); + if (isOK()) + Globals::instance().getPortList().addNewPort(port,net::TCP,true); + } + + void Server::addPeerManager(PeerManager* pman) + { + peer_managers.append(pman); + } + + void Server::removePeerManager(PeerManager* pman) + { + peer_managers.remove(pman); + } + + void Server::newConnection(int socket) + { + mse::StreamSocket* s = new mse::StreamSocket(socket); + if (peer_managers.count() == 0) + { + s->close(); + delete s; + } + else + { + IPBlocklist& ipfilter = IPBlocklist::instance(); + QString IP(s->getRemoteIPAddress()); + if (ipfilter.isBlocked( IP )) + { + delete s; + return; + } + + ServerAuthenticate* auth = 0; + + if (encryption) + auth = new mse::EncryptedServerAuthenticate(s,this); + else + auth = new ServerAuthenticate(s,this); + + AuthenticationMonitor::instance().add(auth); + } + } + + void Server::close() + { + delete sock; + sock= 0; + } + + Uint16 Server::getPortInUse() const + { + return port; + } + + PeerManager* Server::findPeerManager(const SHA1Hash & hash) + { + QPtrList<PeerManager>::iterator i = peer_managers.begin(); + while (i != peer_managers.end()) + { + PeerManager* pm = *i; + if (pm && pm->getTorrent().getInfoHash() == hash) + { + if (!pm->isStarted()) + return 0; + else + return pm; + } + i++; + } + return 0; + } + + bool Server::findInfoHash(const SHA1Hash & skey,SHA1Hash & info_hash) + { + Uint8 buf[24]; + memcpy(buf,"req2",4); + QPtrList<PeerManager>::iterator i = peer_managers.begin(); + while (i != peer_managers.end()) + { + PeerManager* pm = *i; + memcpy(buf+4,pm->getTorrent().getInfoHash().getData(),20); + if (SHA1Hash::generate(buf,24) == skey) + { + info_hash = pm->getTorrent().getInfoHash(); + return true; + } + i++; + } + return false; + } + + void Server::onError(int) + { + } + + + void Server::enableEncryption(bool allow_unencrypted) + { + encryption = true; + this->allow_unencrypted = allow_unencrypted; + } + + void Server::disableEncryption() + { + encryption = false; + } +} + +#include "server.moc" diff --git a/libktorrent/torrent/server.h b/libktorrent/torrent/server.h new file mode 100644 index 0000000..99c06eb --- /dev/null +++ b/libktorrent/torrent/server.h @@ -0,0 +1,125 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTSERVER_H +#define BTSERVER_H + +#include <qptrlist.h> +#include <qobject.h> +#include "globals.h" + +namespace bt +{ + class PeerManager; + class ServerAuthenticate; + class SHA1Hash; + class ServerSocket; + + + /** + * @author Joris Guisson + * + * Class which listens for incoming connections. + * Handles authentication and then hands of the new + * connections to a PeerManager. + * + * All PeerManager's should register with this class when they + * are created and should unregister when they are destroyed. + */ + class Server : public QObject + { + Q_OBJECT + + QPtrList<PeerManager> peer_managers; + ServerSocket* sock; + Uint16 port; + bool encryption; + bool allow_unencrypted; + public: + Server(Uint16 port); + virtual ~Server(); + + /// Check if everything is ok (are we successfully listening on the port) + bool isOK() const; + + /** + * Change the port. + * @param port The new port + */ + void changePort(Uint16 port); + + /// Get the port in use + Uint16 getPortInUse() const; + + /** + * Add a PeerManager. + * @param pman The PeerManager + */ + void addPeerManager(PeerManager* pman); + + /** + * Remove a PeerManager. + * @param pman The PeerManager + */ + void removePeerManager(PeerManager* pman); + + /** + * Find the PeerManager given the info_hash of it's torrent. + * @param hash The info_hash + * @return The PeerManager or 0 if one can't be found + */ + PeerManager* findPeerManager(const SHA1Hash & hash); + + /** + * Find the info_hash based on the skey hash. The skey hash is a hash + * of 'req2' followed by the info_hash. This function finds the info_hash + * which matches the skey hash. + * @param skey HASH('req2',info_hash) + * @param info_hash which matches + * @return true If one was found + */ + bool findInfoHash(const SHA1Hash & skey,SHA1Hash & info_hash); + + /** + * Enable encryption. + * @param allow_unencrypted Allow unencrypted connections (if encryption fails) + */ + void enableEncryption(bool allow_unencrypted); + + /** + * Disable encrypted authentication. + */ + void disableEncryption(); + + bool isEncryptionEnabled() const {return encryption;} + bool unencryptedConnectionsAllowed() const {return allow_unencrypted;} + + void close(); + + private slots: + void newConnection(int sock); + void onError(int); + + private: + friend class ServerSocket; + }; + +} + +#endif diff --git a/libktorrent/torrent/serverauthenticate.cpp b/libktorrent/torrent/serverauthenticate.cpp new file mode 100644 index 0000000..479f0ce --- /dev/null +++ b/libktorrent/torrent/serverauthenticate.cpp @@ -0,0 +1,126 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <mse/streamsocket.h> +#include <util/sha1hash.h> +#include <util/log.h> +#include <util/log.h> +#include "globals.h" +#include "server.h" +#include "peermanager.h" +#include "serverauthenticate.h" +#include "peerid.h" +#include "torrent.h" +#include "ipblocklist.h" + + +namespace bt +{ + bool ServerAuthenticate::s_firewalled = true; + + + ServerAuthenticate::ServerAuthenticate(mse::StreamSocket* sock,Server* server) + : AuthenticateBase(sock),server(server) + { + } + + + ServerAuthenticate::~ServerAuthenticate() + { + } + + + void ServerAuthenticate::onFinish(bool succes) + { + Out(SYS_CON|LOG_NOTICE) << "Authentication(S) to " << sock->getRemoteIPAddress() + << " : " << (succes ? "ok" : "failure") << endl; + finished = true; + setFirewalled(false); + + if (!succes) + { + sock->deleteLater(); + sock = 0; + } + + timer.stop(); + } + + void ServerAuthenticate::handshakeRecieved(bool full) + { + Uint8* hs = handshake; + IPBlocklist& ipfilter = IPBlocklist::instance(); + + QString IP = sock->getRemoteIPAddress(); + + if (ipfilter.isBlocked( IP )) + { + onFinish(false); + return; + } + + // try to find a PeerManager which has te right info hash + SHA1Hash rh(hs+28); + PeerManager* pman = server->findPeerManager(rh); + if (!pman) + { + Out(SYS_GEN|LOG_DEBUG) << "Cannot find PeerManager for hash : " << rh.toString() << endl; + onFinish(false); + return; + } + + if (full) + { + // check if we aren't connecting to ourself + char tmp[21]; + tmp[20] = '\0'; + memcpy(tmp,hs+48,20); + PeerID peer_id = PeerID(tmp); + if (pman->getTorrent().getPeerID() == peer_id) + { + Out(SYS_CON|LOG_NOTICE) << "Lets not connect to our self" << endl; + onFinish(false); + return; + } + + // check if we aren't already connected to the client + if (pman->connectedTo(peer_id)) + { + Out(SYS_CON|LOG_NOTICE) << "Already connected to " << peer_id.toString() << endl; + onFinish(false); + return; + } + + + // send handshake and finish off + sendHandshake(rh,pman->getTorrent().getPeerID()); + onFinish(true); + // hand over connection + pman->newConnection(sock,peer_id,supportedExtensions()); + sock = 0; + } + else + { + // if the handshake wasn't fully received just send our handshake + sendHandshake(rh,pman->getTorrent().getPeerID()); + } + } +} + +#include "serverauthenticate.moc" diff --git a/libktorrent/torrent/serverauthenticate.h b/libktorrent/torrent/serverauthenticate.h new file mode 100644 index 0000000..70d3739 --- /dev/null +++ b/libktorrent/torrent/serverauthenticate.h @@ -0,0 +1,72 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTSERVERAUTHENTICATE_H +#define BTSERVERAUTHENTICATE_H + +#include "authenticatebase.h" + +namespace bt +{ + class Server; + class SHA1Hash; + class PeerID; + + /** + * @author Joris Guisson + * + * Handles the authentication of incoming connections on the Server. + * Once the authentication is finished, the socket gets handed over + * to the right PeerManager. + */ + class ServerAuthenticate : public AuthenticateBase + { + Q_OBJECT + public: + ServerAuthenticate(mse::StreamSocket* sock,Server* server); + virtual ~ServerAuthenticate(); + + static bool isFirewalled(); + static void setFirewalled(bool Firewalled); + + protected: + void onFinish(bool succes); + void handshakeRecieved(bool full); + + protected: + Server* server; + + private: + static bool s_firewalled; + }; + +} + +inline bool bt::ServerAuthenticate::isFirewalled() +{ + return s_firewalled; +} + +inline void bt::ServerAuthenticate::setFirewalled(bool Firewalled) +{ + s_firewalled = Firewalled; +} + + +#endif diff --git a/libktorrent/torrent/singlefilecache.cpp b/libktorrent/torrent/singlefilecache.cpp new file mode 100644 index 0000000..7d31bef --- /dev/null +++ b/libktorrent/torrent/singlefilecache.cpp @@ -0,0 +1,232 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <klocale.h> +#include <qfileinfo.h> +#include <qstringlist.h> +#include <util/fileops.h> +#include <util/error.h> +#include <util/functions.h> +#include <util/log.h> +#include "torrent.h" +#include "chunk.h" +#include "globals.h" +#include "cachefile.h" +#include "singlefilecache.h" +#include "preallocationthread.h" + + +namespace bt +{ + + SingleFileCache::SingleFileCache(Torrent& tor,const QString & tmpdir,const QString & datadir) + : Cache(tor,tmpdir,datadir),fd(0) + { + cache_file = tmpdir + "cache"; + output_file = QFileInfo(cache_file).readLink(); + } + + + SingleFileCache::~SingleFileCache() + {} + + void SingleFileCache::changeTmpDir(const QString & ndir) + { + Cache::changeTmpDir(ndir); + cache_file = tmpdir + "cache"; + } + + KIO::Job* SingleFileCache::moveDataFiles(const QString & ndir) + { + return KIO::move(KURL::fromPathOrURL(output_file),KURL::fromPathOrURL(ndir)); + } + + void SingleFileCache::moveDataFilesCompleted(KIO::Job* /*job*/) + { + } + + void bt::SingleFileCache::changeOutputPath(const QString & outputpath) + { + bt::Delete(cache_file); + output_file = outputpath; + datadir = output_file.left(output_file.findRev(bt::DirSeparator())); + + bt::SymLink(output_file, cache_file); + } + + bool SingleFileCache::prep(Chunk* c) + { + if (mmap_failures >= 3) + { + // mmap continuously fails, so stop using it + c->allocate(); + c->setStatus(Chunk::BUFFERED); + } + else + { + Uint64 off = c->getIndex() * tor.getChunkSize(); + Uint8* buf = (Uint8*)fd->map(c,off,c->getSize(),CacheFile::RW); + if (!buf) + { + mmap_failures++; + // buffer it if mmapping fails + Out(SYS_GEN|LOG_IMPORTANT) << "Warning : mmap failure, falling back to buffered mode" << endl; + c->allocate(); + c->setStatus(Chunk::BUFFERED); + } + else + { + c->setData(buf,Chunk::MMAPPED); + } + } + return true; + } + + void SingleFileCache::load(Chunk* c) + { + Uint64 off = c->getIndex() * tor.getChunkSize(); + Uint8* buf = 0; + if (mmap_failures >= 3 || !(buf = (Uint8*)fd->map(c,off,c->getSize(),CacheFile::READ))) + { + c->allocate(); + c->setStatus(Chunk::BUFFERED); + fd->read(c->getData(),c->getSize(),off); + if (mmap_failures < 3) + mmap_failures++; + } + else + { + c->setData(buf,Chunk::MMAPPED); + } + } + + void SingleFileCache::save(Chunk* c) + { + // unmap the chunk if it is mapped + if (c->getStatus() == Chunk::MMAPPED) + { + fd->unmap(c->getData(),c->getSize()); + c->clear(); + c->setStatus(Chunk::ON_DISK); + } + else if (c->getStatus() == Chunk::BUFFERED) + { + Uint64 off = c->getIndex() * tor.getChunkSize(); + fd->write(c->getData(),c->getSize(),off); + c->clear(); + c->setStatus(Chunk::ON_DISK); + } + } + + void SingleFileCache::create() + { + QFileInfo fi(cache_file); + if (!fi.exists()) + { + QString out_file = fi.readLink(); + + if (out_file.isNull()) + out_file = datadir + tor.getNameSuggestion(); + + if (!bt::Exists(out_file)) + bt::Touch(out_file); + else + preexisting_files = true; + + if (bt::Exists(cache_file)) + bt::Delete(cache_file); + + bt::SymLink(out_file,cache_file); + output_file = out_file; + } + else + { + QString out_file = fi.readLink(); + if (!bt::Exists(out_file)) + bt::Touch(out_file); + else + preexisting_files = true; + } + } + + void SingleFileCache::close() + { + if (fd) + { + fd->close(); + delete fd; + fd = 0; + } + } + + void SingleFileCache::open() + { + if (fd) + return; + + try + { + fd = new CacheFile(); + fd->open(cache_file,tor.getFileLength()); + } + catch (...) + { + fd->close(); + delete fd; + fd = 0; + throw; + } + } + + void SingleFileCache::preallocateDiskSpace(PreallocationThread* prealloc) + { + if (!fd) + open(); + + if (!prealloc->isStopped()) + fd->preallocate(prealloc); + else + prealloc->setNotFinished(); + } + + bool SingleFileCache::hasMissingFiles(QStringList & sl) + { + QFileInfo fi(cache_file); + if (!fi.exists()) + { + QString out_file = fi.readLink(); + sl.append(fi.readLink()); + return true; + } + return false; + } + + void SingleFileCache::deleteDataFiles() + { + bt::Delete(output_file); + } + + Uint64 SingleFileCache::diskUsage() + { + if (!fd) + open(); + + return fd->diskUsage(); + } +} diff --git a/libktorrent/torrent/singlefilecache.h b/libktorrent/torrent/singlefilecache.h new file mode 100644 index 0000000..faa71b6 --- /dev/null +++ b/libktorrent/torrent/singlefilecache.h @@ -0,0 +1,64 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTSINGLEFILECACHE_H +#define BTSINGLEFILECACHE_H + +#include "cache.h" + +namespace bt +{ + class CacheFile; + + + /** + * @author Joris Guisson + * @brief Cache for single file torrents + * + * This class implements Cache for a single file torrent + */ + class SingleFileCache : public Cache + { + QString cache_file; + QString output_file; + CacheFile* fd; + public: + SingleFileCache(Torrent& tor,const QString & tmpdir,const QString & datadir); + virtual ~SingleFileCache(); + + virtual bool prep(Chunk* c); + virtual void load(Chunk* c); + virtual void save(Chunk* c); + virtual void create(); + virtual void close(); + virtual void open(); + virtual void changeTmpDir(const QString & ndir); + virtual KIO::Job* moveDataFiles(const QString & ndir); + virtual void moveDataFilesCompleted(KIO::Job* job); + virtual void changeOutputPath(const QString& outputpath); + virtual QString getOutputPath() const {return output_file;} + virtual void preallocateDiskSpace(PreallocationThread* prealloc); + virtual bool hasMissingFiles(QStringList & sl); + virtual void deleteDataFiles(); + virtual Uint64 diskUsage(); + }; + +} + +#endif diff --git a/libktorrent/torrent/speedestimater.cpp b/libktorrent/torrent/speedestimater.cpp new file mode 100644 index 0000000..f12b5ac --- /dev/null +++ b/libktorrent/torrent/speedestimater.cpp @@ -0,0 +1,105 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <qpair.h> +#include <qvaluelist.h> +#include <util/log.h> +#include <util/timer.h> +#include "speedestimater.h" +#include <util/functions.h> + +namespace bt +{ + class SpeedEstimater::SpeedEstimaterPriv + { + float rate; + QValueList<QPair<Uint32,TimeStamp> > dlrate; + public: + SpeedEstimaterPriv() : rate(0) {} + ~SpeedEstimaterPriv() {} + + void data(Uint32 bytes) + { + dlrate.append(qMakePair(bytes,GetCurrentTime())); + } + + void update() + { + TimeStamp now = GetCurrentTime(); + + Uint32 bytes = 0,oldest = now; + QValueList<QPair<Uint32,TimeStamp> >::iterator i = dlrate.begin(); + while (i != dlrate.end()) + { + QPair<Uint32,TimeStamp> & p = *i; + if (now - p.second > 3000) + { + i = dlrate.erase(i); + } + else + { + if (p.second < oldest) + oldest = p.second; + + bytes += p.first; + i++; + } + } + + Uint32 d = 3000; + + if (bytes == 0) + { + rate = 0; + } + else + { + // Out() << "bytes = " << bytes << " d = " << d << endl; + rate = (float) bytes / (d * 0.001f); + } + } + + float getRate() const {return rate;} + }; + + SpeedEstimater::SpeedEstimater() + { + download_rate = 0; + down = new SpeedEstimaterPriv(); + } + + + SpeedEstimater::~SpeedEstimater() + { + delete down; + } + + + + void SpeedEstimater::onRead(Uint32 bytes) + { + down->data(bytes); + } + + void SpeedEstimater::update() + { + down->update(); + download_rate = down->getRate(); + } +} diff --git a/libktorrent/torrent/speedestimater.h b/libktorrent/torrent/speedestimater.h new file mode 100644 index 0000000..16bbdcc --- /dev/null +++ b/libktorrent/torrent/speedestimater.h @@ -0,0 +1,55 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTSPEEDESTIMATER_H +#define BTSPEEDESTIMATER_H + + +#include "globals.h" + +namespace bt +{ + + /** + * @author Joris Guisson + * @brief Estimates download speed + * + * This class estimates the download speed. + */ + class SpeedEstimater + { + class SpeedEstimaterPriv; + public: + SpeedEstimater(); + virtual ~SpeedEstimater(); + + + void onRead(Uint32 bytes); + void update(); + + double downloadRate() const {return download_rate;} + + private: + double download_rate; + SpeedEstimaterPriv* down; + }; + +} + +#endif diff --git a/libktorrent/torrent/statsfile.cpp b/libktorrent/torrent/statsfile.cpp new file mode 100644 index 0000000..2ffd3ae --- /dev/null +++ b/libktorrent/torrent/statsfile.cpp @@ -0,0 +1,120 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include "statsfile.h" + +#include "globals.h" +#include <util/log.h> +#include <util/functions.h> + +#include <qstring.h> +#include <qfile.h> +#include <qtextstream.h> + +namespace bt +{ + + StatsFile::StatsFile(QString filename) + :m_filename(filename) + { + m_file.setName(filename); + readSync(); + } + + StatsFile::~StatsFile() + { + close(); + } + + void StatsFile::close() + { + m_file.close(); + } + + void StatsFile::write(QString key, QString value) + { + m_values.insert(key.stripWhiteSpace(), value.stripWhiteSpace()); + } + + QString StatsFile::readString(QString key) + { + return m_values[key].stripWhiteSpace(); + } + + Uint64 StatsFile::readUint64(QString key) + { + bool ok = true; + Uint64 val = readString(key).toULongLong(&ok); + return val; + } + + int StatsFile::readInt(QString key) + { + bool ok = true; + int val = readString(key).toInt(&ok); + return val; + } + + bool StatsFile::readBoolean(QString key) + { + return (bool) readInt(key); + } + + unsigned long StatsFile::readULong(QString key) + { + bool ok = true; + return readString(key).toULong(&ok); + } + + float bt::StatsFile::readFloat( QString key ) + { + bool ok = true; + return readString(key).toFloat(&ok); + } + + void StatsFile::readSync() + { + if (!m_file.open(IO_ReadOnly)) + return; + + QTextStream in(&m_file); + while (!in.atEnd()) + { + QString line = in.readLine(); + QString tmp = line.left(line.find('=')); + m_values.insert(tmp, line.mid(tmp.length()+1)); + } + close(); + } + + void StatsFile::writeSync() + { + if (!m_file.open(IO_WriteOnly)) + return; + QTextStream out(&m_file); + QMap<QString, QString>::iterator it = m_values.begin(); + while(it!=m_values.end()) + { + out << it.key() << "=" << it.data() << ::endl; + ++it; + } + close(); + } + +} diff --git a/libktorrent/torrent/statsfile.h b/libktorrent/torrent/statsfile.h new file mode 100644 index 0000000..9f7a145 --- /dev/null +++ b/libktorrent/torrent/statsfile.h @@ -0,0 +1,91 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTSTATSFILE_H +#define BTSTATSFILE_H + +#include <qstring.h> +#include <qfile.h> +#include <qmap.h> + +#include <util/constants.h> + +namespace bt +{ + + /** + * @brief This class is used for loading/storing torrent stats in a file. + * @author Ivan Vasic <ivasic@gmail.com> + */ + class StatsFile + { + public: + /** + * @brief A constructor. + * Constructs StatsFile object and calls readSync(). + */ + StatsFile(QString filename); + ~StatsFile(); + + ///Closes QFile + void close(); + + /** + * @brief Main read function. + * @return QString value that correspodents to key. + * @param key - QString stats key. + */ + QString readString(QString key); + + Uint64 readUint64(QString key); + bool readBoolean(QString key); + int readInt(QString key); + unsigned long readULong(QString key); + float readFloat(QString key); + + /** + * @brief Writes key and value. + * It only inserts pair of key/value to the m_values. To make changes to file call writeSync(). + * @param key - QString key + * @param value - QString value. + */ + void write(QString key, QString value); + + ///Reads data from stats file to m_values. + void readSync(); + + ///Writes data from m_values to stats file. + void writeSync(); + + /** + * See if there is a key in the stats file + * @param key The key + * @return true if key is in the stats file + */ + bool hasKey(const QString & key) const {return m_values.contains(key);} + + private: + QString m_filename; + QFile m_file; + + QMap<QString, QString> m_values; + }; +} + +#endif diff --git a/libktorrent/torrent/timeestimator.cpp b/libktorrent/torrent/timeestimator.cpp new file mode 100644 index 0000000..7d18300 --- /dev/null +++ b/libktorrent/torrent/timeestimator.cpp @@ -0,0 +1,278 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ivan Vasić * + * ivasic@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <math.h> +#include "timeestimator.h" +#include "torrentcontrol.h" +#include "settings.h" + +#include <torrent/globals.h> +#include <util/log.h> +#include <util/constants.h> + +using namespace kt; + +namespace bt +{ + TimeEstimator::TimeEstimator(TorrentControl* tc) + : m_tc(tc) + { + m_samples = new SampleQueue(20); + m_lastAvg = 0; + m_perc = -1; + + //default is KT algorithm + m_algorithm = (ETAlgorithm) Settings::eta(); + } + + + TimeEstimator::~TimeEstimator() + { + delete m_samples; + } + + Uint32 TimeEstimator::estimate() + { + const TorrentStats& s = m_tc->getStats(); + + // only estimate when we are downloading or stalled + if (!(s.status == kt::DOWNLOADING || s.status == kt::STALLED)) + return (Uint32) - 1; + + //ones without pre-calculations + switch (m_algorithm) + { + + case ETA_CSA: + return estimateCSA(); + + case ETA_GASA: + return estimateGASA(); + + case ETA_KT: + return estimateKT(); + } + + //complicated ones :) + + Uint32 sample = (Uint32) s.download_rate; + //push new sample + m_samples->push(sample); + + + switch (m_algorithm) + { + case ETA_MAVG: + return estimateMAVG(); + + case ETA_WINX: + return estimateWINX(); + + default: + return -1; + } + } + + Uint32 TimeEstimator::estimateCSA() + { + const TorrentStats& s = m_tc->getStats(); + + if (s.download_rate == 0) + return (Uint32) - 1; + + return (int)floor((float)s.bytes_left_to_download / (float)s.download_rate); + } + + Uint32 TimeEstimator::estimateGASA() + { + const TorrentStats& s = m_tc->getStats(); + + if (m_tc->getRunningTimeDL() > 0 && s.bytes_downloaded > 0) + { + double avg_speed = (double) s.bytes_downloaded / (double) m_tc->getRunningTimeDL(); + return (Uint32) floor((double) s.bytes_left_to_download / avg_speed); + } + + return (Uint32) - 1; + } + + Uint32 TimeEstimator::estimateWINX() + { + const TorrentStats& s = m_tc->getStats(); + + if (m_samples->sum() > 0 && m_samples->count() > 0) + return (Uint32) floor((double) s.bytes_left_to_download / ((double) m_samples->sum() / (double) m_samples->count())); + + return (Uint32) - 1; + } + + Uint32 TimeEstimator::estimateMAVG() + { + const TorrentStats& s = m_tc->getStats(); + + if (m_samples->count() > 0) + { + double lavg; + + if (m_lastAvg == 0) + lavg = (Uint32) m_samples->sum() / m_samples->count(); + else + lavg = m_lastAvg - ((double) m_samples->first() / (double) m_samples->count()) + ((double) m_samples->last() / (double) m_samples->count()); + + m_lastAvg = (Uint32) floor(lavg); + + if (lavg > 0) + return (Uint32) floor((double) s.bytes_left_to_download / ((lavg + (m_samples->sum() / m_samples->count())) / 2)); + + return (Uint32) - 1; + } + + return (Uint32) - 1; + } + +} + +bt::SampleQueue::SampleQueue(int max) + : m_size(max), m_count(0) +{ + m_samples = new Uint32[max]; + + for (int i = 0; i < m_size; ++i) + m_samples[i] = 0; + + m_end = -1; + + m_start = 0; +} + +bt::SampleQueue::~ SampleQueue() +{ + delete [] m_samples; +} + +void bt::SampleQueue::push(Uint32 sample) +{ + if (m_count < m_size) + { + //it's not full yet + m_samples[(++m_end) % m_size ] = sample; + m_count++; + + return; + } + + //since it's full I'll just replace the oldest value with new one and update all variables. + m_end = (++m_end) % m_size; + + m_start = (++m_start) % m_size; + + m_samples[m_end] = sample; +} + +Uint32 bt::SampleQueue::first() +{ + return m_samples[m_start]; +} + +Uint32 bt::SampleQueue::last() +{ + return m_samples[m_end]; +} + +bool bt::SampleQueue::isFull() +{ + return m_count >= m_size; +} + +int bt::SampleQueue::count() +{ + return m_count; +} + +Uint32 bt::SampleQueue::sum() +{ + Uint32 s = 0; + + for (int i = 0; i < m_count; ++i) + s += m_samples[i]; + + return s; +} + +void bt::TimeEstimator::setAlgorithm(const ETAlgorithm& theValue) +{ + m_algorithm = theValue; +} + +Uint32 bt::TimeEstimator::estimateKT() +{ + const TorrentStats& s = m_tc->getStats(); + + Uint32 sample = (Uint32) s.download_rate; + + //push new sample + m_samples->push(sample); + + double perc = (double) s.bytes_downloaded / (double) s.total_bytes; + + int percentage = (int)(perc) * 100; + + //calculate percentage increasement + double delta = 1 - 1 / (perc / m_perc); + + //remember last percentage + m_perc = perc; + + + if (s.bytes_downloaded < 1024*1024*100 && sample > 0) // < 100KB + { + m_lastETA = estimateGASA(); + return m_lastETA; + } + + if (percentage >= 99 && sample > 0 && s.bytes_left_to_download <= 10*1024*1024*1024) //1% of a very large torrent could be hundreds of MB so limit it to 10MB + { + + if (!m_samples->isFull()) + { + m_lastETA = estimateWINX(); + + if (m_lastETA == (Uint32) - 1) + m_lastETA = estimateGASA(); + + return m_lastETA; + } + else + { + m_lastETA = (Uint32) - 1; + + if (delta > 0.0001) + m_lastETA = estimateMAVG(); + + if (m_lastETA == (Uint32) - 1) + m_lastETA = estimateGASA(); + } + + return m_lastETA; + } + + m_lastETA = estimateGASA(); + + return m_lastETA; +} diff --git a/libktorrent/torrent/timeestimator.h b/libktorrent/torrent/timeestimator.h new file mode 100644 index 0000000..972e239 --- /dev/null +++ b/libktorrent/torrent/timeestimator.h @@ -0,0 +1,119 @@ +/*************************************************************************** + * Copyright (C) 2006 by Ivan Vasić * + * ivasic@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTTIMEESTIMATOR_H +#define BTTIMEESTIMATOR_H + +#include <util/constants.h> + +namespace bt +{ + class TorrentControl; + + /** + * Simple queue class for samples. Optimized for speed and size + * without posibility to dynamically resize itself. + * @author Ivan Vasic <ivasic@gmail.com> + */ + class SampleQueue + { + public: + SampleQueue(int max); + ~SampleQueue(); + + /** + * Inserts new sample into the queue. The oldest sample is overwritten. + */ + void push(Uint32 sample); + + Uint32 first(); + Uint32 last(); + + bool isFull(); + + /** + * This function will return the number of samples in queue until it counts m_size number of elements. + * After this point it will always return m_size since no samples are being deleted. + */ + int count(); + + /** + * Returns the sum of all samples. + */ + Uint32 sum(); + + private: + int m_size; + int m_count; + + int m_start; + int m_end; + + Uint32* m_samples; + }; + + /** + * ETA estimator class. It will use different algorithms for different download phases. + * @author Ivan Vasic <ivasic@gmail.com> + */ + class TimeEstimator + { + public: + + enum ETAlgorithm + { + ETA_KT, //ktorrent default algorithm - combination of the following according to our tests + ETA_CSA, //current speed algorithm + ETA_GASA, //global average speed algorithm + ETA_WINX, //window of X algorithm + ETA_MAVG //moving average algorithm + }; + + TimeEstimator(TorrentControl* tc); + ~TimeEstimator(); + + ///Returns ETA for m_tc torrent. + Uint32 estimate(); + + void setAlgorithm(const ETAlgorithm& theValue); + ETAlgorithm algorithm() const { return m_algorithm; } + + private: + + Uint32 estimateCSA(); + Uint32 estimateGASA(); + Uint32 estimateWINX(); + Uint32 estimateMAVG(); + Uint32 estimateKT(); + + TorrentControl* m_tc; + SampleQueue* m_samples; + + Uint32 m_lastAvg; + Uint32 m_lastETA; + + //last percentage + double m_perc; + + ETAlgorithm m_algorithm; + }; + +} + +#endif diff --git a/libktorrent/torrent/torrent.cpp b/libktorrent/torrent/torrent.cpp new file mode 100644 index 0000000..6b8739b --- /dev/null +++ b/libktorrent/torrent/torrent.cpp @@ -0,0 +1,449 @@ +/*************************************************************************** + * Copyright (C) 2005 by * + * Joris Guisson <joris.guisson@gmail.com> * + * Ivan Vasic <ivasic@gmail.com> * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <qfile.h> +#include <qdatastream.h> +#include <qstringlist.h> +#include <util/log.h> +#include <util/functions.h> +#include <util/error.h> +#include <util/sha1hashgen.h> +#include <time.h> +#include <stdlib.h> +#include "torrent.h" +#include "bdecoder.h" +#include "bnode.h" +#include "announcelist.h" + +#include <klocale.h> + +namespace bt +{ + + Torrent::Torrent() : piece_length(0),file_length(0),priv_torrent(false) + { + encoding = "utf8"; + trackers = 0; + } + + + Torrent::~Torrent() + { + delete trackers; + } + + + void Torrent::load(const QByteArray & data,bool verbose) + { + BNode* node = 0; + + try + { + BDecoder decoder(data,verbose); + node = decoder.decode(); + BDictNode* dict = dynamic_cast<BDictNode*>(node); + if (!dict) + throw Error(i18n("Corrupted torrent!")); + + // see if we can find an encoding node + BValueNode* enc = dict->getValue("encoding"); + if (enc) + { + encoding = enc->data().toString(); + Out() << "Encoding : " << encoding << endl; + } + + BValueNode* announce = dict->getValue("announce"); + BListNode* nodes = dict->getList("nodes"); + if (!announce && !nodes) + throw Error(i18n("Torrent has no announce or nodes field")); + + if (announce) + loadTrackerURL(announce); + + if (nodes) // DHT torrrents have a node key + loadNodes(nodes); + + loadInfo(dict->getDict("info")); + loadAnnounceList(dict->getData("announce-list")); + BNode* n = dict->getData("info"); + SHA1HashGen hg; + Uint8* info = (Uint8*)data.data(); + info_hash = hg.generate(info + n->getOffset(),n->getLength()); + delete node; + } + catch (...) + { + delete node; + throw; + } + } + + void Torrent::load(const QString & file,bool verbose) + { + QFile fptr(file); + if (!fptr.open(IO_ReadOnly)) + throw Error(i18n(" Unable to open torrent file %1 : %2") + .arg(file).arg(fptr.errorString())); + + QByteArray data(fptr.size()); + // Out() << "File size = " << fptr.size() << endl; + fptr.readBlock(data.data(),fptr.size()); + + load(data,verbose); + } + + void Torrent::loadInfo(BDictNode* dict) + { + if (!dict) + throw Error(i18n("Corrupted torrent!")); + + loadPieceLength(dict->getValue("piece length")); + BValueNode* n = dict->getValue("length"); + if (n) + loadFileLength(n); + else + loadFiles(dict->getList("files")); + + loadHash(dict->getValue("pieces")); + loadName(dict->getValue("name")); + n = dict->getValue("private"); + if (n && n->data().toInt() == 1) + priv_torrent = true; + + // do a safety check to see if the number of hashes matches the file_length + Uint32 num_chunks = (file_length / this->piece_length); + if (file_length % piece_length > 0) + num_chunks++; + + if (num_chunks != hash_pieces.count()) + { + Out(SYS_GEN|LOG_DEBUG) << "File sizes and number of hashes do not match for " << name_suggestion << endl; + throw Error(i18n("Corrupted torrent!")); + } + } + + void Torrent::loadFiles(BListNode* node) + { + Out() << "Multi file torrent" << endl; + if (!node) + throw Error(i18n("Corrupted torrent!")); + Uint32 idx = 0; + BListNode* fl = node; + for (Uint32 i = 0;i < fl->getNumChildren();i++) + { + BDictNode* d = fl->getDict(i); + if (!d) + throw Error(i18n("Corrupted torrent!")); + + BListNode* ln = d->getList("path"); + if (!ln) + throw Error(i18n("Corrupted torrent!")); + + QString path; + for (Uint32 j = 0;j < ln->getNumChildren();j++) + { + BValueNode* v = ln->getValue(j); + if (!v || v->data().getType() != Value::STRING) + throw Error(i18n("Corrupted torrent!")); + + QString sd = v->data().toString(encoding); + path += sd; + if (j + 1 < ln->getNumChildren()) + path += bt::DirSeparator(); + } + + // we do not want empty dirs + if (path.endsWith(bt::DirSeparator())) + continue; + + if (!checkPathForDirectoryTraversal(path)) + throw Error(i18n("Corrupted torrent!")); + + BValueNode* v = d->getValue("length"); + if (!v) + throw Error(i18n("Corrupted torrent!")); + + if (v->data().getType() == Value::INT || v->data().getType() == Value::INT64) + { + Uint64 s = v->data().toInt64(); + TorrentFile file(idx,path,file_length,s,piece_length); + + // update file_length + file_length += s; + files.append(file); + } + else + { + throw Error(i18n("Corrupted torrent!")); + } + idx++; + } + } + + void Torrent::loadTrackerURL(BValueNode* node) + { + if (!node || node->data().getType() != Value::STRING) + throw Error(i18n("Corrupted torrent!")); + + // tracker_urls.append(KURL(node->data().toString(encoding).stripWhiteSpace())); + if (!trackers) + trackers = new TrackerTier(); + + trackers->urls.append(KURL(node->data().toString(encoding).stripWhiteSpace())); + } + + void Torrent::loadPieceLength(BValueNode* node) + { + if (!node) + throw Error(i18n("Corrupted torrent!")); + + if (node->data().getType() == Value::INT) + piece_length = node->data().toInt(); + else if (node->data().getType() == Value::INT64) + piece_length = node->data().toInt64(); + else + throw Error(i18n("Corrupted torrent!")); + } + + void Torrent::loadFileLength(BValueNode* node) + { + if (!node) + throw Error(i18n("Corrupted torrent!")); + + if (node->data().getType() == Value::INT) + file_length = node->data().toInt(); + else if (node->data().getType() == Value::INT64) + file_length = node->data().toInt64(); + else + throw Error(i18n("Corrupted torrent!")); + } + + void Torrent::loadHash(BValueNode* node) + { + if (!node || node->data().getType() != Value::STRING) + throw Error(i18n("Corrupted torrent!")); + + + QByteArray hash_string = node->data().toByteArray(); + for (unsigned int i = 0;i < hash_string.size();i+=20) + { + Uint8 h[20]; + memcpy(h,hash_string.data()+i,20); + SHA1Hash hash(h); + hash_pieces.append(hash); + } + } + + void Torrent::loadName(BValueNode* node) + { + if (!node || node->data().getType() != Value::STRING) + throw Error(i18n("Corrupted torrent!")); + + name_suggestion = node->data().toString(encoding); + } + + void Torrent::loadAnnounceList(BNode* node) + { + if (!node) + return; + + BListNode* ml = dynamic_cast<BListNode*>(node); + if (!ml) + return; + + if (!trackers) + trackers = new TrackerTier(); + + TrackerTier* tier = trackers; + //ml->printDebugInfo(); + for (Uint32 i = 0;i < ml->getNumChildren();i++) + { + BListNode* url = dynamic_cast<BListNode*>(ml->getChild(i)); + if (!url) + throw Error(i18n("Parse Error")); + + for (Uint32 j = 0;j < url->getNumChildren();j++) + { + BValueNode* vn = dynamic_cast<BValueNode*>(url->getChild(j)); + if (!vn) + throw Error(i18n("Parse Error")); + + KURL url(vn->data().toString().stripWhiteSpace()); + tier->urls.append(url); + //Out() << "Added tracker " << url << endl; + } + tier->next = new TrackerTier(); + tier = tier->next; + } + } + + void Torrent::loadNodes(BListNode* node) + { + for (Uint32 i = 0;i < node->getNumChildren();i++) + { + BListNode* c = node->getList(i); + if (!c || c->getNumChildren() != 2) + throw Error(i18n("Corrupted torrent!")); + + // first child is the IP, second the port + BValueNode* ip = c->getValue(0); + BValueNode* port = c->getValue(1); + if (!ip || !port) + throw Error(i18n("Corrupted torrent!")); + + if (ip->data().getType() != Value::STRING) + throw Error(i18n("Corrupted torrent!")); + + if (port->data().getType() != Value::INT) + throw Error(i18n("Corrupted torrent!")); + + // add the DHT node + kt::DHTNode n; + n.ip = ip->data().toString(); + n.port = port->data().toInt(); + nodes.append(n); + } + } + + void Torrent::debugPrintInfo() + { + Out() << "Name : " << name_suggestion << endl; + +// for (KURL::List::iterator i = tracker_urls.begin();i != tracker_urls.end();i++) +// Out() << "Tracker URL : " << *i << endl; + + Out() << "Piece Length : " << piece_length << endl; + if (this->isMultiFile()) + { + Out() << "Files : " << endl; + Out() << "===================================" << endl; + for (Uint32 i = 0;i < getNumFiles();i++) + { + TorrentFile & tf = getFile(i); + Out() << "Path : " << tf.getPath() << endl; + Out() << "Size : " << tf.getSize() << endl; + Out() << "First Chunk : " << tf.getFirstChunk() << endl; + Out() << "Last Chunk : " << tf.getLastChunk() << endl; + Out() << "First Chunk Off : " << tf.getFirstChunkOffset() << endl; + Out() << "Last Chunk Size : " << tf.getLastChunkSize() << endl; + Out() << "===================================" << endl; + } + } + else + { + Out() << "File Length : " << file_length << endl; + } + Out() << "Pieces : " << hash_pieces.size() << endl; + } + + bool Torrent::verifyHash(const SHA1Hash & h,Uint32 index) + { + if (index >= hash_pieces.count()) + return false; + + const SHA1Hash & ph = hash_pieces[index]; + return ph == h; + } + + const SHA1Hash & Torrent::getHash(Uint32 idx) const + { + if (idx >= hash_pieces.count()) + throw Error(QString("Torrent::getHash %1 is out of bounds").arg(idx)); + + return hash_pieces[idx]; + } + + TorrentFile & Torrent::getFile(Uint32 idx) + { + if (idx >= files.size()) + return TorrentFile::null; + + return files.at(idx); + } + + const TorrentFile & Torrent::getFile(Uint32 idx) const + { + if (idx >= files.size()) + return TorrentFile::null; + + return files.at(idx); + } + + unsigned int Torrent::getNumTrackerURLs() const + { + Uint32 count = 0; + TrackerTier* tt = trackers; + while (tt) + { + count += tt->urls.count(); + tt = tt->next; + } + return count; + } + + void Torrent::calcChunkPos(Uint32 chunk,QValueList<Uint32> & file_list) const + { + file_list.clear(); + if (chunk >= hash_pieces.size() || files.empty()) + return; + + for (Uint32 i = 0;i < files.count();i++) + { + const TorrentFile & f = files[i]; + if (chunk >= f.getFirstChunk() && chunk <= f.getLastChunk() && f.getSize() != 0) + file_list.append(f.getIndex()); + } + } + + bool Torrent::isMultimedia() const + { + return IsMultimediaFile(this->getNameSuggestion()); + } + + void Torrent::updateFilePercentage(const BitSet & bs) + { + for (Uint32 i = 0;i < files.count();i++) + { + TorrentFile & f = files[i]; + f.updateNumDownloadedChunks(bs); + } + } + + void Torrent::updateFilePercentage(Uint32 chunk,const BitSet & bs) + { + QValueList<Uint32> cfiles; + calcChunkPos(chunk,cfiles); + + QValueList<Uint32>::iterator i = cfiles.begin(); + while (i != cfiles.end()) + { + TorrentFile & f = getFile(*i); + f.updateNumDownloadedChunks(bs); + i++; + } + } + + bool Torrent::checkPathForDirectoryTraversal(const QString & p) + { + QStringList sl = QStringList::split(bt::DirSeparator(),p); + return !sl.contains(".."); + } +} diff --git a/libktorrent/torrent/torrent.h b/libktorrent/torrent/torrent.h new file mode 100644 index 0000000..04c45cd --- /dev/null +++ b/libktorrent/torrent/torrent.h @@ -0,0 +1,218 @@ +/*************************************************************************** + * Copyright (C) 2005 by * + * Joris Guisson <joris.guisson@gmail.com> * + * Ivan Vasic <ivasic@gmail.com> * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTTORRENT_H +#define BTTORRENT_H + +#include <kurl.h> +#include <qvaluevector.h> +#include <util/sha1hash.h> +#include <util/constants.h> +#include <interfaces/torrentinterface.h> +#include "globals.h" +#include "peerid.h" +#include "torrentfile.h" + + + +namespace bt +{ + class BNode; + class BValueNode; + class BDictNode; + class BListNode; + + + struct TrackerTier + { + KURL::List urls; + TrackerTier* next; + + TrackerTier() : next(0) + {} + + ~TrackerTier() + { + delete next; + } + }; + + + /** + * @author Joris Guisson + * @brief Loads a .torrent file + * + * Loads a torrent file and calculates some miscelanious other data, + * like the info_hash and the peer_id. + */ + class Torrent + { + public: + Torrent(); + virtual ~Torrent(); + + /** + * Load a .torrent file. + * @param file The file + * @param verbose Wether to print information to the log + * @throw Error if something goes wrong + */ + void load(const QString & file,bool verbose); + + /** + * Load a .torrent file. + * @param data The data + * @param verbose Wether to print information to the log + * @throw Error if something goes wrong + */ + void load(const QByteArray & data,bool verbose); + + void debugPrintInfo(); + + /// Get the number of chunks. + Uint32 getNumChunks() const {return hash_pieces.size();} + + /// Get the size of a chunk. + Uint64 getChunkSize() const {return piece_length;} + + /// Get the info_hash. + const SHA1Hash & getInfoHash() const {return info_hash;} + + /// Get our peer_id. + const PeerID & getPeerID() const {return peer_id;} + + /// Get the file size in number of bytes. + Uint64 getFileLength() const {return file_length;} + + /// Get the suggested name. + QString getNameSuggestion() const {return name_suggestion;} + + /** + * Verify wether a hash matches the hash + * of a Chunk + * @param h The hash + * @param index The index of the chunk + * @return true if they match + */ + bool verifyHash(const SHA1Hash & h,Uint32 index); + + /// Get the number of tracker URL's + unsigned int getNumTrackerURLs() const; + + /** + * Get the hash of a Chunk. Throws an Error + * if idx is out of bounds. + * @param idx Index of Chunk + * @return The SHA1 hash of the chunk + */ + const SHA1Hash & getHash(Uint32 idx) const; + + /// See if we have a multi file torrent. + bool isMultiFile() const {return files.count() > 0;} + + /// Get the number of files in a multi file torrent. + /// If we have a single file torrent, this will return 0. + Uint32 getNumFiles() const {return files.count();} + + /** + * Get a TorrentFile. If the index is out of range, or + * we have a single file torrent we return a null TorrentFile. + * @param idx Index of the file + * @param A reference to the file + */ + TorrentFile & getFile(Uint32 idx); + + /** + * Get a TorrentFile. If the index is out of range, or + * we have a single file torrent we return a null TorrentFile. + * @param idx Index of the file + * @param A reference to the file + */ + const TorrentFile & getFile(Uint32 idx) const; + + /** + * Calculate in which file(s) a Chunk lies. A list will + * get filled with the indices of all the files. The list gets cleared at + * the beginning. If something is wrong only the list will + * get cleared. + * @param chunk The index of the chunk + * @param file_list This list will be filled with all the indices + */ + void calcChunkPos(Uint32 chunk,QValueList<Uint32> & file_list) const; + + /** + * Checks if torrent file is audio or video. + **/ + bool isMultimedia() const; + + /// See if the torrent is private + bool isPrivate() const {return priv_torrent;} + + ///Gets a pointer to AnnounceList + const TrackerTier* getTrackerList() const { return trackers; } + + /// Get the number of initial DHT nodes + Uint32 getNumDHTNodes() const {return nodes.count();} + + /// Get a DHT node + const kt::DHTNode & getDHTNode(Uint32 i) {return nodes[i];} + + /** + * Update the percentage of all files. + * @param bs The BitSet with all downloaded chunks + */ + void updateFilePercentage(const BitSet & bs); + + /** + * Update the percentage of a all files which have a particular chunk. + * @param bs The BitSet with all downloaded chunks + */ + void updateFilePercentage(Uint32 chunk,const BitSet & bs); + + + private: + void loadInfo(BDictNode* node); + void loadTrackerURL(BValueNode* node); + void loadPieceLength(BValueNode* node); + void loadFileLength(BValueNode* node); + void loadHash(BValueNode* node); + void loadName(BValueNode* node); + void loadFiles(BListNode* node); + void loadNodes(BListNode* node); + void loadAnnounceList(BNode* node); + bool checkPathForDirectoryTraversal(const QString & p); + + private: + TrackerTier* trackers; + QString name_suggestion; + Uint64 piece_length; + Uint64 file_length; + SHA1Hash info_hash; + PeerID peer_id; + QValueVector<SHA1Hash> hash_pieces; + QValueVector<TorrentFile> files; + QValueVector<kt::DHTNode> nodes; + QString encoding; + bool priv_torrent; + }; + +} + +#endif diff --git a/libktorrent/torrent/torrentcontrol.cpp b/libktorrent/torrent/torrentcontrol.cpp new file mode 100644 index 0000000..71b4e64 --- /dev/null +++ b/libktorrent/torrent/torrentcontrol.cpp @@ -0,0 +1,1770 @@ +/*************************************************************************** + * Copyright (C) 2005 by * + * Joris Guisson <joris.guisson@gmail.com> * + * Ivan Vasic <ivasic@gmail.com> * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <qdir.h> +#include <qfile.h> +#include <klocale.h> +#include <kmessagebox.h> +#include <kfiledialog.h> +#include <qtextstream.h> +#include <util/log.h> +#include <util/error.h> +#include <util/bitset.h> +#include <util/functions.h> +#include <util/fileops.h> +#include <util/waitjob.h> +#include <interfaces/functions.h> +#include <interfaces/trackerslist.h> +#include <datachecker/singledatachecker.h> +#include <datachecker/multidatachecker.h> +#include <datachecker/datacheckerlistener.h> +#include <datachecker/datacheckerthread.h> +#include <migrate/ccmigrate.h> +#include <migrate/cachemigrate.h> +#include <kademlia/dhtbase.h> + +#include "downloader.h" +#include "uploader.h" +#include "peersourcemanager.h" +#include "chunkmanager.h" +#include "torrent.h" +#include "peermanager.h" + +#include "torrentfile.h" +#include "torrentcontrol.h" + +#include "peer.h" +#include "choker.h" + +#include "globals.h" +#include "server.h" +#include "packetwriter.h" +#include "httptracker.h" +#include "udptracker.h" +#include "downloadcap.h" +#include "uploadcap.h" +#include "queuemanager.h" +#include "statsfile.h" +#include "announcelist.h" +#include "preallocationthread.h" +#include "timeestimator.h" +#include "settings.h" + +#include <net/socketmonitor.h> + + +using namespace kt; + +namespace bt +{ + + + + TorrentControl::TorrentControl() + : tor(0),psman(0),cman(0),pman(0),down(0),up(0),choke(0),tmon(0),prealloc(false) + { + istats.last_announce = 0; + stats.imported_bytes = 0; + stats.trk_bytes_downloaded = 0; + stats.trk_bytes_uploaded = 0; + stats.running = false; + stats.started = false; + stats.stopped_by_error = false; + stats.session_bytes_downloaded = 0; + stats.session_bytes_uploaded = 0; + istats.session_bytes_uploaded = 0; + old_datadir = QString::null; + stats.status = NOT_STARTED; + stats.autostart = true; + stats.user_controlled = false; + stats.priv_torrent = false; + stats.seeders_connected_to = stats.seeders_total = 0; + stats.leechers_connected_to = stats.leechers_total = 0; + istats.running_time_dl = istats.running_time_ul = 0; + istats.prev_bytes_dl = 0; + istats.prev_bytes_ul = 0; + istats.trk_prev_bytes_dl = istats.trk_prev_bytes_ul = 0; + istats.io_error = false; + istats.priority = 0; + stats.max_share_ratio = 0.00f; + istats.custom_output_name = false; + istats.diskspace_warning_emitted = false; + stats.max_seed_time = 0; + updateStats(); + prealoc_thread = 0; + dcheck_thread = 0; + istats.dht_on = false; + stats.num_corrupted_chunks = 0; + + m_eta = new TimeEstimator(this); + // by default no torrent limits + upload_gid = download_gid = 0; + upload_limit = download_limit = 0; + moving_files = false; + } + + + + + TorrentControl::~TorrentControl() + { + if (stats.running) + stop(false); + + if (tmon) + tmon->destroyed(); + delete choke; + delete down; + delete up; + delete cman; + delete pman; + delete psman; + delete tor; + delete m_eta; + } + + void TorrentControl::update() + { + UpdateCurrentTime(); + if (stats.status == kt::CHECKING_DATA || moving_files) + return; + + if (istats.io_error) + { + stop(false); + emit stoppedByError(this, error_msg); + return; + } + + if (prealoc_thread) + { + if (prealoc_thread->isDone()) + { + // thread done + if (prealoc_thread->errorHappened()) + { + // upon error just call onIOError and return + onIOError(prealoc_thread->errorMessage()); + delete prealoc_thread; + prealoc_thread = 0; + prealloc = true; // still need to do preallocation + return; + } + else + { + // continue the startup of the torrent + delete prealoc_thread; + prealoc_thread = 0; + prealloc = false; + stats.status = kt::NOT_STARTED; + saveStats(); + continueStart(); + } + } + else + return; // preallocation still going on, so just return + } + + + try + { + // first update peermanager + pman->update(); + bool comp = stats.completed; + + //helper var, check if needed to move completed files somewhere + bool moveCompleted = false; + + // then the downloader and uploader + up->update(choke->getOptimisticlyUnchokedPeerID()); + down->update(); + + stats.completed = cman->completed(); + if (stats.completed && !comp) + { + pman->killSeeders(); + QDateTime now = QDateTime::currentDateTime(); + istats.running_time_dl += istats.time_started_dl.secsTo(now); + updateStatusMsg(); + updateStats(); + + // download has just been completed + // only sent completed to tracker when we have all chunks (so no excluded chunks) + if (cman->haveAllChunks()) + psman->completed(); + + finished(this); + + //Move completed download to specified directory if needed + if(Settings::useCompletedDir()) + { + moveCompleted = true; + } + } + else if (!stats.completed && comp) + { + // restart download if necesarry + // when user selects that files which were previously excluded, + // should now be downloaded + if (!psman->isStarted()) + psman->start(); + else + psman->manualUpdate(); + istats.last_announce = bt::GetCurrentTime(); + istats.time_started_dl = QDateTime::currentDateTime(); + } + updateStatusMsg(); + + // get rid of dead Peers + Uint32 num_cleared = pman->clearDeadPeers(); + + // we may need to update the choker + if (choker_update_timer.getElapsedSinceUpdate() >= 10000 || num_cleared > 0) + { + // also get rid of seeders & uninterested when download is finished + // no need to keep them around, but also no need to do this + // every update, so once every 10 seconds is fine + if (stats.completed) + { + pman->killSeeders(); + } + + doChoking(); + choker_update_timer.update(); + // a good opportunity to make sure we are not keeping to much in memory + cman->checkMemoryUsage(); + } + + // to satisfy people obsessed with their share ratio + if (stats_save_timer.getElapsedSinceUpdate() >= 5*60*1000) + { + saveStats(); + stats_save_timer.update(); + } + + // Update DownloadCap + updateStats(); + + if (stats.download_rate > 0) + stalled_timer.update(); + + // do a manual update if we are stalled for more then 2 minutes + // we do not do this for private torrents + if (stalled_timer.getElapsedSinceUpdate() > 120000 && !stats.completed && + !stats.priv_torrent) + { + Out(SYS_TRK|LOG_NOTICE) << "Stalled for too long, time to get some fresh blood" << endl; + psman->manualUpdate(); + stalled_timer.update(); + } + + if(overMaxRatio() || overMaxSeedTime()) + { + if(istats.priority!=0) //if it's queued make sure to dequeue it + { + setPriority(0); + stats.user_controlled = true; + } + + stop(true); + emit seedingAutoStopped(this, overMaxRatio() ? kt::MAX_RATIO_REACHED : kt::MAX_SEED_TIME_REACHED); + } + + //Update diskspace if needed (every 1 min) + if(!stats.completed && stats.running && bt::GetCurrentTime() - last_diskspace_check >= 60 * 1000) + { + checkDiskSpace(true); + } + + //Move completed files if needed: + if (moveCompleted) + { + QString outdir = Settings::completedDir(); + if(!outdir.endsWith(bt::DirSeparator())) + outdir += bt::DirSeparator(); + + changeOutputDir(outdir); + } + } + catch (Error & e) + { + onIOError(e.toString()); + } + } + + void TorrentControl::onIOError(const QString & msg) + { + Out(SYS_DIO|LOG_IMPORTANT) << "Error : " << msg << endl; + stats.stopped_by_error = true; + stats.status = ERROR; + error_msg = msg; + istats.io_error = true; + } + + void TorrentControl::start() + { + // do not start running torrents + if (stats.running || stats.status == kt::ALLOCATING_DISKSPACE || moving_files) + return; + + stats.stopped_by_error = false; + istats.diskspace_warning_emitted = false; + istats.io_error = false; + try + { + bool ret = true; + aboutToBeStarted(this,ret); + if (!ret) + return; + } + catch (Error & err) + { + // something went wrong when files were recreated, set error and rethrow + onIOError(err.toString()); + return; + } + + try + { + cman->start(); + } + catch (Error & e) + { + onIOError(e.toString()); + throw; + } + + istats.time_started_ul = istats.time_started_dl = QDateTime::currentDateTime(); + resetTrackerStats(); + + if (prealloc) + { + // only start preallocation if we are allowed by the settings + if (Settings::diskPrealloc() && !cman->haveAllChunks()) + { + Out(SYS_GEN|LOG_NOTICE) << "Pre-allocating diskspace" << endl; + prealoc_thread = new PreallocationThread(cman); + stats.running = true; + stats.status = kt::ALLOCATING_DISKSPACE; + prealoc_thread->start(); + return; + } + else + { + prealloc = false; + } + } + + continueStart(); + } + + void TorrentControl::continueStart() + { + // continues start after the prealoc_thread has finished preallocation + pman->start(); + pman->loadPeerList(datadir + "peer_list"); + try + { + down->loadDownloads(datadir + "current_chunks"); + } + catch (Error & e) + { + // print out warning in case of failure + // we can still continue the download + Out(SYS_GEN|LOG_NOTICE) << "Warning : " << e.toString() << endl; + } + + loadStats(); + stats.running = true; + stats.started = true; + stats.autostart = true; + choker_update_timer.update(); + stats_save_timer.update(); + + + stalled_timer.update(); + psman->start(); + istats.last_announce = bt::GetCurrentTime(); + stalled_timer.update(); + } + + + void TorrentControl::stop(bool user,WaitJob* wjob) + { + QDateTime now = QDateTime::currentDateTime(); + if(!stats.completed) + istats.running_time_dl += istats.time_started_dl.secsTo(now); + istats.running_time_ul += istats.time_started_ul.secsTo(now); + istats.time_started_ul = istats.time_started_dl = now; + + // stop preallocation thread if necesarry + if (prealoc_thread) + { + prealoc_thread->stop(); + prealoc_thread->wait(); + + if (prealoc_thread->errorHappened() || prealoc_thread->isNotFinished()) + { + delete prealoc_thread; + prealoc_thread = 0; + prealloc = true; + saveStats(); // save stats, so that we will start preallocating the next time + } + else + { + delete prealoc_thread; + prealoc_thread = 0; + prealloc = false; + } + } + + if (stats.running) + { + psman->stop(wjob); + + if (tmon) + tmon->stopped(); + + try + { + down->saveDownloads(datadir + "current_chunks"); + } + catch (Error & e) + { + // print out warning in case of failure + // it doesn't corrupt the data, so just a couple of lost chunks + Out(SYS_GEN|LOG_NOTICE) << "Warning : " << e.toString() << endl; + } + + down->clearDownloads(); + if (user) + { + //make this torrent user controlled + setPriority(0); + stats.autostart = false; + } + } + pman->savePeerList(datadir + "peer_list"); + pman->stop(); + pman->closeAllConnections(); + pman->clearDeadPeers(); + cman->stop(); + + stats.running = false; + saveStats(); + updateStatusMsg(); + updateStats(); + stats.trk_bytes_downloaded = 0; + stats.trk_bytes_uploaded = 0; + + emit torrentStopped(this); + } + + void TorrentControl::setMonitor(kt::MonitorInterface* tmo) + { + tmon = tmo; + down->setMonitor(tmon); + if (tmon) + { + for (Uint32 i = 0;i < pman->getNumConnectedPeers();i++) + tmon->peerAdded(pman->getPeer(i)); + } + } + + + void TorrentControl::init(QueueManager* qman, + const QString & torrent, + const QString & tmpdir, + const QString & ddir, + const QString & default_save_dir) + { + // first load the torrent file + tor = new Torrent(); + try + { + tor->load(torrent,false); + } + catch (...) + { + delete tor; + tor = 0; + throw Error(i18n("An error occurred while loading the torrent." + " The torrent is probably corrupt or is not a torrent file.\n%1").arg(torrent)); + } + + initInternal(qman,tmpdir,ddir,default_save_dir,torrent.startsWith(tmpdir)); + + // copy torrent in tor dir + QString tor_copy = datadir + "torrent"; + if (tor_copy != torrent) + { + bt::CopyFile(torrent,tor_copy); + } + + } + + + void TorrentControl::init(QueueManager* qman, const QByteArray & data,const QString & tmpdir, + const QString & ddir,const QString & default_save_dir) + { + // first load the torrent file + tor = new Torrent(); + try + { + tor->load(data,false); + } + catch (...) + { + delete tor; + tor = 0; + throw Error(i18n("An error occurred while loading the torrent." + " The torrent is probably corrupt or is not a torrent file.")); + } + + initInternal(qman,tmpdir,ddir,default_save_dir,true); + // copy data into torrent file + QString tor_copy = datadir + "torrent"; + QFile fptr(tor_copy); + if (!fptr.open(IO_WriteOnly)) + throw Error(i18n("Unable to create %1 : %2") + .arg(tor_copy).arg(fptr.errorString())); + + fptr.writeBlock(data.data(),data.size()); + } + + void TorrentControl::checkExisting(QueueManager* qman) + { + // check if we haven't already loaded the torrent + // only do this when qman isn't 0 + if (qman && qman->allreadyLoaded(tor->getInfoHash())) + { + if (!stats.priv_torrent) + { + qman->mergeAnnounceList(tor->getInfoHash(),tor->getTrackerList()); + + throw Error(i18n("You are already downloading this torrent %1, the list of trackers of both torrents has been merged.").arg(tor->getNameSuggestion())); + } + else + { + throw Error(i18n("You are already downloading the torrent %1") + .arg(tor->getNameSuggestion())); + } + } + } + + void TorrentControl::setupDirs(const QString & tmpdir,const QString & ddir) + { + datadir = tmpdir; + + if (!datadir.endsWith(DirSeparator())) + datadir += DirSeparator(); + + outputdir = ddir.stripWhiteSpace(); + if (outputdir.length() > 0 && !outputdir.endsWith(DirSeparator())) + outputdir += DirSeparator(); + + if (!bt::Exists(datadir)) + { + bt::MakeDir(datadir); + } + } + + void TorrentControl::setupStats() + { + stats.completed = false; + stats.running = false; + stats.torrent_name = tor->getNameSuggestion(); + stats.multi_file_torrent = tor->isMultiFile(); + stats.total_bytes = tor->getFileLength(); + stats.priv_torrent = tor->isPrivate(); + + // check the stats file for the custom_output_name variable + StatsFile st(datadir + "stats"); + if (st.hasKey("CUSTOM_OUTPUT_NAME") && st.readULong("CUSTOM_OUTPUT_NAME") == 1) + { + istats.custom_output_name = true; + } + + // load outputdir if outputdir is null + if (outputdir.isNull() || outputdir.length() == 0) + loadOutputDir(); + } + + void TorrentControl::setupData(const QString & ddir) + { + // create PeerManager and Tracker + pman = new PeerManager(*tor); + //Out() << "Tracker url " << url << " " << url.protocol() << " " << url.prettyURL() << endl; + psman = new PeerSourceManager(this,pman); + connect(psman,SIGNAL(statusChanged( const QString& )), + this,SLOT(trackerStatusChanged( const QString& ))); + + + // Create chunkmanager, load the index file if it exists + // else create all the necesarry files + cman = new ChunkManager(*tor,datadir,outputdir,istats.custom_output_name); + // outputdir is null, see if the cache has figured out what it is + if (outputdir.length() == 0) + outputdir = cman->getDataDir(); + + // store the outputdir into the output_path variable, so others can access it + + connect(cman,SIGNAL(updateStats()),this,SLOT(updateStats())); + if (bt::Exists(datadir + "index")) + cman->loadIndexFile(); + + stats.completed = cman->completed(); + + // create downloader,uploader and choker + down = new Downloader(*tor,*pman,*cman); + connect(down,SIGNAL(ioError(const QString& )), + this,SLOT(onIOError(const QString& ))); + up = new Uploader(*cman,*pman); + choke = new Choker(*pman,*cman); + + + connect(pman,SIGNAL(newPeer(Peer* )),this,SLOT(onNewPeer(Peer* ))); + connect(pman,SIGNAL(peerKilled(Peer* )),this,SLOT(onPeerRemoved(Peer* ))); + connect(cman,SIGNAL(excluded(Uint32, Uint32 )),down,SLOT(onExcluded(Uint32, Uint32 ))); + connect(cman,SIGNAL(included( Uint32, Uint32 )),down,SLOT(onIncluded( Uint32, Uint32 ))); + connect(cman,SIGNAL(corrupted( Uint32 )),this,SLOT(corrupted( Uint32 ))); + } + + void TorrentControl::initInternal(QueueManager* qman, + const QString & tmpdir, + const QString & ddir, + const QString & default_save_dir, + bool first_time) + { + checkExisting(qman); + setupDirs(tmpdir,ddir); + setupStats(); + + if (!first_time) + { + // if we do not need to copy the torrent, it is an existing download and we need to see + // if it is not an old download + try + { + migrateTorrent(default_save_dir); + } + catch (Error & err) + { + + throw Error( + i18n("Cannot migrate %1 : %2") + .arg(tor->getNameSuggestion()).arg(err.toString())); + } + } + setupData(ddir); + + updateStatusMsg(); + + // to get rid of phantom bytes we need to take into account + // the data from downloads already in progress + try + { + Uint64 db = down->bytesDownloaded(); + Uint64 cb = down->getDownloadedBytesOfCurrentChunksFile(datadir + "current_chunks"); + istats.prev_bytes_dl = db + cb; + + // Out() << "Downloaded : " << kt::BytesToString(db) << endl; + // Out() << "current_chunks : " << kt::BytesToString(cb) << endl; + } + catch (Error & e) + { + // print out warning in case of failure + Out() << "Warning : " << e.toString() << endl; + istats.prev_bytes_dl = down->bytesDownloaded(); + } + + loadStats(); + updateStats(); + saveStats(); + stats.output_path = cman->getOutputPath(); + /* if (stats.output_path.isNull()) + { + cman->createFiles(); + stats.output_path = cman->getOutputPath(); + }*/ + Out() << "OutputPath = " << stats.output_path << endl; + } + + + + bool TorrentControl::announceAllowed() + { + if(istats.last_announce == 0) + return true; + + if (psman && psman->getNumFailures() == 0) + return bt::GetCurrentTime() - istats.last_announce >= 60 * 1000; + else + return true; + } + + void TorrentControl::updateTracker() + { + if (stats.running && announceAllowed()) + { + psman->manualUpdate(); + istats.last_announce = bt::GetCurrentTime(); + } + } + + void TorrentControl::onNewPeer(Peer* p) + { + connect(p,SIGNAL(gotPortPacket( const QString&, Uint16 )), + this,SLOT(onPortPacket( const QString&, Uint16 ))); + + if (p->getStats().fast_extensions) + { + const BitSet & bs = cman->getBitSet(); + if (bs.allOn()) + p->getPacketWriter().sendHaveAll(); + else if (bs.numOnBits() == 0) + p->getPacketWriter().sendHaveNone(); + else + p->getPacketWriter().sendBitSet(bs); + } + else + { + p->getPacketWriter().sendBitSet(cman->getBitSet()); + } + + if (!stats.completed) + p->getPacketWriter().sendInterested(); + + if (!stats.priv_torrent) + { + if (p->isDHTSupported()) + p->getPacketWriter().sendPort(Globals::instance().getDHT().getPort()); + else + // WORKAROUND so we can contact µTorrent's DHT + // They do not properly support the standard and do not turn on + // the DHT bit in the handshake, so we just ping each peer by default. + p->emitPortPacket(); + } + + // set group ID's for traffic shaping + p->setGroupIDs(upload_gid,download_gid); + + if (tmon) + tmon->peerAdded(p); + } + + void TorrentControl::onPeerRemoved(Peer* p) + { + disconnect(p,SIGNAL(gotPortPacket( const QString&, Uint16 )), + this,SLOT(onPortPacket( const QString&, Uint16 ))); + if (tmon) + tmon->peerRemoved(p); + } + + void TorrentControl::doChoking() + { + choke->update(stats.completed,stats); + } + + bool TorrentControl::changeDataDir(const QString & new_dir) + { + int pos = datadir.findRev(bt::DirSeparator(),-2); + if (pos == -1) + { + Out(SYS_GEN|LOG_DEBUG) << "Could not find torX part in " << datadir << endl; + return false; + } + + QString ndatadir = new_dir + datadir.mid(pos + 1); + + Out(SYS_GEN|LOG_DEBUG) << datadir << " -> " << ndatadir << endl; + try + { + bt::Move(datadir,ndatadir); + old_datadir = datadir; + datadir = ndatadir; + } + catch (Error & err) + { + Out(SYS_GEN|LOG_IMPORTANT) << "Could not move " << datadir << " to " << ndatadir << endl; + return false; + } + + cman->changeDataDir(datadir); + return true; + } + + bool TorrentControl::changeOutputDir(const QString & new_dir, bool moveFiles) + { + if (moving_files) + return false; + + Out(SYS_GEN|LOG_NOTICE) << "Moving data for torrent " << stats.torrent_name << " to " << new_dir << endl; + + restart_torrent_after_move_data_files = false; + + //check if torrent is running and stop it before moving data + if(stats.running) + { + restart_torrent_after_move_data_files = true; + this->stop(false); + } + + moving_files = true; + try + { + QString nd; + if (istats.custom_output_name) + { + int slash_pos = stats.output_path.findRev(bt::DirSeparator(),-2); + nd = new_dir + stats.output_path.mid(slash_pos + 1); + } + else + { + nd = new_dir + tor->getNameSuggestion(); + } + + if (stats.output_path != nd) + { + KIO::Job* j = 0; + if(moveFiles) + { + if (stats.multi_file_torrent) + j = cman->moveDataFiles(nd); + else + j = cman->moveDataFiles(new_dir); + } + + move_data_files_destination_path = nd; + if (j) + { + connect(j,SIGNAL(result(KIO::Job*)),this,SLOT(moveDataFilesJobDone(KIO::Job*))); + return true; + } + else + { + moveDataFilesJobDone(0); + } + } + else + { + Out(SYS_GEN|LOG_NOTICE) << "Source is the same as destination, so doing nothing" << endl; + } + } + catch (Error& err) + { + Out(SYS_GEN|LOG_IMPORTANT) << "Could not move " << stats.output_path << " to " << new_dir << ". Exception: " << err.toString() << endl; + moving_files = false; + return false; + } + + moving_files = false; + if (restart_torrent_after_move_data_files) + { + this->start(); + } + + return true; + } + + void TorrentControl::moveDataFilesJobDone(KIO::Job* job) + { + if (job) + cman->moveDataFilesCompleted(job); + + if (!job || (job && !job->error())) + { + cman->changeOutputPath(move_data_files_destination_path); + outputdir = stats.output_path = move_data_files_destination_path; + istats.custom_output_name = true; + + saveStats(); + Out(SYS_GEN|LOG_NOTICE) << "Data directory changed for torrent " << "'" << stats.torrent_name << "' to: " << move_data_files_destination_path << endl; + } + else if (job->error()) + { + Out(SYS_GEN|LOG_IMPORTANT) << "Could not move " << stats.output_path << " to " << move_data_files_destination_path << endl; + } + + moving_files = false; + if (restart_torrent_after_move_data_files) + { + this->start(); + } + } + + + void TorrentControl::rollback() + { + try + { + bt::Move(datadir,old_datadir); + datadir = old_datadir; + cman->changeDataDir(datadir); + } + catch (Error & err) + { + Out(SYS_GEN|LOG_IMPORTANT) << "Could not move " << datadir << " to " << old_datadir << endl; + } + } + + void TorrentControl::updateStatusMsg() + { + if (stats.stopped_by_error) + stats.status = kt::ERROR; + else if (!stats.started) + stats.status = kt::NOT_STARTED; + else if(!stats.running && !stats.user_controlled) + stats.status = kt::QUEUED; + else if (!stats.running && stats.completed && (overMaxRatio() || overMaxSeedTime())) + stats.status = kt::SEEDING_COMPLETE; + else if (!stats.running && stats.completed) + stats.status = kt::DOWNLOAD_COMPLETE; + else if (!stats.running) + stats.status = kt::STOPPED; + else if (stats.running && stats.completed) + stats.status = kt::SEEDING; + else if (stats.running) + // protocol messages are also included in speed calculation, so lets not compare with 0 + stats.status = down->downloadRate() > 100 ? + kt::DOWNLOADING : kt::STALLED; + } + + const BitSet & TorrentControl::downloadedChunksBitSet() const + { + if (cman) + return cman->getBitSet(); + else + return BitSet::null; + } + + const BitSet & TorrentControl::availableChunksBitSet() const + { + if (!pman) + return BitSet::null; + else + return pman->getAvailableChunksBitSet(); + } + + const BitSet & TorrentControl::excludedChunksBitSet() const + { + if (!cman) + return BitSet::null; + else + return cman->getExcludedBitSet(); + } + + const BitSet & TorrentControl::onlySeedChunksBitSet() const + { + if (!cman) + return BitSet::null; + else + return cman->getOnlySeedBitSet(); + } + + void TorrentControl::saveStats() + { + StatsFile st(datadir + "stats"); + + st.write("OUTPUTDIR", cman->getDataDir()); + + if (cman->getDataDir() != outputdir) + outputdir = cman->getDataDir(); + + st.write("UPLOADED", QString::number(up->bytesUploaded())); + + if (stats.running) + { + QDateTime now = QDateTime::currentDateTime(); + st.write("RUNNING_TIME_DL",QString("%1").arg(istats.running_time_dl + istats.time_started_dl.secsTo(now))); + st.write("RUNNING_TIME_UL",QString("%1").arg(istats.running_time_ul + istats.time_started_ul.secsTo(now))); + } + else + { + st.write("RUNNING_TIME_DL", QString("%1").arg(istats.running_time_dl)); + st.write("RUNNING_TIME_UL", QString("%1").arg(istats.running_time_ul)); + } + + st.write("PRIORITY", QString("%1").arg(istats.priority)); + st.write("AUTOSTART", QString("%1").arg(stats.autostart)); + st.write("IMPORTED", QString("%1").arg(stats.imported_bytes)); + st.write("CUSTOM_OUTPUT_NAME",istats.custom_output_name ? "1" : "0"); + st.write("MAX_RATIO", QString("%1").arg(stats.max_share_ratio,0,'f',2)); + st.write("MAX_SEED_TIME",QString::number(stats.max_seed_time)); + st.write("RESTART_DISK_PREALLOCATION",prealloc ? "1" : "0"); + + if(!stats.priv_torrent) + { + //save dht and pex + st.write("DHT", isFeatureEnabled(kt::DHT_FEATURE) ? "1" : "0"); + st.write("UT_PEX", isFeatureEnabled(kt::UT_PEX_FEATURE) ? "1" : "0"); + } + + st.write("UPLOAD_LIMIT",QString::number(upload_limit)); + st.write("DOWNLOAD_LIMIT",QString::number(download_limit)); + + st.writeSync(); + } + + void TorrentControl::loadStats() + { + StatsFile st(datadir + "stats"); + + Uint64 val = st.readUint64("UPLOADED"); + // stats.session_bytes_uploaded will be calculated based upon prev_bytes_ul + // seeing that this will change here, we need to save it + istats.session_bytes_uploaded = stats.session_bytes_uploaded; + istats.prev_bytes_ul = val; + up->setBytesUploaded(val); + + this->istats.running_time_dl = st.readULong("RUNNING_TIME_DL"); + this->istats.running_time_ul = st.readULong("RUNNING_TIME_UL"); + outputdir = st.readString("OUTPUTDIR").stripWhiteSpace(); + if (st.hasKey("CUSTOM_OUTPUT_NAME") && st.readULong("CUSTOM_OUTPUT_NAME") == 1) + { + istats.custom_output_name = true; + } + + setPriority(st.readInt("PRIORITY")); + stats.user_controlled = istats.priority == 0 ? true : false; + stats.autostart = st.readBoolean("AUTOSTART"); + + stats.imported_bytes = st.readUint64("IMPORTED"); + float rat = st.readFloat("MAX_RATIO"); + stats.max_share_ratio = rat; + if (st.hasKey("RESTART_DISK_PREALLOCATION")) + prealloc = st.readString("RESTART_DISK_PREALLOCATION") == "1"; + + stats.max_seed_time = st.readFloat("MAX_SEED_TIME"); + + if (!stats.priv_torrent) + { + if(st.hasKey("DHT")) + istats.dht_on = st.readBoolean("DHT"); + else + istats.dht_on = true; + + setFeatureEnabled(kt::DHT_FEATURE,istats.dht_on); + if (st.hasKey("UT_PEX")) + setFeatureEnabled(kt::UT_PEX_FEATURE,st.readBoolean("UT_PEX")); + } + + net::SocketMonitor & smon = net::SocketMonitor::instance(); + + Uint32 nl = st.readInt("UPLOAD_LIMIT"); + if (nl != upload_limit) + { + if (nl > 0) + { + if (upload_gid) + smon.setGroupLimit(net::SocketMonitor::UPLOAD_GROUP,upload_gid,nl); + else + upload_gid = smon.newGroup(net::SocketMonitor::UPLOAD_GROUP,nl); + } + else + { + smon.removeGroup(net::SocketMonitor::UPLOAD_GROUP,upload_gid); + upload_gid = 0; + } + } + upload_limit = nl; + + nl = st.readInt("DOWNLOAD_LIMIT"); + if (nl != download_limit) + { + if (nl > 0) + { + if (download_gid) + smon.setGroupLimit(net::SocketMonitor::DOWNLOAD_GROUP,download_gid,nl); + else + download_gid = smon.newGroup(net::SocketMonitor::DOWNLOAD_GROUP,nl); + } + else + { + smon.removeGroup(net::SocketMonitor::DOWNLOAD_GROUP,download_gid); + download_gid = 0; + } + } + download_limit = nl; + } + + void TorrentControl::loadOutputDir() + { + StatsFile st(datadir + "stats"); + if (!st.hasKey("OUTPUTDIR")) + return; + + outputdir = st.readString("OUTPUTDIR").stripWhiteSpace(); + if (st.hasKey("CUSTOM_OUTPUT_NAME") && st.readULong("CUSTOM_OUTPUT_NAME") == 1) + { + istats.custom_output_name = true; + } + } + + bool TorrentControl::readyForPreview(int start_chunk, int end_chunk) + { + if ( !tor->isMultimedia() && !tor->isMultiFile()) return false; + + const BitSet & bs = downloadedChunksBitSet(); + for(int i = start_chunk; i<end_chunk; ++i) + { + if ( !bs.get(i) ) return false; + } + return true; + } + + Uint32 TorrentControl::getTimeToNextTrackerUpdate() const + { + if (psman) + return psman->getTimeToNextUpdate(); + else + return 0; + } + + void TorrentControl::updateStats() + { + stats.num_chunks_downloading = down ? down->numActiveDownloads() : 0; + stats.num_peers = pman ? pman->getNumConnectedPeers() : 0; + stats.upload_rate = up && stats.running ? up->uploadRate() : 0; + stats.download_rate = down && stats.running ? down->downloadRate() : 0; + stats.bytes_left = cman ? cman->bytesLeft() : 0; + stats.bytes_left_to_download = cman ? cman->bytesLeftToDownload() : 0; + stats.bytes_uploaded = up ? up->bytesUploaded() : 0; + stats.bytes_downloaded = down ? down->bytesDownloaded() : 0; + stats.total_chunks = tor ? tor->getNumChunks() : 0; + stats.num_chunks_downloaded = cman ? cman->chunksDownloaded() : 0; + stats.num_chunks_excluded = cman ? cman->chunksExcluded() : 0; + stats.chunk_size = tor ? tor->getChunkSize() : 0; + stats.num_chunks_left = cman ? cman->chunksLeft() : 0; + stats.total_bytes_to_download = (tor && cman) ? tor->getFileLength() - cman->bytesExcluded() : 0; + + if (stats.bytes_downloaded >= istats.prev_bytes_dl) + stats.session_bytes_downloaded = stats.bytes_downloaded - istats.prev_bytes_dl; + else + stats.session_bytes_downloaded = 0; + + if (stats.bytes_uploaded >= istats.prev_bytes_ul) + stats.session_bytes_uploaded = (stats.bytes_uploaded - istats.prev_bytes_ul) + istats.session_bytes_uploaded; + else + stats.session_bytes_uploaded = istats.session_bytes_uploaded; + /* + Safety check, it is possible that stats.bytes_downloaded gets subtracted in Downloader. + Which can cause stats.bytes_downloaded to be smaller the istats.trk_prev_bytes_dl. + This can screw up your download ratio. + */ + if (stats.bytes_downloaded >= istats.trk_prev_bytes_dl) + stats.trk_bytes_downloaded = stats.bytes_downloaded - istats.trk_prev_bytes_dl; + else + stats.trk_bytes_downloaded = 0; + + if (stats.bytes_uploaded >= istats.trk_prev_bytes_ul) + stats.trk_bytes_uploaded = stats.bytes_uploaded - istats.trk_prev_bytes_ul; + else + stats.trk_bytes_uploaded = 0; + + getSeederInfo(stats.seeders_total,stats.seeders_connected_to); + getLeecherInfo(stats.leechers_total,stats.leechers_connected_to); + } + + void TorrentControl::getSeederInfo(Uint32 & total,Uint32 & connected_to) const + { + total = 0; + connected_to = 0; + if (!pman || !psman) + return; + + for (Uint32 i = 0;i < pman->getNumConnectedPeers();i++) + { + if (pman->getPeer(i)->isSeeder()) + connected_to++; + } + total = psman->getNumSeeders(); + if (total == 0) + total = connected_to; + } + + void TorrentControl::getLeecherInfo(Uint32 & total,Uint32 & connected_to) const + { + total = 0; + connected_to = 0; + if (!pman || !psman) + return; + + for (Uint32 i = 0;i < pman->getNumConnectedPeers();i++) + { + if (!pman->getPeer(i)->isSeeder()) + connected_to++; + } + total = psman->getNumLeechers(); + if (total == 0) + total = connected_to; + } + + Uint32 TorrentControl::getRunningTimeDL() const + { + if (!stats.running || stats.completed) + return istats.running_time_dl; + else + return istats.running_time_dl + istats.time_started_dl.secsTo(QDateTime::currentDateTime()); + } + + Uint32 TorrentControl::getRunningTimeUL() const + { + if (!stats.running) + return istats.running_time_ul; + else + return istats.running_time_ul + istats.time_started_ul.secsTo(QDateTime::currentDateTime()); + } + + Uint32 TorrentControl::getNumFiles() const + { + if (tor && tor->isMultiFile()) + return tor->getNumFiles(); + else + return 0; + } + + TorrentFileInterface & TorrentControl::getTorrentFile(Uint32 index) + { + if (tor) + return tor->getFile(index); + else + return TorrentFile::null; + } + + void TorrentControl::migrateTorrent(const QString & default_save_dir) + { + if (bt::Exists(datadir + "current_chunks") && bt::IsPreMMap(datadir + "current_chunks")) + { + // in case of error copy torX dir to migrate-failed-tor + QString dd = datadir; + int pos = dd.findRev("tor"); + if (pos != - 1) + { + dd = dd.replace(pos,3,"migrate-failed-tor"); + Out() << "Copying " << datadir << " to " << dd << endl; + bt::CopyDir(datadir,dd,true); + } + + bt::MigrateCurrentChunks(*tor,datadir + "current_chunks"); + if (outputdir.isNull() && bt::IsCacheMigrateNeeded(*tor,datadir + "cache")) + { + // if the output dir is NULL + if (default_save_dir.isNull()) + { + KMessageBox::information(0, + i18n("The torrent %1 was started with a previous version of KTorrent." + " To make sure this torrent still works with this version of KTorrent, " + "we will migrate this torrent. You will be asked for a location to save " + "the torrent to. If you press cancel, we will select your home directory.") + .arg(tor->getNameSuggestion())); + outputdir = KFileDialog::getExistingDirectory(QString::null, 0,i18n("Select Folder to Save To")); + if (outputdir.isNull()) + outputdir = QDir::homeDirPath(); + } + else + { + outputdir = default_save_dir; + } + + if (!outputdir.endsWith(bt::DirSeparator())) + outputdir += bt::DirSeparator(); + + bt::MigrateCache(*tor,datadir + "cache",outputdir); + } + + // delete backup + if (pos != - 1) + bt::Delete(dd); + } + } + + void TorrentControl::setPriority(int p) + { + istats.priority = p; + stats.user_controlled = p == 0 ? true : false; + if(p) + stats.status = kt::QUEUED; + else + updateStatusMsg(); + + saveStats(); + } + + void TorrentControl::setMaxShareRatio(float ratio) + { + if(ratio == 1.00f) + { + if (stats.max_share_ratio != ratio) + stats.max_share_ratio = ratio; + } + else + stats.max_share_ratio = ratio; + + if(stats.completed && !stats.running && !stats.user_controlled && (kt::ShareRatio(stats) >= stats.max_share_ratio)) + setPriority(0); //dequeue it + + saveStats(); + emit maxRatioChanged(this); + } + + void TorrentControl::setMaxSeedTime(float hours) + { + stats.max_seed_time = hours; + saveStats(); + } + + bool TorrentControl::overMaxRatio() + { + if(stats.completed && stats.bytes_uploaded != 0 && stats.bytes_downloaded != 0 && stats.max_share_ratio > 0) + { + if(kt::ShareRatio(stats) >= stats.max_share_ratio) + return true; + } + + return false; + } + + bool TorrentControl::overMaxSeedTime() + { + if(stats.completed && stats.bytes_uploaded != 0 && stats.bytes_downloaded != 0 && stats.max_seed_time > 0) + { + Uint32 dl = getRunningTimeDL(); + Uint32 ul = getRunningTimeUL(); + if ((ul - dl) / 3600.0f > stats.max_seed_time) + return true; + } + + return false; + } + + + QString TorrentControl::statusToString() const + { + switch (stats.status) + { + case kt::NOT_STARTED : + return i18n("Not started"); + case kt::DOWNLOAD_COMPLETE : + return i18n("Download completed"); + case kt::SEEDING_COMPLETE : + return i18n("Seeding completed"); + case kt::SEEDING : + return i18n("Seeding"); + case kt::DOWNLOADING: + return i18n("Downloading"); + case kt::STALLED: + return i18n("Stalled"); + case kt::STOPPED: + return i18n("Stopped"); + case kt::ERROR : + return i18n("Error: ") + getShortErrorMessage(); + case kt::ALLOCATING_DISKSPACE: + return i18n("Allocating diskspace"); + case kt::QUEUED: + return i18n("Queued"); + case kt::CHECKING_DATA: + return i18n("Checking data"); + case kt::NO_SPACE_LEFT: + return i18n("Stopped. No space left on device."); + } + return QString::null; + } + + TrackersList* TorrentControl::getTrackersList() + { + return psman; + } + + const TrackersList* TorrentControl::getTrackersList() const + { + return psman; + } + + void TorrentControl::onPortPacket(const QString & ip,Uint16 port) + { + if (Globals::instance().getDHT().isRunning() && !stats.priv_torrent) + Globals::instance().getDHT().portRecieved(ip,port); + } + + void TorrentControl::startDataCheck(bt::DataCheckerListener* lst,bool auto_import) + { + if (stats.status == kt::ALLOCATING_DISKSPACE) + return; + + + DataChecker* dc = 0; + stats.status = kt::CHECKING_DATA; + stats.num_corrupted_chunks = 0; // reset the number of corrupted chunks found + if (stats.multi_file_torrent) + dc = new MultiDataChecker(); + else + dc = new SingleDataChecker(); + + dc->setListener(lst); + + dcheck_thread = new DataCheckerThread(dc,stats.output_path,*tor,datadir + "dnd" + bt::DirSeparator()); + + // dc->check(stats.output_path,*tor,datadir + "dnd" + bt::DirSeparator()); + dcheck_thread->start(); + } + + void TorrentControl::afterDataCheck() + { + DataChecker* dc = dcheck_thread->getDataChecker(); + DataCheckerListener* lst = dc->getListener(); + + bool err = !dcheck_thread->getError().isNull(); + if (err) + { + // show a queued error message when an error has occurred + KMessageBox::queuedMessageBox(0,KMessageBox::Error,dcheck_thread->getError()); + lst->stop(); + } + + if (lst && !lst->isStopped()) + { + down->dataChecked(dc->getDownloaded()); + // update chunk manager + cman->dataChecked(dc->getDownloaded()); + if (lst->isAutoImport()) + { + down->recalcDownloaded(); + stats.imported_bytes = down->bytesDownloaded(); + if (cman->haveAllChunks()) + stats.completed = true; + } + else + { + Uint64 downloaded = stats.bytes_downloaded; + down->recalcDownloaded(); + updateStats(); + if (stats.bytes_downloaded > downloaded) + stats.imported_bytes = stats.bytes_downloaded - downloaded; + + if (cman->haveAllChunks()) + stats.completed = true; + } + } + + stats.status = kt::NOT_STARTED; + // update the status + updateStatusMsg(); + updateStats(); + if (lst) + lst->finished(); + delete dcheck_thread; + dcheck_thread = 0; + } + + bool TorrentControl::isCheckingData(bool & finished) const + { + if (dcheck_thread) + { + finished = !dcheck_thread->isRunning(); + return true; + } + return false; + } + + bool TorrentControl::hasExistingFiles() const + { + return cman->hasExistingFiles(); + } + + bool TorrentControl::hasMissingFiles(QStringList & sl) + { + return cman->hasMissingFiles(sl); + } + + void TorrentControl::recreateMissingFiles() + { + try + { + cman->recreateMissingFiles(); + prealloc = true; // set prealloc to true so files will be truncated again + down->dataChecked(cman->getBitSet()); // update chunk selector + } + catch (Error & err) + { + onIOError(err.toString()); + throw; + } + } + + void TorrentControl::dndMissingFiles() + { + try + { + cman->dndMissingFiles(); + prealloc = true; // set prealloc to true so files will be truncated again + missingFilesMarkedDND(this); + down->dataChecked(cman->getBitSet()); // update chunk selector + } + catch (Error & err) + { + onIOError(err.toString()); + throw; + } + } + + void TorrentControl::handleError(const QString & err) + { + onIOError(err); + } + + Uint32 TorrentControl::getNumDHTNodes() const + { + return tor->getNumDHTNodes(); + } + + const kt::DHTNode & TorrentControl::getDHTNode(Uint32 i) const + { + return tor->getDHTNode(i); + } + + void TorrentControl::deleteDataFiles() + { + cman->deleteDataFiles(); + } + + const bt::SHA1Hash & TorrentControl::getInfoHash() const + { + return tor->getInfoHash(); + } + + void TorrentControl::resetTrackerStats() + { + istats.trk_prev_bytes_dl = stats.bytes_downloaded, + istats.trk_prev_bytes_ul = stats.bytes_uploaded, + stats.trk_bytes_downloaded = 0; + stats.trk_bytes_uploaded = 0; + } + + void TorrentControl::trackerStatusChanged(const QString & ns) + { + stats.trackerstatus = ns; + } + + void TorrentControl::addPeerSource(kt::PeerSource* ps) + { + if (psman) + psman->addPeerSource(ps); + } + + void TorrentControl::removePeerSource(kt::PeerSource* ps) + { + if (psman) + psman->removePeerSource(ps); + } + + void TorrentControl::corrupted(Uint32 chunk) + { + // make sure we will redownload the chunk + down->corrupted(chunk); + if (stats.completed) + stats.completed = false; + + // emit signal to show a systray message + stats.num_corrupted_chunks++; + corruptedDataFound(this); + } + + Uint32 TorrentControl::getETA() + { + return m_eta->estimate(); + } + + + + const bt::PeerID & TorrentControl::getOwnPeerID() const + { + return tor->getPeerID(); + } + + + bool TorrentControl::isFeatureEnabled(TorrentFeature tf) + { + switch (tf) + { + case kt::DHT_FEATURE: + return psman->dhtStarted(); + case kt::UT_PEX_FEATURE: + return pman->isPexEnabled(); + default: + return false; + } + } + + void TorrentControl::setFeatureEnabled(TorrentFeature tf,bool on) + { + switch (tf) + { + case kt::DHT_FEATURE: + if (on) + { + if(!stats.priv_torrent) + { + psman->addDHT(); + istats.dht_on = psman->dhtStarted(); + saveStats(); + } + } + else + { + psman->removeDHT(); + istats.dht_on = false; + saveStats(); + } + break; + case kt::UT_PEX_FEATURE: + if (on) + { + if (!stats.priv_torrent && !pman->isPexEnabled()) + { + pman->setPexEnabled(true); + } + } + else + { + pman->setPexEnabled(false); + } + break; + } + } + + void TorrentControl::createFiles() + { + cman->createFiles(true); + stats.output_path = cman->getOutputPath(); + } + + bool TorrentControl::checkDiskSpace(bool emit_sig) + { + last_diskspace_check = bt::GetCurrentTime(); + + //calculate free disk space + Uint64 bytes_free = 0; + if (FreeDiskSpace(getDataDir(),bytes_free)) + { + Uint64 bytes_to_download = stats.total_bytes_to_download; + Uint64 downloaded = 0; + try + { + downloaded = cman->diskUsage(); + } + catch (bt::Error & err) + { + Out(SYS_GEN|LOG_DEBUG) << "Error : " << err.toString() << endl; + } + Uint64 remaining = 0; + if (downloaded <= bytes_to_download) + remaining = bytes_to_download - downloaded; + + if (remaining > bytes_free) + { + bool toStop = bytes_free < (Uint64) Settings::minDiskSpace() * 1024 * 1024; + + // if we don't need to stop the torrent, only emit the signal once + // so that we do bother the user continously + if (emit_sig && (toStop || !istats.diskspace_warning_emitted)) + { + emit diskSpaceLow(this, toStop); + istats.diskspace_warning_emitted = true; + } + + if (!stats.running) + { + stats.status = NO_SPACE_LEFT; + } + + return false; + } + } + + return true; + } + + void TorrentControl::setTrafficLimits(Uint32 up,Uint32 down) + { + net::SocketMonitor & smon = net::SocketMonitor::instance(); + if (up && !upload_gid) + { + // create upload group + upload_gid = smon.newGroup(net::SocketMonitor::UPLOAD_GROUP,up); + upload_limit = up; + } + else if (up && upload_gid) + { + // change existing group limit + smon.setGroupLimit(net::SocketMonitor::UPLOAD_GROUP,upload_gid,up); + upload_limit = up; + } + else if (!up && !upload_gid) + { + upload_limit = up; + } + else // !up && upload_gid + { + // remove existing group + smon.removeGroup(net::SocketMonitor::UPLOAD_GROUP,upload_gid); + upload_gid = upload_limit = 0; + } + + if (down && !download_gid) + { + // create download grodown + download_gid = smon.newGroup(net::SocketMonitor::DOWNLOAD_GROUP,down); + download_limit = down; + } + else if (down && download_gid) + { + // change existing grodown limit + smon.setGroupLimit(net::SocketMonitor::DOWNLOAD_GROUP,download_gid,down); + download_limit = down; + } + else if (!down && !download_gid) + { + download_limit = down; + } + else // !down && download_gid + { + // remove existing grodown + smon.removeGroup(net::SocketMonitor::DOWNLOAD_GROUP,download_gid); + download_gid = download_limit = 0; + } + + saveStats(); + pman->setGroupIDs(upload_gid,download_gid); + } + + void TorrentControl::getTrafficLimits(Uint32 & up,Uint32 & down) + { + up = upload_limit; + down = download_limit; + } + + const PeerManager * TorrentControl::getPeerMgr() const + { + return pman; + } +} + +#include "torrentcontrol.moc" diff --git a/libktorrent/torrent/torrentcontrol.h b/libktorrent/torrent/torrentcontrol.h new file mode 100644 index 0000000..33610de --- /dev/null +++ b/libktorrent/torrent/torrentcontrol.h @@ -0,0 +1,394 @@ +/*************************************************************************** + * Copyright (C) 2005 by * + * Joris Guisson <joris.guisson@gmail.com> * + * Ivan Vasic <ivasic@gmail.com> * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTTORRENTCONTROL_H +#define BTTORRENTCONTROL_H + +#include <qobject.h> +#include <qcstring.h> +#include <qtimer.h> +#include <kurl.h> +#include "globals.h" +#include <util/timer.h> +#include <interfaces/torrentinterface.h> +#include <interfaces/monitorinterface.h> +#include <interfaces/trackerslist.h> + +class QStringList; +class QString; + +namespace KIO +{ + class Job; +} + + +namespace bt +{ + class Choker; + class Torrent; + class PeerSourceManager; + class ChunkManager; + class PeerManager; + class Downloader; + class Uploader; + class Peer; + class BitSet; + class QueueManager; + class PreallocationThread; + class TimeEstimator; + class DataCheckerThread; + class WaitJob; + + /** + * @author Joris Guisson + * @brief Controls just about everything + * + * This is the interface which any user gets to deal with. + * This class controls the uploading, downloading, choking, + * updating the tracker and chunk management. + */ + class TorrentControl : public kt::TorrentInterface + { + Q_OBJECT + public: + TorrentControl(); + virtual ~TorrentControl(); + + /** + * Get a BitSet of the status of all Chunks + */ + const BitSet & downloadedChunksBitSet() const; + + /** + * Get a BitSet of the availability of all Chunks + */ + const BitSet & availableChunksBitSet() const; + + /** + * Get a BitSet of the excluded Chunks + */ + const BitSet & excludedChunksBitSet() const; + + /** + * Get a BitSet of the only seed chunks + */ + const BitSet & onlySeedChunksBitSet() const; + + /** + * Initialize the TorrentControl. + * @param qman The QueueManager + * @param torrent The filename of the torrent file + * @param tmpdir The directory to store temporary data + * @param datadir The directory to store the actual file(s) + * (only used the first time we load a torrent) + * @param default_save_dir Default save directory (null if not set) + * @throw Error when something goes wrong + */ + void init(QueueManager* qman, + const QString & torrent, + const QString & tmpdir, + const QString & datadir, + const QString & default_save_dir); + + /** + * Initialize the TorrentControl. + * @param qman The QueueManager + * @param data The data of the torrent + * @param tmpdir The directory to store temporary data + * @param datadir The directory to store the actual file(s) + * (only used the first time we load a torrent) + * @param default_save_dir Default save directory (null if not set) + * @throw Error when something goes wrong + */ + void init(QueueManager* qman, + const QByteArray & data, + const QString & tmpdir, + const QString & datadir, + const QString & default_save_dir); + + /** + * Change to a new data dir. If this fails + * we will fall back on the old directory. + * @param new_dir The new directory + * @return true upon succes + */ + bool changeDataDir(const QString & new_dir); + + + /** + * Change torrents output directory. If this fails we will fall back on the old directory. + * @param new_dir The new directory + * @param moveFiles Wheather to actually move the files or just change the directory without moving them. + * @return true upon success. + */ + bool changeOutputDir(const QString& new_dir, bool moveFiles = true); + + /** + * Roll back the previous changeDataDir call. + * Does nothing if there was no previous changeDataDir call. + */ + void rollback(); + + /// Gets the TrackersList interface + kt::TrackersList* getTrackersList(); + + /// Gets the TrackersList interface + const kt::TrackersList* getTrackersList() const; + + /// Get the data directory of this torrent + QString getDataDir() const {return outputdir;} + + /// Get the torX dir. + QString getTorDir() const {return datadir;} + + /// Set the monitor + void setMonitor(kt::MonitorInterface* tmo); + + /// Get the Torrent. + const Torrent & getTorrent() const {return *tor;} + + /** + * Get the download running time of this torrent in seconds + * @return Uint32 - time in seconds + */ + Uint32 getRunningTimeDL() const; + + /** + * Get the upload running time of this torrent in seconds + * @return Uint32 - time in seconds + */ + Uint32 getRunningTimeUL() const; + + /** + * Checks if torrent is multimedial and chunks needed for preview are downloaded + * @param start_chunk The index of starting chunk to check + * @param end_chunk The index of the last chunk to check + * In case of single torrent file defaults can be used (0,1) + **/ + bool readyForPreview(int start_chunk = 0, int end_chunk = 1); + + /// Get the time to the next tracker update in seconds. + Uint32 getTimeToNextTrackerUpdate() const; + + /// Get a short error message + QString getShortErrorMessage() const {return error_msg;} + + virtual Uint32 getNumFiles() const; + virtual kt::TorrentFileInterface & getTorrentFile(Uint32 index); + virtual void recreateMissingFiles(); + virtual void dndMissingFiles(); + virtual void addPeerSource(kt::PeerSource* ps); + virtual void removePeerSource(kt::PeerSource* ps); + + int getPriority() const { return istats.priority; } + void setPriority(int p); + + virtual bool overMaxRatio(); + virtual void setMaxShareRatio(float ratio); + virtual float getMaxShareRatio() const { return stats.max_share_ratio; } + + virtual bool overMaxSeedTime(); + virtual void setMaxSeedTime(float hours); + virtual float getMaxSeedTime() const {return stats.max_seed_time;} + + /// Tell the TorrentControl obj to preallocate diskspace in the next update + void setPreallocateDiskSpace(bool pa) {prealloc = pa;} + + /// Make a string out of the status message + virtual QString statusToString() const; + + /// Checks if tracker announce is allowed (minimum interval 60 seconds) + bool announceAllowed(); + + void startDataCheck(bt::DataCheckerListener* lst,bool auto_import); + + /// Test if the torrent has existing files, only works the first time a torrent is loaded + bool hasExistingFiles() const; + + /** + * Test all files and see if they are not missing. + * If so put them in a list + */ + bool hasMissingFiles(QStringList & sl); + + + virtual Uint32 getNumDHTNodes() const; + virtual const kt::DHTNode & getDHTNode(Uint32 i) const; + virtual void deleteDataFiles(); + virtual const SHA1Hash & getInfoHash() const; + virtual const bt::PeerID & getOwnPeerID() const; + + /** + * Called by the PeerSourceManager when it is going to start a new tracker. + */ + void resetTrackerStats(); + + /** + * Returns estimated time left for finishing download. Returned value is in seconds. + * Uses TimeEstimator class to calculate this value. + */ + Uint32 getETA(); + + /// Is a feature enabled + bool isFeatureEnabled(kt::TorrentFeature tf); + + /// Disable or enable a feature + void setFeatureEnabled(kt::TorrentFeature tf,bool on); + + /// Create all the necessary files + void createFiles(); + + ///Checks if diskspace is low + bool checkDiskSpace(bool emit_sig = true); + + virtual void setTrafficLimits(Uint32 up,Uint32 down); + virtual void getTrafficLimits(Uint32 & up,Uint32 & down); + + ///Get the PeerManager + const PeerManager * getPeerMgr() const; + + /// Are we in the process of moving files + bool isMovingFiles() const {return moving_files;} + + public slots: + /** + * Update the object, should be called periodically. + */ + void update(); + + /** + * Start the download of the torrent. + */ + void start(); + + /** + * Stop the download, closes all connections. + * @param user wether or not the user did this explicitly + * @param wjob WaitJob to wait at exit for the completion of stopped requests + */ + void stop(bool user,WaitJob* wjob = 0); + + /** + * Update the tracker, this should normally handled internally. + * We leave it public so that the user can do a manual announce. + */ + void updateTracker(); + + /** + * The tracker status has changed. + * @param ns New status + */ + void trackerStatusChanged(const QString & ns); + + private slots: + void onNewPeer(Peer* p); + void onPeerRemoved(Peer* p); + void doChoking(); + void onIOError(const QString & msg); + void onPortPacket(const QString & ip,Uint16 port); + /// Update the stats of the torrent. + void updateStats(); + void corrupted(Uint32 chunk); + void moveDataFilesJobDone(KIO::Job* job); + + private: + void updateTracker(const QString & ev,bool last_succes = true); + void updateStatusMsg(); + void saveStats(); + void loadStats(); + void loadOutputDir(); + void getSeederInfo(Uint32 & total,Uint32 & connected_to) const; + void getLeecherInfo(Uint32 & total,Uint32 & connected_to) const; + void migrateTorrent(const QString & default_save_dir); + void continueStart(); + virtual void handleError(const QString & err); + + void initInternal(QueueManager* qman,const QString & tmpdir, + const QString & ddir,const QString & default_save_dir,bool first_time); + + void checkExisting(QueueManager* qman); + void setupDirs(const QString & tmpdir,const QString & ddir); + void setupStats(); + void setupData(const QString & ddir); + virtual void afterDataCheck(); + virtual bool isCheckingData(bool & finished) const; + + private: + Torrent* tor; + PeerSourceManager* psman; + ChunkManager* cman; + PeerManager* pman; + Downloader* down; + Uploader* up; + Choker* choke; + TimeEstimator* m_eta; + kt::MonitorInterface* tmon; + + Timer choker_update_timer; + Timer stats_save_timer; + Timer stalled_timer; + + QString datadir; + QString old_datadir; + QString outputdir; + QString error_msg; + + QString move_data_files_destination_path; + bool restart_torrent_after_move_data_files; + + bool prealloc; + PreallocationThread* prealoc_thread; + DataCheckerThread* dcheck_thread; + TimeStamp last_diskspace_check; + bool moving_files; + + struct InternalStats + { + QDateTime time_started_dl; + QDateTime time_started_ul; + Uint32 running_time_dl; + Uint32 running_time_ul; + Uint64 prev_bytes_dl; + Uint64 prev_bytes_ul; + Uint64 trk_prev_bytes_dl; + Uint64 trk_prev_bytes_ul; + Uint64 session_bytes_uploaded; + bool io_error; + bool custom_output_name; + Uint16 port; + int priority; + bool dht_on; + TimeStamp last_announce; + bool diskspace_warning_emitted; + }; + + Uint32 upload_gid; // group ID for upload + Uint32 upload_limit; + Uint32 download_gid; // group ID for download + Uint32 download_limit; + + InternalStats istats; + }; + + +} + +#endif diff --git a/libktorrent/torrent/torrentcreator.cpp b/libktorrent/torrent/torrentcreator.cpp new file mode 100644 index 0000000..7b132b8 --- /dev/null +++ b/libktorrent/torrent/torrentcreator.cpp @@ -0,0 +1,388 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <qdir.h> +#include <qfileinfo.h> +#include <klocale.h> +#include <time.h> +#include <util/error.h> +#include <ktversion.h> +#include "torrentcontrol.h" +#include "torrentcreator.h" +#include "bencoder.h" +#include <util/file.h> +#include <util/sha1hash.h> +#include <util/fileops.h> +#include <util/log.h> +#include <util/array.h> +#include <util/functions.h> +#include "globals.h" +#include "chunkmanager.h" +#include "statsfile.h" + +namespace bt +{ + + TorrentCreator::TorrentCreator(const QString & tar, + const QStringList & track, + Uint32 cs, + const QString & name, + const QString & comments,bool priv, bool decentralized) + : target(tar),trackers(track),chunk_size(cs), + name(name),comments(comments),cur_chunk(0),priv(priv),tot_size(0), decentralized(decentralized) + { + this->chunk_size *= 1024; + QFileInfo fi(target); + if (fi.isDir()) + { + if (!this->target.endsWith(bt::DirSeparator())) + this->target += bt::DirSeparator(); + + tot_size = 0; + buildFileList(""); + num_chunks = tot_size / chunk_size; + if (tot_size % chunk_size > 0) + num_chunks++; + last_size = tot_size % chunk_size; + Out() << "Tot Size : " << tot_size << endl; + } + else + { + tot_size = bt::FileSize(target); + num_chunks = tot_size / chunk_size; + if (tot_size % chunk_size > 0) + num_chunks++; + last_size = tot_size % chunk_size; + Out() << "Tot Size : " << tot_size << endl; + } + + if (last_size == 0) + last_size = chunk_size; + + Out() << "Num Chunks : " << num_chunks << endl; + Out() << "Chunk Size : " << chunk_size << endl; + Out() << "Last Size : " << last_size << endl; + } + + + TorrentCreator::~TorrentCreator() + {} + + void TorrentCreator::buildFileList(const QString & dir) + { + QDir d(target + dir); + // first get all files (we ignore symlinks) + QStringList dfiles = d.entryList(QDir::Files|QDir::NoSymLinks); + Uint32 cnt = 0; // counter to keep track of file index + for (QStringList::iterator i = dfiles.begin();i != dfiles.end();++i) + { + // add a TorrentFile to the list + Uint64 fs = bt::FileSize(target + dir + *i); + TorrentFile f(cnt,dir + *i,tot_size,fs,chunk_size); + files.append(f); + // update total size + tot_size += fs; + cnt++; + } + + // now for each subdir do a buildFileList + QStringList subdirs = d.entryList(QDir::Dirs|QDir::NoSymLinks); + for (QStringList::iterator i = subdirs.begin();i != subdirs.end();++i) + { + if (*i == "." || *i == "..") + continue; + + QString sd = dir + *i; + if (!sd.endsWith(bt::DirSeparator())) + sd += bt::DirSeparator(); + buildFileList(sd); + } + } + + + void TorrentCreator::saveTorrent(const QString & url) + { + File fptr; + if (!fptr.open(url,"wb")) + throw Error(i18n("Cannot open file %1: %2").arg(url).arg(fptr.errorString())); + + BEncoder enc(&fptr); + enc.beginDict(); // top dict + + if(!decentralized) + { + enc.write("announce"); enc.write(trackers[0]); + if (trackers.count() > 1) + { + enc.write("announce-list"); + enc.beginList(); + enc.beginList(); + for (Uint32 i = 0;i < trackers.count();i++) + enc.write(trackers[i]); + enc.end(); + enc.end(); + + } + } + + + if (comments.length() > 0) + { + enc.write("comments"); + enc.write(comments); + } + enc.write("created by");enc.write(QString("KTorrent %1").arg(kt::VERSION_STRING)); + enc.write("creation date");enc.write((Uint64)time(0)); + enc.write("info"); + saveInfo(enc); + // save the nodes list after the info hash, keys must be sorted ! + if (decentralized) + { + //DHT torrent + enc.write("nodes"); + enc.beginList(); + + for(int i=0; i < trackers.count(); ++i) + { + QString t = trackers[i]; + enc.beginList(); + enc.write(t.section(',',0,0)); + enc.write((Uint32)t.section(',',1,1).toInt()); + enc.end(); + } + enc.end(); + } + + enc.end(); + } + + void TorrentCreator::saveInfo(BEncoder & enc) + { + enc.beginDict(); + + QFileInfo fi(target); + if (fi.isDir()) + { + enc.write("files"); + enc.beginList(); + QValueList<TorrentFile>::iterator i = files.begin(); + while (i != files.end()) + { + saveFile(enc,*i); + i++; + } + enc.end(); + } + else + { + enc.write("length"); enc.write(bt::FileSize(target)); + } + enc.write("name"); enc.write(name); + enc.write("piece length"); enc.write((Uint64)chunk_size); + enc.write("pieces"); savePieces(enc); + if (priv) + { + enc.write("private"); + enc.write((Uint64)1); + } + enc.end(); + } + + void TorrentCreator::saveFile(BEncoder & enc,const TorrentFile & file) + { + enc.beginDict(); + enc.write("length");enc.write(file.getSize()); + enc.write("path"); + enc.beginList(); + QStringList sl = QStringList::split(bt::DirSeparator(),file.getPath()); + for (QStringList::iterator i = sl.begin();i != sl.end();i++) + enc.write(*i); + enc.end(); + enc.end(); + } + + void TorrentCreator::savePieces(BEncoder & enc) + { + if (hashes.empty()) + while (!calculateHash()) + ; + + Array<Uint8> big_hash(num_chunks*20); + for (Uint32 i = 0;i < num_chunks;++i) + { + memcpy(big_hash+(20*i),hashes[i].getData(),20); + } + enc.write(big_hash,num_chunks*20); + } + + bool TorrentCreator::calcHashSingle() + { + Array<Uint8> buf(chunk_size); + File fptr; + if (!fptr.open(target,"rb")) + throw Error(i18n("Cannot open file %1: %2") + .arg(target).arg(fptr.errorString())); + + Uint32 s = cur_chunk != num_chunks - 1 ? chunk_size : last_size; + fptr.seek(File::BEGIN,(Int64)cur_chunk*chunk_size); + + fptr.read(buf,s); + SHA1Hash h = SHA1Hash::generate(buf,s); + hashes.append(h); + cur_chunk++; + return cur_chunk >= num_chunks; + } + + bool TorrentCreator::calcHashMulti() + { + Uint32 s = cur_chunk != num_chunks - 1 ? chunk_size : last_size; + // first find the file(s) the chunk lies in + Array<Uint8> buf(s); + QValueList<TorrentFile> file_list; + Uint32 i = 0; + while (i < files.size()) + { + const TorrentFile & tf = files[i]; + if (cur_chunk >= tf.getFirstChunk() && cur_chunk <= tf.getLastChunk()) + { + file_list.append(tf); + } + + i++; + } + + Uint32 read = 0; + for (i = 0;i < file_list.count();i++) + { + const TorrentFile & f = file_list[i]; + File fptr; + if (!fptr.open(target + f.getPath(),"rb")) + { + throw Error(i18n("Cannot open file %1: %2") + .arg(f.getPath()).arg(fptr.errorString())); + } + + // first calculate offset into file + // only the first file can have an offset + // the following files will start at the beginning + Uint64 off = 0; + if (i == 0) + off = f.fileOffset(cur_chunk,chunk_size); + + Uint32 to_read = 0; + // then the amount of data we can read from this file + if (file_list.count() == 1) + to_read = s; + else if (i == 0) + to_read = f.getLastChunkSize(); + else if (i == file_list.count() - 1) + to_read = s - read; + else + to_read = f.getSize(); + + // read part of data + fptr.seek(File::BEGIN,(Int64)off); + fptr.read(buf + read,to_read); + read += to_read; + } + + // generate hash + SHA1Hash h = SHA1Hash::generate(buf,s); + hashes.append(h); + + cur_chunk++; + // Out() << "=============================================" << endl; + return cur_chunk >= num_chunks; + } + + bool TorrentCreator::calculateHash() + { + if (cur_chunk >= num_chunks) + return true; + if (files.empty()) + return calcHashSingle(); + else + return calcHashMulti(); + } + + TorrentControl* TorrentCreator::makeTC(const QString & data_dir) + { + QString dd = data_dir; + if (!dd.endsWith(bt::DirSeparator())) + dd += bt::DirSeparator(); + + // make data dir if necessary + if (!bt::Exists(dd)) + bt::MakeDir(dd); + + // save the torrent + saveTorrent(dd + "torrent"); + // write full index file + File fptr; + if (!fptr.open(dd + "index","wb")) + throw Error(i18n("Cannot create index file: %1").arg(fptr.errorString())); + + for (Uint32 i = 0;i < num_chunks;i++) + { + NewChunkHeader hdr; + hdr.index = i; + fptr.write(&hdr,sizeof(NewChunkHeader)); + } + fptr.close(); + + // now create the torrentcontrol object + TorrentControl* tc = new TorrentControl(); + try + { + // get the parent dir of target + QFileInfo fi = QFileInfo(target); + + QString odir; + StatsFile st(dd + "stats"); + if (fi.fileName() == name) + { + st.write("OUTPUTDIR", fi.dirPath(true)); + odir = fi.dirPath(true); + } + else + { + st.write("CUSTOM_OUTPUT_NAME","1"); + st.write("OUTPUTDIR", target); + odir = target; + } + st.write("UPLOADED", "0"); + st.write("RUNNING_TIME_DL","0"); + st.write("RUNNING_TIME_UL","0"); + st.write("PRIORITY", "0"); + st.write("AUTOSTART", "1"); + st.write("IMPORTED", QString::number(tot_size)); + st.writeSync(); + + tc->init(0,dd + "torrent",dd,odir,QString::null); + tc->createFiles(); + } + catch (...) + { + delete tc; + throw; + } + + return tc; + } +} diff --git a/libktorrent/torrent/torrentcreator.h b/libktorrent/torrent/torrentcreator.h new file mode 100644 index 0000000..c7057e2 --- /dev/null +++ b/libktorrent/torrent/torrentcreator.h @@ -0,0 +1,114 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTTORRENTCREATOR_H +#define BTTORRENTCREATOR_H + +#include <qstringlist.h> +#include "torrent.h" +#include <util/sha1hash.h> + +namespace bt +{ + class BEncoder; + class TorrentControl; + + /** + * @author Joris Guisson + * @brief Class to generate torrent files + * + * This class generates torrent files. + * It also allows to create a TorrentControl object, so + * that we immediately can start to share the torrent. + */ + class TorrentCreator + { + // input values + QString target; + QStringList trackers; + int chunk_size; + QString name,comments; + // calculated values + Uint32 num_chunks; + Uint64 last_size; + QValueList<TorrentFile> files; + QValueList<SHA1Hash> hashes; + // + Uint32 cur_chunk; + bool priv; + Uint64 tot_size; + bool decentralized; + public: + /** + * Constructor. + * @param target The file or directory to make a torrent of + * @param trackers A list of tracker urls + * @param chunk_size The size of each chunk + * @param name The name suggestion + * @param comments The comments field of the torrent + * @param priv Private torrent or not + */ + TorrentCreator(const QString & target,const QStringList & trackers, + Uint32 chunk_size,const QString & name, + const QString & comments,bool priv,bool decentralized); + virtual ~TorrentCreator(); + + + /** + * Calculate the hash of a chunk, this function should be called + * until it returns true. We do it this way so that the calling + * function can display a progress dialog. + * @return true if all hashes are calculated, false otherwise + */ + bool calculateHash(); + + /// Get the number of chunks + Uint32 getNumChunks() const {return num_chunks;} + + /** + * Save the torrent file. + * @param url Filename + * @throw Error if something goes wrong + */ + void saveTorrent(const QString & url); + + /** + * Make a TorrentControl object for this torrent. + * This will also create the files : + * data_dir/index + * data_dir/torrent + * data_dir/cache (symlink to target) + * @param data_dir The data directory + * @throw Error if something goes wrong + * @return The newly created object + */ + TorrentControl* makeTC(const QString & data_dir); + + private: + void saveInfo(BEncoder & enc); + void saveFile(BEncoder & enc,const TorrentFile & file); + void savePieces(BEncoder & enc); + void buildFileList(const QString & dir); + bool calcHashSingle(); + bool calcHashMulti(); + }; + +} + +#endif diff --git a/libktorrent/torrent/torrentfile.cpp b/libktorrent/torrent/torrentfile.cpp new file mode 100644 index 0000000..9c21a4a --- /dev/null +++ b/libktorrent/torrent/torrentfile.cpp @@ -0,0 +1,200 @@ +/*************************************************************************** + * Copyright (C) 2005 by * + * Joris Guisson <joris.guisson@gmail.com> * + * Ivan Vasic <ivasic@gmail.com> * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <math.h> +#include <util/log.h> +#include <util/bitset.h> +#include <util/functions.h> +#include "globals.h" +#include "torrentfile.h" + +namespace bt +{ + + TorrentFile::TorrentFile() : TorrentFileInterface(QString::null,0),missing(false),filetype(UNKNOWN) + {} + + TorrentFile::TorrentFile(Uint32 index,const QString & path, + Uint64 off,Uint64 size,Uint64 chunk_size) + : TorrentFileInterface(path,size),index(index),cache_offset(off),missing(false),filetype(UNKNOWN) + { + first_chunk = off / chunk_size; + first_chunk_off = off % chunk_size; + if (size > 0) + last_chunk = (off + size - 1) / chunk_size; + else + last_chunk = first_chunk; + last_chunk_size = (off + size) - last_chunk * chunk_size; + priority = old_priority = NORMAL_PRIORITY; + } + + TorrentFile::TorrentFile(const TorrentFile & tf) + : TorrentFileInterface(QString::null,0) + { + index = tf.getIndex(); + path = tf.getPath(); + size = tf.getSize(); + cache_offset = tf.getCacheOffset(); + first_chunk = tf.getFirstChunk(); + first_chunk_off = tf.getFirstChunkOffset(); + last_chunk = tf.getLastChunk(); + last_chunk_size = tf.getLastChunkSize(); + old_priority = priority = tf.getPriority(); + missing = tf.isMissing(); + filetype = UNKNOWN; + } + + TorrentFile::~TorrentFile() + {} + + void TorrentFile::setDoNotDownload(bool dnd) + { + if (priority != EXCLUDED && dnd) + { + if(m_emitDlStatusChanged) + old_priority = priority; + + priority = EXCLUDED; + + if(m_emitDlStatusChanged) + emit downloadPriorityChanged(this,priority,old_priority); + } + if (priority == EXCLUDED && (!dnd)) + { + if(m_emitDlStatusChanged) + old_priority = priority; + + priority = NORMAL_PRIORITY; + + if(m_emitDlStatusChanged) + emit downloadPriorityChanged(this,priority,old_priority); + } + } + + void TorrentFile::emitDownloadStatusChanged() + { + // only emit when old_priority is not equal to the new priority + if (priority != old_priority) + emit downloadPriorityChanged(this,priority,old_priority); + } + + + bool TorrentFile::isMultimedia() const + { + if (filetype == UNKNOWN) + { + if (IsMultimediaFile(getPath())) + { + filetype = MULTIMEDIA; + return true; + } + else + { + filetype = NORMAL; + return false; + } + } + return filetype == MULTIMEDIA; + } + + void TorrentFile::setPriority(Priority newpriority) + { + if(priority != newpriority) + { + if (priority == EXCLUDED) + { + setDoNotDownload(false); + } + if (newpriority == EXCLUDED) + { + setDoNotDownload(true); + } + else + { + old_priority = priority; + priority = newpriority; + emit downloadPriorityChanged(this,newpriority,old_priority); + } + } + } + + TorrentFile & TorrentFile::operator = (const TorrentFile & tf) + { + index = tf.getIndex(); + path = tf.getPath(); + size = tf.getSize(); + cache_offset = tf.getCacheOffset(); + first_chunk = tf.getFirstChunk(); + first_chunk_off = tf.getFirstChunkOffset(); + last_chunk = tf.getLastChunk(); + last_chunk_size = tf.getLastChunkSize(); + priority = tf.getPriority(); + missing = tf.isMissing(); + return *this; + } + + TorrentFile TorrentFile::null; + + + Uint64 TorrentFile::fileOffset(Uint32 cindex,Uint64 chunk_size) const + { + Uint64 off = 0; + if (getFirstChunkOffset() == 0) + { + off = (cindex - getFirstChunk()) * chunk_size; + } + else + { + if (cindex - this->getFirstChunk() > 0) + off = (cindex - this->getFirstChunk() - 1) * chunk_size; + if (cindex > 0) + off += (chunk_size - this->getFirstChunkOffset()); + } + return off; + } + + void TorrentFile::updateNumDownloadedChunks(const BitSet & bs) + { + float p = getDownloadPercentage(); + num_chunks_downloaded = 0; + bool prev = preview; + preview = true; + for (Uint32 i = first_chunk;i <= last_chunk;i++) + { + if (bs.get(i)) + { + num_chunks_downloaded++; + } + else if (i == first_chunk || i == first_chunk + 1) + { + preview = false; + } + } + preview = isMultimedia() && preview; + + float np = getDownloadPercentage(); + if (fabs(np - p) >= 0.01f) + downloadPercentageChanged(np); + + if (prev != preview) + previewAvailable(preview); + } +} +#include "torrentfile.moc" diff --git a/libktorrent/torrent/torrentfile.h b/libktorrent/torrent/torrentfile.h new file mode 100644 index 0000000..9e0c397 --- /dev/null +++ b/libktorrent/torrent/torrentfile.h @@ -0,0 +1,158 @@ +/*************************************************************************** + * Copyright (C) 2005 by * + * Joris Guisson <joris.guisson@gmail.com> * + * Ivan Vasic <ivasic@gmail.com> * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTTORRENTFILE_H +#define BTTORRENTFILE_H + +#include <qstring.h> +#include <util/constants.h> +#include <interfaces/torrentfileinterface.h> + +namespace bt +{ + class BitSet; + + /** + * @author Joris Guisson + * + * File in a multi file torrent. Keeps track of the path of the file, + * it's size, offset into the cache and between which chunks it lies. + */ + class TorrentFile : public kt::TorrentFileInterface + { + Q_OBJECT + + Uint32 index; + Uint64 cache_offset; + Uint64 first_chunk_off; + Uint64 last_chunk_size; + Priority priority; + Priority old_priority; + bool missing; + enum FileType + { + UNKNOWN, + MULTIMEDIA, + NORMAL + }; + mutable FileType filetype; + public: + /** + * Default constructor. Creates a null TorrentFile. + */ + TorrentFile(); + + /** + * Constructor. + * @param index Index number of the file + * @param path Path of the file + * @param off Offset into the torrent + * (i.e. how many bytes were all the previous files in the torrent combined) + * @param size Size of the file + * @param chunk_size Size of each chunk + */ + TorrentFile(Uint32 index,const QString & path,Uint64 off,Uint64 size,Uint64 chunk_size); + + /** + * Copy constructor. + * @param tf The TorrentFile to copy + */ + TorrentFile(const TorrentFile & tf); + virtual ~TorrentFile(); + + /// Get the index of the file + Uint32 getIndex() const {return index;} + + /// Get the offset into the torrent + Uint64 getCacheOffset() const {return cache_offset;} + + /// Get the offset at which the file starts in the first chunk + Uint64 getFirstChunkOffset() const {return first_chunk_off;} + + /// Get how many bytes the files takes up of the last chunk + Uint64 getLastChunkSize() const {return last_chunk_size;} + + /// Check if this file doesn't have to be downloaded + bool doNotDownload() const + {if(priority == EXCLUDED) return true; else return false;} + + /// Set wether we have to not download this file + void setDoNotDownload(bool dnd); + + /// Checks if this file is multimedial + bool isMultimedia() const; + + /// Gets the priority of the file + Priority getPriority() const {return priority;} + + /// Sets the priority of the file + void setPriority(Priority newpriority = NORMAL_PRIORITY); + + /// Get the previous priority value + Priority getOldPriority() const {return old_priority;} + + + /// emits signal. + void emitDownloadStatusChanged(); + + void setEmitDownloadStatusChanged(bool show) { m_emitDlStatusChanged = show; } + + /** + * Assignment operator + * @param tf The file to copy + * @return *this + */ + TorrentFile & operator = (const TorrentFile & tf); + + /// See if the file is missing + bool isMissing() const {return missing;} + + /// Set the file to be missing or not + void setMissing(bool m) {missing = m;} + + /** + * Calculate the offset of a chunk in the file + * @param cindex Index of chunk + * @param chunk_size Size of each chunk + */ + Uint64 fileOffset(Uint32 cindex,Uint64 chunk_size) const; + + static TorrentFile null; + + /** + * Update the number of downloaded chunks for this file. + * @param bs The current bitset of all chunks + */ + void updateNumDownloadedChunks(const BitSet & bs); + + signals: + /** + * Signal emitted when the Priority variable changes. + * @param tf The TorrentFile which emitted the signal + * @param newpriority THe new priority of the file + * @param oldpriority Previous priority + */ + void downloadPriorityChanged(TorrentFile* tf,Priority newpriority,Priority oldpriority); + + }; + +} + +#endif diff --git a/libktorrent/torrent/torrentmonitor.cpp b/libktorrent/torrent/torrentmonitor.cpp new file mode 100644 index 0000000..ff33acb --- /dev/null +++ b/libktorrent/torrent/torrentmonitor.cpp @@ -0,0 +1,33 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include "torrentmonitor.h" + +namespace bt +{ + + TorrentMonitor::TorrentMonitor() + {} + + + TorrentMonitor::~TorrentMonitor() + {} + + +} diff --git a/libktorrent/torrent/torrentmonitor.h b/libktorrent/torrent/torrentmonitor.h new file mode 100644 index 0000000..52e3835 --- /dev/null +++ b/libktorrent/torrent/torrentmonitor.h @@ -0,0 +1,47 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTTORRENTMONITOR_H +#define BTTORRENTMONITOR_H + +namespace bt +{ + class Peer; + class ChunkDownload; + + /** + @author Joris Guisson + */ + class TorrentMonitor + { + public: + TorrentMonitor(); + virtual ~TorrentMonitor(); + + virtual void peerAdded(Peer* peer) = 0; + virtual void peerRemoved(Peer* peer) = 0; + virtual void downloadStarted(ChunkDownload* cd) = 0; + virtual void downloadRemoved(ChunkDownload* cd) = 0; + virtual void stopped() = 0; + virtual void destroyed() = 0; + }; + +} + +#endif diff --git a/libktorrent/torrent/tracker.cpp b/libktorrent/torrent/tracker.cpp new file mode 100644 index 0000000..261169c --- /dev/null +++ b/libktorrent/torrent/tracker.cpp @@ -0,0 +1,93 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <stdlib.h> +#include <time.h> +#include <kurl.h> +#include <kresolver.h> +#include <util/functions.h> +#include <util/log.h> +#include <torrent/globals.h> +#include <interfaces/torrentinterface.h> +#include <kademlia/dhtbase.h> +#include <kademlia/dhttrackerbackend.h> +#include "server.h" +#include "tracker.h" +#include "udptracker.h" +#include "httptracker.h" + +using namespace KNetwork; + +namespace bt +{ + static QString custom_ip; + static QString custom_ip_resolved; + + Tracker::Tracker(const KURL & url,kt::TorrentInterface* tor,const PeerID & id,int tier) + : url(url),tier(tier),peer_id(id),tor(tor) + { + // default 5 minute interval + interval = 5 * 60 * 1000; + seeders = leechers = 0; + srand(time(0)); + key = rand(); + started = false; + } + + Tracker::~Tracker() + { + } + + void Tracker::setCustomIP(const QString & ip) + { + if (custom_ip == ip) + return; + + Out(SYS_TRK|LOG_NOTICE) << "Setting custom ip to " << ip << endl; + custom_ip = ip; + custom_ip_resolved = QString::null; + if (ip.isNull()) + return; + + KResolverResults res = KResolver::resolve(ip,QString::null); + if (res.error() || res.empty()) + { + custom_ip = custom_ip_resolved = QString::null; + } + else + { + custom_ip_resolved = res.first().address().nodeName(); + Out(SYS_TRK|LOG_NOTICE) << "custom_ip_resolved = " << custom_ip_resolved << endl; + } + } + + QString Tracker::getCustomIP() + { + return custom_ip_resolved; + } + + void Tracker::timedDelete(int ms) + { + QTimer::singleShot(ms,this,SLOT(deleteLater())); + connect(this,SIGNAL(stopDone()),this,SLOT(deleteLater())); + } + +} + +#include "tracker.moc" diff --git a/libktorrent/torrent/tracker.h b/libktorrent/torrent/tracker.h new file mode 100644 index 0000000..d254b63 --- /dev/null +++ b/libktorrent/torrent/tracker.h @@ -0,0 +1,136 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTTRACKER_H +#define BTTRACKER_H + +#include <kurl.h> +#include <util/sha1hash.h> +#include <interfaces/peersource.h> +#include "globals.h" +#include "peerid.h" + +class KURL; + +namespace kt +{ + class TorrentInterface; +} + + +namespace bt +{ + class Tracker; + + /** + * Base class for all tracker classes. + */ + class Tracker : public kt::PeerSource + { + Q_OBJECT + public: + Tracker(const KURL & url,kt::TorrentInterface* tor,const PeerID & id,int tier); + virtual ~Tracker(); + + /// See if a start request succeeded + bool isStarted() const {return started;} + + /** + * Set the custom IP + * @param str + */ + static void setCustomIP(const QString & str); + + /// get the tracker url + KURL trackerURL() const {return url;} + + /** + * Delete the tracker in ms milliseconds, or when the stopDone signal is emitted. + * @param ms Number of ms to wait + */ + void timedDelete(int ms); + + /** + * Get the number of failed attempts to reach a tracker. + * @return The number of failed attempts + */ + virtual Uint32 failureCount() const = 0; + + /** + * Do a tracker scrape to get more accurate stats about a torrent. + * Does nothing if the tracker does not support this. + */ + virtual void scrape() = 0; + + /// Get the trackers tier + int getTier() const {return tier;} + + /** + * Get the update interval in ms + * @return interval + */ + Uint32 getInterval() const {return interval;} + + /// Set the interval + void setInterval(Uint32 i) {interval = i;} + + /// Get the number of seeders + Uint32 getNumSeeders() const {return seeders;} + + /// Get the number of leechers + Uint32 getNumLeechers() const {return leechers;} + + /// Get the custom ip to use, null if none is set + static QString getCustomIP(); + signals: + /** + * Emitted when an error happens. + * @param failure_reason The reason why we couldn't reach the tracker + */ + void requestFailed(const QString & failure_reason); + + /** + * Emitted when a stop is done. + */ + void stopDone(); + + /** + * Emitted when a request to the tracker succeeded + */ + void requestOK(); + + /** + * A request to the tracker has been started. + */ + void requestPending(); + + protected: + KURL url; + int tier; + PeerID peer_id; + kt::TorrentInterface* tor; + Uint32 interval,seeders,leechers,key; + bool started; + private: + //static QString custom_ip,custom_ip_resolved; + }; + +} + +#endif diff --git a/libktorrent/torrent/udptracker.cpp b/libktorrent/torrent/udptracker.cpp new file mode 100644 index 0000000..2dd4a01 --- /dev/null +++ b/libktorrent/torrent/udptracker.cpp @@ -0,0 +1,291 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <stdlib.h> +#include <kresolver.h> +#include <util/functions.h> +#include <util/log.h> +#include <ksocketaddress.h> +#include "peermanager.h" +#include "udptracker.h" +#include "torrentcontrol.h" +#include "globals.h" +#include "server.h" +#include "udptrackersocket.h" + + +using namespace kt; +using namespace KNetwork; + +namespace bt +{ + + UDPTrackerSocket* UDPTracker::socket = 0; + Uint32 UDPTracker::num_instances = 0; + + + UDPTracker::UDPTracker(const KURL & url,kt::TorrentInterface* tor,const PeerID & id,int tier) + : Tracker(url,tor,id,tier) + { + num_instances++; + if (!socket) + socket = new UDPTrackerSocket(); + + connection_id = 0; + transaction_id = 0; + interval = 0; + + connect(&conn_timer,SIGNAL(timeout()),this,SLOT(onConnTimeout())); + connect(socket,SIGNAL(announceRecieved(Int32, const QByteArray &)), + this,SLOT(announceRecieved(Int32, const QByteArray& ))); + connect(socket,SIGNAL(connectRecieved(Int32, Int64 )), + this,SLOT(connectRecieved(Int32, Int64 ))); + connect(socket,SIGNAL(error(Int32, const QString& )), + this,SLOT(onError(Int32, const QString& ))); + + KResolver::resolveAsync(this,SLOT(onResolverResults(KResolverResults )), + url.host(),QString::number(url.port())); + } + + + UDPTracker::~UDPTracker() + { + num_instances--; + if (num_instances == 0) + { + delete socket; + socket = 0; + } + } + + void UDPTracker::start() + { + event = STARTED; + conn_timer.stop(); + doRequest(); + } + + void UDPTracker::stop(WaitJob* ) + { + if (!started) + return; + + event = STOPPED; + conn_timer.stop(); + doRequest(); + started = false; + } + + void UDPTracker::completed() + { + event = COMPLETED; + conn_timer.stop(); + doRequest(); + } + + void UDPTracker::manualUpdate() + { + conn_timer.stop(); + if (!started) + event = STARTED; + doRequest(); + } + + void UDPTracker::connectRecieved(Int32 tid,Int64 cid) + { + if (tid != transaction_id) + return; + + connection_id = cid; + n = 0; + sendAnnounce(); + } + + void UDPTracker::announceRecieved(Int32 tid,const QByteArray & data) + { + if (tid != transaction_id) + return; + + const Uint8* buf = (const Uint8*)data.data(); + + /* + 0 32-bit integer action 1 + 4 32-bit integer transaction_id + 8 32-bit integer interval + 12 32-bit integer leechers + 16 32-bit integer seeders + 20 + 6 * n 32-bit integer IP address + 24 + 6 * n 16-bit integer TCP port + 20 + 6 * N + */ + interval = ReadInt32(buf,8); + leechers = ReadInt32(buf,12); + seeders = ReadInt32(buf,16); + + Uint32 nip = leechers + seeders; + Uint32 j = 0; + for (Uint32 i = 20;i < data.size() && j < nip;i+=6,j++) + { + Uint32 ip = ReadUint32(buf,i); + addPeer(QString("%1.%2.%3.%4") + .arg((ip & (0xFF000000)) >> 24) + .arg((ip & (0x00FF0000)) >> 16) + .arg((ip & (0x0000FF00)) >> 8) + .arg(ip & 0x000000FF), + ReadUint16(buf,i+4)); + } + + peersReady(this); + connection_id = 0; + conn_timer.stop(); + if (event != STOPPED) + { + if (event == STARTED) + started = true; + event = NONE; + requestOK(); + } + else + { + stopDone(); + requestOK(); + } + } + + void UDPTracker::onError(Int32 tid,const QString & error_string) + { + if (tid != transaction_id) + return; + + Out(SYS_TRK|LOG_IMPORTANT) << "UDPTracker::error : " << error_string << endl; + requestFailed(error_string); + } + + + bool UDPTracker::doRequest() + { + Out(SYS_TRK|LOG_NOTICE) << "Doing tracker request to url : " << url << endl; + if (connection_id == 0) + { + n = 0; + sendConnect(); + } + else + sendAnnounce(); + + requestPending(); + return true; + } + + void UDPTracker::scrape() + { + } + + void UDPTracker::sendConnect() + { + transaction_id = socket->newTransactionID(); + socket->sendConnect(transaction_id,address); + int tn = 1; + for (int i = 0;i < n;i++) + tn *= 2; + conn_timer.start(60000 * tn,true); + } + + void UDPTracker::sendAnnounce() + { + // Out(SYS_TRK|LOG_NOTICE) << "UDPTracker::sendAnnounce()" << endl; + transaction_id = socket->newTransactionID(); + /* + 0 64-bit integer connection_id + 8 32-bit integer action 1 + 12 32-bit integer transaction_id + 16 20-byte string info_hash + 36 20-byte string peer_id + 56 64-bit integer downloaded + 64 64-bit integer left + 72 64-bit integer uploaded + 80 32-bit integer event + 84 32-bit integer IP address 0 + 88 32-bit integer key + 92 32-bit integer num_want -1 + 96 16-bit integer port + 98 + */ + + Uint32 ev = event; + const TorrentStats & s = tor->getStats(); + Uint16 port = Globals::instance().getServer().getPortInUse(); + Uint8 buf[98]; + WriteInt64(buf,0,connection_id); + WriteInt32(buf,8,ANNOUNCE); + WriteInt32(buf,12,transaction_id); + const SHA1Hash & info_hash = tor->getInfoHash(); + memcpy(buf+16,info_hash.getData(),20); + memcpy(buf+36,peer_id.data(),20); + WriteInt64(buf,56,s.trk_bytes_downloaded); + if (ev == COMPLETED) + WriteInt64(buf,64,0); + else + WriteInt64(buf,64,s.bytes_left); + WriteInt64(buf,72,s.trk_bytes_uploaded); + WriteInt32(buf,80,ev); + QString cip = Tracker::getCustomIP(); + if (cip.isNull()) + { + WriteUint32(buf,84,0); + } + else + { + KNetwork::KIpAddress addr(cip); + WriteUint32(buf,84,addr.IPv4Addr(true)); + } + WriteUint32(buf,88,key); + if (ev != STOPPED) + WriteInt32(buf,92,100); + else + WriteInt32(buf,92,0); + WriteUint16(buf,96,port); + + socket->sendAnnounce(transaction_id,buf,address); + } + + void UDPTracker::onConnTimeout() + { + if (connection_id) + { + connection_id = 0; + n++; + if (event != STOPPED) + sendConnect(); + else + stopDone(); + } + else + { + doRequest(); + } + } + + void UDPTracker::onResolverResults(KResolverResults res) + { + address = res.front().address(); + } + +} +#include "udptracker.moc" diff --git a/libktorrent/torrent/udptracker.h b/libktorrent/torrent/udptracker.h new file mode 100644 index 0000000..5107fb9 --- /dev/null +++ b/libktorrent/torrent/udptracker.h @@ -0,0 +1,105 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTUDPTRACKER_H +#define BTUDPTRACKER_H + +#include <kurl.h> +#include <qvaluelist.h> +#include <qcstring.h> +#include <qtimer.h> +#include <ksocketaddress.h> +#include "tracker.h" +#include "globals.h" +#include "peermanager.h" + + + +namespace KNetwork +{ + class KResolverResults; +} + + +namespace bt +{ + using KNetwork::KResolverResults; + + enum Event + { + NONE = 0, + COMPLETED = 1, + STARTED = 2, + STOPPED = 3 + }; + + class UDPTrackerSocket; + + /** + * @author Joris Guisson + * @brief Communicates with an UDP tracker + * + * This class is able to communicate with an UDP tracker. + * This is an implementation of the protocol described in + * http://xbtt.sourceforge.net/udp_tracker_protocol.html + */ + class UDPTracker : public Tracker + { + Q_OBJECT + public: + UDPTracker(const KURL & url,kt::TorrentInterface* tor,const PeerID & id,int tier); + virtual ~UDPTracker(); + + virtual void start(); + virtual void stop(WaitJob* wjob = 0); + virtual void completed(); + virtual void manualUpdate(); + virtual Uint32 failureCount() const {return n;} + virtual void scrape(); + + private slots: + void onConnTimeout(); + void connectRecieved(Int32 tid,Int64 connection_id); + void announceRecieved(Int32 tid,const QByteArray & buf); + void onError(Int32 tid,const QString & error_string); + void onResolverResults(KResolverResults res); + + private: + void sendConnect(); + void sendAnnounce(); + bool doRequest(); + + private: + KNetwork::KSocketAddress address; + + Int32 transaction_id; + Int64 connection_id; + + Uint32 data_read; + int n; + QTimer conn_timer; + Event event; + + static UDPTrackerSocket* socket; + static Uint32 num_instances; + }; + +} + +#endif diff --git a/libktorrent/torrent/udptrackersocket.cpp b/libktorrent/torrent/udptrackersocket.cpp new file mode 100644 index 0000000..43ef2b6 --- /dev/null +++ b/libktorrent/torrent/udptrackersocket.cpp @@ -0,0 +1,222 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <time.h> +#include <stdlib.h> +#include <unistd.h> +#include <util/array.h> +#include <ksocketaddress.h> +#include <kdatagramsocket.h> +#include <ksocketdevice.h> +#include <net/portlist.h> +#include <util/log.h> +#include <util/functions.h> +#include <klocale.h> +#include <kmessagebox.h> +#include "globals.h" +#include "udptrackersocket.h" + +using namespace KNetwork; + +namespace bt +{ + Uint16 UDPTrackerSocket::port = 4444; + + UDPTrackerSocket::UDPTrackerSocket() + { + sock = new KNetwork::KDatagramSocket(this); + sock->setAddressReuseable(true); + connect(sock,SIGNAL(readyRead()),this,SLOT(dataReceived())); + int i = 0; + if (port == 0) + port = 4444; + + bool bound = false; + + while (!(bound = sock->bind(QString::null,QString::number(port + i))) && i < 10) + { + Out() << "Failed to bind socket to port " << (port+i) << endl; + i++; + } + + + if (!bound) + { + KMessageBox::error(0, + i18n("Cannot bind to udp port %1 or the 10 following ports.").arg(port)); + } + else + { + port = port + i; + Globals::instance().getPortList().addNewPort(port,net::UDP,true); + } + } + + + UDPTrackerSocket::~UDPTrackerSocket() + { + Globals::instance().getPortList().removePort(port,net::UDP); + delete sock; + } + + void UDPTrackerSocket::sendConnect(Int32 tid,const KNetwork::KSocketAddress & addr) + { + Int64 cid = 0x41727101980LL; + Uint8 buf[16]; + + WriteInt64(buf,0,cid); + WriteInt32(buf,8,CONNECT); + WriteInt32(buf,12,tid); + + sock->send(KDatagramPacket((char*)buf,16,addr)); + transactions.insert(tid,CONNECT); + } + + void UDPTrackerSocket::sendAnnounce(Int32 tid,const Uint8* data,const KNetwork::KSocketAddress & addr) + { + transactions.insert(tid,ANNOUNCE); + sock->send(KDatagramPacket((char*)data,98,addr)); + } + + void UDPTrackerSocket::cancelTransaction(Int32 tid) + { + transactions.remove(tid); + } + + void UDPTrackerSocket::handleConnect(const QByteArray & data) + { + const Uint8* buf = (const Uint8*)data.data(); + + // Read the transaction_id and check it + Int32 tid = ReadInt32(buf,4); + QMap<Int32,Action>::iterator i = transactions.find(tid); + // if we can't find the transaction, just return + if (i == transactions.end()) + { + return; + } + + // check wether the transaction is a CONNECT + if (i.data() != CONNECT) + { + transactions.erase(i); + error(tid,QString::null); + return; + } + + // everything ok, emit signal + transactions.erase(i); + connectRecieved(tid,ReadInt64(buf,8)); + } + + void UDPTrackerSocket::handleAnnounce(const QByteArray & data) + { + const Uint8* buf = (const Uint8*)data.data(); + + // Read the transaction_id and check it + Int32 tid = ReadInt32(buf,4); + QMap<Int32,Action>::iterator i = transactions.find(tid); + // if we can't find the transaction, just return + if (i == transactions.end()) + return; + + // check wether the transaction is a ANNOUNCE + if (i.data() != ANNOUNCE) + { + transactions.erase(i); + error(tid,QString::null); + return; + } + + // everything ok, emit signal + transactions.erase(i); + announceRecieved(tid,data); + } + + void UDPTrackerSocket::handleError(const QByteArray & data) + { + const Uint8* buf = (const Uint8*)data.data(); + // Read the transaction_id and check it + Int32 tid = ReadInt32(buf,4); + QMap<Int32,Action>::iterator it = transactions.find(tid); + // if we can't find the transaction, just return + if (it == transactions.end()) + return; + + // extract error message + transactions.erase(it); + QString msg; + for (Uint32 i = 8;i < data.size();i++) + msg += (char)buf[i]; + + // emit signal + error(tid,msg); + } + + void UDPTrackerSocket::dataReceived() + { + if (sock->bytesAvailable() == 0) + { + Out(SYS_TRK|LOG_NOTICE) << "0 byte UDP packet " << endl; + // KDatagramSocket wrongly handles UDP packets with no payload + // so we need to deal with it oursleves + int fd = sock->socketDevice()->socket(); + char tmp; + read(fd,&tmp,1); + return; + } + + KDatagramPacket pck = sock->receive(); + const QByteArray & data = pck.data(); + const Uint8* buf = (const Uint8*)data.data(); + Uint32 type = ReadUint32(buf,0); + switch (type) + { + case CONNECT: + handleConnect(data); + break; + case ANNOUNCE: + handleAnnounce(data); + break; + case ERROR: + handleError(data); + break; + } + } + + Int32 UDPTrackerSocket::newTransactionID() + { + Int32 transaction_id = rand() * time(0); + while (transactions.contains(transaction_id)) + transaction_id++; + return transaction_id; + } + + void UDPTrackerSocket::setPort(Uint16 p) + { + port = p; + } + + Uint16 UDPTrackerSocket::getPort() + { + return port; + } +} + +#include "udptrackersocket.moc" diff --git a/libktorrent/torrent/udptrackersocket.h b/libktorrent/torrent/udptrackersocket.h new file mode 100644 index 0000000..1537598 --- /dev/null +++ b/libktorrent/torrent/udptrackersocket.h @@ -0,0 +1,139 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTUDPTRACKERSOCKET_H +#define BTUDPTRACKERSOCKET_H + +#include <qobject.h> +#include <qmap.h> +#include <qcstring.h> +#include <util/constants.h> + + +namespace KNetwork +{ + class KDatagramSocket; + class KSocketAddress; +} + +namespace bt +{ + + + enum Action + { + CONNECT = 0, + ANNOUNCE = 1, + SCRAPE = 2, + ERROR = 3 + }; + + + + /** + * @author Joris Guisson + * + * Class which handles communication with one or more UDP trackers. + */ + class UDPTrackerSocket : public QObject + { + Q_OBJECT + public: + UDPTrackerSocket(); + virtual ~UDPTrackerSocket(); + + /** + * Send a connect message. As a response to this, the connectRecieved + * signal will be emitted, classes recieving this signal should check if + * the transaction_id is the same. + * @param tid The transaction_id + * @param addr The address to send to + */ + void sendConnect(Int32 tid,const KNetwork::KSocketAddress & addr); + + /** + * Send an announce message. As a response to this, the announceRecieved + * signal will be emitted, classes recieving this signal should check if + * the transaction_id is the same. + * @param tid The transaction_id + * @param data The data to send (connect input structure, in UDP Tracker specifaction) + * @param addr The address to send to + */ + void sendAnnounce(Int32 tid,const Uint8* data,const KNetwork::KSocketAddress & addr); + + /** + * If a transaction times out, this should be used to cancel it. + * @param tid + */ + void cancelTransaction(Int32 tid); + + + /** + * Compute a free transaction_id. + * @return A free transaction_id + */ + Int32 newTransactionID(); + + /** + * Set the port ot use. + * @param p The port + */ + static void setPort(Uint16 p); + + /// Get the port in use. + static Uint16 getPort(); + private slots: + void dataReceived(); + + signals: + /** + * Emitted when a connect message is received. + * @param tid The transaction_id + * @param connection_id The connection_id returned + */ + void connectRecieved(Int32 tid,Int64 connection_id); + + /** + * Emitted when an announce message is received. + * @param tid The transaction_id + * @param buf The data + */ + void announceRecieved(Int32 tid,const QByteArray & buf); + + /** + * Signal emitted, when an error occurs during a transaction. + * @param tid The transaction_id + * @param error_string Potential error string + */ + void error(Int32 tid,const QString & error_string); + + private: + void handleConnect(const QByteArray & buf); + void handleAnnounce(const QByteArray & buf); + void handleError(const QByteArray & buf); + + private: + Uint16 udp_port; + KNetwork::KDatagramSocket* sock; + QMap<Int32,Action> transactions; + static Uint16 port; + }; +} + +#endif diff --git a/libktorrent/torrent/uploadcap.cpp b/libktorrent/torrent/uploadcap.cpp new file mode 100644 index 0000000..701d854 --- /dev/null +++ b/libktorrent/torrent/uploadcap.cpp @@ -0,0 +1,46 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#if 0 +#include <math.h> +#include <util/log.h> +#include <torrent/globals.h> +#include "uploadcap.h" +#include "peer.h" +#include "packetwriter.h" + +namespace bt +{ + + UploadCap UploadCap::self; + + UploadCap::UploadCap() : Cap(false) + { + } + + UploadCap::~UploadCap() + { + } + + + + +} +#endif + diff --git a/libktorrent/torrent/uploadcap.h b/libktorrent/torrent/uploadcap.h new file mode 100644 index 0000000..f766f59 --- /dev/null +++ b/libktorrent/torrent/uploadcap.h @@ -0,0 +1,56 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTUPLOADCAP_H +#define BTUPLOADCAP_H + +// DEPRECATED +#if 0 +#include "cap.h" + +namespace bt +{ + class PacketWriter; + + + + /** + * @author Joris Guisson + * @brief Keeps the upload rate under control + * + * Before a PeerUploader can send a piece, it must first ask + * permission to a UploadCap object. This object will make sure + * that the upload rate remains under a specified threshold. When the + * threshold is set to 0, no upload capping will be done. + */ + class UploadCap : public Cap + { + static UploadCap self; + + UploadCap(); + public: + virtual ~UploadCap(); + + + static UploadCap & instance() {return self;} + }; + +} +#endif +#endif diff --git a/libktorrent/torrent/uploader.cpp b/libktorrent/torrent/uploader.cpp new file mode 100644 index 0000000..0cf3677 --- /dev/null +++ b/libktorrent/torrent/uploader.cpp @@ -0,0 +1,67 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <util/log.h> +#include "uploader.h" +#include "peer.h" +#include "chunkmanager.h" +#include "request.h" +#include "uploader.h" +#include "peeruploader.h" +#include "peermanager.h" + + +namespace bt +{ + + Uploader::Uploader(ChunkManager & cman,PeerManager & pman) + : cman(cman),pman(pman),uploaded(0) + {} + + + Uploader::~Uploader() + { + } + + + + void Uploader::update(Uint32 opt_unchoked) + { + for (Uint32 i = 0;i < pman.getNumConnectedPeers();++i) + { + PeerUploader* p = pman.getPeer(i)->getPeerUploader(); + uploaded += p->update(cman,opt_unchoked); + } + } + + + Uint32 Uploader::uploadRate() const + { + Uint32 rate = 0; + for (Uint32 i = 0;i < pman.getNumConnectedPeers();++i) + { + const Peer* p = pman.getPeer(i); + rate += p->getUploadRate(); + } + return rate; + } + + +} +#include "uploader.moc" diff --git a/libktorrent/torrent/uploader.h b/libktorrent/torrent/uploader.h new file mode 100644 index 0000000..4370d69 --- /dev/null +++ b/libktorrent/torrent/uploader.h @@ -0,0 +1,75 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTUPLOADER_H +#define BTUPLOADER_H + +#include <qobject.h> +#include "globals.h" + +namespace bt +{ + class Peer; + class PeerID; + class ChunkManager; + class Request; + class PeerManager; + + + /** + * @author Joris Guisson + * + * Class which manages the uploading of data. It has a PeerUploader for + * each Peer. + */ + class Uploader : public QObject + { + Q_OBJECT + public: + /** + * Constructor, sets the ChunkManager. + * @param cman The ChunkManager + */ + Uploader(ChunkManager & cman,PeerManager & pman); + virtual ~Uploader(); + + /// Get the number of bytes uploaded. + Uint64 bytesUploaded() const {return uploaded;} + + /// Get the upload rate of all Peers combined. + Uint32 uploadRate() const; + + /// Set the number of bytes which have been uploaded. + void setBytesUploaded(Uint64 b) {uploaded = b;} + public slots: + /** + * Update every PeerUploader. + * @param opt_unchoked ID of optimisticly unchoked peer + */ + void update(Uint32 opt_unchoked); + + private: + ChunkManager & cman; + PeerManager & pman; + Uint64 uploaded; + }; + +} + +#endif diff --git a/libktorrent/torrent/upspeedestimater.cpp b/libktorrent/torrent/upspeedestimater.cpp new file mode 100644 index 0000000..0d6c544 --- /dev/null +++ b/libktorrent/torrent/upspeedestimater.cpp @@ -0,0 +1,148 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <math.h> +#include <util/functions.h> +#include <util/log.h> +#include <torrent/globals.h> +#include "upspeedestimater.h" + +namespace bt +{ + + UpSpeedEstimater::UpSpeedEstimater() + { + accumulated_bytes = 0; + upload_rate = 0.0; + proto_upload_rate = 0.0; + } + + + UpSpeedEstimater::~UpSpeedEstimater() + {} + + + void UpSpeedEstimater::writeBytes(Uint32 bytes,bool proto) + { + // add entry to outstanding_bytes + Entry e; + e.bytes = bytes; + e.data = !proto; + e.start_time = GetCurrentTime(); + outstanding_bytes.append(e); + } + + void UpSpeedEstimater::bytesWritten(Uint32 bytes) + { + QValueList<Entry>::iterator i = outstanding_bytes.begin(); + TimeStamp now = GetCurrentTime(); + while (bytes > 0 && i != outstanding_bytes.end()) + { + Entry e = *i; + if (e.bytes <= bytes + accumulated_bytes) + { + // first remove outstanding bytes + i = outstanding_bytes.erase(i); + bytes -= e.bytes; + accumulated_bytes = 0; + if (e.data) + { + // if it's data move it to the written_bytes list + // but first store time it takes to send in e.t + e.duration = now - e.start_time; + written_bytes.append(e); + } + else + { + e.duration = now - e.start_time; +#ifdef MEASURE_PROTO_OVERHEAD + proto_bytes.append(e); +#endif + } + } + else + { + accumulated_bytes += bytes; + bytes = 0; + } + } + } + + double UpSpeedEstimater::rate(QValueList<Entry> & el) + { + TimeStamp now = GetCurrentTime(); + const Uint32 INTERVAL = 3000; + + Uint32 tot_bytes = 0; + Uint32 oldest_time = now; + + QValueList<Entry>::iterator i = el.begin(); + while (i != el.end()) + { + Entry & e = *i; + Uint32 end_time = e.start_time + e.duration; + + if (now - end_time > INTERVAL) + { + // get rid of old entries + i = el.erase(i); + } + else if (now - e.start_time <= INTERVAL) + { + // entry was fully sent in the last 3 seconds + // so fully add it + tot_bytes += e.bytes; + if (e.start_time < oldest_time) + oldest_time = e.start_time; + i++; + } + else + { + // entry was partially sent in the last 3 seconds + // so we need to take into account a part of the bytes; + Uint32 part_dur = end_time - (now - INTERVAL); + double dur_perc = (double)part_dur / e.duration; + tot_bytes += (Uint32)ceil(dur_perc * e.bytes); + oldest_time = (now - INTERVAL); + i++; + } + } + + return (double)tot_bytes / (INTERVAL * 0.001); + } + + void UpSpeedEstimater::update() + { + if (!written_bytes.empty()) + { + upload_rate = 0; + upload_rate = rate(written_bytes); + } + + +#ifdef MEASURE_PROTO_OVERHEAD + if (!proto_bytes.empty()) + { + proto_upload_rate = 0; + proto_upload_rate = rate(proto_bytes); + } +#endif + } + +} diff --git a/libktorrent/torrent/upspeedestimater.h b/libktorrent/torrent/upspeedestimater.h new file mode 100644 index 0000000..6503499 --- /dev/null +++ b/libktorrent/torrent/upspeedestimater.h @@ -0,0 +1,86 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTUPSPEEDESTIMATER_H +#define BTUPSPEEDESTIMATER_H + +#include <qvaluelist.h> +#include <util/constants.h> + +namespace bt +{ + + /** + * @author Joris Guisson + * + * Measures upload speed. + */ + class UpSpeedEstimater + { + struct Entry + { + Uint32 bytes; + TimeStamp start_time; + Uint32 duration; + bool data; + }; + public: + UpSpeedEstimater(); + virtual ~UpSpeedEstimater(); + + /** + * Start sending bytes. + * @param bytes The number of bytes + * @param rec Wether to record or not (i.e. is this data) + */ + void writeBytes(Uint32 bytes,bool rec); + + /** + * The socket has finished sending bytes. + * @param bytes The number of bytes. + */ + void bytesWritten(Uint32 bytes); + + /** + * Update the upload speed estimater. + */ + void update(); + + /// Get the upload rate + double uploadRate() const {return upload_rate;} + + /// Get the protocol overhead + double protocollOverhead() const {return proto_upload_rate;} + private: + double rate(QValueList<Entry> & el); + + private: + double upload_rate; + double proto_upload_rate; + Uint32 accumulated_bytes; + QValueList<Entry> outstanding_bytes; + QValueList<Entry> written_bytes; +#ifdef MEASURE_PROTO_OVERHEAD + QValueList<Entry> proto_bytes; +#endif + }; + +} + +#endif diff --git a/libktorrent/torrent/utpex.cpp b/libktorrent/torrent/utpex.cpp new file mode 100644 index 0000000..4933218 --- /dev/null +++ b/libktorrent/torrent/utpex.cpp @@ -0,0 +1,155 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <net/address.h> +#include <util/functions.h> +#include <util/log.h> +#include "utpex.h" +#include "peer.h" +#include "packetwriter.h" +#include "bdecoder.h" +#include "bencoder.h" +#include "bnode.h" +#include "peermanager.h" + + +namespace bt +{ + + UTPex::UTPex(Peer* peer,Uint32 id) : peer(peer),id(id),last_updated(0) + {} + + + UTPex::~UTPex() + {} + + + + void UTPex::handlePexPacket(const Uint8* packet,Uint32 size) + { + if (size <= 2 || packet[1] != 1) + return; + + QByteArray tmp; + tmp.setRawData((const char*)packet,size); + BNode* node = 0; + try + { + BDecoder dec(tmp,false,2); + node = dec.decode(); + if (node && node->getType() == BNode::DICT) + { + BDictNode* dict = (BDictNode*)node; + + // ut_pex packet, emit signal to notify PeerManager + BValueNode* val = dict->getValue("added"); + if (val) + { + QByteArray data = val->data().toByteArray(); + peer->emitPex(data); + } + } + } + catch (...) + { + // just ignore invalid packets + Out(SYS_CON|LOG_DEBUG) << "Invalid extended packet" << endl; + } + delete node; + tmp.resetRawData((const char*)packet,size); + } + + bool UTPex::needsUpdate() const + { + return bt::GetCurrentTime() - last_updated >= 60*1000; + } + + void UTPex::update(PeerManager* pman) + { + last_updated = bt::GetCurrentTime(); + + std::map<Uint32,net::Address> added; + std::map<Uint32,net::Address> npeers; + + PeerManager::CItr itr = pman->beginPeerList(); + while (itr != pman->endPeerList()) + { + const Peer* p = *itr; + if (p != peer) + { + npeers.insert(std::make_pair(p->getID(),p->getAddress())); + if (peers.count(p->getID()) == 0) + { + // new one, add to added + added.insert(std::make_pair(p->getID(),p->getAddress())); + } + else + { + // erase from old list, so only the dropped ones are left + peers.erase(p->getID()); + } + } + itr++; + } + + if (!(peers.size() == 0 && added.size() == 0)) + { + // encode the whole lot + QByteArray data; + BEncoder enc(new BEncoderBufferOutput(data)); + enc.beginDict(); + enc.write("added"); + encode(enc,added); + enc.write("added.f"); // no idea what this added.f thing means + enc.write(""); + enc.write("dropped"); + encode(enc,peers); + enc.end(); + + peer->getPacketWriter().sendExtProtMsg(id,data); + } + + peers = npeers; + } + + void UTPex::encode(BEncoder & enc,const std::map<Uint32,net::Address> & ps) + { + if (ps.size() == 0) + { + enc.write(""); + return; + } + + Uint8* buf = new Uint8[ps.size() * 6]; + Uint32 size = 0; + + std::map<Uint32,net::Address>::const_iterator i = ps.begin(); + while (i != ps.end()) + { + const net::Address & addr = i->second; + WriteUint32(buf,size,addr.ip()); + WriteUint16(buf,size + 4,addr.port()); + size += 6; + i++; + } + + enc.write(buf,size); + delete [] buf; + } +} diff --git a/libktorrent/torrent/utpex.h b/libktorrent/torrent/utpex.h new file mode 100644 index 0000000..cab2f39 --- /dev/null +++ b/libktorrent/torrent/utpex.h @@ -0,0 +1,71 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTUTPEX_H +#define BTUTPEX_H + +#include <map> +#include <net/address.h> +#include <util/constants.h> + +namespace bt +{ + class Peer; + class PeerManager; + class BEncoder; + + /** + * @author Joris Guisson <joris.guisson@gmail.com> + * + * Class which handles µTorrent's peer exchange + */ + class UTPex + { + public: + UTPex(Peer* peer,Uint32 id); + virtual ~UTPex(); + + /** + * Handle a PEX packet + * @param packet The packet + * @param size The size of the packet + */ + void handlePexPacket(const Uint8* packet,Uint32 size); + + /// Do we need to update PEX (should happen every minute) + bool needsUpdate() const; + + /// Send a new PEX packet to the Peer + void update(PeerManager* pman); + + /// Change the ID used in the extended packets + void changeID(Uint32 nid) {id = nid;} + private: + void encode(BEncoder & enc,const std::map<Uint32,net::Address> & ps); + + private: + Peer* peer; + Uint32 id; + std::map<Uint32,net::Address> peers; + TimeStamp last_updated; + }; + +} + +#endif diff --git a/libktorrent/torrent/value.cpp b/libktorrent/torrent/value.cpp new file mode 100644 index 0000000..df063ab --- /dev/null +++ b/libktorrent/torrent/value.cpp @@ -0,0 +1,91 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include <qtextcodec.h> +#include "value.h" + +namespace bt +{ + + Value::Value() : type(INT),ival(0),big_ival(0) + {} + + Value::Value(int val) : type(INT),ival(val),big_ival(val) + {} + + Value::Value(Int64 val) : type(INT64),big_ival(val) + {} + + Value::Value(const QByteArray & val) : type(STRING),ival(0),strval(val),big_ival(0) + {} + + Value::Value(const Value & val) + : type(val.type),ival(val.ival),strval(val.strval),big_ival(val.big_ival) + {} + + Value::~Value() + {} + + + QString Value::toString(const QString & encoding) const + { + if (encoding.isNull() || encoding.isEmpty()) + return toString(); + + QTextCodec* tc = QTextCodec::codecForName(encoding.ascii()); + if (!tc) + return toString(); + + return tc->toUnicode(strval); + } + + + Value & Value::operator = (const Value & val) + { + type = val.type; + ival = val.ival; + strval = val.strval; + big_ival = val.big_ival; + return *this; + } + + Value & Value::operator = (Int32 val) + { + type = INT; + ival = val; + big_ival = val; + return *this; + } + + Value & Value::operator = (Int64 val) + { + type = INT64; + big_ival = val; + return *this; + } + + Value & Value::operator = (const QByteArray & val) + { + type = STRING; + strval = val; + big_ival = 0; + return *this; + } + +} diff --git a/libktorrent/torrent/value.h b/libktorrent/torrent/value.h new file mode 100644 index 0000000..cd7c879 --- /dev/null +++ b/libktorrent/torrent/value.h @@ -0,0 +1,67 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.com * + * * + * 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., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef BTVALUE_H +#define BTVALUE_H + +#include <qstring.h> +#include <util/constants.h> + +namespace bt +{ + + /** + @author Joris Guisson + */ + class Value + { + public: + enum Type + { + STRING,INT,INT64 + }; + + + Value(); + Value(int val); + Value(Int64 val); + Value(const QByteArray & val); + Value(const Value & val); + ~Value(); + + Value & operator = (const Value & val); + Value & operator = (Int32 val); + Value & operator = (Int64 val); + Value & operator = (const QByteArray & val); + + Type getType() const {return type;} + Int32 toInt() const {return ival;} + Int64 toInt64() const {return big_ival;} + QString toString() const {return QString(strval);} + QString toString(const QString & encoding) const; + QByteArray toByteArray() const {return strval;} + private: + Type type; + Int32 ival; + QByteArray strval; + Int64 big_ival; + }; +} + +#endif |