From 3e2f026f2281aa511f1a9106cf48a1f5344ae06b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Karl=20Strau=C3=9Fberger?= <k_straussberger@netzland.net>
Date: Tue, 9 Jan 2024 13:12:10 +0100
Subject: [PATCH] Build Support for NPUPNP

---
 .github/workflows/ci.yml            | 23 ++++++++
 CMakeLists.txt                      | 43 ++++++++++++---
 ChangeLog.md                        |  5 ++
 README.md                           |  2 +-
 cmake/FindNPUPNP.cmake              | 54 +++++++++++++++++++
 cmake/FindUPNP.cmake                |  6 +--
 scripts/debian/build-deb.sh         | 33 ++++++++++--
 scripts/install-googletest.sh       |  4 +-
 scripts/install-npupnp.sh           | 83 +++++++++++++++++++++++++++++
 scripts/versions.sh                 |  4 +-
 src/iohandler/process_io_handler.cc |  1 +
 src/server.cc                       |  2 +
 src/server.h                        |  2 +
 13 files changed, 244 insertions(+), 18 deletions(-)
 create mode 100644 cmake/FindNPUPNP.cmake
 create mode 100755 scripts/install-npupnp.sh

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1c301ecd9..5299b7bee 100644
--- CMakeLists.txt
+++ CMakeLists.txt
@@ -385,14 +385,45 @@ target_compile_options(libgerbera PUBLIC ${LFS_COMPILE_OPTIONS})
 target_link_libraries(libgerbera PUBLIC ${LFS_LIBRARIES})
 
 if (WITH_NPUPNP)
-    find_package(PkgConfig QUIET)
-    pkg_check_modules(NPUPNP QUIET libnpupnp>=4.2.1)
-    if (NOT NPUPNP_FOUND)
-        find_package(NPUPNP REQUIRED)
+    set(REQ_NPUPNP_VERSION 4.2.1)
+
+    find_package(NPUPNP ${REQ_NPUPNP_VERSION} QUIET)
+
+    if (NOT TARGET NPUPNP::NPUPNP)
+        find_package(npupnp ${REQ_NPUPNP_VERSION} QUIET)
+        if(TARGET npupnp::npupnp)
+            add_library(NPUPNP::NPUPNP ALIAS npupnp::npupnp)
+        endif()
     endif()
-    target_include_directories(libgerbera PUBLIC ${NPUPNP_INCLUDE_DIRS})
+    if(NOT TARGET NPUPNP::NPUPNP)
+        message(FATAL_ERROR "Gerbera failed to find libnpupnp! at least version ${REQ_UPNP_VERSION} is needed.")
+    endif()
+    # Conan autogenerated targets dont set a version, but to set name_VERSION var
+    get_target_property(_UPNP_VERSION NPUPNP::NPUPNP VERSION)
+    if (NOT _UPNP_VERSION)
+        set(_UPNP_VERSION ${npupnp_VERSION})
+    endif()
+    get_target_property(_UPNP_LIB NPUPNP::NPUPNP LOCATION)
+
+    # Check and warn for libnpupnp features
+    include(CheckCXXSymbolExists)
+    get_target_property(UPNP_INCLUDE_DIR NPUPNP::NPUPNP INTERFACE_INCLUDE_DIRECTORIES)
+    check_cxx_symbol_exists(UPNP_ENABLE_IPV6 "${UPNP_INCLUDE_DIR}/upnpconfig.h" UPNP_HAS_IPV6)
+    if (NOT UPNP_HAS_IPV6)
+        message(FATAL_ERROR "Gerbera requires libnpupnp with IPv6 support.")
+    endif()
+
+    check_cxx_symbol_exists(UPNP_HAVE_TOOLS "${UPNP_INCLUDE_DIR}/upnpconfig.h" UPNP_HAS_TOOLS)
+    if (NOT UPNP_HAS_TOOLS)
+        message(WARNING [=[
+    !! It is strongly recommended to build libupnp with --enable-tools !!
+        ]=])
+    endif()
+
+    message(STATUS "Found libnpupnp: ${_UPNP_LIB} (found version ${_UPNP_VERSION})")
+
     target_compile_definitions(libgerbera PUBLIC USING_NPUPNP)
