]> git.ipfire.org Git - thirdparty/ldns.git/commitdiff
more code and logging and randomness
authorMiek Gieben <miekg@NLnetLabs.nl>
Mon, 8 Aug 2005 16:32:57 +0000 (16:32 +0000)
committerMiek Gieben <miekg@NLnetLabs.nl>
Mon, 8 Aug 2005 16:32:57 +0000 (16:32 +0000)
tests/lua-rns.c
tests/reverse-pkt.lua

index 955d38d4d0880a8100392242183c7c022a32f173..7b7d07713978fd02287e19178ae5c7d3b70ed4fa 100644 (file)
@@ -231,6 +231,8 @@ main(int argc, char *argv[])
        
         L = lua_open();
         lua_baselibopen(L);
+       luaopen_math(L);
+       luaopen_io(L);
 
        register_ldns_functions();
 
index 6dccc2b5b0ab22d44a662d9e0c46d77867768779..bc347a68ac39387308ce5d2f786734d11d375fb3 100644 (file)
@@ -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)