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 "libtorrent/pch.hpp"
35 #include "libtorrent/lsd.hpp"
36 #include "libtorrent/io.hpp"
37 #include "libtorrent/http_tracker_connection.hpp"
38 #include "libtorrent/xml_parse.hpp"
39 #include <boost/bind.hpp>
40 #include <boost/ref.hpp>
41 #include <asio/ip/host_name.hpp>
42 #include <asio/ip/multicast.hpp>
43 #include <boost/thread/mutex.hpp>
45 #include <boost/config.hpp>
48 using namespace libtorrent
;
52 // defined in upnp.cpp
53 address_v4
guess_local_address(asio::io_service
&);
56 address_v4
lsd::lsd_multicast_address
;
57 udp::endpoint
lsd::lsd_multicast_endpoint
;
59 lsd::lsd(io_service
& ios
, address
const& listen_interface
60 , peer_callback_t
const& cb
)
64 , m_broadcast_timer(ios
)
67 // Bittorrent Local discovery multicast address and port
68 lsd_multicast_address
= address_v4::from_string("239.192.152.143");
69 lsd_multicast_endpoint
= udp::endpoint(lsd_multicast_address
, 6771);
71 #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
72 m_log
.open("lsd.log", std::ios::in
| std::ios::out
| std::ios::trunc
);
74 assert(lsd_multicast_address
.is_multicast());
75 rebind(listen_interface
);
80 void lsd::rebind(address
const& listen_interface
)
82 address_v4 local_ip
= address_v4::any();
83 if (listen_interface
.is_v4() && listen_interface
!= address_v4::any())
85 local_ip
= listen_interface
.to_v4();
90 // the local interface hasn't changed
91 if (m_socket
.is_open()
92 && m_socket
.local_endpoint().address() == local_ip
)
97 using namespace asio::ip::multicast
;
99 m_socket
.open(udp::v4());
100 m_socket
.set_option(datagram_socket::reuse_address(true));
101 m_socket
.bind(udp::endpoint(local_ip
, 6771));
103 #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
104 m_log
<< "local ip: " << local_ip
<< std::endl
;
107 m_socket
.set_option(join_group(lsd_multicast_address
));
108 m_socket
.set_option(outbound_interface(local_ip
));
109 m_socket
.set_option(enable_loopback(true));
110 m_socket
.set_option(hops(255));
112 catch (std::exception
& e
)
114 #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
115 m_log
<< "socket multicast error " << e
.what()
116 << ". disabling local service discovery" << std::endl
;
126 void lsd::announce(sha1_hash
const& ih
, int listen_port
)
128 if (m_disabled
) return;
130 std::stringstream btsearch
;
131 btsearch
<< "BT-SEARCH * HTTP/1.1\r\n"
132 "Host: 239.192.152.143:6771\r\n"
133 "Port: " << listen_port
<< "\r\n"
134 "Infohash: " << ih
<< "\r\n"
136 std::string
const& msg
= btsearch
.str();
140 m_socket
.send_to(asio::buffer(msg
.c_str(), msg
.size() - 1)
141 , lsd_multicast_endpoint
, 0, ec
);
148 #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
149 m_log
<< time_now_string()
150 << " ==> announce: ih: " << ih
<< " port: " << listen_port
<< std::endl
;
153 m_broadcast_timer
.expires_from_now(milliseconds(250 * m_retry_count
));
154 m_broadcast_timer
.async_wait(bind(&lsd::resend_announce
, this, _1
, msg
));
157 void lsd::resend_announce(asio::error_code
const& e
, std::string msg
) try
161 m_socket
.send_to(asio::buffer(msg
, msg
.size() - 1)
162 , lsd_multicast_endpoint
);
165 if (m_retry_count
>= 5)
168 m_broadcast_timer
.expires_from_now(milliseconds(250 * m_retry_count
));
169 m_broadcast_timer
.async_wait(bind(&lsd::resend_announce
, this, _1
, msg
));
171 catch (std::exception
&)
174 void lsd::on_announce(asio::error_code
const& e
175 , std::size_t bytes_transferred
)
177 using namespace libtorrent::detail
;
180 char* p
= m_receive_buffer
;
181 char* end
= m_receive_buffer
+ bytes_transferred
;
182 char* line
= std::find(p
, end
, '\n');
183 for (char* i
= p
; i
< line
; ++i
) *i
= std::tolower(*i
);
184 #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
185 m_log
<< time_now_string()
186 << " <== announce: " << std::string(p
, line
) << std::endl
;
188 if (line
== end
|| (line
- p
>= 9 && std::memcmp("bt-search", p
, 9)))
190 #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
191 m_log
<< time_now_string()
192 << " *** assumed 'bt-search', ignoring" << std::endl
;
202 line
= std::find(p
, end
, '\n');
203 if (line
== end
) break;
205 for (char* i
= p
; i
< line
; ++i
) *i
= std::tolower(*i
);
206 if (line
- p
>= 5 && memcmp(p
, "port:", 5) == 0)
209 while (*p
== ' ') ++p
;
212 else if (line
- p
>= 9 && memcmp(p
, "infohash:", 9) == 0)
215 while (*p
== ' ') ++p
;
216 if (line
- p
> 40) p
[40] = 0;
217 try { ih
= boost::lexical_cast
<sha1_hash
>(p
); }
218 catch (std::exception
&) {}
223 if (!ih
.is_all_zeros() && port
!= 0)
225 #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
226 m_log
<< time_now_string()
227 << " *** incoming local announce " << m_remote
.address()
228 << ":" << port
<< " ih: " << ih
<< std::endl
;
230 // we got an announce, pass it on through the callback
231 try { m_callback(tcp::endpoint(m_remote
.address(), port
), ih
); }
232 catch (std::exception
&) {}
237 void lsd::setup_receive() try
239 assert(m_socket
.is_open());
240 m_socket
.async_receive_from(asio::buffer(m_receive_buffer
241 , sizeof(m_receive_buffer
)), m_remote
, bind(&lsd::on_announce
, this, _1
, _2
));
243 catch (std::exception
&)