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)
This commit is contained in:
1 parent
8eafa11b22
commit
ae72b00323
54 files changed
+9607
-704
No files matched your search
@@ -5,6 +5,7 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <netdb.h>
|
||||
@@ -19,6 +20,7 @@
|
||||
#include <sys/stat.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
@@ -111,6 +113,111 @@ int32_t moon_i2p_net_tcp_connect(
|
||||
return (int32_t)fd;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t moon_i2p_net_tcp_set_nonblocking(int32_t fd, int32_t nonblocking) {
|
||||
int flags = fcntl((int)fd, F_GETFL, 0);
|
||||
if (flags < 0) {
|
||||
net_set_error(strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
if (nonblocking) {
|
||||
flags |= O_NONBLOCK;
|
||||
} else {
|
||||
flags &= ~O_NONBLOCK;
|
||||
}
|
||||
if (fcntl((int)fd, F_SETFL, flags) < 0) {
|
||||
net_set_error(strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t moon_i2p_net_tcp_connect_nb(
|
||||
moonbit_bytes_t host_bytes,
|
||||
int32_t host_len,
|
||||
int32_t port) {
|
||||
if (host_len <= 0 || host_len >= 256 || port <= 0 || port > 65535) {
|
||||
net_set_error("invalid host or port");
|
||||
return -1;
|
||||
}
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
|
||||
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) {
|
||||
net_set_error(gai_strerror(gai_err));
|
||||
return -1;
|
||||
}
|
||||
|
||||
int fd = -1;
|
||||
int last_errno = 0;
|
||||
for (struct addrinfo *p = res; p != NULL; p = p->ai_next) {
|
||||
fd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
|
||||
if (fd < 0) {
|
||||
last_errno = errno;
|
||||
continue;
|
||||
}
|
||||
int flags = fcntl(fd, F_GETFL, 0);
|
||||
if (flags >= 0) {
|
||||
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
||||
}
|
||||
if (connect(fd, p->ai_addr, p->ai_addrlen) == 0 || errno == EINPROGRESS) {
|
||||
break;
|
||||
}
|
||||
last_errno = errno;
|
||||
close(fd);
|
||||
fd = -1;
|
||||
}
|
||||
freeaddrinfo(res);
|
||||
|
||||
if (fd < 0) {
|
||||
net_set_error(strerror(last_errno ? last_errno : ECONNREFUSED));
|
||||
return -1;
|
||||
}
|
||||
return (int32_t)fd;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t moon_i2p_net_tcp_check_connected(int32_t fd) {
|
||||
struct pollfd pfd;
|
||||
pfd.fd = (int)fd;
|
||||
pfd.events = POLLOUT | POLLERR | POLLHUP;
|
||||
pfd.revents = 0;
|
||||
int rc = poll(&pfd, 1, 0);
|
||||
if (rc < 0) {
|
||||
net_set_error(strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
if (rc == 0) {
|
||||
return 0; // still in progress
|
||||
}
|
||||
int err = 0;
|
||||
socklen_t len = sizeof(err);
|
||||
if (getsockopt((int)fd, SOL_SOCKET, SO_ERROR, &err, &len) < 0) {
|
||||
net_set_error(strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
if (err != 0) {
|
||||
net_set_error(strerror(err));
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t moon_i2p_net_tcp_listen(
|
||||
moonbit_bytes_t host_bytes,
|
||||
@@ -224,12 +331,29 @@ int32_t moon_i2p_net_tcp_send(
|
||||
int32_t len) {
|
||||
ssize_t n = send((int)fd, data + offset, (size_t)len, MSG_NOSIGNAL);
|
||||
if (n < 0) {
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||
return -2;
|
||||
}
|
||||
net_set_error(strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
return (int32_t)n;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t moon_i2p_net_tcp_poll_writable(int32_t fd, int32_t timeout_ms) {
|
||||
struct pollfd pfd;
|
||||
pfd.fd = (int)fd;
|
||||
pfd.events = POLLOUT;
|
||||
pfd.revents = 0;
|
||||
int rc = poll(&pfd, 1, (int)timeout_ms);
|
||||
if (rc < 0) {
|
||||
net_set_error(strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
return rc > 0 ? 1 : 0;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t moon_i2p_net_tcp_recv(
|
||||
int32_t fd,
|
||||
@@ -246,6 +370,22 @@ int32_t moon_i2p_net_tcp_recv(
|
||||
return (int32_t)n;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t moon_i2p_net_tcp_recv_nb(
|
||||
int32_t fd,
|
||||
moonbit_bytes_t buf,
|
||||
int32_t max_len) {
|
||||
ssize_t n = recv((int)fd, buf, (size_t)max_len, MSG_DONTWAIT);
|
||||
if (n < 0) {
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
||||
return -2; // would block
|
||||
}
|
||||
net_set_error(strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
return (int32_t)n;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t moon_i2p_net_tcp_poll_readable(int32_t fd, int32_t timeout_ms) {
|
||||
struct pollfd pfd;
|
||||
@@ -364,3 +504,112 @@ MOONBIT_FFI_EXPORT
|
||||
void moon_i2p_net_flush_stdout(void) {
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t moon_i2p_net_list_dir(
|
||||
moonbit_bytes_t path_bytes,
|
||||
int32_t path_len,
|
||||
moonbit_bytes_t dst,
|
||||
int32_t max_len) {
|
||||
if (path_len <= 0 || path_len >= 1024 || max_len <= 0) {
|
||||
net_set_error("invalid list_dir arguments");
|
||||
return -1;
|
||||
}
|
||||
char path[1024];
|
||||
memcpy(path, path_bytes, (size_t)path_len);
|
||||
path[path_len] = '\0';
|
||||
DIR *d = opendir(path);
|
||||
if (d == NULL) {
|
||||
net_set_error(strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
int32_t pos = 0;
|
||||
struct dirent *ent;
|
||||
while ((ent = readdir(d)) != NULL) {
|
||||
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) {
|
||||
continue;
|
||||
}
|
||||
int32_t nlen = (int32_t)strlen(ent->d_name);
|
||||
if (pos + nlen + 1 > max_len) {
|
||||
break;
|
||||
}
|
||||
memcpy(dst + pos, ent->d_name, (size_t)nlen);
|
||||
pos += nlen;
|
||||
dst[pos++] = '\n';
|
||||
}
|
||||
closedir(d);
|
||||
return pos;
|
||||
}
|
||||
|
||||
MOONBIT_FFI_EXPORT
|
||||
int32_t moon_i2p_net_fetch_https(
|
||||
moonbit_bytes_t url_bytes,
|
||||
int32_t url_len,
|
||||
moonbit_bytes_t dst,
|
||||
int32_t max_len) {
|
||||
if (url_len < 9 || url_len >= 1024 || max_len <= 0) {
|
||||
net_set_error("invalid https url");
|
||||
return -1;
|
||||
}
|
||||
char url[1024];
|
||||
memcpy(url, url_bytes, (size_t)url_len);
|
||||
url[url_len] = '\0';
|
||||
if (strncmp(url, "https://", 8) != 0) {
|
||||
net_set_error("reseed URL must start with https://");
|
||||
return -1;
|
||||
}
|
||||
for (int32_t i = 0; i < url_len; i++) {
|
||||
if ((unsigned char)url[i] < 0x21) {
|
||||
net_set_error("reseed URL contains control or whitespace character");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
int pfd[2];
|
||||
if (pipe(pfd) < 0) {
|
||||
net_set_error(strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
pid_t pid = fork();
|
||||
if (pid < 0) {
|
||||
net_set_error(strerror(errno));
|
||||
close(pfd[0]);
|
||||
close(pfd[1]);
|
||||
return -1;
|
||||
}
|
||||
if (pid == 0) {
|
||||
close(pfd[0]);
|
||||
dup2(pfd[1], STDOUT_FILENO);
|
||||
close(pfd[1]);
|
||||
int devnull = open("/dev/null", O_WRONLY);
|
||||
if (devnull >= 0) {
|
||||
dup2(devnull, STDERR_FILENO);
|
||||
close(devnull);
|
||||
}
|
||||
char *argv[] = {
|
||||
"curl", "-fsSL", "--max-time", "15", "--max-filesize", "2097152", url, NULL
|
||||
};
|
||||
execvp("curl", argv);
|
||||
_exit(127);
|
||||
}
|
||||
close(pfd[1]);
|
||||
int32_t total = 0;
|
||||
while (total < max_len) {
|
||||
ssize_t n = read(pfd[0], dst + total, (size_t)(max_len - total));
|
||||
if (n < 0) {
|
||||
if (errno == EINTR) continue;
|
||||
net_set_error(strerror(errno));
|
||||
close(pfd[0]);
|
||||
waitpid(pid, NULL, 0);
|
||||
return -1;
|
||||
}
|
||||
if (n == 0) break;
|
||||
total += (int32_t)n;
|
||||
}
|
||||
close(pfd[0]);
|
||||
int status = 0;
|
||||
if (waitpid(pid, &status, 0) < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
|
||||
net_set_error("curl HTTPS fetch failed");
|
||||
return -1;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
Reference in new issue
Block a user