/*
 * Copyright (c) 1994, 1996, 1997
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that: (1) source code distributions
 * retain the above copyright notice and this paragraph in its entirety, (2)
 * distributions including binary code include the above copyright notice and
 * this paragraph in its entirety in the documentation or other materials
 * provided with the distribution, and (3) all advertising materials mentioning
 * features or use of this software display the following acknowledgement:
 * ``This product includes software developed by the University of California,
 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
 * the University nor the names of its contributors may be used to endorse
 * or promote products derived from this software without specific prior
 * written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */
#ifndef lint
static const char copyright[] =
    "@(#) Copyright (c) 1994, 1996, 1997\n\
The Regents of the University of California.  All rights reserved.\n";
static const char rcsid[] =
    "@(#)$Header: /afs/sipb/project/tcpdump/repository/pcapture/pcapture.c,v 1.1.1.2 1997/08/17 23:19:51 jhawk Exp $ (LBL)";
#endif

/*
 * pcapture - capture the last few packets
 */

#include <sys/types.h>

#include <pcap.h>
#include <signal.h>
#include <stdio.h>
#ifdef __STDC__
#include <stdlib.h>
#endif
#include <string.h>
#include <unistd.h>

#include "interface.h"
#include "machdep.h"
#include "setsignal.h"

/* Globals */
char *program_name;

/* Locals */
static pcap_t *p;
static pcap_dumper_t *d;
static char *WFileName;

/* Forwards */
RETSIGTYPE cleanup(int);
__dead void usage(void) __attribute__((volatile));

extern int optind;
extern int opterr;
extern char *optarg;

int
main(int argc, char **argv)
{
	register int cnt, op, pflag, Oflag, snaplen;
	register char *cp, *infile, *expression, *device;
	u_int32_t localnet, netmask;
	struct bpf_program fcode;
	RETSIGTYPE (*oldhandler)(int);
	char ebuf[PCAP_ERRBUF_SIZE];

	cnt = DEFAULT_COUNT;
	device = NULL;
	infile = NULL;
	pflag = 0;
	Oflag = 1;
	snaplen = DEFAULT_SNAPLEN;
	if ((cp = strrchr(argv[0], '/')) != NULL)
		program_name = cp + 1;
	else
		program_name = argv[0];

	if (abort_on_misalignment(ebuf) < 0)
		error(ebuf);

	opterr = 0;
	while ((op = getopt(argc, argv, "c:F:i:Ops:w:")) != EOF)
		switch (op) {
		case 'c':
			cnt = atoi(optarg);
			break;

		case 'F':
			infile = optarg;
			break;

		case 'i':
			device = optarg;
			break;

		case 'O':
			Oflag = 0;
			break;

		case 'p':
			++pflag;
			break;

		case 's':
			snaplen = atoi(optarg);
			break;

		case 'w':
			WFileName = optarg;
			break;

		default:
			usage();
			/* NOTREACHED */
		}

	/* The raw output file is required */
	if (WFileName == NULL)
		usage();

	/* Determine the device name if not specified */
	if (device == NULL && (device = pcap_lookupdev(ebuf)) == NULL)
		error(ebuf);

	/* Fire up libpcap */
	p = pcap_open_live(device, snaplen, !pflag, 1000, ebuf);
	if (p == NULL)
		error(ebuf);
	if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0)
		error(ebuf);

	/* Now that the socket is open, throw away potential setuid/setgid */
	(void)setgid(getgid());
	(void)setuid(getuid());

	/* Construct, compile and set filter */
	if (infile)
		expression = read_infile(infile);
	else
		expression = copy_argv(argv + optind);
	if (pcap_compile(p, &fcode, expression, Oflag, netmask) < 0 ||
	    pcap_setfilter(p, &fcode) < 0)
		error(pcap_geterr(p));

	/* Setup signal handlers */
	(void)setsignal(SIGTERM, cleanup);
	(void)setsignal(SIGINT, cleanup);
	/* Cooperate with nohup(1) */
	if ((oldhandler = setsignal(SIGHUP, cleanup)) != SIG_DFL)
		(void)setsignal(SIGHUP, oldhandler);

	/* Open dump file (do it now in case there's a problem). */
	d = pcap_dump_open(p, WFileName);
	if (d == NULL)
		error(pcap_geterr(p));

	/* Iintialize packet buffers */
	pbuf_init(cnt, snaplen);

	(void)fprintf(stderr, "%s: listening on %s\n", program_name, device);
	(void)fflush(stderr);
	pcap_loop(p, -1, pbuf_process, NULL);
	error("pcap_loop exited");
}

/* Report statistics and write out the raw packet file */
RETSIGTYPE
cleanup(int signo)
{
	register u_int cnt;
	struct pcap_stat stat;

	(void)fflush(stdout);
	putc('\n', stderr);
	if (pcap_stats(p, &stat) < 0)
		(void)fprintf(stderr, "pcap_stats: %s\n", pcap_geterr(p));
	else {
		(void)fprintf(stderr, "%d packets received by filter\n",
		    stat.ps_recv);
		(void)fprintf(stderr, "%d packets dropped by kernel\n",
		    stat.ps_drop);
	}
	cnt = pbuf_dump(d);
	(void)fprintf(stderr, "%d packets written to %s\n", cnt, WFileName);
	exit(0);
}

__dead void
usage()
{
	extern char version[];
	extern char pcap_version[];

	(void)fprintf(stderr, "%s version %s\n", program_name, version);
	(void)fprintf(stderr, "libpcap version %s\n", pcap_version);
	(void)fprintf(stderr,
"Usage: %s [-Op] [-c count] [ -F file ] [ -i interface ]\n", program_name);
	(void)fprintf(stderr,
"\t\t[ -s snaplen ] -w file [ expression ]\n");
	exit(-1);
}
