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
41
#include <rak/functional.h>
42
#include <rak/socket_address.h>
44
#include "download/available_list.h"
45
#include "torrent/peer/client_list.h"
47
#include "exceptions.h"
50
#include "peer_info.h"
51
#include "peer_list.h"
56
socket_address_less(const sockaddr* s1, const sockaddr* s2) {
57
const rak::socket_address* sa1 = rak::socket_address::cast_from(s1);
58
const rak::socket_address* sa2 = rak::socket_address::cast_from(s2);
60
if (sa1->family() != sa2->family())
61
return sa1->family() < sa2->family();
63
else if (sa1->family() == rak::socket_address::af_inet)
64
// Sort by hardware byte order to ensure proper ordering for
66
return sa1->sa_inet()->address_h() < sa2->sa_inet()->address_h();
69
// When we implement INET6 handling, embed the ipv4 address in
71
throw internal_error("socket_address_key(...) tried to compare an invalid family type.");
75
socket_address_key::is_comparable(const sockaddr* sa) {
76
return rak::socket_address::cast_from(sa)->family() == rak::socket_address::af_inet;
79
struct peer_list_equal_port : public std::binary_function<PeerList::reference, uint16_t, bool> {
80
bool operator () (PeerList::reference p, uint16_t port) {
81
return rak::socket_address::cast_from(p.second->socket_address())->port() == port;
85
PeerList::PeerList() :
86
m_availableList(new AvailableList) {
89
PeerList::~PeerList() {
90
std::for_each(begin(), end(), rak::on(rak::mem_ref(&value_type::second), rak::call_delete<PeerInfo>()));
93
delete m_availableList;
97
PeerList::insert_address(const sockaddr* sa, int flags) {
98
if (!socket_address_key::is_comparable(sa))
101
range_type range = base_type::equal_range(sa);
103
// Do some special handling if we got a new port number but the
104
// address was present.
106
// What we do depends on the flags, but for now just allow one
107
// PeerInfo per address key and do nothing.
108
if (range.first != range.second)
111
const rak::socket_address* address = rak::socket_address::cast_from(sa);
113
PeerInfo* peerInfo = new PeerInfo(sa);
114
peerInfo->set_listen_port(address->port());
116
manager->client_list()->retrieve_unknown(&peerInfo->mutable_client_info());
118
base_type::insert(range.second, value_type(socket_address_key(peerInfo->socket_address()), peerInfo));
120
if (flags & address_available && peerInfo->listen_port() != 0)
121
m_availableList->push_back(address);
127
socket_address_less_rak(const rak::socket_address& s1, const rak::socket_address& s2) {
128
return socket_address_less(s1.c_sockaddr(), s2.c_sockaddr());
132
PeerList::insert_available(const void* al) {
133
uint32_t inserted = 0;
134
const AddressList* addressList = static_cast<const AddressList*>(al);
136
if (m_availableList->size() + addressList->size() > m_availableList->capacity())
137
m_availableList->reserve(m_availableList->size() + addressList->size() + 128);
139
// Optimize this so that we don't traverse the tree for every
140
// insert, since we know 'al' is sorted.
142
AddressList::const_iterator itr = addressList->begin();
143
AddressList::const_iterator last = addressList->end();
144
AvailableList::const_iterator availItr = m_availableList->begin();
145
AvailableList::const_iterator availLast = m_availableList->end();
147
for (; itr != last; itr++) {
148
if (!socket_address_key::is_comparable(itr->c_sockaddr()) || itr->port() == 0)
151
availItr = std::find_if(availItr, availLast, rak::bind2nd(std::ptr_fun(&socket_address_less_rak), *itr));
153
if (availItr != availLast && !socket_address_less(availItr->c_sockaddr(), itr->c_sockaddr())) {
154
// The address is already in m_availableList, so don't bother
159
// Check if the peerinfo exists, if it does, check if we would
160
// ever want to connect. Just update the timer for the last
161
// availability notice if the peer isn't really ideal, but might
162
// be used in an emergency.
163
range_type range = base_type::equal_range(itr->c_sockaddr());
165
if (range.first != range.second) {
166
// Add some logic here to select the best PeerInfo, but for now
167
// just assume the first one is the only one that exists.
168
PeerInfo* peerInfo = range.first->second;
170
if (peerInfo->listen_port() == 0)
171
peerInfo->set_port(itr->port());
173
if (peerInfo->connection() != NULL ||
174
peerInfo->last_handshake() + 600 > (uint32_t)cachedTime.seconds())
177
// If the peer has sent us bad chunks or we just connected or
178
// tried to do so a few minutes ago, only update its
179
// availability timer.
182
// Should we perhaps add to available list even though we don't
183
// want the peer, just to ensure we don't need to search for the
184
// PeerInfo every time it gets reported. Though I'd assume it
185
// won't happen often enough to be worth it.
188
m_availableList->push_back(&*itr);
195
PeerList::available_list_size() const {
196
return m_availableList->size();
200
PeerList::connected(const sockaddr* sa, int flags) {
201
if (!socket_address_key::is_comparable(sa))
205
const rak::socket_address* address = rak::socket_address::cast_from(sa);
207
range_type range = base_type::equal_range(sa);
209
if (range.first == range.second) {
210
// Create a new entry.
211
peerInfo = new PeerInfo(sa);
213
base_type::insert(range.second, value_type(socket_address_key(peerInfo->socket_address()), peerInfo));
215
} else if (!range.first->second->is_connected()) {
217
peerInfo = range.first->second;
218
peerInfo->set_port(address->port());
221
// Make sure we don't end up throwing away the port the host is
222
// actually listening on, when there may be several simultaneous
223
// connection attempts to/from different ports.
225
// This also ensure we can connect to peers running on the same
226
// host as the tracker.
227
if (flags & connect_keep_handshakes &&
228
range.first->second->is_handshake() &&
229
rak::socket_address::cast_from(range.first->second->socket_address())->port() != address->port())
230
m_availableList->buffer()->push_back(*address);
235
if (flags & connect_filter_recent &&
236
peerInfo->last_handshake() + 600 > (uint32_t)cachedTime.seconds())
239
if (!(flags & connect_incoming))
240
peerInfo->set_listen_port(address->port());
242
if (flags & connect_incoming)
243
peerInfo->set_flags(PeerInfo::flag_incoming);
245
peerInfo->unset_flags(PeerInfo::flag_incoming);
247
peerInfo->set_flags(PeerInfo::flag_connected);
248
peerInfo->set_last_handshake(cachedTime.seconds());
253
// Make sure we properly clear port when disconnecting.
256
PeerList::disconnected(PeerInfo* p, int flags) {
257
range_type range = base_type::equal_range(p->socket_address());
259
iterator itr = std::find_if(range.first, range.second, rak::equal(p, rak::mem_ref(&value_type::second)));
261
if (itr == range.second) {
262
if (std::find_if(base_type::begin(), base_type::end(), rak::equal(p, rak::mem_ref(&value_type::second))) == base_type::end())
263
throw internal_error("PeerList::disconnected(...) itr == range.second, doesn't exist.");
265
throw internal_error("PeerList::disconnected(...) itr == range.second, not in the range.");
268
disconnected(itr, flags);
272
PeerList::disconnected(iterator itr, int flags) {
273
if (itr == base_type::end())
274
throw internal_error("PeerList::disconnected(...) itr == end().");
276
if (!itr->second->is_connected())
277
throw internal_error("PeerList::disconnected(...) !itr->is_connected().");
279
itr->second->unset_flags(PeerInfo::flag_connected);
281
// Replace the socket address port with the listening port so that
282
// future outgoing connections will connect to the right port.
283
itr->second->set_port(0);
285
if (flags & disconnect_set_time)
286
itr->second->set_last_connection(cachedTime.seconds());
288
if (flags & disconnect_available && itr->second->listen_port() != 0)
289
m_availableList->push_back(rak::socket_address::cast_from(itr->second->socket_address()));
291
// Do magic to get rid of unneeded entries.
296
PeerList::cull_peers(int flags) {
297
uint32_t counter = 0;
300
if (flags & cull_old)
301
timer = cachedTime.seconds() - 24 * 60 * 60;
305
for (iterator itr = base_type::begin(); itr != base_type::end(); ) {
306
if (itr->second->is_connected() ||
307
itr->second->transfer_counter() != 0 ||
308
itr->second->last_connection() >= timer ||
310
(flags & cull_keep_interesting &&
311
(itr->second->failed_counter() != 0 || itr->second->is_blocked()))) {
316
// The key is a pointer to a member in the value, although the key
317
// shouldn't actually be used in erase (I think), just ot be safe
318
// we delete it after erase.
319
iterator tmp = itr++;
320
PeerInfo* peerInfo = tmp->second;
322
base_type::erase(tmp);