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

« back to all changes in this revision

Viewing changes to src/serv.c

  • Committer: Bazaar Package Importer
  • Author(s): Frederic Peters
  • Date: 2006-10-15 13:34:14 UTC
  • mfrom: (1.1.2 upstream) (3.1.1 dapper)
  • Revision ID: james.westby@ubuntu.com-20061015133414-77itbhydih1z3amr
* Sync with Ubuntu fixes (by Oliver Grawert and Michael Vogt)
  * added fix for /var/run detection (since it is a tmpfs by default on
    Ubuntu) [and fixed ubuntu fix]
  * added support for ath devices
  * fixed FTBFS by changing linux/if_tr.h to netinet/if_tr.h

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
#include "deskman.h"
46
46
#include "isdntab.h"
47
47
#include "fltdefs.h"
48
 
#include "utfilter.h"
49
48
#include "fltselect.h"
50
49
#include "packet.h"
51
50
#include "ipfrag.h"
74
73
extern int daemonized;
75
74
 
76
75
extern void writeutslog(struct portlistent *list, unsigned long nsecs,
77
 
                        int unit, FILE * logfile);
 
76
                        int unit, FILE * logfile);
78
77
 
79
78
/*
80
79
 * SIGUSR1 logfile rotation signal handler
91
90
{
92
91
    float screen_scale = ((float) COLS / 80 + 1) / 2;
93
92
    int scratchx;
94
 
    
 
93
 
95
94
    list->head = list->tail = list->barptr = NULL;
96
95
    list->firstvisible = list->lastvisible = NULL;
97
96
    list->count = 0;
98
97
    list->baridx = 0;
99
 
    
 
98
 
100
99
    list->borderwin = newwin(LINES - 3, COLS, 1, 0);
101
100
    list->borderpanel = new_panel(list->borderwin);
102
101
    wattrset(list->borderwin, BOXATTR);
103
 
    box(list->borderwin, ACS_VLINE, ACS_HLINE);
 
102
    tx_box(list->borderwin, ACS_VLINE, ACS_HLINE);
104
103
 
105
104
    wmove(list->borderwin, 0, 1 * screen_scale);
106
105
    wprintw(list->borderwin, " Proto/Port ");
130
129
}
131
130
 
132
131
struct portlistent *addtoportlist(struct portlist *list,
133
 
                                  unsigned int protocol, unsigned int port,
134
 
                                  int *nomem, int servnames)
 
132
                                  unsigned int protocol, unsigned int port,
 
133
                                  int *nomem, int servnames)
135
134
{
136
135
    struct portlistent *ptemp;
137
136
 
138
137
    ptemp = malloc(sizeof(struct portlistent));
139
138
 
140
139
    if (ptemp == NULL) {
141
 
        printnomem();
142
 
        *nomem = 1;
143
 
        return NULL;
 
140
        printnomem();
 
141
        *nomem = 1;
 
142
        return NULL;
144
143
    }
145
144
    if (list->head == NULL) {
146
 
        ptemp->prev_entry = NULL;
147
 
        list->head = ptemp;
148
 
        list->firstvisible = ptemp;
 
145
        ptemp->prev_entry = NULL;
 
146
        list->head = ptemp;
 
147
        list->firstvisible = ptemp;
149
148
    }
150
 
    
 
149
 
151
150
    if (list->tail != NULL) {
152
 
        list->tail->next_entry = ptemp;
153
 
        ptemp->prev_entry = list->tail;
 
151
        list->tail->next_entry = ptemp;
 
152
        ptemp->prev_entry = list->tail;
154
153
    }
155
154
    list->tail = ptemp;
156
155
    ptemp->next_entry = NULL;
157
156
 
158
157
    ptemp->protocol = protocol;
159
 
    ptemp->port = port;         /* This is used in checks later. */
 
158
    ptemp->port = port;         /* This is used in checks later. */
160
159
 
161
160
    /* 
162
161
     * Obtain appropriate service name
172
171
    ptemp->idx = list->count;
173
172
 
174
173
    ptemp->proto_starttime = time(NULL);
175
 
    
 
174
 
176
175
    if (list->count <= LINES - 5)
177
 
        list->lastvisible = ptemp;
 
176
        list->lastvisible = ptemp;
178
177
 
179
178
    wmove(list->borderwin, LINES - 4, 1);
180
179
    wprintw(list->borderwin, " %u entries ", list->count);
187
186
    struct porttab *ptmp = table;
188
187
 
189
188
    while (ptmp != NULL) {
190
 
        if (((ptmp->port_max == 0) && (ptmp->port_min == port)) ||
191
 
            ((port >= ptmp->port_min) && (port <= ptmp->port_max)))
192
 
            return 1;
 
189
        if (((ptmp->port_max == 0) && (ptmp->port_min == port)) ||
 
190
            ((port >= ptmp->port_min) && (port <= ptmp->port_max)))
 
191
            return 1;
193
192
 
194
 
        ptmp = ptmp->next_entry;
 
193
        ptmp = ptmp->next_entry;
195
194
    }
196
195
 
197
196
    return 0;
203
202
}
204
203
 
205
204
struct portlistent *inportlist(struct portlist *list,
206
 
                               unsigned int protocol, unsigned int port)
 
205
                               unsigned int protocol, unsigned int port)
207
206
{
208
207
    struct portlistent *ptmp = list->head;
209
208
 
210
209
    while (ptmp != NULL) {
211
 
        if ((ptmp->port == port) && (ptmp->protocol == protocol))
212
 
            return ptmp;
 
210
        if ((ptmp->port == port) && (ptmp->protocol == protocol))
 
211
            return ptmp;
213
212
 
214
 
        ptmp = ptmp->next_entry;
 
213
        ptmp = ptmp->next_entry;
215
214
    }
216
215
 
217
216
    return NULL;
218
217
}
219
218
 
220
219
void printportent(struct portlist *list, struct portlistent *entry,
221
 
                  unsigned int idx)
 
220
                  unsigned int idx)
222
221
{
223
222
    unsigned int target_row;
224
223
    float screen_scale = ((float) COLS / 80 + 1) / 2;
226
225
    int udplabelattr;
227
226
    int highattr;
228
227
    char sp_buf[10];
229
 
    
 
228
 
230
229
    if ((entry->idx < idx) || (entry->idx > idx + (LINES - 6)))
231
 
        return;
 
230
        return;
232
231
 
233
232
    target_row = entry->idx - idx;
234
 
    
 
233
 
235
234
    if (entry == list->barptr) {
236
235
        tcplabelattr = BARSTDATTR;
237
236
        udplabelattr = BARPTRATTR;
241
240
        udplabelattr = PTRATTR;
242
241
        highattr = HIGHATTR;
243
242
    }
244
 
    
 
243
 
245
244
    wattrset(list->win, tcplabelattr);
246
245
    sprintf(sp_buf, "%%%dc", COLS - 2);
247
246
    scrollok(list->win, 0);
248
247
    mvwprintw(list->win, target_row, 0, sp_buf, ' ');
249
248
    scrollok(list->win, 1);
250
 
    
 
249
 
251
250
    wmove(list->win, target_row, 1);
252
251
    if (entry->protocol == IPPROTO_TCP) {
253
 
        wattrset(list->win, tcplabelattr);
254
 
        wprintw(list->win, "TCP");
 
252
        wattrset(list->win, tcplabelattr);
 
253
        wprintw(list->win, "TCP");
255
254
    } else if (entry->protocol == IPPROTO_UDP) {
256
 
        wattrset(list->win, udplabelattr);
257
 
        wprintw(list->win, "UDP");
 
255
        wattrset(list->win, udplabelattr);
 
256
        wprintw(list->win, "UDP");
258
257
    }
259
 
    
 
258
 
260
259
    wprintw(list->win, "/%s          ", entry->servname);
261
260
    wattrset(list->win, highattr);
262
261
    wmove(list->win, target_row, 17 * screen_scale);
279
278
    struct portlistent *ctmp = NULL;
280
279
 
281
280
    if (list->head != NULL)
282
 
        ctmp = list->head->next_entry;
 
281
        ctmp = list->head->next_entry;
283
282
 
284
283
    while (ptmp != NULL) {
285
 
        free(ptmp);
286
 
        ptmp = ctmp;
 
284
        free(ptmp);
 
285
        ptmp = ctmp;
287
286
 
288
 
        if (ctmp != NULL)
289
 
            ctmp = ctmp->next_entry;
 
287
        if (ctmp != NULL)
 
288
            ctmp = ctmp->next_entry;
290
289
    }
291
290
}
292
291
 
293
 
void printport(struct portlist *list, unsigned int protocol,
294
 
               unsigned int port, int br, unsigned int idx, int fromto,
295
 
               struct porttab *ports, int servnames)
 
292
void updateportent(struct portlist *list, unsigned int protocol,
 
293
                   unsigned int sport, unsigned int dport, int br,
 
294
                   unsigned int idx, struct porttab *ports, int servnames)
296
295
{
297
 
    struct portlistent *listent;
 
296
    struct portlistent *sport_listent = NULL;
 
297
    struct portlistent *dport_listent = NULL;
298
298
    int nomem = 0;
299
299
 
300
 
    if (goodport(port, ports)) {
301
 
        listent = inportlist(list, protocol, port);
302
 
 
303
 
        if ((listent == NULL) && (!nomem))
304
 
            listent =
305
 
                addtoportlist(list, protocol, port, &nomem, servnames);
306
 
 
307
 
        if (listent == NULL)
308
 
            return;
309
 
 
310
 
        listent->count++;
311
 
        listent->bcount += br;
312
 
        listent->spans.spanbr += br;
313
 
        
314
 
        if (fromto == 0) {      /* from */
