AUTO_LT_SYNC
[tore.git] / libtorrent / src / broadcast_socket.cpp
blob43654a11afd314399710fcb91b96f28652bfeacd
1 /*
3 Copyright (c) 2007, Arvid Norberg
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
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>
38 #else
39 #include <boost/asio/ip/host_name.hpp>
40 #include <boost/asio/ip/multicast.hpp>
41 #endif
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"
50 namespace libtorrent
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)
64 if (addr.is_v4())
65 return addr.to_v4() == address_v4::loopback();
66 else
67 return addr.to_v6() == address_v6::loopback();
70 bool is_multicast(address const& addr)
72 if (addr.is_v4())
73 return addr.to_v4().is_multicast();
74 else
75 return addr.to_v6().is_multicast();
78 bool is_any(address const& addr)
80 if (addr.is_v4())
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());
84 else
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
91 error_code ec;
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;
98 if (is_loopback(a)
99 || is_multicast(a)
100 || is_any(a)) continue;
102 // prefer a v4 address, but return a v6 if
103 // there are no v4
104 if (a.is_v4()) return a;
106 if (ret != address_v4::any())
107 ret = a;
109 return ret;
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;
120 int ret = i * 8 + 8;
121 for (; a > 0; a >>= 1) --ret;
122 return ret;
124 return n * 8;
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())
133 // both are 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
153 , bool loopback)
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;
161 error_code ec;
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);
166 else
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;
177 #ifndef NDEBUG
178 // std::cerr << "broadcast socket [ if: " << i->interface_address.to_v4().to_string()
179 // << " group: " << multicast_endpoint.address() << " ]" << std::endl;
180 #endif
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;
190 error_code ec;
191 boost::shared_ptr<datagram_socket> s(new datagram_socket(ios));
192 if (addr.is_v4())
193 s->open(udp::v4(), ec);
194 else
195 s->open(udp::v6(), ec);
196 if (ec) return;
197 s->set_option(datagram_socket::reuse_address(true), ec);
198 if (ec) return;
199 s->bind(udp::endpoint(addr, m_multicast_endpoint.port()), ec);
200 if (ec) return;
201 s->set_option(join_group(m_multicast_endpoint.address()), ec);
202 if (ec) return;
203 s->set_option(hops(255), ec);
204 if (ec) return;
205 s->set_option(enable_loopback(loopback), ec);
206 if (ec) return;
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;
216 error_code ec;
217 boost::shared_ptr<datagram_socket> s(new datagram_socket(ios));
218 s->open(addr.is_v4() ? udp::v4() : udp::v6(), ec);
219 if (ec) return;
220 s->bind(udp::endpoint(addr, 0), ec);
221 if (ec) return;
222 if (addr.is_v4())
223 s->set_option(outbound_interface(addr.to_v4()), ec);
224 if (ec) return;
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;
237 error_code e;
238 i->socket->send_to(asio::buffer(buffer, size), m_multicast_endpoint, 0, e);
239 #ifndef NDEBUG
240 // std::cerr << " sending on " << i->socket->local_endpoint().address().to_string() << " to: " << m_multicast_endpoint << std::endl;
241 #endif
242 if (e)
244 i->socket->close(e);
245 i->socket.reset();
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();