| 1 | /* |
| 2 | * Small pcap precompiler |
| 3 | * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 |
| 7 | * as published by the Free Software Foundation |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | |
| 15 | #include <sys/types.h> |
| 16 | #include <sys/time.h> |
| 17 | #include <string.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <pcap.h> |
| 20 | |
| 21 | int main (int argc, char ** argv) |
| 22 | { |
| 23 | struct bpf_program filter; |
| 24 | pcap_t *pc; |
| 25 | int i; |
| 26 | |
| 27 | if (argc != 2) |
| 28 | { |
| 29 | printf ("Usage: %s <expression>\n", argv[0]); |
| 30 | return 1; |
| 31 | } |
| 32 | |
| 33 | pc = pcap_open_dead(DLT_EN10MB, 1500); |
| 34 | if (pcap_compile(pc, &filter, argv[1], 1, 0) != 0) { |
| 35 | printf("error in active-filter expression: %s\n", pcap_geterr(pc)); |
| 36 | return 1; |
| 37 | } |
| 38 | |
| 39 | printf("/* precompiled expression: %s */\n\n" |
| 40 | "static struct bpf_insn pktfilter_insns[] = {\n", |
| 41 | argv[1]); |
| 42 | |
| 43 | for (i = 0; i < filter.bf_len; i++) { |
| 44 | struct bpf_insn *in = &filter.bf_insns[i]; |
| 45 | printf("\t{ .code = 0x%04x, .jt = 0x%02x, .jf = 0x%02x, .k = 0x%08x },\n", in->code, in->jt, in->jf, in->k); |
| 46 | } |
| 47 | printf("};\n\n" |
| 48 | "static struct bpf_program pktfilter = {\n" |
| 49 | "\t.bf_len = %d,\n" |
| 50 | "\t.bf_insns = pktfilter_insns,\n" |
| 51 | "};\n", filter.bf_len); |
| 52 | return 0; |
| 53 | |
| 54 | } |
| 55 | |