using string_view in node::incoming_request and plugin::on_dht_request (#1001)

use string_view in node::incoming_request. changed plugin::on_dht_request to use string_view
This commit is contained in:
Alden Torres 2016-08-15 16:17:13 -04:00 committed by Arvid Norberg
parent 2f7adfbb92
commit f51e782905
19 changed files with 30 additions and 53 deletions

View File

@ -1,3 +1,4 @@
* plugin API changed to handle DHT requests using string_view
* removed support for lt_trackers and metadata_transfer extensions * removed support for lt_trackers and metadata_transfer extensions
(pre-dating ut_metadata) (pre-dating ut_metadata)
* support windows' CryptoAPI for SHA-1 * support windows' CryptoAPI for SHA-1

View File

@ -625,7 +625,7 @@ namespace libtorrent
, udp::endpoint node) override; , udp::endpoint node) override;
#endif #endif
virtual bool on_dht_request(char const* query, int query_len virtual bool on_dht_request(string_view query
, dht::msg const& request, entry& response) override; , dht::msg const& request, entry& response) override;
void set_external_address(address const& ip void set_external_address(address const& ip
@ -1247,6 +1247,4 @@ namespace libtorrent
} }
} }
#endif #endif

View File

@ -72,7 +72,6 @@ POSSIBILITY OF SUCH DAMAGE.
// structures it's parsing. // structures it's parsing.
#include <string> #include <string>
#include <exception>
#include <iterator> // for distance #include <iterator> // for distance
#include "libtorrent/config.hpp" #include "libtorrent/config.hpp"

View File

@ -46,7 +46,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/aux_/disable_warnings_push.hpp" #include "libtorrent/aux_/disable_warnings_push.hpp"
#include <boost/smart_ptr.hpp> #include <boost/smart_ptr.hpp>
#include <boost/noncopyable.hpp>
#include <boost/optional.hpp> #include <boost/optional.hpp>
#include "libtorrent/aux_/disable_warnings_pop.hpp" #include "libtorrent/aux_/disable_warnings_pop.hpp"

View File

@ -33,7 +33,6 @@ POSSIBILITY OF SUCH DAMAGE.
#ifndef TORRENT_ERROR_CODE_HPP_INCLUDED #ifndef TORRENT_ERROR_CODE_HPP_INCLUDED
#define TORRENT_ERROR_CODE_HPP_INCLUDED #define TORRENT_ERROR_CODE_HPP_INCLUDED
#include <boost/version.hpp>
#include "libtorrent/config.hpp" #include "libtorrent/config.hpp"
#include "libtorrent/string_util.hpp" // for allocate_string_copy #include "libtorrent/string_util.hpp" // for allocate_string_copy
@ -49,10 +48,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/aux_/disable_warnings_pop.hpp" #include "libtorrent/aux_/disable_warnings_pop.hpp"
#ifndef BOOST_SYSTEM_NOEXCEPT
#define BOOST_SYSTEM_NOEXCEPT TORRENT_EXCEPTION_THROW_SPECIFIER
#endif
namespace libtorrent namespace libtorrent
{ {
@ -572,4 +567,3 @@ namespace boost { namespace system {
} } } }
#endif #endif

View File

