From: Miek Gieben Date: Mon, 8 Aug 2005 16:32:57 +0000 (+0000) Subject: more code and logging and randomness X-Git-Tag: release-1.0.0~348 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=663ee2bc396be99e118e8b33c0d2de5ae3cc4be2;p=thirdparty%2Fldns.git more code and logging and randomness --- diff --git a/tests/lua-rns.c b/tests/lua-rns.c index 955d38d4..7b7d0771 100644 --- a/tests/lua-rns.c +++ b/tests/lua-rns.c @@ -231,6 +231,8 @@ main(int argc, char *argv[]) L = lua_open(); lua_baselibopen(L); + luaopen_math(L); + luaopen_io(L); register_ldns_functions(); diff --git a/tests/reverse-pkt.lua b/tests/reverse-pkt.lua index 6dccc2b5..bc347a68 100644 --- a/tests/reverse-pkt.lua +++ b/tests/reverse-pkt.lua @@ -1,5 +1,4 @@ -- ldns defines - LDNS_SECTION_QUESTION = 0 LDNS_SECTION_ANSWER = 1 LDNS_SECTION_AUTHORITY = 2 @@ -8,30 +7,40 @@ LDNS_SECTION_ANY = 4 LDNS_SECTION_ANY_NOQUESTION = 5 -- swap 2 rrs in a pkt -- -function lua_swap_rr (pkt, n1, n2) - +function lua_swap_rr(pkt, n1, n2) + print("[info] [RR] swapping", n1, n2) local rr_n1 = l_pkt_get_rr(pkt, n1) local rr_n2 = l_pkt_set_rr(pkt, rr_n1, n2) local rr_tm = l_pkt_set_rr(pkt, rr_n2, n1) - -- rm_tm is mem leak atm -- need free functions of ldns end +function lua_swap_rr_random(pkt) + local total = l_pkt_rr_count(pkt) - 1 + local rn1 = math.random(0, total) + local rn2 = math.random(0, total) + lua_swap_rr(pkt, rn1, rn2) +end -- reverse all the rrs in a pkt -- -function lua_reverse_pkt (pkt) - local total - - total = l_pkt_rr_count(pkt) - 1; - local j = total / 2 - +function lua_reverse_pkt(pkt) + local total = l_pkt_rr_count(pkt) - 1 for i=0, (total / 2) do - print(i, total - i) lua_swap_rr(pkt, i, total - i) end +end +-- initialize the pseudo random number generator +-- frm: http://lua-users.org/wiki/MathLibraryTutorial +function lua_rand_init() + math.randomseed(os.time()) + math.random() + math.random() + math.random() end +lua_rand_init() + rr1 = l_rr_new_frm_str("www.miek.nl IN A 192.168.1.2") rr2 = l_rr_new_frm_str("miek.nl IN ns gaap") rr3 = l_rr_new_frm_str("miek.nl IN ns gaap2") @@ -48,3 +57,9 @@ l_pkt_print(pkt) lua_reverse_pkt(pkt) l_pkt_print(pkt) + +-- now do it at random +lua_swap_rr_random(pkt) + +-- print again +l_pkt_print(pkt)