2007-09-10 01:52:34 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2007, Arvid Norberg
|
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2008-05-20 11:45:55 +02:00
|
|
|
#include <boost/version.hpp>
|
|
|
|
|
2009-11-27 08:08:47 +01:00
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
|
|
|
#include "libtorrent/socket.hpp"
|
|
|
|
#include "libtorrent/enum_net.hpp"
|
|
|
|
#include "libtorrent/broadcast_socket.hpp"
|
|
|
|
#include "libtorrent/assert.hpp"
|
|
|
|
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
#include "libtorrent/debug.hpp"
|
|
|
|
#endif
|
|
|
|
|
2009-11-29 20:41:07 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
//#include "libtorrent/socket_io.hpp"
|
|
|
|
#endif
|
|
|
|
|
2008-05-20 08:03:46 +02:00
|
|
|
#if BOOST_VERSION < 103500
|
|
|
|
#include <asio/ip/host_name.hpp>
|
|
|
|
#include <asio/ip/multicast.hpp>
|
|
|
|
#else
|
2008-05-03 18:05:42 +02:00
|
|
|
#include <boost/asio/ip/host_name.hpp>
|
|
|
|
#include <boost/asio/ip/multicast.hpp>
|
2008-05-20 08:03:46 +02:00
|
|
|
#endif
|
|
|
|
|
2007-09-10 01:52:34 +02:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
bool is_local(address const& a)
|
|
|
|
{
|
2011-02-25 18:00:36 +01:00
|
|
|
TORRENT_TRY {
|
2009-04-13 07:11:44 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
2011-02-25 18:00:36 +01:00
|
|
|
if (a.is_v6()) return a.to_v6().is_link_local();
|
2009-04-13 07:11:44 +02:00
|
|
|
#endif
|
2011-02-25 18:00:36 +01:00
|
|
|
address_v4 a4 = a.to_v4();
|
|
|
|
unsigned long ip = a4.to_ulong();
|
|
|
|
return ((ip & 0xff000000) == 0x0a000000 // 10.x.x.x
|
|
|
|
|| (ip & 0xfff00000) == 0xac100000 // 172.16.x.x
|
|
|
|
|| (ip & 0xffff0000) == 0xc0a80000 // 192.168.x.x
|
|
|
|
|| (ip & 0xffff0000) == 0xa9fe0000 // 169.254.x.x
|
|
|
|
|| (ip & 0xff000000) == 0x7f000000); // 127.x.x.x
|
|
|
|
} TORRENT_CATCH(std::exception& e) { return false; }
|
2007-09-10 01:52:34 +02:00
|
|
|
}
|
|
|
|
|
2007-09-19 23:54:26 +02:00
|
|
|
bool is_loopback(address const& addr)
|
|
|
|
{
|
2009-04-13 07:11:44 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
2011-02-25 18:00:36 +01:00
|
|
|
TORRENT_TRY {
|
|
|
|
if (addr.is_v4())
|
|
|
|
return addr.to_v4() == address_v4::loopback();
|
|
|
|
else
|
|
|
|
return addr.to_v6() == address_v6::loopback();
|
|
|
|
} TORRENT_CATCH(std::exception& e) { return false; }
|
2009-04-13 07:11:44 +02:00
|
|
|
#else
|
|
|
|
return addr.to_v4() == address_v4::loopback();
|
|
|
|
#endif
|
2007-09-19 23:54:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool is_multicast(address const& addr)
|
|
|
|
{
|
2009-04-13 07:11:44 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
2011-02-25 18:00:36 +01:00
|
|
|
TORRENT_TRY {
|
|
|
|
if (addr.is_v4())
|
|
|
|
return addr.to_v4().is_multicast();
|
|
|
|
else
|
|
|
|
return addr.to_v6().is_multicast();
|
|
|
|
} TORRENT_CATCH(std::exception& e) { return false; }
|
2009-04-13 07:11:44 +02:00
|
|
|
#else
|
|
|
|
return addr.to_v4().is_multicast();
|
|
|
|
#endif
|
2007-09-19 23:54:26 +02:00
|
|
|
}
|
|
|
|
|
2007-09-22 18:26:03 +02:00
|
|
|
bool is_any(address const& addr)
|
|
|
|
{
|
2011-02-25 18:00:36 +01:00
|
|
|
TORRENT_TRY {
|
2009-04-13 07:11:44 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
2007-09-22 18:26:03 +02:00
|
|
|
if (addr.is_v4())
|
|
|
|
return addr.to_v4() == address_v4::any();
|
2008-09-20 18:21:16 +02:00
|
|
|
else if (addr.to_v6().is_v4_mapped())
|
|
|
|
return (addr.to_v6().to_v4() == address_v4::any());
|
2007-09-22 18:26:03 +02:00
|
|
|
else
|
|
|
|
return addr.to_v6() == address_v6::any();
|
2009-04-13 07:11:44 +02:00
|
|
|
#else
|
|
|
|
return addr.to_v4() == address_v4::any();
|
|
|
|
#endif
|
2011-02-25 18:00:36 +01:00
|
|
|
} TORRENT_CATCH(std::exception& e) { return false; }
|
2009-04-13 07:11:44 +02:00
|
|
|
}
|
|
|
|
|
2010-11-29 02:33:05 +01:00
|
|
|
TORRENT_EXPORT bool is_teredo(address const& addr)
|
|
|
|
{
|
|
|
|
#if TORRENT_USE_IPV6
|
2011-02-25 18:00:36 +01:00
|
|
|
TORRENT_TRY {
|
|
|
|
if (!addr.is_v6()) return false;
|
|
|
|
boost::uint8_t teredo_prefix[] = {0x20, 0x01, 0, 0};
|
|
|
|
address_v6::bytes_type b = addr.to_v6().to_bytes();
|
|
|
|
return memcmp(&b[0], teredo_prefix, 4) == 0;
|
|
|
|
} TORRENT_CATCH(std::exception& e) { return false; }
|
2010-11-29 02:33:05 +01:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2009-04-13 07:11:44 +02:00
|
|
|
bool supports_ipv6()
|
|
|
|
{
|
|
|
|
#if TORRENT_USE_IPV6
|
2011-02-25 18:00:36 +01:00
|
|
|
TORRENT_TRY {
|
|
|
|
error_code ec;
|
|
|
|
address::from_string("::1", ec);
|
|
|
|
return !ec;
|
|
|
|
} TORRENT_CATCH(std::exception& e) { return false; }
|
2009-04-13 07:11:44 +02:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
2007-09-22 18:26:03 +02:00
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
address guess_local_address(io_service& ios)
|
2007-10-01 19:21:19 +02:00
|
|
|
{
|
|
|
|
// make a best guess of the interface we're using and its IP
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2008-01-11 07:49:37 +01:00
|
|
|
std::vector<ip_interface> const& interfaces = enum_net_interfaces(ios, ec);
|
2007-10-01 19:21:19 +02:00
|
|
|
address ret = address_v4::any();
|
2008-01-11 07:49:37 +01:00
|
|
|
for (std::vector<ip_interface>::const_iterator i = interfaces.begin()
|
2007-10-01 19:21:19 +02:00
|
|
|
, end(interfaces.end()); i != end; ++i)
|
|
|
|
{
|
2008-01-11 07:49:37 +01:00
|
|
|
address const& a = i->interface_address;
|
2007-10-01 19:21:19 +02:00
|
|
|
if (is_loopback(a)
|
|
|
|
|| is_multicast(a)
|
|
|
|
|| is_any(a)) continue;
|
|
|
|
|
|
|
|
// prefer a v4 address, but return a v6 if
|
|
|
|
// there are no v4
|
|
|
|
if (a.is_v4()) return a;
|
|
|
|
|
|
|
|
if (ret != address_v4::any())
|
|
|
|
ret = a;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-02-28 04:09:34 +01:00
|
|
|
// count the length of the common bit prefix
|
|
|
|
int common_bits(unsigned char const* b1
|
|
|
|
, unsigned char const* b2, int n)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < n; ++i, ++b1, ++b2)
|
|
|
|
{
|
|
|
|
unsigned char a = *b1 ^ *b2;
|
|
|
|
if (a == 0) continue;
|
|
|
|
int ret = i * 8 + 8;
|
|
|
|
for (; a > 0; a >>= 1) --ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
return n * 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns the number of bits in that differ from the right
|
2011-01-08 09:54:51 +01:00
|
|
|
// between the addresses. The larger number, the further apart
|
|
|
|
// the IPs are
|
2008-02-28 04:09:34 +01:00
|
|
|
int cidr_distance(address const& a1, address const& a2)
|
|
|
|
{
|
2009-04-13 07:11:44 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
2009-03-26 00:58:39 +01:00
|
|
|
if (a1.is_v4() && a2.is_v4())
|
2008-02-28 04:09:34 +01:00
|
|
|
{
|
2009-04-13 07:11:44 +02:00
|
|
|
#endif
|
2008-02-28 04:09:34 +01:00
|
|
|
// both are v4
|
|
|
|
address_v4::bytes_type b1 = a1.to_v4().to_bytes();
|
|
|
|
address_v4::bytes_type b2 = a2.to_v4().to_bytes();
|
2011-08-08 01:40:39 +02:00
|
|
|
return address_v4::bytes_type().size() * 8
|
|
|
|
- common_bits(b1.data(), b2.data(), b1.size());
|
2009-04-13 07:11:44 +02:00
|
|
|
#if TORRENT_USE_IPV6
|
2008-02-28 04:09:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
address_v6::bytes_type b1;
|
|
|
|
address_v6::bytes_type b2;
|
|
|
|
if (a1.is_v4()) b1 = address_v6::v4_mapped(a1.to_v4()).to_bytes();
|
|
|
|
else b1 = a1.to_v6().to_bytes();
|
|
|
|
if (a2.is_v4()) b2 = address_v6::v4_mapped(a2.to_v4()).to_bytes();
|
|
|
|
else b2 = a2.to_v6().to_bytes();
|
2011-08-08 01:40:39 +02:00
|
|
|
return address_v6::bytes_type().size() * 8
|
|
|
|
- common_bits(b1.data(), b2.data(), b1.size());
|
2009-04-13 07:11:44 +02:00
|
|
|
#endif
|
2008-02-28 04:09:34 +01:00
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
broadcast_socket::broadcast_socket(io_service& ios
|
2007-09-10 01:52:34 +02:00
|
|
|
, udp::endpoint const& multicast_endpoint
|
2007-09-18 03:25:28 +02:00
|
|
|
, receive_handler_t const& handler
|
|
|
|
, bool loopback)
|
2007-09-10 01:52:34 +02:00
|
|
|
: m_multicast_endpoint(multicast_endpoint)
|
|
|
|
, m_on_receive(handler)
|
2011-10-25 07:55:32 +02:00
|
|
|
, m_outstanding_operations(0)
|
|
|
|
, m_abort(false)
|
2007-09-10 01:52:34 +02:00
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(is_multicast(m_multicast_endpoint.address()));
|
2007-09-10 01:52:34 +02:00
|
|
|
|
|
|
|
using namespace asio::ip::multicast;
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2008-01-11 07:49:37 +01:00
|
|
|
std::vector<ip_interface> interfaces = enum_net_interfaces(ios, ec);
|
2007-09-10 01:52:34 +02:00
|
|
|
|
2009-12-13 17:29:58 +01:00
|
|
|
#if TORRENT_USE_IPV6
|
|
|
|
if (multicast_endpoint.address().is_v6())
|
2009-11-29 20:41:07 +01:00
|
|
|
open_multicast_socket(ios, address_v6::any(), loopback, ec);
|
2009-12-13 17:29:58 +01:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
open_multicast_socket(ios, address_v4::any(), loopback, ec);
|
2009-11-29 20:41:07 +01:00
|
|
|
|
2008-01-11 07:49:37 +01:00
|
|
|
for (std::vector<ip_interface>::const_iterator i = interfaces.begin()
|
2007-09-10 01:52:34 +02:00
|
|
|
, end(interfaces.end()); i != end; ++i)
|
|
|
|
{
|
2007-09-19 23:54:26 +02:00
|
|
|
// only multicast on compatible networks
|
2008-01-11 07:49:37 +01:00
|
|
|
if (i->interface_address.is_v4() != multicast_endpoint.address().is_v4()) continue;
|
2007-09-19 23:54:26 +02:00
|
|
|
// ignore any loopback interface
|
2009-11-29 20:41:07 +01:00
|
|
|
if (!loopback && is_loopback(i->interface_address)) continue;
|
2007-09-10 01:52:34 +02:00
|
|
|
|
2009-11-16 08:42:50 +01:00
|
|
|
ec = error_code();
|
2009-11-14 21:35:24 +01:00
|
|
|
open_multicast_socket(ios, i->interface_address, loopback, ec);
|
2007-09-15 22:20:07 +02:00
|
|
|
#ifndef NDEBUG
|
2011-02-17 07:47:26 +01:00
|
|
|
// fprintf(stderr, "broadcast socket [ if: %s group: %s mask: %s ] %s\n"
|
2009-11-14 21:35:24 +01:00
|
|
|
// , i->interface_address.to_string().c_str()
|
2011-02-17 07:47:26 +01:00
|
|
|
// , multicast_endpoint.address().to_string().c_str()
|
|
|
|
// , i->netmask.to_string().c_str()
|
2009-11-14 21:35:24 +01:00
|
|
|
// , ec.message().c_str());
|
2007-09-15 22:20:07 +02:00
|
|
|
#endif
|
2010-03-26 18:45:16 +01:00
|
|
|
open_unicast_socket(ios, i->interface_address
|
|
|
|
, i->netmask.is_v4() ? i->netmask.to_v4() : address_v4());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-09 19:51:15 +01:00
|
|
|
void broadcast_socket::open_multicast_socket(io_service& ios
|
2009-11-14 21:35:24 +01:00
|
|
|
, address const& addr, bool loopback, error_code& ec)
|
2008-01-09 19:51:15 +01:00
|
|
|
{
|
|
|
|
using namespace asio::ip::multicast;
|
|
|
|
|
|
|
|
boost::shared_ptr<datagram_socket> s(new datagram_socket(ios));
|
2009-04-04 18:59:53 +02:00
|
|
|
s->open(addr.is_v4() ? udp::v4() : udp::v6(), ec);
|
2008-01-09 19:51:15 +01:00
|
|
|
if (ec) return;
|
|
|
|
s->set_option(datagram_socket::reuse_address(true), ec);
|
|
|
|
if (ec) return;
|
|
|
|
s->bind(udp::endpoint(addr, m_multicast_endpoint.port()), ec);
|
|
|
|
if (ec) return;
|
|
|
|
s->set_option(join_group(m_multicast_endpoint.address()), ec);
|
|
|
|
if (ec) return;
|
|
|
|
s->set_option(hops(255), ec);
|
|
|
|
if (ec) return;
|
|
|
|
s->set_option(enable_loopback(loopback), ec);
|
|
|
|
if (ec) return;
|
|
|
|
m_sockets.push_back(socket_entry(s));
|
|
|
|
socket_entry& se = m_sockets.back();
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("broadcast_socket::on_receive");
|
|
|
|
#endif
|
2008-01-09 19:51:15 +01:00
|
|
|
s->async_receive_from(asio::buffer(se.buffer, sizeof(se.buffer))
|
2010-04-30 21:08:16 +02:00
|
|
|
, se.remote, boost::bind(&broadcast_socket::on_receive, this, &se, _1, _2));
|
2011-10-25 07:55:32 +02:00
|
|
|
++m_outstanding_operations;
|
2007-12-22 18:33:04 +01:00
|
|
|
}
|
|
|
|
|
2010-03-26 18:45:16 +01:00
|
|
|
void broadcast_socket::open_unicast_socket(io_service& ios, address const& addr
|
|
|
|
, address_v4 const& mask)
|
2007-12-22 18:33:04 +01:00
|
|
|
{
|
2008-01-09 19:51:15 +01:00
|
|
|
using namespace asio::ip::multicast;
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2007-12-22 18:33:04 +01:00
|
|
|
boost::shared_ptr<datagram_socket> s(new datagram_socket(ios));
|
|
|
|
s->open(addr.is_v4() ? udp::v4() : udp::v6(), ec);
|
|
|
|
if (ec) return;
|
|
|
|
s->bind(udp::endpoint(addr, 0), ec);
|
|
|
|
if (ec) return;
|
2011-02-17 07:47:26 +01:00
|
|
|
|
2010-03-26 18:45:16 +01:00
|
|
|
m_unicast_sockets.push_back(socket_entry(s, mask));
|
2007-12-22 18:33:04 +01:00
|
|
|
socket_entry& se = m_unicast_sockets.back();
|
2011-02-17 07:47:26 +01:00
|
|
|
|
|
|
|
// allow sending broadcast messages
|
|
|
|
asio::socket_base::broadcast option(true);
|
|
|
|
s->set_option(option, ec);
|
|
|
|
if (!ec) se.broadcast = true;
|
|
|
|
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("broadcast_socket::on_receive");
|
|
|
|
#endif
|
2007-12-22 18:33:04 +01:00
|
|
|
s->async_receive_from(asio::buffer(se.buffer, sizeof(se.buffer))
|
2010-04-30 21:08:16 +02:00
|
|
|
, se.remote, boost::bind(&broadcast_socket::on_receive, this, &se, _1, _2));
|
2011-10-25 07:55:32 +02:00
|
|
|
++m_outstanding_operations;
|
2007-09-10 01:52:34 +02:00
|
|
|
}
|
|
|
|
|
2011-02-17 07:47:26 +01:00
|
|
|
void broadcast_socket::send(char const* buffer, int size, error_code& ec, int flags)
|
2007-09-10 01:52:34 +02:00
|
|
|
{
|
2007-12-22 18:33:04 +01:00
|
|
|
for (std::list<socket_entry>::iterator i = m_unicast_sockets.begin()
|
|
|
|
, end(m_unicast_sockets.end()); i != end; ++i)
|
2007-09-10 01:52:34 +02:00
|
|
|
{
|
2008-01-03 11:58:16 +01:00
|
|
|
if (!i->socket) continue;
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code e;
|
2008-01-07 03:33:45 +01:00
|
|
|
i->socket->send_to(asio::buffer(buffer, size), m_multicast_endpoint, 0, e);
|
2011-02-17 07:47:26 +01:00
|
|
|
|
|
|
|
// if the user specified the broadcast flag, send one to the broadcast
|
|
|
|
// address as well
|
|
|
|
if ((flags & broadcast_socket::broadcast) && i->can_broadcast())
|
|
|
|
i->socket->send_to(asio::buffer(buffer, size)
|
|
|
|
, udp::endpoint(i->broadcast_address(), m_multicast_endpoint.port()), 0, e);
|
|
|
|
|
2008-01-07 03:33:45 +01:00
|
|
|
#ifndef NDEBUG
|
2009-11-29 20:41:07 +01:00
|
|
|
// fprintf(stderr, " sending on unicast %s to: %s\n", print_address(i->socket->local_endpoint().address()).c_str()
|
|
|
|
// , print_endpoint(m_multicast_endpoint).c_str());
|
|
|
|
#endif
|
|
|
|
if (e)
|
|
|
|
{
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// fprintf(stderr, " ERROR: %s\n", e.message().c_str());
|
|
|
|
#endif
|
|
|
|
i->socket->close(e);
|
|
|
|
i->socket.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::list<socket_entry>::iterator i = m_sockets.begin()
|
|
|
|
, end(m_sockets.end()); i != end; ++i)
|
|
|
|
{
|
|
|
|
if (!i->socket) continue;
|
|
|
|
error_code e;
|
|
|
|
i->socket->send_to(asio::buffer(buffer, size), m_multicast_endpoint, 0, e);
|
|
|
|
#ifndef NDEBUG
|
2009-11-14 21:35:24 +01:00
|
|
|
// extern std::string print_address(address const& addr);
|
|
|
|
// extern std::string print_endpoint(udp::endpoint const& ep);
|
2009-11-29 20:41:07 +01:00
|
|
|
// fprintf(stderr, " sending on multicast %s to: %s\n", print_address(i->socket->local_endpoint().address()).c_str()
|
2009-11-14 21:35:24 +01:00
|
|
|
// , print_endpoint(m_multicast_endpoint).c_str());
|
2007-09-15 22:20:07 +02:00
|
|
|
#endif
|
2008-01-03 11:58:16 +01:00
|
|
|
if (e)
|
|
|
|
{
|
2009-11-29 20:41:07 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
// fprintf(stderr, " ERROR: %s\n", e.message().c_str());
|
|
|
|
#endif
|
2008-01-03 11:58:16 +01:00
|
|
|
i->socket->close(e);
|
|
|
|
i->socket.reset();
|
|
|
|
}
|
2007-09-10 01:52:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void broadcast_socket::on_receive(socket_entry* s, error_code const& ec
|
2007-09-10 01:52:34 +02:00
|
|
|
, std::size_t bytes_transferred)
|
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
complete_async("broadcast_socket::on_receive");
|
|
|
|
#endif
|
2011-10-25 07:55:32 +02:00
|
|
|
TORRENT_ASSERT(m_outstanding_operations > 0);
|
|
|
|
--m_outstanding_operations;
|
|
|
|
|
|
|
|
if (ec || bytes_transferred == 0 || !m_on_receive)
|
|
|
|
{
|
|
|
|
maybe_abort();
|
|
|
|
return;
|
|
|
|
}
|
2007-09-10 01:52:34 +02:00
|
|
|
m_on_receive(s->remote, s->buffer, bytes_transferred);
|
2011-10-25 07:55:32 +02:00
|
|
|
|
|
|
|
if (maybe_abort()) return;
|
2008-01-03 11:58:16 +01:00
|
|
|
if (!s->socket) return;
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("broadcast_socket::on_receive");
|
|
|
|
#endif
|
2007-09-10 01:52:34 +02:00
|
|
|
s->socket->async_receive_from(asio::buffer(s->buffer, sizeof(s->buffer))
|
2010-04-30 21:08:16 +02:00
|
|
|
, s->remote, boost::bind(&broadcast_socket::on_receive, this, s, _1, _2));
|
2011-10-25 07:55:32 +02:00
|
|
|
++m_outstanding_operations;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool broadcast_socket::maybe_abort()
|
|
|
|
{
|
2011-11-19 23:17:13 +01:00
|
|
|
bool ret = m_abort;
|
2011-10-25 07:55:32 +02:00
|
|
|
if (m_abort && m_outstanding_operations == 0)
|
2011-10-25 12:28:54 +02:00
|
|
|
{
|
|
|
|
// it's important that m_on_receive is cleared
|
|
|
|
// before the object is destructed, since it may
|
|
|
|
// hold a reference to ourself, which would otherwise
|
|
|
|
// cause an infinite recursion destructing the objects
|
|
|
|
receive_handler_t().swap(m_on_receive);
|
|
|
|
}
|
2011-11-19 23:17:13 +01:00
|
|
|
return ret;
|
2007-09-10 01:52:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void broadcast_socket::close()
|
|
|
|
{
|
2010-04-30 21:08:16 +02:00
|
|
|
std::for_each(m_sockets.begin(), m_sockets.end(), boost::bind(&socket_entry::close, _1));
|
|
|
|
std::for_each(m_unicast_sockets.begin(), m_unicast_sockets.end(), boost::bind(&socket_entry::close, _1));
|
2008-01-07 03:33:45 +01:00
|
|
|
|
2011-10-25 07:55:32 +02:00
|
|
|
m_abort = true;
|
|
|
|
maybe_abort();
|
2007-09-10 01:52:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|