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

« back to all changes in this revision

Viewing changes to src/landesc.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:
49
49
 
50
50
void add_desclist_node(struct desclist *list, struct desclistent *ptmp)
51
51
{
52
 
        if (list->head == NULL) {
53
 
            list->head = ptmp;
54
 
            ptmp->prev_entry = NULL;
55
 
        } else {
56
 
            list->tail->next_entry = ptmp;
57
 
            ptmp->prev_entry = list->tail;
58
 
        }
 
52
    if (list->head == NULL) {
 
53
        list->head = ptmp;
 
54
        ptmp->prev_entry = NULL;
 
55
    } else {
 
56
        list->tail->next_entry = ptmp;
 
57
        ptmp->prev_entry = list->tail;
 
58
    }
59
59
 
60
 
        ptmp->next_entry = NULL;
61
 
        list->tail = ptmp;
 
60
    ptmp->next_entry = NULL;
 
61
    list->tail = ptmp;
62
62
}
63
63
 
64
64
/*
68
68
 * In case of a duplicate in the IPTraf definition files and /etc/ethers,
69
69
 * the IPTraf definition files take precedence.
70
70
 */
71
 
 
72
 
void loaddesclist(struct desclist *list, unsigned int linktype, int withethers)
 
71
 
 
72
void loaddesclist(struct desclist *list, unsigned int linktype,
 
73
                  int withethers)
