1*dd49816bSKristof Provost /*-
2*dd49816bSKristof Provost * SPDX-License-Identifier: BSD-2-Clause
3*dd49816bSKristof Provost *
4*dd49816bSKristof Provost * Copyright (c) 2025 Rubicon Communications, LLC (Netgate)
5*dd49816bSKristof Provost *
6*dd49816bSKristof Provost * Redistribution and use in source and binary forms, with or without
7*dd49816bSKristof Provost * modification, are permitted provided that the following conditions
8*dd49816bSKristof Provost * are met:
9*dd49816bSKristof Provost * 1. Redistributions of source code must retain the above copyright
10*dd49816bSKristof Provost * notice, this list of conditions and the following disclaimer.
11*dd49816bSKristof Provost * 2. Redistributions in binary form must reproduce the above copyright
12*dd49816bSKristof Provost * notice, this list of conditions and the following disclaimer in the
13*dd49816bSKristof Provost * documentation and/or other materials provided with the distribution.
14*dd49816bSKristof Provost *
15*dd49816bSKristof Provost * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*dd49816bSKristof Provost * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*dd49816bSKristof Provost * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*dd49816bSKristof Provost * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*dd49816bSKristof Provost * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*dd49816bSKristof Provost * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*dd49816bSKristof Provost * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*dd49816bSKristof Provost * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*dd49816bSKristof Provost * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*dd49816bSKristof Provost * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*dd49816bSKristof Provost * SUCH DAMAGE.
26*dd49816bSKristof Provost *
27*dd49816bSKristof Provost */
28*dd49816bSKristof Provost
29*dd49816bSKristof Provost #include <err.h>
30*dd49816bSKristof Provost #include <stdio.h>
31*dd49816bSKristof Provost #include <pcap.h>
32*dd49816bSKristof Provost #include <unistd.h>
33*dd49816bSKristof Provost
34*dd49816bSKristof Provost static void
callback(u_char * arg __unused,const struct pcap_pkthdr * hdr __unused,const unsigned char * bytes __unused)35*dd49816bSKristof Provost callback(u_char *arg __unused, const struct pcap_pkthdr *hdr __unused,
36*dd49816bSKristof Provost const unsigned char *bytes __unused)
37*dd49816bSKristof Provost {
38*dd49816bSKristof Provost }
39*dd49816bSKristof Provost
40*dd49816bSKristof Provost int
main(int argc,const char ** argv)41*dd49816bSKristof Provost main(int argc, const char **argv)
42*dd49816bSKristof Provost {
43*dd49816bSKristof Provost pcap_t *pcap;
44*dd49816bSKristof Provost const char *interface;
45*dd49816bSKristof Provost char errbuf[PCAP_ERRBUF_SIZE] = { 0 };
46*dd49816bSKristof Provost int ret;
47*dd49816bSKristof Provost
48*dd49816bSKristof Provost if (argc != 2)
49*dd49816bSKristof Provost err(1, "Usage: %s <interface>\n", argv[0]);
50*dd49816bSKristof Provost
51*dd49816bSKristof Provost interface = argv[1];
52*dd49816bSKristof Provost
53*dd49816bSKristof Provost pcap = pcap_create(interface, errbuf);
54*dd49816bSKristof Provost if (! pcap)
55*dd49816bSKristof Provost perror("Failed to pcap interface");
56*dd49816bSKristof Provost
57*dd49816bSKristof Provost ret = pcap_set_snaplen(pcap, 86);
58*dd49816bSKristof Provost if (ret != 0)
59*dd49816bSKristof Provost perror("Failed to set snaplen");
60*dd49816bSKristof Provost
61*dd49816bSKristof Provost ret = pcap_set_timeout(pcap, 100);
62*dd49816bSKristof Provost if (ret != 0)
63*dd49816bSKristof Provost perror("Failed to set timeout");
64*dd49816bSKristof Provost
65*dd49816bSKristof Provost ret = pcap_activate(pcap);
66*dd49816bSKristof Provost if (ret != 0)
67*dd49816bSKristof Provost perror("Failed to activate");
68*dd49816bSKristof Provost
69*dd49816bSKristof Provost /* So we have two readers on one /dev/bpf fd */
70*dd49816bSKristof Provost fork();
71*dd49816bSKristof Provost
72*dd49816bSKristof Provost printf("Interface open\n");
73*dd49816bSKristof Provost pcap_loop(pcap, 0, callback, NULL);
74*dd49816bSKristof Provost
75*dd49816bSKristof Provost return (0);
76*dd49816bSKristof Provost }
77