Skip to content

Commit 4788c91

Browse files
committed
Optional tools and extensions for working with pcaps.
1 parent 2799018 commit 4788c91

File tree

4 files changed

+64
-26
lines changed

4 files changed

+64
-26
lines changed

Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,21 @@ prefix=/usr/local
1616

1717
SODIR = $(DESTDIR)/$(prefix)/lib/lua/5.1/
1818

19+
LIBDIR = $(DESTDIR)/$(prefix)/share/lua/5.1/
20+
BINDIR = $(DESTDIR)/$(prefix)/bin/
21+
1922
.PHONY: install
2023
install: $(BINDING)
2124
mkdir -p $(SODIR)
2225
install -t $(SODIR) $(BINDING)
2326

27+
.PHONY: install-all
28+
install-all: install
29+
mkdir -p $(LIBDIR)
30+
mkdir -p $(BINDIR)
31+
install -t $(LIBDIR) pcapx.lua
32+
install -t $(BINDIR) pcap-recode pcap-dump pcap-split
33+
2434
CWARNS = -Wall \
2535
-pedantic \
2636
-Wcast-align \

netutil.lua

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,6 @@ function assertmostlyeql(threshold, s0, s1)
7272
assert(diff <= threshold, diff.." is less than threshold "..threshold)
7373
end
7474

75-
function pcaprecode(incap, outcap)
76-
if not outcap then
77-
outcap = "recoded-"..incap
78-
end
79-
os.remove(outcap)
80-
81-
local cap = assert(pcap.open_offline(incap))
82-
local dmp = assert(cap:dump_open(outcap))
83-
local n = assert(net.init())
84-
local i = 0
85-
for pkt, time, len in cap.next, cap do
86-
i = i + 1
87-
print("packet", i, "wirelen", len, "timestamp", time, os.date("!%c", time))
88-
assert(n:clear())
89-
assert(n:decode_eth(pkt))
90-
assert(dmp:dump(n:block(), time, len))
91-
--print(n:dump())
92-
end
93-
dmp:close()
94-
cap:close()
95-
n:destroy()
96-
return outcap
97-
end
98-
9975
function assertpcapsimilar(threshold, file0, file1)
10076
local n0 = assert(net.init())
10177
local n1 = assert(net.init())

pcap-recode

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env lua
22

3-
require"netutil"
3+
require"pcapx"
44

5-
pcaprecode(arg[1], arg[2])
5+
pcap.recode(arg[1], arg[2])
66

pcapx.lua

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
--[[-
2+
pcapx - extensions to pcap
3+
4+
]]
5+
6+
require"pcap"
7+
require"net"
8+
9+
local function NOP()
10+
end
11+
12+
--[[-
13+
- pcap.recode(incap, outcap, progress, debug)
14+
15+
- incap, name of input pcap
16+
- outcap, name of output pcap, default to "recoded-"..incap
17+
- progress, pass print-like function to receive progress messages,
18+
defaults to no progress
19+
- debug, as above, but for debug output
20+
21+
Re-encode file.pcap as recoded-file.pcap, using print()
22+
to report progress:
23+
24+
pcap.recode("file.pcap", nil, print)
25+
]]
26+
function pcap.recode(incap, outcap, progress, debug)
27+
progress = progress or NOP
28+
debug = debug or NOP
29+
30+
if not outcap then
31+
outcap = "recoded-"..incap
32+
end
33+
os.remove(outcap)
34+
35+
local cap = assert(pcap.open_offline(incap))
36+
local dmp = assert(cap:dump_open(outcap))
37+
local n = assert(net.init())
38+
local i = 0
39+
for pkt, time, len in cap.next, cap do
40+
i = i + 1
41+
progress("packet", i, "wirelen", len, "timestamp", time, os.date("!%c", time))
42+
assert(n:clear())
43+
assert(n:decode_eth(pkt))
44+
assert(dmp:dump(n:block(), time, len))
45+
debug(n:dump())
46+
end
47+
dmp:close()
48+
cap:close()
49+
n:destroy()
50+
return outcap
51+
end
52+

0 commit comments

Comments
 (0)