bio
 
Loading...
Searching...
No Matches
Network I/O

Read/write over the network. More...

Data Structures

struct  bio_socket_t
 Handle to a socket. More...
 
struct  bio_addr_t
 Net address. More...
 

Typedefs

typedef uint16_t bio_port_t
 Port number in host byte order.
 

Enumerations

enum  bio_addr_type_t { BIO_ADDR_IPV4 , BIO_ADDR_IPV6 , BIO_ADDR_NAMED }
 Address type. More...
 
enum  bio_socket_type_t { BIO_SOCKET_STREAM , BIO_SOCKET_DATAGRAM }
 Socket type. More...
 

Functions

bool bio_net_wrap (bio_socket_t *sock, uintptr_t handle, bio_addr_type_t addr_type, bio_error_t *error)
 Wrap a socket handle from the OS into a bio_socket_t.
 
uintptr_t bio_net_unwrap (bio_socket_t socket)
 Retrieve an OS handle from a bio_socket_t.
 
bool bio_net_listen (bio_socket_type_t socket_type, const bio_addr_t *addr, bio_port_t port, bio_socket_t *sock, bio_error_t *error)
 Create a listening socket.
 
bool bio_net_accept (bio_socket_t socket, bio_socket_t *client, bio_error_t *error)
 Accept a new connection.
 
bool bio_net_connect (bio_socket_type_t socket_type, const bio_addr_t *addr, bio_port_t port, bio_socket_t *socket, bio_error_t *error)
 Make an outgoing connection.
 
bool bio_net_close (bio_socket_t socket, bio_error_t *error)
 Close a socket.
 
size_t bio_net_send (bio_socket_t socket, const void *buf, size_t size, bio_error_t *error)
 Send to a socket.
 
size_t bio_net_recv (bio_socket_t socket, void *buf, size_t size, bio_error_t *error)
 Receive from a socket.
 
size_t bio_net_sendto (bio_socket_t socket, const bio_addr_t *addr, const void *buf, size_t size, bio_error_t *error)
 Not implemented.
 
size_t bio_net_recvfrom (bio_socket_t socket, bio_addr_t *addr, void *buf, size_t size, bio_error_t *error)
 Not implemented.
 
int bio_net_address_compare (const bio_addr_t *lhs, const bio_addr_t *rhs)
 Compare two addresses.
 
static size_t bio_net_send_exactly (bio_socket_t socket, const void *buf, size_t size, bio_error_t *error)
 Helper to deal with short write.
 
static size_t bio_net_recv_exactly (bio_socket_t socket, void *buf, size_t size, bio_error_t *error)
 Helper to deal with short read.
 

Variables

static const bio_port_t BIO_PORT_ANY = 0
 Any port.
 
static const bio_addr_t BIO_ADDR_IPV4_ANY
 Any IPv4 address.
 
static const bio_addr_t BIO_ADDR_IPV4_LOOPBACK
 Local loopback IPv4 address.
 
static const bio_addr_t BIO_ADDR_IPV6_ANY
 Any IPv6 address.
 
static const bio_addr_t BIO_ADDR_IPV6_LOOPBACK
 Local loopback IPv6 address.
 

Detailed Description

Read/write over the network.

Typedef Documentation

◆ bio_port_t

typedef uint16_t bio_port_t

Port number in host byte order.

Enumeration Type Documentation

◆ bio_addr_type_t

Address type.

Enumerator
BIO_ADDR_IPV4 

IPv4.

BIO_ADDR_IPV6 

IPv6.

BIO_ADDR_NAMED 

On Unix platforms, this will be a Unix domain socket.

On Windows, this will be a named pipe.

◆ bio_socket_type_t

Socket type.

Enumerator
BIO_SOCKET_STREAM 

Stream socket.

BIO_SOCKET_DATAGRAM 

Datagram socket.

Function Documentation

◆ bio_net_accept()

bool bio_net_accept ( bio_socket_t  socket,
bio_socket_t client,
bio_error_t error 
)

Accept a new connection.

Parameters
socketA socket handle
clientPointer to a socket handle to receive the new connection
errorSeee Error handling
Returns
Whether the operation was successful.

◆ bio_net_address_compare()

int bio_net_address_compare ( const bio_addr_t lhs,
const bio_addr_t rhs 
)

Compare two addresses.

◆ bio_net_close()

bool bio_net_close ( bio_socket_t  socket,
bio_error_t error 
)

Close a socket.

◆ bio_net_connect()

bool bio_net_connect ( bio_socket_type_t  socket_type,
const bio_addr_t addr,
bio_port_t  port,
bio_socket_t socket,
bio_error_t error 
)

Make an outgoing connection.