315
 
            listent->obcount += br;
316
 
            listent->spans.spanbr_out += br;
317
 
            listent->ocount++;
318
 
        } else {                /* to */
319
 
            listent->ibcount += br;
320
 
            listent->spans.spanbr_in += br;
321
 
            listent->icount++;
322
 
        }
323
 
 
324
 
        printportent(list, listent, idx);
 
300
    if (goodport(sport, ports)) {
 
301
        sport_listent = inportlist(list, protocol, sport);
 
302
 
 
303
        if ((sport_listent == NULL) && (!nomem))
 
304
            sport_listent =
 
305
                addtoportlist(list, protocol, sport, &nomem, servnames);
 
306
 
 
307
        if (sport_listent == NULL)
 
308
            return;
 
309
 
 
310
        sport_listent->count++;
 
311
        sport_listent->bcount += br;
 
312
        sport_listent->spans.spanbr += br;
 
313
 
 
314
        sport_listent->obcount += br;
 
315
        sport_listent->spans.spanbr_out += br;
 
316
        sport_listent->ocount++;
 
317
    }
 
318
 
 
319
    if (goodport(dport, ports)) {
 
320
        dport_listent = inportlist(list, protocol, dport);
 
321
 
 
322
        if ((dport_listent == NULL) && (!nomem))
 
323
            dport_listent =
 
324
                addtoportlist(list, protocol, dport, &nomem, servnames);
 
325
 
 
326
        if (dport_listent == NULL)
 
327
            return;
 
328
 
 
329
        if (dport_listent != sport_listent) {
 
330
            dport_listent->count++;
 
331
            dport_listent->bcount += br;
 
332
            dport_listent->spans.spanbr += br;
 
333
        }
 
334
 
 
335
        dport_listent->ibcount += br;
 
336
        dport_listent->spans.spanbr_out += br;
 
337
        dport_listent->icount++;
 
338
    }
 
339
    if (sport_listent != NULL || dport_listent != NULL) {
 
340
        if (sport_listent != NULL)
 
341
            printportent(list, sport_listent, idx);
 
342
        if (dport_listent != NULL && dport_listent != sport_listent)
 
343
            printportent(list, dport_listent, idx);
325
344
    }
326
345
}
327
346
 
328
347
/*
329
348
 * Swap two port list entries.  p1 must be previous to p2.
330
349
 */
331
 
 
 
350
 
332
351
void swapportents(struct portlist *list,
333
 
                  struct portlistent *p1, struct portlistent *p2)
 
352
                  struct portlistent *p1, struct portlistent *p2)
334
353
{
335
354
    register unsigned int tmp;
336
355
    struct portlistent *p1prevsaved;
337
356
    struct portlistent *p2nextsaved;
338
357
 
339
358
    if (p1 == p2)
340
 
        return;
 
359
        return;
341
360
 
342
361
    tmp = p1->idx;
343
362
    p1->idx = p2->idx;
344
363
    p2->idx = tmp;
345
364
 
346
365
    if (p1->prev_entry != NULL)
347
 
        p1->prev_entry->next_entry = p2;
 
366
        p1->prev_entry->next_entry = p2;
348
367
    else
349
 
        list->head = p2;
 
368
        list->head = p2;
350
369
 
351
370
    if (p2->next_entry != NULL)
352
 
        p2->next_entry->prev_entry = p1;
 
371
        p2->next_entry->prev_entry = p1;
353
372
    else
354
 
        list->tail = p1;
 
373
        list->tail = p1;
355
374
 
356
375
    p2nextsaved = p2->next_entry;
357
376
    p1prevsaved = p1->prev_entry;
358
377
 
359
378
    if (p1->next_entry == p2) {
360
 
        p2->next_entry = p1;
361
 
        p1->prev_entry = p2;
 
379
        p2->next_entry = p1;
 
380
        p1->prev_entry = p2;
362
381
    } else {
363
 
        p2->next_entry = p1->next_entry;
364
 
        p1->prev_entry = p2->prev_entry;
365
 
        p2->prev_entry->next_entry = p1;
366
 
        p1->next_entry->prev_entry = p2;
 
382
        p2->next_entry = p1->next_entry;
 
383
        p1->prev_entry = p2->prev_entry;
 
384
        p2->prev_entry->next_entry = p1;
 
385
        p1->next_entry->prev_entry = p2;
367
386
    }
368
387
 
369
388
    p2->prev_entry = p1prevsaved;
376
395
unsigned long long qp_getkey(struct portlistent *entry, int ch)
377
396
{
378
397
    unsigned long long result = 0;
379
 
    
380
 
    switch(ch) {
 
398
 
 
399
    switch (ch) {
381
400
    case 'R':
382
401
        result = entry->port;
383
402
        break;
400
419
        result = entry->ocount;
401
420
        break;
402
421
    }
403
 
    
 
422
 
404
423
    return result;
405
424
}
406
425
 
407
426
/*
408
427
 * Refresh TCP/UDP service screen.
409
428
 */
410
 
 
 
429
 
411
430
void refresh_serv_screen(struct portlist *table, int idx)
412
431
{
413
432
    struct portlistent *ptmp = table->firstvisible;
414
433
 
415
 
    wattrset(table->win, STDATTR);    
 
434
    wattrset(table->win, STDATTR);
416
435
    tx_colorwin(table->win);
417
 
    
 
436
 
418
437
    while ((ptmp != NULL) && (ptmp->prev_entry != table->lastvisible)) {
419
438
        printportent(table, ptmp, idx);
420
439
        ptmp = ptmp->next_entry;
422
441
    update_panels();
423
442
    doupdate();
424
443
}
425
 
      
 
444
 
426
445
 
427
446
/*
428
447
 * Compare the sort criterion with the pivot value.  Receives a parameter
438
457
 *     right, return true if the value is less than the pivot.  This results
439
458
 *     in a descending sort. 
440
459
 */
441
 
 
 
460
 
442
461
int qp_compare(struct portlistent *entry, unsigned long long pv, int ch,
443
 
                    int side)
 
462
               int side)
444
463
{
445
464
    int result = 0;
446
465
    unsigned long long value;
447
 
    
 
466
 
448
467
    value = qp_getkey(entry, ch);
449
 
    
 
468
 
450
469
    if (ch == 'R') {
451
470
        if (side == LEFT)
452
471
            result = (value <= pv);
458
477
        else
459
478
            result = (value < pv);
460
479
    }
461
 
    
 
480
 
462
481
    return result;
463
482
}
464
483
 
473
492
                                 struct portlistent **high, int ch)
474
493
{
475
494
    struct portlistent *pivot = *low;
476
 
    
 
495
 
477
496
    struct portlistent *left = *low;
478
497
    struct portlistent *right = *high;
479
498
    struct portlistent *ptmp;
480
 
    
 
499
 
481
500
    unsigned long long pivot_value;
482
 
    
 
501
 
483
502
    pivot_value = qp_getkey(pivot, ch);
484
 
    
 
503
 
485
504
    while (left->idx < right->idx) {
486
505
        while ((qp_compare(left, pivot_value, ch, LEFT))
487
506
               && (left->next_entry != NULL))
488
507
            left = left->next_entry;
489
 
            
 
508
 
490
509
        while (qp_compare(right, pivot_value, ch, RIGHT))
491
510
            right = right->prev_entry;
492
 
            
 
511
 
493
512
        if (left->idx < right->idx) {
494
513
            swapportents(table, left, right);
495
514
            if (*low == left)
496
515
                *low = right;
497
 
                
 
516
 
498
517
            if (*high == right)
499
518
                *high = left;
500
 
                
501
 
            ptmp = left;
502
 
            left = right;
503
 
            right = ptmp;
504
 
        }
 
519
 
 
520
            ptmp = left;
 
521
            left = right;
 
522
            right = ptmp;
 
523
        }
505
524
    }
506
 
    swapportents(table, pivot, right);      
507
 
    
 
525
    swapportents(table, pivot, right);
 
526
 
508
527
    if (*low == pivot)
509
528
        *low = right;
510
 
                
 
529
 
511
530
    if (*high == right)
512
531
        *high = pivot;
513
 
            
 
532
 
514
533
    return pivot;
515
534
}
516
535
 
