Implement --snaplen
[darkstat] / darkstat.c
1 /* darkstat 3
2 * copyright (c) 2001-2009 Emil Mikulic.
3 *
4 * darkstat.c: signals, cmdline parsing, program body.
5 *
6 * You may use, modify and redistribute this file under the terms of the
7 * GNU General Public License version 2. (see COPYING.GPL)
8 */
9
10 #include "darkstat.h"
11 #include "acct.h"
12 #include "cap.h"
13 #include "conv.h"
14 #include "daylog.h"
15 #include "db.h"
16 #include "dns.h"
17 #include "http.h"
18 #include "hosts_db.h"
19 #include "localip.h"
20 #include "ncache.h"
21 #include "pidfile.h"
22
23 #include "err.h"
24 #include <arpa/inet.h>
25 #include <assert.h>
26 #include <errno.h>
27 #include <signal.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <pcap.h>
33
34 #include "now.h"
35 time_t now;
36
37 #ifndef INADDR_NONE
38 # define INADDR_NONE (-1) /* Solaris */
39 #endif
40
41 /* --- Signal handling --- */
42 static volatile int running = 1;
43 static void sig_shutdown(int signum _unused_) { running = 0; }
44
45 static volatile int reset_pending = 0;
46 static void sig_reset(int signum _unused_) { reset_pending = 1; }
47
48 /* --- Commandline parsing --- */
49 static unsigned long
50 parsenum(const char *str, unsigned long max /* 0 for no max */)
51 {
52 unsigned long n;
53 char *end;
54
55 errno = 0;
56 n = strtoul(str, &end, 10);
57 if (*end != '\0')
58 errx(1, "\"%s\" is not a valid number", str);
59 if (errno == ERANGE)
60 errx(1, "\"%s\" is out of range", str);
61 if ((max != 0) && (n > max))
62 errx(1, "\"%s\" is out of range (max %lu)", str, max);
63 return n;
64 }
65
66 const char *interface = NULL;
67 static void cb_interface(const char *arg) { interface = arg; }
68
69 const char *capfile = NULL;
70 static void cb_capfile(const char *arg) { capfile = arg; }
71
72 int want_snaplen = -1;
73 static void cb_snaplen(const char *arg) { want_snaplen = parsenum(arg, 0); }
74
75 int want_pppoe = 0;
76 static void cb_pppoe(const char *arg _unused_) { want_pppoe = 1; }
77
78 static void cb_verbose(const char *arg _unused_) { want_verbose = 1; }
79
80 int want_daemonize = 1;
81 static void cb_no_daemon(const char *arg _unused_) { want_daemonize = 0; }
82
83 int want_promisc = 1;
84 static void cb_no_promisc(const char *arg _unused_) { want_promisc = 0; }
85
86 int want_dns = 1;
87 static void cb_no_dns(const char *arg _unused_) { want_dns = 0; }
88
89 int want_macs = 1;
90 static void cb_no_macs(const char *arg _unused_) { want_macs = 0; }
91
92 int want_lastseen = 1;
93 static void cb_no_lastseen(const char *arg _unused_) { want_lastseen = 0; }
94
95 unsigned short bindport = 667;
96 static void cb_port(const char *arg) { bindport = parsenum(arg, 65536); }
97
98 in_addr_t bindaddr = INADDR_ANY;
99 static void cb_bindaddr(const char *arg)
100 {
101 bindaddr = inet_addr(arg);
102 if (bindaddr == (in_addr_t)INADDR_NONE)
103 errx(1, "malformed address \"%s\"", arg);
104 }
105
106 const char *filter = NULL;
107 static void cb_filter(const char *arg) { filter = arg; }
108
109 static void cb_local(const char *arg) { acct_init_localnet(arg); }
110
111 const char *chroot_dir = NULL;
112 static void cb_chroot(const char *arg) { chroot_dir = arg; }
113
114 const char *privdrop_user = NULL;
115 static void cb_user(const char *arg) { privdrop_user = arg; }
116
117 const char *daylog_fn = NULL;
118 static void cb_daylog(const char *arg)
119 {
120 if (chroot_dir == NULL)
121 errx(1, "the daylog file is relative to the chroot.\n"
122 "You must specify a --chroot dir before you can use --daylog.");
123 else
124 daylog_fn = arg;
125 }
126
127 const char *import_fn = NULL;
128 static void cb_import(const char *arg)
129 {
130 if (chroot_dir == NULL)
131 errx(1, "the import file is relative to the chroot.\n"
132 "You must specify a --chroot dir before you can use --import.");
133 else
134 import_fn = arg;
135 }
136
137 const char *export_fn = NULL;
138 static void cb_export(const char *arg)
139 {
140 if ((chroot_dir == NULL) && (capfile == NULL))
141 errx(1, "the export file is relative to the chroot.\n"
142 "You must specify a --chroot dir before you can use --export.");
143 else
144 export_fn = arg;
145 }
146
147 static const char *pid_fn = NULL;
148 static void cb_pidfile(const char *arg)
149 {
150 if (chroot_dir == NULL)
151 errx(1, "the pidfile is relative to the chroot.\n"
152 "You must specify a --chroot dir before you can use --pidfile.");
153 else
154 pid_fn = arg;
155 }
156
157 unsigned int hosts_max = 1000;
158 static void cb_hosts_max(const char *arg)
159 { hosts_max = parsenum(arg, 0); }
160
161 unsigned int hosts_keep = 500;
162 static void cb_hosts_keep(const char *arg)
163 { hosts_keep = parsenum(arg, 0); }
164
165 unsigned int ports_max = 200;
166 static void cb_ports_max(const char *arg)
167 { ports_max = parsenum(arg, 65536); }
168
169 unsigned int ports_keep = 30;
170 static void cb_ports_keep(const char *arg)
171 { ports_keep = parsenum(arg, 65536); }
172
173 unsigned int highest_port = 65535;
174 static void cb_highest_port(const char *arg)
175 { highest_port = parsenum(arg, 65535); }
176
177 int want_hexdump = 0;
178 static void cb_hexdump(const char *arg _unused_) { want_hexdump = 1; }
179
180 /* --- */
181
182 struct cmdline_arg {
183 const char *name, *arg_name; /* NULL arg_name means unary */
184 void (*callback)(const char *arg);
185 int num_seen;
186 };
187
188 static struct cmdline_arg cmdline_args[] = {
189 {"-i", "interface", cb_interface, 0},
190 {"-r", "file", cb_capfile, 0},
191 {"--snaplen", "bytes", cb_snaplen, 0},
192 {"--pppoe", NULL, cb_pppoe, 0},
193 {"--verbose", NULL, cb_verbose, 0},
194 {"--no-daemon", NULL, cb_no_daemon, 0},
195 {"--no-promisc", NULL, cb_no_promisc, 0},
196 {"--no-dns", NULL, cb_no_dns, 0},
197 {"--no-macs", NULL, cb_no_macs, 0},
198 {"--no-lastseen", NULL, cb_no_lastseen, 0},
199 {"-p", "port", cb_port, 0},
200 {"-b", "bindaddr", cb_bindaddr, 0},
201 {"-f", "filter", cb_filter, 0},
202 {"-l", "network/netmask", cb_local, 0},
203 {"--chroot", "dir", cb_chroot, 0},
204 {"--user", "username", cb_user, 0},
205 {"--daylog", "filename", cb_daylog, 0},
206 {"--import", "filename", cb_import, 0},
207 {"--export", "filename", cb_export, 0},
208 {"--pidfile", "filename", cb_pidfile, 0},
209 {"--hosts-max", "count", cb_hosts_max, 0},
210 {"--hosts-keep", "count", cb_hosts_keep, 0},
211 {"--ports-max", "count", cb_ports_max, 0},
212 {"--ports-keep", "count", cb_ports_keep, 0},
213 {"--highest-port", "port", cb_highest_port, 0},
214 {"--hexdump", NULL, cb_hexdump, 0},
215 {NULL, NULL, NULL, 0}
216 };
217
218 static void
219 pad(const int width)
220 {
221 int i;
222 for (i=0; i<width; i++) printf(" ");
223 }
224
225 /*
226 * We autogenerate the usage statement from the cmdline_args data structure.
227 */
228 static void
229 usage(void)
230 {
231 int width, first;
232 struct cmdline_arg *arg;
233
234 printf(PACKAGE_STRING " (built with libpcap %d.%d)\n\n",
235 PCAP_VERSION_MAJOR, PCAP_VERSION_MINOR);
236
237 width = printf("usage: darkstat ");
238 first = 1;
239
240 for (arg = cmdline_args; arg->name != NULL; arg++) {
241 if (first) first = 0; else pad(width);
242 printf("[ %s", arg->name);
243 if (arg->arg_name != NULL) printf(" %s", arg->arg_name);
244 printf(" ]\n");
245 }
246 printf("\n"
247 "Please refer to the darkstat(8) manual page for further\n"
248 "documentation and usage examples.\n");
249 }
250
251 static void
252 parse_sub_cmdline(const int argc, char * const *argv)
253 {
254 struct cmdline_arg *arg;
255
256 if (argc == 0) return;
257 for (arg = cmdline_args; arg->name != NULL; arg++)
258 if (strcmp(argv[0], arg->name) == 0) {
259 if ((arg->arg_name != NULL) && (argc == 1)) {
260 printf("\nerror: argument \"%s\" requires parameter \"%s\"\n",
261 arg->name, arg->arg_name);
262 usage();
263 exit(EXIT_FAILURE);
264 }
265 if (arg->num_seen > 0) {
266 printf("\nerror: already specified argument \"%s\"\n",
267 arg->name);
268 usage();
269 exit(EXIT_FAILURE);
270 }
271
272 arg->num_seen++;
273 if (arg->arg_name == NULL) {
274 arg->callback(NULL);
275 parse_sub_cmdline(argc-1, argv+1);
276 } else {
277 arg->callback(argv[1]);
278 parse_sub_cmdline(argc-2, argv+2);
279 }
280 return;
281 }
282
283 printf("\nerror: illegal argument: \"%s\"\n", argv[0]);
284 usage();
285 exit(EXIT_FAILURE);
286 }
287
288 static void
289 parse_cmdline(const int argc, char * const *argv)
290 {
291 if (argc < 1) {
292 /* Not enough args. */
293 usage();
294 exit(EXIT_FAILURE);
295 }
296
297 parse_sub_cmdline(argc, argv);
298
299 /* some default values */
300 if (chroot_dir == NULL) chroot_dir = CHROOT_DIR;
301 if (privdrop_user == NULL) privdrop_user = PRIVDROP_USER;
302
303 /* sanity check args */
304 if ((interface == NULL) && (capfile == NULL))
305 errx(1, "must specify either interface (-i) or capture file (-r)");
306
307 if ((interface != NULL) && (capfile != NULL))
308 errx(1, "can't specify both interface (-i) and capture file (-r)");
309
310 if ((hosts_max != 0) && (hosts_keep >= hosts_max)) {
311 hosts_keep = hosts_max / 2;
312 warnx("reducing --hosts-keep to %u, to be under --hosts-max (%u)",
313 hosts_keep, hosts_max);
314 }
315 verbosef("max %u hosts, cutting down to %u when exceeded",
316 hosts_max, hosts_keep);
317
318 if ((ports_max != 0) && (ports_keep >= ports_max)) {
319 ports_keep = ports_max / 2;
320 warnx("reducing --ports-keep to %u, to be under --ports-max (%u)",
321 ports_keep, ports_max);
322 }
323 verbosef("max %u ports per host, cutting down to %u when exceeded",
324 ports_max, ports_keep);
325
326 if (want_hexdump && !want_verbose) {
327 want_verbose = 1;
328 verbosef("--hexdump implies --verbose");
329 }
330
331 if (want_hexdump && want_daemonize) {
332 want_daemonize = 0;
333 verbosef("--hexdump implies --no-daemon");
334 }
335 }
336
337 static void
338 run_from_capfile(void)
339 {
340 graph_init();
341 hosts_db_init();
342 cap_from_file(capfile, filter);
343 cap_stop();
344 if (export_fn != NULL) db_export(export_fn);
345 hosts_db_free();
346 graph_free();
347 verbosef("Total packets: %qu, bytes: %qu", total_packets, total_bytes);
348 }
349
350 /* --- Program body --- */
351 int
352 main(int argc, char **argv)
353 {
354 test_64order();
355 parse_cmdline(argc-1, argv+1);
356
357 if (capfile) {
358 /*
359 * This is very different from a regular run against a network
360 * interface.
361 */
362 run_from_capfile();
363 return 0;
364 }
365
366 /* must verbosef() before first fork to init lock */
367 verbosef("starting up");
368 if (pid_fn) pidfile_create(chroot_dir, pid_fn, privdrop_user);
369
370 if (want_daemonize) {
371 verbosef("daemonizing to run in the background!");
372 daemonize_start();
373 verbosef("I am the main process");
374 }
375 if (pid_fn) pidfile_write_close();
376
377 /* do this first as it forks - minimize memory use */
378 if (want_dns) dns_init(privdrop_user);
379 cap_init(interface, filter, want_promisc); /* needs root */
380 http_init(bindaddr, bindport, /*maxconn=*/ -1); /* low ports need root */
381 ncache_init(); /* must do before chroot() */
382
383 privdrop(chroot_dir, privdrop_user);
384
385 /* Don't need root privs for these: */
386 now = time(NULL);
387 if (daylog_fn != NULL) daylog_init(daylog_fn);
388 graph_init();
389 hosts_db_init();
390 if (import_fn != NULL) db_import(import_fn);
391 localip_init(interface);
392
393 if (signal(SIGTERM, sig_shutdown) == SIG_ERR)
394 errx(1, "signal(SIGTERM) failed");
395 if (signal(SIGINT, sig_shutdown) == SIG_ERR)
396 errx(1, "signal(SIGINT) failed");
397 if (signal(SIGUSR1, sig_reset) == SIG_ERR)
398 errx(1, "signal(SIGUSR1) failed");
399
400 verbosef("entering main loop");
401 daemonize_finish();
402
403 while (running) {
404 int select_ret, max_fd = -1, use_timeout = 0;
405 struct timeval timeout;
406 fd_set rs, ws;
407
408 now = time(NULL);
409
410 if (reset_pending) {
411 if (export_fn != NULL) db_export(export_fn); /* FIXME: USR2? */
412 hosts_db_reset();
413 graph_reset();
414 reset_pending = 0;
415 }
416
417 FD_ZERO(&rs);
418 FD_ZERO(&ws);
419
420 cap_fd_set(&rs, &max_fd, &timeout, &use_timeout);
421 http_fd_set(&rs, &ws, &max_fd, &timeout, &use_timeout);
422
423 select_ret = select(max_fd+1, &rs, &ws, NULL,
424 (use_timeout) ? &timeout : NULL);
425
426 if ((select_ret == 0) && (!use_timeout))
427 errx(1, "select() erroneously timed out");
428
429 if (select_ret == -1) {
430 if (errno == EINTR)
431 continue;
432 else
433 err(1, "select()");
434 }
435 else {
436 graph_rotate();
437 cap_poll(&rs);
438 dns_poll();
439 http_poll(&rs, &ws);
440 }
441 }
442
443 verbosef("shutting down");
444 verbosef("pcap stats: %u packets received, %u packets dropped",
445 pkts_recv, pkts_drop);
446 cap_stop();
447 dns_stop();
448 if (export_fn != NULL) db_export(export_fn);
449 hosts_db_free();
450 graph_free();
451 if (daylog_fn != NULL) daylog_free();
452 ncache_free();
453 if (pid_fn) pidfile_unlink();
454 verbosef("shut down");
455 return (EXIT_SUCCESS);
456 }
457
458 /* vim:set ts=3 sw=3 tw=78 expandtab: */