There is no point to maintain screen_idx variable: its value follows
the value of table->firstvisible->index variable. Kill this variable
in ipmon() and fix all the places which used it.
Signed-off-by: Vitezslav Samel <vitezslav(a)samel.cz>
---
src/itrafmon.c | 76 ++++++++++++++++++++++------------------------------------
src/tcptable.c | 43 ++++++++++++++++-----------------
src/tcptable.h | 10 ++++----
3 files changed, 54 insertions(+), 75 deletions(-)
diff --git a/src/itrafmon.c b/src/itrafmon.c
index d6360a1..458c674 100644
--- a/src/itrafmon.c
+++ b/src/itrafmon.c
@@ -113,8 +113,7 @@ static void show_stats(WINDOW * win, unsigned long long total)
* Scrolling and paging routines for the upper (TCP) window
*/
-static void scrollupperwin(struct tcptable *table, int direction,
- unsigned long *idx, int mode)
+static void scrollupperwin(struct tcptable *table, int direction, int mode)
{
wattrset(table->tcpscreen, STDATTR);
if (direction == SCROLLUP) {
@@ -122,27 +121,24 @@ static void scrollupperwin(struct tcptable *table, int direction,
wscrl(table->tcpscreen, 1);
table->lastvisible = table->lastvisible->next_entry;
table->firstvisible = table->firstvisible->next_entry;
- (*idx)++;
wmove(table->tcpscreen, table->imaxy - 1, 0);
scrollok(table->tcpscreen, 0);
wprintw(table->tcpscreen, "%*c", COLS - 2, ' ');
scrollok(table->tcpscreen, 1);
- printentry(table, table->lastvisible, *idx, mode);
+ printentry(table, table->lastvisible, mode);
}
} else {
if (table->firstvisible != table->head) {
wscrl(table->tcpscreen, -1);
table->firstvisible = table->firstvisible->prev_entry;
table->lastvisible = table->lastvisible->prev_entry;
- (*idx)--;
mvwprintw(table->tcpscreen, 0, 0, "%*c", COLS - 2, ' ');
- printentry(table, table->firstvisible, *idx, mode);
+ printentry(table, table->firstvisible, mode);
}
}
}
-static void pageupperwin(struct tcptable *table, int direction,
- unsigned long *idx)
+static void pageupperwin(struct tcptable *table, int direction)
{
unsigned int i = 1;
@@ -153,7 +149,6 @@ static void pageupperwin(struct tcptable *table, int direction,
i++;
table->firstvisible = table->firstvisible->next_entry;
table->lastvisible = table->lastvisible->next_entry;
- (*idx)++;
}
} else {
while ((i <= table->imaxy - 3)
@@ -161,7 +156,6 @@ static void pageupperwin(struct tcptable *table, int direction,
i++;
table->firstvisible = table->firstvisible->prev_entry;
table->lastvisible = table->lastvisible->prev_entry;
- (*idx)--;
}
}
}
@@ -434,8 +428,8 @@ static void quicksort_tcp_entries(struct tcptable *table,
* replaced with a Quicksort algorithm.
*/
-static void sortipents(struct tcptable *table, unsigned long *idx, int ch,
- int logging, FILE *logfile)
+static void sortipents(struct tcptable *table, int ch, int logging,
+ FILE *logfile)
{
if ((table->head == NULL)
|| (table->head->next_entry->next_entry == NULL))
@@ -451,7 +445,6 @@ static void sortipents(struct tcptable *table, unsigned long *idx, int
ch,
table->firstvisible = table->head;
struct tcptableent *ptmp = table->head;
- *idx = 1;
while (ptmp && ((int)ptmp->index <= getmaxy(table->tcpscreen))) {
table->lastvisible = ptmp;
@@ -535,8 +528,6 @@ void ipmon(time_t facilitytime, char *ifptr)
in_port_t sport = 0, dport = 0; /* TCP/UDP port values */
- unsigned long screen_idx = 1;
-
struct timeval tv;
struct timeval tv_rate;
time_t starttime = 0;
@@ -707,9 +698,8 @@ void ipmon(time_t facilitytime, char *ifptr)
if ((options.closedint != 0)
&& ((now - closedint) / 60 >= options.closedint)) {
- flushclosedentries(&table, &screen_idx, logging,
- logfile);
- refreshtcpwin(&table, screen_idx, mode);
+ flushclosedentries(&table, logging, logfile);
+ refreshtcpwin(&table, mode);
closedint = now;
}
@@ -787,16 +777,14 @@ void ipmon(time_t facilitytime, char *ifptr)
tmptcp = table.barptr;
table.barptr = table.barptr->prev_entry;
- printentry(&table, tmptcp, screen_idx, mode);
+ printentry(&table, tmptcp, mode);
if (table.baridx == 1)
- scrollupperwin(&table, SCROLLDOWN,
- &screen_idx, mode);
+ scrollupperwin(&table, SCROLLDOWN, mode);
else
(table.baridx)--;
- printentry(&table, table.barptr, screen_idx,
- mode);
+ printentry(&table, table.barptr, mode);
break;
case KEY_DOWN:
if (curwin) {
@@ -809,16 +797,14 @@ void ipmon(time_t facilitytime, char *ifptr)
tmptcp = table.barptr;
table.barptr = table.barptr->next_entry;
- printentry(&table, tmptcp, screen_idx,mode);
+ printentry(&table, tmptcp, mode);
if (table.baridx == table.imaxy)
- scrollupperwin(&table, SCROLLUP,
- &screen_idx, mode);
+ scrollupperwin(&table, SCROLLUP, mode);
else
(table.baridx)++;
- printentry(&table,table.barptr, screen_idx,
- mode);
+ printentry(&table,table.barptr, mode);
break;
case KEY_RIGHT:
if (!curwin)
@@ -849,11 +835,11 @@ void ipmon(time_t facilitytime, char *ifptr)
if (!table.barptr)
break;
- pageupperwin(&table, SCROLLDOWN, &screen_idx);
+ pageupperwin(&table, SCROLLDOWN);
table.barptr = table.lastvisible;
table.baridx = table.lastvisible->index
- - screen_idx + 1;
- refreshtcpwin(&table, screen_idx, mode);
+ - table.firstvisible->index + 1;
+ refreshtcpwin(&table, mode);
break;
case KEY_NPAGE:
case ' ':
@@ -866,10 +852,10 @@ void ipmon(time_t facilitytime, char *ifptr)
if (!table.barptr)
break;
- pageupperwin(&table, SCROLLUP, &screen_idx);
+ pageupperwin(&table, SCROLLUP);
table.barptr = table.firstvisible;
table.baridx = 1;
- refreshtcpwin(&table, screen_idx, mode);
+ refreshtcpwin(&table, mode);
break;
case KEY_F(6):
case 'w':
@@ -887,7 +873,7 @@ void ipmon(time_t facilitytime, char *ifptr)
mode = (mode + 1) % 3;
if ((mode == 1) && !options.mac)
mode = 2;
- refreshtcpwin(&table, screen_idx, mode);
+ refreshtcpwin(&table, mode);
break;
case 12:
case 'l':
@@ -899,9 +885,8 @@ void ipmon(time_t facilitytime, char *ifptr)
case 'f':
case 'c':
case 'C':
- flushclosedentries(&table, &screen_idx, logging,
- logfile);
- refreshtcpwin(&table, screen_idx, mode);
+ flushclosedentries(&table, logging, logfile);
+ refreshtcpwin(&table, mode);
break;
case 's':
case 'S':
@@ -924,14 +909,13 @@ void ipmon(time_t facilitytime, char *ifptr)
show_sort_statwin(&sortwin, &sortpanel);
update_panels();
doupdate();
- sortipents(&table, &screen_idx, ch, logging,
- logfile);
+ sortipents(&table, ch, logging, logfile);
if (table.barptr != NULL) {
table.barptr = table.firstvisible;
table.baridx = 1;
}
- refreshtcpwin(&table, screen_idx, mode);
+ refreshtcpwin(&table, mode);
del_panel(sortpanel);
delwin(sortwin);
update_panels();
@@ -1011,8 +995,7 @@ void ipmon(time_t facilitytime, char *ifptr)
pkt_ip_protocol(&pkt),
ifname, &revlook, rvnfd);
if (tcpentry != NULL) {
- printentry(&table, tcpentry->oth_connection, screen_idx,
- mode);
+ printentry(&table, tcpentry->oth_connection, mode);
if (wasempty) {
table.barptr = table.firstvisible;
@@ -1071,11 +1054,10 @@ void ipmon(time_t facilitytime, char *ifptr)
&& (tcpentry->s_fstat == RESOLVED))
|| ((p_dstat != RESOLVED)
&& (tcpentry->d_fstat == RESOLVED)))) {
- clearaddr(&table, tcpentry, screen_idx);
- clearaddr(&table, tcpentry->oth_connection,
- screen_idx);
+ clearaddr(&table, tcpentry);
+ clearaddr(&table, tcpentry->oth_connection);
}
- printentry(&table, tcpentry, screen_idx, mode);
+ printentry(&table, tcpentry, mode);
/*
* Special cases: Update other direction if it's
@@ -1096,7 +1078,7 @@ void ipmon(time_t facilitytime, char *ifptr)
|| ((p_dstat != RESOLVED)
&& (tcpentry->d_fstat == RESOLVED)))))
printentry(&table, tcpentry->oth_connection,
- screen_idx, mode);
+ mode);
}
break; }
case IPPROTO_ICMP:
diff --git a/src/tcptable.c b/src/tcptable.c
index f9ef908..ee26a99 100644
--- a/src/tcptable.c
+++ b/src/tcptable.c
@@ -746,16 +746,15 @@ void updateentry(struct tcptable *table, struct pkt_hdr *pkt,
* Returns immediately if the entry is not visible in the window.
*/
-void clearaddr(struct tcptable *table, struct tcptableent *tableentry,
- unsigned int screen_idx)
+void clearaddr(struct tcptable *table, struct tcptableent *tableentry)
{
unsigned int target_row;
- if ((tableentry->index < screen_idx)
- || (tableentry->index > screen_idx + (table->imaxy - 1)))
+ if ((tableentry->index < table->firstvisible->index)
+ || (tableentry->index > table->lastvisible->index))
return;
- target_row = (tableentry->index) - screen_idx;
+ target_row = tableentry->index - table->firstvisible->index;
mvwprintw(table->tcpscreen, target_row, 1, "%44c", ' ');
}
@@ -766,7 +765,7 @@ void clearaddr(struct tcptable *table, struct tcptableent
*tableentry,
*/
void printentry(struct tcptable *table, struct tcptableent *tableentry,
- unsigned int screen_idx, int mode)
+ int mode)
{
char stat[7] = "";
unsigned int target_row;
@@ -786,11 +785,11 @@ void printentry(struct tcptable *table, struct tcptableent
*tableentry,
highattr = HIGHATTR;
}
- if ((tableentry->index < screen_idx)
- || (tableentry->index > screen_idx + (table->imaxy - 1)))
+ if ((tableentry->index < table->firstvisible->index)
+ || (tableentry->index > table->lastvisible->index))
return;
- target_row = (tableentry->index) - screen_idx;
+ target_row = tableentry->index - table->firstvisible->index;
/* clear the data if it's a reused entry */
@@ -877,7 +876,7 @@ void printentry(struct tcptable *table, struct tcptableent
*tableentry,
* Redraw the TCP window
*/
-void refreshtcpwin(struct tcptable *table, unsigned int idx, int mode)
+void refreshtcpwin(struct tcptable *table, int mode)
{
struct tcptableent *ptmp;
@@ -887,7 +886,7 @@ void refreshtcpwin(struct tcptable *table, unsigned int idx, int
mode)
ptmp = table->firstvisible;
while ((ptmp != NULL) && (ptmp->prev_entry != table->lastvisible)) {
- printentry(table, ptmp, idx, mode);
+ printentry(table, ptmp, mode);
ptmp = ptmp->next_entry;
}
@@ -1006,12 +1005,12 @@ static void destroy_tcp_entry(struct tcptable *table, struct
tcptableent *ptmp)
* entries.
*/
-void flushclosedentries(struct tcptable *table, unsigned long *screen_idx,
- int logging, FILE *logfile)
+void flushclosedentries(struct tcptable *table, int logging, FILE *logfile)
{
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;
@@ -1064,21 +1063,21 @@ void flushclosedentries(struct tcptable *table, unsigned long
*screen_idx,
* Adjust screen index if the deleted entry was "above"
* the screen.
*/
- if (idx < *screen_idx)
- (*screen_idx)--;
+ if (idx < screen_idx)
+ screen_idx--;
} else {
/*
* Set the first visible pointer once the index matches
* the screen index.
*/
- if (idx == *screen_idx)
+ if (idx == screen_idx)
table->firstvisible = ptmp;
/*
* Keep setting the last visible pointer until the scan
* index "leaves" the screen
*/
- if (idx <= (*screen_idx) + (table->imaxy - 1))
+ if (idx <= screen_idx + (table->imaxy - 1))
table->lastvisible = ptmp;
ptmp->index = idx;
@@ -1103,8 +1102,8 @@ void flushclosedentries(struct tcptable *table, unsigned long
*screen_idx,
* "above" the screen index. Set the firstvisible pointer to that
* as well.
*/
- if (table->tail->index < *screen_idx) {
- *screen_idx = table->tail->index;
+ if (table->tail->index < screen_idx) {
+ screen_idx = table->tail->index;
table->firstvisible = table->tail;
}
@@ -1115,16 +1114,16 @@ void flushclosedentries(struct tcptable *table, unsigned long
*screen_idx,
* head of the table. The highlight bar should "go along" with
* the shifting.
*/
- while ((table->tail->index < *screen_idx + table->imaxy - 1)
+ while ((table->tail->index < screen_idx + table->imaxy - 1)
&& (table->firstvisible->prev_entry != NULL)) {
table->firstvisible = table->firstvisible->prev_entry;
- (*screen_idx)--;
+ screen_idx--;
}
/*
* Set the bar position index once everything's done.
*/
- table->baridx = table->barptr->index - *screen_idx + 1;
+ table->baridx = table->barptr->index - screen_idx + 1;
}
}
diff --git a/src/tcptable.h b/src/tcptable.h
index 85d39ec..06a1262 100644
--- a/src/tcptable.h
+++ b/src/tcptable.h
@@ -124,18 +124,16 @@ void updateentry(struct tcptable *table, struct pkt_hdr *pkt,
void addtoclosedlist(struct tcptable *table, struct tcptableent *tableentry);
-void clearaddr(struct tcptable *table, struct tcptableent *tableentry,
- unsigned int screen_idx);
+void clearaddr(struct tcptable *table, struct tcptableent *tableentry);
void printentry(struct tcptable *table, struct tcptableent *tableentry,
- unsigned int screen_idx, int mode);
+ int mode);
-void refreshtcpwin(struct tcptable *table, unsigned int idx, int mode);
+void refreshtcpwin(struct tcptable *table, int mode);
void destroytcptable(struct tcptable *table);
-void flushclosedentries(struct tcptable *table, unsigned long *screen_idx,
- int logging, FILE *logfile);
+void flushclosedentries(struct tcptable *table, int logging, FILE *logfile);
void write_timeout_log(int logging, FILE *logfile, struct tcptableent *tcpnode);
--
1.8.4