capt_get_packet(): check for key press only every 20ms
[iptraf-ng.git] / src / fltmgr.c
blobe7c31e75cc555f2cf5f9cf6ea208123ff628a277
1 /* For terms of usage/redistribution/modification see the LICENSE file */
2 /* For authors and contributors see the AUTHORS file */
4 /***
6 fltmgr.c - filter list management routines
8 ***/
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"
19 #include "attrs.h"
20 #include "deskman.h"
21 #include "dirs.h"
22 #include "fltdefs.h"
23 #include "fltmgr.h"
24 #include "error.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)
49 sprintf(m, "%lu", n);
52 void listfileerr(int code)
54 if (code == 1)
55 write_error("Error loading filter list file");
56 else
57 write_error("Error writing filter list file");
60 unsigned long int nametoaddr(char *ascname, int *err)
62 unsigned long int result;
63 struct hostent *he;
64 char imsg[45];
65 struct in_addr inp;
66 int resolv_err = 0;
68 resolv_err = inet_aton(ascname, &inp);
69 if (resolv_err == 0) {
70 snprintf(imsg, 44, "Resolving %s", ascname);
71 indicate(imsg);
73 he = gethostbyname(ascname);
74 if (he != NULL)
75 bcopy((he->h_addr_list)[0], &result, he->h_length);
76 else {
77 write_error("Unable to resolve %s", ascname);
78 *err = 1;
79 return (-1);
81 } else
82 result = inp.s_addr;
84 return (result);
85 *err = 0;
88 int loadfilterlist(struct ffnode **fltfile)
90 int pfd = 0;
91 int result = 0;
93 struct ffnode *ffiles = NULL;
94 struct ffnode *ptemp;
95 struct ffnode *tail = NULL;
96 struct ffnode *insert_point = NULL; /* new node is inserted *above* this */
98 int br;
100 pfd = open(OTHIPFLNAME, O_RDONLY);
102 if (pfd < 0) {
103 *fltfile = NULL;
104 return 1;
107 do {
108 ptemp = xmalloc(sizeof(struct ffnode));
109 br = read(pfd, &(ptemp->ffe), sizeof(struct filterfileent));
111 if (br > 0) {
112 if (ffiles == NULL) {
114 * Create single-node list should initial list pointer be empty
116 ffiles = ptemp;
117 ffiles->prev_entry = ffiles->next_entry = NULL;
118 tail = ffiles;
119 } else {
121 * Find appropriate point for insertion into sorted list.
124 insert_point = ffiles;
125 while (insert_point != NULL) {
126 if (strcasecmp
127 (insert_point->ffe.desc,
128 ptemp->ffe.desc)
129 < 0)
130 insert_point =
131 insert_point->next_entry;
132 else
133 break;
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;
146 tail = ptemp;
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;
151 ffiles = ptemp;
152 ffiles->prev_entry = NULL;
153 ffiles->next_entry = insert_point;
154 insert_point->prev_entry = ffiles;
155 } else {
156 /* Case 3: middle of list */
157 ptemp->prev_entry =
158 insert_point->prev_entry;
159 ptemp->next_entry = insert_point;
160 insert_point->prev_entry->next_entry =
161 ptemp;
162 insert_point->prev_entry = ptemp;
165 } else {
166 free(ptemp);
168 if (br < 0)
169 result = 1;
171 } while (br > 0);
173 close(pfd);
174 *fltfile = ffiles;
176 if (ffiles == NULL)
177 result = 1;
179 return result;
182 void destroyfilterlist(struct ffnode *fltlist)
184 while (fltlist != NULL) {
185 struct ffnode *fftemp = fltlist->next_entry;
187 free(fltlist);
188 fltlist = fftemp;
192 void save_filterlist(struct ffnode *fltlist)
194 struct ffnode *fltfile;
195 struct ffnode *ffntemp;
196 int fd;
197 int bw;
199 fd = open(OTHIPFLNAME, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
201 if (fd < 0) {
202 listfileerr(2);
203 return;
206 fltfile = fltlist;
207 while (fltfile != NULL) {
208 bw = write(fd, &(fltfile->ffe), sizeof(struct filterfileent));
210 if (bw < 0) {
211 listfileerr(2);
212 return;
214 ffntemp = fltfile;
215 fltfile = fltfile->next_entry;
216 free(ffntemp);
219 close(fd);
222 void operate_select(struct ffnode *ffiles, struct ffnode **item, int *aborted)
224 struct ffnode *pptr;
225 struct scroll_list list;
227 tx_listkeyhelp(STDATTR, HIGHATTR);
228 update_panels();
229 doupdate();
231 pptr = ffiles;
233 tx_init_listbox(&list, 60, 10, (COLS - 60) / 2 - 2,
234 (LINES - 10) / 2 - 2, STDATTR, BOXATTR, BARSTDATTR,
235 HIGHATTR);
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);
247 if (!(*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);
258 update_panels();
259 doupdate();
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);
270 return filterfile;
273 ftmp = ftmp->next_entry;
276 return NULL;
279 void selectfilter(struct filterfileent *ffe, int *aborted)
281 struct ffnode *fltfile;
282 struct ffnode *ffiles;
284 if (loadfilterlist(&ffiles)) {
285 listfileerr(1);
286 *aborted = 1;
287 destroyfilterlist(ffiles);
288 return;
290 pickafilter(ffiles, &fltfile, aborted);
292 if (!(*aborted))
293 *ffe = fltfile->ffe;
295 destroyfilterlist(ffiles);
299 void get_filter_description(char *description, int *aborted, char *pre_edit)
301 struct FIELDLIST descfield;
302 int dlgwintop;
303 WINDOW *dlgwin;
304 PANEL *dlgpanel;
306 dlgwintop = (LINES - 9) / 2;
307 dlgwin = newwin(7, 42, dlgwintop, (COLS - 42) / 2 - 10);
308 dlgpanel = new_panel(dlgwin);
309 wattrset(dlgwin, DLGBOXATTR);
310 tx_colorwin(dlgwin);
311 tx_box(dlgwin, ACS_VLINE, ACS_HLINE);
312 wattrset(dlgwin, DLGTEXTATTR);
313 wmove(dlgwin, 2, 2);
314 wprintw(dlgwin, "Enter a description for this filter");
315 wmove(dlgwin, 5, 2);
316 stdkeyhelp(dlgwin);
317 update_panels();
318 doupdate();
320 tx_initfields(&descfield, 1, 35, dlgwintop + 3, (COLS - 42) / 2 - 8,
321 DLGTEXTATTR, FIELDATTR);
322 tx_addfield(&descfield, 33, 0, 0, pre_edit);
324 do {
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)));
333 if (!(*aborted))
334 strcpy(description, descfield.list->buf);
336 tx_destroyfields(&descfield);
337 del_panel(dlgpanel);
338 delwin(dlgwin);
339 update_panels();
340 doupdate();