Skip to content

Commit

Permalink
Merge pull request #75 from jelu/objs
Browse files Browse the repository at this point in the history
Objects, assert, returns
  • Loading branch information
jelu authored Jun 18, 2018
2 parents e49fdb4 + 0449513 commit 06a11cb
Show file tree
Hide file tree
Showing 161 changed files with 2,203 additions and 7,704 deletions.
6 changes: 0 additions & 6 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
[submodule "src/pcap-thread"]
path = src/pcap-thread
url = https://github.com/DNS-OARC/pcap-thread.git
[submodule "src/omg-dns"]
path = src/omg-dns
url = https://github.com/DNS-OARC/omg-dns.git
[submodule "src/sllq"]
path = src/sllq
url = https://github.com/DNS-OARC/sllq.git
20 changes: 16 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
addons:
apt:
sources:
- ubuntu-toolchain-r-test
- sourceline: 'ppa:dns-oarc/ck'
update: true
packages:
- gcc-5
- libluajit-5.1-dev
- libpcap-dev
- luajit
- liblmdb-dev
- libck-dev
language: c
compiler:
- clang
- gcc
before_install:
- sudo apt-get -qq update
- sudo apt-get install -y libluajit-5.1-dev libpcap-dev libev-dev luajit
install: ./autogen.sh
install:
- if [ "$CC" = "gcc" ]; then export CC="gcc-5"; fi
- ./autogen.sh
script:
- ./configure --enable-warn-all
- make dist
Expand Down
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# You should have received a copy of the GNU General Public License
# along with dnsjit. If not, see <http://www.gnu.org/licenses/>.

ACLOCAL_AMFLAGS = -I m4 -I src/pcap-thread/m4
ACLOCAL_AMFLAGS = -I m4

MAINTAINERCLEANFILES = $(srcdir)/Makefile.in \
$(srcdir)/src/config.h.in~ \
Expand Down
38 changes: 20 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,18 @@ architectures because of LuaJIT.

