diff --git a/include/libtorrent/upnp.hpp b/include/libtorrent/upnp.hpp index 7b4559dd91e..47a3b79618c 100644 --- a/include/libtorrent/upnp.hpp +++ b/include/libtorrent/upnp.hpp @@ -280,7 +280,7 @@ struct TORRENT_EXTRA_EXPORT upnp final // the url to the WANIP or WANPPP interface std::string control_url; // either the WANIP namespace or the WANPPP namespace - char const* service_namespace = nullptr; + std::string service_namespace; std::vector mapping; diff --git a/src/upnp.cpp b/src/upnp.cpp index 220edd3ca5b..6934f27848b 100644 --- a/src/upnp.cpp +++ b/src/upnp.cpp @@ -225,7 +225,7 @@ int upnp::add_mapping(portmap_protocol const p, int const external_port m.external_port = external_port; m.local_port = local_port; - if (d.service_namespace) update_map(d, mapping_index); + if (!d.service_namespace.empty()) update_map(d, mapping_index); } return mapping_index; @@ -255,7 +255,7 @@ void upnp::delete_mapping(int mapping) TORRENT_ASSERT(mapping < int(d.mapping.size())); d.mapping[mapping].act = mapping_t::action::del; - if (d.service_namespace) update_map(d, mapping); + if (!d.service_namespace.empty()) update_map(d, mapping); } } @@ -699,7 +699,7 @@ void upnp::post(upnp::rootdevice const& d, char const* soap "Soapaction: \"%s#%s\"\r\n\r\n" "%s" , d.path.c_str(), d.hostname.c_str(), d.port - , int(strlen(soap)), d.service_namespace, soap_action + , int(strlen(soap)), d.service_namespace.c_str(), soap_action , soap); d.upnp_connection->m_sendbuffer = header; @@ -743,7 +743,7 @@ void upnp::create_port_mapping(http_connection& c, rootdevice& d, int i) "%s at %s:%d" "%u" "" - , soap_action, d.service_namespace, d.mapping[i].external_port + , soap_action, d.service_namespace.c_str(), d.mapping[i].external_port , (d.mapping[i].protocol == portmap_protocol::udp ? "UDP" : "TCP") , d.mapping[i].local_port , local_endpoint.c_str() @@ -795,7 +795,7 @@ void upnp::update_map(rootdevice& d, int i) } TORRENT_ASSERT(!d.upnp_connection); - TORRENT_ASSERT(d.service_namespace); + TORRENT_ASSERT(!d.service_namespace.empty()); #ifndef TORRENT_DISABLE_LOGGING log("connecting to %s", d.hostname.c_str()); @@ -852,6 +852,8 @@ void upnp::delete_port_mapping(rootdevice& d, int i) char const* soap_action = "DeletePortMapping"; + TORRENT_ASSERT(!d.service_namespace.empty()); + char soap[2048]; error_code ec; std::snprintf(soap, sizeof(soap), "\n" @@ -862,7 +864,7 @@ void upnp::delete_port_mapping(rootdevice& d, int i) "%u" "%s" "" - , soap_action, d.service_namespace + , soap_action, d.service_namespace.c_str() , d.mapping[i].external_port , (d.mapping[i].protocol == portmap_protocol::udp ? "UDP" : "TCP") , soap_action); @@ -996,9 +998,10 @@ void upnp::on_upnp_xml(error_code const& e d.disabled = true; return; } - static std::string service_type; - service_type.swap(s.service_type); - d.service_namespace = service_type.c_str(); + d.service_namespace = s.service_type; + + TORRENT_ASSERT(!d.service_namespace.empty()); + if (!s.model.empty()) m_model = s.model; if (!s.url_base.empty() && s.control_url.substr(0, 7) != "http://") @@ -1029,7 +1032,7 @@ void upnp::on_upnp_xml(error_code const& e { log("found control URL: %s namespace %s " "urlbase: %s in response from %s" - , d.control_url.c_str(), d.service_namespace + , d.control_url.c_str(), d.service_namespace.c_str() , s.url_base.c_str(), d.url.c_str()); } #endif @@ -1077,6 +1080,8 @@ void upnp::get_ip_address(rootdevice& d) char const* soap_action = "GetExternalIPAddress"; + TORRENT_ASSERT(!d.service_namespace.empty()); + char soap[2048]; error_code ec; std::snprintf(soap, sizeof(soap), "\n" @@ -1084,7 +1089,7 @@ void upnp::get_ip_address(rootdevice& d) "s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" "" "" - , soap_action, d.service_namespace + , soap_action, d.service_namespace.c_str() , soap_action); post(d, soap, soap_action);