522
541
                            struct portlistent *high, int ch)
523
542
{
524
543
    struct portlistent *pivot;
525
 
    
 
544
 
526
545
    if ((high == NULL) || (low == NULL))
527
546
        return;
528
 
        
 
547
 
529
548
    if (high->idx > low->idx) {
530
549
        pivot = qp_partition(table, &low, &high, ch);
531
 
        
 
550
 
532
551
        quicksort_port_entries(table, low, pivot->prev_entry, ch);
533
552
        quicksort_port_entries(table, pivot->next_entry, high, ch);
534
553
    }
538
557
{
539
558
    struct portlistent *ptemp1;
540
559
    unsigned int idxtmp;
541
 
    
 
560
 
542
561
    if (!(list->head))
543
562
        return;
544
 
        
 
563
 
545
564
    command = toupper(command);
546
565
 
547
566
    if ((command != 'R') && (command != 'B') && (command != 'O') &&
548
 
       (command != 'M') && (command != 'P') && (command != 'T') &&
549
 
       (command != 'F'))
 
567
        (command != 'M') && (command != 'P') && (command != 'T') &&
 
568
        (command != 'F'))
550
569
        return;
551
 
        
 
570
 
552
571
    quicksort_port_entries(list, list->head, list->tail, command);
553
 
    
 
572
 
554
573
    ptemp1 = list->firstvisible = list->head;
555
574
    *idx = 1;
556
575
    idxtmp = 1;
557
 
    
558
 
    while ((ptemp1) && (idxtmp <= LINES - 5)) { /* printout */
559
 
        printportent(list, ptemp1, *idx);
560
 
        if (idxtmp <= LINES - 5)
561
 
            list->lastvisible = ptemp1;
562
 
        ptemp1 = ptemp1->next_entry;
563
 
        idxtmp++;
 
576
 
 
577
    while ((ptemp1) && (idxtmp <= LINES - 5)) { /* printout */
 
578
        printportent(list, ptemp1, *idx);
 
579
        if (idxtmp <= LINES - 5)
 
580
            list->lastvisible = ptemp1;
 
581
        ptemp1 = ptemp1->next_entry;
 
582
        idxtmp++;
564
583
    }
565
584
}
566
585
 
570
589
    sprintf(sp_buf, "%%%dc", COLS - 2);
571
590
    wattrset(table->win, STDATTR);
572
591
    if (direction == SCROLLUP) {
573
 
        if (table->lastvisible != table->tail) {
574
 
            wscrl(table->win, 1);
575
 
            table->lastvisible = table->lastvisible->next_entry;
576
 
            table->firstvisible = table->firstvisible->next_entry;
577
 
            (*idx)++;
578
 
            wmove(table->win, LINES - 6, 0);
579
 
            scrollok(table->win, 0);
580
 
            wprintw(table->win, sp_buf, ' ');
581
 
            scrollok(table->win, 1);
582
 
            printportent(table, table->lastvisible, *idx);
583
 
        }
 
592
        if (table->lastvisible != table->tail) {
 
593
            wscrl(table->win, 1);
 
594
            table->lastvisible = table->lastvisible->next_entry;
 
595
            table->firstvisible = table->firstvisible->next_entry;
 
596
            (*idx)++;
 
597
            wmove(table->win, LINES - 6, 0);
 
598
            scrollok(table->win, 0);
 
599
            wprintw(table->win, sp_buf, ' ');
 
600
            scrollok(table->win, 1);
 
601
            printportent(table, table->lastvisible, *idx);
 
602
        }
584
603
    } else {
585
 
        if (table->firstvisible != table->head) {
586
 
            wscrl(table->win, -1);
587
 
            table->lastvisible = table->lastvisible->prev_entry;
588
 
            table->firstvisible = table->firstvisible->prev_entry;
589
 
            (*idx)--;
590
 
            wmove(table->win, 0, 0);
591
 
            wprintw(table->win, sp_buf, ' ');
592
 
            printportent(table, table->firstvisible, *idx);
593
 
        }
 
604
        if (table->firstvisible != table->head) {
 
605
            wscrl(table->win, -1);
 
606
            table->lastvisible = table->lastvisible->prev_entry;
 
607
            table->firstvisible = table->firstvisible->prev_entry;
 
608
            (*idx)--;
 
609
            wmove(table->win, 0, 0);
 
610
            wprintw(table->win, sp_buf, ' ');
 
611
            printportent(table, table->firstvisible, *idx);
 
612
        }
594
613
    }
595
614
}
596
615
 
599
618
    int i = 1;
600
619
 
601
620
    if (direction == SCROLLUP) {
602
 
        while ((i <= LINES - 9) && (table->lastvisible != table->tail)) {
603
 
            i++;
604
 
            table->firstvisible = table->firstvisible->next_entry;
605
 
            table->lastvisible = table->lastvisible->next_entry;
606
 
            (*idx)++;
607
 
        }
 
621
        while ((i <= LINES - 9) && (table->lastvisible != table->tail)) {
 
622
            i++;
 
623
            table->firstvisible = table->firstvisible->next_entry;
 
624
            table->lastvisible = table->lastvisible->next_entry;
 
625
            (*idx)++;
 
626
        }
608
627
    } else {
609
 
        while ((i <= LINES - 9) && (table->firstvisible != table->head)) {
610
 
            i++;
 
628
        while ((i <= LINES - 9) && (table->firstvisible != table->head)) {
 
629
            i++;
611
630
            table->firstvisible = table->firstvisible->prev_entry;
612
631
            table->lastvisible = table->lastvisible->prev_entry;
613
632
            (*idx)--;
614
 
        }
 
633
        }
615
634
    }
616
635
    refresh_serv_screen(table, *idx);
617
636
}
623
642
 
624
643
    wattrset(*win, DLGBOXATTR);
625
644
    tx_colorwin(*win);
626
 
    box(*win, ACS_VLINE, ACS_HLINE);
 
645
    tx_box(*win, ACS_VLINE, ACS_HLINE);
627
646
 
628
647
    wattrset(*win, DLGTEXTATTR);
629
648
    mvwprintw(*win, 2, 2, "Select sort criterion");
630
649
    wmove(*win, 4, 2);
631
650
    tx_printkeyhelp("R", " - port number", *win, DLGHIGHATTR, DLGTEXTATTR);
632
651
    wmove(*win, 5, 2);
633
 
    tx_printkeyhelp("P", " - total packets", *win, DLGHIGHATTR, DLGTEXTATTR);
 
