miTLS Thread-Local Storage Logging
@@ -364,6 +364,8 @@ typedef struct QUIC_TLS {
} QUIC_TLS;
+DWORD miTlsCurrentConnectionIndex = TLS_OUT_OF_INDEXES; // Thread-local storage index
+
//
// Callback from mitls for logging purposes.
//
@@ -376,7 +378,7 @@ MiTlsTraceCallback(
QuicTraceEvent(
TlsMessage,
"[ tls][%p] %s",
- NULL, // TODO - Save connection in thread-local storage and retrieve it?
+ TlsGetValue(miTlsCurrentConnectionIndex),
Msg);
}
@@ -387,6 +389,8 @@ QuicTlsLibraryInitialize(
{
QUIC_STATUS Status;
+ miTlsCurrentConnectionIndex = TlsAlloc();
+
QuicTraceLogVerbose(
miTlsInitialize,
"[ tls] Initializing miTLS library");
@@ -429,6 +433,10 @@ QuicTlsLibraryInitialize(
Error:
+ if (QUIC_FAILED(Status)) {
+ TlsFree(miTlsCurrentConnectionIndex);
+ }
+
return Status;
}
@@ -441,6 +449,7 @@ QuicTlsLibraryUninitialize(
miTlsUninitialize,
"[ tls] Cleaning up miTLS library");
FFI_mitls_cleanup();
+ TlsFree(miTlsCurrentConnectionIndex);
}
_IRQL_requires_max_(PASSIVE_LEVEL)
@@ -911,6 +920,8 @@ QuicTlsInitialize(
QUIC_DBG_ASSERT(NewTlsContext != NULL);
UNREFERENCED_PARAMETER(State);
+ TlsSetValue(miTlsCurrentConnectionIndex, Config->Connection);
+
TlsContext = QUIC_ALLOC_PAGED(sizeof(QUIC_TLS) + sizeof(uint16_t) + Config->AlpnBufferLength);
if (TlsContext == NULL) {
QuicTraceEvent(
@@ -1112,6 +1123,8 @@ QuicTlsReset(
{
QUIC_DBG_ASSERT(TlsContext->IsServer == FALSE);
+ TlsSetValue(miTlsCurrentConnectionIndex, TlsContext->Connection);
+
TlsContext->BufferLength = 0;
TlsContext->CurrentReaderKey = -1;
TlsContext->CurrentWriterKey = -1;
@@ -1161,6 +1174,8 @@ QuicTlsProcessData(
QUIC_DBG_ASSERT(Buffer != NULL || *BufferLength == 0);
+ TlsSetValue(miTlsCurrentConnectionIndex, TlsContext->Connection);
+
//
// Validate buffer lengths.
//
@@ -1255,6 +1270,8 @@ QuicTlsProcessDataComplete(
QUIC_DBG_ASSERT(TlsContext->State->HandshakeComplete || TlsContext->Buffer != NULL);
}
+ TlsSetValue(miTlsCurrentConnectionIndex, TlsContext->Connection);
+
uint32_t BufferOffset = 0;
while (!(ResultFlags & QUIC_TLS_RESULT_ERROR)) {
