Net: add TLS client stream and SMTP submission (closes #48)

This commit is contained in:
faumaray committed 2026-12-03 16:37:13 +03:00
1 parent 72148da7b2
commit 44686a4444
8 files changed
+2787 -1

No files matched your search

+11 -1
View File
@@ -31,7 +31,10 @@ namespace Void::Net
io_error = 9,
redirect_blocked = 10,
payload_too_large = 11,
headers_too_large = 12
headers_too_large = 12,
smtp_rejected = 13,
auth_failed = 14,
tls_required = 15
};
[[nodiscard]] inline Void::Result::Error
@@ -71,6 +74,13 @@ namespace Void::Net
case NetErrorCode::headers_too_large:
kind = ErrorKind::exhausted;
break;
case NetErrorCode::smtp_rejected:
case NetErrorCode::auth_failed:
kind = ErrorKind::conflict;
break;
case NetErrorCode::tls_required:
kind = ErrorKind::invalid_argument;
break;
}
return Error{kind, std::move(message),
ErrorCode{Void::Result::kNetErrorDomain,
+112
View File
@@ -0,0 +1,112 @@
#pragma once
#include <Void/Functional/Error.hpp>
#include <Void/Functional/Result.hpp>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <optional>
#include <stop_token>
#include <string>
#include <string_view>
#include <vector>
namespace Void::Net
{
enum class SmtpSecurity {
/// TLS from the first byte (typically port 465).
implicit_tls,
/// Plaintext greeting/EHLO, then a required STARTTLS upgrade
/// (typically port 587 or 25).
starttls_required,
};
struct SmtpCredentials {
std::string username{};
std::string password{};
};
struct SmtpLimits {
/// Maximum raw message body accepted by `send()`.
std::uint64_t max_message_bytes{10ULL * 1024ULL * 1024ULL};
/// Maximum bytes in one complete (possibly multi-line) reply.
std::uint64_t max_response_bytes{64ULL * 1024ULL};
/// Maximum bytes in one reply line.
std::size_t max_line_bytes{4096};
/// Maximum recipients accepted by `send()`.
std::size_t max_recipients{100};
};
struct SmtpOptions {
std::string host{};
std::uint16_t port{587};
SmtpSecurity security{SmtpSecurity::starttls_required};
/// Identity verified against the server certificate; defaults
/// to `host`. Peer and hostname verification are always on.
std::string server_name{};
/// PEM trust store; empty uses the system default store.
std::string ca_file{};
/// Credentials are sent with AUTH PLAIN and only over TLS.
std::optional<SmtpCredentials> credentials{};
/// Name announced in EHLO.
std::string client_domain{"localhost"};
std::chrono::milliseconds connect_timeout{
std::chrono::seconds{10}};
std::chrono::milliseconds io_timeout{std::chrono::seconds{30}};
SmtpLimits limits{};
std::stop_token stop{};
};
struct SmtpMessage {
std::string from{};
std::vector<std::string> recipients{};
std::string data{};
};
/// Synchronous SMTP submission client over a verified TLS connection.
///
/// Supports implicit TLS and STARTTLS-required dialogue, AUTH PLAIN
/// (TLS only), MAIL/RCPT/DATA with CRLF normalization and dot-stuffing,
/// bounded replies, bounded message size, deadlines, and cancellation.
/// A failed send invalidates the client.
class SmtpClient final
{
public:
SmtpClient() noexcept;
~SmtpClient() noexcept;
SmtpClient(SmtpClient&&) noexcept;
SmtpClient& operator=(SmtpClient&&) noexcept;
SmtpClient(const SmtpClient&) = delete;
SmtpClient& operator=(const SmtpClient&) = delete;
/// Connect, greet, negotiate TLS (when required), authenticate
/// (when credentials are configured), and prepare submission.
[[nodiscard]] static Void::Result::Result<SmtpClient>
connect(SmtpOptions options);
/// Convenience: connect, send one message, then QUIT.
[[nodiscard]] static Void::Result::Result<void>
submit(SmtpOptions options, const SmtpMessage& message);
[[nodiscard]] bool valid() const noexcept;
[[nodiscard]] bool secure() const noexcept;
[[nodiscard]] Void::Result::Result<void>
send(const SmtpMessage& message);
/// Send QUIT (best effort) and close the connection.
[[nodiscard]] Void::Result::Result<void> quit();
void close() noexcept;
private:
struct Impl;
std::unique_ptr<Impl> m_impl;
explicit SmtpClient(std::unique_ptr<Impl> impl) noexcept;
};
} // namespace Void::Net
+117
View File
@@ -0,0 +1,117 @@
#pragma once
#include <Void/Functional/Error.hpp>
#include <Void/Functional/Result.hpp>
#include <chrono>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <span>
#include <stop_token>
#include <string>
#include <string_view>
namespace Void::Net
{
/// Options for a verified TLS client stream.
///
/// Peer and hostname verification are always enabled; there is no
/// insecure mode and no fallback to plaintext when the handshake fails.
/// `server_name` is the identity checked against the peer certificate
/// (and sent as SNI when it is a DNS name); it defaults to `host`.
/// `ca_file` selects an explicit PEM trust store; when empty the system
/// default trust store is used.
struct TlsClientOptions {
std::string host{};
std::uint16_t port{443};
std::string server_name{};
std::string ca_file{};
std::chrono::milliseconds connect_timeout{
std::chrono::seconds{10}};
std::chrono::milliseconds handshake_timeout{
std::chrono::seconds{10}};
std::chrono::milliseconds io_timeout{std::chrono::seconds{30}};
std::stop_token stop{};
};
/// Synchronous TLS client stream over a TCP socket (POSIX, OpenSSL).
///
/// `connect()` establishes TCP and completes a verified TLS handshake.
/// `connect_plain()` establishes TCP only, for protocols that negotiate
/// TLS in-band (SMTP STARTTLS, IMAP STARTTLS, ...); call `start_tls()`
/// after the plaintext negotiation. A failed `start_tls()` closes the
/// stream, so a caller can never continue on an unauthenticated channel.
///
/// Reads and writes accept an optional per-operation timeout (zero uses
/// `TlsClientOptions::io_timeout`) and an optional stop token (empty uses
/// `TlsClientOptions::stop`). Timeouts and cancellation interrupt the
/// operation without closing the stream.
class TlsClientStream final
{
public:
TlsClientStream() noexcept;
~TlsClientStream() noexcept;
TlsClientStream(TlsClientStream&&) noexcept;
TlsClientStream& operator=(TlsClientStream&&) noexcept;
TlsClientStream(const TlsClientStream&) = delete;
TlsClientStream& operator=(const TlsClientStream&) = delete;
/// Resolve, connect, and complete a verified TLS handshake.
[[nodiscard]] static Void::Result::Result<TlsClientStream>
connect(TlsClientOptions options);
/// Connect TCP without starting TLS (STARTTLS plaintext phase).
[[nodiscard]] static Void::Result::Result<TlsClientStream>
connect_plain(TlsClientOptions options);
/// Take ownership of an already connected socket and start TLS
/// immediately. `native_socket` must be a connected TCP socket.
[[nodiscard]] static Void::Result::Result<TlsClientStream>
adopt(int native_socket, TlsClientOptions options);
/// Complete the verified TLS handshake on a stream created by
/// `connect_plain()` (or a plaintext `adopt()`). On failure the
/// stream is closed and `valid()` becomes false.
[[nodiscard]] Void::Result::Result<void> start_tls();
[[nodiscard]] bool valid() const noexcept;
[[nodiscard]] bool secure() const noexcept;
[[nodiscard]] int native_handle() const noexcept;
/// Read up to `destination.size()` bytes. Returns 0 on orderly EOF.
[[nodiscard]] Void::Result::Result<std::size_t>
read(std::span<std::byte> destination,
std::chrono::milliseconds timeout = {},
std::stop_token stop = {});
/// Write up to `source.size()` bytes (a positive result may be a
/// partial write).
[[nodiscard]] Void::Result::Result<std::size_t>
write(std::span<const std::byte> source,
std::chrono::milliseconds timeout = {},
std::stop_token stop = {});
/// Write every byte, retrying partial writes.
[[nodiscard]] Void::Result::Result<void>
write_all(std::span<const std::byte> source,
std::chrono::milliseconds timeout = {},
std::stop_token stop = {});
[[nodiscard]] Void::Result::Result<void>
write_all(std::string_view text,
std::chrono::milliseconds timeout = {},
std::stop_token stop = {});
/// Send TLS close_notify (best effort) and close the socket.
[[nodiscard]] Void::Result::Result<void> shutdown();
void close() noexcept;
private:
struct Impl;
std::unique_ptr<Impl> m_impl;
explicit TlsClientStream(std::unique_ptr<Impl> impl) noexcept;
};
} // namespace Void::Net