|
| struct bj_tcp_listener * | bj_listen_tcp (const char *host, uint16_t port, uint16_t hint, int backlog, struct bj_error **error) |
| struct bj_tcp_stream * | bj_connect_tcp (const char *host, uint16_t port, uint16_t hint, uint32_t ms_timeout, struct bj_error **error) |
| struct bj_tcp_stream * | bj_connect_tcp_to (struct bj_address *addr, uint16_t hints, uint32_t ms_timeout, struct bj_error **error) |
| struct bj_tcp_stream * | bj_accept_tcp (struct bj_tcp_listener *listener, uint32_t ms_timeout, struct bj_error **error) |
| int | bj_tcp_recv (struct bj_tcp_stream *stream, void *buf, size_t len) |
| int | bj_tcp_peek (struct bj_tcp_stream *stream, void *buf, size_t len) |
| int | bj_tcp_send (struct bj_tcp_stream *stream, const void *buf, size_t len) |
| void | bj_tcp_shutdown (struct bj_tcp_stream *stream, int mode) |
| bj_bool | bj_set_tcp_keepalive (struct bj_tcp_stream *stream, bj_bool enable) |
| bj_bool | bj_get_tcp_keepalive (struct bj_tcp_stream *stream) |
| bj_bool | bj_tcp_wait (struct bj_tcp_stream *stream, uint32_t ms_timeout) |
| bj_bool | bj_set_tcp_recv_timeout (struct bj_tcp_stream *stream, uint32_t ms_timeout) |
| uint32_t | bj_get_tcp_recv_timeout (struct bj_tcp_stream *stream) |
| bj_bool | bj_set_tcp_send_timeout (struct bj_tcp_stream *stream, uint32_t ms_timeout) |
| uint32_t | bj_get_tcp_send_timeout (struct bj_tcp_stream *stream) |
| void | bj_close_tcp_stream (struct bj_tcp_stream *stream) |
| void | bj_close_tcp_listener (struct bj_tcp_listener *listener) |
| size_t | bj_tcp_peer_address (const struct bj_tcp_stream *stream, char *buf, size_t size) |
| size_t | bj_tcp_local_address (const struct bj_tcp_stream *stream, char *buf, size_t size) |
| uint16_t | bj_tcp_listener_port (const struct bj_tcp_listener *listener) |
| int | bj_tcp_available (struct bj_tcp_stream *stream) |
|
| struct bj_udp * | bj_open_udp (const char *host, uint16_t port, uint16_t hints, struct bj_error **error) |
| void | bj_close_udp (struct bj_udp *udp) |
| int | bj_udp_send (struct bj_udp *udp, const char *host, uint16_t port, const void *data, size_t len, struct bj_error **error) |
| int | bj_udp_send_to (struct bj_udp *udp, const struct bj_address *addr, const void *data, size_t len, struct bj_error **error) |
| struct bj_datagram * | bj_udp_recv (struct bj_udp *udp, struct bj_error **error) |
| bj_bool | bj_udp_available (struct bj_udp *udp) |
| bj_bool | bj_udp_wait (struct bj_udp *udp, uint32_t ms_timeout) |
| uint16_t | bj_udp_local_port (const struct bj_udp *udp) |
| size_t | bj_udp_local_address (const struct bj_udp *udp, char *buf, size_t size) |
Basic network API.
The net API provides a thin wrapping mechanism around POSIX sockets and Winsocks for TCP network communication.
Basic Usage
struct bj_tcp_listener bj_tcp_listener
Definition api.h:305
struct bj_tcp_stream bj_tcp_stream
Definition api.h:306
struct bj_tcp_stream * bj_accept_tcp(struct bj_tcp_listener *listener, uint32_t ms_timeout, struct bj_error **error)
Accepts an incoming connection on a TCP listener with timeout.
struct bj_tcp_listener * bj_listen_tcp(const char *host, uint16_t port, uint16_t hint, int backlog, struct bj_error **error)
Creates a TCP listener bound to the specified host and port.
struct bj_tcp_stream * bj_connect_tcp(const char *host, uint16_t port, uint16_t hint, uint32_t ms_timeout, struct bj_error **error)
Connects to a TCP server at the specified host and port.
Address Family Hints
Use BJ_RESOLVE_IPV4 and BJ_RESOLVE_IPV6 to control address resolution:
- 0 or both flags: Accept any address family
- BJ_RESOLVE_IPV4: IPv4 only
- BJ_RESOLVE_IPV6: IPv6 only
◆ BJ_ADDR_STRLEN
| #define BJ_ADDR_STRLEN 46 |
Maximum length of an address string (including null terminator).
Sufficient for both IPv4 (e.g., "255.255.255.255") and IPv6 (e.g., "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255") addresses.
◆ BJ_NET_BACKLOG_DEFAULT
| #define BJ_NET_BACKLOG_DEFAULT 0 |
Default backlog value for bj_listen_tcp.
Uses a reasonable implementation-defined default.
◆ BJ_NET_BACKLOG_MAX
| #define BJ_NET_BACKLOG_MAX (-1) |
Maximum backlog value for bj_listen_tcp.
Uses the system's maximum allowed backlog (SOMAXCONN).
◆ bj_net_hints
Hint flags for network operations.
These flags can be combined with bitwise OR to configure socket behavior.
| Enumerator |
|---|
| BJ_NET_NOHINT | No hint provided.
|
| BJ_RESOLVE_IPV4 | Restrict address resolution to IPv4.
|
| BJ_RESOLVE_IPV6 | Restrict address resolution to IPv6.
|
| BJ_EXCLUSIVE_BIND | Disable address reuse.
|
| BJ_TCP_DELAY_ACK | Enable Nagle's algorithm.
|
| BJ_NONBLOCKING | Non-blocking mode.
|
| BJ_BROADCAST | Enable UDP broadcast (SO_BROADCAST).
|
◆ bj_net_shutdown
Shutdown mode for bj_tcp_shutdown.
| Enumerator |
|---|
| BJ_SHUTDOWN_RECEIVE | Shutdown receive operations.
|
| BJ_SHUTDOWN_SEND | Shutdown send operations.
|
| BJ_SHUTDOWN_BOTH | Shutdown both send and receive.
|
◆ bj_accept_tcp()
Accepts an incoming connection on a TCP listener with timeout.
Waits for a client connection up to the specified timeout.
- Parameters
-
| listener | The listener to accept connections on. |
| ms_timeout | Timeout in milliseconds. 0 = non-blocking, UINT32_MAX = infinite. |
| error | Optional error output. Set to BJ_ERROR_NETWORK_TIMEOUT on timeout. |
- Returns
- A new TCP stream for the accepted connection, or NULL on timeout/failure.
- See also
- bj_listen_tcp To create a listener.
-
bj_close_tcp_stream To close the accepted connection.
◆ bj_address_count()
| size_t bj_address_count |
( |
const struct bj_address * | addr | ) |
|
Returns number of resolved addresses.
DNS resolution may return multiple IPs for a single hostname.
- Parameters
-
| addr | The address to query. |
- Returns
- Number of resolved addresses.
◆ bj_address_equals()
Compares two addresses for equality.
- Parameters
-
| a | First address. |
| b | Second address. |
- Returns
- BJ_TRUE if addresses are equal.
◆ bj_address_next()
Advances to the next resolved address.
Use this to manually iterate through multiple resolved IPs for fallback on connection failure.
- Parameters
-
| addr | The address to advance. |
- Returns
- BJ_TRUE if advanced to next, BJ_FALSE if no more addresses.
◆ bj_address_port()
| uint16_t bj_address_port |
( |
const struct bj_address * | addr | ) |
|
Gets the port from a resolved address.
- Parameters
-
| addr | The address to query. |
- Returns
- Port number in host byte order.
◆ bj_address_reset()
Resets address iteration to the first resolved address.
- Parameters
-
| addr | The address to reset. |
◆ bj_address_string()
| size_t bj_address_string |
( |
const struct bj_address * | addr, |
|
|
char * | buf, |
|
|
size_t | size ) |
Gets the IP address string from a resolved address.
- Parameters
-
| addr | The address to query. |
| buf | Buffer to store the string. May be NULL to query length. |
| size | Buffer size. |
- Returns
- Length of address string, or 0 on error.
◆ bj_close_datagram()
Closes a datagram and frees resources.
NULL-safe.
- Parameters
-
| dgram | The datagram to close. May be NULL. |
◆ bj_close_tcp_listener()
Closes a TCP listener and releases associated resources.
Safe to call with NULL.
- Parameters
-
| listener | The TCP listener to close. May be NULL. |
◆ bj_close_tcp_stream()
Closes a TCP stream and releases associated resources.
Safe to call with NULL.
- Parameters
-
| stream | The TCP stream to close. May be NULL. |
◆ bj_close_udp()
| void bj_close_udp |
( |
struct bj_udp * | udp | ) |
|
Closes a UDP socket.
NULL-safe.
- Parameters
-
| udp | The UDP socket to close. May be NULL. |
◆ bj_connect_tcp()
| struct bj_tcp_stream * bj_connect_tcp |
( |
const char * | host, |
|
|
uint16_t | port, |
|
|
uint16_t | hint, |
|
|
uint32_t | ms_timeout, |
|
|
struct bj_error ** | error ) |
Connects to a TCP server at the specified host and port.
Resolves the hostname and attempts to connect. If multiple addresses are returned by DNS, each is tried until one succeeds.
- Parameters
-
| host | Hostname or IP address to connect to. |
| port | Port number to connect to. |
| hint | Address family hint. Use 0 for any, BJ_RESOLVE_IPV4 for IPv4 only, or BJ_RESOLVE_IPV6 for IPv6 only. |
| ms_timeout | Attempt timeout in milliseconds (0 = infinite) |
| error | Optional error output. May be NULL. |
- Returns
- A new TCP stream, or NULL on failure.
- See also
- bj_tcp_send To send data.
-
bj_tcp_recv To receive data.
-
bj_close_tcp_stream To close the connection.
◆ bj_connect_tcp_to()
Connects to a pre-resolved address, trying all resolved IPs.
Automatically iterates through all addresses returned by DNS resolution until one succeeds.
- Parameters
-
| addr | Destination address (from bj_resolve_address). |
| hints | Socket hints (BJ_TCP_DELAY_ACK, etc.). |
| ms_timeout | Connection timeout per address in milliseconds. |
| error | Optional error output. |
- Returns
- TCP stream, or NULL on failure.
- See also
- bj_resolve_address To create the address.
-
bj_connect_tcp For hostname-based connection.
◆ bj_datagram_addr()
| size_t bj_datagram_addr |
( |
const struct bj_datagram * | dgram, |
|
|
char * | buf, |
|
|
size_t | size ) |
Gets the sender's address from a datagram.
- Parameters
-
| dgram | Datagram. |
| buf | Output buffer (may be NULL to query length). |
| size | Buffer size. |
- Returns
- Address string length, or 0 on error.
◆ bj_datagram_data()
| const void * bj_datagram_data |
( |
const struct bj_datagram * | dgram | ) |
|
Gets the data pointer from a datagram.
- Parameters
-
- Returns
- Pointer to data, or NULL on error.
◆ bj_datagram_len()
| size_t bj_datagram_len |
( |
const struct bj_datagram * | dgram | ) |
|
Gets the data length from a datagram.
- Parameters
-
- Returns
- Data length, or 0 on error.
◆ bj_datagram_port()
| uint16_t bj_datagram_port |
( |
const struct bj_datagram * | dgram | ) |
|
Gets the sender's port from a datagram.
- Parameters
-
- Returns
- Sender port, or 0 on error.
◆ bj_free_address()
Frees a resolved address.
NULL-safe.
- Parameters
-
| addr | The address to free. May be NULL. |
◆ bj_get_tcp_keepalive()
Gets the current TCP keep-alive setting.
- Parameters
-
- Returns
- BJ_TRUE if keep-alive is enabled, BJ_FALSE otherwise.
◆ bj_get_tcp_recv_timeout()
| uint32_t bj_get_tcp_recv_timeout |
( |
struct bj_tcp_stream * | stream | ) |
|
Gets the current receive timeout.
- Parameters
-
- Returns
- Timeout in milliseconds, or 0 if no timeout set.
◆ bj_get_tcp_send_timeout()
| uint32_t bj_get_tcp_send_timeout |
( |
struct bj_tcp_stream * | stream | ) |
|
Gets the current send timeout.
- Parameters
-
- Returns
- Timeout in milliseconds, or 0 if no timeout set.
◆ bj_listen_tcp()
| struct bj_tcp_listener * bj_listen_tcp |
( |
const char * | host, |
|
|
uint16_t | port, |
|
|
uint16_t | hint, |
|
|
int | backlog, |
|
|
struct bj_error ** | error ) |
Creates a TCP listener bound to the specified host and port.
The listener accepts incoming TCP connections via bj_accept_tcp.
- Parameters
-
| host | Hostname or IP address to bind to. Pass NULL to bind to all interfaces. |
| port | Port number to listen on. |
| hint | Address family hint. Use 0 for any, BJ_RESOLVE_IPV4 for IPv4 only, or BJ_RESOLVE_IPV6 for IPv6 only. |
| backlog | Maximum pending connections queue size. Use BJ_NET_BACKLOG_DEFAULT for a reasonable default, or BJ_NET_BACKLOG_MAX for the system maximum. |
| error | Optional error output. May be NULL. |
- Returns
- A new TCP listener, or NULL on failure.
- See also
- bj_accept_tcp To accept incoming connections.
-
bj_close_tcp_listener To close the listener.
◆ bj_open_udp()
| struct bj_udp * bj_open_udp |
( |
const char * | host, |
|
|
uint16_t | port, |
|
|
uint16_t | hints, |
|
|
struct bj_error ** | error ) |
Opens a UDP socket bound to the specified port.
- Parameters
-
| host | Hostname/IP to bind to. NULL for any interface. |
| port | Port to bind. 0 for ephemeral (system-assigned). |
| hints | Flags: BJ_RESOLVE_IPV4, BJ_RESOLVE_IPV6, BJ_NONBLOCKING, BJ_BROADCAST |
| error | Optional error output. |
- Returns
- New UDP socket, or NULL on failure.
◆ bj_resolve_address()
| struct bj_address * bj_resolve_address |
( |
const char * | host, |
|
|
uint16_t | port, |
|
|
uint16_t | hints, |
|
|
struct bj_error ** | error ) |
Resolves a hostname to a network address.
The returned address can be used with bj_connect_tcp_to and bj_udp_send_to to avoid repeated DNS lookups.
- Parameters
-
| host | Hostname or IP string to resolve. |
| port | Port number. |
| hints | BJ_RESOLVE_IPV4, BJ_RESOLVE_IPV6, or 0 for any. |
| error | Optional error output. |
- Returns
- Resolved address, or NULL on failure.
- See also
- bj_free_address To free the address.
-
bj_address_next To iterate through multiple resolved IPs.
◆ bj_set_tcp_keepalive()
Enables or disables TCP keep-alive probes.
When enabled, the OS sends periodic probes on idle connections to detect dead peers. If no response is received, the connection is terminated.
- Parameters
-
| stream | TCP stream. |
| enable | BJ_TRUE to enable, BJ_FALSE to disable. |
- Returns
- BJ_TRUE on success.
- Note
- Keep-alive timing is controlled by the implementation.
◆ bj_set_tcp_recv_timeout()
Sets the receive timeout for a TCP stream.
When set, recv operations will timeout after the specified duration.
- Parameters
-
| stream | TCP stream. |
| ms_timeout | Timeout in milliseconds. 0 = no timeout (blocking). |
- Returns
- BJ_TRUE on success.
◆ bj_set_tcp_send_timeout()
Sets the send timeout for a TCP stream.
When set, send operations will timeout after the specified duration.
- Parameters
-
| stream | TCP stream. |
| ms_timeout | Timeout in milliseconds. 0 = no timeout (blocking). |
- Returns
- BJ_TRUE on success.
◆ bj_tcp_available()
Checks bytes available to read without blocking.
- Parameters
-
- Returns
- Bytes available (>=0), or -1 on error/closed connection.
◆ bj_tcp_listener_port()
Get the listening port of the given server.
- Parameters
-
| listener | The listener to get the local port from. |
- Returns
- Listener port number
Will return 0 if listener is 0 or closed.
◆ bj_tcp_local_address()
| size_t bj_tcp_local_address |
( |
const struct bj_tcp_stream * | stream, |
|
|
char * | buf, |
|
|
size_t | size ) |
Gets the local address of a TCP stream.
Writes the local IP address as a string into buf. Works like snprintf: returns the length of the address string regardless of buffer size.
- Parameters
-
| stream | The TCP stream to query. |
| buf | Buffer to store the address string. May be NULL to query length only. |
| size | Size of the buffer in bytes. |
- Returns
- Length of the address string (excluding null terminator), or 0 on error. If the return value is >= size, the output was truncated.
- See also
- BJ_ADDR_STRLEN For the recommended buffer size.
-
bj_tcp_peer_address To get the remote address.
◆ bj_tcp_peek()
| int bj_tcp_peek |
( |
struct bj_tcp_stream * | stream, |
|
|
void * | buf, |
|
|
size_t | len ) |
Peek at incoming data without consuming it.
Like bj_tcp_recv but data remains in the buffer for subsequent reads. Useful for protocol parsing where you need to inspect headers.
- Parameters
-
| stream | The TCP stream to peek from. |
| buf | Buffer to store peeked data. |
| len | Maximum number of bytes to peek. |
- Returns
- Number of bytes peeked, 0 if connection closed, or -1 on error.
- See also
- bj_tcp_recv
◆ bj_tcp_peer_address()
| size_t bj_tcp_peer_address |
( |
const struct bj_tcp_stream * | stream, |
|
|
char * | buf, |
|
|
size_t | size ) |
Gets the remote address of a TCP stream.
Writes the peer's IP address as a string into buf. Works like snprintf: returns the length of the address string regardless of buffer size.
- Parameters
-
| stream | The TCP stream to query. |
| buf | Buffer to store the address string. May be NULL to query length only. |
| size | Size of the buffer in bytes. |
- Returns
- Length of the address string (excluding null terminator), or 0 on error. If the return value is >= size, the output was truncated.
- See also
- BJ_ADDR_STRLEN For the recommended buffer size.
-
bj_tcp_local_address To get the local address.
◆ bj_tcp_recv()
| int bj_tcp_recv |
( |
struct bj_tcp_stream * | stream, |
|
|
void * | buf, |
|
|
size_t | len ) |
Receives data from a TCP stream.
Reads up to len bytes from the stream into buf. Blocks until data is available or the connection is closed.
- Parameters
-
| stream | The TCP stream to receive from. |
| buf | Buffer to store received data. |
| len | Maximum number of bytes to receive. |
- Returns
- Number of bytes received, 0 if connection closed, or -1 on error.
- See also
- bj_tcp_peek
◆ bj_tcp_send()
| int bj_tcp_send |
( |
struct bj_tcp_stream * | stream, |
|
|
const void * | buf, |
|
|
size_t | len ) |
Sends data over a TCP stream.
Sends up to len bytes from buf over the stream.
- Parameters
-
| stream | The TCP stream to send on. |
| buf | Buffer containing data to send. |
| len | Number of bytes to send. |
- Returns
- Number of bytes sent, or -1 on error.
◆ bj_tcp_shutdown()
Shuts down part or all of a TCP connection.
Gracefully shuts down the send and/or receive side of the connection without closing the socket.
- Parameters
-
- See also
- bj_close_tcp_stream To fully close the connection.
◆ bj_tcp_simulate_loss()
| void bj_tcp_simulate_loss |
( |
struct bj_tcp_stream * | stream, |
|
|
int | percent ) |
Simulates packet loss on a TCP stream.
- Parameters
-
| stream | TCP stream. |
| percent | Loss percentage (0-100). 0 disables simulation. |
◆ bj_tcp_wait()
Waits for data to become available on a TCP stream.
Uses select() internally to wait for readability with a timeout.
- Parameters
-
| stream | TCP stream to wait on. |
| ms_timeout | Timeout in milliseconds. 0 = non-blocking poll. |
- Returns
- BJ_TRUE if data available, BJ_FALSE on timeout.
◆ bj_udp_available()
Checks if a datagram is available without blocking.
- Parameters
-
- Returns
- BJ_TRUE if data available, BJ_FALSE otherwise.
◆ bj_udp_local_address()
| size_t bj_udp_local_address |
( |
const struct bj_udp * | udp, |
|
|
char * | buf, |
|
|
size_t | size ) |
Gets the local address of a UDP socket (snprintf-style).
- Parameters
-
| udp | UDP socket. |
| buf | Output buffer (may be NULL to query length). |
| size | Buffer size. |
- Returns
- Address string length, or 0 on error.
◆ bj_udp_local_port()
| uint16_t bj_udp_local_port |
( |
const struct bj_udp * | udp | ) |
|
Gets the local port of a UDP socket.
- Parameters
-
- Returns
- Local port, or 0 on error.
◆ bj_udp_recv()
Receives a datagram from the UDP socket.
If BJ_NONBLOCKING was set and no data available, returns NULL without error. If blocking, waits until a datagram arrives.
- Parameters
-
| udp | UDP socket. |
| error | Optional error output. |
- Returns
- Received datagram (caller must close), or NULL if no data/error. Check error to distinguish: NULL + no error = no data available.
◆ bj_udp_send()
| int bj_udp_send |
( |
struct bj_udp * | udp, |
|
|
const char * | host, |
|
|
uint16_t | port, |
|
|
const void * | data, |
|
|
size_t | len, |
|
|
struct bj_error ** | error ) |
Sends a datagram to the specified destination.
Resolves hostname internally on each call.
- Parameters
-
| udp | UDP socket. |
| host | Destination hostname or IP. |
| port | Destination port. |
| data | Data to send. |
| len | Data length. |
| error | Optional error output. |
- Returns
- Bytes sent, or -1 on error.
◆ bj_udp_send_to()
| int bj_udp_send_to |
( |
struct bj_udp * | udp, |
|
|
const struct bj_address * | addr, |
|
|
const void * | data, |
|
|
size_t | len, |
|
|
struct bj_error ** | error ) |
Sends a datagram to a pre-resolved address (no DNS lookup).
More efficient than bj_udp_send when sending many packets to the same destination, as it avoids repeated DNS lookups.
- Parameters
-
| udp | UDP socket. |
| addr | Destination address (from bj_resolve_address). |
| data | Data to send. |
| len | Data length. |
| error | Optional error output. |
- Returns
- Bytes sent, or -1 on error.
- See also
- bj_resolve_address To create the address.
-
bj_udp_send For hostname-based sending.
◆ bj_udp_simulate_loss()
| void bj_udp_simulate_loss |
( |
struct bj_udp * | udp, |
|
|
int | percent ) |
Simulates packet loss on a UDP socket.
- Parameters
-
| udp | UDP socket. |
| percent | Loss percentage (0-100). 0 disables simulation. |
◆ bj_udp_wait()
Waits for data to become available on a UDP socket.
Uses select() internally to wait for readability with a timeout.
- Parameters
-
| udp | UDP socket. |
| ms_timeout | Timeout in milliseconds. 0 = non-blocking poll. |
- Returns
- BJ_TRUE if data available, BJ_FALSE on timeout.