#include <stdbool.h>#include <stddef.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <arpa/inet.h>#include <errno.h>#include <fcntl.h>#include <netdb.h>#include <netinet/in.h>#include <sys/socket.h>#include <sys/un.h>#include <unistd.h>Go to the source code of this file.
Classes | |
| struct | MXSocket |
| Cross-platform socket state used by the C API. More... | |
| struct | MXSocketAddress |
Macros | |
| #define | mx_close_socket(s) |
| #define | MX_LEN(x) |
| #define | mx_set_err(e) |
| #define | NULL_SOCKET -1 |
| #define | SOCK_EAGAIN EAGAIN |
| #define | SOCK_EBADF EBADF |
| #define | SOCK_ECONNABORTED ECONNABORTED |
| #define | SOCK_ECONNRESET ECONNRESET |
| #define | SOCK_EINTR EINTR |
| #define | SOCK_EINVAL EINVAL |
| #define | SOCK_ENOTSOCK ENOTSOCK |
| #define | SOCK_ERRNO errno |
| #define | SOCK_ESHUTDOWN ESHUTDOWN |
| #define | SOCK_EWOULDBLOCK EWOULDBLOCK |
Typedefs | |
| typedef int | mx_socket_fd |
Functions | |
| bool | mx_socket_accept (const MXSocket *input, MXSocket *output) |
| Accept an incoming connection. | |
| bool | mx_socket_bind (MXSocket *s, const char *port) |
| Bind an Internet socket to a local port. | |
| void | mx_socket_close (MXSocket *s) |
| Close a socket if it is open. | |
| bool | mx_socket_connect (MXSocket *s, const char *host, const char *port, int type) |
| Connect an Internet socket to a remote host. | |
| void | mx_socket_ignore_pipe_signal () |
| Ignore SIGPIPE on platforms that require it. | |
| bool | mx_socket_init (MXSocket *s) |
| Initialize a socket structure with default values. | |
| bool | mx_socket_ipv6_bind (MXSocket *s, const char *port) |
| Bind an IPv6 Internet socket to a local port. | |
| bool | mx_socket_ipv6_connect (MXSocket *s, const char *host, const char *port, int type) |
| Connect an IPv6 Internet socket to a remote host. | |
| bool | mx_socket_ipv6_listen (MXSocket *s, const char *port, int backlog, int type) |
| Create and bind an IPv6 Internet socket for listening. | |
| ssize_t | mx_socket_ipv6_recvfrom (MXSocket *sock, void *buf, size_t bytes) |
| Receive a datagram using an IPv6 Internet socket. | |
| ssize_t | mx_socket_ipv6_sendto (MXSocket *sock, const void *buf, size_t bytes) |
| Send a datagram using an IPv6 Internet socket. | |
| bool | mx_socket_is_open (const MXSocket *s) |
| Check whether the socket is open. | |
| bool | mx_socket_listen (MXSocket *s, const char *port, int backlog, int type) |
| Create and bind an Internet socket for listening. | |
| ssize_t | mx_socket_read (MXSocket *s, void *data, size_t len, int flags) |
| Read data from a socket. | |
| ssize_t | mx_socket_read_all (MXSocket *sock, void *buf, size_t bytes) |
| Read the requested number of bytes from a socket. | |
| bool | mx_socket_readline (MXSocket *s, char **buffer, size_t *len) |
| Read a line of text from a socket. | |
| ssize_t | mx_socket_recvfrom (MXSocket *sock, void *buf, size_t bytes) |
| Receive a datagram using an Internet socket. | |
| ssize_t | mx_socket_recvfrom_address (MXSocket *sock, void *buf, size_t bytes, MXSocketAddress *address) |
| ssize_t | mx_socket_send (MXSocket *s, const void *data, size_t len, int flags) |
| Write data to a socket. | |
| ssize_t | mx_socket_sendto (MXSocket *sock, const void *buf, size_t bytes) |
| Send a datagram using an Internet socket. | |
| ssize_t | mx_socket_sendto_address (MXSocket *sock, const void *buf, size_t bytes, const MXSocketAddress *address) |
| bool | mx_socket_set_blocking (MXSocket *s, bool state) |
| Toggle blocking mode for a socket. | |
| bool | mx_socket_unix_bind (MXSocket *s, const char *path) |
| Bind a UNIX-domain socket to a local path. | |
| bool | mx_socket_unix_connect (MXSocket *s, const char *path, int type) |
| Connect a UNIX-domain socket to a local path. | |
| bool | mx_socket_unix_listen (MXSocket *s, const char *path, int backlog, int type) |
| Create and bind a UNIX-domain socket for listening. | |
| ssize_t | mx_socket_unix_recvfrom (MXSocket *sock, void *buf, size_t bytes) |
| Receive a datagram using a UNIX-domain socket. | |
| ssize_t | mx_socket_unix_sendto (MXSocket *sock, const void *buf, size_t bytes) |
| Send a datagram using a UNIX-domain socket. | |
| bool | mx_socket_valid (const MXSocket *s) |
| Check whether the socket handle is valid. | |
| ssize_t | mx_socket_write_all (MXSocket *sock, const void *buf, size_t bytes) |
| Write the requested number of bytes to a socket. | |
| #define mx_close_socket | ( | s | ) |
Definition at line 49 of file mxsocket.h.
| #define MX_LEN | ( | x | ) |
Definition at line 61 of file mxsocket.h.
| #define mx_set_err | ( | e | ) |
Definition at line 50 of file mxsocket.h.
| #define NULL_SOCKET -1 |
Definition at line 62 of file mxsocket.h.
| #define SOCK_EAGAIN EAGAIN |
Definition at line 53 of file mxsocket.h.
| #define SOCK_EBADF EBADF |
Definition at line 57 of file mxsocket.h.
| #define SOCK_ECONNABORTED ECONNABORTED |
Definition at line 55 of file mxsocket.h.
| #define SOCK_ECONNRESET ECONNRESET |
Definition at line 60 of file mxsocket.h.
| #define SOCK_EINTR EINTR |
Definition at line 52 of file mxsocket.h.
| #define SOCK_EINVAL EINVAL |
Definition at line 56 of file mxsocket.h.
| #define SOCK_ENOTSOCK ENOTSOCK |
Definition at line 58 of file mxsocket.h.
| #define SOCK_ERRNO errno |
Definition at line 51 of file mxsocket.h.
| #define SOCK_ESHUTDOWN ESHUTDOWN |
Definition at line 59 of file mxsocket.h.
| #define SOCK_EWOULDBLOCK EWOULDBLOCK |
Definition at line 54 of file mxsocket.h.
| typedef int mx_socket_fd |
Definition at line 48 of file mxsocket.h.
Accept an incoming connection.
| input | Listening socket. |
| output | Newly accepted socket state. |
Definition at line 278 of file mxsocket.c.
|
nodiscard |
Bind an Internet socket to a local port.
| s | Socket state to configure. |
| port | Service or port name. |
Definition at line 370 of file mxsocket.c.
| void mx_socket_close | ( | MXSocket * | s | ) |
Close a socket if it is open.
| s | Socket state to close. |
Definition at line 323 of file mxsocket.c.
|
nodiscard |
Connect an Internet socket to a remote host.
| s | Socket state to configure. |
| host | Remote host name or address. |
| port | Remote service or port name. |
| type | Socket kind, typically SOCK_STREAM or SOCK_DGRAM. |
Definition at line 362 of file mxsocket.c.
| void mx_socket_ignore_pipe_signal | ( | ) |
Ignore SIGPIPE on platforms that require it.
Definition at line 577 of file mxsocket.c.
|
nodiscard |
Initialize a socket structure with default values.
| s | Socket state to initialize. |
Definition at line 407 of file mxsocket.c.
|
nodiscard |
Bind an IPv6 Internet socket to a local port.
| s | Socket state to configure. |
| port | Service or port name. |
Definition at line 374 of file mxsocket.c.
|
nodiscard |
Connect an IPv6 Internet socket to a remote host.
| s | Socket state to configure. |
| host | Remote host name or address. |
| port | Remote service or port name. |
| type | Socket kind, typically SOCK_STREAM or SOCK_DGRAM. |
Definition at line 366 of file mxsocket.c.
|
nodiscard |
Create and bind an IPv6 Internet socket for listening.
| s | Socket state to configure. |
| port | Service or port name to bind. |
| backlog | Maximum pending connection queue length. |
| type | Socket kind, typically SOCK_STREAM or SOCK_DGRAM. |
Definition at line 274 of file mxsocket.c.
| ssize_t mx_socket_ipv6_recvfrom | ( | MXSocket * | sock, |
| void * | buf, | ||
| size_t | bytes ) |
Receive a datagram using an IPv6 Internet socket.
| sock | Socket state. |
| buf | Output buffer. |
| bytes | Maximum number of bytes to receive. |
Definition at line 632 of file mxsocket.c.
| ssize_t mx_socket_ipv6_sendto | ( | MXSocket * | sock, |
| const void * | buf, | ||
| size_t | bytes ) |
Send a datagram using an IPv6 Internet socket.
| sock | Socket state. |
| buf | Input buffer. |
| bytes | Number of bytes to send. |
Definition at line 592 of file mxsocket.c.
|
nodiscard |
Check whether the socket is open.
| s | Socket state to inspect. |
Definition at line 442 of file mxsocket.c.
|
nodiscard |
Create and bind an Internet socket for listening.
| s | Socket state to configure. |
| port | Service or port name to bind. |
| backlog | Maximum pending connection queue length. |
| type | Socket kind, typically SOCK_STREAM or SOCK_DGRAM. |
Definition at line 270 of file mxsocket.c.
| ssize_t mx_socket_read | ( | MXSocket * | s, |
| void * | data, | ||
| size_t | len, | ||
| int | flags ) |
Read data from a socket.
| s | Socket state. |
| data | Output buffer. |
| len | Number of bytes to read. |
| flags | Platform socket flags. |
Definition at line 422 of file mxsocket.c.
| ssize_t mx_socket_read_all | ( | MXSocket * | sock, |
| void * | buf, | ||
| size_t | bytes ) |
Read the requested number of bytes from a socket.
| sock | Socket state. |
| buf | Output buffer. |
| bytes | Number of bytes to read. |
Definition at line 555 of file mxsocket.c.
|
nodiscard |
Read a line of text from a socket.
| s | Socket state. |
| buffer | Output buffer pointer. |
| len | Output length. |
Definition at line 474 of file mxsocket.c.
| ssize_t mx_socket_recvfrom | ( | MXSocket * | sock, |
| void * | buf, | ||
| size_t | bytes ) |
Receive a datagram using an Internet socket.
| sock | Socket state. |
| buf | Output buffer. |
| bytes | Maximum number of bytes to receive. |
Definition at line 601 of file mxsocket.c.
| ssize_t mx_socket_recvfrom_address | ( | MXSocket * | sock, |
| void * | buf, | ||
| size_t | bytes, | ||
| MXSocketAddress * | address ) |
Definition at line 614 of file mxsocket.c.
| ssize_t mx_socket_send | ( | MXSocket * | s, |
| const void * | data, | ||
| size_t | len, | ||
| int | flags ) |
Write data to a socket.
| s | Socket state. |
| data | Input buffer. |
| len | Number of bytes to send. |
| flags | Platform socket flags. |
Definition at line 432 of file mxsocket.c.
| ssize_t mx_socket_sendto | ( | MXSocket * | sock, |
| const void * | buf, | ||
| size_t | bytes ) |
Send a datagram using an Internet socket.
| sock | Socket state. |
| buf | Input buffer. |
| bytes | Number of bytes to send. |
Definition at line 583 of file mxsocket.c.
| ssize_t mx_socket_sendto_address | ( | MXSocket * | sock, |
| const void * | buf, | ||
| size_t | bytes, | ||
| const MXSocketAddress * | address ) |
Definition at line 625 of file mxsocket.c.
|
nodiscard |
Toggle blocking mode for a socket.
| s | Socket state to update. |
| state | Desired blocking state. |
Definition at line 331 of file mxsocket.c.
|
nodiscard |
Bind a UNIX-domain socket to a local path.
| s | Socket state to configure. |
| path | Filesystem path for the socket. |
Definition at line 378 of file mxsocket.c.
|
nodiscard |
Connect a UNIX-domain socket to a local path.
| s | Socket state to configure. |
| path | Filesystem path for the socket. |
| type | Socket kind, typically SOCK_STREAM or SOCK_DGRAM. |
Definition at line 241 of file mxsocket.c.
|
nodiscard |
Create and bind a UNIX-domain socket for listening.
| s | Socket state to configure. |
| path | Filesystem path for the socket. |
| backlog | Maximum pending connection queue length. |
| type | Socket kind, typically SOCK_STREAM or SOCK_DGRAM. |
Definition at line 202 of file mxsocket.c.
| ssize_t mx_socket_unix_recvfrom | ( | MXSocket * | sock, |
| void * | buf, | ||
| size_t | bytes ) |
Receive a datagram using a UNIX-domain socket.
| sock | Socket state. |
| buf | Output buffer. |
| bytes | Maximum number of bytes to receive. |
Definition at line 653 of file mxsocket.c.
| ssize_t mx_socket_unix_sendto | ( | MXSocket * | sock, |
| const void * | buf, | ||
| size_t | bytes ) |
Send a datagram using a UNIX-domain socket.
| sock | Socket state. |
| buf | Input buffer. |
| bytes | Number of bytes to send. |
Definition at line 645 of file mxsocket.c.
|
nodiscard |
Check whether the socket handle is valid.
| s | Socket state to inspect. |
Definition at line 416 of file mxsocket.c.
| ssize_t mx_socket_write_all | ( | MXSocket * | sock, |
| const void * | buf, | ||
| size_t | bytes ) |
Write the requested number of bytes to a socket.
| sock | Socket state. |
| buf | Input buffer. |
| bytes | Number of bytes to write. |
Definition at line 535 of file mxsocket.c.