Home | History | Annotate | Line # | Download | only in npfd
npfd_log.c revision 1.2
      1  1.2  christos /*	$NetBSD: npfd_log.c,v 1.2 2016/12/28 01:25:48 christos Exp $	*/
      2  1.1     rmind 
      3  1.1     rmind /*-
      4  1.1     rmind  * Copyright (c) 2015 The NetBSD Foundation, Inc.
      5  1.1     rmind  * All rights reserved.
      6  1.1     rmind  *
      7  1.1     rmind  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1     rmind  * by Mindaugas Rasiukevicius.
      9  1.1     rmind  *
     10  1.1     rmind  * Redistribution and use in source and binary forms, with or without
     11  1.1     rmind  * modification, are permitted provided that the following conditions
     12  1.1     rmind  * are met:
     13  1.1     rmind  * 1. Redistributions of source code must retain the above copyright
     14  1.1     rmind  *    notice, this list of conditions and the following disclaimer.
     15  1.1     rmind  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1     rmind  *    notice, this list of conditions and the following disclaimer in the
     17  1.1     rmind  *    documentation and/or other materials provided with the distribution.
     18  1.1     rmind  *
     19  1.1     rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1     rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1     rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1     rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1     rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1     rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1     rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1     rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1     rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1     rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1     rmind  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1     rmind  */
     31  1.1     rmind 
     32  1.1     rmind #include <sys/cdefs.h>
     33  1.2  christos __RCSID("$NetBSD: npfd_log.c,v 1.2 2016/12/28 01:25:48 christos Exp $");
     34  1.2  christos 
     35  1.2  christos #include <sys/types.h>
     36  1.2  christos #include <sys/param.h>
     37  1.2  christos #include <net/if.h>
     38  1.1     rmind 
     39  1.1     rmind #include <stdio.h>
     40  1.1     rmind #include <inttypes.h>
     41  1.1     rmind #include <limits.h>
     42  1.2  christos #include <stdlib.h>
     43  1.2  christos #include <syslog.h>
     44  1.2  christos #include <stdbool.h>
     45  1.1     rmind 
     46  1.1     rmind #include <pcap/pcap.h>
     47  1.2  christos #include "npfd.h"
     48  1.1     rmind 
     49  1.1     rmind struct npfd_log {
     50  1.2  christos 	char ifname[IFNAMSIZ];
     51  1.2  christos 	char path[MAXPATHLEN];
     52  1.2  christos 	pcap_t *pcap;
     53  1.2  christos 	pcap_dumper_t *dumper;
     54  1.1     rmind };
     55  1.1     rmind 
     56  1.1     rmind npfd_log_t *
     57  1.1     rmind npfd_log_create(unsigned if_idx)
     58  1.1     rmind {
     59  1.1     rmind 	npfd_log_t *ctx;
     60  1.1     rmind 	char errbuf[PCAP_ERRBUF_SIZE];
     61  1.1     rmind 
     62  1.2  christos 	if ((ctx = calloc(1, sizeof(*ctx))) == NULL) {
     63  1.1     rmind 		syslog(LOG_ERR, "malloc failed: %m");
     64  1.1     rmind 		return NULL;
     65  1.1     rmind 	}
     66  1.1     rmind 
     67  1.1     rmind 	/*
     68  1.1     rmind 	 * Open a live capture handle in non-blocking mode.
     69  1.1     rmind 	 */
     70  1.2  christos 	snprintf(ctx->ifname, sizeof(ctx->ifname), NPFD_NPFLOG "%u", if_idx);
     71  1.2  christos 	ctx->pcap = pcap_create(ctx->ifname, errbuf);
     72  1.2  christos 	if (ctx->pcap == NULL) {
     73  1.1     rmind 		syslog(LOG_ERR, "pcap_create failed: %s", errbuf);
     74  1.1     rmind 		goto err;
     75  1.1     rmind 	}
     76  1.2  christos 	if (pcap_setnonblock(ctx->pcap, 1, errbuf) == -1) {
     77  1.1     rmind 		syslog(LOG_ERR, "pcap_setnonblock failed: %s", errbuf);
     78  1.1     rmind 		goto err;
     79  1.1     rmind 	}
     80  1.1     rmind 
     81  1.2  christos 	pcap_set_snaplen(ctx->pcap, 10240);
     82  1.2  christos 
     83  1.2  christos 	snprintf(ctx->path, sizeof(ctx->path), "%s/%s%s",
     84  1.2  christos 	    NPFD_LOG_PATH, ctx->ifname, ".pcap");
     85  1.2  christos 
     86  1.2  christos 	if (!npfd_log_reopen(ctx))
     87  1.1     rmind 		goto err;
     88  1.2  christos 
     89  1.1     rmind 	return ctx;
     90  1.1     rmind err:
     91  1.1     rmind 	npfd_log_destroy(ctx);
     92  1.1     rmind 	return NULL;
     93  1.1     rmind }
     94  1.1     rmind 
     95  1.2  christos bool
     96  1.2  christos npfd_log_reopen(npfd_log_t *ctx)
     97  1.2  christos {
     98  1.2  christos 	if (ctx->dumper)
     99  1.2  christos 		pcap_dump_close(ctx->dumper);
    100  1.2  christos 	/*
    101  1.2  christos 	 * Open a log file to write for a given interface and dump there.
    102  1.2  christos 	 */
    103  1.2  christos 	ctx->dumper = pcap_dump_open_append(ctx->pcap, ctx->path);
    104  1.2  christos 	if (ctx->dumper == NULL) {
    105  1.2  christos 		syslog(LOG_ERR, "pcap_dump_open_append failed for `%s': %s",
    106  1.2  christos 		    ctx->path, pcap_geterr(ctx->pcap));
    107  1.2  christos 		return false;
    108  1.2  christos 	}
    109  1.2  christos 	return true;
    110  1.2  christos }
    111  1.2  christos 
    112  1.1     rmind void
    113  1.1     rmind npfd_log_destroy(npfd_log_t *ctx)
    114  1.1     rmind {
    115  1.1     rmind 	if (ctx->dumper)
    116  1.1     rmind 		pcap_dump_close(ctx->dumper);
    117  1.1     rmind 	if (ctx->pcap)
    118  1.1     rmind 		pcap_close(ctx->pcap);
    119  1.1     rmind 	free(ctx);
    120  1.1     rmind }
    121  1.1     rmind 
    122  1.1     rmind int
    123  1.1     rmind npfd_log_getsock(npfd_log_t *ctx)
    124  1.1     rmind {
    125  1.1     rmind 	return pcap_get_selectable_fd(ctx->pcap);
    126  1.1     rmind }
    127  1.1     rmind 
    128  1.1     rmind void
    129  1.1     rmind npfd_log(npfd_log_t *ctx)
    130  1.1     rmind {
    131  1.1     rmind 	pcap_dumper_t *dumper = ctx->dumper;
    132  1.1     rmind 
    133  1.1     rmind 	pcap_dispatch(ctx->pcap, PCAP_NPACKETS, pcap_dump, (uint8_t *)dumper);
    134  1.1     rmind }
    135  1.1     rmind 
    136  1.1     rmind void
    137  1.1     rmind npfd_log_stats(npfd_log_t *ctx)
    138  1.1     rmind {
    139  1.1     rmind 	pcap_t *pcap = ctx->pcap;
    140  1.1     rmind 	struct pcap_stat ps;
    141  1.1     rmind 
    142  1.2  christos 	if (pcap_stats(pcap, &ps) == -1) {
    143  1.1     rmind 		syslog(LOG_ERR, "pcap_stats failed: %s", pcap_geterr(pcap));
    144  1.1     rmind 		return;
    145  1.1     rmind 	}
    146  1.1     rmind 	syslog(LOG_NOTICE, "packet statistics: %u received, %u dropped",
    147  1.1     rmind 	    ps.ps_recv, ps.ps_drop);
    148  1.1     rmind }
    149