From 3e89c0c5576326e1214e0d92e163bc6962f58ae2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Mon, 14 Sep 2020 15:56:43 -0400
Subject: [PATCH] contrib/pupnp: update to 1.14, cleanup

Change-Id: I76689a7312e3839aaee3fdac4fa760e0ca5dc109
---
 contrib/src/upnp/SHA512SUMS         |  3 +-
 contrib/src/upnp/libupnp-ipv6.patch | 49 -----------------------------
 contrib/src/upnp/miniserver.patch   | 31 ------------------
 contrib/src/upnp/rules.mak          |  6 ++--
 src/upnp/protocol/pupnp/pupnp.cpp   | 37 ++++++++++++----------
 src/upnp/protocol/pupnp/pupnp.h     | 13 ++------
 6 files changed, 26 insertions(+), 113 deletions(-)
 delete mode 100644 contrib/src/upnp/libupnp-ipv6.patch
 delete mode 100644 contrib/src/upnp/miniserver.patch

diff --git a/contrib/src/upnp/SHA512SUMS b/contrib/src/upnp/SHA512SUMS
index bb7c67c744..219dd6541c 100644
--- a/contrib/src/upnp/SHA512SUMS
+++ b/contrib/src/upnp/SHA512SUMS
@@ -1,2 +1 @@
-bcf6a806772187b681ba293be3b060f3da534f553056bf0d98d213869f6dc587ad7aa9dd06b0a232995198adc461b53861415198f7f2decda17fd19163a7a39f  pupnp-edd037bf9b66637db42c08981c0e4ffeb6faadfa.tar.gz
-998ef22d2af8e4cbc7fa1f87613ae0aa840e636bb72a5656a97327b0820a46b2b3ed2f3200260c018b5339de2fc4be760d42a3c687e49c7fedb58c1ad2287699  pupnp-release-1.12.0.tar.gz
\ No newline at end of file
+d7cf50e168aff2ef3ad08b4c383108d5044e4c9d2923088bb93423ee5fc555c32f0254f307cb86ea7c18197612cf2d2c6c681f82d99a6f01a68f86a21c6eecb7  pupnp-release-1.14.0.tar.gz
\ No newline at end of file
diff --git a/contrib/src/upnp/libupnp-ipv6.patch b/contrib/src/upnp/libupnp-ipv6.patch
deleted file mode 100644
index 81df7a64f5..0000000000
--- a/contrib/src/upnp/libupnp-ipv6.patch
+++ /dev/null
@@ -1,49 +0,0 @@
-From 438ace99538713fb1370411188e0f370069a1818 Mon Sep 17 00:00:00 2001
-From: Konstantin Pavlov <thresh@videolan.org>
-Date: Tue, 29 May 2012 10:18:40 +0400
-Subject: [PATCH] Fix compile under mingw with IPv6 enabled.
-
----
- upnp/src/genlib/miniserver/miniserver.c |    7 +++++++
- upnp/src/ssdp/ssdp_server.c             |    7 +++++++
- 2 files changed, 14 insertions(+)
-
-diff --git a/upnp/src/genlib/miniserver/miniserver.c b/upnp/src/genlib/miniserver/miniserver.c
-index af310ca..1ae422f 100644
---- a/upnp/src/genlib/miniserver/miniserver.c
-+++ b/upnp/src/genlib/miniserver/miniserver.c
-@@ -68,6 +68,13 @@
- /*! . */
- #define APPLICATION_LISTENING_PORT 49152
-
-+/* IPV6_V6ONLY is missing from MinGW, hack taken from
-+ * http://svn.apache.org/repos/asf/apr/apr/trunk/network_io/win32/sockopt.c
-+ */
-+#ifndef IPV6_V6ONLY
-+#define IPV6_V6ONLY 27
-+#endif
-+
- struct mserv_request_t {
- 	/*! Connection handle. */
- 	SOCKET connfd;
-diff --git a/upnp/src/ssdp/ssdp_server.c b/upnp/src/ssdp/ssdp_server.c
-index 231c2c5..6a9c27f 100644
---- a/upnp/src/ssdp/ssdp_server.c
-+++ b/upnp/src/ssdp/ssdp_server.c
-@@ -69,6 +69,13 @@
- 	#endif /* UPNP_ENABLE_IPV6 */
- #endif /* INCLUDE_CLIENT_APIS */
-
-+/* IPV6_V6ONLY is missing from MinGW, hack taken from
-+ * http://svn.apache.org/repos/asf/apr/apr/trunk/network_io/win32/sockopt.c
-+ */
-+#ifndef IPV6_V6ONLY
-+#define IPV6_V6ONLY 27
-+#endif
-+
- void RequestHandler();
-
- enum Listener {
---
-1.7.9.7
-
diff --git a/contrib/src/upnp/miniserver.patch b/contrib/src/upnp/miniserver.patch
deleted file mode 100644
index a2114bef24..0000000000
--- a/contrib/src/upnp/miniserver.patch
+++ /dev/null
@@ -1,31 +0,0 @@
-From 39f8e5f5379607166cf6e54d270590e1fd13c12f Mon Sep 17 00:00:00 2001
-From: Eden Abitbol <eden.abitbol@savoirfairelinux.com>
-Date: Mon, 3 Jun 2019 15:55:40 -0400
-Subject: [PATCH] web server patch
-
----
- upnp/src/api/upnpapi.c | 2 +-
- 1 file changed, 1 insertion(+), 1 deletion(-)
-
-diff --git a/upnp/src/api/upnpapi.c b/upnp/src/api/upnpapi.c
-index 9ddc7fb..d286092 100644
---- a/upnp/src/api/upnpapi.c
-+++ b/upnp/src/api/upnpapi.c
-@@ -383,13 +383,13 @@ static int UpnpInitPreamble(void)
- 		return retVal;
- 	}
-
-+#ifdef INTERNAL_WEB_SERVER
- #ifdef INCLUDE_DEVICE_APIS
- #if EXCLUDE_SOAP == 0
- 	SetSoapCallback(soap_device_callback);
- #endif
- #endif /* INCLUDE_DEVICE_APIS */
-
--#ifdef INTERNAL_WEB_SERVER
- #if EXCLUDE_GENA == 0
- 	SetGenaCallback(genaCallback);
- #endif
---
-2.17.1
-
diff --git a/contrib/src/upnp/rules.mak b/contrib/src/upnp/rules.mak
index 998d352af2..87ebd9fe94 100644
--- a/contrib/src/upnp/rules.mak
+++ b/contrib/src/upnp/rules.mak
@@ -1,6 +1,6 @@
 # UPNP
-UPNP_VERSION := 1.12.0
-UPNP_URL := https://github.com/mrjimenez/pupnp/archive/release-$(UPNP_VERSION).tar.gz
+UPNP_VERSION := 1.14.0
+UPNP_URL := https://github.com/pupnp/pupnp/archive/release-$(UPNP_VERSION).tar.gz
 
 PKGS += upnp
 ifeq ($(call need_pkg,"libupnp >= 1.8.4"),)
@@ -17,8 +17,6 @@ upnp: pupnp-release-$(UPNP_VERSION).tar.gz .sum-upnp
 ifeq ($(OS),Windows_NT)
 	$(APPLY) $(SRC)/upnp/libupnp-windows.patch
 endif
-	$(APPLY) $(SRC)/upnp/libupnp-ipv6.patch
-	#$(APPLY) $(SRC)/upnp/miniserver.patch
 	$(UPDATE_AUTOCONFIG) && cd $(UNPACK_DIR) && mv config.guess config.sub
 	$(MOVE)
 
diff --git a/src/upnp/protocol/pupnp/pupnp.cpp b/src/upnp/protocol/pupnp/pupnp.cpp
index 8c9cce1aea..8b1406aa33 100644
--- a/src/upnp/protocol/pupnp/pupnp.cpp
+++ b/src/upnp/protocol/pupnp/pupnp.cpp
@@ -28,6 +28,13 @@
 namespace jami {
 namespace upnp {
 
+// Action identifiers.
+constexpr static const char* ACTION_ADD_PORT_MAPPING {"AddPortMapping"};
+constexpr static const char* ACTION_DELETE_PORT_MAPPING {"DeletePortMapping"};
+constexpr static const char* ACTION_GET_GENERIC_PORT_MAPPING_ENTRY {"GetGenericPortMappingEntry"};
+constexpr static const char* ACTION_GET_STATUS_INFO {"GetStatusInfo"};
+constexpr static const char* ACTION_GET_EXTERNAL_IP_ADDRESS {"GetExternalIPAddress"};
+
 // Helper functions for xml parsing.
 static std::string
 getElementText(IXML_Node* node)
@@ -89,13 +96,7 @@ errorOnResponse(IXML_Document* doc)
 
 PUPnP::PUPnP()
 {
-    int upnp_err = UPNP_E_SUCCESS;
-
-#if UPNP_ENABLE_IPV6
-    upnp_err = UpnpInit2(nullptr, 0);
-#else
-    upnp_err = UpnpInit(0, 0); // Deprecated function but fall back on it if IPv6 not enabled.
-#endif
+    int upnp_err = UpnpInit2(nullptr, 0);
 
     if (upnp_err != UPNP_E_SUCCESS) {
         JAMI_ERR("PUPnP: Can't initialize libupnp: %s", UpnpGetErrorMessage(upnp_err));
@@ -413,7 +414,7 @@ PUPnP::actionAddPortMappingAsync(const UPnPIGD& igd, const Mapping& mapping)
 }
 
 void
-PUPnP::processAddMapAction(const std::string& ctrlURL, IXML_Document* actionRequest)
+PUPnP::processAddMapAction(const std::string_view& ctrlURL, IXML_Document* actionRequest)
 {
     std::string portExternal(getFirstDocItem(actionRequest, "NewExternalPort"));
     std::string portInternal(getFirstDocItem(actionRequest, "NewInternalPort"));
@@ -446,7 +447,7 @@ PUPnP::processAddMapAction(const std::string& ctrlURL, IXML_Document* actionRequ
 }
 
 void
-PUPnP::processRemoveMapAction(const std::string& ctrlURL, IXML_Document* actionRequest)
+PUPnP::processRemoveMapAction(const std::string_view& ctrlURL, IXML_Document* actionRequest)
 {
     std::string portExternal(getFirstDocItem(actionRequest, "NewExternalPort"));
     std::string protocol(getFirstDocItem(actionRequest, "NewProtocol"));
@@ -516,7 +517,7 @@ PUPnP::ctrlPtCallback(Upnp_EventType event_type, const void* event, void* user_d
 }
 
 PUPnP::CtrlAction
-PUPnP::getAction(char* xmlNode)
+PUPnP::getAction(const char* xmlNode)
 {
     if (strstr(xmlNode, ACTION_ADD_PORT_MAPPING)) {
         return CtrlAction::ADD_PORT_MAPPING;
@@ -533,6 +534,11 @@ PUPnP::getAction(char* xmlNode)
     }
 }
 
+inline std::string_view
+viewFromUpnpString(const UpnpString* ustr) {
+    return {UpnpString_get_String(ustr), UpnpString_get_Length(ustr)};
+}
+
 int
 PUPnP::handleCtrlPtUPnPEvents(Upnp_EventType event_type, const void* event)
 {
@@ -648,14 +654,10 @@ PUPnP::handleCtrlPtUPnPEvents(Upnp_EventType event_type, const void* event)
     case UPNP_CONTROL_ACTION_COMPLETE: {
         const UpnpActionComplete* a_event = (const UpnpActionComplete*) event;
         if (UpnpActionComplete_get_ErrCode(a_event) == UPNP_E_SUCCESS) {
-            IXML_Document* actionRequest = UpnpActionComplete_get_ActionRequest(a_event);
-            if (actionRequest) {
-                std::string ctrlURL(UpnpString_get_String(UpnpActionComplete_get_CtrlUrl(a_event)));
-                char* xmlbuff = ixmlPrintNode((IXML_Node*) actionRequest);
-                if (xmlbuff) {
+            if (IXML_Document* actionRequest = UpnpActionComplete_get_ActionRequest(a_event)) {
+                auto ctrlURL = viewFromUpnpString(UpnpActionComplete_get_CtrlUrl(a_event));
+                if (const char* xmlbuff = ixmlPrintNode((IXML_Node*) actionRequest)) {
                     switch (getAction(xmlbuff)) {
-                    case CtrlAction::UNKNOWN:
-                        break;
                     case CtrlAction::ADD_PORT_MAPPING:
                         processAddMapAction(ctrlURL, actionRequest);
                         break;
@@ -668,6 +670,7 @@ PUPnP::handleCtrlPtUPnPEvents(Upnp_EventType event_type, const void* event)
                         break;
                     case CtrlAction::GET_EXTERNAL_IP_ADDRESS:
                         break;
+                    case CtrlAction::UNKNOWN:
                     default:
                         break;
                     }
diff --git a/src/upnp/protocol/pupnp/pupnp.h b/src/upnp/protocol/pupnp/pupnp.h
index 50ba46d695..31ce740e28 100644
--- a/src/upnp/protocol/pupnp/pupnp.h
+++ b/src/upnp/protocol/pupnp/pupnp.h
@@ -57,13 +57,6 @@
 #include <memory>
 #include <future>
 
-// Action identifiers.
-constexpr static const char* ACTION_ADD_PORT_MAPPING {"AddPortMapping"};
-constexpr static const char* ACTION_DELETE_PORT_MAPPING {"DeletePortMapping"};
-constexpr static const char* ACTION_GET_GENERIC_PORT_MAPPING_ENTRY {"GetGenericPortMappingEntry"};
-constexpr static const char* ACTION_GET_STATUS_INFO {"GetStatusInfo"};
-constexpr static const char* ACTION_GET_EXTERNAL_IP_ADDRESS {"GetExternalIPAddress"};
-
 namespace jami {
 class IpAddr;
 }
@@ -116,15 +109,15 @@ public:
                            uint16_t port_internal,
                            PortType type) override;
     // Treats the reception of an add mapping action answer.
-    void processAddMapAction(const std::string& ctrlURL, IXML_Document* actionRequest);
+    void processAddMapAction(const std::string_view& ctrlURL, IXML_Document* actionRequest);
 
     // Returns control point action callback based on xml node.
-    CtrlAction getAction(char* xmlNode);
+    CtrlAction getAction(const char* xmlNode);
 
     // Removes a mapping.
     void requestMappingRemove(const Mapping& igdMapping) override;
     // Treats the reception of a remove mapping action answer.
-    void processRemoveMapAction(const std::string& ctrlURL, IXML_Document* actionRequest);
+    void processRemoveMapAction(const std::string_view& ctrlURL, IXML_Document* actionRequest);
 
     // Removes all local mappings of IGD that we're added by the application.
     void removeAllLocalMappings(IGD* igd) override;
-- 
GitLab