From f51e7829053f3f74a70b79b2d4b45b3306023250 Mon Sep 17 00:00:00 2001 From: Alden Torres Date: Mon, 15 Aug 2016 16:17:13 -0400 Subject: [PATCH] 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 --- ChangeLog | 1 + include/libtorrent/aux_/session_impl.hpp | 4 +--- include/libtorrent/bencode.hpp | 1 - include/libtorrent/bt_peer_connection.hpp | 1 - include/libtorrent/error_code.hpp | 6 ------ include/libtorrent/extensions.hpp | 3 ++- include/libtorrent/kademlia/dht_observer.hpp | 3 +-- include/libtorrent/kademlia/dht_storage.hpp | 3 ++- include/libtorrent/receive_buffer.hpp | 2 +- include/libtorrent/string_view.hpp | 1 - simulation/test_dht_rate_limit.cpp | 3 +-- src/entry.cpp | 6 +----- src/kademlia/dht_storage.cpp | 6 ++---- src/kademlia/node.cpp | 20 +++++++++----------- src/peer_connection.cpp | 5 ++--- src/session_impl.cpp | 6 ++---- src/string_util.cpp | 7 +++---- test/test_dht.cpp | 3 +-- test/test_direct_dht.cpp | 2 +- 19 files changed, 30 insertions(+), 53 deletions(-) diff --git a/ChangeLog b/ChangeLog index fdffcbb5b..4cfa301c9 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,4 @@ + * plugin API changed to handle DHT requests using string_view * removed support for lt_trackers and metadata_transfer extensions (pre-dating ut_metadata) * support windows' CryptoAPI for SHA-1 diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index a281efb80..48f6b42d3 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -625,7 +625,7 @@ namespace libtorrent , udp::endpoint node) override; #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; void set_external_address(address const& ip @@ -1247,6 +1247,4 @@ namespace libtorrent } } - #endif - diff --git a/include/libtorrent/bencode.hpp b/include/libtorrent/bencode.hpp index 0326708ee..6a046d67c 100644 --- a/include/libtorrent/bencode.hpp +++ b/include/libtorrent/bencode.hpp @@ -72,7 +72,6 @@ POSSIBILITY OF SUCH DAMAGE. // structures it's parsing. #include -#include #include // for distance #include "libtorrent/config.hpp" diff --git a/include/libtorrent/bt_peer_connection.hpp b/include/libtorrent/bt_peer_connection.hpp index 4bb53f348..46a604b46 100644 --- a/include/libtorrent/bt_peer_connection.hpp +++ b/include/libtorrent/bt_peer_connection.hpp @@ -46,7 +46,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/aux_/disable_warnings_push.hpp" #include -#include #include #include "libtorrent/aux_/disable_warnings_pop.hpp" diff --git a/include/libtorrent/error_code.hpp b/include/libtorrent/error_code.hpp index 72ec0c0fd..5c4e2195e 100644 --- a/include/libtorrent/error_code.hpp +++ b/include/libtorrent/error_code.hpp @@ -33,7 +33,6 @@ POSSIBILITY OF SUCH DAMAGE. #ifndef TORRENT_ERROR_CODE_HPP_INCLUDED #define TORRENT_ERROR_CODE_HPP_INCLUDED -#include #include "libtorrent/config.hpp" #include "libtorrent/string_util.hpp" // for allocate_string_copy @@ -49,10 +48,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/aux_/disable_warnings_pop.hpp" -#ifndef BOOST_SYSTEM_NOEXCEPT -#define BOOST_SYSTEM_NOEXCEPT TORRENT_EXCEPTION_THROW_SPECIFIER -#endif - namespace libtorrent { @@ -572,4 +567,3 @@ namespace boost { namespace system { } } #endif - diff --git a/include/libtorrent/extensions.hpp b/include/libtorrent/extensions.hpp index bbfc751bf..7d7cedd43 100644 --- a/include/libtorrent/extensions.hpp +++ b/include/libtorrent/extensions.hpp @@ -163,6 +163,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/session_handle.hpp" #include "libtorrent/peer_connection_handle.hpp" #include "libtorrent/span.hpp" +#include "libtorrent/string_view.hpp" namespace libtorrent { @@ -230,7 +231,7 @@ namespace libtorrent // called when a dht request is received. // If your plugin expects this to be called, make sure to include the flag // ``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 */ , entry& /* response */) { return false; } diff --git a/include/libtorrent/kademlia/dht_observer.hpp b/include/libtorrent/kademlia/dht_observer.hpp index 40d984151..eb5ccc43b 100644 --- a/include/libtorrent/kademlia/dht_observer.hpp +++ b/include/libtorrent/kademlia/dht_observer.hpp @@ -75,7 +75,7 @@ namespace libtorrent { namespace dht virtual void outgoing_get_peers(sha1_hash const& target , sha1_hash const& sent_target, udp::endpoint const& ep) = 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; protected: @@ -84,4 +84,3 @@ namespace libtorrent { namespace dht }} #endif - diff --git a/include/libtorrent/kademlia/dht_storage.hpp b/include/libtorrent/kademlia/dht_storage.hpp index decfd4cf2..1ff065b3e 100644 --- a/include/libtorrent/kademlia/dht_storage.hpp +++ b/include/libtorrent/kademlia/dht_storage.hpp @@ -42,6 +42,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include namespace libtorrent { @@ -140,7 +141,7 @@ namespace dht // virtual void announce_peer(sha1_hash const& info_hash , 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. // diff --git a/include/libtorrent/receive_buffer.hpp b/include/libtorrent/receive_buffer.hpp index 017d92277..1b8bcb922 100644 --- a/include/libtorrent/receive_buffer.hpp +++ b/include/libtorrent/receive_buffer.hpp @@ -152,7 +152,7 @@ private: int m_packet_size = 0; // 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; buffer m_recv_buffer; diff --git a/include/libtorrent/string_view.hpp b/include/libtorrent/string_view.hpp index 80b451b5c..fb9064595 100644 --- a/include/libtorrent/string_view.hpp +++ b/include/libtorrent/string_view.hpp @@ -52,4 +52,3 @@ using string_view = boost::string_view; #include "libtorrent/aux_/disable_warnings_pop.hpp" #endif - diff --git a/simulation/test_dht_rate_limit.cpp b/simulation/test_dht_rate_limit.cpp index c61dcfb9c..d9e97ef55 100644 --- a/simulation/test_dht_rate_limit.cpp +++ b/simulation/test_dht_rate_limit.cpp @@ -80,7 +80,7 @@ struct obs : dht::dht_observer void log_packet(message_direction_t /* dir */ , char const* /* pkt */, int /* len */ , 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 { return false; } }; @@ -207,4 +207,3 @@ TORRENT_TEST(dht_rate_limit) #endif // #if !defined TORRENT_DISABLE_EXTENSIONS && !defined TORRENT_DISABLE_DHT } - diff --git a/src/entry.cpp b/src/entry.cpp index ee818caf4..7964248ea 100644 --- a/src/entry.cpp +++ b/src/entry.cpp @@ -43,15 +43,11 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/entry.hpp" #include "libtorrent/hex.hpp" -namespace -{ -} - namespace libtorrent { 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) { int sign = 0; diff --git a/src/kademlia/dht_storage.cpp b/src/kademlia/dht_storage.cpp index 4cd7e3af7..a4e89ac75 100644 --- a/src/kademlia/dht_storage.cpp +++ b/src/kademlia/dht_storage.cpp @@ -274,7 +274,7 @@ namespace void announce_peer(sha1_hash const& info_hash , 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); torrent_entry* v; @@ -312,9 +312,7 @@ namespace // for this torrent. Store it. if (!name.empty() && v->name.empty()) { - std::string tname = name; - if (tname.size() > 100) tname.resize(100); - v->name = tname; + v->name = name.substr(0, 100).to_string(); } peer_entry peer; diff --git a/src/kademlia/node.cpp b/src/kademlia/node.cpp index 293757380..22e69d24d 100644 --- a/src/kademlia/node.cpp +++ b/src/kademlia/node.cpp @@ -837,19 +837,18 @@ void node::incoming_request(msg const& m, entry& e) // mirror back the other node's external port reply["p"] = m.addr.port(); - char const* query = top_level[0].string_ptr(); - int query_len = top_level[0].string_length(); + string_view query = top_level[0].string_value(); - 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; - if (query_len == 4 && memcmp(query, "ping", 4) == 0) + if (query == "ping") { m_counters.inc_stats_counter(counters::dht_ping_in); // we already have 't' and 'id' in the response // 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[] = { {"info_hash", bdecode_node::string_t, 20, 0}, @@ -888,7 +887,7 @@ void node::incoming_request(msg const& m, entry& e) } #endif } - else if (query_len == 9 && memcmp(query, "find_node", 9) == 0) + else if (query == "find_node") { key_desc_t msg_desc[] = { {"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); } - else if (query_len == 13 && memcmp(query, "announce_peer", 13) == 0) + else if (query == "announce_peer") { key_desc_t msg_desc[] = { {"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(); 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.to_string(), seed); + m_storage.announce_peer(info_hash, addr, name, seed); } - else if (query_len == 3 && memcmp(query, "put", 3) == 0) + else if (query == "put") { // the first 2 entries are for both mutable and // immutable puts @@ -1104,7 +1102,7 @@ void node::incoming_request(msg const& m, entry& e) 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[] = { {"seq", bdecode_node::int_t, 0, key_desc_t::optional}, diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 34ad7253b..60a2652a1 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -532,10 +532,9 @@ namespace libtorrent peer_plugin const* peer_connection::find_plugin(char const* type) { TORRENT_ASSERT(is_single_thread()); - for (extension_list_t::iterator i = m_extensions.begin() - , end(m_extensions.end()); i != end; ++i) + for (auto p : m_extensions) { - if (strcmp((*i)->type(), type) == 0) return (*i).get(); + if (std::strcmp(p->type(), type) == 0) return p.get(); } return nullptr; } diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 52a4a63a2..79bc38c3f 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -6617,19 +6617,18 @@ namespace aux { } #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) { #ifndef TORRENT_DISABLE_EXTENSIONS 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)) return true; } #else TORRENT_UNUSED(query); - TORRENT_UNUSED(query_len); TORRENT_UNUSED(request); TORRENT_UNUSED(response); #endif @@ -6949,4 +6948,3 @@ namespace aux { } #endif // TORRENT_DISABLE_LOGGING }} - diff --git a/src/string_util.cpp b/src/string_util.cpp index 45457dcfa..1beee3c6e 100644 --- a/src/string_util.cpp +++ b/src/string_util.cpp @@ -143,7 +143,7 @@ namespace libtorrent // the random number while (begin != end) - *begin++ = printable[random(sizeof(printable)-2)]; + *begin++ = printable[random(sizeof(printable) - 2)]; } char* allocate_string_copy(char const* str) @@ -353,11 +353,10 @@ namespace libtorrent error_code ec; std::tie(ignore, ignore, hostname, ignore, ignore) = parse_url_components(url, ec); - char const* top_domain = strrchr(hostname.c_str(), '.'); - return top_domain && strcmp(top_domain, ".i2p") == 0; + char const* top_domain = std::strrchr(hostname.c_str(), '.'); + return top_domain && std::strcmp(top_domain, ".i2p") == 0; } #endif } - diff --git a/test/test_dht.cpp b/test/test_dht.cpp index b0defc509..b4e134523 100644 --- a/test/test_dht.cpp +++ b/test/test_dht.cpp @@ -499,7 +499,7 @@ struct obs : dht::dht_observer void log_packet(message_direction_t dir, char const* pkt, int len , udp::endpoint node) override {} #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; } address m_external_address = addr4("236.0.0.1"); @@ -3143,4 +3143,3 @@ TORRENT_TEST(dht) } #endif - diff --git a/test/test_direct_dht.cpp b/test/test_direct_dht.cpp index c8b927fdc..307f04108 100644 --- a/test/test_direct_dht.cpp +++ b/test/test_direct_dht.cpp @@ -52,7 +52,7 @@ struct test_plugin : plugin 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 , entry& response) override {