summaryrefslogtreecommitdiffstats
path: root/src/tcpstate.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2023-02-06 08:48:24 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2023-02-06 08:48:24 +0000
commit6392e20948d13429246ecadb20d7195461293e10 (patch)
tree07e2e4907fe639c06c50e8cc0b7d133d7bf62413 /src/tcpstate.c
parentAdding upstream version 2.1.0. (diff)
downloaddnscap-6392e20948d13429246ecadb20d7195461293e10.tar.xz
dnscap-6392e20948d13429246ecadb20d7195461293e10.zip
Adding upstream version 2.1.1.upstream/2.1.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--src/tcpstate.c43
1 files changed, 23 insertions, 20 deletions
diff --git a/src/tcpstate.c b/src/tcpstate.c
index 19f5420..fce5ba1 100644
--- a/src/tcpstate.c
+++ b/src/tcpstate.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2018-2022, OARC, Inc.
+ * Copyright (c) 2018-2023, OARC, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -48,6 +48,17 @@ tcpstate_ptr tcpstate_find(iaddr from, iaddr to, unsigned sport, unsigned dport,
static time_t next_gc = 0;
tcpstate_ptr tcpstate;
+#ifndef __clang_analyzer__
+ /* disabled during scan-build due to false-positives */
+ if (t >= next_gc || tcpstate_count > MAX_TCP_IDLE_COUNT) {
+ /* garbage collect stale states */
+ while ((tcpstate = TAIL(tcpstates)) && tcpstate->last_use < t - MAX_TCP_IDLE_TIME) {
+ tcpstate_discard(tcpstate, "gc stale");
+ }
+ next_gc = t + TCP_GC_TIME;
+ }
+#endif
+
for (tcpstate = HEAD(tcpstates);
tcpstate != NULL;
tcpstate = NEXT(tcpstate, link)) {
@@ -63,22 +74,11 @@ tcpstate_ptr tcpstate_find(iaddr from, iaddr to, unsigned sport, unsigned dport,
}
}
- if (t >= next_gc || tcpstate_count > MAX_TCP_IDLE_COUNT) {
- /* garbage collect stale states */
- time_t min_last_use = t - MAX_TCP_IDLE_TIME;
- while ((tcpstate = TAIL(tcpstates)) && tcpstate->last_use < min_last_use) {
- UNLINK(tcpstates, tcpstate, link);
- tcpstate_count--;
- }
- next_gc = t + TCP_GC_TIME;
- }
-
return tcpstate;
}
tcpstate_ptr tcpstate_new(iaddr from, iaddr to, unsigned sport, unsigned dport)
{
-
tcpstate_ptr tcpstate = calloc(1, sizeof *tcpstate);
if (tcpstate == NULL) {
/* Out of memory; recycle the least recently used */
@@ -86,6 +86,7 @@ tcpstate_ptr tcpstate_new(iaddr from, iaddr to, unsigned sport, unsigned dport)
"discarding some TCP state early");
tcpstate = TAIL(tcpstates);
assert(tcpstate != NULL);
+ UNLINK(tcpstates, tcpstate, link);
} else {
tcpstate_count++;
}
@@ -98,6 +99,13 @@ tcpstate_ptr tcpstate_new(iaddr from, iaddr to, unsigned sport, unsigned dport)
return tcpstate;
}
+tcpstate_ptr _curr_tcpstate = 0;
+
+tcpstate_ptr tcpstate_getcurr(void)
+{
+ return _curr_tcpstate;
+}
+
/* Discard this packet. If it's part of TCP stream, all subsequent pkts on
* the same tcp stream will also be discarded. */
void tcpstate_discard(tcpstate_ptr tcpstate, const char* msg)
@@ -110,18 +118,13 @@ void tcpstate_discard(tcpstate_ptr tcpstate, const char* msg)
tcpreasm_free(tcpstate->reasm);
}
free(tcpstate);
+ if (_curr_tcpstate == tcpstate) {
+ _curr_tcpstate = 0;
+ }
tcpstate_count--;
- return;
}
}
-tcpstate_ptr _curr_tcpstate = 0;
-
-tcpstate_ptr tcpstate_getcurr(void)
-{
- return _curr_tcpstate;
-}
-
void tcpstate_reset(tcpstate_ptr tcpstate, const char* msg)
{
if (options.allow_reset_tcpstate && tcpstate) {