Skip to content

Commit

Permalink
Updated lua bindings, they are a mess, but at least they are here.
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-github committed Feb 10, 2010
1 parent 4d6d155 commit 96bc726
Show file tree
Hide file tree
Showing 11 changed files with 1,997 additions and 95 deletions.
1 change: 1 addition & 0 deletions lua/.gitignore
@@ -0,0 +1 @@
*.so
76 changes: 61 additions & 15 deletions lua/Makefile
@@ -1,25 +1,71 @@

.PHONY: default build

default: build

BINDING=net.so pcap.so

# OS X
CC = MACOSX_DEPLOYMENT_TARGET="10.3" gcc
LDFLAGS = -fno-common -bundle -undefined dynamic_lookup

# Linux
CC = gcc `libnet-config --cflags --defines` `dnet-config --cflags`
LDFLAGS = -fPIC -fno-common -shared `dnet-config --libs` `libnet-config --libs` -llua5.1

BINDING += nfq.so

build: $(BINDING)

PREFIX=/usr/local

SODIR = $(DESTDIR)$(PREFIX)/lib/lua/5.1/

.PHONY: install
install: $(BINDING)
mkdir -p $(SODIR)
install -t $(SODIR) $(BINDING)

.PHONY: net pcap nfq

net: net.so
./recoding-test
sudo ./net-test

pcap: pcap.so
#./pcap-test

nfq: nfq.so
#sudo ./nfq-test

# Example:
# cc -Wall -Werror -g -I wurldtech/rst `libnet-config --cflags --defines`
# `dnet-config --cflags` -O0 -DNDEBUG -fPIC -fno-common -shared
# -I/usr/include/lua5.1 -o wurldtech/lgram/net.so wurldtech/lgram/net.c -lrt
# -lm `dnet-config --libs` `libnet-config --libs` -llua5.1

CWARNS = -Wall \
-pedantic \
-Wcast-align \
-Wnested-externs \
-Wpointer-arith \
-Wshadow \
-Wwrite-strings
-pedantic \
-Wcast-align \
-Wnested-externs \
-Wpointer-arith \
-Wshadow \
-Wwrite-strings

COPT = -O2 -DNDEBUG -g
CFLAGS = $(CWARNS) -ansi -I$(LUADIR) -fno-common -bundle -undefined dynamic_lookup
CC = gcc
LUADIR = /usr/include/lua5.1
CFLAGS = $(CWARNS) -I$(LUADIR) $(LDFLAGS)
LDLIBS=-ldnet -lnet

.PHONY: net.so
net.so: net.c
MACOSX_DEPLOYMENT_TARGET="10.3" $(CC) $(COPT) $(CFLAGS) -o $@ $< $(LDLIBS)
CC.SO := $(CC) $(COPT) $(CFLAGS)

deb: net.c
$(CC) -g $(CFLAGS) net.c; touch deb; rm -f opt
%.so: %.c
$(CC.SO) -o $@ $< $(LDLIBS)

opt: net.c
$(CC) $(COPT) $(CFLAGS) net.c; touch opt; rm -f deb
net.so: net.c
pcap.so: pcap.c
nfq.so: nfq.c
nfq.so: LDLIBS+=-lnetfilter_queue

test: test.lua net.so
lua test.lua
Expand Down
149 changes: 145 additions & 4 deletions lua/net-test
@@ -1,6 +1,147 @@
#!/usr/bin/env lua
#!/usr/bin/env lua5.1

dofile"test-misc.lua"
dofile"test-ipv4.lua"
dofile"test-decode.lua"
require"net"
require"netutil"

hex_dump = h

local function check_ipv4()
local n = net.init("link", "eth0")
n:ipv4{src="1.2.3.1", dst="1.2.3.2", protocol=2, len=20+4, options="AAAA"}
n:eth{src="01:02:03:04:05:01", dst="01:02:03:04:05:02"}

--ok=pcall(n.ipv4, n, {src="1.2.3.1", dst="1.2.3.2", protocol=2, len=20+4, options="AAAA"})
--assert(not ok, "net:ipv4 fails to detect incorrect usage of IPv4 options")
end

