diff --git a/UI/GameSettingsScreen.cpp b/UI/GameSettingsScreen.cpp index 9d1012f97b..0aaff82ee0 100644 --- a/UI/GameSettingsScreen.cpp +++ b/UI/GameSettingsScreen.cpp @@ -688,7 +688,7 @@ void GameSettingsScreen::CreateViews() { networkingSettings->Add(new CheckBox(&g_Config.bEnableWlan, n->T("Enable networking", "Enable networking/wlan (beta)"))); networkingSettings->Add(new CheckBox(&g_Config.bDiscordPresence, n->T("Send Discord Presence information"))); - networkingSettings->Add(new ChoiceWithValueDisplay(&g_Config.proAdhocServer, n->T("Change proAdhocServer Address"), (const char *)nullptr))->OnClick.Handle(this, &GameSettingsScreen::OnChangeproAdhocServerAddress); + networkingSettings->Add(new ChoiceWithValueDisplay(&g_Config.proAdhocServer, n->T("Change proAdhocServer Address (localhost = multiple instance)"), (const char *)nullptr))->OnClick.Handle(this, &GameSettingsScreen::OnChangeproAdhocServerAddress); networkingSettings->Add(new CheckBox(&g_Config.bEnableAdhocServer, n->T("Enable built-in PRO Adhoc Server", "Enable built-in PRO Adhoc Server"))); networkingSettings->Add(new ChoiceWithValueDisplay(&g_Config.sMACAddress, n->T("Change Mac Address"), (const char *)nullptr))->OnClick.Handle(this, &GameSettingsScreen::OnChangeMacAddress); static const char* wlanChannels[] = { "Auto", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11" }; @@ -1391,17 +1391,9 @@ UI::EventReturn GameSettingsScreen::OnChangeNickname(UI::EventParams &e) { } UI::EventReturn GameSettingsScreen::OnChangeproAdhocServerAddress(UI::EventParams &e) { - auto sy = GetI18NCategory("System"); + auto n = GetI18NCategory("Networking"); -#if defined(__ANDROID__) - System_InputBoxGetString(sy->T("proAdhocServer Address:"), g_Config.proAdhocServer, [](bool result, const std::string &value) { - if (result) { - g_Config.proAdhocServer = value; - } - }); -#else - screenManager()->push(new HostnameSelectScreen(&g_Config.proAdhocServer, sy->T("proAdhocServer Address:"))); -#endif + screenManager()->push(new HostnameSelectScreen(&g_Config.proAdhocServer, n->T("proAdhocServer Address:"))); return UI::EVENT_DONE; } @@ -1698,10 +1690,35 @@ void HostnameSelectScreen::CreatePopupContents(UI::ViewGroup *parent) { buttonsRow1->Add(new Spacer(new LinearLayoutParams(1.0, G_RIGHT))); buttonsRow2->Add(new Spacer(new LinearLayoutParams(1.0, G_LEFT))); +#if defined(__ANDROID__) + buttonsRow2->Add(new Button(di->T("Edit")))->OnClick.Handle(this, &HostnameSelectScreen::OnEditClick); // Since we don't have OnClick Event triggered from TextEdit's Touch().. here we go! +#endif buttonsRow2->Add(new Button(di->T("Delete")))->OnClick.Handle(this, &HostnameSelectScreen::OnDeleteClick); buttonsRow2->Add(new Button(di->T("Delete all")))->OnClick.Handle(this, &HostnameSelectScreen::OnDeleteAllClick); + buttonsRow2->Add(new Button(di->T("Toggle List")))->OnClick.Handle(this, &HostnameSelectScreen::OnShowIPListClick); buttonsRow2->Add(new Spacer(new LinearLayoutParams(1.0, G_RIGHT))); + std::vector listIP = {"localhost","myneighborsushicat.com"}; + net::GetIPList(listIP); + ipRows_ = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(1.0)); + ScrollView* scrollView = new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT)); + LinearLayout* innerView = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT)); + if (listIP.size() > 0) { + for (const auto& label : listIP) { + // Filter out IP prefixed with "127." and "169.254." also "0." since they can be rendundant or unusable + if (label.find("127.") != 0 && label.find("169.254.") != 0 && label.find("0.") != 0) { + auto button = innerView->Add(new Button(label, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT))); + button->OnClick.Handle(this, &HostnameSelectScreen::OnIPClick); + button->SetTag(label); + } + } + } + scrollView->Add(innerView); + ipRows_->Add(scrollView); + ipRows_->SetVisibility(V_GONE); + parent->Add(ipRows_); + listIP.clear(); listIP.shrink_to_fit(); + errorView_ = parent->Add(new TextView(n->T("Invalid IP or hostname"), ALIGN_HCENTER, false, new LinearLayoutParams(Margins(0, 10, 0, 0)))); errorView_->SetTextColor(0xFF3030FF); errorView_->SetVisibility(V_GONE); @@ -1741,6 +1758,38 @@ UI::EventReturn HostnameSelectScreen::OnDeleteAllClick(UI::EventParams &e) { return UI::EVENT_DONE; } +UI::EventReturn HostnameSelectScreen::OnEditClick(UI::EventParams& e) { + auto n = GetI18NCategory("Networking"); +#if defined(__ANDROID__) + System_InputBoxGetString(n->T("proAdhocServer Address:"), addrView_->GetText(), [this](bool result, const std::string& value) { + if (result) { + addrView_->SetText(value); + } + }); +#endif + return UI::EVENT_DONE; +} + +UI::EventReturn HostnameSelectScreen::OnShowIPListClick(UI::EventParams& e) { + if (ipRows_->GetVisibility() == UI::V_GONE) { + ipRows_->SetVisibility(UI::V_VISIBLE); + } + else { + ipRows_->SetVisibility(UI::V_GONE); + } + return UI::EVENT_DONE; +} + +UI::EventReturn HostnameSelectScreen::OnIPClick(UI::EventParams& e) { + std::string text = e.v ? e.v->Tag() : ""; + if (text.length() > 0) { + addrView_->SetText(text); + // TODO: Copy the IP to clipboard for the host to easily share their IP through chatting apps. + System_SendMessage("setclipboardtext", text.c_str()); // Doesn't seems to be working on windows (yet?) + } + return UI::EVENT_DONE; +} + void HostnameSelectScreen::ResolverThread() { std::unique_lock guard(resolverLock_); diff --git a/UI/GameSettingsScreen.h b/UI/GameSettingsScreen.h index cfcd7fae27..2279b620ce 100644 --- a/UI/GameSettingsScreen.h +++ b/UI/GameSettingsScreen.h @@ -208,6 +208,9 @@ private: UI::EventReturn OnPointClick(UI::EventParams &e); UI::EventReturn OnDeleteClick(UI::EventParams &e); UI::EventReturn OnDeleteAllClick(UI::EventParams &e); + UI::EventReturn OnEditClick(UI::EventParams& e); + UI::EventReturn OnShowIPListClick(UI::EventParams& e); + UI::EventReturn OnIPClick(UI::EventParams& e); enum class ResolverState { WAITING, @@ -221,6 +224,7 @@ private: UI::TextEdit *addrView_ = nullptr; UI::TextView *errorView_ = nullptr; UI::TextView *progressView_ = nullptr; + UI::LinearLayout *ipRows_ = nullptr; std::thread resolver_; ResolverState resolverState_ = ResolverState::WAITING; diff --git a/ext/native/net/resolve.cpp b/ext/native/net/resolve.cpp index a5bd024c7a..c4fc78fc62 100644 --- a/ext/native/net/resolve.cpp +++ b/ext/native/net/resolve.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #ifdef _WIN32 @@ -101,6 +102,105 @@ void DNSResolveFree(addrinfo *res) freeaddrinfo(res); } +bool GetIPList(std::vector& IP4s) { + char ipstr[INET6_ADDRSTRLEN]; // We use IPv6 length since it's longer than IPv4 +#if defined(getifaddrs) // On Android: Requires __ANDROID_API__ >= 24 + struct ifaddrs* ifAddrStruct = NULL; + struct ifaddrs* ifa = NULL; + + getifaddrs(&ifAddrStruct); + if (ifAddrStruct != NULL) { + for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) { + if (!ifa->ifa_addr) { + continue; + } + if (ifa->ifa_addr->sa_family == AF_INET) { + // is a valid IP4 Address + if (inet_ntop(AF_INET, &((struct sockaddr_in*)ifa->ifa_addr)->sin_addr, ipstr, sizeof(ipstr)) != 0) { + IP4s.push_back(ipstr); + } + } + /*else if (ifa->ifa_addr->sa_family == AF_INET6) { + // is a valid IP6 Address + if (inet_ntop(AF_INET6, &((struct sockaddr_in6*)ifa->ifa_addr)->sin6_addr, ipstr, sizeof(ipstr)) != 0) { + IP6s.push_back(ipstr); + } + }*/ + } + + freeifaddrs(ifAddrStruct); + return true; + } +#elif defined(SIOCGIFCONF) // Better detection on Linux/UNIX/MacOS/some Android +#include + static struct ifreq ifreqs[32]; + struct ifconf ifconf; + memset(&ifconf, 0, sizeof(ifconf)); + ifconf.ifc_req = ifreqs; + ifconf.ifc_len = sizeof(ifreqs); + + int sd = socket(AF_INET, SOCK_DGRAM, 0); + if (sd < 0) return false; + + int r = ioctl(sd, SIOCGIFCONF, (char*)&ifconf); + if (r != 0) return false; + + for (int i = 0; i < ifconf.ifc_len / sizeof(struct ifreq); ++i) + { + if (ifreqs[i].ifr_addr.sa_family == AF_INET) { + // is a valid IP4 Address + if (inet_ntop(AF_INET, &((struct sockaddr_in*)&ifreqs[i].ifr_addr)->sin_addr, ipstr, sizeof(ipstr)) != 0) { + IP4s.push_back(ipstr); + } + } + /*else if (ifreqs[i].ifr_addr.sa_family == AF_INET6) { + // is a valid IP6 Address + if (inet_ntop(AF_INET6, &((struct sockaddr_in6*)&ifreqs[i].ifr_addr)->sin6_addr, ipstr, sizeof(ipstr)) != 0) { + IP6s.push_back(ipstr); + } + }*/ + } + + close(sd); + return true; +#else // Fallback to POSIX/Cross-platform way but may not works well on Linux (ie. only shows 127.0.0.1) + struct addrinfo hints, * res, * p; + memset(&hints, 0, sizeof hints); + hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version + hints.ai_socktype = SOCK_DGRAM; + + // Get local host name + char szHostName[256] = ""; + if (::gethostname(szHostName, sizeof(szHostName))) { + // Error handling + } + + int status; + if ((status = getaddrinfo(szHostName, NULL, &hints, &res)) != 0) { + //fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status)); + return false; + } + for (p = res; p != NULL; p = p->ai_next) { + if (p->ai_family == AF_INET) { + // is a valid IP4 Address + if (inet_ntop(p->ai_family, &(((struct sockaddr_in*)p->ai_addr)->sin_addr), ipstr, sizeof(ipstr)) != 0) { + IP4s.push_back(ipstr); + } + } + /*else if (p->ai_family == AF_INET6) { + // is a valid IP6 Address + if (inet_ntop(p->ai_family, &(((struct sockaddr_in6*)p->ai_addr)->sin6_addr), ipstr, sizeof(ipstr)) != 0) { + IP6s.push_back(ipstr); + } + }*/ + } + + freeaddrinfo(res); // free the linked list + return true; +#endif + return false; +} + int inet_pton(int af, const char* src, void* dst) { if (af == AF_INET) diff --git a/ext/native/net/resolve.h b/ext/native/net/resolve.h index 58ce0984d9..8cc9756392 100644 --- a/ext/native/net/resolve.h +++ b/ext/native/net/resolve.h @@ -1,6 +1,7 @@ #pragma once #include +#include struct addrinfo; @@ -22,6 +23,7 @@ enum class DNSType { bool DNSResolve(const std::string &host, const std::string &service, addrinfo **res, std::string &error, DNSType type = DNSType::ANY); void DNSResolveFree(addrinfo *res); +bool GetIPList(std::vector& IP4s); int inet_pton(int af, const char* src, void* dst); } // namespace net