diff --git a/src/sniffer.c b/src/sniffer.c index db78c2c9e..1f905e634 100644 --- a/src/sniffer.c +++ b/src/sniffer.c @@ -410,6 +410,10 @@ static word32 MissedDataSessions = 0; /* # of sessions with missed data */ static SSLConnCb ConnectionCb; static void* ConnectionCbCtx = NULL; +/* Sessions Statistics */ +static SSLStats SnifferStats; +static wolfSSL_Mutex StatsMutex; + static void UpdateMissedDataSessions(void) { @@ -419,13 +423,21 @@ static void UpdateMissedDataSessions(void) } +#define ADD_TO_STAT(x,y) do { wc_LockMutex(&StatsMutex); \ + x += y; \ + wc_UnLockMutex(&StatsMutex); } while (0) +#define INC_STAT(x) ADD_TO_STAT(x,1) + + /* Initialize overall Sniffer */ void ssl_InitSniffer(void) { wolfSSL_Init(); + XMEMSET(&SnifferStats, 0, sizeof(SSLStats)); wc_InitMutex(&ServerListMutex); wc_InitMutex(&SessionMutex); wc_InitMutex(&RecoveryMutex); + wc_InitMutex(&StatsMutex); } @@ -3577,6 +3589,7 @@ doPart: break; case alert: Trace(GOT_ALERT_STR); + INC_STAT(SnifferStats.sslAlerts); sslFrame += rhSize; sslBytes -= rhSize; break; @@ -3845,6 +3858,30 @@ int ssl_SetConnectionCtx(void* ctx) } +/* Resets the statistics tracking global structure. + * returns 0 on success, -1 on error */ +int ssl_ResetStatistics(void) +{ + wc_LockMutex(&StatsMutex); + XMEMSET(&SnifferStats, 0, sizeof(SSLStats)); + wc_UnLockMutex(&StatsMutex); + return 0; +} + + +/* Copies the SSL statistics into the provided stats record. + * returns 0 on success, -1 on error */ +int ssl_ReadStatistics(SSLStats* stats) +{ + if (stats == NULL) + return -1; + + wc_LockMutex(&StatsMutex); + XMEMCPY(stats, &SnifferStats, sizeof(SSLStats)); + wc_UnLockMutex(&StatsMutex); + return 0; +} + #endif /* WOLFSSL_SNIFFER */ #endif /* WOLFCRYPT_ONLY */ diff --git a/wolfssl/sniffer.h b/wolfssl/sniffer.h index 33b26da87..96371fcfe 100644 --- a/wolfssl/sniffer.h +++ b/wolfssl/sniffer.h @@ -134,6 +134,40 @@ WOLFSSL_API SSL_SNIFFER_API int ssl_SetConnectionCtx(void* ctx); +typedef struct SSLStats +{ + unsigned int sslStandardConns; + unsigned int sslRehandshakeConns; + unsigned int sslClientAuthConns; + unsigned int sslResumedConns; + unsigned int sslResumedRehandshakeConns; + unsigned int sslClientAuthRehandshakeConns; + unsigned int sslEphemeralMisses; + unsigned int sslResumeMisses; + unsigned int sslCiphersUnsupported; + unsigned int sslKeysUnmatched; + unsigned int sslKeyFails; + unsigned int sslDecodeFails; + unsigned int sslAlerts; + unsigned int sslDecryptedBytes; + unsigned int sslEncryptedBytes; + unsigned int sslEncryptedPackets; + unsigned int sslDecryptedPackets; + unsigned int sslEncryptedConns; + unsigned int sslKeyMatches; + unsigned int sslEncryptedConnsPerSecond; + unsigned int sslActiveFlowsPerSecond; +} SSLStats; + + +WOLFSSL_API +SSL_SNIFFER_API int ssl_ResetStatistics(void); + + +WOLFSSL_API +SSL_SNIFFER_API int ssl_ReadStatistics(SSLStats* stats); + + #ifdef __cplusplus } /* extern "C" */ #endif