Skip to content

Commit

Permalink
libpcap
Browse files Browse the repository at this point in the history
- `input.pcap`:
  - Issue DNS-OARC#44: Add `create()` and `activate()`
  - Fix module name in usage documentation
  • Loading branch information
jelu committed Aug 7, 2019
1 parent 4d8270e commit 6c4fdcf
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
38 changes: 38 additions & 0 deletions src/input/pcap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
2 changes: 2 additions & 0 deletions src/input/pcap.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 16 additions & 1 deletion src/input/pcap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 6c4fdcf

Please sign in to comment.