mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-19 12:59:58 -04:00
Add co-op, off by default: rooms through a relay, progress sync between the players' own saves, and a Co-op tab.
Each player keeps their own game and save. One player makes a room, the others join it with its secret, and story flags and, by the room's rules, items, songs, quest items, small keys (a shared pool), rupees and ammunition reach every save. The layer is a host thread with one UDP socket to a relay the players run themselves (tools/coop_relay.py, Python's standard library only); the messages between the players are sealed with a key from the room's secret (XChaCha20-Poly1305, Monocypher 4.0.2 vendored with its licence and a row in THIRD_PARTY_NOTICES.md), every change is signed by the player who made it, a player accepts a room by its members' keys and the key of its rules, the save is copied to coop-backup before anything is applied, and values are set through the game's own routines between frames. The Co-op tab makes, joins, accepts, removes and leaves, and the menu takes typed text for the name, the relay and the secret. This is the contributor's pull request #518 on this line. Nothing reaches the network while co-op is off: the socket is opened, and the relay's name looked up, only after co-op is on, a relay is set and a room is made or joined. There is no default relay. Left out of the pull request: the other players drawn in the world (the "puppets") with the copied engine they need. They were unfinished, added two patch sites to the game's per-frame update that could not be put through the differential tests here, and their file helpers clashed with the ones this line already has. So no position is sent: the POS message's codec stays, nothing sends it. Changed against the pull request: - The menu's test hook drives the Co-op tab only when it comes from the environment. From nx2navi.ini it could turn co-op on, join a room and accept its offer. - The log's label for a room is hashed from the secret instead of being its first three characters. - A game that has not saved yet has no save to copy, and takes nothing from the room until it has saved once; that used to count as a copy made. - A PING's token no longer carries the PC's uptime. - On Windows the maker's key file and its folder get an access list with the player's account alone. - The tests that link the platform layer get the socket library, and the save test a stand-in for the title screen's hook. Checked on Windows, where the pull request had never been compiled: the runtime builds and links; --probe-coop passes (sockets on 127.0.0.1, the codec against its byte vectors, a fuzz of every parser, two sessions through a hostile mock relay, a relay restart, the sync engine); tests/coop_relay_test.py passes its 61 tests, with the probe through the real relay on 127.0.0.1 at 0 and 30 % loss; tests/coop_page_check.py passes and its deliberate fault fails 48 checks. Not checked: two games in one room (one game runs at a time here), any real network, the tab in the game. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
// Windows implementation of platform.h: CreateThread, QueryPerformanceCounter, high-resolution waitable timers,
|
||||
// SRW locks and condition variables.
|
||||
#include <io.h>
|
||||
#include <limits.h>
|
||||
#include <stdio.h>
|
||||
#include <winsock2.h> // before windows.h, which would otherwise pull in the old winsock.h
|
||||
#include <ws2tcpip.h>
|
||||
#include <windows.h>
|
||||
#include <aclapi.h> // SetNamedSecurityInfoW (plat_file_private)
|
||||
#include <bcrypt.h>
|
||||
#include <pdh.h>
|
||||
#include <pdhmsg.h>
|
||||
@@ -753,6 +757,27 @@ int plat_file_set_readonly(const char *path, int on) {
|
||||
return SetFileAttributesW(w, on ? a | FILE_ATTRIBUTE_READONLY : a & ~(DWORD)FILE_ATTRIBUTE_READONLY) != 0;
|
||||
}
|
||||
|
||||
// The file or folder gets an access list of its own with one entry, full control for the account the game runs as,
|
||||
// and inherits none from its folder: a game folder outside the user's own folders is often readable by every account
|
||||
// of the PC. Administrators can still take ownership, as on any file.
|
||||
int plat_file_private(const char *path) {
|
||||
wchar_t w[WPATH];
|
||||
if (!widen(path, w, WPATH) || GetFileAttributesW(w) == INVALID_FILE_ATTRIBUTES) return 0;
|
||||
HANDLE tok = NULL;
|
||||
union { TOKEN_USER u; BYTE raw[sizeof(TOKEN_USER) + SECURITY_MAX_SID_SIZE]; } user;
|
||||
DWORD got = 0;
|
||||
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &tok)) return 0;
|
||||
BOOL have = GetTokenInformation(tok, TokenUser, &user, sizeof user, &got);
|
||||
CloseHandle(tok);
|
||||
if (!have) return 0;
|
||||
union { ACL acl; BYTE raw[sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) + SECURITY_MAX_SID_SIZE]; } list;
|
||||
if (!InitializeAcl(&list.acl, sizeof list, ACL_REVISION) ||
|
||||
!AddAccessAllowedAceEx(&list.acl, ACL_REVISION, OBJECT_INHERIT_ACE | CONTAINER_INHERIT_ACE, GENERIC_ALL, user.u.User.Sid))
|
||||
return 0;
|
||||
return SetNamedSecurityInfoW(w, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION | PROTECTED_DACL_SECURITY_INFORMATION, NULL,
|
||||
NULL, &list.acl, NULL) == ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
int plat_file_list(const char *dir, const char *ext, int (*cb)(const char *name, void *ctx), void *ctx) {
|
||||
wchar_t wd[WPATH], we[64], pat[WPATH + 64];
|
||||
if (!widen(dir, wd, WPATH) || !widen(ext, we, 64)) return 0;
|
||||
@@ -1690,6 +1715,19 @@ int plat_clipboard_set(const char *utf8) {
|
||||
return ok;
|
||||
}
|
||||
|
||||
int plat_clipboard_get(char *out, size_t cap) {
|
||||
if (!cap) return 0;
|
||||
out[0] = 0;
|
||||
if (!OpenClipboard(NULL)) return 0; // reading needs no owner window
|
||||
HANDLE h = GetClipboardData(CF_UNICODETEXT);
|
||||
const wchar_t *w = h ? GlobalLock(h) : NULL;
|
||||
int n = w ? WideCharToMultiByte(CP_UTF8, 0, w, -1, out, (int)cap, NULL, NULL) : 0; // 0 when it does not fit
|
||||
if (w) GlobalUnlock(h);
|
||||
CloseClipboard();
|
||||
if (n <= 0) out[0] = 0;
|
||||
return out[0] != 0;
|
||||
}
|
||||
|
||||
static DWORD WINAPI open_folder_main(void *arg) {
|
||||
ShellExecuteW(NULL, L"open", arg, NULL, NULL, SW_SHOWNORMAL);
|
||||
free(arg);
|
||||
@@ -2100,6 +2138,194 @@ void plat_ipc_close(PlatIpc *p) {
|
||||
free(p);
|
||||
}
|
||||
|
||||
// ---- UDP sockets (platform.h): the co-op layer's transport. Winsock starts at the first call that needs it.
|
||||
#ifndef SIO_UDP_CONNRESET
|
||||
#define SIO_UDP_CONNRESET _WSAIOW(IOC_VENDOR, 12)
|
||||
#endif
|
||||
#ifndef WSA_FLAG_NO_HANDLE_INHERIT
|
||||
#define WSA_FLAG_NO_HANDLE_INHERIT 0x80
|
||||
#endif
|
||||
|
||||
struct PlatUdp { SOCKET s; int v6; };
|
||||
|
||||
static PlatOnce wsa_once = PLAT_ONCE_INIT;
|
||||
static int wsa_ok;
|
||||
|
||||
static void wsa_start(void *arg) {
|
||||
WSADATA d;
|
||||
(void)arg;
|
||||
wsa_ok = WSAStartup(MAKEWORD(2, 2), &d) == 0;
|
||||
}
|
||||
|
||||
static int net_ready(void) {
|
||||
plat_once(&wsa_once, wsa_start, NULL);
|
||||
return wsa_ok;
|
||||
}
|
||||
|
||||
static void addr_from_sockaddr(PlatAddr *out, const SOCKADDR_STORAGE *ss) {
|
||||
uint8_t ip[16] = {0};
|
||||
if (ss->ss_family == AF_INET6) {
|
||||
const SOCKADDR_IN6 *a = (const SOCKADDR_IN6 *)ss;
|
||||
memcpy(ip, &a->sin6_addr, 16);
|
||||
plat_addr_set(out, ip, ntohs(a->sin6_port), a->sin6_scope_id);
|
||||
} else {
|
||||
const SOCKADDR_IN *a = (const SOCKADDR_IN *)ss;
|
||||
ip[10] = ip[11] = 0xff;
|
||||
memcpy(ip + 12, &a->sin_addr, 4);
|
||||
plat_addr_set(out, ip, ntohs(a->sin_port), 0);
|
||||
}
|
||||
}
|
||||
|
||||
// The socket address of a for a socket of family v6 (1: AF_INET6, 0: AF_INET); 0 when an IPv4 socket cannot reach it.
|
||||
static int addr_to_sockaddr(const PlatAddr *a, int v6, SOCKADDR_STORAGE *ss, int *len) {
|
||||
uint8_t ip[16];
|
||||
uint32_t scope;
|
||||
plat_addr_get(a, ip, &scope);
|
||||
memset(ss, 0, sizeof *ss);
|
||||
if (v6) {
|
||||
SOCKADDR_IN6 *s = (SOCKADDR_IN6 *)ss;
|
||||
s->sin6_family = AF_INET6;
|
||||
s->sin6_port = htons(plat_addr_port(a));
|
||||
s->sin6_scope_id = scope;
|
||||
memcpy(&s->sin6_addr, ip, 16);
|
||||
*len = (int)sizeof *s;
|
||||
return 1;
|
||||
}
|
||||
static const uint8_t mapped[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff};
|
||||
if (memcmp(ip, mapped, sizeof mapped)) return 0;
|
||||
SOCKADDR_IN *s = (SOCKADDR_IN *)ss;
|
||||
s->sin_family = AF_INET;
|
||||
s->sin_port = htons(plat_addr_port(a));
|
||||
memcpy(&s->sin_addr, ip + 12, 4);
|
||||
*len = (int)sizeof *s;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int plat_net_resolve(const char *host, uint16_t port, PlatAddr *out) {
|
||||
wchar_t whost[256];
|
||||
if (!host || !*host || !net_ready() || !widen(host, whost, 256)) return 0;
|
||||
ADDRINFOW hints = {.ai_family = AF_UNSPEC, .ai_socktype = SOCK_DGRAM}, *res = NULL, *pick = NULL;
|
||||
if (GetAddrInfoW(whost, NULL, &hints, &res) != 0) return 0;
|
||||
for (ADDRINFOW *r = res; r; r = r->ai_next) {
|
||||
if (r->ai_addrlen > sizeof(SOCKADDR_STORAGE) || (r->ai_family != AF_INET && r->ai_family != AF_INET6)) continue;
|
||||
if (!pick || (r->ai_family == AF_INET && pick->ai_family != AF_INET)) pick = r; // IPv4 first
|
||||
}
|
||||
int ok = pick != NULL;
|
||||
if (ok) {
|
||||
SOCKADDR_STORAGE ss;
|
||||
memset(&ss, 0, sizeof ss);
|
||||
memcpy(&ss, pick->ai_addr, pick->ai_addrlen);
|
||||
addr_from_sockaddr(out, &ss);
|
||||
uint8_t ip[16];
|
||||
uint32_t scope;
|
||||
plat_addr_get(out, ip, &scope);
|
||||
plat_addr_set(out, ip, port, scope);
|
||||
}
|
||||
FreeAddrInfoW(res);
|
||||
return ok;
|
||||
}
|
||||
|
||||
// A non-blocking UDP socket of family af that no child process inherits, with Windows' report of an ICMP port-unreachable
|
||||
// as a failed receive (WSAECONNRESET on an unconnected socket) switched off: a peer that is gone is not an error here.
|
||||
static SOCKET udp_socket(int af) {
|
||||
SOCKET s = WSASocketW(af, SOCK_DGRAM, IPPROTO_UDP, NULL, 0, WSA_FLAG_OVERLAPPED | WSA_FLAG_NO_HANDLE_INHERIT);
|
||||
if (s == INVALID_SOCKET) return s;
|
||||
u_long nonblocking = 1;
|
||||
BOOL report = FALSE;
|
||||
DWORD got = 0;
|
||||
if (ioctlsocket(s, FIONBIO, &nonblocking) != 0) {
|
||||
closesocket(s);
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
WSAIoctl(s, SIO_UDP_CONNRESET, &report, sizeof report, NULL, 0, &got, NULL, NULL);
|
||||
return s;
|
||||
}
|
||||
|
||||
PlatUdp *plat_udp_open(uint16_t port, int dual_stack) {
|
||||
if (!net_ready()) return NULL;
|
||||
int v6 = dual_stack;
|
||||
SOCKET s = v6 ? udp_socket(AF_INET6) : INVALID_SOCKET;
|
||||
if (s != INVALID_SOCKET) {
|
||||
DWORD only_v6 = 0; // Windows defaults to IPv6 only
|
||||
setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, (const char *)&only_v6, sizeof only_v6);
|
||||
SOCKADDR_IN6 a;
|
||||
memset(&a, 0, sizeof a); // in6addr_any
|
||||
a.sin6_family = AF_INET6;
|
||||
a.sin6_port = htons(port);
|
||||
if (bind(s, (SOCKADDR *)&a, sizeof a) != 0) closesocket(s), s = INVALID_SOCKET;
|
||||
}
|
||||
if (s == INVALID_SOCKET) { // IPv4 only, asked for or all the machine has
|
||||
v6 = 0;
|
||||
s = udp_socket(AF_INET);
|
||||
if (s == INVALID_SOCKET) return NULL;
|
||||
SOCKADDR_IN a;
|
||||
memset(&a, 0, sizeof a);
|
||||
a.sin_family = AF_INET;
|
||||
a.sin_port = htons(port);
|
||||
a.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
if (bind(s, (SOCKADDR *)&a, sizeof a) != 0) {
|
||||
closesocket(s);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
PlatUdp *u = malloc(sizeof *u);
|
||||
if (!u) {
|
||||
closesocket(s);
|
||||
return NULL;
|
||||
}
|
||||
u->s = s, u->v6 = v6;
|
||||
return u;
|
||||
}
|
||||
|
||||
uint16_t plat_udp_port(const PlatUdp *u) {
|
||||
SOCKADDR_STORAGE ss;
|
||||
int len = (int)sizeof ss;
|
||||
if (!u || getsockname(u->s, (SOCKADDR *)&ss, &len) != 0) return 0;
|
||||
PlatAddr a;
|
||||
addr_from_sockaddr(&a, &ss);
|
||||
return plat_addr_port(&a);
|
||||
}
|
||||
|
||||
int plat_udp_send(PlatUdp *u, const PlatAddr *to, const void *p, size_t n) {
|
||||
SOCKADDR_STORAGE ss;
|
||||
int len;
|
||||
if (!u || n > 65507 || !addr_to_sockaddr(to, u->v6, &ss, &len)) return 0;
|
||||
if (sendto(u->s, (const char *)p, (int)n, 0, (SOCKADDR *)&ss, len) != SOCKET_ERROR) return 1;
|
||||
// a full buffer, a missing route or an ICMP error is a lost datagram; a datagram too long or a socket that is not
|
||||
// ours to use can never be sent
|
||||
int e = WSAGetLastError();
|
||||
return e != WSAEMSGSIZE && e != WSAENOTSOCK && e != WSAEINVAL && e != WSAEAFNOSUPPORT;
|
||||
}
|
||||
|
||||
int plat_udp_recv(PlatUdp *u, void *buf, size_t cap, PlatAddr *from, uint32_t ms) {
|
||||
if (!u) return -1;
|
||||
WSAPOLLFD pf = {.fd = u->s, .events = POLLRDNORM};
|
||||
int r = WSAPoll(&pf, 1, ms > (uint32_t)INT_MAX ? INT_MAX : (int)ms);
|
||||
if (r == SOCKET_ERROR) return WSAGetLastError() == WSAEINTR ? 0 : -1;
|
||||
if (r == 0) return 0;
|
||||
SOCKADDR_STORAGE ss;
|
||||
int len = (int)sizeof ss;
|
||||
int k = recvfrom(u->s, (char *)buf, cap > (size_t)INT_MAX ? INT_MAX : (int)cap, 0, (SOCKADDR *)&ss, &len);
|
||||
if (k == SOCKET_ERROR) {
|
||||
int e = WSAGetLastError();
|
||||
if (e == WSAEMSGSIZE) { // longer than buf: Windows reports it as an error but has filled buf
|
||||
k = cap > (size_t)INT_MAX ? INT_MAX : (int)cap;
|
||||
} else {
|
||||
return e == WSAEWOULDBLOCK || e == WSAEINTR || e == WSAECONNRESET || e == WSAENETRESET || e == WSAEHOSTUNREACH ||
|
||||
e == WSAENETUNREACH
|
||||
? 0 : -1;
|
||||
}
|
||||
}
|
||||
if (from) addr_from_sockaddr(from, &ss);
|
||||
return k;
|
||||
}
|
||||
|
||||
void plat_udp_close(PlatUdp *u) {
|
||||
if (!u) return;
|
||||
closesocket(u->s);
|
||||
free(u);
|
||||
}
|
||||
|
||||
|
||||
// ---- restart
|
||||
// NX2NAVI_RESTART_WAIT names the process that called plat_restart, as its id and creation time: a process that has only
|
||||
|
||||
Reference in New Issue
Block a user