From 1f4f3185bd592e791e1ab91b5747be307756074b Mon Sep 17 00:00:00 2001 From: Bryan Roe Date: Tue, 29 Jan 2019 22:41:35 -0800 Subject: [PATCH] Updated JS WebRTC debug hooks --- microscript/ILibDuktape_WebRTC.c | 75 +++++++++++++++++++++----------- microstack/ILibWebRTC.c | 6 ++- microstack/ILibWebRTC.h | 2 + microstack/ILibWrapperWebRTC.c | 5 ++- microstack/ILibWrapperWebRTC.h | 1 + 5 files changed, 62 insertions(+), 27 deletions(-) diff --git a/microscript/ILibDuktape_WebRTC.c b/microscript/ILibDuktape_WebRTC.c index 2ecf9c4..527df8a 100644 --- a/microscript/ILibDuktape_WebRTC.c +++ b/microscript/ILibDuktape_WebRTC.c @@ -37,12 +37,16 @@ limitations under the License. #define ILibDuktape_WebRTC_ConnectionPtr "\xFF_WebRTC_ConnectionPtr" #define ILibDuktape_WebRTC_DataChannelPtr "\xFF_WebRTC_DataChannelPtr" extern void ILibWrapper_WebRTC_ConnectionFactory_RemoveFromChain(ILibWrapper_WebRTC_ConnectionFactory factory); +int ILibDuktape_WebRTC_SSL2Duktape = -1; typedef struct ILibWebRTC_Duktape_Handlers { duk_context *ctx; void *ConnectionObject; ILibDuktape_EventEmitter *emitter; +#ifdef _WEBRTCDEBUG + int debugRegistered; +#endif void *OnConnectionSendOK; }ILibWebRTC_Duktape_Handlers; @@ -233,9 +237,10 @@ duk_ret_t ILibDuktape_WebRTC_ConnectionFactory_Finalizer(duk_context *ctx) #ifdef _WEBRTCDEBUG void ILibDuktape_WebRTC_Connection_Debug(void* dtlsSession, char* debugField, int data) { - ILibHashtable *t = ILibChain_GetBaseHashtable(((ILibTransport*)dtlsSession)->ChainLink.ParentChain); - ILibWebRTC_Duktape_Handlers *ptrs = (ILibWebRTC_Duktape_Handlers*)ILibHashtable_Get(t, dtlsSession, NULL, 0); - if (ptrs != NULL) + SSL *ssl = (SSL*)ILibWrapper_WebRTC_DtlsSessionToSSL(dtlsSession); + ILibWebRTC_Duktape_Handlers *ptrs = (ILibWebRTC_Duktape_Handlers*)SSL_get_ex_data(ssl, ILibDuktape_WebRTC_SSL2Duktape); + + if (ptrs != NULL && ptrs->ConnectionObject != NULL) { if (strcmp(debugField, "OnHold") == 0) { @@ -314,23 +319,7 @@ void ILibDuktape_WebRTC_OnConnection(ILibWrapper_WebRTC_Connection connection, i if (duk_pcall_method(ptrs->ctx, 1) != 0) { ILibDuktape_Process_UncaughtException(ptrs->ctx); } duk_pop(ptrs->ctx); // ... #ifdef _WEBRTCDEBUG - ILibHashtable *t = ILibChain_GetBaseHashtable(Duktape_GetChain(ptrs->ctx)); - - if (connected != 0) - { - ILibSCTP_Debug_SetDebugCallback(ILibWrapper_WebRTC_Connection2DtlsSession(connection), "OnHold", ILibDuktape_WebRTC_Connection_Debug); - ILibSCTP_Debug_SetDebugCallback(ILibWrapper_WebRTC_Connection2DtlsSession(connection), "OnCongestionWindowSizeChanged", ILibDuktape_WebRTC_Connection_Debug); - ILibSCTP_Debug_SetDebugCallback(ILibWrapper_WebRTC_Connection2DtlsSession(connection), "OnRTTCalculated", ILibDuktape_WebRTC_Connection_Debug); - ILibSCTP_Debug_SetDebugCallback(ILibWrapper_WebRTC_Connection2DtlsSession(connection), "OnFastRecovery", ILibDuktape_WebRTC_Connection_Debug); - ILibSCTP_Debug_SetDebugCallback(ILibWrapper_WebRTC_Connection2DtlsSession(connection), "OnReceiverCredits", ILibDuktape_WebRTC_Connection_Debug); - ILibSCTP_Debug_SetDebugCallback(ILibWrapper_WebRTC_Connection2DtlsSession(connection), "OnT3RTX", ILibDuktape_WebRTC_Connection_Debug); - ILibSCTP_Debug_SetDebugCallback(ILibWrapper_WebRTC_Connection2DtlsSession(connection), "OnSendRetry", ILibDuktape_WebRTC_Connection_Debug); - ILibSCTP_Debug_SetDebugCallback(ILibWrapper_WebRTC_Connection2DtlsSession(connection), "OnSendFastRetry", ILibDuktape_WebRTC_Connection_Debug); - ILibSCTP_Debug_SetDebugCallback(ILibWrapper_WebRTC_Connection2DtlsSession(connection), "OnRetryPacket", ILibDuktape_WebRTC_Connection_Debug); - ILibSCTP_Debug_SetDebugCallback(ILibWrapper_WebRTC_Connection2DtlsSession(connection), "OnSACKReceived", ILibDuktape_WebRTC_Connection_Debug); - ILibHashtable_Put(t, ILibWrapper_WebRTC_Connection2DtlsSession(connection), NULL, 0, ptrs); - } - else + if (connected == 0 && ptrs->debugRegistered != 0) { ILibSCTP_Debug_SetDebugCallback(ILibWrapper_WebRTC_Connection2DtlsSession(connection), "OnHold", NULL); ILibSCTP_Debug_SetDebugCallback(ILibWrapper_WebRTC_Connection2DtlsSession(connection), "OnCongestionWindowSizeChanged", NULL); @@ -342,7 +331,6 @@ void ILibDuktape_WebRTC_OnConnection(ILibWrapper_WebRTC_Connection connection, i ILibSCTP_Debug_SetDebugCallback(ILibWrapper_WebRTC_Connection2DtlsSession(connection), "OnSendFastRetry", NULL); ILibSCTP_Debug_SetDebugCallback(ILibWrapper_WebRTC_Connection2DtlsSession(connection), "OnRetryPacket", NULL); ILibSCTP_Debug_SetDebugCallback(ILibWrapper_WebRTC_Connection2DtlsSession(connection), "OnSACKReceived", NULL); - ILibHashtable_Remove(t, ILibWrapper_WebRTC_Connection2DtlsSession(connection), NULL, 0); } #endif @@ -478,10 +466,6 @@ duk_ret_t ILibDuktape_WebRTC_Connection_Finalizer(duk_context *ctx) //printf("WebRTCConnection Finalizer on %p\n", (void*)connection); if (connection == NULL) { return 0; } -#ifdef _WEBRTCDEBUG - ILibHashtable_Remove(ILibChain_GetBaseHashtable(Duktape_GetChain(ctx)), ILibWrapper_WebRTC_Connection2DtlsSession(connection), NULL, 0); -#endif - if (ILibWrapper_WebRTC_Connection_IsConnected(connection) != 0) { ILibWrapper_WebRTC_Connection_CloseAllDataChannels(connection); @@ -490,6 +474,34 @@ duk_ret_t ILibDuktape_WebRTC_Connection_Finalizer(duk_context *ctx) return 0; } +#ifdef _WEBRTCDEBUG +void ILibDuktape_WebRTC_Connection_DebugHook(ILibDuktape_EventEmitter *sender, char *eventName, void *hookedCallback) +{ + // Only register the debug handlers if someone actually subscribed to the event + duk_push_heapptr(sender->ctx, sender->object); // [connection] + ILibWrapper_WebRTC_Connection connection = (ILibWrapper_WebRTC_Connection)Duktape_GetPointerProperty(sender->ctx, -1, ILibDuktape_WebRTC_ConnectionPtr); + ILibWebRTC_Duktape_Handlers *ptrs = (ILibWebRTC_Duktape_Handlers*)ILibMemory_Extra(connection); + duk_pop(sender->ctx); // ... + + if (ptrs->debugRegistered == 0) + { + SSL* ssl = ILibWrapper_WebRTC_DtlsSessionToSSL(ILibWrapper_WebRTC_Connection2DtlsSession(connection)); + if (ILibDuktape_WebRTC_SSL2Duktape < 0) { ILibDuktape_WebRTC_SSL2Duktape = SSL_get_ex_new_index(0, "ILibDuktape_WebRTC_SSL2Connection index", NULL, NULL, NULL); } + SSL_set_ex_data(ssl, ILibDuktape_WebRTC_SSL2Duktape, ptrs); + + ILibSCTP_Debug_SetDebugCallback(ILibWrapper_WebRTC_Connection2DtlsSession(connection), "OnHold", ILibDuktape_WebRTC_Connection_Debug); + ILibSCTP_Debug_SetDebugCallback(ILibWrapper_WebRTC_Connection2DtlsSession(connection), "OnCongestionWindowSizeChanged", ILibDuktape_WebRTC_Connection_Debug); + ILibSCTP_Debug_SetDebugCallback(ILibWrapper_WebRTC_Connection2DtlsSession(connection), "OnRTTCalculated", ILibDuktape_WebRTC_Connection_Debug); + ILibSCTP_Debug_SetDebugCallback(ILibWrapper_WebRTC_Connection2DtlsSession(connection), "OnFastRecovery", ILibDuktape_WebRTC_Connection_Debug); + ILibSCTP_Debug_SetDebugCallback(ILibWrapper_WebRTC_Connection2DtlsSession(connection), "OnReceiverCredits", ILibDuktape_WebRTC_Connection_Debug); + ILibSCTP_Debug_SetDebugCallback(ILibWrapper_WebRTC_Connection2DtlsSession(connection), "OnT3RTX", ILibDuktape_WebRTC_Connection_Debug); + ILibSCTP_Debug_SetDebugCallback(ILibWrapper_WebRTC_Connection2DtlsSession(connection), "OnSendRetry", ILibDuktape_WebRTC_Connection_Debug); + ILibSCTP_Debug_SetDebugCallback(ILibWrapper_WebRTC_Connection2DtlsSession(connection), "OnSendFastRetry", ILibDuktape_WebRTC_Connection_Debug); + ILibSCTP_Debug_SetDebugCallback(ILibWrapper_WebRTC_Connection2DtlsSession(connection), "OnRetryPacket", ILibDuktape_WebRTC_Connection_Debug); + ILibSCTP_Debug_SetDebugCallback(ILibWrapper_WebRTC_Connection2DtlsSession(connection), "OnSACKReceived", ILibDuktape_WebRTC_Connection_Debug); + } +} +#endif duk_ret_t ILibDuktape_WebRTC_CreateConnection(duk_context *ctx) { ILibWebRTC_Duktape_Handlers *ptrs; @@ -513,6 +525,7 @@ duk_ret_t ILibDuktape_WebRTC_CreateConnection(duk_context *ctx) ILibDuktape_EventEmitter_CreateEventEx(ptrs->emitter, "disconnected"); #ifdef _WEBRTCDEBUG + ptrs->debugRegistered = 0; ILibDuktape_EventEmitter_CreateEventEx(ptrs->emitter, "_hold"); ILibDuktape_EventEmitter_CreateEventEx(ptrs->emitter, "_lastSackTime"); ILibDuktape_EventEmitter_CreateEventEx(ptrs->emitter, "_lastSentTime"); @@ -524,6 +537,18 @@ duk_ret_t ILibDuktape_WebRTC_CreateConnection(duk_context *ctx) ILibDuktape_EventEmitter_CreateEventEx(ptrs->emitter, "_retransmit"); ILibDuktape_EventEmitter_CreateEventEx(ptrs->emitter, "_retransmitPacket"); ILibDuktape_EventEmitter_CreateEventEx(ptrs->emitter, "_sackReceived"); + + ILibDuktape_EventEmitter_AddHook(ptrs->emitter, "_hold", ILibDuktape_WebRTC_Connection_DebugHook); + ILibDuktape_EventEmitter_AddHook(ptrs->emitter, "_lastSackTime", ILibDuktape_WebRTC_Connection_DebugHook); + ILibDuktape_EventEmitter_AddHook(ptrs->emitter, "_lastSentTime", ILibDuktape_WebRTC_Connection_DebugHook); + ILibDuktape_EventEmitter_AddHook(ptrs->emitter, "_congestionWindowSizeChange", ILibDuktape_WebRTC_Connection_DebugHook); + ILibDuktape_EventEmitter_AddHook(ptrs->emitter, "_fastRecovery", ILibDuktape_WebRTC_Connection_DebugHook); + ILibDuktape_EventEmitter_AddHook(ptrs->emitter, "_rttCalculated", ILibDuktape_WebRTC_Connection_DebugHook); + ILibDuktape_EventEmitter_AddHook(ptrs->emitter, "_receiverCredits", ILibDuktape_WebRTC_Connection_DebugHook); + ILibDuktape_EventEmitter_AddHook(ptrs->emitter, "_t3tx", ILibDuktape_WebRTC_Connection_DebugHook); + ILibDuktape_EventEmitter_AddHook(ptrs->emitter, "_retransmit", ILibDuktape_WebRTC_Connection_DebugHook); + ILibDuktape_EventEmitter_AddHook(ptrs->emitter, "_retransmitPacket", ILibDuktape_WebRTC_Connection_DebugHook); + ILibDuktape_EventEmitter_AddHook(ptrs->emitter, "_sackReceived", ILibDuktape_WebRTC_Connection_DebugHook); #endif duk_push_pointer(ctx, connection); // [factory][connection][ptr] duk_put_prop_string(ctx, -2, ILibDuktape_WebRTC_ConnectionPtr); // [factory][connection] diff --git a/microstack/ILibWebRTC.c b/microstack/ILibWebRTC.c index 5a8374d..886ac5c 100644 --- a/microstack/ILibWebRTC.c +++ b/microstack/ILibWebRTC.c @@ -912,6 +912,10 @@ typedef struct ILibSCTP_HoldingQueueFlags #define ILibSCTP_GetHoldingQueueFlags(node) ((ILibSCTP_HoldingQueueFlags*)ILibLinkedList_GetExtendedMemory(node)) +void* ILibWebRTC_Dtls2SSL(void *dtls) +{ + return((void*)((ILibStun_dTlsSession*)dtls)->ssl); +} void ILibWebRTC_DTLS_HandshakeDetect(struct ILibStun_Module* obj, char* directionPrefix, char* buffer, int offset, int length) { ILibWebRTC_DTLS_ContentTypes contentType = (ILibWebRTC_DTLS_ContentTypes)buffer[offset+0]; @@ -3273,7 +3277,7 @@ ILibTransport_DoneState ILibStun_SctpSendDataEx(struct ILibStun_Module *obj, int // Only set the T3RTX timer if it is not already running obj->dTlsSessions[session]->T3RTXTIME = rpacket->LastSentTimeStamp; #ifdef _WEBRTCDEBUG - if (obj->dTlsSessions[session]->onT3RTX != NULL){ obj->dTlsSessions[session]->onT3RTX(obj, "OnT3RTX", obj->dTlsSessions[session]->RTO); } + if (obj->dTlsSessions[session]->onT3RTX != NULL){ obj->dTlsSessions[session]->onT3RTX(obj->dTlsSessions[session], "OnT3RTX", obj->dTlsSessions[session]->RTO); } #endif } diff --git a/microstack/ILibWebRTC.h b/microstack/ILibWebRTC.h index b347021..69317d5 100644 --- a/microstack/ILibWebRTC.h +++ b/microstack/ILibWebRTC.h @@ -162,6 +162,8 @@ int ILibSCTP_DoesPeerSupportFeature(void* SctpSession, int feature); void ILibSCTP_Pause(void* SctpSession); void ILibSCTP_Resume(void* SctpSession); +void* ILibWebRTC_Dtls2SSL(void *dtls); + void ILibSCTP_SetCallbacks(void* StunModule, ILibSCTP_OnConnect onconnect, ILibSCTP_OnData ondata, ILibSCTP_OnSendOK onsendok); ILibTransport_DoneState ILibSCTP_Send(void* SctpSession, unsigned short streamId, char* data, int datalen); ILibTransport_DoneState ILibSCTP_SendEx(void* SctpSession, unsigned short streamId, char* data, int datalen, int dataType); diff --git a/microstack/ILibWrapperWebRTC.c b/microstack/ILibWrapperWebRTC.c index a09a5fc..8e77338 100644 --- a/microstack/ILibWrapperWebRTC.c +++ b/microstack/ILibWrapperWebRTC.c @@ -108,7 +108,10 @@ void* ILibWrapper_WebRTC_Connection_GetStunModule(ILibWrapper_WebRTC_Connection { return(((ILibWrapper_WebRTC_ConnectionStruct*)connection)->mFactory->mStunModule); } - +void* ILibWrapper_WebRTC_DtlsSessionToSSL(void *dtls) +{ + return(ILibWebRTC_Dtls2SSL(dtls)); +} ILibTransport_DoneState ILibWrapper_ILibTransport_SendSink(void *transport, char* buffer, int bufferLength, ILibTransport_MemoryOwnership ownership, ILibTransport_DoneState done) { ILibTransport_DoneState retVal = ILibWrapper_WebRTC_DataChannel_Send((ILibWrapper_WebRTC_DataChannel*)transport, buffer, bufferLength); diff --git a/microstack/ILibWrapperWebRTC.h b/microstack/ILibWrapperWebRTC.h index 38cd6a0..2d7058a 100644 --- a/microstack/ILibWrapperWebRTC.h +++ b/microstack/ILibWrapperWebRTC.h @@ -213,6 +213,7 @@ ILibWrapper_WebRTC_Connection ILibWrapper_WebRTC_ConnectionFactory_CreateConnect int ILibWrapper_WebRTC_Connection_GetID(ILibWrapper_WebRTC_Connection connection); char* ILibWrapper_WebRTC_Connection_GetLocalUsername(ILibWrapper_WebRTC_Connection connection); #define ILibWrapper_WebRTC_Connection2DtlsSession(connection) ((void**)(connection))[0] +void* ILibWrapper_WebRTC_DtlsSessionToSSL(void *dtls); ILibTransport* ILibWrapper_WebRTC_Connection_GetRawTransport(ILibWrapper_WebRTC_Connection connection); //! Set the STUN Servers to use with the WebRTC Connection when gathering candidates