Home | History | Annotate | Line # | Download | only in npfd
npfd_log.c revision 1.1
      1  1.1  rmind /*	$NetBSD: npfd_log.c,v 1.1 2016/12/27 22:20:00 rmind 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.1  rmind __RCSID("$NetBSD: npfd_log.c,v 1.1 2016/12/27 22:20:00 rmind Exp $");
     34  1.1  rmind 
     35  1.1  rmind #include <stdio.h>
     36  1.1  rmind #include <inttypes.h>
     37  1.1  rmind #include <limits.h>
     38  1.1  rmind 
     39  1.1  rmind #include <pcap/pcap.h>
     40  1.1  rmind 
     41  1.1  rmind struct npfd_log {
     42  1.1  rmind 	pcap_t *	pcap;
     43  1.1  rmind 	pcap_dumper_t *	dumper;
     44  1.1  rmind };
     45  1.1  rmind 
     46  1.1  rmind npfd_log_t *
     47  1.1  rmind npfd_log_create(unsigned if_idx)
     48  1.1  rmind {
     49  1.1  rmind 	npfd_log_t *ctx;
     50  1.1  rmind 	char errbuf[PCAP_ERRBUF_SIZE];
     51  1.1  rmind 	char ifname[IFNAMSIZ], path[PATH_MAX];
     52  1.1  rmind 	FILE *fp;
     53  1.1  rmind 
     54  1.1  rmind 	if ((ctx = calloc(1, sizeof(npfd_log_t))) == NULL) {
     55  1.1  rmind 		syslog(LOG_ERR, "malloc failed: %m");
     56  1.1  rmind 		return NULL;
     57  1.1  rmind 	}
     58  1.1  rmind 
     59  1.1  rmind 	/*
     60  1.1  rmind 	 * Open a live capture handle in non-blocking mode.
     61  1.1  rmind 	 */
     62  1.1  rmind 	snprintf(ifname, sizeof(ifname), NPFD_NPFLOG "%u", if_idx);
     63  1.1  rmind 	pcap = pcap_create(ifname, errbuf);
     64  1.1  rmind 	if ((ctx->pcap = pcap) == NULL) {
     65  1.1  rmind 		syslog(LOG_ERR, "pcap_create failed: %s", errbuf);
     66  1.1  rmind 		goto err;
     67  1.1  rmind 	}
     68  1.1  rmind 	if (pcap_setnonblock(pcap, 1, errbuf) == -1) {
     69  1.1  rmind 		syslog(LOG_ERR, "pcap_setnonblock failed: %s", errbuf);
     70  1.1  rmind 		goto err;
     71  1.1  rmind 	}
     72  1.1  rmind 	pcap_set_snaplen(pcap, snaplen);
     73  1.1  rmind 
     74  1.1  rmind 	/*
     75  1.1  rmind 	 * Open a log file to write for a given interface and dump there.
     76  1.1  rmind 	 */
     77  1.1  rmind 	snprintf(path, sizeof(path), "%s/%s%s", NPFD_LOG_PATH, ifname, ".pcap");
     78  1.1  rmind 	if ((fp = fopen(path, "w")) == NULL) {
     79  1.1  rmind 		syslog(LOG_ERR, "open failed: %m");
     80  1.1  rmind 		goto err;
     81  1.1  rmind 	}
     82  1.1  rmind 	if ((ctx->dumper = pcap_dump_fopen(pcap, fp)) == NULL) {
     83  1.1  rmind 		syslog(LOG_ERR, "pcap_dump_fopen failed: %s", errbuf);
     84  1.1  rmind 		goto err;
     85  1.1  rmind 	}
     86  1.1  rmind 	return ctx;
     87  1.1  rmind err:
     88  1.1  rmind 	if (!ctx->dumper && fp) {
     89  1.1  rmind 		fclose(fp);
     90  1.1  rmind 	}
     91  1.1  rmind 	npfd_log_destroy(ctx);
     92  1.1  rmind 	return NULL;
     93  1.1  rmind }
     94  1.1  rmind 
     95  1.1  rmind void
     96  1.1  rmind npfd_log_destroy(npfd_log_t *ctx)
     97  1.1  rmind {
     98  1.1  rmind 	if (ctx->dumper)
     99  1.1  rmind 		pcap_dump_close(ctx->dumper);
    100  1.1  rmind 	if (ctx->pcap)
    101  1.1  rmind 		pcap_close(ctx->pcap);
    102  1.1  rmind 	free(ctx);
    103  1.1  rmind }
    104  1.1  rmind 
    105  1.1  rmind int
    106  1.1  rmind npfd_log_getsock(npfd_log_t *ctx)
    107  1.1  rmind {
    108  1.1  rmind 	return pcap_get_selectable_fd(ctx->pcap);
    109  1.1  rmind }
    110  1.1  rmind 
    111  1.1  rmind void
    112  1.1  rmind npfd_log(npfd_log_t *ctx)
    113  1.1  rmind {
    114  1.1  rmind 	pcap_dumper_t *dumper = ctx->dumper;
    115  1.1  rmind 
    116  1.1  rmind 	pcap_dispatch(ctx->pcap, PCAP_NPACKETS, pcap_dump, (uint8_t *)dumper);
    117  1.1  rmind }
    118  1.1  rmind 
    119  1.1  rmind void
    120  1.1  rmind npfd_log_stats(npfd_log_t *ctx)
    121  1.1  rmind {
    122  1.1  rmind 	pcap_t *pcap = ctx->pcap;
    123  1.1  rmind 	struct pcap_stat ps;
    124  1.1  rmind 
    125  1.1  rmind 	if (pcap_stats(pcap, &ps) == -1)
    126  1.1  rmind 		syslog(LOG_ERR, "pcap_stats failed: %s", pcap_geterr(pcap));
    127  1.1  rmind 		return;
    128  1.1  rmind 	}
    129  1.1  rmind 	syslog(LOG_NOTICE, "packet statistics: %u received, %u dropped",
    130  1.1  rmind 	    ps.ps_recv, ps.ps_drop);
    131  1.1  rmind }
    132