4
4
ifstats.c - the interface statistics module
5
5
Written by Gerard Paul Java
6
Copyright (c) Gerard Paul Java 1997
6
Copyright (c) Gerard Paul Java 1997-2002
8
8
This software is open source; you can redistribute it and/or modify
9
9
it under the terms of the GNU General Public License as published by
242
void no_ifaces_error(void)
244
write_error("No active interfaces. Check their status or the /proc filesystem",
235
248
void updaterates(struct iftab *table, int unit, time_t starttime,
236
249
time_t now, unsigned int idx)
322
335
void initiftab(struct iftab *table)
324
initiflist(&(table->head));
326
337
table->borderwin = newwin(LINES - 2, COLS, 1, 0);
327
338
table->borderpanel = new_panel(table->borderwin);
334
345
labelstats(table->borderwin);
335
346
table->statwin = newwin(LINES - 4, COLS - 2, 2, 1);
336
347
table->statpanel = new_panel(table->statwin);
337
stdwinset(table->statwin);
348
tx_stdwinset(table->statwin);
338
349
wtimeout(table->statwin, -1);
339
350
wattrset(table->statwin, STDATTR);
340
colorwin(table->statwin);
351
tx_colorwin(table->statwin);
341
352
wattrset(table->statwin, BOXATTR);
342
353
wmove(table->borderwin, LINES - 3, 32 * COLS / 80);
343
354
wprintw(table->borderwin,
401
412
* The general interface statistics function
404
void ifstats(const struct OPTIONS *options, int facilitytime)
415
void ifstats(const struct OPTIONS *options, struct filterstate *ofilter,
406
418
int logging = options->logging;
407
419
struct iftab table;
409
struct isdntab isdntable;
411
421
char buf[MAX_PACKET_SIZE];
412
char aligned_buf[120];
414
struct iphdr *ipacket;
417
425
struct sockaddr_ll fromaddr;
418
426
unsigned short linktype;
428
unsigned sport, dport;
420
430
struct iflist *ptmp = NULL;
422
432
unsigned int idx = 1;
425
435
FILE *logfile = NULL;
436
442
struct timeval tv;
462
initiflist(&(table.head));
463
if (table.head == NULL) {
465
unmark_facility(GSTATIDFILE, "");
456
470
open_socket(&fd);
459
473
unmark_facility(GSTATIDFILE, "");
464
477
if ((first_active_facility()) && (options->promisc)) {
465
478
init_promisc_list(&promisc_list);
555
pkt_result = processpacket(buf, &packet, &br, NULL, &sport, &dport,
556
&fromaddr, &linktype, ofilter, ifname, NULL);
558
if (pkt_result == INVALID_PACKET)
543
561
positionptr(&table, &ptmp, ifname);
547
565
ptmp->spanbr += br;
550
if (fromaddr.sll_protocol == ntohs(ETH_P_IP)) {
553
isdn_iface_check(&isdn_fd, ifname);
554
linktype = getlinktype(fromaddr.sll_hatype, ifname,
555
isdn_fd, &isdntable);
557
adjustpacket(buf, linktype, &packet, aligned_buf, &br);
562
ipacket = (struct iphdr *) packet;
563
iphlen = ipacket->ihl * 4;
564
tpacket = packet + iphlen;
566
/* recalculate checksum */
568
ipcsum = ipacket->check;
570
ipck = in_cksum((u_short *) ipacket, iphlen);
571
if ((ipcsum != ipck) || (!ipck)) {
568
if (fromaddr.sll_protocol == ETH_P_IP) {
571
if (pkt_result == CHECKSUM_ERROR) {
572
572
(ptmp->badtotal)++;
576
576
(ptmp->noniptotal)++;
637
637
destroyiflist(table.head);
638
destroy_isdn_table(&isdntable);
639
639
unmark_facility(GSTATIDFILE, "");
640
640
strcpy(current_logfile, "");
742
742
* The detailed interface statistics function
745
void detstats(char *iface, const struct OPTIONS *options, int facilitytime)
745
void detstats(char *iface, const struct OPTIONS *options, int facilitytime,
746
struct filterstate *ofilter)
747
748
int logging = options->logging;
750
751
PANEL *statpanel;
752
753
char buf[MAX_PACKET_SIZE];
753
char aligned_buf[120];
755
755
struct iphdr *ipacket = NULL;
758
unsigned int sport, dport;
759
761
struct sockaddr_ll fromaddr;
760
762
unsigned short linktype;
765
766
int framelen = 0;
767
769
FILE *logfile = NULL;
771
771
unsigned int iplen = 0;
773
773
struct iftotals totals;
777
struct isdntab isdntable;
779
777
struct timeval tv;
780
778
unsigned long updtime = 0;
781
779
unsigned long long updtime_usec = 0;
853
851
stdexitkeyhelp();
854
852
statwin = newwin(LINES - 2, COLS, 1, 0);
855
853
statpanel = new_panel(statwin);
854
tx_stdwinset(statwin);
857
855
wtimeout(statwin, -1);
858
856
wattrset(statwin, BOXATTR);
857
tx_colorwin(statwin);
860
858
box(statwin, ACS_VLINE, ACS_HLINE);
861
859
wmove(statwin, 0, 1);
862
860
wprintw(statwin, " Statistics for %s ", iface);
933
if (strcmp(iface, ifname) != 0)
931
pkt_result = processpacket(buf, &packet, &br, NULL,
932
&sport, &dport, &fromaddr, &linktype,
933
ofilter, ifname, iface);
935
if (pkt_result == INVALID_PACKET)
938
totals.bytestotal += br;
939
totals.bytestotal += framelen;
940
941
if (fromaddr.sll_pkttype == PACKET_OUTGOING) {
941
942
totals.total_out++;
942
totals.bytestotal_out += br;
943
totals.bytestotal_out += framelen;
944
spanbr_out += framelen;
946
947
totals.total_in++;
947
totals.bytestotal_in += br;
948
totals.bytestotal_in += framelen;
949
spanbr_in += framelen;
952
953
if (fromaddr.sll_pkttype == PACKET_BROADCAST) {
954
totals.bcastbytes += br;
955
totals.bcastbytes += framelen;
960
if (fromaddr.sll_protocol == ntohs(ETH_P_IP)) {
961
isdn_iface_check(&isdn_fd, ifname);
962
linktype = getlinktype(fromaddr.sll_hatype, ifname,
963
isdn_fd, &isdntable);
965
adjustpacket(buf, linktype, &packet, aligned_buf, &br);
970
ipacket = (struct iphdr *) packet;
971
iphlen = ipacket->ihl * 4;
972
tpacket = packet + iphlen;
974
ipcsum = ipacket->check;
976
ipck = in_cksum((u_short *) ipacket, iphlen);
977
if ((ipcsum != ipck) || (!ipck)) {
961
if (fromaddr.sll_protocol == ETH_P_IP) {
963
if (pkt_result == CHECKSUM_ERROR) {
978
964
totals.badtotal++;
968
ipacket = (struct iphdr *) packet;
969
iphlen = ipacket->ihl * 4;
970
tpacket = packet + iphlen;
981
971
iplen = ntohs(ipacket->tot_len);
983
973
totals.iptotal++;
1168
1158
delwin(statwin);
1169
1159
unmark_facility(DSTATIDFILE, iface);
1170
1160
strcpy(current_logfile, "");
1171
destroy_isdn_table(&isdntable);
1172
1162
update_panels();
1176
1166
void selectiface(char *ifname, int withall, int *aborted)
1188
1170
struct iflist *list;
1189
1171
struct iflist *ptmp;
1173
struct scroll_list scrolllist;
1191
1175
initiflist(&list);
1183
if ((withall) && (list != NULL)) {
1193
1184
ptmp = malloc(sizeof(struct iflist));
1194
1185
strcpy(ptmp->ifname, "All interfaces");
1198
1189
ptmp->next_entry = list;
1202
bw = newwin(14, 24, (LINES - 14) / 2, (COLS - 24) / 2 - 9);
1203
/* I'm guessing (COLS-24)/2-9*COLS/80) is overkill */
1205
wattrset(bw, BOXATTR);
1206
box(bw, ACS_VLINE, ACS_HLINE);
1208
win = newwin(12, 22, (LINES - 14) / 2 + 1, (COLS - 24) / 2 - 8);
1209
panel = new_panel(win);
1211
wattrset(win, BOXATTR);
1214
wprintw(bw, " Select Interface ");
1219
wattrset(win, STDATTR);
1222
wprintw(win, "%s", ptmp->ifname);
1223
ptmp = ptmp->next_entry;
1225
} while ((ptmp != NULL) && (row <= 11));
1233
wattrset(win, PTRATTR);
1235
waddch(win, ACS_RARROW);
1240
wattrset(win, STDATTR);
1243
if (ptmp->next_entry != NULL) {
1250
wprintw(win, "%22c", ' ');
1252
wprintw(win, ptmp->next_entry->ifname);
1255
ptmp = ptmp->next_entry;
1259
if (ptmp->prev_entry != NULL) {
1265
wprintw(win, "%22c", ' ');
1267
wprintw(win, ptmp->prev_entry->ifname);
1269
ptmp = ptmp->prev_entry;
1192
tx_listkeyhelp(STDATTR, HIGHATTR);
1196
tx_init_listbox(&scrolllist, 24, 14, (COLS - 24) / 2 - 9, (LINES - 14) / 2,
1197
STDATTR, BOXATTR, BARSTDATTR, HIGHATTR);
1199
tx_set_listbox_title(&scrolllist, "Select Interface", 1);
1201
while (ptmp != NULL) {
1202
tx_add_list_entry(&scrolllist, (char *) ptmp, ptmp->ifname);
1203
ptmp = ptmp->next_entry;
1206
tx_show_listbox(&scrolllist);
1207
tx_operate_listbox(&scrolllist, &ch, aborted);
1208
tx_close_listbox(&scrolllist);
1210
if (!(*aborted) && (list != NULL)) {
1211
ptmp = (struct iflist *) scrolllist.textptr->nodeptr;
1296
1212
if ((withall) && (ptmp->prev_entry == NULL)) /* All Interfaces */
1297
1213
strcpy(ifname, "");
1299
1215
strcpy(ifname, ptmp->ifname);
1218
tx_destroy_list(&scrolllist);
1301
1219
destroyiflist(list);
1306
1220
update_panels();