Skip to content

Commit 11f008e

Browse files
committed
use a strong typedef for port mapping id
1 parent ac914d4 commit 11f008e

15 files changed

+153
-139
lines changed

include/libtorrent/alert_types.hpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1401,7 +1401,7 @@ namespace libtorrent {
14011401
struct TORRENT_EXPORT portmap_error_alert final : alert
14021402
{
14031403
// internal
1404-
portmap_error_alert(aux::stack_allocator& alloc, int i
1404+
portmap_error_alert(aux::stack_allocator& alloc, port_mapping_t i
14051405
, portmap_transport t
14061406
, error_code const& e);
14071407

@@ -1413,7 +1413,7 @@ namespace libtorrent {
14131413

14141414
// refers to the mapping index of the port map that failed, i.e.
14151415
// the index returned from add_mapping().
1416-
int const mapping;
1416+
port_mapping_t const mapping;
14171417

14181418
// UPnP or NAT-PMP
14191419
portmap_transport map_transport;
@@ -1435,7 +1435,7 @@ namespace libtorrent {
14351435
struct TORRENT_EXPORT portmap_alert final : alert
14361436
{
14371437
// internal
1438-
portmap_alert(aux::stack_allocator& alloc, int i, int port
1438+
portmap_alert(aux::stack_allocator& alloc, port_mapping_t i, int port
14391439
, portmap_transport t, portmap_protocol protocol);
14401440

14411441
TORRENT_DEFINE_ALERT(portmap_alert, 51)
@@ -1445,7 +1445,7 @@ namespace libtorrent {
14451445

14461446
// refers to the mapping index of the port map that failed, i.e.
14471447
// the index returned from add_mapping().
1448-
int const mapping;
1448+
port_mapping_t const mapping;
14491449

14501450
// the external port allocated for the mapping.
14511451
int const external_port;

include/libtorrent/aux_/portmap.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ namespace aux {
5252
// int: protocol (UDP, TCP)
5353
// error_code: error, an empty error means success
5454
// int: transport is 0 for NAT-PMP and 1 for UPnP
55-
virtual void on_port_mapping(int mapping, address const& ip, int port
55+
virtual void on_port_mapping(port_mapping_t mapping, address const& ip, int port
5656
, portmap_protocol proto, error_code const& ec, portmap_transport transport) = 0;
5757
#ifndef TORRENT_DISABLE_LOGGING
5858
virtual bool should_log_portmap(portmap_transport transport) const = 0;

include/libtorrent/aux_/session_impl.hpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ namespace aux {
137137
{
138138
listen_socket_t()
139139
{
140-
tcp_port_mapping[0] = -1;
141-
tcp_port_mapping[1] = -1;
142-
udp_port_mapping[0] = -1;
143-
udp_port_mapping[1] = -1;
140+
tcp_port_mapping[0] = port_mapping_t{-1};
141+
tcp_port_mapping[1] = port_mapping_t{-1};
142+
udp_port_mapping[0] = port_mapping_t{-1};
143+
udp_port_mapping[1] = port_mapping_t{-1};
144144
}
145145

146146
// listen_socket_t should not be copied or moved because
@@ -180,8 +180,8 @@ namespace aux {
180180
int udp_external_port = 0;
181181

182182
// 0 is natpmp 1 is upnp
183-
int tcp_port_mapping[2];
184-
int udp_port_mapping[2];
183+
port_mapping_t tcp_port_mapping[2];
184+
port_mapping_t udp_port_mapping[2];
185185

186186
// indicates whether this is an SSL listen socket or not
187187
transport ssl = transport::plaintext;
@@ -433,7 +433,7 @@ namespace aux {
433433

434434
// called when a port mapping is successful, or a router returns
435435
// a failure to map a port
436-
void on_port_mapping(int mapping, address const& ip, int port
436+
void on_port_mapping(port_mapping_t mapping, address const& ip, int port
437437
, portmap_protocol proto, error_code const& ec
438438
, portmap_transport transport) override;
439439

@@ -633,9 +633,9 @@ namespace aux {
633633
void stop_natpmp();
634634
void stop_upnp();
635635

636-
int add_port_mapping(portmap_protocol t, int external_port
636+
port_mapping_t add_port_mapping(portmap_protocol t, int external_port
637637
, int local_port);
638-
void delete_port_mapping(int handle);
638+
void delete_port_mapping(port_mapping_t handle);
639639

640640
int next_port() const;
641641

include/libtorrent/natpmp.hpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ struct TORRENT_EXTRA_EXPORT natpmp
5555

5656
// maps the ports, if a port is set to 0
5757
// it will not be mapped
58-
int add_mapping(portmap_protocol p, int external_port, tcp::endpoint local_ep);
59-
void delete_mapping(int mapping_index);
60-
bool get_mapping(int mapping_index, int& local_port, int& external_port
58+
port_mapping_t add_mapping(portmap_protocol p, int external_port, tcp::endpoint local_ep);
59+
void delete_mapping(port_mapping_t mapping_index);
60+
bool get_mapping(port_mapping_t mapping_index, int& local_port, int& external_port
6161
, portmap_protocol& protocol) const;
6262

6363
void close();
@@ -66,15 +66,15 @@ struct TORRENT_EXTRA_EXPORT natpmp
6666

6767
std::shared_ptr<natpmp> self() { return shared_from_this(); }
6868

69-
void update_mapping(int i);
70-
void send_map_request(int i);
69+
void update_mapping(port_mapping_t i);
70+
void send_map_request(port_mapping_t i);
7171
void send_get_ip_address_request();
72-
void resend_request(int i, error_code const& e);
72+
void resend_request(port_mapping_t i, error_code const& e);
7373
void on_reply(error_code const& e
7474
, std::size_t bytes_transferred);
75-
void try_next_mapping(int i);
75+
void try_next_mapping(port_mapping_t i);
7676
void update_expiration_timer();
77-
void mapping_expired(error_code const& e, int i);
77+
void mapping_expired(error_code const& e, port_mapping_t i);
7878
void close_impl();
7979

8080
void disable(error_code const& ec);
@@ -100,15 +100,15 @@ struct TORRENT_EXTRA_EXPORT natpmp
100100

101101
aux::portmap_callback& m_callback;
102102

103-
aux::vector<mapping_t> m_mappings;
103+
aux::vector<mapping_t, port_mapping_t> m_mappings;
104104

105105
// the endpoint to the nat router
106106
udp::endpoint m_nat_endpoint;
107107

108108
// this is the mapping that is currently
109109
// being updated. It is -1 in case no
110110
// mapping is being updated at the moment
111-
int m_currently_mapping = -1;
111+
port_mapping_t m_currently_mapping{-1};
112112

113113
// current retry count
114114
int m_retry_count = 0;
@@ -134,7 +134,7 @@ struct TORRENT_EXTRA_EXPORT natpmp
134134
deadline_timer m_refresh_timer;
135135

136136
// the mapping index that will expire next
137-
int m_next_refresh = -1;
137+
port_mapping_t m_next_refresh{-1};
138138

139139
bool m_disabled = false;
140140

include/libtorrent/portmap.hpp

+7
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ POSSIBILITY OF SUCH DAMAGE.
3434
#define TORRENT_PORTMAP_HPP_INCLUDED
3535

3636
#include "libtorrent/config.hpp"
37+
#include "libtorrent/units.hpp"
3738

3839
namespace libtorrent {
40+
namespace aux {
41+
struct port_mapping_tag;
42+
}
3943

4044
enum class portmap_transport : std::uint8_t
4145
{
@@ -47,6 +51,9 @@ namespace libtorrent {
4751
none, tcp, udp
4852
};
4953

54+
// this type represents an index referring to a port mapping
55+
using port_mapping_t = aux::strong_typedef<int, aux::port_mapping_tag>;
56+
5057
}
5158

5259
#endif //TORRENT_PORTMAP_HPP_INCLUDED

include/libtorrent/session_handle.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1004,8 +1004,8 @@ namespace libtorrent {
10041004
// whichever is enabled. The return value is a handle referring to the
10051005
// port mapping that was just created. Pass it to delete_port_mapping()
10061006
// to remove it.
1007-
int add_port_mapping(portmap_protocol t, int external_port, int local_port);
1008-
void delete_port_mapping(int handle);
1007+
port_mapping_t add_port_mapping(portmap_protocol t, int external_port, int local_port);
1008+
void delete_port_mapping(port_mapping_t handle);
10091009

10101010
// This function is intended only for use by plugins. This type does
10111011
// not have a stable API and should be relied on as little as possible.

include/libtorrent/units.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ POSSIBILITY OF SUCH DAMAGE.
4343

4444
namespace libtorrent { namespace aux {
4545
template <typename Tag>
46-
struct difference_tag {};
46+
struct difference_tag;
4747

4848
template<typename UnderlyingType, typename Tag
4949
, typename Cond = typename std::enable_if<std::is_integral<UnderlyingType>::value>::type>

include/libtorrent/upnp.hpp

+13-13
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,13 @@ struct TORRENT_EXTRA_EXPORT upnp final
172172
// portmap_alert_ respectively. If The mapping fails immediately, the return value
173173
// is -1, which means failure. There will not be any error alert notification for
174174
// mappings that fail with a -1 return value.
175-
int add_mapping(portmap_protocol p, int external_port, tcp::endpoint local_ep);
175+
port_mapping_t add_mapping(portmap_protocol p, int external_port, tcp::endpoint local_ep);
176176

177177
// This function removes a port mapping. ``mapping_index`` is the index that refers
178178
// to the mapping you want to remove, which was returned from add_mapping().
179-
void delete_mapping(int mapping_index);
179+
void delete_mapping(port_mapping_t mapping_index);
180180

181-
bool get_mapping(int mapping_index, tcp::endpoint& local_ep, int& external_port
181+
bool get_mapping(port_mapping_t mapping_index, tcp::endpoint& local_ep, int& external_port
182182
, portmap_protocol& protocol) const;
183183

184184
void discover_device();
@@ -207,8 +207,8 @@ struct TORRENT_EXTRA_EXPORT upnp final
207207
, std::size_t bytes_transferred);
208208

209209
struct rootdevice;
210-
void next(rootdevice& d, int i);
211-
void update_map(rootdevice& d, int i);
210+
void next(rootdevice& d, port_mapping_t i);
211+
void update_map(rootdevice& d, port_mapping_t i);
212212

213213
void on_upnp_xml(error_code const& e
214214
, libtorrent::http_parser const& p, rootdevice& d
@@ -218,22 +218,22 @@ struct TORRENT_EXTRA_EXPORT upnp final
218218
, http_connection& c);
219219
void on_upnp_map_response(error_code const& e
220220
, libtorrent::http_parser const& p, rootdevice& d
221-
, int mapping, http_connection& c);
221+
, port_mapping_t mapping, http_connection& c);
222222
void on_upnp_unmap_response(error_code const& e
223223
, libtorrent::http_parser const& p, rootdevice& d
224-
, int mapping, http_connection& c);
224+
, port_mapping_t mapping, http_connection& c);
225225
void on_expire(error_code const& e);
226226

227227
void disable(error_code const& ec);
228-
void return_error(int mapping, int code);
228+
void return_error(port_mapping_t mapping, int code);
229229
#ifndef TORRENT_DISABLE_LOGGING
230230
bool should_log() const;
231231
void log(char const* msg, ...) const TORRENT_FORMAT(2,3);
232232
#endif
233233

234234
void get_ip_address(rootdevice& d);
235-
void delete_port_mapping(rootdevice& d, int i);
236-
void create_port_mapping(http_connection& c, rootdevice& d, int i);
235+
void delete_port_mapping(rootdevice& d, port_mapping_t i);
236+
void create_port_mapping(http_connection& c, rootdevice& d, port_mapping_t i);
237237
void post(upnp::rootdevice const& d, char const* soap
238238
, char const* soap_action);
239239

@@ -278,7 +278,7 @@ struct TORRENT_EXTRA_EXPORT upnp final
278278
// either the WANIP namespace or the WANPPP namespace
279279
std::string service_namespace;
280280

281-
aux::vector<mapping_t> mapping;
281+
aux::vector<mapping_t, port_mapping_t> mapping;
282282

283283
// this is the hostname, port and path
284284
// component of the url or the control_url
@@ -326,11 +326,11 @@ struct TORRENT_EXTRA_EXPORT upnp final
326326

327327
struct upnp_state_t
328328
{
329-
std::vector<global_mapping_t> mappings;
329+
aux::vector<global_mapping_t, port_mapping_t> mappings;
330330
std::set<rootdevice> devices;
331331
};
332332

333-
aux::vector<global_mapping_t> m_mappings;
333+
aux::vector<global_mapping_t, port_mapping_t> m_mappings;
334334

335335
std::string m_user_agent;
336336

src/alert.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,7 @@ namespace {
10721072
}
10731073

10741074
portmap_error_alert::portmap_error_alert(aux::stack_allocator&
1075-
, int i, portmap_transport const t, error_code const& e)
1075+
, port_mapping_t const i, portmap_transport const t, error_code const& e)
10761076
: mapping(i)
10771077
, map_transport(t)
10781078
, error(e)
@@ -1089,7 +1089,8 @@ namespace {
10891089
+ ": " + convert_from_native(error.message());
10901090
}
10911091

1092-
portmap_alert::portmap_alert(aux::stack_allocator&, int i, int port
1092+
portmap_alert::portmap_alert(aux::stack_allocator&, port_mapping_t const i
1093+
, int port
10931094
, portmap_transport const t
10941095
, portmap_protocol const proto)
10951096
: mapping(i)

0 commit comments

Comments
 (0)