gui/settings dialog: Add adhoc network configuration.

util/net utils: Add fonctions to get ip and network card name.
- Add func for init netAddr and boracastAddr.

Co-authored-by: EXtremeExploit <pedro.montes.alcalde@gmail.com>
This commit is contained in:
Zangetsu38
2024-12-18 13:54:50 -06:00
committed by bookmist
parent d01dda60cf
commit ae8607435f
4 changed files with 143 additions and 0 deletions
+1
View File
@@ -160,6 +160,7 @@ enum ScreenshotFormat {
code(int, "http-timeout-sleep-ms", 100, http_timeout_sleep_ms) \
code(int, "http-read-end-attempts", 10, http_read_end_attempts) \
code(int, "http-read-end-sleep-ms", 250, http_read_end_sleep_ms) \
code(int, "adhoc-addr", 0, adhoc_addr) \
code(bool, "tracy-primitive-impl", false, tracy_primitive_impl)
// Vector members produced in the config file
+34
View File
@@ -38,6 +38,7 @@
#include <string>
#include <util/fs.h>
#include <util/log.h>
#include <util/net_utils.h>
#include <SDL3/SDL_video.h>
@@ -1065,6 +1066,39 @@ void draw_settings_dialog(GuiState &gui, EmuEnvState &emuenv) {
ImGui::Checkbox(lang.network["psn_signed_in"].c_str(), &config.psn_signed_in);
SetTooltipEx(lang.network["psn_signed_in_description"].c_str());
// Adhoc
ImGui::Spacing();
ImGui::Separator();
ImGui::Spacing();
TextColoredCentered(GUI_COLOR_TEXT_MENUBAR, "Adhoc");
ImGui::Spacing();
std::vector<std::string> addrsStrings;
std::vector<const char *> addrsSelect;
std::vector<const char *> nMaskSelect;
const auto addrs = net_utils::get_all_assigned_addrs();
for (const auto &addr : addrs) {
addrsStrings.emplace_back(fmt::format("{} ({})", addr.addr, addr.name).c_str());
addrsSelect.emplace_back(addrsStrings.back().c_str());
nMaskSelect.emplace_back(addr.netMask.c_str());
}
if (emuenv.cfg.adhoc_addr >= addrs.size()) {
emuenv.cfg.adhoc_addr = 0;
LOG_WARN("Invalid adhoc address index {}, resetting to 0", emuenv.cfg.adhoc_addr);
save_config(gui, emuenv);
}
ImGui::PushItemWidth(ImGui::CalcTextSize(addrsStrings[emuenv.cfg.adhoc_addr].c_str()).x + (30.f * SCALE.x));
ImGui::Combo("Network Address", &emuenv.cfg.adhoc_addr, addrsSelect.data(), static_cast<int>(addrsSelect.size()));
SetTooltipEx("Select which Address to use in adhoc.");
ImGui::BeginDisabled();
ImGui::Combo("Network Mask", &emuenv.cfg.adhoc_addr, nMaskSelect.data(), static_cast<int>(nMaskSelect.size()));
ImGui::EndDisabled();
ImGui::PopItemWidth();
// HTTP
ImGui::Spacing();
ImGui::Separator();
+11
View File
@@ -44,6 +44,13 @@ struct ProgressData {
uint64_t time;
uint64_t bytes_already_downloaded;
};
struct AssignedAddr {
std::string name; // Name of the interface
std::string addr; // Assigned address
std::string netMask; // Network mask
};
typedef const std::function<ProgressState *(float, uint64_t)> &ProgressCallback;
typedef std::pair<ProgressData, ProgressCallback> CallbackData;
@@ -61,4 +68,8 @@ bool parseResponse(const std::string &response, SceRequestResponse &reqres);
bool socketSetBlocking(int sockfd, bool blocking);
std::vector<AssignedAddr> get_all_assigned_addrs();
AssignedAddr get_selected_assigned_addr(int32_t &outIndex);
void init_address(int32_t &outIndex, uint32_t &netAddr, uint32_t &broadcastAddr);
} // namespace net_utils
+97
View File
@@ -15,14 +15,20 @@
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#include <util/log.h>
#include <util/net_utils.h>
#include <curl/curl.h>
#ifdef _WIN32
#include <iphlpapi.h>
#include <winsock2.h>
#else
#include <arpa/inet.h>
#include <fcntl.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <netinet/in.h>
#endif
#include <condition_variable>
@@ -471,4 +477,95 @@ bool download_file(const std::string &url, const std::string &output_file_path,
return res == CURLE_OK;
}
std::vector<AssignedAddr> get_all_assigned_addrs() {
std::vector<AssignedAddr> out_addrs;
const auto ret_addrs = [&out_addrs]() {
if (out_addrs.empty())
out_addrs.push_back({ "localhost", "127.0.0.1", "255.255.255.255" });
return out_addrs;
};
#ifdef _WIN32
PIP_ADAPTER_INFO pAdapterInfo;
DWORD dwRetVal = 0;
UINT i;
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
pAdapterInfo = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));
if (pAdapterInfo == NULL) {
LOG_CRITICAL("Error allocating memory needed to call GetAdaptersinfo");
return ret_addrs();
}
// Make an initial call to GetAdaptersInfo to get the necessary size into the ulOutBufLen variable
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
free(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *)malloc(ulOutBufLen);
if (pAdapterInfo == NULL) {
LOG_CRITICAL("Error allocating memory needed to call GetAdaptersinfo");
return ret_addrs();
}
}
if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
PIP_ADAPTER_INFO pAdapter = pAdapterInfo;
const std::string noAddress = "0.0.0.0";
while (pAdapter) {
IP_ADDR_STRING *pIPAddr = &pAdapter->IpAddressList;
while (pIPAddr) {
if (noAddress.compare(pIPAddr->IpAddress.String) != 0)
out_addrs.push_back({ pAdapter->Description, pIPAddr->IpAddress.String, pIPAddr->IpMask.String });
pIPAddr = pIPAddr->Next;
}
pAdapter = pAdapter->Next;
}
} else {
LOG_CRITICAL("GetAdaptersInfo failed with error: {}", dwRetVal);
}
#else
struct ifaddrs *ifAddrStruct = NULL;
struct ifaddrs *ifa = NULL;
void *tmpAddrPtr = NULL;
getifaddrs(&ifAddrStruct);
for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
if (!ifa->ifa_addr)
continue;
if ((ifa->ifa_flags & IFF_LOOPBACK) != 0)
continue;
if (ifa->ifa_flags)
if (ifa->ifa_addr->sa_family == AF_INET) { // check it is IP4
char netMaskAddrStr[INET_ADDRSTRLEN];
auto netMaskAddr = ((sockaddr_in *)ifa->ifa_netmask)->sin_addr;
inet_ntop(AF_INET, &netMaskAddr, netMaskAddrStr, INET_ADDRSTRLEN);
// is a valid IP4 Address
tmpAddrPtr = &((sockaddr_in *)ifa->ifa_addr)->sin_addr;
char addressBuffer[INET_ADDRSTRLEN];
inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
out_addrs.push_back({ ifa->ifa_name, addressBuffer, netMaskAddrStr });
}
}
if (ifAddrStruct != NULL)
freeifaddrs(ifAddrStruct);
#endif
return ret_addrs();
}
AssignedAddr get_selected_assigned_addr(int32_t &outIndex) {
const auto addrs = get_all_assigned_addrs();
if (outIndex >= addrs.size()) {
LOG_ERROR("Invalid index {}, returning first address", outIndex);
outIndex = 0;
}
return addrs[outIndex];
}
void init_address(int32_t &outIndex, uint32_t &netAddr, uint32_t &broadcastAddr) {
// Initialize the net and broadcast address based on the assigned address and netmask
const auto addr = get_selected_assigned_addr(outIndex);
int netMask;
inet_pton(AF_INET, addr.addr.c_str(), &netAddr);
inet_pton(AF_INET, addr.netMask.c_str(), &netMask);
broadcastAddr = netAddr | ~netMask;
}
} // namespace net_utils