Parameters
socket_typeThe socket type
addrThe remote address
addrThe port number for IP socket. Ignored for "named" socket.
sockPointer to a socket handle. This is only assigned if the operation is successful.
errorSeee Error handling
Returns
Whether the operation was successful.

◆ bio_net_listen()

bool bio_net_listen ( bio_socket_type_t  socket_type,
const bio_addr_t addr,
bio_port_t  port,
bio_socket_t sock,
bio_error_t error 
)

Create a listening socket.

Parameters
socket_typeThe socket type
addrThe socket address to listen from
addrThe port number for IP socket. Ignored for "named" socket.
sockPointer to a socket handle. This is only assigned if the operation is successful.
errorSeee Error handling
Returns
Whether the operation was successful.
See also
bio_net_accept

◆ bio_net_recv()

size_t bio_net_recv ( bio_socket_t  socket,
void *  buf,
size_t  size,
bio_error_t error 
)

Receive from a socket.

Remarks
Short read is possible
See also
bio_net_recv_exactly

◆ bio_net_recv_exactly()

static size_t bio_net_recv_exactly ( bio_socket_t  socket,
void *  buf,
size_t  size,
bio_error_t error 
)
inlinestatic

Helper to deal with short read.

◆ bio_net_recvfrom()

size_t bio_net_recvfrom ( bio_socket_t  socket,
bio_addr_t addr,
void *  buf,
size_t  size,
bio_error_t error 
)

Not implemented.

◆ bio_net_send()

size_t bio_net_send ( bio_socket_t  socket,
const void *  buf,
size_t  size,
bio_error_t error 
)

Send to a socket.

Remarks
Short write is possible
See also
bio_net_send_exactly

◆ bio_net_send_exactly()

static size_t bio_net_send_exactly ( bio_socket_t  socket,
const void *  buf,
size_t  size,
bio_error_t error 
)
inlinestatic

Helper to deal with short write.

◆ bio_net_sendto()

size_t bio_net_sendto ( bio_socket_t  socket,
const bio_addr_t addr,
const void *  buf,
size_t  size,
bio_error_t error 
)

Not implemented.

◆ bio_net_unwrap()

uintptr_t bio_net_unwrap ( bio_socket_t  socket)

Retrieve an OS handle from a bio_socket_t.

This can be used to call platform-specific API such as getsockopt or WSAIoctl

For Unix platforms, this is will return a file descriptor. A value of -1 is possible if the socket was closed with bio_net_close.

For Windows, this will return a SOCKET/HANDLE. A value of INVALID_HANDLE_VALUE/INVALID_SOCKET may be returned.

Remarks
It is important not to interfere with bio when manipulating the OS handle directly. Please refer to the implementation details for the given platform.

◆ bio_net_wrap()

bool bio_net_wrap ( bio_socket_t sock,
uintptr_t  handle,
bio_addr_type_t  addr_type,
bio_error_t error 
)

Wrap a socket handle from the OS into a bio_socket_t.

This can be used to handle platform-specific special sockets such as netlink socket on Linux.

Parameters
sockPointer to a socket handle. This will only be assigned when the operation is successful.
handleThe OS socket handle. On Unix platforms, this is a file descriptor. On Windows, this is a SOCKET/HANDLE.
addr_typeThe address type of the socket. This only matters on Windows as it uses two separate sets of APIs: one for named pipe and one for socket.
errorSee Error handling.
Returns
Whether this operation is successful.

Variable Documentation

◆ BIO_ADDR_IPV4_ANY

const bio_addr_t BIO_ADDR_IPV4_ANY
static
Initial value:
= {
.type = BIO_ADDR_IPV4,
.ipv4 = { 0 },
}
@ BIO_ADDR_IPV4
IPv4.
Definition net.h:22

Any IPv4 address.

See also
bio_net_listen

◆ BIO_ADDR_IPV4_LOOPBACK

const bio_addr_t BIO_ADDR_IPV4_LOOPBACK
static
Initial value:
= {
.type = BIO_ADDR_IPV4,
.ipv4 = { 127, 0, 0, 1 },
}

Local loopback IPv4 address.

◆ BIO_ADDR_IPV6_ANY

const bio_addr_t BIO_ADDR_IPV6_ANY
static
Initial value:
= {
.type = BIO_ADDR_IPV6,
.ipv6 = { 0 },
}
@ BIO_ADDR_IPV6
IPv6.
Definition net.h:24

Any IPv6 address.

See also
bio_net_listen

◆ BIO_ADDR_IPV6_LOOPBACK

const bio_addr_t BIO_ADDR_IPV6_LOOPBACK
static
Initial value:
= {
.type = BIO_ADDR_IPV6,
.ipv6 = { [15] = 1 },
}

Local loopback IPv6 address.

◆ BIO_PORT_ANY

const bio_port_t BIO_PORT_ANY = 0
static

Any port.

See also
bio_net_listen