ifpps: Remove unnecessary memset()
[netsniff-ng.git] / bpf_comp.c
blob8946621a40243a58ecf4336e98a59d35d120e1d5
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2013 Daniel Borkmann.
4 * Subject to the GPL, version 2.
5 */
7 #define NEED_TCPDUMP_LIKE_FILTER
9 #include <pcap.h>
10 #include <linux/filter.h>
12 #include "xmalloc.h"
13 #include "config.h"
14 #include "bpf.h"
15 #include "die.h"
17 void bpf_try_compile(const char *rulefile, struct sock_fprog *bpf, uint32_t link_type)
19 int i, ret;
20 const struct bpf_insn *ins;
21 struct sock_filter *out;
22 struct bpf_program _bpf;
24 ret = pcap_compile_nopcap(65535, link_type, &_bpf, rulefile, 1, 0xffffffff);
25 if (ret < 0)
26 panic("Cannot compile filter %s\n", rulefile);
28 bpf->len = _bpf.bf_len;
29 bpf->filter = xrealloc(bpf->filter, 1, bpf->len * sizeof(*out));
31 for (i = 0, ins = _bpf.bf_insns, out = bpf->filter; i < bpf->len;
32 ++i, ++ins, ++out) {
33 out->code = ins->code;
34 out->jt = ins->jt;
35 out->jf = ins->jf;
36 out->k = ins->k;
38 if (out->code == 0x06 && out->k > 0)
39 out->k = 0xFFFFFFFF;
42 pcap_freecode(&_bpf);
44 if (__bpf_validate(bpf) == 0)
45 panic("This is not a valid BPF program!\n");