FreeRTOS:
Secure Sockets
|
Return to main page ↑ |
Secure Sockets Interface. More...
#include <stdint.h>
#include <stddef.h>
#include "iot_secure_sockets_config.h"
#include "iot_secure_sockets_config_defaults.h"
#include "iot_secure_sockets_wrapper_metrics.h"
#include "iot_lib_init.h"
Go to the source code of this file.
Data Structures | |
struct | SocketsSockaddr_t |
Socket address. More... | |
Macros | |
#define | Socklen_t uint32_t |
The "size_t" of secure sockets. More... | |
#define | SOCKETS_INVALID_SOCKET ( ( Socket_t ) ~0U ) |
Assigned to an Socket_t variable when the socket is not valid. | |
#define | securesocketsMAX_DNS_NAME_LENGTH ( 253 ) |
Maximum length of an ASCII DNS name. | |
#define | securesocketsDEFAULT_TLS_DESTINATION_PORT 443 |
Well-known port numbers. | |
#define | socketsAWS_IOT_ALPN_MQTT "x-amzn-mqtt-ca" |
AWS IoT ALPN protocol name for MQTT over TLS on server port 443. | |
#define | SOCKETS_htonl(usIn) ( ( uint32_t ) ( usIn ) ) |
Convert an unsigned thirty-two-bit value from host endianness to network endianness. More... | |
#define | SOCKETS_ntohl(usIn) SOCKETS_htonl( usIn ) |
Convert an unsigned thirty-two-bit value from network endianness to host endianness. More... | |
#define | SOCKETS_htons(usIn) ( ( uint16_t ) ( usIn ) ) |
Convert an unsigned sixteen-bit value from host endianness to network endianness. More... | |
#define | SOCKETS_ntohs(usIn) SOCKETS_htons( usIn ) |
Convert an unsigned sixteen-bit value from network endianness to host endianness. More... | |
#define | SOCKETS_inet_addr_quick(ucOctet0, ucOctet1, ucOctet2, ucOctet3) |
Convert an IP address expressed as four separate numeric octets into a an IP address expressed as a 32-bit number in network byte order (for example 192, 168, 0, 100) More... | |
#define | SOCKETS_inet_ntoa(ulIPAddress, pucBuffer) |
Convert an IP address expressed as a 32-bit number in network byte order to a string in decimal dot notation. (for example "192.168.0.100") More... | |
SocketsErrors | |
#define | SOCKETS_ERROR_NONE ( 0 ) |
#define | SOCKETS_SOCKET_ERROR ( -1 ) |
#define | SOCKETS_EWOULDBLOCK ( -11 ) |
#define | SOCKETS_ENOMEM ( -12 ) |
#define | SOCKETS_EINVAL ( -22 ) |
#define | SOCKETS_ENOPROTOOPT ( -109 ) |
#define | SOCKETS_ENOTCONN ( -126 ) |
#define | SOCKETS_EISCONN ( -127 ) |
#define | SOCKETS_ECLOSED ( -128 ) |
#define | SOCKETS_TLS_INIT_ERROR ( -1001 ) |
#define | SOCKETS_TLS_HANDSHAKE_ERROR ( -1002 ) |
#define | SOCKETS_TLS_SERVER_UNVERIFIED ( -1003 ) |
#define | SOCKETS_TLS_RECV_ERROR ( -1004 ) |
#define | SOCKETS_TLS_SEND_ERROR ( -1005 ) |
#define | SOCKETS_PERIPHERAL_RESET ( -1006 ) |
SocketDomains | |
Options for the lDomain parameter of SOCKETS_Socket() function. These select the protocol family to be used for communication. | |
#define | SOCKETS_AF_INET ( 2 ) |
#define | SOCKETS_PF_INET SOCKETS_AF_INET |
#define | SOCKETS_AF_INET6 ( 10 ) |
SocketTypes | |
Options for the lType parameter of SOCKETS_Socket() function. These specify the communication semantics. | |
#define | SOCKETS_SOCK_DGRAM ( 2 ) |
#define | SOCKETS_SOCK_STREAM ( 1 ) |
Protocols | |
Options for the lProtocol parameter of SOCKETS_Socket() function. | |
#define | SOCKETS_IPPROTO_UDP ( 17 ) |
#define | SOCKETS_IPPROTO_TCP ( 6 ) |
SetSockOptOptions | |
Options for lOptionName in SOCKETS_SetSockOpt(). | |
#define | SOCKETS_SO_RCVTIMEO ( 0 ) |
#define | SOCKETS_SO_SNDTIMEO ( 1 ) |
#define | SOCKETS_SO_SNDBUF ( 4 ) |
#define | SOCKETS_SO_RCVBUF ( 5 ) |
#define | SOCKETS_SO_SERVER_NAME_INDICATION ( 6 ) |
#define | SOCKETS_SO_TRUSTED_SERVER_CERTIFICATE ( 7 ) |
#define | SOCKETS_SO_REQUIRE_TLS ( 8 ) |
#define | SOCKETS_SO_NONBLOCK ( 9 ) |
#define | SOCKETS_SO_ALPN_PROTOCOLS ( 10 ) |
#define | SOCKETS_SO_WAKEUP_CALLBACK ( 17 ) |
#define | SOCKETS_SO_TCPKEEPALIVE ( 18 ) |
#define | SOCKETS_SO_TCPKEEPALIVE_INTERVAL ( 19 ) |
#define | SOCKETS_SO_TCPKEEPALIVE_COUNT ( 20 ) |
#define | SOCKETS_SO_TCPKEEPALIVE_IDLE_TIME ( 21 ) |
ShutdownFlags | |
Options for the ulHow parameter in SOCKETS_Shutdown(). | |
#define | SOCKETS_SHUT_RD ( 0 ) |
#define | SOCKETS_SHUT_WR ( 1 ) |
#define | SOCKETS_SHUT_RDWR ( 2 ) |
Typedefs | |
typedef struct xSOCKET * | Socket_t |
Socket handle data type. | |
Functions | |
BaseType_t | SOCKETS_Init (void) |
Secure Sockets library initialization function. More... | |
Socket_t | SOCKETS_Socket (int32_t lDomain, int32_t lType, int32_t lProtocol) |
Creates a TCP socket. More... | |
int32_t | SOCKETS_Bind (Socket_t xSocket, SocketsSockaddr_t *pxAddress, Socklen_t xAddressLength) |
Bind a TCP socket. More... | |
int32_t | SOCKETS_Connect (Socket_t xSocket, SocketsSockaddr_t *pxAddress, Socklen_t xAddressLength) |
Connects the socket to the specified IP address and port. More... | |
int32_t | SOCKETS_Recv (Socket_t xSocket, void *pvBuffer, size_t xBufferLength, uint32_t ulFlags) |
Receive data from a TCP socket. More... | |
int32_t | SOCKETS_Send (Socket_t xSocket, const void *pvBuffer, size_t xDataLength, uint32_t ulFlags) |
Transmit data to the remote socket. More... | |
int32_t | SOCKETS_Shutdown (Socket_t xSocket, uint32_t ulHow) |
Closes all or part of a full-duplex connection on the socket. More... | |
int32_t | SOCKETS_Close (Socket_t xSocket) |
Closes the socket and frees the related resources. More... | |
int32_t | SOCKETS_SetSockOpt (Socket_t xSocket, int32_t lLevel, int32_t lOptionName, const void *pvOptionValue, size_t xOptionLength) |
Manipulates the options for the socket. More... | |
uint32_t | SOCKETS_GetHostByName (const char *pcHostName) |
Resolve a host name using Domain Name Service. More... | |
Secure Sockets Interface.
Secure sockets is a portable layer for establishing a TCP/IP connection, with the option of using TLS.
Secure sockets is based on the Berkeley sockets API. A few difference general differences between Berkeley and SOCKETS are:
#define Socklen_t uint32_t |
The "size_t" of secure sockets.
This type is used for compatibility with the expected Berkeley sockets naming.
#define SOCKETS_ERROR_NONE ( 0 ) |
No error.
#define SOCKETS_SOCKET_ERROR ( -1 ) |
Catch-all sockets error code.
#define SOCKETS_EWOULDBLOCK ( -11 ) |
A resource is temporarily unavailable.
#define SOCKETS_ENOMEM ( -12 ) |
Memory allocation failed.
#define SOCKETS_EINVAL ( -22 ) |
Invalid argument.
#define SOCKETS_ENOPROTOOPT ( -109 ) |
A bad option was specified .
#define SOCKETS_ENOTCONN ( -126 ) |
The supplied socket is not connected.
#define SOCKETS_EISCONN ( -127 ) |
The supplied socket is already connected.
#define SOCKETS_ECLOSED ( -128 ) |
The supplied socket has already been closed.
#define SOCKETS_TLS_INIT_ERROR ( -1001 ) |
TLS initialization failed.
#define SOCKETS_TLS_HANDSHAKE_ERROR ( -1002 ) |
TLS handshake failed.
#define SOCKETS_TLS_SERVER_UNVERIFIED ( -1003 ) |
A connection was made but the server could not be verified. It is recommended that the socket be closed.
#define SOCKETS_TLS_RECV_ERROR ( -1004 ) |
TLS receive operation failed.
#define SOCKETS_TLS_SEND_ERROR ( -1005 ) |
TLS send operation failed.
#define SOCKETS_PERIPHERAL_RESET ( -1006 ) |
Communications peripheral has been reset.
#define SOCKETS_AF_INET ( 2 ) |
IPv4 Internet Protocols.
#define SOCKETS_PF_INET SOCKETS_AF_INET |
IPv4 Internet Protocol.
#define SOCKETS_AF_INET6 ( 10 ) |
IPv6 Internet Protocols. This option is currently not supported.
#define SOCKETS_SOCK_DGRAM ( 2 ) |
Datagram.
#define SOCKETS_SOCK_STREAM ( 1 ) |
Byte-stream.
#define SOCKETS_IPPROTO_UDP ( 17 ) |
UDP. This option is currently not supported.
#define SOCKETS_IPPROTO_TCP ( 6 ) |
TCP.
#define SOCKETS_SO_RCVTIMEO ( 0 ) |
Set the receive timeout.
#define SOCKETS_SO_SNDTIMEO ( 1 ) |
Set the send timeout.
#define SOCKETS_SO_SNDBUF ( 4 ) |
Set the size of the send buffer (TCP only).
#define SOCKETS_SO_RCVBUF ( 5 ) |
Set the size of the receive buffer (TCP only).
#define SOCKETS_SO_SERVER_NAME_INDICATION ( 6 ) |
Toggle client use of TLS SNI.
#define SOCKETS_SO_TRUSTED_SERVER_CERTIFICATE ( 7 ) |
Override default TLS server certificate trust. Must be PEM encoded and length must include null terminator.
#define SOCKETS_SO_REQUIRE_TLS ( 8 ) |
Toggle client enforcement of TLS.
#define SOCKETS_SO_NONBLOCK ( 9 ) |
Socket is nonblocking.
#define SOCKETS_SO_ALPN_PROTOCOLS ( 10 ) |
Application protocol list to be included in TLS ClientHello.
#define SOCKETS_SO_WAKEUP_CALLBACK ( 17 ) |
Set the callback to be called whenever there is data available on the socket for reading.
#define SOCKETS_SO_TCPKEEPALIVE ( 18 ) |
Enable or Disable TCP keep-alive functionality.
#define SOCKETS_SO_TCPKEEPALIVE_INTERVAL ( 19 ) |
Set the time in seconds between individual TCP keep-alive probes.
#define SOCKETS_SO_TCPKEEPALIVE_COUNT ( 20 ) |
Set the maximum number of keep-alive probes TCP should send before dropping the connection.
#define SOCKETS_SO_TCPKEEPALIVE_IDLE_TIME ( 21 ) |
Set the time in seconds for which the connection needs to remain idle before TCP starts sending keep-alive probes.
#define SOCKETS_SHUT_RD ( 0 ) |
No further receives.
#define SOCKETS_SHUT_WR ( 1 ) |
No further sends.
#define SOCKETS_SHUT_RDWR ( 2 ) |
No further send or receive.
#define SOCKETS_htonl | ( | usIn | ) | ( ( uint32_t ) ( usIn ) ) |
Convert an unsigned thirty-two-bit value from host endianness to network endianness.
[in] | usIn | The unsigned thirty-two-bit value to convert. |
#define SOCKETS_ntohl | ( | usIn | ) | SOCKETS_htonl( usIn ) |
Convert an unsigned thirty-two-bit value from network endianness to host endianness.
[in] | usIn | The unsigned thirty-two-bit value to convert. |
#define SOCKETS_htons | ( | usIn | ) | ( ( uint16_t ) ( usIn ) ) |
Convert an unsigned sixteen-bit value from host endianness to network endianness.
[in] | usIn | The unsigned sixteen-bit value to convert. |
#define SOCKETS_ntohs | ( | usIn | ) | SOCKETS_htons( usIn ) |
Convert an unsigned sixteen-bit value from network endianness to host endianness.
[in] | usIn | The unsigned sixteen-bit value to convert. |
#define SOCKETS_inet_addr_quick | ( | ucOctet0, | |
ucOctet1, | |||
ucOctet2, | |||
ucOctet3 | |||
) |
Convert an IP address expressed as four separate numeric octets into a an IP address expressed as a 32-bit number in network byte order (for example 192, 168, 0, 100)
[in] | ucOctet0 | 0th IP Octet |
[in] | ucOctet1 | 1st IP Octet |
[in] | ucOctet2 | 2nd IP Octet |
[in] | ucOctet3 | 3rd IP Octet |
#define SOCKETS_inet_ntoa | ( | ulIPAddress, | |
pucBuffer | |||
) |
Convert an IP address expressed as a 32-bit number in network byte order to a string in decimal dot notation. (for example "192.168.0.100")
[in] | ulIPAddress | An IP address expressed as a 32-bit value in network byte order. |
[in] | pucBuffer | A pointer to a buffer into which the IP address will be written in decimal dot notation. |
BaseType_t SOCKETS_Init | ( | void | ) |
Secure Sockets library initialization function.
This function does general initialization and setup. It must be called once and only once before calling any other function.
pdPASS
if everything succeedspdFAIL
otherwise. Socket_t SOCKETS_Socket | ( | int32_t | lDomain, |
int32_t | lType, | ||
int32_t | lProtocol | ||
) |
Creates a TCP socket.
See the FreeRTOS+TCP networking tutorial for more information on TCP sockets.
See the Berkeley Sockets API in wikipedia
[in] | lDomain | Must be set to SOCKETS_AF_INET. See SocketDomains. |
[in] | lType | Set to SOCKETS_SOCK_STREAM to create a TCP socket. No other value is valid. See SocketTypes. |
[in] | lProtocol | Set to SOCKETS_IPPROTO_TCP to create a TCP socket. No other value is valid. See Protocols. |
int32_t SOCKETS_Bind | ( | Socket_t | xSocket, |
SocketsSockaddr_t * | pxAddress, | ||
Socklen_t | xAddressLength | ||
) |
Bind a TCP socket.
See the FreeRTOS+TCP networking tutorial for more information on TCP sockets.
See the Berkeley Sockets API in wikipedia
Limitations:
i. The caller of SOCKETS_Bind() API should make sure the socket address has the correct local IP address for the interface. ii. Some source ports may be unavailable depending on the TCP/IP stack implementation.
NOTE: If the SOCKETS_Bind() API binds to a source port in ephemeral port range, and the caller calls SOCKETS_Bind() API before SOCKETS_Connect() API, then a conflict of source port arises as another TCP connection may pick the the same chosen port via tcp_new_port() API ( by scanning its internal TCP connection list )
[in] | xSocket | The handle of the socket to which specified address to be bound. |
[in] | pxAddress | A pointer to a SocketsSockaddr_t structure that contains the address and port to be bound to the socket. |
[in] | xAddressLength | Should be set to sizeof( SocketsSockaddr_t ). |
int32_t SOCKETS_Connect | ( | Socket_t | xSocket, |
SocketsSockaddr_t * | pxAddress, | ||
Socklen_t | xAddressLength | ||
) |
Connects the socket to the specified IP address and port.
The socket must first have been successfully created by a call to SOCKETS_Socket().
If this function returns an error the socket is considered invalid.
See the Berkeley Sockets API in wikipedia
[in] | xSocket | The handle of the socket to be connected. |
[in] | pxAddress | A pointer to a SocketsSockaddr_t structure that contains the the address to connect the socket to. |
[in] | xAddressLength | Should be set to sizeof( SocketsSockaddr_t ). |
int32_t SOCKETS_Recv | ( | Socket_t | xSocket, |
void * | pvBuffer, | ||
size_t | xBufferLength, | ||
uint32_t | ulFlags | ||
) |
Receive data from a TCP socket.
The socket must have already been created using a call to SOCKETS_Socket() and connected to a remote socket using SOCKETS_Connect().
See the Berkeley Sockets API in wikipedia
[in] | xSocket | The handle of the socket from which data is being received. |
[out] | pvBuffer | The buffer into which the received data will be placed. |
[in] | xBufferLength | The maximum number of bytes which can be received. pvBuffer must be at least xBufferLength bytes long. |
[in] | ulFlags | Not currently used. Should be set to 0. |
int32_t SOCKETS_Send | ( | Socket_t | xSocket, |
const void * | pvBuffer, | ||
size_t | xDataLength, | ||
uint32_t | ulFlags | ||
) |
Transmit data to the remote socket.
The socket must have already been created using a call to SOCKETS_Socket() and connected to a remote socket using SOCKETS_Connect().
See the Berkeley Sockets API in wikipedia
[in] | xSocket | The handle of the sending socket. |
[in] | pvBuffer | The buffer containing the data to be sent. |
[in] | xDataLength | The length of the data to be sent. |
[in] | ulFlags | Not currently used. Should be set to 0. |
int32_t SOCKETS_Shutdown | ( | Socket_t | xSocket, |
uint32_t | ulHow | ||
) |
Closes all or part of a full-duplex connection on the socket.
Disable reads and writes on a connected TCP socket. A connected TCP socket must be gracefully shut down before it can be closed.
See the Berkeley Sockets API in wikipedia
[in] | xSocket | The handle of the socket to shutdown. |
[in] | ulHow | SOCKETS_SHUT_RD, SOCKETS_SHUT_WR or SOCKETS_SHUT_RDWR. ShutdownFlags |
int32_t SOCKETS_Close | ( | Socket_t | xSocket | ) |
Closes the socket and frees the related resources.
A socket should be shutdown gracefully before it is closed, and cannot be used after it has been closed.
See the Berkeley Sockets API in wikipedia
[in] | xSocket | The handle of the socket to close. |
int32_t SOCKETS_SetSockOpt | ( | Socket_t | xSocket, |
int32_t | lLevel, | ||
int32_t | lOptionName, | ||
const void * | pvOptionValue, | ||
size_t | xOptionLength | ||
) |
Manipulates the options for the socket.
See the Berkeley Sockets API in wikipedia
[in] | xSocket | The handle of the socket to set the option for. |
[in] | lLevel | Not currently used. Should be set to 0. |
[in] | lOptionName | See SetSockOptOptions. |
[in] | pvOptionValue | A buffer containing the value of the option to set. |
[in] | xOptionLength | The length of the buffer pointed to by pvOptionValue. |
uint32_t SOCKETS_GetHostByName | ( | const char * | pcHostName | ) |
Resolve a host name using Domain Name Service.
See the Berkeley Sockets API in wikipedia
[in] | pcHostName | The host name to resolve. |