RunicVTT Open Source Virtual Tabletop for TTRPG using P2P
Loading...
Searching...
No Matches
UPnPManager Class Reference

#include <UPnPManager.h>

Collaboration diagram for UPnPManager:

Static Public Member Functions

static std::string getExternalIPv4Address ()
 
static std::string getLocalIPv4Address ()
 
static bool addPortMapping (const std::string &internalIp, unsigned short internalPort, unsigned short externalPort, const std::string &protocol, const std::string &description="", unsigned int duration=0)
 
static bool removePortMapping (unsigned short externalPort, const std::string &protocol)
 

Static Private Member Functions

static std::wstring stringToWString (const std::string &s)
 
static std::string toUpper (std::string s)
 

Detailed Description

Definition at line 16 of file UPnPManager.h.

Member Function Documentation

◆ addPortMapping()

static bool UPnPManager::addPortMapping ( const std::string & internalIp,
unsigned short internalPort,
unsigned short externalPort,
const std::string & protocol,
const std::string & description = "",
unsigned int duration = 0 )
inlinestatic

Definition at line 60 of file UPnPManager.h.

68 {
69 std::wostringstream cmd;
70 // Use your PathManager to get the path
71 cmd << L"\"" << PathManager::getUpnpcExePath().wstring() << L"\" -a " // .wstring() for std::wostringstream
72 << stringToWString(internalIp) << L" "
73 << internalPort << L" "
74 << externalPort << L" "
75 << stringToWString(toUpper(protocol)); // Ensure protocol is uppercase
76
77 if (!description.empty())
78 {
79 cmd << L" \"" << stringToWString(description) << L"\"";
80 }
81 else
82 {
83 cmd << L" \"\""; // Pass empty string if no description for consistency
84 }
85 cmd << L" " << duration;
86
87 std::wcout << L"Executing UPNP command: " << cmd.str() << std::endl; // For debugging
88 int result = _wsystem(cmd.str().c_str());
89
90 if (result != 0)
91 {
92 std::wcerr << L"UPNP addPortMapping command failed. _wsystem returned: " << result << std::endl;
93 }
94 return result == 0;
95 }
static fs::path getUpnpcExePath()
static std::wstring stringToWString(const std::string &s)
static std::string toUpper(std::string s)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getExternalIPv4Address()

static std::string UPnPManager::getExternalIPv4Address ( )
inlinestatic

Definition at line 19 of file UPnPManager.h.

20 {
21 return std::string("NOT IMPLEMENTED YET!!");
22 }

◆ getLocalIPv4Address()

static std::string UPnPManager::getLocalIPv4Address ( )
inlinestatic

Definition at line 24 of file UPnPManager.h.

25 {
26 std::string ipAddress = "";
27 std::filesystem::path scriptPath = PathManager::getResPath() / "GetCorrectIPv4.ps1";
28 std::string command = "powershell.exe -NoProfile -ExecutionPolicy Bypass -File \"" + scriptPath.string() + "\"";
29
30 FILE* pipe = _popen(command.c_str(), "r");
31 if (!pipe)
32 {
33 std::cerr << "Erro: Falha ao executar o script PowerShell. Comando: " << command << std::endl;
34 return ipAddress; // Retorna string vazia em caso de falha ao iniciar o processo
35 }
36
37 std::array<char, 256> buffer; // Buffer para ler a linha
38 std::string line;
39
40 if (fgets(buffer.data(), buffer.size(), pipe) != nullptr)
41 {
42 line = buffer.data();
43 line.erase(0, line.find_first_not_of(" \t\r\n"));
44 line.erase(line.find_last_not_of(" \t\r\n") + 1);
45 ipAddress = line;
46 }
47
48 _pclose(pipe);
49 std::regex ipv4_format_regex("^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$");
50 if (!std::regex_match(ipAddress, ipv4_format_regex))
51 {
52 std::cerr << "Aviso: O script PowerShell retornou algo que não parece um IPv4 válido ou nada: '" << ipAddress << "'" << std::endl;
53 return ""; // Retorna string vazia se o formato não for válido ou se nenhum IP foi encontrado
54 }
55
56 return ipAddress;
57 }
static fs::path getResPath()
Here is the call graph for this function:

◆ removePortMapping()

static bool UPnPManager::removePortMapping ( unsigned short externalPort,
const std::string & protocol )
inlinestatic

Definition at line 98 of file UPnPManager.h.

102 {
103 std::wostringstream cmd;
104 // Use your PathManager to get the path
105 cmd << L"\"" << PathManager::getUpnpcExePath().wstring() << L"\" -d "
106 << externalPort << L" "
107 << stringToWString(toUpper(protocol)); // Ensure protocol is uppercase
108
109 std::wcout << L"Executing UPNP command: " << cmd.str() << std::endl; // For debugging
110 int result = _wsystem(cmd.str().c_str());
111
112 if (result != 0)
113 {
114 std::wcerr << L"UPNP removePortMapping command failed. _wsystem returned: " << result << std::endl;
115 }
116 return result == 0;
117 }
Here is the call graph for this function:

◆ stringToWString()

static std::wstring UPnPManager::stringToWString ( const std::string & s)
inlinestaticprivate

Definition at line 121 of file UPnPManager.h.

122 {
123 int len;
124 // +1 for null terminator, assuming s.length() is byte length.
125 // For UTF-8, this is safer, but if you're dealing with raw bytes, length might be different.
126 // For general usage with ASCII/Latin-1 strings in arguments, this is fine.
127 int slength = (int)s.length() + 1;
128 len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
129 std::vector<wchar_t> buf(len);
130 MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, &buf[0], len);
131 return std::wstring(&buf[0]);
132 }
Here is the caller graph for this function:

◆ toUpper()

static std::string UPnPManager::toUpper ( std::string s)
inlinestaticprivate

Definition at line 135 of file UPnPManager.h.

136 {
137 std::transform(s.begin(), s.end(), s.begin(),
138 [](unsigned char c)
139 { return static_cast<unsigned char>(std::toupper(c)); });
140 return s;
141 }
Here is the caller graph for this function:

The documentation for this class was generated from the following file: