summaryrefslogtreecommitdiffstats
path: root/libktorrent/torrent/torrent.h
blob: 04c45cd7c7465f6135edf987c0d1c3d6d8189295 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
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