Skip to content

Commit

Permalink
Use only the necessary protocols in UPnP (#2571)
Browse files Browse the repository at this point in the history
* Do not request UPnP for UDP when disabled

* (unrelated) print endline on flag errors
  • Loading branch information
guilhermelawless committed Feb 18, 2020
1 parent f53811f commit e87670a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion nano/nano_node/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ int main (int argc, char * const * argv)
auto flags_ec = nano::update_flags (flags, vm);
if (flags_ec)
{
std::cerr << flags_ec.message ();
std::cerr << flags_ec.message () << std::endl;
std::exit (1);
}
auto config (vm.find ("config"));
Expand Down
11 changes: 6 additions & 5 deletions nano/node/portmapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
#include <nano/node/portmapping.hpp>

#include <boost/format.hpp>
#include <boost/range/adaptor/filtered.hpp>

#include <upnpcommands.h>
#include <upnperrors.h>

nano::port_mapping::port_mapping (nano::node & node_a) :
node (node_a),
protocols ({ { { "TCP", 0, boost::asio::ip::address_v4::any (), 0 }, { "UDP", 0, boost::asio::ip::address_v4::any (), 0 } } })
protocols ({ { { "TCP", 0, boost::asio::ip::address_v4::any (), 0, true }, { "UDP", 0, boost::asio::ip::address_v4::any (), 0, !node_a.flags.disable_udp } } })
{
}

Expand Down Expand Up @@ -61,7 +62,7 @@ nano::endpoint nano::port_mapping::external_address ()
{
nano::endpoint result_l (boost::asio::ip::address_v6{}, 0);
nano::lock_guard<std::mutex> guard_l (mutex);
for (auto & protocol : protocols)
for (auto & protocol : protocols | boost::adaptors::filtered ([](auto const & p) { return p.enabled; }))
{
if (protocol.external_port != 0)
{
Expand All @@ -80,7 +81,7 @@ void nano::port_mapping::refresh_mapping ()
auto config_port_l (get_config_port (node_port_l));

// We don't map the RPC port because, unless RPC authentication was added, this would almost always be a security risk
for (auto & protocol : protocols)
for (auto & protocol : protocols | boost::adaptors::filtered ([](auto const & p) { return p.enabled; }))
{
auto upnp_description = std::string ("Nano Node (") + network_params.network.get_current_network_as_string () + ")";
auto add_port_mapping_error_l (UPNP_AddPortMapping (upnp.urls.controlURL, upnp.data.first.servicetype, config_port_l.c_str (), node_port_l.c_str (), address.to_string ().c_str (), upnp_description.c_str (), protocol.name, nullptr, nullptr));
Expand Down Expand Up @@ -114,7 +115,7 @@ int nano::port_mapping::check_mapping ()
nano::lock_guard<std::mutex> guard_l (mutex);
auto node_port_l (std::to_string (node.network.endpoint ().port ()));
auto config_port_l (get_config_port (node_port_l));
for (auto & protocol : protocols)
for (auto & protocol : protocols | boost::adaptors::filtered ([](auto const & p) { return p.enabled; }))
{
std::array<char, 64> int_client_l;
std::array<char, 6> int_port_l;
Expand Down Expand Up @@ -188,7 +189,7 @@ void nano::port_mapping::stop ()
{
on = false;
nano::lock_guard<std::mutex> guard_l (mutex);
for (auto & protocol : protocols)
for (auto & protocol : protocols | boost::adaptors::filtered ([](auto const & p) { return p.enabled; }))
{
if (protocol.external_port != 0)
{
Expand Down
1 change: 1 addition & 0 deletions nano/node/portmapping.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class mapping_protocol
int remaining;
boost::asio::ip::address_v4 external_address;
uint16_t external_port;
bool enabled;
};

/** Collection of discovered UPnP devices and state*/
Expand Down

0 comments on commit e87670a

Please sign in to comment.