tdslite latest
C++ API reference
Loading...
Searching...
No Matches
tdsl::net::tdsl_netimpl_esp_tls Struct Reference

Synchronous networking code for ESP-IDF, with TLS through esp-tls. More...

#include <tdslite-net/esp-idf/tdsl_netimpl_esp_tls.hpp>

Public Types

enum class  e_socket_error : tdsl::int32_t {
  host_too_long = -1 , resolve_failed = -2 , connection_failed = -3 , disconnected = -4 ,
  timeout = -5 , io_error = -6 , not_enough_capacity = -7 , not_connected = -8 ,
  tls_handshake_failed = -9 , tls_verify_failed = -10 , tls_setup_failed = -11 , packet_too_large = -12 ,
  tls_init_failed = -13 , not_contiguous = -14
}
 
using network_io_result = typename network_io_base< tdsl_netimpl_esp_tls >::network_io_result
 
enum  e_conection_state
 
using tds_msg_handler_result = packet_handler_result< bool, false >
 
using tds_packet_data_callback = callback< void, tdsl::uint32_t(*)(void *, tdsl::detail::e_tds_message_type, tdsl::binary_reader< tdsl::endian::little > &)>
 Packet data callback.
 
using connection_state_callback_ctx = callback< void, void(*)(void *, e_conection_state)>
 

Public Member Functions

template<tdsl::uint32_t BufSize>
 tdsl_netimpl_esp_tls (tdsl::uint8_t(&network_io_buffer)[BufSize]) noexcept
 
 tdsl_netimpl_esp_tls (tdsl::byte_span network_io_buffer) noexcept
 
 ~tdsl_netimpl_esp_tls () noexcept
 
 tdsl_netimpl_esp_tls (const tdsl_netimpl_esp_tls &)=delete
 
tdsl_netimpl_esp_tls & operator= (const tdsl_netimpl_esp_tls &)=delete
 
void set_tls_options (const tdsl_esp_tls_options &o) noexcept
 Set the TLS settings for the following connections.
 
void set_socket_timeouts (tdsl::uint32_t connect_ms, tdsl::uint32_t io_ms) noexcept
 Set timeouts.
 
bool is_connected () const noexcept
 
