oveRTOS C++ API
C++20 RAII wrappers for the oveRTOS C API
Loading...
Searching...
No Matches
net_tls.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 Kamil Lulko <kamil.lulko@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 *
6 * This file is part of oveRTOS.
7 */
8
14#pragma once
15
16#include <ove/net_tls.h>
17#include <ove/net.hpp>
18#include <ove/types.hpp>
19
20#ifdef CONFIG_OVE_NET_TLS
21
22namespace ove::tls {
23
27struct Config {
28 const unsigned char *ca_cert{};
29 size_t ca_cert_len{};
30 const char *hostname{};
31 const unsigned char *client_cert{};
32 size_t client_cert_len{};
33 const unsigned char *client_key{};
34 size_t client_key_len{};
35};
36
44class Session {
45public:
46 Session() {
47#ifdef CONFIG_OVE_ZERO_HEAP
48 int err = ove_tls_init(&handle_, &storage_);
49#else
50 int err = ove_tls_create(&handle_);
51#endif
52 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
53 }
54
55 ~Session() {
56 if (!handle_) return;
57#ifdef CONFIG_OVE_ZERO_HEAP
58 ove_tls_deinit(handle_);
59#else
60 ove_tls_destroy(handle_);
61#endif
62 }
63
64 Session(const Session &) = delete;
65 Session &operator=(const Session &) = delete;
66 Session(Session &&) = delete;
67 Session &operator=(Session &&) = delete;
68
72 [[nodiscard]] int handshake(ove_socket_t sock, const Config &cfg = {}) {
73 ove_tls_config_t c{cfg.ca_cert, cfg.ca_cert_len, cfg.hostname,
74 cfg.client_cert, cfg.client_cert_len,
75 cfg.client_key, cfg.client_key_len};
76 return ove_tls_handshake(handle_, sock, &c);
77 }
78
79 [[nodiscard]] int send(const void *data, size_t len, size_t *sent = nullptr) {
80 return ove_tls_send(handle_, data, len, sent);
81 }
82
83 [[nodiscard]] int recv(void *buf, size_t len, size_t *received = nullptr) {
84 return ove_tls_recv(handle_, buf, len, received);
85 }
86
87 void close() {
88 ove_tls_close(handle_);
89 }
90
91private:
92 ove_tls_t handle_{};
93#ifdef CONFIG_OVE_ZERO_HEAP
94 ove_tls_storage_t storage_{};
95#endif
96};
97
98} // namespace ove::tls
99
100#endif /* CONFIG_OVE_NET_TLS */
RAII networking: sockets, network interface, DNS.
Common type definitions and concepts for the C++ wrapper layer.