~ubuntu-branches/ubuntu/wily/libtorrent/wily-proposed

« back to all changes in this revision

Viewing changes to src/dht/dht_server.h

  • Committer: Bazaar Package Importer
  • Author(s): Rogério Brito
  • Date: 2011-03-20 01:06:18 UTC
  • mfrom: (1.1.13 upstream) (4.1.9 sid)
  • Revision ID: james.westby@ubuntu.com-20110320010618-g3wyylccqzqko73c
Tags: 0.12.7-5
* Use Steinar's "real" patch for IPv6. Addresses #490277, #618275,
  and Closes: #617791.
* Adapt libtorrent-0.12.6-ipv6-07.patch. It FTBFS otherwise.
* Add proper attibution to the IPv6 patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// libTorrent - BitTorrent library
 
2
// Copyright (C) 2005-2007, Jari Sundell
 
3
//
 
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.
 
8
// 
 
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.
 
13
// 
 
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
 
17
//
 
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
 
22
// including the two.
 
23
//
 
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.
 
31
//
 
32
// Contact:  Jari Sundell <jaris@ifi.uio.no>
 
33
//
 
34
//           Skomakerveien 33
 
35
//           3185 Skoppum, NORWAY
 
36
 
 
37
#ifndef LIBTORRENT_DHT_SERVER_H
 
38
#define LIBTORRENT_DHT_SERVER_H
 
39
 
 
40
#include <map>
 
41
#include <deque>
 
42
#include <rak/priority_queue_default.h>
 
43
#include <rak/socket_address.h>
 
44
 
 
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"
 
49
 
 
50
#include "dht_transaction.h"
 
51
 
 
52
namespace torrent {
 
53
 
 
54
class DhtBucket;
 
55
class DhtNode;
 
56
class DhtRouter;
 
57
 
 
58
class DownloadInfo;
 
59
class DhtMessage;
 
60
class TrackerDht;
 
61
 
 
62
// UDP server that handles the DHT node communications.
 
63
 
 
64
class DhtServer : public SocketDatagram {
 
65
public:
 
66
  DhtServer(DhtRouter* self);
 
67
  ~DhtServer();
 
68
 
 
69
  void                start(int port);
 
70
  void                stop();
 
71
  bool                is_active() const                  { return get_fd().is_valid(); }
 
72
 
 
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();
 
79
 
 
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);
 
82
 
 
83
  // Do a find_node search with the given contacts as starting point for the
 
84
  // search.
 
85
  void                find_node(const DhtBucket& contacts, const HashString& target);
 
86
 
 
87
  // Do DHT announce, starting with the given contacts.
 
88
  void                announce(const DhtBucket& contacts, const HashString& infoHash, TrackerDht* tracker);
 
89
 
 
90
  // Cancel given announce for given tracker, or all matching announces if info/tracker NULL.
 
91
  void                cancel_announce(DownloadInfo* info, const TrackerDht* tracker);
 
92
 
 
93
  // Called every 15 minutes.
 
94
  void                update();
 
95
 
 
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; }
 
100
 
 
101
  void                set_upload_throttle(ThrottleList* t)    { m_uploadThrottle = t; }
 
102
  void                set_download_throttle(ThrottleList* t)  { m_downloadThrottle = t; }
 
103
 
 
104
  virtual void        event_read();
 
105
  virtual void        event_write();
 
106
  virtual void        event_error();
 
107
 
 
108
private:
 
109
  // DHT error codes.
 
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;
 
114
 
 
115
  struct compact_node_info {
 
116
    char                 _id[20];
 
117
    SocketAddressCompact _addr;
 
118
 
 
119
    HashString&          id()          { return *HashString::cast_from(_id); }
 
120
    rak::socket_address  address()     { return rak::socket_address(_addr); }
 
121
  } __attribute__ ((packed));
 
122
 
 
123
  typedef std::deque<DhtTransactionPacket*> packet_queue;
 
124
  typedef std::list<compact_node_info> node_info_list;
 
125
 
 
126
  // Pending transactions.
 
127
  typedef std::map<DhtTransaction::key_type, DhtTransaction*> transaction_map;
 
128
  typedef transaction_map::iterator transaction_itr;
 
129
 
 
130
  // DHT transaction names for given transaction type.
 
131
  static const char* queries[];
 
132
 
 
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.
 
137
 
 
138
  void                start_write();
 
139
 
 
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);
 
143
 
 
144
  void                parse_find_node_reply(DhtTransactionSearch* t, raw_string nodes);
 
145
  void                parse_get_peers_reply(DhtTransactionGetPeers* t, const DhtMessage& res);
 
146
 
 
147
  void                find_node_next(DhtTransactionSearch* t);
 
148
 
 
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);
 
153
 
 
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);
 
157
 
 
158
  int                 add_transaction(DhtTransaction* t, int priority);
 
159
 
 
160
  // This returns the iterator after the given one or end()
 
161
  transaction_itr     failed_transaction(transaction_itr itr, bool quick);
 
162
 
 
163
  void                clear_transactions();
 
164
 
 
165
  bool                process_queue(packet_queue& queue, uint32_t* quota);
 
166
  void                receive_timeout();
 
167
 
 
168
  DhtRouter*          m_router;
 
169
  packet_queue        m_highQueue;
 
170
  packet_queue        m_lowQueue;
 
171
  transaction_map     m_transactions;
 
172
 
 
173
  rak::priority_item  m_taskTimeout;
 
174
 
 
175
  ThrottleNode        m_uploadNode;
 
176
  ThrottleNode        m_downloadNode;
 
177
 
 
178
  ThrottleList*       m_uploadThrottle;
 
179
  ThrottleList*       m_downloadThrottle;
 
180
 
 
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;
 
186
 
 
187
  bool                m_networkUp;
 
188
};
 
189
 
 
190
}
 
191
 
 
192
#endif