- [libluajit](http://luajit.org/) 2.0+
- [libpcap](http://www.tcpdump.org/)
- libev
- [liblmdb](https://github.com/LMDB/lmdb)
- [libck](https://github.com/concurrencykit/ck)
- [luajit](http://luajit.org/) (for building)
- automake/autoconf/libtool/pkg-config (for building)

Debian/Ubuntu: `apt-get install libluajit-5.1-dev libpcap-dev libev-dev luajit liblmdb-dev libck-dev`
Debian/Ubuntu: `apt-get install libluajit-5.1-dev libpcap-dev luajit liblmdb-dev libck-dev`

CentOS: `yum install luajit-devel libpcap-devel libev-devel lmdb-devel ck-devel`
CentOS: `yum install luajit-devel libpcap-devel lmdb-devel ck-devel`

FreeBSD: `pkg install luajit libpcap libev lmdb` + manual install of libck
FreeBSD: `pkg install luajit libpcap lmdb` + manual install of libck

OpenBSD: `pkg_add luajit libev` + manual install of libpcap, liblmdb and libck
OpenBSD: `pkg_add luajit` + manual install of libpcap, liblmdb and libck

## Build

Expand Down Expand Up @@ -97,20 +96,23 @@ Following example display the DNS ID found in queries.

```lua
require("dnsjit.core.objects")
local input = require("dnsjit.input.pcapthread").new()
local output = require("dnsjit.filter.coro").new()

output:func(function(filter, object)
local dns = require("dnsjit.core.object.dns").new(object)
if dns and dns:parse() == 0 then
print(dns.id)
local input = require("dnsjit.input.pcap").new()
local layer = require("dnsjit.filter.layer").new()

input:open_offline(arg[2])
layer:producer(input)
local producer, ctx = layer:produce()

while true do
local object = producer(ctx)
if object == nil then break end
if object:type() == "payload" then
local dns = require("dnsjit.core.object.dns").new(object)
if dns and dns:parse() == 0 then
print(dns.id)
end
end
end)

input:open_offline("file.pcap")
input:only_queries(true)
input:receiver(output)
input:run()
end
```

See more examples in the [examples](https://github.com/DNS-OARC/dnsjit/tree/develop/examples) directory.
Expand Down
2 changes: 1 addition & 1 deletion autogen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
# You should have received a copy of the GNU General Public License
# along with dnsjit. If not, see <http://www.gnu.org/licenses/>.

autoreconf --force --install --no-recursive --include=m4 --include=src/pcap-thread/m4
autoreconf --force --install --no-recursive --include=m4
41 changes: 30 additions & 11 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,31 @@ AC_ARG_WITH([extra-ldflags], [AS_HELP_STRING([--with-extra-ldflags=CFLAGS], [Add
AX_EXT
AC_HEADER_TIME
AX_PTHREAD
AX_PCAP_THREAD_PCAP
AC_CHECK_LIB([ev], [ev_now], [], AC_MSG_ERROR([libev not found]))
AC_CHECK_HEADER([ev.h], [AC_CHECK_HEADERS([ev.h])], [AC_CHECK_HEADER([libev/ev.h], [AC_CHECK_HEADERS([libev/ev.h])], [AC_MSG_ERROR([libev header not found])])])
AC_CHECK_LIB([pcap], [pcap_open_live], [], [AC_MSG_ERROR([libpcap not found])])
AC_CHECK_HEADER([pcap/pcap.h], [], [AC_MSG_ERROR([libpcap header not found])])
AC_CHECK_HEADERS([endian.h sys/endian.h machine/endian.h sys/time.h])
AC_CHECK_FUNCS([pcap_create pcap_set_tstamp_precision pcap_set_immediate_mode])
AC_CHECK_FUNCS([pcap_set_tstamp_type pcap_setdirection sched_yield])
AC_CHECK_FUNCS([pcap_open_offline_with_tstamp_precision pcap_activate])
AC_CHECK_TYPES([pcap_direction_t], [], [], [[#include <pcap/pcap.h>]])
AC_CHECK_HEADERS([net/ethernet.h])
AC_CHECK_HEADERS([net/ethertypes.h])
AC_SEARCH_LIBS([clock_gettime],[rt])
AC_CHECK_FUNCS([clock_nanosleep nanosleep])
PKG_CHECK_MODULES([luajit], [luajit >= 2],,AC_MSG_ERROR([luajit v2+ not found]))
PKG_CHECK_MODULES([luajit], [luajit >= 2],, [AC_MSG_ERROR([luajit v2+ not found])])
AC_PATH_PROGS([LUAJIT], [luajit luajit51])
if test "x$ac_cv_path_LUAJIT" = "x"; then
AC_MSG_ERROR([luajit not found])
fi
AC_CHECK_HEADERS([lmdb.h])
AC_CHECK_LIB([lmdb], [mdb_env_create])
AC_CHECK_HEADERS([ck_ring.h],,[
AC_CHECK_HEADERS([ck/ck_ring.h],,[
ac_cv_header_ck_ring_h=; unset ac_cv_header_ck_ring_h
AS_VAR_APPEND(CFLAGS, [" -I/usr/include/ck"])
AC_CHECK_HEADERS([ck_ring.h])
])
PKG_CHECK_MODULES([ck], [ck >= 0], [
AS_VAR_APPEND([CFLAGS], [" $ck_CFLAGS"])
AS_VAR_APPEND([LIBS], [" $ck_LIBS"])
], [
AC_CHECK_HEADERS([ck_ring.h ck_pr.h],, [AC_MSG_ERROR([libck headers not found])])
AC_CHECK_LIB([ck], [ck_array_init],, [AC_MSG_ERROR([libck not found])])
])
AC_CHECK_LIB([ck], [ck_array_init])

# Checks for sizes
AC_CHECK_SIZEOF([void*])
Expand All @@ -79,6 +82,22 @@ AC_CHECK_SIZEOF([pthread_mutex_t],,[#include <pthread.h>])
AC_CHECK_SIZEOF([pthread_cond_t],,[#include <pthread.h>])
AC_CHECK_SIZEOF([struct sockaddr_storage],,[#include <sys/types.h>
#include <sys/socket.h>])
AC_CHECK_SIZEOF([ck_ring_t],,[#if defined(__GNUC__) || defined(__SUNPRO_C)
#include "gcc/ck_cc.h"
#ifdef CK_CC_RESTRICT
#undef CK_CC_RESTRICT
#define CK_CC_RESTRICT __restrict__
#endif
#endif
#include <ck_ring.h>])
AC_CHECK_SIZEOF([ck_ring_buffer_t],,[#if defined(__GNUC__) || defined(__SUNPRO_C)
#include "gcc/ck_cc.h"
#ifdef CK_CC_RESTRICT
#undef CK_CC_RESTRICT
#define CK_CC_RESTRICT __restrict__
#endif
#endif
#include <ck_ring.h>])

# Output Makefiles
AC_CONFIG_FILES([
Expand Down
82 changes: 52 additions & 30 deletions examples/dumpdns-qr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,66 @@ if pcap == nil then
return
end

require("dnsjit.core.objects")
local object = require("dnsjit.core.objects")
local input = require("dnsjit.input.pcap").new()
local layer = require("dnsjit.filter.layer").new()

input:open_offline(pcap)
layer:producer(input)
local producer, ctx = layer:produce()

local c = require("dnsjit.filter.coro").new()
local queries = {}
local responses = {}
c:func(function(c,o)
local dns = require("dnsjit.core.object.dns").new(o)
local pkt = o:cast()
if dns and dns:parse() == 0 then
if dns.qr == 1 then
table.insert(responses, {
src = pkt:src(),
sport = pkt.sport,
dst = pkt:dst(),
dport = pkt.dport,
id = dns.id,
rcode = dns.rcode,
})
else
if dns.questions > 0 and dns:rr_next() == 0 and dns:rr_ok() then
table.insert(queries, {
src = pkt:src(),
sport = pkt.sport,
dst = pkt:dst(),
dport = pkt.dport,

while true do
local obj = producer(ctx)
if obj == nil then break end
if obj:type() == "payload" then
local transport = obj.obj_prev
while transport do
if transport.obj_type == object.CORE_OBJECT_IP or transport.obj_type == object.CORE_OBJECT_IP6 then
break
end
transport = transport.obj_prev
end
local protocol = obj.obj_prev
while protocol do
if protocol.obj_type == object.CORE_OBJECT_UDP or protocol.obj_type == object.CORE_OBJECT_TCP then
break
end
protocol = protocol.obj_prev
end

local dns = require("dnsjit.core.object.dns").new(obj)
if transport and protocol and dns and dns:parse() == 0 then
transport = transport:cast()
protocol = protocol:cast()

if dns.qr == 1 then
table.insert(responses, {
src = transport:source(),
sport = protocol.sport,
dst = transport:destination(),
dport = protocol.dport,
id = dns.id,
qname = dns:rr_label(),
qtype = dns:rr_type(),
rcode = dns.rcode,
})
else
if dns.questions > 0 and dns:rr_next() == 0 and dns:rr_ok() then
table.insert(queries, {
src = transport:source(),
sport = protocol.sport,
dst = transport:destination(),
dport = protocol.dport,
id = dns.id,
qname = dns:rr_label(),
qtype = dns:rr_type(),
})
end
end
end
end
end)

local i = require("dnsjit.input.pcapthread").new()
i:receiver(c)
i:open_offline(pcap)
i:run()
end

print("src", "dst", "id", "rcode", "qname", "qtype")
local q, r
Expand Down
47 changes: 33 additions & 14 deletions examples/dumpdns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,39 @@ if pcap == nil then
return
end

require("dnsjit.core.objects")
local object = require("dnsjit.core.objects")
local input = require("dnsjit.input.pcap").new()
local layer = require("dnsjit.filter.layer").new()

local input = require("dnsjit.input.pcapthread").new()
local output = require("dnsjit.filter.coro").new()
input:open_offline(pcap)
layer:producer(input)
local producer, ctx = layer:produce()

output:func(function(filter, obj)
local pkt = obj:cast()
local dns = require("dnsjit.core.object.dns").new(obj)
if pkt and dns and dns:parse() == 0 then
print(pkt:src()..":"..pkt.sport.." -> "..pkt:dst()..":"..pkt.dport)
dns:print()
end
end)
while true do
local obj = producer(ctx)
if obj == nil then break end
if obj:type() == "payload" then
local transport = obj.obj_prev
while transport do
if transport.obj_type == object.CORE_OBJECT_IP or transport.obj_type == object.CORE_OBJECT_IP6 then
break
end
transport = transport.obj_prev
end
local protocol = obj.obj_prev
while protocol do
if protocol.obj_type == object.CORE_OBJECT_UDP or protocol.obj_type == object.CORE_OBJECT_TCP then
break
end
protocol = protocol.obj_prev
end

input:open_offline(pcap)
input:receiver(output)
input:run()
local dns = require("dnsjit.core.object.dns").new(obj)
if transport and protocol and dns and dns:parse() == 0 then
transport = transport:cast()
protocol = protocol:cast()
print(protocol:type().." "..transport:source()..":"..tonumber(protocol.sport).." -> "..transport:destination()..":"..tonumber(protocol.dport))
dns:print()
end
end
end
37 changes: 24 additions & 13 deletions examples/filter_rcode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,30 @@ if pcap == nil or rcode == nil then
return
end

require("dnsjit.core.objects")
local object = require("dnsjit.core.objects")
local input = require("dnsjit.input.pcap").new()
local layer = require("dnsjit.filter.layer").new()

local input = require("dnsjit.input.pcapthread").new()
local output = require("dnsjit.filter.coro").new()
input:open_offline(pcap)
layer:producer(input)
local producer, ctx = layer:produce()

output:func(function(filter, obj)
local pkt = obj:cast()
local dns = require("dnsjit.core.object.dns").new(obj)
if pkt and dns and dns:parse() == 0 and dns.have_rcode == 1 and dns.rcode == rcode then
print(dns.id, pkt:src().." -> "..pkt:dst())
end
end)
while true do
local obj = producer(ctx)
if obj == nil then break end
if obj:type() == "payload" then
local transport = obj.obj_prev
while transport do
if transport.obj_type == object.CORE_OBJECT_IP or transport.obj_type == object.CORE_OBJECT_IP6 then
break
end
transport = transport.obj_prev
end

input:open_offline(pcap)
input:receiver(output)
input:run()
local dns = require("dnsjit.core.object.dns").new(obj)
if transport and dns and dns:parse() == 0 and dns.have_rcode == 1 and dns.rcode == rcode then
transport = transport:cast()
print(dns.id, transport:source().." -> "..transport:destination())
end
end
end
Loading

0 comments on commit 06a11cb

Please sign in to comment.