~ubuntu-branches/ubuntu/natty/iptraf/natty-proposed

« back to all changes in this revision

Viewing changes to src/ifstats.c

  • Committer: Bazaar Package Importer
  • Author(s): Frederic Peters
  • Date: 2005-02-26 10:37:01 UTC
  • mfrom: (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050226103701-pqfbo1f0nch5h3rw
Tags: 2.7.0-7
Documentation/iptraf.8: use \- to denote the dash signs before options.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 
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
7
7
 
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
23
23
#include <string.h>
24
24
#include <signal.h>
25
25
#include <unistd.h>
 
26
#include <time.h>
26
27
#include <sys/time.h>
27
28
#include <sys/types.h>
28
29
#include <sys/socket.h>
36
37
#include <netinet/tcp.h>
37
38
#include <netinet/udp.h>
38
39
#include <linux/if_ether.h>
 
40
#include <winops.h>
 
41
#include <labels.h>
 
42
#include <listbox.h>
 
43
#include <msgboxes.h>
39
44
#include "ifstats.h"
40
45
#include "ifaces.h"
41
46
#include "isdntab.h"
 
47
#include "fltdefs.h"
 
48
#include "utfilter.h"
 
49
#include "fltselect.h"
42
50
#include "packet.h"
 
51
#include "options.h"
43
52
#include "log.h"
44
53
#include "dirs.h"
45
 
#include "stdwinset.h"
46
54
#include "deskman.h"
47
55
#include "ipcsum.h"
48
56
#include "attrs.h"
49
 
#include "error.h"
50
57
#include "serv.h"
51
58
#include "timer.h"
52
 
#include "options.h"
53
59
#include "instances.h"
54
60
#include "mode.h"
55
61
#include "logvars.h"
56
62
#include "promisc.h"
 
63
#include "error.h"
57
64
 
58
65
#define SCROLLUP 0
59
66
#define SCROLLDOWN 1
123
130
{
124
131
    FILE *fd;
125
132
    char buf[161];
126
 
    char ifname[8];
 
133
    char ifname[18];
127
134
    struct iflist *itmp = NULL;
128
135
    struct iflist *tail = NULL;
129
136
    unsigned int index = 0;
133
140
 
134
141
    fd = open_procnetdev();
135
142
    if (fd == NULL) {
136
 
        errbox("Unable to obtain interface list", ANYKEY_MSG, &resp);
 
143
        tx_errbox("Unable to obtain interface list", ANYKEY_MSG, &resp);
137
144
        return;
138
145
    }
139
146
 
232
239
    }
233
240
}
234
241
 
 
242
void no_ifaces_error(void)
 
243
{
 
244
    write_error("No active interfaces.  Check their status or the /proc filesystem",
 
245
                daemonized);
 
246
}
 
247
 
235
248
void updaterates(struct iftab *table, int unit, time_t starttime,
236
249
                 time_t now, unsigned int idx)
