Root/package/wprobe/src/filter/gen_filter.pl

1#!/usr/bin/perl
2use strict;
3
4# helpers for custom packet format
5# bytes 0-7 are used by a dummy radiotap header
6my $WLAN_LEN = "radio[8:2]";
7my $SNR = "radio[10:1]";
8my $DEFAULT = undef;
9
10my $MAGIC = "WPFF";
11my $VERSION = 1; # filter binary format version
12my $HDRLEN = 3; # assumed storage space for custom fields
13
14my $output = "filter.bin";
15my $config = {
16    "packetsize" => [
17        [ "small", "$WLAN_LEN < 250" ],
18        [ "medium", "$WLAN_LEN < 800" ],
19        [ "big", $DEFAULT ],
20    ],
21    "snr" => [
22        [ "low", "$SNR < 10" ],
23        [ "medium", "$SNR < 20" ],
24        [ "high", $DEFAULT ],
25    ],
26    "type" => [
27        [ "beacon", "type mgt subtype beacon" ],
28        [ "data", "type data subtype data" ],
29        [ "qosdata", "type data subtype qos-data" ],
30        [ "other", "$DEFAULT" ]
31    ]
32};
33
34sub escape_q($) {
35    my $str = shift;
36    $str =~ s/'/'\\''/g;
37    return $str;
38}
39
40my $GROUPS = scalar(keys %$config);
41open OUTPUT, ">$output" or die "Cannot open output file: $!\n";
42print OUTPUT pack("a4CCn", $MAGIC, $VERSION, $HDRLEN, $GROUPS);
43
44foreach my $groupname (keys %$config) {
45    my $default = 0;
46    my $group = $config->{$groupname};
47    print OUTPUT pack("a32N", $groupname, scalar(@$group));
48    foreach my $filter (@$group) {
49        if (!$filter->[1]) {
50            $default > 0 and print "Cannot add more than one default filter per group: $groupname -> ".$filter->[0]."\n";
51            print OUTPUT pack("a32N", $filter->[0], 0);
52            $default++;
53        } else {
54            open FILTER, "./pfc '".escape_q($filter->[0])."' '".escape_q($filter->[1])."' |"
55                or die "Failed to run filter command for '".$filter->[0]."': $!\n";
56            while (<FILTER>) {
57                print OUTPUT $_;
58            }
59            close FILTER;
60            $? and die "Filter '".$filter->[0]."' did not compile.\n";
61        }
62    }
63}
64

Archive Download this file



interactive