Skip to content
modkitv0.2

net.h

#include <modkit/net.h>23 functions · 13 structs · 5 enums · 20 typedefs · 3 macros

Poll-driven TCP and UDP networking primitives.

Networking is asynchronous. Backend threads and browser callbacks only enqueue work; user callbacks run from mk_net_poll() on the thread that initialized the subsystem.

Functions

mk_net_capabilitiesfunction

mk_net_capabilities_t mk_net_capabilities(void)

Return the capabilities supported by the current target at runtime.

Returns The resulting handle or value.

mk_net_desc_initfunction

mk_net_desc mk_net_desc_init(void)

Return limits of 4096 pending events and 16 MiB of pending payload data.

Returns The resulting value.

mk_net_initfunction

mk_result mk_net_init(const mk_net_desc *desc)

Initialize networking and make the calling thread its owner.

ParameterTypeDescription
descconst mk_net_desc *Runtime limits, or NULL for mk_net_desc_init() defaults.

Returns MK_SUCCESS, or an initialization/state error.

mk_net_is_initializedfunction

bool mk_net_is_initialized(void)

Return whether the networking runtime is initialized.

Returns True when the condition holds.

mk_net_last_errorfunction

mk_net_error mk_net_last_error(void)

Return the calling thread's most recent immediate networking error.

Returns The resulting value.

mk_net_pollfunction

uint32_t mk_net_poll(uint32_t timeout_ms, uint32_t max_events)

Deliver a snapshot of pending events.

ParameterTypeDescription
timeout_msuint32_tMaximum wait when no event is ready; zero is non-blocking.
max_eventsuint32_tMaximum callbacks to deliver; zero drains the snapshot.

Returns Number of callbacks delivered.

mk_net_shutdownfunction

void mk_net_shutdown(void)

Close all networking resources and discard pending events.

mk_tcp_closefunction

mk_result mk_tcp_close(mk_tcp_t connection)

Close a TCP connection; any queued CLOSED event remains deliverable.

ParameterTypeDescription
connectionmk_tcp_tValue for connection.

Returns MK_SUCCESS or an error result.

mk_tcp_connectfunction

mk_result mk_tcp_connect(const mk_tcp_connect_desc *desc, mk_tcp_t *out_connection)

Start an asynchronous TCP connection and return its handle.

ParameterTypeDescription
descconst mk_tcp_connect_desc *Configuration descriptor.
out_connectionmk_tcp_t *Receives the connection.

Returns MK_SUCCESS or an error result.

mk_tcp_connect_desc_initfunction

mk_tcp_connect_desc mk_tcp_connect_desc_init(void)

Return 10-second, 4-MiB defaults with TCP_NODELAY and keepalive enabled.

Returns The resulting value.

mk_tcp_listenfunction

mk_result mk_tcp_listen(const mk_tcp_listen_desc *desc, mk_tcp_listener_t *out_listener)

Create and start a native TCP listener.

ParameterTypeDescription
descconst mk_tcp_listen_desc *Configuration descriptor.
out_listenermk_tcp_listener_t *Receives the listener.

Returns MK_SUCCESS or an error result.

mk_tcp_listen_desc_initfunction

mk_tcp_listen_desc mk_tcp_listen_desc_init(void)

Return all-interface, 1024-client, 4-MiB listener defaults.

Returns The resulting value.

mk_tcp_listener_closefunction

mk_result mk_tcp_listener_close(mk_tcp_listener_t listener)

Stop a listener and close its accepted connections.

ParameterTypeDescription
listenermk_tcp_listener_tValue for listener.

Returns MK_SUCCESS or an error result.

mk_tcp_listener_portfunction

uint16_t mk_tcp_listener_port(mk_tcp_listener_t listener)

Return the listener's bound port, including an automatically selected port.

ParameterTypeDescription
listenermk_tcp_listener_tValue for listener.

Returns The resulting value.

mk_tcp_listener_validfunction

bool mk_tcp_listener_valid(mk_tcp_listener_t handle)

Return whether a TCP listener handle is nonzero.

ParameterTypeDescription
handlemk_tcp_listener_tValue for handle.

Returns True when the operation succeeds.

mk_tcp_sendfunction

mk_result mk_tcp_send(mk_tcp_t connection, const void *data, size_t size)

Queue bytes for transmission without blocking.

