Socket commmunication. More...
Data Structures | |
struct | vtm_socket_tls_opts |
Macros | |
#define | VTM_SOCK_STAT_DEFAULT 0 |
#define | VTM_SOCK_STAT_ERR (1 << 0) |
#define | VTM_SOCK_STAT_CLOSED (1 << 1) |
#define | VTM_SOCK_STAT_READ_AGAIN (1 << 2) |
#define | VTM_SOCK_STAT_WRITE_AGAIN (1 << 3) |
#define | VTM_SOCK_STAT_READ_LOCKED (1 << 4) |
#define | VTM_SOCK_STAT_WRITE_LOCKED (1 << 5) |
#define | VTM_SOCK_STAT_FREE_WANTED (1 << 6) |
#define | VTM_SOCK_STAT_FREE_ON_UNREF (1 << 7) |
#define | VTM_SOCK_STAT_NONBLOCKING (1 << 8) |
#define | VTM_SOCK_STAT_NBL_READ (1 << 9) |
#define | VTM_SOCK_STAT_NBL_WRITE (1 << 10) |
#define | VTM_SOCK_STAT_NBL_AUTO (1 << 11) |
#define | VTM_SOCK_SHUT_RD 1 |
#define | VTM_SOCK_SHUT_WR 2 |
#define | VTM_SOCK_OPT_NONBLOCKING 1 |
#define | VTM_SOCK_OPT_TCP_NODELAY 2 |
#define | VTM_SOCK_OPT_RECV_TIMEOUT 3 |
#define | VTM_SOCK_OPT_SEND_TIMEOUT 4 |
#define | VTM_SOCKET_TLS_DEFAULT_CIPHERS |
Typedefs | |
typedef struct vtm_socket | vtm_socket |
Functions | |
VTM_API vtm_socket * | vtm_socket_new (enum vtm_socket_family fam, int type) |
VTM_API vtm_socket * | vtm_socket_tls_new (enum vtm_socket_family fam, struct vtm_socket_tls_opts *opts) |
VTM_API void | vtm_socket_free (vtm_socket *sock) |
VTM_API int | vtm_socket_make_threadsafe (vtm_socket *sock) |
VTM_API void | vtm_socket_set_usr_data (vtm_socket *sock, void *data) |
VTM_API void * | vtm_socket_get_usr_data (vtm_socket *sock) |
VTM_API enum vtm_socket_family | vtm_socket_get_family (vtm_socket *sock) |
VTM_API int | vtm_socket_get_type (vtm_socket *sock) |
VTM_API int | vtm_socket_bind (vtm_socket *sock, const char *addr, unsigned int port) |
VTM_API int | vtm_socket_listen (vtm_socket *sock, unsigned int backlog) |
VTM_API int | vtm_socket_accept (vtm_socket *sock, vtm_socket **client) |
VTM_API int | vtm_socket_connect (vtm_socket *sock, const char *host, unsigned int port) |
VTM_API int | vtm_socket_shutdown (vtm_socket *sock, int dir) |
VTM_API int | vtm_socket_close (vtm_socket *sock) |
VTM_API int | vtm_socket_write (vtm_socket *sock, const void *src, size_t len, size_t *out_written) |
VTM_API int | vtm_socket_read (vtm_socket *sock, void *buf, size_t len, size_t *out_read) |
VTM_API int | vtm_socket_dgram_recv (vtm_socket *sock, void *buf, size_t maxlen, size_t *out_recv, struct vtm_socket_saddr *saddr) |
VTM_API int | vtm_socket_dgram_send (vtm_socket *sock, const void *buf, size_t len, size_t *out_send, const struct vtm_socket_saddr *saddr) |
VTM_API void | vtm_socket_set_state (vtm_socket *sock, unsigned int flags) |
VTM_API void | vtm_socket_unset_state (vtm_socket *sock, unsigned int flags) |
VTM_API unsigned int | vtm_socket_get_state (vtm_socket *sock) |
VTM_API int | vtm_socket_set_opt (vtm_socket *sock, int opt, const void *val, size_t len) |
VTM_API int | vtm_socket_get_opt (vtm_socket *sock, int opt, void *val, size_t len) |
VTM_API int | vtm_socket_get_remote_addr (vtm_socket *sock, struct vtm_socket_saddr *addr) |
#define VTM_SOCK_STAT_NBL_READ (1 << 9) |
Non-blocking read
#define VTM_SOCK_STAT_NBL_WRITE (1 << 10) |
Non-blocking write
#define VTM_SOCK_STAT_NBL_AUTO (1 << 11) |
Non-blocking read or write, automatically switched
#define VTM_SOCK_SHUT_RD 1 |
Shutdown read-side
#define VTM_SOCK_SHUT_WR 2 |
Shutdown write-side
#define VTM_SOCK_OPT_NONBLOCKING 1 |
expects bool
#define VTM_SOCK_OPT_TCP_NODELAY 2 |
expects bool
#define VTM_SOCK_OPT_RECV_TIMEOUT 3 |
expects unsigned long, values is milliseconds
#define VTM_SOCK_OPT_SEND_TIMEOUT 4 |
expects unsigned long, values is milliseconds
#define VTM_SOCKET_TLS_DEFAULT_CIPHERS |
VTM_API vtm_socket* vtm_socket_new | ( | enum vtm_socket_family | fam, |
int | type | ||
) |
Creates a new plain (non-encrypted) socket.
fam | the socket family |
int | the type of socket |
VTM_API vtm_socket* vtm_socket_tls_new | ( | enum vtm_socket_family | fam, |
struct vtm_socket_tls_opts * | opts | ||
) |
Creates a new stream-based TLS socket.
fam | the socket family |
opts | TLS options |
VTM_API void vtm_socket_free | ( | vtm_socket * | sock | ) |
Releases the socket and all allocated resources.
After this call the socket pointer is no longer valid.
sock | the socket that should be released |
VTM_API int vtm_socket_make_threadsafe | ( | vtm_socket * | sock | ) |
Makes the socket threadsafe.
sock | the socket that should be made threadsafe |
VTM_API void vtm_socket_set_usr_data | ( | vtm_socket * | sock, |
void * | data | ||
) |
Stores arbitrary user data with the socket.
sock | the socket that should store the user data |
data | the data that should be stored |
VTM_API void* vtm_socket_get_usr_data | ( | vtm_socket * | sock | ) |
Retrieves the stored user data.
sock | the socket |
VTM_API enum vtm_socket_family vtm_socket_get_family | ( | vtm_socket * | sock | ) |
Gets the socket family of the given socket.
sock | the socket |
VTM_API int vtm_socket_get_type | ( | vtm_socket * | sock | ) |
Gets the type of the given socket.
sock | the socket |
VTM_API int vtm_socket_bind | ( | vtm_socket * | sock, |
const char * | addr, | ||
unsigned int | port | ||
) |
Binds the socket the given address and port.
addr | the address |
port | the port, must be in range 0-65535 |
VTM_API int vtm_socket_listen | ( | vtm_socket * | sock, |
unsigned int | backlog | ||
) |
Starts listening on the given socket.
Marks the socket as passive, so that incoming connections can be accepted with vtm_socket_accept().
sock | the socket that should start listening |
backlog | maximum number of pending connections that are not accepted yet |
VTM_API int vtm_socket_accept | ( | vtm_socket * | sock, |
vtm_socket ** | client | ||
) |
Accepts a new incoming connection.
If the listening socket is not in non-blocking mode, this call blocks until a new connection was accepted.
sock | the listening socket | |
[out] | client | pointer to connected socket which represents the accepted client connection |
VTM_API int vtm_socket_connect | ( | vtm_socket * | sock, |
const char * | host, | ||
unsigned int | port | ||
) |
Connects the socket to given address and port.
host | either a hostname or an ip address |
port | the port number |
VTM_API int vtm_socket_shutdown | ( | vtm_socket * | sock, |
int | dir | ||
) |
Shuts down the reading or writing side of a socket.
sock | the socket that should shutdown |
dir | the direction or a combination of multiple directions that should be shut down |
VTM_API int vtm_socket_close | ( | vtm_socket * | sock | ) |
Closes a socket.
sock | the socket that should be closed |
VTM_API int vtm_socket_write | ( | vtm_socket * | sock, |
const void * | src, | ||
size_t | len, | ||
size_t * | out_written | ||
) |
Writes data to the socket.
If the socket is not in non-blocking mode, this call blocks until all data is written.
sock | the socket where the data should be written to | |
src | pointer to the data that should be written | |
len | length of data in bytes | |
[out] | out_written | number of bytes that were successfully written |
VTM_API int vtm_socket_read | ( | vtm_socket * | sock, |
void * | buf, | ||
size_t | len, | ||
size_t * | out_read | ||
) |
Reads data from the socket.
If the socket is not in non-blocking mode, this call blocks until some data is available to read.
sock | the socket where the data should be read from | |
[out] | buf | the buffer where the data will be stored |
len | the size of the buffer in bytes | |
[out] | out_read | the number of bytes that were written to the buffer |
VTM_API int vtm_socket_dgram_recv | ( | vtm_socket * | sock, |
void * | buf, | ||
size_t | maxlen, | ||
size_t * | out_recv, | ||
struct vtm_socket_saddr * | saddr | ||
) |
Receive a datagram.
If the socket is not in non-blocking mode, this call blocks until a datagram is available to read.
sock | the socket where the datagram should be read from | |
[out] | buf | the buffer where the data will be stored |
maxlen | size of the buffer in bytes | |
[out] | out_recv | number of bytes that were written to the buffer |
saddr | the source address of the datagram |
VTM_API int vtm_socket_dgram_send | ( | vtm_socket * | sock, |
const void * | buf, | ||
size_t | len, | ||
size_t * | out_send, | ||
const struct vtm_socket_saddr * | saddr | ||
) |
Send a datagram.
sock | the socket that should send the datagram | |
buf | the buffer with the payload | |
len | length of payload in bytes | |
[out] | out_send | number of bytes that were sent |
saddr | the destination address of the datagram |
VTM_API void vtm_socket_set_state | ( | vtm_socket * | sock, |
unsigned int | flags | ||
) |
Adds one or a combination of state flags.
sock | the socket where the state flags should be set |
flags | one or more OR-combined flags |
VTM_API void vtm_socket_unset_state | ( | vtm_socket * | sock, |
unsigned int | flags | ||
) |
Removes one or a combination of state flags.
sock | the socket where the state flags should be removed |
flags | one or more OR-combined flags |
VTM_API unsigned int vtm_socket_get_state | ( | vtm_socket * | sock | ) |
Retrieves the current socket state.
sock | the socket |
VTM_API int vtm_socket_set_opt | ( | vtm_socket * | sock, |
int | opt, | ||
const void * | val, | ||
size_t | len | ||
) |
Set socket option.
sock | the socket where the option should be set |
opt | the option that should be set |
val | the value that should be set |
len | length of the value in bytes |
VTM_API int vtm_socket_get_opt | ( | vtm_socket * | sock, |
int | opt, | ||
void * | val, | ||
size_t | len | ||
) |
Retrieve current value of a socket option.
sock | the socket where the option should be retrieved | |
opt | the option that should be read | |
[out] | val | the current value of the option |
len | length of value in bytes |
VTM_API int vtm_socket_get_remote_addr | ( | vtm_socket * | sock, |
struct vtm_socket_saddr * | addr | ||
) |
Reads the remote peer address of a connected socket.
sock | a connected socket | |
[out] | addr | the buffer where the address is stored |