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 static void cb_base(const char *arg
) { http_init_base(arg
); }
139 const char *privdrop_user
= NULL
;
140 static void cb_user(const char *arg
) { privdrop_user
= arg
; }
142 const char *daylog_fn
= NULL
;
143 static void cb_daylog(const char *arg
)
145 if (chroot_dir
== NULL
)
146 errx(1, "the daylog file is relative to the chroot.\n"
147 "You must specify a --chroot dir before you can use --daylog.");
152 const char *import_fn
= NULL
;
153 static void cb_import(const char *arg
)
155 if (chroot_dir
== NULL
)
156 errx(1, "the import file is relative to the chroot.\n"
157 "You must specify a --chroot dir before you can use --import.");
162 const char *export_fn
= NULL
;
163 static void cb_export(const char *arg
)
165 if ((chroot_dir
== NULL
) && (capfile
== NULL
))
166 errx(1, "the export file is relative to the chroot.\n"
167 "You must specify a --chroot dir before you can use --export.");
172 static const char *pid_fn
= NULL
;
173 static void cb_pidfile(const char *arg
)
175 if (chroot_dir
== NULL
)
176 errx(1, "the pidfile is relative to the chroot.\n"
177 "You must specify a --chroot dir before you can use --pidfile.");
182 unsigned int hosts_max
= 1000;
183 static void cb_hosts_max(const char *arg
)
184 { hosts_max
= parsenum(arg
, 0); }
186 unsigned int hosts_keep
= 500;
187 static void cb_hosts_keep(const char *arg
)
188 { hosts_keep
= parsenum(arg
, 0); }
190 unsigned int ports_max
= 200;
191 static void cb_ports_max(const char *arg
)
192 { ports_max
= parsenum(arg
, 65536); }
194 unsigned int ports_keep
= 30;
195 static void cb_ports_keep(const char *arg
)
196 { ports_keep
= parsenum(arg
, 65536); }
198 unsigned int highest_port
= 65535;
199 static void cb_highest_port(const char *arg
)
200 { highest_port
= parsenum(arg
, 65535); }
203 static void cb_wait_secs(const char *arg
)
204 { wait_secs
= parsenum(arg
, 0); }
206 int want_hexdump
= 0;
207 static void cb_hexdump(const char *arg _unused_
) { want_hexdump
= 1; }
212 const char *name
, *arg_name
; /* NULL arg_name means unary */
213 void (*callback
)(const char *arg
);
217 static struct cmdline_arg cmdline_args
[] = {
218 {"-i", "interface", cb_interface
, 0},
219 {"-r", "file", cb_capfile
, 0},
220 {"--snaplen", "bytes", cb_snaplen
, 0},
221 {"--pppoe", NULL
, cb_pppoe
, 0},
222 {"--syslog", NULL
, cb_syslog
, 0},
223 {"--verbose", NULL
, cb_verbose
, 0},
224 {"--no-daemon", NULL
, cb_no_daemon
, 0},
225 {"--no-promisc", NULL
, cb_no_promisc
, 0},
226 {"--no-dns", NULL
, cb_no_dns
, 0},
227 {"--no-macs", NULL
, cb_no_macs
, 0},
228 {"--no-lastseen", NULL
, cb_no_lastseen
, 0},
229 {"-p", "port", cb_port
, 0},
230 {"-b", "bindaddr", cb_bindaddr
, 0},
231 {"--base", "path", cb_base
, 0},
232 {"-f", "filter", cb_filter
, 0},
233 {"-l", "network/netmask", cb_local
, 0},
234 {"--chroot", "dir", cb_chroot
, 0},
235 {"--user", "username", cb_user
, 0},
236 {"--daylog", "filename", cb_daylog
, 0},
237 {"--import", "filename", cb_import
, 0},
238 {"--export", "filename", cb_export
, 0},
239 {"--pidfile", "filename", cb_pidfile
, 0},
240 {"--hosts-max", "count", cb_hosts_max
, 0},
241 {"--hosts-keep", "count", cb_hosts_keep
, 0},
242 {"--ports-max", "count", cb_ports_max
, 0},
243 {"--ports-keep", "count", cb_ports_keep
, 0},
244 {"--highest-port", "port", cb_highest_port
, 0},
245 {"--wait", "secs", cb_wait_secs
, 0},
246 {"--hexdump", NULL
, cb_hexdump
, 0},
247 {NULL
, NULL
, NULL
, 0}
254 for (i
=0; i
<width
; i
++) printf(" ");
258 * We autogenerate the usage statement from the cmdline_args data structure.
264 struct cmdline_arg
*arg
;
266 printf(PACKAGE_STRING
" (built with libpcap %d.%d)\n\n",
267 PCAP_VERSION_MAJOR
, PCAP_VERSION_MINOR
);
269 width
= printf("usage: darkstat ");
272 for (arg
= cmdline_args
; arg
->name
!= NULL
; arg
++) {
273 if (first
) first
= 0; else pad(width
);
274 printf("[ %s", arg
->name
);
275 if (arg
->arg_name
!= NULL
) printf(" %s", arg
->arg_name
);
279 "Please refer to the darkstat(8) manual page for further\n"
280 "documentation and usage examples.\n");
284 parse_sub_cmdline(const int argc
, char * const *argv
)
286 struct cmdline_arg
*arg
;
288 if (argc
== 0) return;
289 for (arg
= cmdline_args
; arg
->name
!= NULL
; arg
++)
290 if (strcmp(argv
[0], arg
->name
) == 0) {
291 if ((arg
->arg_name
!= NULL
) && (argc
== 1)) {
293 "error: argument \"%s\" requires parameter \"%s\"\n",
294 arg
->name
, arg
->arg_name
);
298 if (arg
->num_seen
> 0) {
300 "error: already specified argument \"%s\"\n",
307 if (arg
->arg_name
== NULL
) {
309 parse_sub_cmdline(argc
-1, argv
+1);
311 arg
->callback(argv
[1]);
312 parse_sub_cmdline(argc
-2, argv
+2);
317 fprintf(stderr
, "error: illegal argument: \"%s\"\n", argv
[0]);
323 parse_cmdline(const int argc
, char * const *argv
)
326 /* Not enough args. */
331 parse_sub_cmdline(argc
, argv
);
333 /* start syslogging as early as possible */
334 if (want_syslog
) openlog("darkstat", LOG_NDELAY
| LOG_PID
, LOG_DAEMON
);
336 /* some default values */
337 if (chroot_dir
== NULL
) chroot_dir
= CHROOT_DIR
;
338 if (privdrop_user
== NULL
) privdrop_user
= PRIVDROP_USER
;
340 /* sanity check args */
341 if ((interface
== NULL
) && (capfile
== NULL
))
342 errx(1, "must specify either interface (-i) or capture file (-r)");
344 if ((interface
!= NULL
) && (capfile
!= NULL
))
345 errx(1, "can't specify both interface (-i) and capture file (-r)");
347 if ((hosts_max
!= 0) && (hosts_keep
>= hosts_max
)) {
348 hosts_keep
= hosts_max
/ 2;
349 warnx("reducing --hosts-keep to %u, to be under --hosts-max (%u)",
350 hosts_keep
, hosts_max
);
352 verbosef("max %u hosts, cutting down to %u when exceeded",
353 hosts_max
, hosts_keep
);
355 if ((ports_max
!= 0) && (ports_keep
>= ports_max
)) {
356 ports_keep
= ports_max
/ 2;
357 warnx("reducing --ports-keep to %u, to be under --ports-max (%u)",
358 ports_keep
, ports_max
);
360 verbosef("max %u ports per host, cutting down to %u when exceeded",
361 ports_max
, ports_keep
);
363 if (want_hexdump
&& !want_verbose
) {
365 verbosef("--hexdump implies --verbose");
368 if (want_hexdump
&& want_daemonize
) {
370 verbosef("--hexdump implies --no-daemon");
375 run_from_capfile(void)
379 cap_from_file(capfile
, filter
);
381 if (export_fn
!= NULL
) db_export(export_fn
);
384 verbosef("Total packets: %qu, bytes: %qu", total_packets
, total_bytes
);
387 /* --- Program body --- */
389 main(int argc
, char **argv
)
392 parse_cmdline(argc
-1, argv
+1);
396 * This is very different from a regular run against a network
403 /* must verbosef() before first fork to init lock */
404 verbosef("starting up");
405 if (pid_fn
) pidfile_create(chroot_dir
, pid_fn
, privdrop_user
);
407 if (want_daemonize
) {
408 verbosef("daemonizing to run in the background!");
410 verbosef("I am the main process");
412 if (pid_fn
) pidfile_write_close();
414 /* do this first as it forks - minimize memory use */
415 if (want_dns
) dns_init(privdrop_user
);
416 cap_init(interface
, filter
, want_promisc
); /* needs root */
417 http_init(bindaddr
, bindport
, /*maxconn=*/ -1); /* low ports need root */
418 ncache_init(); /* must do before chroot() */
420 privdrop(chroot_dir
, privdrop_user
);
422 /* Don't need root privs for these: */
424 if (daylog_fn
!= NULL
) daylog_init(daylog_fn
);
427 if (import_fn
!= NULL
) db_import(import_fn
);
428 localip_init(interface
);
430 if (signal(SIGTERM
, sig_shutdown
) == SIG_ERR
)
431 errx(1, "signal(SIGTERM) failed");
432 if (signal(SIGINT
, sig_shutdown
) == SIG_ERR
)
433 errx(1, "signal(SIGINT) failed");
434 if (signal(SIGUSR1
, sig_reset
) == SIG_ERR
)
435 errx(1, "signal(SIGUSR1) failed");
436 if (signal(SIGUSR2
, sig_export
) == SIG_ERR
)
437 errx(1, "signal(SIGUSR2) failed");
439 verbosef("entering main loop");
443 int select_ret
, max_fd
= -1, use_timeout
= 0;
444 struct timeval timeout
;
449 if (export_pending
) {
450 if (export_fn
!= NULL
)
451 db_export(export_fn
);
464 cap_fd_set(&rs
, &max_fd
, &timeout
, &use_timeout
);
465 http_fd_set(&rs
, &ws
, &max_fd
, &timeout
, &use_timeout
);
467 select_ret
= select(max_fd
+1, &rs
, &ws
, NULL
,
468 (use_timeout
) ? &timeout
: NULL
);
470 if ((select_ret
== 0) && (!use_timeout
))
471 errx(1, "select() erroneously timed out");
473 if (select_ret
== -1) {
487 verbosef("shutting down");
488 verbosef("pcap stats: %u packets received, %u packets dropped",
489 pkts_recv
, pkts_drop
);
492 if (export_fn
!= NULL
) db_export(export_fn
);
495 if (daylog_fn
!= NULL
) daylog_free();
497 if (pid_fn
) pidfile_unlink();
498 verbosef("shut down");
499 return (EXIT_SUCCESS
);
502 /* vim:set ts=3 sw=3 tw=78 expandtab: */