Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
sudo apt-get update
sudo apt-get install -y --no-install-recommends build-essential cmake pkg-config \
libjson-c-dev libssl-dev libcurl4-openssl-dev zlib1g-dev libreadline-dev \
liblua5.4-dev
liblua5.4-dev libsodium-dev

- name: Cache the fetched dependencies (libdatachannel)
uses: actions/cache@v4
Expand Down Expand Up @@ -85,7 +85,7 @@ jobs:
submodules: recursive

- name: Dependencies
run: brew install json-c openssl@3 lua pkg-config
run: brew install json-c openssl@3 lua pkg-config libsodium

- name: Build (Release, -Werror)
run: make server
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ jobs:
mingw-w64-ucrt-x86_64-gcc mingw-w64-ucrt-x86_64-cmake mingw-w64-ucrt-x86_64-pkgconf
mingw-w64-ucrt-x86_64-json-c mingw-w64-ucrt-x86_64-readline mingw-w64-ucrt-x86_64-lua54
mingw-w64-ucrt-x86_64-curl mingw-w64-ucrt-x86_64-openssl mingw-w64-ucrt-x86_64-zlib
mingw-w64-ucrt-x86_64-libsodium

- name: Build the static server
run: |
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@
[submodule "server/Extern/tomlc99/tomlc99"]
path = server/Extern/tomlc99/tomlc99
url = https://github.com/cktan/tomlc99
[submodule "common/atyha"]
path = common/atyha
url = https://github.com/ngspades/atyha
1 change: 1 addition & 0 deletions common/atyha
Submodule atyha added at c96b71
8 changes: 4 additions & 4 deletions netlib/include/ngnet/p2p.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@

#include <stddef.h>

/* the join code's contract is the master's README ("join codes"): six
unambiguous letters and digits, `XXX - XXX` to read out */
/* the join code's contract is atyha's spec (atyha-v1.md section 3): eight
unambiguous letters and digits, `XXXX - XXXX` to read out */

/* 1 when the read takes the code (pasted alone or "XXX-XXX"), "" when not
/* 1 when the read takes the code (pasted alone or "XXXX-XXXX"), "" when not
out when it is not one */
int p2p_code_norm(const char* pasted, char* out, size_t size_out);
/* any text that begins a code: the server list's address box may take
the friend's message's code as it is */
int p2p_code_loose(const char* pasted, char* out, size_t size_out);
/* as a friend reads it out over the phone: `XXX - XXX` */
/* as a friend reads it out over the phone: `XXXX - XXXX` */
void p2p_code_display(const char* code, char* out, size_t size_out);
/* a fresh code for this match, handed to the started server
(--join-code) */
Expand Down
12 changes: 11 additions & 1 deletion netlib/include/ngnet/transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,18 @@ extern const struct transport_impl transport_web_rtc_impl;
extern const struct transport_impl transport_rtc_native_impl;
extern const struct transport_impl transport_worker_impl;

/* does this build carry the join code at all (a desktop with WebRTC) */
/* does this build carry the join code at all (a desktop with WebRTC, or
the browser) */
int transport_has_code_join(void);
/* the room's password for the next join by code ("" none: most rooms have
none). Kept until changed */
void transport_set_code_password(const char* password);
const char* transport_code_password(void);
/* the last join by code failed on its password (wrong, or none given to
a room that has one): the player is asked for it. Set by the code
transports, cleared at every connect */
int transport_code_wrong_password(void);
void transport_code_mark_wrong_password(void);
/* how long a native connect waits for its CONNECT event (the join by
code's signalling, ICE and DTLS take seconds longer than ENet's look) */
float transport_connect_wait(void);
Expand Down
73 changes: 46 additions & 27 deletions netlib/src/p2p.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@

/* The match's join code, the way a player runs its own multiplayer for
its friends with no port to forward: the host shows it,
a friend types or pastes it and the join finds the host's machine. The
code is six letters and digits, shown as `XXX - XXX`, and its letters
and digits are only the ones nobody misreads: no I, L, O, Q, 0 or 1
(the rest of it the master refuses). Everything a player may read of
a friend types or pastes it and the join finds the host's machine
through public relays (Atyha, github.com/ngspades/atyha: its
spec/atyha-v1.md section 3 is the rule here). The code is eight
letters and digits, shown as `XXXX - XXXX`, and its letters and digits
are only the ones nobody misreads: no I, L, O, Q, G, S, 0, 1, 2, 6 or 8. Everything a player may read of
it goes through the game's own language texts, never as a technical
word */

