Skip to content

Commit 63c3c54

Browse files
authored
Merge pull request #8768 from lealem47/zd19853
Add sniffer cleanup API's
2 parents c13be21 + 4af0e14 commit 63c3c54

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

src/sniffer.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5129,6 +5129,12 @@ static void RemoveStaleSessions(void)
51295129
}
51305130
}
51315131

5132+
void ssl_RemoveStaleSessions(void)
5133+
{
5134+
LOCK_SESSION();
5135+
RemoveStaleSessions();
5136+
UNLOCK_SESSION();
5137+
}
51325138

51335139
/* Create a new Sniffer Session */
51345140
static SnifferSession* CreateSession(IpInfo* ipInfo, TcpInfo* tcpInfo,
@@ -7620,6 +7626,106 @@ int ssl_LoadSecretsFromKeyLogFile(const char* keylogfile, char* error)
76207626
#endif /* WOLFSSL_SNIFFER_KEYLOGFILE */
76217627

76227628

7629+
/*
7630+
* Removes a session from the SessionTable based on client/server IP & ports
7631+
* Returns 0 if a session was found and freed, -1 otherwise
7632+
*/
7633+
int ssl_RemoveSession(const char* clientIp, int clientPort,
7634+
const char* serverIp, int serverPort,
7635+
char* error)
7636+
{
7637+
IpAddrInfo clientAddr;
7638+
IpAddrInfo serverAddr;
7639+
IpInfo ipInfo;
7640+
TcpInfo tcpInfo;
7641+
SnifferSession* session;
7642+
int ret = -1; /* Default to not found */
7643+
word32 row;
7644+
7645+
if (clientIp == NULL || serverIp == NULL) {
7646+
SetError(BAD_IPVER_STR, error, NULL, 0);
7647+
return ret;
7648+
}
7649+
7650+
/* Set up client IP address */
7651+
clientAddr.version = IPV4;
7652+
clientAddr.ip4 = XINET_ADDR(clientIp);
7653+
if (clientAddr.ip4 == XINADDR_NONE) {
7654+
#ifdef FUSION_RTOS
7655+
if (XINET_PTON(AF_INET6, clientIp, clientAddr.ip6,
7656+
sizeof(clientAddr.ip4)) == 1)
7657+
#else
7658+
if (XINET_PTON(AF_INET6, clientIp, clientAddr.ip6) == 1)
7659+
#endif
7660+
{
7661+
clientAddr.version = IPV6;
7662+
}
7663+
else {
7664+
SetError(BAD_IPVER_STR, error, NULL, 0);
7665+
return ret;
7666+
}
7667+
}
7668+
7669+
/* Set up server IP address */
7670+
serverAddr.version = IPV4;
7671+
serverAddr.ip4 = XINET_ADDR(serverIp);
7672+
if (serverAddr.ip4 == XINADDR_NONE) {
7673+
#ifdef FUSION_RTOS
7674+
if (XINET_PTON(AF_INET6, serverIp, serverAddr.ip6,
7675+
sizeof(serverAddr.ip4)) == 1)
7676+
#else
7677+
if (XINET_PTON(AF_INET6, serverIp, serverAddr.ip6) == 1)
7678+
#endif
7679+
{
7680+
serverAddr.version = IPV6;
7681+
}
7682+
else {
7683+
SetError(BAD_IPVER_STR, error, NULL, 0);
7684+
return ret;
7685+
}
7686+
}
7687+
7688+
XMEMSET(&ipInfo, 0, sizeof(ipInfo));
7689+
XMEMSET(&tcpInfo, 0, sizeof(tcpInfo));
7690+
7691+
/* Set up client->server direction */
7692+
ipInfo.src = clientAddr;
7693+
ipInfo.dst = serverAddr;
7694+
tcpInfo.srcPort = clientPort;
7695+
tcpInfo.dstPort = serverPort;
7696+
7697+
/* Calculate the hash row for this session */
7698+
row = SessionHash(&ipInfo, &tcpInfo);
7699+
7700+
LOCK_SESSION();
7701+
7702+
/* Search only the specific row in the session table */
7703+
session = SessionTable[row];
7704+
7705+
while (session) {
7706+
SnifferSession* next = session->next;
7707+
7708+
/* Check if this session matches the specified client/server IP/port */
7709+
if (MatchAddr(session->client, clientAddr) &&
7710+
MatchAddr(session->server, serverAddr) &&
7711+
session->cliPort == clientPort &&
7712+
session->srvPort == serverPort) {
7713+
7714+
/* Use RemoveSession to remove and free the session */
7715+
RemoveSession(session, NULL, NULL, row);
7716+
ret = 0; /* Session found and freed */
7717+
break;
7718+
}
7719+
7720+
session = next;
7721+
}
7722+
7723+
UNLOCK_SESSION();
7724+
7725+
return ret;
7726+
}
7727+
7728+
76237729
#undef ERROR_OUT
76247730

76257731
#endif /* WOLFSSL_SNIFFER */

wolfssl/sniffer.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ SSL_SNIFFER_API void ssl_InitSniffer_ex2(int threadNum);
150150
WOLFSSL_API
151151
SSL_SNIFFER_API void ssl_FreeSniffer(void);
152152

153+
WOLFSSL_API
154+
SSL_SNIFFER_API void ssl_RemoveStaleSessions(void);
153155

154156
/* ssl_SetPrivateKey typeKs */
155157
enum {
@@ -343,6 +345,11 @@ typedef int (*SSLSnifferSecretCb)(unsigned char* client_random,
343345

344346
#endif /* WOLFSSL_SNIFFER_KEYLOGFILE */
345347

348+
WOLFSSL_API
349+
SSL_SNIFFER_API int ssl_RemoveSession(const char* clientIp, int clientPort,
350+
const char* serverIp, int serverPort,
351+
char* error);
352+
346353

347354
#ifdef __cplusplus
348355
} /* extern "C" */

0 commit comments

Comments
 (0)