/*-
* Copyright (c) 2015 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Mindaugas Rasiukevicius.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
if (ctx->dumper)
pcap_dump_close(ctx->dumper);
/*
* Open a log file to write for a given interface and dump there.
*/
switch (npfd_log_validate(ctx)) {
case -1:
syslog(LOG_ERR, "Giving up");
exit(EXIT_FAILURE);
/*NOTREACHED*/
case 0:
ctx->dumper = pcap_dump_open(ctx->pcap, ctx->path);
break;
default:
ctx->dumper = pcap_dump_open_append(ctx->pcap, ctx->path);
break;
}
(void)umask(omask);
if (ctx->dumper == NULL) {
if (die)
errx(EXIT_FAILURE, "pcap_dump_open failed for `%s': %s",
ctx->path, pcap_geterr(ctx->pcap));
syslog(LOG_ERR, "pcap_dump_open failed for `%s': %s",
ctx->path, pcap_geterr(ctx->pcap));
return false;
}
return true;
}
void
npfd_log_destroy(npfd_log_t *ctx)
{
if (ctx->dumper)
pcap_dump_close(ctx->dumper);
if (ctx->pcap)
pcap_close(ctx->pcap);
free(ctx);
}
int
npfd_log_getsock(npfd_log_t *ctx)
{
return pcap_get_selectable_fd(ctx->pcap);
}
void
npfd_log_flush(npfd_log_t *ctx)
{
if (!ctx->dumper)
return;
if (pcap_dump_flush(ctx->dumper) == -1)
syslog(LOG_ERR, "pcap_dump_flush failed for `%s': %m",
ctx->path);
}
int
npfd_log(npfd_log_t *ctx)
{
pcap_dumper_t *dumper = ctx->dumper;