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();
82 else if (addr
.to_v6().is_v4_mapped())
83 return (addr
.to_v6().to_v4() == address_v4::any());
85 return addr
.to_v6() == address_v6::any();
88 address
guess_local_address(io_service
& ios
)
90 // make a best guess of the interface we're using and its IP
92 std::vector
<ip_interface
> const& interfaces
= enum_net_interfaces(ios
, ec
);
93 address ret
= address_v4::any();
94 for (std::vector
<ip_interface
>::const_iterator i
= interfaces
.begin()
95 , end(interfaces
.end()); i
!= end
; ++i
)
97 address
const& a
= i
->interface_address
;
100 || is_any(a
)) continue;
102 // prefer a v4 address, but return a v6 if
104 if (a
.is_v4()) return a
;
106 if (ret
!= address_v4::any())
112 // count the length of the common bit prefix
113 int common_bits(unsigned char const* b1
114 , unsigned char const* b2
, int n
)
116 for (int i
= 0; i
< n
; ++i
, ++b1
, ++b2
)
118 unsigned char a
= *b1
^ *b2
;
119 if (a
== 0) continue;
121 for (; a
> 0; a
>>= 1) --ret
;
127 // returns the number of bits in that differ from the right
128 // between the addresses.
129 int cidr_distance(address
const& a1
, address
const& a2
)
131 if (a1
.is_v4() == a2
.is_v4())
134 address_v4::bytes_type b1
= a1
.to_v4().to_bytes();
135 address_v4::bytes_type b2
= a2
.to_v4().to_bytes();
136 return address_v4::bytes_type::static_size
* 8
137 - common_bits(b1
.c_array(), b2
.c_array(), b1
.size());
140 address_v6::bytes_type b1
;
141 address_v6::bytes_type b2
;
142 if (a1
.is_v4()) b1
= address_v6::v4_mapped(a1
.to_v4()).to_bytes();
143 else b1
= a1
.to_v6().to_bytes();
144 if (a2
.is_v4()) b2
= address_v6::v4_mapped(a2
.to_v4()).to_bytes();
145 else b2
= a2
.to_v6().to_bytes();
146 return address_v6::bytes_type::static_size
* 8
147 - common_bits(b1
.c_array(), b2
.c_array(), b1
.size());
150 broadcast_socket::broadcast_socket(io_service
& ios
151 , udp::endpoint
const& multicast_endpoint
152 , receive_handler_t
const& handler
154 : m_multicast_endpoint(multicast_endpoint
)
155 , m_on_receive(handler
)
157 TORRENT_ASSERT(is_multicast(m_multicast_endpoint
.address()));
159 using namespace asio::ip::multicast
;
162 std::vector
<ip_interface
> interfaces
= enum_net_interfaces(ios
, ec
);
164 if (multicast_endpoint
.address().is_v4())
165 open_multicast_socket(ios
, address_v4::any(), loopback
);
167 open_multicast_socket(ios
, address_v6::any(), loopback
);
169 for (std::vector
<ip_interface
>::const_iterator i
= interfaces
.begin()
170 , end(interfaces
.end()); i
!= end
; ++i
)
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->interface_address.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
);
222 m_unicast_sockets
.push_back(socket_entry(s
));
223 socket_entry
& se
= m_unicast_sockets
.back();
224 s
->async_receive_from(asio::buffer(se
.buffer
, sizeof(se
.buffer
))
225 , se
.remote
, bind(&broadcast_socket::on_receive
, this, &se
, _1
, _2
));
228 void broadcast_socket::send(char const* buffer
, int size
, error_code
& ec
)
230 for (std::list
<socket_entry
>::iterator i
= m_unicast_sockets
.begin()
231 , end(m_unicast_sockets
.end()); i
!= end
; ++i
)
233 if (!i
->socket
) continue;
235 i
->socket
->send_to(asio::buffer(buffer
, size
), m_multicast_endpoint
, 0, e
);
237 // std::cerr << " sending on " << i->socket->local_endpoint().address().to_string() << " to: " << m_multicast_endpoint << std::endl;
247 void broadcast_socket::on_receive(socket_entry
* s
, error_code
const& ec
248 , std::size_t bytes_transferred
)
250 if (ec
|| bytes_transferred
== 0 || !m_on_receive
) return;
251 m_on_receive(s
->remote
, s
->buffer
, bytes_transferred
);
252 if (!s
->socket
) return;
253 s
->socket
->async_receive_from(asio::buffer(s
->buffer
, sizeof(s
->buffer
))
254 , s
->remote
, bind(&broadcast_socket::on_receive
, this, s
, _1
, _2
));
257 void broadcast_socket::close()
259 std::for_each(m_sockets
.begin(), m_sockets
.end(), bind(&socket_entry::close
, _1
));
260 std::for_each(m_unicast_sockets
.begin(), m_unicast_sockets
.end(), bind(&socket_entry::close
, _1
));
262 m_on_receive
.clear();