-    target_link_libraries(libgerbera PUBLIC ${NPUPNP_LIBRARIES})
+    target_link_libraries(libgerbera PUBLIC NPUPNP::NPUPNP)
 else()
     set(REQ_UPNP_VERSION 1.14.6)
     # LibUPnP official target since 1.16 (Lib version 18)
new file mode 100644
index 000000000..b83168295
--- /dev/null
+++ cmake/FindNPUPNP.cmake
@@ -0,0 +1,54 @@
+# - Try to find LibNPUPnP (npupnp) 4.1.2
+# Once done this will define
+#  NPUPNP_FOUND - System has LibNPUPnP
+#  UPNP_INCLUDE_DIRS - The LibNPUPnP include directories
+#  NPUPNP_LIBRARIES - The libraries needed to use LibNPUPnP
+#  NPUPNP_VERSION_STRING - The version of LibNPUPnP found
+#  NPUPNP_HAS_IPV6 - If LibNPUPnP was built with IPv6 support
+#  NPUPNP_HAS_REUSEADDR - If LibNPUPnP was built with SO_REUSEADDR support
+find_package(PkgConfig QUIET)
+pkg_search_module (PC_NPUPNP libnpupnp QUIET)
+
+find_path(UPNP_INCLUDE_DIR upnp.h
+    HINTS ${PC_NPUPNP_INCLUDEDIR} ${PC_UPNP_INCLUDE_DIRS}
+    PATH_SUFFIXES npupnp)
+
+#if (STATIC_LIBUPNP)
+#    set(OLD_SUFFIX ${CMAKE_FIND_LIBRARY_SUFFIXES})
+#    set(CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_STATIC_LIBRARY_SUFFIX})
+#endif()
+
+find_library(NPUPNP_LIBRARY
+    NAMES libnpupnp npupnp
+    HINTS ${PC_NPUPNP_LIBDIR} ${PC_NPUPNP_LIBRARY_DIRS})
+
+# Restore
+#if (STATIC_LIBUPNP)
+#    set(CMAKE_FIND_LIBRARY_SUFFIXES ${OLD_SUFFIX})
+#endif()
+
+if(EXISTS ${UPNP_INCLUDE_DIR}/upnpconfig.h)
+    file (STRINGS ${UPNP_INCLUDE_DIR}/upnpconfig.h upnp_ver_str REGEX "^#define[ \t]+NPUPNP_VERSION_STRING[ \t]+\".+\"")
+    string(REGEX REPLACE "^#define[ \t]+NPUPNP_VERSION_STRING[ \t]+\"([^\"]+)\".*" "\\1" NPUPNP_VERSION "${upnp_ver_str}")
+endif()
+
+include(FindPackageHandleStandardArgs)
+FIND_PACKAGE_HANDLE_STANDARD_ARGS(NPUPNP
+    REQUIRED_VARS NPUPNP_LIBRARY UPNP_INCLUDE_DIR
+    VERSION_VAR NPUPNP_VERSION)
+
+if (NPUPNP_FOUND)
+    if(NOT TARGET NPUPNP::NPUPNP)
+        add_library(NPUPNP::NPUPNP SHARED IMPORTED)
+        set_target_properties(NPUPNP::NPUPNP PROPERTIES
+                IMPORTED_LOCATION ${NPUPNP_LIBRARY}
+                INTERFACE_INCLUDE_DIRECTORIES ${UPNP_INCLUDE_DIR}
+                VERSION ${NPUPNP_VERSION}
+        )
+    endif()
+endif ()
+
+MARK_AS_ADVANCED(
+    UPNP_INCLUDE_DIR
+    NPUPNP_LIBRARY
+)
diff --git a/cmake/FindUPNP.cmake b/cmake/FindUPNP.cmake
index 587e1c5ef..246328806 100644
--- cmake/FindUPNP.cmake
+++ cmake/FindUPNP.cmake
@@ -3,9 +3,9 @@
 #  UPNP_FOUND - System has LibUPnP
 #  UPNP_INCLUDE_DIRS - The LibUPnP include directories
 #  UPNP_LIBRARIES - The libraries needed to use LibUPnP