652
    tx_printkeyhelp("P", " - total packets", *win, DLGHIGHATTR,
 
653
                    DLGTEXTATTR);
634
654
    wmove(*win, 6, 2);
635
655
    tx_printkeyhelp("B", " - total bytes", *win, DLGHIGHATTR, DLGTEXTATTR);
636
656
    wmove(*win, 7, 2);
638
658
    wmove(*win, 8, 2);
639
659
    tx_printkeyhelp("O", " - bytes to", *win, DLGHIGHATTR, DLGTEXTATTR);
640
660
    wmove(*win, 9, 2);
641
 
    tx_printkeyhelp("F", " - packets from", *win, DLGHIGHATTR, DLGTEXTATTR);
 
661
    tx_printkeyhelp("F", " - packets from", *win, DLGHIGHATTR,
 
662
                    DLGTEXTATTR);
642
663
    wmove(*win, 10, 2);
643
664
    tx_printkeyhelp("M", " - bytes from", *win, DLGHIGHATTR, DLGTEXTATTR);
644
665
    wmove(*win, 11, 2);
645
 
    tx_printkeyhelp("Any other key", " - cancel sort", *win, DLGHIGHATTR, DLGTEXTATTR);
 
666
    tx_printkeyhelp("Any other key", " - cancel sort", *win, DLGHIGHATTR,
 
667
                    DLGTEXTATTR);
646
668
    update_panels();
647
669
    doupdate();
648
670
}
649
671
 
650
 
void update_serv_rates(struct portlist *list, WINDOW *win, int actmode,
651
 
    int *cleared)
 
672
void update_serv_rates(struct portlist *list, WINDOW * win, int actmode,
 
673
                       int *cleared)
652
674
{
653
675
    char act_unit[10];
654
676
    float inrate, outrate, totalrate;
655
677
    time_t now = time(NULL);
656
678
 
657
679
    dispmode(actmode, act_unit);
658
 
            
 
680
 
659
681
    if (actmode == KBITS) {
660
 
        inrate = (float) (list->barptr->spans.spanbr_in * 8 / 1000) / (float) (now - list->barptr->starttime);
661
 
        outrate = (float) (list->barptr->spans.spanbr_out * 8 / 1000) / (float) (now - list->barptr->starttime);
662
 
        totalrate = (float) (list->barptr->spans.spanbr * 8 / 1000) / (float) (now - list->barptr->starttime);
 
682
        inrate =
 
683
            (float) (list->barptr->spans.spanbr_in * 8 / 1000) /
 
684
            (float) (now - list->barptr->starttime);
 
685
        outrate =
 
686
            (float) (list->barptr->spans.spanbr_out * 8 / 1000) /
 
687
            (float) (now - list->barptr->starttime);
 
688
        totalrate =
 
689
            (float) (list->barptr->spans.spanbr * 8 / 1000) /
 
690
            (float) (now - list->barptr->starttime);
663
691
    } else {
664
 
        inrate = (float) (list->barptr->spans.spanbr_in / 1024) / (float) (now - list->barptr->starttime);
665
 
        outrate = (float) (list->barptr->spans.spanbr_out / 1024) / (float) (now - list->barptr->starttime);
666
 
        totalrate = (float) (list->barptr->spans.spanbr / 1024) / (float) (now - list->barptr->starttime);
667
 
    }    
668
 
        
 
692
        inrate =
 
693
            (float) (list->barptr->spans.spanbr_in / 1024) / (float) (now -
 
694
                                                                      list->
 
695
                                                                      barptr->
 
696
                                                                      starttime);
 
697
        outrate =
 
698
            (float) (list->barptr->spans.spanbr_out / 1024) /
 
699
            (float) (now - list->barptr->starttime);
 
700
        totalrate =
 
701
            (float) (list->barptr->spans.spanbr / 1024) / (float) (now -
 
702
                                                                   list->
 
703
                                                                   barptr->
 
704
                                                                   starttime);
 
705
    }
 
706
 
669
707
    wattrset(win, IPSTATLABELATTR);
670
 
    mvwprintw(win, 0, 1, "Protocol data rates (%s/s):           in             out            total", act_unit);
 
708
    mvwprintw(win, 0, 1,
 
709
              "Protocol data rates (%s/s):           in             out            total",
 
710
              act_unit);
671
711
    wattrset(win, IPSTATATTR);
672
 
    mvwprintw(win, 0, 31, "%10.2f", inrate);            
673
 
    mvwprintw(win, 0, 46, "%10.2f", outrate);            
 
712
    mvwprintw(win, 0, 31, "%10.2f", inrate);
 
713
    mvwprintw(win, 0, 46, "%10.2f", outrate);
674
714
    mvwprintw(win, 0, 61, "%10.2f", totalrate);
675
 
    
 
715
 
676
716
    bzero(&(list->barptr->spans), sizeof(struct serv_spans));
677
717
    list->barptr->starttime = time(NULL);
678
 
    *cleared = 0;            
 
718
    *cleared = 0;
679
719
}
680
720
 
681
721
/*
683
723
 */
684
724
 
685
725
void servmon(char *ifname, struct porttab *ports,
686
 
             const struct OPTIONS *options, int facilitytime,
687
 
             struct filterstate *ofilter)
 
726
             const struct OPTIONS *options, int facilitytime,
 
727
             struct filterstate *ofilter)
688
728
{
689
729
    int logging = options->logging;
690
730
    int fd;
719
759
    struct portlist list;
720
760
    struct portlistent *serv_tmp;
721
761
    int statcleared = 0;
722
 
    
 
762
 
723
763
    FILE *logfile = NULL;
724
764
 
725
765
    struct promisc_states *promisc_list;
726
766
 
727
767
    WINDOW *sortwin;
728
768
    PANEL *sortpanel;
729
 
    
 
769
 
730
770
    WINDOW *statwin;
731
771
    PANEL *statpanel;
732
 
    
 
772
 
733
773
    char msgstring[80];
734
774
    char sp_buf[10];
735
 
    
 
775
 
736
776
    const int statx = 1;
737
 
    
 
777
 
738
778
    /*
739
779
     * Mark this facility
740
780
     */
741
781
 
742
782
    if (!facility_active(TCPUDPIDFILE, ifname))
743
 
        mark_facility(TCPUDPIDFILE, "TCP/UDP monitor", ifname);
 
783
        mark_facility(TCPUDPIDFILE, "TCP/UDP monitor", ifname);
744
784
    else {
745
 
        snprintf(msgstring, 80, "TCP/UDP monitor already running on %s", ifname);
746
 
        write_error(msgstring, daemonized);
747
 
        return;
 
785
        snprintf(msgstring, 80, "TCP/UDP monitor already running on %s",
 
786
                 ifname);
 
787
        write_error(msgstring, daemonized);
 
788
        return;
748
789
    }
749
790
 
750
791
    open_socket(&fd);
751
792
 
752
793
    if (fd < 0) {
753
 
        unmark_facility(TCPUDPIDFILE, ifname);
754
 
        return;
 
794
        unmark_facility(TCPUDPIDFILE, ifname);
 
795
        return;
755
796
    }
756
797
    if (!iface_supported(ifname)) {
757
 
        err_iface_unsupported();
758
 
        unmark_facility(TCPUDPIDFILE, ifname);
759
 
        return;
 
798
        err_iface_unsupported();
 
799
        unmark_facility(TCPUDPIDFILE, ifname);
 
800
        return;
760
801
    }
761
802
    if (!iface_up(ifname)) {
762
 
        err_iface_down();
763
 
        unmark_facility(TCPUDPIDFILE, ifname);
764
 
        return;
 
803
        err_iface_down();
 
804
        unmark_facility(TCPUDPIDFILE, ifname);
 
805
        return;
765
806
    }
766
807
 
767
808
    if ((first_active_facility()) && (options->promisc)) {
768
 
        init_promisc_list(&promisc_list);
769
 
        save_promisc_list(promisc_list);
770
 
        srpromisc(1, promisc_list);
771
 
        destroy_promisc_list(&promisc_list);
 
809
        init_promisc_list(&promisc_list);
 
810
        save_promisc_list(promisc_list);
 
811
        srpromisc(1, promisc_list);
 
812
        destroy_promisc_list(&promisc_list);
772
813
    }
773
814
 
774
815
    adjust_instance_count(PROCCOUNTFILE, 1);
775
816
    active_facility_countfile[0] = '\0';
776
 
    
 
817
 
777
818
    initportlist(&list);
778
819
    statwin = newwin(1, COLS, LINES - 2, 0);
779
820
    statpanel = new_panel(statwin);
781
822
    wattrset(statwin, IPSTATLABELATTR);
782
823
    sprintf(sp_buf, "%%%dc", COLS);
783
824
    mvwprintw(statwin, 0, 0, sp_buf, ' ');
784
 
        
 
825
 
785
826
    move(LINES - 1, 1);
786
827
    scrollkeyhelp();
787
828
    sortkeyhelp();
788
829
    stdexitkeyhelp();
789
830
 
790
831
    if (options->servnames)
791
 
        setservent(1);
 
832
        setservent(1);
792
833
 
793
834
    if (logging) {
794
 
        if (strcmp(current_logfile, "") == 0)
 
835
        if (strcmp(current_logfile, "") == 0) {
795
836
            snprintf(current_logfile, 80, "%s-%s.log", TCPUDPLOG, ifname);
796
 
        
797
 
        if (!daemonized)
798
 
            input_logfile(current_logfile, &logging);
 
837
 
 
838
            if (!daemonized)
 
839
                input_logfile(current_logfile, &logging);
 
840
        }
799
841
    }
800
 
    
 
842
 
801
843
    if (logging) {
802
 
        opentlog(&logfile, current_logfile);
 
844
        opentlog(&logfile, current_logfile);
803
845
 
804
 
        if (logfile == NULL)
805
 
            logging = 0;
 
846
        if (logfile == NULL)
 
847
            logging = 0;
806
848
    }
807
849
    if (logging)
808
 
        signal(SIGUSR1, rotate_serv_log);
 
850
        signal(SIGUSR1, rotate_serv_log);
809
851
 
810
852
    rotate_flag = 0;
811
853
    writelog(logging, logfile,
812
 
             "******** TCP/UDP service monitor started ********");
 
854
             "******** TCP/UDP service monitor started ********");
