Skip to content

Commit

Permalink
fix: add Windows-specific library linking and improve error logging i…
Browse files Browse the repository at this point in the history
…n ParseArgument class
  • Loading branch information
MasterLaplace committed Dec 10, 2024
1 parent 5566f23 commit 996812c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ target_include_directories(flakkari PRIVATE ${singleton_SOURCE_DIR})
# Link Libraries:
target_link_libraries(flakkari PRIVATE nlohmann_json::nlohmann_json)

if (WIN32)
target_link_libraries(flakkari PRIVATE Iphlpapi)
endif()

# Documentation: sudo apt-get install graphviz
find_package(Doxygen)
if (DOXYGEN_FOUND)
Expand Down
36 changes: 27 additions & 9 deletions Flakkari/ParseArgument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ ParseArgument::ParseArgument(int ac, const char *av[])
else if (std::string(av[i]) == "-port")
{
_port = static_cast<unsigned short>(std::stoi(av[i + 1]));
if (_port < 1024 || _port > 65535)
if (_port < 1024 || (uint32_t)_port > 65535)
throw std::runtime_error("Invalid port number, must be between 1024 and 65535");
++i;
}
Expand Down Expand Up @@ -90,7 +90,7 @@ bool ParseArgument::GetIPv4Addresses()
{
free(pAdapterAddresses);
WSACleanup();
throw std::runtime_error("GetAdaptersAddresses failed with error: " + std::to_string(dwRetVal));
return FLAKKARI_LOG_ERROR("GetAdaptersAddresses failed with error: " + std::to_string(dwRetVal)), false;
}

IP_ADAPTER_ADDRESSES *pAdapter = pAdapterAddresses;
Expand All @@ -107,8 +107,8 @@ bool ParseArgument::GetIPv4Addresses()

char ipStr[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(sa_in->sin_addr), ipStr, INET_ADDRSTRLEN);
FLAKKARI_LOG_INFO("Adresse IPv4 de l'interface " + std::string(pAdapter->AdapterName) + ": " + ipStr);
_ip = ipStr;
FLAKKARI_LOG_INFO("Adresse IPv4 de l'interface " + std::string(pAdapter->AdapterName) + ": " + _ip);
}
pAdapter = pAdapter->Next;
}
Expand All @@ -120,7 +120,7 @@ bool ParseArgument::GetIPv4Addresses()
struct ifaddrs *ifa = nullptr;

if (getifaddrs(&ifAddrStruct) == -1)
throw std::runtime_error("getifaddrs failed!");
return FLAKKARI_LOG_ERROR("getifaddrs failed!"), false;

for (ifa = ifAddrStruct; ifa != nullptr; ifa = ifa->ifa_next)
{
Expand All @@ -132,11 +132,15 @@ bool ParseArgument::GetIPv4Addresses()
inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);

if (strcmp(addressBuffer, "127.0.0.1") != 0)
FLAKKARI_LOG_INFO("Interface " + ifa->ifa_name + " a l'adresse IPv4 : " + addressBuffer);
{
_ip = addressBuffer;
FLAKKARI_LOG_INFO("Interface " + std::string(ifa->ifa_name) + " a l'adresse IPv4 : " + _ip);
}
}

freeifaddrs(ifAddrStruct);
#endif
return true;
}

bool ParseArgument::GetIPv6Addresses()
Expand All @@ -162,7 +166,7 @@ bool ParseArgument::GetIPv6Addresses()
{
free(pAdapterAddresses);
WSACleanup();
throw std::runtime_error("GetAdaptersAddresses failed with error: " + std::to_string(dwRetVal));
return FLAKKARI_LOG_ERROR("GetAdaptersAddresses failed with error: " + std::to_string(dwRetVal)), false;
}

IP_ADAPTER_ADDRESSES *pAdapter = pAdapterAddresses;
Expand All @@ -181,8 +185,8 @@ bool ParseArgument::GetIPv6Addresses()
inet_ntop(AF_INET6, &(sa_in6->sin6_addr), ipStr, INET6_ADDRSTRLEN);
if (strcmp(ipStr, "::1") != 0)
{
FLAKKARI_LOG_INFO("IPv6 address of interface " + std::string(pAdapter->AdapterName) + ": " + ipStr);
_ip = ipStr;
FLAKKARI_LOG_INFO("IPv6 address of interface " + std::string(pAdapter->AdapterName) + ": " + _ip);
}
}
pAdapter = pAdapter->Next;
Expand All @@ -195,7 +199,7 @@ bool ParseArgument::GetIPv6Addresses()
struct ifaddrs *ifa = nullptr;

if (getifaddrs(&ifAddrStruct) == -1)
throw std::runtime_error("getifaddrs failed!");
return FLAKKARI_LOG_ERROR("getifaddrs failed!"), false;

for (ifa = ifAddrStruct; ifa != nullptr; ifa = ifa->ifa_next)
{
Expand All @@ -207,19 +211,33 @@ bool ParseArgument::GetIPv6Addresses()
inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);

if (strcmp(addressBuffer, "::1") != 0)
FLAKKARI_LOG_INFO("Interface " + ifa->ifa_name + " a l'adresse IPv6 : " + addressBuffer);
{
_ip = addressBuffer;
FLAKKARI_LOG_INFO("Interface " + std::cout(ifa->ifa_name) + " a l'adresse IPv6 : " + _ip);
}
}

freeifaddrs(ifAddrStruct);
#endif
return true;
}

void ParseArgument::GetGameDirEnv()
{
#if defined(FLAKKARI_SYSTEM_WINDOWS)
char *dir = nullptr;
size_t len = 0;
if (_dupenv_s(&dir, &len, "FLAKKARI_GAME_DIR") == 0 && dir != nullptr)
{
_gameDir = dir;
free(dir);
}
#else
const char *dir = std::getenv("FLAKKARI_GAME_DIR");

if (dir)
_gameDir = dir;
#endif
else
throw std::runtime_error("FLAKKARI_GAME_DIR not set");
}
Expand Down

0 comments on commit 996812c

Please sign in to comment.