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/buffer.hpp"
39 #include "libtorrent/http_parser.hpp"
41 #include <boost/bind.hpp>
42 #include <boost/ref.hpp>
43 #if BOOST_VERSION < 103500
44 #include <asio/ip/host_name.hpp>
45 #include <asio/ip/multicast.hpp>
47 #include <boost/asio/ip/host_name.hpp>
48 #include <boost/asio/ip/multicast.hpp>
50 #include <boost/thread/mutex.hpp>
52 #include <boost/config.hpp>
55 using namespace libtorrent
;
59 // defined in broadcast_socket.cpp
60 address
guess_local_address(io_service
&);
63 lsd::lsd(io_service
& ios
, address
const& listen_interface
64 , peer_callback_t
const& cb
)
67 , m_socket(ios
, udp::endpoint(address_v4::from_string("239.192.152.143"), 6771)
68 , bind(&lsd::on_announce
, self(), _1
, _2
, _3
))
69 , m_broadcast_timer(ios
)
72 #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
73 m_log
.open("lsd.log", std::ios::in
| std::ios::out
| std::ios::trunc
);
79 void lsd::announce(sha1_hash
const& ih
, int listen_port
)
81 if (m_disabled
) return;
83 std::stringstream btsearch
;
84 btsearch
<< "BT-SEARCH * HTTP/1.1\r\n"
85 "Host: 239.192.152.143:6771\r\n"
86 "Port: " << listen_port
<< "\r\n"
87 "Infohash: " << ih
<< "\r\n"
89 std::string
const& msg
= btsearch
.str();
93 m_socket
.send(msg
.c_str(), int(msg
.size()), ec
);
100 #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
101 m_log
<< time_now_string()
102 << " ==> announce: ih: " << ih
<< " port: " << listen_port
<< std::endl
;
105 m_broadcast_timer
.expires_from_now(milliseconds(250 * m_retry_count
), ec
);
106 m_broadcast_timer
.async_wait(bind(&lsd::resend_announce
, self(), _1
, msg
));
109 void lsd::resend_announce(error_code
const& e
, std::string msg
)
114 m_socket
.send(msg
.c_str(), int(msg
.size()), ec
);
117 if (m_retry_count
>= 5)
120 m_broadcast_timer
.expires_from_now(milliseconds(250 * m_retry_count
), ec
);
121 m_broadcast_timer
.async_wait(bind(&lsd::resend_announce
, self(), _1
, msg
));
124 void lsd::on_announce(udp::endpoint
const& from
, char* buffer
125 , std::size_t bytes_transferred
)
127 using namespace libtorrent::detail
;
132 p
.incoming(buffer::const_interval(buffer
, buffer
+ bytes_transferred
)
135 if (!p
.header_finished() || error
)
137 #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
138 m_log
<< time_now_string()
139 << " <== announce: incomplete HTTP message\n";
144 if (p
.method() != "bt-search")
146 #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
147 m_log
<< time_now_string()
148 << " <== announce: invalid HTTP method: " << p
.method() << std::endl
;
153 std::string
const& port_str
= p
.header("port");
154 if (port_str
.empty())
156 #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
157 m_log
<< time_now_string()
158 << " <== announce: invalid BT-SEARCH, missing port" << std::endl
;
163 std::string
const& ih_str
= p
.header("infohash");
166 #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
167 m_log
<< time_now_string()
168 << " <== announce: invalid BT-SEARCH, missing infohash" << std::endl
;
174 std::istringstream
ih_sstr(ih_str
);
176 int port
= atoi(port_str
.c_str());
178 if (!ih
.is_all_zeros() && port
!= 0)
180 #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
181 m_log
<< time_now_string()
182 << " *** incoming local announce " << from
.address()
183 << ":" << port
<< " ih: " << ih
<< std::endl
;
185 // we got an announce, pass it on through the callback
186 #ifndef BOOST_NO_EXCEPTIONS
189 m_callback(tcp::endpoint(from
.address(), port
), ih
);
190 #ifndef BOOST_NO_EXCEPTIONS
192 catch (std::exception
&) {}
201 m_broadcast_timer
.cancel(ec
);