commit 06562e9f0e1eef14214dfc691c945c8be164e545 Author: King_DuckZ Date: Thu Aug 25 00:47:22 2016 +0200 First commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..25a06b8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +build/ +*.swp +compile_commands.json diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..b9fa25b --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "lib/miniupnp"] + path = lib/miniupnp + url = https://github.com/miniupnp/miniupnp.git diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..df9508b --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,30 @@ +cmake_minimum_required(VERSION 3.2 FATAL_ERROR) +project(keepupnpup CXX) + +if (BUILD_SHARED_LIBS) + set(upnp_lib_type "shared") + set(UPNPC_BUILD_STATIC OFF CACHE BOOL "" FORCE) + set(UPNPC_BUILD_SHARED ON CACHE BOOL "" FORCE) +else() + set(upnp_lib_type "static") + set(UPNPC_BUILD_STATIC ON CACHE BOOL "" FORCE) + set(UPNPC_BUILD_SHARED OFF CACHE BOOL "" FORCE) +endif() +set(UPNPC_BUILD_TESTS OFF CACHE BOOL "" FORCE) +set(CXX_STANDARD_REQUIRED ON) + +add_subdirectory(lib/miniupnp/miniupnpc) + +add_executable(${PROJECT_NAME} + src/main.cpp +) + +target_include_directories(${PROJECT_NAME} + PRIVATE lib/miniupnp/miniupnpc +) + +target_link_libraries(${PROJECT_NAME} + libminiupnpc-${upnp_lib_type} +) + +set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11) diff --git a/lib/miniupnp b/lib/miniupnp new file mode 160000 index 0000000..28d9c8e --- /dev/null +++ b/lib/miniupnp @@ -0,0 +1 @@ +Subproject commit 28d9c8e44884dea3785c533b9ef3fda6bbedc984 diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..0b19c79 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,70 @@ +#include +#include "miniupnpc.h" +#include "upnpcommands.h" +#include "upnperrors.h" +#include +#include + +namespace { + class UPNPUrlsResource { + public: + UPNPUrlsResource () : + m_initialized(false) + { + } + + ~UPNPUrlsResource() noexcept { + if (m_initialized) { + m_initialized = false; + FreeUPNPUrls(&m_urls); + } + } + + struct UPNPUrls* urls() { return &m_urls; } + + private: + struct UPNPUrls m_urls; + bool m_initialized; + }; + + using UPNPDevsPtr = std::unique_ptr; + + std::vector dev_list_to_vector (struct UPNPDev* parList) { + std::vector retval; + for (auto dev = parList; dev; dev = dev->pNext) { + retval.push_back(dev); + } + return retval; + } +} //unnamed namespace + +int main() { + int error; + UPNPDevsPtr devlist(nullptr, &freeUPNPDevlist); + + devlist.reset(upnpDiscover( + 2000, + nullptr, //multicastif + nullptr, //minissdpdpath + UPNP_LOCAL_PORT_ANY, //localport + 0, //ipv6 + 2, //ttl + &error + )); + + const auto devs = dev_list_to_vector(devlist.get()); + if (devs.empty()) { + std::cerr << "No UPNP devices found\n"; + return 1; + } + else if (devs.size() > 1) { + std::cerr << "More than one UPNP device found (" << devs.size() << "), aborting\n"; + return 1; + } + + std::cout << "Found: " << devs.front()->descURL << ", " << devs.front()->st << '\n'; + //struct IGDdatas data; + + return 0; +} +