1 /* For terms of usage/redistribution/modification see the LICENSE file */
2 /* For authors and contributors see the AUTHORS file */
6 options.c - implements the configuration section of the utility
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"
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
,
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
)
92 static void indicatesetting(int row
, WINDOW
*win
)
95 wattrset(win
, HIGHATTR
);
99 printoptonoff(options
.revlook
, win
);
102 printoptonoff(options
.servnames
, win
);
105 printoptonoff(options
.promisc
, win
);
108 printoptonoff(options
.color
, win
);
111 printoptonoff(options
.logging
, win
);
115 if (options
.actmode
== KBITS
)
116 wprintw(win
, " kbits/s");
118 wprintw(win
, "kbytes/s");
121 printoptonoff(options
.mac
, win
);
124 printoptonoff(options
.v6inv4asv6
, win
);
129 void saveoptions(void)
134 fd
= open(CONFIGFILE
, O_CREAT
| O_TRUNC
| O_WRONLY
, S_IRUSR
| S_IWUSR
);
137 tui_error(ANYKEY_MSG
, "Cannot create config file: %s %s",
138 CONFIGFILE
, strerror(errno
));
141 bw
= write(fd
, &options
, sizeof(struct OPTIONS
));
144 tui_error(ANYKEY_MSG
, "Unable to write config file");
149 static void setdefaultopts(void)
153 options
.servnames
= 0;
156 options
.actmode
= KBITS
;
158 options
.timeout
= 15;
159 options
.logspan
= 3600;
161 options
.closedint
= 0;
162 options
.v6inv4asv6
= 1;
165 void loadoptions(void)
170 fd
= open(CONFIGFILE
, O_RDONLY
);
175 read(fd
, &options
, sizeof(struct OPTIONS
));
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
)
193 for (i
= 1; i
<= 8; i
++)
194 indicatesetting(i
, win
);
199 static void settimeout(time_t *value
, const char *units
, int allow_zero
,
204 struct FIELDLIST field
;
207 dlgwin
= newwin(7, 40, (LINES
- 7) / 2, (COLS
- 40) / 4);
208 dlgpanel
= new_panel(dlgwin
);
210 wattrset(dlgwin
, DLGBOXATTR
);
212 tx_box(dlgwin
, ACS_VLINE
, ACS_HLINE
);
214 wattrset(dlgwin
, DLGTEXTATTR
);
215 mvwprintw(dlgwin
, 2, 2, "Enter value in %s", units
);
219 tx_initfields(&field
, 1, 10, (LINES
- 7) / 2 + 3, (COLS
- 40) / 4 + 2,
220 DLGTEXTATTR
, FIELDATTR
);
221 tx_addfield(&field
, 3, 0, 0, "");
224 tx_fillfields(&field
, aborted
);
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");
236 } while (((!allow_zero
) && (tmval
== 0)) && (!(*aborted
)));
244 tx_destroyfields(&field
);
249 void setoptions(void)
252 int trow
= 1; /* row for timer submenu */
256 struct MENU timermenu
;
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
);
293 tx_operatemenu(&menu
, &row
, &aborted
);
297 options
.revlook
= ~options
.revlook
;
300 options
.servnames
= ~options
.servnames
;
303 options
.promisc
= ~options
.promisc
;
306 options
.color
= ~options
.color
;
309 options
.logging
= ~options
.logging
;
312 options
.actmode
= ~options
.actmode
;
315 options
.mac
= ~options
.mac
;
318 options
.v6inv4asv6
= ~options
.v6inv4asv6
;
321 maketimermenu(&timermenu
);
324 tx_showmenu(&timermenu
);
325 tx_operatemenu(&timermenu
, &trow
, &aborted
);
329 settimeout(&options
.timeout
,
330 "minutes", DONT_ALLOW_ZERO
,
333 updatetimes(statwin
);
336 settimeout(&options
.logspan
,
337 "minutes", DONT_ALLOW_ZERO
,
341 options
.logspan
* 60;
342 updatetimes(statwin
);
346 settimeout(&options
.updrate
, "seconds",
347 ALLOW_ZERO
, &aborted
);
349 updatetimes(statwin
);
352 settimeout(&options
.closedint
,
353 "minutes", ALLOW_ZERO
,
356 updatetimes(statwin
);
361 tx_destroymenu(&timermenu
);
366 addmoreports(&ports
);
372 manage_eth_desc(ARPHRD_ETHER
);
375 manage_eth_desc(ARPHRD_FDDI
);
379 indicatesetting(row
, statwin
);
382 destroyporttab(ports
);
383 tx_destroymenu(&menu
);
384 del_panel(statpanel
);