237
250
{
321
334
 
322
335
void initiftab(struct iftab *table)
323
336
{
324
 
    initiflist(&(table->head));
325
 
 
326
337
    table->borderwin = newwin(LINES - 2, COLS, 1, 0);
327
338
    table->borderpanel = new_panel(table->borderwin);
328
339
 
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
402
413
 */
403
414
 
404
 
void ifstats(const struct OPTIONS *options, int facilitytime)
 
415
void ifstats(const struct OPTIONS *options, struct filterstate *ofilter,
 
416
    int facilitytime)
405
417
{
406
418
    int logging = options->logging;
407
419
    struct iftab table;
408
420
 
409
 
    struct isdntab isdntable;
410
 
 
411
421
    char buf[MAX_PACKET_SIZE];
412
 
    char aligned_buf[120];
413
422
    char *packet;
414
 
    struct iphdr *ipacket;
415
 
    char *tpacket;
 
423
    int pkt_result = 0;
416
424
 
417
425
    struct sockaddr_ll fromaddr;
418
426
    unsigned short linktype;
419
 
 
 
427
    
 
428
    unsigned sport, dport;
 
429
    
420
430
    struct iflist *ptmp = NULL;
421
431
 
422
432
    unsigned int idx = 1;
423
433
 
424
434
    int fd;
425
435
    FILE *logfile = NULL;
426
 
    int isdn_fd = -1;
427
436
 
428
437
    int br;
429
438
    char ifname[10];
430
439
 
431
 
    int iphlen;
432
 
    int ipck, ipcsum;
433
 
 
434
440
    int ch;
435
441
 
436
442
    struct timeval tv;
453
459
        return;
454
460
    }
455
461
 
 
462
    initiflist(&(table.head));
 
463
    if (table.head == NULL) {
 
464
        no_ifaces_error();
 
465
        unmark_facility(GSTATIDFILE, "");
 
466
        return;
 
467
    }
 
468
    
 
469
    initiftab(&table);
456
470
    open_socket(&fd);
457
471
 
458
472
    if (fd < 0) {
459
473
        unmark_facility(GSTATIDFILE, "");
460
474
        return;
461
475
    }
462
 
    initiftab(&table);
463
476
 
464
477
    if ((first_active_facility()) && (options->promisc)) {
465
478
        init_promisc_list(&promisc_list);
498
511
        update_panels();
499
512
        doupdate();
500
513
 
501
 
        bzero(&isdntable, sizeof(struct isdntab));
502
 
 
 
514
        isdnfd = -1;
503
515
        exitloop = 0;
504
516
        gettimeofday(&tv, NULL);
505
517
        starttime = startlog = statbegin = tv.tv_sec;
526
538
                case 12:
527
539
                case 'l':
528
540
                case 'L':
529
 
                    refresh_screen();
 
541
                    tx_refresh_screen();
530
542
                    break;
531
543
 
532
544
                case 'Q':
540
552
                }
541
553
            }
542
554
            if (br > 0) {
 
555
                pkt_result = processpacket(buf, &packet, &br, NULL, &sport, &dport,
 
556
                    &fromaddr, &linktype, ofilter, ifname, NULL);
 
557
 
 
558
                if (pkt_result == INVALID_PACKET)
 
559
                    continue;
 
560
                
543
561
                positionptr(&table, &ptmp, ifname);
544
562
 
545
563
                ptmp->total++;
547
565
                ptmp->spanbr += br;
548
566
                ptmp->br += br;
549
567
 
550
 
                if (fromaddr.sll_protocol == ntohs(ETH_P_IP)) {
551
 
                    (ptmp->iptotal)++;
552
 
 
553
 
                    isdn_iface_check(&isdn_fd, ifname);
554
 
                    linktype = getlinktype(fromaddr.sll_hatype, ifname,
555
 
                                           isdn_fd, &isdntable);
556
 
 
557
 
                    adjustpacket(buf, linktype, &packet, aligned_buf, &br);
558
 
 
559
 
                    if (packet == NULL)
560
 
                        continue;
561
 
 
562
 
                    ipacket = (struct iphdr *) packet;
563
 
                    iphlen = ipacket->ihl * 4;
564
 
                    tpacket = packet + iphlen;
565
 
 
566
 
                    /* recalculate checksum */
567
 
 
568
 
                    ipcsum = ipacket->check;
569
 
                    ipacket->check = 0;
570
 
                    ipck = in_cksum((u_short *) ipacket, iphlen);
571
 
                    if ((ipcsum != ipck) || (!ipck)) {
 
568
                if (fromaddr.sll_protocol == ETH_P_IP) {
 
569
                    ptmp->iptotal++;
 
570
                
 
571
                    if (pkt_result == CHECKSUM_ERROR) {
572
572
                        (ptmp->badtotal)++;
573
573
                        continue;
574
 
                    }
 
574
                    }
575
575
                } else {
576
576
                    (ptmp->noniptotal)++;
577
577
                }
635
635
        fclose(logfile);
636
636
    }
637
637
    destroyiflist(table.head);
638
 
    destroy_isdn_table(&isdntable);
 
638
    pkt_cleanup();
639
639
    unmark_facility(GSTATIDFILE, "");
640
640
    strcpy(current_logfile, "");
641
641
}
742
742
 * The detailed interface statistics function
743
743
 */
744
744
 
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)
746
747
{
747
748
    int logging = options->logging;
748
749
 
750
751
    PANEL *statpanel;
751
752
 
752
753
    char buf[MAX_PACKET_SIZE];
753
 
    char aligned_buf[120];
754
754
    char *packet;
755
755
    struct iphdr *ipacket = NULL;
756
756
    char *tpacket;
 
757
    unsigned int iphlen;
 
758
    unsigned int sport, dport;
757
759
 
758
 
    char ifname[8];
 
760
    char ifname[18];
759
761
    struct sockaddr_ll fromaddr;
760
762
    unsigned short linktype;
761
 
 
 
763
    
762
764
    int fd;
763
 
    int isdn_fd = -1;
764
765
    int br;
765
766
    int framelen = 0;
 
767
    int pkt_result = 0;
766
768
 
767
769
    FILE *logfile = NULL;
768
770
    
769
 
    int iphlen;
770
 
    int ipck, ipcsum;
771
771
    unsigned int iplen = 0;
772
772
 
773
773
    struct iftotals totals;
774
774
 
775
775
    int ch;
776
776
 
777
 
    struct isdntab isdntable;
778
 
 
779
777
    struct timeval tv;
780
778
    unsigned long updtime = 0;
781
779
    unsigned long long updtime_usec = 0;
807
805
    char unitstring[7];
808
806
 
809
807
    struct promisc_states *promisc_list;
810
 
    
811
808
    char err_msg[80];
812
809
 
 
810
 
813
811
    /*
814
812
     * Mark this facility
815
813
     */
853
851
    stdexitkeyhelp();
854
852
    statwin = newwin(LINES - 2, COLS, 1, 0);
855
853
    statpanel = new_panel(statwin);
856
 
    stdwinset(statwin);
 
854
    tx_stdwinset(statwin);
857
855
    wtimeout(statwin, -1);
858
856
    wattrset(statwin, BOXATTR);
859
 
    colorwin(statwin);
 
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);
894
892
 
