/* * 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 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #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_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, 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) { 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, 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_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; 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); } 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; }