2 * copyright (c) 2001-2010 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)
24 #include <arpa/inet.h>
25 #include <sys/socket.h>
41 # define INADDR_NONE (-1) /* Solaris */
44 /* --- Signal handling --- */
45 static volatile int running
= 1;
46 static void sig_shutdown(int signum _unused_
) { running
= 0; }
48 static volatile int reset_pending
= 0, export_pending
= 0;
49 static void sig_reset(int signum _unused_
)
55 static void sig_export(int signum _unused_
) { export_pending
= 1; }
57 /* --- Commandline parsing --- */
59 parsenum(const char *str
, unsigned long max
/* 0 for no max */)
65 n
= strtoul(str
, &end
, 10);
67 errx(1, "\"%s\" is not a valid number", str
);
69 errx(1, "\"%s\" is out of range", str
);
70 if ((max
!= 0) && (n
> max
))
71 errx(1, "\"%s\" is out of range (max %lu)", str
, max
);
75 const char *interface
= NULL
;
76 static void cb_interface(const char *arg
) { interface
= arg
; }
78 const char *capfile
= NULL
;
79 static void cb_capfile(const char *arg
) { capfile
= arg
; }
81 int want_snaplen
= -1;
82 static void cb_snaplen(const char *arg
) { want_snaplen
= parsenum(arg
, 0); }
85 static void cb_pppoe(const char *arg _unused_
) { want_pppoe
= 1; }
87 static void cb_syslog(const char *arg _unused_
) { want_syslog
= 1; }
89 static void cb_verbose(const char *arg _unused_
) { want_verbose
= 1; }
91 int want_daemonize
= 1;
92 static void cb_no_daemon(const char *arg _unused_
) { want_daemonize
= 0; }
95 static void cb_no_promisc(const char *arg _unused_
) { want_promisc
= 0; }
98 static void cb_no_dns(const char *arg _unused_
) { want_dns
= 0; }
101 static void cb_no_macs(const char *arg _unused_
) { want_macs
= 0; }
103 int want_lastseen
= 1;
104 static void cb_no_lastseen(const char *arg _unused_
) { want_lastseen
= 0; }
106 unsigned short bindport
= 667;
107 static void cb_port(const char *arg
) { bindport
= parsenum(arg
, 65536); }
109 const char *bindaddr
= NULL
;
110 static void cb_bindaddr(const char *arg
)
112 struct addrinfo hints
, *ai
;
114 memset(&hints
, 0, sizeof(hints
));
115 hints
.ai_flags
= AI_PASSIVE
;
117 hints
.ai_flags
|= AI_ADDRCONFIG
;
119 hints
.ai_family
= AF_UNSPEC
;
120 hints
.ai_socktype
= SOCK_STREAM
;
122 if (getaddrinfo(arg
, NULL
, &hints
, &ai
))
123 errx(1, "malformed address \"%s\"", arg
);
129 const char *filter
= NULL
;
130 static void cb_filter(const char *arg
) { filter
= arg
; }
132 static void cb_local(const char *arg
) { acct_init_localnet(arg
); }
134 const char *chroot_dir
= NULL
;
135 static void cb_chroot(const char *arg
) { chroot_dir
= arg
; }
137 const char *privdrop_user
= NULL
;
138 static void cb_user(const char *arg
) { privdrop_user
= arg
; }
140 const char *daylog_fn
= NULL
;
141 static void cb_daylog(const char *arg
)
143 if (chroot_dir
== NULL
)
144 errx(1, "the daylog file is relative to the chroot.\n"
145 "You must specify a --chroot dir before you can use --daylog.");
150 const char *import_fn
= NULL
;
151 static void cb_import(const char *arg
)
153 if (chroot_dir
== NULL
)
154 errx(1, "the import file is relative to the chroot.\n"
155 "You must specify a --chroot dir before you can use --import.");
160 const char *export_fn
= NULL
;
161 static void cb_export(const char *arg
)
163 if ((chroot_dir
== NULL
) && (capfile
== NULL
))
164 errx(1, "the export file is relative to the chroot.\n"
165 "You must specify a --chroot dir before you can use --export.");
170 static const char *pid_fn
= NULL
;
171 static void cb_pidfile(const char *arg
)
173 if (chroot_dir
== NULL
)
174 errx(1, "the pidfile is relative to the chroot.\n"
175 "You must specify a --chroot dir before you can use --pidfile.");
180 unsigned int hosts_max
= 1000;
181 static void cb_hosts_max(const char *arg
)
182 { hosts_max
= parsenum(arg
, 0); }
184 unsigned int hosts_keep
= 500;
185 static void cb_hosts_keep(const char *arg
)
186 { hosts_keep
= parsenum(arg
, 0); }
188 unsigned int ports_max
= 200;
189 static void cb_ports_max(const char *arg
)
190 { ports_max
= parsenum(arg
, 65536); }
192 unsigned int ports_keep
= 30;
193 static void cb_ports_keep(const char *arg
)
194 { ports_keep
= parsenum(arg
, 65536); }
196 unsigned int highest_port
= 65535;
197 static void cb_highest_port(const char *arg
)
198 { highest_port
= parsenum(arg
, 65535); }
201 static void cb_wait_secs(const char *arg
)
202 { wait_secs
= parsenum(arg
, 0); }
204 int want_hexdump
= 0;
205 static void cb_hexdump(const char *arg _unused_
) { want_hexdump
= 1; }
210 const char *name
, *arg_name
; /* NULL arg_name means unary */
211 void (*callback
)(const char *arg
);
215 static struct cmdline_arg cmdline_args
[] = {
216 {"-i", "interface", cb_interface
, 0},
217 {"-r", "file", cb_capfile
, 0},
218 {"--snaplen", "bytes", cb_snaplen
, 0},
219 {"--pppoe", NULL
, cb_pppoe
, 0},
220 {"--syslog", NULL
, cb_syslog
, 0},
221 {"--verbose", NULL
, cb_verbose
, 0},
222 {"--no-daemon", NULL
, cb_no_daemon
, 0},
223 {"--no-promisc", NULL
, cb_no_promisc
, 0},
224 {"--no-dns", NULL
, cb_no_dns
, 0},
225 {"--no-macs", NULL
, cb_no_macs
, 0},
226 {"--no-lastseen", NULL
, cb_no_lastseen
, 0},
227 {"-p", "port", cb_port
, 0},
228 {"-b", "bindaddr", cb_bindaddr
, 0},
229 {"-f", "filter", cb_filter
, 0},
230 {"-l", "network/netmask", cb_local
, 0},
231 {"--chroot", "dir", cb_chroot
, 0},
232 {"--user", "username", cb_user
, 0},
233 {"--daylog", "filename", cb_daylog
, 0},
234 {"--import", "filename", cb_import
, 0},
235 {"--export", "filename", cb_export
, 0},
236 {"--pidfile", "filename", cb_pidfile
, 0},
237 {"--hosts-max", "count", cb_hosts_max
, 0},
238 {"--hosts-keep", "count", cb_hosts_keep
, 0},
239 {"--ports-max", "count", cb_ports_max
, 0},
240 {"--ports-keep", "count", cb_ports_keep
, 0},
241 {"--highest-port", "port", cb_highest_port
, 0},
242 {"--wait", "secs", cb_wait_secs
, 0},
243 {"--hexdump", NULL
, cb_hexdump
, 0},
244 {NULL
, NULL
, NULL
, 0}
251 for (i
=0; i
<width
; i
++) printf(" ");
255 * We autogenerate the usage statement from the cmdline_args data structure.
261 struct cmdline_arg
*arg
;
263 printf(PACKAGE_STRING
" (built with libpcap %d.%d)\n\n",
264 PCAP_VERSION_MAJOR
, PCAP_VERSION_MINOR
);
266 width
= printf("usage: darkstat ");
269 for (arg
= cmdline_args
; arg
->name
!= NULL
; arg
++) {
270 if (first
) first
= 0; else pad(width
);
271 printf("[ %s", arg
->name
);
272 if (arg
->arg_name
!= NULL
) printf(" %s", arg
->arg_name
);
276 "Please refer to the darkstat(8) manual page for further\n"
277 "documentation and usage examples.\n");
281 parse_sub_cmdline(const int argc
, char * const *argv
)
283 struct cmdline_arg
*arg
;
285 if (argc
== 0) return;
286 for (arg
= cmdline_args
; arg
->name
!= NULL
; arg
++)
287 if (strcmp(argv
[0], arg
->name
) == 0) {
288 if ((arg
->arg_name
!= NULL
) && (argc
== 1)) {
290 "error: argument \"%s\" requires parameter \"%s\"\n",
291 arg
->name
, arg
->arg_name
);
295 if (arg
->num_seen
> 0) {
297 "error: already specified argument \"%s\"\n",
304 if (arg
->arg_name
== NULL
) {
306 parse_sub_cmdline(argc
-1, argv
+1);
308 arg
->callback(argv
[1]);
309 parse_sub_cmdline(argc
-2, argv
+2);
314 fprintf(stderr
, "error: illegal argument: \"%s\"\n", argv
[0]);
320 parse_cmdline(const int argc
, char * const *argv
)
323 /* Not enough args. */
328 parse_sub_cmdline(argc
, argv
);
330 /* start syslogging as early as possible */
331 if (want_syslog
) openlog("darkstat", LOG_NDELAY
| LOG_PID
, LOG_DAEMON
);
333 /* some default values */
334 if (chroot_dir
== NULL
) chroot_dir
= CHROOT_DIR
;
335 if (privdrop_user
== NULL
) privdrop_user
= PRIVDROP_USER
;
337 /* sanity check args */
338 if ((interface
== NULL
) && (capfile
== NULL
))
339 errx(1, "must specify either interface (-i) or capture file (-r)");
341 if ((interface
!= NULL
) && (capfile
!= NULL
))
342 errx(1, "can't specify both interface (-i) and capture file (-r)");
344 if ((hosts_max
!= 0) && (hosts_keep
>= hosts_max
)) {
345 hosts_keep
= hosts_max
/ 2;
346 warnx("reducing --hosts-keep to %u, to be under --hosts-max (%u)",
347 hosts_keep
, hosts_max
);
349 verbosef("max %u hosts, cutting down to %u when exceeded",
350 hosts_max
, hosts_keep
);
352 if ((ports_max
!= 0) && (ports_keep
>= ports_max
)) {
353 ports_keep
= ports_max
/ 2;
354 warnx("reducing --ports-keep to %u, to be under --ports-max (%u)",
355 ports_keep
, ports_max
);
357 verbosef("max %u ports per host, cutting down to %u when exceeded",
358 ports_max
, ports_keep
);
360 if (want_hexdump
&& !want_verbose
) {
362 verbosef("--hexdump implies --verbose");
365 if (want_hexdump
&& want_daemonize
) {
367 verbosef("--hexdump implies --no-daemon");
372 run_from_capfile(void)
376 cap_from_file(capfile
, filter
);
378 if (export_fn
!= NULL
) db_export(export_fn
);
381 verbosef("Total packets: %qu, bytes: %qu", total_packets
, total_bytes
);
384 /* --- Program body --- */
386 main(int argc
, char **argv
)
389 parse_cmdline(argc
-1, argv
+1);
393 * This is very different from a regular run against a network
400 /* must verbosef() before first fork to init lock */
401 verbosef("starting up");
402 if (pid_fn
) pidfile_create(chroot_dir
, pid_fn
, privdrop_user
);
404 if (want_daemonize
) {
405 verbosef("daemonizing to run in the background!");
407 verbosef("I am the main process");
409 if (pid_fn
) pidfile_write_close();
411 /* do this first as it forks - minimize memory use */
412 if (want_dns
) dns_init(privdrop_user
);
413 cap_init(interface
, filter
, want_promisc
); /* needs root */
414 http_init(bindaddr
, bindport
, /*maxconn=*/ -1); /* low ports need root */
415 ncache_init(); /* must do before chroot() */
417 privdrop(chroot_dir
, privdrop_user
);
419 /* Don't need root privs for these: */
421 if (daylog_fn
!= NULL
) daylog_init(daylog_fn
);
424 if (import_fn
!= NULL
) db_import(import_fn
);
425 localip_init(interface
);
427 if (signal(SIGTERM
, sig_shutdown
) == SIG_ERR
)
428 errx(1, "signal(SIGTERM) failed");
429 if (signal(SIGINT
, sig_shutdown
) == SIG_ERR
)
430 errx(1, "signal(SIGINT) failed");
431 if (signal(SIGUSR1
, sig_reset
) == SIG_ERR
)
432 errx(1, "signal(SIGUSR1) failed");
433 if (signal(SIGUSR2
, sig_export
) == SIG_ERR
)
434 errx(1, "signal(SIGUSR2) failed");
436 verbosef("entering main loop");
440 int select_ret
, max_fd
= -1, use_timeout
= 0;
441 struct timeval timeout
;
446 if (export_pending
) {
447 if (export_fn
!= NULL
)
448 db_export(export_fn
);
461 cap_fd_set(&rs
, &max_fd
, &timeout
, &use_timeout
);
462 http_fd_set(&rs
, &ws
, &max_fd
, &timeout
, &use_timeout
);
464 select_ret
= select(max_fd
+1, &rs
, &ws
, NULL
,
465 (use_timeout
) ? &timeout
: NULL
);
467 if ((select_ret
== 0) && (!use_timeout
))
468 errx(1, "select() erroneously timed out");
470 if (select_ret
== -1) {
484 verbosef("shutting down");
485 verbosef("pcap stats: %u packets received, %u packets dropped",
486 pkts_recv
, pkts_drop
);
489 if (export_fn
!= NULL
) db_export(export_fn
);
492 if (daylog_fn
!= NULL
) daylog_free();
494 if (pid_fn
) pidfile_unlink();
495 verbosef("shut down");
496 return (EXIT_SUCCESS
);
499 /* vim:set ts=3 sw=3 tw=78 expandtab: */