reimplement srv record support, add server motd

This commit is contained in:
DrPerkyLegit committed 2026-11-24 22:22:14 -05:00
1 parent dd36e52221
commit 3ebbf6f243
10 files changed
+168 -49

No files matched your search

@@ -5,7 +5,9 @@
#include <winsock2.h>
#include <ws2tcpip.h>
#include <mstcpip.h>
#include <windns.h>
#include <algorithm>
#include <string.h>
#include "PlatformNetworkManagerWindows.h"
#include "WindowsNetworkConfig.h"
@@ -26,6 +28,7 @@ char g_Win64MultiplayerIP[256] = "127.0.0.1";
bool g_Win64DedicatedServer = false;
int g_Win64DedicatedServerPort = WIN64_NET_DEFAULT_PORT;
char g_Win64DedicatedServerBindIP[256] = "";
char g_Win64DedicatedServerDescription[1024] = "";
bool g_Win64DedicatedServerLanAdvertise = true;
namespace {
@@ -82,19 +85,101 @@ namespace {
return true;
}
enum SrvLookupResult {
SRV_LOOKUP_NOT_FOUND,
SRV_LOOKUP_RESOLVED,
SRV_LOOKUP_UNAVAILABLE
};
struct SrvEndpoint {
std::string host;
unsigned short port;
WORD weight;
};
SrvLookupResult ResolveMinecraftSrv(const char* host, std::string* targetHost, unsigned short* targetPort) {
in_addr numericAddress = {};
if (host == nullptr || targetHost == nullptr || targetPort == nullptr || inet_pton(AF_INET, host, &numericAddress) == 1)
return SRV_LOOKUP_NOT_FOUND;
const std::string queryName = std::string("_minecraft._tcp.") + host;
PDNS_RECORD queryRecords = nullptr;
DNS_STATUS status = DnsQuery_A(queryName.c_str(), DNS_TYPE_SRV, DNS_QUERY_STANDARD, nullptr, &queryRecords, nullptr);
if (status != ERROR_SUCCESS || queryRecords == nullptr) {
if (queryRecords != nullptr) DnsRecordListFree(queryRecords, DnsFreeRecordList);
return SRV_LOOKUP_NOT_FOUND;
}
DNS_RECORDA* records = reinterpret_cast<DNS_RECORDA*>(queryRecords);
std::vector<SrvEndpoint> endpoints;
bool serviceUnavailable = false;
WORD bestPriority = 0xFFFF;
for (DNS_RECORDA* record = records; record != nullptr; record = record->pNext) {
if (record->wType != DNS_TYPE_SRV) continue;
const DNS_SRV_DATAA& srv = record->Data.SRV;
if (srv.pNameTarget == nullptr) continue;
if (srv.wPriority < bestPriority) {
bestPriority = srv.wPriority;
endpoints.clear();
serviceUnavailable = false;
}
if (srv.wPriority > bestPriority) continue;
if (strcmp(srv.pNameTarget, ".") == 0) {
serviceUnavailable = true;
endpoints.clear();
continue;
}
if (srv.wPort == 0 || serviceUnavailable) continue;
endpoints.push_back({ srv.pNameTarget, srv.wPort, srv.wWeight });
}
DnsRecordListFree(queryRecords, DnsFreeRecordList);
if (endpoints.empty()) return serviceUnavailable ? SRV_LOOKUP_UNAVAILABLE : SRV_LOOKUP_NOT_FOUND;
unsigned int totalWeight = 0;
for (size_t index = 0; index < endpoints.size(); ++index) totalWeight += endpoints[index].weight;
size_t selectedIndex = 0;
if (totalWeight == 0) {
selectedIndex = static_cast<size_t>(GetTickCount64() % endpoints.size());
} else {
unsigned int choice = static_cast<unsigned int>(GetTickCount64() % totalWeight);
for (size_t index = 0; index < endpoints.size(); ++index) {
if (choice < endpoints[index].weight) {
selectedIndex = index;
break;
}
choice -= endpoints[index].weight;
}
}
*targetHost = endpoints[selectedIndex].host;
*targetPort = endpoints[selectedIndex].port;
return SRV_LOOKUP_RESOLVED;
}
bool ResolveIPv4(const char* host, unsigned short port, sockaddr_in* address) {
if (host == nullptr || host[0] == '\0' || address == nullptr || port == 0) return false;
std::string resolvedHost = host;
unsigned short resolvedPort = port;
if (port == WINDOWS_NETWORK_PORT) {
SrvLookupResult srvResult = ResolveMinecraftSrv(host, &resolvedHost, &resolvedPort);
if (srvResult == SRV_LOOKUP_UNAVAILABLE) return false;
}
addrinfo hints = {};
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
char portText[6] = {};
sprintf_s(portText, "%u", static_cast<unsigned int>(port));
sprintf_s(portText, "%u", static_cast<unsigned int>(resolvedPort));
addrinfo* results = nullptr;
if (getaddrinfo(host, portText, &hints, &results) != 0 || results == nullptr) return false;
if (getaddrinfo(resolvedHost.c_str(), portText, &hints, &results) != 0 || results == nullptr) return false;
*address = *reinterpret_cast<sockaddr_in*>(results->ai_addr);
address->sin_port = htons(port);
address->sin_port = htons(resolvedPort);
freeaddrinfo(results);
return true;
}
@@ -118,6 +203,7 @@ namespace {
unsigned long texturePackParentId;
unsigned char subTexturePackId;
wchar_t displayName[32];
wchar_t description[1024];
wchar_t playerNames[MINECRAFT_NET_MAX_PLAYERS][XUSER_NAME_SIZE];
};
@@ -139,6 +225,7 @@ namespace {
sessionInfo->data.isJoinable = sessionInfo->data.isReadyToJoin &&
(serverData.maxPlayers == 0 || serverData.playerCount < serverData.maxPlayers);
sessionInfo->isDedicatedSession = serverData.isDedicated;
sessionInfo->description = std::wstring(serverData.description);
sessionInfo->hasServerInfo = true;
sessionInfo->serverPlayerNames.clear();
@@ -271,6 +358,8 @@ CPlatformNetworkManagerWindows::CPlatformNetworkManagerWindows() {
m_maxPlayers = static_cast<unsigned char>(MINECRAFT_NET_MAX_PLAYERS);
m_flagIndexSize = 0;
m_serverDescription = L"";
m_listenSocket = INVALID_SOCKET;
m_discoverySocket = INVALID_SOCKET;
m_gamePort = WINDOWS_NETWORK_PORT;
@@ -463,6 +552,10 @@ void CPlatformNetworkManagerWindows::ProcessPendingClients() {
response.ready = m_inSession ? 1 : 0;
response.netVersion = MINECRAFT_NET_VERSION;
response.gamePort = m_gamePort;
{
const wchar_t* descriptionString = m_serverDescription.c_str();
wcsncpy_s(response.description, 1024, descriptionString, _TRUNCATE);
}
response.hostSettings = app.GetGameHostOption(eGameHostOption_All);
response.texturePackParentId = m_sessionData.texturePackParentId;
response.subTexturePackId = m_sessionData.subTexturePackId;
@@ -743,6 +836,15 @@ void CPlatformNetworkManagerWindows::HostGame(int localUsersMask, bool bOnlineGa
port = g_Win64DedicatedServerPort;
if (g_Win64DedicatedServerBindIP[0] != '\0')
bindIp = g_Win64DedicatedServerBindIP;
if (g_Win64DedicatedServerDescription[0] != '\0') {
std::string tempDescription = std::string(g_Win64DedicatedServerDescription);
m_serverDescription = convStringToWstring(tempDescription);
} else {
m_serverDescription = L"A Minecraft Server";
}
} else {
m_serverDescription = L"A Minecraft World";
}
m_gamePort = static_cast<unsigned short>(port);
if (!m_winsockStarted) {