-#  UPNP_VERSION_STRING - The version of LinUPnP found
-#  UPNP_HAS_IPV6 - If LinUPnP was built with IPv6 support
-#  UPNP_HAS_REUSEADDR - If LinUPnP was built with SO_REUSEADDR support
+#  UPNP_VERSION_STRING - The version of LibUPnP found
+#  UPNP_HAS_IPV6 - If LibUPnP was built with IPv6 support
+#  UPNP_HAS_REUSEADDR - If LibUPnP was built with SO_REUSEADDR support
 find_package(PkgConfig QUIET)
 pkg_search_module (PC_UPNP QUIET libupnp)
 
diff --git a/scripts/versions.sh b/scripts/versions.sh
index c859e701b..86612f3e3 100755
--- scripts/versions.sh
+++ scripts/versions.sh
@@ -30,6 +30,7 @@ if [[ "${GERBERA_ENV-head}" == "minimum" ]]; then
     MATROSKA="1.5.2"
     PUGIXML="1.10"
     PUPNP="1.14.6"
+    NPUPNP="4.2.1"
     SPDLOG="1.8.1"
     WAVPACK="5.1.0"
     TAGLIB="1.12"
@@ -46,6 +47,7 @@ elif [[ "${GERBERA_ENV-head}" == "default" ]]; then
     MATROSKA="1.5.2"
     PUGIXML="1.10"
     PUPNP="1.14.17"
+    NPUPNP="5.1.2"
     SPDLOG="1.11.0"
     WAVPACK="5.4.0"
     TAGLIB="1.12"
@@ -63,8 +65,8 @@ else
     PUGIXML="1.14"
     PUPNP="1.14.18"
     SPDLOG="1.12.0"
+    NPUPNP="6.1.0"
     WAVPACK="5.6.0"
     TAGLIB="1.13.1"
 
 fi
-
diff --git a/src/iohandler/process_io_handler.cc b/src/iohandler/process_io_handler.cc
index de174eab2..3ff9e8099 100644
--- src/iohandler/process_io_handler.cc
+++ src/iohandler/process_io_handler.cc
@@ -39,6 +39,7 @@
 
 #include <fcntl.h>
 #include <sys/select.h>
+#include <unistd.h>
 
 #include "content/content_manager.h"
 
diff --git a/src/server.cc b/src/server.cc
index c8a0c2c75..bfd5d6477 100644
--- src/server.cc
+++ src/server.cc
@@ -231,8 +231,10 @@ void Server::run()
         throw UpnpException(ret, fmt::format("run: UpnpSendAdvertisement {} failed", aliveAdvertisementInterval));
     }
 
+#if !defined(USING_NPUPNP)
     if (config->getBoolOption(CFG_UPNP_LITERAL_HOST_REDIRECTION))
         UpnpSetAllowLiteralHostRedirection(1);
+#endif
 
     UpnpSetHostValidateCallback(
         [](auto host, auto cookie) -> int {
diff --git a/src/server.h b/src/server.h
index a91466b65..5826a1a77 100644
--- src/server.h
+++ src/server.h
@@ -34,6 +34,8 @@
 #ifndef __SERVER_H__
 #define __SERVER_H__
 
+#include <netinet/in.h>
+
 #include "request_handler.h"
 
 // forward declaration
