AUTO_LT_SYNC
[tore.git] / libtorrent / src / lsd.cpp
blob2d4e8b7da2d8a8eb724d67403bc0321f5505a8a6
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 "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>
46 #else
47 #include <boost/asio/ip/host_name.hpp>
48 #include <boost/asio/ip/multicast.hpp>
49 #endif
50 #include <boost/thread/mutex.hpp>
51 #include <cstdlib>
52 #include <boost/config.hpp>
54 using boost::bind;
55 using namespace libtorrent;
57 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)
65 : m_callback(cb)
66 , m_retry_count(1)
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)
70 , m_disabled(false)
72 #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
73 m_log.open("lsd.log", std::ios::in | std::ios::out | std::ios::trunc);
74 #endif
77 lsd::~lsd() {}
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"
88 "\r\n\r\n";
89 std::string const& msg = btsearch.str();
91 m_retry_count = 1;
92 error_code ec;
93 m_socket.send(msg.c_str(), int(msg.size()), ec);
94 if (ec)
96 m_disabled = true;
97 return;
100 #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
101 m_log << time_now_string()
102 << " ==> announce: ih: " << ih << " port: " << listen_port << std::endl;
103 #endif
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)
111 if (e) return;
113 error_code ec;
114 m_socket.send(msg.c_str(), int(msg.size()), ec);
116 ++m_retry_count;
117 if (m_retry_count >= 5)
118 return;
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;
129 http_parser p;
131 bool error = false;
132 p.incoming(buffer::const_interval(buffer, buffer + bytes_transferred)
133 , error);
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";
140 #endif
141 return;
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;
149 #endif
150 return;
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;
159 #endif
160 return;
163 std::string const& ih_str = p.header("infohash");
164 if (ih_str.empty())
166 #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING)
167 m_log << time_now_string()
168 << " <== announce: invalid BT-SEARCH, missing infohash" << std::endl;
169 #endif
170 return;
173 sha1_hash ih(0);
174 std::istringstream ih_sstr(ih_str);
175 ih_sstr >> ih;
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;
184 #endif
185 // we got an announce, pass it on through the callback
186 #ifndef BOOST_NO_EXCEPTIONS
187 try {
188 #endif
189 m_callback(tcp::endpoint(from.address(), port), ih);
190 #ifndef BOOST_NO_EXCEPTIONS
192 catch (std::exception&) {}
193 #endif
197 void lsd::close()
199 m_socket.close();
200 error_code ec;
201 m_broadcast_timer.cancel(ec);
202 m_disabled = true;
203 m_callback.clear();