mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-17 13:39: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:
@@ -9,6 +9,8 @@
|
||||
#include <linux/fs.h>
|
||||
#include <linux/futex.h>
|
||||
#include <linux/userfaultfd.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <poll.h>
|
||||
#include <pthread.h>
|
||||
#include <sched.h>
|
||||
@@ -1072,6 +1074,12 @@ int plat_file_set_readonly(const char *path, int on) {
|
||||
return chmod(path, m & 07777) == 0;
|
||||
}
|
||||
|
||||
int plat_file_private(const char *path) {
|
||||
struct stat st;
|
||||
if (stat(path, &st)) return 0;
|
||||
return chmod(path, S_ISDIR(st.st_mode) ? 0700 : 0600) == 0;
|
||||
}
|
||||
|
||||
int plat_file_list(const char *dir, const char *ext, int (*cb)(const char *name, void *ctx), void *ctx) {
|
||||
DIR *d = opendir(dir);
|
||||
if (!d) return 0;
|
||||
@@ -1828,6 +1836,22 @@ int plat_random(void *out, size_t n) {
|
||||
|
||||
int plat_clipboard_set(const char *utf8) { return SDL_SetClipboardText(utf8); }
|
||||
|
||||
int plat_clipboard_get(char *out, size_t cap) {
|
||||
if (!cap) return 0;
|
||||
out[0] = 0;
|
||||
char *t = SDL_GetClipboardText(); // "" when there is none, never NULL but on failure
|
||||
if (!t) return 0;
|
||||
size_t n = strlen(t);
|
||||
if (n >= cap) { // cut where a character starts, not inside one
|
||||
n = cap - 1;
|
||||
while (n && ((unsigned char)t[n] & 0xc0) == 0x80) n--;
|
||||
}
|
||||
memcpy(out, t, n);
|
||||
out[n] = 0;
|
||||
SDL_free(t);
|
||||
return n != 0;
|
||||
}
|
||||
|
||||
static void reap_main(void *arg) {
|
||||
waitpid((pid_t)(intptr_t)arg, NULL, 0);
|
||||
}
|
||||
@@ -2358,6 +2382,142 @@ void plat_ipc_close(PlatIpc *p) {
|
||||
free(p);
|
||||
}
|
||||
|
||||
// ---- UDP sockets (platform.h): the co-op layer's transport
|
||||
struct PlatUdp { int fd, v6; };
|
||||
|
||||
static void addr_from_sockaddr(PlatAddr *out, const struct sockaddr_storage *ss) {
|
||||
uint8_t ip[16] = {0};
|
||||
if (ss->ss_family == AF_INET6) {
|
||||
const struct sockaddr_in6 *a = (const struct sockaddr_in6 *)ss;
|
||||
memcpy(ip, &a->sin6_addr, 16);
|
||||
plat_addr_set(out, ip, ntohs(a->sin6_port), a->sin6_scope_id);
|
||||
} else {
|
||||
const struct sockaddr_in *a = (const struct 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, struct sockaddr_storage *ss, socklen_t *len) {
|
||||
uint8_t ip[16];
|
||||
uint32_t scope;
|
||||
plat_addr_get(a, ip, &scope);
|
||||
memset(ss, 0, sizeof *ss);
|
||||
if (v6) {
|
||||
struct sockaddr_in6 *s = (struct 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 = 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;
|
||||
struct sockaddr_in *s = (struct sockaddr_in *)ss;
|
||||
s->sin_family = AF_INET;
|
||||
s->sin_port = htons(plat_addr_port(a));
|
||||
memcpy(&s->sin_addr, ip + 12, 4);
|
||||
*len = sizeof *s;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int plat_net_resolve(const char *host, uint16_t port, PlatAddr *out) {
|
||||
struct addrinfo hints = {.ai_family = AF_UNSPEC, .ai_socktype = SOCK_DGRAM}, *res = NULL, *pick = NULL;
|
||||
if (!host || !*host || getaddrinfo(host, NULL, &hints, &res) != 0) return 0;
|
||||
for (struct addrinfo *r = res; r; r = r->ai_next) {
|
||||
if (r->ai_addrlen > sizeof(struct 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) {
|
||||
struct 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);
|
||||
}
|
||||
freeaddrinfo(res);
|
||||
return ok;
|
||||
}
|
||||
|
||||
PlatUdp *plat_udp_open(uint16_t port, int dual_stack) {
|
||||
int v6 = dual_stack;
|
||||
int fd = v6 ? socket(AF_INET6, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0) : -1;
|
||||
if (fd >= 0) {
|
||||
int off = 0;
|
||||
setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &off, sizeof off);
|
||||
struct sockaddr_in6 a = {.sin6_family = AF_INET6, .sin6_addr = in6addr_any, .sin6_port = htons(port)};
|
||||
if (bind(fd, (struct sockaddr *)&a, sizeof a) != 0) close(fd), fd = -1;
|
||||
}
|
||||
if (fd < 0) { // IPv4 only, asked for or all the machine has
|
||||
v6 = 0;
|
||||
fd = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
|
||||
if (fd < 0) return NULL;
|
||||
struct sockaddr_in a = {.sin_family = AF_INET, .sin_port = htons(port)};
|
||||
a.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
if (bind(fd, (struct sockaddr *)&a, sizeof a) != 0) {
|
||||
close(fd);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
PlatUdp *u = malloc(sizeof *u);
|
||||
if (!u) {
|
||||
close(fd);
|
||||
return NULL;
|
||||
}
|
||||
u->fd = fd, u->v6 = v6;
|
||||
return u;
|
||||
}
|
||||
|
||||
uint16_t plat_udp_port(const PlatUdp *u) {
|
||||
struct sockaddr_storage ss;
|
||||
socklen_t len = sizeof ss;
|
||||
if (!u || getsockname(u->fd, (struct 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) {
|
||||
struct sockaddr_storage ss;
|
||||
socklen_t len;
|
||||
if (!u || !addr_to_sockaddr(to, u->v6, &ss, &len)) return 0;
|
||||
for (;;) {
|
||||
ssize_t k = sendto(u->fd, p, n, MSG_NOSIGNAL, (struct sockaddr *)&ss, len);
|
||||
if (k >= 0) return 1;
|
||||
if (errno == EINTR) continue;
|
||||
// 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
|
||||
return errno != EMSGSIZE && errno != EBADF && errno != EINVAL && errno != EAFNOSUPPORT && errno != ENOTSOCK;
|
||||
}
|
||||
}
|
||||
|
||||
int plat_udp_recv(PlatUdp *u, void *buf, size_t cap, PlatAddr *from, uint32_t ms) {
|
||||
if (!u) return -1;
|
||||
struct pollfd pf = {.fd = u->fd, .events = POLLIN};
|
||||
int r = poll(&pf, 1, ms > (uint32_t)INT32_MAX ? INT32_MAX : (int)ms);
|
||||
if (r < 0) return errno == EINTR ? 0 : -1;
|
||||
if (r == 0) return 0;
|
||||
struct sockaddr_storage ss;
|
||||
socklen_t len = sizeof ss;
|
||||
ssize_t k = recvfrom(u->fd, buf, cap, MSG_DONTWAIT, (struct sockaddr *)&ss, &len);
|
||||
if (k < 0) return errno == EAGAIN || errno == EINTR || errno == ECONNREFUSED || errno == EHOSTUNREACH ? 0 : -1;
|
||||
if (from) addr_from_sockaddr(from, &ss);
|
||||
return (int)k;
|
||||
}
|
||||
|
||||
void plat_udp_close(PlatUdp *u) {
|
||||
if (!u) return;
|
||||
close(u->fd);
|
||||
free(u);
|
||||
}
|
||||
|
||||
|
||||
// ---- restart
|
||||
// NX2NAVI_RESTART_WAIT names the process that called plat_restart, as its id and start time (field 22 of /proc/<pid>/stat,
|
||||
|
||||
Reference in New Issue
Block a user