ParameterTypeDescription
connectionmk_tcp_tValue for connection.
dataconst void *Data buffer.
sizesize_tSize in bytes.

Returns MK_SUCCESS or an error result.

mk_tcp_validfunction

bool mk_tcp_valid(mk_tcp_t handle)

Return whether a TCP connection handle is nonzero.

ParameterTypeDescription
handlemk_tcp_tValue for handle.

Returns True when the operation succeeds.

mk_udp_closefunction

mk_result mk_udp_close(mk_udp_t socket)

Close a UDP socket and invalidate its handle.

ParameterTypeDescription
socketmk_udp_tValue for socket.

Returns MK_SUCCESS or an error result.

mk_udp_desc_initfunction

mk_udp_desc mk_udp_desc_init(void)

Return an all-interface descriptor with a 65507-byte datagram limit.

Returns The resulting value.

mk_udp_openfunction

mk_result mk_udp_open(const mk_udp_desc *desc, mk_udp_t *out_socket)

Create and bind a native UDP socket.

ParameterTypeDescription
descconst mk_udp_desc *Configuration descriptor.
out_socketmk_udp_t *Receives the socket.

Returns MK_SUCCESS or an error result.

mk_udp_portfunction

uint16_t mk_udp_port(mk_udp_t socket)

Return the socket's bound port, including an automatically selected port.

ParameterTypeDescription
socketmk_udp_tValue for socket.

Returns The resulting value.

mk_udp_send_tofunction

mk_result mk_udp_send_to(mk_udp_t socket, const mk_net_address *destination, const void *data, size_t size)

Send one datagram to a numeric address or DNS hostname.

ParameterTypeDescription
socketmk_udp_tValue for socket.
destinationconst mk_net_address *Value for destination.
dataconst void *Data buffer.
sizesize_tSize in bytes.

Returns MK_SUCCESS or an error result.

mk_udp_validfunction

bool mk_udp_valid(mk_udp_t handle)

Return whether a UDP socket handle is nonzero.

ParameterTypeDescription
handlemk_udp_tValue for handle.

Returns True when the operation succeeds.

Structs

mk_net_addressstruct

Numeric address used by UDP events and sends.

FieldTypeDescription
hostcharNumeric IPv4 or IPv6 address without brackets.
portuint16_tHost-order port number.
ipv6boolTrue when host contains an IPv6 address.

mk_net_descstruct

Process-wide networking runtime limits.

FieldTypeDescription
max_queued_eventsuint32_tPending callback limit; zero uses the default.
max_queued_bytessize_tPending payload-byte limit; zero uses the default.

mk_net_errorstruct

Detailed error information returned in events and by mk_net_last_error().

FieldTypeDescription
codemk_net_error_codePortable error category.
platform_codeint32_tBackend/native error code, or zero if unavailable.
messagecharNull-terminated diagnostic text.

mk_tcp_connect_descstruct

Descriptor for an asynchronous TCP client connection.

FieldTypeDescription
hostconst char *DNS name or numeric address; required.
portuint16_tRemote port; must be nonzero.
connect_timeout_msuint32_tConnect timeout; zero uses 10 seconds.
max_send_queue_bytessize_tPending-send limit; zero uses 4 MiB.
no_delayboolEnable TCP_NODELAY.
keepaliveboolEnable TCP keepalive.
callbackmk_tcp_event_fnRequired connection callback.
user_datavoid *Opaque value passed to callback.

mk_tcp_eventstruct

Data delivered to a TCP callback from mk_net_poll().

FieldTypeDescription
typemk_tcp_event_typeEvent kind.
connectionmk_tcp_tConnection associated with the event.
listenermk_tcp_listener_tAccepting listener, or invalid for clients.
peermk_net_addressRemote numeric address when known.
dataconst void *Received bytes for MK_TCP_EVENT_DATA.
sizesize_tNumber of bytes in data.
errormk_net_errorClose/error detail; code is NONE on success.

mk_tcp_handlestruct

Generational handle for a TCP connection.

FieldTypeDescription
iduint32_tOpaque identifier; zero is invalid.

mk_tcp_listen_descstruct

Descriptor for a native TCP listener.

