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
class NativeDtlsEngine
Types 1
classNativeDtlsEngine : DtlsEngine
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 loopFields
private ubyte[2] DTLS_RECORD_VERSIONDtlsTransport transport_DtlsTimerSink timer_bool isServer_string serverName_DatagramEndpoint serverAddr_Dtls12Client client12_Dtls13Client client13_bool handshakeStarted_bool useDtls13_DtlsPeerState[DatagramEndpoint] peers_size_t maxPeers_size_t maxHandshakingPeers_Duration handshakeTimeout_Duration idleTimeout_void delegate(DatagramEndpoint, DtlsEvictReason) @safe onPeerEvicted_DtlsVersion minVersion_DtlsVersion maxVersion_size_t mtu_string[] alpnProtocols_const(ubyte)[][] certChainDer_string privateKeyPem_NativeTlsTrustStore trustStore_TlsVerifyMode verifyMode_DtlsVerifyCallback verifyCallback_ubyte[] cookieSecret_DtlsPmtudEngine pmtudEngine_PmtudPeerState[DatagramEndpoint] pmtudStates_DtlsMtuChangedCallback onMtuChanged_DtlsCidManager cidManager_DtlsPeerMigratedCallback onPeerMigrated_SrtpProfile[] srtpProfiles_bool sessionResumptionEnabled_Duration sessionTicketLifetime_size_t maxEarlyDataSize_EarlyDataReplayProtection earlyDataReplayMode_Duration earlyDataTimeWindow_DtlsSessionCallback onSessionTicket_DtlsEarlyDataCallback onEarlyData_DtlsSessionInfo resumeSession_bool hasResumeSession_DtlsTicketKeyManager ticketKeyMgr_DtlsReplayCache replayCache_Methods
DtlsCidManager cidManager() @safe pure nothrow @nogcReturns: The CID manager (package-internal access for NativeDtlsPeer).void setCertificateChain(const(ubyte[])[] derCerts) @safeSet the certificate chain for authentication.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 processDatagram(const(ubyte)[] data, DatagramEndpoint from) @safeProcess an incoming datagram.void enablePmtuDiscovery(bool enable) @safesize_t getEffectiveMtu(DatagramEndpoint peer) @safevoid setOnMtuChanged(DtlsMtuChangedCallback callback) @safevoid enableConnectionId(bool enable) @safevoid setConnectionIdLength(ubyte length) @safevoid setOnPeerMigrated(DtlsPeerMigratedCallback callback) @safevoid enableSessionResumption(bool enable) @safevoid setSessionTicketLifetime(Duration lifetime) @safevoid setMaxEarlyDataSize(size_t bytes) @safevoid setEarlyDataReplayProtection(EarlyDataReplayProtection mode) @safevoid setEarlyDataTimeWindow(Duration window) @safevoid setOnSessionTicket(DtlsSessionCallback callback) @safevoid setOnEarlyData(DtlsEarlyDataCallback callback) @safevoid resumeWithSession(scope ref const DtlsSessionInfo session) @safevoid setSrtpProfiles(const(SrtpProfile[]) profiles) @safevoid setMaxHandshakingPeers(size_t limit) @safe pure nothrow @nogcSet the maximum number of peers in handshake state.void setOnPeerEvicted(DtlsEvictCallback callback) @safe pure nothrow @nogcSet the peer eviction callback.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.string serverName() @property const @safe pure nothrow @nogcGet the server name (SNI) for client engines.private
ubyte[] encryptAppData(DtlsPeerState state, TlsAead aead,
const(ubyte)[] plaintext)Encrypt application data into a DTLS record.private
ubyte[] encryptAppData12(DtlsPeerState state, TlsAead aead,
const(ubyte)[] plaintext)Encrypt application data using DTLS 1.2 format (explicit nonce).private
ubyte[] encryptAppData13(DtlsPeerState state, TlsAead aead,
const(ubyte)[] plaintext)Encrypt application data using DTLS 1.3 format (XOR nonce).private
ubyte[] decryptAppDataRecord(DtlsPeerState state,
const(ubyte)[] payload, DtlsRecordHeader header)Decrypt an APPLICATION_DATA record payload.private
ubyte[] decryptAppData12(TlsAead aead, const(ubyte)[] payload,
DtlsRecordHeader header)Decrypt a DTLS 1.2 APPLICATION_DATA record (explicit nonce).private
ubyte[] decryptAppData13(TlsAead aead, const(ubyte)[] payload,
DtlsRecordHeader header)Decrypt a DTLS 1.3 APPLICATION_DATA record (XOR nonce).private
ubyte[] buildDtlsRecord(ContentType type, ushort epoch,
ulong seq, const(ubyte)[] payload) @safe pure nothrowBuild a complete DTLS record with the given parameters.TlsProgress sendAlert(DtlsPeerState state, AlertLevel level,
AlertDescription description) @safeBuild and send a DTLS alert record to a peer.private
ubyte[] encryptAlertRecord(DtlsPeerState state, TlsAead aead,
const(ubyte)[] alertPayload)Encrypt an alert record payload using the peer's write AEAD.private
TlsProgress processAppDataDatagram(DtlsPeerState state,
DatagramEndpoint from, const(ubyte)[] data) @safeProcess a datagram containing APPLICATION_DATA for a connected peer.private
void handleAlertRecord(DtlsPeerState state,
ref DtlsRecord rec) @safeHandle a received ALERT record for a connected peer.private
string cipherSuiteName(CipherSuiteId id) @safe pure nothrow Converts a CipherSuiteId to its IANA name string. Params: id = The cipher suite identifier. Returns: * The IANA cipher suite name.Constructors
this(DtlsTransport transport, DtlsTimerSink timer, string serverName)Create a client engine.this(DtlsTransport transport, DtlsTimerSink timer)Create a server engine.