use a strong typedef for port mapping id

This commit is contained in:
arvidn 2017-08-31 00:49:49 +02:00 committed by Arvid Norberg
parent ac914d4415
commit 11f008e90e
15 changed files with 153 additions and 139 deletions

View File

@ -1401,7 +1401,7 @@ namespace libtorrent {
struct TORRENT_EXPORT portmap_error_alert final : alert
{
// internal
portmap_error_alert(aux::stack_allocator& alloc, int i
portmap_error_alert(aux::stack_allocator& alloc, port_mapping_t i
, portmap_transport t
, error_code const& e);
@ -1413,7 +1413,7 @@ namespace libtorrent {
// refers to the mapping index of the port map that failed, i.e.
// the index returned from add_mapping().
int const mapping;
port_mapping_t const mapping;
// UPnP or NAT-PMP
portmap_transport map_transport;
@ -1435,7 +1435,7 @@ namespace libtorrent {
struct TORRENT_EXPORT portmap_alert final : alert
{
// internal
portmap_alert(aux::stack_allocator& alloc, int i, int port
portmap_alert(aux::stack_allocator& alloc, port_mapping_t i, int port
, portmap_transport t, portmap_protocol protocol);
TORRENT_DEFINE_ALERT(portmap_alert, 51)
@ -1445,7 +1445,7 @@ namespace libtorrent {
// refers to the mapping index of the port map that failed, i.e.
// the index returned from add_mapping().
int const mapping;
port_mapping_t const mapping;
// the external port allocated for the mapping.
int const external_port;

View File

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

View File

@ -137,10 +137,10 @@ namespace aux {
{
listen_socket_t()
{
tcp_port_mapping[0] = -1;
tcp_port_mapping[1] = -1;
udp_port_mapping[0] = -1;
udp_port_mapping[1] = -1;
tcp_port_mapping[0] = port_mapping_t{-1};
tcp_port_mapping[1] = port_mapping_t{-1};
udp_port_mapping[0] = port_mapping_t{-1};
udp_port_mapping[1] = port_mapping_t{-1};
}
// listen_socket_t should not be copied or moved because
@ -180,8 +180,8 @@ namespace aux {
int udp_external_port = 0;
// 0 is natpmp 1 is upnp
int tcp_port_mapping[2];
int udp_port_mapping[2];
port_mapping_t tcp_port_mapping[2];
port_mapping_t udp_port_mapping[2];
// indicates whether this is an SSL listen socket or not
transport ssl = transport::plaintext;
@ -433,7 +433,7 @@ namespace aux {
// called when a port mapping is successful, or a router returns
// a failure to map a port
void on_port_mapping(int mapping, address const& ip, int port
void on_port_mapping(port_mapping_t mapping, address const& ip, int port
, portmap_protocol proto, error_code const& ec
, portmap_transport transport) override;
@ -633,9 +633,9 @@ namespace aux {
void stop_natpmp();
void stop_upnp();
int add_port_mapping(portmap_protocol t, int external_port
port_mapping_t add_port_mapping(portmap_protocol t, int external_port
, int local_port);
void delete_port_mapping(int handle);
void delete_port_mapping(port_mapping_t handle);
int next_port() const;

View File

@ -55,9 +55,9 @@ struct TORRENT_EXTRA_EXPORT natpmp
// maps the ports, if a port is set to 0
// it will not be mapped
int add_mapping(portmap_protocol p, int external_port, tcp::endpoint local_ep);
void delete_mapping(int mapping_index);
bool get_mapping(int mapping_index, int& local_port, int& external_port
port_mapping_t add_mapping(portmap_protocol p, int external_port, tcp::endpoint local_ep);
void delete_mapping(port_mapping_t mapping_index);
bool get_mapping(port_mapping_t mapping_index, int& local_port, int& external_port
, portmap_protocol& protocol) const;
void close();
@ -66,15 +66,15 @@ private:
std::shared_ptr<natpmp> self() { return shared_from_this(); }
void update_mapping(int i);
void send_map_request(int i);
void update_mapping(port_mapping_t i);
void send_map_request(port_mapping_t i);
void send_get_ip_address_request();
void resend_request(int i, error_code const& e);
void resend_request(port_mapping_t i, error_code const& e);
void on_reply(error_code const& e
, std::size_t bytes_transferred);
void try_next_mapping(int i);
void try_next_mapping(port_mapping_t i);
void update_expiration_timer();
void mapping_expired(error_code const& e, int i);
void mapping_expired(error_code const& e, port_mapping_t i);
void close_impl();
void disable(error_code const& ec);
@ -100,7 +100,7 @@ private:
aux::portmap_callback& m_callback;
aux::vector<mapping_t> m_mappings;
aux::vector<mapping_t, port_mapping_t> m_mappings;
// the endpoint to the nat router
udp::endpoint m_nat_endpoint;
@ -108,7 +108,7 @@ private:
// this is the mapping that is currently
// being updated. It is -1 in case no
// mapping is being updated at the moment
int m_currently_mapping = -1;
port_mapping_t m_currently_mapping{-1};
// current retry count
int m_retry_count = 0;
@ -134,7 +134,7 @@ private:
deadline_timer m_refresh_timer;
// the mapping index that will expire next
int m_next_refresh = -1;
port_mapping_t m_next_refresh{-1};
bool m_disabled = false;

View File

@ -34,8 +34,12 @@ POSSIBILITY OF SUCH DAMAGE.
#define TORRENT_PORTMAP_HPP_INCLUDED
#include "libtorrent/config.hpp"
#include "libtorrent/units.hpp"
namespace libtorrent {
namespace aux {
struct port_mapping_tag;
}
enum class portmap_transport : std::uint8_t
{
@ -47,6 +51,9 @@ namespace libtorrent {
none, tcp, udp
};
// this type represents an index referring to a port mapping
using port_mapping_t = aux::strong_typedef<int, aux::port_mapping_tag>;
}
#endif //TORRENT_PORTMAP_HPP_INCLUDED

View File

@ -1004,8 +1004,8 @@ namespace libtorrent {
// whichever is enabled. The return value is a handle referring to the
// port mapping that was just created. Pass it to delete_port_mapping()
// to remove it.
int add_port_mapping(portmap_protocol t, int external_port, int local_port);
void delete_port_mapping(int handle);
port_mapping_t add_port_mapping(portmap_protocol t, int external_port, int local_port);
void delete_port_mapping(port_mapping_t handle);
// This function is intended only for use by plugins. This type does
// not have a stable API and should be relied on as little as possible.

View File

@ -43,7 +43,7 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent { namespace aux {
template <typename Tag>
struct difference_tag {};
struct difference_tag;
template<typename UnderlyingType, typename Tag
, typename Cond = typename std::enable_if<std::is_integral<UnderlyingType>::value>::type>

View File

@ -172,13 +172,13 @@ struct TORRENT_EXTRA_EXPORT upnp final
// portmap_alert_ respectively. If The mapping fails immediately, the return value
// is -1, which means failure. There will not be any error alert notification for
// mappings that fail with a -1 return value.
int add_mapping(portmap_protocol p, int external_port, tcp::endpoint local_ep);
port_mapping_t add_mapping(portmap_protocol p, int external_port, tcp::endpoint local_ep);
// This function removes a port mapping. ``mapping_index`` is the index that refers
// to the mapping you want to remove, which was returned from add_mapping().
void delete_mapping(int mapping_index);
void delete_mapping(port_mapping_t mapping_index);
bool get_mapping(int mapping_index, tcp::endpoint& local_ep, int& external_port
bool get_mapping(port_mapping_t mapping_index, tcp::endpoint& local_ep, int& external_port
, portmap_protocol& protocol) const;
void discover_device();
@ -207,8 +207,8 @@ private:
, std::size_t bytes_transferred);
struct rootdevice;
void next(rootdevice& d, int i);
void update_map(rootdevice& d, int i);
void next(rootdevice& d, port_mapping_t i);
void update_map(rootdevice& d, port_mapping_t i);
void on_upnp_xml(error_code const& e
, libtorrent::http_parser const& p, rootdevice& d
@ -218,22 +218,22 @@ private:
, http_connection& c);
void on_upnp_map_response(error_code const& e
, libtorrent::http_parser const& p, rootdevice& d
, int mapping, http_connection& c);
, port_mapping_t mapping, http_connection& c);
void on_upnp_unmap_response(error_code const& e
, libtorrent::http_parser const& p, rootdevice& d
, int mapping, http_connection& c);
, port_mapping_t mapping, http_connection& c);
void on_expire(error_code const& e);
void disable(error_code const& ec);
void return_error(int mapping, int code);
void return_error(port_mapping_t mapping, int code);
#ifndef TORRENT_DISABLE_LOGGING
bool should_log() const;
void log(char const* msg, ...) const TORRENT_FORMAT(2,3);
#endif
void get_ip_address(rootdevice& d);
void delete_port_mapping(rootdevice& d, int i);
void create_port_mapping(http_connection& c, rootdevice& d, int i);
void delete_port_mapping(rootdevice& d, port_mapping_t i);
void create_port_mapping(http_connection& c, rootdevice& d, port_mapping_t i);
void post(upnp::rootdevice const& d, char const* soap
, char const* soap_action);
@ -278,7 +278,7 @@ private:
// either the WANIP namespace or the WANPPP namespace
std::string service_namespace;
aux::vector<mapping_t> mapping;
aux::vector<mapping_t, port_mapping_t> mapping;
// this is the hostname, port and path
// component of the url or the control_url
@ -326,11 +326,11 @@ private:
struct upnp_state_t
{
std::vector<global_mapping_t> mappings;
aux::vector<global_mapping_t, port_mapping_t> mappings;
std::set<rootdevice> devices;
};
aux::vector<global_mapping_t> m_mappings;
aux::vector<global_mapping_t, port_mapping_t> m_mappings;
std::string m_user_agent;

View File

@ -1072,7 +1072,7 @@ namespace {
}
portmap_error_alert::portmap_error_alert(aux::stack_allocator&
, int i, portmap_transport const t, error_code const& e)
, port_mapping_t const i, portmap_transport const t, error_code const& e)
: mapping(i)
, map_transport(t)
, error(e)
@ -1089,7 +1089,8 @@ namespace {
+ ": " + convert_from_native(error.message());
}
portmap_alert::portmap_alert(aux::stack_allocator&, int i, int port
portmap_alert::portmap_alert(aux::stack_allocator&, port_mapping_t const i
, int port
, portmap_transport const t
, portmap_protocol const proto)
: mapping(i)

View File

@ -134,7 +134,7 @@ void natpmp::start()
|| i->act != portmap_action::none)
continue;
i->act = portmap_action::add;
update_mapping(int(i - m_mappings.begin()));
update_mapping(port_mapping_t(int(i - m_mappings.begin())));
}
}
@ -155,13 +155,13 @@ void natpmp::send_get_ip_address_request()
m_socket.send_to(boost::asio::buffer(buf, sizeof(buf)), m_nat_endpoint, 0, ec);
}
bool natpmp::get_mapping(int const index, int& local_port
bool natpmp::get_mapping(port_mapping_t const index, int& local_port
, int& external_port, portmap_protocol& protocol) const
{
TORRENT_ASSERT(is_single_thread());
TORRENT_ASSERT(index < int(m_mappings.size()) && index >= 0);
if (index >= int(m_mappings.size()) || index < 0) return false;
TORRENT_ASSERT(index < m_mappings.end_index() && index >= port_mapping_t{});
if (index >= m_mappings.end_index() || index < port_mapping_t{}) return false;
mapping_t const& m = m_mappings[index];
if (m.protocol == portmap_protocol::none) return false;
local_port = m.local_port;
@ -216,19 +216,19 @@ void natpmp::disable(error_code const& ec)
if (i->protocol == portmap_protocol::none) continue;
portmap_protocol const proto = i->protocol;
i->protocol = portmap_protocol::none;
int const index = int(i - m_mappings.begin());
port_mapping_t const index(static_cast<int>(i - m_mappings.begin()));
m_callback.on_port_mapping(index, address(), 0, proto, ec
, portmap_transport::natpmp);
}
close_impl();
}
void natpmp::delete_mapping(int const index)
void natpmp::delete_mapping(port_mapping_t const index)
{
TORRENT_ASSERT(is_single_thread());
TORRENT_ASSERT(index < int(m_mappings.size()) && index >= 0);
if (index >= int(m_mappings.size()) || index < 0) return;
TORRENT_ASSERT(index < m_mappings.end_index() && index >= port_mapping_t{});
if (index >= m_mappings.end_index() || index < port_mapping_t{}) return;
mapping_t& m = m_mappings[index];
if (m.protocol == portmap_protocol::none) return;
@ -243,12 +243,12 @@ void natpmp::delete_mapping(int const index)
update_mapping(index);
}
int natpmp::add_mapping(portmap_protocol const p, int const external_port
port_mapping_t natpmp::add_mapping(portmap_protocol const p, int const external_port
, tcp::endpoint const local_ep)
{
TORRENT_ASSERT(is_single_thread());
if (m_disabled) return -1;
if (m_disabled) return port_mapping_t{-1};
auto i = std::find_if(m_mappings.begin()
, m_mappings.end(), [] (mapping_t const& m) { return m.protocol == portmap_protocol::none; });
@ -262,21 +262,21 @@ int natpmp::add_mapping(portmap_protocol const p, int const external_port
i->local_port = local_ep.port();
i->act = portmap_action::add;
int const mapping_index = int(i - m_mappings.begin());
port_mapping_t const mapping_index(static_cast<int>(i - m_mappings.begin()));
#ifndef TORRENT_DISABLE_LOGGING
mapping_log("add",*i);
#endif
update_mapping(mapping_index);
return mapping_index;
return port_mapping_t{mapping_index};
}
void natpmp::try_next_mapping(int const i)
void natpmp::try_next_mapping(port_mapping_t const i)
{
TORRENT_ASSERT(is_single_thread());
if (i < int(m_mappings.size()) - 1)
if (i < prev(m_mappings.end_index()))
{
update_mapping(i + 1);
update_mapping(next(i));
return;
}
@ -296,13 +296,13 @@ void natpmp::try_next_mapping(int const i)
return;
}
update_mapping(int(m - m_mappings.begin()));
update_mapping(port_mapping_t(static_cast<int>(m - m_mappings.begin())));
}
void natpmp::update_mapping(int const i)
void natpmp::update_mapping(port_mapping_t const i)
{
TORRENT_ASSERT(is_single_thread());
if (i == int(m_mappings.size()))
if (i == m_mappings.end_index())
{
if (m_abort)
{
@ -326,7 +326,7 @@ void natpmp::update_mapping(int const i)
return;
}
if (m_currently_mapping == -1)
if (m_currently_mapping == port_mapping_t{-1})
{
// the socket is not currently in use
// send out a mapping request
@ -335,12 +335,12 @@ void natpmp::update_mapping(int const i)
}
}
void natpmp::send_map_request(int const i)
void natpmp::send_map_request(port_mapping_t const i)
{
TORRENT_ASSERT(is_single_thread());
using namespace libtorrent::detail;
TORRENT_ASSERT(m_currently_mapping == -1
TORRENT_ASSERT(m_currently_mapping == port_mapping_t{-1}
|| m_currently_mapping == i);
m_currently_mapping = i;
mapping_t& m = m_mappings[i];
@ -360,7 +360,7 @@ void natpmp::send_map_request(int const i)
{
log("==> port map [ mapping: %d action: %s"
" proto: %s local: %u external: %u ttl: %u ]"
, i, to_string(m.act)
, static_cast<int>(i), to_string(m.act)
, to_string(m.protocol)
, m.local_port, m.external_port, ttl);
}
@ -375,7 +375,7 @@ void natpmp::send_map_request(int const i)
// when we're shutting down, ignore the
// responses and just remove all mappings
// immediately
m_currently_mapping = -1;
m_currently_mapping = port_mapping_t{-1};
m.act = portmap_action::none;
try_next_mapping(i);
}
@ -389,7 +389,7 @@ void natpmp::send_map_request(int const i)
}
}
void natpmp::resend_request(int const i, error_code const& e)
void natpmp::resend_request(port_mapping_t const i, error_code const& e)
{
TORRENT_ASSERT(is_single_thread());
COMPLETE_ASYNC("natpmp::resend_request");
@ -400,7 +400,7 @@ void natpmp::resend_request(int const i, error_code const& e)
// to the next mapping
if (m_retry_count >= 9 || m_abort)
{
m_currently_mapping = -1;
m_currently_mapping = port_mapping_t{-1};
m_mappings[i].act = portmap_action::none;
// try again in two hours
m_mappings[i].expires = aux::time_now() + hours(2);
@ -518,7 +518,7 @@ void natpmp::on_reply(error_code const& e
#endif
mapping_t* m = nullptr;
int index = -1;
port_mapping_t index{-1};
for (auto i = m_mappings.begin(), end(m_mappings.end()); i != end; ++i)
{
if (private_port != i->local_port) continue;
@ -526,7 +526,7 @@ void natpmp::on_reply(error_code const& e
if (!i->map_sent) continue;
if (!i->outstanding_request) continue;
m = &*i;
index = int(i - m_mappings.begin());
index = port_mapping_t(static_cast<int>(i - m_mappings.begin()));
break;
}
@ -572,19 +572,19 @@ void natpmp::on_reply(error_code const& e
m->expires = aux::time_now() + hours(2);
portmap_protocol const proto = m->protocol;
m_callback.on_port_mapping(index, address(), 0, proto
m_callback.on_port_mapping(port_mapping_t{index}, address(), 0, proto
, ev, portmap_transport::natpmp);
}
else if (m->act == portmap_action::add)
{
portmap_protocol const proto = m->protocol;
m_callback.on_port_mapping(index, m_external_ip, m->external_port, proto
m_callback.on_port_mapping(port_mapping_t{index}, m_external_ip, m->external_port, proto
, errors::no_error, portmap_transport::natpmp);
}
if (m_abort) return;
m_currently_mapping = -1;
m_currently_mapping = port_mapping_t{-1};
m->act = portmap_action::none;
m_send_timer.cancel(ec);
update_expiration_timer();
@ -598,20 +598,20 @@ void natpmp::update_expiration_timer()
time_point const now = aux::time_now() + milliseconds(100);
time_point min_expire = now + seconds(3600);
int min_index = -1;
port_mapping_t min_index{-1};
for (std::vector<mapping_t>::iterator i = m_mappings.begin()
, end(m_mappings.end()); i != end; ++i)
{
if (i->protocol == portmap_protocol::none
|| i->act != portmap_action::none) continue;
int const index = int(i - m_mappings.begin());
port_mapping_t const index(static_cast<int>(i - m_mappings.begin()));
if (i->expires < now)
{
#ifndef TORRENT_DISABLE_LOGGING
log("mapping %u expired", index);
log("mapping %u expired", static_cast<int>(index));
#endif
i->act = portmap_action::add;
if (m_next_refresh == index) m_next_refresh = -1;
if (m_next_refresh == index) m_next_refresh = port_mapping_t{-1};
update_mapping(index);
}
else if (i->expires < min_expire)
@ -624,14 +624,14 @@ void natpmp::update_expiration_timer()
// this is already the mapping we're waiting for
if (m_next_refresh == min_index) return;
if (min_index >= 0)
if (min_index >= port_mapping_t{})
{
#ifndef TORRENT_DISABLE_LOGGING
log("next expiration [ idx: %d ttl: %" PRId64 " ]"
, min_index, total_seconds(min_expire - aux::time_now()));
, static_cast<int>(min_index), total_seconds(min_expire - aux::time_now()));
#endif
error_code ec;
if (m_next_refresh >= 0) m_refresh_timer.cancel(ec);
if (m_next_refresh >= port_mapping_t{}) m_refresh_timer.cancel(ec);
ADD_OUTSTANDING_ASYNC("natpmp::mapping_expired");
m_refresh_timer.expires_from_now(min_expire - now, ec);
@ -640,16 +640,16 @@ void natpmp::update_expiration_timer()
}
}
void natpmp::mapping_expired(error_code const& e, int const i)
void natpmp::mapping_expired(error_code const& e, port_mapping_t const i)
{
TORRENT_ASSERT(is_single_thread());
COMPLETE_ASYNC("natpmp::mapping_expired");
if (e) return;
#ifndef TORRENT_DISABLE_LOGGING
log("mapping %u expired", i);
log("mapping %u expired", static_cast<int>(i));
#endif
m_mappings[i].act = portmap_action::add;
if (m_next_refresh == i) m_next_refresh = -1;
if (m_next_refresh == i) m_next_refresh = port_mapping_t{-1};
update_mapping(i);
}
@ -674,8 +674,8 @@ void natpmp::close_impl()
}
error_code ec;
m_refresh_timer.cancel(ec);
m_currently_mapping = -1;
update_mapping(0);
m_currently_mapping = port_mapping_t{-1};
update_mapping(port_mapping_t{});
}
} // namespace libtorrent

View File

@ -1201,13 +1201,13 @@ namespace {
}
#endif // TORRENT_NO_DEPRECATE
int session_handle::add_port_mapping(portmap_protocol const t
port_mapping_t session_handle::add_port_mapping(portmap_protocol const t
, int external_port, int local_port)
{
return sync_call_ret<int>(&session_impl::add_port_mapping, t, external_port, local_port);
return sync_call_ret<port_mapping_t>(&session_impl::add_port_mapping, t, external_port, local_port);
}
void session_handle::delete_port_mapping(int handle)
void session_handle::delete_port_mapping(port_mapping_t handle)
{
async_call(&session_impl::delete_port_mapping, handle);
}

View File

@ -2167,10 +2167,10 @@ namespace {
namespace {
template <typename MapProtocol, typename ProtoType, typename EndpointType>
void map_port(MapProtocol& m, ProtoType protocol, EndpointType const& ep
, int& map_handle)
, port_mapping_t& map_handle)
{
if (map_handle != -1) m.delete_mapping(map_handle);
map_handle = -1;
if (map_handle != port_mapping_t{-1}) m.delete_mapping(map_handle);
map_handle = port_mapping_t{-1};
#if TORRENT_USE_IPV6
address const addr = ep.address();
@ -5423,19 +5423,20 @@ namespace {
namespace {
bool find_tcp_port_mapping(portmap_transport const transport
, int mapping, std::shared_ptr<listen_socket_t> const& ls)
, port_mapping_t mapping, std::shared_ptr<listen_socket_t> const& ls)
{
return ls->tcp_port_mapping[static_cast<int>(transport)] == mapping;
}
bool find_udp_port_mapping(portmap_transport const transport
, int mapping, std::shared_ptr<listen_socket_t> const& ls)
, port_mapping_t mapping, std::shared_ptr<listen_socket_t> const& ls)
{
return ls->udp_port_mapping[static_cast<int>(transport)] == mapping;
}
}
void session_impl::on_port_mapping(int mapping, address const& ip, int port
void session_impl::on_port_mapping(port_mapping_t const mapping
, address const& ip, int port
, portmap_protocol const proto, error_code const& ec
, portmap_transport const transport)
{
@ -6601,11 +6602,11 @@ namespace {
return m_upnp.get();
}
int session_impl::add_port_mapping(portmap_protocol const t
port_mapping_t session_impl::add_port_mapping(portmap_protocol const t
, int const external_port
, int const local_port)
{
int ret = 0;
port_mapping_t ret{-1};
if (m_upnp) ret = m_upnp->add_mapping(t, external_port
, tcp::endpoint({}, static_cast<std::uint16_t>(local_port)));
if (m_natpmp) ret = m_natpmp->add_mapping(t, external_port
@ -6613,7 +6614,7 @@ namespace {
return ret;
}
void session_impl::delete_port_mapping(int handle)
void session_impl::delete_port_mapping(port_mapping_t handle)
{
if (m_upnp) m_upnp->delete_mapping(handle);
if (m_natpmp) m_natpmp->delete_mapping(handle);
@ -6633,8 +6634,8 @@ namespace {
m_natpmp->close();
for (auto& s : m_listen_sockets)
{
s->tcp_port_mapping[0] = -1;
s->udp_port_mapping[0] = -1;
s->tcp_port_mapping[0] = port_mapping_t{-1};
s->udp_port_mapping[0] = port_mapping_t{-1};
}
m_natpmp.reset();
@ -6647,8 +6648,8 @@ namespace {
m_upnp->close();
for (auto& s : m_listen_sockets)
{
s->tcp_port_mapping[1] = -1;
s->udp_port_mapping[1] = -1;
s->tcp_port_mapping[1] = port_mapping_t{-1};
s->udp_port_mapping[1] = port_mapping_t{-1};
}
m_upnp.reset();
}

View File

@ -181,7 +181,7 @@ void upnp::discover_device_impl()
}
// returns a reference to a mapping or -1 on failure
int upnp::add_mapping(portmap_protocol const p, int const external_port
port_mapping_t upnp::add_mapping(portmap_protocol const p, int const external_port
, tcp::endpoint const local_ep)
{
TORRENT_ASSERT(is_single_thread());
@ -194,7 +194,7 @@ int upnp::add_mapping(portmap_protocol const p, int const external_port
, external_port
, print_endpoint(local_ep).c_str(), m_disabled ? "DISABLED": "");
#endif
if (m_disabled) return -1;
if (m_disabled) return port_mapping_t{-1};
auto mapping_it = std::find_if(m_mappings.begin(), m_mappings.end()
, [](global_mapping_t const& m) { return m.protocol == portmap_protocol::none; });
@ -209,7 +209,7 @@ int upnp::add_mapping(portmap_protocol const p, int const external_port
mapping_it->external_port = external_port;
mapping_it->local_ep = local_ep;
int const mapping_index = int(mapping_it - m_mappings.begin());
port_mapping_t const mapping_index{static_cast<int>(mapping_it - m_mappings.begin())};
for (auto const& dev : m_devices)
{
@ -217,7 +217,7 @@ int upnp::add_mapping(portmap_protocol const p, int const external_port
TORRENT_ASSERT(d.magic == 1337);
if (d.mapping.end_index() <= mapping_index)
d.mapping.resize(mapping_index + 1);
d.mapping.resize(static_cast<int>(mapping_index) + 1);
mapping_t& m = d.mapping[mapping_index];
m.act = portmap_action::add;
@ -228,14 +228,14 @@ int upnp::add_mapping(portmap_protocol const p, int const external_port
if (!d.service_namespace.empty()) update_map(d, mapping_index);
}
return mapping_index;
return port_mapping_t{mapping_index};
}
void upnp::delete_mapping(int const mapping)
void upnp::delete_mapping(port_mapping_t const mapping)
{
TORRENT_ASSERT(is_single_thread());
if (mapping >= int(m_mappings.size())) return;
if (mapping >= m_mappings.end_index()) return;
global_mapping_t const& m = m_mappings[mapping];
@ -259,14 +259,14 @@ void upnp::delete_mapping(int const mapping)
}
}
bool upnp::get_mapping(int const index
bool upnp::get_mapping(port_mapping_t const index
, tcp::endpoint& local_ep
, int& external_port
, portmap_protocol& protocol) const
{
TORRENT_ASSERT(is_single_thread());
TORRENT_ASSERT(index < int(m_mappings.size()) && index >= 0);
if (index >= int(m_mappings.size()) || index < 0) return false;
TORRENT_ASSERT(index < m_mappings.end_index() && index >= port_mapping_t{0});
if (index >= m_mappings.end_index() || index < port_mapping_t{0}) return false;
global_mapping_t const& m = m_mappings[index];
if (m.protocol == portmap_protocol::none) return false;
local_ep = m.local_ep;
@ -706,7 +706,8 @@ void upnp::post(upnp::rootdevice const& d, char const* soap
#endif
}
void upnp::create_port_mapping(http_connection& c, rootdevice& d, int const i)
void upnp::create_port_mapping(http_connection& c, rootdevice& d
, port_mapping_t const i)
{
TORRENT_ASSERT(is_single_thread());
@ -716,7 +717,7 @@ void upnp::create_port_mapping(http_connection& c, rootdevice& d, int const i)
{
TORRENT_ASSERT(d.disabled);
#ifndef TORRENT_DISABLE_LOGGING
log("mapping %u aborted", i);
log("mapping %u aborted", static_cast<int>(i));
#endif
return;
}
@ -750,12 +751,12 @@ void upnp::create_port_mapping(http_connection& c, rootdevice& d, int const i)
post(d, soap, soap_action);
}
void upnp::next(rootdevice& d, int const i)
void upnp::next(rootdevice& d, port_mapping_t const i)
{
TORRENT_ASSERT(is_single_thread());
if (i < num_mappings() - 1)
if (i < prev(m_mappings.end_index()))
{
update_map(d, i + 1);
update_map(d, lt::next(i));
}
else
{
@ -763,11 +764,11 @@ void upnp::next(rootdevice& d, int const i)
, [](mapping_t const& m) { return m.act != portmap_action::none; });
if (j == d.mapping.end()) return;
update_map(d, int(j - d.mapping.begin()));
update_map(d, port_mapping_t{static_cast<int>(j - d.mapping.begin())});
}
}
void upnp::update_map(rootdevice& d, int const i)
void upnp::update_map(rootdevice& d, port_mapping_t const i)
{
TORRENT_ASSERT(is_single_thread());
TORRENT_ASSERT(d.magic == 1337);
@ -787,7 +788,7 @@ void upnp::update_map(rootdevice& d, int const i)
|| m.protocol == portmap_protocol::none)
{
#ifndef TORRENT_DISABLE_LOGGING
log("mapping %u does not need updating, skipping", i);
log("mapping %u does not need updating, skipping", static_cast<int>(i));
#endif
m.act = portmap_action::none;
next(d, i);
@ -835,7 +836,7 @@ void upnp::update_map(rootdevice& d, int const i)
m.act = portmap_action::none;
}
void upnp::delete_port_mapping(rootdevice& d, int const i)
void upnp::delete_port_mapping(rootdevice& d, port_mapping_t const i)
{
TORRENT_ASSERT(is_single_thread());
@ -845,7 +846,7 @@ void upnp::delete_port_mapping(rootdevice& d, int const i)
{
TORRENT_ASSERT(d.disabled);
#ifndef TORRENT_DISABLE_LOGGING
log("unmapping %u aborted", i);
log("unmapping %u aborted", static_cast<int>(i));
#endif
return;
}
@ -1082,7 +1083,7 @@ void upnp::disable(error_code const& ec)
if (i->protocol == portmap_protocol::none) continue;
portmap_protocol const proto = i->protocol;
i->protocol = portmap_protocol::none;
m_callback.on_port_mapping(int(i - m_mappings.begin()), address(), 0, proto, ec
m_callback.on_port_mapping(port_mapping_t(static_cast<int>(i - m_mappings.begin())), address(), 0, proto, ec
, portmap_transport::upnp);
}
@ -1217,7 +1218,7 @@ void upnp::on_upnp_get_ip_address_response(error_code const& e
, convert_from_native(e.message()).c_str());
}
#endif
if (num_mappings() > 0) update_map(d, 0);
if (num_mappings() > 0) update_map(d, port_mapping_t{0});
return;
}
@ -1226,7 +1227,7 @@ void upnp::on_upnp_get_ip_address_response(error_code const& e
#ifndef TORRENT_DISABLE_LOGGING
log("error while getting external IP address: incomplete http message");
#endif
if (num_mappings() > 0) update_map(d, 0);
if (num_mappings() > 0) update_map(d, port_mapping_t{0});
return;
}
@ -1239,7 +1240,7 @@ void upnp::on_upnp_get_ip_address_response(error_code const& e
, convert_from_native(p.message()).c_str());
}
#endif
if (num_mappings() > 0) update_map(d, 0);
if (num_mappings() > 0) update_map(d, port_mapping_t{0});
return;
}
@ -1284,11 +1285,11 @@ void upnp::on_upnp_get_ip_address_response(error_code const& e
#endif
}
if (num_mappings() > 0) update_map(d, 0);
if (num_mappings() > 0) update_map(d, port_mapping_t{0});
}
void upnp::on_upnp_map_response(error_code const& e
, libtorrent::http_parser const& p, rootdevice& d, int const mapping
, libtorrent::http_parser const& p, rootdevice& d, port_mapping_t const mapping
, http_connection& c)
{
TORRENT_ASSERT(is_single_thread());
@ -1440,7 +1441,7 @@ void upnp::on_upnp_map_response(error_code const& e
next(d, mapping);
}
void upnp::return_error(int const mapping, int const code)
void upnp::return_error(port_mapping_t const mapping, int const code)
{
TORRENT_ASSERT(is_single_thread());
int num_errors = sizeof(error_codes) / sizeof(error_codes[0]);
@ -1463,7 +1464,8 @@ void upnp::return_error(int const mapping, int const code)
}
void upnp::on_upnp_unmap_response(error_code const& e
, libtorrent::http_parser const& p, rootdevice& d, int const mapping
, libtorrent::http_parser const& p, rootdevice& d
, port_mapping_t const mapping
, http_connection& c)
{
TORRENT_ASSERT(is_single_thread());
@ -1546,7 +1548,7 @@ void upnp::on_expire(error_code const& ec)
{
rootdevice& d = const_cast<rootdevice&>(*i);
TORRENT_ASSERT(d.magic == 1337);
for (int m = 0; m < num_mappings(); ++m)
for (port_mapping_t m{0}; m < m_mappings.end_index(); ++m)
{
if (d.mapping[m].expires != max_time())
continue;
@ -1596,9 +1598,9 @@ void upnp::close()
continue;
}
j->act = portmap_action::del;
m_mappings[int(j - d.mapping.begin())].protocol = portmap_protocol::none;
m_mappings[port_mapping_t{static_cast<int>(j - d.mapping.begin())}].protocol = portmap_protocol::none;
}
if (num_mappings() > 0) update_map(d, 0);
if (num_mappings() > 0) update_map(d, port_mapping_t{0});
}
}

View File

@ -43,7 +43,8 @@ namespace
{
struct natpmp_callback : aux::portmap_callback
{
void on_port_mapping(int mapping, address const& ip, int port
void on_port_mapping(port_mapping_t const mapping
, address const& ip, int port
, portmap_protocol const protocol, error_code const& err
, portmap_transport const transport) override
{
@ -84,7 +85,7 @@ int main(int argc, char* argv[])
deadline_timer timer(ios);
int const tcp_map = natpmp_handler->add_mapping(portmap_protocol::tcp
auto const tcp_map = natpmp_handler->add_mapping(portmap_protocol::tcp
, atoi(argv[1]), tcp::endpoint({}, atoi(argv[1])));
natpmp_handler->add_mapping(portmap_protocol::udp, atoi(argv[2])
, tcp::endpoint({}, atoi(argv[2])));

View File

@ -106,7 +106,7 @@ void incoming_msearch(udp::endpoint const& from, char* buffer
struct callback_info
{
int mapping;
port_mapping_t mapping;
int port;
error_code ec;
bool operator==(callback_info const& e)
@ -119,13 +119,15 @@ namespace
{
struct upnp_callback : aux::portmap_callback
{
void on_port_mapping(int mapping, address const& ip, int port
void on_port_mapping(port_mapping_t const mapping
, address const& ip, int port
, portmap_protocol const protocol, error_code const& err
, portmap_transport const transport) override
{
callback_info info = {mapping, port, err};
callbacks.push_back(info);
std::cout << "mapping: " << mapping << ", port: " << port << ", IP: " << ip
std::cout << "mapping: " << static_cast<int>(mapping)
<< ", port: " << port << ", IP: " << ip
<< ", proto: " << static_cast<int>(protocol)
<< ", error: \"" << err.message() << "\"\n";
}
@ -198,8 +200,8 @@ void run_upnp_test(char const* root_filename, char const* router_model, char con
std::cout << "router: " << upnp_handler->router_model() << std::endl;
TEST_EQUAL(upnp_handler->router_model(), router_model);
int const mapping1 = upnp_handler->add_mapping(portmap_protocol::tcp, 500, ep("127.0.0.1", 500));
int const mapping2 = upnp_handler->add_mapping(portmap_protocol::udp, 501, ep("127.0.0.1", 501));
auto const mapping1 = upnp_handler->add_mapping(portmap_protocol::tcp, 500, ep("127.0.0.1", 500));
auto const mapping2 = upnp_handler->add_mapping(portmap_protocol::udp, 501, ep("127.0.0.1", 501));
for (int i = 0; i < 40; ++i)
{