813
855
 
814
856
    isdnfd = -1;
815
857
    exitloop = 0;
820
862
    mvwprintw(statwin, 0, 1, "No entries");
821
863
    update_panels();
822
864
    doupdate();
823
 
    
 
865
 
824
866
    while (!exitloop) {
825
 
        getpacket(fd, buf, &fromaddr, &ch, &br, iface, list.win);
826
 
 
827
 
        if (ch != ERR) {
828
 
            if (keymode == 0) {
829
 
                switch (ch) {
830
 
                case KEY_UP:
831
 
                    if (list.barptr != NULL) {
832
 
                        if (list.barptr->prev_entry != NULL) {
833
 
                            serv_tmp = list.barptr;
834
 
                            set_barptr((char **) &(list.barptr),
835
 
                                (char *) list.barptr->prev_entry,
836
 
                                &(list.barptr->prev_entry->starttime),
837
 
                                (char *) &(list.barptr->prev_entry->spans),
838
 
                                sizeof(struct serv_spans), statwin, &statcleared, statx);
839
 
                            printportent(&list, serv_tmp, idx);
840
 
                            
841
 
                            if (list.baridx == 1) {
842
 
                                scrollservwin(&list, SCROLLDOWN, &idx);
843
 
                            } else
844
 
                                list.baridx--;
845
 
                            
846
 
                            printportent(&list, list.barptr, idx);
847
 
                        }
848
 
                    }
849
 
                    break;
850
 
                case KEY_DOWN:
851
 
                    if (list.barptr != NULL) {
852
 
                        if (list.barptr->next_entry != NULL) {
853
 
                            serv_tmp = list.barptr;
854
 
                            set_barptr((char **) &(list.barptr),
855
 
                                (char *) list.barptr->next_entry,
856
 
                                &(list.barptr->next_entry->starttime),
857
 
                                (char *) &(list.barptr->next_entry->spans),
858
 
                                sizeof(struct serv_spans), statwin, &statcleared, statx);
859
 
                            printportent(&list, serv_tmp, idx);
860
 
                            
861
 
                            if (list.baridx == list.imaxy) {
862
 
                                scrollservwin(&list, SCROLLUP, &idx);
863
 
                            } else
864
 
                                list.baridx++;
865
 
                            
866
 
                            printportent(&list, list.barptr, idx);
867
 
                        }
868
 
                    }
869
 
                    break;
870
 
                case KEY_PPAGE:
871
 
                case '-':
872
 
                    if (list.barptr != NULL) {
873
 
                        pageservwin(&list, SCROLLDOWN, &idx);
874
 
                        
875
 
                        set_barptr((char **) &(list.barptr), (char *) (list.lastvisible),
876
 
                            &(list.lastvisible->starttime),
877
 
                            (char *) &(list.lastvisible->spans),
878
 
                            sizeof(struct serv_spans), statwin, &statcleared, statx);
879
 
                        list.baridx = list.lastvisible->idx - idx + 1;
880
 
                        
881
 
                        refresh_serv_screen(&list, idx);
882
 
                    }
883
 
                    break;
884
 
                case KEY_NPAGE:
885
 
                case ' ':
886
 
                    if (list.barptr != NULL) {
887
 
                        pageservwin(&list, SCROLLUP, &idx);
888
 
                        
889
 
                        set_barptr((char **) &(list.barptr), (char *) (list.firstvisible),
890
 
                            &(list.firstvisible->starttime),
891
 
                            (char *) &(list.firstvisible->spans),
892
 
                            sizeof(struct serv_spans), statwin, &statcleared, statx);
893
 
                        list.baridx = 1;
894
 
                        
895
 
                        refresh_serv_screen(&list, idx);
896
 
                    }
897
 
                    break;
898
 
                case 12:
899
 
                case 'l':
900
 
                case 'L':
901
 
                    tx_refresh_screen();
902
 
                    break;
903
 
                case 's':
904
 
                case 'S':
905
 
                    show_portsort_keywin(&sortwin, &sortpanel);
906
 
                    keymode = 1;
907
 
                    break;
908
 
                case 'q':
909
 
                case 'Q':
910
 
                case 'x':
911
 
                case 'X':
912
 
                case 27:
913
 
                case 24:
914
 
                    exitloop = 1;
915
 
                }
916
 
            } else if (keymode == 1) {
917
 
                del_panel(sortpanel);
918
 
                delwin(sortwin);
919
 
                sortportents(&list, &idx, ch);
920
 
                keymode = 0;
921
 
                if (list.barptr != NULL) {
922
 
                    set_barptr((char **) &(list.barptr), (char *) list.firstvisible,
923
 
                        &(list.firstvisible->starttime),
924
 
                        (char *) &(list.firstvisible->spans),
925
 
                        sizeof(struct serv_spans), statwin, &statcleared, statx);
926
 
                    list.baridx = 1;
927
 
                }
928
 
                refresh_serv_screen(&list, idx);
929
 
                update_panels();
930
 
                doupdate();
931
 
            }
932
 
        }
933
 
 
934
 
        if (br > 0) {
 
867
        gettimeofday(&tv, NULL);
 
868
        now = tv.tv_sec;
 
869
        unow = tv.tv_sec * 1e+6 + tv.tv_usec;
 
870
 
 
871
        if (now - timeint >= 5) {
 
872
            printelapsedtime(starttime, now, LINES - 4, 20,
 
873
                             list.borderwin);
 
874
            timeint = now;
 
875
        }
 
876
        if (((now - startlog) >= options->logspan) && (logging)) {
 
877
            writeutslog(list.head, now - starttime, options->actmode,
 
878
                        logfile);
 
879
            startlog = now;
 
880
        }
 
881
 
 
882
        if (list.barptr != NULL) {
 
883
            if ((now - list.barptr->starttime) >= 5) {
 
884
                update_serv_rates(&list, statwin, options->actmode,
 
885
                                  &statcleared);
 
886
            }
 
887
        }
 
888
 
 
889
        if (((options->updrate != 0)
 
890
             && (now - updtime >= options->updrate))
 
891
            || ((options->updrate == 0)
 
892
                && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY))) {
 
893
            update_panels();
 
894
            doupdate();
 
895
            updtime = now;
 
896
            updtime_usec = unow;
 
897
        }
 
898
        check_rotate_flag(&logfile, logging);
 
899
 
 
900
        if ((facilitytime != 0)
 
901
            && (((now - starttime) / 60) >= facilitytime))
 
902
            exitloop = 1;
 
903
 
 
904
        getpacket(fd, buf, &fromaddr, &ch, &br, iface, list.win);
 
905
 
 
906
        if (ch != ERR) {
 
907
            if (keymode == 0) {
 
908
                switch (ch) {
 
909
                case KEY_UP:
 
910
                    if (list.barptr != NULL) {
 
911
                        if (list.barptr->prev_entry != NULL) {
 
912
                            serv_tmp = list.barptr;
 
913
                            set_barptr((char **) &(list.barptr),
 
914
                                       (char *) list.barptr->prev_entry,
 
915
                                       &(list.barptr->prev_entry->
 
916
                                         starttime),
 
917
                                       (char *) &(list.barptr->prev_entry->
 
918
                                                  spans),
 
919
                                       sizeof(struct serv_spans), statwin,
 
920
                                       &statcleared, statx);
 
921
                            printportent(&list, serv_tmp, idx);
 
922
 
 
923
                            if (list.baridx == 1) {
 
924
                                scrollservwin(&list, SCROLLDOWN, &idx);
 
925
                            } else
 
926
                                list.baridx--;
 
927
 
 
928
                            printportent(&list, list.barptr, idx);
 
929
                        }
 
930
                    }
 
931
                    break;
 
932
                case KEY_DOWN:
 
933
                    if (list.barptr != NULL) {
 
934
                        if (list.barptr->next_entry != NULL) {
 
935
                            serv_tmp = list.barptr;
 
936
                            set_barptr((char **) &(list.barptr),
 
937
                                       (char *) list.barptr->next_entry,
 
938
                                       &(list.barptr->next_entry->
 
939
                                         starttime),
 
940
                                       (char *) &(list.barptr->next_entry->
 
941
                                                  spans),
 
942
                                       sizeof(struct serv_spans), statwin,
 
943
                                       &statcleared, statx);
 
944
                            printportent(&list, serv_tmp, idx);
 
945
 
 
946
                            if (list.baridx == list.imaxy) {
 
947
                                scrollservwin(&list, SCROLLUP, &idx);
 
948
                            } else
 
949
                                list.baridx++;
 
950
 
 
951
                            printportent(&list, list.barptr, idx);
 
952
                        }
 
953
                    }
 
954
                    break;
 
955
                case KEY_PPAGE:
 
956
                case '-':
 
957
                    if (list.barptr != NULL) {
 
958
                        pageservwin(&list, SCROLLDOWN, &idx);
 
959
 
 
960
                        set_barptr((char **) &(list.barptr),
 
961
                                   (char *) (list.lastvisible),
 
962
                                   &(list.lastvisible->starttime),
 
963
                                   (char *) &(list.lastvisible->spans),
 
964
                                   sizeof(struct serv_spans), statwin,
 
965
                                   &statcleared, statx);
 
966
                        list.baridx = list.lastvisible->idx - idx + 1;
 
967
 
 
968
                        refresh_serv_screen(&list, idx);
 
969
                    }
 
970
                    break;
 
971
                case KEY_NPAGE:
 
972
                case ' ':
 
973
                    if (list.barptr != NULL) {
 
974
                        pageservwin(&list, SCROLLUP, &idx);
 
975
 
 
976
                        set_barptr((char **) &(list.barptr),
 
977
                                   (char *) (list.firstvisible),
 
978
                                   &(list.firstvisible->starttime),
 
979
                                   (char *) &(list.firstvisible->spans),
 
980
                                   sizeof(struct serv_spans), statwin,
 
981
                                   &statcleared, statx);
 
982
                        list.baridx = 1;
 
983
 
 
984
                        refresh_serv_screen(&list, idx);
 
985
                    }
 
986
                    break;
 
987
                case 12:
 
988
                case 'l':
 
989
                case 'L':
 
990
                    tx_refresh_screen();
 
991
                    break;
 
992
                case 's':
 
993
                case 'S':
 
994
                    show_portsort_keywin(&sortwin, &sortpanel);
 
995
                    keymode = 1;
 
996
                    break;
 
997
                case 'q':
 
998
                case 'Q':
 
999
                case 'x':
 
1000
                case 'X':
 
1001
                case 27:
 
1002
                case 24:
 
1003
                    exitloop = 1;
 
1004
                }
 
1005
            } else if (keymode == 1) {
 
1006
                del_panel(sortpanel);
 
1007
                delwin(sortwin);
 
1008
                sortportents(&list, &idx, ch);
 
1009
                keymode = 0;
 
1010
                if (list.barptr != NULL) {
 
1011
                    set_barptr((char **) &(list.barptr),
 
1012
                               (char *) list.firstvisible,
 
1013
                               &(list.firstvisible->starttime),
 
1014
                               (char *) &(list.firstvisible->spans),
 
1015
                               sizeof(struct serv_spans), statwin,
 
1016
                               &statcleared, statx);
 
1017
                    list.baridx = 1;
 
1018
                }
 
1019
                refresh_serv_screen(&list, idx);
 
1020
                update_panels();
 
1021
                doupdate();
 
1022
            }
 
1023
        }
 
1024
 
 
1025
        if (br > 0) {
935
1026
            pkt_result = processpacket(buf, &ipacket, &br, &tot_br,
936
 
                    &sport, &dport, &fromaddr, &linktype,
937
 
                    ofilter, iface, ifname);
938
 
                    
 
1027
                                       &sport, &dport, &fromaddr,
 
1028
                                       &linktype, ofilter,
 
1029
                                       MATCH_OPPOSITE_USECONFIG, iface,
 
1030
                                       ifname);
 
1031
 
939
1032
            if (pkt_result != PACKET_OK)
940
 
                    continue;
 
1033
                continue;
941
1034
 
942
1035
            if ((((struct iphdr *) ipacket)->protocol == IPPROTO_TCP)
943
 
                    || (((struct iphdr *) ipacket)->protocol ==
944
 
                        IPPROTO_UDP)) {
945
 
                printport(&list, ((struct iphdr *) ipacket)->protocol,
946
 
                              ntohs(sport),
947
 
                              ntohs(((struct iphdr *) ipacket)->tot_len),
948
 
                              idx, 0, ports, options->servnames);
949
 
                printport(&list, ((struct iphdr *) ipacket)->protocol,
950
 
                              ntohs(dport),
951
 
                              ntohs(((struct iphdr *) ipacket)->tot_len),
952
 
                              idx, 1, ports, options->servnames);
953
 
                    
 
1036
                || (((struct iphdr *) ipacket)->protocol == IPPROTO_UDP)) {
 
1037
                updateportent(&list, ((struct iphdr *) ipacket)->protocol,
 
1038
                              ntohs(sport), ntohs(dport),
 
1039
                              ntohs(((struct iphdr *) ipacket)->tot_len),
 
1040
                              idx, ports, options->servnames);
 
1041
 
954
1042
                if ((list.barptr == NULL) && (list.head != NULL)) {
955
 
                    set_barptr((char **) &(list.barptr), (char *) list.head,
956
 
                            &(list.head->starttime), (char *) &(list.head->spans),
957
 
                            sizeof(struct serv_spans), statwin, &statcleared, statx);
 
1043
                    set_barptr((char **) &(list.barptr),
 
1044
                               (char *) list.head, &(list.head->starttime),
 
1045
                               (char *) &(list.head->spans),
 
1046
                               sizeof(struct serv_spans), statwin,
 
1047
                               &statcleared, statx);
958
1048
                    list.baridx = 1;
959
 
                }        
960
 
            }
961
 
        }
