diff --git a/ChangeLog b/ChangeLog index fc39a722a..bacb1605f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,5 @@ + * add getters for peer_class_filter and peer_class_type_filter * make torrent_handler::set_priority() to use peer_classes * fix support for boost-1.66 (requires C++11) * fix i2p support diff --git a/include/libtorrent/ip_filter.hpp b/include/libtorrent/ip_filter.hpp index 82b6a5640..dac1cab6f 100644 --- a/include/libtorrent/ip_filter.hpp +++ b/include/libtorrent/ip_filter.hpp @@ -66,6 +66,12 @@ struct ip_range Addr first; Addr last; boost::uint32_t flags; + friend bool operator==(ip_range const& lhs, ip_range const& rhs) + { + return lhs.first == rhs.first + && lhs.last == rhs.last + && lhs.flags == rhs.flags; + } }; namespace detail diff --git a/include/libtorrent/peer_class.hpp b/include/libtorrent/peer_class.hpp index 300282543..b2b8ed183 100644 --- a/include/libtorrent/peer_class.hpp +++ b/include/libtorrent/peer_class.hpp @@ -47,7 +47,7 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { - typedef boost::uint32_t peer_class_t; + typedef int peer_class_t; // holds settings for a peer class. Used in set_peer_class() and // get_peer_class() calls. diff --git a/include/libtorrent/peer_class_type_filter.hpp b/include/libtorrent/peer_class_type_filter.hpp index 458a39b58..ed213e03a 100644 --- a/include/libtorrent/peer_class_type_filter.hpp +++ b/include/libtorrent/peer_class_type_filter.hpp @@ -33,8 +33,8 @@ POSSIBILITY OF SUCH DAMAGE. #ifndef TORRENT_PEER_CLASS_TYPE_FILTER_HPP_INCLUDED #define TORRENT_PEER_CLASS_TYPE_FILTER_HPP_INCLUDED -#include // for memset #include +#include namespace libtorrent { @@ -46,8 +46,8 @@ namespace libtorrent { peer_class_type_filter() { - memset(m_peer_class_type_mask, 0xff, sizeof(m_peer_class_type_mask)); - memset(m_peer_class_type, 0, sizeof(m_peer_class_type)); + m_peer_class_type_mask.fill(0xffffffff); + m_peer_class_type.fill(0); } enum socket_type_t @@ -64,7 +64,7 @@ namespace libtorrent // ``add()`` and ``remove()`` adds and removes a peer class to be added // to new peers based on socket type. - void add(socket_type_t st, int peer_class) + void add(socket_type_t st, peer_class_t peer_class) { TORRENT_ASSERT(peer_class >= 0); TORRENT_ASSERT(peer_class < 32); @@ -74,7 +74,7 @@ namespace libtorrent if (st < 0 || st >= num_socket_types) return; m_peer_class_type[st] |= 1 << peer_class; } - void remove(socket_type_t st, int peer_class) + void remove(socket_type_t st, peer_class_t peer_class) { TORRENT_ASSERT(peer_class >= 0); TORRENT_ASSERT(peer_class < 32); @@ -90,7 +90,7 @@ namespace libtorrent // // The ``peer_class`` argument cannot be greater than 31. The bitmasks representing // peer classes in the ``peer_class_type_filter`` are 32 bits. - void disallow(socket_type_t st, int peer_class) + void disallow(socket_type_t st, peer_class_t peer_class) { TORRENT_ASSERT(peer_class >= 0); TORRENT_ASSERT(peer_class < 32); @@ -100,7 +100,7 @@ namespace libtorrent if (st < 0 || st >= num_socket_types) return; m_peer_class_type_mask[st] &= ~(1 << peer_class); } - void allow(socket_type_t st, int peer_class) + void allow(socket_type_t st, peer_class_t peer_class) { TORRENT_ASSERT(peer_class >= 0); TORRENT_ASSERT(peer_class < 32); @@ -126,12 +126,19 @@ namespace libtorrent return peer_class_mask; } + friend bool operator==(peer_class_type_filter const& lhs + , peer_class_type_filter const& rhs) + { + return lhs.m_peer_class_type_mask == rhs.m_peer_class_type_mask + && lhs.m_peer_class_type == rhs.m_peer_class_type; + } + private: // maps socket type to a bitmask that's used to filter out // (mask) bits from the m_peer_class_filter. - boost::uint32_t m_peer_class_type_mask[num_socket_types]; + boost::array m_peer_class_type_mask; // peer class bitfield added based on socket type - boost::uint32_t m_peer_class_type[num_socket_types]; + boost::array m_peer_class_type; }; } diff --git a/include/libtorrent/session_handle.hpp b/include/libtorrent/session_handle.hpp index 7f8620ae6..798d0e4d5 100644 --- a/include/libtorrent/session_handle.hpp +++ b/include/libtorrent/session_handle.hpp @@ -634,7 +634,7 @@ namespace libtorrent // .. code:: c++ // // ip_filter f; - // int my_class = ses.create_peer_class("200.1.x.x IP range"); + // peer_class_t my_class = ses.create_peer_class("200.1.x.x IP range"); // f.add_rule(address_v4::from_string("200.1.1.0") // , address_v4::from_string("200.1.255.255") // , 1 << my_class); @@ -651,8 +651,11 @@ namespace libtorrent // The ``peer_class`` argument cannot be greater than 31. The bitmasks // representing peer classes in the ``peer_class_filter`` are 32 bits. // + // The ``get_peer_class_filter()`` function returns the current filter. + // // For more information, see peer-classes_. void set_peer_class_filter(ip_filter const& f); + ip_filter get_peer_class_filter() const; // Sets and gets the *peer class type filter*. This is controls automatic // peer class assignments to peers based on what kind of socket it is. @@ -667,8 +670,8 @@ namespace libtorrent // 3. peer-class type filter, adding classes // // For more information, see peer-classes_. - // TODO: add get_peer_class_type_filter() as well void set_peer_class_type_filter(peer_class_type_filter const& f); + peer_class_type_filter get_peer_class_type_filter() const; // Creates a new peer class (see peer-classes_) with the given name. The // returned integer is the new peer class identifier. Peer classes may diff --git a/src/session_handle.cpp b/src/session_handle.cpp index 563d8ad77..a795631aa 100644 --- a/src/session_handle.cpp +++ b/src/session_handle.cpp @@ -35,6 +35,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/aux_/session_call.hpp" #include "libtorrent/torrent.hpp" #include "libtorrent/lazy_entry.hpp" +#include "libtorrent/peer_class_type_filter.hpp" #ifdef __GNUC__ #pragma GCC diagnostic push @@ -611,11 +612,21 @@ namespace libtorrent TORRENT_ASYNC_CALL1(set_peer_class_filter, f); } + ip_filter session_handle::get_peer_class_filter() const + { + return TORRENT_SYNC_CALL_RET(ip_filter, get_peer_class_filter); + } + void session_handle::set_peer_class_type_filter(peer_class_type_filter const& f) { TORRENT_ASYNC_CALL1(set_peer_class_type_filter, f); } + peer_class_type_filter session_handle::get_peer_class_type_filter() const + { + return TORRENT_SYNC_CALL_RET(peer_class_type_filter, get_peer_class_type_filter); + } + int session_handle::create_peer_class(char const* name) { return TORRENT_SYNC_CALL_RET1(int, create_peer_class, name); diff --git a/test/test_peer_classes.cpp b/test/test_peer_classes.cpp index 541af1766..138c1acc1 100644 --- a/test/test_peer_classes.cpp +++ b/test/test_peer_classes.cpp @@ -34,6 +34,8 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/peer_class.hpp" #include "libtorrent/peer_class_set.hpp" #include "libtorrent/peer_class_type_filter.hpp" +#include "libtorrent/ip_filter.hpp" +#include "libtorrent/session.hpp" using namespace libtorrent; @@ -114,3 +116,37 @@ TORRENT_TEST(peer_class) TEST_CHECK(pool.at(id1) == NULL); } +TORRENT_TEST(session_peer_class_filter) +{ + using namespace libtorrent; + session ses; + peer_class_t my_class = ses.create_peer_class("200.1.x.x IP range"); + + ip_filter f; + f.add_rule(address_v4::from_string("200.1.1.0") + , address_v4::from_string("200.1.255.255") + , 1 << my_class); + ses.set_peer_class_filter(f); + +#if TORRENT_USE_IPV6 + TEST_CHECK(boost::get<0>(ses.get_peer_class_filter().export_filter()) + == boost::get<0>(f.export_filter())); +#else + TEST_CHECK(ses.get_peer_class_filter().export_filter() == f.export_filter()); +#endif +} + +TORRENT_TEST(session_peer_class_type_filter) +{ + using namespace libtorrent; + session ses; + peer_class_t my_class = ses.create_peer_class("all utp sockets"); + + peer_class_type_filter f; + f.add(peer_class_type_filter::utp_socket, my_class); + f.disallow(peer_class_type_filter::utp_socket, session::global_peer_class_id); + ses.set_peer_class_type_filter(f); + + TEST_CHECK(ses.get_peer_class_type_filter() == f); +} +