73
74
{
74
75
    struct desclistent *ptmp = NULL;
75
76
    FILE *fd = NULL;
76
77
    char descline[140];
77
78
    char *desctoken;
78
79
    char etherline[140];
79
 
    int i,j;   /* counters used when parsing /etc/ethers */
80
 
        
 
80
    int i, j;                   /* counters used when parsing /etc/ethers */
 
81
 
81
82
    bzero(list, sizeof(struct desclist));
82
83
 
83
84
    if (linktype == LINK_ETHERNET)
84
 
        fd = fopen(ETHFILE, "r");
 
85
        fd = fopen(ETHFILE, "r");
85
86
    else if (linktype == LINK_FDDI)
86
 
        fd = fopen(FDDIFILE, "r");
 
87
        fd = fopen(FDDIFILE, "r");
87
88
 
88
89
    if (fd == NULL) {
89
 
        return;
 
90
        return;
90
91
    }
91
92
    while (!feof(fd)) {
92
 
        ptmp = malloc(sizeof(struct desclistent));
93
 
        if (ptmp == NULL) {
94
 
            printnomem();
95
 
            return;
96
 
        }
97
 
        bzero(ptmp, sizeof(struct desclistent));
98
 
        bzero(descline, 140);
99
 
        fgets(descline, 140, fd);
100
 
 
101
 
        if (strcmp(descline, "") == 0) {
102
 
            free(ptmp);
103
 
            continue;
104
 
        }
105
 
        strncpy(ptmp->rec.address, strtok(descline, ":"), 12);
106
 
        desctoken = strtok(NULL, "\n");
107
 
 
108
 
        if (desctoken != NULL)
109
 
            strncpy(ptmp->rec.desc, desctoken, 64);
110
 
        else
111
 
            strcpy(ptmp->rec.desc, "");
112
 
        
 
93
        ptmp = malloc(sizeof(struct desclistent));
 
94
        if (ptmp == NULL) {
 
95
            printnomem();
 
96
            return;
 
97
        }
 
98
        bzero(ptmp, sizeof(struct desclistent));
 
99
        bzero(descline, 140);
 
100
        fgets(descline, 140, fd);
 
101
 
 
102
        if (strcmp(descline, "") == 0) {
 
103
            free(ptmp);
 
104
            continue;
 
105
        }
 
106
        strncpy(ptmp->rec.address, strtok(descline, ":"), 12);
 
107
        desctoken = strtok(NULL, "\n");
 
108
 
 
109
        if (desctoken != NULL)
 
110
            strncpy(ptmp->rec.desc, desctoken, 64);
 
111
        else
 
112
            strcpy(ptmp->rec.desc, "");
 
113
 
113
114
        add_desclist_node(list, ptmp);
114
115
    }
115
116
 
116
117
    fclose(fd);
117
 
    
 
118
 
118
119
    /* 
119
120
     * Loads MAC addresses defined in /etc/ethers.  Contributed by
120
121
     * Debian maintainter Frederic Peters <fpeters@debian.org>.  Thanks
132
133
 
133
134
    if (linktype != LINK_ETHERNET)
134
135
        return;
135
 
            
 
136
 
136
137
    fd = fopen("/etc/ethers", "r");
137
 
    
 
138
 
138
139
    if (fd == NULL)
139
 
        return;
140
 
    
 
140
        return;
 
141
 
141
142
    while (!feof(fd)) {
142
 
        ptmp = malloc(sizeof(struct desclistent));
143
 
        if (ptmp == NULL) {
144
 
            printnomem();
145
 
            return;
146
 
        }
147
 
        bzero(ptmp, sizeof(struct desclistent));
148
 
        bzero(descline, 140);
149
 
        bzero(etherline, 140);
150
 
        fgets(etherline, 140, fd);
151
 
 
152
 
        /* 
153
 
         * Convert /etc/ethers line to a descline
154
 
         */
155
 
        if (etherline[0] == '#' || etherline[0] == '\n' || etherline[0] == 0) {
156
 
            free(ptmp);
157
 
            continue;
158
 
        }
159
 
        
160
 
        if (strchr(etherline, '\n') )
161
 
            strchr(etherline, '\n')[0] = 0;
162
 
        
163
 
        j = 0;
164
 
        for (i = 0; i < 20 && !isspace(etherline[i]); i++) {
165
 
            if (etherline[i] == ':')
166
 
                continue;
167
 
            descline[j++] = tolower(etherline[i]);
168
 
        }
169
 
        descline[j] = ':';
170
 
        
171
 
        /* 
172
 
         * Skip over whitespace between MAC address and IP addr/host name
173
 
         */
174
 
         
175
 
        while (isspace(etherline[i++]))
176
 
            ;
177
 
           
178
 
        strncat(descline, etherline + i - 1, 130);
179
 
        
180
 
        if (strcmp(descline, "") == 0) {
181
 
            free(ptmp);
182
 
            continue;
183
 
        }
184
 
        
185
 
        strncpy(ptmp->rec.address, strtok(descline, ":"), 12);
186
 
        desctoken = strtok(NULL, "\n");
187
 
 
188
 
        if (desctoken != NULL)
189
 
            strncpy(ptmp->rec.desc, desctoken, 64);
190
 
        else
191
 
            strcpy(ptmp->rec.desc, "");
 
143
        ptmp = malloc(sizeof(struct desclistent));
 
144
        if (ptmp == NULL) {
 
145
            printnomem();
 
146
            return;
 
147
        }
 
148
        bzero(ptmp, sizeof(struct desclistent));
 
149
        bzero(descline, 140);
 
150
        bzero(etherline, 140);
 
151
        fgets(etherline, 140, fd);
 
152
 
 
153
        /* 
 
154
         * Convert /etc/ethers line to a descline
 
155
         */
 
156
        if (etherline[0] == '#' || etherline[0] == '\n'
 
157
            || etherline[0] == 0) {
 
158
            free(ptmp);
 
159
            continue;
 
160
        }
 
161
 
 
162
        if (strchr(etherline, '\n'))
 
163
            strchr(etherline, '\n')[0] = 0;
 
164
 
 
165
        j = 0;
 
166
        for (i = 0; i < 20 && !isspace(etherline[i]); i++) {
 
167
            if (etherline[i] == ':')
 
168
                continue;
 
169
            descline[j++] = tolower(etherline[i]);
 
170
        }
 
171
        descline[j] = ':';
 
172
 
 
173
        /* 
 
174
         * Skip over whitespace between MAC address and IP addr/host name
 
175
         */
 
176
 
 
177
        while (isspace(etherline[i++]));
 
178
 
 
179
        strncat(descline, etherline + i - 1, 130);
 
180
 
 
181
        if (strcmp(descline, "") == 0) {
 
182
            free(ptmp);
 
183
            continue;
 
184
        }
 
185
 
 
186
        strncpy(ptmp->rec.address, strtok(descline, ":"), 12);
 
187
        desctoken = strtok(NULL, "\n");
 
188
 
 
189
        if (desctoken != NULL)
 
190
            strncpy(ptmp->rec.desc, desctoken, 64);
 
191
        else
 
192
            strcpy(ptmp->rec.desc, "");
192
193
 
193
194
        add_desclist_node(list, ptmp);
194
195
    }
