FreeRTOS: Secure Sockets
Return to main page ↑
iot_secure_sockets.h
Go to the documentation of this file.
1 /*
2  * FreeRTOS Secure Sockets V1.3.1
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://aws.amazon.com/freertos
23  * http://www.FreeRTOS.org
24  */
25 
43 #ifndef _AWS_SECURE_SOCKETS_H_
44 #define _AWS_SECURE_SOCKETS_H_
45 
46 /*
47  #ifdef __cplusplus
48  * extern "C" {
49  #endif
50  */
51 #include <stdint.h>
52 #include <stddef.h>
53 #include "iot_secure_sockets_config.h"
55 #include "iot_secure_sockets_wrapper_metrics.h"
56 #include "iot_lib_init.h"
57 
67 struct xSOCKET;
68 typedef struct xSOCKET * Socket_t;
76 #define Socklen_t uint32_t
77 
90 #define SOCKETS_ERROR_NONE ( 0 )
91 #define SOCKETS_SOCKET_ERROR ( -1 )
92 #define SOCKETS_EWOULDBLOCK ( -11 )
93 #define SOCKETS_ENOMEM ( -12 )
94 #define SOCKETS_EINVAL ( -22 )
95 #define SOCKETS_ENOPROTOOPT ( -109 )
96 #define SOCKETS_ENOTCONN ( -126 )
97 #define SOCKETS_EISCONN ( -127 )
98 #define SOCKETS_ECLOSED ( -128 )
99 #define SOCKETS_TLS_INIT_ERROR ( -1001 )
100 #define SOCKETS_TLS_HANDSHAKE_ERROR ( -1002 )
101 #define SOCKETS_TLS_SERVER_UNVERIFIED ( -1003 )
102 #define SOCKETS_TLS_RECV_ERROR ( -1004 )
103 #define SOCKETS_TLS_SEND_ERROR ( -1005 )
104 #define SOCKETS_PERIPHERAL_RESET ( -1006 )
110 #define SOCKETS_INVALID_SOCKET ( ( Socket_t ) ~0U )
111 
122 #define SOCKETS_AF_INET ( 2 )
123 #define SOCKETS_PF_INET SOCKETS_AF_INET
124 #define SOCKETS_AF_INET6 ( 10 )
137 #define SOCKETS_SOCK_DGRAM ( 2 )
138 #define SOCKETS_SOCK_STREAM ( 1 )
149 #define SOCKETS_IPPROTO_UDP ( 17 )
150 #define SOCKETS_IPPROTO_TCP ( 6 )
161 #define SOCKETS_SO_RCVTIMEO ( 0 )
162 #define SOCKETS_SO_SNDTIMEO ( 1 )
163 #define SOCKETS_SO_SNDBUF ( 4 )
164 #define SOCKETS_SO_RCVBUF ( 5 )
165 #define SOCKETS_SO_SERVER_NAME_INDICATION ( 6 )
166 #define SOCKETS_SO_TRUSTED_SERVER_CERTIFICATE ( 7 )
167 #define SOCKETS_SO_REQUIRE_TLS ( 8 )
168 #define SOCKETS_SO_NONBLOCK ( 9 )
169 #define SOCKETS_SO_ALPN_PROTOCOLS ( 10 )
170 #define SOCKETS_SO_WAKEUP_CALLBACK ( 17 )
171 #define SOCKETS_SO_TCPKEEPALIVE ( 18 )
172 #define SOCKETS_SO_TCPKEEPALIVE_INTERVAL ( 19 )
173 #define SOCKETS_SO_TCPKEEPALIVE_COUNT ( 20 )
174 #define SOCKETS_SO_TCPKEEPALIVE_IDLE_TIME ( 21 )
185 #define SOCKETS_SHUT_RD ( 0 )
186 #define SOCKETS_SHUT_WR ( 1 )
187 #define SOCKETS_SHUT_RDWR ( 2 )
193 #define securesocketsMAX_DNS_NAME_LENGTH ( 253 )
194 
201 typedef struct SocketsSockaddr
202 {
203  uint8_t ucLength;
204  uint8_t ucSocketDomain;
205  uint16_t usPort;
206  uint32_t ulAddress;
208 
212 #define securesocketsDEFAULT_TLS_DESTINATION_PORT 443
213 
224 extern BaseType_t SOCKETS_Init( void );
225 
251 /*
252  * This call allocates memory and claims a socket resource.
253  */
254 /* @[declare_secure_sockets_socket] */
255 Socket_t SOCKETS_Socket( int32_t lDomain,
256  int32_t lType,
257  int32_t lProtocol );
258 /* @[declare_secure_sockets_socket] */
259 
294 /* @[declare_secure_sockets_bind] */
295 int32_t SOCKETS_Bind( Socket_t xSocket,
296  SocketsSockaddr_t * pxAddress,
297  Socklen_t xAddressLength );
298 /* @[declare_secure_sockets_bind] */
299 
327 /* @[declare_secure_sockets_connect] */
328 int32_t SOCKETS_Connect( Socket_t xSocket,
329  SocketsSockaddr_t * pxAddress,
330  Socklen_t xAddressLength );
331 /* @[declare_secure_sockets_connect] */
332 
356 /* @[declare_secure_sockets_recv] */
357 int32_t SOCKETS_Recv( Socket_t xSocket,
358  void * pvBuffer,
359  size_t xBufferLength,
360  uint32_t ulFlags );
361 /* @[declare_secure_sockets_recv] */
362 
382 /* @[declare_secure_sockets_send] */
383 int32_t SOCKETS_Send( Socket_t xSocket,
384  const void * pvBuffer,
385  size_t xDataLength,
386  uint32_t ulFlags );
387 /* @[declare_secure_sockets_send] */
388 
411 /* @[declare_secure_sockets_shutdown] */
412 int32_t SOCKETS_Shutdown( Socket_t xSocket,
413  uint32_t ulHow );
414 /* @[declare_secure_sockets_shutdown] */
415 
435 /* @[declare_secure_sockets_close] */
436 int32_t SOCKETS_Close( Socket_t xSocket );
437 /* @[declare_secure_sockets_close] */
438 
442 #define socketsAWS_IOT_ALPN_MQTT "x-amzn-mqtt-ca"
443 
537 /* @[declare_secure_sockets_setsockopt] */
538 int32_t SOCKETS_SetSockOpt( Socket_t xSocket,
539  int32_t lLevel,
540  int32_t lOptionName,
541  const void * pvOptionValue,
542  size_t xOptionLength );
543 /* @[declare_secure_sockets_setsockopt] */
544 
557 /* @[declare_secure_sockets_gethostbyname] */
558 uint32_t SOCKETS_GetHostByName( const char * pcHostName );
559 /* @[declare_secure_sockets_gethostbyname] */
560 
561 
562 
569 #if defined( socketsconfigBYTE_ORDER ) && ( socketsconfigBYTE_ORDER == pdLITTLE_ENDIAN )
570  #define SOCKETS_htonl( ulIn ) ( ( uint32_t ) ( ( ( ulIn & 0xFF ) << 24 ) | ( ( ulIn & 0xFF00 ) << 8 ) | ( ( ulIn & 0xFF0000 ) >> 8 ) | ( ( ulIn & 0xFF000000 ) >> 24 ) ) )
571 #else
572  #define SOCKETS_htonl( usIn ) ( ( uint32_t ) ( usIn ) )
573 #endif
574 
581 #define SOCKETS_ntohl( usIn ) SOCKETS_htonl( usIn )
582 
583 
591 #if defined( socketsconfigBYTE_ORDER ) && ( socketsconfigBYTE_ORDER == pdLITTLE_ENDIAN )
592  #define SOCKETS_htons( usIn ) ( ( uint16_t ) ( ( ( usIn ) << 8U ) | ( ( usIn ) >> 8U ) ) )
593 #else
594  #define SOCKETS_htons( usIn ) ( ( uint16_t ) ( usIn ) )
595 #endif
596 
597 
604 #define SOCKETS_ntohs( usIn ) SOCKETS_htons( usIn )
605 
615 #if defined( socketsconfigBYTE_ORDER ) && ( socketsconfigBYTE_ORDER == pdLITTLE_ENDIAN )
616 
617  #define SOCKETS_inet_addr_quick( ucOctet0, ucOctet1, ucOctet2, ucOctet3 ) \
618  ( ( ( ( uint32_t ) ( ucOctet3 ) ) << 24UL ) | \
619  ( ( ( uint32_t ) ( ucOctet2 ) ) << 16UL ) | \
620  ( ( ( uint32_t ) ( ucOctet1 ) ) << 8UL ) | \
621  ( ( uint32_t ) ( ucOctet0 ) ) )
622 
630  #define SOCKETS_inet_ntoa( ulIPAddress, pucBuffer ) \
631  sprintf( ( char * ) ( pucBuffer ), "%u.%u.%u.%u", \
632  ( ( unsigned ) ( ( ulIPAddress ) & 0xffUL ) ), \
633  ( ( unsigned ) ( ( ( ulIPAddress ) >> 8 ) & 0xffUL ) ), \
634  ( ( unsigned ) ( ( ( ulIPAddress ) >> 16 ) & 0xffUL ) ), \
635  ( ( unsigned ) ( ( ulIPAddress ) >> 24 ) ) )
636 
637 #else /* socketsconfigBYTE_ORDER. */
638 
639  #define SOCKETS_inet_addr_quick( ucOctet0, ucOctet1, ucOctet2, ucOctet3 ) \
640  ( ( ( ( uint32_t ) ( ucOctet0 ) ) << 24UL ) | \
641  ( ( ( uint32_t ) ( ucOctet1 ) ) << 16UL ) | \
642  ( ( ( uint32_t ) ( ucOctet2 ) ) << 8UL ) | \
643  ( ( uint32_t ) ( ucOctet3 ) ) )
644 
652  #define SOCKETS_inet_ntoa( ulIPAddress, pucBuffer ) \
653  sprintf( ( char * ) ( pucBuffer ), "%u.%u.%u.%u", \
654  ( ( unsigned ) ( ( ulIPAddress ) >> 24 ) ), \
655  ( ( unsigned ) ( ( ( ulIPAddress ) >> 16 ) & 0xffUL ) ), \
656  ( ( unsigned ) ( ( ( ulIPAddress ) >> 8 ) & 0xffUL ) ), \
657  ( ( unsigned ) ( ( ulIPAddress ) & 0xffUL ) ) )
658 
659 #endif /* socketsconfigBYTE_ORDER. */
660 
661 /*
662  #ifdef __cplusplus
663  * }
664  #endif
665  */
666 
667 #endif /* _AWS_SECURE_SOCKETS_H_ */
int32_t SOCKETS_Recv(Socket_t xSocket, void *pvBuffer, size_t xBufferLength, uint32_t ulFlags)
Receive data from a TCP socket.
int32_t SOCKETS_Bind(Socket_t xSocket, SocketsSockaddr_t *pxAddress, Socklen_t xAddressLength)
Bind a TCP socket.
uint32_t SOCKETS_GetHostByName(const char *pcHostName)
Resolve a host name using Domain Name Service.
int32_t SOCKETS_Send(Socket_t xSocket, const void *pvBuffer, size_t xDataLength, uint32_t ulFlags)
Transmit data to the remote socket.
Ensures that the required sockets configuration options are supplied and the optional ones are set to...
Socket_t SOCKETS_Socket(int32_t lDomain, int32_t lType, int32_t lProtocol)
Creates a TCP socket.
BaseType_t SOCKETS_Init(void)
Secure Sockets library initialization function.
#define Socklen_t
The "size_t" of secure sockets.
Definition: iot_secure_sockets.h:76
struct xSOCKET * Socket_t
Socket handle data type.
Definition: iot_secure_sockets.h:68
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.
uint8_t ucLength
Definition: iot_secure_sockets.h:203
int32_t SOCKETS_Shutdown(Socket_t xSocket, uint32_t ulHow)
Closes all or part of a full-duplex connection on the socket.
uint8_t ucSocketDomain
Definition: iot_secure_sockets.h:204
int32_t SOCKETS_Connect(Socket_t xSocket, SocketsSockaddr_t *pxAddress, Socklen_t xAddressLength)
Connects the socket to the specified IP address and port.
Socket address.
Definition: iot_secure_sockets.h:201
uint32_t ulAddress
Definition: iot_secure_sockets.h:206
uint16_t usPort
Definition: iot_secure_sockets.h:205
int32_t SOCKETS_Close(Socket_t xSocket)
Closes the socket and frees the related resources.