~ubuntu-branches/ubuntu/oneiric/libtorrent/oneiric

« back to all changes in this revision

Viewing changes to src/torrent/connection_manager.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
// Add some helpfull words here.
 
38
 
 
39
#ifndef LIBTORRENT_CONNECTION_MANAGER_H
 
40
#define LIBTORRENT_CONNECTION_MANAGER_H
 
41
 
 
42
#include <sys/socket.h>
 
43
#include <arpa/inet.h>
 
44
#include <netinet/in.h>
 
45
#include <netinet/in_systm.h>
 
46
#include <netinet/ip.h>
 
47
#include <sigc++/connection.h>
 
48
#include <sigc++/signal.h>
 
49
#include <sigc++/functors/slot.h>
 
50
 
 
51
#include <torrent/common.h>
 
52
 
 
53
namespace torrent {
 
54
 
 
55
// Standard pair of up/down throttles.
 
56
// First element is upload throttle, second element is download throttle.
 
57
typedef std::pair<Throttle*, Throttle*> ThrottlePair;
 
58
 
 
59
class LIBTORRENT_EXPORT ConnectionManager {
 
60
public:
 
61
  typedef uint32_t                              size_type;
 
62
  typedef uint16_t                              port_type;
 
63
  typedef uint8_t                               priority_type;
 
64
  typedef sigc::slot<uint32_t, const sockaddr*> slot_filter_type;
 
65
 
 
66
  static const priority_type iptos_default     = 0;
 
67
  static const priority_type iptos_lowdelay    = IPTOS_LOWDELAY;
 
68
  static const priority_type iptos_throughput  = IPTOS_THROUGHPUT;
 
69
  static const priority_type iptos_reliability = IPTOS_RELIABILITY;
 
70
 
 
71
#if defined IPTOS_MINCOST
 
72
  static const priority_type iptos_mincost     = IPTOS_MINCOST;
 
73
#elif defined IPTOS_LOWCOST
 
74
  static const priority_type iptos_mincost     = IPTOS_LOWCOST;
 
75
#else
 
76
  static const priority_type iptos_mincost     = iptos_throughput;
 
77
#endif
 
78
 
 
79
  static const uint32_t encryption_none             = 0;
 
80
  static const uint32_t encryption_allow_incoming   = (1 << 0);
 
81
  static const uint32_t encryption_try_outgoing     = (1 << 1);
 
82
  static const uint32_t encryption_require          = (1 << 2);
 
83
  static const uint32_t encryption_require_RC4      = (1 << 3);
 
84
  static const uint32_t encryption_enable_retry     = (1 << 4);
 
85
  static const uint32_t encryption_prefer_plaintext = (1 << 5);
 
86
 
 
87
  // Internal to libtorrent.
 
88
  static const uint32_t encryption_use_proxy        = (1 << 6);
 
89
  static const uint32_t encryption_dont_filter_recent = (1 << 7);
 
90
 
 
91
  enum {
 
92
    handshake_incoming           = 1,
 
93
    handshake_outgoing           = 2,
 
94
    handshake_outgoing_encrypted = 3,
 
95
    handshake_outgoing_proxy     = 4,
 
96
    handshake_success            = 5,
 
97
    handshake_dropped            = 6,
 
98
    handshake_failed             = 7,
 
99
    handshake_retry_plaintext    = 8,
 
100
    handshake_retry_encrypted    = 9
 
101
  };
 
102
 
 
103
  // The sockaddr argument in the result slot call is NULL if the resolve failed, and the int holds the errno.
 
104
  typedef sigc::slot2<void, const sockaddr*, int>                                                   slot_resolver_result_type;
 
105
  typedef sigc::slot4<slot_resolver_result_type*, const char*, int, int, slot_resolver_result_type> slot_resolver_type;
 
106
 
 
107
  typedef sigc::slot4<void, const sockaddr*, int, int, const HashString*>   slot_handshake_type;
 
108
  typedef sigc::signal4<void, const sockaddr*, int, int, const HashString*> signal_handshake_type;
 
109
 
 
110
  typedef sigc::slot1<ThrottlePair, const sockaddr*>  slot_address_throttle_type;
 
111
 
 
112
  ConnectionManager();
 
113
  ~ConnectionManager();
 
114
  
 
115
  // Check that we have not surpassed the max number of open sockets
 
116
  // and that we're allowed to connect to the socket address.
 
117
  //
 
118
  // Consider only checking max number of open sockets.
 
119
  bool                can_connect() const;
 
120
 
 
121
  // Call this to keep the socket count up to date.
 
122
  void                inc_socket_count()                      { m_size++; }
 
123
  void                dec_socket_count()                      { m_size--; }
 
124
 
 
125
  size_type           size() const                            { return m_size; }
 
126
 
 
127
  size_type           max_size() const                        { return m_maxSize; }
 
128
  void                set_max_size(size_type s)               { m_maxSize = s; }
 
129
 
 
130
  priority_type       priority() const                        { return m_priority; }
 
131
  void                set_priority(priority_type p)           { m_priority = p; }
 
132
 
 
133
  uint32_t            send_buffer_size() const                { return m_sendBufferSize; }
 
134
  void                set_send_buffer_size(uint32_t s);
 
135
 
 
136
  uint32_t            receive_buffer_size() const             { return m_receiveBufferSize; }
 
137
  void                set_receive_buffer_size(uint32_t s);
 
138
 
 
139
  uint32_t            encryption_options()                    { return m_encryptionOptions; }
 
140
  void                set_encryption_options(uint32_t options); 
 
141
 
 
142
  // Setting the addresses creates a copy of the address.
 
143
  const sockaddr*     bind_address() const                    { return m_bindAddress; }
 
144
  void                set_bind_address(const sockaddr* sa);
 
145
 
 
146
  const sockaddr*     local_address() const                   { return m_localAddress; }
 
147
  void                set_local_address(const sockaddr* sa);
 
148
 
 
149
  const sockaddr*     proxy_address() const                   { return m_proxyAddress; }
 
150
  void                set_proxy_address(const sockaddr* sa);
 
151
 
 
152
  uint32_t            filter(const sockaddr* sa);
 
153
  void                set_filter(const slot_filter_type& s)   { m_slotFilter = s; }
 
154
 
 
155
  bool                listen_open(port_type begin, port_type end);
 
156
  void                listen_close();  
 
157
 
 
158
  signal_handshake_type& signal_handshake_log()                          { return m_signalHandshakeLog; }
 
159
  sigc::connection       set_signal_handshake_log(slot_handshake_type s) { return m_signalHandshakeLog.connect(s); }
 
160
 
 
161
  // The resolver returns a pointer to its copy of the result slot
 
162
  // which the caller may set blocked to prevent the slot from being
 
163
  // called. The pointer must be NULL if the result slot was already
 
164
  // called because the resolve was synchronous.
 
165
  const slot_resolver_type& resolver() const                             { return m_slotResolver; }
 
166
  void                      set_resolver(const slot_resolver_type& s)    { m_slotResolver = s; }
 
167
 
 
168
  // Since trackers need our port number, it doesn't get cleared after
 
169
  // 'listen_close()'. The client may change the reported port number,
 
170
  // but do note that it gets overwritten after 'listen_open(...)'.
 
171
  port_type           listen_port() const                     { return m_listenPort; }
 
172
  void                set_listen_port(port_type p)            { m_listenPort = p; }
 
173
 
 
174
  // The slot returns a ThrottlePair to use for the given address, or NULLs to use the default throttle.
 
175
  const slot_address_throttle_type&
 
176
                      address_throttle() const                { return m_slotAddressThrottle; }
 
177
  void                set_address_throttle(const slot_address_throttle_type& s)
 
178
                                                              { m_slotAddressThrottle = s; }
 
179
 
 
180
  // For internal usage.
 
181
  Listen*             listen()                                { return m_listen; }
 
182
 
 
183
private:
 
184
  ConnectionManager(const ConnectionManager&);
 
185
  void operator = (const ConnectionManager&);
 
186
 
 
187
  size_type           m_size;
 
188
  size_type           m_maxSize;
 
189
 
 
190
  priority_type       m_priority;
 
191
  uint32_t            m_sendBufferSize;
 
192
  uint32_t            m_receiveBufferSize;
 
193
  int                 m_encryptionOptions;
 
194
 
 
195
  sockaddr*           m_bindAddress;
 
196
  sockaddr*           m_localAddress;
 
197
  sockaddr*           m_proxyAddress;
 
198
 
 
199
  Listen*             m_listen;
 
200
  port_type           m_listenPort;
 
201
 
 
202
  slot_filter_type      m_slotFilter;
 
203
  signal_handshake_type m_signalHandshakeLog;
 
204
  slot_resolver_type    m_slotResolver;
 
205
  slot_address_throttle_type  m_slotAddressThrottle;
 
206
};
 
207
 
 
208
}
 
209
 
 
210
#endif
 
211