Expand All @@ -48,38 +49,57 @@
#include "ngnet/p2p.h"

enum {
/* the code's length and the separators: XXX-XXX, spaces to spare */
P2P_CODE_LENGTH = 6,
P2P_HALF = 3,
/* the code's length and the separators: XXXX-XXXX, spaces to spare */
P2P_CODE_LENGTH = 8,
P2P_HALF = 4,
};

/* what a code may hold, as the pasted one reads: every message may be
either hand */
static const char p2p_alphabet[] = "ABCDEFHJKMNPRTUVWXYZ34579";

static int p2p_code_char(char c) {
c = (char)(c >= 'a' && c <= 'z' ? c - 32 : c);
if((c >= 'A' && c <= 'Z') && c != 'I' && c != 'L' && c != 'O' && c != 'Q')
return 1;
if(c >= '2' && c <= '9')
return 1;
return 0;
return c != 0 && strchr(p2p_alphabet, c) != NULL;
}

/* "ngspades://" at c, in any case: a room link (the desktop builds
register the scheme) */
static int p2p_link_scheme(const char* c) {
static const char scheme[] = "ngspades://";
for(size_t k = 0; k < sizeof(scheme) - 1; k++) {
char ch = (char)(c[k] >= 'A' && c[k] <= 'Z' ? c[k] + 32 : c[k]);
if(ch != scheme[k])
return 0;
}
return 1;
}

static int p2p_space(char c) {
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
}

