diff --git a/src/upnp.cpp b/src/upnp.cpp index 2dc7ff1..215c9f4 100644 --- a/src/upnp.cpp +++ b/src/upnp.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #if !defined(NDEBUG) # define KUU_VERBOSE @@ -51,11 +52,13 @@ namespace kuu { struct UPNPUrlsWithInitFlag { UPNPUrlsWithInitFlag() : initialized(false) {} + explicit UPNPUrlsWithInitFlag(std::nullptr_t) : initialized(false) {} + explicit UPNPUrlsWithInitFlag(bool parInit) : initialized(parInit) {} struct UPNPUrls urls; bool initialized; - operator struct UPNPUrls* () { return &urls; } - UPNPUrlsWithInitFlag* operator-> () { return this; } + operator struct UPNPUrls* () { return (initialized ? &urls : nullptr); } + UPNPUrls* operator-> () { return (initialized ? &urls : nullptr); } bool operator not() const { return not initialized; } }; @@ -179,15 +182,19 @@ namespace kuu { throw ScanException("More than one UPNP device found (" + std::to_string(devs.size()) + ")"); UPNPUrlsPtr urls; + assert(not urls.get()); { + UPNPUrlsWithInitFlag urls_local(true); + assert(urls_local.initialized); std::array lanaddr; const auto i = UPNP_GetValidIGD( devlist.get(), - urls.get(), + urls_local, &m_local_data->data, lanaddr.data(), lanaddr.size() ); + urls.reset(urls_local); switch (i) { case 1: @@ -198,14 +205,13 @@ namespace kuu { default: m_local_data->igd_reply = IGDNotSure; } - urls->initialized = true; m_local_data->lanaddr = lanaddr.data(); } { std::array externaladdr; - const auto r = UPNP_GetExternalIPAddress(urls->urls.controlURL, m_local_data->data.first.servicetype, externaladdr.data()); + const auto r = UPNP_GetExternalIPAddress(urls->controlURL, m_local_data->data.first.servicetype, externaladdr.data()); if (UPNPCOMMAND_SUCCESS != r) m_local_data->externaladdr = externaladdr.data(); } @@ -254,7 +260,7 @@ namespace kuu { intClient[0] = '\0'; r = UPNP_GetGenericPortMappingEntry( - m_local_data->urls->urls.controlURL, + m_local_data->urls->controlURL, m_local_data->data.first.servicetype, std::to_string(z).c_str(), extPort, @@ -299,7 +305,7 @@ namespace kuu { Protocol parProtocol ) { const auto r = UPNP_AddPortMapping( - m_local_data->urls->urls.controlURL, + m_local_data->urls->controlURL, m_local_data->data.first.servicetype, std::to_string(parExtPort).c_str(), std::to_string(parIntPort).c_str(), @@ -312,7 +318,7 @@ namespace kuu { if (UPNPCOMMAND_SUCCESS != r) { std::ostringstream oss; oss << "Error in UPNP_AddPortMapping(\"" << - m_local_data->urls->urls.controlURL << "\", \"" << + m_local_data->urls->controlURL << "\", \"" << m_local_data->data.first.servicetype << "\", " << parExtPort << ", " << parIntPort << ", \"" <<