... using newly introduced struct psizetab.
Signed-off-by: Vitezslav Samel <vitezslav(a)samel.cz>
---
src/pktsize.c | 253 +++++++++++++++++++++++++++++++---------------------------
1 file changed, 134 insertions(+), 119 deletions(-)
diff --git a/src/pktsize.c b/src/pktsize.c
index 4127b14..3a16454 100644
--- a/src/pktsize.c
+++ b/src/pktsize.c
@@ -31,6 +31,18 @@ struct ifstat_brackets {
unsigned long count;
};
+struct psizetab {
+ WINDOW *win;
+ PANEL *panel;
+ WINDOW *borderwin;
+ PANEL *borderpanel;
+
+ struct ifstat_brackets brackets[20];
+ unsigned int interval;
+
+ int mtu;
+};
+
static void rotate_size_log(int s __unused)
{
rotate_flag = 1;
@@ -38,9 +50,8 @@ static void rotate_size_log(int s __unused)
signal(SIGUSR1, rotate_size_log);
}
-static void write_size_log(struct ifstat_brackets *brackets,
- unsigned long nsecs, char *ifname, unsigned int mtu,
- FILE *logfile)
+static void write_size_log(struct psizetab *table, unsigned long nsecs,
+ char *ifname, FILE *logfile)
{
char atime[TIME_TARGET_MAX];
int i;
@@ -48,110 +59,140 @@ static void write_size_log(struct ifstat_brackets *brackets,
genatime(time(NULL), atime);
fprintf(logfile, "*** Packet Size Distribution, generated %s\n\n",
atime);
- fprintf(logfile, "Interface: %s MTU: %u\n\n", ifname, mtu);
+ fprintf(logfile, "Interface: %s MTU: %u\n\n", ifname, table->mtu);
fprintf(logfile, "Packet Size (bytes)\tCount\n");
for (i = 0; i <= 19; i++) {
- fprintf(logfile, "%u to %u:\t\t%lu\n", brackets[i].floor,
- brackets[i].ceil, brackets[i].count);
+ fprintf(logfile, "%u to %u:\t\t%lu\n", table->brackets[i].floor,
+ table->brackets[i].ceil, table->brackets[i].count);
}
fprintf(logfile, "\nRunning time: %lu seconds\n", nsecs);
fflush(logfile);
}
-static int initialize_brackets(struct ifstat_brackets *brackets,
- unsigned int *interval, int mtu,
- WINDOW *win)
+static void psizetab_init(struct psizetab *table, char *ifname)
+{
+ table->borderwin = newwin(LINES - 2, COLS, 1, 0);
+ table->borderpanel = new_panel(table->borderwin);
+
+ wattrset(table->borderwin, BOXATTR);
+ tx_box(table->borderwin, ACS_VLINE, ACS_HLINE);
+ mvwprintw(table->borderwin, 0, 1, " Packet Distribution by Size ");
+
+ table->win = newwin(LINES - 4, COLS - 2, 2, 1);
+ table->panel = new_panel(table->win);
+
+ tx_stdwinset(table->win);
+ wtimeout(table->win, -1);
+ wattrset(table->win, STDATTR);
+ tx_colorwin(table->win);
+
+ mvwprintw(table->win, 1, 1, "Packet size brackets for interface %s",
ifname);
+ wattrset(table->win, BOXATTR);
+ mvwprintw(table->win, 4, 1, "Packet Size (bytes)");
+ mvwprintw(table->win, 4, 26, "Count");
+ mvwprintw(table->win, 4, 36, "Packet Size (bytes)");
+ mvwprintw(table->win, 4, 60, "Count");
+ wattrset(table->win, HIGHATTR);
+
+ move(LINES - 1, 1);
+ stdexitkeyhelp();
+
+ update_panels();
+ doupdate();
+}
+
+static void psizetab_destroy(struct psizetab *table)
+{
+ del_panel(table->panel);
+ delwin(table->win);
+
+ del_panel(table->borderpanel);
+ delwin(table->borderwin);
+
+ update_panels();
+ doupdate();
+}
+
+static int initialize_brackets(struct psizetab *table)
{
int i;
- *interval = mtu / 20; /* There are 20 packet size brackets */
+ table->interval = table->mtu / 20; /* There are 20 packet size brackets */
for (i = 0; i <= 19; i++) {
- brackets[i].floor = *interval * i + 1;
- brackets[i].ceil = *interval * (i + 1);
- brackets[i].count = 0;
+ table->brackets[i].floor = table->interval * i + 1;
+ table->brackets[i].ceil = table->interval * (i + 1);
+ table->brackets[i].count = 0;
}
- brackets[19].ceil = mtu;
+ table->brackets[19].ceil = table->mtu;
for (i = 0; i <= 9; i++) {
- wattrset(win, STDATTR);
- mvwprintw(win, i + 5, 2, "%4u to %4u:", brackets[i].floor,
- brackets[i].ceil);
- wattrset(win, HIGHATTR);
- mvwprintw(win, i + 5, 23, "%8lu", 0);
+ wattrset(table->win, STDATTR);
+ mvwprintw(table->win, i + 5, 2, "%4u to %4u:",
table->brackets[i].floor,
+ table->brackets[i].ceil);
+ wattrset(table->win, HIGHATTR);
+ mvwprintw(table->win, i + 5, 23, "%8lu", 0);
}
for (i = 10; i <= 19; i++) {
- wattrset(win, STDATTR);
- wmove(win, (i - 10) + 5, 36);
+ wattrset(table->win, STDATTR);
+ wmove(table->win, (i - 10) + 5, 36);
if (i != 19)
- wprintw(win, "%4u to %4u:", brackets[i].floor,
- brackets[i].ceil);
+ wprintw(table->win, "%4u to %4u:", table->brackets[i].floor,
+ table->brackets[i].ceil);
else
- wprintw(win, "%4u to %4u+:", brackets[i].floor,
- brackets[i].ceil);
+ wprintw(table->win, "%4u to %4u+:", table->brackets[i].floor,
+ table->brackets[i].ceil);
- wattrset(win, HIGHATTR);
- mvwprintw(win, (i - 10) + 5, 57, "%8lu", 0);
+ wattrset(table->win, HIGHATTR);
+ mvwprintw(table->win, (i - 10) + 5, 57, "%8lu", 0);
}
- wattrset(win, STDATTR);
- mvwprintw(win, 17, 1,
+ wattrset(table->win, STDATTR);
+ mvwprintw(table->win, 17, 1,
"Interface MTU is %d bytes, not counting the data-link header",
- mtu);
- mvwprintw(win, 18, 1,
+ table->mtu);
+ mvwprintw(table->win, 18, 1,
"Maximum packet size is the MTU plus the data-link header length");
- mvwprintw(win, 19, 1,
+ mvwprintw(table->win, 19, 1,
"Packet size computations include data-link headers, if any");
return 0;
}
-static void update_size_distrib(unsigned int length,
- struct ifstat_brackets *brackets,
- unsigned int interval)
+static void update_size_distrib(struct psizetab *table, unsigned int length)
{
unsigned int i;
- i = (length - 1) / interval; /* minus 1 to keep interval
- boundary lengths within the
- proper brackets */
+ i = (length - 1) / table->interval; /* minus 1 to keep interval
+ boundary lengths within the
+ proper brackets */
if (i > 19) /* This is for extras for MTU's not */
i = 19; /* divisible by 20 */
- brackets[i].count++;
+ table->brackets[i].count++;
}
-static void print_size_distrib(struct ifstat_brackets *brackets, WINDOW *win)
+static void print_size_distrib(struct psizetab *table)
{
for (unsigned int i = 0; i <= 19; i++) {
if (i < 10)
- wmove(win, i + 5, 23);
+ wmove(table->win, i + 5, 23);
else
- wmove(win, (i - 10) + 5, 57);
+ wmove(table->win, (i - 10) + 5, 57);
- wprintw(win, "%8lu", brackets[i].count);
+ wprintw(table->win, "%8lu", table->brackets[i].count);
}
}
void packet_size_breakdown(char *ifname, time_t facilitytime)
{
- WINDOW *win;
- PANEL *panel;
- WINDOW *borderwin;
- PANEL *borderpanel;
-
- struct ifstat_brackets brackets[20];
- unsigned int interval;
-
int ch;
- int mtu;
-
int pkt_result;
struct timeval tv;
@@ -162,6 +203,8 @@ void packet_size_breakdown(char *ifname, time_t facilitytime)
int logging = options.logging;
FILE *logfile = NULL;
+ struct psizetab table;
+
int fd;
struct pkt_hdr pkt;
@@ -170,43 +213,34 @@ void packet_size_breakdown(char *ifname, time_t facilitytime)
if (!dev_up(ifname)) {
err_iface_down();
- goto err_unmark;
- }
-
- mtu = dev_get_mtu(ifname);
- if (mtu < 0) {
- write_error("Unable to obtain interface MTU");
- goto err_unmark;
+ return;
}
- borderwin = newwin(LINES - 2, COLS, 1, 0);
- borderpanel = new_panel(borderwin);
+ psizetab_init(&table, ifname);
- wattrset(borderwin, BOXATTR);
- tx_box(borderwin, ACS_VLINE, ACS_HLINE);
- mvwprintw(borderwin, 0, 1, " Packet Distribution by Size ");
-
- win = newwin(LINES - 4, COLS - 2, 2, 1);
- panel = new_panel(win);
-
- tx_stdwinset(win);
- wtimeout(win, -1);
- wattrset(win, STDATTR);
- tx_colorwin(win);
-
- move(LINES - 1, 1);
- stdexitkeyhelp();
+ LIST_HEAD(promisc);
+ if (options.promisc) {
+ promisc_init(&promisc, ifname);
+ promisc_set_list(&promisc);
+ }
- initialize_brackets(brackets, &interval, mtu, win);
+ fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
+ if(fd == -1) {
+ write_error("Unable to obtain monitoring socket");
+ goto err;
+ }
+ if(dev_bind_ifname(fd, ifname) == -1) {
+ write_error("Unable to bind interface on the socket");
+ goto err_close;
+ }
- mvwprintw(win, 1, 1, "Packet size brackets for interface %s", ifname);
- wattrset(win, BOXATTR);
+ table.mtu = dev_get_mtu(ifname);
+ if (table.mtu < 0) {
+ write_error("Unable to obtain interface MTU");
+ goto err_close;
+ }
- mvwprintw(win, 4, 1, "Packet Size (bytes)");
- mvwprintw(win, 4, 26, "Count");
- mvwprintw(win, 4, 36, "Packet Size (bytes)");
- mvwprintw(win, 4, 60, "Count");
- wattrset(win, HIGHATTR);
+ initialize_brackets(&table);
if (logging) {
if (strcmp(current_logfile, "") == 0) {
@@ -232,35 +266,20 @@ void packet_size_breakdown(char *ifname, time_t facilitytime)
"******** Packet size distribution facility started ********");
}
+ packet_init(&pkt);
+
exitloop = 0;
+
gettimeofday(&tv, NULL);
updtime = tv;
now = starttime = startlog = timeint = tv.tv_sec;
- LIST_HEAD(promisc);
- if (options.promisc) {
- promisc_init(&promisc, ifname);
- promisc_set_list(&promisc);
- }
-
- fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
- if(fd == -1) {
- write_error("Unable to obtain monitoring socket");
- goto err;
- }
- if(dev_bind_ifname(fd, ifname) == -1) {
- write_error("Unable to bind interface on the socket");
- goto err_close;
- }
-
- packet_init(&pkt);
-
do {
gettimeofday(&tv, NULL);
now = tv.tv_sec;
if (screen_update_needed(&tv, &updtime)) {
- print_size_distrib(brackets, win);
+ print_size_distrib(&table);
update_panels();
doupdate();
@@ -268,16 +287,16 @@ void packet_size_breakdown(char *ifname, time_t facilitytime)
updtime = tv;
}
if (now - timeint >= 5) {
- printelapsedtime(now - starttime, 1, borderwin);
+ printelapsedtime(now - starttime, 1, table.borderwin);
dropped += packet_get_dropped(fd);
- print_packet_drops(dropped, borderwin, 49);
+ print_packet_drops(dropped, table.borderwin, 49);
timeint = now;
}
if (logging) {
check_rotate_flag(&logfile);
if ((now - startlog) >= options.logspan) {
- write_size_log(brackets, now - starttime,
- ifname, mtu, logfile);
+ write_size_log(&table, now - starttime,
+ ifname, logfile);
startlog = now;
}
}
@@ -286,7 +305,7 @@ void packet_size_breakdown(char *ifname, time_t facilitytime)
&& (((now - starttime) / 60) >= facilitytime))
exitloop = 1;
- if (packet_get(fd, &pkt, &ch, win) == -1) {
+ if (packet_get(fd, &pkt, &ch, table.win) == -1) {
write_error("Packet receive failed");
exitloop = 1;
break;
@@ -318,31 +337,27 @@ void packet_size_breakdown(char *ifname, time_t facilitytime)
if (pkt_result != PACKET_OK)
continue;
- update_size_distrib(pkt.pkt_len, brackets, interval);
+ update_size_distrib(&table, pkt.pkt_len);
} while (!exitloop);
packet_destroy(&pkt);
-err_close:
- close(fd);
-err:
if (logging) {
signal(SIGUSR1, SIG_DFL);
- write_size_log(brackets, now - starttime, ifname, mtu, logfile);
+ write_size_log(&table, now - starttime, ifname, logfile);
writelog(logging, logfile,
"******** Packet size distribution facility stopped ********");
fclose(logfile);
}
+ strcpy(current_logfile, "");
+err_close:
+ close(fd);
+err:
if (options.promisc) {
promisc_restore_list(&promisc);
promisc_destroy(&promisc);
}
- del_panel(panel);
- delwin(win);
- del_panel(borderpanel);
- delwin(borderwin);
-err_unmark:
- strcpy(current_logfile, "");
+ psizetab_destroy(&table);
}
--
1.9.1