203
204
    struct desclistent *ptmp = list->head;
204
205
 
205
206
    if (linktype == LINK_ETHERNET)
206
 
        fd = fopen(ETHFILE, "w");
 
207
        fd = fopen(ETHFILE, "w");
207
208
    else if (linktype == LINK_FDDI)
208
 
        fd = fopen(FDDIFILE, "w");
 
209
        fd = fopen(FDDIFILE, "w");
209
210
 
210
211
    if (fd < 0) {
211
 
        etherr();
212
 
        return;
 
212
        etherr();
 
213
        return;
213
214
    }
214
215
    while (ptmp != NULL) {
215
 
        fprintf(fd, "%s:%s\n", ptmp->rec.address, ptmp->rec.desc);
216
 
        ptmp = ptmp->next_entry;
 
216
        fprintf(fd, "%s:%s\n", ptmp->rec.address, ptmp->rec.desc);
 
217
        ptmp = ptmp->next_entry;
217
218
    }
218
219
 
219
220
    fclose(fd);
225
226
    short i = 0;
226
227
 
227
228
    do {
228
 
        wmove(win, i, 2);
229
 
        wprintw(win, "%s    %s", ptmp->rec.address, ptmp->rec.desc);
230
 
        i++;
231
 
        ptmp = ptmp->next_entry;
 
229
        wmove(win, i, 2);
 
230
        wprintw(win, "%s    %s", ptmp->rec.address, ptmp->rec.desc);
 
231
        i++;
 
232
        ptmp = ptmp->next_entry;
232
233
    } while ((i < 18) && (ptmp != NULL));
233
234
 
234
235
    update_panels();
241
242
    struct desclistent *ctmp = NULL;
242
243
 
243
244
    if (list->head != NULL)
244
 
        ctmp = ptmp->next_entry;
 
245
        ctmp = ptmp->next_entry;
245
246
 
246
247
    while (ptmp != NULL) {
247
 
        free(ptmp);
248
 
        ptmp = ctmp;
 
248
        free(ptmp);
 
249
        ptmp = ctmp;
249
250
 
250
 
        if (ctmp != NULL)
251
 
            ctmp = ctmp->next_entry;
 
251
        if (ctmp != NULL)
 
252
            ctmp = ctmp->next_entry;
252
253
    }
253
254
}
254
255
 
255
256
void operate_descselect(struct desclist *list, struct desclistent **node,
256
 
                        WINDOW * win, int *aborted)
 
257
                        WINDOW * win, int *aborted)
