2 * copyright (c) 2001-2011 Emil Mikulic.
4 * darkstat.c: signals, cmdline parsing, program body.
6 * You may use, modify and redistribute this file under the terms of the
7 * GNU General Public License version 2. (see COPYING.GPL)
39 # define INADDR_NONE (-1) /* Solaris */
42 /* --- Signal handling --- */
43 static volatile int running
= 1;
44 static void sig_shutdown(int signum _unused_
) { running
= 0; }
46 static volatile int reset_pending
= 0, export_pending
= 0;
47 static void sig_reset(int signum _unused_
)
53 static void sig_export(int signum _unused_
) { export_pending
= 1; }
55 /* --- Commandline parsing --- */
57 parsenum(const char *str
, unsigned long max
/* 0 for no max */)
63 n
= strtoul(str
, &end
, 10);
65 errx(1, "\"%s\" is not a valid number", str
);
67 errx(1, "\"%s\" is out of range", str
);
68 if ((max
!= 0) && (n
> max
))
69 errx(1, "\"%s\" is out of range (max %lu)", str
, max
);
73 const char *opt_interface
= NULL
;
74 static void cb_interface(const char *arg
) { opt_interface
= arg
; }
76 const char *opt_capfile
= NULL
;
77 static void cb_capfile(const char *arg
) { opt_capfile
= arg
; }
79 int opt_want_snaplen
= -1;
80 static void cb_snaplen(const char *arg
)
81 { opt_want_snaplen
= (int)parsenum(arg
, 0); }
83 int opt_want_pppoe
= 0;
84 static void cb_pppoe(const char *arg _unused_
) { opt_want_pppoe
= 1; }
86 int opt_want_syslog
= 0;
87 static void cb_syslog(const char *arg _unused_
) { opt_want_syslog
= 1; }
89 int opt_want_verbose
= 0;
90 static void cb_verbose(const char *arg _unused_
) { opt_want_verbose
= 1; }
92 int opt_want_daemonize
= 1;
93 static void cb_no_daemon(const char *arg _unused_
) { opt_want_daemonize
= 0; }
95 int opt_want_promisc
= 1;
96 static void cb_no_promisc(const char *arg _unused_
) { opt_want_promisc
= 0; }
99 static void cb_no_dns(const char *arg _unused_
) { opt_want_dns
= 0; }
101 int opt_want_macs
= 1;
102 static void cb_no_macs(const char *arg _unused_
) { opt_want_macs
= 0; }
104 int opt_want_lastseen
= 1;
105 static void cb_no_lastseen(const char *arg _unused_
) { opt_want_lastseen
= 0; }
107 unsigned short opt_bindport
= 667;
108 static void cb_port(const char *arg
)
109 { opt_bindport
= (unsigned short)parsenum(arg
, 65536); }
111 static void cb_bindaddr(const char *arg
) { http_add_bindaddr(arg
); }
113 const char *opt_filter
= NULL
;
114 static void cb_filter(const char *arg
) { opt_filter
= arg
; }
116 static void cb_local(const char *arg
) { acct_init_localnet(arg
); }
118 const char *opt_chroot_dir
= NULL
;
119 static void cb_chroot(const char *arg
) { opt_chroot_dir
= arg
; }
121 const char *opt_privdrop_user
= NULL
;
122 static void cb_user(const char *arg
) { opt_privdrop_user
= arg
; }
124 const char *daylog_fn
= NULL
;
125 static void cb_daylog(const char *arg
)
127 if (opt_chroot_dir
== NULL
)
128 errx(1, "the daylog file is relative to the chroot.\n"
129 "You must specify a --chroot dir before you can use --daylog.");
134 const char *import_fn
= NULL
;
135 static void cb_import(const char *arg
)
137 if (opt_chroot_dir
== NULL
)
138 errx(1, "the import file is relative to the chroot.\n"
139 "You must specify a --chroot dir before you can use --import.");
144 const char *export_fn
= NULL
;
145 static void cb_export(const char *arg
)
147 if ((opt_chroot_dir
== NULL
) && (opt_capfile
== NULL
))
148 errx(1, "the export file is relative to the chroot.\n"
149 "You must specify a --chroot dir before you can use --export.");
154 static const char *pid_fn
= NULL
;
155 static void cb_pidfile(const char *arg
)
157 if (opt_chroot_dir
== NULL
)
158 errx(1, "the pidfile is relative to the chroot.\n"
159 "You must specify a --chroot dir before you can use --pidfile.");
164 unsigned int opt_hosts_max
= 1000;
165 static void cb_hosts_max(const char *arg
)
166 { opt_hosts_max
= parsenum(arg
, 0); }
168 unsigned int opt_hosts_keep
= 500;
169 static void cb_hosts_keep(const char *arg
)
170 { opt_hosts_keep
= parsenum(arg
, 0); }
172 unsigned int opt_ports_max
= 200;
173 static void cb_ports_max(const char *arg
)
174 { opt_ports_max
= parsenum(arg
, 65536); }
176 unsigned int opt_ports_keep
= 30;
177 static void cb_ports_keep(const char *arg
)
178 { opt_ports_keep
= parsenum(arg
, 65536); }
180 unsigned int opt_highest_port
= 65535;
181 static void cb_highest_port(const char *arg
)
182 { opt_highest_port
= parsenum(arg
, 65535); }
184 int opt_wait_secs
= -1;
185 static void cb_wait_secs(const char *arg
)
186 { opt_wait_secs
= (int)parsenum(arg
, 0); }
188 int opt_want_hexdump
= 0;
189 static void cb_hexdump(const char *arg _unused_
) { opt_want_hexdump
= 1; }
194 const char *name
, *arg_name
; /* NULL arg_name means unary */
195 void (*callback
)(const char *arg
);
199 static struct cmdline_arg cmdline_args
[] = {
200 {"-i", "interface", cb_interface
, 0},
201 {"-r", "file", cb_capfile
, 0},
202 {"--snaplen", "bytes", cb_snaplen
, 0},
203 {"--pppoe", NULL
, cb_pppoe
, 0},
204 {"--syslog", NULL
, cb_syslog
, 0},
205 {"--verbose", NULL
, cb_verbose
, 0},
206 {"--no-daemon", NULL
, cb_no_daemon
, 0},
207 {"--no-promisc", NULL
, cb_no_promisc
, 0},
208 {"--no-dns", NULL
, cb_no_dns
, 0},
209 {"--no-macs", NULL
, cb_no_macs
, 0},
210 {"--no-lastseen", NULL
, cb_no_lastseen
, 0},
211 {"-p", "port", cb_port
, 0},
212 {"-b", "bindaddr", cb_bindaddr
, -1},
213 {"-f", "filter", cb_filter
, 0},
214 {"-l", "network/netmask", cb_local
, 0},
215 {"--chroot", "dir", cb_chroot
, 0},
216 {"--user", "username", cb_user
, 0},
217 {"--daylog", "filename", cb_daylog
, 0},
218 {"--import", "filename", cb_import
, 0},
219 {"--export", "filename", cb_export
, 0},
220 {"--pidfile", "filename", cb_pidfile
, 0},
221 {"--hosts-max", "count", cb_hosts_max
, 0},
222 {"--hosts-keep", "count", cb_hosts_keep
, 0},
223 {"--ports-max", "count", cb_ports_max
, 0},
224 {"--ports-keep", "count", cb_ports_keep
, 0},
225 {"--highest-port", "port", cb_highest_port
, 0},
226 {"--wait", "secs", cb_wait_secs
, 0},
227 {"--hexdump", NULL
, cb_hexdump
, 0},
228 {NULL
, NULL
, NULL
, 0}
235 for (i
=0; i
<width
; i
++) printf(" ");
239 * We autogenerate the usage statement from the cmdline_args data structure.
245 struct cmdline_arg
*arg
;
247 printf(PACKAGE_STRING
" (built with libpcap %d.%d)\n\n",
248 PCAP_VERSION_MAJOR
, PCAP_VERSION_MINOR
);
250 width
= printf("usage: darkstat ");
253 for (arg
= cmdline_args
; arg
->name
!= NULL
; arg
++) {
254 if (first
) first
= 0; else pad(width
);
255 printf("[ %s", arg
->name
);
256 if (arg
->arg_name
!= NULL
) printf(" %s", arg
->arg_name
);
260 "Please refer to the darkstat(8) manual page for further\n"
261 "documentation and usage examples.\n");
265 parse_sub_cmdline(const int argc
, char * const *argv
)
267 struct cmdline_arg
*arg
;
269 if (argc
== 0) return;
270 for (arg
= cmdline_args
; arg
->name
!= NULL
; arg
++)
271 if (strcmp(argv
[0], arg
->name
) == 0) {
272 if ((arg
->arg_name
!= NULL
) && (argc
== 1)) {
274 "error: argument \"%s\" requires parameter \"%s\"\n",
275 arg
->name
, arg
->arg_name
);
279 if (arg
->num_seen
> 0) {
281 "error: already specified argument \"%s\"\n",
287 if (arg
->num_seen
!= -1) /* accept more than one */
290 if (arg
->arg_name
== NULL
) {
292 parse_sub_cmdline(argc
-1, argv
+1);
294 arg
->callback(argv
[1]);
295 parse_sub_cmdline(argc
-2, argv
+2);
300 fprintf(stderr
, "error: illegal argument: \"%s\"\n", argv
[0]);
306 parse_cmdline(const int argc
, char * const *argv
)
309 /* Not enough args. */
314 parse_sub_cmdline(argc
, argv
);
316 /* start syslogging as early as possible */
317 if (opt_want_syslog
) openlog("darkstat", LOG_NDELAY
| LOG_PID
, LOG_DAEMON
);
319 /* some default values */
320 if (opt_chroot_dir
== NULL
) opt_chroot_dir
= CHROOT_DIR
;
321 if (opt_privdrop_user
== NULL
) opt_privdrop_user
= PRIVDROP_USER
;
323 /* sanity check args */
324 if ((opt_interface
== NULL
) && (opt_capfile
== NULL
))
325 errx(1, "must specify either interface (-i) or capture file (-r)");
327 if ((opt_interface
!= NULL
) && (opt_capfile
!= NULL
))
328 errx(1, "can't specify both interface (-i) and capture file (-r)");
330 if ((opt_hosts_max
!= 0) && (opt_hosts_keep
>= opt_hosts_max
)) {
331 opt_hosts_keep
= opt_hosts_max
/ 2;
332 warnx("reducing --hosts-keep to %u, to be under --hosts-max (%u)",
333 opt_hosts_keep
, opt_hosts_max
);
335 verbosef("max %u hosts, cutting down to %u when exceeded",
336 opt_hosts_max
, opt_hosts_keep
);
338 if ((opt_ports_max
!= 0) && (opt_ports_keep
>= opt_ports_max
)) {
339 opt_ports_keep
= opt_ports_max
/ 2;
340 warnx("reducing --ports-keep to %u, to be under --ports-max (%u)",
341 opt_ports_keep
, opt_ports_max
);
343 verbosef("max %u ports per host, cutting down to %u when exceeded",
344 opt_ports_max
, opt_ports_keep
);
346 if (opt_want_hexdump
&& !opt_want_verbose
) {
347 opt_want_verbose
= 1;
348 verbosef("--hexdump implies --verbose");
351 if (opt_want_hexdump
&& opt_want_daemonize
) {
352 opt_want_daemonize
= 0;
353 verbosef("--hexdump implies --no-daemon");
358 run_from_capfile(void)
362 cap_from_file(opt_capfile
, opt_filter
);
364 if (export_fn
!= NULL
) db_export(export_fn
);
367 verbosef("Total packets: %qu, bytes: %qu",
368 acct_total_packets
, acct_total_bytes
);
371 /* --- Program body --- */
373 main(int argc
, char **argv
)
376 parse_cmdline(argc
-1, argv
+1);
380 * This is very different from a regular run against a network
387 /* must verbosef() before first fork to init lock */
388 verbosef("starting up");
389 if (pid_fn
) pidfile_create(opt_chroot_dir
, pid_fn
, opt_privdrop_user
);
391 if (opt_want_daemonize
) {
392 verbosef("daemonizing to run in the background!");
394 verbosef("I am the main process");
396 if (pid_fn
) pidfile_write_close();
398 /* do this first as it forks - minimize memory use */
399 if (opt_want_dns
) dns_init(opt_privdrop_user
);
400 cap_init(opt_interface
, opt_filter
, opt_want_promisc
); /* needs root */
401 http_listen(opt_bindport
);
402 ncache_init(); /* must do before chroot() */
404 privdrop(opt_chroot_dir
, opt_privdrop_user
);
406 /* Don't need root privs for these: */
408 if (daylog_fn
!= NULL
) daylog_init(daylog_fn
);
411 if (import_fn
!= NULL
) db_import(import_fn
);
412 localip_init(opt_interface
);
414 if (signal(SIGTERM
, sig_shutdown
) == SIG_ERR
)
415 errx(1, "signal(SIGTERM) failed");
416 if (signal(SIGINT
, sig_shutdown
) == SIG_ERR
)
417 errx(1, "signal(SIGINT) failed");
418 if (signal(SIGUSR1
, sig_reset
) == SIG_ERR
)
419 errx(1, "signal(SIGUSR1) failed");
420 if (signal(SIGUSR2
, sig_export
) == SIG_ERR
)
421 errx(1, "signal(SIGUSR2) failed");
423 verbosef("entering main loop");
427 int select_ret
, max_fd
= -1, use_timeout
= 0;
428 struct timeval timeout
;
433 if (export_pending
) {
434 if (export_fn
!= NULL
)
435 db_export(export_fn
);
448 cap_fd_set(&rs
, &max_fd
, &timeout
, &use_timeout
);
449 http_fd_set(&rs
, &ws
, &max_fd
, &timeout
, &use_timeout
);
451 select_ret
= select(max_fd
+1, &rs
, &ws
, NULL
,
452 (use_timeout
) ? &timeout
: NULL
);
454 if ((select_ret
== 0) && (!use_timeout
))
455 errx(1, "select() erroneously timed out");
457 if (select_ret
== -1) {
471 verbosef("shutting down");
472 verbosef("pcap stats: %u packets received, %u packets dropped",
473 cap_pkts_recv
, cap_pkts_drop
);
477 if (export_fn
!= NULL
) db_export(export_fn
);
480 if (daylog_fn
!= NULL
) daylog_free();
482 if (pid_fn
) pidfile_unlink();
483 verbosef("shut down");
484 return (EXIT_SUCCESS
);
487 /* vim:set ts=3 sw=3 tw=78 expandtab: */