diff --git a/src/upnp.cpp b/src/upnp.cpp index 7a6f7efc2ec..3131941d584 100644 --- a/src/upnp.cpp +++ b/src/upnp.cpp @@ -19,19 +19,6 @@ using namespace std::literals; namespace upnp { - constexpr auto INET6_ADDRESS_STRLEN = 46; - - constexpr auto PORT_MAPPING_LIFETIME = 3600s; - constexpr auto REFRESH_INTERVAL = 120s; - - constexpr auto IPv4 = 0; - constexpr auto IPv6 = 1; - - using device_t = util::safe_ptr; - - KITTY_USING_MOVE_T(urls_t, UPNPUrls, , { - FreeUPNPUrls(&el); - }); struct mapping_t { struct { @@ -59,6 +46,19 @@ namespace upnp { return "Unknown status"sv; } + /** + * This function is a wrapper around UPNP_GetValidIGD() that returns the status code. There is a pre-processor + * check to determine which version of the function to call based on the version of the MiniUPnPc library. + */ + int + UPNP_GetValidIGDStatus(device_t &device, urls_t *urls, IGDdatas *data, std::array& lan_addr) { +#if (MINIUPNPC_API_VERSION >= 18) + return UPNP_GetValidIGD(device.get(), &urls->el, data, lan_addr.data(), lan_addr.size(), nullptr, 0); +#else + return UPNP_GetValidIGD(device.get(), &urls->el, data, lan_addr.data(), lan_addr.size()); +#endif + } + class deinit_t: public platf::deinit_t { public: deinit_t() { @@ -109,7 +109,7 @@ namespace upnp { IGDdatas data; urls_t urls; std::array lan_addr; - auto status = UPNP_GetValidIGD(device.get(), &urls.el, &data, lan_addr.data(), lan_addr.size()); + auto status = upnp::UPNP_GetValidIGDStatus(device, &urls, &data, lan_addr); if (status != 1 && status != 2) { BOOST_LOG(debug) << "No valid IPv6 IGD: "sv << status_string(status); return false; @@ -331,7 +331,7 @@ namespace upnp { std::array lan_addr; urls_t urls; - auto status = UPNP_GetValidIGD(device.get(), &urls.el, &data, lan_addr.data(), lan_addr.size()); + auto status = upnp::UPNP_GetValidIGDStatus(device, &urls, &data, lan_addr); if (status != 1 && status != 2) { BOOST_LOG(error) << status_string(status); mapped = false; diff --git a/src/upnp.h b/src/upnp.h index 0234c96f28e..cf3a7e45589 100644 --- a/src/upnp.h +++ b/src/upnp.h @@ -10,6 +10,34 @@ * @brief UPnP port mapping. */ namespace upnp { + constexpr auto INET6_ADDRESS_STRLEN = 46; + constexpr auto IPv4 = 0; + constexpr auto IPv6 = 1; + constexpr auto PORT_MAPPING_LIFETIME = 3600s; + constexpr auto REFRESH_INTERVAL = 120s; + + using device_t = util::safe_ptr; + + KITTY_USING_MOVE_T(urls_t, UPNPUrls, , { + FreeUPNPUrls(&el); + }); + + /** + * @brief Get the valid IGD status. + * @param device The device. + * @param urls The URLs. + * @param data The IGD data. + * @param lan_addr The LAN address. + * @return The UPnP Status. + * @retval 0 No IGD found. + * @retval 1 A valid connected IGD has been found. + * @retval 2 A valid IGD has been found but it reported as not connected. + * @retval 3 An UPnP device has been found but was not recognized as an IGD. + */ + [[nodiscard]] + int + UPNP_GetValidIGDStatus(device_t &device, urls_t *urls, IGDdatas *data, std::array& lan_addr); + [[nodiscard]] std::unique_ptr start(); }