257
258
{
258
259
    int ch = 0;
259
260
    int i = 0;
266
267
    sprintf(sp_buf, "%%%dc", COLS - 2);
267
268
 
268
269
    do {
269
 
        wattrset(win, PTRATTR);
270
 
        wmove(win, i, 1);
271
 
        waddch(win, ACS_RARROW);
272
 
        ch = wgetch(win);
273
 
        wmove(win, i, 1);
274
 
        waddch(win, ' ');
275
 
        wattrset(win, STDATTR);
276
 
 
277
 
        switch (ch) {
278
 
        case KEY_DOWN:
279
 
            if ((*node)->next_entry != NULL) {
280
 
                *node = (*node)->next_entry;
281
 
 
282
 
                if (i < 17)
283
 
                    i++;
284
 
                else {
285
 
                    wscrl(win, 1);
286
 
                    scrollok(win, 0);
287
 
                    wmove(win, 17, 0);
288
 
                    wprintw(win, sp_buf, ' ');
289
 
                    scrollok(win, 1);
290
 
                    wmove(win, 17, 2);
291
 
                    wprintw(win, "%s    %s", (*node)->rec.address,
292
 
                            (*node)->rec.desc);
293
 
                }
294
 
            }
295
 
            break;
296
 
        case KEY_UP:
297
 
            if ((*node)->prev_entry != NULL) {
298
 
                *node = (*node)->prev_entry;
299
 
 
300
 
                if (i > 0)
301
 
                    i--;
302
 
                else {
303
 
                    wscrl(win, -1);
304
 
                    wmove(win, 0, 0);
305
 
                    wprintw(win, sp_buf, ' ');
306
 
                    wmove(win, 0, 2);
307
 
                    wprintw(win, "%s    %s", (*node)->rec.address,
308
 
                            (*node)->rec.desc);
309
 
                }
310
 
            }
311
 
            break;
312
 
        case 13:
313
 
            exitloop = 1;
314
 
            *aborted = 0;
315
 
            break;
316
 
        case 27:
317
 
        case 24:
318
 
        case 'x':
319
 
        case 'X':
320
 
        case 'q':
321
 
        case 'Q':
322
 
            exitloop = 1;
323
 
            *aborted = 1;
324
 
            break;
325
 
        }
 
270
        wattrset(win, PTRATTR);
 
271
        wmove(win, i, 1);
 
272
        waddch(win, ACS_RARROW);
 
273
        ch = wgetch(win);
 
274
        wmove(win, i, 1);
 
275
        waddch(win, ' ');
 
276
        wattrset(win, STDATTR);
 
277
 
 
278
        switch (ch) {
 
279
        case KEY_DOWN:
 
280
            if ((*node)->next_entry != NULL) {
 
281
                *node = (*node)->next_entry;
 
282
 
 
283
                if (i < 17)
 
284
                    i++;
 
285
                else {
 
286
                    wscrl(win, 1);
 
287
                    scrollok(win, 0);
 
288
                    wmove(win, 17, 0);
 
289
                    wprintw(win, sp_buf, ' ');
 
290
                    scrollok(win, 1);
 
291
                    wmove(win, 17, 2);
 
292
                    wprintw(win, "%s    %s", (*node)->rec.address,
 
293
                            (*node)->rec.desc);
 
294
                }
 
295
            }
 
296
            break;
 
297
        case KEY_UP:
 
298
            if ((*node)->prev_entry != NULL) {
 
299
                *node = (*node)->prev_entry;
 
300
 
 
301
                if (i > 0)
 
302
                    i--;
 
303
                else {
 
304
                    wscrl(win, -1);
 
305
                    wmove(win, 0, 0);
 
306
                    wprintw(win, sp_buf, ' ');
 
307
                    wmove(win, 0, 2);
 
308
                    wprintw(win, "%s    %s", (*node)->rec.address,
 
309
                            (*node)->rec.desc);
 
310
                }
 
311
            }
 
312
            break;
 
313
        case 13:
 
314
            exitloop = 1;
 
315
            *aborted = 0;
 
316
            break;
 
317
        case 27:
 
318
        case 24:
 
319
        case 'x':
 
320
        case 'X':
 
321
        case 'q':
 
322
        case 'Q':
 
323
            exitloop = 1;
 
324
            *aborted = 1;
 
325
            break;
 
326
        }
326
327
    } while (!exitloop);
327
328
}
328
329
 
329
330
void selectdesc(struct desclist *list, struct desclistent **node,
330
 
                int *aborted)
 
331
                int *aborted)
331
332
{
332
333
    int resp;
333
334
    struct scroll_list slist;
334
335
    char descline[80];
335
 
    
 
336
 
336
337
    if (list->head == NULL) {
337
 
        tx_errbox("No descriptions", ANYKEY_MSG, &resp);
338
 
        return;
 
338
        tx_errbox("No descriptions", ANYKEY_MSG, &resp);
 
339
        return;
339
340
    }
340
341
 
341
342
    *node = list->head;
342
343
    tx_init_listbox(&slist, COLS, 20, 0, (LINES - 20) / 2,
343
 
        STDATTR, BOXATTR, BARSTDATTR, HIGHATTR);
344
 
    
 
344
                    STDATTR, BOXATTR, BARSTDATTR, HIGHATTR);
 
