2010-11-29 02:33:05 +01:00
|
|
|
/*
|
|
|
|
|
2014-02-23 20:12:25 +01:00
|
|
|
Copyright (c) 2009-2014, Arvid Norberg
|
2010-11-29 02:33:05 +01:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the author nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libtorrent/utp_stream.hpp"
|
|
|
|
#include "libtorrent/udp_socket.hpp"
|
|
|
|
#include "libtorrent/utp_socket_manager.hpp"
|
|
|
|
#include "libtorrent/instantiate_connection.hpp"
|
|
|
|
#include "libtorrent/socket_io.hpp"
|
|
|
|
#include "libtorrent/broadcast_socket.hpp" // for is_teredo
|
2011-02-26 08:55:51 +01:00
|
|
|
#include "libtorrent/random.hpp"
|
2014-07-06 21:18:00 +02:00
|
|
|
#include "libtorrent/performance_counters.hpp"
|
2015-03-12 05:34:54 +01:00
|
|
|
#include "libtorrent/aux_/time.hpp" // for aux::time_now()
|
2010-11-29 02:33:05 +01:00
|
|
|
|
|
|
|
// #define TORRENT_DEBUG_MTU 1135
|
|
|
|
|
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
utp_socket_manager::utp_socket_manager(aux::session_settings const& sett
|
|
|
|
, udp_socket& s
|
|
|
|
, counters& cnt
|
2014-10-06 05:03:01 +02:00
|
|
|
, void* ssl_context
|
2010-11-29 02:33:05 +01:00
|
|
|
, incoming_utp_callback_t cb)
|
|
|
|
: m_sock(s)
|
|
|
|
, m_cb(cb)
|
|
|
|
, m_last_socket(0)
|
|
|
|
, m_new_connection(-1)
|
|
|
|
, m_sett(sett)
|
2010-12-24 23:53:00 +01:00
|
|
|
, m_last_route_update(min_time())
|
2012-11-12 10:49:00 +01:00
|
|
|
, m_last_if_update(min_time())
|
2010-11-29 02:33:05 +01:00
|
|
|
, m_sock_buf_size(0)
|
2014-07-06 21:18:00 +02:00
|
|
|
, m_counters(cnt)
|
2014-10-06 05:03:01 +02:00
|
|
|
, m_ssl_context(ssl_context)
|
2014-07-06 21:18:00 +02:00
|
|
|
{}
|
2010-11-29 02:33:05 +01:00
|
|
|
|
|
|
|
utp_socket_manager::~utp_socket_manager()
|
|
|
|
{
|
|
|
|
for (socket_map_t::iterator i = m_utp_sockets.begin()
|
|
|
|
, end(m_utp_sockets.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
delete_utp_impl(i->second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-12 05:34:54 +01:00
|
|
|
void utp_socket_manager::tick(time_point now)
|
2010-11-29 02:33:05 +01:00
|
|
|
{
|
|
|
|
for (socket_map_t::iterator i = m_utp_sockets.begin()
|
|
|
|
, end(m_utp_sockets.end()); i != end;)
|
|
|
|
{
|
|
|
|
if (should_delete(i->second))
|
|
|
|
{
|
|
|
|
delete_utp_impl(i->second);
|
|
|
|
if (m_last_socket == i->second) m_last_socket = 0;
|
|
|
|
m_utp_sockets.erase(i++);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
tick_utp_impl(i->second, now);
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void utp_socket_manager::mtu_for_dest(address const& addr, int& link_mtu, int& utp_mtu)
|
|
|
|
{
|
2015-03-12 05:34:54 +01:00
|
|
|
if (aux::time_now() - seconds(60) > m_last_route_update)
|
2010-11-29 02:33:05 +01:00
|
|
|
{
|
2015-03-12 05:34:54 +01:00
|
|
|
m_last_route_update = aux::time_now();
|
2010-11-29 02:33:05 +01:00
|
|
|
error_code ec;
|
2015-05-18 07:04:55 +02:00
|
|
|
m_routes = enum_routes(ec);
|
2010-11-29 02:33:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int mtu = 0;
|
|
|
|
if (!m_routes.empty())
|
|
|
|
{
|
|
|
|
for (std::vector<ip_route>::iterator i = m_routes.begin()
|
|
|
|
, end(m_routes.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (!match_addr_mask(addr, i->destination, i->netmask)) continue;
|
|
|
|
|
|
|
|
// assume that we'll actually use the route with the largest
|
|
|
|
// MTU (seems like a reasonable assumption).
|
|
|
|
// this could however be improved by using the route metrics
|
|
|
|
// and the prefix length of the netmask to order the matches
|
|
|
|
if (mtu < i->mtu) mtu = i->mtu;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mtu == 0)
|
|
|
|
{
|
|
|
|
if (is_teredo(addr)) mtu = TORRENT_TEREDO_MTU;
|
|
|
|
else mtu = TORRENT_ETHERNET_MTU;
|
|
|
|
}
|
|
|
|
|
2013-02-25 06:09:35 +01:00
|
|
|
#if defined __APPLE__
|
|
|
|
// apple has a very strange loopback. It appears you can't
|
|
|
|
// send messages of the reported MTU size, and you don't get
|
|
|
|
// EWOULDBLOCK either.
|
|
|
|
if (is_loopback(addr))
|
|
|
|
{
|
|
|
|
if (is_teredo(addr)) mtu = TORRENT_TEREDO_MTU;
|
|
|
|
else mtu = TORRENT_ETHERNET_MTU;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
// clamp the MTU within reasonable bounds
|
|
|
|
if (mtu < TORRENT_INET_MIN_MTU) mtu = TORRENT_INET_MIN_MTU;
|
|
|
|
else if (mtu > TORRENT_INET_MAX_MTU) mtu = TORRENT_INET_MAX_MTU;
|
|
|
|
|
|
|
|
link_mtu = mtu;
|
|
|
|
|
|
|
|
mtu -= TORRENT_UDP_HEADER;
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (m_sock.get_proxy_settings().type == settings_pack::socks5
|
|
|
|
|| m_sock.get_proxy_settings().type == settings_pack::socks5_pw)
|
2010-11-29 02:33:05 +01:00
|
|
|
{
|
|
|
|
// this is for the IP layer
|
|
|
|
address proxy_addr = m_sock.proxy_addr().address();
|
|
|
|
if (proxy_addr.is_v4()) mtu -= TORRENT_IPV4_HEADER;
|
|
|
|
else mtu -= TORRENT_IPV6_HEADER;
|
|
|
|
|
|
|
|
// this is for the SOCKS layer
|
|
|
|
mtu -= TORRENT_SOCKS5_HEADER;
|
|
|
|
|
|
|
|
// the address field in the SOCKS header
|
|
|
|
if (addr.is_v4()) mtu -= 4;
|
|
|
|
else mtu -= 16;
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (addr.is_v4()) mtu -= TORRENT_IPV4_HEADER;
|
|
|
|
else mtu -= TORRENT_IPV6_HEADER;
|
|
|
|
}
|
|
|
|
|
|
|
|
utp_mtu = mtu;
|
|
|
|
}
|
|
|
|
|
|
|
|
void utp_socket_manager::send_packet(udp::endpoint const& ep, char const* p
|
|
|
|
, int len, error_code& ec, int flags)
|
|
|
|
{
|
2015-05-18 03:30:32 +02:00
|
|
|
#if !defined TORRENT_HAS_DONT_FRAGMENT && !defined TORRENT_DEBUG_MTU
|
|
|
|
TORRENT_UNUSED(flags);
|
|
|
|
#endif
|
2010-11-29 02:33:05 +01:00
|
|
|
if (!m_sock.is_open())
|
|
|
|
{
|
|
|
|
ec = asio::error::operation_aborted;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TORRENT_DEBUG_MTU
|
|
|
|
// drop packets that exceed the debug MTU
|
|
|
|
if ((flags & dont_fragment) && len > TORRENT_DEBUG_MTU) return;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef TORRENT_HAS_DONT_FRAGMENT
|
|
|
|
error_code tmp;
|
|
|
|
if (flags & utp_socket_manager::dont_fragment)
|
|
|
|
m_sock.set_option(libtorrent::dont_fragment(true), tmp);
|
|
|
|
#endif
|
|
|
|
m_sock.send(ep, p, len, ec);
|
|
|
|
#ifdef TORRENT_HAS_DONT_FRAGMENT
|
|
|
|
if (flags & utp_socket_manager::dont_fragment)
|
|
|
|
m_sock.set_option(libtorrent::dont_fragment(false), tmp);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-10-10 06:40:18 +02:00
|
|
|
int utp_socket_manager::local_port(error_code& ec) const
|
2010-11-29 02:33:05 +01:00
|
|
|
{
|
2012-10-10 06:40:18 +02:00
|
|
|
return m_sock.local_endpoint(ec).port();
|
|
|
|
}
|
|
|
|
|
|
|
|
tcp::endpoint utp_socket_manager::local_endpoint(address const& remote, error_code& ec) const
|
|
|
|
{
|
|
|
|
tcp::endpoint socket_ep = m_sock.local_endpoint(ec);
|
|
|
|
|
|
|
|
// first enumerate the routes in the routing table
|
2015-03-12 05:34:54 +01:00
|
|
|
if (aux::time_now() - seconds(60) > m_last_route_update)
|
2012-11-08 10:16:40 +01:00
|
|
|
{
|
2015-03-12 05:34:54 +01:00
|
|
|
m_last_route_update = aux::time_now();
|
2012-11-08 10:16:40 +01:00
|
|
|
error_code ec;
|
2015-05-18 07:04:55 +02:00
|
|
|
m_routes = enum_routes(ec);
|
2012-11-08 10:16:40 +01:00
|
|
|
if (ec) return socket_ep;
|
|
|
|
}
|
2012-10-10 06:40:18 +02:00
|
|
|
|
2012-11-08 10:16:40 +01:00
|
|
|
if (m_routes.empty()) return socket_ep;
|
2012-10-10 06:40:18 +02:00
|
|
|
// then find the best match
|
2012-11-08 10:16:40 +01:00
|
|
|
ip_route* best = &m_routes[0];
|
|
|
|
for (std::vector<ip_route>::iterator i = m_routes.begin()
|
|
|
|
, end(m_routes.end()); i != end; ++i)
|
2012-10-10 06:40:18 +02:00
|
|
|
{
|
|
|
|
if (is_any(i->destination) && i->destination.is_v4() == remote.is_v4())
|
|
|
|
{
|
|
|
|
best = &*i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match_addr_mask(remote, i->destination, i->netmask))
|
|
|
|
{
|
|
|
|
best = &*i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// best now tells us which interface we would send over
|
|
|
|
// for this target. Now figure out what the local address
|
|
|
|
// is for that interface
|
|
|
|
|
2015-03-12 05:34:54 +01:00
|
|
|
if (aux::time_now() - seconds(60) > m_last_if_update)
|
2012-11-12 10:49:00 +01:00
|
|
|
{
|
2015-03-12 05:34:54 +01:00
|
|
|
m_last_if_update = aux::time_now();
|
2012-11-12 10:49:00 +01:00
|
|
|
error_code ec;
|
|
|
|
m_interfaces = enum_net_interfaces(m_sock.get_io_service(), ec);
|
|
|
|
if (ec) return socket_ep;
|
|
|
|
}
|
2012-10-10 06:40:18 +02:00
|
|
|
|
2012-11-12 10:49:00 +01:00
|
|
|
for (std::vector<ip_interface>::iterator i = m_interfaces.begin()
|
|
|
|
, end(m_interfaces.end()); i != end; ++i)
|
2012-10-10 06:40:18 +02:00
|
|
|
{
|
|
|
|
if (i->interface_address.is_v4() != remote.is_v4())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (strcmp(best->name, i->name) == 0)
|
|
|
|
return tcp::endpoint(i->interface_address, socket_ep.port());
|
|
|
|
}
|
|
|
|
return socket_ep;
|
2010-11-29 02:33:05 +01:00
|
|
|
}
|
|
|
|
|
2012-06-22 06:21:20 +02:00
|
|
|
bool utp_socket_manager::incoming_packet(error_code const& ec, udp::endpoint const& ep
|
|
|
|
, char const* p, int size)
|
2010-11-29 02:33:05 +01:00
|
|
|
{
|
2015-05-18 03:30:32 +02:00
|
|
|
// TODO: 2 we may want to take ec into account here. possibly close
|
|
|
|
// connections quicker
|
|
|
|
TORRENT_UNUSED(ec);
|
2010-11-29 02:33:05 +01:00
|
|
|
// UTP_LOGV("incoming packet size:%d\n", size);
|
|
|
|
|
2011-02-21 06:24:41 +01:00
|
|
|
if (size < int(sizeof(utp_header))) return false;
|
2010-11-29 02:33:05 +01:00
|
|
|
|
|
|
|
utp_header const* ph = (utp_header*)p;
|
|
|
|
|
|
|
|
// UTP_LOGV("incoming packet version:%d\n", int(ph->get_version()));
|
|
|
|
|
|
|
|
if (ph->get_version() != 1) return false;
|
|
|
|
|
2015-03-12 05:34:54 +01:00
|
|
|
const time_point receive_time = clock_type::now();
|
2015-05-18 03:30:32 +02:00
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
// parse out connection ID and look for existing
|
|
|
|
// connections. If found, forward to the utp_stream.
|
|
|
|
boost::uint16_t id = ph->connection_id;
|
|
|
|
|
|
|
|
// first test to see if it's the same socket as last time
|
|
|
|
// in most cases it is
|
|
|
|
if (m_last_socket
|
|
|
|
&& utp_match(m_last_socket, ep, id))
|
|
|
|
{
|
|
|
|
return utp_incoming_packet(m_last_socket, p, size, ep, receive_time);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<socket_map_t::iterator, socket_map_t::iterator> r =
|
|
|
|
m_utp_sockets.equal_range(id);
|
|
|
|
|
|
|
|
for (; r.first != r.second; ++r.first)
|
|
|
|
{
|
|
|
|
if (!utp_match(r.first->second, ep, id)) continue;
|
|
|
|
bool ret = utp_incoming_packet(r.first->second, p, size, ep, receive_time);
|
|
|
|
if (ret) m_last_socket = r.first->second;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// UTP_LOGV("incoming packet id:%d source:%s\n", id, print_endpoint(ep).c_str());
|
|
|
|
|
2014-07-06 21:18:00 +02:00
|
|
|
if (!m_sett.get_bool(settings_pack::enable_incoming_utp))
|
2010-11-29 02:33:05 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// if not found, see if it's a SYN packet, if it is,
|
|
|
|
// create a new utp_stream
|
|
|
|
if (ph->get_type() == ST_SYN)
|
|
|
|
{
|
2012-05-19 22:39:55 +02:00
|
|
|
// possible SYN flood. Just ignore
|
2014-07-06 21:18:00 +02:00
|
|
|
if (int(m_utp_sockets.size()) > m_sett.get_int(settings_pack::connections_limit) * 2)
|
2012-05-19 22:39:55 +02:00
|
|
|
return false;
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
// UTP_LOGV("not found, new connection id:%d\n", m_new_connection);
|
|
|
|
|
|
|
|
boost::shared_ptr<socket_type> c(new (std::nothrow) socket_type(m_sock.get_io_service()));
|
|
|
|
if (!c) return false;
|
2012-10-18 09:42:15 +02:00
|
|
|
|
|
|
|
TORRENT_ASSERT(m_new_connection == -1);
|
|
|
|
// create the new socket with this ID
|
|
|
|
m_new_connection = id;
|
|
|
|
|
2014-10-06 05:03:01 +02:00
|
|
|
instantiate_connection(m_sock.get_io_service(), proxy_settings(), *c
|
|
|
|
, m_ssl_context, this, true);
|
|
|
|
|
|
|
|
|
|
|
|
utp_stream* str = NULL;
|
|
|
|
#ifdef TORRENT_USE_OPENSSL
|
|
|
|
if (is_ssl(*c))
|
|
|
|
str = &c->get<ssl_stream<utp_stream> >()->next_layer();
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
str = c->get<utp_stream>();
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
TORRENT_ASSERT(str);
|
|
|
|
int link_mtu, utp_mtu;
|
|
|
|
mtu_for_dest(ep.address(), link_mtu, utp_mtu);
|
|
|
|
utp_init_mtu(str->get_impl(), link_mtu, utp_mtu);
|
|
|
|
bool ret = utp_incoming_packet(str->get_impl(), p, size, ep, receive_time);
|
|
|
|
if (!ret) return false;
|
|
|
|
m_cb(c);
|
|
|
|
// the connection most likely changed its connection ID here
|
|
|
|
// we need to move it to the correct ID
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-29 20:35:39 +01:00
|
|
|
if (ph->get_type() == ST_RESET) return false;
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
// #error send reset
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-01 20:44:46 +02:00
|
|
|
void utp_socket_manager::subscribe_writable(utp_socket_impl* s)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(std::find(m_stalled_sockets.begin(), m_stalled_sockets.end()
|
|
|
|
, s) == m_stalled_sockets.end());
|
|
|
|
m_stalled_sockets.push_back(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
void utp_socket_manager::writable()
|
|
|
|
{
|
|
|
|
std::vector<utp_socket_impl*> stalled_sockets;
|
|
|
|
m_stalled_sockets.swap(stalled_sockets);
|
|
|
|
for (std::vector<utp_socket_impl*>::iterator i = stalled_sockets.begin()
|
|
|
|
, end(stalled_sockets.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
utp_socket_impl* s = *i;
|
|
|
|
utp_writable(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-21 17:05:57 +02:00
|
|
|
void utp_socket_manager::socket_drained()
|
|
|
|
{
|
|
|
|
// flush all deferred acks
|
2015-05-18 03:30:32 +02:00
|
|
|
|
2012-06-21 17:05:57 +02:00
|
|
|
std::vector<utp_socket_impl*> deferred_acks;
|
|
|
|
m_deferred_acks.swap(deferred_acks);
|
|
|
|
for (std::vector<utp_socket_impl*>::iterator i = deferred_acks.begin()
|
|
|
|
, end(deferred_acks.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
utp_socket_impl* s = *i;
|
|
|
|
utp_send_ack(s);
|
|
|
|
}
|
2013-02-06 05:38:30 +01:00
|
|
|
|
|
|
|
std::vector<utp_socket_impl*> drained_event;
|
|
|
|
m_drained_event.swap(drained_event);
|
|
|
|
for (std::vector<utp_socket_impl*>::iterator i = drained_event.begin()
|
|
|
|
, end(drained_event.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
utp_socket_impl* s = *i;
|
|
|
|
utp_socket_drained(s);
|
|
|
|
}
|
2012-06-21 17:05:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void utp_socket_manager::defer_ack(utp_socket_impl* s)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(std::find(m_deferred_acks.begin(), m_deferred_acks.end(), s)
|
|
|
|
== m_deferred_acks.end());
|
|
|
|
m_deferred_acks.push_back(s);
|
|
|
|
}
|
|
|
|
|
2013-02-06 05:38:30 +01:00
|
|
|
void utp_socket_manager::subscribe_drained(utp_socket_impl* s)
|
|
|
|
{
|
|
|
|
TORRENT_ASSERT(std::find(m_drained_event.begin(), m_drained_event.end(), s)
|
|
|
|
== m_drained_event.end());
|
|
|
|
m_drained_event.push_back(s);
|
|
|
|
}
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
void utp_socket_manager::remove_socket(boost::uint16_t id)
|
|
|
|
{
|
|
|
|
socket_map_t::iterator i = m_utp_sockets.find(id);
|
|
|
|
if (i == m_utp_sockets.end()) return;
|
|
|
|
delete_utp_impl(i->second);
|
|
|
|
if (m_last_socket == i->second) m_last_socket = 0;
|
|
|
|
m_utp_sockets.erase(i);
|
|
|
|
}
|
2015-05-18 03:30:32 +02:00
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
void utp_socket_manager::set_sock_buf(int size)
|
|
|
|
{
|
|
|
|
if (size < m_sock_buf_size) return;
|
|
|
|
m_sock.set_buf_size(size);
|
|
|
|
error_code ec;
|
|
|
|
// add more socket buffer storage on the lower level socket
|
|
|
|
// to avoid dropping packets because of a full receive buffer
|
|
|
|
// while processing a packet
|
|
|
|
|
|
|
|
// only update the buffer size if it's bigger than
|
|
|
|
// what we already have
|
|
|
|
datagram_socket::receive_buffer_size recv_buf_size_opt;
|
|
|
|
m_sock.get_option(recv_buf_size_opt, ec);
|
|
|
|
if (recv_buf_size_opt.value() < size * 10)
|
|
|
|
{
|
|
|
|
m_sock.set_option(datagram_socket::receive_buffer_size(size * 10), ec);
|
|
|
|
m_sock.set_option(datagram_socket::send_buffer_size(size * 3), ec);
|
|
|
|
}
|
|
|
|
m_sock_buf_size = size;
|
|
|
|
}
|
|
|
|
|
2015-01-04 22:31:02 +01:00
|
|
|
void utp_socket_manager::inc_stats_counter(int counter, int delta)
|
2013-09-14 12:06:48 +02:00
|
|
|
{
|
2015-01-04 22:31:02 +01:00
|
|
|
TORRENT_ASSERT((counter >= counters::utp_packet_loss
|
|
|
|
&& counter <= counters::utp_redundant_pkts_in)
|
|
|
|
|| (counter >= counters::num_utp_idle
|
|
|
|
&& counter <= counters::num_utp_deleted));
|
|
|
|
m_counters.inc_stats_counter(counter, delta);
|
2013-09-14 12:06:48 +02:00
|
|
|
}
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
utp_socket_impl* utp_socket_manager::new_utp_socket(utp_stream* str)
|
|
|
|
{
|
|
|
|
boost::uint16_t send_id = 0;
|
|
|
|
boost::uint16_t recv_id = 0;
|
|
|
|
if (m_new_connection != -1)
|
|
|
|
{
|
|
|
|
send_id = m_new_connection;
|
|
|
|
recv_id = m_new_connection + 1;
|
|
|
|
m_new_connection = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-02-05 10:38:32 +01:00
|
|
|
send_id = random() & 0xffff;
|
2010-11-29 02:33:05 +01:00
|
|
|
recv_id = send_id - 1;
|
|
|
|
}
|
|
|
|
utp_socket_impl* impl = construct_utp_impl(recv_id, send_id, str, this);
|
|
|
|
m_utp_sockets.insert(std::make_pair(recv_id, impl));
|
|
|
|
return impl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|