1
// libTorrent - BitTorrent library
2
// Copyright (C) 2005-2007, Jari Sundell
4
// This program is free software; you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation; either version 2 of the License, or
7
// (at your option) any later version.
9
// This program is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
// GNU General Public License for more details.
14
// You should have received a copy of the GNU General Public License
15
// along with this program; if not, write to the Free Software
16
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
// In addition, as a special exception, the copyright holders give
19
// permission to link the code of portions of this program with the
20
// OpenSSL library under certain conditions as described in each
21
// individual source file, and distribute linked combinations
24
// You must obey the GNU General Public License in all respects for
25
// all of the code used other than OpenSSL. If you modify file(s)
26
// with this exception, you may extend this exception to your version
27
// of the file(s), but you are not obligated to do so. If you do not
28
// wish to do so, delete this exception statement from your version.
29
// If you delete this exception statement from all source files in the
30
// program, then also delete it here.
32
// Contact: Jari Sundell <jaris@ifi.uio.no>
35
// 3185 Skoppum, NORWAY
37
#ifndef LIBTORRENT_DHT_SERVER_H
38
#define LIBTORRENT_DHT_SERVER_H
42
#include <rak/priority_queue_default.h>
43
#include <rak/socket_address.h>
45
#include "net/socket_datagram.h"
46
#include "net/throttle_node.h"
47
#include "torrent/hash_string.h"
48
#include "torrent/object_raw_bencode.h"
50
#include "dht_transaction.h"
62
// UDP server that handles the DHT node communications.
64
class DhtServer : public SocketDatagram {
66
DhtServer(DhtRouter* self);
71
bool is_active() const { return get_fd().is_valid(); }
73
unsigned int queries_received() const { return m_queriesReceived; }
74
unsigned int queries_sent() const { return m_queriesSent; }
75
unsigned int replies_received() const { return m_repliesReceived; }
76
unsigned int errors_received() const { return m_errorsReceived; }
77
unsigned int errors_caught() const { return m_errorsCaught; }
78
void reset_statistics();
80
// Contact a node to see if it replies. Set id=0 if unknown.
81
void ping(const HashString& id, const rak::socket_address* sa);
83
// Do a find_node search with the given contacts as starting point for the
85
void find_node(const DhtBucket& contacts, const HashString& target);
87
// Do DHT announce, starting with the given contacts.
88
void announce(const DhtBucket& contacts, const HashString& infoHash, TrackerDht* tracker);
90
// Cancel given announce for given tracker, or all matching announces if info/tracker NULL.
91
void cancel_announce(DownloadInfo* info, const TrackerDht* tracker);
93
// Called every 15 minutes.
96
ThrottleNode* upload_throttle_node() { return &m_uploadNode; }
97
const ThrottleNode* upload_throttle_node() const { return &m_uploadNode; }
98
ThrottleNode* download_throttle_node() { return &m_downloadNode; }
99
const ThrottleNode* download_throttle_node() const { return &m_downloadNode; }
101
void set_upload_throttle(ThrottleList* t) { m_uploadThrottle = t; }
102
void set_download_throttle(ThrottleList* t) { m_downloadThrottle = t; }
104
virtual void event_read();
105
virtual void event_write();
106
virtual void event_error();
110
static const int dht_error_generic = 201;
111
static const int dht_error_server = 202;
112
static const int dht_error_protocol = 203;
113
static const int dht_error_bad_method = 204;
115
struct compact_node_info {
117
SocketAddressCompact _addr;
119
HashString& id() { return *HashString::cast_from(_id); }
120
rak::socket_address address() { return rak::socket_address(_addr); }
121
} __attribute__ ((packed));
123
typedef std::deque<DhtTransactionPacket*> packet_queue;
124
typedef std::list<compact_node_info> node_info_list;
126
// Pending transactions.
127
typedef std::map<DhtTransaction::key_type, DhtTransaction*> transaction_map;
128
typedef transaction_map::iterator transaction_itr;
130
// DHT transaction names for given transaction type.
131
static const char* queries[];
133
// Priorities for the outgoing packets.
134
static const int packet_prio_high = 2; // For important queries we send (announces).
135
static const int packet_prio_low = 1; // For (relatively) unimportant queries we send.
136
static const int packet_prio_reply = 0; // For replies to peer queries.
140
void process_query(const HashString& id, const rak::socket_address* sa, const DhtMessage& req);
141
void process_response(const HashString& id, const rak::socket_address* sa, const DhtMessage& req);
142
void process_error(const rak::socket_address* sa, const DhtMessage& error);
144
void parse_find_node_reply(DhtTransactionSearch* t, raw_string nodes);
145
void parse_get_peers_reply(DhtTransactionGetPeers* t, const DhtMessage& res);
147
void find_node_next(DhtTransactionSearch* t);
149
void add_packet(DhtTransactionPacket* packet, int priority);
150
void create_query(transaction_itr itr, int tID, const rak::socket_address* sa, int priority);
151
void create_response(const DhtMessage& req, const rak::socket_address* sa, DhtMessage& reply);
152
void create_error(const DhtMessage& req, const rak::socket_address* sa, int num, const char* msg);
154
void create_find_node_response(const DhtMessage& arg, DhtMessage& reply);
155
void create_get_peers_response(const DhtMessage& arg, const rak::socket_address* sa, DhtMessage& reply);
156
void create_announce_peer_response(const DhtMessage& arg, const rak::socket_address* sa, DhtMessage& reply);
158
int add_transaction(DhtTransaction* t, int priority);
160
// This returns the iterator after the given one or end()
161
transaction_itr failed_transaction(transaction_itr itr, bool quick);
163
void clear_transactions();
165
bool process_queue(packet_queue& queue, uint32_t* quota);
166
void receive_timeout();
169
packet_queue m_highQueue;
170
packet_queue m_lowQueue;
171
transaction_map m_transactions;
173
rak::priority_item m_taskTimeout;
175
ThrottleNode m_uploadNode;
176
ThrottleNode m_downloadNode;
178
ThrottleList* m_uploadThrottle;
179
ThrottleList* m_downloadThrottle;
181
unsigned int m_queriesReceived;
182
unsigned int m_queriesSent;
183
unsigned int m_repliesReceived;
184
unsigned int m_errorsReceived;
185
unsigned int m_errorsCaught;