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
// Add some helpfull words here.
39
#ifndef LIBTORRENT_CONNECTION_MANAGER_H
40
#define LIBTORRENT_CONNECTION_MANAGER_H
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>
51
#include <torrent/common.h>
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;
59
class LIBTORRENT_EXPORT ConnectionManager {
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;
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;
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;
76
static const priority_type iptos_mincost = iptos_throughput;
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);
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);
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,
99
handshake_retry_plaintext = 8,
100
handshake_retry_encrypted = 9
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;
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;
110
typedef sigc::slot1<ThrottlePair, const sockaddr*> slot_address_throttle_type;
113
~ConnectionManager();
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.
118
// Consider only checking max number of open sockets.
119
bool can_connect() const;
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--; }
125
size_type size() const { return m_size; }
127
size_type max_size() const { return m_maxSize; }
128
void set_max_size(size_type s) { m_maxSize = s; }
130
priority_type priority() const { return m_priority; }
131
void set_priority(priority_type p) { m_priority = p; }
133
uint32_t send_buffer_size() const { return m_sendBufferSize; }
134
void set_send_buffer_size(uint32_t s);
136
uint32_t receive_buffer_size() const { return m_receiveBufferSize; }
137
void set_receive_buffer_size(uint32_t s);
139
uint32_t encryption_options() { return m_encryptionOptions; }
140
void set_encryption_options(uint32_t options);
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);
146
const sockaddr* local_address() const { return m_localAddress; }
147
void set_local_address(const sockaddr* sa);
149
const sockaddr* proxy_address() const { return m_proxyAddress; }
150
void set_proxy_address(const sockaddr* sa);
152
uint32_t filter(const sockaddr* sa);
153
void set_filter(const slot_filter_type& s) { m_slotFilter = s; }
155
bool listen_open(port_type begin, port_type end);
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); }
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; }
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; }
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; }
180
// For internal usage.
181
Listen* listen() { return m_listen; }
184
ConnectionManager(const ConnectionManager&);
185
void operator = (const ConnectionManager&);
190
priority_type m_priority;
191
uint32_t m_sendBufferSize;
192
uint32_t m_receiveBufferSize;
193
int m_encryptionOptions;
195
sockaddr* m_bindAddress;
196
sockaddr* m_localAddress;
197
sockaddr* m_proxyAddress;
200
port_type m_listenPort;
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;