@ -163,6 +163,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/session_handle.hpp" #include "libtorrent/session_handle.hpp"
#include "libtorrent/peer_connection_handle.hpp" #include "libtorrent/peer_connection_handle.hpp"
#include "libtorrent/span.hpp" #include "libtorrent/span.hpp"
#include "libtorrent/string_view.hpp"
namespace libtorrent namespace libtorrent
{ {
@ -230,7 +231,7 @@ namespace libtorrent
// called when a dht request is received. // called when a dht request is received.
// If your plugin expects this to be called, make sure to include the flag // If your plugin expects this to be called, make sure to include the flag
// ``dht_request_feature`` in the return value from implemented_features(). // ``dht_request_feature`` in the return value from implemented_features().
virtual bool on_dht_request(char const* /* query */, int const /* query_len */ virtual bool on_dht_request(string_view /* query */
, udp::endpoint const& /* source */, bdecode_node const& /* message */ , udp::endpoint const& /* source */, bdecode_node const& /* message */
, entry& /* response */) , entry& /* response */)
{ return false; } { return false; }

View File

@ -75,7 +75,7 @@ namespace libtorrent { namespace dht
virtual void outgoing_get_peers(sha1_hash const& target virtual void outgoing_get_peers(sha1_hash const& target
, sha1_hash const& sent_target, udp::endpoint const& ep) = 0; , sha1_hash const& sent_target, udp::endpoint const& ep) = 0;
virtual void announce(sha1_hash const& ih, address const& addr, int port) = 0; virtual void announce(sha1_hash const& ih, address const& addr, int port) = 0;
virtual bool on_dht_request(char const* query, int query_len virtual bool on_dht_request(string_view query
, dht::msg const& request, entry& response) = 0; , dht::msg const& request, entry& response) = 0;
protected: protected:
@ -84,4 +84,3 @@ namespace libtorrent { namespace dht
}} }}
#endif #endif

View File

@ -42,6 +42,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <libtorrent/sha1_hash.hpp> #include <libtorrent/sha1_hash.hpp>
#include <libtorrent/address.hpp> #include <libtorrent/address.hpp>
#include <libtorrent/span.hpp> #include <libtorrent/span.hpp>
#include <libtorrent/string_view.hpp>
namespace libtorrent namespace libtorrent
{ {
@ -140,7 +141,7 @@ namespace dht
// //
virtual void announce_peer(sha1_hash const& info_hash virtual void announce_peer(sha1_hash const& info_hash
, tcp::endpoint const& endp , tcp::endpoint const& endp
, std::string const& name, bool seed) = 0; , string_view name, bool seed) = 0;
// This function retrieves the immutable item given its target hash. // This function retrieves the immutable item given its target hash.
// //

View File

@ -152,7 +152,7 @@ private:
int m_packet_size = 0; int m_packet_size = 0;
// keep track of how much of the receive buffer we use, if we're not using // keep track of how much of the receive buffer we use, if we're not using
// enuogh of it we shrink it // enough of it we shrink it
sliding_average<20> m_watermark; sliding_average<20> m_watermark;
buffer m_recv_buffer; buffer m_recv_buffer;

View File

@ -52,4 +52,3 @@ using string_view = boost::string_view;
#include "libtorrent/aux_/disable_warnings_pop.hpp" #include "libtorrent/aux_/disable_warnings_pop.hpp"
#endif #endif

View File

@ -80,7 +80,7 @@ struct obs : dht::dht_observer
void log_packet(message_direction_t /* dir */ void log_packet(message_direction_t /* dir */
, char const* /* pkt */, int /* len */ , char const* /* pkt */, int /* len */
, udp::endpoint /* node */) override {} , udp::endpoint /* node */) override {}
bool on_dht_request(char const* /* query */, int /* query_len */ bool on_dht_request(string_view /* query */
, dht::msg const& /* request */, entry& /* response */) override , dht::msg const& /* request */, entry& /* response */) override
{ return false; } { return false; }
}; };
@ -207,4 +207,3 @@ TORRENT_TEST(dht_rate_limit)
#endif // #if !defined TORRENT_DISABLE_EXTENSIONS && !defined TORRENT_DISABLE_DHT #endif // #if !defined TORRENT_DISABLE_EXTENSIONS && !defined TORRENT_DISABLE_DHT
} }

View File