/* the master's rule (its README): "ABC-DEF", "ABC DEF", "ABCDEF" and
"abc-def" are the same code; 0 when it is not one */
/* atyha's rule: "ABCD-EFHJ", "ABCD EFHJ", "ABCDEFHJ" and "abcd-efhj"
are the same code; 0 when it is not one */
int p2p_code_norm(const char* pasted, char* out, size_t size_out) {
if(out == NULL || size_out < P2P_CODE_LENGTH + 1 || pasted == NULL)
return 0;
size_t n = 0;
for(const char* c = pasted; *c && n < size_out - 1; c++) {
const char* end = pasted + strlen(pasted);
while(p2p_space(*pasted))
pasted++;
while(end > pasted && p2p_space(end[-1]))
end--;
for(const char* c = pasted; c < end && n < size_out - 1; c++) {
/* a code may be told apart even in whatever message a friend
sent: the separators and the scheme that came with a
shortcut are out */
if(*c == '-' || *c == ' ' || *c == '\t')
sent: the separators and a link's ngspades:// are out
(a trailing slash too: a browser may add one) */
if(*c == '-' || *c == ' ' || *c == '\t' || *c == '/')
continue;
/* strncmp, not memcmp: it stops at the text's end, and a pasted
text shorter than the scheme was read past its NUL */
if(!strncmp(c, "sadcode://", 10)) {
c += 9;
if(p2p_link_scheme(c)) {
c += 10;
continue;
}
if(!p2p_code_char(*c))
Expand All @@ -99,15 +119,15 @@ int p2p_code_loose(const char* pasted, char* out, size_t size_out) {
return p2p_code_norm(pasted, out, size_out);
}

/* draws it as a friend reads it out on the phone: `XXX - XXX`,
/* draws it as a friend reads it out on the phone: `XXXX - XXXX`,
space-hyphen-space in the middle */
void p2p_code_display(const char* code, char* out, size_t size_out) {
if(code == NULL || strlen(code) != P2P_CODE_LENGTH) {
if(out && size_out)
out[0] = 0;
return;
}
snprintf(out, size_out, "%c%c%c - %c%c%c", code[0], code[1], code[2], code[3], code[4], code[5]);
snprintf(out, size_out, "%.4s - %.4s", code, code + 4);
}

/* the client picks the code and hands it over with the server (its
Expand Down Expand Up @@ -142,11 +162,10 @@ static int p2p_os_random(unsigned char* buf, size_t n) {
}

const char* p2p_code_generate(void) {
/* exactly the master's own clear letters and digits, and one step
stricter (no G, S, 2, 6, 8): always checked below, so a character
the master would refuse can never reach the wire */
static const char alphabet[] = "ABCDEFHJKMNPRTUVWXYZ34579";
enum { ALPHABET = sizeof(alphabet) - 1 };
/* atyha's alphabet: always checked below, so a character the
other side would refuse can never be shown */
static const char* alphabet = p2p_alphabet;
enum { ALPHABET = sizeof(p2p_alphabet) - 1 };
/* a byte at or over the largest multiple of the alphabet is thrown
away: taken modulo, the first letters would come up more often */
enum { BYTE_LIMIT = 256 - 256 % ALPHABET };
Expand Down
9 changes: 8 additions & 1 deletion server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ if(NOT SERVER_WEB_HOST)
# and LTO below, which would apply to its C++ too. Offline builds (Flatpak)
# point FETCHCONTENT_SOURCE_DIR_LIBDATACHANNEL at a checkout with submodules.
include(cmake/libdatachannel.cmake)
# Atyha (common/atyha, a submodule of github.com/ngspades/atyha, MIT; its C
# library is c/): the match's room on public Nostr relays
# (Net/Room.c), its WebSocket libdatachannel's own. Needs libsodium.
set(ATYHA_TESTS OFF CACHE BOOL "" FORCE)
set(ATYHA_CURL OFF CACHE BOOL "" FORCE)
set(ATYHA_WITH_LIBDATACHANNEL ON CACHE BOOL "" FORCE)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../common/atyha/c ${CMAKE_BINARY_DIR}/atyha)
endif() # NOT SERVER_WEB_HOST

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
Expand Down Expand Up @@ -163,7 +170,7 @@ include_directories(${CURL_INCLUDE_DIRS})

set(SERVER_JSONC_LIBS json-c)
set(SERVER_READLINE_LIBS readline)
set(SERVER_NET_LIBS datachannel OpenSSL::Crypto)
set(SERVER_NET_LIBS datachannel OpenSSL::Crypto atyha_libdatachannel)
endif()
# a static library does not bring its own dependencies: pkg-config's static
# lists name them (libcurl's TLS, compression and IDN libraries, readline's
Expand Down
6 changes: 6 additions & 0 deletions server/Source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
set(SPADESX_SOURCES
Main.c
)
# netlib's join code (p2p.c, with its log hook host.c): --join-code is read
# by its rule. Not on the Switch, whose game already links them (it links
# this server in as a library)
if(NOT SERVER_SWITCH_HOST)
list(APPEND SPADESX_SOURCES ${NGNET_COMMON_SOURCES})
endif()

target_sources(SpadesX
PRIVATE
Expand Down
24 changes: 10 additions & 14 deletions server/Source/Main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <Util/TOMLHelpers.h>
#include <Util/Log.h>
#include <Util/Types.h>
#include <ngnet/p2p.h>
#include <uthash/utlist.h>
#include <Util/Alloc.h>
#include <signal.h>
Expand Down Expand Up @@ -821,23 +822,18 @@ int main(int argc, char** argv)
}
}
if (join_code_arg != NULL) {
/* the player's paste: separators and the case out */
char out[sizeof(get_server()->join_code)];
size_t n = 0;
for (const char* c = join_code_arg; *c && n + 1 < sizeof(out); c++) {
if (*c == '-' || *c == ' ') {
continue;
}
out[n++] = (char) (*c >= 'a' && *c <= 'z' ? *c - 32 : *c);
}
out[n] = '\0';
if (n == 6) {
/* the player's paste, read by atyha's rule (netlib's p2p.c):
eight letters and digits nobody misreads */
char out[sizeof(get_server()->join_code)];
if (p2p_code_norm(join_code_arg, out, sizeof out)) {
snprintf(get_server()->join_code, sizeof(get_server()->join_code), "%s", out);
/* the host sees in the log what its friends will type (and a
test reads it without the master) */
LOG_STATUS("Join code: %c%c%c-%c%c%c", out[0], out[1], out[2], out[3], out[4], out[5]);
test reads it) */
char shown[16];
p2p_code_display(out, shown, sizeof shown);
LOG_STATUS("Join code: %s", shown);
} else {
LOG_WARNING("--join-code %s: a code is six letters and digits (like ABC-DEF)", join_code_arg);
LOG_WARNING("--join-code %s: a code is eight letters and digits (like HK4M-7TRW)", join_code_arg);
}
}

Expand Down
2 changes: 2 additions & 0 deletions server/Source/Server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ set(SERVER_SOURCES
Net/Conn.c
Net/EnetConn.c
Net/WebRtc.c
Net/Room.c
Net/RtcPeer.cpp
Net/Master.c
Plugins.c
Expand All @@ -267,6 +268,7 @@ if(SERVER_WEB_HOST)
list(REMOVE_ITEM SERVER_SOURCES
Net/EnetConn.c
Net/WebRtc.c
Net/Room.c
Net/RtcPeer.cpp
Net/Master.c
Ping.c
Expand Down
Loading