FieldTypeDescription
bind_addressconst char *Local address; NULL means all IPv4 interfaces.
portuint16_tLocal port; zero selects an available port.
max_connectionsuint32_tConnection limit; zero uses 1024.
max_send_queue_bytessize_tPer-client send limit; zero uses 4 MiB.
no_delayboolEnable TCP_NODELAY on accepted connections.
keepaliveboolEnable keepalive on accepted connections.
callbackmk_tcp_event_fnRequired listener/connection callback.
user_datavoid *Opaque value passed to callback.

mk_tcp_listener_handlestruct

Generational handle for a TCP listener.

FieldTypeDescription
iduint32_tOpaque identifier; zero is invalid.

mk_tls_client_configstruct

Optional TLS trust configuration for an HTTPS or WSS client.

FieldTypeDescription
trustmk_tls_trust_modeTrust policy; defaults to MK_TLS_TRUST_DEFAULT.
ca_pemconst void *PEM CA bundle used by custom trust modes.
ca_pem_sizesize_tCA bundle size in bytes; a trailing NUL is optional.

mk_tls_server_configstruct

In-memory credentials for a native HTTPS or WSS server.

FieldTypeDescription
certificate_pemconst void *PEM certificate or certificate chain.
certificate_pem_sizesize_tCertificate data size in bytes.
private_key_pemconst void *PEM private key matching the certificate.
private_key_pem_sizesize_tPrivate-key data size in bytes.

mk_udp_descstruct

Descriptor for a native UDP socket.

FieldTypeDescription
bind_addressconst char *Local address; NULL means all IPv4 interfaces.
portuint16_tLocal port; zero selects an available port.
max_datagram_sizesize_tRetained byte limit; zero uses 65507.
allow_broadcastboolEnable IPv4 broadcast sends.
callbackmk_udp_event_fnRequired socket callback.
user_datavoid *Opaque value passed to callback.

mk_udp_eventstruct

Data delivered to a UDP callback from mk_net_poll().

FieldTypeDescription
typemk_udp_event_typeEvent kind.
socketmk_udp_tReceiving socket.
sourcemk_net_addressDatagram source address.
dataconst void *Received bytes for DATAGRAM events.
sizesize_tBytes retained in data.
original_sizesize_tWire size before application truncation.
dropped_datagramsuint64_tCumulative drops reported by OVERFLOW.
truncatedboolTrue if original_size exceeded size.
errormk_net_errorError detail for ERROR/OVERFLOW events.

mk_udp_handlestruct

Generational handle for a UDP socket.

FieldTypeDescription
iduint32_tOpaque identifier; zero is invalid.

Enums

mk_net_capabilityenum

Runtime networking capability bits.

ValueDescription
MK_NET_CAP_TCPRaw TCP clients are available.
MK_NET_CAP_TCP_SERVERRaw TCP listeners are available.
MK_NET_CAP_UDPUDP sockets are available.
MK_NET_CAP_HTTP_CLIENTHTTP/HTTPS clients are available.
MK_NET_CAP_HTTP_SERVERHTTP/HTTPS servers are available.
MK_NET_CAP_WEBSOCKET_CLIENTWebSocket clients are available.
MK_NET_CAP_WEBSOCKET_SERVERWebSocket servers are available.
MK_NET_CAP_TLSHTTPS and WSS are available.
MK_NET_CAP_CUSTOM_CAClient CA bundles can be supplied.
MK_NET_CAP_CUSTOM_WS_HEADERSWS handshake headers can be supplied.

mk_net_error_codeenum

Portable category for the most recent networking failure.

ValueDescription
MK_NET_ERROR_NONENo networking error.
MK_NET_ERROR_CANCELLEDOperation was cancelled by the application.
MK_NET_ERROR_TIMEOUTConnection or operation timed out.
MK_NET_ERROR_DNSHostname resolution failed.
MK_NET_ERROR_CONNECTConnect, bind, or transport setup failed.
MK_NET_ERROR_TLSTLS configuration or negotiation failed.
MK_NET_ERROR_PROTOCOLMalformed or unsupported protocol data.
MK_NET_ERROR_LIMITConfigured resource or body limit was reached.
MK_NET_ERROR_QUEUE_FULLA bounded event or send queue was full.
MK_NET_ERROR_CLOSEDThe transport closed before completion.
MK_NET_ERROR_PLATFORMPlatform restriction or native API failure.

mk_tcp_event_typeenum

TCP connection event kind.