345
 
345
346
    tx_set_listbox_title(&slist, "Address", 1);
346
347
    tx_set_listbox_title(&slist, "Description", 19);
347
 
        
 
348
 
348
349
    while (*node != NULL) {
349
 
        snprintf(descline, 80, "%-18s%s", (*node)->rec.address, (*node)->rec.desc);    
 
350
        snprintf(descline, 80, "%-18s%s", (*node)->rec.address,
 
351
                 (*node)->rec.desc);
350
352
        tx_add_list_entry(&slist, (char *) (*node), descline);
351
353
        (*node) = (*node)->next_entry;
352
354
    }
353
 
    
 
355
 
354
356
    tx_show_listbox(&slist);
355
357
    tx_operate_listbox(&slist, &resp, aborted);
356
358
 
358
360
        *node = (struct desclistent *) slist.textptr->nodeptr;
359
361
 
360
362
    tx_close_listbox(&slist);
361
 
    tx_destroy_list(&slist);    
 
363
    tx_destroy_list(&slist);
362
364
 
363
365
    update_panels();
364
366
    doupdate();
365
367
}
366
368
 
367
369
void descdlg(struct descrec *rec, char *initaddr, char *initdesc,
368
 
             int *aborted)
 
370
             int *aborted)
369
371
{
370
372
    WINDOW *win;
371
373
    PANEL *panel;
376
378
 
377
379
    wattrset(win, DLGBOXATTR);
378
380
    tx_colorwin(win);
379
 
    box(win, ACS_VLINE, ACS_HLINE);
 
381
    tx_box(win, ACS_VLINE, ACS_HLINE);
380
382
    wmove(win, 6, 2 * COLS / 80);
381
383
    tabkeyhelp(win);
382
384
    wmove(win, 6, 20 * COLS / 80);
387
389
    wprintw(win, "MAC Address:");
388
390
    wmove(win, 4, 2 * COLS / 80);
389
391
    wprintw(win, "Description:");
390
 
    
 
392
 
391
393
    tx_initfields(&fieldlist, 3, 52, 10, (COLS - 52) / 2 + 6 * COLS / 80,
392
 
        DLGTEXTATTR, FIELDATTR);
 
394
                  DLGTEXTATTR, FIELDATTR);
393
395
    tx_addfield(&fieldlist, 12, 0, 0, initaddr);
394
396
    tx_addfield(&fieldlist, 50, 2, 0, initdesc);
395
397
 
396
398
    tx_fillfields(&fieldlist, aborted);
397
399
 
398
400
    if (!(*aborted)) {
399
 
        strcpy(rec->address, fieldlist.list->buf);
400
 
        strcpy(rec->desc, fieldlist.list->nextfield->buf);
 
401
        strcpy(rec->address, fieldlist.list->buf);
 
402
        strcpy(rec->desc, fieldlist.list->nextfield->buf);
401
403
    }
402
404
    tx_destroyfields(&fieldlist);
403
405
    del_panel(panel);
413
415
    descdlg(&rec, "", "", &aborted);
414
416
 
415
417
    if (!aborted) {
416
 
        ptmp = malloc(sizeof(struct desclistent));
417
 
        if (list->head == NULL) {
418
 
            list->head = ptmp;
419
 
            ptmp->prev_entry = NULL;
420
 
        } else {
421
 
            ptmp->prev_entry = list->tail;
422
 
            list->tail->next_entry = ptmp;
423
 
        }
 
418
        ptmp = malloc(sizeof(struct desclistent));
 
419
        if (list->head == NULL) {
 
420
            list->head = ptmp;
 
421
            ptmp->prev_entry = NULL;
 
422
        } else {
 
423
            ptmp->prev_entry = list->tail;
 
424
            list->tail->next_entry = ptmp;
 
425
        }
424
426
 
425
 
        list->tail = ptmp;
426
 
        ptmp->next_entry = NULL;
427
 
        memcpy(&(ptmp->rec), &rec, sizeof(struct descrec));
 
427
        list->tail = ptmp;
 
428
        ptmp->next_entry = NULL;
 
429
        memcpy(&(ptmp->rec), &rec, sizeof(struct descrec));
428
430
    }
