3
Copyright (c) 2007, Arvid Norberg
6
Redistribution and use in source and binary forms, with or without
7
modification, are permitted provided that the following conditions
10
* Redistributions of source code must retain the above copyright
11
notice, this list of conditions and the following disclaimer.
12
* Redistributions in binary form must reproduce the above copyright
13
notice, this list of conditions and the following disclaimer in
14
the documentation and/or other materials provided with the distribution.
15
* Neither the name of the author nor the names of its
16
contributors may be used to endorse or promote products derived
17
from this software without specific prior written permission.
19
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
POSSIBILITY OF SUCH DAMAGE.
33
#include <asio/ip/host_name.hpp>
34
#include <asio/ip/multicast.hpp>
35
#include <boost/bind.hpp>
37
#include "libtorrent/socket.hpp"
38
#include "libtorrent/enum_net.hpp"
39
#include "libtorrent/broadcast_socket.hpp"
40
#include "libtorrent/assert.hpp"
44
bool is_local(address const& a)
46
if (a.is_v6()) return a.to_v6().is_link_local();
47
address_v4 a4 = a.to_v4();
48
unsigned long ip = a4.to_ulong();
49
return ((ip & 0xff000000) == 0x0a000000
50
|| (ip & 0xfff00000) == 0xac100000
51
|| (ip & 0xffff0000) == 0xc0a80000);
54
bool is_loopback(address const& addr)
57
return addr.to_v4() == address_v4::loopback();
59
return addr.to_v6() == address_v6::loopback();
62
bool is_multicast(address const& addr)
65
return addr.to_v4().is_multicast();
67
return addr.to_v6().is_multicast();
70
bool is_any(address const& addr)
73
return addr.to_v4() == address_v4::any();
75
return addr.to_v6() == address_v6::any();
78
address guess_local_address(asio::io_service& ios)
80
// make a best guess of the interface we're using and its IP
82
std::vector<ip_interface> const& interfaces = enum_net_interfaces(ios, ec);
83
address ret = address_v4::any();
84
for (std::vector<ip_interface>::const_iterator i = interfaces.begin()
85
, end(interfaces.end()); i != end; ++i)
87
address const& a = i->interface_address;
90
|| is_any(a)) continue;
92
// prefer a v4 address, but return a v6 if
94
if (a.is_v4()) return a;
96
if (ret != address_v4::any())
102
// count the length of the common bit prefix
103
int common_bits(unsigned char const* b1
104
, unsigned char const* b2, int n)
106
for (int i = 0; i < n; ++i, ++b1, ++b2)
108
unsigned char a = *b1 ^ *b2;
109
if (a == 0) continue;
111
for (; a > 0; a >>= 1) --ret;
117
// returns the number of bits in that differ from the right
118
// between the addresses.
119
int cidr_distance(address const& a1, address const& a2)
121
if (a1.is_v4() == a2.is_v4())
124
address_v4::bytes_type b1 = a1.to_v4().to_bytes();
125
address_v4::bytes_type b2 = a2.to_v4().to_bytes();
126
return address_v4::bytes_type::static_size * 8
127
- common_bits(b1.c_array(), b2.c_array(), b1.size());
130
address_v6::bytes_type b1;
131
address_v6::bytes_type b2;
132
if (a1.is_v4()) b1 = address_v6::v4_mapped(a1.to_v4()).to_bytes();
133
else b1 = a1.to_v6().to_bytes();
134
if (a2.is_v4()) b2 = address_v6::v4_mapped(a2.to_v4()).to_bytes();
135
else b2 = a2.to_v6().to_bytes();
136
return address_v6::bytes_type::static_size * 8
137
- common_bits(b1.c_array(), b2.c_array(), b1.size());
140
broadcast_socket::broadcast_socket(asio::io_service& ios
141
, udp::endpoint const& multicast_endpoint
142
, receive_handler_t const& handler
144
: m_multicast_endpoint(multicast_endpoint)
145
, m_on_receive(handler)
147
TORRENT_ASSERT(is_multicast(m_multicast_endpoint.address()));
149
using namespace asio::ip::multicast;
152
std::vector<ip_interface> interfaces = enum_net_interfaces(ios, ec);
154
for (std::vector<ip_interface>::const_iterator i = interfaces.begin()
155
, end(interfaces.end()); i != end; ++i)
157
// only broadcast to IPv4 addresses that are not local
158
if (!is_local(i->interface_address)) continue;
159
// only multicast on compatible networks
160
if (i->interface_address.is_v4() != multicast_endpoint.address().is_v4()) continue;
161
// ignore any loopback interface
162
if (is_loopback(i->interface_address)) continue;
164
boost::shared_ptr<datagram_socket> s(new datagram_socket(ios));
165
if (i->interface_address.is_v4())
167
s->open(udp::v4(), ec);
169
s->set_option(datagram_socket::reuse_address(true), ec);
171
s->bind(udp::endpoint(address_v4::any(), multicast_endpoint.port()), ec);
173
s->set_option(join_group(multicast_endpoint.address()), ec);
175
s->set_option(outbound_interface(i->interface_address.to_v4()), ec);
180
s->open(udp::v6(), ec);
182
s->set_option(datagram_socket::reuse_address(true), ec);
184
s->bind(udp::endpoint(address_v6::any(), multicast_endpoint.port()), ec);
186
s->set_option(join_group(multicast_endpoint.address()), ec);
188
// s->set_option(outbound_interface(i->interface_address.to_v6()), ec);
191
s->set_option(hops(255), ec);
193
s->set_option(enable_loopback(loopback), ec);
195
m_sockets.push_back(socket_entry(s));
196
socket_entry& se = m_sockets.back();
197
s->async_receive_from(asio::buffer(se.buffer, sizeof(se.buffer))
198
, se.remote, bind(&broadcast_socket::on_receive, this, &se, _1, _2));
200
// std::cerr << "broadcast socket [ if: " << i->to_v4().to_string()
201
// << " group: " << multicast_endpoint.address() << " ]" << std::endl;
206
void broadcast_socket::send(char const* buffer, int size, asio::error_code& ec)
208
for (std::list<socket_entry>::iterator i = m_sockets.begin()
209
, end(m_sockets.end()); i != end; ++i)
211
if (!i->socket) continue;
213
i->socket->send_to(asio::buffer(buffer, size), m_multicast_endpoint, 0, e);
215
// std::cerr << " sending on " << i->socket->local_endpoint().address().to_string() << std::endl;
225
void broadcast_socket::on_receive(socket_entry* s, asio::error_code const& ec
226
, std::size_t bytes_transferred)
228
if (ec || bytes_transferred == 0 || !m_on_receive) return;
229
m_on_receive(s->remote, s->buffer, bytes_transferred);
230
if (!s->socket) return;
231
s->socket->async_receive_from(asio::buffer(s->buffer, sizeof(s->buffer))
232
, s->remote, bind(&broadcast_socket::on_receive, this, s, _1, _2));
235
void broadcast_socket::close()
237
m_on_receive.clear();
239
for (std::list<socket_entry>::iterator i = m_sockets.begin()
240
, end(m_sockets.end()); i != end; ++i)
242
if (!i->socket) continue;