Instead of checking for timeouted TCP entries in qt_partition(),
in_table() and flushclosedentries(), just scan the entire list for
timedout entries every second, mark them and list them in the closed
entries list. Their deletion/reuse is the same as for closed entries
(done in flushclosedentries()/addentry()). Also show the timeouted
entries as "TMOU" on the screen.
Signed-off-by: Vitezslav Samel <vitezslav(a)samel.cz>
---
src/itrafmon.c | 70 ++++++++++++++--------------------------------------------
src/othptab.c | 4 ++--
src/tcptable.c | 70 +++++++++++++++++++++++-----------------------------------
src/tcptable.h | 7 +++---
4 files changed, 50 insertions(+), 101 deletions(-)
diff --git a/src/itrafmon.c b/src/itrafmon.c
index acc4a3a..393d07a 100644
--- a/src/itrafmon.c
+++ b/src/itrafmon.c
@@ -391,8 +391,7 @@ static unsigned long long qt_getkey(struct tcptableent *entry, int
ch)
static struct tcptableent *qt_partition(struct tcptable *table,
struct tcptableent **low,
- struct tcptableent **high, int ch,
- int logging, FILE *logfile)
+ struct tcptableent **high, int ch)
{
struct tcptableent *pivot = *low;
@@ -402,51 +401,16 @@ static struct tcptableent *qt_partition(struct tcptable *table,
unsigned long long pivot_value;
- time_t now;
-
pivot_value = qt_getkey(pivot, ch);
- now = time(NULL);
-
while (left->index < right->index) {
while ((qt_getkey(left, ch) >= pivot_value)
- && (left->next_entry->next_entry != NULL)) {
-
- /*
- * Might as well check out timed out entries here too.
- */
- if ((options.timeout > 0)
- && ((now - left->lastupdate) / 60 > options.timeout)
- && (!(left->inclosed))) {
- left->timedout =
- left->oth_connection->timedout = 1;
- addtoclosedlist(table, left);
-
- if (logging)
- write_timeout_log(logging, logfile,
- left);
- }
+ && (left->next_entry->next_entry != NULL))
left = left->next_entry->next_entry;
- }
- while (qt_getkey(right, ch) < pivot_value) {
- /*
- * Might as well check out timed out entries here too.
- */
- if ((options.timeout > 0)
- && ((now - right->lastupdate) / 60 > options.timeout)
- && (!(right->inclosed))) {
- right->timedout =
- right->oth_connection->timedout = 1;
- addtoclosedlist(table, right);
-
- if (logging)
- write_timeout_log(logging, logfile,
- right);
- }
+ while (qt_getkey(right, ch) < pivot_value)
right = right->prev_entry->prev_entry;
- }
if (left->index < right->index) {
swap_tcp_entries(table, left, right);
@@ -478,8 +442,7 @@ static struct tcptableent *qt_partition(struct tcptable *table,
*/
static void quicksort_tcp_entries(struct tcptable *table,
struct tcptableent *low,
- struct tcptableent *high, int ch,
- int logging, FILE *logfile)
+ struct tcptableent *high, int ch)
{
struct tcptableent *pivot;
@@ -488,15 +451,14 @@ static void quicksort_tcp_entries(struct tcptable *table,
if (high->index > low->index) {
pivot =
- qt_partition(table, &low, &high, ch, logging, logfile);
+ qt_partition(table, &low, &high, ch);
if (pivot->prev_entry != NULL)
quicksort_tcp_entries(table, low,
- pivot->prev_entry->prev_entry, ch,
- logging, logfile);
+ pivot->prev_entry->prev_entry, ch);
quicksort_tcp_entries(table, pivot->next_entry->next_entry,
- high, ch, logging, logfile);
+ high, ch);
}
}
@@ -505,8 +467,7 @@ static void quicksort_tcp_entries(struct tcptable *table,
* replaced with a Quicksort algorithm.
*/
-static void sortipents(struct tcptable *table, int ch, int logging,
- FILE *logfile)
+static void sortipents(struct tcptable *table, int ch)
{
if ((table->head == NULL)
|| (table->head->next_entry->next_entry == NULL))
@@ -517,8 +478,7 @@ static void sortipents(struct tcptable *table, int ch, int logging,
if ((ch != 'P') && (ch != 'B'))
return;
- quicksort_tcp_entries(table, table->head, table->tail->prev_entry, ch,
- logging, logfile);
+ quicksort_tcp_entries(table, table->head, table->tail->prev_entry, ch);
table->firstvisible = table->head;
struct tcptableent *ptmp = table->head;
@@ -755,9 +715,11 @@ void ipmon(time_t facilitytime, char *ifptr)
dropped += packet_get_dropped(fd);
print_packet_drops(dropped, othptbl.borderwin, othptbl.obmaxy - 1, 40);
+ mark_timeouted_entries(&table, logging, logfile);
+
/* automatically clear closed/timed out entries */
if (now.tv_sec > check_closed) {
- flushclosedentries(&table, logging, logfile);
+ flushclosedentries(&table);
refreshtcpwin(&table);
check_closed = now.tv_sec + options.closedint * 60;
}
@@ -873,7 +835,7 @@ void ipmon(time_t facilitytime, char *ifptr)
case 'f':
case 'c':
case 'C':
- flushclosedentries(&table, logging, logfile);
+ flushclosedentries(&table);
refreshtcpwin(&table);
break;
case 's':
@@ -894,7 +856,8 @@ void ipmon(time_t facilitytime, char *ifptr)
keymode = 0;
del_panel(sortpanel);
delwin(sortwin);
- sortipents(&table, ch, logging, logfile);
+ flushclosedentries(&table);
+ sortipents(&table, ch);
if (table.barptr != NULL) {
table.barptr = table.firstvisible;
}
@@ -953,8 +916,7 @@ void ipmon(time_t facilitytime, char *ifptr)
struct tcphdr *tcp = (struct tcphdr *)ip_payload;
sockaddr_set_port(&saddr, sport);
sockaddr_set_port(&daddr, dport);
- tcpentry = in_table(&table, &saddr, &daddr, ifname,
- logging, logfile, options.timeout);
+ tcpentry = in_table(&table, &saddr, &daddr, ifname);
/*
* Add a new entry if it doesn't exist, and,
diff --git a/src/othptab.c b/src/othptab.c
index 40acebf..2aa54f5 100644
--- a/src/othptab.c
+++ b/src/othptab.c
@@ -144,7 +144,7 @@ void check_icmp_dest_unreachable(struct tcptable *table, struct
pkt_hdr *pkt,
sockaddr_set_port(&daddr, ntohs(tcp->dest));
/* check if this tcpentry exists */
- tcpentry = in_table(table, &saddr, &daddr, ifname, 0, NULL, 0);
+ tcpentry = in_table(table, &saddr, &daddr, ifname);
break; }
case IPPROTO_ICMPV6: {
@@ -167,7 +167,7 @@ void check_icmp_dest_unreachable(struct tcptable *table, struct
pkt_hdr *pkt,
sockaddr_set_port(&daddr, ntohs(tcp->dest));
/* check if this tcpentry exists */
- tcpentry = in_table(table, &saddr, &daddr, ifname, 0, NULL, 0);
+ tcpentry = in_table(table, &saddr, &daddr, ifname);
break; }
default:
diff --git a/src/tcptable.c b/src/tcptable.c
index 06d1572..7693b41 100644
--- a/src/tcptable.c
+++ b/src/tcptable.c
@@ -485,17 +485,36 @@ void write_timeout_log(int logging, FILE *logfile, struct
tcptableent *tcpnode)
}
}
+void mark_timeouted_entries(struct tcptable *table, int logging, FILE *logfile)
+{
+ if (options.timeout == 0)
+ return;
+
+ time_t now = time(NULL);
+ struct tcptableent *ptmp = table->head;
+
+ while (ptmp != NULL) {
+ if (((now - ptmp->lastupdate) / 60 > options.timeout)
+ && !ptmp->inclosed) {
+ ptmp->timedout = 1;
+ ptmp->oth_connection->timedout = 1;
+ addtoclosedlist(table, ptmp);
+
+ if (logging)
+ write_timeout_log(logging, logfile, ptmp);
+ }
+ ptmp = ptmp->next_entry;
+ }
+}
+
struct tcptableent *in_table(struct tcptable *table,
struct sockaddr_storage *saddr,
struct sockaddr_storage *daddr,
- char *ifname, int logging,
- FILE *logfile, time_t timeout)
+ char *ifname)
{
struct tcp_hashentry *hashptr;
unsigned int hp;
- time_t now;
-
if (table->head == NULL) {
return 0;
}
@@ -512,24 +531,6 @@ struct tcptableent *in_table(struct tcptable *table,
&& (strcmp(hashptr->tcpnode->ifname, ifname) == 0))
break;
- now = time(NULL);
-
- /*
- * Add the timed out entries to the closed list in case we didn't
- * find any closed ones.
- */
-
- if ((timeout > 0)
- && ((now - hashptr->tcpnode->lastupdate) / 60 > timeout)
- && (!(hashptr->tcpnode->inclosed))) {
- hashptr->tcpnode->timedout = 1;
- hashptr->tcpnode->oth_connection->timedout = 1;
- addtoclosedlist(table, hashptr->tcpnode);
-
- if (logging)
- write_timeout_log(logging, logfile,
- hashptr->tcpnode);
- }
hashptr = hashptr->next_entry;
}
@@ -869,7 +870,9 @@ void printentry(struct tcptable *table, struct tcptableent
*tableentry)
wattrset(table->tcpscreen, normalattr);
- if (tableentry->finsent == 1)
+ if (tableentry->timedout)
+ strcpy(stat, "TMOU");
+ else if (tableentry->finsent == 1)
strcpy(stat, "DONE");
else if (tableentry->finsent == 2)
strcpy(stat, "CLOS");
@@ -1001,34 +1004,17 @@ static void destroy_tcp_entry(struct tcptable *table, struct
tcptableent *ptmp)
* entries.
*/
-void flushclosedentries(struct tcptable *table, int logging, FILE *logfile)
+void flushclosedentries(struct tcptable *table)
{
struct tcptableent *ptmp = table->head;
struct tcptableent *ctmp = NULL;
unsigned long idx = 1;
unsigned long screen_idx = table->firstvisible->index;
- time_t now;
- time_t lastupdated = 0;
while (ptmp != NULL) {
- now = time(NULL);
- lastupdated = (now - ptmp->lastupdate) / 60;
-
- if ((ptmp->inclosed) || (lastupdated > options.timeout)) {
+ if (ptmp->inclosed) {
ctmp = ptmp;
/*
- * Mark and flush timed out TCP entries.
- */
- if (lastupdated > options.timeout) {
- if ((!(ptmp->timedout)) && (!(ptmp->inclosed))) {
- write_timeout_log(logging, logfile,
- ptmp);
- ptmp->timedout =
- ptmp->oth_connection->timedout = 1;
- }
- }
-
- /*
* Advance to next entry and destroy target entry.
*/
ptmp = ptmp->next_entry;
diff --git a/src/tcptable.h b/src/tcptable.h
index ff692fe..6ee90ad 100644
--- a/src/tcptable.h
+++ b/src/tcptable.h
@@ -115,11 +115,12 @@ struct tcptableent *addentry(struct tcptable *table,
int protocol, char *ifname,
int *rev_lookup, int rvnamedon);
+void mark_timeouted_entries(struct tcptable *table, int logging, FILE *logfile);
+
struct tcptableent *in_table(struct tcptable *table,
struct sockaddr_storage *saddr,
struct sockaddr_storage *daddr,
- char *ifname, int logging,
- FILE *logfile, time_t timeout);
+ char *ifname);
void updateentry(struct tcptable *table, struct pkt_hdr *pkt,
struct tcptableent *tableentry, struct tcphdr *transpacket,
@@ -136,7 +137,7 @@ void refreshtcpwin(struct tcptable *table);
void destroytcptable(struct tcptable *table);
-void flushclosedentries(struct tcptable *table, int logging, FILE *logfile);
+void flushclosedentries(struct tcptable *table);
void write_timeout_log(int logging, FILE *logfile, struct tcptableent *tcpnode);
--
1.8.4