Skip to content

Commit a940cdb

Browse files
fix(upnp): support newer miniupnpc library (#2782)
Co-authored-by: Vithorio Polten <[email protected]>
1 parent 90fd371 commit a940cdb

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

src/upnp.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,6 @@
1919
using namespace std::literals;
2020

2121
namespace upnp {
22-
constexpr auto INET6_ADDRESS_STRLEN = 46;
23-
24-
constexpr auto PORT_MAPPING_LIFETIME = 3600s;
25-
constexpr auto REFRESH_INTERVAL = 120s;
26-
27-
constexpr auto IPv4 = 0;
28-
constexpr auto IPv6 = 1;
29-
30-
using device_t = util::safe_ptr<UPNPDev, freeUPNPDevlist>;
31-
32-
KITTY_USING_MOVE_T(urls_t, UPNPUrls, , {
33-
FreeUPNPUrls(&el);
34-
});
3522

3623
struct mapping_t {
3724
struct {
@@ -59,6 +46,19 @@ namespace upnp {
5946
return "Unknown status"sv;
6047
}
6148

49+
/**
50+
* This function is a wrapper around UPNP_GetValidIGD() that returns the status code. There is a pre-processor
51+
* check to determine which version of the function to call based on the version of the MiniUPnPc library.
52+
*/
53+
int
54+
UPNP_GetValidIGDStatus(device_t &device, urls_t *urls, IGDdatas *data, std::array<char, INET6_ADDRESS_STRLEN> &lan_addr) {
55+
#if (MINIUPNPC_API_VERSION >= 18)
56+
return UPNP_GetValidIGD(device.get(), &urls->el, data, lan_addr.data(), lan_addr.size(), nullptr, 0);
57+
#else
58+
return UPNP_GetValidIGD(device.get(), &urls->el, data, lan_addr.data(), lan_addr.size());
59+
#endif
60+
}
61+
6262
class deinit_t: public platf::deinit_t {
6363
public:
6464
deinit_t() {
@@ -109,7 +109,7 @@ namespace upnp {
109109
IGDdatas data;
110110
urls_t urls;
111111
std::array<char, INET6_ADDRESS_STRLEN> lan_addr;
112-
auto status = UPNP_GetValidIGD(device.get(), &urls.el, &data, lan_addr.data(), lan_addr.size());
112+
auto status = upnp::UPNP_GetValidIGDStatus(device, &urls, &data, lan_addr);
113113
if (status != 1 && status != 2) {
114114
BOOST_LOG(debug) << "No valid IPv6 IGD: "sv << status_string(status);
115115
return false;
@@ -331,7 +331,7 @@ namespace upnp {
331331
std::array<char, INET6_ADDRESS_STRLEN> lan_addr;
332332

333333
urls_t urls;
334-
auto status = UPNP_GetValidIGD(device.get(), &urls.el, &data, lan_addr.data(), lan_addr.size());
334+
auto status = upnp::UPNP_GetValidIGDStatus(device, &urls, &data, lan_addr);
335335
if (status != 1 && status != 2) {
336336
BOOST_LOG(error) << status_string(status);
337337
mapped = false;

src/upnp.h

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,41 @@
44
*/
55
#pragma once
66

7+
#include <miniupnpc/miniupnpc.h>
8+
79
#include "platform/common.h"
810

911
/**
1012
* @brief UPnP port mapping.
1113
*/
1214
namespace upnp {
15+
constexpr auto INET6_ADDRESS_STRLEN = 46;
16+
constexpr auto IPv4 = 0;
17+
constexpr auto IPv6 = 1;
18+
constexpr auto PORT_MAPPING_LIFETIME = 3600s;
19+
constexpr auto REFRESH_INTERVAL = 120s;
20+
21+
using device_t = util::safe_ptr<UPNPDev, freeUPNPDevlist>;
22+
23+
KITTY_USING_MOVE_T(urls_t, UPNPUrls, , {
24+
FreeUPNPUrls(&el);
25+
});
26+
27+
/**
28+
* @brief Get the valid IGD status.
29+
* @param device The device.
30+
* @param urls The URLs.
31+
* @param data The IGD data.
32+
* @param lan_addr The LAN address.
33+
* @return The UPnP Status.
34+
* @retval 0 No IGD found.
35+
* @retval 1 A valid connected IGD has been found.
36+
* @retval 2 A valid IGD has been found but it reported as not connected.
37+
* @retval 3 An UPnP device has been found but was not recognized as an IGD.
38+
*/
39+
int
40+
UPNP_GetValidIGDStatus(device_t &device, urls_t *urls, IGDdatas *data, std::array<char, INET6_ADDRESS_STRLEN> &lan_addr);
41+
1342
[[nodiscard]] std::unique_ptr<platf::deinit_t>
1443
start();
15-
}
44+
} // namespace upnp

0 commit comments

Comments
 (0)