962
 
        gettimeofday(&tv, NULL);
963
 
        now = tv.tv_sec;
964
 
        unow = tv.tv_sec * 1e+6 + tv.tv_usec;
965
 
 
966
 
        if (now - timeint >= 5) {
967
 
            printelapsedtime(starttime, now, LINES - 4, 20,
968
 
                             list.borderwin);
969
 
            timeint = now;
970
 
        }
971
 
        if (((now - startlog) >= options->logspan) && (logging)) {
972
 
            writeutslog(list.head, now - starttime, options->actmode,
973
 
                logfile);
974
 
            startlog = now;
975
 
        }
976
 
        
977
 
        if (list.barptr != NULL) {
978
 
            if ((now - list.barptr->starttime) >= 5) {
979
 
                update_serv_rates(&list, statwin, options->actmode, &statcleared);
 
1049
                }
980
1050
            }
981
1051
        }
982
 
        
983
 
        if (((options->updrate != 0)
984
 
             && (now - updtime >= options->updrate))
985
 
            || ((options->updrate == 0)
986
 
                && (unow - updtime_usec >= DEFAULT_UPDATE_DELAY))) {
987
 
            update_panels();
988
 
            doupdate();
989
 
            updtime = now;
990
 
            updtime_usec = unow;
991
 
        }
