2 * copyright (c) 2001-2009 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>
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;
47 static void sig_reset(int signum _unused_
) { reset_pending
= 1; }
49 /* --- Commandline parsing --- */
51 parsenum(const char *str
, unsigned long max
/* 0 for no max */)
57 n
= strtoul(str
, &end
, 10);
59 errx(1, "\"%s\" is not a valid number", str
);
61 errx(1, "\"%s\" is out of range", str
);
62 if ((max
!= 0) && (n
> max
))
63 errx(1, "\"%s\" is out of range (max %lu)", str
, max
);
67 const char *interface
= NULL
;
68 static void cb_interface(const char *arg
) { interface
= arg
; }
70 const char *capfile
= NULL
;
71 static void cb_capfile(const char *arg
) { capfile
= arg
; }
73 int want_snaplen
= -1;
74 static void cb_snaplen(const char *arg
) { want_snaplen
= parsenum(arg
, 0); }
77 static void cb_pppoe(const char *arg _unused_
) { want_pppoe
= 1; }
79 static void cb_syslog(const char *arg _unused_
) { want_syslog
= 1; }
81 static void cb_verbose(const char *arg _unused_
) { want_verbose
= 1; }
83 int want_daemonize
= 1;
84 static void cb_no_daemon(const char *arg _unused_
) { want_daemonize
= 0; }
87 static void cb_no_promisc(const char *arg _unused_
) { want_promisc
= 0; }
90 static void cb_no_dns(const char *arg _unused_
) { want_dns
= 0; }
93 static void cb_no_macs(const char *arg _unused_
) { want_macs
= 0; }
95 int want_lastseen
= 1;
96 static void cb_no_lastseen(const char *arg _unused_
) { want_lastseen
= 0; }
98 unsigned short bindport
= 667;
99 static void cb_port(const char *arg
) { bindport
= parsenum(arg
, 65536); }
101 in_addr_t bindaddr
= INADDR_ANY
;
102 static void cb_bindaddr(const char *arg
)
104 bindaddr
= inet_addr(arg
);
105 if (bindaddr
== (in_addr_t
)INADDR_NONE
)
106 errx(1, "malformed address \"%s\"", arg
);
109 const char *filter
= NULL
;
110 static void cb_filter(const char *arg
) { filter
= arg
; }
112 static void cb_local(const char *arg
) { acct_init_localnet(arg
); }
114 const char *chroot_dir
= NULL
;
115 static void cb_chroot(const char *arg
) { chroot_dir
= arg
; }
117 const char *privdrop_user
= NULL
;
118 static void cb_user(const char *arg
) { privdrop_user
= arg
; }
120 const char *daylog_fn
= NULL
;
121 static void cb_daylog(const char *arg
)
123 if (chroot_dir
== NULL
)
124 errx(1, "the daylog file is relative to the chroot.\n"
125 "You must specify a --chroot dir before you can use --daylog.");
130 const char *import_fn
= NULL
;
131 static void cb_import(const char *arg
)
133 if (chroot_dir
== NULL
)
134 errx(1, "the import file is relative to the chroot.\n"
135 "You must specify a --chroot dir before you can use --import.");
140 const char *export_fn
= NULL
;
141 static void cb_export(const char *arg
)
143 if ((chroot_dir
== NULL
) && (capfile
== NULL
))
144 errx(1, "the export file is relative to the chroot.\n"
145 "You must specify a --chroot dir before you can use --export.");
150 static const char *pid_fn
= NULL
;
151 static void cb_pidfile(const char *arg
)
153 if (chroot_dir
== NULL
)
154 errx(1, "the pidfile is relative to the chroot.\n"
155 "You must specify a --chroot dir before you can use --pidfile.");
160 unsigned int hosts_max
= 1000;
161 static void cb_hosts_max(const char *arg
)
162 { hosts_max
= parsenum(arg
, 0); }
164 unsigned int hosts_keep
= 500;
165 static void cb_hosts_keep(const char *arg
)
166 { hosts_keep
= parsenum(arg
, 0); }
168 unsigned int ports_max
= 200;
169 static void cb_ports_max(const char *arg
)
170 { ports_max
= parsenum(arg
, 65536); }
172 unsigned int ports_keep
= 30;
173 static void cb_ports_keep(const char *arg
)
174 { ports_keep
= parsenum(arg
, 65536); }
176 unsigned int highest_port
= 65535;
177 static void cb_highest_port(const char *arg
)
178 { highest_port
= parsenum(arg
, 65535); }
180 int want_hexdump
= 0;
181 static void cb_hexdump(const char *arg _unused_
) { want_hexdump
= 1; }
186 const char *name
, *arg_name
; /* NULL arg_name means unary */
187 void (*callback
)(const char *arg
);
191 static struct cmdline_arg cmdline_args
[] = {
192 {"-i", "interface", cb_interface
, 0},
193 {"-r", "file", cb_capfile
, 0},
194 {"--snaplen", "bytes", cb_snaplen
, 0},
195 {"--pppoe", NULL
, cb_pppoe
, 0},
196 {"--syslog", NULL
, cb_syslog
, 0},
197 {"--verbose", NULL
, cb_verbose
, 0},
198 {"--no-daemon", NULL
, cb_no_daemon
, 0},
199 {"--no-promisc", NULL
, cb_no_promisc
, 0},
200 {"--no-dns", NULL
, cb_no_dns
, 0},
201 {"--no-macs", NULL
, cb_no_macs
, 0},
202 {"--no-lastseen", NULL
, cb_no_lastseen
, 0},
203 {"-p", "port", cb_port
, 0},
204 {"-b", "bindaddr", cb_bindaddr
, 0},
205 {"-f", "filter", cb_filter
, 0},
206 {"-l", "network/netmask", cb_local
, 0},
207 {"--chroot", "dir", cb_chroot
, 0},
208 {"--user", "username", cb_user
, 0},
209 {"--daylog", "filename", cb_daylog
, 0},
210 {"--import", "filename", cb_import
, 0},
211 {"--export", "filename", cb_export
, 0},
212 {"--pidfile", "filename", cb_pidfile
, 0},
213 {"--hosts-max", "count", cb_hosts_max
, 0},
214 {"--hosts-keep", "count", cb_hosts_keep
, 0},
215 {"--ports-max", "count", cb_ports_max
, 0},
216 {"--ports-keep", "count", cb_ports_keep
, 0},
217 {"--highest-port", "port", cb_highest_port
, 0},
218 {"--hexdump", NULL
, cb_hexdump
, 0},
219 {NULL
, NULL
, NULL
, 0}
226 for (i
=0; i
<width
; i
++) printf(" ");
230 * We autogenerate the usage statement from the cmdline_args data structure.
236 struct cmdline_arg
*arg
;
238 printf(PACKAGE_STRING
" (built with libpcap %d.%d)\n\n",
239 PCAP_VERSION_MAJOR
, PCAP_VERSION_MINOR
);
241 width
= printf("usage: darkstat ");
244 for (arg
= cmdline_args
; arg
->name
!= NULL
; arg
++) {
245 if (first
) first
= 0; else pad(width
);
246 printf("[ %s", arg
->name
);
247 if (arg
->arg_name
!= NULL
) printf(" %s", arg
->arg_name
);
251 "Please refer to the darkstat(8) manual page for further\n"
252 "documentation and usage examples.\n");
256 parse_sub_cmdline(const int argc
, char * const *argv
)
258 struct cmdline_arg
*arg
;
260 if (argc
== 0) return;
261 for (arg
= cmdline_args
; arg
->name
!= NULL
; arg
++)
262 if (strcmp(argv
[0], arg
->name
) == 0) {
263 if ((arg
->arg_name
!= NULL
) && (argc
== 1)) {
265 "error: argument \"%s\" requires parameter \"%s\"\n",
266 arg
->name
, arg
->arg_name
);
270 if (arg
->num_seen
> 0) {
272 "error: already specified argument \"%s\"\n",
279 if (arg
->arg_name
== NULL
) {
281 parse_sub_cmdline(argc
-1, argv
+1);
283 arg
->callback(argv
[1]);
284 parse_sub_cmdline(argc
-2, argv
+2);
289 fprintf(stderr
, "error: illegal argument: \"%s\"\n", argv
[0]);
295 parse_cmdline(const int argc
, char * const *argv
)
298 /* Not enough args. */
303 parse_sub_cmdline(argc
, argv
);
305 /* start syslogging as early as possible */
306 if (want_syslog
) openlog("darkstat", LOG_NDELAY
| LOG_PID
, LOG_DAEMON
);
308 /* some default values */
309 if (chroot_dir
== NULL
) chroot_dir
= CHROOT_DIR
;
310 if (privdrop_user
== NULL
) privdrop_user
= PRIVDROP_USER
;
312 /* sanity check args */
313 if ((interface
== NULL
) && (capfile
== NULL
))
314 errx(1, "must specify either interface (-i) or capture file (-r)");
316 if ((interface
!= NULL
) && (capfile
!= NULL
))
317 errx(1, "can't specify both interface (-i) and capture file (-r)");
319 if ((hosts_max
!= 0) && (hosts_keep
>= hosts_max
)) {
320 hosts_keep
= hosts_max
/ 2;
321 warnx("reducing --hosts-keep to %u, to be under --hosts-max (%u)",
322 hosts_keep
, hosts_max
);
324 verbosef("max %u hosts, cutting down to %u when exceeded",
325 hosts_max
, hosts_keep
);
327 if ((ports_max
!= 0) && (ports_keep
>= ports_max
)) {
328 ports_keep
= ports_max
/ 2;
329 warnx("reducing --ports-keep to %u, to be under --ports-max (%u)",
330 ports_keep
, ports_max
);
332 verbosef("max %u ports per host, cutting down to %u when exceeded",
333 ports_max
, ports_keep
);
335 if (want_hexdump
&& !want_verbose
) {
337 verbosef("--hexdump implies --verbose");
340 if (want_hexdump
&& want_daemonize
) {
342 verbosef("--hexdump implies --no-daemon");
347 run_from_capfile(void)
351 cap_from_file(capfile
, filter
);
353 if (export_fn
!= NULL
) db_export(export_fn
);
356 verbosef("Total packets: %qu, bytes: %qu", total_packets
, total_bytes
);
359 /* --- Program body --- */
361 main(int argc
, char **argv
)
364 parse_cmdline(argc
-1, argv
+1);
368 * This is very different from a regular run against a network
375 /* must verbosef() before first fork to init lock */
376 verbosef("starting up");
377 if (pid_fn
) pidfile_create(chroot_dir
, pid_fn
, privdrop_user
);
379 if (want_daemonize
) {
380 verbosef("daemonizing to run in the background!");
382 verbosef("I am the main process");
384 if (pid_fn
) pidfile_write_close();
386 /* do this first as it forks - minimize memory use */
387 if (want_dns
) dns_init(privdrop_user
);
388 cap_init(interface
, filter
, want_promisc
); /* needs root */
389 http_init(bindaddr
, bindport
, /*maxconn=*/ -1); /* low ports need root */
390 ncache_init(); /* must do before chroot() */
392 privdrop(chroot_dir
, privdrop_user
);
394 /* Don't need root privs for these: */
396 if (daylog_fn
!= NULL
) daylog_init(daylog_fn
);
399 if (import_fn
!= NULL
) db_import(import_fn
);
400 localip_init(interface
);
402 if (signal(SIGTERM
, sig_shutdown
) == SIG_ERR
)
403 errx(1, "signal(SIGTERM) failed");
404 if (signal(SIGINT
, sig_shutdown
) == SIG_ERR
)
405 errx(1, "signal(SIGINT) failed");
406 if (signal(SIGUSR1
, sig_reset
) == SIG_ERR
)
407 errx(1, "signal(SIGUSR1) failed");
409 verbosef("entering main loop");
413 int select_ret
, max_fd
= -1, use_timeout
= 0;
414 struct timeval timeout
;
420 if (export_fn
!= NULL
) db_export(export_fn
); /* FIXME: USR2? */
429 cap_fd_set(&rs
, &max_fd
, &timeout
, &use_timeout
);
430 http_fd_set(&rs
, &ws
, &max_fd
, &timeout
, &use_timeout
);
432 select_ret
= select(max_fd
+1, &rs
, &ws
, NULL
,
433 (use_timeout
) ? &timeout
: NULL
);
435 if ((select_ret
== 0) && (!use_timeout
))
436 errx(1, "select() erroneously timed out");
438 if (select_ret
== -1) {
452 verbosef("shutting down");
453 verbosef("pcap stats: %u packets received, %u packets dropped",
454 pkts_recv
, pkts_drop
);
457 if (export_fn
!= NULL
) db_export(export_fn
);
460 if (daylog_fn
!= NULL
) daylog_free();
462 if (pid_fn
) pidfile_unlink();
463 verbosef("shut down");
464 return (EXIT_SUCCESS
);
467 /* vim:set ts=3 sw=3 tw=78 expandtab: */