mirror of
https://github.com/tangerineturtle1/NX2navi.git
synced 2026-11-18 23:59:59 -04:00
Merge co-op, off by default: rooms through a relay, progress sync between the players' own saves, and a Co-op tab.
Without the other players drawn in the world, which are unfinished. No relay address is built in, and with co-op off nothing opens a socket. Built on Windows and tested against a relay on this PC only; two games in one room and any real network have not been tried. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
// The parts of platform.h built on its locks rather than on the OS: recursive mutexes, one-time initialisation and
|
||||
// events. Every OS shares them.
|
||||
// events, and the network address helpers. Every OS shares them.
|
||||
#include "platform.h"
|
||||
#include <string.h>
|
||||
|
||||
// ---- recursive mutex: owner is the holder's thread id (0 when free); only the holder writes depth.
|
||||
|
||||
@@ -100,3 +101,61 @@ int plat_event_wait_ns(PlatEvent *e, uint64_t ns) {
|
||||
plat_mutex_unlock(&e->m);
|
||||
return got;
|
||||
}
|
||||
|
||||
// ---- network addresses: the OS-free part of platform.h's UDP sockets. Layout of PlatAddr.b: the IPv6 address at 0 (an
|
||||
// IPv4 one as ::ffff:a.b.c.d), the port big-endian at 16, the scope id little-endian at 18, zeros after.
|
||||
|
||||
void plat_addr_set(PlatAddr *a, const uint8_t ip[16], uint16_t port, uint32_t scope) {
|
||||
memset(a, 0, sizeof *a);
|
||||
memcpy(a->b, ip, 16);
|
||||
a->b[16] = (uint8_t)(port >> 8), a->b[17] = (uint8_t)port;
|
||||
for (int i = 0; i < 4; i++) a->b[18 + i] = (uint8_t)(scope >> (8 * i));
|
||||
}
|
||||
|
||||
void plat_addr_get(const PlatAddr *a, uint8_t ip[16], uint32_t *scope) {
|
||||
memcpy(ip, a->b, 16);
|
||||
if (scope) *scope = (uint32_t)a->b[18] | (uint32_t)a->b[19] << 8 | (uint32_t)a->b[20] << 16 | (uint32_t)a->b[21] << 24;
|
||||
}
|
||||
|
||||
uint16_t plat_addr_port(const PlatAddr *a) { return (uint16_t)(a->b[16] << 8 | a->b[17]); }
|
||||
|
||||
int plat_addr_eq(const PlatAddr *a, const PlatAddr *b) { return !memcmp(a, b, sizeof *a); }
|
||||
|
||||
static int addr_is_v4(const uint8_t *ip) {
|
||||
static const uint8_t mapped[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff};
|
||||
return !memcmp(ip, mapped, sizeof mapped);
|
||||
}
|
||||
|
||||
int plat_addr_str(const PlatAddr *a, char *out, size_t cap) {
|
||||
char s[96];
|
||||
int n;
|
||||
uint32_t scope;
|
||||
uint8_t ip[16];
|
||||
plat_addr_get(a, ip, &scope);
|
||||
if (addr_is_v4(ip)) {
|
||||
n = snprintf(s, sizeof s, "%u.%u.%u.%u:%u", ip[12], ip[13], ip[14], ip[15], plat_addr_port(a));
|
||||
} else {
|
||||
unsigned g[8];
|
||||
for (int i = 0; i < 8; i++) g[i] = (unsigned)ip[2 * i] << 8 | ip[2 * i + 1];
|
||||
int best = -1, best_len = 1; // the longest run of two or more zero groups becomes "::"
|
||||
for (int i = 0, run; i < 8; i += run ? run : 1) {
|
||||
run = 0;
|
||||
while (i + run < 8 && !g[i + run]) run++;
|
||||
if (run > best_len) best = i, best_len = run;
|
||||
}
|
||||
n = snprintf(s, sizeof s, "[");
|
||||
for (int i = 0; i < 8; i++) {
|
||||
if (i == best) {
|
||||
n += snprintf(s + n, sizeof s - (size_t)n, "::");
|
||||
i += best_len - 1;
|
||||
continue;
|
||||
}
|
||||
n += snprintf(s + n, sizeof s - (size_t)n, "%s%x", i && i != best + best_len ? ":" : "", g[i]);
|
||||
}
|
||||
if (scope) n += snprintf(s + n, sizeof s - (size_t)n, "%%%u", scope);
|
||||
n += snprintf(s + n, sizeof s - (size_t)n, "]:%u", plat_addr_port(a));
|
||||
}
|
||||
if (n <= 0 || (size_t)n >= sizeof s || (size_t)n >= cap) return 0;
|
||||
memcpy(out, s, (size_t)n + 1);
|
||||
return n;
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -359,6 +359,7 @@ void plat_file_delete(const char *path);
|
||||
int plat_file_truncate(const char *path, uint64_t size); // 1 on success
|
||||
int64_t plat_file_size(const char *path); // bytes of a file, -1 when missing or unreadable
|
||||
int plat_file_set_readonly(const char *path, int on); // sets or clears the read-only attribute; 1 on success
|
||||
int plat_file_private(const char *path); // only this account may read it (0600, a folder 0700); 1 on success
|
||||
// A file that only grows and other processes may read meanwhile: each write lands whole at its end, from any thread,
|
||||
// and reaches the file even if the process dies right after (no user-space buffer, no flush to the disk).
|
||||
typedef struct PlatAppend PlatAppend;
|
||||
@@ -447,8 +448,12 @@ int plat_is_wine(void); // 1 when the Windows
|
||||
// The file the process's stderr (the log) goes to, as a UTF-8 path; 0 when stderr is not a file (a console, a pipe).
|
||||
int plat_stderr_path(char *out, size_t cap);
|
||||
|
||||
// ---- desktop: the in-game menu's "Copy system info" and "Open log folder"
|
||||
// ---- desktop: the in-game menu's "Copy system info", "Open log folder" and the Co-op tab's secret
|
||||
int plat_clipboard_set(const char *utf8); // replaces the clipboard's text; 1 on success
|
||||
// The clipboard's text as UTF-8 in out (cap bytes with the NUL; a longer text: "" on Windows, cut at a character
|
||||
// elsewhere); 1 when it had text. With SDL only on the window thread, as SDL asks: the menu goes through
|
||||
// gfx_window_paste (ui/menu.h).
|
||||
int plat_clipboard_get(char *out, size_t cap);
|
||||
int plat_open_folder(const char *path); // shows the folder in the file manager; 1 on success
|
||||
|
||||
// ---- dynamic libraries: a library some machines lack (a GPU vendor's), loaded at run time. A bare name is searched
|
||||
@@ -519,6 +524,34 @@ int plat_ipc_read(PlatIpc *p, void *buf, size_t n, uint32_t ms);
|
||||
int plat_ipc_write(PlatIpc *p, const void *buf, size_t n); // 1 when all n bytes were written, 0 the connection broke
|
||||
void plat_ipc_close(PlatIpc *p);
|
||||
|
||||
// ---- UDP sockets: the co-op layer's transport (coop.c). One unconnected datagram socket per PlatUdp; who a datagram
|
||||
// came from is checked by the protocol, not by the OS. On Windows the first call of any of these starts Winsock.
|
||||
// A PlatAddr is an IPv6 address (an IPv4 one as ::ffff:a.b.c.d), the port and the scope id in the same layout on every
|
||||
// OS, so two equal addresses compare equal bytewise (plat_addr_eq) whichever way the OS reported them.
|
||||
typedef struct PlatUdp PlatUdp;
|
||||
typedef struct { uint8_t b[32]; } PlatAddr;
|
||||
// host (a name, or an IPv4 or IPv6 literal without brackets) and port into *out. Blocks while the resolver runs: call
|
||||
// it on a thread that may wait, never on the game's. An IPv4 answer is preferred over an IPv6 one. 1 on success.
|
||||
int plat_net_resolve(const char *host, uint16_t port, PlatAddr *out);
|
||||
// A non-blocking UDP socket bound to port (0: any free port) on every local address. dual_stack 1: IPv6 with IPv4 in the
|
||||
// same socket (IPv4 alone where the machine has no IPv6); 0: IPv4 only. NULL on failure.
|
||||
PlatUdp *plat_udp_open(uint16_t port, int dual_stack);
|
||||
uint16_t plat_udp_port(const PlatUdp *u); // the port it is bound to; 0 on failure
|
||||
// One datagram to *to; never blocks. 1 when it was sent or the OS dropped it (a full send buffer, no route, an ICMP
|
||||
// error: a datagram may be lost anyway), 0 when it can never be sent (too long, an address the socket cannot reach).
|
||||
int plat_udp_send(PlatUdp *u, const PlatAddr *to, const void *p, size_t n);
|
||||
// The next datagram into buf, waiting at most ms for it; *from (may be NULL) gets its sender. Bytes received (a datagram
|
||||
// longer than cap is cut to cap), 0 when none came in time, -1 when the socket is unusable.
|
||||
int plat_udp_recv(PlatUdp *u, void *buf, size_t cap, PlatAddr *from, uint32_t ms);
|
||||
void plat_udp_close(PlatUdp *u);
|
||||
// Address helpers that need no OS (common.c); the back ends build addresses with plat_addr_set.
|
||||
void plat_addr_set(PlatAddr *a, const uint8_t ip[16], uint16_t port, uint32_t scope);
|
||||
void plat_addr_get(const PlatAddr *a, uint8_t ip[16], uint32_t *scope);
|
||||
uint16_t plat_addr_port(const PlatAddr *a);
|
||||
int plat_addr_eq(const PlatAddr *a, const PlatAddr *b);
|
||||
// "1.2.3.4:5" or "[::1]:5" ("%scope" after a link-local address) into out; its length, 0 when it does not fit cap.
|
||||
int plat_addr_str(const PlatAddr *a, char *out, size_t cap);
|
||||
|
||||
// ---- restart: this program again, in a new process that starts once this one is gone
|
||||
// Starts this executable again with the same command line, environment and working folder, and no handle of this
|
||||
// process. 1 when it started; the caller then exits.
|
||||
|
||||
@@ -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