X-Git-Url: https://unix4lyfe.org/gitweb/darkstat-debian/blobdiff_plain/a1e8056c92203d02860d719abb1d562453896da8..HEAD:/darkstat.c diff --git a/darkstat.c b/darkstat.c index 0f4b70d..47a81ad 100644 --- a/darkstat.c +++ b/darkstat.c @@ -1,5 +1,5 @@ /* darkstat 3 - * copyright (c) 2001-2011 Emil Mikulic. + * copyright (c) 2001-2014 Emil Mikulic. * * darkstat.c: signals, cmdline parsing, program body. * @@ -16,11 +16,13 @@ #include "db.h" #include "dns.h" #include "err.h" -#include "http.h" #include "hosts_db.h" +#include "http.h" #include "localip.h" #include "ncache.h" +#include "now.h" #include "pidfile.h" +#include "str.h" #include #include @@ -33,9 +35,6 @@ #include #include -#include "now.h" -time_t now; - #ifndef INADDR_NONE # define INADDR_NONE (-1) /* Solaris */ #endif @@ -45,8 +44,7 @@ static volatile int running = 1; static void sig_shutdown(int signum _unused_) { running = 0; } static volatile int reset_pending = 0, export_pending = 0; -static void sig_reset(int signum _unused_) -{ +static void sig_reset(int signum _unused_) { reset_pending = 1; export_pending = 1; } @@ -54,9 +52,8 @@ static void sig_reset(int signum _unused_) static void sig_export(int signum _unused_) { export_pending = 1; } /* --- Commandline parsing --- */ -static unsigned long -parsenum(const char *str, unsigned long max /* 0 for no max */) -{ +static unsigned long parsenum(const char *str, + unsigned long max /* 0 for no max */) { unsigned long n; char *end; @@ -71,8 +68,13 @@ parsenum(const char *str, unsigned long max /* 0 for no max */) return n; } -const char *opt_interface = NULL; -static void cb_interface(const char *arg) { opt_interface = arg; } +static int opt_iface_seen = 0; +static void cb_interface(const char *arg) { + cap_add_ifname(arg); + opt_iface_seen = 1; +} + +static void cb_filter(const char *arg) { cap_add_filter(arg); } const char *opt_capfile = NULL; static void cb_capfile(const char *arg) { opt_capfile = arg; } @@ -111,9 +113,6 @@ static void cb_port(const char *arg) static void cb_bindaddr(const char *arg) { http_add_bindaddr(arg); } -const char *opt_filter = NULL; -static void cb_filter(const char *arg) { opt_filter = arg; } - static int is_localnet_specified = 0; static void cb_local(const char *arg) { @@ -128,48 +127,23 @@ static void cb_local_only(const char *arg _unused_) const char *opt_chroot_dir = NULL; static void cb_chroot(const char *arg) { opt_chroot_dir = arg; } +const char *opt_base = NULL; +static void cb_base(const char *arg) { opt_base = arg; } + const char *opt_privdrop_user = NULL; static void cb_user(const char *arg) { opt_privdrop_user = arg; } -const char *daylog_fn = NULL; -static void cb_daylog(const char *arg) -{ - if (opt_chroot_dir == NULL) - errx(1, "the daylog file is relative to the chroot.\n" - "You must specify a --chroot dir before you can use --daylog."); - else - daylog_fn = arg; -} +const char *opt_daylog_fn = NULL; +static void cb_daylog(const char *arg) { opt_daylog_fn = arg; } const char *import_fn = NULL; -static void cb_import(const char *arg) -{ - if (opt_chroot_dir == NULL) - errx(1, "the import file is relative to the chroot.\n" - "You must specify a --chroot dir before you can use --import."); - else - import_fn = arg; -} +static void cb_import(const char *arg) { import_fn = arg; } const char *export_fn = NULL; -static void cb_export(const char *arg) -{ - if ((opt_chroot_dir == NULL) && (opt_capfile == NULL)) - errx(1, "the export file is relative to the chroot.\n" - "You must specify a --chroot dir before you can use --export."); - else - export_fn = arg; -} +static void cb_export(const char *arg) { export_fn = arg; } static const char *pid_fn = NULL; -static void cb_pidfile(const char *arg) -{ - if (opt_chroot_dir == NULL) - errx(1, "the pidfile is relative to the chroot.\n" - "You must specify a --chroot dir before you can use --pidfile."); - else - pid_fn = arg; -} +static void cb_pidfile(const char *arg) { pid_fn = arg; } unsigned int opt_hosts_max = 1000; static void cb_hosts_max(const char *arg) @@ -179,7 +153,7 @@ unsigned int opt_hosts_keep = 500; static void cb_hosts_keep(const char *arg) { opt_hosts_keep = parsenum(arg, 0); } -unsigned int opt_ports_max = 200; +unsigned int opt_ports_max = 60; static void cb_ports_max(const char *arg) { opt_ports_max = parsenum(arg, 65536); } @@ -214,12 +188,13 @@ struct cmdline_arg { }; static struct cmdline_arg cmdline_args[] = { - {"-i", "interface", cb_interface, 0}, - {"-r", "file", cb_capfile, 0}, + {"-i", "interface", cb_interface, -1}, + {"-f", "filter", cb_filter, -1}, + {"-r", "capfile", cb_capfile, 0}, {"-p", "port", cb_port, 0}, {"-b", "bindaddr", cb_bindaddr, -1}, - {"-f", "filter", cb_filter, 0}, {"-l", "network/netmask", cb_local, 0}, + {"--base", "path", cb_base, 0}, {"--local-only", NULL, cb_local_only, 0}, {"--snaplen", "bytes", cb_snaplen, 0}, {"--pppoe", NULL, cb_pppoe, 0}, @@ -248,12 +223,8 @@ static struct cmdline_arg cmdline_args[] = { {NULL, NULL, NULL, 0} }; -/* - * We autogenerate the usage statement from the cmdline_args data structure. - */ -static void -usage(void) -{ +/* We autogenerate the usage statement from the cmdline_args data structure. */ +static void usage(void) { static char intro[] = "usage: darkstat "; char indent[sizeof(intro)]; struct cmdline_arg *arg; @@ -278,9 +249,7 @@ usage(void) "documentation and usage examples.\n"); } -static void -parse_sub_cmdline(const int argc, char * const *argv) -{ +static void parse_sub_cmdline(const int argc, char * const *argv) { struct cmdline_arg *arg; if (argc == 0) return; @@ -319,9 +288,7 @@ parse_sub_cmdline(const int argc, char * const *argv) exit(EXIT_FAILURE); } -static void -parse_cmdline(const int argc, char * const *argv) -{ +static void parse_cmdline(const int argc, char * const *argv) { if (argc < 1) { /* Not enough args. */ usage(); @@ -336,17 +303,18 @@ parse_cmdline(const int argc, char * const *argv) } /* start syslogging as early as possible */ - if (opt_want_syslog) openlog("darkstat", LOG_NDELAY | LOG_PID, LOG_DAEMON); + if (opt_want_syslog) + openlog("darkstat", LOG_NDELAY | LOG_PID, LOG_DAEMON); - /* some default values */ - if (opt_chroot_dir == NULL) opt_chroot_dir = CHROOT_DIR; - if (opt_privdrop_user == NULL) opt_privdrop_user = PRIVDROP_USER; + /* default value */ + if (opt_privdrop_user == NULL) + opt_privdrop_user = PRIVDROP_USER; /* sanity check args */ - if ((opt_interface == NULL) && (opt_capfile == NULL)) + if (!opt_iface_seen && opt_capfile == NULL) errx(1, "must specify either interface (-i) or capture file (-r)"); - if ((opt_interface != NULL) && (opt_capfile != NULL)) + if (opt_iface_seen && opt_capfile != NULL) errx(1, "can't specify both interface (-i) and capture file (-r)"); if ((opt_hosts_max != 0) && (opt_hosts_keep >= opt_hosts_max)) { @@ -379,22 +347,17 @@ parse_cmdline(const int argc, char * const *argv) verbosef("WARNING: --local-only without -l only matches the local host"); } -static void -run_from_capfile(void) -{ +static void run_from_capfile(void) { + now_init(); graph_init(); hosts_db_init(); - cap_from_file(opt_capfile, opt_filter); - cap_stop(); + cap_from_file(opt_capfile); if (export_fn != NULL) db_export(export_fn); hosts_db_free(); graph_free(); -#ifndef PRIu64 -#warning "PRIu64 is not defined, using qu instead" -#define PRIu64 "qu" -#endif - verbosef("Total packets: %"PRIu64", bytes: %"PRIu64, - acct_total_packets, acct_total_bytes); + verbosef("Total packets: %llu, bytes: %llu", + (llu)acct_total_packets, + (llu)acct_total_bytes); } /* --- Program body --- */ @@ -405,10 +368,6 @@ main(int argc, char **argv) parse_cmdline(argc-1, argv+1); if (opt_capfile) { - /* - * This is very different from a regular run against a network - * interface. - */ run_from_capfile(); return 0; } @@ -426,19 +385,19 @@ main(int argc, char **argv) /* do this first as it forks - minimize memory use */ if (opt_want_dns) dns_init(opt_privdrop_user); - cap_init(opt_interface, opt_filter, opt_want_promisc); /* needs root */ + cap_start(opt_want_promisc); /* needs root */ + http_init_base(opt_base); http_listen(opt_bindport); ncache_init(); /* must do before chroot() */ privdrop(opt_chroot_dir, opt_privdrop_user); /* Don't need root privs for these: */ - now = time(NULL); - if (daylog_fn != NULL) daylog_init(daylog_fn); + now_init(); + if (opt_daylog_fn != NULL) daylog_init(opt_daylog_fn); graph_init(); hosts_db_init(); if (import_fn != NULL) db_import(import_fn); - localip_init(opt_interface); if (signal(SIGTERM, sig_shutdown) == SIG_ERR) errx(1, "signal(SIGTERM) failed"); @@ -453,11 +412,32 @@ main(int argc, char **argv) daemonize_finish(); while (running) { - int select_ret, max_fd = -1, use_timeout = 0; + int select_ret; + int max_fd = -1; + int use_timeout = 0; + int cap_ret; struct timeval timeout; + struct timespec t; fd_set rs, ws; - now = time(NULL); + FD_ZERO(&rs); + FD_ZERO(&ws); + cap_fd_set(&rs, &max_fd, &timeout, &use_timeout); + http_fd_set(&rs, &ws, &max_fd, &timeout, &use_timeout); + + select_ret = select(max_fd+1, &rs, &ws, NULL, + (use_timeout) ? &timeout : NULL); + if (select_ret == 0 && !use_timeout) + errx(1, "select() erroneously timed out"); + if (select_ret == -1) { + if (errno == EINTR) + continue; + else + err(1, "select()"); + } + + timer_start(&t); + now_update(); if (export_pending) { if (export_fn != NULL) @@ -473,29 +453,14 @@ main(int argc, char **argv) reset_pending = 0; } - FD_ZERO(&rs); - FD_ZERO(&ws); - - cap_fd_set(&rs, &max_fd, &timeout, &use_timeout); - http_fd_set(&rs, &ws, &max_fd, &timeout, &use_timeout); - - select_ret = select(max_fd+1, &rs, &ws, NULL, - (use_timeout) ? &timeout : NULL); - - if ((select_ret == 0) && (!use_timeout)) - errx(1, "select() erroneously timed out"); + graph_rotate(); + cap_ret = cap_poll(&rs); + dns_poll(); + http_poll(&rs, &ws); + timer_stop(&t, 1000000000, "event processing took longer than a second"); - if (select_ret == -1) { - if (errno == EINTR) - continue; - else - err(1, "select()"); - } - else { - graph_rotate(); - cap_poll(&rs); - dns_poll(); - http_poll(&rs, &ws); + if (!cap_ret) { + running = 0; } } @@ -508,7 +473,7 @@ main(int argc, char **argv) if (export_fn != NULL) db_export(export_fn); hosts_db_free(); graph_free(); - if (daylog_fn != NULL) daylog_free(); + if (opt_daylog_fn != NULL) daylog_free(); ncache_free(); if (pid_fn) pidfile_unlink(); verbosef("shut down");