992
 
        check_rotate_flag(&logfile, logging);
993
 
 
994
 
        if ((facilitytime != 0)
995
 
            && (((now - starttime) / 60) >= facilitytime))
996
 
            exitloop = 1;
997
1052
    }
998
1053
 
999
1054
    if (logging) {
1000
 
        signal(SIGUSR1, SIG_DFL);
1001
 
        writeutslog(list.head, time((time_t *) NULL) - starttime, options->actmode,
1002
 
            logfile);
1003
 
        writelog(logging, logfile,
1004
 
                 "******** TCP/UDP service monitor stopped ********");
1005
 
        fclose(logfile);
 
1055
        signal(SIGUSR1, SIG_DFL);
 
1056
        writeutslog(list.head, time((time_t *) NULL) - starttime,
 
1057
                    options->actmode, logfile);
 
1058
        writelog(logging, logfile,
 
1059
                 "******** TCP/UDP service monitor stopped ********");
 
1060
        fclose(logfile);
1006
1061
    }
1007
1062
    if (options->servnames)
1008
 
        endservent();
 
1063
        endservent();
1009
1064
 
1010
1065
    if ((options->promisc) && (is_last_instance())) {
1011
 
        load_promisc_list(&promisc_list);
1012
 
        srpromisc(0, promisc_list);
1013
 
        destroy_promisc_list(&promisc_list);
 
1066
        load_promisc_list(&promisc_list);
 
1067
        srpromisc(0, promisc_list);
 
1068
        destroy_promisc_list(&promisc_list);
1014
1069
    }
1015
1070
 
1016
1071
    adjust_instance_count(PROCCOUNTFILE, -1);
1045
1100
    panel = new_panel(win);
1046
1101
 
1047
1102
    wattrset(bw, DLGBOXATTR);
1048
 
    box(bw, ACS_VLINE, ACS_HLINE);
 
1103
    tx_box(bw, ACS_VLINE, ACS_HLINE);
1049
1104
 
1050
1105
    wattrset(win, DLGTEXTATTR);
1051
1106
    tx_colorwin(win);
1065
1120
    stdkeyhelp(win);
1066
1121
 
1067
1122
    tx_initfields(&list, 1, 20, (LINES - 14) / 2 + 10, (COLS - 50) / 2 - 8,
1068
 
        DLGTEXTATTR, FIELDATTR);
 
1123
                  DLGTEXTATTR, FIELDATTR);
1069
1124
    mvwprintw(list.fieldwin, 0, 6, "to");
1070
1125
 
1071
1126
    tx_addfield(&list, 5, 0, 0, "");
1074
1129
    tx_fillfields(&list, aborted);
1075
1130
 
1076
1131
    if (!(*aborted)) {
1077
 
        *port_min = atoi(list.list->buf);
1078
 
        *port_max = atoi(list.list->nextfield->buf);
 
1132
        *port_min = atoi(list.list->buf);
 
1133
        *port_max = atoi(list.list->nextfield->buf);
1079
1134
    }
1080
1135
    del_panel(bp);
1081
1136
    delwin(bw);
1094
1149
    fd = open(PORTFILE, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
1095
1150
 
1096
1151
    if (fd < 0) {
1097
 
        tx_errbox("Unable to open port list file", ANYKEY_MSG, &resp);
1098
 
        return;
 
1152
        tx_errbox("Unable to open port list file", ANYKEY_MSG, &resp);
 
1153
        return;
1099
1154
    }
1100
1155
    while (ptmp != NULL) {
1101
 
        bw = write(fd, &(ptmp->port_min), sizeof(unsigned int));
1102
 
        bw = write(fd, &(ptmp->port_max), sizeof(unsigned int));
 
1156
        bw = write(fd, &(ptmp->port_min), sizeof(unsigned int));
 
1157
        bw = write(fd, &(ptmp->port_max), sizeof(unsigned int));
1103
1158
 
1104
 
        if (bw < 0) {
1105
 
            tx_errbox("Unable to write port/range entry", ANYKEY_MSG, &resp);
1106
 
            destroyporttab(table);
1107
 
            close(fd);
1108
 
            return;
1109
 
        }
1110
 
        ptmp = ptmp->next_entry;
 
1159
        if (bw < 0) {
 
1160
            tx_errbox("Unable to write port/range entry", ANYKEY_MSG,
 
1161
                      &resp);
 
1162
            destroyporttab(table);
 
1163
            close(fd);
 
1164
            return;
 
1165
        }
 
1166
        ptmp = ptmp->next_entry;
1111
1167
    }
1112
1168
 
1113
1169
    close(fd);
1114
1170
}
1115
1171
 
1116
1172
int dup_portentry(struct porttab *table, unsigned int min,
1117
 
                  unsigned int max)
 
1173
                  unsigned int max)
