diff options
author | Sam Roberts <vieuxtech@gmail.com> | 2011-08-09 13:12:02 -0700 |
---|---|---|
committer | Sam Roberts <vieuxtech@gmail.com> | 2011-08-09 13:12:02 -0700 |
commit | cf3173abf313690527dad3217e7124218c1973fe (patch) | |
tree | f5f7f8b73a349273838f519d3e309b94119063ba /lua | |
parent | ea04fca6d2b03e605a29fbcccf807f9d82ec16fe (diff) | |
download | libnet-cf3173abf313690527dad3217e7124218c1973fe.tar.gz |
Add n:igmp(), a function for building igmp protocol blocks.
Diffstat (limited to 'lua')
-rw-r--r-- | lua/net.c | 26 | ||||
-rw-r--r-- | lua/test-igmp.lua | 47 |
2 files changed, 73 insertions, 0 deletions
@@ -558,6 +558,31 @@ static int lnet_data (lua_State *L) } /*- +-- ptag = net:igmp{type=NUM, code=NUM, ip=IP, payload=STR, ptag=int} + +Build IGMP packet inside net context. + +ptag is optional, defaults to creating a new protocol block +*/ +static int lnet_igmp (lua_State *L) +{ + libnet_t* ud = checkudata(L); + int type = v_arg_integer(L, 2, "type"); + int code = v_arg_integer(L, 2, "code"); + const char* ip = v_arg_string(L, 2, "ip"); + uint32_t ip_n = check_ip_pton(L, ip, "ip"); + uint32_t payloadsz = 0; + const uint8_t* payload = checkpayload(L, 2, &payloadsz); + int cksum = 0; + int ptag = lnet_arg_ptag(L, ud, 2, LIBNET_PBLOCK_IGMP_H); + + ptag = libnet_build_igmp(type, code, cksum, ip_n, payload, payloadsz, ud, ptag); + check_error(L, ud, ptag); + lua_pushinteger(L, ptag); + return 1; +} + +/*- -- ptag = net:udp{src=NUM, dst=NUM, len=NUM, payload=STR, ptag=int} Build UDP packet inside net context. @@ -1364,6 +1389,7 @@ static const luaL_reg net_methods[] = {"fd", lnet_getfd}, {"device", lnet_getdevice}, {"data", lnet_data}, + {"igmp", lnet_igmp}, {"udp", lnet_udp}, {"get_udp", lnet_get_udp}, {"tcp", lnet_tcp}, diff --git a/lua/test-igmp.lua b/lua/test-igmp.lua new file mode 100644 index 0000000..cb8e810 --- /dev/null +++ b/lua/test-igmp.lua @@ -0,0 +1,47 @@ +dofile"setup.lua" + +test("+igmp", function() + local n = net.init() + local igmpt = {type=3, code=5, ip="1.2.3.4"} + local ipt = {src="1.2.3.4", dst="5.6.7.8", protocol=2} + local etht = {src="01:02:03:01:02:03", dst="04:05:06:04:05:06"} + local pkt = "0405060405060102030102030800".. -- eth + "4500001c0000000040026acd0102030405060708".. -- ip + "0305f6f604030201" -- igmp + + print" w/no-payload" + + local gtag = n:igmp(igmpt) + local itag = n:ipv4(ipt) + local etag = n:eth(etht) + local b = n:block() + + dump(n) + + print("want", pkt) + print("have", h(b)) + + assert(pkt == h(b), h(b)) + + print"ok" + + print" w/payload" + + igmpt.payload = "\0\0\0" + igmpt.ptag = gtag + + local gtag = n:igmp(igmpt) + local b = n:block() + + dump(n) + + print("want", pkt) + print("have", h(b)) + + assert(pkt.."000000" == h(b), h(b)) + + print"ok" + + n:destroy() +end) + |