From 2e0140b6f6e32bc9615a2b44c66b48ede06e30c1 Mon Sep 17 00:00:00 2001 From: Steven Siloti Date: Sat, 24 Sep 2016 06:08:52 -0700 Subject: [PATCH 1/5] fix peer picking algorithm (#1141) fix peer picking algorithm. The old code was always picking the first to_pick peers from the set. --- src/kademlia/dht_storage.cpp | 14 +++++++++---- test/test_dht_storage.cpp | 38 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/kademlia/dht_storage.cpp b/src/kademlia/dht_storage.cpp index 57d2fd13c..f824752c7 100644 --- a/src/kademlia/dht_storage.cpp +++ b/src/kademlia/dht_storage.cpp @@ -241,22 +241,28 @@ namespace } else { - int num = (std::min)(int(v.peers.size()), m_settings.max_peers_reply); + int to_pick = m_settings.max_peers_reply; + int candidates = int(v.peers.size()); std::set::const_iterator iter = v.peers.begin(); entry::list_type& pe = peers["values"].list(); std::string endpoint; - for (int t = 0, m = 0; m < num && iter != v.peers.end(); ++iter, ++t) + for (; to_pick > 0 && iter != v.peers.end(); ++iter, --candidates) { - if ((random() / float(UINT_MAX + 1.f)) * (num - t) >= num - m) continue; if (noseed && iter->seed) continue; + + // pick this peer with probability + // / + if (random() % candidates > to_pick) + continue; + endpoint.resize(18); std::string::iterator out = endpoint.begin(); write_endpoint(iter->addr, out); endpoint.resize(out - endpoint.begin()); pe.push_back(entry(endpoint)); - ++m; + --to_pick; } } return true; diff --git a/test/test_dht_storage.cpp b/test/test_dht_storage.cpp index a79f4e71c..6daac1910 100644 --- a/test/test_dht_storage.cpp +++ b/test/test_dht_storage.cpp @@ -316,5 +316,43 @@ TORRENT_TEST(mutable_item_limit) TEST_EQUAL(cnt.mutable_data, 42); } +TORRENT_TEST(get_peers_dist) +{ + // test that get_peers returns reasonably disjoint sets of peers with each call + // take two samples of 100 peers from 1000 and make sure there aren't too many + // peers found in both lists + dht_settings sett = test_settings(); + sett.max_peers = 1000; + sett.max_peers_reply = 100; + boost::scoped_ptr s(dht_default_storage_constructor(node_id(0), sett)); + + address addr = rand_v4(); + for (int i = 0; i < 1000; ++i) + { + s->announce_peer(n1, tcp::endpoint(addr, uint16_t(i)) + , "torrent_name", false); + } + + std::set peer_set; + int duplicates = 0; + for (int i = 0; i < 2; ++i) + { + entry peers; + s->get_peers(n1, false, false, peers); + TEST_EQUAL(peers["values"].list().size(), 100); + entry::list_type const& peers_list = peers["values"].list(); + for (entry::list_type::const_iterator p = peers_list.begin(); + p != peers_list.end(); ++p) + { + std::string::const_iterator it = p->string().begin(); + int port = detail::read_v4_endpoint(it).port(); + if (!peer_set.insert(port).second) + ++duplicates; + } + } + std::printf("duplicate peers found: %d\n", duplicates); + TEST_CHECK(duplicates < 20); +} + #endif From ab846d2d311455c048f64b6c6f23bfad10e4142a Mon Sep 17 00:00:00 2001 From: minus Date: Wed, 28 Sep 2016 05:22:06 +0200 Subject: [PATCH 2/5] fixed python scrape_tracker arguments (#1160) --- bindings/python/src/torrent_handle.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindings/python/src/torrent_handle.cpp b/bindings/python/src/torrent_handle.cpp index 62e035631..bdb93d75a 100644 --- a/bindings/python/src/torrent_handle.cpp +++ b/bindings/python/src/torrent_handle.cpp @@ -460,7 +460,7 @@ void bind_torrent_handle() #ifndef TORRENT_DISABLE_DHT .def("force_dht_announce", _(&torrent_handle::force_dht_announce)) #endif - .def("scrape_tracker", _(&torrent_handle::scrape_tracker)) + .def("scrape_tracker", _(&torrent_handle::scrape_tracker), arg("index") = -1) .def("set_upload_mode", _(&torrent_handle::set_upload_mode)) .def("set_share_mode", _(&torrent_handle::set_share_mode)) .def("flush_cache", &torrent_handle::flush_cache) From 7a52a285a114c782e37e8f2a6323b114649ad639 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Mon, 3 Oct 2016 16:32:40 -0700 Subject: [PATCH 3/5] back-port error_code cleanup from master (#1173) back-port error_code cleanup from master --- .travis.yml | 2 +- Jamfile | 1 - bindings/python/src/error_code.cpp | 38 +++++++++--------- docs/manual.rst | 4 +- examples/client_test.cpp | 3 +- include/libtorrent/bdecode.hpp | 11 +++-- include/libtorrent/entry.hpp | 3 +- include/libtorrent/error_code.hpp | 19 ++++----- include/libtorrent/gzip.hpp | 7 +++- include/libtorrent/i2p_stream.hpp | 11 ++--- include/libtorrent/socks5_stream.hpp | 9 +++-- include/libtorrent/upnp.hpp | 10 +++-- simulation/test_tracker.cpp | 7 ++-- src/bdecode.cpp | 13 ++++-- src/close_reason.cpp | 4 +- src/create_torrent.cpp | 4 +- src/error_code.cpp | 14 +++++-- src/file.cpp | 2 +- src/gzip.cpp | 13 ++++-- src/http_connection.cpp | 2 +- src/http_seed_connection.cpp | 4 +- src/http_tracker_connection.cpp | 24 +++++------ src/i2p_stream.cpp | 13 ++++-- src/natpmp.cpp | 10 ++--- src/peer_connection.cpp | 60 ++++++++++++++-------------- src/session_impl.cpp | 9 ++--- src/socks5_stream.cpp | 17 +++++--- src/torrent.cpp | 29 ++++++-------- src/upnp.cpp | 15 ++++--- src/web_peer_connection.cpp | 2 +- test/test_primitives.cpp | 8 ++-- test/test_tracker.cpp | 8 ++-- 32 files changed, 207 insertions(+), 169 deletions(-) diff --git a/.travis.yml b/.travis.yml index 9e4a3164a..4637424dc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -73,7 +73,7 @@ install: # shipping with the system having marked all functions as deprecated. Since # we're building with -Werror, we can't have those warnings - 'echo "using darwin : cpp11 : ccache clang++ : -std=c11 -std=c++11 -Wno-deprecated-declarations ;" >> ~/user-config.jam' - - 'echo "using darwin : cpp98 : ccache clang++ : -std=c99 -std=c++98 -Wno-deprecated-declarations ;" >> ~/user-config.jam' + - 'echo "using darwin : cpp98 : ccache clang++ : off -std=c99 -std=c++98 -Wno-deprecated-declarations ;" >> ~/user-config.jam' - 'echo "using python : 2.7 ;" >> ~/user-config.jam' - ccache -V && ccache --show-stats && ccache --zero-stats - if [[ $docs == "1" && $TRAVIS_OS_NAME == "osx" ]]; then rst2html.py --version; fi diff --git a/Jamfile b/Jamfile index 490d80dcb..1869a29ff 100644 --- a/Jamfile +++ b/Jamfile @@ -274,7 +274,6 @@ rule warnings ( properties * ) { # disable warning C4503: decorated name length exceeded, name was truncated result += /wd4503 ; - result += all ; # enable these warnings again, once the other ones are dealt with diff --git a/bindings/python/src/error_code.cpp b/bindings/python/src/error_code.cpp index caea76ba9..c01022b97 100644 --- a/bindings/python/src/error_code.cpp +++ b/bindings/python/src/error_code.cpp @@ -60,30 +60,30 @@ void bind_error_code() .def("assign", &error_code::assign) ; - def("get_libtorrent_category", &get_libtorrent_category - , return_internal_reference<>()); - - def("get_upnp_category", &get_upnp_category - , return_internal_reference<>()); - - def("get_http_category", &get_http_category - , return_internal_reference<>()); - - def("get_socks_category", &get_socks_category - , return_internal_reference<>()); +typedef return_value_policy return_existing; + def("libtorrent_category", &libtorrent_category, return_existing()); + def("upnp_category", &upnp_category, return_existing()); + def("http_category", &http_category, return_existing()); + def("socks_category", &socks_category, return_existing()); + def("bdecode_category", &bdecode_category, return_existing()); #if TORRENT_USE_I2P - def("get_i2p_category", &get_i2p_category - , return_internal_reference<>()); + def("i2p_category", &i2p_category, return_existing()); #endif - def("get_bdecode_category", &get_bdecode_category - , return_internal_reference<>()); +#ifndef TORRENT_NO_DEPRECATE + def("get_libtorrent_category", &libtorrent_category, return_existing()); + def("get_upnp_category", &upnp_category, return_existing()); + def("get_http_category", &http_category, return_existing()); + def("get_socks_category", &socks_category, return_existing()); + def("get_bdecode_category", &bdecode_category, return_existing()); +#if TORRENT_USE_I2P + def("get_i2p_category", &i2p_category, return_existing()); +#endif +#endif // TORRENT_NO_DEPRECATE - def("generic_category", &boost::system::generic_category - , return_internal_reference<>()); + def("generic_category", &boost::system::generic_category, return_existing()); - def("system_category", &boost::system::system_category - , return_internal_reference<>()); + def("system_category", &boost::system::system_category, return_existing()); } diff --git a/docs/manual.rst b/docs/manual.rst index 13066d5a7..43000d7e4 100644 --- a/docs/manual.rst +++ b/docs/manual.rst @@ -119,7 +119,7 @@ for system errors. That is, errors that belong to the generic or system category Errors that belong to the libtorrent error category are not localized however, they are only available in english. In order to translate libtorrent errors, compare the -error category of the ``error_code`` object against ``libtorrent::get_libtorrent_category()``, +error category of the ``error_code`` object against ``libtorrent::libtorrent_category()``, and if matches, you know the error code refers to the list above. You can provide your own mapping from error code to string, which is localized. In this case, you cannot rely on ``error_code::message()`` to generate your strings. @@ -133,7 +133,7 @@ Here's a simple example of how to translate error codes: std::string error_code_to_string(boost::system::error_code const& ec) { - if (ec.category() != libtorrent::get_libtorrent_category()) + if (ec.category() != libtorrent::libtorrent_category()) { return ec.message(); } diff --git a/examples/client_test.cpp b/examples/client_test.cpp index 28b3d7e43..c3b462cb1 100644 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -1001,8 +1001,7 @@ bool handle_alert(libtorrent::session& ses, libtorrent::alert* a // handshake. The peers that we successfully connect to and then // disconnect is more interesting. if (pd->operation == op_connect - || pd->error == error_code(errors::timed_out_no_handshake - , get_libtorrent_category())) + || pd->error == errors::timed_out_no_handshake) return true; } diff --git a/include/libtorrent/bdecode.hpp b/include/libtorrent/bdecode.hpp index d95a59d5c..9d29dc1ca 100644 --- a/include/libtorrent/bdecode.hpp +++ b/include/libtorrent/bdecode.hpp @@ -98,12 +98,17 @@ example layout: namespace libtorrent { -TORRENT_EXPORT boost::system::error_category& get_bdecode_category(); +TORRENT_EXPORT boost::system::error_category& bdecode_category(); + +#ifndef TORRENT_NO_DEPRECATED +TORRENT_DEPRECATED TORRENT_EXPORT +boost::system::error_category& get_bdecode_category(); +#endif namespace bdecode_errors { // libtorrent uses boost.system's ``error_code`` class to represent - // errors. libtorrent has its own error category get_bdecode_category() + // errors. libtorrent has its own error category bdecode_category() // with the error codes defined by error_code_enum. enum error_code_enum { @@ -138,8 +143,6 @@ namespace boost { namespace system { template<> struct is_error_code_enum { static const bool value = true; }; - template<> struct is_error_condition_enum - { static const bool value = true; }; } } namespace libtorrent { diff --git a/include/libtorrent/entry.hpp b/include/libtorrent/entry.hpp index e365fd5e6..4e655d13d 100644 --- a/include/libtorrent/entry.hpp +++ b/include/libtorrent/entry.hpp @@ -323,8 +323,7 @@ namespace libtorrent // internal TORRENT_NO_RETURN inline void throw_type_error() { - throw libtorrent_exception(error_code(errors::invalid_entry_type - , get_libtorrent_category())); + throw libtorrent_exception(errors::invalid_entry_type); } #endif diff --git a/include/libtorrent/error_code.hpp b/include/libtorrent/error_code.hpp index f09935c2f..f166e203c 100644 --- a/include/libtorrent/error_code.hpp +++ b/include/libtorrent/error_code.hpp @@ -61,7 +61,7 @@ namespace libtorrent { // libtorrent uses boost.system's ``error_code`` class to represent // errors. libtorrent has its own error category - // get_libtorrent_category() with the error codes defined by + // libtorrent_category() with the error codes defined by // error_code_enum. enum error_code_enum { @@ -480,10 +480,10 @@ namespace libtorrent // return the instance of the libtorrent_error_category which // maps libtorrent error codes to human readable error messages. - TORRENT_EXPORT boost::system::error_category& get_libtorrent_category(); + TORRENT_EXPORT boost::system::error_category& libtorrent_category(); // returns the error_category for HTTP errors - TORRENT_EXPORT boost::system::error_category& get_http_category(); + TORRENT_EXPORT boost::system::error_category& http_category(); using boost::system::error_code; using boost::system::error_condition; @@ -492,6 +492,13 @@ namespace libtorrent using boost::system::generic_category; using boost::system::system_category; +#ifndef TORRENT_NO_DEPRECATE + TORRENT_DEPRECATED TORRENT_EXPORT + boost::system::error_category& get_libtorrent_category(); + TORRENT_DEPRECATED TORRENT_EXPORT + boost::system::error_category& get_http_category(); +#endif + #ifndef BOOST_NO_EXCEPTIONS struct TORRENT_EXPORT libtorrent_exception: std::exception { @@ -571,14 +578,8 @@ namespace boost { namespace system { template<> struct is_error_code_enum { static const bool value = true; }; - template<> struct is_error_condition_enum - { static const bool value = true; }; - template<> struct is_error_code_enum { static const bool value = true; }; - - template<> struct is_error_condition_enum - { static const bool value = true; }; } } #endif diff --git a/include/libtorrent/gzip.hpp b/include/libtorrent/gzip.hpp index 7df99549b..54f2679fc 100644 --- a/include/libtorrent/gzip.hpp +++ b/include/libtorrent/gzip.hpp @@ -48,7 +48,12 @@ namespace libtorrent , error_code& error); // get the ``error_category`` for zip errors - TORRENT_EXPORT boost::system::error_category& get_gzip_category(); + TORRENT_EXPORT boost::system::error_category& gzip_category(); + +#ifndef TORRENT_NO_DEPRECATE + TORRENT_DEPRECATED TORRENT_EXPORT + boost::system::error_category& get_gzip_category(); +#endif namespace gzip_errors { diff --git a/include/libtorrent/i2p_stream.hpp b/include/libtorrent/i2p_stream.hpp index 39c5e3747..a680dafab 100644 --- a/include/libtorrent/i2p_stream.hpp +++ b/include/libtorrent/i2p_stream.hpp @@ -77,7 +77,12 @@ namespace libtorrent { } // returns the error category for I2P errors - TORRENT_EXPORT boost::system::error_category& get_i2p_category(); + TORRENT_EXPORT boost::system::error_category& i2p_category(); + +#ifndef TORRENT_NO_DEPRECATE + TORRENT_DEPRECATED TORRENT_EXPORT + boost::system::error_category& get_i2p_category(); +#endif class i2p_stream : public proxy_base { @@ -232,10 +237,6 @@ template<> struct is_error_code_enum { static const bool value = true; }; -template<> -struct is_error_condition_enum -{ static const bool value = true; }; - } } #endif // TORRENT_USE_I2P diff --git a/include/libtorrent/socks5_stream.hpp b/include/libtorrent/socks5_stream.hpp index 5be4ef06d..c0dd810f7 100644 --- a/include/libtorrent/socks5_stream.hpp +++ b/include/libtorrent/socks5_stream.hpp @@ -76,7 +76,12 @@ namespace socks_error { } // namespace socks_error // returns the error_category for SOCKS5 errors -TORRENT_EXPORT boost::system::error_category& get_socks_category(); +TORRENT_EXPORT boost::system::error_category& socks_category(); + +#ifndef TORRENT_NO_DEPRECATE +TORRENT_DEPRECATED TORRENT_EXPORT +boost::system::error_category& get_socks_category(); +#endif class socks5_stream : public proxy_base { @@ -259,8 +264,6 @@ namespace boost { namespace system { template<> struct is_error_code_enum { static const bool value = true; }; - template<> struct is_error_condition_enum - { static const bool value = true; }; } } #endif diff --git a/include/libtorrent/upnp.hpp b/include/libtorrent/upnp.hpp index ec1614429..e4c7e73d0 100644 --- a/include/libtorrent/upnp.hpp +++ b/include/libtorrent/upnp.hpp @@ -93,7 +93,12 @@ namespace libtorrent } // the boost.system error category for UPnP errors - TORRENT_EXPORT boost::system::error_category& get_upnp_category(); + TORRENT_EXPORT boost::system::error_category& upnp_category(); + +#ifndef TORRENT_NO_DEPRECATED + TORRENT_DEPRECATED TORRENT_EXPORT + boost::system::error_category& get_upnp_category(); +#endif // int: port-mapping index // address: external address as queried from router @@ -204,7 +209,6 @@ private: void next(rootdevice& d, int i, mutex::scoped_lock& l); void update_map(rootdevice& d, int i, mutex::scoped_lock& l); - void on_upnp_xml(error_code const& e , libtorrent::http_parser const& p, rootdevice& d , http_connection& c); @@ -412,8 +416,6 @@ namespace boost { namespace system { template<> struct is_error_code_enum { static const bool value = true; }; - template<> struct is_error_condition_enum - { static const bool value = true; }; } } #endif diff --git a/simulation/test_tracker.cpp b/simulation/test_tracker.cpp index e5abcee0f..afe8ed364 100644 --- a/simulation/test_tracker.cpp +++ b/simulation/test_tracker.cpp @@ -474,8 +474,7 @@ TORRENT_TEST(test_error) TEST_EQUAL(ae.is_working(), false); TEST_EQUAL(ae.message, "test"); TEST_EQUAL(ae.url, "http://tracker.com:8080/announce"); - TEST_EQUAL(ae.last_error, error_code(errors::tracker_failure - , get_libtorrent_category())); + TEST_EQUAL(ae.last_error, error_code(errors::tracker_failure)); TEST_EQUAL(ae.fails, 1); }); } @@ -572,7 +571,7 @@ TORRENT_TEST(test_http_status) TEST_EQUAL(ae.is_working(), false); TEST_EQUAL(ae.message, "Not A Tracker"); TEST_EQUAL(ae.url, "http://tracker.com:8080/announce"); - TEST_EQUAL(ae.last_error, error_code(410, get_http_category())); + TEST_EQUAL(ae.last_error, error_code(410, http_category())); TEST_EQUAL(ae.fails, 1); }); } @@ -619,7 +618,7 @@ TORRENT_TEST(test_invalid_bencoding) TEST_EQUAL(ae.message, ""); TEST_EQUAL(ae.url, "http://tracker.com:8080/announce"); TEST_EQUAL(ae.last_error, error_code(bdecode_errors::expected_value - , get_bdecode_category())); + , bdecode_category())); TEST_EQUAL(ae.fails, 1); }); } diff --git a/src/bdecode.cpp b/src/bdecode.cpp index e5bee9b0e..fb366cb72 100644 --- a/src/bdecode.cpp +++ b/src/bdecode.cpp @@ -193,17 +193,22 @@ namespace libtorrent return msgs[ev]; } - boost::system::error_category& get_bdecode_category() + boost::system::error_category& bdecode_category() { static bdecode_error_category bdecode_category; return bdecode_category; } +#ifndef TORRENT_NO_DEPRECATED + boost::system::error_category& get_bdecode_category() + { return bdecode_category(); } +#endif + namespace bdecode_errors { boost::system::error_code make_error_code(error_code_enum e) { - return boost::system::error_code(e, get_bdecode_category()); + return boost::system::error_code(e, bdecode_category()); } } @@ -689,7 +694,7 @@ namespace libtorrent } #define TORRENT_FAIL_BDECODE(code) do { \ - ec = make_error_code(code); \ + ec = code; \ if (error_pos) *error_pos = start - orig_start; \ goto done; \ } TORRENT_WHILE_0 @@ -703,7 +708,7 @@ namespace libtorrent if (end - start > bdecode_token::max_offset) { if (error_pos) *error_pos = 0; - ec = make_error_code(bdecode_errors::limit_exceeded); + ec = bdecode_errors::limit_exceeded; return -1; } diff --git a/src/close_reason.cpp b/src/close_reason.cpp index bb4cbe595..974143010 100644 --- a/src/close_reason.cpp +++ b/src/close_reason.cpp @@ -47,7 +47,7 @@ namespace libtorrent close_reason_t error_to_close_reason(error_code const& ec) { - if (ec.category() == get_libtorrent_category()) + if (ec.category() == libtorrent_category()) { #define TORRENT_MAP(error, close_reason) \ case errors:: error : \ @@ -165,7 +165,7 @@ namespace libtorrent return close_no_memory; } } - else if (ec.category() == get_http_category()) + else if (ec.category() == http_category()) { return close_no_memory; } diff --git a/src/create_torrent.cpp b/src/create_torrent.cpp index dda3d4712..78d2b67f7 100644 --- a/src/create_torrent.cpp +++ b/src/create_torrent.cpp @@ -259,13 +259,13 @@ namespace libtorrent if (t.files().num_files() == 0) { - ec = error_code(errors::no_files_in_torrent, get_libtorrent_category()); + ec = errors::no_files_in_torrent; return; } if (t.files().total_size() == 0) { - ec = error_code(errors::torrent_invalid_length, get_libtorrent_category()); + ec = errors::torrent_invalid_length; return; } diff --git a/src/error_code.cpp b/src/error_code.cpp index c6b017596..a41e4b047 100644 --- a/src/error_code.cpp +++ b/src/error_code.cpp @@ -273,12 +273,20 @@ namespace libtorrent return msgs[ev]; } - boost::system::error_category& get_libtorrent_category() + boost::system::error_category& libtorrent_category() { static libtorrent_error_category libtorrent_category; return libtorrent_category; } +#ifndef TORRENT_NO_DEPRECATE + boost::system::error_category& get_libtorrent_category() + { return libtorrent_category(); } + + boost::system::error_category& get_http_category() + { return http_category(); } +#endif + struct TORRENT_EXPORT http_error_category : boost::system::error_category { virtual const char* name() const BOOST_SYSTEM_NOEXCEPT @@ -316,7 +324,7 @@ namespace libtorrent { return boost::system::error_condition(ev, *this); } }; - boost::system::error_category& get_http_category() + boost::system::error_category& http_category() { static http_error_category http_category; return http_category; @@ -345,7 +353,7 @@ namespace libtorrent // hidden boost::system::error_code make_error_code(error_code_enum e) { - return boost::system::error_code(e, get_libtorrent_category()); + return boost::system::error_code(e, libtorrent_category()); } } diff --git a/src/file.cpp b/src/file.cpp index 6557de4b0..cba76c36b 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -537,7 +537,7 @@ namespace libtorrent // something failed. Does the filesystem not support hard links? // TODO: 3 find out what error code is reported when the filesystem // does not support hard links. - DWORD error = GetLastError(); + DWORD const error = GetLastError(); if (error != ERROR_NOT_SUPPORTED && error != ERROR_ACCESS_DENIED) { // it's possible CreateHardLink will copy the file internally too, diff --git a/src/gzip.cpp b/src/gzip.cpp index 637d5cfd5..b1c99e776 100644 --- a/src/gzip.cpp +++ b/src/gzip.cpp @@ -95,17 +95,22 @@ namespace libtorrent return msgs[ev]; } - boost::system::error_category& get_gzip_category() + boost::system::error_category& gzip_category() { - static gzip_error_category gzip_category; - return gzip_category; + static gzip_error_category category; + return category; } +#ifndef TORRENT_NO_DEPRECATE + boost::system::error_category& get_gzip_category() + { return gzip_category(); } +#endif + namespace gzip_errors { boost::system::error_code make_error_code(error_code_enum e) { - return boost::system::error_code(e, get_gzip_category()); + return boost::system::error_code(e, gzip_category()); } } diff --git a/src/http_connection.cpp b/src/http_connection.cpp index 8f2f9eb70..37b28aa38 100644 --- a/src/http_connection.cpp +++ b/src/http_connection.cpp @@ -306,7 +306,7 @@ void http_connection::start(std::string const& hostname, int port if (is_i2p && i2p_conn->proxy().type != settings_pack::i2p_proxy) { m_timer.get_io_service().post(boost::bind(&http_connection::callback - , me, error_code(errors::no_i2p_router, get_libtorrent_category()), static_cast(NULL), 0)); + , me, error_code(errors::no_i2p_router), static_cast(NULL), 0)); return; } #endif diff --git a/src/http_seed_connection.cpp b/src/http_seed_connection.cpp index ac84d7136..79c014bca 100644 --- a/src/http_seed_connection.cpp +++ b/src/http_seed_connection.cpp @@ -294,7 +294,7 @@ namespace libtorrent , error_msg); } received_bytes(0, bytes_transferred); - disconnect(error_code(m_parser.status_code(), get_http_category()), op_bittorrent, 1); + disconnect(error_code(m_parser.status_code(), http_category()), op_bittorrent, 1); return; } if (!m_parser.header_finished()) @@ -430,7 +430,7 @@ namespace libtorrent received_bytes(0, bytes_transferred); // temporarily unavailable, retry later t->retry_web_seed(this, retry_time); - disconnect(error_code(m_parser.status_code(), get_http_category()), op_bittorrent, 1); + disconnect(error_code(m_parser.status_code(), http_category()), op_bittorrent, 1); return; } diff --git a/src/http_tracker_connection.cpp b/src/http_tracker_connection.cpp index 4887462e8..6827ccf2c 100644 --- a/src/http_tracker_connection.cpp +++ b/src/http_tracker_connection.cpp @@ -87,7 +87,7 @@ namespace libtorrent std::size_t pos = url.find("announce"); if (pos == std::string::npos) { - tracker_connection::fail(error_code(errors::scrape_not_available)); + tracker_connection::fail(errors::scrape_not_available); return; } url.replace(pos, 8, "scrape"); @@ -166,7 +166,7 @@ namespace libtorrent { if (tracker_req().i2pconn->local_endpoint().empty()) { - fail(error_code(errors::no_i2p_endpoint), -1, "Waiting for i2p acceptor from SAM bridge", 5); + fail(errors::no_i2p_endpoint, -1, "Waiting for i2p acceptor from SAM bridge", 5); return; } else @@ -285,7 +285,7 @@ namespace libtorrent } #endif if (endpoints.empty()) - fail(error_code(errors::banned_by_ip_filter)); + fail(errors::banned_by_ip_filter); } void http_tracker_connection::on_connect(http_connection& c) @@ -316,7 +316,7 @@ namespace libtorrent if (parser.status_code() != 200) { - fail(error_code(parser.status_code(), get_http_category()) + fail(error_code(parser.status_code(), http_category()) , parser.status_code(), parser.message().c_str()); return; } @@ -385,7 +385,7 @@ namespace libtorrent // extract peer id (if any) if (info.type() != bdecode_node::dict_t) { - ec.assign(errors::invalid_peer_dict, get_libtorrent_category()); + ec = errors::invalid_peer_dict; return false; } bdecode_node i = info.dict_find_string("peer id"); @@ -403,7 +403,7 @@ namespace libtorrent i = info.dict_find_string("ip"); if (i == 0) { - ec.assign(errors::invalid_tracker_response, get_libtorrent_category()); + ec = errors::invalid_tracker_response; return false; } ret.hostname = i.string_value(); @@ -412,7 +412,7 @@ namespace libtorrent i = info.dict_find_int("port"); if (i == 0) { - ec.assign(errors::invalid_tracker_response, get_libtorrent_category()); + ec = errors::invalid_tracker_response; return false; } ret.port = boost::uint16_t(i.int_value()); @@ -432,7 +432,7 @@ namespace libtorrent if (res != 0 || e.type() != bdecode_node::dict_t) { - ec.assign(errors::invalid_tracker_response, get_libtorrent_category()); + ec = errors::invalid_tracker_response; return resp; } @@ -453,7 +453,7 @@ namespace libtorrent if (failure) { resp.failure_reason = failure.string_value(); - ec.assign(errors::tracker_failure, get_libtorrent_category()); + ec = errors::tracker_failure; return resp; } @@ -466,7 +466,7 @@ namespace libtorrent bdecode_node files = e.dict_find_dict("files"); if (!files) { - ec.assign(errors::invalid_files_entry, get_libtorrent_category()); + ec = errors::invalid_files_entry; return resp; } @@ -475,7 +475,7 @@ namespace libtorrent if (!scrape_data) { - ec.assign(errors::invalid_hash_entry, get_libtorrent_category()); + ec = errors::invalid_hash_entry; return resp; } @@ -580,7 +580,7 @@ namespace libtorrent if (peers_ent == 0 && ipv6_peers == 0 && tracker_req().event != tracker_request::stopped) { - ec.assign(errors::invalid_peers_entry, get_libtorrent_category()); + ec = errors::invalid_peers_entry; return resp; } */ diff --git a/src/i2p_stream.cpp b/src/i2p_stream.cpp index 09377c264..cb0e736a9 100644 --- a/src/i2p_stream.cpp +++ b/src/i2p_stream.cpp @@ -77,17 +77,22 @@ namespace libtorrent }; - boost::system::error_category& get_i2p_category() + boost::system::error_category& i2p_category() { static i2p_error_category i2p_category; return i2p_category; } +#ifndef TORRENT_NO_DEPRECATE + boost::system::error_category& get_i2p_category() + { return i2p_category(); } +#endif + namespace i2p_error { boost::system::error_code make_error_code(i2p_error_code e) { - return error_code(e, get_i2p_category()); + return error_code(e, i2p_category()); } } @@ -325,7 +330,7 @@ namespace libtorrent } error_code invalid_response(i2p_error::parse_failed - , get_i2p_category()); + , i2p_category()); // null-terminate the string and parse it m_buffer.push_back(0); @@ -414,7 +419,7 @@ namespace libtorrent } } - error_code ec(result, get_i2p_category()); + error_code ec(result, i2p_category()); switch (result) { case i2p_error::no_error: diff --git a/src/natpmp.cpp b/src/natpmp.cpp index e107fa985..2a795d3e9 100644 --- a/src/natpmp.cpp +++ b/src/natpmp.cpp @@ -554,7 +554,8 @@ void natpmp::on_reply(error_code const& e if (result != 0) { - int errors[] = + // TODO: 3 it would be nice to have a separate NAT-PMP error category + errors::error_code_enum errors[] = { errors::unsupported_protocol_version, errors::natpmp_not_authorized, @@ -562,14 +563,13 @@ void natpmp::on_reply(error_code const& e errors::no_resources, errors::unsupported_opcode, }; - int ev = errors::no_error; + errors::error_code_enum ev = errors::no_error; if (result >= 1 && result <= 5) ev = errors[result - 1]; m->expires = aux::time_now() + hours(2); int const proto = m->protocol; l.unlock(); - m_callback(index, address(), 0, proto - , error_code(ev, get_libtorrent_category())); + m_callback(index, address(), 0, proto, ev); l.lock(); } else if (m->action == mapping_t::action_add) @@ -577,7 +577,7 @@ void natpmp::on_reply(error_code const& e int const proto = m->protocol; l.unlock(); m_callback(index, m_external_ip, m->external_port, proto - , error_code(errors::no_error, get_libtorrent_category())); + , errors::no_error); l.lock(); } diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 229505f05..670b6fc97 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -2165,7 +2165,7 @@ namespace libtorrent if (t->share_mode()) return false; if (m_upload_only && t->is_upload_only() - && can_disconnect(error_code(errors::upload_upload_connection, get_libtorrent_category()))) + && can_disconnect(errors::upload_upload_connection)) { #ifndef TORRENT_DISABLE_LOGGING peer_log(peer_log_alert::info, "UPLOAD_ONLY", "the peer is upload-only and our torrent is also upload-only"); @@ -2178,7 +2178,7 @@ namespace libtorrent && !m_interesting && m_bitfield_received && t->are_files_checked() - && can_disconnect(error_code(errors::uninteresting_upload_peer, get_libtorrent_category()))) + && can_disconnect(errors::uninteresting_upload_peer)) { #ifndef TORRENT_DISABLE_LOGGING peer_log(peer_log_alert::info, "UPLOAD_ONLY", "the peer is upload-only and we're not interested in it"); @@ -2387,8 +2387,7 @@ namespace libtorrent // may be a legitimate number of requests to have in flight when // getting choked if (m_num_invalid_requests > 300 && !m_peer_choked - && can_disconnect(error_code(errors::too_many_requests_when_choked - , get_libtorrent_category()))) + && can_disconnect(errors::too_many_requests_when_choked)) { disconnect(errors::too_many_requests_when_choked, op_bittorrent, 2); return; @@ -2410,7 +2409,7 @@ namespace libtorrent // disconnect peers that downloads more than foo times an allowed // fast piece if (m_choked && fast_idx != -1 && m_accept_fast_piece_cnt[fast_idx] >= 3 * blocks_per_piece - && can_disconnect(error_code(errors::too_many_requests_when_choked, get_libtorrent_category()))) + && can_disconnect(errors::too_many_requests_when_choked)) { disconnect(errors::too_many_requests_when_choked, op_bittorrent, 2); return; @@ -2429,7 +2428,7 @@ namespace libtorrent // allow peers to send request up to 2 seconds after getting choked, // then disconnect them if (aux::time_now() - seconds(2) > m_last_choke - && can_disconnect(error_code(errors::too_many_requests_when_choked, get_libtorrent_category()))) + && can_disconnect(errors::too_many_requests_when_choked)) { disconnect(errors::too_many_requests_when_choked, op_bittorrent, 2); return; @@ -4101,8 +4100,7 @@ namespace libtorrent break; } - if (ec == error_code(boost::asio::error::eof - , boost::asio::error::get_misc_category()) + if (ec == boost::asio::error::eof && !in_handshake() && !is_connecting() && aux::time_now() - connected_time() < seconds(15)) @@ -4166,29 +4164,29 @@ namespace libtorrent m_counters.inc_stats_counter(counters::invalid_arg_peers); else if (ec == error::operation_aborted) m_counters.inc_stats_counter(counters::aborted_peers); - else if (ec == error_code(errors::upload_upload_connection) - || ec == error_code(errors::uninteresting_upload_peer) - || ec == error_code(errors::torrent_aborted) - || ec == error_code(errors::self_connection) - || ec == error_code(errors::torrent_paused)) + else if (ec == errors::upload_upload_connection + || ec == errors::uninteresting_upload_peer + || ec == errors::torrent_aborted + || ec == errors::self_connection + || ec == errors::torrent_paused) m_counters.inc_stats_counter(counters::uninteresting_peers); - if (ec == error_code(errors::timed_out) + if (ec == errors::timed_out || ec == error::timed_out) m_counters.inc_stats_counter(counters::transport_timeout_peers); - if (ec == error_code(errors::timed_out_inactivity) - || ec == error_code(errors::timed_out_no_request) - || ec == error_code(errors::timed_out_no_interest)) + if (ec == errors::timed_out_inactivity + || ec == errors::timed_out_no_request + || ec == errors::timed_out_no_interest) m_counters.inc_stats_counter(counters::timeout_peers); - if (ec == error_code(errors::no_memory)) + if (ec == errors::no_memory) m_counters.inc_stats_counter(counters::no_memory_peers); - if (ec == error_code(errors::too_many_connections)) + if (ec == errors::too_many_connections) m_counters.inc_stats_counter(counters::too_many_peers); - if (ec == error_code(errors::timed_out_no_handshake)) + if (ec == errors::timed_out_no_handshake) m_counters.inc_stats_counter(counters::connect_timeouts); if (error > 0) @@ -4258,7 +4256,7 @@ namespace libtorrent { if (ec) { - if ((error > 1 || ec.category() == get_socks_category()) + if ((error > 1 || ec.category() == socks_category()) && t->alerts().should_post()) { t->alerts().emplace_alert(handle, remote() @@ -4801,7 +4799,7 @@ namespace libtorrent #endif if (d > seconds(connect_timeout) - && can_disconnect(error_code(errors::timed_out, get_libtorrent_category()))) + && can_disconnect(errors::timed_out)) { #ifndef TORRENT_DISABLE_LOGGING peer_log(peer_log_alert::info, "CONNECT_FAILED", "waited %d seconds" @@ -4822,7 +4820,7 @@ namespace libtorrent // because of less work in second_tick(), and might let use remove ticking // entirely eventually if (may_timeout && d > seconds(timeout()) && !m_connecting && m_reading_bytes == 0 - && can_disconnect(error_code(errors::timed_out_inactivity, get_libtorrent_category()))) + && can_disconnect(errors::timed_out_inactivity)) { #ifndef TORRENT_DISABLE_LOGGING peer_log(peer_log_alert::info, "LAST_ACTIVITY", "%d seconds ago" @@ -4865,7 +4863,7 @@ namespace libtorrent && m_peer_interested && t && t->is_upload_only() && d > seconds(60) - && can_disconnect(error_code(errors::timed_out_no_request, get_libtorrent_category()))) + && can_disconnect(errors::timed_out_no_request)) { #ifndef TORRENT_DISABLE_LOGGING peer_log(peer_log_alert::info, "NO_REQUEST", "waited %d seconds" @@ -4895,7 +4893,7 @@ namespace libtorrent && d2 > time_limit && (m_ses.num_connections() >= m_settings.get_int(settings_pack::connections_limit) || (t && t->num_peers() >= t->max_connections())) - && can_disconnect(error_code(errors::timed_out_no_interest))) + && can_disconnect(errors::timed_out_no_interest)) { #ifndef TORRENT_DISABLE_LOGGING peer_log(peer_log_alert::info, "MUTUAL_NO_INTEREST", "t1: %d t2: %d" @@ -6693,12 +6691,12 @@ namespace libtorrent && !t->share_mode()) { bool const ok_to_disconnect = - can_disconnect(error_code(errors::upload_upload_connection)) - || can_disconnect(error_code(errors::uninteresting_upload_peer)) - || can_disconnect(error_code(errors::too_many_requests_when_choked)) - || can_disconnect(error_code(errors::timed_out_no_interest)) - || can_disconnect(error_code(errors::timed_out_no_request)) - || can_disconnect(error_code(errors::timed_out_inactivity)); + can_disconnect(errors::upload_upload_connection) + || can_disconnect(errors::uninteresting_upload_peer) + || can_disconnect(errors::too_many_requests_when_choked) + || can_disconnect(errors::timed_out_no_interest) + || can_disconnect(errors::timed_out_no_request) + || can_disconnect(errors::timed_out_inactivity); // make sure upload only peers are disconnected if (t->is_upload_only() diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 4117879c8..dc1474f67 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -2829,7 +2829,7 @@ retry: { m_alerts.emplace_alert(torrent_handle(), endp, peer_id() , op_bittorrent, s->type() - , error_code(errors::too_many_connections, get_libtorrent_category()) + , error_code(errors::too_many_connections) , close_no_reason); } #ifndef TORRENT_DISABLE_LOGGING @@ -3496,7 +3496,7 @@ retry: int(i->second->num_peers() * m_settings.get_int(settings_pack::peer_turnover) / 100), 1) , i->second->num_connect_candidates()); i->second->disconnect_peers(peers_to_disconnect - , error_code(errors::optimistic_disconnect, get_libtorrent_category())); + , error_code(errors::optimistic_disconnect)); } else { @@ -3518,7 +3518,7 @@ retry: * m_settings.get_int(settings_pack::peer_turnover) / 100), 1) , t->num_connect_candidates()); t->disconnect_peers(peers_to_disconnect - , error_code(errors::optimistic_disconnect, get_libtorrent_category())); + , error_code(errors::optimistic_disconnect)); } } } @@ -6638,8 +6638,7 @@ retry: int disconnect = (std::min)(to_disconnect, num - my_average); to_disconnect -= disconnect; - i->second->disconnect_peers(disconnect - , error_code(errors::too_many_connections, get_libtorrent_category())); + i->second->disconnect_peers(disconnect, errors::too_many_connections); } } } diff --git a/src/socks5_stream.cpp b/src/socks5_stream.cpp index 4ad498e29..afaf6a089 100644 --- a/src/socks5_stream.cpp +++ b/src/socks5_stream.cpp @@ -41,7 +41,7 @@ namespace libtorrent { boost::system::error_code make_error_code(socks_error_code e) { - return error_code(e, get_socks_category()); + return error_code(e, socks_category()); } } @@ -73,12 +73,17 @@ namespace libtorrent { return boost::system::error_condition(ev, *this); } }; - TORRENT_EXPORT boost::system::error_category& get_socks_category() + boost::system::error_category& socks_category() { - static socks_error_category socks_category; - return socks_category; + static socks_error_category cat; + return cat; } +#ifndef TORRENT_NO_DEPRECATE + boost::system::error_category& get_socks_category() + { return socks_category(); } +#endif + namespace { // parse out the endpoint from a SOCKS response @@ -413,7 +418,7 @@ namespace libtorrent } if (response != 0) { - error_code ec(socks_error::general_failure, get_socks_category()); + error_code ec(socks_error::general_failure); switch (response) { case 2: ec = boost::asio::error::no_permission; break; @@ -512,7 +517,7 @@ namespace libtorrent return; } - error_code ec(socks_error::general_failure, get_socks_category()); + error_code ec(socks_error::general_failure); switch (response) { case 91: ec = boost::asio::error::connection_refused; break; diff --git a/src/torrent.cpp b/src/torrent.cpp index 2ba72218b..5ae0e7f5e 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -394,7 +394,7 @@ namespace libtorrent if (parser.header_finished() && parser.status_code() != 200) { - set_error(error_code(parser.status_code(), get_http_category()), error_file_url); + set_error(error_code(parser.status_code(), http_category()), error_file_url); pause(); return; } @@ -470,7 +470,7 @@ namespace libtorrent // TODO: if the existing torrent doesn't have metadata, insert // the metadata we just downloaded into it. - set_error(error_code(errors::duplicate_torrent, get_libtorrent_category()), error_file_url); + set_error(errors::duplicate_torrent, error_file_url); abort(); return; } @@ -533,7 +533,7 @@ namespace libtorrent if (parser.status_code() != 200) { - set_error(error_code(parser.status_code(), get_http_category()), torrent_status::error_file_url); + set_error(error_code(parser.status_code(), http_category()), torrent_status::error_file_url); pause(); return; } @@ -582,7 +582,7 @@ namespace libtorrent // TODO: if the existing torrent doesn't have metadata, insert // the metadata we just downloaded into it. - set_error(error_code(errors::duplicate_torrent, get_libtorrent_category()), torrent_status::error_file_url); + set_error(errors::duplicate_torrent, torrent_status::error_file_url); abort(); return; } @@ -1869,7 +1869,7 @@ namespace libtorrent if (m_resume_data && m_resume_data->node.type() == bdecode_node::dict_t) { - int ev = 0; + errors::error_code_enum ev = errors::no_error; if (m_resume_data->node.dict_find_string_value("file-format") != "libtorrent resume file") { @@ -1885,16 +1885,15 @@ namespace libtorrent if (ev && m_ses.alerts().should_post()) { - error_code ec = error_code(ev, get_libtorrent_category()); m_ses.alerts().emplace_alert(get_handle() - , ec, "", static_cast(0)); + , error_code(ev), "", static_cast(0)); } if (ev) { #ifndef TORRENT_DISABLE_LOGGING debug_log("fastresume data rejected: %s" - , error_code(ev, get_libtorrent_category()).message().c_str()); + , error_code(ev).message().c_str()); #endif m_resume_data.reset(); } @@ -6043,7 +6042,7 @@ namespace libtorrent { if (alerts().should_post()) alerts().emplace_alert(get_handle() - , error_code(errors::not_an_ssl_torrent), ""); + , errors::not_an_ssl_torrent, ""); return; } @@ -6291,7 +6290,7 @@ namespace libtorrent if (m_ses.alerts().should_post()) { m_ses.alerts().emplace_alert(get_handle(), web->url - , error_code(libtorrent::errors::peer_banned, get_libtorrent_category())); + , libtorrent::errors::peer_banned); } // never try it again remove_web_seed(web); @@ -7708,8 +7707,7 @@ namespace libtorrent // we have an i2p torrent, but we're not connected to an i2p // SAM proxy. if (alerts().should_post()) - alerts().emplace_alert(error_code(errors::no_i2p_router - , get_libtorrent_category())); + alerts().emplace_alert(errors::no_i2p_router); return false; } @@ -7876,7 +7874,7 @@ namespace libtorrent if (alerts().should_post()) { alerts().emplace_alert(get_handle() - , error_code(errors::mismatching_info_hash, get_libtorrent_category())); + , errors::mismatching_info_hash); } return false; } @@ -8850,8 +8848,7 @@ namespace libtorrent { if (alerts().should_post()) alerts().emplace_alert(get_handle() - , index, error_code(errors::session_is_closing - , get_libtorrent_category())); + , index, errors::session_is_closing); return; } @@ -9280,7 +9277,7 @@ namespace libtorrent if (num_peers() > int(m_max_connections)) { disconnect_peers(num_peers() - m_max_connections - , error_code(errors::too_many_connections, get_libtorrent_category())); + , errors::too_many_connections); } if (state_update) diff --git a/src/upnp.cpp b/src/upnp.cpp index c2a80e759..85f462322 100644 --- a/src/upnp.cpp +++ b/src/upnp.cpp @@ -62,7 +62,7 @@ namespace upnp_errors { boost::system::error_code make_error_code(error_code_enum e) { - return error_code(e, get_upnp_category()); + return error_code(e, upnp_category()); } } // upnp_errors namespace @@ -1165,12 +1165,17 @@ struct upnp_error_category : boost::system::error_category } }; -boost::system::error_category& get_upnp_category() +boost::system::error_category& upnp_category() { static upnp_error_category cat; return cat; } +#ifndef TORRENT_NO_DEPRECATED + boost::system::error_category& get_upnp_category() + { return upnp_category(); } +#endif + void upnp::on_upnp_get_ip_address_response(error_code const& e , libtorrent::http_parser const& p, rootdevice& d , http_connection& c) @@ -1420,7 +1425,7 @@ void upnp::return_error(int mapping, int code, mutex::scoped_lock& l) } const int proto = m_mappings[mapping].protocol; l.unlock(); - m_callback(mapping, address(), 0, proto, error_code(code, get_upnp_category())); + m_callback(mapping, address(), 0, proto, error_code(code, upnp_category())); l.lock(); } @@ -1477,8 +1482,8 @@ void upnp::on_upnp_unmap_response(error_code const& e l.unlock(); m_callback(mapping, address(), 0, proto, p.status_code() != 200 - ? error_code(p.status_code(), get_http_category()) - : error_code(s.error_code, get_upnp_category())); + ? error_code(p.status_code(), http_category()) + : error_code(s.error_code, upnp_category())); l.lock(); d.mapping[mapping].protocol = none; diff --git a/src/web_peer_connection.cpp b/src/web_peer_connection.cpp index 234269c84..39afd81d3 100644 --- a/src/web_peer_connection.cpp +++ b/src/web_peer_connection.cpp @@ -563,7 +563,7 @@ void web_peer_connection::handle_error(int bytes_left) , error_msg); } received_bytes(0, bytes_left); - disconnect(error_code(m_parser.status_code(), get_http_category()), op_bittorrent, 1); + disconnect(error_code(m_parser.status_code(), http_category()), op_bittorrent, 1); return; } diff --git a/test/test_primitives.cpp b/test/test_primitives.cpp index 7a1f91c0f..e86f03785 100644 --- a/test/test_primitives.cpp +++ b/test/test_primitives.cpp @@ -90,8 +90,8 @@ TORRENT_TEST(primitives) TEST_CHECK(error_code(errors::http_parse_error).message() == "Invalid HTTP header"); TEST_CHECK(error_code(errors::error_code_max).message() == "Unknown error"); - TEST_CHECK(error_code(errors::unauthorized, get_http_category()).message() == "401 Unauthorized"); - TEST_CHECK(error_code(errors::service_unavailable, get_http_category()).message() == "503 Service Unavailable"); + TEST_CHECK(error_code(errors::unauthorized, http_category()).message() == "401 Unauthorized"); + TEST_CHECK(error_code(errors::service_unavailable, http_category()).message() == "503 Service Unavailable"); // test snprintf @@ -146,11 +146,11 @@ TORRENT_TEST(primitives) #if TORRENT_USE_IPV6 TEST_EQUAL(print_address(v6("2001:ff::1")), "2001:ff::1"); parse_endpoint("[ff::1]", ec); - TEST_EQUAL(ec, error_code(errors::invalid_port, get_libtorrent_category())); + TEST_EQUAL(ec, error_code(errors::invalid_port)); #endif parse_endpoint("[ff::1:5", ec); - TEST_EQUAL(ec, error_code(errors::expected_close_bracket_in_address, get_libtorrent_category())); + TEST_EQUAL(ec, error_code(errors::expected_close_bracket_in_address)); // test address_to_bytes TEST_EQUAL(address_to_bytes(address_v4::from_string("10.11.12.13")), "\x0a\x0b\x0c\x0d"); diff --git a/test/test_tracker.cpp b/test/test_tracker.cpp index fb4f7fdc5..0f25c4570 100644 --- a/test/test_tracker.cpp +++ b/test/test_tracker.cpp @@ -200,7 +200,7 @@ TORRENT_TEST(parse_failure_reason) tracker_response resp = parse_tracker_response(response, sizeof(response) - 1 , ec, false, sha1_hash()); - TEST_EQUAL(ec, error_code(errors::tracker_failure)); + TEST_EQUAL(ec, errors::tracker_failure); TEST_EQUAL(resp.peers.size(), 0); TEST_EQUAL(resp.failure_reason, "test message"); } @@ -295,21 +295,21 @@ TORRENT_TEST(extract_peer_not_a_dictionary) { // not a dictionary peer_entry result = extract_peer("2:ip11:example.com" - , error_code(errors::invalid_peer_dict, get_libtorrent_category()), false); + , errors::invalid_peer_dict, false); } TORRENT_TEST(extract_peer_missing_ip) { // missing IP peer_entry result = extract_peer("d7:peer id20:abababababababababab4:porti1337ee" - , error_code(errors::invalid_tracker_response, get_libtorrent_category()), false); + , errors::invalid_tracker_response, false); } TORRENT_TEST(extract_peer_missing_port) { // missing port peer_entry result = extract_peer("d7:peer id20:abababababababababab2:ip4:abcde" - , error_code(errors::invalid_tracker_response, get_libtorrent_category()), false); + , errors::invalid_tracker_response, false); } TORRENT_TEST(udp_tracker) From 15bf443bfdf9df41c06048e0bd952b6c443c76ea Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Tue, 4 Oct 2016 07:31:07 -0700 Subject: [PATCH 4/5] fix connection_tester to support generating fake data for arbitrary torrents (#1181) --- examples/connection_tester.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/connection_tester.cpp b/examples/connection_tester.cpp index b35a422d3..a820bac4e 100644 --- a/examples/connection_tester.cpp +++ b/examples/connection_tester.cpp @@ -840,7 +840,8 @@ void generate_data(char const* path, torrent_info const& ti) for (int j = 0; j < ti.piece_size(i); j += 0x4000) { generate_block(piece, i, j, 0x4000); - file::iovec_t b = { piece, 0x4000}; + int const left_in_piece = ti.piece_size(i) - j; + file::iovec_t const b = { piece, size_t(std::min(left_in_piece, 0x4000))}; storage_error error; st->writev(&b, 1, i, j, 0, error); if (error) From 5f7e2eb9286d3e092254e0ce64945314c8837521 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 8 Oct 2016 12:29:46 -0400 Subject: [PATCH 5/5] run undefined sanitier on travis/linux (#1134) run undefined sanitizer on travis/linux. back-port ed25519 unit test from master --- .travis.yml | 59 ++++--- Jamfile | 2 +- ed25519/src/fe.cpp | 136 ++++++++-------- ed25519/src/ge.cpp | 6 +- ed25519/src/sc.cpp | 234 ++++++++++++++------------- include/libtorrent/alert_manager.hpp | 3 +- include/libtorrent/bitfield.hpp | 2 + src/disk_io_thread.cpp | 18 +-- src/enum_net.cpp | 52 +++--- src/file.cpp | 2 +- src/torrent.cpp | 11 +- src/utp_stream.cpp | 17 +- test/Jamfile | 1 + test/Makefile.am | 3 +- test/main.cpp | 64 ++++---- test/test_ed25519.cpp | 228 ++++++++++++++++++++++++++ 16 files changed, 552 insertions(+), 286 deletions(-) create mode 100644 test/test_ed25519.cpp diff --git a/.travis.yml b/.travis.yml index 4637424dc..15488a51c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,14 +1,21 @@ language: cpp -os: - - linux - - osx +matrix: + include: + - env: variant=test_release lang=cpp11 sim=0 coverage=1 docs=1 + - env: variant=test_debug lang=ubsan sim=1 coverage=0 + - env: variant=test_debug lang=cpp98 sim=0 coverage=0 + - env: variant=test_barebones lang=cpp11 sim=0 coverage=0 -env: - - variant=test_release lang=cpp11 sim=0 coverage=1 docs=1 - - variant=test_debug lang=cpp11 sim=1 coverage=0 - - variant=test_debug lang=cpp98 sim=0 coverage=0 - - variant=test_barebones lang=cpp11 sim=0 coverage=0 + - env: variant=test_debug lang=cpp11 sim=0 coverage=0 target=osx-tests + os: osx + osx_image: xcode6.4 + - env: variant=test_release lang=cpp11 sim=0 coverage=0 docs=1 target=osx-tests + os: osx + osx_image: xcode6.4 + - env: variant=test_debug lang=cpp98 sim=0 coverage=0 target=osx-tests + os: osx + osx_image: xcode6.4 git: submodules: false @@ -36,7 +43,7 @@ addons: - libboost1.55-all-dev - libboost1.55-tools-dev - python2.7-dev - - g++-4.8 + - g++-5 before_install: - git submodule update --init --recursive @@ -52,8 +59,7 @@ before_install: fi' # disable simulations on OSX for now. It hangs on travis - - if [ $TRAVIS_OS_NAME == "osx" ]; then export toolset="darwin-${lang}"; export sim="0"; fi - - if [ $TRAVIS_OS_NAME == "osx" ]; then export target="osx-tests"; fi + - if [ $TRAVIS_OS_NAME == "osx" ]; then export toolset="darwin-${lang}"; fi - if [ $TRAVIS_OS_NAME == "linux" ]; then export toolset="gcc-${lang}"; fi - if [[ $TRAVIS_OS_NAME == "linux" && $coverage == "1" ]]; then export coverage_toolset=gcc-coverage; @@ -61,13 +67,19 @@ before_install: else export coverage_toolset=$toolset; fi - - 'echo "using toolset: " ${toolset}' + - 'if [[ $lang == "ubsan" ]]; then export test_args=testing.arg="--no-stderr-redirect"; fi' + - 'echo "toolset: " ${toolset}' + - 'echo "target: " ${target}' + - 'echo "coverage_toolset: " ${coverage_toolset}' + - 'echo "test_args: " ${test_args}' + - 'echo "variant: " ${variant}' install: - - g++-4.8 --version - - 'echo "using gcc : cpp11 : ccache g++-4.8 : -std=c11 -std=c++11 ;" > ~/user-config.jam' - - 'echo "using gcc : coverage : ccache g++-4.8 : -std=c11 -std=c++11 --coverage --coverage ;" >> ~/user-config.jam' - - 'echo "using gcc : cpp98 : ccache g++-4.8 : -std=c99 -std=c++98 ;" >> ~/user-config.jam' + - 'if [[ $toolset == "gcc" ]]; then g++-5 --version; fi' + - 'echo "using gcc : cpp11 : ccache g++-5 : -std=c11 -std=c++11 ;" > ~/user-config.jam' + - 'echo "using gcc : ubsan : ccache g++-5 : -std=c11 -std=c++11 -fsanitize=undefined -fno-sanitize-recover=undefined -fsanitize=undefined -fno-sanitize-recover=undefined ;" >> ~/user-config.jam' + - 'echo "using gcc : coverage : ccache g++-5 : -std=c11 -std=c++11 --coverage --coverage ;" >> ~/user-config.jam' + - 'echo "using gcc : cpp98 : ccache g++-5 : -std=c99 -std=c++98 ;" >> ~/user-config.jam' # osx builds need to disable the deprecated warning because of the openssl # shipping with the system having marked all functions as deprecated. Since @@ -85,7 +97,7 @@ script: - cd .. - cd test - - bjam --hash -j3 warnings-as-errors=on invariant-checks=full variant=$variant -l900 $coverage_toolset $target + - bjam --hash -j3 warnings-as-errors=on invariant-checks=full variant=$variant -l900 $coverage_toolset $target $test_args # if we're building with code coverage, report it as soon as possible - if [[ $TRAVIS_OS_NAME == "linux" && $coverage == "1" ]]; then codecov --root .. --gcov-exec gcov-4.8; @@ -103,15 +115,18 @@ script: # as the main library, so we can't stage them to the same directory - bjam --hash -j3 warnings-as-errors=on link=shared debug-iterators=on picker-debugging=on invariant-checks=full variant=$variant $coverage_toolset install location=./lib - - cd bindings/python # here we specify the temporary lib dir as a path to look for the main library - - bjam --hash -j3 warnings-as-errors=on debug-iterators=on picker-debugging=on invariant-checks=full variant=$variant $coverage_toolset stage_module libtorrent-link=shared install-type=LIB dll-path=../../lib - - LD_LIBRARY_PATH=../../lib DYLD_LIBRARY_PATH=../../lib python test.py - - cd ../.. +# a binary built with ubsan does not interact well with python + - 'if [[ $lang != "ubsan" ]]; then + cd bindings/python; + bjam --hash -j3 warnings-as-errors=on debug-iterators=on picker-debugging=on invariant-checks=full variant=$variant $coverage_toolset stage_module libtorrent-link=shared install-type=LIB dll-path=../../lib; + LD_LIBRARY_PATH=../../lib DYLD_LIBRARY_PATH=../../lib python test.py; + cd ../..; + fi' - cd simulation - if [ $sim = "1" ]; then - bjam --hash -j2 crypto=built-in warnings-as-errors=on $coverage_toolset; + bjam --hash -j2 crypto=built-in warnings-as-errors=on $coverage_toolset $test_args; fi - cd .. - ccache --show-stats diff --git a/Jamfile b/Jamfile index 1869a29ff..31a2023cc 100644 --- a/Jamfile +++ b/Jamfile @@ -504,7 +504,7 @@ feature export-extra : off on : composite propagated ; variant test_release : release : production on full shared off - on on multi + on on multi on ; variant test_debug : debug : openssl on on diff --git a/ed25519/src/fe.cpp b/ed25519/src/fe.cpp index 809f0c335..d8b1a7e2d 100644 --- a/ed25519/src/fe.cpp +++ b/ed25519/src/fe.cpp @@ -34,6 +34,9 @@ static u64 load_4(const unsigned char *in) { return result; } +static inline i64 shift_left(i64 v, int s) { + return i64(u64(v) << s); +} /* @@ -326,34 +329,34 @@ void fe_frombytes(fe h, const unsigned char *s) { carry9 = (h9 + (i64) (1 << 24)) >> 25; h0 += carry9 * 19; - h9 -= carry9 << 25; + h9 -= shift_left(carry9, 25); carry1 = (h1 + (i64) (1 << 24)) >> 25; h2 += carry1; - h1 -= carry1 << 25; + h1 -= shift_left(carry1, 25); carry3 = (h3 + (i64) (1 << 24)) >> 25; h4 += carry3; - h3 -= carry3 << 25; + h3 -= shift_left(carry3, 25); carry5 = (h5 + (i64) (1 << 24)) >> 25; h6 += carry5; - h5 -= carry5 << 25; + h5 -= shift_left(carry5, 25); carry7 = (h7 + (i64) (1 << 24)) >> 25; h8 += carry7; - h7 -= carry7 << 25; + h7 -= shift_left(carry7, 25); carry0 = (h0 + (i64) (1 << 25)) >> 26; h1 += carry0; - h0 -= carry0 << 26; + h0 -= shift_left(carry0, 26); carry2 = (h2 + (i64) (1 << 25)) >> 26; h3 += carry2; - h2 -= carry2 << 26; + h2 -= shift_left(carry2, 26); carry4 = (h4 + (i64) (1 << 25)) >> 26; h5 += carry4; - h4 -= carry4 << 26; + h4 -= shift_left(carry4, 26); carry6 = (h6 + (i64) (1 << 25)) >> 26; h7 += carry6; - h6 -= carry6 << 26; + h6 -= shift_left(carry6, 26); carry8 = (h8 + (i64) (1 << 25)) >> 26; h9 += carry8; - h8 -= carry8 << 26; + h8 -= shift_left(carry8, 26); h[0] = (i32) h0; h[1] = (i32) h1; @@ -528,7 +531,6 @@ int fe_isnonzero(const fe f) { } - /* h = f * g Can overlap h with f or g. @@ -719,46 +721,46 @@ void fe_mul(fe h, const fe f, const fe g) { carry0 = (h0 + (i64) (1 << 25)) >> 26; h1 += carry0; - h0 -= carry0 << 26; + h0 -= shift_left(carry0, 26); carry4 = (h4 + (i64) (1 << 25)) >> 26; h5 += carry4; - h4 -= carry4 << 26; + h4 -= shift_left(carry4, 26); carry1 = (h1 + (i64) (1 << 24)) >> 25; h2 += carry1; - h1 -= carry1 << 25; + h1 -= shift_left(carry1, 25); carry5 = (h5 + (i64) (1 << 24)) >> 25; h6 += carry5; - h5 -= carry5 << 25; + h5 -= shift_left(carry5, 25); carry2 = (h2 + (i64) (1 << 25)) >> 26; h3 += carry2; - h2 -= carry2 << 26; + h2 -= shift_left(carry2, 26); carry6 = (h6 + (i64) (1 << 25)) >> 26; h7 += carry6; - h6 -= carry6 << 26; + h6 -= shift_left(carry6, 26); carry3 = (h3 + (i64) (1 << 24)) >> 25; h4 += carry3; - h3 -= carry3 << 25; + h3 -= shift_left(carry3, 25); carry7 = (h7 + (i64) (1 << 24)) >> 25; h8 += carry7; - h7 -= carry7 << 25; + h7 -= shift_left(carry7, 25); carry4 = (h4 + (i64) (1 << 25)) >> 26; h5 += carry4; - h4 -= carry4 << 26; + h4 -= shift_left(carry4, 26); carry8 = (h8 + (i64) (1 << 25)) >> 26; h9 += carry8; - h8 -= carry8 << 26; + h8 -= shift_left(carry8, 26); carry9 = (h9 + (i64) (1 << 24)) >> 25; h0 += carry9 * 19; - h9 -= carry9 << 25; + h9 -= shift_left(carry9, 25); carry0 = (h0 + (i64) (1 << 25)) >> 26; h1 += carry0; - h0 -= carry0 << 26; + h0 -= shift_left(carry0, 26); h[0] = (i32) h0; h[1] = (i32) h1; @@ -816,17 +818,17 @@ void fe_mul121666(fe h, fe f) { i64 carry8; i64 carry9; - carry9 = (h9 + (i64) (1<<24)) >> 25; h0 += carry9 * 19; h9 -= carry9 << 25; - carry1 = (h1 + (i64) (1<<24)) >> 25; h2 += carry1; h1 -= carry1 << 25; - carry3 = (h3 + (i64) (1<<24)) >> 25; h4 += carry3; h3 -= carry3 << 25; - carry5 = (h5 + (i64) (1<<24)) >> 25; h6 += carry5; h5 -= carry5 << 25; - carry7 = (h7 + (i64) (1<<24)) >> 25; h8 += carry7; h7 -= carry7 << 25; + carry9 = (h9 + (i64) (1<<24)) >> 25; h0 += carry9 * 19; h9 -= shift_left(carry9, 25); + carry1 = (h1 + (i64) (1<<24)) >> 25; h2 += carry1; h1 -= shift_left(carry1, 25); + carry3 = (h3 + (i64) (1<<24)) >> 25; h4 += carry3; h3 -= shift_left(carry3, 25); + carry5 = (h5 + (i64) (1<<24)) >> 25; h6 += carry5; h5 -= shift_left(carry5, 25); + carry7 = (h7 + (i64) (1<<24)) >> 25; h8 += carry7; h7 -= shift_left(carry7, 25); - carry0 = (h0 + (i64) (1<<25)) >> 26; h1 += carry0; h0 -= carry0 << 26; - carry2 = (h2 + (i64) (1<<25)) >> 26; h3 += carry2; h2 -= carry2 << 26; - carry4 = (h4 + (i64) (1<<25)) >> 26; h5 += carry4; h4 -= carry4 << 26; - carry6 = (h6 + (i64) (1<<25)) >> 26; h7 += carry6; h6 -= carry6 << 26; - carry8 = (h8 + (i64) (1<<25)) >> 26; h9 += carry8; h8 -= carry8 << 26; + carry0 = (h0 + (i64) (1<<25)) >> 26; h1 += carry0; h0 -= shift_left(carry0, 26); + carry2 = (h2 + (i64) (1<<25)) >> 26; h3 += carry2; h2 -= shift_left(carry2, 26); + carry4 = (h4 + (i64) (1<<25)) >> 26; h5 += carry4; h4 -= shift_left(carry4, 26); + carry6 = (h6 + (i64) (1<<25)) >> 26; h7 += carry6; h6 -= shift_left(carry6, 26); + carry8 = (h8 + (i64) (1<<25)) >> 26; h9 += carry8; h8 -= shift_left(carry8, 26); h[0] = h0; h[1] = h1; @@ -1088,40 +1090,40 @@ void fe_sq(fe h, const fe f) { i64 carry9; carry0 = (h0 + (i64) (1 << 25)) >> 26; h1 += carry0; - h0 -= carry0 << 26; + h0 -= shift_left(carry0, 26); carry4 = (h4 + (i64) (1 << 25)) >> 26; h5 += carry4; - h4 -= carry4 << 26; + h4 -= shift_left(carry4, 26); carry1 = (h1 + (i64) (1 << 24)) >> 25; h2 += carry1; - h1 -= carry1 << 25; + h1 -= shift_left(carry1, 25); carry5 = (h5 + (i64) (1 << 24)) >> 25; h6 += carry5; - h5 -= carry5 << 25; + h5 -= shift_left(carry5, 25); carry2 = (h2 + (i64) (1 << 25)) >> 26; h3 += carry2; - h2 -= carry2 << 26; + h2 -= shift_left(carry2, 26); carry6 = (h6 + (i64) (1 << 25)) >> 26; h7 += carry6; - h6 -= carry6 << 26; + h6 -= shift_left(carry6, 26); carry3 = (h3 + (i64) (1 << 24)) >> 25; h4 += carry3; - h3 -= carry3 << 25; + h3 -= shift_left(carry3, 25); carry7 = (h7 + (i64) (1 << 24)) >> 25; h8 += carry7; - h7 -= carry7 << 25; + h7 -= shift_left(carry7, 25); carry4 = (h4 + (i64) (1 << 25)) >> 26; h5 += carry4; - h4 -= carry4 << 26; + h4 -= shift_left(carry4, 26); carry8 = (h8 + (i64) (1 << 25)) >> 26; h9 += carry8; - h8 -= carry8 << 26; + h8 -= shift_left(carry8, 26); carry9 = (h9 + (i64) (1 << 24)) >> 25; h0 += carry9 * 19; - h9 -= carry9 << 25; + h9 -= shift_left(carry9, 25); carry0 = (h0 + (i64) (1 << 25)) >> 26; h1 += carry0; - h0 -= carry0 << 26; + h0 -= shift_left(carry0, 26); h[0] = (i32) h0; h[1] = (i32) h1; h[2] = (i32) h2; @@ -1261,40 +1263,40 @@ void fe_sq2(fe h, const fe f) { h9 += h9; carry0 = (h0 + (i64) (1 << 25)) >> 26; h1 += carry0; - h0 -= carry0 << 26; + h0 -= shift_left(carry0, 26); carry4 = (h4 + (i64) (1 << 25)) >> 26; h5 += carry4; - h4 -= carry4 << 26; + h4 -= shift_left(carry4, 26); carry1 = (h1 + (i64) (1 << 24)) >> 25; h2 += carry1; - h1 -= carry1 << 25; + h1 -= shift_left(carry1, 25); carry5 = (h5 + (i64) (1 << 24)) >> 25; h6 += carry5; - h5 -= carry5 << 25; + h5 -= shift_left(carry5, 25); carry2 = (h2 + (i64) (1 << 25)) >> 26; h3 += carry2; - h2 -= carry2 << 26; + h2 -= shift_left(carry2, 26); carry6 = (h6 + (i64) (1 << 25)) >> 26; h7 += carry6; - h6 -= carry6 << 26; + h6 -= shift_left(carry6, 26); carry3 = (h3 + (i64) (1 << 24)) >> 25; h4 += carry3; - h3 -= carry3 << 25; + h3 -= shift_left(carry3, 25); carry7 = (h7 + (i64) (1 << 24)) >> 25; h8 += carry7; - h7 -= carry7 << 25; + h7 -= shift_left(carry7, 25); carry4 = (h4 + (i64) (1 << 25)) >> 26; h5 += carry4; - h4 -= carry4 << 26; + h4 -= shift_left(carry4, 26); carry8 = (h8 + (i64) (1 << 25)) >> 26; h9 += carry8; - h8 -= carry8 << 26; + h8 -= shift_left(carry8, 26); carry9 = (h9 + (i64) (1 << 24)) >> 25; h0 += carry9 * 19; - h9 -= carry9 << 25; + h9 -= shift_left(carry9, 25); carry0 = (h0 + (i64) (1 << 25)) >> 26; h1 += carry0; - h0 -= carry0 << 26; + h0 -= shift_left(carry0, 26); h[0] = (i32) h0; h[1] = (i32) h1; h[2] = (i32) h2; @@ -1429,33 +1431,33 @@ void fe_tobytes(unsigned char *s, const fe h) { /* Goal: Output h-2^255 q, which is between 0 and 2^255-20. */ carry0 = h0 >> 26; h1 += carry0; - h0 -= carry0 << 26; + h0 -= shift_left(carry0, 26); carry1 = h1 >> 25; h2 += carry1; - h1 -= carry1 << 25; + h1 -= shift_left(carry1, 25); carry2 = h2 >> 26; h3 += carry2; - h2 -= carry2 << 26; + h2 -= shift_left(carry2, 26); carry3 = h3 >> 25; h4 += carry3; - h3 -= carry3 << 25; + h3 -= shift_left(carry3, 25); carry4 = h4 >> 26; h5 += carry4; - h4 -= carry4 << 26; + h4 -= shift_left(carry4, 26); carry5 = h5 >> 25; h6 += carry5; - h5 -= carry5 << 25; + h5 -= shift_left(carry5, 25); carry6 = h6 >> 26; h7 += carry6; - h6 -= carry6 << 26; + h6 -= shift_left(carry6, 26); carry7 = h7 >> 25; h8 += carry7; - h7 -= carry7 << 25; + h7 -= shift_left(carry7, 25); carry8 = h8 >> 26; h9 += carry8; - h8 -= carry8 << 26; + h8 -= shift_left(carry8, 26); carry9 = h9 >> 25; - h9 -= carry9 << 25; + h9 -= shift_left(carry9, 25); /* h10 = carry9 */ /* diff --git a/ed25519/src/ge.cpp b/ed25519/src/ge.cpp index c6fc00472..8bcb12fdc 100644 --- a/ed25519/src/ge.cpp +++ b/ed25519/src/ge.cpp @@ -357,9 +357,11 @@ static void cmov(ge_precomp *t, ge_precomp *u, unsigned char b) { static void select(ge_precomp *t, int pos, signed char b) { + typedef signed char schar; + typedef unsigned char uchar; ge_precomp minust; - unsigned char bnegative = negative(b); - unsigned char babs = b - (((-bnegative) & b) << 1); + unsigned char const bnegative = negative(b); + unsigned char const babs = b - schar(uchar((-bnegative) & b) << 1); fe_1(t->yplusx); fe_1(t->yminusx); fe_0(t->xy2d); diff --git a/ed25519/src/sc.cpp b/ed25519/src/sc.cpp index a6656c4d5..18b8b4771 100644 --- a/ed25519/src/sc.cpp +++ b/ed25519/src/sc.cpp @@ -25,6 +25,10 @@ static u64 load_4(const unsigned char *in) { return result; } +static inline i64 shift_left(i64 v, int s) { + return i64(u64(v) << s); +} + /* Input: s[0]+256*s[1]+...+256^63*s[63] = s @@ -122,37 +126,37 @@ void sc_reduce(unsigned char *s) { s18 = 0; carry6 = (s6 + (1 << 20)) >> 21; s7 += carry6; - s6 -= carry6 << 21; + s6 -= shift_left(carry6, 21); carry8 = (s8 + (1 << 20)) >> 21; s9 += carry8; - s8 -= carry8 << 21; + s8 -= shift_left(carry8, 21); carry10 = (s10 + (1 << 20)) >> 21; s11 += carry10; - s10 -= carry10 << 21; + s10 -= shift_left(carry10, 21); carry12 = (s12 + (1 << 20)) >> 21; s13 += carry12; - s12 -= carry12 << 21; + s12 -= shift_left(carry12, 21); carry14 = (s14 + (1 << 20)) >> 21; s15 += carry14; - s14 -= carry14 << 21; + s14 -= shift_left(carry14, 21); carry16 = (s16 + (1 << 20)) >> 21; s17 += carry16; - s16 -= carry16 << 21; + s16 -= shift_left(carry16, 21); carry7 = (s7 + (1 << 20)) >> 21; s8 += carry7; - s7 -= carry7 << 21; + s7 -= shift_left(carry7, 21); carry9 = (s9 + (1 << 20)) >> 21; s10 += carry9; - s9 -= carry9 << 21; + s9 -= shift_left(carry9, 21); carry11 = (s11 + (1 << 20)) >> 21; s12 += carry11; - s11 -= carry11 << 21; + s11 -= shift_left(carry11, 21); carry13 = (s13 + (1 << 20)) >> 21; s14 += carry13; - s13 -= carry13 << 21; + s13 -= shift_left(carry13, 21); carry15 = (s15 + (1 << 20)) >> 21; s16 += carry15; - s15 -= carry15 << 21; + s15 -= shift_left(carry15, 21); s5 += s17 * 666643; s6 += s17 * 470296; s7 += s17 * 654183; @@ -197,40 +201,40 @@ void sc_reduce(unsigned char *s) { s12 = 0; carry0 = (s0 + (1 << 20)) >> 21; s1 += carry0; - s0 -= carry0 << 21; + s0 -= shift_left(carry0, 21); carry2 = (s2 + (1 << 20)) >> 21; s3 += carry2; - s2 -= carry2 << 21; + s2 -= shift_left(carry2, 21); carry4 = (s4 + (1 << 20)) >> 21; s5 += carry4; - s4 -= carry4 << 21; + s4 -= shift_left(carry4, 21); carry6 = (s6 + (1 << 20)) >> 21; s7 += carry6; - s6 -= carry6 << 21; + s6 -= shift_left(carry6, 21); carry8 = (s8 + (1 << 20)) >> 21; s9 += carry8; - s8 -= carry8 << 21; + s8 -= shift_left(carry8, 21); carry10 = (s10 + (1 << 20)) >> 21; s11 += carry10; - s10 -= carry10 << 21; + s10 -= shift_left(carry10, 21); carry1 = (s1 + (1 << 20)) >> 21; s2 += carry1; - s1 -= carry1 << 21; + s1 -= shift_left(carry1, 21); carry3 = (s3 + (1 << 20)) >> 21; s4 += carry3; - s3 -= carry3 << 21; + s3 -= shift_left(carry3, 21); carry5 = (s5 + (1 << 20)) >> 21; s6 += carry5; - s5 -= carry5 << 21; + s5 -= shift_left(carry5, 21); carry7 = (s7 + (1 << 20)) >> 21; s8 += carry7; - s7 -= carry7 << 21; + s7 -= shift_left(carry7, 21); carry9 = (s9 + (1 << 20)) >> 21; s10 += carry9; - s9 -= carry9 << 21; + s9 -= shift_left(carry9, 21); carry11 = (s11 + (1 << 20)) >> 21; s12 += carry11; - s11 -= carry11 << 21; + s11 -= shift_left(carry11, 21); s0 += s12 * 666643; s1 += s12 * 470296; s2 += s12 * 654183; @@ -240,40 +244,40 @@ void sc_reduce(unsigned char *s) { s12 = 0; carry0 = s0 >> 21; s1 += carry0; - s0 -= carry0 << 21; + s0 -= shift_left(carry0, 21); carry1 = s1 >> 21; s2 += carry1; - s1 -= carry1 << 21; + s1 -= shift_left(carry1, 21); carry2 = s2 >> 21; s3 += carry2; - s2 -= carry2 << 21; + s2 -= shift_left(carry2, 21); carry3 = s3 >> 21; s4 += carry3; - s3 -= carry3 << 21; + s3 -= shift_left(carry3, 21); carry4 = s4 >> 21; s5 += carry4; - s4 -= carry4 << 21; + s4 -= shift_left(carry4, 21); carry5 = s5 >> 21; s6 += carry5; - s5 -= carry5 << 21; + s5 -= shift_left(carry5, 21); carry6 = s6 >> 21; s7 += carry6; - s6 -= carry6 << 21; + s6 -= shift_left(carry6, 21); carry7 = s7 >> 21; s8 += carry7; - s7 -= carry7 << 21; + s7 -= shift_left(carry7, 21); carry8 = s8 >> 21; s9 += carry8; - s8 -= carry8 << 21; + s8 -= shift_left(carry8, 21); carry9 = s9 >> 21; s10 += carry9; - s9 -= carry9 << 21; + s9 -= shift_left(carry9, 21); carry10 = s10 >> 21; s11 += carry10; - s10 -= carry10 << 21; + s10 -= shift_left(carry10, 21); carry11 = s11 >> 21; s12 += carry11; - s11 -= carry11 << 21; + s11 -= shift_left(carry11, 21); s0 += s12 * 666643; s1 += s12 * 470296; s2 += s12 * 654183; @@ -283,37 +287,37 @@ void sc_reduce(unsigned char *s) { s12 = 0; carry0 = s0 >> 21; s1 += carry0; - s0 -= carry0 << 21; + s0 -= shift_left(carry0, 21); carry1 = s1 >> 21; s2 += carry1; - s1 -= carry1 << 21; + s1 -= shift_left(carry1, 21); carry2 = s2 >> 21; s3 += carry2; - s2 -= carry2 << 21; + s2 -= shift_left(carry2, 21); carry3 = s3 >> 21; s4 += carry3; - s3 -= carry3 << 21; + s3 -= shift_left(carry3, 21); carry4 = s4 >> 21; s5 += carry4; - s4 -= carry4 << 21; + s4 -= shift_left(carry4, 21); carry5 = s5 >> 21; s6 += carry5; - s5 -= carry5 << 21; + s5 -= shift_left(carry5, 21); carry6 = s6 >> 21; s7 += carry6; - s6 -= carry6 << 21; + s6 -= shift_left(carry6, 21); carry7 = s7 >> 21; s8 += carry7; - s7 -= carry7 << 21; + s7 -= shift_left(carry7, 21); carry8 = s8 >> 21; s9 += carry8; - s8 -= carry8 << 21; + s8 -= shift_left(carry8, 21); carry9 = s9 >> 21; s10 += carry9; - s9 -= carry9 << 21; + s9 -= shift_left(carry9, 21); carry10 = s10 >> 21; s11 += carry10; - s10 -= carry10 << 21; + s10 -= shift_left(carry10, 21); s[0] = (unsigned char) ((s0 >> 0) & 0xff); s[1] = (unsigned char) ((s0 >> 8) & 0xff); @@ -473,73 +477,73 @@ void sc_muladd(unsigned char *s, const unsigned char *a, const unsigned char *b, s23 = 0; carry0 = (s0 + (1 << 20)) >> 21; s1 += carry0; - s0 -= carry0 << 21; + s0 -= shift_left(carry0, 21); carry2 = (s2 + (1 << 20)) >> 21; s3 += carry2; - s2 -= carry2 << 21; + s2 -= shift_left(carry2, 21); carry4 = (s4 + (1 << 20)) >> 21; s5 += carry4; - s4 -= carry4 << 21; + s4 -= shift_left(carry4, 21); carry6 = (s6 + (1 << 20)) >> 21; s7 += carry6; - s6 -= carry6 << 21; + s6 -= shift_left(carry6, 21); carry8 = (s8 + (1 << 20)) >> 21; s9 += carry8; - s8 -= carry8 << 21; + s8 -= shift_left(carry8, 21); carry10 = (s10 + (1 << 20)) >> 21; s11 += carry10; - s10 -= carry10 << 21; + s10 -= shift_left(carry10, 21); carry12 = (s12 + (1 << 20)) >> 21; s13 += carry12; - s12 -= carry12 << 21; + s12 -= shift_left(carry12, 21); carry14 = (s14 + (1 << 20)) >> 21; s15 += carry14; - s14 -= carry14 << 21; + s14 -= shift_left(carry14, 21); carry16 = (s16 + (1 << 20)) >> 21; s17 += carry16; - s16 -= carry16 << 21; + s16 -= shift_left(carry16, 21); carry18 = (s18 + (1 << 20)) >> 21; s19 += carry18; - s18 -= carry18 << 21; + s18 -= shift_left(carry18, 21); carry20 = (s20 + (1 << 20)) >> 21; s21 += carry20; - s20 -= carry20 << 21; + s20 -= shift_left(carry20, 21); carry22 = (s22 + (1 << 20)) >> 21; s23 += carry22; - s22 -= carry22 << 21; + s22 -= shift_left(carry22, 21); carry1 = (s1 + (1 << 20)) >> 21; s2 += carry1; - s1 -= carry1 << 21; + s1 -= shift_left(carry1, 21); carry3 = (s3 + (1 << 20)) >> 21; s4 += carry3; - s3 -= carry3 << 21; + s3 -= shift_left(carry3, 21); carry5 = (s5 + (1 << 20)) >> 21; s6 += carry5; - s5 -= carry5 << 21; + s5 -= shift_left(carry5, 21); carry7 = (s7 + (1 << 20)) >> 21; s8 += carry7; - s7 -= carry7 << 21; + s7 -= shift_left(carry7, 21); carry9 = (s9 + (1 << 20)) >> 21; s10 += carry9; - s9 -= carry9 << 21; + s9 -= shift_left(carry9, 21); carry11 = (s11 + (1 << 20)) >> 21; s12 += carry11; - s11 -= carry11 << 21; + s11 -= shift_left(carry11, 21); carry13 = (s13 + (1 << 20)) >> 21; s14 += carry13; - s13 -= carry13 << 21; + s13 -= shift_left(carry13, 21); carry15 = (s15 + (1 << 20)) >> 21; s16 += carry15; - s15 -= carry15 << 21; + s15 -= shift_left(carry15, 21); carry17 = (s17 + (1 << 20)) >> 21; s18 += carry17; - s17 -= carry17 << 21; + s17 -= shift_left(carry17, 21); carry19 = (s19 + (1 << 20)) >> 21; s20 += carry19; - s19 -= carry19 << 21; + s19 -= shift_left(carry19, 21); carry21 = (s21 + (1 << 20)) >> 21; s22 += carry21; - s21 -= carry21 << 21; + s21 -= shift_left(carry21, 21); s11 += s23 * 666643; s12 += s23 * 470296; s13 += s23 * 654183; @@ -584,37 +588,37 @@ void sc_muladd(unsigned char *s, const unsigned char *a, const unsigned char *b, s18 = 0; carry6 = (s6 + (1 << 20)) >> 21; s7 += carry6; - s6 -= carry6 << 21; + s6 -= shift_left(carry6, 21); carry8 = (s8 + (1 << 20)) >> 21; s9 += carry8; - s8 -= carry8 << 21; + s8 -= shift_left(carry8, 21); carry10 = (s10 + (1 << 20)) >> 21; s11 += carry10; - s10 -= carry10 << 21; + s10 -= shift_left(carry10, 21); carry12 = (s12 + (1 << 20)) >> 21; s13 += carry12; - s12 -= carry12 << 21; + s12 -= shift_left(carry12, 21); carry14 = (s14 + (1 << 20)) >> 21; s15 += carry14; - s14 -= carry14 << 21; + s14 -= shift_left(carry14, 21); carry16 = (s16 + (1 << 20)) >> 21; s17 += carry16; - s16 -= carry16 << 21; + s16 -= shift_left(carry16, 21); carry7 = (s7 + (1 << 20)) >> 21; s8 += carry7; - s7 -= carry7 << 21; + s7 -= shift_left(carry7, 21); carry9 = (s9 + (1 << 20)) >> 21; s10 += carry9; - s9 -= carry9 << 21; + s9 -= shift_left(carry9, 21); carry11 = (s11 + (1 << 20)) >> 21; s12 += carry11; - s11 -= carry11 << 21; + s11 -= shift_left(carry11, 21); carry13 = (s13 + (1 << 20)) >> 21; s14 += carry13; - s13 -= carry13 << 21; + s13 -= shift_left(carry13, 21); carry15 = (s15 + (1 << 20)) >> 21; s16 += carry15; - s15 -= carry15 << 21; + s15 -= shift_left(carry15, 21); s5 += s17 * 666643; s6 += s17 * 470296; s7 += s17 * 654183; @@ -659,40 +663,40 @@ void sc_muladd(unsigned char *s, const unsigned char *a, const unsigned char *b, s12 = 0; carry0 = (s0 + (1 << 20)) >> 21; s1 += carry0; - s0 -= carry0 << 21; + s0 -= shift_left(carry0, 21); carry2 = (s2 + (1 << 20)) >> 21; s3 += carry2; - s2 -= carry2 << 21; + s2 -= shift_left(carry2, 21); carry4 = (s4 + (1 << 20)) >> 21; s5 += carry4; - s4 -= carry4 << 21; + s4 -= shift_left(carry4, 21); carry6 = (s6 + (1 << 20)) >> 21; s7 += carry6; - s6 -= carry6 << 21; + s6 -= shift_left(carry6, 21); carry8 = (s8 + (1 << 20)) >> 21; s9 += carry8; - s8 -= carry8 << 21; + s8 -= shift_left(carry8, 21); carry10 = (s10 + (1 << 20)) >> 21; s11 += carry10; - s10 -= carry10 << 21; + s10 -= shift_left(carry10, 21); carry1 = (s1 + (1 << 20)) >> 21; s2 += carry1; - s1 -= carry1 << 21; + s1 -= shift_left(carry1, 21); carry3 = (s3 + (1 << 20)) >> 21; s4 += carry3; - s3 -= carry3 << 21; + s3 -= shift_left(carry3, 21); carry5 = (s5 + (1 << 20)) >> 21; s6 += carry5; - s5 -= carry5 << 21; + s5 -= shift_left(carry5, 21); carry7 = (s7 + (1 << 20)) >> 21; s8 += carry7; - s7 -= carry7 << 21; + s7 -= shift_left(carry7, 21); carry9 = (s9 + (1 << 20)) >> 21; s10 += carry9; - s9 -= carry9 << 21; + s9 -= shift_left(carry9, 21); carry11 = (s11 + (1 << 20)) >> 21; s12 += carry11; - s11 -= carry11 << 21; + s11 -= shift_left(carry11, 21); s0 += s12 * 666643; s1 += s12 * 470296; s2 += s12 * 654183; @@ -702,40 +706,40 @@ void sc_muladd(unsigned char *s, const unsigned char *a, const unsigned char *b, s12 = 0; carry0 = s0 >> 21; s1 += carry0; - s0 -= carry0 << 21; + s0 -= shift_left(carry0, 21); carry1 = s1 >> 21; s2 += carry1; - s1 -= carry1 << 21; + s1 -= shift_left(carry1, 21); carry2 = s2 >> 21; s3 += carry2; - s2 -= carry2 << 21; + s2 -= shift_left(carry2, 21); carry3 = s3 >> 21; s4 += carry3; - s3 -= carry3 << 21; + s3 -= shift_left(carry3, 21); carry4 = s4 >> 21; s5 += carry4; - s4 -= carry4 << 21; + s4 -= shift_left(carry4, 21); carry5 = s5 >> 21; s6 += carry5; - s5 -= carry5 << 21; + s5 -= shift_left(carry5, 21); carry6 = s6 >> 21; s7 += carry6; - s6 -= carry6 << 21; + s6 -= shift_left(carry6, 21); carry7 = s7 >> 21; s8 += carry7; - s7 -= carry7 << 21; + s7 -= shift_left(carry7, 21); carry8 = s8 >> 21; s9 += carry8; - s8 -= carry8 << 21; + s8 -= shift_left(carry8, 21); carry9 = s9 >> 21; s10 += carry9; - s9 -= carry9 << 21; + s9 -= shift_left(carry9, 21); carry10 = s10 >> 21; s11 += carry10; - s10 -= carry10 << 21; + s10 -= shift_left(carry10, 21); carry11 = s11 >> 21; s12 += carry11; - s11 -= carry11 << 21; + s11 -= shift_left(carry11, 21); s0 += s12 * 666643; s1 += s12 * 470296; s2 += s12 * 654183; @@ -745,37 +749,37 @@ void sc_muladd(unsigned char *s, const unsigned char *a, const unsigned char *b, s12 = 0; carry0 = s0 >> 21; s1 += carry0; - s0 -= carry0 << 21; + s0 -= shift_left(carry0, 21); carry1 = s1 >> 21; s2 += carry1; - s1 -= carry1 << 21; + s1 -= shift_left(carry1, 21); carry2 = s2 >> 21; s3 += carry2; - s2 -= carry2 << 21; + s2 -= shift_left(carry2, 21); carry3 = s3 >> 21; s4 += carry3; - s3 -= carry3 << 21; + s3 -= shift_left(carry3, 21); carry4 = s4 >> 21; s5 += carry4; - s4 -= carry4 << 21; + s4 -= shift_left(carry4, 21); carry5 = s5 >> 21; s6 += carry5; - s5 -= carry5 << 21; + s5 -= shift_left(carry5, 21); carry6 = s6 >> 21; s7 += carry6; - s6 -= carry6 << 21; + s6 -= shift_left(carry6, 21); carry7 = s7 >> 21; s8 += carry7; - s7 -= carry7 << 21; + s7 -= shift_left(carry7, 21); carry8 = s8 >> 21; s9 += carry8; - s8 -= carry8 << 21; + s8 -= shift_left(carry8, 21); carry9 = s9 >> 21; s10 += carry9; - s9 -= carry9 << 21; + s9 -= shift_left(carry9, 21); carry10 = s10 >> 21; s11 += carry10; - s10 -= carry10 << 21; + s10 -= shift_left(carry10, 21); s[0] = (unsigned char) ((s0 >> 0) & 0xff); s[1] = (unsigned char) ((s0 >> 8) & 0xff); diff --git a/include/libtorrent/alert_manager.hpp b/include/libtorrent/alert_manager.hpp index 41e883a41..017240264 100644 --- a/include/libtorrent/alert_manager.hpp +++ b/include/libtorrent/alert_manager.hpp @@ -74,7 +74,8 @@ namespace libtorrent { , boost::uint32_t alert_mask = alert::error_notification); ~alert_manager(); -#ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES +#if !defined BOOST_NO_CXX11_VARIADIC_TEMPLATES \ + && !defined BOOST_NO_CXX11_RVALUE_REFERENCES template void emplace_alert(Args&&... args) diff --git a/include/libtorrent/bitfield.hpp b/include/libtorrent/bitfield.hpp index cd815950a..81dd1ff12 100644 --- a/include/libtorrent/bitfield.hpp +++ b/include/libtorrent/bitfield.hpp @@ -226,11 +226,13 @@ namespace libtorrent // set all bits in the bitfield to 1 (set_all) or 0 (clear_all). void set_all() { + if (m_buf == NULL) return; std::memset(m_buf, 0xff, size_t(num_words() * 4)); clear_trailing_bits(); } void clear_all() { + if (m_buf == NULL) return; std::memset(m_buf, 0x00, size_t(num_words() * 4)); } diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index 0499608a0..07c1089e6 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -76,7 +76,8 @@ POSSIBILITY OF SUCH DAMAGE. #if DEBUG_DISK_THREAD #define DLOG debug_log #else -#define DLOG TORRENT_WHILE_0 debug_log +inline void dummy(char const*, ...) {} +#define DLOG TORRENT_WHILE_0 dummy #endif #endif // cplusplus @@ -95,14 +96,10 @@ namespace libtorrent namespace { -#ifdef __clang__ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wunused-function" -#endif +#if DEBUG_DISK_THREAD void debug_log(char const* fmt, ...) { -#if DEBUG_DISK_THREAD static mutex log_mutex; static const time_point start = clock_type::now(); va_list v; @@ -126,11 +123,10 @@ namespace libtorrent prepend_time = (usr[len-1] == '\n'); mutex::scoped_lock l(log_mutex); fputs(buf, stderr); -#else - TORRENT_UNUSED(fmt); -#endif } +#endif // DEBUG_DISK_THREAD + int file_flags_for_job(disk_io_job* j , bool const coalesce_buffers) { @@ -140,10 +136,6 @@ namespace libtorrent return ret; } -#ifdef __clang__ -#pragma clang diagnostic pop -#endif - } // anonymous namespace // ------- disk_io_thread ------ diff --git a/src/enum_net.cpp b/src/enum_net.cpp index 7d612bb80..a9cdca5f3 100644 --- a/src/enum_net.cpp +++ b/src/enum_net.cpp @@ -102,6 +102,7 @@ const unsigned long siocgifmtu = SIOCGIFMTU; namespace libtorrent { namespace { +#if !defined TORRENT_BUILD_SIMULATOR address inaddr_to_address(in_addr const* ina, int len = 4) { typedef boost::asio::ip::address_v4::bytes_type bytes_t; @@ -157,7 +158,7 @@ namespace libtorrent { namespace int read_len = recv(sock, buf, bufsize - msg_len, 0); if (read_len < 0) return -1; - nl_hdr = (nlmsghdr*)buf; + nl_hdr = reinterpret_cast(buf); if ((NLMSG_OK(nl_hdr, read_len) == 0) || (nl_hdr->nlmsg_type == NLMSG_ERROR)) return -1; @@ -175,7 +176,7 @@ namespace libtorrent { namespace bool parse_route(int s, nlmsghdr* nl_hdr, ip_route* rt_info) { - rtmsg* rt_msg = (rtmsg*)NLMSG_DATA(nl_hdr); + rtmsg* rt_msg = reinterpret_cast(NLMSG_DATA(nl_hdr)); if((rt_msg->rtm_family != AF_INET && rt_msg->rtm_family != AF_INET6) || (rt_msg->rtm_table != RT_TABLE_MAIN && rt_msg->rtm_table != RT_TABLE_LOCAL)) @@ -183,40 +184,47 @@ namespace libtorrent { namespace int if_index = 0; int rt_len = RTM_PAYLOAD(nl_hdr); - for (rtattr* rt_attr = (rtattr*)RTM_RTA(rt_msg); +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wcast-align" +#endif + for (rtattr* rt_attr = reinterpret_cast(RTM_RTA(rt_msg)); RTA_OK(rt_attr,rt_len); rt_attr = RTA_NEXT(rt_attr,rt_len)) { switch(rt_attr->rta_type) { case RTA_OIF: - if_index = *(int*)RTA_DATA(rt_attr); + if_index = *reinterpret_cast(RTA_DATA(rt_attr)); break; case RTA_GATEWAY: #if TORRENT_USE_IPV6 if (rt_msg->rtm_family == AF_INET6) { - rt_info->gateway = inaddr6_to_address((in6_addr*)RTA_DATA(rt_attr)); + rt_info->gateway = inaddr6_to_address(reinterpret_cast(RTA_DATA(rt_attr))); } else #endif { - rt_info->gateway = inaddr_to_address((in_addr*)RTA_DATA(rt_attr)); + rt_info->gateway = inaddr_to_address(reinterpret_cast(RTA_DATA(rt_attr))); } break; case RTA_DST: #if TORRENT_USE_IPV6 if (rt_msg->rtm_family == AF_INET6) { - rt_info->destination = inaddr6_to_address((in6_addr*)RTA_DATA(rt_attr)); + rt_info->destination = inaddr6_to_address(reinterpret_cast(RTA_DATA(rt_attr))); } else #endif { - rt_info->destination = inaddr_to_address((in_addr*)RTA_DATA(rt_attr)); + rt_info->destination = inaddr_to_address(reinterpret_cast(RTA_DATA(rt_attr))); } break; } } +#ifdef __clang__ +#pragma clang diagnostic pop +#endif if_indextoname(if_index, rt_info->name); ifreq req; @@ -230,7 +238,8 @@ namespace libtorrent { namespace // } return true; } -#endif +#endif // TORRENT_USE_NETLINK +#endif // !BUILD_SIMULATOR #if TORRENT_USE_SYSCTL && !defined TORRENT_BUILD_SIMULATOR #ifdef TORRENT_OS2 @@ -375,7 +384,7 @@ namespace libtorrent typedef boost::asio::ip::address_v4::bytes_type bytes_t; bytes_t b; std::memset(&b[0], 0xff, b.size()); - for (int i = sizeof(bytes_t)/8-1; i > 0; --i) + for (int i = int(sizeof(bytes_t)) / 8 - 1; i > 0; --i) { if (bits < 8) { @@ -393,7 +402,7 @@ namespace libtorrent typedef boost::asio::ip::address_v6::bytes_type bytes_t; bytes_t b; std::memset(&b[0], 0xff, b.size()); - for (int i = sizeof(bytes_t)/8-1; i > 0; --i) + for (int i = int(sizeof(bytes_t)) / 8 - 1; i > 0; --i) { if (bits < 8) { @@ -465,9 +474,9 @@ namespace libtorrent if (iface_from_ifaddrs(ifa, iface)) { ifreq req; - memset(&req, 0, sizeof(req)); - // -1 to leave a null terminator - strncpy(req.ifr_name, iface.name, IF_NAMESIZE - 1); + std::memset(&req, 0, sizeof(req)); + // -1 to leave a 0-terminator + std::strncpy(req.ifr_name, iface.name, IF_NAMESIZE - 1); // ignore errors here. This is best-effort ioctl(s, siocgifmtu, &req); @@ -490,7 +499,7 @@ namespace libtorrent // make sure the buffer is aligned to hold ifreq structs ifreq buf[40]; ifc.ifc_len = sizeof(buf); - ifc.ifc_buf = (char*)buf; + ifc.ifc_buf = reinterpret_cast(buf); if (ioctl(s, SIOCGIFCONF, &ifc) < 0) { ec = error_code(errno, system_category()); @@ -498,7 +507,7 @@ namespace libtorrent return ret; } - char *ifr = (char*)ifc.ifc_req; + char *ifr = reinterpret_cast(ifc.ifc_req); int remaining = ifc.ifc_len; while (remaining > 0) @@ -527,7 +536,7 @@ namespace libtorrent ifreq req; memset(&req, 0, sizeof(req)); - // -1 to leave a null terminator + // -1 to leave a 0-terminator strncpy(req.ifr_name, item.ifr_name, IF_NAMESIZE - 1); if (ioctl(s, siocgifmtu, &req) < 0) { @@ -1081,7 +1090,7 @@ namespace libtorrent char msg[BUFSIZE]; memset(msg, 0, BUFSIZE); - nlmsghdr* nl_msg = (nlmsghdr*)msg; + nlmsghdr* nl_msg = reinterpret_cast(msg); nl_msg->nlmsg_len = NLMSG_LENGTH(sizeof(rtmsg)); nl_msg->nlmsg_type = RTM_GETROUTE; @@ -1110,11 +1119,18 @@ namespace libtorrent ec = error_code(errno, system_category()); return std::vector(); } +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wcast-align" +#endif for (; NLMSG_OK(nl_msg, len); nl_msg = NLMSG_NEXT(nl_msg, len)) { ip_route r; if (parse_route(s, nl_msg, &r)) ret.push_back(r); } +#ifdef __clang__ +#pragma clang diagnostic pop +#endif close(s); close(sock); diff --git a/src/file.cpp b/src/file.cpp index cba76c36b..2d3fe2b89 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -1681,6 +1681,7 @@ typedef struct _FILE_ALLOCATED_RANGE_BUFFER { namespace { +#if !TORRENT_USE_PREADV void gather_copy(file::iovec_t const* bufs, int num_bufs, char* dst) { std::size_t offset = 0; @@ -1701,7 +1702,6 @@ typedef struct _FILE_ALLOCATED_RANGE_BUFFER { } } -#if !TORRENT_USE_PREADV bool coalesce_read_buffers(file::iovec_t const*& bufs, int& num_bufs , file::iovec_t* tmp) { diff --git a/src/torrent.cpp b/src/torrent.cpp index 5ae0e7f5e..e27cdb567 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -1062,14 +1062,15 @@ namespace libtorrent // since the call to disconnect_if_redundant() may // delete the entry from this container, make sure // to increment the iterator early - bt_peer_connection* p = static_cast(*i); + peer_connection* p = *i; if (p->type() == peer_connection::bittorrent_connection) { - boost::shared_ptr me(p->self()); - if (!p->is_disconnecting()) + bt_peer_connection* btp = static_cast(*i); + boost::shared_ptr me(btp->self()); + if (!btp->is_disconnecting()) { - p->send_not_interested(); - p->write_upload_only(); + btp->send_not_interested(); + btp->write_upload_only(); } } diff --git a/src/utp_stream.cpp b/src/utp_stream.cpp index 630fffa9b..ff53ee193 100644 --- a/src/utp_stream.cpp +++ b/src/utp_stream.cpp @@ -807,7 +807,7 @@ void utp_socket_impl::update_mtu_limits() m_mtu = (m_mtu_floor + m_mtu_ceiling) / 2; - if ((m_cwnd >> 16) < m_mtu) m_cwnd = boost::int64_t(m_mtu) << 16; + if ((m_cwnd >> 16) < m_mtu) m_cwnd = boost::int64_t(m_mtu) * (1 << 16); UTP_LOGV("%8p: updating MTU to: %d [%d, %d]\n" , static_cast(this), m_mtu, m_mtu_floor, m_mtu_ceiling); @@ -2338,7 +2338,8 @@ void utp_socket_impl::experienced_loss(int const seq_nr) if (compare_less_wrap(seq_nr, m_loss_seq_nr + 1, ACK_MASK)) return; // cut window size in 2 - m_cwnd = (std::max)(m_cwnd * m_sm->loss_multiplier() / 100, boost::int64_t(m_mtu << 16)); + m_cwnd = std::max(m_cwnd * m_sm->loss_multiplier() / 100 + , boost::int64_t(m_mtu) * (1 << 16)); m_loss_seq_nr = m_seq_nr; UTP_LOGV("%8p: Lost packet %d caused cwnd cut\n", static_cast(this), seq_nr); @@ -2699,7 +2700,7 @@ void utp_socket_impl::init_mtu(int link_mtu, int utp_mtu) // if the window size is smaller than one packet size // set it to one - if ((m_cwnd >> 16) < m_mtu) m_cwnd = boost::int64_t(m_mtu) << 16; + if ((m_cwnd >> 16) < m_mtu) m_cwnd = boost::int64_t(m_mtu) * (1 << 16); UTP_LOGV("%8p: initializing MTU to: %d [%d, %d]\n" , static_cast(this), m_mtu, m_mtu_floor, m_mtu_ceiling); @@ -3424,8 +3425,8 @@ void utp_socket_impl::do_ledbat(const int acked_bytes, const int delay const bool cwnd_saturated = (m_bytes_in_flight + acked_bytes + m_mtu > (m_cwnd >> 16)); // all of these are fixed points with 16 bits fraction portion - const boost::int64_t window_factor = (boost::int64_t(acked_bytes) << 16) / in_flight; - const boost::int64_t delay_factor = (boost::int64_t(target_delay - delay) << 16) / target_delay; + const boost::int64_t window_factor = (boost::int64_t(acked_bytes) * (1 << 16)) / in_flight; + const boost::int64_t delay_factor = (boost::int64_t(target_delay - delay) * (1 << 16)) / target_delay; boost::int64_t scaled_gain; if (delay >= target_delay) @@ -3452,7 +3453,7 @@ void utp_socket_impl::do_ledbat(const int acked_bytes, const int delay // congestion window), don't adjust it at all. if (cwnd_saturated) { - boost::int64_t exponential_gain = boost::int64_t(acked_bytes) << 16; + boost::int64_t exponential_gain = boost::int64_t(acked_bytes) * (1 << 16); if (m_slow_start) { // mimic TCP slow-start by adding the number of acked @@ -3623,13 +3624,13 @@ void utp_socket_impl::tick(time_point now) { // this is just a timeout because this direction of // the stream is idle. Don't reset the cwnd, just decay it - m_cwnd = (std::max)(m_cwnd * 2 / 3, boost::int64_t(m_mtu) << 16); + m_cwnd = std::max(m_cwnd * 2 / 3, boost::int64_t(m_mtu) * (1 << 16)); } else { // we timed out because a packet was not ACKed or because // the cwnd was made smaller than one packet - m_cwnd = boost::int64_t(m_mtu) << 16; + m_cwnd = boost::int64_t(m_mtu) * (1 << 16); } TORRENT_ASSERT(m_cwnd >= 0); diff --git a/test/Jamfile b/test/Jamfile index b5e69338a..58b9c3f2b 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -151,6 +151,7 @@ test-suite libtorrent : [ run test_web_seed_chunked.cpp ] [ run test_web_seed_ban.cpp ] [ run test_pe_crypto.cpp ] + [ run test_ed25519.cpp ] [ run test_remap_files.cpp ] [ run test_utp.cpp ] diff --git a/test/Makefile.am b/test/Makefile.am index f4695c899..a3c966dc8 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -45,7 +45,8 @@ test_programs = \ test_enum_net \ test_file_progress \ test_linked_list \ - test_direct_dht + test_direct_dht \ + test_ed25519.cpp if ENABLE_TESTS check_PROGRAMS = $(test_programs) $(benchmark_programs) diff --git a/test/main.cpp b/test/main.cpp index 8061038e0..e6dfd541e 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -68,7 +68,8 @@ using namespace libtorrent; // out, such as the log int old_stdout = -1; int old_stderr = -1; -bool redirect_output = true; +bool redirect_stdout = true; +bool redirect_stderr = true; bool keep_files = false; extern int _g_test_idx; @@ -79,7 +80,7 @@ unit_test_t* current_test = NULL; void output_test_log_to_terminal() { if (current_test == NULL || old_stdout == -1 || old_stderr == -1 - || !redirect_output || current_test->output == NULL) + || !redirect_stdout || current_test->output == NULL) return; fflush(stdout); @@ -88,12 +89,12 @@ void output_test_log_to_terminal() dup2(old_stderr, fileno(stderr)); fseek(current_test->output, 0, SEEK_SET); - fprintf(stderr, "\x1b[1m[%s]\x1b[0m\n\n", current_test->name); + fprintf(stdout, "\x1b[1m[%s]\x1b[0m\n\n", current_test->name); char buf[4096]; int size = 0; do { size = fread(buf, 1, sizeof(buf), current_test->output); - if (size > 0) fwrite(buf, 1, size, stderr); + if (size > 0) fwrite(buf, 1, size, stdout); } while (size > 0); } @@ -205,13 +206,17 @@ void print_usage(char const* executable) printf("%s [options] [tests...]\n" "\n" "OPTIONS:\n" - "-h,--help show this help\n" - "-l,--list list the tests available to run\n" - "-k,--keep keep files created by the test\n" - " regardless of whether it passed or not\n" - "-n,--no-redirect don't redirect test output to\n" - " temporary file, but let it go straight\n" - " to stdout\n" + "-h,--help show this help\n" + "-l,--list list the tests available to run\n" + "-k,--keep keep files created by the test\n" + " regardless of whether it passed or not\n" + "-n,--no-redirect don't redirect test output to\n" + " temporary file, but let it go straight\n" + " to stdout\n" + "--no-stderr-redirect don't redirect stderr, but still redirect\n" + " stdout. This is useful when building with\n" + " sanitizers, which rely on being able to print\n" + " to stderr and exit\n" "\n" "for tests, specify one or more test names as printed\n" "by -l. If no test is specified, all tests are run\n", executable); @@ -245,7 +250,13 @@ EXPORT int main(int argc, char const* argv[]) if (strcmp(argv[0], "-n") == 0 || strcmp(argv[0], "--no-redirect") == 0) { - redirect_output = false; + redirect_stdout = false; + redirect_stderr = false; + } + + if (strcmp(argv[0], "--no-stderr-redirect") == 0) + { + redirect_stderr = false; } if (strcmp(argv[0], "-k") == 0 || strcmp(argv[0], "--keep") == 0) @@ -335,11 +346,8 @@ EXPORT int main(int argc, char const* argv[]) return 1; } - if (redirect_output) - { - old_stdout = dup(fileno(stdout)); - old_stderr = dup(fileno(stderr)); - } + if (redirect_stdout) old_stdout = dup(fileno(stdout)); + if (redirect_stderr) old_stderr = dup(fileno(stderr)); int num_run = 0; for (int i = 0; i < _g_num_unit_tests; ++i) @@ -349,7 +357,7 @@ EXPORT int main(int argc, char const* argv[]) unit_test_t& t = _g_unit_tests[i]; - if (redirect_output) + if (redirect_stdout) { // redirect test output to a temporary file fflush(stdout); @@ -359,7 +367,7 @@ EXPORT int main(int argc, char const* argv[]) if (f != NULL) { int ret1 = dup2(fileno(f), fileno(stdout)); - dup2(fileno(f), fileno(stderr)); + if (redirect_stderr) dup2(fileno(f), fileno(stderr)); if (ret1 >= 0) { t.output = f; @@ -417,17 +425,12 @@ EXPORT int main(int argc, char const* argv[]) total_failures += _g_test_failures; ++num_run; - if (redirect_output && t.output) - { + if (redirect_stdout && t.output) fclose(t.output); - } } - if (redirect_output) - { - dup2(old_stdout, fileno(stdout)); - dup2(old_stderr, fileno(stderr)); - } + if (redirect_stdout) dup2(old_stdout, fileno(stdout)); + if (redirect_stderr) dup2(old_stderr, fileno(stderr)); if (!tests_to_run.empty()) { @@ -453,11 +456,8 @@ EXPORT int main(int argc, char const* argv[]) stop_peer(); stop_dht(); - if (redirect_output) - { - fflush(stdout); - fflush(stderr); - } + if (redirect_stdout) fflush(stdout); + if (redirect_stderr) fflush(stderr); int ret = print_failures(); #if !defined TORRENT_LOGGING diff --git a/test/test_ed25519.cpp b/test/test_ed25519.cpp new file mode 100644 index 000000000..5bebe69d4 --- /dev/null +++ b/test/test_ed25519.cpp @@ -0,0 +1,228 @@ +/* + +Copyright (c) 2016, Alden Torres +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the distribution. + * Neither the name of the author nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +*/ + +#include "test.hpp" + +#ifndef TORRENT_DISABLE_DHT + +#include + +#include +#include "libtorrent/hex.hpp" + +using namespace libtorrent; + +namespace +{ + void test_vector(std::string seed, std::string pub, std::string sig_hex, std::string message) + { + typedef unsigned char uchar; + uchar s[32]; + uchar sk[64]; + uchar pk[32]; + uchar sig[64]; + std::vector msg(int(message.size()) / 2); + + from_hex(seed.c_str(), seed.size(), reinterpret_cast(s)); + ed25519_create_keypair(pk, sk, s); + + TEST_EQUAL(to_hex(std::string(reinterpret_cast(pk), sizeof(pk))), pub); + + from_hex(message.c_str(), message.size(), reinterpret_cast(&msg[0])); + ed25519_sign(sig, &msg[0], msg.size(), pk, sk); + + TEST_EQUAL(to_hex(std::string(reinterpret_cast(sig), sizeof(sig))), sig_hex); + + bool r = ed25519_verify(sig, &msg[0], msg.size(), pk); + + TEST_CHECK(r); + } +} + +// https://git.gnupg.org/cgi-bin/gitweb.cgi?p=libgcrypt.git;a=blob;f=tests/t-ed25519.inp;hb=HEAD +TORRENT_TEST(ed25519_test_vec1) +{ + // TST: 2 + test_vector( + "4ccd089b28ff96da9db6c346ec114e0f5b8a319f35aba624da8cf6ed4fb8a6fb" + , "3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c" + , "92a009a9f0d4cab8720e820b5f642540a2b27b5416503f8fb3762223ebdb69da" + "085ac1e43e15996e458f3613d0f11d8c387b2eaeb4302aeeb00d291612bb0c00" + , "72" + ); + + // TST: 3 + test_vector( + "c5aa8df43f9f837bedb7442f31dcb7b166d38535076f094b85ce3a2e0b4458f7" + , "fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025" + , "6291d657deec24024827e69c3abe01a30ce548a284743a445e3680d7db5ac3ac" + "18ff9b538d16f290ae67f760984dc6594a7c15e9716ed28dc027beceea1ec40a" + , "af82" + ); + + // TST: 4 + test_vector( + "0d4a05b07352a5436e180356da0ae6efa0345ff7fb1572575772e8005ed978e9" + , "e61a185bcef2613a6c7cb79763ce945d3b245d76114dd440bcf5f2dc1aa57057" + , "d9868d52c2bebce5f3fa5a79891970f309cb6591e3e1702a70276fa97c24b3a8" + "e58606c38c9758529da50ee31b8219cba45271c689afa60b0ea26c99db19b00c" + , "cbc77b" + ); + + // TST: 47 + test_vector( + "89f0d68299ba0a5a83f248ae0c169f8e3849a9b47bd4549884305c9912b46603" + , "aba3e795aab2012acceadd7b3bd9daeeed6ff5258bdcd7c93699c2a3836e3832" + , "2c691fa8d487ce20d5d2fa41559116e0bbf4397cf5240e152556183541d66cf7" + "53582401a4388d390339dbef4d384743caa346f55f8daba68ba7b9131a8a6e0b" + , "4f1846dd7ad50e545d4cfbffbb1dc2ff145dc123754d08af4e44ecc0bc8c9141" + "1388bc7653e2d893d1eac2107d05" + ); + + // TST: 48 + test_vector( + "0a3c1844e2db070fb24e3c95cb1cc6714ef84e2ccd2b9dd2f1460ebf7ecf13b1" + , "72e409937e0610eb5c20b326dc6ea1bbbc0406701c5cd67d1fbde09192b07c01" + , "87f7fdf46095201e877a588fe3e5aaf476bd63138d8a878b89d6ac60631b3458" + "b9d41a3c61a588e1db8d29a5968981b018776c588780922f5aa732ba6379dd05" + , "4c8274d0ed1f74e2c86c08d955bde55b2d54327e82062a1f71f70d536fdc8722" + "cdead7d22aaead2bfaa1ad00b82957" + ); + + // TST: 49 + test_vector( + "c8d7a8818b98dfdb20839c871cb5c48e9e9470ca3ad35ba2613a5d3199c8ab23" + , "90d2efbba4d43e6b2b992ca16083dbcfa2b322383907b0ee75f3e95845d3c47f" + , "fa2e994421aef1d5856674813d05cbd2cf84ef5eb424af6ecd0dc6fdbdc2fe60" + "5fe985883312ecf34f59bfb2f1c9149e5b9cc9ecda05b2731130f3ed28ddae0b" + , "783e33c3acbdbb36e819f544a7781d83fc283d3309f5d3d12c8dcd6b0b3d0e89" + "e38cfd3b4d0885661ca547fb9764abff" + ); + + // TST: 50 + test_vector( + "b482703612d0c586f76cfcb21cfd2103c957251504a8c0ac4c86c9c6f3e429ff" + , "fd711dc7dd3b1dfb9df9704be3e6b26f587fe7dd7ba456a91ba43fe51aec09ad" + , "58832bdeb26feafc31b46277cf3fb5d7a17dfb7ccd9b1f58ecbe6feb97966682" + "8f239ba4d75219260ecac0acf40f0e5e2590f4caa16bbbcd8a155d347967a607" + , "29d77acfd99c7a0070a88feb6247a2bce9984fe3e6fbf19d4045042a21ab26cb" + "d771e184a9a75f316b648c6920db92b87b" + ); + + // TST: 51 + test_vector( + "84e50dd9a0f197e3893c38dbd91fafc344c1776d3a400e2f0f0ee7aa829eb8a2" + , "2c50f870ee48b36b0ac2f8a5f336fb090b113050dbcc25e078200a6e16153eea" + , "69e6a4491a63837316e86a5f4ba7cd0d731ecc58f1d0a264c67c89befdd8d382" + "9d8de13b33cc0bf513931715c7809657e2bfb960e5c764c971d733746093e500" + , "f3992cde6493e671f1e129ddca8038b0abdb77bb9035f9f8be54bd5d68c1aeff" + "724ff47d29344391dc536166b8671cbbf123" + ); + + // TST: 52 + test_vector( + "b322d46577a2a991a4d1698287832a39c487ef776b4bff037a05c7f1812bdeec" + , "eb2bcadfd3eec2986baff32b98e7c4dbf03ff95d8ad5ff9aa9506e5472ff845f" + , "c7b55137317ca21e33489ff6a9bfab97c855dc6f85684a70a9125a261b56d5e6" + "f149c5774d734f2d8debfc77b721896a8267c23768e9badb910eef83ec258802" + , "19f1bf5dcf1750c611f1c4a2865200504d82298edd72671f62a7b1471ac3d4a3" + "0f7de9e5da4108c52a4ce70a3e114a52a3b3c5" + ); + + // TST: 53 + test_vector( + "960cab5034b9838d098d2dcbf4364bec16d388f6376d73a6273b70f82bbc98c0" + , "5e3c19f2415acf729f829a4ebd5c40e1a6bc9fbca95703a9376087ed0937e51a" + , "27d4c3a1811ef9d4360b3bdd133c2ccc30d02c2f248215776cb07ee4177f9b13" + "fc42dd70a6c2fed8f225c7663c7f182e7ee8eccff20dc7b0e1d5834ec5b1ea01" + , "f8b21962447b0a8f2e4279de411bea128e0be44b6915e6cda88341a68a0d8183" + "57db938eac73e0af6d31206b3948f8c48a447308" + ); + + // TST: 224 + test_vector( + "ae1d2c6b171be24c2e413d364dcda97fa476aaf9123d3366b0be03a142fe6e7d" + , "d437f57542c681dd543487408ec7a44bd42a5fd545ce2f4c8297d67bb0b3aa7b" + , "909008f3fcfff43988aee1314b15b1822caaa8dab120bd452af494e08335b44a" + "94c313c4b145eadd5166eaac034e29b7e6ac7941d5961fc49d260e1c4820b00e" + , "9e6c2fc76e30f17cd8b498845da44f22d55bec150c6130b411c6339d14b39969" + "ab1033be687569a991a06f70b2a8a6931a777b0e4be6723cd75e5aa7532813ef" + "50b3d37271640fa2fb287c0355257641ea935c851c0b6ac68be72c88dfc5856f" + "b53543fb377b0dbf64808afcc4274aa456855ad28f61267a419bc72166b9ca73" + "cd3bb79bf7dd259baa75911440974b68e8ba95a78cbbe1cb6ad807a33a1cce2f" + "406ff7bcbd058b44a311b38ab4d4e61416c4a74d883d6a6a794abd9cf1c03902" + "8bf1b20e3d4990aae86f32bf06cd8349a7a884cce0165e36a0640e987b9d51" + ); + + // TST: 225 + test_vector( + "0265a7944baccfebf417b87ae1e6df2ff2a544ffb58225a08e092be03f026097" + , "63d327615ea0139be0740b618aff1acfa818d4b0c2cfeaf0da93cdd5245fb5a9" + , "b6c445b7eddca5935c61708d44ea5906bd19cc54224eae3c8e46ce99f5cbbd34" + "1f26623938f5fe04070b1b02e71fbb7c78a90c0dda66cb143fab02e6a0bae306" + , "874ed712a2c41c26a2d9527c55233fde0a4ffb86af8e8a1dd0a820502c5a2693" + "2bf87ee0de72a8874ef2eebf83384d443f7a5f46a1233b4fb514a24699818248" + "94f325bf86aa0fe1217153d40f3556c43a8ea9269444e149fb70e9415ae0766c" + "565d93d1d6368f9a23a0ad76f9a09dbf79634aa97178677734d04ef1a5b3f87c" + "e1ee9fc5a9ac4e7a72c9d7d31ec89e28a845d2e1103c15d6410ce3c723b0cc22" + "09f698aa9fa288bbbecfd9e5f89cdcb09d3c215feb47a58b71ea70e2abead67f" + "1b08ea6f561fb93ef05232eedabfc1c7702ab039bc465cf57e207f1093fc8208" + ); +} + +TORRENT_TEST(create_seed) +{ + typedef unsigned char uchar; + uchar s1[32]; + ed25519_create_seed(s1); + uchar s2[32]; + ed25519_create_seed(s2); + + TEST_CHECK(memcpy(s1, s2, sizeof(s1)) != 0); // what are the odds + + int n1 = 0; + int n2 = 0; + for (int i = 0; i < 32; i++) + { + if (s1[i] != 0) n1++; + if (s2[i] != 0) n2++; + } + TEST_CHECK(n1 > 0); + TEST_CHECK(n2 > 0); +} + +#else +TORRENT_TEST(empty) +{ + TEST_CHECK(true); +} +#endif // TORRENT_DISABLE_DHT