ValueDescription
MK_TCP_EVENT_CONNECTEDAn outgoing connection is ready.
MK_TCP_EVENT_ACCEPTEDA listener accepted a connection.
MK_TCP_EVENT_DATAA byte-stream chunk was received.
MK_TCP_EVENT_CLOSEDThe connection closed; terminal event.

mk_tls_trust_modeenum

Certificate-authority policy for native HTTPS and WSS clients.

ValueDescription
MK_TLS_TRUST_DEFAULTTrust ModKit's embedded public roots.
MK_TLS_TRUST_DEFAULT_PLUS_CUSTOMTrust public roots and ca_pem.
MK_TLS_TRUST_CUSTOM_ONLYTrust only ca_pem.
MK_TLS_TRUST_INSECUREDisable peer verification; testing only.

mk_udp_event_typeenum

UDP socket event kind.

ValueDescription
MK_UDP_EVENT_DATAGRAMA datagram was received.
MK_UDP_EVENT_OVERFLOWOne or more datagrams were dropped by limits.
MK_UDP_EVENT_ERRORA transport error occurred.

Typedefs

mk_net_addresstypedef

typedef struct mk_net_address mk_net_address

Numeric address used by UDP events and sends.

mk_net_capabilities_ttypedef

typedef uint64_t mk_net_capabilities_t

Bitmask returned by mk_net_capabilities().

mk_net_desctypedef

typedef struct mk_net_desc mk_net_desc

Process-wide networking runtime limits.

mk_net_errortypedef

typedef struct mk_net_error mk_net_error

Detailed error information returned in events and by mk_net_last_error().

mk_net_error_codetypedef

typedef enum mk_net_error_code mk_net_error_code

Portable category for the most recent networking failure.

mk_tcp_connect_desctypedef

typedef struct mk_tcp_connect_desc mk_tcp_connect_desc

Descriptor for an asynchronous TCP client connection.

mk_tcp_eventtypedef

typedef struct mk_tcp_event mk_tcp_event

Data delivered to a TCP callback from mk_net_poll().

mk_tcp_event_fntypedef

typedef void(*) mk_tcp_event_fn(const mk_tcp_event *event, void *user_data)

TCP event callback invoked by mk_net_poll() on the owner thread.

mk_tcp_event_typetypedef

typedef enum mk_tcp_event_type mk_tcp_event_type

TCP connection event kind.

mk_tcp_listen_desctypedef

typedef struct mk_tcp_listen_desc mk_tcp_listen_desc

Descriptor for a native TCP listener.

mk_tcp_listener_ttypedef

typedef struct mk_tcp_listener_handle mk_tcp_listener_t

Generational handle for a TCP listener.

mk_tcp_ttypedef

typedef struct mk_tcp_handle mk_tcp_t

Generational handle for a TCP connection.

mk_tls_client_configtypedef

typedef struct mk_tls_client_config mk_tls_client_config

Optional TLS trust configuration for an HTTPS or WSS client.

mk_tls_server_configtypedef

typedef struct mk_tls_server_config mk_tls_server_config

In-memory credentials for a native HTTPS or WSS server.

mk_tls_trust_modetypedef

typedef enum mk_tls_trust_mode mk_tls_trust_mode

Certificate-authority policy for native HTTPS and WSS clients.

mk_udp_desctypedef

typedef struct mk_udp_desc mk_udp_desc

Descriptor for a native UDP socket.

mk_udp_eventtypedef

typedef struct mk_udp_event mk_udp_event

Data delivered to a UDP callback from mk_net_poll().

mk_udp_event_fntypedef

typedef void(*) mk_udp_event_fn(const mk_udp_event *event, void *user_data)

UDP event callback invoked by mk_net_poll() on the owner thread.

mk_udp_event_typetypedef

typedef enum mk_udp_event_type mk_udp_event_type

UDP socket event kind.

mk_udp_ttypedef

typedef struct mk_udp_handle mk_udp_t

Generational handle for a UDP socket.

Macros

MK_TCP_INVALIDdefine

MK_TCP_INVALID

Invalid TCP connection handle.

MK_TCP_LISTENER_INVALIDdefine

MK_TCP_LISTENER_INVALID

Invalid TCP listener handle.

MK_UDP_INVALIDdefine

MK_UDP_INVALID

Invalid UDP socket handle.