1 From f4f1800f5e67ab59312ccf710695acf06fb4ae26 Mon Sep 17 00:00:00 2001
2 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com>
3 Date: Mon, 1 Jul 2024 10:07:06 -0400
4 Subject: [PATCH] fix(upnp): support newer miniupnpc library (#2782)
6 Co-authored-by: Vithorio Polten <reach@vithor.io>
8 src/upnp.cpp | 30 +++++++++++++++---------------
9 src/upnp.h | 31 ++++++++++++++++++++++++++++++-
10 2 files changed, 45 insertions(+), 16 deletions(-)
12 diff --git a/src/upnp.cpp b/src/upnp.cpp
13 index f65bcb87..fcbaaeb5 100644
17 using namespace std::literals;
20 - constexpr auto INET6_ADDRESS_STRLEN = 46;
22 - constexpr auto PORT_MAPPING_LIFETIME = 3600s;
23 - constexpr auto REFRESH_INTERVAL = 120s;
25 - constexpr auto IPv4 = 0;
26 - constexpr auto IPv6 = 1;
28 - using device_t = util::safe_ptr<UPNPDev, freeUPNPDevlist>;
30 - KITTY_USING_MOVE_T(urls_t, UPNPUrls, , {
36 @@ -59,6 +46,19 @@ namespace upnp {
37 return "Unknown status"sv;
41 + * This function is a wrapper around UPNP_GetValidIGD() that returns the status code. There is a pre-processor
42 + * check to determine which version of the function to call based on the version of the MiniUPnPc library.
45 + UPNP_GetValidIGDStatus(device_t &device, urls_t *urls, IGDdatas *data, std::array<char, INET6_ADDRESS_STRLEN> &lan_addr) {
46 +#if (MINIUPNPC_API_VERSION >= 18)
47 + return UPNP_GetValidIGD(device.get(), &urls->el, data, lan_addr.data(), lan_addr.size(), nullptr, 0);
49 + return UPNP_GetValidIGD(device.get(), &urls->el, data, lan_addr.data(), lan_addr.size());
53 class deinit_t: public platf::deinit_t {
56 @@ -109,7 +109,7 @@ namespace upnp {
59 std::array<char, INET6_ADDRESS_STRLEN> lan_addr;
60 - auto status = UPNP_GetValidIGD(device.get(), &urls.el, &data, lan_addr.data(), lan_addr.size());
61 + auto status = upnp::UPNP_GetValidIGDStatus(device, &urls, &data, lan_addr);
62 if (status != 1 && status != 2) {
63 BOOST_LOG(debug) << "No valid IPv6 IGD: "sv << status_string(status);
65 @@ -331,7 +331,7 @@ namespace upnp {
66 std::array<char, INET6_ADDRESS_STRLEN> lan_addr;
69 - auto status = UPNP_GetValidIGD(device.get(), &urls.el, &data, lan_addr.data(), lan_addr.size());
70 + auto status = upnp::UPNP_GetValidIGDStatus(device, &urls, &data, lan_addr);
71 if (status != 1 && status != 2) {
72 BOOST_LOG(error) << status_string(status);
74 diff --git a/src/upnp.h b/src/upnp.h
75 index 73fc4f79..4b2e3296 100644
82 +#include <miniupnpc/miniupnpc.h>
84 #include "platform/common.h"
87 + constexpr auto INET6_ADDRESS_STRLEN = 46;
88 + constexpr auto IPv4 = 0;
89 + constexpr auto IPv6 = 1;
90 + constexpr auto PORT_MAPPING_LIFETIME = 3600s;
91 + constexpr auto REFRESH_INTERVAL = 120s;
93 + using device_t = util::safe_ptr<UPNPDev, freeUPNPDevlist>;
95 + KITTY_USING_MOVE_T(urls_t, UPNPUrls, , {
100 + * @brief Get the valid IGD status.
101 + * @param device The device.
102 + * @param urls The URLs.
103 + * @param data The IGD data.
104 + * @param lan_addr The LAN address.
105 + * @return The UPnP Status.
106 + * @retval 0 No IGD found.
107 + * @retval 1 A valid connected IGD has been found.
108 + * @retval 2 A valid IGD has been found but it reported as not connected.
109 + * @retval 3 An UPnP device has been found but was not recognized as an IGD.
112 + UPNP_GetValidIGDStatus(device_t &device, urls_t *urls, IGDdatas *data, std::array<char, INET6_ADDRESS_STRLEN> &lan_addr);
114 [[nodiscard]] std::unique_ptr<platf::deinit_t>