X_NETWORK_VERSION_MAJOR
#define X_NETWORK_VERSION_MAJOR 1
STDX - Cross-Platform Networking API Part of the STDX General Purpose C Library by marciovmf License: MIT https://github.com/marciovmf/stdx
To compile the implementation define X_IMPL_NET
in one source file before including this header.
To customize how this module allocates memory, define
X_NET_ALLOC / X_NET_FREE before including.
#define X_NETWORK_VERSION_MAJOR 1
#define X_NETWORK_VERSION_MINOR 0
#define X_NETWORK_VERSION_PATCH 0
#define X_NETWORK_VERSION(X_NETWORK_VERSION_MAJOR *10000+X_NETWORK_VERSION_MINOR *100+X_NETWORK_VERSION_PATCH)
typedef SOCKET XSocket;
typedef int32_t socklen_t;
#define INVALID_SOCKET(-1)
typedef struct XAddress XAddress;
typedef enum{
X_NET_AF_IPV4=1,
X_NET_AF_IPV6=2,
}XAddressFamily;
typedef enum{
X_NET_SOCK_STREAM=1,
X_NET_SOCK_DGRAM=2,
}XSocketType;
typedef struct{
int8_t name[128];
}XNetAdapter;
typedef struct{
int8_t name[128];
int8_t description[256];
int8_t mac[18];
int8_t ipv4[16];
int8_t ipv6[46];
uint64_t speed_bps;
bool is_wireless;
int32_t mtu;
uint32_t ifindex;
}XNetAdapterInfo;
Initialize the networking subsystem.
bool x_net_init(void);
True on success, false on failure.
Shut down the networking subsystem and release associated resources.
void x_net_shutdown(void);
Nothing.
Check whether a socket handle represents a valid socket.
bool x_net_socket_is_valid(XSocket sock);
XSocket sockTrue if the socket is valid, false otherwise.
Close a socket handle.
void x_net_close(XSocket sock);
XSocket sockNothing.
Enable or disable non-blocking mode on a socket.
int32_t x_net_set_nonblocking(
XSocket sock,
int32_t nonblocking
);
XSocket sockint32_t nonblocking0 on success, or -1 on error.
Create a socket with the specified address family and socket type.
XSocket x_net_socket(
XAddressFamily family,
XSocketType type
);
XAddressFamily familyXSocketType typeNew socket handle, or an invalid socket value on failure.
Create an IPv4 TCP socket.
XSocket x_net_socket_tcp4(void);
New socket handle, or an invalid socket value on failure.
Create an IPv6 TCP socket.
XSocket x_net_socket_tcp6(void);
New socket handle, or an invalid socket value on failure.
Create an IPv4 UDP socket.
XSocket x_net_socket_udp4(void);
New socket handle, or an invalid socket value on failure.
Create an IPv6 UDP socket.
XSocket x_net_socket_udp6(void);
New socket handle, or an invalid socket value on failure.
Bind a socket to a specific local address.
bool x_net_bind(
XSocket sock,
const XAddress *addr
);
True on success, false on failure.
Bind a socket to any local address of the given family and port.
bool x_net_bind_any(
XSocket sock,
XAddressFamily family,
uint16_t port
);
XSocket sockXAddressFamily familyuint16_t portTrue on success, false on failure.
Bind a UDP socket to any local IPv4 address (system-chosen port/address if applicable).
bool x_net_bind_any_udp(XSocket sock);
XSocket sockTrue on success, false on failure.
Bind a UDP socket to any local IPv6 address (system-chosen port/address if applicable).
bool x_net_bind_any_udp6(XSocket sock);
XSocket sockTrue on success, false on failure.
Mark a bound socket as listening for incoming connections.
bool x_net_listen(
XSocket sock,
int32_t backlog
);
XSocket sockint32_t backlogTrue on success, false on failure.
Accept an incoming connection on a listening socket.
XSocket x_net_accept(
XSocket sock,
XAddress *out_addr
);
New connected socket handle, or an invalid socket value on failure.
Connect a socket to a remote address.
int32_t x_net_connect(
XSocket sock,
const XAddress *addr
);
0 on success, -1 on error, or a platform-defined in-progress code for non-blocking sockets.
Send data on a connected socket.
size_t x_net_send(
XSocket sock,
const void *buf,
size_t len
);
XSocket sockconst void *bufsize_t lenNumber of bytes sent (may be less than len), or 0 on error/connection closed depending on implementation.
Receive data from a connected socket.
size_t x_net_recv(
XSocket sock,
void *buf,
size_t len
);
XSocket sockvoid *bufsize_t lenNumber of bytes received, or 0 on connection closed/error depending on implementation.
Send data to a specific address (datagram sockets).
size_t x_net_sendto(
XSocket sock,
const void *buf,
size_t len,
const XAddress *addr
);
Number of bytes sent, or 0 on error depending on implementation.
Receive data from a socket and optionally retrieve the sender address.
size_t x_net_recvfrom(
XSocket sock,
void *buf,
size_t len,
XAddress *out_addr
);
Number of bytes received, or 0 on error/no data depending on implementation.
Wait for readability on multiple sockets.
int32_t x_net_select(
XSocket *read_sockets,
int32_t read_count,
int32_t timeout_ms
);
XSocket *read_socketsint32_t read_countint32_t timeout_msNumber of sockets ready for reading, 0 on timeout, or -1 on error.
Wait for specific events on a socket.
int32_t x_net_poll(
XSocket sock,
int32_t events,
int32_t timeout_ms
);
XSocket sockint32_t eventsint32_t timeout_msEvent bitmask of occurred events, 0 on timeout, or -1 on error.
Resolve a host and service/port into a network address.
bool x_net_resolve(
const int8_t *host,
const int8_t *port,
XAddressFamily family,
XAddress *out_addr
);
const int8_t *hostconst int8_t *portXAddressFamily familyXAddress *out_addrTrue on success, false on failure.
Parse an IP string into a raw binary address.
int32_t x_net_parse_ip(
XAddressFamily family,
const int8_t *ip,
void *out_addr
);
XAddressFamily familyconst int8_t *ipvoid *out_addr0 on success, or -1 on failure.
Format an address as a human-readable string.
int32_t x_net_format_address(
const XAddress *addr,
char *out_str,
int32_t maxlen
);
const XAddress *addrchar *out_strint32_t maxlenNumber of characters written (excluding terminator), or -1 on failure.
Clear an XAddress structure (set to zero).
void x_net_address_clear(XAddress *addr);
XAddress *addrNothing.
Create an "any" address for binding on the given family and port.
void x_net_address_any(
XAddress *out_addr,
int32_t family,
uint16_t port
);
XAddress *out_addrint32_t familyuint16_t portNothing.
Build an XAddress from an IP string and port.
int32_t x_net_address_from_ip_port(
const int8_t *ip,
uint16_t port,
XAddress *out_addr
);
const int8_t *ipuint16_t portXAddress *out_addr0 on success, or -1 on failure.
Compare two XAddress values for equality.
int32_t x_net_address_equal(
const XAddress *a,
const XAddress *b
);
Non-zero if equal, 0 if not equal.
Convert an XAddress to a string representation (typically "IP:port").
int32_t x_net_address_to_string(
const XAddress *addr,
char *buf,
int32_t buf_len
);
const XAddress *addrchar *bufint32_t buf_lenNumber of characters written (excluding terminator), or -1 on failure.
Resolve a DNS hostname to an address.
int32_t x_net_dns_resolve(
const int8_t *hostname,
XAddressFamily family,
XAddress *out_addr
);
const int8_t *hostnameXAddressFamily familyXAddress *out_addr0 on success, or -1 on failure.
Join an IPv4 multicast group by textual group address.
bool x_net_join_multicast_ipv4(
XSocket sock,
const int8_t *group
);
XSocket sockconst int8_t *groupTrue on success, false on failure.
Leave an IPv4 multicast group by textual group address.
bool x_net_leave_multicast_ipv4(
XSocket sock,
const int8_t *group
);
XSocket sockconst int8_t *groupTrue on success, false on failure.
Join an IPv6 multicast group.
bool x_net_join_multicast_ipv6(
XSocket sock,
const int8_t *multicast_ip,
uint32_t ifindex
);
XSocket sockconst int8_t *multicast_ipuint32_t ifindexTrue on success, false on failure.
Leave an IPv6 multicast group.
bool x_net_leave_multicast_ipv6(
XSocket sock,
const int8_t *multicast_ip,
uint32_t ifindex
);
XSocket sockconst int8_t *multicast_ipuint32_t ifindexTrue on success, false on failure.
Join an IPv4 multicast group using an XAddress structure.
bool x_net_join_multicast_ipv4_addr(
XSocket sock,
const XAddress *group_addr
);
True on success, false on failure.
Leave an IPv4 multicast group using an XAddress structure.
bool x_net_leave_multicast_ipv4_addr(
XSocket sock,
const XAddress *group_addr
);
True on success, false on failure.
Enable or disable broadcast capability on a socket.
bool x_net_enable_broadcast(
XSocket sock,
bool enable
);
XSocket sockbool enableTrue on success, false on failure.
Get the number of network adapters available on the system.
int32_t x_net_get_adapter_count(void);
Number of adapters, or -1 on error.
List available network adapters.
int32_t x_net_list_adapters(
XNetAdapter *out_adapters,
int32_t max_adapters
);
XNetAdapter *out_adaptersint32_t max_adaptersNumber of adapters written, or -1 on error.
Retrieve detailed information about a network adapter by name.
bool x_net_get_adapter_info(
const int8_t *name,
XNetAdapterInfo *out_info
);
const int8_t *nameXNetAdapterInfo *out_infoTrue on success, false on failure.
Get the last network error code for the current thread/process.
int32_t x_net_get_last_error(void);
Last error code (WSAGetLastError() on Windows, errno on POSIX).
Write a human-readable message for the last network error into a buffer.
int32_t x_net_get_last_error_message(
char *buf,
int32_t buf_len
);
char *bufint32_t buf_len0 on success, or -1 on failure (e.g., buffer too small).
Internal macro for allocating memory. To override how this header allocates memory, define this macro with a different implementation before including this header.
#define X_NET_ALLOC(sz) malloc(sz)
szInternal macro for freeing memory. To override how this header frees memory, define this macro with a different implementation before including this header.
#define X_NET_FREE(p) free(p)
p#define X_NET_POLLIN 0x01
#define X_NET_POLLOUT 0x02
int32_t x_net_get_adapter_count_win32(void);
int32_t x_net_list_adapters_win32(
XNetAdapter *out_adapters,
int32_t max_adapters
);
int32_t x_net_get_adapter_info_win32(
const int8_t *name,
XNetAdapterInfo *out_info
);
int32_t x_net_get_adapter_count_posix(void);
int32_t x_net_list_adapters_posix(
XNetAdapter *out_adapters,
int32_t max_adapters
);
int32_t x_net_get_adapter_info_posix(
const int8_t *name,
XNetAdapterInfo *out_info
);