895
893
    spanbr = 0;
896
894
 
897
 
    bzero(&isdntable, sizeof(struct isdntab));
898
 
 
899
895
    gettimeofday(&tv, NULL);
900
896
    starttime = startlog = statbegin = tv.tv_sec;
901
897
 
902
898
    leaveok(statwin, TRUE);
903
899
 
 
900
    isdnfd = -1;
904
901
    exitloop = 0;
905
902
    dispmode(options->actmode, unitstring);
906
903
 
916
913
            case 12:
917
914
            case 'l':
918
915
            case 'L':
919
 
                refresh_screen();
 
916
                tx_refresh_screen();
920
917
                break;
921
918
 
922
919
            case 'Q':
930
927
            }
931
928
        }
932
929
        if (br > 0) {
933
 
            if (strcmp(iface, ifname) != 0)
934
 
                continue;
935
 
 
936
930
            framelen = br;
 
931
            pkt_result = processpacket(buf, &packet, &br, NULL,
 
932
                             &sport, &dport, &fromaddr, &linktype,
 
933
                             ofilter, ifname, iface);
 
934
            
 
935
            if (pkt_result == INVALID_PACKET)
 
936
                continue;
 
937
                                 
937
938
            totals.total++;
938
 
            totals.bytestotal += br;
 
939
            totals.bytestotal += framelen;
939
940
 
940
941
            if (fromaddr.sll_pkttype == PACKET_OUTGOING) {
941
942
                totals.total_out++;
942
 
                totals.bytestotal_out += br;
943
 
                spanbr_out += br;
 
943
                totals.bytestotal_out += framelen;
 
944
                spanbr_out += framelen;
944
945
                spanpkt_out++;
945
946
            } else {
946
947
                totals.total_in++;
947
 
                totals.bytestotal_in += br;
948
 
                spanbr_in += br;
 
948
                totals.bytestotal_in += framelen;
 
949
                spanbr_in += framelen;
949
950
                spanpkt_in++;
950
951
            }
951
952
 
952
953
            if (fromaddr.sll_pkttype == PACKET_BROADCAST) {
953
954
                totals.bcast++;
954
 
                totals.bcastbytes += br;
 
955
                totals.bcastbytes += framelen;
955
956
            }
956
957
            
957
 
            spanbr += br;
 
958
            spanbr += framelen;
958
959
            spanpkt++;
959
960
 
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);
964
 
 
965
 
                adjustpacket(buf, linktype, &packet, aligned_buf, &br);