429
431
    update_panels();
430
432
    doupdate();
438
440
    selectdesc(list, &ptmp, &aborted);
439
441
 
440
442
    if (!aborted)
441
 
        descdlg(&(ptmp->rec), ptmp->rec.address, ptmp->rec.desc, &aborted);
 
443
        descdlg(&(ptmp->rec), ptmp->rec.address, ptmp->rec.desc, &aborted);
442
444
}
443
445
 
444
446
void delethdesc(struct desclist *list)
449
451
    selectdesc(list, &ptmp, &aborted);
450
452
 
451
453
    if (!aborted) {
452
 
        if (ptmp->prev_entry != NULL)
453
 
            ptmp->prev_entry->next_entry = ptmp->next_entry;
454
 
        else
455
 
            list->head = ptmp->next_entry;
456
 
 
457
 
        if (ptmp->next_entry != NULL)
458
 
            ptmp->next_entry->prev_entry = ptmp->prev_entry;
459
 
        else
460
 
            list->tail = ptmp->prev_entry;
461
 
 
462
 
        free(ptmp);
 
454
        if (ptmp->prev_entry != NULL)
 
455
            ptmp->prev_entry->next_entry = ptmp->next_entry;
 
456
        else
 
457
            list->head = ptmp->next_entry;
 
458
 
 
459
        if (ptmp->next_entry != NULL)
 
460
            ptmp->next_entry->prev_entry = ptmp->prev_entry;
 
461
        else
 
462
            list->tail = ptmp->prev_entry;
 
463
 
 
464
        free(ptmp);
463
465
    }
464
466
}
465
467
 
474
476
    loaddesclist(&list, linktype, WITHOUTETCETHERS);
475
477
 
476
478
    tx_initmenu(&menu, 7, 31, (LINES - 6) / 2, (COLS - 31) / 2,
477
 
        BOXATTR, STDATTR, HIGHATTR, BARSTDATTR, BARHIGHATTR, DESCATTR);
 
479
                BOXATTR, STDATTR, HIGHATTR, BARSTDATTR, BARHIGHATTR,
 
480
                DESCATTR);
478
481
    tx_additem(&menu, " ^A^dd description...",
479
 
            "Adds a description for a MAC address");
 
482
               "Adds a description for a MAC address");
480
483
    tx_additem(&menu, " ^E^dit description...",
481
 
            "Modifies an existing MAC address description");
 
484
               "Modifies an existing MAC address description");
482
485
    tx_additem(&menu, " ^D^elete description...",
483
 
            "Deletes an existing MAC address description");
 
486
               "Deletes an existing MAC address description");
484
487
    tx_additem(&menu, NULL, NULL);
485
488
    tx_additem(&menu, " E^x^it menu", "Returns to the main menu");
486
489
 
487
490
    do {
488
 
        tx_showmenu(&menu);
489
 
        tx_operatemenu(&menu, &row, &aborted);
 
491
        tx_showmenu(&menu);
 
492
        tx_operatemenu(&menu, &row, &aborted);
490
493
 
491
 
        switch (row) {
492
 
        case 1:
493
 
            addethdesc(&list);
494
 
            break;
495
 
        case 2:
496
 
            editethdesc(&list);
497
 
            break;
498
 
        case 3:
499
 
            delethdesc(&list);
500
 
            break;
501
 
        }
 
494
        switch (row) {
 
495
        case 1:
 
496
            addethdesc(&list);
 
497
            break;
 
498
        case 2:
 
499
            editethdesc(&list);
 
500
            break;
 
501
        case 3:
 
502
            delethdesc(&list);
 
503
            break;
 
504
        }
502
505
    } while (row != 5);
503
506
 
504
507
    tx_destroymenu(&menu);