Skip to content

Commit 9111d59

Browse files
committed
make the variables holding the source of external IPs type-safe
1 parent 60b74d2 commit 9111d59

9 files changed

+58
-37
lines changed

include/libtorrent/aux_/session_impl.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -658,10 +658,10 @@ namespace aux {
658658
, dht::msg const& request, entry& response) override;
659659

660660
void set_external_address(address const& ip
661-
, int source_type, address const& source) override;
661+
, ip_source_t source_type, address const& source) override;
662662
void set_external_address(tcp::endpoint const& local_endpoint
663663
, address const& ip
664-
, int source_type, address const& source) override;
664+
, ip_source_t source_type, address const& source) override;
665665
external_ip external_address() const override;
666666

667667
// used when posting synchronous function
@@ -754,7 +754,7 @@ namespace aux {
754754
void setup_socket_buffers(socket_type& s) override;
755755

756756
void set_external_address(std::shared_ptr<listen_socket_t> const& sock, address const& ip
757-
, int const source_type, address const& source);
757+
, ip_source_t const source_type, address const& source);
758758

759759
void interface_to_endpoints(std::string const& device, int const port
760760
, bool const ssl, std::vector<listen_endpoint_t>& eps);

include/libtorrent/aux_/session_interface.hpp

+15-9
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ POSSIBILITY OF SUCH DAMAGE.
4444
#include "libtorrent/aux_/vector.hpp"
4545
#include "libtorrent/aux_/listen_socket_handle.hpp"
4646
#include "libtorrent/session_types.hpp"
47+
#include "libtorrent/flags.hpp"
4748

