-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathpflua-compile
executable file
·66 lines (54 loc) · 1.85 KB
/
pflua-compile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env luajit
-- -*- lua -*-
package.path = package.path .. ";../src/?.lua"
local pf = require("pf")
local bpf = require("pf.bpf")
local match = require("pf.match")
local utils = require("pf.utils")
function usage()
local content = [=[
Usage: pflua-compile [-O0] [--bpf-asm | --bpf-lua | --lua | --native | --match] <expression>
Options:
--bpf-asm Print libpcap-generated BPF asm code for the pflang <expression>
--bpf-lua Print Lua code compiled from BPF for the pflang <expression>
--lua Print Lua code compiled directly for the pflang <expression> (DEFAULT)
--native Print dynasm-generated asm code for the pflang <expression>
--match Print Lua code compiled from the pfmatch <expression>
-O0 Disable optimizations. (Optimizations are on by default) ]=]
print(content);
os.exit()
end
-- Print help
if #arg == 0 then
usage()
end
local flags = utils.set(...)
-- Print help
if flags["--help"] or flags["-h"] then
usage()
end
-- No code-generation flag defined
if (not(flags["--bpf-asm"] or flags["--bpf-lua"] or flags["--lua"] or flags["--native"] or flags['--match'])) then
-- Default action
flags["--lua"] = true
end
local optimize = true
if flags["-O0"] then optimize = false end
local filter = arg[#arg]
if flags["--bpf-asm"] then
print(pf.compile_filter(filter, {libpcap=true, source=true,
optimize=optimize}))
end
if flags["--bpf-lua"] then
print(pf.compile_filter(filter, {bpf=true, source=true,
optimize=optimize}))
end
if flags["--lua"] then
print(pf.compile_filter(filter, {source=true, optimize=optimize}))
end
if flags["--native"] then
print(pf.compile_filter(filter, {native=true, source=true, optimize=optimize}))
end
if flags["--match"] then
print(match.compile(filter, {source=true, optimize=optimize}))
end