-
Notifications
You must be signed in to change notification settings - Fork 12
/
pcapx.lua
53 lines (43 loc) · 1.18 KB
/
pcapx.lua
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
--[[-
pcapx - extensions to pcap
]]
local pcap = require"pcap"
require"net"
local function NOP()
end
--[[-
- pcap.recode(incap, outcap, progress, debug)
- incap, name of input pcap
- outcap, name of output pcap, default to "recoded-"..incap
- progress, pass print-like function to receive progress messages,
defaults to no progress
- debug, as above, but for debug output
Re-encode file.pcap as recoded-file.pcap, using print()
to report progress:
pcap.recode("file.pcap", nil, print)
]]
function pcap.recode(incap, outcap, progress, debug)
progress = progress or NOP
debug = debug or NOP
if not outcap then
outcap = "recoded-"..incap
end
os.remove(outcap)
local cap = assert(pcap.open_offline(incap))
local dmp = assert(cap:dump_open(outcap))
local n = assert(net.init())
local i = 0
for pkt, time, len in cap.next, cap do
i = i + 1
progress("packet", i, "wirelen", len, "timestamp", time, os.date("!%c", time))
assert(n:clear())
assert(n:decode_eth(pkt))
assert(dmp:dump(n:block(), time, len))
debug(n:dump())
end
dmp:close()
cap:close()
n:destroy()
return outcap
end
return pcap