4849
#include <functional>
4950
#include <memory>
@@ -101,6 +102,9 @@ namespace libtorrent { namespace aux {
101102
struct proxy_settings;
102103
struct session_settings;
103104

105+
struct ip_source_tag;
106+
using ip_source_t = flags::bitfield_flag<std::uint8_t, ip_source_tag>;
107+
104108
#if !defined TORRENT_DISABLE_LOGGING || TORRENT_USE_ASSERTS
105109
// This is the basic logging and debug interface offered by the session.
106110
// a release build with logging disabled (which is the default) will
@@ -131,21 +135,23 @@ namespace libtorrent { namespace aux {
131135
: session_logger
132136
#endif
133137
{
138+
134139
// TODO: 2 the IP voting mechanism should be factored out
135140
// to its own class, not part of the session
136-
enum
137-
{
138-
source_dht = 1,
139-
source_peer = 2,
140-
source_tracker = 4,
141-
source_router = 8
142-
};
141+
// and these constants should move too
142+
143+
// the logic in ip_voter relies on more reliable sources are represented
144+
// by more significant bits
145+
static constexpr ip_source_t source_dht = 1_bit;
146+
static constexpr ip_source_t source_peer = 2_bit;
147+
static constexpr ip_source_t source_tracker = 3_bit;
148+
static constexpr ip_source_t source_router = 4_bit;
143149

144150
virtual void set_external_address(address const& ip
145-
, int source_type, address const& source) = 0;
151+
, ip_source_t source_type, address const& source) = 0;
146152
virtual void set_external_address(tcp::endpoint const& local_endpoint
147153
, address const& ip
148-
, int source_type, address const& source) = 0;
154+
, ip_source_t source_type, address const& source) = 0;
149155
virtual external_ip external_address() const = 0;
150156

151157
virtual disk_interface& disk_thread() = 0;

include/libtorrent/ip_voter.hpp

+6-7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ POSSIBILITY OF SUCH DAMAGE.
3737
#include "libtorrent/address.hpp"
3838
#include "libtorrent/bloom_filter.hpp"
3939
#include "libtorrent/time.hpp" // for time_point
40+
#include "libtorrent/aux_/session_interface.hpp" // for ip_source_t
4041

4142
namespace libtorrent {
4243

@@ -48,7 +49,7 @@ namespace libtorrent {
4849

4950
// returns true if a different IP is the top vote now
5051
// i.e. we changed our idea of what our external IP is
51-
bool cast_vote(address const& ip, int source_type, address const& source);
52+
bool cast_vote(address const& ip, aux::ip_source_t source_type, address const& source);
5253

5354
address external_address() const { return m_external_address; }
5455

@@ -58,16 +59,14 @@ namespace libtorrent {
5859

5960
struct external_ip_t
6061
{
61-
external_ip_t(): sources(0), num_votes(0) {}
62-
63-
bool add_vote(sha1_hash const& k, int type);
62+
bool add_vote(sha1_hash const& k, aux::ip_source_t type);
6463

6564
// we want to sort descending
6665
bool operator<(external_ip_t const& rhs) const
6766
{
6867
if (num_votes > rhs.num_votes) return true;
6968
if (num_votes < rhs.num_votes) return false;
70-
return sources > rhs.sources;
69+
return static_cast<std::uint8_t>(sources) > static_cast<std::uint8_t>(rhs.sources);
7170
}
7271

7372
// this is a bloom filter of the IPs that have
@@ -76,9 +75,9 @@ namespace libtorrent {
7675
// this is the actual external address
7776
address addr;
7877
// a bitmask of sources the reporters have come from
79-
std::uint16_t sources;
78+
aux::ip_source_t sources{};
8079
// the total number of votes for this IP
81-
std::uint16_t num_votes;
80+
std::uint16_t num_votes = 0;
8281
};
8382

8483
// this is a bloom filter of all the IPs that have

simulation/setup_dht.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ namespace {
8080
std::shared_ptr<lt::aux::listen_socket_t> sim_listen_socket(tcp::endpoint ep)
8181
{
8282
auto ls = std::make_shared<lt::aux::listen_socket_t>();
83-
ls->external_address.cast_vote(ep.address(), 1, lt::address());
83+
ls->external_address.cast_vote(ep.address()
84+
, lt::aux::session_interface::source_dht, lt::address());
8485
ls->local_endpoint = ep;
8586
return ls;
8687
}

simulation/test_dht_rate_limit.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ TORRENT_TEST(dht_rate_limit)
105105
lt::udp_socket sock(dht_ios);
106106
obs o;
107107
auto ls = std::make_shared<lt::aux::listen_socket_t>();
108-
ls->external_address.cast_vote(address_v4::from_string("40.30.20.10"), 1, lt::address());
108+
ls->external_address.cast_vote(address_v4::from_string("40.30.20.10")
109+
, lt::aux::session_interface::source_dht, lt::address());
109110
ls->local_endpoint = tcp::endpoint(address_v4::from_string("40.30.20.10"), 8888);
110111
error_code ec;
111112
sock.bind(udp::endpoint(address_v4::from_string("40.30.20.10"), 8888), ec);
@@ -233,7 +234,8 @@ TORRENT_TEST(dht_delete_socket)
233234

234235
obs o;
235236
auto ls = std::make_shared<lt::aux::listen_socket_t>();
236-
ls->external_address.cast_vote(address_v4::from_string("40.30.20.10"), 1, lt::address());
237+
ls->external_address.cast_vote(address_v4::from_string("40.30.20.10")
238+
, lt::aux::session_interface::source_dht, lt::address());
237239
ls->local_endpoint = tcp::endpoint(address_v4::from_string("40.30.20.10"), 8888);
238240
dht::dht_settings dhtsett;
239241
counters cnt;

src/ip_voter.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ namespace libtorrent {
9898
}
9999

100100
bool ip_voter::cast_vote(address const& ip
101-
, int const source_type, address const& source)
101+
, aux::ip_source_t const source_type, address const& source)
102102
{
103103
if (is_any(ip)) return false;
104104
if (is_local(ip)) return false;
@@ -164,7 +164,8 @@ namespace libtorrent {
164164
return true;
165165
}
166166

167-
bool ip_voter::external_ip_t::add_vote(sha1_hash const& k, int type)
167+
bool ip_voter::external_ip_t::add_vote(sha1_hash const& k
168+
, aux::ip_source_t const type)
168169
{
169170
sources |= type;
170171
if (voters.find(k)) return false;

src/session_impl.cpp

+16-8
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ namespace libtorrent {
189189

190190
namespace aux {
191191

192+
constexpr ip_source_t session_interface::source_dht;
193+
constexpr ip_source_t session_interface::source_peer;
194+
constexpr ip_source_t session_interface::source_tracker;
195+
constexpr ip_source_t session_interface::source_router;
196+
192197
std::vector<std::shared_ptr<listen_socket_t>>::iterator partition_listen_sockets(
193198
std::vector<listen_endpoint_t>& eps
194199
, std::vector<std::shared_ptr<listen_socket_t>>& sockets)
@@ -6638,13 +6643,14 @@ namespace {
66386643
}
66396644

66406645
// this is the DHT observer version. DHT is the implied source
6641-
void session_impl::set_external_address(aux::listen_socket_handle const& iface, address const& ip
6642-
, address const& source)
6646+
void session_impl::set_external_address(aux::listen_socket_handle const& iface
6647+
, address const& ip, address const& source)
66436648
{
66446649
auto i = iface.m_sock.lock();
66456650
TORRENT_ASSERT(i);
66466651
if (!i) return;
6647-
set_external_address(std::static_pointer_cast<listen_socket_t>(i), ip, source_dht, source);
6652+
set_external_address(std::static_pointer_cast<listen_socket_t>(i), ip
6653+
, source_dht, source);
66486654
}
66496655

66506656
void session_impl::get_peers(sha1_hash const& ih)
@@ -6738,7 +6744,7 @@ namespace {
67386744
}
67396745

67406746
void session_impl::set_external_address(address const& ip
6741-
, int const source_type, address const& source)
6747+
, ip_source_t const source_type, address const& source)
67426748
{
67436749
// for now, just pick the first socket with a matching address family
67446750
// TODO: remove this function once all callers are updated to specify a listen socket
@@ -6754,7 +6760,7 @@ namespace {
67546760

67556761
void session_impl::set_external_address(
67566762
tcp::endpoint const& local_endpoint, address const& ip
6757-
, int const source_type, address const& source)
6763+
, ip_source_t const source_type, address const& source)
67586764
{
67596765
auto sock = std::find_if(m_listen_sockets.begin(), m_listen_sockets.end()
67606766
, [&](std::shared_ptr<listen_socket_t> const& v) { return v->local_endpoint == local_endpoint; });
@@ -6764,13 +6770,15 @@ namespace {
67646770
}
67656771

67666772
void session_impl::set_external_address(std::shared_ptr<listen_socket_t> const& sock
6767-
, address const& ip, int const source_type, address const& source)
6773+
, address const& ip, ip_source_t const source_type, address const& source)
67686774
{
67696775
#ifndef TORRENT_DISABLE_LOGGING
67706776
if (should_log())
67716777
{
6772-
session_log(": set_external_address(%s, %d, %s)", print_address(ip).c_str()
6773-
, source_type, print_address(source).c_str());
6778+
session_log(": set_external_address(%s, %d, %s)"
6779+
, print_address(ip).c_str()
6780+
, static_cast<std::uint8_t>(source_type)
6781+
, print_address(source).c_str());
67746782
}
67756783
#endif
67766784

test/test_dht.cpp

+8-4
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,17 @@ std::shared_ptr<aux::listen_socket_t> dummy_listen_socket(udp::endpoint src)
128128
{
129129
auto ret = std::make_shared<aux::listen_socket_t>();
130130
ret->local_endpoint = tcp::endpoint(src.address(), src.port());
131-
ret->external_address.cast_vote(src.address(), 1, rand_v4());
131+
ret->external_address.cast_vote(src.address()
132+
, aux::session_interface::source_dht, rand_v4());
132133
return ret;
133134
}
134135

135136
std::shared_ptr<aux::listen_socket_t> dummy_listen_socket4()
136137
{
137138
auto ret = std::make_shared<aux::listen_socket_t>();
138139
ret->local_endpoint = tcp::endpoint(addr4("192.168.4.1"), 6881);
139-
ret->external_address.cast_vote(addr4("236.0.0.1"), 1, rand_v4());
140+
ret->external_address.cast_vote(addr4("236.0.0.1")
141+
, aux::session_interface::source_dht, rand_v4());
140142
return ret;
141143
}
142144

@@ -145,7 +147,8 @@ std::shared_ptr<aux::listen_socket_t> dummy_listen_socket6()
145147
{
146148
auto ret = std::make_shared<aux::listen_socket_t>();
147149
ret->local_endpoint = tcp::endpoint(addr6("2002::1"), 6881);
148-
ret->external_address.cast_vote(addr6("2002::1"), 1, rand_v6());
150+
ret->external_address.cast_vote(addr6("2002::1")
151+
, aux::session_interface::source_dht, rand_v6());
149152
return ret;
150153
}
151154
#endif
@@ -519,7 +522,8 @@ struct obs : dht::dht_observer
519522
void set_external_address(aux::listen_socket_handle const& s, address const& addr
520523
, address const& source) override
521524
{
522-
s.get()->external_address.cast_vote(addr, 1, rand_v4());
525+
s.get()->external_address.cast_vote(addr
526+
, aux::session_interface::source_dht, rand_v4());
523527
}
524528

525529
void get_peers(sha1_hash const& ih) override {}

test/test_ip_voter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ using namespace lt;
4444

4545
bool cast_vote(ip_voter& ipv, address ext_ip, address voter)
4646
{
47-
bool new_ip = ipv.cast_vote(ext_ip, 1, voter);
47+
bool new_ip = ipv.cast_vote(ext_ip, aux::session_interface::source_dht, voter);
4848
std::printf("%15s -> %-15s\n"
4949
, print_address(voter).c_str()
5050
, print_address(ext_ip).c_str());

0 commit comments

Comments
 (0)