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

« back to all changes in this revision

Viewing changes to src/torrent/connection_manager.cc

  • 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
#include "config.h"
 
38
 
 
39
#include <sys/types.h>
 
40
 
 
41
#include <rak/address_info.h>
 
42
#include <rak/socket_address.h>
 
43
 
 
44
#include "net/listen.h"
 
45
 
 
46
#include "connection_manager.h"
 
47
#include "error.h"
 
48
#include "exceptions.h"
 
49
 
 
50
namespace torrent {
 
51
 
 
52
static ConnectionManager::slot_resolver_result_type*
 
53
resolve_host(const char* host, int family, int socktype, ConnectionManager::slot_resolver_result_type slot) {
 
54
  rak::address_info* ai;
 
55
  int err;
 
56
  if ((err = rak::address_info::get_address_info(host, family, socktype, &ai)) != 0) {
 
57
    slot(NULL, err);
 
58
    return NULL;
 
59
  }
 
60
 
 
61
  rak::socket_address sa;
 
62
  sa.copy(*ai->address(), ai->length());
 
63
  rak::address_info::free_address_info(ai);
 
64
 
 
65
  slot(sa.c_sockaddr(), 0);
 
66
  return NULL;
 
67
}
 
68
 
 
69
ConnectionManager::ConnectionManager() :
 
70
  m_size(0),
 
71
  m_maxSize(0),
 
72
 
 
73
  m_priority(iptos_throughput),
 
74
  m_sendBufferSize(0),
 
75
  m_receiveBufferSize(0),
 
76
  m_encryptionOptions(encryption_none),
 
77
 
 
78
  m_listen(new Listen),
 
79
  m_listenPort(0),
 
80
  m_slotResolver(&resolve_host) {
 
81
 
 
82
  m_bindAddress = (new rak::socket_address())->c_sockaddr();
 
83
  m_localAddress = (new rak::socket_address())->c_sockaddr();
 
84
  m_proxyAddress = (new rak::socket_address())->c_sockaddr();
 
85
 
 
86
#ifdef RAK_USE_INET6
 
87
  rak::socket_address::cast_from(m_bindAddress)->sa_inet6()->clear();
 
88
  rak::socket_address::cast_from(m_localAddress)->sa_inet6()->clear();
 
89
  rak::socket_address::cast_from(m_proxyAddress)->sa_inet6()->clear();
 
90
#else
 
91
  rak::socket_address::cast_from(m_bindAddress)->sa_inet()->clear();
 
92
  rak::socket_address::cast_from(m_localAddress)->sa_inet()->clear();
 
93
  rak::socket_address::cast_from(m_proxyAddress)->sa_inet()->clear();
 
94
#endif
 
95
}
 
96
 
 
97
ConnectionManager::~ConnectionManager() {
 
98
  delete m_listen;
 
99
 
 
100
  delete m_bindAddress;
 
101
  delete m_localAddress;
 
102
  delete m_proxyAddress;
 
103
}
 
104
 
 
105
bool
 
106
ConnectionManager::can_connect() const {
 
107
  return m_size < m_maxSize;
 
108
}
 
109
 
 
110
void
 
111
ConnectionManager::set_send_buffer_size(uint32_t s) {
 
112
  m_sendBufferSize = s;
 
113
}
 
114
 
 
115
void
 
116
ConnectionManager::set_receive_buffer_size(uint32_t s) {
 
117
  m_receiveBufferSize = s;
 
118
}
 
119
 
 
120
void
 
121
ConnectionManager::set_encryption_options(uint32_t options) {
 
122
#ifdef USE_OPENSSL
 
123
  m_encryptionOptions = options;
 
124
#else
 
125
  throw input_error("Compiled without encryption support.");
 
126
#endif
 
127
}
 
128
 
 
129
void
 
130
ConnectionManager::set_bind_address(const sockaddr* sa) {
 
131
  const rak::socket_address* rsa = rak::socket_address::cast_from(sa);
 
132
 
 
133
#ifndef RAK_USE_INET6
 
134
  if (rsa->family() != rak::socket_address::af_inet)
 
135
    throw input_error("Tried to set a bind address that is not an af_inet address.");
 
136
#endif
 
137
 
 
138
  rak::socket_address::cast_from(m_bindAddress)->copy(*rsa, rsa->length());
 
139
}
 
140
 
 
141
void
 
142
ConnectionManager::set_local_address(const sockaddr* sa) {
 
143
  const rak::socket_address* rsa = rak::socket_address::cast_from(sa);
 
144
 
 
145
#ifndef RAK_USE_INET6
 
146
  if (rsa->family() != rak::socket_address::af_inet)
 
147
    throw input_error("Tried to set a local address that is not an af_inet address.");
 
148
#endif
 
149
 
 
150
  rak::socket_address::cast_from(m_localAddress)->copy(*rsa, rsa->length());
 
151
}
 
152
 
 
153
void
 
154
ConnectionManager::set_proxy_address(const sockaddr* sa) {
 
155
  const rak::socket_address* rsa = rak::socket_address::cast_from(sa);
 
156
 
 
157
#ifndef RAK_USE_INET6
 
158
  if (rsa->family() != rak::socket_address::af_inet)
 
159
    throw input_error("Tried to set a proxy address that is not an af_inet address.");
 
160
#endif
 
161
 
 
162
  rak::socket_address::cast_from(m_proxyAddress)->copy(*rsa, rsa->length());
 
163
}
 
164
 
 
165
uint32_t
 
166
ConnectionManager::filter(const sockaddr* sa) {
 
167
  if (m_slotFilter.empty())
 
168
    return 1;
 
169
  else
 
170
    return m_slotFilter(sa);
 
171
}
 
172
 
 
173
bool
 
174
ConnectionManager::listen_open(port_type begin, port_type end) {
 
175
  if (!m_listen->open(begin, end, rak::socket_address::cast_from(m_bindAddress)))
 
176
    return false;
 
177
 
 
178
  m_listenPort = m_listen->port();
 
179
 
 
180
  return true;
 
181
}
 
182
 
 
183
void
 
184
ConnectionManager::listen_close() {
 
185
  m_listen->close();
 
186
}
 
187
 
 
188
}