966
 
 
967
 
                if (packet == NULL)
968
 
                    continue;
969
 
 
970
 
                ipacket = (struct iphdr *) packet;
971
 
                iphlen = ipacket->ihl * 4;
972
 
                tpacket = packet + iphlen;
973
 
 
974
 
                ipcsum = ipacket->check;
975
 
                ipacket->check = 0;
976
 
                ipck = in_cksum((u_short *) ipacket, iphlen);
977
 
                if ((ipcsum != ipck) || (!ipck)) {
 
961
            if (fromaddr.sll_protocol == ETH_P_IP) {
 
962
 
 
963
                if (pkt_result == CHECKSUM_ERROR) {
978
964
                    totals.badtotal++;
979
965
                    continue;
980
966
                }
 
967
 
 
968
                ipacket = (struct iphdr *) packet;
 
969
                iphlen = ipacket->ihl * 4;
 
970
                tpacket = packet + iphlen;
981
971
                iplen = ntohs(ipacket->tot_len);
982
972
 
983
973
                totals.iptotal++;
1168
1158
    delwin(statwin);
1169
1159
    unmark_facility(DSTATIDFILE, iface);
1170
1160
    strcpy(current_logfile, "");
1171
 
    destroy_isdn_table(&isdntable);
 
1161
    pkt_cleanup();
1172
1162
    update_panels();
1173
1163
    doupdate();
1174
1164
}
1175
1165
 
1176
1166
void selectiface(char *ifname, int withall, int *aborted)
1177
1167
{
1178
 
    WINDOW *win;
1179
 
    PANEL *panel;
1180
 
    WINDOW *bw;
1181
 
    PANEL *bp;
1182
 
 
1183
 
    int row;
1184
1168
    int ch;
1185
1169
 
1186
 
    int endloop = 0;
1187
 
 
1188
1170
    struct iflist *list;
1189
1171
    struct iflist *ptmp;
1190
1172
 
 
1173
    struct scroll_list scrolllist;
 
1174
    
1191
1175
    initiflist(&list);
1192
 
    if (withall) {
 
1176
    
 
1177
    if (list == NULL) {
 
1178
        no_ifaces_error();
 
1179
        *aborted = 1;
 
1180
        return;
 
1181
    }
 
1182
        
 
1183
    if ((withall) && (list != NULL)) {
1193
1184
        ptmp = malloc(sizeof(struct iflist));
1194
1185
        strcpy(ptmp->ifname, "All interfaces");
1195
1186
 
1198
1189
        ptmp->next_entry = list;
1199
1190
        list = ptmp;
1200
1191
    }
1201
 
    listkeyhelp();
1202
 
    bw = newwin(14, 24, (LINES - 14) / 2, (COLS - 24) / 2 - 9);
1203
 
    /* I'm guessing (COLS-24)/2-9*COLS/80) is overkill */
1204
 
    bp = new_panel(bw);
1205
 
    wattrset(bw, BOXATTR);
1206
 
    box(bw, ACS_VLINE, ACS_HLINE);
1207
 
 
1208
 
    win = newwin(12, 22, (LINES - 14) / 2 + 1, (COLS - 24) / 2 - 8);
1209
 
    panel = new_panel(win);
1210
 
    stdwinset(win);
1211
 
    wattrset(win, BOXATTR);
1212
 
    colorwin(win);
1213
 
    wmove(bw, 0, 1);
1214
 
    wprintw(bw, " Select Interface ");
1215
 
 
1216
 
    row = 0;
1217
 
    ptmp = list;
1218
 
 
1219
 
    wattrset(win, STDATTR);
1220
 
    do {
1221
 
        wmove(win, row, 2);
1222
 
        wprintw(win, "%s", ptmp->ifname);
1223
 
        ptmp = ptmp->next_entry;
1224
 
        row++;
1225
 
    } while ((ptmp != NULL) && (row <= 11));
1226
 
    update_panels();
1227
 
    doupdate();
1228
 
 
1229
 
    row = 0;
1230
 
    ptmp = list;
1231
 
 
1232
 
    do {
1233
 
        wattrset(win, PTRATTR);
1234
 
        wmove(win, row, 1);
1235
 
        waddch(win, ACS_RARROW);
1236
 
        ch = wgetch(win);
1237
 
        wmove(win, row, 1);
1238
 
        wprintw(win, " ");
1239
 
 
1240
 
        wattrset(win, STDATTR);
1241
 
        switch (ch) {
1242
 
        case KEY_DOWN:
1243
 
            if (ptmp->next_entry != NULL) {
1244
 
                if (row < 11)
1245
 
                    row++;
1246
 
                else {
1247
 
                    wscrl(win, 1);
1248
 
                    scrollok(win, 0);
1249
 
                    wmove(win, 11, 0);
1250
 
                    wprintw(win, "%22c", ' ');
1251
 
                    wmove(win, 11, 2);
1252
 
                    wprintw(win, ptmp->next_entry->ifname);
1253
 
                    scrollok(win, 1);
1254
 
                }
1255
 
                ptmp = ptmp->next_entry;
1256
 
            }
1257
 
            break;
1258
 
        case KEY_UP:
1259
 
            if (ptmp->prev_entry != NULL) {
1260
 
                if (row > 0)
1261
 
                    row--;
1262
 
                else {
1263
 
                    wscrl(win, -1);
1264
 
                    wmove(win, 0, 0);
1265
 
                    wprintw(win, "%22c", ' ');
1266
 
                    wmove(win, 0, 2);
1267
 
                    wprintw(win, ptmp->prev_entry->ifname);
1268
 
                }
1269
 
                ptmp = ptmp->prev_entry;
1270
 
            }
1271
 
            break;
1272
 
        case 13:
1273
 
            endloop = 1;
1274
 
            *aborted = 0;
1275
 
            break;
1276
 
        case 12:
1277
 
        case 'l':
1278
 
        case 'L':
1279
 
            refresh_screen();
1280
 
            break;
1281
 
        case 27:
1282
 
        case 24:
1283
 
        case 'X':
1284
 
        case 'x':
1285
 
        case 'Q':
1286
 
        case 'q':
1287
 
            endloop = 1;
1288
 
            *aborted = 1;
1289
 
            break;
1290
 
        }
1291
 
        update_panels();
1292
 
        doupdate();
1293
 
    } while (!endloop);
1294
 
 
1295
 
    if (!(*aborted)) {
 
1192
    tx_listkeyhelp(STDATTR, HIGHATTR);
 
1193
 
 
1194
    ptmp = list;
 
1195
 
 
1196
    tx_init_listbox(&scrolllist, 24, 14, (COLS - 24) / 2 - 9, (LINES - 14) / 2,
 
1197
        STDATTR, BOXATTR, BARSTDATTR, HIGHATTR);
 
1198
    
 
1199
    tx_set_listbox_title(&scrolllist, "Select Interface", 1);
 
1200
    
 
1201
    while (ptmp != NULL) {
 
1202
        tx_add_list_entry(&scrolllist, (char *) ptmp, ptmp->ifname);
 
1203
        ptmp = ptmp->next_entry;
 
1204
    }
 
1205
    
 
1206
    tx_show_listbox(&scrolllist);
 
1207
    tx_operate_listbox(&scrolllist, &ch, aborted);
 
1208
    tx_close_listbox(&scrolllist);
 
1209
    
 
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, "");
1298
1214
        else
1299
1215
            strcpy(ifname, ptmp->ifname);
1300
1216
    }
 
1217
    
 
1218
    tx_destroy_list(&scrolllist);
1301
1219
    destroyiflist(list);
1302
 
    del_panel(panel);
1303
 
    delwin(win);
1304
 
    del_panel(bp);
1305
 
    delwin(bw);
1306
1220
    update_panels();
1307
1221
    doupdate();
1308
1222
}