bool peer_closed () const noexcept
 Whether the server has closed the connection (or reset it, or sent data nobody asked for, e.g.
 
e_socket_error last_connect_error () const noexcept
 Why the last do_connect() failed; e_socket_error{} (0) after a successful one.
 
int last_mbedtls_error () const noexcept
 Of the last failed connection attempt: mbedTLS' error code (negative, e.g.
 
int last_verify_flags () const noexcept
 Of the last failed connection attempt: mbedTLS' certificate verification flags (MBEDTLS_X509_BADCERT_*), or 0.
 
template<typename T >
tdsl::expected< tdsl::traits::true_type, int > do_connect (T target, tdsl::uint16_t port) noexcept
 Connect to target : port and do the TLS handshake.
 
tdsl::int32_t do_disconnect () noexcept
 Close the connection, if any.
 
tdsl::int32_t do_send (byte_view header, byte_view message) noexcept
 Send a TDS packet (header and message) to the server, in one TLS record.
 
auto do_recv (tdsl::uint32_t transfer_exactly, byte_span dst_buf) noexcept -> network_io_result
 Read exactly transfer_exactly bytes into dst_buf.
 
auto do_recv (tdsl::uint32_t transfer_exactly) noexcept -> network_io_result
 Read exactly transfer_exactly bytes into the network buffer.
 
tdsl::expected< tdsl::traits::true_type, int > connect (T host, tdsl::uint16_t port) noexcept
 Connect to host: port.
 
tdsl::uint32_t abandon_response (tdsl::uint32_t processed) noexcept
 Give up on the response currently being received.
 
bool is_response_too_large () const noexcept
 Whether the last response had an item (e.g.
 
tdsl::uint32_t do_receive_tds_pdu () noexcept
 Receive one, complete TDS PDU.
 
tdsl::detail::e_send_status do_send_tds_pdu (tdsl::detail::e_tds_message_type mtype) noexcept
 Send contents of the message buffer in one or more TDS PDU's, depending on negotiated packet size.
 
void discard_tx () noexcept
 Discard the message being built in the network buffer, including its overflow state.
 
void do_write (tdsl::span< T > data) noexcept
 Append data to network buffer.
 
void do_write (tdsl::size_t offset, tdsl::span< T > data) noexcept
 Append data to network buffer, starting from offset.
 
tdsl::size_t do_get_write_offset () noexcept
 Get current write offset.
 
void register_packet_data_callback (tds_packet_data_callback::function_type cb, void *user_ptr) noexcept
 Set TDS packet data callback.
 
void set_tds_packet_size (tdsl::uint16_t value) noexcept
 Update negotiated packet size.
 
void set_connection_timeout_params (tdsl::uint16_t attempts, tdsl::uint16_t delay_ms) noexcept
 Set the conn retry count.
 
bool is_strict_encryption () const noexcept
 Whether the current connection is a TDS 8.0 "strict" one (TLS from the first byte).
 

Static Public Attributes

static constexpr tdsl::uint16_t k_min_tds_packet_size
 Smallest/largest valid TDS packet size (MS-TDS 2.2.7.9)
 
static constexpr tdsl::uint16_t k_max_tds_packet_size
 

Static Protected Member Functions

static byte_view as_packet (byte_view header, byte_view message) noexcept
 The whole TDS packet given to do_send(header, message).
 

Protected Attributes

bool strict_encryption
 
bool response_too_large
 
tdsl::uint16_t conn_retry_count
 
tdsl::uint16_t conn_retry_delay_ms
 
tdsl_buffer_object network_buffer
 

Detailed Description

Synchronous networking code for ESP-IDF, with TLS through esp-tls.

Every connection is a TDS 8.0 "strict" one: the TLS handshake happens right after the TCP connection, before any TDS traffic. The server must support strict mode and have a TLS certificate configured (SQL Server 2025, SQL Server 2022 on Windows, Azure SQL). The server's certificate is always verified, against tdsl_esp_tls_options::ca_pem or the certificate bundle.

The caller supplies the network buffer, like the other embedded implementations.

Member Typedef Documentation

◆ network_io_result

◆ tds_msg_handler_result

using tdsl::net::network_io_base< tdsl_netimpl_esp_tls >::tds_msg_handler_result = packet_handler_result<bool, false>
inherited

◆ tds_packet_data_callback

using tdsl::net::network_io_base< tdsl_netimpl_esp_tls >::tds_packet_data_callback = callback<void, tdsl::uint32_t (*)(void *, tdsl::detail::e_tds_message_type, tdsl::binary_reader<tdsl::endian::little> &)>
inherited

Packet data callback.

Returns amount of needed bytes, if any.

◆ connection_state_callback_ctx

using tdsl::net::network_io_base< tdsl_netimpl_esp_tls >::connection_state_callback_ctx = callback<void, void (*)( void *, e_conection_state)>
inherited

Member Enumeration Documentation

◆ e_socket_error

enum class tdsl::net::tdsl_netimpl_esp_tls::e_socket_error : tdsl::int32_t
strong
Enumerator
host_too_long 
resolve_failed 
connection_failed 
disconnected 
timeout 
io_error 
not_enough_capacity 
not_connected 
tls_handshake_failed 

TLS handshake failed (e.g.

the server does not support strict mode, or no TLS version / cipher in common)

tls_verify_failed 

The server's certificate was rejected (unknown CA, wrong host name, expired, ...)

tls_setup_failed 

The TLS settings could not be applied (e.g.

no CA given, or the CA certificate can't be parsed). Connecting again doesn't help.

packet_too_large 
tls_init_failed 

The TLS session could not be set up for another reason, e.g.

not enough heap for mbedTLS. Connecting again may help.

not_contiguous 

◆ e_conection_state

enum tdsl::net::network_io_base::e_conection_state : tdsl::int8_t
stronginherited

Constructor & Destructor Documentation

◆ tdsl_netimpl_esp_tls() [1/3]

template<tdsl::uint32_t BufSize>
tdsl::net::tdsl_netimpl_esp_tls::tdsl_netimpl_esp_tls ( tdsl::uint8_t(&)  network_io_buffer[BufSize])
inlinenoexcept
Parameters
[in]network_io_bufferNetwork I/O buffer

◆ tdsl_netimpl_esp_tls() [2/3]

tdsl::net::tdsl_netimpl_esp_tls::tdsl_netimpl_esp_tls ( tdsl::byte_span  network_io_buffer)
inlinenoexcept
Parameters
[in]network_io_bufferNetwork I/O buffer

◆ ~tdsl_netimpl_esp_tls()

tdsl::net::tdsl_netimpl_esp_tls::~tdsl_netimpl_esp_tls ( )
inlinenoexcept

◆ tdsl_netimpl_esp_tls() [3/3]

tdsl::net::tdsl_netimpl_esp_tls::tdsl_netimpl_esp_tls ( const tdsl_netimpl_esp_tls &  )
delete

Member Function Documentation

◆ operator=()

tdsl_netimpl_esp_tls & tdsl::net::tdsl_netimpl_esp_tls::operator= ( const tdsl_netimpl_esp_tls &  )
delete

◆ set_tls_options()

void tdsl::net::tdsl_netimpl_esp_tls::set_tls_options ( const tdsl_esp_tls_options &  o)
inlinenoexcept

Set the TLS settings for the following connections.

◆ set_socket_timeouts()

void tdsl::net::tdsl_netimpl_esp_tls::set_socket_timeouts ( tdsl::uint32_t  connect_ms,
tdsl::uint32_t  io_ms 
)
inlinenoexcept

Set timeouts.

Parameters
[in]connect_msTimeout for each connection attempt, TLS handshake included (milliseconds)
[in]io_msTimeout for send and receive operations (milliseconds). A receive fails if no data arrives for this long.

◆ is_connected()

bool tdsl::net::tdsl_netimpl_esp_tls::is_connected ( ) const
inlinenoexcept

◆ peer_closed()

bool tdsl::net::tdsl_netimpl_esp_tls::peer_closed ( ) const
inlinenoexcept

Whether the server has closed the connection (or reset it, or sent data nobody asked for, e.g.

a TLS close_notify: TDS is request/response, so an idle connection has nothing to read). Doesn't block. Call it between requests, e.g. to reconnect before sending instead of losing the request to a connection the server ended while it was idle.

Returns
true if not connected, or the connection can't be used

◆ last_connect_error()

e_socket_error tdsl::net::tdsl_netimpl_esp_tls::last_connect_error ( ) const
inlinenoexcept

Why the last do_connect() failed; e_socket_error{} (0) after a successful one.

◆ last_mbedtls_error()

int tdsl::net::tdsl_netimpl_esp_tls::last_mbedtls_error ( ) const
inlinenoexcept

Of the last failed connection attempt: mbedTLS' error code (negative, e.g.

-0x2700 when the certificate was rejected), or 0

◆ last_verify_flags()

int tdsl::net::tdsl_netimpl_esp_tls::last_verify_flags ( ) const
inlinenoexcept

Of the last failed connection attempt: mbedTLS' certificate verification flags (MBEDTLS_X509_BADCERT_*), or 0.

◆ do_connect()

template<typename T >
tdsl::expected< tdsl::traits::true_type, int > tdsl::net::tdsl_netimpl_esp_tls::do_connect ( T  target,
tdsl::uint16_t  port 
)
inlinenoexcept

Connect to target : port and do the TLS handshake.

An existing connection is closed first.

The whole procedure is retried conn_retry_count times, with conn_retry_delay_ms milliseconds between the attempts. TLS setup and certificate errors are not retried.

Returns
true_type if connected, e_socket_error otherwise

◆ do_disconnect()

tdsl::int32_t tdsl::net::tdsl_netimpl_esp_tls::do_disconnect ( )
inlinenoexcept

Close the connection, if any.

No TLS close_notify is sent: SQL Server doesn't need one.

Returns
0

◆ do_send()

tdsl::int32_t tdsl::net::tdsl_netimpl_esp_tls::do_send ( byte_view  header,
byte_view  message 
)
inlinenoexcept

Send a TDS packet (header and message) to the server, in one TLS record.

SQL Server closes a strict connection when a TDS packet arrives in more than one TLS record. network_io_base puts the header directly in front of the message, so the packet goes out in one write.

Returns
0 on success, e_socket_error otherwise. The connection is closed if sending fails. A packet larger than a TLS record (k_max_tls_record_payload, i.e. a TDS packet size above it) is not sent (packet_too_large), and the connection stays usable: the first packet of a message is its largest, so nothing of the message has been sent.

◆ do_recv() [1/2]

auto tdsl::net::tdsl_netimpl_esp_tls::do_recv ( tdsl::uint32_t  transfer_exactly,
byte_span  dst_buf 
) -> network_io_result
inlinenoexcept

Read exactly transfer_exactly bytes into dst_buf.

Returns
Amount of bytes read (always transfer_exactly) on success, e_socket_error otherwise. The connection is closed on error.

◆ do_recv() [2/2]

auto tdsl::net::tdsl_netimpl_esp_tls::do_recv ( tdsl::uint32_t  transfer_exactly) -> network_io_result
inlinenoexcept

Read exactly transfer_exactly bytes into the network buffer.

◆ connect()

tdsl::expected< tdsl::traits::true_type, int > tdsl::net::network_io_base< tdsl_netimpl_esp_tls >::connect ( T  host,
tdsl::uint16_t  port 
)
inlinenoexceptinherited

Connect to host: port.

Parameters
[in]hostHostname or IP address
[in]portTCP port number
Returns
int 0 if connection is successful, false otherwise.

◆ abandon_response()

tdsl::uint32_t tdsl::net::network_io_base< tdsl_netimpl_esp_tls >::abandon_response ( tdsl::uint32_t  processed)
inlinenoexceptinherited

Give up on the response currently being received.

The rest of the response (if any) is still on its way, and nothing tells where it ends: the next request would read it as its own response (e.g. the previous query's result, as a success). The only way to keep the stream in step is to close the connection, so that the next request fails instead and the application reconnects.

Parameters
[in]processedNumber of complete TDS messages so far
Returns
processed

◆ is_response_too_large()

bool tdsl::net::network_io_base< tdsl_netimpl_esp_tls >::is_response_too_large ( ) const
inlinenoexceptinherited

Whether the last response had an item (e.g.

a row, the column metadata or a message) larger than the network buffer. Such a response is given up on, and the connection closed (see abandon_response()).

◆ do_receive_tds_pdu()

tdsl::uint32_t tdsl::net::network_io_base< tdsl_netimpl_esp_tls >::do_receive_tds_pdu ( )
inlinenoexceptinherited

Receive one, complete TDS PDU.

TDS packet data can span multiple TDS messages. In such scenarios, each TDS message will have its EOM flag set to false except the last TDS message for the packet data.

The amount of successfully read TDS messages. (for diagnostic purposes only)

The main TDS message receive loop.

The loop is mildly stream-based, meaning it will not wait for a whole TDS packet data to arrive before delivering the data down to parsing, but it waits until we have a complete TDS message before doing so.

The downstream parsing code immediately parses the tokens and fires the callback functions, which allows us to discard the processed data from receive buffer (e.g. a processed ROW token's data)

◆ do_send_tds_pdu()

tdsl::detail::e_send_status tdsl::net::network_io_base< tdsl_netimpl_esp_tls >::do_send_tds_pdu ( tdsl::detail::e_tds_message_type  mtype)
inlinenoexceptinherited

Send contents of the message buffer in one or more TDS PDU's, depending on negotiated packet size.

If any part of the message could not be written into the network buffer (i.e. the message is larger than the buffer), nothing is sent and the message is discarded.

Parameters
[in]mtypeThe type of the message currently in the network buffer

If sending a packet fails, the rest of the message is discarded.

Returns
e_send_status::success if the message is sent
e_send_status::request_too_large if the message was incomplete and discarded (nothing is sent)
e_send_status::network_error if sending failed

◆ discard_tx()

void tdsl::net::network_io_base< tdsl_netimpl_esp_tls >::discard_tx ( )
inlinenoexceptinherited

Discard the message being built in the network buffer, including its overflow state.

◆ do_write() [1/2]

void tdsl::net::network_io_base< tdsl_netimpl_esp_tls >::do_write ( tdsl::span< T >  data)
inlinenoexceptinherited

Append data to network buffer.

Parameters
[in]dataData to append

◆ do_write() [2/2]

void tdsl::net::network_io_base< tdsl_netimpl_esp_tls >::do_write ( tdsl::size_t  offset,
tdsl::span< T >  data 
)
inlinenoexceptinherited

Append data to network buffer, starting from offset.

Parameters
[in]offsetOffset to start from
[in]dataData to append

◆ do_get_write_offset()

tdsl::size_t tdsl::net::network_io_base< tdsl_netimpl_esp_tls >::do_get_write_offset ( )
inlinenoexceptinherited

Get current write offset.

◆ register_packet_data_callback()

void tdsl::net::network_io_base< tdsl_netimpl_esp_tls >::register_packet_data_callback ( tds_packet_data_callback::function_type  cb,
void *  user_ptr 
)
inlinenoexceptinherited

Set TDS packet data callback.

Parameters
[in]cbNew callback
[in]user_ptruptr

◆ set_tds_packet_size()

void tdsl::net::network_io_base< tdsl_netimpl_esp_tls >::set_tds_packet_size ( tdsl::uint16_t  value)
inlinenoexceptinherited

Update negotiated packet size.

The packet size may be larger than the network buffer. Incoming packets are received in chunks, and outgoing messages are never larger than the network buffer anyway.

Parameters
[in]valueNew packet size. Values outside of the [512, 32767] range are ignored and the current packet size is kept.

◆ set_connection_timeout_params()

void tdsl::net::network_io_base< tdsl_netimpl_esp_tls >::set_connection_timeout_params ( tdsl::uint16_t  attempts,
tdsl::uint16_t  delay_ms 
)
inlinenoexceptinherited

Set the conn retry count.

Parameters
attemptsNumber of attempts
delay_msDelay between each attempt (millseconds)

◆ is_strict_encryption()

bool tdsl::net::network_io_base< tdsl_netimpl_esp_tls >::is_strict_encryption ( ) const
inlinenoexceptinherited

Whether the current connection is a TDS 8.0 "strict" one (TLS from the first byte).

The server then uses the TDS 7.2+ message framing (ALL_HEADERS, 8-byte DONE row counts), even though tdslite logs in as TDS 7.1.

◆ as_packet()

static byte_view tdsl::net::network_io_base< tdsl_netimpl_esp_tls >::as_packet ( byte_view  header,
byte_view  message 
)
inlinestaticprotectednoexceptinherited

The whole TDS packet given to do_send(header, message).

do_send_tds_pdu() always puts the header directly in front of the message in memory, so a network implementation can send a packet with a single write.

Returns
An empty view when the header isn't directly in front of the message (do_send() called by other code with two separate buffers): the caller must not send then.

Member Data Documentation

◆ k_min_tds_packet_size

constexpr tdsl::uint16_t tdsl::net::network_io_base< tdsl_netimpl_esp_tls >::k_min_tds_packet_size
staticconstexprinherited

Smallest/largest valid TDS packet size (MS-TDS 2.2.7.9)

◆ k_max_tds_packet_size

constexpr tdsl::uint16_t tdsl::net::network_io_base< tdsl_netimpl_esp_tls >::k_max_tds_packet_size
staticconstexprinherited

◆ strict_encryption

bool tdsl::net::network_io_base< tdsl_netimpl_esp_tls >::strict_encryption
protectedinherited

◆ response_too_large

bool tdsl::net::network_io_base< tdsl_netimpl_esp_tls >::response_too_large
protectedinherited

◆ conn_retry_count

tdsl::uint16_t tdsl::net::network_io_base< tdsl_netimpl_esp_tls >::conn_retry_count
protectedinherited

◆ conn_retry_delay_ms

tdsl::uint16_t tdsl::net::network_io_base< tdsl_netimpl_esp_tls >::conn_retry_delay_ms
protectedinherited

◆ network_buffer

tdsl_buffer_object tdsl::net::network_io_base< tdsl_netimpl_esp_tls >::network_buffer
protectedinherited

The documentation for this struct was generated from the following file: