mirror of
https://bitbucket.org/King_DuckZ/keepupnpup.git
synced 2024-11-07 21:29:00 +00:00
First commit
This commit is contained in:
commit
06562e9f0e
5 changed files with 107 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
build/
|
||||
*.swp
|
||||
compile_commands.json
|
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
[submodule "lib/miniupnp"]
|
||||
path = lib/miniupnp
|
||||
url = https://github.com/miniupnp/miniupnp.git
|
30
CMakeLists.txt
Normal file
30
CMakeLists.txt
Normal file
|
@ -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)
|
1
lib/miniupnp
Submodule
1
lib/miniupnp
Submodule
|
@ -0,0 +1 @@
|
|||
Subproject commit 28d9c8e44884dea3785c533b9ef3fda6bbedc984
|
70
src/main.cpp
Normal file
70
src/main.cpp
Normal file
|
@ -0,0 +1,70 @@
|
|||
#include <iostream>
|
||||
#include "miniupnpc.h"
|
||||
#include "upnpcommands.h"
|
||||
#include "upnperrors.h"
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
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<struct UPNPDev, void(*)(struct UPNPDev*)>;
|
||||
|
||||
std::vector<struct UPNPDev*> dev_list_to_vector (struct UPNPDev* parList) {
|
||||
std::vector<struct UPNPDev*> 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;
|
||||
}
|
||||
|
Loading…
Reference in a new issue