-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #475 from atsign-foundation/feat-socket-layer
feat: move mbedtls sockets into its own layer under connection
- Loading branch information
Showing
21 changed files
with
852 additions
and
407 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
#ifndef ATCLIENT_SOCKET_H | ||
#define ATCLIENT_SOCKET_H | ||
#include <atchops/platform.h> | ||
#ifndef ATCLIENT_SOCKET_SHARED_H | ||
#include <atclient/socket_shared.h> | ||
#endif | ||
#include <stddef.h> | ||
#include <stdint.h> | ||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#ifndef ATCLIENT_SSL_TIMEOUT_EXITCODE | ||
|
||
#if defined(ATCLIENT_SOCKET_PROVIDER_MBEDTLS) | ||
#define ATCLIENT_SSL_TIMEOUT_EXITCODE MBEDTLS_ERR_SSL_TIMEOUT | ||
|
||
#elif defined(ATCLIENT_SOCKET_PROVIDER_ARDUINO_BEARSSL) | ||
// Most arduino libraries only use -1 or positive integers | ||
#define ATCLIENT_SSL_TIMEOUT_EXITCODE -101 | ||
|
||
#else | ||
#error "ATCLIENT_ERR_SSL_TIMEOUT is undefined" | ||
|
||
#endif | ||
|
||
#endif | ||
|
||
// IWYU pragma: begin_exports | ||
|
||
// Export the appropriate platform specific struct implementation | ||
#if defined(ATCLIENT_SOCKET_PROVIDER_MBEDTLS) | ||
#include "socket_mbedtls.h" | ||
#endif | ||
|
||
// IWYU pragma: end_exports | ||
|
||
/** | ||
* @brief Initializes a raw socket | ||
* | ||
* @param socket The socket structure to initialize | ||
*/ | ||
void atclient_raw_socket_init(struct atclient_raw_socket *socket); | ||
|
||
/** | ||
* @brief Frees resources associated with a network socket | ||
* | ||
* @param socket The socket structure to free resources from | ||
*/ | ||
void atclient_raw_socket_free(struct atclient_raw_socket *socket); | ||
|
||
/** | ||
* @brief Initializes a tls socket with the specified parameters | ||
* | ||
* @param socket The socket structure to initialize | ||
*/ | ||
void atclient_tls_socket_init(struct atclient_tls_socket *socket); | ||
|
||
/** | ||
* @brief Configures the SSL on a TLS socket | ||
* | ||
* @param ca_pem The X.509 CA certificates in pem format (leave NULL to use the provided default certificates) | ||
* @param ca_pem_len Length of the ca_pem, ignored if ca_pem is NULL | ||
* | ||
* @return 0 on success, non-zero on failure | ||
* | ||
* @note Should be called after atclient_tls_socket_init, note that this | ||
* contains the rest of the initialization operations which have potential | ||
* to fail | ||
*/ | ||
int atclient_tls_socket_configure(struct atclient_tls_socket *socket, unsigned char *ca_pem, size_t ca_pem_len); | ||
|
||
/** | ||
* @brief Frees resources associated with a network socket | ||
* | ||
* @param socket The socket structure to free resources from | ||
*/ | ||
void atclient_tls_socket_free(struct atclient_tls_socket *socket); | ||
|
||
/** | ||
* @brief Establishes a connection to the specified host and port using the network socket | ||
* | ||
* @param socket Pointer to the initialized network socket structure | ||
* @param host The hostname or IP address to connect to | ||
* @param port The port number to connect to | ||
* | ||
* @return 0 on success, non-zero on failure | ||
*/ | ||
int atclient_tls_socket_connect(struct atclient_tls_socket *socket, const char *host, const uint16_t port); | ||
|
||
/** | ||
* @brief Disconnects and closes an established network socket connection | ||
* | ||
* @param socket Pointer to the network socket structure to disconnect | ||
* | ||
* @return 0 on success, non-zero on failure | ||
*/ | ||
int atclient_tls_socket_disconnect(struct atclient_tls_socket *socket); | ||
|
||
/** | ||
* @brief Writes data to an established network socket connection | ||
* | ||
* @param socket Pointer to the network socket structure | ||
* @param value Pointer to the buffer containing data to write | ||
* @param value_len Length of the data to write in bytes | ||
* | ||
* @return 0 on success, non-zero on failure | ||
*/ | ||
int atclient_tls_socket_write(struct atclient_tls_socket *socket, const unsigned char *value, size_t value_len); | ||
|
||
/** | ||
* @brief Reads data from an established network socket connection | ||
* | ||
* @param socket Pointer to the network socket structure | ||
* @param value Pointer to the buffer where read data will be stored | ||
* @param value_len Pointer to store the length of data read in bytes | ||
* @param options Options which specify the behaviour of reading the data | ||
* | ||
* @return 0 on success, non-zero on failure | ||
*/ | ||
int atclient_tls_socket_read(struct atclient_tls_socket *socket, unsigned char **value, size_t *value_len, | ||
const struct atclient_socket_read_options options); | ||
|
||
/** | ||
* @brief Sets the read timeout for a TLS socket | ||
* | ||
* @param socket Pointer to the initialized TLS socket structure | ||
* @param timeout_ms The timeout value in milliseconds | ||
*/ | ||
void atclient_tls_socket_set_read_timeout(struct atclient_tls_socket *socket, const int timeout_ms); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// IWYU pragma: private, include "atclient/socket.h" | ||
// IWYU pragma: friend "socket_mbedtls.*" | ||
#ifndef ATCLIENT_SOCKET_MBEDTLS_H | ||
#define ATCLIENT_SOCKET_MBEDTLS_H | ||
#include <atchops/platform.h> | ||
#if defined(ATCLIENT_SOCKET_PROVIDER_MBEDTLS) | ||
#include <atclient/socket_shared.h> | ||
#include <mbedtls/ctr_drbg.h> | ||
#include <mbedtls/entropy.h> | ||
#include <mbedtls/net_sockets.h> | ||
#include <mbedtls/ssl.h> | ||
#include <mbedtls/threading.h> | ||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
// TODO: Make this type more portable to consume later | ||
struct atclient_raw_socket { | ||
mbedtls_net_context net; | ||
}; | ||
|
||
struct atclient_tls_socket { | ||
struct atclient_raw_socket raw; | ||
mbedtls_ssl_context ssl; | ||
mbedtls_ssl_config ssl_config; | ||
mbedtls_x509_crt cacert; | ||
mbedtls_entropy_context entropy; | ||
mbedtls_ctr_drbg_context ctr_drbg; | ||
}; | ||
#ifdef __cplusplus | ||
} | ||
#endif | ||
#endif | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// IWYU pragma: private, include "atclient/socket.h" | ||
// IWYU pragma: friend "socket_mbedtls.*" | ||
#ifndef ATCLIENT_SOCKET_SHARED_H | ||
#define ATCLIENT_SOCKET_SHARED_H | ||
#include <stddef.h> | ||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#include <atchops/platform.h> | ||
|
||
#if defined(ATCLIENT_SOCKET_PROVIDER_EXTERNAL) | ||
// Noop, this indicates an external socket provider will be linked | ||
#else | ||
#define ATCLIENT_SOCKET_PROVIDER_MBEDTLS | ||
#endif | ||
|
||
#ifdef ATCLIENT_SOCKET_PROVIDER_EXTERNAL | ||
#include "../atsdk_socket.h" // IWYU pragma: export | ||
#else | ||
// Defined later based on platform specific implementation | ||
struct atclient_tls_socket; | ||
|
||
// Raw socket is only implemented as an internal construct for now | ||
// In the future it will be a supported standalone socket that can | ||
// be used directly | ||
struct atclient_raw_socket; | ||
#endif | ||
|
||
enum atclient_socket_read_type { | ||
// ATCLIENT_SOCKET_READ_NUM_BYTES, | ||
ATCLIENT_SOCKET_READ_UNTIL_CHAR, | ||
ATCLIENT_SOCKET_READ_CLEAR_AT_PROMPT, | ||
}; | ||
|
||
// Define how much we should try to read | ||
struct atclient_socket_read_options { | ||
enum atclient_socket_read_type type; | ||
union { | ||
// size_t num_bytes; | ||
char until_char; | ||
}; | ||
}; | ||
|
||
/** | ||
* @brief Creates read options configured to read until a number of characters have been read | ||
* | ||
* @param bytes The number of characters to try to read | ||
* | ||
* @return struct atclient_socket_read_options Configuration structure for read operation | ||
*/ | ||
// struct atclient_socket_read_options atclient_socket_read_num_bytes(size_t bytes); | ||
|
||
/** | ||
* @brief Creates read options configured to read until a specific character is encountered | ||
* | ||
* @param read_until The character to read until (delimiter) | ||
*/ | ||
struct atclient_socket_read_options atclient_socket_read_until_char(char read_until); | ||
|
||
/** | ||
* @brief Creates read options configured to read until a specific character is encountered | ||
*/ | ||
struct atclient_socket_read_options atclient_socket_read_clear_at_prompt(); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
#endif |
Oops, something went wrong.