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
39
#include <sys/types.h>
41
#include <rak/address_info.h>
42
#include <rak/socket_address.h>
44
#include "net/listen.h"
46
#include "connection_manager.h"
48
#include "exceptions.h"
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;
56
if ((err = rak::address_info::get_address_info(host, family, socktype, &ai)) != 0) {
61
rak::socket_address sa;
62
sa.copy(*ai->address(), ai->length());
63
rak::address_info::free_address_info(ai);
65
slot(sa.c_sockaddr(), 0);
69
ConnectionManager::ConnectionManager() :
73
m_priority(iptos_throughput),
75
m_receiveBufferSize(0),
76
m_encryptionOptions(encryption_none),
80
m_slotResolver(&resolve_host) {
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();
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();
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();
97
ConnectionManager::~ConnectionManager() {
100
delete m_bindAddress;
101
delete m_localAddress;
102
delete m_proxyAddress;
106
ConnectionManager::can_connect() const {
107
return m_size < m_maxSize;
111
ConnectionManager::set_send_buffer_size(uint32_t s) {
112
m_sendBufferSize = s;
116
ConnectionManager::set_receive_buffer_size(uint32_t s) {
117
m_receiveBufferSize = s;
121
ConnectionManager::set_encryption_options(uint32_t options) {
123
m_encryptionOptions = options;
125
throw input_error("Compiled without encryption support.");
130
ConnectionManager::set_bind_address(const sockaddr* sa) {
131
const rak::socket_address* rsa = rak::socket_address::cast_from(sa);
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.");
138
rak::socket_address::cast_from(m_bindAddress)->copy(*rsa, rsa->length());
142
ConnectionManager::set_local_address(const sockaddr* sa) {
143
const rak::socket_address* rsa = rak::socket_address::cast_from(sa);
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.");
150
rak::socket_address::cast_from(m_localAddress)->copy(*rsa, rsa->length());
154
ConnectionManager::set_proxy_address(const sockaddr* sa) {
155
const rak::socket_address* rsa = rak::socket_address::cast_from(sa);
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.");
162
rak::socket_address::cast_from(m_proxyAddress)->copy(*rsa, rsa->length());
166
ConnectionManager::filter(const sockaddr* sa) {
167
if (m_slotFilter.empty())
170
return m_slotFilter(sa);
174
ConnectionManager::listen_open(port_type begin, port_type end) {
175
if (!m_listen->open(begin, end, rak::socket_address::cast_from(m_bindAddress)))
178
m_listenPort = m_listen->port();
184
ConnectionManager::listen_close() {