1118
1174
{
1119
1175
    struct porttab *ptmp = table;
1120
1176
 
1121
1177
    while (ptmp != NULL) {
1122
 
        if ((ptmp->port_min == min) && (ptmp->port_max == max))
1123
 
            return 1;
 
1178
        if ((ptmp->port_min == min) && (ptmp->port_max == max))
 
1179
            return 1;
1124
1180
 
1125
 
        ptmp = ptmp->next_entry;
 
1181
        ptmp = ptmp->next_entry;
1126
1182
    }
1127
1183
 
1128
1184
    return 0;
1138
1194
    portdlg(&port_min, &port_max, &aborted, 0);
1139
1195
 
1140
1196
    if (!aborted) {
1141
 
        if (dup_portentry(*table, port_min, port_max))
1142
 
            tx_errbox("Duplicate port/range entry", ANYKEY_MSG, &resp);
1143
 
        else {
1144
 
            ptmp = malloc(sizeof(struct porttab));
1145
 
 
1146
 
            ptmp->port_min = port_min;
1147
 
            ptmp->port_max = port_max;
1148
 
            ptmp->prev_entry = NULL;
1149
 
            ptmp->next_entry = *table;
1150
 
 
1151
 
            if (*table != NULL)
1152
 
                (*table)->prev_entry = ptmp;
1153
 
 
1154
 
            *table = ptmp;
1155
 
            saveportlist(*table);
1156
 
        }
 
1197
        if (dup_portentry(*table, port_min, port_max))
 
1198
            tx_errbox("Duplicate port/range entry", ANYKEY_MSG, &resp);
 
1199
        else {
 
1200
            ptmp = malloc(sizeof(struct porttab));
 
1201
 
 
1202
            ptmp->port_min = port_min;
 
1203
            ptmp->port_max = port_max;
 
1204
            ptmp->prev_entry = NULL;
 
1205
            ptmp->next_entry = *table;
 
1206
 
 
1207
            if (*table != NULL)
 
1208
                (*table)->prev_entry = ptmp;
 
1209
 
 
1210
            *table = ptmp;
 
1211
            saveportlist(*table);
 
1212
        }
1157
1213
    }
1158
1214
    update_panels();
1159
1215
    doupdate();
1171
1227
 
1172
1228
    fd = open(PORTFILE, O_RDONLY);
1173
1229
    if (fd < 0)
1174
 
        return;
 
1230
        return;
1175
1231
 
1176
1232
    do {
1177
 
        ptemp = malloc(sizeof(struct porttab));
1178
 
 
1179
 
        br = read(fd, &(ptemp->port_min), sizeof(unsigned int));
1180
 
        br = read(fd, &(ptemp->port_max), sizeof(unsigned int));
1181
 
 
1182
 
        if (br < 0) {
1183
 
            tx_errbox("Error reading port list", ANYKEY_MSG, &resp);
1184
 
            close(fd);
1185
 
            destroyporttab(*table);
1186
 
            return;
1187
 
        }
1188
 
        if (br > 0) {
1189
 
            if (*table == NULL) {
1190
 
                *table = ptemp;
1191
 
                ptemp->prev_entry = NULL;
1192
 
            }
1193
 
            if (tail != NULL) {
1194
 
                tail->next_entry = ptemp;
1195
 
                ptemp->prev_entry = tail;
1196
 
            }
1197
 
            tail = ptemp;
1198
 
            ptemp->next_entry = NULL;
1199
 
        } else
1200
 
            free(ptemp);
 
1233
        ptemp = malloc(sizeof(struct porttab));
 
1234
 
 
1235
        br = read(fd, &(ptemp->port_min), sizeof(unsigned int));
 
1236
        br = read(fd, &(ptemp->port_max), sizeof(unsigned int));
 
1237
 
 
1238
        if (br < 0) {
 
1239
            tx_errbox("Error reading port list", ANYKEY_MSG, &resp);
 
1240
            close(fd);
 
1241
            destroyporttab(*table);
 
1242
            return;
 
1243
        }
 
1244
        if (br > 0) {
 
1245
            if (*table == NULL) {
 
1246
                *table = ptemp;
 
1247
                ptemp->prev_entry = NULL;
 
1248
            }
 
1249
            if (tail != NULL) {
 
1250
                tail->next_entry = ptemp;
 
1251
                ptemp->prev_entry = tail;
 
1252
            }
 
1253
            tail = ptemp;
 
1254
            ptemp->next_entry = NULL;
 
1255
        } else
 
1256
            free(ptemp);
1201
1257
 
1202
1258
    } while (br > 0);
1203
1259
 
1208
1264
{
1209
1265
    wprintw(win, "%u", ptmp->port_min);
1210
1266
    if (ptmp->port_max != 0)
1211
 
        wprintw(win, " to %u", ptmp->port_max);
 
1267
        wprintw(win, " to %u", ptmp->port_max);
1212
1268
}
1213
1269
 
1214
1270
void displayports(struct porttab **table, WINDOW * win)
1217
1273
    short i = 0;
1218
1274
 
1219
1275
    do {
1220
 
        wmove(win, i, 2);
1221
 
        displayportentry(ptmp, win);
 
1276
        wmove(win, i, 2);
 
1277
        displayportentry(ptmp, win);
1222
1278
 
1223
 
        i++;
1224
 
        ptmp = ptmp->next_entry;
 
1279
        i++;
 
1280
        ptmp = ptmp->next_entry;
1225
1281
    } while ((i < 18) && (ptmp != NULL));
1226
1282
 
1227
1283
    update_panels();
1229
1285
}
1230
1286
 
1231
1287
void operate_portselect(struct porttab **table, struct porttab **node,
1232
 
                        int *aborted)
 
1288
                        int *aborted)
1233
1289
{
1234
1290
    int ch = 0;
1235
1291
    struct scroll_list list;
1236
1292
    char listtext[20];
1237
1293
 
1238
1294
    tx_init_listbox(&list, 25, 22, (COLS - 25) / 2, (LINES - 22) / 2,
1239
 
        STDATTR, BOXATTR, BARSTDATTR, HIGHATTR);
 
1295
                    STDATTR, BOXATTR, BARSTDATTR, HIGHATTR);
1240
1296
 
1241
1297
    tx_set_listbox_title(&list, "Select Port/Range", 1);
1242
 
    
1243
 
    *node = *table;    
 
1298
 
 
1299
    *node = *table;
1244
1300
    while (*node != NULL) {
1245
 
        snprintf(listtext, 20, "%d to %d", (*node)->port_min, (*node)->port_max);
 
1301
        snprintf(listtext, 20, "%d to %d", (*node)->port_min,
 
1302
                 (*node)->port_max);
1246
1303
        tx_add_list_entry(&list, (char *) *node, listtext);
1247
1304
        *node = (*node)->next_entry;
1248
1305
    }
1249
 
    
 
1306
 
1250
1307
    tx_show_listbox(&list);
1251
1308
    tx_operate_listbox(&list, &ch, aborted);
1252
 
    
 
1309
 
1253
1310
    if (!(*aborted))
1254
 
        *node = (struct porttab *) list.textptr->nodeptr;    
 
1311
        *node = (struct porttab *) list.textptr->nodeptr;
1255
1312
 
1256
1313
    tx_close_listbox(&list);
1257
1314
    tx_destroy_list(&list);
1258
1315
}
1259
1316
 
1260
1317
void selectport(struct porttab **table, struct porttab **node,
1261
 
                int *aborted)
 
1318
                int *aborted)
1262
1319
{
1263
1320
    int resp;
1264
 
    
 
1321
 
1265
1322
    if (*table == NULL) {
1266
 
        tx_errbox("No custom ports", ANYKEY_MSG, &resp);
1267
 
        return;
 
1323
        tx_errbox("No custom ports", ANYKEY_MSG, &resp);
 
1324
        return;
1268
1325
    }
1269
1326
 
1270
1327
    operate_portselect(table, node, aborted);
1273
1330
void delport(struct porttab **table, struct porttab *ptmp)
1274
1331
{
1275
1332
    if (ptmp != NULL) {
1276
 
        if (ptmp == *table) {
1277
 
            *table = (*table)->next_entry;
1278
 
            if (*table != NULL)
1279
 
                (*table)->prev_entry = NULL;
1280
 
        } else {
1281
 
            ptmp->prev_entry->next_entry = ptmp->next_entry;
1282
 
 
1283
 
            if (ptmp->next_entry != NULL)
1284
 
                ptmp->next_entry->prev_entry = ptmp->prev_entry;
1285
 
        }
1286
 
 
1287
 
        free(ptmp);
 
1333
        if (ptmp == *table) {
 
1334
            *table = (*table)->next_entry;
 
1335
            if (*table != NULL)
 
1336
                (*table)->prev_entry = NULL;
 
1337
        } else {
 
1338
            ptmp->prev_entry->next_entry = ptmp->next_entry;
 
1339
 
 
1340
            if (ptmp->next_entry != NULL)
 
1341
                ptmp->next_entry->prev_entry = ptmp->prev_entry;
 
1342
        }
 
1343
 
 
1344
        free(ptmp);
1288
1345
    }
1289
1346
}
1290
1347
 
1296
1353
    selectport(table, &ptmp, &aborted);
1297
1354
 
1298
1355
    if (!aborted) {
1299
 
        delport(table, ptmp);
1300
 
        saveportlist(*table);
 
1356
        delport(table, ptmp);
 
1357
        saveportlist(*table);
1301
1358
    }
1302
1359
}
1303
1360
 
1307
1364
    struct porttab *ctemp = NULL;
1308
1365
 
1309
1366
    if (ptemp != NULL)
1310
 
        ctemp = ptemp->next_entry;
 
1367
        ctemp = ptemp->next_entry;
1311
1368
 
1312
1369
    while (ptemp != NULL) {
1313
 
        free(ptemp);
1314
 
        ptemp = ctemp;
 
1370
        free(ptemp);
 
1371
        ptemp = ctemp;
1315
1372
 
1316
 
        if (ctemp != NULL)
1317
 
            ctemp = ctemp->next_entry;
 
1373
        if (ctemp != NULL)
 
1374
            ctemp = ctemp->next_entry;
1318
1375
    }
1319
1376
}