@ -43,15 +43,11 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/entry.hpp" #include "libtorrent/entry.hpp"
#include "libtorrent/hex.hpp" #include "libtorrent/hex.hpp"
namespace
{
}
namespace libtorrent namespace libtorrent
{ {
namespace detail namespace detail
{ {
TORRENT_EXPORT char const* integer_to_str(char* buf, int size char const* integer_to_str(char* buf, int size
, entry::integer_type val) , entry::integer_type val)
{ {
int sign = 0; int sign = 0;

View File

@ -274,7 +274,7 @@ namespace
void announce_peer(sha1_hash const& info_hash void announce_peer(sha1_hash const& info_hash
, tcp::endpoint const& endp , tcp::endpoint const& endp
, std::string const& name, bool const seed) override , string_view name, bool const seed) override
{ {
table_t::iterator ti = m_map.find(info_hash); table_t::iterator ti = m_map.find(info_hash);
torrent_entry* v; torrent_entry* v;
@ -312,9 +312,7 @@ namespace
// for this torrent. Store it. // for this torrent. Store it.
if (!name.empty() && v->name.empty()) if (!name.empty() && v->name.empty())
{ {
std::string tname = name; v->name = name.substr(0, 100).to_string();
if (tname.size() > 100) tname.resize(100);
v->name = tname;
} }
peer_entry peer; peer_entry peer;

View File

@ -837,19 +837,18 @@ void node::incoming_request(msg const& m, entry& e)
// mirror back the other node's external port // mirror back the other node's external port
reply["p"] = m.addr.port(); reply["p"] = m.addr.port();
char const* query = top_level[0].string_ptr(); string_view query = top_level[0].string_value();
int query_len = top_level[0].string_length();
if (m_observer && m_observer->on_dht_request(query, query_len, m, e)) if (m_observer && m_observer->on_dht_request(query, m, e))
return; return;
if (query_len == 4 && memcmp(query, "ping", 4) == 0) if (query == "ping")
{ {
m_counters.inc_stats_counter(counters::dht_ping_in); m_counters.inc_stats_counter(counters::dht_ping_in);
// we already have 't' and 'id' in the response // we already have 't' and 'id' in the response
// no more left to add // no more left to add
} }
else if (query_len == 9 && memcmp(query, "get_peers", 9) == 0) else if (query == "get_peers")
{ {
key_desc_t msg_desc[] = { key_desc_t msg_desc[] = {
{"info_hash", bdecode_node::string_t, 20, 0}, {"info_hash", bdecode_node::string_t, 20, 0},
@ -888,7 +887,7 @@ void node::incoming_request(msg const& m, entry& e)
} }
#endif #endif
} }
else if (query_len == 9 && memcmp(query, "find_node", 9) == 0) else if (query == "find_node")
{ {
key_desc_t msg_desc[] = { key_desc_t msg_desc[] = {
{"target", bdecode_node::string_t, 20, 0}, {"target", bdecode_node::string_t, 20, 0},
@ -907,7 +906,7 @@ void node::incoming_request(msg const& m, entry& e)
write_nodes_entries(target, msg_keys[1], reply); write_nodes_entries(target, msg_keys[1], reply);
} }
else if (query_len == 13 && memcmp(query, "announce_peer", 13) == 0) else if (query == "announce_peer")
{ {
key_desc_t msg_desc[] = { key_desc_t msg_desc[] = {
{"info_hash", bdecode_node::string_t, 20, 0}, {"info_hash", bdecode_node::string_t, 20, 0},
@ -964,10 +963,9 @@ void node::incoming_request(msg const& m, entry& e)
string_view name = msg_keys[3] ? msg_keys[3].string_value() : string_view(); string_view name = msg_keys[3] ? msg_keys[3].string_value() : string_view();
bool seed = msg_keys[4] && msg_keys[4].int_value(); bool seed = msg_keys[4] && msg_keys[4].int_value();
// TODO: 3 should we update the dht storage API to take a string_ref? m_storage.announce_peer(info_hash, addr, name, seed);
m_storage.announce_peer(info_hash, addr, name.to_string(), seed);
} }
else if (query_len == 3 && memcmp(query, "put", 3) == 0) else if (query == "put")
{ {
// the first 2 entries are for both mutable and // the first 2 entries are for both mutable and
// immutable puts // immutable puts
@ -1104,7 +1102,7 @@ void node::incoming_request(msg const& m, entry& e)
m_table.node_seen(id, m.addr, 0xffff); m_table.node_seen(id, m.addr, 0xffff);
} }
else if (query_len == 3 && memcmp(query, "get", 3) == 0) else if (query == "get")
{ {
key_desc_t msg_desc[] = { key_desc_t msg_desc[] = {
{"seq", bdecode_node::int_t, 0, key_desc_t::optional}, {"seq", bdecode_node::int_t, 0, key_desc_t::optional},

View File

@ -532,10 +532,9 @@ namespace libtorrent
peer_plugin const* peer_connection::find_plugin(char const* type) peer_plugin const* peer_connection::find_plugin(char const* type)
{ {
TORRENT_ASSERT(is_single_thread()); TORRENT_ASSERT(is_single_thread());
for (extension_list_t::iterator i = m_extensions.begin() for (auto p : m_extensions)
, end(m_extensions.end()); i != end; ++i)
{ {
if (strcmp((*i)->type(), type) == 0) return (*i).get(); if (std::strcmp(p->type(), type) == 0) return p.get();
} }
return nullptr; return nullptr;
} }

View File

@ -6617,19 +6617,18 @@ namespace aux {
} }
#endif #endif
bool session_impl::on_dht_request(char const* query, int query_len bool session_impl::on_dht_request(string_view query
, dht::msg const& request, entry& response) , dht::msg const& request, entry& response)
{ {
#ifndef TORRENT_DISABLE_EXTENSIONS #ifndef TORRENT_DISABLE_EXTENSIONS
for (auto& ext : m_ses_extensions[plugins_dht_request_idx]) for (auto& ext : m_ses_extensions[plugins_dht_request_idx])
{ {
if (ext->on_dht_request(query, query_len if (ext->on_dht_request(query
, request.addr, request.message, response)) , request.addr, request.message, response))
return true; return true;
} }
#else #else
TORRENT_UNUSED(query); TORRENT_UNUSED(query);
TORRENT_UNUSED(query_len);
TORRENT_UNUSED(request); TORRENT_UNUSED(request);
TORRENT_UNUSED(response); TORRENT_UNUSED(response);
#endif #endif
@ -6949,4 +6948,3 @@ namespace aux {
} }
#endif // TORRENT_DISABLE_LOGGING #endif // TORRENT_DISABLE_LOGGING
}} }}

View File

@ -143,7 +143,7 @@ namespace libtorrent
// the random number // the random number
while (begin != end) while (begin != end)
*begin++ = printable[random(sizeof(printable)-2)]; *begin++ = printable[random(sizeof(printable) - 2)];
} }
char* allocate_string_copy(char const* str) char* allocate_string_copy(char const* str)
@ -353,11 +353,10 @@ namespace libtorrent
error_code ec; error_code ec;
std::tie(ignore, ignore, hostname, ignore, ignore) std::tie(ignore, ignore, hostname, ignore, ignore)
= parse_url_components(url, ec); = parse_url_components(url, ec);
char const* top_domain = strrchr(hostname.c_str(), '.'); char const* top_domain = std::strrchr(hostname.c_str(), '.');
return top_domain && strcmp(top_domain, ".i2p") == 0; return top_domain && std::strcmp(top_domain, ".i2p") == 0;
} }
#endif #endif
} }

View File

@ -499,7 +499,7 @@ struct obs : dht::dht_observer
void log_packet(message_direction_t dir, char const* pkt, int len void log_packet(message_direction_t dir, char const* pkt, int len
, udp::endpoint node) override {} , udp::endpoint node) override {}
#endif #endif
bool on_dht_request(char const* query, int query_len bool on_dht_request(string_view query
, dht::msg const& request, entry& response) override { return false; } , dht::msg const& request, entry& response) override { return false; }
address m_external_address = addr4("236.0.0.1"); address m_external_address = addr4("236.0.0.1");
@ -3143,4 +3143,3 @@ TORRENT_TEST(dht)
} }
#endif #endif

View File

@ -52,7 +52,7 @@ struct test_plugin : plugin
return plugin::dht_request_feature; return plugin::dht_request_feature;
} }
bool on_dht_request(char const* /* query */, int const /* query_len */ bool on_dht_request(string_view /* query */
, udp::endpoint const& /* source */, bdecode_node const& message , udp::endpoint const& /* source */, bdecode_node const& message
, entry& response) override , entry& response) override
{ {