From 6c4fdcf71bfa653f58deda97b6d7e3c3cdd8f98c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jerry=20Lundstr=C3=B6m?= Date: Wed, 7 Aug 2019 13:11:40 +0200 Subject: [PATCH] libpcap - `input.pcap`: - Issue #44: Add `create()` and `activate()` - Fix module name in usage documentation --- src/input/pcap.c | 38 ++++++++++++++++++++++++++++++++++++++ src/input/pcap.hh | 2 ++ src/input/pcap.lua | 17 ++++++++++++++++- 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/input/pcap.c b/src/input/pcap.c index 80f93cb7..c3e937f2 100644 --- a/src/input/pcap.c +++ b/src/input/pcap.c @@ -55,6 +55,44 @@ void input_pcap_destroy(input_pcap_t* self) } } +int input_pcap_create(input_pcap_t* self, const char* source) +{ + char errbuf[PCAP_ERRBUF_SIZE] = { 0 }; + mlassert_self(); + lassert(source, "source is nil"); + + if (self->pcap) { + lfatal("already opened"); + } + + if (!(self->pcap = pcap_create(source, errbuf))) { + lcritical("pcap_create(%s) error: %s", source, errbuf); + return -1; + } + + self->snaplen = pcap_snapshot(self->pcap); + self->linktype = pcap_datalink(self->pcap); + self->is_swapped = 0; + + self->prod_pkt.snaplen = self->snaplen; + self->prod_pkt.linktype = self->linktype; + self->prod_pkt.is_swapped = self->is_swapped; + + ldebug("pcap v%u.%u snaplen:%lu %s", pcap_major_version(self->pcap), pcap_minor_version(self->pcap), self->snaplen, self->is_swapped ? " swapped" : ""); + + return 0; +} + +int input_pcap_activate(input_pcap_t* self) +{ + mlassert_self(); + if (!self->pcap) { + lfatal("no PCAP opened"); + } + + return pcap_activate(self->pcap); +} + int input_pcap_open_offline(input_pcap_t* self, const char* file) { char errbuf[PCAP_ERRBUF_SIZE] = { 0 }; diff --git a/src/input/pcap.hh b/src/input/pcap.hh index da48741d..775ea189 100644 --- a/src/input/pcap.hh +++ b/src/input/pcap.hh @@ -47,6 +47,8 @@ core_log_t* input_pcap_log(); void input_pcap_init(input_pcap_t* self); void input_pcap_destroy(input_pcap_t* self); +int input_pcap_create(input_pcap_t* self, const char* source); +int input_pcap_activate(input_pcap_t* self); int input_pcap_open_offline(input_pcap_t* self, const char* file); int input_pcap_loop(input_pcap_t* self, int cnt); int input_pcap_dispatch(input_pcap_t* self, int cnt); diff --git a/src/input/pcap.lua b/src/input/pcap.lua index 9958e98b..d884c0ae 100644 --- a/src/input/pcap.lua +++ b/src/input/pcap.lua @@ -18,7 +18,7 @@ -- dnsjit.input.pcap -- Read input from an interface or PCAP file using libpcap --- local input = require("dnsjit.input.pcapthread").new() +-- local input = require("dnsjit.input.pcap").new() -- input:open_offline("file.pcap") -- input:receiver(filter_or_output) -- input:run() @@ -64,6 +64,21 @@ function Pcap:produce() return C.input_pcap_producer(self.obj), self.obj end +-- Open a live packet capture on +-- .IR source , +-- which is an interface name or "any" (Linux) / "all" (BSD). +-- Must be activated before use. +function Pcap:create(source) + return C.input_pcap_create(self.obj, source) +end + +-- Activate a live packet capture, see +-- .BR pcap_activate (3pcap) +-- for more information and possible return values. +function Pcap:activate() + return C.input_pcap_activate(self.obj) +end + -- Open a PCAP file for processing. -- Returns 0 on success. function Pcap:open_offline(file)