/* * Minimal POSIX TCP socket & stdin FFI stub for moon-i2p sam_client (native target). */ #define _POSIX_C_SOURCE 200809L #include #include #include #include #include #include #include #include #include #include #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); }