All API functions except cy_socket_init and cy_socket_deinit are thread-safe.
All API functions are blocking API functions.
Secure Sockets Library creates a worker thread for processing events from the network stack. The priority of the thread is CY_RTOS_PRIORITY_ABOVENORMAL. The macro CY_RTOS_PRIORITY_ABOVENORMAL is defined in abstraction-rtos/include/COMPONENT_FREERTOS/cyabs_rtos_impl.h.
Functions | |
cy_rslt_t | cy_socket_init (void) |
Does general allocation and initialization of resources needed for the library. More... | |
cy_rslt_t | cy_socket_deinit (void) |
Releases the resources allocated in cy_socket_init function. More... | |
cy_rslt_t | cy_socket_create (int domain, int type, int protocol, cy_socket_t *handle) |
Creates a new socket. More... | |
cy_rslt_t | cy_socket_connect (cy_socket_t handle, cy_socket_sockaddr_t *address, uint32_t address_length) |
Connects a TCP/TLS socket to the specified server IP address and port. More... | |
cy_rslt_t | cy_socket_disconnect (cy_socket_t handle, uint32_t timeout) |
Disconnects a TCP/TLS socket's remote connection. More... | |
cy_rslt_t | cy_socket_bind (cy_socket_t handle, cy_socket_sockaddr_t *address, uint32_t address_length) |
Binds the socket to the given socket address. More... | |
cy_rslt_t | cy_socket_listen (cy_socket_t handle, int backlog) |
Listens for TCP/TLS socket connections and limits the queue of incoming connections. More... | |
cy_rslt_t | cy_socket_accept (cy_socket_t handle, cy_socket_sockaddr_t *address, uint32_t *address_length, cy_socket_t *socket) |
Accepts a new TCP/TLS connection on a socket. More... | |
cy_rslt_t | cy_socket_send (cy_socket_t handle, const void *buffer, uint32_t length, int flags, uint32_t *bytes_sent) |
Sends data over a connected TCP/TLS socket. More... | |
cy_rslt_t | cy_socket_sendto (cy_socket_t handle, const void *buffer, uint32_t length, int flags, const cy_socket_sockaddr_t *dest_addr, uint32_t address_length, uint32_t *bytes_sent) |
Sends a UDP datagram over a specified socket. More... | |
cy_rslt_t | cy_socket_recv (cy_socket_t handle, void *buffer, uint32_t length, int flags, uint32_t *bytes_received) |
Receives the data from a connected TCP/TLS socket. More... | |
cy_rslt_t | cy_socket_recvfrom (cy_socket_t handle, void *buffer, uint32_t length, int flags, cy_socket_sockaddr_t *src_addr, uint32_t *src_addr_length, uint32_t *bytes_received) |
Receives a UDP datagram for the specified socket. More... | |
cy_rslt_t | cy_socket_setsockopt (cy_socket_t handle, int level, int optname, const void *optval, uint32_t optlen) |
Sets a particular socket option. More... | |
cy_rslt_t | cy_socket_getsockopt (cy_socket_t handle, int level, int optname, void *optval, uint32_t *optlen) |
Gets the value of a particular socket option. More... | |
cy_rslt_t | cy_socket_gethostbyname (const char *hostname, cy_socket_ip_version_t ip_ver, cy_socket_ip_address_t *addr) |
Resolves a host name using Domain Name Service. More... | |
cy_rslt_t | cy_socket_poll (cy_socket_t handle, uint32_t *rwflags, uint32_t timeout) |
Checks whether data is available on the socket. More... | |
cy_rslt_t | cy_socket_shutdown (cy_socket_t handle, int how) |
Shuts down the send and/or receive operation on the given TCP socket. More... | |
cy_rslt_t | cy_socket_delete (cy_socket_t handle) |
Releases the resources allocated for the socket by the cy_socket_create function. More... | |
cy_rslt_t cy_socket_init | ( | void | ) |
Does general allocation and initialization of resources needed for the library.
This API function must be called before using any other socket API.
cy_rslt_t cy_socket_deinit | ( | void | ) |
Releases the resources allocated in cy_socket_init function.
Prior to calling this API function, all created sockets must be disconnected and deleted.
cy_rslt_t cy_socket_create | ( | int | domain, |
int | type, | ||
int | protocol, | ||
cy_socket_t * | handle | ||
) |
Creates a new socket.
Valid type/protocol combinations are:
[in] | domain | Protocol family to be used by the socket. Refer CY_SOCKET_DOMAIN_AF_INET and CY_SOCKET_DOMAIN_AF_INET6. |
[in] | type | Protocol type to be used by the socket. Refer CY_SOCKET_TYPE_DGRAM and CY_SOCKET_TYPE_STREAM. |
[in] | protocol | Transport protocol to be used by the socket. Refer CY_SOCKET_IPPROTO_TCP, CY_SOCKET_IPPROTO_UDP, and CY_SOCKET_IPPROTO_TLS. |
[out] | handle | Socket handle; contents of this handle are specific to the socket layer implementation. |
cy_rslt_t cy_socket_connect | ( | cy_socket_t | handle, |
cy_socket_sockaddr_t * | address, | ||
uint32_t | address_length | ||
) |
Connects a TCP/TLS socket to the specified server IP address and port.
This API function is a blocking call.
For secure (TLS) sockets, before calling this API function, the following TLS configuration can be set:
[in] | handle | Socket handle returned by the cy_socket_create API function. |
[in] | address | Pointer to the cy_socket_sockaddr_t structure that contains the address to connect the socket to. Refer cy_socket_ip_address_t for IP address endienness. |
[in] | address_length | Length of the cy_socket_sockaddr_t structure. |
cy_rslt_t cy_socket_disconnect | ( | cy_socket_t | handle, |
uint32_t | timeout | ||
) |
Disconnects a TCP/TLS socket's remote connection.
Timeout is not supported by all network stacks. If the underlying network stack does not support the timeout option, this function returns after a clean disconnect. lwIP does not support the timeout option.
[in] | handle | Socket handle returned by either the cy_socket_create API function for client sockets, or by the cy_socket_accept API function for accepted sockets. |
[in] | timeout | Maximum amount of time to wait in milliseconds for a clean disconnect. When the timeout is zero, the function returns after a clean disconnect or when the operation results in an error. |
cy_rslt_t cy_socket_bind | ( | cy_socket_t | handle, |
cy_socket_sockaddr_t * | address, | ||
uint32_t | address_length | ||
) |
Binds the socket to the given socket address.
[in] | handle | Socket handle returned by the cy_socket_create API function. |
[in] | address | Address to be bound to the socket. Refer cy_socket_ip_address_t for IP address endienness. |
[in] | address_length | Length of the cy_socket_sockaddr_t structure. |
cy_rslt_t cy_socket_listen | ( | cy_socket_t | handle, |
int | backlog | ||
) |
Listens for TCP/TLS socket connections and limits the queue of incoming connections.
If the socket has been configured with a connection request callback, the registered callback will be invoked when the new client connection request is received. Invoke the cy_socket_accept API function from the callback function to accept the client connection.
[in] | handle | Socket handle returned by the cy_socket_create API function. |
[in] | backlog | Maximum pending connections allowed. |
cy_rslt_t cy_socket_accept | ( | cy_socket_t | handle, |
cy_socket_sockaddr_t * | address, | ||
uint32_t * | address_length, | ||
cy_socket_t * | socket | ||
) |
Accepts a new TCP/TLS connection on a socket.
This is a blocking API function that returns when there is an incoming connection request from a client.
However, when the CY_SOCKET_SO_RCVTIMEO socket option is set on the listening socket (input socket handle param), this API function returns with a CY_RSLT_MODULE_SECURE_SOCKETS_TIMEOUT error if there is no connection request from a client within the timeout period.
CY_SOCKET_SO_RCVTIMEO can be set using the cy_socket_setsockopt API function.
[in] | handle | Socket handle that has been created with cy_socket_create, bound to a local address with cy_socket_bind, and is listening for connections after a call to cy_socket_listen. This is the server-side socket that is reused to establish connections across clients. |
[out] | address | Address of the peer socket in the cy_socket_sockaddr_t structure. Refer cy_socket_ip_address_t for IP address endienness. |
[out] | address_length | Contains the actual size of the peer socket address. |
[out] | socket | Socket handle for the accepted connection with a client. This is the socket that should be used for further communication over a new client connection. |
cy_rslt_t cy_socket_send | ( | cy_socket_t | handle, |
const void * | buffer, | ||
uint32_t | length, | ||
int | flags, | ||
uint32_t * | bytes_sent | ||
) |
Sends data over a connected TCP/TLS socket.
[in] | handle | Socket handle returned by either the cy_socket_create API function for client sockets, or by the cy_socket_accept API function for accepted sockets. |
[in] | buffer | Buffer containing the data to be sent. |
[in] | length | Length of the data to be sent. |
[in] | flags | Flags to indicate the send options: CY_SOCKET_FLAGS_NONE and CY_SOCKET_FLAGS_MORE |
[out] | bytes_sent | Number of bytes sent. |
cy_rslt_t cy_socket_sendto | ( | cy_socket_t | handle, |
const void * | buffer, | ||
uint32_t | length, | ||
int | flags, | ||
const cy_socket_sockaddr_t * | dest_addr, | ||
uint32_t | address_length, | ||
uint32_t * | bytes_sent | ||
) |
Sends a UDP datagram over a specified socket.
[in] | handle | Socket handle returned by the cy_socket_create API function for UDP sockets. |
[in] | buffer | Buffer containing the data to be sent. |
[in] | length | Length of the data to be sent. |
[in] | flags | Flags to indicate send options. Currently, this argument is not used; this is reserved for the future. |
[in] | dest_addr | Pointer to the cy_socket_sockaddr_t structure that contains the destination address to send the data to. Refer cy_socket_ip_address_t for IP address endienness. |
[in] | address_length | Length of the cy_socket_sockaddr_t structure. |
[out] | bytes_sent | Number of bytes sent. |
cy_rslt_t cy_socket_recv | ( | cy_socket_t | handle, |
void * | buffer, | ||
uint32_t | length, | ||
int | flags, | ||
uint32_t * | bytes_received | ||
) |
Receives the data from a connected TCP/TLS socket.
[in] | handle | Socket handle returned by either the cy_socket_create API function for client sockets, or by cy_socket_accept API function for accepted sockets. |
[out] | buffer | Buffer into which received data will be placed. |
[in] | length | Size of the data buffer. |
[in] | flags | Not currently used. Should be set to CY_SOCKET_FLAGS_NONE. |
[out] | bytes_received | Number of bytes received. |
cy_rslt_t cy_socket_recvfrom | ( | cy_socket_t | handle, |
void * | buffer, | ||
uint32_t | length, | ||
int | flags, | ||
cy_socket_sockaddr_t * | src_addr, | ||
uint32_t * | src_addr_length, | ||
uint32_t * | bytes_received | ||
) |
Receives a UDP datagram for the specified socket.
[in] | handle | Socket handle returned by either the cy_socket_create API function for client sockets, or by cy_socket_accept API function for accepted sockets. |
[out] | buffer | Buffer into which the received data will be placed. |
[in] | length | Size of the data buffer. |
[in] | flags | Flags to control the datagram to be received. Refer CY_SOCKET_FLAGS_RECVFROM_NONE and CY_SOCKET_FLAGS_RECVFROM_SRC_FILTER. |
[in,out] | src_addr | Pointer to the cy_socket_sockaddr_t structure to store the sender's address. If passed as NULL, the sender's address is not returned to the caller. If the CY_SOCKET_FLAGS_RECVFROM_SRC_FILTER flag is set, it contains the source address from which the datagram to be received. Refer cy_socket_ip_address_t for IP address endienness. |
[in] | src_addr_length | Pointer containing source address length. Currently, this argument is not used; this is reserved for the future. |
[out] | bytes_received | Number of bytes received. |
cy_rslt_t cy_socket_setsockopt | ( | cy_socket_t | handle, |
int | level, | ||
int | optname, | ||
const void * | optval, | ||
uint32_t | optlen | ||
) |
Sets a particular socket option.
This API function can be called multiple times for the same socket to set various socket options.
[in] | handle | Handle of the socket to set the option for. |
[in] | level | Level at which the option resides: CY_SOCKET_SOL_SOCKET CY_SOCKET_SOL_TCP CY_SOCKET_SOL_TLS |
[in] | optname | Socket option to be set: CY_SOCKET_SO_RCVTIMEO CY_SOCKET_SO_SNDTIMEO CY_SOCKET_SO_NONBLOCK CY_SOCKET_SO_TCP_USER_TIMEOUT CY_SOCKET_SO_CONNECT_REQUEST_CALLBACK CY_SOCKET_SO_RECEIVE_CALLBACK CY_SOCKET_SO_DISCONNECT_CALLBACK CY_SOCKET_SO_TRUSTED_ROOTCA_CERTIFICATE CY_SOCKET_SO_TLS_IDENTITY CY_SOCKET_SO_SERVER_NAME_INDICATION CY_SOCKET_SO_ALPN_PROTOCOLS CY_SOCKET_SO_TLS_AUTH_MODE CY_SOCKET_SO_TLS_MFL CY_SOCKET_SO_JOIN_MULTICAST_GROUP CY_SOCKET_SO_LEAVE_MULTICAST_GROUP CY_SOCKET_SO_IP_MULTICAST_TTL CY_SOCKET_SO_BROADCAST CY_SOCKET_SO_IP_TOS |
[in] | optval | A buffer containing the value of the option to set. |
[in] | optlen | The length of the buffer pointed to by optval. |
cy_rslt_t cy_socket_getsockopt | ( | cy_socket_t | handle, |
int | level, | ||
int | optname, | ||
void * | optval, | ||
uint32_t * | optlen | ||
) |
Gets the value of a particular socket option.
[in] | handle | Handle of the socket to get the option value for. |
[in] | level | Level at which the option resides: CY_SOCKET_SOL_SOCKET CY_SOCKET_SOL_TCP CY_SOCKET_SOL_TLS |
[in] | optname | Socket options: CY_SOCKET_SO_RCVTIMEO CY_SOCKET_SO_SNDTIMEO CY_SOCKET_SO_NONBLOCK CY_SOCKET_SO_NWRITE CY_SOCKET_SO_TCP_USER_TIMEOUT CY_SOCKET_SO_SERVER_NAME_INDICATION CY_SOCKET_SO_TLS_AUTH_MODE CY_SOCKET_SO_IP_TOS |
[out] | optval | Buffer containing the value of the option to get. |
[in,out] | optlen | Length of the option value. It is a value-result argument; the caller provides the size of the buffer pointed to by optval, and is modified by this function on return to indicate the actual size of the value returned. |
cy_rslt_t cy_socket_gethostbyname | ( | const char * | hostname, |
cy_socket_ip_version_t | ip_ver, | ||
cy_socket_ip_address_t * | addr | ||
) |
Resolves a host name using Domain Name Service.
[in] | hostname | Hostname to resolve. It should be a null-terminated string containing ASCII characters. |
[in] | ip_ver | IP version type cy_socket_ip_version_t for which the hostname has to be resolved. |
[out] | addr | IP address of the specified host. Refer cy_socket_ip_address_t for IP address endienness. |
cy_rslt_t cy_socket_poll | ( | cy_socket_t | handle, |
uint32_t * | rwflags, | ||
uint32_t | timeout | ||
) |
Checks whether data is available on the socket.
[in] | handle | Socket handle returned by either the cy_socket_create API function for client sockets, or by cy_socket_accept API function for accepted sockets. |
[in,out] | rwflags | On input, the flags indicate whether the socket needs to be polled for read/write/read-write operation. On return, the flags are updated to indicate the status of the socket readiness for a read/write/read-write operation. |
[in] | timeout | Maximum amount of time in milliseconds to wait before returning. If timeout is zero, the function returns immediately. If timeout is CY_SOCKET_NEVER_TIMEOUT, the function waits indefinitely. |
cy_rslt_t cy_socket_shutdown | ( | cy_socket_t | handle, |
int | how | ||
) |
Shuts down the send and/or receive operation on the given TCP socket.
[in] | handle | Socket handle. |
[in] | how | Socket shutdown modes. Supported modes: CY_SOCKET_SHUT_RD, CY_SOCKET_SHUT_WR, and CY_SOCKET_SHUT_RDWR. |
cy_rslt_t cy_socket_delete | ( | cy_socket_t | handle | ) |
Releases the resources allocated for the socket by the cy_socket_create function.
[in] | handle | Socket handle returned by the cy_socket_create API function. |