Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use only the necessary protocols in UPnP #2571

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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