[PATCH] promics: full rewrite
by Nikola Pajkovsky
code was so difficult to read, that I had to rewrite it from scratch.
Original code was using list structure designed only for promisc code, and
that means it has to have its own adding / removing / traversing code.
Not smart idea. Also I removed the code which stores and loads from disk
state of promosic mode. Code does:
init path
init_promisc_list(&promisc_list);
save_promisc_list(promisc_list);
srpromisc(1, promisc_list);
destroy_promisc_list(&promisc_list);
...
...
exit/error path
load_promisc_list(&promisc_list);
srpromisc(0, promisc_list);
destroy_promisc_list(&promisc_list);
now it does
init path
init_promisc_list(&promisc_list);
promisc_set(promisc_list);
...
...
exit/error path
promisc_restore(promisc_list);
destroy_promisc_list(&promisc_list);
Signed-off-by: Nikola Pajkovsky <npajkovs(a)redhat.com>
---
src/detstats.c | 15 ++---
src/hostmon.c | 16 ++---
src/ifstats.c | 16 ++---
src/itrafmon.c | 14 ++--
src/list.h | 6 ++
src/pktsize.c | 14 ++--
src/promisc.c | 200 +++++++++++++--------------------------------------------
src/promisc.h | 28 +++-----
src/serv.c | 14 ++--
9 files changed, 92 insertions(+), 231 deletions(-)
diff --git a/src/detstats.c b/src/detstats.c
index c1e415f..02b243e 100644
--- a/src/detstats.c
+++ b/src/detstats.c
@@ -291,7 +291,6 @@ void detstats(char *iface, time_t facilitytime)
unsigned long peakpps_in = 0;
unsigned long peakpps_out = 0;
- struct promisc_states *promisc_list;
int fd;
/*
@@ -312,11 +311,10 @@ void detstats(char *iface, time_t facilitytime)
return;
}
- if (first_active_facility() && options.promisc) {
- init_promisc_list(&promisc_list);
- save_promisc_list(promisc_list);
- srpromisc(1, promisc_list);
- destroy_promisc_list(&promisc_list);
+ LIST_HEAD(promisc);
+ if (options.promisc && first_active_facility()) {
+ init_promisc_list(&promisc);
+ promisc_set(&promisc);
}
adjust_instance_count(PROCCOUNTFILE, 1);
@@ -594,9 +592,8 @@ err:
rate_destroy(&rate);
if (options.promisc && is_last_instance()) {
- load_promisc_list(&promisc_list);
- srpromisc(0, promisc_list);
- destroy_promisc_list(&promisc_list);
+ promisc_restore(&promisc);
+ promisc_destroy(&promisc);
}
adjust_instance_count(PROCCOUNTFILE, -1);
diff --git a/src/hostmon.c b/src/hostmon.c
index 21b1797..8b98cef 100644
--- a/src/hostmon.c
+++ b/src/hostmon.c
@@ -778,8 +778,6 @@ void hostmon(time_t facilitytime, char *ifptr)
int fd;
- struct promisc_states *promisc_list;
-
if (!facility_active(LANMONIDFILE, ifptr))
mark_facility(LANMONIDFILE, "LAN monitor", ifptr);
else {
@@ -796,11 +794,10 @@ void hostmon(time_t facilitytime, char *ifptr)
}
}
- if (first_active_facility() && options.promisc) {
- init_promisc_list(&promisc_list);
- save_promisc_list(promisc_list);
- srpromisc(1, promisc_list);
- destroy_promisc_list(&promisc_list);
+ LIST_HEAD(promisc);
+ if (options.promisc && first_active_facility()) {
+ init_promisc_list(&promisc);
+ promisc_set(&promisc);
}
adjust_instance_count(PROCCOUNTFILE, 1);
@@ -1031,9 +1028,8 @@ err_close:
err:
if (options.promisc && is_last_instance()) {
- load_promisc_list(&promisc_list);
- srpromisc(0, promisc_list);
- destroy_promisc_list(&promisc_list);
+ promisc_restore(&promisc);
+ promisc_destroy(&promisc);
}
adjust_instance_count(PROCCOUNTFILE, -1);
diff --git a/src/ifstats.c b/src/ifstats.c
index 8214838..6712ed5 100644
--- a/src/ifstats.c
+++ b/src/ifstats.c
@@ -451,8 +451,6 @@ void ifstats(time_t facilitytime)
time_t startlog = 0;
struct timeval updtime;
- struct promisc_states *promisc_list;
-
if (!facility_active(GSTATIDFILE, ""))
mark_facility(GSTATIDFILE, "general interface statistics", "");
else {
@@ -469,11 +467,10 @@ void ifstats(time_t facilitytime)
initiftab(&table);
- if (first_active_facility() && options.promisc) {
- init_promisc_list(&promisc_list);
- save_promisc_list(promisc_list);
- srpromisc(1, promisc_list);
- destroy_promisc_list(&promisc_list);
+ LIST_HEAD(promisc);
+ if (options.promisc && first_active_facility()) {
+ init_promisc_list(&promisc);
+ promisc_set(&promisc);
}
adjust_instance_count(PROCCOUNTFILE, 1);
@@ -630,9 +627,8 @@ void ifstats(time_t facilitytime)
err:
if (options.promisc && is_last_instance()) {
- load_promisc_list(&promisc_list);
- srpromisc(0, promisc_list);
- destroy_promisc_list(&promisc_list);
+ promisc_restore(&promisc);
+ promisc_destroy(&promisc);
}
adjust_instance_count(PROCCOUNTFILE, -1);
diff --git a/src/itrafmon.c b/src/itrafmon.c
index b35468f..b27722e 100644
--- a/src/itrafmon.c
+++ b/src/itrafmon.c
@@ -599,8 +599,6 @@ void ipmon(time_t facilitytime, char *ifptr)
int keymode = 0;
char msgstring[80];
- struct promisc_states *promisc_list;
-
int rvnfd = 0;
int instance_id;
@@ -628,11 +626,12 @@ void ipmon(time_t facilitytime, char *ifptr)
}
}
+ LIST_HEAD(promisc);
+
if (options.promisc) {
if (first_active_facility()) {
- init_promisc_list(&promisc_list);
- save_promisc_list(promisc_list);
- srpromisc(1, promisc_list);
+ init_promisc_list(&promisc);
+ promisc_set(&promisc);
}
}
@@ -1188,9 +1187,8 @@ err:
close_rvn_socket(rvnfd);
if (options.promisc && is_last_instance()) {
- load_promisc_list(&promisc_list);
- srpromisc(0, promisc_list);
- destroy_promisc_list(&promisc_list);
+ promisc_restore(&promisc);
+ promisc_destroy(&promisc);
}
attrset(STDATTR);
diff --git a/src/list.h b/src/list.h
index daa7628..a7b495e 100644
--- a/src/list.h
+++ b/src/list.h
@@ -80,4 +80,10 @@ static inline int list_empty(const struct list_head *head)
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
+#define list_for_each_entry_safe(pos, n, head, member) \
+ for (pos = list_entry((head)->next, typeof(*pos), member), \
+ n = list_entry(pos->member.next, typeof(*pos), member); \
+ &pos->member != (head); \
+ pos = n, n = list_entry(n->member.next, typeof(*n), member))
+
#endif /* IPTRAF_NG_LIST_H */
diff --git a/src/pktsize.c b/src/pktsize.c
index 44bc61f..c5af7fa 100644
--- a/src/pktsize.c
+++ b/src/pktsize.c
@@ -161,8 +161,6 @@ void packet_size_breakdown(char *ifname, time_t facilitytime)
int logging = options.logging;
FILE *logfile = NULL;
- struct promisc_states *promisc_list;
-
int fd;
if (!facility_active(PKTSIZEIDFILE, ifname))
@@ -241,11 +239,10 @@ void packet_size_breakdown(char *ifname, time_t facilitytime)
updtime = tv;
now = starttime = startlog = timeint = tv.tv_sec;
+ LIST_HEAD(promisc);
if (first_active_facility() && options.promisc) {
- init_promisc_list(&promisc_list);
- save_promisc_list(promisc_list);
- srpromisc(1, promisc_list);
- destroy_promisc_list(&promisc_list);
+ init_promisc_list(&promisc);
+ promisc_set(&promisc);
}
adjust_instance_count(PROCCOUNTFILE, 1);
@@ -337,9 +334,8 @@ err:
}
if (options.promisc && is_last_instance()) {
- load_promisc_list(&promisc_list);
- srpromisc(0, promisc_list);
- destroy_promisc_list(&promisc_list);
+ promisc_restore(&promisc);
+ promisc_destroy(&promisc);
}
adjust_instance_count(PROCCOUNTFILE, -1);
diff --git a/src/promisc.c b/src/promisc.c
index a0f5364..bc0e1fb 100644
--- a/src/promisc.c
+++ b/src/promisc.c
@@ -17,178 +17,66 @@ promisc.c - handles the promiscuous mode flag for the Ethernet/FDDI/
#define PROMISC_MSG_MAX 80
-void init_promisc_list(struct promisc_states **list)
+void init_promisc_list(struct list_head *promisc)
{
- FILE *fd;
char buf[IFNAMSIZ];
- struct promisc_states *ptmp;
- struct promisc_states *tail = NULL;
-
- *list = NULL;
- fd = open_procnetdev();
-
- while (get_next_iface(fd, buf, sizeof(buf))) {
- if (strcmp(buf, "") != 0) {
- ptmp = xmalloc(sizeof(struct promisc_states));
- strcpy(ptmp->params.ifname, buf);
-
- if (*list == NULL) {
- *list = ptmp;
- } else
- tail->next_entry = ptmp;
-
- tail = ptmp;
- ptmp->next_entry = NULL;
-
- /*
- * Retrieve and save interface flags
- */
-
- if ((strncmp(buf, "eth", 3) == 0)
- || (strncmp(buf, "ra", 2) == 0)
- || (strncmp(buf, "fddi", 4) == 0)
- || (strncmp(buf, "tr", 2) == 0)
- || (strncmp(buf, "ath", 3) == 0)
- || (strncmp(buf, "bnep", 4) == 0)
- || (strncmp(buf, "ni", 2) == 0)
- || (strncmp(buf, "tap", 3) == 0)
- || (strncmp(buf, "dummy", 5) == 0)
- || (strncmp(buf, "br", 2) == 0)
- || (strncmp(buf, "vmnet", 5) == 0)
- || (strncmp(ptmp->params.ifname, "wvlan", 4) == 0)
- || (strncmp(ptmp->params.ifname, "lec", 3) == 0)) {
- int flags = dev_get_flags(buf);
-
- if (flags < 0) {
- write_error("Unable to obtain interface parameters for %s",
- buf);
- ptmp->params.state_valid = 0;
- } else {
- ptmp->params.saved_state = flags;
- ptmp->params.state_valid = 1;
- }
- }
- }
- }
-}
-
-/*
- * Save interfaces and their states to a temporary file. Used only by the
- * first IPTraf instance. Needed in case there are subsequent, simultaneous
- * instances of IPTraf, which may still need promiscuous mode even after
- * the first instance exits. These subsequent instances will need to restore
- * the promiscuous state from this file.
- */
-
-void save_promisc_list(struct promisc_states *list)
-{
- int fd;
- struct promisc_states *ptmp = list;
-
- fd = open(PROMISCLISTFILE, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
- if (fd < 0) {
- write_error("Unable to save interface flags");
- return;
- }
+ FILE *fp = open_procnetdev();
+ if (!fp)
+ die_errno("%s: open_procnetdev", __func__);
+
+ while (get_next_iface(fp, buf, sizeof(buf))) {
+ if (!strcmp(buf, ""))
+ continue;
+
+ struct promisc_list *p = xmallocz(sizeof(*p));
+ strcpy(p->ifname, buf);
+ INIT_LIST_HEAD(&p->list);
+
+ int flags = dev_get_flags(buf);
+ if (flags < 0) {
+ write_error("Unable to obtain interface parameters for %s",
+ buf);
+ } else {
+ p->flags = flags;
+ p->state_valid = 1;
+ }
- while (ptmp != NULL) {
- write(fd, &(ptmp->params), sizeof(struct promisc_params));
- ptmp = ptmp->next_entry;
+ list_add_tail(&p->list, promisc);
}
- close(fd);
+ fclose(fp);
}
-/*
- * Load promiscuous states into list
- */
-
-void load_promisc_list(struct promisc_states **list)
+void promisc_set(struct list_head *promisc)
{
- int fd;
- struct promisc_states *ptmp = NULL;
- struct promisc_states *tail = NULL;
- int br;
-
- fd = open(PROMISCLISTFILE, O_RDONLY);
-
- if (fd < 0) {
- write_error("Unable to retrieve saved interface flags");
- *list = NULL;
- return;
+ struct promisc_list *entry = NULL;
+ list_for_each_entry(entry, promisc, list) {
+ int r = dev_set_promisc(entry->ifname);
+ if (r < 0)
+ write_error("Failed to set promiscuous mode on %s",
+ entry->ifname);
}
-
- do {
- ptmp = xmalloc(sizeof(struct promisc_states));
- br = read(fd, &(ptmp->params), sizeof(struct promisc_params));
-
- if (br > 0) {
- if (tail != NULL)
- tail->next_entry = ptmp;
- else
- *list = ptmp;
-
- ptmp->next_entry = NULL;
- tail = ptmp;
- } else
- free(ptmp);
- } while (br > 0);
-
- close(fd);
}
-/*
- * Set/restore interface promiscuous mode.
- */
-
-void srpromisc(int mode, struct promisc_states *list)
+void promisc_restore(struct list_head *promisc)
{
- struct promisc_states *ptmp;
-
- ptmp = list;
-
- while (ptmp != NULL) {
- if (((strncmp(ptmp->params.ifname, "eth", 3) == 0)
- || (strncmp(ptmp->params.ifname, "fddi", 4) == 0)
- || (strncmp(ptmp->params.ifname, "tr", 2) == 0)
- || (strncmp(ptmp->params.ifname, "ra", 2) == 0)
- || (strncmp(ptmp->params.ifname, "ath", 3) == 0)
- || (strncmp(ptmp->params.ifname, "wvlan", 4) == 0)
- || (strncmp(ptmp->params.ifname, "lec", 3) == 0))
- && (ptmp->params.state_valid)) {
- if (mode) {
- /* set promiscuous */
- int r = dev_set_promisc(ptmp->params.ifname);
- if(r < 0)
- write_error("Failed to set promiscuous mode on %s", ptmp->params.ifname);
- } else {
- /* restore saved state */
- if (ptmp->params.saved_state & IFF_PROMISC)
- /* was promisc, so leave it as is */
- continue;
- /* wasn't promisc, clear it */
- int r = dev_clear_promisc(ptmp->params.ifname);
- if(r < 0)
- write_error("Failed to clear promiscuous mode on %s", ptmp->params.ifname);
- }
- }
- ptmp = ptmp->next_entry;
+ struct promisc_list *entry = NULL;
+ list_for_each_entry(entry, promisc, list) {
+ if (entry->flags & IFF_PROMISC)
+ continue;
+
+ int r = dev_clear_promisc(entry->ifname);
+ if (r < 0)
+ write_error("Failed to clear promiscuous mode on %s", entry->ifname);
}
}
-void destroy_promisc_list(struct promisc_states **list)
+void promisc_destroy(struct list_head *promisc)
{
- struct promisc_states *ptmp = *list;
- struct promisc_states *ctmp;
-
- if (ptmp != NULL)
- ctmp = ptmp->next_entry;
-
- while (ptmp != NULL) {
- free(ptmp);
- ptmp = ctmp;
- if (ctmp != NULL)
- ctmp = ctmp->next_entry;
+ struct promisc_list *entry, *tmp;
+ list_for_each_entry_safe(entry, tmp, promisc, list) {
+ list_del(&entry->list);
+ free(entry);
}
}
diff --git a/src/promisc.h b/src/promisc.h
index f062ca1..c4150b3 100644
--- a/src/promisc.h
+++ b/src/promisc.h
@@ -1,31 +1,19 @@
#ifndef IPTRAF_NG_PROMISC_H
#define IPTRAF_NG_PROMISC_H
-/*
- * promisc.h - definitions for promiscuous state save/recovery
- *
- * Thanks to Holger Friese
- * <evildead(a)bs-pc5.et-inf.fho-emden.de> for the base patch.
- * Applied it, but then additional issues came up and I ended up doing more
- * than slight modifications. struct iflist is becoming way too large for
- * comfort and for something as little as this.
- */
+#include "list.h"
-struct promisc_params {
+struct promisc_list {
+ struct list_head list;
char ifname[IFNAMSIZ];
- int saved_state;
+ int flags;
int state_valid;
};
-struct promisc_states {
- struct promisc_params params;
- struct promisc_states *next_entry;
-};
-void init_promisc_list(struct promisc_states **list);
-void save_promisc_list(struct promisc_states *list);
-void load_promisc_list(struct promisc_states **list);
-void srpromisc(int mode, struct promisc_states *promisc_list);
-void destroy_promisc_list(struct promisc_states **list);
+void init_promisc_list(struct list_head *promisc);
+void promisc_set(struct list_head *promisc);
+void promisc_restore(struct list_head *promisc);
+void promisc_destroy(struct list_head *promisc);
#endif /* IPTRAF_NG_PROMISC_H */
diff --git a/src/serv.c b/src/serv.c
index 513cc94..f0bbd46 100644
--- a/src/serv.c
+++ b/src/serv.c
@@ -779,8 +779,6 @@ void servmon(char *ifname, time_t facilitytime)
FILE *logfile = NULL;
- struct promisc_states *promisc_list;
-
WINDOW *sortwin;
PANEL *sortpanel;
@@ -812,11 +810,10 @@ void servmon(char *ifname, time_t facilitytime)
loadaddports(&ports);
+ LIST_HEAD(promisc);
if (first_active_facility() && options.promisc) {
- init_promisc_list(&promisc_list);
- save_promisc_list(promisc_list);
- srpromisc(1, promisc_list);
- destroy_promisc_list(&promisc_list);
+ init_promisc_list(&promisc);
+ promisc_set(&promisc);
}
adjust_instance_count(PROCCOUNTFILE, 1);
@@ -1091,9 +1088,8 @@ err:
endservent();
if (options.promisc && is_last_instance()) {
- load_promisc_list(&promisc_list);
- srpromisc(0, promisc_list);
- destroy_promisc_list(&promisc_list);
+ promisc_restore(&promisc);
+ promisc_destroy(&promisc);
}
adjust_instance_count(PROCCOUNTFILE, -1);
--
1.7.11.5
10 years, 9 months
[PATCH 0/2] iptraf-ng: ipfilter() simplification
by Vitezslav Samel
Vitezslav Samel (2):
ipfilter(): simplify the code using new port_in_range() function
ipfilter(): simplify the code using new addr_in_net() function
src/ipfilter.c | 80 ++++++++++++++++++++-----------------------------------
1 files changed, 29 insertions(+), 51 deletions(-)
--
1.7.8.4
10 years, 9 months
[PATCH] tcplog_flowrate_msg(): cleanup and fix
by Vitezslav Samel
cleanup: use rate_print() instead of open coded equivalent
fix: we can't return pointer to the local static buffer because
we are calling this func twice in one printf() - the second
invocation will overwrite the result of the first invocation.
This fixes it by passing a buffer as argument to this function.
Signed-off-by: Vitezslav Samel <vitezslav(a)samel.cz>
---
src/tcptable.c | 50 ++++++++++++++++++--------------------------------
1 files changed, 18 insertions(+), 32 deletions(-)
diff --git a/src/tcptable.c b/src/tcptable.c
index d88dc6b..3f492dd 100644
--- a/src/tcptable.c
+++ b/src/tcptable.c
@@ -435,36 +435,17 @@ void addtoclosedlist(struct tcptable *table, struct tcptableent *entry)
}
-static char *tcplog_flowrate_msg(struct tcptableent *entry)
+static char *tcplog_flowrate_msg(struct tcptableent *entry, char *buf,
+ size_t bufsize)
{
- char rateunit[10];
- float rate = 0;
- static char message[60];
- time_t interval;
+ time_t interval = time(NULL) - entry->conn_starttime;
- interval = time(NULL) - entry->conn_starttime;
+ char rbuf[64];
+ rate_print(entry->bcount / interval, rbuf, sizeof(rbuf));
- if (options.actmode == KBITS) {
- strcpy(rateunit, "kbits/s");
-
- if (interval > 0)
- rate =
- (float) (entry->bcount * 8 / 1000) /
- (float) interval;
- else
- rate = (float) (entry->bcount * 8 / 1000);
- } else {
- strcpy(rateunit, "kbytes/s");
-
- if (interval > 0)
- rate =
- (float) (entry->bcount / 1024) / (float) interval;
- else
- rate = (float) (entry->bcount / 1024);
- }
-
- snprintf(message, 60, "avg flow rate %.2f %s", rate, rateunit);
- return message;
+ snprintf(buf, bufsize - 1, "avg flow rate %s", rbuf);
+ buf[bufsize - 1] = '\0';
+ return buf;
}
void write_timeout_log(int logging, FILE *logfile, struct tcptableent *tcpnode)
@@ -472,14 +453,16 @@ void write_timeout_log(int logging, FILE *logfile, struct tcptableent *tcpnode)
char msgstring[MSGSTRING_MAX];
if (logging) {
+ char flowrate1[64];
+ char flowrate2[64];
snprintf(msgstring, MSGSTRING_MAX,
"TCP; Connection %s:%s to %s:%s timed out, %lu packets, %lu bytes, %s; opposite direction %lu packets, %lu bytes, %s",
tcpnode->s_fqdn, tcpnode->s_sname, tcpnode->d_fqdn,
tcpnode->d_sname, tcpnode->pcount, tcpnode->bcount,
- tcplog_flowrate_msg(tcpnode),
+ tcplog_flowrate_msg(tcpnode, flowrate1, sizeof(flowrate1)),
tcpnode->oth_connection->pcount,
tcpnode->oth_connection->bcount,
- tcplog_flowrate_msg(tcpnode->oth_connection));
+ tcplog_flowrate_msg(tcpnode->oth_connection, flowrate2, sizeof(flowrate2)));
writelog(logging, logfile, msgstring);
}
}
@@ -703,10 +686,11 @@ void updateentry(struct tcptable *table, struct tcptableent *tableentry,
tableentry->finack = ntohl(transpacket->ack_seq);
}
if (logging) {
+ char flowrate[64];
sprintf(msgstring,
"FIN sent; %lu packets, %lu bytes, %s",
tableentry->pcount, tableentry->bcount,
- tcplog_flowrate_msg(tableentry));
+ tcplog_flowrate_msg(tableentry, flowrate, sizeof(flowrate)));
writetcplog(logging, logfile, tableentry,
tableentry->psize, msgstring);
@@ -718,13 +702,15 @@ void updateentry(struct tcptable *table, struct tcptableent *tableentry,
addtoclosedlist(table, tableentry);
if (logging) {
+ char flowrate1[64];
+ char flowrate2[64];
snprintf(msgstring, MSGSTRING_MAX,
"Connection reset; %lu packets, %lu bytes, %s; opposite direction %lu packets, %lu bytes; %s",
tableentry->pcount, tableentry->bcount,
- tcplog_flowrate_msg(tableentry),
+ tcplog_flowrate_msg(tableentry, flowrate1, sizeof(flowrate1)),
tableentry->oth_connection->pcount,
tableentry->oth_connection->bcount,
- tcplog_flowrate_msg(tableentry->oth_connection));
+ tcplog_flowrate_msg(tableentry->oth_connection, flowrate2, sizeof(flowrate2)));
writetcplog(logging, logfile, tableentry,
tableentry->psize, msgstring);
}
--
1.7.8.4
10 years, 9 months
[PATCH] sockaddr_ntop(): kill warning on printf argument
by Vitezslav Samel
gcc-4.7.0 on x86-64 spills out this warning:
src/sockaddr.c: In function "sockaddr_ntop":
src/sockaddr.c:131:4: warning: format "%u" expects argument of type
"unsigned int", but argument 3 has type
"size_t" [-Wformat]
Change the "%u" format to "%zu" (available since glibc-2.1) to prevent
this warning.
Signed-off-by: Vitezslav Samel <vitezslav(a)samel.cz>
---
src/sockaddr.c | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
diff --git a/src/sockaddr.c b/src/sockaddr.c
index 6ffc989..f5de68c 100644
--- a/src/sockaddr.c
+++ b/src/sockaddr.c
@@ -128,7 +128,7 @@ void sockaddr_ntop(struct sockaddr_storage *addr, char *buf, size_t buflen)
if (ret == NULL) {
switch (errno) {
case ENOSPC:
- die("%s(): buffer too small (must be at least %u bytes)", __FUNCTION__, minlen);
+ die("%s(): buffer too small (must be at least %zu bytes)", __FUNCTION__, minlen);
case EAFNOSUPPORT:
die("%s(): Unknown address family", __FUNCTION__);
}
--
1.7.8.4
10 years, 9 months
[PATCH 0/2] iptraf-ng: cleanup
by Vitezslav Samel
Vitezslav Samel (2):
push ntohs(port) down to the point we're getting it from packet
change port-like variables to type in_port_t
src/fltdefs.h | 8 ++++----
src/ipfilter.c | 25 +++++++++++++++----------
src/ipfilter.h | 4 ++--
src/ipfrag.c | 12 ++++++------
src/ipfrag.h | 8 ++++----
src/itrafmon.c | 6 +++---
src/packet.c | 33 +++++++++++++++------------------
src/packet.h | 5 ++---
src/serv.c | 24 ++++++++++++------------
src/serv.h | 4 ++--
10 files changed, 65 insertions(+), 64 deletions(-)
--
1.7.8.4
10 years, 9 months
[PATCH] screen_update_needed(): merge the same code into the function
by Vitezslav Samel
The test if we need to update the display is the same in 5 files;
make this a function: code is more readable, code is smaller,
code is (marginally) faster (I eliminated 64-bit multiply).
Signed-off-by: Vitezslav Samel <vitezslav(a)samel.cz>
---
src/deskman.c | 17 +++++++++++++++++
src/deskman.h | 1 +
src/detstats.c | 15 +++++----------
src/hostmon.c | 15 +++++----------
src/ifstats.c | 15 +++++----------
src/iptraf-ng-compat.h | 3 +++
src/itrafmon.c | 15 +++++----------
src/options.h | 2 --
src/packet.c | 2 +-
src/pktsize.c | 15 +++++----------
src/serv.c | 15 +++++----------
11 files changed, 52 insertions(+), 63 deletions(-)
diff --git a/src/deskman.c b/src/deskman.c
index 25940dc..e180ea4 100644
--- a/src/deskman.c
+++ b/src/deskman.c
@@ -14,6 +14,7 @@ deskman.c - desktop management routines
#include "tui/winops.h"
#include "deskman.h"
+#include "options.h"
/* Attribute variables */
@@ -194,6 +195,22 @@ void printlargenum(unsigned long long i, WINDOW * win)
wprintw(win, "%8lluT", i / 1000000000000ULL);
}
+int screen_update_needed(const struct timeval *now, const struct timeval *last)
+{
+ unsigned long msecs = timeval_diff_msec(now, last);
+ if (options.updrate == 0) {
+ if (msecs >= DEFAULT_UPDATE_DELAY)
+ return 1;
+ else
+ return 0;
+ } else {
+ if (msecs >= (options.updrate * 1000UL))
+ return 1;
+ else
+ return 0;
+ }
+}
+
void standardcolors(int color)
{
if ((color) && (has_colors())) {
diff --git a/src/deskman.h b/src/deskman.h
index a745f0c..7536459 100644
--- a/src/deskman.h
+++ b/src/deskman.h
@@ -17,6 +17,7 @@ void scrollkeyhelp(void);
void stdexitkeyhelp(void);
void indicate(char *message);
void printlargenum(unsigned long long i, WINDOW * win);
+int screen_update_needed(const struct timeval *now, const struct timeval *last);
void infobox(char *text, char *prompt);
void standardcolors(int color);
void show_sort_statwin(WINDOW **, PANEL **);
diff --git a/src/detstats.c b/src/detstats.c
index 6fbee28..c1e415f 100644
--- a/src/detstats.c
+++ b/src/detstats.c
@@ -269,13 +269,11 @@ void detstats(char *iface, time_t facilitytime)
struct timeval tv;
struct timeval start_tv;
- time_t updtime = 0;
- unsigned long long updtime_usec = 0;
+ struct timeval updtime;
time_t starttime;
time_t now;
time_t statbegin;
time_t startlog;
- unsigned long long unow;
struct proto_counter span;
@@ -380,6 +378,7 @@ void detstats(char *iface, time_t facilitytime)
gettimeofday(&tv, NULL);
start_tv = tv;
+ updtime = tv;
starttime = startlog = statbegin = tv.tv_sec;
leaveok(statwin, TRUE);
@@ -405,7 +404,6 @@ void detstats(char *iface, time_t facilitytime)
while (!exitloop) {
gettimeofday(&tv, NULL);
now = tv.tv_sec;
- unow = tv.tv_sec * 1000000ULL + tv.tv_usec;
if ((now - starttime) >= 1) {
char buf[64];
@@ -482,15 +480,12 @@ void detstats(char *iface, time_t facilitytime)
}
}
- if (((options.updrate == 0)
- && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY))
- || ((options.updrate != 0)
- && (now - updtime >= options.updrate))) {
+ if (screen_update_needed(&tv, &updtime)) {
printdetails(&ifcounts, statwin);
update_panels();
doupdate();
- updtime_usec = unow;
- updtime = now;
+
+ updtime = tv;
}
if ((facilitytime != 0)
diff --git a/src/hostmon.c b/src/hostmon.c
index 5ba7f3c..21b1797 100644
--- a/src/hostmon.c
+++ b/src/hostmon.c
@@ -760,11 +760,9 @@ void hostmon(time_t facilitytime, char *ifptr)
struct timeval tv;
struct timeval tv_rate;
time_t now = 0;
- unsigned long long unow = 0;
time_t statbegin = 0;
time_t startlog = 0;
- time_t updtime = 0;
- unsigned long long updtime_usec = 0;
+ struct timeval updtime;
struct eth_desc *list = NULL;
@@ -857,6 +855,7 @@ void hostmon(time_t facilitytime, char *ifptr)
exitloop = 0;
gettimeofday(&tv, NULL);
tv_rate = tv;
+ updtime = tv;
statbegin = startlog = tv.tv_sec;
PACKET_INIT(pkt);
@@ -864,7 +863,6 @@ void hostmon(time_t facilitytime, char *ifptr)
do {
gettimeofday(&tv, NULL);
now = tv.tv_sec;
- unow = tv.tv_sec * 1000000ULL + tv.tv_usec;
unsigned long msecs = timeval_diff_msec(&tv, &tv_rate);
if (msecs >= 1000) {
@@ -881,14 +879,11 @@ void hostmon(time_t facilitytime, char *ifptr)
startlog = now;
}
}
- if (((options.updrate != 0)
- && (now - updtime >= options.updrate))
- || ((options.updrate == 0)
- && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY))) {
+ if (screen_update_needed(&tv, &updtime)) {
update_panels();
doupdate();
- updtime = now;
- updtime_usec = unow;
+
+ updtime = tv;
}
if ((facilitytime != 0)
diff --git a/src/ifstats.c b/src/ifstats.c
index fa64619..8214838 100644
--- a/src/ifstats.c
+++ b/src/ifstats.c
@@ -448,10 +448,8 @@ void ifstats(time_t facilitytime)
time_t statbegin = 0;
time_t now = 0;
struct timeval start_tv;
- unsigned long long unow = 0;
time_t startlog = 0;
- time_t updtime = 0;
- unsigned long long updtime_usec = 0;
+ struct timeval updtime;
struct promisc_states *promisc_list;
@@ -517,6 +515,7 @@ void ifstats(time_t facilitytime)
exitloop = 0;
gettimeofday(&tv, NULL);
start_tv = tv;
+ updtime = tv;
starttime = startlog = statbegin = tv.tv_sec;
PACKET_INIT(pkt);
@@ -524,7 +523,6 @@ void ifstats(time_t facilitytime)
while (!exitloop) {
gettimeofday(&tv, NULL);
now = tv.tv_sec;
- unow = tv.tv_sec * 1000000ULL + tv.tv_usec;
if ((now - starttime) >= 1) {
unsigned long msecs;
@@ -545,14 +543,11 @@ void ifstats(time_t facilitytime)
startlog = now;
}
}
- if (((options.updrate != 0)
- && (now - updtime >= options.updrate))
- || ((options.updrate == 0)
- && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY))) {
+ if (screen_update_needed(&tv, &updtime)) {
update_panels();
doupdate();
- updtime = now;
- updtime_usec = unow;
+
+ updtime = tv;
}
if ((facilitytime != 0)
diff --git a/src/iptraf-ng-compat.h b/src/iptraf-ng-compat.h
index 8aa0be0..64b3db0 100644
--- a/src/iptraf-ng-compat.h
+++ b/src/iptraf-ng-compat.h
@@ -61,6 +61,9 @@
#define __unused __attribute__((unused))
#define __printf(x, y) __attribute__((format(printf, (x), (y))))
+/* screen delay (in msecs) if update rate == 0 */
+#define DEFAULT_UPDATE_DELAY 50
+
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#define alloc_nr(x) (((x)+16)*3/2)
diff --git a/src/itrafmon.c b/src/itrafmon.c
index 55cb52c..597920c 100644
--- a/src/itrafmon.c
+++ b/src/itrafmon.c
@@ -560,9 +560,7 @@ void ipmon(time_t facilitytime, char *ifptr)
time_t starttime = 0;
time_t now = 0;
time_t timeint = 0;
- time_t updtime = 0;
- unsigned long long updtime_usec = 0;
- unsigned long long unow = 0;
+ struct timeval updtime;
time_t closedint = 0;
WINDOW *statwin;
@@ -722,6 +720,7 @@ void ipmon(time_t facilitytime, char *ifptr)
exitloop = 0;
gettimeofday(&tv, NULL);
tv_rate = tv;
+ updtime = tv;
starttime = timeint = closedint = tv.tv_sec;
PACKET_INIT(pkt);
@@ -731,7 +730,6 @@ void ipmon(time_t facilitytime, char *ifptr)
gettimeofday(&tv, NULL);
now = tv.tv_sec;
- unow = tv.tv_sec * 1000000ULL + tv.tv_usec;
/*
* Print timer at bottom of screen
@@ -759,14 +757,11 @@ void ipmon(time_t facilitytime, char *ifptr)
* Update screen at configured intervals.
*/
- if (((options.updrate != 0)
- && (now - updtime >= options.updrate))
- || ((options.updrate == 0)
- && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY))) {
+ if (screen_update_needed(&tv, &updtime)) {
update_panels();
doupdate();
- updtime = now;
- updtime_usec = unow;
+
+ updtime = tv;
}
/*
diff --git a/src/options.h b/src/options.h
index b47c243..2304600 100644
--- a/src/options.h
+++ b/src/options.h
@@ -10,8 +10,6 @@ struct OPTIONS {
time_t closedint;
};
-#define DEFAULT_UPDATE_DELAY 50000 /* usec screen delay if update rate 0 */
-
extern struct OPTIONS options;
void setoptions(void);
diff --git a/src/packet.c b/src/packet.c
index c831848..d881e14 100644
--- a/src/packet.c
+++ b/src/packet.c
@@ -141,7 +141,7 @@ int packet_get(int fd, struct pkt_hdr *pkt, int *ch, WINDOW *win)
nfds++;
}
do {
- ss = poll(pfds, nfds, DEFAULT_UPDATE_DELAY / 1000);
+ ss = poll(pfds, nfds, DEFAULT_UPDATE_DELAY);
} while ((ss == -1) && (errno == EINTR));
PACKET_INIT_STRUCT(pkt);
diff --git a/src/pktsize.c b/src/pktsize.c
index 296b8fa..44bc61f 100644
--- a/src/pktsize.c
+++ b/src/pktsize.c
@@ -156,9 +156,7 @@ void packet_size_breakdown(char *ifname, time_t facilitytime)
struct timeval tv;
time_t starttime, startlog, timeint;
time_t now;
- unsigned long long unow;
- time_t updtime = 0;
- unsigned long long updtime_usec = 0;
+ struct timeval updtime;
int logging = options.logging;
FILE *logfile = NULL;
@@ -240,6 +238,7 @@ void packet_size_breakdown(char *ifname, time_t facilitytime)
exitloop = 0;
gettimeofday(&tv, NULL);
+ updtime = tv;
now = starttime = startlog = timeint = tv.tv_sec;
if (first_active_facility() && options.promisc) {
@@ -266,16 +265,12 @@ void packet_size_breakdown(char *ifname, time_t facilitytime)
do {
gettimeofday(&tv, NULL);
now = tv.tv_sec;
- unow = tv.tv_sec * 1000000ULL + tv.tv_usec;
- if (((options.updrate != 0)
- && (now - updtime >= options.updrate))
- || ((options.updrate == 0)
- && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY))) {
+ if (screen_update_needed(&tv, &updtime)) {
update_panels();
doupdate();
- updtime = now;
- updtime_usec = unow;
+
+ updtime = tv;
}
if (now - timeint >= 5) {
printelapsedtime(starttime, now, LINES - 3, 1,
diff --git a/src/serv.c b/src/serv.c
index 1fbf877..fdadf03 100644
--- a/src/serv.c
+++ b/src/serv.c
@@ -768,9 +768,7 @@ void servmon(char *ifname, time_t facilitytime)
struct timeval tv_rate;
time_t starttime, startlog, timeint;
time_t now;
- unsigned long long unow;
- time_t updtime = 0;
- unsigned long long updtime_usec = 0;
+ struct timeval updtime;
unsigned int tot_br;
@@ -866,6 +864,7 @@ void servmon(char *ifname, time_t facilitytime)
exitloop = 0;
gettimeofday(&tv, NULL);
tv_rate = tv;
+ updtime = tv;
starttime = startlog = timeint = tv.tv_sec;
wattrset(statwin, IPSTATATTR);
@@ -888,7 +887,6 @@ void servmon(char *ifname, time_t facilitytime)
while (!exitloop) {
gettimeofday(&tv, NULL);
now = tv.tv_sec;
- unow = tv.tv_sec * 1000000ULL + tv.tv_usec;
if (now - timeint >= 5) {
printelapsedtime(starttime, now, LINES - 4, 20,
@@ -916,14 +914,11 @@ void servmon(char *ifname, time_t facilitytime)
tv_rate = tv;
}
- if (((options.updrate != 0)
- && (now - updtime >= options.updrate))
- || ((options.updrate == 0)
- && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY))) {
+ if (screen_update_needed(&tv, &updtime)) {
update_panels();
doupdate();
- updtime = now;
- updtime_usec = unow;
+
+ updtime = tv;
}
if ((facilitytime != 0)
--
1.7.8.4
10 years, 9 months
[PATCH] update_needed(): merge the same code into the function
by Vitezslav Samel
The test if we need to update the display is the same in 5 files;
make this a function: code is more readable, code is smaller,
code is (marginally) faster (I eliminated 64-bit multiply).
Signed-off-by: Vitezslav Samel <vitezslav(a)samel.cz>
---
src/deskman.c | 17 +++++++++++++++++
src/deskman.h | 1 +
src/detstats.c | 15 +++++----------
src/hostmon.c | 15 +++++----------
src/ifstats.c | 15 +++++----------
src/iptraf-ng-compat.h | 3 +++
src/itrafmon.c | 15 +++++----------
src/options.h | 2 --
src/packet.c | 2 +-
src/pktsize.c | 15 +++++----------
src/serv.c | 15 +++++----------
11 files changed, 52 insertions(+), 63 deletions(-)
diff --git a/src/deskman.c b/src/deskman.c
index 25940dc..e0a76cf 100644
--- a/src/deskman.c
+++ b/src/deskman.c
@@ -14,6 +14,7 @@ deskman.c - desktop management routines
#include "tui/winops.h"
#include "deskman.h"
+#include "options.h"
/* Attribute variables */
@@ -194,6 +195,22 @@ void printlargenum(unsigned long long i, WINDOW * win)
wprintw(win, "%8lluT", i / 1000000000000ULL);
}
+int update_needed(const struct timeval *now, const struct timeval *last)
+{
+ unsigned long msecs = timeval_diff_msec(now, last);
+ if (options.updrate == 0) {
+ if (msecs >= DEFAULT_UPDATE_DELAY)
+ return 1;
+ else
+ return 0;
+ } else {
+ if (msecs >= (options.updrate * 1000UL))
+ return 1;
+ else
+ return 0;
+ }
+}
+
void standardcolors(int color)
{
if ((color) && (has_colors())) {
diff --git a/src/deskman.h b/src/deskman.h
index a745f0c..eebcfc3 100644
--- a/src/deskman.h
+++ b/src/deskman.h
@@ -17,6 +17,7 @@ void scrollkeyhelp(void);
void stdexitkeyhelp(void);
void indicate(char *message);
void printlargenum(unsigned long long i, WINDOW * win);
+int update_needed(const struct timeval *now, const struct timeval *last);
void infobox(char *text, char *prompt);
void standardcolors(int color);
void show_sort_statwin(WINDOW **, PANEL **);
diff --git a/src/detstats.c b/src/detstats.c
index efe7d1c..ca353d3 100644
--- a/src/detstats.c
+++ b/src/detstats.c
@@ -270,13 +270,11 @@ void detstats(char *iface, time_t facilitytime, struct filterstate *ofilter)
struct timeval tv;
struct timeval start_tv;
- time_t updtime = 0;
- unsigned long long updtime_usec = 0;
+ struct timeval updtime;
time_t starttime;
time_t now;
time_t statbegin;
time_t startlog;
- unsigned long long unow;
struct proto_counter span;
@@ -381,6 +379,7 @@ void detstats(char *iface, time_t facilitytime, struct filterstate *ofilter)
gettimeofday(&tv, NULL);
start_tv = tv;
+ updtime = tv;
starttime = startlog = statbegin = tv.tv_sec;
leaveok(statwin, TRUE);
@@ -406,7 +405,6 @@ void detstats(char *iface, time_t facilitytime, struct filterstate *ofilter)
while (!exitloop) {
gettimeofday(&tv, NULL);
now = tv.tv_sec;
- unow = tv.tv_sec * 1000000ULL + tv.tv_usec;
if ((now - starttime) >= 1) {
char buf[64];
@@ -483,15 +481,12 @@ void detstats(char *iface, time_t facilitytime, struct filterstate *ofilter)
}
}
- if (((options.updrate == 0)
- && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY))
- || ((options.updrate != 0)
- && (now - updtime >= options.updrate))) {
+ if (update_needed(&tv, &updtime)) {
printdetails(&ifcounts, statwin);
update_panels();
doupdate();
- updtime_usec = unow;
- updtime = now;
+
+ updtime = tv;
}
if ((facilitytime != 0)
diff --git a/src/hostmon.c b/src/hostmon.c
index 341a4a2..6ad9340 100644
--- a/src/hostmon.c
+++ b/src/hostmon.c
@@ -761,11 +761,9 @@ void hostmon(time_t facilitytime, char *ifptr, struct filterstate *ofilter)
struct timeval tv;
struct timeval tv_rate;
time_t now = 0;
- unsigned long long unow = 0;
time_t statbegin = 0;
time_t startlog = 0;
- time_t updtime = 0;
- unsigned long long updtime_usec = 0;
+ struct timeval updtime;
struct eth_desc *list = NULL;
@@ -858,6 +856,7 @@ void hostmon(time_t facilitytime, char *ifptr, struct filterstate *ofilter)
exitloop = 0;
gettimeofday(&tv, NULL);
tv_rate = tv;
+ updtime = tv;
statbegin = startlog = tv.tv_sec;
PACKET_INIT(pkt);
@@ -865,7 +864,6 @@ void hostmon(time_t facilitytime, char *ifptr, struct filterstate *ofilter)
do {
gettimeofday(&tv, NULL);
now = tv.tv_sec;
- unow = tv.tv_sec * 1000000ULL + tv.tv_usec;
unsigned long msecs = timeval_diff_msec(&tv, &tv_rate);
if (msecs >= 1000) {
@@ -882,14 +880,11 @@ void hostmon(time_t facilitytime, char *ifptr, struct filterstate *ofilter)
startlog = now;
}
}
- if (((options.updrate != 0)
- && (now - updtime >= options.updrate))
- || ((options.updrate == 0)
- && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY))) {
+ if (update_needed(&tv, &updtime)) {
update_panels();
doupdate();
- updtime = now;
- updtime_usec = unow;
+
+ updtime = tv;
}
if ((facilitytime != 0)
diff --git a/src/ifstats.c b/src/ifstats.c
index 35a5e18..a66cab8 100644
--- a/src/ifstats.c
+++ b/src/ifstats.c
@@ -449,10 +449,8 @@ void ifstats(struct filterstate *ofilter, time_t facilitytime)
time_t statbegin = 0;
time_t now = 0;
struct timeval start_tv;
- unsigned long long unow = 0;
time_t startlog = 0;
- time_t updtime = 0;
- unsigned long long updtime_usec = 0;
+ struct timeval updtime;
struct promisc_states *promisc_list;
@@ -518,6 +516,7 @@ void ifstats(struct filterstate *ofilter, time_t facilitytime)
exitloop = 0;
gettimeofday(&tv, NULL);
start_tv = tv;
+ updtime = tv;
starttime = startlog = statbegin = tv.tv_sec;
PACKET_INIT(pkt);
@@ -525,7 +524,6 @@ void ifstats(struct filterstate *ofilter, time_t facilitytime)
while (!exitloop) {
gettimeofday(&tv, NULL);
now = tv.tv_sec;
- unow = tv.tv_sec * 1000000ULL + tv.tv_usec;
if ((now - starttime) >= 1) {
unsigned long msecs;
@@ -546,14 +544,11 @@ void ifstats(struct filterstate *ofilter, time_t facilitytime)
startlog = now;
}
}
- if (((options.updrate != 0)
- && (now - updtime >= options.updrate))
- || ((options.updrate == 0)
- && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY))) {
+ if (update_needed(&tv, &updtime)) {
update_panels();
doupdate();
- updtime = now;
- updtime_usec = unow;
+
+ updtime = tv;
}
if ((facilitytime != 0)
diff --git a/src/iptraf-ng-compat.h b/src/iptraf-ng-compat.h
index 8aa0be0..64b3db0 100644
--- a/src/iptraf-ng-compat.h
+++ b/src/iptraf-ng-compat.h
@@ -61,6 +61,9 @@
#define __unused __attribute__((unused))
#define __printf(x, y) __attribute__((format(printf, (x), (y))))
+/* screen delay (in msecs) if update rate == 0 */
+#define DEFAULT_UPDATE_DELAY 50
+
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#define alloc_nr(x) (((x)+16)*3/2)
diff --git a/src/itrafmon.c b/src/itrafmon.c
index d902407..531315e 100644
--- a/src/itrafmon.c
+++ b/src/itrafmon.c
@@ -561,9 +561,7 @@ void ipmon(struct filterstate *ofilter, time_t facilitytime, char *ifptr)
time_t starttime = 0;
time_t now = 0;
time_t timeint = 0;
- time_t updtime = 0;
- unsigned long long updtime_usec = 0;
- unsigned long long unow = 0;
+ struct timeval updtime;
time_t closedint = 0;
WINDOW *statwin;
@@ -723,6 +721,7 @@ void ipmon(struct filterstate *ofilter, time_t facilitytime, char *ifptr)
exitloop = 0;
gettimeofday(&tv, NULL);
tv_rate = tv;
+ updtime = tv;
starttime = timeint = closedint = tv.tv_sec;
PACKET_INIT(pkt);
@@ -732,7 +731,6 @@ void ipmon(struct filterstate *ofilter, time_t facilitytime, char *ifptr)
gettimeofday(&tv, NULL);
now = tv.tv_sec;
- unow = tv.tv_sec * 1000000ULL + tv.tv_usec;
/*
* Print timer at bottom of screen
@@ -760,14 +758,11 @@ void ipmon(struct filterstate *ofilter, time_t facilitytime, char *ifptr)
* Update screen at configured intervals.
*/
- if (((options.updrate != 0)
- && (now - updtime >= options.updrate))
- || ((options.updrate == 0)
- && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY))) {
+ if (update_needed(&tv, &updtime)) {
update_panels();
doupdate();
- updtime = now;
- updtime_usec = unow;
+
+ updtime = tv;
}
/*
diff --git a/src/options.h b/src/options.h
index b47c243..2304600 100644
--- a/src/options.h
+++ b/src/options.h
@@ -10,8 +10,6 @@ struct OPTIONS {
time_t closedint;
};
-#define DEFAULT_UPDATE_DELAY 50000 /* usec screen delay if update rate 0 */
-
extern struct OPTIONS options;
void setoptions(void);
diff --git a/src/packet.c b/src/packet.c
index ef76925..65cf352 100644
--- a/src/packet.c
+++ b/src/packet.c
@@ -141,7 +141,7 @@ int packet_get(int fd, struct pkt_hdr *pkt, int *ch, WINDOW *win)
nfds++;
}
do {
- ss = poll(pfds, nfds, DEFAULT_UPDATE_DELAY / 1000);
+ ss = poll(pfds, nfds, DEFAULT_UPDATE_DELAY);
} while ((ss == -1) && (errno == EINTR));
PACKET_INIT_STRUCT(pkt);
diff --git a/src/pktsize.c b/src/pktsize.c
index ff5604d..218ef68 100644
--- a/src/pktsize.c
+++ b/src/pktsize.c
@@ -158,9 +158,7 @@ void packet_size_breakdown(char *ifname, time_t facilitytime,
struct timeval tv;
time_t starttime, startlog, timeint;
time_t now;
- unsigned long long unow;
- time_t updtime = 0;
- unsigned long long updtime_usec = 0;
+ struct timeval updtime;
int logging = options.logging;
FILE *logfile = NULL;
@@ -242,6 +240,7 @@ void packet_size_breakdown(char *ifname, time_t facilitytime,
exitloop = 0;
gettimeofday(&tv, NULL);
+ updtime = tv;
now = starttime = startlog = timeint = tv.tv_sec;
if (first_active_facility() && options.promisc) {
@@ -268,16 +267,12 @@ void packet_size_breakdown(char *ifname, time_t facilitytime,
do {
gettimeofday(&tv, NULL);
now = tv.tv_sec;
- unow = tv.tv_sec * 1000000ULL + tv.tv_usec;
- if (((options.updrate != 0)
- && (now - updtime >= options.updrate))
- || ((options.updrate == 0)
- && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY))) {
+ if (update_needed(&tv, &updtime)) {
update_panels();
doupdate();
- updtime = now;
- updtime_usec = unow;
+
+ updtime = tv;
}
if (now - timeint >= 5) {
printelapsedtime(starttime, now, LINES - 3, 1,
diff --git a/src/serv.c b/src/serv.c
index 1491ae5..4fdc96b 100644
--- a/src/serv.c
+++ b/src/serv.c
@@ -769,9 +769,7 @@ void servmon(char *ifname, time_t facilitytime, struct filterstate *ofilter)
struct timeval tv_rate;
time_t starttime, startlog, timeint;
time_t now;
- unsigned long long unow;
- time_t updtime = 0;
- unsigned long long updtime_usec = 0;
+ struct timeval updtime;
unsigned int tot_br;
@@ -867,6 +865,7 @@ void servmon(char *ifname, time_t facilitytime, struct filterstate *ofilter)
exitloop = 0;
gettimeofday(&tv, NULL);
tv_rate = tv;
+ updtime = tv;
starttime = startlog = timeint = tv.tv_sec;
wattrset(statwin, IPSTATATTR);
@@ -889,7 +888,6 @@ void servmon(char *ifname, time_t facilitytime, struct filterstate *ofilter)
while (!exitloop) {
gettimeofday(&tv, NULL);
now = tv.tv_sec;
- unow = tv.tv_sec * 1000000ULL + tv.tv_usec;
if (now - timeint >= 5) {
printelapsedtime(starttime, now, LINES - 4, 20,
@@ -917,14 +915,11 @@ void servmon(char *ifname, time_t facilitytime, struct filterstate *ofilter)
tv_rate = tv;
}
- if (((options.updrate != 0)
- && (now - updtime >= options.updrate))
- || ((options.updrate == 0)
- && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY))) {
+ if (update_needed(&tv, &updtime)) {
update_panels();
doupdate();
- updtime = now;
- updtime_usec = unow;
+
+ updtime = tv;
}
if ((facilitytime != 0)
--
1.7.8.4
10 years, 9 months
[PATCH 0/3] iptraf-ng: cleanup and fixes
by Vitezslav Samel
Vitezslav Samel (3):
kill unfinished work on graphing logging
fix: show program interface only if not given any command line arg
fix: clear screen before exit when given arg on cmdline
src/iptraf.c | 53 ++++++++++++-----------------------------------------
src/log.c | 1 -
src/logvars.h | 1 -
3 files changed, 12 insertions(+), 43 deletions(-)
--
1.7.8.4
10 years, 9 months
[PATCH 0/2] iptraf-ng: make "struct filterstate ofilter" global
by Vitezslav Samel
Vitezslav Samel (2):
make struct filterstate ofilter global
don't pass ofilter variable to functions when not needed
src/detstats.c | 4 +--
src/detstats.h | 4 +--
src/fltselect.c | 62 +++++++++++++++++++++++++++---------------------------
src/fltselect.h | 10 +++++---
src/hostmon.c | 4 +--
src/hostmon.h | 4 +--
src/ifstats.c | 4 +--
src/ifstats.h | 4 +--
src/ipfilter.c | 23 +++++++++----------
src/ipfilter.h | 7 +----
src/iptraf.c | 42 ++++++++++++++++---------------------
src/itrafmon.c | 5 +--
src/itrafmon.h | 4 +--
src/packet.c | 8 +++---
src/packet.h | 4 +--
src/pktsize.c | 6 +---
src/pktsize.h | 5 +---
src/serv.c | 4 +--
src/serv.h | 5 +---
19 files changed, 87 insertions(+), 122 deletions(-)
--
1.7.8.4
10 years, 9 months
[PATCH 0/5] iptraf-ng: make "struct OPTIONS options" global
by Vitezslav Samel
There's no point in passing the struct OPTIONS pointer to every
other function; this patch make the "struct OPTIONS options" variable
global and fixes every reference to it. Entire patch series also makes
iptraf-ng code size by 1,5 kB smaller.
Vitezslav Samel (5):
make struct OPTIONS options global
move options.actmode variable passing one layer down
push options.actmode variable passing one more layer down
push options.servnames variable down ...
push options.mac variable down ...
src/detstats.c | 44 ++++++++++-----------
src/detstats.h | 4 +-
src/hostmon.c | 48 ++++++++++-------------
src/hostmon.h | 4 +-
src/ifstats.c | 39 +++++++++----------
src/ifstats.h | 4 +-
src/iptraf.c | 39 +++++++++----------
src/itrafmon.c | 98 +++++++++++++++++++++--------------------------
src/itrafmon.h | 4 +-
src/options.c | 114 ++++++++++++++++++++++++++++---------------------------
src/options.h | 8 ++-
src/othptab.c | 29 +++++---------
src/othptab.h | 6 +--
src/pktsize.c | 18 ++++----
src/pktsize.h | 5 +-
src/rate.c | 9 ++--
src/rate.h | 4 +-
src/serv.c | 74 +++++++++++++++++-------------------
src/serv.h | 4 +-
src/servname.c | 6 +-
src/servname.h | 3 +-
src/tcptable.c | 57 ++++++++++++----------------
src/tcptable.h | 13 +++---
23 files changed, 290 insertions(+), 344 deletions(-)
--
1.7.8.4
10 years, 9 months