1 /* For terms of usage/redistribution/modification see the LICENSE file */
2 /* For authors and contributors see the AUTHORS file */
6 fltmgr.c - filter list management routines
10 #include "iptraf-ng-compat.h"
12 #include "tui/input.h"
13 #include "tui/labels.h"
14 #include "tui/listbox.h"
15 #include "tui/menurt.h"
16 #include "tui/msgboxes.h"
17 #include "tui/winops.h"
26 void makestdfiltermenu(struct MENU
*menu
)
28 tx_initmenu(menu
, 9, 31, (LINES
- 8) / 2, (COLS
- 31) / 2 + 15, BOXATTR
,
29 STDATTR
, HIGHATTR
, BARSTDATTR
, BARHIGHATTR
, DESCATTR
);
30 tx_additem(menu
, " ^D^efine new filter...",
31 "Defines a new set of IP filter parameters");
32 tx_additem(menu
, " ^A^pply filter...", "Applies a defined filter");
33 tx_additem(menu
, " Detac^h^ filter",
34 "Removes the currently applied filter");
35 tx_additem(menu
, " ^E^dit filter...", "Modifies existing filter data");
36 tx_additem(menu
, " Dele^t^e filter...",
37 "Removes an IP filter from the filter list");
38 tx_additem(menu
, NULL
, NULL
);
39 tx_additem(menu
, " E^x^it menu", "Returns to the main menu");
44 * Generate a string representation of a number to be used as a name.
47 void genname(unsigned long n
, char *m
)
52 void listfileerr(int code
)
55 write_error("Error loading filter list file");
57 write_error("Error writing filter list file");
60 unsigned long int nametoaddr(char *ascname
, int *err
)
62 unsigned long int result
;
68 resolv_err
= inet_aton(ascname
, &inp
);
69 if (resolv_err
== 0) {
70 snprintf(imsg
, 44, "Resolving %s", ascname
);
73 he
= gethostbyname(ascname
);
75 bcopy((he
->h_addr_list
)[0], &result
, he
->h_length
);
77 write_error("Unable to resolve %s", ascname
);
88 int loadfilterlist(struct ffnode
**fltfile
)
93 struct ffnode
*ffiles
= NULL
;
95 struct ffnode
*tail
= NULL
;
96 struct ffnode
*insert_point
= NULL
; /* new node is inserted *above* this */
100 pfd
= open(OTHIPFLNAME
, O_RDONLY
);
108 ptemp
= xmalloc(sizeof(struct ffnode
));
109 br
= read(pfd
, &(ptemp
->ffe
), sizeof(struct filterfileent
));
112 if (ffiles
== NULL
) {
114 * Create single-node list should initial list pointer be empty
117 ffiles
->prev_entry
= ffiles
->next_entry
= NULL
;
121 * Find appropriate point for insertion into sorted list.
124 insert_point
= ffiles
;
125 while (insert_point
!= NULL
) {
127 (insert_point
->ffe
.desc
,
131 insert_point
->next_entry
;
137 * Insert new node depending on whether insert_point = top of list;
138 * middle of list; end of list.
141 if (insert_point
== NULL
) {
142 /* Case 1: end of list; if insert_point is NULL, we get it
143 out of the way first */
144 tail
->next_entry
= ptemp
;
145 ptemp
->prev_entry
= tail
;
147 ptemp
->next_entry
= NULL
;
148 } else if (insert_point
->prev_entry
== NULL
) {
149 /* Case 2: top of list */
150 insert_point
->prev_entry
= ptemp
;
152 ffiles
->prev_entry
= NULL
;
153 ffiles
->next_entry
= insert_point
;
154 insert_point
->prev_entry
= ffiles
;
156 /* Case 3: middle of list */
158 insert_point
->prev_entry
;
159 ptemp
->next_entry
= insert_point
;
160 insert_point
->prev_entry
->next_entry
=
162 insert_point
->prev_entry
= ptemp
;
182 void destroyfilterlist(struct ffnode
*fltlist
)
184 while (fltlist
!= NULL
) {
185 struct ffnode
*fftemp
= fltlist
->next_entry
;
192 void save_filterlist(struct ffnode
*fltlist
)
194 struct ffnode
*fltfile
;
195 struct ffnode
*ffntemp
;
199 fd
= open(OTHIPFLNAME
, O_WRONLY
| O_CREAT
| O_TRUNC
, S_IRUSR
| S_IWUSR
);
207 while (fltfile
!= NULL
) {
208 bw
= write(fd
, &(fltfile
->ffe
), sizeof(struct filterfileent
));
215 fltfile
= fltfile
->next_entry
;
222 void operate_select(struct ffnode
*ffiles
, struct ffnode
**item
, int *aborted
)
225 struct scroll_list list
;
227 tx_listkeyhelp(STDATTR
, HIGHATTR
);
233 tx_init_listbox(&list
, 60, 10, (COLS
- 60) / 2 - 2,
234 (LINES
- 10) / 2 - 2, STDATTR
, BOXATTR
, BARSTDATTR
,
237 tx_set_listbox_title(&list
, "Select Filter", 1);
239 while (pptr
!= NULL
) {
240 tx_add_list_entry(&list
, (char *) pptr
, pptr
->ffe
.desc
);
241 pptr
= pptr
->next_entry
;
244 tx_show_listbox(&list
);
245 tx_operate_listbox(&list
, aborted
);
248 *item
= (struct ffnode
*) list
.textptr
->nodeptr
;
250 tx_close_listbox(&list
);
251 tx_destroy_list(&list
);
254 void pickafilter(struct ffnode
*ffiles
, struct ffnode
**fltfile
, int *aborted
)
256 operate_select(ffiles
, fltfile
, aborted
);
262 char *pickfilterbyname(struct ffnode
*ffiles
, char *filtername
)
264 struct ffnode
*ftmp
= ffiles
;
265 static char filterfile
[160];
267 while (ftmp
!= NULL
) {
268 if (strcmp(ftmp
->ffe
.desc
, filtername
) == 0) {
269 strncpy(filterfile
, ftmp
->ffe
.filename
, 40);
273 ftmp
= ftmp
->next_entry
;
279 void selectfilter(struct filterfileent
*ffe
, int *aborted
)
281 struct ffnode
*fltfile
;
282 struct ffnode
*ffiles
;
284 if (loadfilterlist(&ffiles
)) {
287 destroyfilterlist(ffiles
);
290 pickafilter(ffiles
, &fltfile
, aborted
);
295 destroyfilterlist(ffiles
);
299 void get_filter_description(char *description
, int *aborted
, char *pre_edit
)
301 struct FIELDLIST descfield
;
306 dlgwintop
= (LINES
- 9) / 2;
307 dlgwin
= newwin(7, 42, dlgwintop
, (COLS
- 42) / 2 - 10);
308 dlgpanel
= new_panel(dlgwin
);
309 wattrset(dlgwin
, DLGBOXATTR
);
311 tx_box(dlgwin
, ACS_VLINE
, ACS_HLINE
);
312 wattrset(dlgwin
, DLGTEXTATTR
);
314 wprintw(dlgwin
, "Enter a description for this filter");
320 tx_initfields(&descfield
, 1, 35, dlgwintop
+ 3, (COLS
- 42) / 2 - 8,
321 DLGTEXTATTR
, FIELDATTR
);
322 tx_addfield(&descfield
, 33, 0, 0, pre_edit
);
325 tx_fillfields(&descfield
, aborted
);
327 if ((descfield
.list
->buf
[0] == '\0') && (!(*aborted
)))
328 tui_error(ANYKEY_MSG
,
329 "Enter an appropriate description for this filter");
331 } while ((descfield
.list
->buf
[0] == '\0') && (!(*aborted
)));
334 strcpy(description
, descfield
.list
->buf
);
336 tx_destroyfields(&descfield
);