local function check_ptag()
local n = net.init("link", "eth0")
eth = n:eth{src="01:02:03:04:05:01", dst="01:02:03:04:05:02"}
ok=pcall(n.ipv4, n, {src="1.2.3.1", dst="1.2.3.2", protocol=2, len=20+4, options="AAAA", ptag = eth})
assert(not ok, "net:ipv4 fails to detect specifying a ptag when creating IPv4 options")
end

local function check_first()
local n = net.init("link", "eth0")
ok=pcall(n.ipv4, n, {src="1.2.3.1", dst="1.2.3.2", protocol=2, len=20+4, options="AAAA"})
eth = n:eth{src="01:02:03:04:05:01", dst="01:02:03:04:05:02"}
assert(ok, "net:ipv4 fails to construct ipv4 options correctly")
end

n = net.init("link", "eth0")

print(q(net.pton("01:02:03:04:05:06")))
print(q(net.pton("1.2.3.4")))
print(q(net.pton("::1")))

n:udp{src=3, dst=5, payload="XXX"}
ipv4 = n:ipv4{src="1.2.3.1", dst="1.2.3.2", protocol=17, len=20+8+3}
n:eth{src="01:02:03:04:05:01", dst="01:02:03:04:05:02"}

print(n:dump())

n:write()

b1 = n:block()
print(#b1, q(b1))

n:ipv4{src="1.2.3.1", dst="1.2.3.2", protocol=17, len=20+8+3, ptag=ipv4}

--print(q({src="1.2.3.1", dst="1.2.3.2", protocol=17, len=20+8+3, ptag=ipv4}))

print(n:dump())

b2 = n:block()

print("b1=", q(b1))
print("b2=", q(b2))

assert(b1 == b2)

check_ipv4()
check_ptag()
check_first()

do
print"\n\n# decode ipv4"

local n = net.init("raw4", "eth0")

n:udp{src=1, dst=2, payload=" "}
n:ipv4{src="1.2.3.1", dst="1.2.3.2", protocol=17, len=20+4, options="AAAA"}

local b0 = n:block()

print"= constructed:"
print(n:dump())
hex_dump(b0)

n:clear()
print(n:dump())

print"= decoded:"
assert(n:decode_ipv4(b0))

local ip1 = n:block(n:tag_below())
local b1 = n:block()
print(n:dump())
hex_dump(b1)
print""

assert(b0 == b1)

local bot = assert(n:tag_below())
local top = assert(n:tag_above())

print("bot", bot, "top", top)
assert(bot == 3)
assert(n:tag_below(bot) == nil)
assert(n:tag_above(bot) == 2)

assert(top == 1)
assert(n:tag_above(top) == nil)
assert(n:tag_below(top) == 2)

assert(n:tag_type(bot) == "ipv4", n:tag_type(bot))
assert(n:tag_type(top) == "udp", n:tag_type(top))

udpt = n:get_udp()
udpt.payload = "\0"
assert(udpt.ptag == top)
assert(n:udp(udpt))

local b2 = n:block()
print(n:dump())
hex_dump(b2)

-- everything up to the checksum should be the same
assert(b1:sub(1, 20+4+6) == b2:sub(1, 20+4+6))
assert(b1:sub(20+4+7, 20+4+8) ~= b2:sub(20+4+7, 20+4+8))

assert(#n:block(n:tag_above()) == (8+1))
assert(n:block(n:tag_below()) == ip1)

print"+pass"
end

do
print"\n\n# pblock dump"

local n = net.init("raw4", "eth0")

n:udp{src=1, dst=2, payload=" "}
n:ipv4{src="1.2.3.1", dst="1.2.3.2", protocol=17, len=20+4, options="AAAA"}

local ptag = n:tag_above()
while ptag do
--print("ptag", ptag)
local pblock = n:pblock(ptag)
local buf = pblock.buf
pblock.buf = nil
print(pblock, h(buf))
ptag = pblock.next
end
end

dofile"recoding-test"

0 comments on commit 96bc726

Please sign in to comment.