capt_get_packet(): check for key press only every 20ms
[iptraf-ng.git] / src / options.c
blobc9b1ed1306dd9316b9feb68bd6dc13a570438589
1 /* For terms of usage/redistribution/modification see the LICENSE file */
2 /* For authors and contributors see the AUTHORS file */
4 /***
6 options.c - implements the configuration section of the utility
8 ***/
10 #include "iptraf-ng-compat.h"
12 #include "tui/input.h"
13 #include "tui/menurt.h"
14 #include "tui/msgboxes.h"
15 #include "tui/winops.h"
17 #include "serv.h"
18 #include "options.h"
19 #include "deskman.h"
20 #include "attrs.h"
21 #include "landesc.h"
22 #include "promisc.h"
23 #include "dirs.h"
25 #define ALLOW_ZERO 1
26 #define DONT_ALLOW_ZERO 0
28 struct OPTIONS options;
30 static void makeoptionmenu(struct MENU *menu)
32 tx_initmenu(menu, 20, 40, (LINES - 19) / 2 - 1, (COLS - 40) / 16,
33 BOXATTR, STDATTR, HIGHATTR, BARSTDATTR, BARHIGHATTR,
34 DESCATTR);
35 tx_additem(menu, " ^R^everse DNS lookups",
36 "Toggles resolution of IP addresses into host names");
37 tx_additem(menu, " TCP/UDP ^s^ervice names",
38 "Displays TCP/UDP service names instead of numeric ports");
39 tx_additem(menu, " Force ^p^romiscuous mode",
40 "Toggles capture of all packets by LAN interfaces");
41 tx_additem(menu, " ^C^olor",
42 "Turns color on or off (restart IPTraf to effect change)");
43 tx_additem(menu, " ^L^ogging",
44 "Toggles logging of traffic to a data file");
45 tx_additem(menu, " Acti^v^ity mode",
46 "Toggles activity indicators between kbits/s and kbytes/s");
47 tx_additem(menu, " Source ^M^AC addrs in traffic monitor",
48 "Toggles display of source MAC addresses in the IP Traffic Monitor");
49 tx_additem(menu, " ^S^how v6-in-v4 traffic as IPv6",
50 "Toggled display of IPv6 tunnel in IPv4 as IPv6 traffic");
51 tx_additem(menu, NULL, NULL);
52 tx_additem(menu, " ^T^imers...", "Configures timeouts and intervals");
53 tx_additem(menu, NULL, NULL);
54 tx_additem(menu, " ^A^dditional ports...",
55 "Allows you to add port numbers higher than 1023 for the service stats");
56 tx_additem(menu, " ^D^elete port/range...",
57 "Deletes a port or range of ports earlier added");
58 tx_additem(menu, NULL, NULL);
59 tx_additem(menu, " ^E^thernet/PLIP host descriptions...",
60 "Manages descriptions for Ethernet and PLIP addresses");
61 tx_additem(menu, " ^F^DDI/Token Ring host descriptions...",
62 "Manages descriptions for FDDI and FDDI addresses");
63 tx_additem(menu, NULL, NULL);
64 tx_additem(menu, " E^x^it configuration", "Returns to main menu");
67 static void maketimermenu(struct MENU *menu)
69 tx_initmenu(menu, 8, 35, (LINES - 19) / 2 + 7, (COLS - 35) / 2, BOXATTR,
70 STDATTR, HIGHATTR, BARSTDATTR, BARHIGHATTR, DESCATTR);
72 tx_additem(menu, " TCP ^t^imeout...",
73 "Sets the length of time before inactive TCP entries are considered idle");
74 tx_additem(menu, " ^L^ogging interval...",
75 "Sets the time between loggings for interface, host, and service stats");
76 tx_additem(menu, " ^S^creen update interval...",
77 "Sets the screen update interval in seconds (set to 0 for fastest updates)");
78 tx_additem(menu, " TCP closed/idle ^p^ersistence...",
79 "Determines how long closed/idle/reset entries stay onscreen");
80 tx_additem(menu, NULL, NULL);
81 tx_additem(menu, " E^x^it menu", "Returns to the configuration menu");
84 static void printoptonoff(unsigned int option, WINDOW * win)
86 if (option)
87 wprintw(win, " On");
88 else
89 wprintw(win, "Off");
92 static void indicatesetting(int row, WINDOW *win)
94 wmove(win, row, 30);
95 wattrset(win, HIGHATTR);
97 switch (row) {
98 case 1:
99 printoptonoff(options.revlook, win);
100 break;
101 case 2:
102 printoptonoff(options.servnames, win);
103 break;
104 case 3:
105 printoptonoff(options.promisc, win);
106 break;
107 case 4:
108 printoptonoff(options.color, win);
109 break;
110 case 5:
111 printoptonoff(options.logging, win);
112 break;
113 case 6:
114 wmove(win, row, 25);
115 if (options.actmode == KBITS)
116 wprintw(win, " kbits/s");
117 else
118 wprintw(win, "kbytes/s");
119 break;
120 case 7:
121 printoptonoff(options.mac, win);
122 break;
123 case 8:
124 printoptonoff(options.v6inv4asv6, win);
129 void saveoptions(void)
131 int fd;
132 int bw;
134 fd = open(CONFIGFILE, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR);
136 if (fd < 0) {
137 tui_error(ANYKEY_MSG, "Cannot create config file: %s %s",
138 CONFIGFILE, strerror(errno));
139 return;
141 bw = write(fd, &options, sizeof(struct OPTIONS));
143 if (bw < 0)
144 tui_error(ANYKEY_MSG, "Unable to write config file");
146 close(fd);
149 static void setdefaultopts(void)
151 options.revlook = 0;
152 options.promisc = 0;
153 options.servnames = 0;
154 options.color = 1;
155 options.logging = 0;
156 options.actmode = KBITS;
157 options.mac = 0;
158 options.timeout = 15;
159 options.logspan = 3600;
160 options.updrate = 0;
161 options.closedint = 0;
162 options.v6inv4asv6 = 1;
165 void loadoptions(void)
167 int fd;
169 setdefaultopts();
170 fd = open(CONFIGFILE, O_RDONLY);
172 if (fd < 0)
173 return;
175 read(fd, &options, sizeof(struct OPTIONS));
177 close(fd);
180 static void updatetimes(WINDOW *win)
182 wattrset(win, HIGHATTR);
183 mvwprintw(win, 10, 25, "%3u mins", options.timeout);
184 mvwprintw(win, 11, 25, "%3u mins", options.logspan / 60);
185 mvwprintw(win, 12, 25, "%3u secs", options.updrate);
186 mvwprintw(win, 13, 25, "%3u mins", options.closedint);
189 static void showoptions(WINDOW *win)
191 int i;
193 for (i = 1; i <= 8; i++)
194 indicatesetting(i, win);
196 updatetimes(win);
199 static void settimeout(time_t *value, const char *units, int allow_zero,
200 int *aborted)
202 WINDOW *dlgwin;
203 PANEL *dlgpanel;
204 struct FIELDLIST field;
205 time_t tmval = 0;
207 dlgwin = newwin(7, 40, (LINES - 7) / 2, (COLS - 40) / 4);
208 dlgpanel = new_panel(dlgwin);
210 wattrset(dlgwin, DLGBOXATTR);
211 tx_colorwin(dlgwin);
212 tx_box(dlgwin, ACS_VLINE, ACS_HLINE);
214 wattrset(dlgwin, DLGTEXTATTR);
215 mvwprintw(dlgwin, 2, 2, "Enter value in %s", units);
216 wmove(dlgwin, 5, 2);
217 stdkeyhelp(dlgwin);
219 tx_initfields(&field, 1, 10, (LINES - 7) / 2 + 3, (COLS - 40) / 4 + 2,
220 DLGTEXTATTR, FIELDATTR);
221 tx_addfield(&field, 3, 0, 0, "");
223 do {
224 tx_fillfields(&field, aborted);
226 if (!(*aborted)) {
227 unsigned int tm;
229 tmval = 0;
230 int ret = strtoul_ui(field.list->buf, 10, &tm);
231 if ((ret == -1) || (!allow_zero && (tm == 0)))
232 tui_error(ANYKEY_MSG, "Invalid timeout value");
233 else
234 tmval = tm;
236 } while (((!allow_zero) && (tmval == 0)) && (!(*aborted)));
238 if (!(*aborted))
239 *value = tmval;
241 del_panel(dlgpanel);
242 delwin(dlgwin);
244 tx_destroyfields(&field);
245 update_panels();
246 doupdate();
249 void setoptions(void)
251 int row = 1;
252 int trow = 1; /* row for timer submenu */
253 int aborted;
255 struct MENU menu;
256 struct MENU timermenu;
258 WINDOW *statwin;
259 PANEL *statpanel;
261 struct porttab *ports;
263 loadaddports(&ports);
265 makeoptionmenu(&menu);
267 statwin = newwin(15, 35, (LINES - 19) / 2 - 1, (COLS - 40) / 16 + 40);
268 statpanel = new_panel(statwin);
270 wattrset(statwin, BOXATTR);
271 tx_colorwin(statwin);
272 tx_box(statwin, ACS_VLINE, ACS_HLINE);
273 wmove(statwin, 9, 1);
274 whline(statwin, ACS_HLINE, 33);
275 mvwprintw(statwin, 0, 1, " Current Settings ");
276 wattrset(statwin, STDATTR);
277 mvwprintw(statwin, 1, 2, "Reverse DNS lookups:");
278 mvwprintw(statwin, 2, 2, "Service names:");
279 mvwprintw(statwin, 3, 2, "Promiscuous:");
280 mvwprintw(statwin, 4, 2, "Color:");
281 mvwprintw(statwin, 5, 2, "Logging:");
282 mvwprintw(statwin, 6, 2, "Activity mode:");
283 mvwprintw(statwin, 7, 2, "MAC addresses:");
284 mvwprintw(statwin, 8, 2, "v6-in-v4 as IPv6:");
285 mvwprintw(statwin, 10, 2, "TCP timeout:");
286 mvwprintw(statwin, 11, 2, "Log interval:");
287 mvwprintw(statwin, 12, 2, "Update interval:");
288 mvwprintw(statwin, 13, 2, "Closed/idle persist:");
289 showoptions(statwin);
291 do {
292 tx_showmenu(&menu);
293 tx_operatemenu(&menu, &row, &aborted);
295 switch (row) {
296 case 1:
297 options.revlook = ~options.revlook;
298 break;
299 case 2:
300 options.servnames = ~options.servnames;
301 break;
302 case 3:
303 options.promisc = ~options.promisc;
304 break;
305 case 4:
306 options.color = ~options.color;
307 break;
308 case 5:
309 options.logging = ~options.logging;
310 break;
311 case 6:
312 options.actmode = ~options.actmode;
313 break;
314 case 7:
315 options.mac = ~options.mac;
316 break;
317 case 8:
318 options.v6inv4asv6 = ~options.v6inv4asv6;
319 break;
320 case 10:
321 maketimermenu(&timermenu);
322 trow = 1;
323 do {
324 tx_showmenu(&timermenu);
325 tx_operatemenu(&timermenu, &trow, &aborted);
327 switch (trow) {
328 case 1:
329 settimeout(&options.timeout,
330 "minutes", DONT_ALLOW_ZERO,
331 &aborted);
332 if (!aborted)
333 updatetimes(statwin);
334 break;
335 case 2:
336 settimeout(&options.logspan,
337 "minutes", DONT_ALLOW_ZERO,
338 &aborted);
339 if (!aborted) {
340 options.logspan =
341 options.logspan * 60;
342 updatetimes(statwin);
344 break;
345 case 3:
346 settimeout(&options.updrate, "seconds",
347 ALLOW_ZERO, &aborted);
348 if (!aborted)
349 updatetimes(statwin);
350 break;
351 case 4:
352 settimeout(&options.closedint,
353 "minutes", ALLOW_ZERO,
354 &aborted);
355 if (!aborted)
356 updatetimes(statwin);
357 break;
359 } while (trow != 6);
361 tx_destroymenu(&timermenu);
362 update_panels();
363 doupdate();
364 break;
365 case 12:
366 addmoreports(&ports);
367 break;
368 case 13:
369 removeaport(&ports);
370 break;
371 case 15:
372 manage_eth_desc(ARPHRD_ETHER);
373 break;
374 case 16:
375 manage_eth_desc(ARPHRD_FDDI);
376 break;
379 indicatesetting(row, statwin);
380 } while (row != 18);
382 destroyporttab(ports);
383 tx_destroymenu(&menu);
384 del_panel(statpanel);
385 delwin(statwin);
386 update_panels();
387 doupdate();