]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
rust/dns/lua - fix call convention to match C.
authorJason Ish <ish@unx.ca>
Thu, 6 Dec 2018 17:16:00 +0000 (11:16 -0600)
committerVictor Julien <victor@inliniac.net>
Sat, 16 Feb 2019 13:58:18 +0000 (14:58 +0100)
Also, when requesting the query, if the request doesn't exist,
return the query from the response. This makes it behave
more like C implementation.

Redmine issue:
https://redmine.openinfosecfoundation.org/issues/2730

rust/src/dns/lua.rs

index 4f8b354b988f93179b57ebccc027317028fc733b..0c1776a741ad7a93744741d7278b04a82ad15192 100644 (file)
@@ -69,14 +69,13 @@ pub extern "C" fn rs_dns_lua_get_query_table(clua: &mut CLuaState,
 
     let mut i: i64 = 0;
 
-    for request in &tx.request {
-
-        if request.queries.len() == 0 {
-            break;
-        }
-
-        lua.newtable();
+    // Create table now to be consistent with C that always returns
+    // table even in the absence of any authorities.
+    lua.newtable();
 
+    // We first look in the request for queries. However, if there is
+    // no request, check the response for queries.
+    if let &Some(ref request) = &tx.request {
         for query in &request.queries {
             lua.pushinteger(i);
             i += 1;
@@ -93,11 +92,28 @@ pub extern "C" fn rs_dns_lua_get_query_table(clua: &mut CLuaState,
 
             lua.settable(-3);
         }
+    } else if let &Some(ref response) = &tx.response {
+        for query in &response.queries {
+            lua.pushinteger(i);
+            i += 1;
+
+            lua.newtable();
+
+            lua.pushstring("type");
+            lua.pushstring(&dns_rrtype_string(query.rrtype));
+            lua.settable(-3);
+
+            lua.pushstring("rrname");
+            lua.pushstring(&String::from_utf8_lossy(&query.name));
+            lua.settable(-3);
 
-        return 1;
+            lua.settable(-3);
+        }
     }
 
-    return 0;
+    // Again, always return 1 to be consistent with C, even if the
+    // table is empty.
+    return 1;
 }
 
 #[no_mangle]
@@ -111,14 +127,11 @@ pub extern "C" fn rs_dns_lua_get_answer_table(clua: &mut CLuaState,
 
     let mut i: i64 = 0;
 
-    for response in &tx.response {
-
-        if response.answers.len() == 0 {
-            break;
-        }
-
-        lua.newtable();
+    // Create table now to be consistent with C that always returns
+    // table even in the absence of any authorities.
+    lua.newtable();
 
+    if let &Some(ref response) = &tx.response {
         for answer in &response.answers {
             lua.pushinteger(i);
             i += 1;
@@ -150,11 +163,11 @@ pub extern "C" fn rs_dns_lua_get_answer_table(clua: &mut CLuaState,
             }
             lua.settable(-3);
         }
-
-        return 1;
     }
 
-    return 0;
+    // Again, always return 1 to be consistent with C, even if the
+    // table is empty.
+    return 1;
 }
 
 #[no_mangle]
@@ -168,14 +181,11 @@ pub extern "C" fn rs_dns_lua_get_authority_table(clua: &mut CLuaState,
 
     let mut i: i64 = 0;
 
-    for response in &tx.response {
-
-        if response.authorities.len() == 0 {
-            break;
-        }
-
-        lua.newtable();
+    // Create table now to be consistent with C that always returns
+    // table even in the absence of any authorities.
+    lua.newtable();
 
+    if let &Some(ref response) = &tx.response {
         for answer in &response.authorities {
             lua.pushinteger(i);
             i += 1;
@@ -195,9 +205,9 @@ pub extern "C" fn rs_dns_lua_get_authority_table(clua: &mut CLuaState,
 
             lua.settable(-3);
         }
-
-        return 1;
     }
 
-    return 0;
+    // Again, always return 1 to be consistent with C, even if the
+    // table is empty.
+    return 1;
 }