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 <boost/version.hpp>
35 #if BOOST_VERSION < 103500
36 #include <asio/ip/host_name.hpp>
37 #include <asio/ip/multicast.hpp>
39 #include <boost/asio/ip/host_name.hpp>
40 #include <boost/asio/ip/multicast.hpp>
43 #include <boost/bind.hpp>
45 #include "libtorrent/socket.hpp"
46 #include "libtorrent/enum_net.hpp"
47 #include "libtorrent/broadcast_socket.hpp"
48 #include "libtorrent/assert.hpp"
52 bool is_local(address
const& a
)
54 if (a
.is_v6()) return a
.to_v6().is_link_local();
55 address_v4 a4
= a
.to_v4();
56 unsigned long ip
= a4
.to_ulong();
57 return ((ip
& 0xff000000) == 0x0a000000
58 || (ip
& 0xfff00000) == 0xac100000
59 || (ip
& 0xffff0000) == 0xc0a80000);
62 bool is_loopback(address
const& addr
)
65 return addr
.to_v4() == address_v4::loopback();
67 return addr
.to_v6() == address_v6::loopback();
70 bool is_multicast(address
const& addr
)
73 return addr
.to_v4().is_multicast();
75 return addr
.to_v6().is_multicast();
78 bool is_any(address
const& addr
)
81 return addr
.to_v4() == address_v4::any();
83 return addr
.to_v6() == address_v6::any();
86 address
guess_local_address(io_service
& ios
)
88 // make a best guess of the interface we're using and its IP
90 std::vector
<ip_interface
> const& interfaces
= enum_net_interfaces(ios
, ec
);
91 address ret
= address_v4::any();
92 for (std::vector
<ip_interface
>::const_iterator i
= interfaces
.begin()
93 , end(interfaces
.end()); i
!= end
; ++i
)
95 address
const& a
= i
->interface_address
;
98 || is_any(a
)) continue;
100 // prefer a v4 address, but return a v6 if
102 if (a
.is_v4()) return a
;
104 if (ret
!= address_v4::any())
110 // count the length of the common bit prefix
111 int common_bits(unsigned char const* b1
112 , unsigned char const* b2
, int n
)
114 for (int i
= 0; i
< n
; ++i
, ++b1
, ++b2
)
116 unsigned char a
= *b1
^ *b2
;
117 if (a
== 0) continue;
119 for (; a
> 0; a
>>= 1) --ret
;
125 // returns the number of bits in that differ from the right
126 // between the addresses.
127 int cidr_distance(address
const& a1
, address
const& a2
)
129 if (a1
.is_v4() == a2
.is_v4())
132 address_v4::bytes_type b1
= a1
.to_v4().to_bytes();
133 address_v4::bytes_type b2
= a2
.to_v4().to_bytes();
134 return address_v4::bytes_type::static_size
* 8
135 - common_bits(b1
.c_array(), b2
.c_array(), b1
.size());
138 address_v6::bytes_type b1
;
139 address_v6::bytes_type b2
;
140 if (a1
.is_v4()) b1
= address_v6::v4_mapped(a1
.to_v4()).to_bytes();
141 else b1
= a1
.to_v6().to_bytes();
142 if (a2
.is_v4()) b2
= address_v6::v4_mapped(a2
.to_v4()).to_bytes();
143 else b2
= a2
.to_v6().to_bytes();
144 return address_v6::bytes_type::static_size
* 8
145 - common_bits(b1
.c_array(), b2
.c_array(), b1
.size());
148 broadcast_socket::broadcast_socket(io_service
& ios
149 , udp::endpoint
const& multicast_endpoint
150 , receive_handler_t
const& handler
152 : m_multicast_endpoint(multicast_endpoint
)
153 , m_on_receive(handler
)
155 TORRENT_ASSERT(is_multicast(m_multicast_endpoint
.address()));
157 using namespace asio::ip::multicast
;
160 std::vector
<ip_interface
> interfaces
= enum_net_interfaces(ios
, ec
);
162 if (multicast_endpoint
.address().is_v4())
163 open_multicast_socket(ios
, address_v4::any(), loopback
);
165 open_multicast_socket(ios
, address_v6::any(), loopback
);
167 for (std::vector
<ip_interface
>::const_iterator i
= interfaces
.begin()
168 , end(interfaces
.end()); i
!= end
; ++i
)
170 // only broadcast to IPv4 addresses that are not local
171 if (!is_local(i
->interface_address
)) continue;
172 // only multicast on compatible networks
173 if (i
->interface_address
.is_v4() != multicast_endpoint
.address().is_v4()) continue;
174 // ignore any loopback interface
175 if (is_loopback(i
->interface_address
)) continue;
178 // std::cerr << "broadcast socket [ if: " << i->to_v4().to_string()
179 // << " group: " << multicast_endpoint.address() << " ]" << std::endl;
181 open_unicast_socket(ios
, i
->interface_address
);
185 void broadcast_socket::open_multicast_socket(io_service
& ios
186 , address
const& addr
, bool loopback
)
188 using namespace asio::ip::multicast
;
191 boost::shared_ptr
<datagram_socket
> s(new datagram_socket(ios
));
193 s
->open(udp::v4(), ec
);
195 s
->open(udp::v6(), ec
);
197 s
->set_option(datagram_socket::reuse_address(true), ec
);
199 s
->bind(udp::endpoint(addr
, m_multicast_endpoint
.port()), ec
);
201 s
->set_option(join_group(m_multicast_endpoint
.address()), ec
);
203 s
->set_option(hops(255), ec
);
205 s
->set_option(enable_loopback(loopback
), ec
);
207 m_sockets
.push_back(socket_entry(s
));
208 socket_entry
& se
= m_sockets
.back();
209 s
->async_receive_from(asio::buffer(se
.buffer
, sizeof(se
.buffer
))
210 , se
.remote
, bind(&broadcast_socket::on_receive
, this, &se
, _1
, _2
));
213 void broadcast_socket::open_unicast_socket(io_service
& ios
, address
const& addr
)
215 using namespace asio::ip::multicast
;
217 boost::shared_ptr
<datagram_socket
> s(new datagram_socket(ios
));
218 s
->open(addr
.is_v4() ? udp::v4() : udp::v6(), ec
);
220 s
->bind(udp::endpoint(addr
, 0), ec
);
223 s
->set_option(outbound_interface(addr
.to_v4()), ec
);
225 m_unicast_sockets
.push_back(socket_entry(s
));
226 socket_entry
& se
= m_unicast_sockets
.back();
227 s
->async_receive_from(asio::buffer(se
.buffer
, sizeof(se
.buffer
))
228 , se
.remote
, bind(&broadcast_socket::on_receive
, this, &se
, _1
, _2
));
231 void broadcast_socket::send(char const* buffer
, int size
, error_code
& ec
)
233 for (std::list
<socket_entry
>::iterator i
= m_unicast_sockets
.begin()
234 , end(m_unicast_sockets
.end()); i
!= end
; ++i
)
236 if (!i
->socket
) continue;
238 i
->socket
->send_to(asio::buffer(buffer
, size
), m_multicast_endpoint
, 0, e
);
240 // std::cerr << " sending on " << i->socket->local_endpoint().address().to_string() << " to: " << m_multicast_endpoint << std::endl;
250 void broadcast_socket::on_receive(socket_entry
* s
, error_code
const& ec
251 , std::size_t bytes_transferred
)
253 if (ec
|| bytes_transferred
== 0 || !m_on_receive
) return;
254 m_on_receive(s
->remote
, s
->buffer
, bytes_transferred
);
255 if (!s
->socket
) return;
256 s
->socket
->async_receive_from(asio::buffer(s
->buffer
, sizeof(s
->buffer
))
257 , s
->remote
, bind(&broadcast_socket::on_receive
, this, s
, _1
, _2
));
260 void broadcast_socket::close()
262 std::for_each(m_sockets
.begin(), m_sockets
.end(), bind(&socket_entry::close
, _1
));
263 std::for_each(m_unicast_sockets
.begin(), m_unicast_sockets
.end(), bind(&socket_entry::close
, _1
));
265 m_on_receive
.clear();