Files
moon-i2p/sam_client/tcp_stub.c
T
nandi ae72b00323
check / check (windows-latest) (push) Failing after 27s
check / check (ubuntu-latest) (push) Failing after 29s
check / check (macos-latest) (push) Failing after 27s
feat: add ratchet, streaming, SU3 reseed, and router floodfill
- Add ratchet/ with ECIES-X25519-AEAD-Ratchet (Proposal 144), Garlic cloves, and ECIES DatabaseLookup replies
- Add streaming/ with Ed25519-signed I2P Streaming (SYN/ACK/NACK/CLOSE/RESET, retransmit, window) and Repliable/Raw Datagrams
- Add common/su3.mbt to parse and verify reseed .su3 bundles
- Add Elligator2 encoding in crypto/ and OfflineSignature support in LeaseSet2 (flags bit 0)
- Router: NTCP2 peer pool, floodfill NetDB crawl, transit tunnel relay, and Garlic/Streaming bridge
- cmd/router: add --peer-dir, --su3, --reseed, --floodfill, --no-clock-skew
- cmd/client: add --dest for persistent destination keys
- Mark Phase 6 complete in README and document the transport scope (NTCP2 only, no SSU2)
2026-11-27 22:23:46 -08:00

245 lines
5.1 KiB
C

/*
* Minimal POSIX TCP socket & stdin FFI stub for moon-i2p sam_client (native target).
*/
#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <netdb.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "moonbit.h"
#ifndef MSG_NOSIGNAL
#define MSG_NOSIGNAL 0
#endif
static char g_last_error[256] = {0};
static void set_last_error(const char *msg) {
if (msg == NULL) {
g_last_error[0] = '\0';
return;
}
strncpy(g_last_error, msg, sizeof(g_last_error) - 1);
g_last_error[sizeof(g_last_error) - 1] = '\0';
}
MOONBIT_FFI_EXPORT
int32_t moon_i2p_last_error_len(void) {
return (int32_t)strlen(g_last_error);
}
MOONBIT_FFI_EXPORT
int32_t moon_i2p_last_error_copy(moonbit_bytes_t dst, int32_t max_len) {
int32_t n = (int32_t)strlen(g_last_error);
if (n > max_len) {
n = max_len;
}
if (n > 0) {
memcpy(dst, g_last_error, (size_t)n);
}
return n;
}
MOONBIT_FFI_EXPORT
int32_t moon_i2p_tcp_connect(
moonbit_bytes_t host_bytes,
int32_t host_len,
int32_t port) {
if (host_len <= 0 || host_len >= 256 || port <= 0 || port > 65535) {
set_last_error("invalid host or port");
return -1;
}
char host_buf[256];
memcpy(host_buf, host_bytes, (size_t)host_len);
host_buf[host_len] = '\0';
char port_buf[16];
snprintf(port_buf, sizeof(port_buf), "%d", port);
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
struct addrinfo *res = NULL;
int gai_err = getaddrinfo(host_buf, port_buf, &hints, &res);
if (gai_err != 0) {
set_last_error(gai_strerror(gai_err));
return -1;
}
int fd = -1;
int saved_errno = 0;
for (struct addrinfo *p = res; p != NULL; p = p->ai_next) {
int s = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (s < 0) {
saved_errno = errno;
continue;
}
if (connect(s, p->ai_addr, p->ai_addrlen) == 0) {
fd = s;
break;
}
saved_errno = errno;
close(s);
}
freeaddrinfo(res);
if (fd < 0) {
set_last_error(saved_errno ? strerror(saved_errno) : "connect failed");
return -1;
}
g_last_error[0] = '\0';
return (int32_t)fd;
}
MOONBIT_FFI_EXPORT
int32_t moon_i2p_tcp_send(
int32_t fd,
moonbit_bytes_t data,
int32_t offset,
int32_t len) {
if (fd < 0 || offset < 0 || len < 0) {
set_last_error("invalid send arguments");
return -1;
}
if (len == 0) {
return 0;
}
ssize_t n;
do {
n = send(fd, data + offset, (size_t)len, MSG_NOSIGNAL);
} while (n < 0 && errno == EINTR);
if (n < 0) {
set_last_error(strerror(errno));
return -1;
}
return (int32_t)n;
}
MOONBIT_FFI_EXPORT
int32_t moon_i2p_tcp_recv(
int32_t fd,
moonbit_bytes_t buf,
int32_t max_len) {
if (fd < 0 || max_len <= 0) {
set_last_error("invalid recv arguments");
return -1;
}
ssize_t n;
do {
n = recv(fd, buf, (size_t)max_len, 0);
} while (n < 0 && errno == EINTR);
if (n < 0) {
set_last_error(strerror(errno));
return -1;
}
return (int32_t)n;
}
MOONBIT_FFI_EXPORT
int32_t moon_i2p_tcp_shutdown_write(int32_t fd) {
if (fd < 0) {
return -1;
}
if (shutdown(fd, SHUT_WR) < 0) {
set_last_error(strerror(errno));
return -1;
}
return 0;
}
MOONBIT_FFI_EXPORT
int32_t moon_i2p_tcp_close(int32_t fd) {
if (fd < 0) {
return 0;
}
if (close(fd) < 0) {
set_last_error(strerror(errno));
return -1;
}
return 0;
}
MOONBIT_FFI_EXPORT
int32_t moon_i2p_read_stdin(moonbit_bytes_t buf, int32_t max_len) {
if (max_len <= 0) {
return 0;
}
ssize_t n;
do {
n = read(STDIN_FILENO, buf, (size_t)max_len);
} while (n < 0 && errno == EINTR);
if (n < 0) {
set_last_error(strerror(errno));
return -1;
}
return (int32_t)n;
}
MOONBIT_FFI_EXPORT
void moon_i2p_flush_stdout(void) {
fflush(stdout);
}
MOONBIT_FFI_EXPORT
int32_t moon_i2p_file_size(moonbit_bytes_t path_bytes, int32_t path_len) {
if (path_len <= 0 || path_len >= 1024) {
set_last_error("invalid file path");
return -1;
}
char path[1024];
memcpy(path, path_bytes, (size_t)path_len);
path[path_len] = '\0';
struct stat st;
if (stat(path, &st) < 0) {
set_last_error(strerror(errno));
return -1;
}
if (!S_ISREG(st.st_mode)) {
set_last_error("not a regular file");
return -1;
}
return (int32_t)st.st_size;
}
MOONBIT_FFI_EXPORT
int32_t moon_i2p_read_file(
moonbit_bytes_t path_bytes,
int32_t path_len,
moonbit_bytes_t dst,
int32_t max_len) {
if (path_len <= 0 || path_len >= 1024) {
set_last_error("invalid file path");
return -1;
}
char path[1024];
memcpy(path, path_bytes, (size_t)path_len);
path[path_len] = '\0';
FILE *f = fopen(path, "rb");
if (!f) {
set_last_error(strerror(errno));
return -1;
}
size_t n = fread(dst, 1, (size_t)max_len, f);
if (ferror(f)) {
set_last_error(strerror(errno));
fclose(f);
return -1;
}
fclose(f);
return (int32_t)n;
}