367 lines
8.2 KiB
C
367 lines
8.2 KiB
C
/*
|
|
* Native POSIX TCP client/server, CSPRNG, clock, and file I/O FFI stub
|
|
* for moon-i2p ntcp2_net & router (native target only).
|
|
*/
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include <arpa/inet.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <netdb.h>
|
|
#include <netinet/in.h>
|
|
#include <poll.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/time.h>
|
|
#include <sys/types.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
|
|
#include "moonbit.h"
|
|
|
|
#ifndef MSG_NOSIGNAL
|
|
#define MSG_NOSIGNAL 0
|
|
#endif
|
|
|
|
static char g_net_last_error[256] = {0};
|
|
|
|
static void net_set_error(const char *msg) {
|
|
if (msg == NULL) {
|
|
g_net_last_error[0] = '\0';
|
|
return;
|
|
}
|
|
strncpy(g_net_last_error, msg, sizeof(g_net_last_error) - 1);
|
|
g_net_last_error[sizeof(g_net_last_error) - 1] = '\0';
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t moon_i2p_net_last_error_len(void) {
|
|
return (int32_t)strlen(g_net_last_error);
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t moon_i2p_net_last_error_copy(moonbit_bytes_t dst, int32_t max_len) {
|
|
int32_t n = (int32_t)strlen(g_net_last_error);
|
|
if (n > max_len) {
|
|
n = max_len;
|
|
}
|
|
if (n > 0) {
|
|
memcpy(dst, g_net_last_error, (size_t)n);
|
|
}
|
|
return n;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t moon_i2p_net_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) {
|
|
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;
|
|
}
|
|
if (connect(fd, p->ai_addr, p->ai_addrlen) == 0) {
|
|
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_listen(
|
|
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 listen 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_INET;
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
hints.ai_protocol = IPPROTO_TCP;
|
|
hints.ai_flags = AI_PASSIVE;
|
|
|
|
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 = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
|
if (fd < 0) {
|
|
net_set_error(strerror(errno));
|
|
freeaddrinfo(res);
|
|
return -1;
|
|
}
|
|
|
|
int opt = 1;
|
|
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
|
|
|
|
if (bind(fd, res->ai_addr, res->ai_addrlen) < 0) {
|
|
net_set_error(strerror(errno));
|
|
close(fd);
|
|
freeaddrinfo(res);
|
|
return -1;
|
|
}
|
|
freeaddrinfo(res);
|
|
|
|
if (listen(fd, 32) < 0) {
|
|
net_set_error(strerror(errno));
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
return (int32_t)fd;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t moon_i2p_net_tcp_local_port(int32_t fd) {
|
|
struct sockaddr_in addr;
|
|
socklen_t len = sizeof(addr);
|
|
if (getsockname((int)fd, (struct sockaddr *)&addr, &len) < 0) {
|
|
net_set_error(strerror(errno));
|
|
return -1;
|
|
}
|
|
return (int32_t)ntohs(addr.sin_port);
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t moon_i2p_net_tcp_accept(int32_t listen_fd, int32_t timeout_ms) {
|
|
if (timeout_ms >= 0) {
|
|
struct pollfd pfd;
|
|
pfd.fd = (int)listen_fd;
|
|
pfd.events = POLLIN;
|
|
pfd.revents = 0;
|
|
int rc = poll(&pfd, 1, (int)timeout_ms);
|
|
if (rc == 0) {
|
|
return -2; // timeout
|
|
}
|
|
if (rc < 0) {
|
|
net_set_error(strerror(errno));
|
|
return -1;
|
|
}
|
|
}
|
|
int cfd = accept((int)listen_fd, NULL, NULL);
|
|
if (cfd < 0) {
|
|
net_set_error(strerror(errno));
|
|
return -1;
|
|
}
|
|
return (int32_t)cfd;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t moon_i2p_net_tcp_set_recv_timeout(int32_t fd, int32_t timeout_ms) {
|
|
struct timeval tv;
|
|
tv.tv_sec = timeout_ms / 1000;
|
|
tv.tv_usec = (timeout_ms % 1000) * 1000;
|
|
if (setsockopt((int)fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
|
|
net_set_error(strerror(errno));
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t moon_i2p_net_tcp_send(
|
|
int32_t fd,
|
|
moonbit_bytes_t data,
|
|
int32_t offset,
|
|
int32_t len) {
|
|
ssize_t n = send((int)fd, data + offset, (size_t)len, MSG_NOSIGNAL);
|
|
if (n < 0) {
|
|
net_set_error(strerror(errno));
|
|
return -1;
|
|
}
|
|
return (int32_t)n;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t moon_i2p_net_tcp_recv(
|
|
int32_t fd,
|
|
moonbit_bytes_t buf,
|
|
int32_t max_len) {
|
|
ssize_t n = recv((int)fd, buf, (size_t)max_len, 0);
|
|
if (n < 0) {
|
|
if (errno == EAGAIN || errno == EWOULDBLOCK) {
|
|
return -2; // timeout
|
|
}
|
|
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;
|
|
pfd.fd = (int)fd;
|
|
pfd.events = POLLIN;
|
|
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_shutdown_write(int32_t fd) {
|
|
if (shutdown((int)fd, SHUT_WR) < 0) {
|
|
net_set_error(strerror(errno));
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t moon_i2p_net_tcp_close(int32_t fd) {
|
|
if (close((int)fd) < 0) {
|
|
net_set_error(strerror(errno));
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t moon_i2p_net_random_bytes(moonbit_bytes_t buf, int32_t len) {
|
|
int fd = open("/dev/urandom", O_RDONLY);
|
|
if (fd < 0) {
|
|
net_set_error(strerror(errno));
|
|
return -1;
|
|
}
|
|
int32_t got = 0;
|
|
while (got < len) {
|
|
ssize_t n = read(fd, buf + got, (size_t)(len - got));
|
|
if (n <= 0) {
|
|
net_set_error(strerror(errno));
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
got += (int32_t)n;
|
|
}
|
|
close(fd);
|
|
return got;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
uint64_t moon_i2p_net_unix_time_ms(void) {
|
|
struct timespec ts;
|
|
if (clock_gettime(CLOCK_REALTIME, &ts) != 0) {
|
|
return (uint64_t)time(NULL) * 1000ULL;
|
|
}
|
|
return (uint64_t)ts.tv_sec * 1000ULL + (uint64_t)(ts.tv_nsec / 1000000L);
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t moon_i2p_net_file_size(moonbit_bytes_t path_bytes, int32_t path_len) {
|
|
if (path_len <= 0 || path_len >= 1024) {
|
|
net_set_error("invalid path length");
|
|
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) {
|
|
net_set_error(strerror(errno));
|
|
return -1;
|
|
}
|
|
return (int32_t)st.st_size;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
int32_t moon_i2p_net_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) {
|
|
net_set_error("invalid path length");
|
|
return -1;
|
|
}
|
|
char path[1024];
|
|
memcpy(path, path_bytes, (size_t)path_len);
|
|
path[path_len] = '\0';
|
|
int fd = open(path, O_RDONLY);
|
|
if (fd < 0) {
|
|
net_set_error(strerror(errno));
|
|
return -1;
|
|
}
|
|
int32_t got = 0;
|
|
while (got < max_len) {
|
|
ssize_t n = read(fd, dst + got, (size_t)(max_len - got));
|
|
if (n < 0) {
|
|
net_set_error(strerror(errno));
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
if (n == 0) {
|
|
break;
|
|
}
|
|
got += (int32_t)n;
|
|
}
|
|
close(fd);
|
|
return got;
|
|
}
|
|
|
|
MOONBIT_FFI_EXPORT
|
|
void moon_i2p_net_flush_stdout(void) {
|
|
fflush(stdout);
|
|
}
|