summaryrefslogtreecommitdiffstats
path: root/examples/count-pkts-per-ip.lua
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2021-03-13 07:54:12 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2021-03-13 07:54:12 +0000
commit4754ed45b607e82450a5e31fea1da3ba61433b04 (patch)
tree3554490bdc003e6004f605abe41929cdf98b0651 /examples/count-pkts-per-ip.lua
parentInitial commit. (diff)
downloaddnsjit-4754ed45b607e82450a5e31fea1da3ba61433b04.tar.xz
dnsjit-4754ed45b607e82450a5e31fea1da3ba61433b04.zip
Adding upstream version 1.1.0+debian.upstream/1.1.0+debian
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'examples/count-pkts-per-ip.lua')
-rwxr-xr-xexamples/count-pkts-per-ip.lua50
1 files changed, 50 insertions, 0 deletions
diff --git a/examples/count-pkts-per-ip.lua b/examples/count-pkts-per-ip.lua
new file mode 100755
index 0000000..9a6908e
--- /dev/null
+++ b/examples/count-pkts-per-ip.lua
@@ -0,0 +1,50 @@
+#!/usr/bin/env dnsjit
+-- count-pkts-per-ip.lua: count number of packets received from each IP/IPv6 address
+
+local input = require("dnsjit.input.pcap").new()
+local layer = require("dnsjit.filter.layer").new()
+local object = require("dnsjit.core.objects")
+local ip = require("dnsjit.lib.ip")
+local trie = require("dnsjit.lib.trie").new("uint64_t", true)
+local getopt = require("dnsjit.lib.getopt").new({})
+
+local pcap = unpack(getopt:parse())
+if pcap == nil then
+ print("usage: "..arg[1].." <pcap>")
+end
+
+-- Set up input
+input:open_offline(pcap)
+layer:producer(input)
+local produce, pctx = layer:produce()
+
+-- Read input and count packets
+while true do
+ local obj = produce(pctx)
+ if obj == nil then break end
+ local pkt = obj:cast_to(object.IP) or obj:cast_to(object.IP6)
+
+ if pkt ~= nil then
+ local iplen = 4
+ if pkt:type() == "ip6" then
+ iplen = 16
+ end
+
+ local node = trie:get_ins(pkt.src, iplen)
+ node:set(node:get() + 1)
+ end
+end
+
+-- Print statistics
+local iter = trie:iter()
+local node = iter:node()
+
+while node ~= nil do
+ local npkts = tonumber(node:get())
+ local key = node:key()
+ local ipstr = ip.tostring(key, true)
+
+ print(ipstr.." sent "..npkts.." packets")
+ iter:next()
+ node = iter:node()
+end