Data Structures | Macros | Typedefs | Functions
socket.h File Reference

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)
 

Macro Definition Documentation

#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
Value:
"ECDH+AESGCM:ECDH+CHACHA20:DH+AESGCM:ECDH+AES256:DH+AES256:" \
"ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS"

Function Documentation

VTM_API vtm_socket* vtm_socket_new ( enum vtm_socket_family  fam,
int  type 
)

Creates a new plain (non-encrypted) socket.

See also
socket_spec.h
Parameters
famthe socket family
intthe type of socket
Returns
a handle to the created socket
NULL if an error occured
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.

Parameters
famthe socket family
optsTLS options
Returns
a handle to the created socket
NULL if an error occured
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.

Parameters
sockthe socket that should be released
VTM_API int vtm_socket_make_threadsafe ( vtm_socket *  sock)

Makes the socket threadsafe.

Parameters
sockthe socket that should be made threadsafe
Returns
VTM_OK if the socket is now threadsafe
VTM_E_MALLOC if the necessary mutex object could not be created
VTM_API void vtm_socket_set_usr_data ( vtm_socket *  sock,
void *  data 
)

Stores arbitrary user data with the socket.

Parameters
sockthe socket that should store the user data
datathe data that should be stored
VTM_API void* vtm_socket_get_usr_data ( vtm_socket *  sock)

Retrieves the stored user data.

Parameters
sockthe socket
Returns
the previous saved user data
NULL if no data was stored
VTM_API enum vtm_socket_family vtm_socket_get_family ( vtm_socket *  sock)

Gets the socket family of the given socket.

Parameters
sockthe socket
Returns
the socket family of the socket
VTM_API int vtm_socket_get_type ( vtm_socket *  sock)

Gets the type of the given socket.

Parameters
sockthe socket
Returns
the type of 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.

Parameters
addrthe address
portthe port, must be in range 0-65535
Returns
VTM_OK if the bind operation was successfull
VTM_E_IO_UNKNOWN or VTM_ERROR if an error occured
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().

Parameters
sockthe socket that should start listening
backlogmaximum number of pending connections that are not accepted yet
Returns
VTM_OK if the call succeeded
VTM_E_IO_UNKNOWN or VTM_ERROR if an error occured
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.

Parameters
sockthe listening socket
[out]clientpointer to connected socket which represents the accepted client connection
Returns
VTM_OK if the call succeeded and a new client connection was accepted
VTM_E_IO_AGAIN if the socket is is in non-blocking mode and there was no pending incoming connection.
VTM_E_IO_UNKNOWN or VTM_ERROR if an error occured
VTM_API int vtm_socket_connect ( vtm_socket *  sock,
const char *  host,
unsigned int  port 
)

Connects the socket to given address and port.

Parameters
hosteither a hostname or an ip address
portthe port number
Returns
VTM_OK if the call succeeded and the socket is now connected
VTM_E_NOT_SUPPORTED if the operation is not supported, for example you cannot connect a datagram based socket
VTM_E_IO_UNKNOWN or VTM_ERROR if an error occured
VTM_API int vtm_socket_shutdown ( vtm_socket *  sock,
int  dir 
)

Shuts down the reading or writing side of a socket.

Parameters
sockthe socket that should shutdown
dirthe direction or a combination of multiple directions that should be shut down
Returns
VTM_OK if the call succeeded
VTM_E_IO_UNKNOWN or VTM_ERROR if an error occured
VTM_API int vtm_socket_close ( vtm_socket *  sock)

Closes a socket.

Parameters
sockthe socket that should be closed
Returns
VTM_OK if the call succeeded
VTM_E_IO_UNKNOWN or VTM_ERROR if an error occured
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.

Parameters
sockthe socket where the data should be written to
srcpointer to the data that should be written
lenlength of data in bytes
[out]out_writtennumber of bytes that were successfully written
Returns
VTM_OK if the call succeeded
VTM_E_IO_AGAIN if not all data could be written at once
VRM_E_IO_CLOSED if the connection was closed
VTM_E_IO_UNKNOWN or VTM_ERROR if an error occured
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.

Parameters
sockthe socket where the data should be read from
[out]bufthe buffer where the data will be stored
lenthe size of the buffer in bytes
[out]out_readthe number of bytes that were written to the buffer
Returns
VTM_OK if the call succeeded
VTM_E_IO_AGAIN if there was no data available
VRM_E_IO_CLOSED if the connection was closed
VTM_E_IO_UNKNOWN or VTM_ERROR if an error occured
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.

Parameters
sockthe socket where the datagram should be read from
[out]bufthe buffer where the data will be stored
maxlensize of the buffer in bytes
[out]out_recvnumber of bytes that were written to the buffer
saddrthe source address of the datagram
Returns
VTM_OK if the call succeeded
VTM_E_IO_AGAIN if there was no data available
VTM_E_IO_UNKNOWN or VTM_ERROR if an error occured
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.

Parameters
sockthe socket that should send the datagram
bufthe buffer with the payload
lenlength of payload in bytes
[out]out_sendnumber of bytes that were sent
saddrthe destination address of the datagram
Returns
VTM_OK if the call succeeded
VTM_E_IO_UNKNOWN or VTM_ERROR if an error occured
VTM_API void vtm_socket_set_state ( vtm_socket *  sock,
unsigned int  flags 
)

Adds one or a combination of state flags.

Parameters
sockthe socket where the state flags should be set
flagsone 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.

Parameters
sockthe socket where the state flags should be removed
flagsone or more OR-combined flags
VTM_API unsigned int vtm_socket_get_state ( vtm_socket *  sock)

Retrieves the current socket state.

Parameters
sockthe socket
Returns
the current state
VTM_API int vtm_socket_set_opt ( vtm_socket *  sock,
int  opt,
const void *  val,
size_t  len 
)

Set socket option.

Parameters
sockthe socket where the option should be set
optthe option that should be set
valthe value that should be set
lenlength of the value in bytes
Returns
VTM_OK if the option was successfully set
VTM_E_NOT_SUPPORTED if the option is not supported
VTM_E_IO_UNKNOWN or VTM_ERROR if an error occured
VTM_API int vtm_socket_get_opt ( vtm_socket *  sock,
int  opt,
void *  val,
size_t  len 
)

Retrieve current value of a socket option.

Parameters
sockthe socket where the option should be retrieved
optthe option that should be read
[out]valthe current value of the option
lenlength of value in bytes
Returns
VTM_OK if the option was successfully read
VTM_E_NOT_SUPPORTED if the option is not supported
VTM_E_IO_UNKNOWN or VTM_ERROR if an error occured
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.

Parameters
socka connected socket
[out]addrthe buffer where the address is stored
Returns
VTM_OK if the peer address was sucessfully read
VTM_E_IO_UNKNOWN or VTM_ERROR if an error occured