From: Eric Leblond Date: Mon, 26 Nov 2012 18:36:09 +0000 (+0100) Subject: unix-socket: cleanup host table instead of destroying it X-Git-Tag: suricata-1.4rc1~16 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=12fd60b545d5bb86b8245d66a5f9c786e61b51fd;p=thirdparty%2Fsuricata.git unix-socket: cleanup host table instead of destroying it This patch should fix the bug #637. Between pcap files, it uses a new function HostCleanup() to clear tag and threshold on host with an IP regputation. An other consequence of this modification is that Host init and shutdown are now init and shutdown unconditionaly. --- diff --git a/src/host.c b/src/host.c index 63b3ef3cfe..a86fd42508 100644 --- a/src/host.c +++ b/src/host.c @@ -286,6 +286,58 @@ void HostShutdown(void) return; } +/** \brief Cleanup the host engine + * + * Cleanup the host engine from tag and threshold. + * + */ +void HostCleanup(void) +{ + Host *h; + uint32_t u; + + if (host_hash != NULL) { + for (u = 0; u < host_config.hash_size; u++) { + h = host_hash[u].head; + HostHashRow *hb = &host_hash[u]; + HRLOCK_LOCK(hb); + while (h) { + if ((SC_ATOMIC_GET(h->use_cnt) > 0) && (h->iprep != NULL)) { + /* iprep is attached to host only clear tag and threshold */ + if (h->tag != NULL) { + DetectTagDataListFree(h->tag); + h->tag = NULL; + } + if (h->threshold != NULL) { + ThresholdListFree(h->threshold); + h->threshold = NULL; + } + h = h->hnext; + } else { + Host *n = h->hnext; + /* remove from the hash */ + if (h->hprev != NULL) + h->hprev->hnext = h->hnext; + if (h->hnext != NULL) + h->hnext->hprev = h->hprev; + if (hb->head == h) + hb->head = h->hnext; + if (hb->tail == h) + hb->tail = h->hprev; + h->hnext = NULL; + h->hprev = NULL; + HostClearMemory(h); + HostMoveToSpare(h); + h = n; + } + } + HRLOCK_UNLOCK(hb); + } + } + + return; +} + /* calculate the hash key for this packet * * we're using: diff --git a/src/host.h b/src/host.h index ee835319f8..49b3b7280d 100644 --- a/src/host.h +++ b/src/host.h @@ -133,6 +133,7 @@ SC_ATOMIC_DECLARE(unsigned int,host_prune_idx); void HostInitConfig(char quiet); void HostShutdown(void); +void HostCleanup(void); Host *HostLookupHostFromHash (Address *); Host *HostGetHostFromHash (Address *); diff --git a/src/runmode-unix-socket.c b/src/runmode-unix-socket.c index 870fa34a4f..7ab8717dd2 100644 --- a/src/runmode-unix-socket.c +++ b/src/runmode-unix-socket.c @@ -273,7 +273,7 @@ TmEcode UnixSocketPcapFilesCheck(void *data) SCPerfReleaseResources(); /* thread killed, we can run non thread-safe shutdown functions */ FlowShutdown(); - HostShutdown(); + HostCleanup(); StreamTcpFreeConfig(STREAM_VERBOSE); DefragDestroy(); TmqResetQueues(); @@ -299,7 +299,6 @@ TmEcode UnixSocketPcapFilesCheck(void *data) PcapFilesFree(cfile); SCPerfInitCounterApi(); DefragInit(); - HostInitConfig(HOST_QUIET); FlowInitConfig(FLOW_QUIET); StreamTcpInitConfig(STREAM_VERBOSE); RunModeInitializeOutputs(); diff --git a/src/suricata.c b/src/suricata.c index 73d7932c9d..5ef181f9cd 100644 --- a/src/suricata.c +++ b/src/suricata.c @@ -1805,8 +1805,8 @@ int main(int argc, char **argv) #endif /* OS_WIN32 */ PacketPoolInit(max_pending_packets); + HostInitConfig(HOST_VERBOSE); if (run_mode != RUNMODE_UNIX_SOCKET) { - HostInitConfig(HOST_VERBOSE); FlowInitConfig(FLOW_VERBOSE); } @@ -2076,9 +2076,9 @@ int main(int argc, char **argv) if (run_mode != RUNMODE_UNIX_SOCKET) { SCPerfReleaseResources(); FlowShutdown(); - HostShutdown(); StreamTcpFreeConfig(STREAM_VERBOSE); } + HostShutdown(); HTPFreeConfig(); HTPAtExitPrintStats();