MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
mxsocket.h
Go to the documentation of this file.
1#ifndef MX_SOCKET_H
2#define MX_SOCKET_H
3#include <stdbool.h>
4#include <stddef.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#ifdef _WIN32
9#include <winsock2.h>
10#include <ws2tcpip.h>
11#if __has_include(<afunix.h>)
12#include <afunix.h>
13#else
14#ifndef UNIX_PATH_MAX
15#define UNIX_PATH_MAX 108
16#endif
17struct sockaddr_un {
18 ADDRESS_FAMILY sun_family; // AF_UNIX
19 char sun_path[UNIX_PATH_MAX]; // pathname
20};
21#endif
22typedef SOCKET mx_socket_fd;
23typedef int socklet_t;
24typedef ptrdiff_t ssize_t;
25#define mx_close_socket(s) closesocket(s)
26#define mx_set_err(e) WSASetLastError(WSA##e)
27#define SOCK_ERRNO WSAGetLastError()
28#define SOCK_EINTR WSAEINTR
29#define SOCK_EAGAIN WSAEWOULDBLOCK
30#define SOCK_EWOULDBLOCK WSAEWOULDBLOCK
31#define SOCK_ECONNABORTED WSAECONNABORTED
32#define SOCK_EINVAL WSAEINVAL
33#define SOCK_EBADF WSAEBADF
34#define SOCK_ENOTSOCK WSAENOTSOCK
35#define SOCK_ESHUTDOWN WSAESHUTDOWN
36#define SOCK_ECONNRESET WSAECONNRESET
37#define NULL_SOCKET INVALID_SOCKET
38#define MX_LEN(x) (int)(x)
39#else
40#include <arpa/inet.h>
41#include <errno.h>
42#include <fcntl.h>
43#include <netdb.h>
44#include <netinet/in.h>
45#include <sys/socket.h>
46#include <sys/un.h>
47#include <unistd.h>
48typedef int mx_socket_fd;
49#define mx_close_socket(s) close(s)
50#define mx_set_err(e) (errno = (e))
51#define SOCK_ERRNO errno
52#define SOCK_EINTR EINTR
53#define SOCK_EAGAIN EAGAIN
54#define SOCK_EWOULDBLOCK EWOULDBLOCK
55#define SOCK_ECONNABORTED ECONNABORTED
56#define SOCK_EINVAL EINVAL
57#define SOCK_EBADF EBADF
58#define SOCK_ENOTSOCK ENOTSOCK
59#define SOCK_ESHUTDOWN ESHUTDOWN
60#define SOCK_ECONNRESET ECONNRESET
61#define MX_LEN(x) (size_t)(x)
62#define NULL_SOCKET -1
63#endif
64
65/**
66 * @brief Cross-platform socket state used by the C API.
67 */
68typedef struct {
69 mx_socket_fd sockfd; /**< Underlying socket handle. */
70 socklen_t addrlen; /**< Length of the active address structure. */
71 bool blocking; /**< True when the socket is in blocking mode. */
72 struct sockaddr_un sun; /**< UNIX-domain address storage. */
73 struct sockaddr_in inet; /**< IPv4 address storage. */
74 struct sockaddr_in6 inet6; /**< IPv6 address storage. */
75} MXSocket;
76
77typedef struct {
78 struct sockaddr_storage storage;
79 socklen_t length;
81
82#ifdef __cplusplus
83extern "C" {
84#endif
85/**
86 * @brief Initialize a socket structure with default values.
87 * @param s Socket state to initialize.
88 * @return True on success.
89 */
90[[nodiscard]] bool mx_socket_init(MXSocket *s);
91/**
92 * @brief Create and bind an Internet socket for listening.
93 * @param s Socket state to configure.
94 * @param port Service or port name to bind.
95 * @param backlog Maximum pending connection queue length.
96 * @param type Socket kind, typically SOCK_STREAM or SOCK_DGRAM.
97 * @return True on success.
98 */
99[[nodiscard]] bool mx_socket_listen(MXSocket *s, const char *port, int backlog, int type);
100/**
101 * @brief Create and bind an IPv6 Internet socket for listening.
102 * @param s Socket state to configure.
103 * @param port Service or port name to bind.
104 * @param backlog Maximum pending connection queue length.
105 * @param type Socket kind, typically SOCK_STREAM or SOCK_DGRAM.
106 * @return True on success.
107 */
108[[nodiscard]] bool mx_socket_ipv6_listen(MXSocket *s, const char *port, int backlog, int type);
109/**
110 * @brief Create and bind a UNIX-domain socket for listening.
111 * @param s Socket state to configure.
112 * @param path Filesystem path for the socket.
113 * @param backlog Maximum pending connection queue length.
114 * @param type Socket kind, typically SOCK_STREAM or SOCK_DGRAM.
115 * @return True on success.
116 */
117[[nodiscard]] bool mx_socket_unix_listen(MXSocket *s, const char *path, int backlog, int type);
118/**
119 * @brief Accept an incoming connection.
120 * @param input Listening socket.
121 * @param output Newly accepted socket state.
122 * @return True on success.
123 */
124[[nodiscard]] bool mx_socket_accept(const MXSocket *input, MXSocket *output);
125/**
126 * @brief Close a socket if it is open.
127 * @param s Socket state to close.
128 */
130/**
131 * @brief Toggle blocking mode for a socket.
132 * @param s Socket state to update.
133 * @param state Desired blocking state.
134 * @return True on success.
135 */
136[[nodiscard]] bool mx_socket_set_blocking(MXSocket *s, bool state);
137/**
138 * @brief Connect an Internet socket to a remote host.
139 * @param s Socket state to configure.
140 * @param host Remote host name or address.
141 * @param port Remote service or port name.
142 * @param type Socket kind, typically SOCK_STREAM or SOCK_DGRAM.
143 * @return True on success.
144 */
145[[nodiscard]] bool mx_socket_connect(MXSocket *s, const char *host, const char *port, int type);
146/**
147 * @brief Connect an IPv6 Internet socket to a remote host.
148 * @param s Socket state to configure.
149 * @param host Remote host name or address.
150 * @param port Remote service or port name.
151 * @param type Socket kind, typically SOCK_STREAM or SOCK_DGRAM.
152 * @return True on success.
153 */
154[[nodiscard]] bool mx_socket_ipv6_connect(MXSocket *s, const char *host, const char *port, int type);
155/**
156 * @brief Connect a UNIX-domain socket to a local path.
157 * @param s Socket state to configure.
158 * @param path Filesystem path for the socket.
159 * @param type Socket kind, typically SOCK_STREAM or SOCK_DGRAM.
160 * @return True on success.
161 */
162[[nodiscard]] bool mx_socket_unix_connect(MXSocket *s, const char *path, int type);
163/**
164 * @brief Read data from a socket.
165 * @param s Socket state.
166 * @param data Output buffer.
167 * @param len Number of bytes to read.
168 * @param flags Platform socket flags.
169 * @return Number of bytes read, or a negative error value.
170 */
171ssize_t mx_socket_read(MXSocket *s, void *data, size_t len, int flags);
172/**
173 * @brief Write data to a socket.
174 * @param s Socket state.
175 * @param data Input buffer.
176 * @param len Number of bytes to send.
177 * @param flags Platform socket flags.
178 * @return Number of bytes written, or a negative error value.
179 */
180ssize_t mx_socket_send(MXSocket *s, const void *data, size_t len, int flags);
181/**
182 * @brief Check whether the socket handle is valid.
183 * @param s Socket state to inspect.
184 * @return True when the handle is valid.
185 */
186[[nodiscard]] bool mx_socket_valid(const MXSocket *s);
187/**
188 * @brief Check whether the socket is open.
189 * @param s Socket state to inspect.
190 * @return True when the socket is open.
191 */
192[[nodiscard]] bool mx_socket_is_open(const MXSocket *s);
193/**
194 * @brief Read a line of text from a socket.
195 * @param s Socket state.
196 * @param buffer Output buffer pointer.
197 * @param len Output length.
198 * @return True on success.
199 */
200[[nodiscard]] bool mx_socket_readline(MXSocket *s, char **buffer, size_t *len);
201/**
202 * @brief Bind an Internet socket to a local port.
203 * @param s Socket state to configure.
204 * @param port Service or port name.
205 * @return True on success.
206 */
207[[nodiscard]] bool mx_socket_bind(MXSocket *s, const char *port);
208/**
209 * @brief Bind an IPv6 Internet socket to a local port.
210 * @param s Socket state to configure.
211 * @param port Service or port name.
212 * @return True on success.
213 */
214[[nodiscard]] bool mx_socket_ipv6_bind(MXSocket *s, const char *port);
215/**
216 * @brief Bind a UNIX-domain socket to a local path.
217 * @param s Socket state to configure.
218 * @param path Filesystem path for the socket.
219 * @return True on success.
220 */
221[[nodiscard]] bool mx_socket_unix_bind(MXSocket *s, const char *path);
222/**
223 * @brief Read the requested number of bytes from a socket.
224 * @param sock Socket state.
225 * @param buf Output buffer.
226 * @param bytes Number of bytes to read.
227 * @return Number of bytes read, or a negative error value.
228 */
229ssize_t mx_socket_read_all(MXSocket *sock, void *buf, size_t bytes);
230/**
231 * @brief Write the requested number of bytes to a socket.
232 * @param sock Socket state.
233 * @param buf Input buffer.
234 * @param bytes Number of bytes to write.
235 * @return Number of bytes written, or a negative error value.
236 */
237ssize_t mx_socket_write_all(MXSocket *sock, const void *buf, size_t bytes);
238/**
239 * @brief Send a datagram using an Internet socket.
240 * @param sock Socket state.
241 * @param buf Input buffer.
242 * @param bytes Number of bytes to send.
243 * @return Number of bytes sent, or a negative error value.
244 */
245ssize_t mx_socket_sendto(MXSocket *sock, const void *buf, size_t bytes);
246/**
247 * @brief Send a datagram using an IPv6 Internet socket.
248 * @param sock Socket state.
249 * @param buf Input buffer.
250 * @param bytes Number of bytes to send.
251 * @return Number of bytes sent, or a negative error value.
252 */
253ssize_t mx_socket_ipv6_sendto(MXSocket *sock, const void *buf, size_t bytes);
254/**
255 * @brief Receive a datagram using an Internet socket.
256 * @param sock Socket state.
257 * @param buf Output buffer.
258 * @param bytes Maximum number of bytes to receive.
259 * @return Number of bytes received, or a negative error value.
260 */
261ssize_t mx_socket_recvfrom(MXSocket *sock, void *buf, size_t bytes);
262ssize_t mx_socket_recvfrom_address(MXSocket *sock, void *buf, size_t bytes, MXSocketAddress *address);
263ssize_t mx_socket_sendto_address(MXSocket *sock, const void *buf, size_t bytes, const MXSocketAddress *address);
264/**
265 * @brief Receive a datagram using an IPv6 Internet socket.
266 * @param sock Socket state.
267 * @param buf Output buffer.
268 * @param bytes Maximum number of bytes to receive.
269 * @return Number of bytes received, or a negative error value.
270 */
271ssize_t mx_socket_ipv6_recvfrom(MXSocket *sock, void *buf, size_t bytes);
272/**
273 * @brief Send a datagram using a UNIX-domain socket.
274 * @param sock Socket state.
275 * @param buf Input buffer.
276 * @param bytes Number of bytes to send.
277 * @return Number of bytes sent, or a negative error value.
278 */
279ssize_t mx_socket_unix_sendto(MXSocket *sock, const void *buf, size_t bytes);
280/**
281 * @brief Receive a datagram using a UNIX-domain socket.
282 * @param sock Socket state.
283 * @param buf Output buffer.
284 * @param bytes Maximum number of bytes to receive.
285 * @return Number of bytes received, or a negative error value.
286 */
287ssize_t mx_socket_unix_recvfrom(MXSocket *sock, void *buf, size_t bytes);
288/**
289 * @brief Ignore SIGPIPE on platforms that require it.
290 */
292#ifdef __cplusplus
293}
294#endif
295#endif
ssize_t mx_socket_read(MXSocket *s, void *data, size_t len, int flags)
Read data from a socket.
Definition mxsocket.c:422
bool mx_socket_ipv6_connect(MXSocket *s, const char *host, const char *port, int type)
Connect an IPv6 Internet socket to a remote host.
Definition mxsocket.c:366
ssize_t mx_socket_recvfrom_address(MXSocket *sock, void *buf, size_t bytes, MXSocketAddress *address)
Definition mxsocket.c:614
int mx_socket_fd
Definition mxsocket.h:48
bool mx_socket_unix_bind(MXSocket *s, const char *path)
Bind a UNIX-domain socket to a local path.
Definition mxsocket.c:378
ssize_t mx_socket_read_all(MXSocket *sock, void *buf, size_t bytes)
Read the requested number of bytes from a socket.
Definition mxsocket.c:555
void mx_socket_close(MXSocket *s)
Close a socket if it is open.
Definition mxsocket.c:323
ssize_t mx_socket_recvfrom(MXSocket *sock, void *buf, size_t bytes)
Receive a datagram using an Internet socket.
Definition mxsocket.c:601
bool mx_socket_is_open(const MXSocket *s)
Check whether the socket is open.
Definition mxsocket.c:442
bool mx_socket_set_blocking(MXSocket *s, bool state)
Toggle blocking mode for a socket.
Definition mxsocket.c:331
void mx_socket_ignore_pipe_signal()
Ignore SIGPIPE on platforms that require it.
Definition mxsocket.c:577
ssize_t mx_socket_sendto_address(MXSocket *sock, const void *buf, size_t bytes, const MXSocketAddress *address)
Definition mxsocket.c:625
ssize_t mx_socket_write_all(MXSocket *sock, const void *buf, size_t bytes)
Write the requested number of bytes to a socket.
Definition mxsocket.c:535
bool mx_socket_unix_connect(MXSocket *s, const char *path, int type)
Connect a UNIX-domain socket to a local path.
Definition mxsocket.c:241
bool mx_socket_valid(const MXSocket *s)
Check whether the socket handle is valid.
Definition mxsocket.c:416
bool mx_socket_unix_listen(MXSocket *s, const char *path, int backlog, int type)
Create and bind a UNIX-domain socket for listening.
Definition mxsocket.c:202
ssize_t mx_socket_unix_sendto(MXSocket *sock, const void *buf, size_t bytes)
Send a datagram using a UNIX-domain socket.
Definition mxsocket.c:645
bool mx_socket_listen(MXSocket *s, const char *port, int backlog, int type)
Create and bind an Internet socket for listening.
Definition mxsocket.c:270
bool mx_socket_accept(const MXSocket *input, MXSocket *output)
Accept an incoming connection.
Definition mxsocket.c:278
ssize_t mx_socket_send(MXSocket *s, const void *data, size_t len, int flags)
Write data to a socket.
Definition mxsocket.c:432
bool mx_socket_connect(MXSocket *s, const char *host, const char *port, int type)
Connect an Internet socket to a remote host.
Definition mxsocket.c:362
ssize_t mx_socket_ipv6_recvfrom(MXSocket *sock, void *buf, size_t bytes)
Receive a datagram using an IPv6 Internet socket.
Definition mxsocket.c:632
bool mx_socket_ipv6_bind(MXSocket *s, const char *port)
Bind an IPv6 Internet socket to a local port.
Definition mxsocket.c:374
bool mx_socket_readline(MXSocket *s, char **buffer, size_t *len)
Read a line of text from a socket.
Definition mxsocket.c:474
bool mx_socket_ipv6_listen(MXSocket *s, const char *port, int backlog, int type)
Create and bind an IPv6 Internet socket for listening.
Definition mxsocket.c:274
bool mx_socket_bind(MXSocket *s, const char *port)
Bind an Internet socket to a local port.
Definition mxsocket.c:370
ssize_t mx_socket_sendto(MXSocket *sock, const void *buf, size_t bytes)
Send a datagram using an Internet socket.
Definition mxsocket.c:583
bool mx_socket_init(MXSocket *s)
Initialize a socket structure with default values.
Definition mxsocket.c:407
ssize_t mx_socket_ipv6_sendto(MXSocket *sock, const void *buf, size_t bytes)
Send a datagram using an IPv6 Internet socket.
Definition mxsocket.c:592
ssize_t mx_socket_unix_recvfrom(MXSocket *sock, void *buf, size_t bytes)
Receive a datagram using a UNIX-domain socket.
Definition mxsocket.c:653
socklen_t length
Definition mxsocket.h:79
struct sockaddr_storage storage
Definition mxsocket.h:78
Cross-platform socket state used by the C API.
Definition mxsocket.h:68
struct sockaddr_in6 inet6
IPv6 address storage.
Definition mxsocket.h:74
bool blocking
True when the socket is in blocking mode.
Definition mxsocket.h:71
struct sockaddr_in inet
IPv4 address storage.
Definition mxsocket.h:73
struct sockaddr_un sun
UNIX-domain address storage.
Definition mxsocket.h:72
mx_socket_fd sockfd
Underlying socket handle.
Definition mxsocket.h:69
socklen_t addrlen
Length of the active address structure.
Definition mxsocket.h:70