#ifndef CST_NET_H #define CST_NET_H /* Minimal TCP sockets for the embedded HTTP server (src/net.c). * * The point of this layer is that src/http_srv.c and src/webui.c never * include a socket header: Unix file descriptors and Windows SOCKETs differ * in type, close() and error reporting, and Windows needs WSAStartup before * anything works. */ #include #include #ifdef __cplusplus extern "C" { #endif #ifdef _WIN32 typedef intptr_t os_socket_t; #define OS_SOCKET_BAD ((os_socket_t)-1) #else typedef int os_socket_t; #define OS_SOCKET_BAD (-1) #endif /* Longest "address:port" os_net_peer writes (IPv6 hosts are bracketed so a * URL built from it stays unambiguous). */ #define OS_NET_ADDR_MAX 64 /* Winsock startup (no-op on Unix, which also ignores SIGPIPE so a client * that hangs up cannot kill the process). 0 / -1. */ int os_net_init(void); void os_net_cleanup(void); /* Bind and listen on host:port. host may be NULL/"" (then "127.0.0.1"), * "0.0.0.0"/"::" (every interface) or any literal address / name; port 0 * picks a free one. Returns the listening socket, or OS_SOCKET_BAD with a * short reason in err. */ os_socket_t os_net_listen(const char *host, int port, char *err, size_t err_sz); /* The port actually bound (0 when unknown). */ int os_net_port(os_socket_t s); /* A one-line "host:port" for messages. */ void os_net_peer(os_socket_t s, char *out, size_t out_sz); /* Connect to host:port, waiting up to timeout_ms: the socket, or * OS_SOCKET_BAD. Used by the tests and by anything that drives the WebUI * API from inside the process. */ os_socket_t os_net_connect(const char *host, int port, int timeout_ms); /* Accept one connection, waiting up to timeout_ms (<0 = forever). * Returns the socket, OS_SOCKET_BAD on error, or OS_SOCKET_BAD with * *timed_out set when the wait ran out. */ os_socket_t os_net_accept(os_socket_t s, int timeout_ms, int *timed_out); /* Read at most n bytes, waiting up to timeout_ms: bytes read, 0 at EOF, * -1 on error, -2 when nothing arrived in time. */ long os_net_recv(os_socket_t s, void *buf, size_t n, int timeout_ms); /* Write everything (timeout_ms per call): 0 / -1. */ int os_net_send_all(os_socket_t s, const void *buf, size_t n, int timeout_ms); /* Wake up a thread blocked in recv/send on this socket (shutdown, no close). */ void os_net_shutdown(os_socket_t s); void os_net_close(os_socket_t s); #ifdef __cplusplus } #endif #endif /* CST_NET_H */