ddn.net.dtls.engine

DTLS engine for client and server roles.

Provides the main DtlsEngine class that manages DTLS connections over a datagram transport. Supports both client and server modes, multiple concurrent peers, and configurable timeouts.

The engine handles:

  • DTLS handshake state machine
  • Peer connection tracking
  • Retransmission timers
  • Idle peer pruning
  • MTU configuration

Types 1

Main DTLS engine for client and server roles.

The engine manages DTLS connections over a single datagram socket. In server mode, it can handle multiple peers simultaneously. In client mode, it connects to a single server.

Thread safety: NOT thread-safe. All calls must be serialized.

Example (client):

auto engine = new NativeDtlsEngine(transport, timer, "example.com");
engine.setMinVersion(DtlsVersion.DTLS_1_2);
while (engine.handshake() != TlsProgress.DONE) {
   // poll and handle I/O
}

Example (server):

auto engine = new NativeDtlsEngine(transport, timer);
engine.setCertificateChain(certs);
engine.setPrivateKey(key);
// Process incoming datagrams in event loop

Methods
DtlsCidManager cidManager() @safe pure nothrow @nogcReturns: The CID manager (package-internal access for NativeDtlsPeer).
void setCertificateChain(const(char)[] pemData)Set the certificate chain for authentication from PEM data.
void setCertificateChainDer(const(ubyte[])[] derCerts) @safeSet the certificate chain for authentication from DER data.
void setCertificateChain(const(ubyte[])[] derCerts) @safeBackward-compatible DER setter alias.
void setPrivateKey(const(char)[] pemKey, const(char)[] password = null)Set the private key for authentication from PEM data.
void setPrivateKeyDer(const(ubyte)[] derKey, const(char)[] password = null)Set the private key for authentication from DER data.
void setMinVersion(DtlsVersion ver) @safe pure nothrow @nogcSet the minimum DTLS version to accept.
void setMaxVersion(DtlsVersion ver) @safe pure nothrow @nogcSet the maximum DTLS version to offer.
void setMtu(size_t mtu) @safe pure nothrow @nogcSet the path MTU for datagram fragmentation.
void setAlpnProtocols(string[] protocols) @safe pure nothrowSet the ALPN protocol list.
void setTrustStore(TlsTrustStore trustStore) @safeSets the trust store for certificate verification.
void setVerifyCallback(DtlsVerifyCallback callback) @safe pure nothrowSets a custom certificate verification callback.
void setVerifyMode(TlsVerifyMode mode) @safe pure nothrow @nogcSets the certificate verification mode.
void setServerAddress(DatagramEndpoint addr) @safe pure nothrow @nogcSets the target server address for client mode.
TlsProgress handshake() @safePerform or continue the DTLS handshake.
TlsProgress processDatagram(const(ubyte)[] data, DatagramEndpoint from) @safeProcess an incoming datagram.
TlsProgress checkTimers() @safeCheck and fire any pending retransmission timers.
TlsError lastError() @propertyThe typed error for the most recent DTLS engine failure.
TlsProgress flush() @safeFlush any pending outgoing data.
void enablePmtuDiscovery(bool enable) @safeEnables or disables path MTU discovery (RFC 8899 PLPMTUD).
size_t getEffectiveMtu(DatagramEndpoint peer) @safeReturns the effective MTU for a peer.
void setOnMtuChanged(DtlsMtuChangedCallback callback) @safeSets the callback fired when a peer's discovered MTU changes.
void enableConnectionId(bool enable) @safeEnables or disables Connection ID negotiation (RFC 9146).
void setConnectionIdLength(ubyte length) @safeSets the local Connection ID length.
void setOnPeerMigrated(DtlsPeerMigratedCallback callback) @safeSets the callback fired when a peer migrates to a new address.
void enableSessionResumption(bool enable) @safeEnables or disables session resumption (and 0-RTT early data).
void setSessionTicketLifetime(Duration lifetime) @safeSets the lifetime of issued session tickets.
void setMaxEarlyDataSize(size_t bytes) @safeSets the maximum 0-RTT early data size advertised to clients.
void setEarlyDataReplayProtection(EarlyDataReplayProtection mode) @safeSets the anti-replay strategy for 0-RTT early data.
void setEarlyDataTimeWindow(Duration window) @safeSets the replay window duration for TIME_WINDOWED protection.
void setOnSessionTicket(DtlsSessionCallback callback) @safeSets the callback fired when a session ticket is issued.
void setOnEarlyData(DtlsEarlyDataCallback callback) @safeSets the callback fired when early data is received.
void resumeWithSession(scope ref const DtlsSessionInfo session) @safeConfigures resumption with a previously stored session.
TlsResult writeEarlyData(const(ubyte)[] data)Writes 0-RTT early data for a pending client resumption handshake.
void setSrtpProfiles(const(SrtpProfile[]) profiles) @safeSets the DTLS-SRTP profiles to negotiate (RFC 5764).
DtlsPeer getPeer(DatagramEndpoint addr) @safeGet a peer by address.
DtlsPeer[] peers() @safeGet all active peers.
void setHandshakeTimeout(Duration timeout) @safe pure nothrow @nogcSet the handshake timeout.
void setIdleTimeout(Duration timeout) @safe pure nothrow @nogcSet the idle timeout.
void setMaxPeers(size_t limit) @safe pure nothrow @nogcSet the maximum number of peers.
void setMaxHandshakingPeers(size_t limit) @safe pure nothrow @nogcSet the maximum number of peers in handshake state.
size_t peerCount() const @safe pure nothrow @nogcGet the current number of peers.
void evictPeer(DatagramEndpoint addr) @safeEvict a peer by address.
size_t pruneIdlePeers(Duration idleTimeout) @safePrune idle peers.
void setOnPeerEvicted(DtlsEvictCallback callback) @safe pure nothrow @nogcSet the peer eviction callback.
TlsResult readFromPeer(DtlsPeerState state, ubyte[] buffer) @safeRead decrypted data from a peer.
TlsResult writeToPeer(DtlsPeerState state, const(ubyte)[] data) @safeWrite data to a peer.
TlsProgress shutdownPeer(DtlsPeerState state) @safeShutdown a peer connection.
DtlsVersion negotiatedVersion(const DtlsPeerState state) const @safeGet the negotiated DTLS version for a peer.
string cipherSuite(const DtlsPeerState state) const @safeGet the negotiated cipher suite for a peer.
string alpnProtocol(const DtlsPeerState state) const @safeGet the negotiated ALPN protocol for a peer.
bool isPeerConnected(const DtlsPeerState state) const @safeCheck if a peer's handshake is complete.
TlsCertificateChain peerCertificates(DtlsPeerState state) @safeGet the peer's certificate chain.
bool isServer() @property const @safe pure nothrow @nogcCheck if this is a server engine.
string serverName() @property const @safe pure nothrow @nogcGet the server name (SNI) for client engines.
size_t mtu() @property const @safe pure nothrow @nogcGet the configured MTU.
DtlsVersion minVersion() @property const @safe pure nothrow @nogcGet the minimum DTLS version.
DtlsVersion maxVersion() @property const @safe pure nothrow @nogcGet the maximum DTLS version.
DtlsTransport transport() @property @safe pure nothrow @nogcGet the transport.
TlsProgress sendAlert(DtlsPeerState state, AlertLevel level, AlertDescription description) @safeBuild and send a DTLS alert record to a peer.
Constructors
this(DtlsTransport transport, DtlsTimerSink timer, string serverName)Create a client engine.
this(DtlsTransport transport, DtlsTimerSink timer)Create a server engine.