From: Jason Ish Date: Thu, 6 Dec 2018 17:16:00 +0000 (-0600) Subject: rust/dns/lua - fix call convention to match C. X-Git-Tag: suricata-4.0.7~26 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=045197c3d817fb44f348bc5771bfee3cc6be8ac8;p=thirdparty%2Fsuricata.git rust/dns/lua - fix call convention to match C. 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 --- diff --git a/rust/src/dns/lua.rs b/rust/src/dns/lua.rs index 4f8b354b98..0c1776a741 100644 --- a/rust/src/dns/lua.rs +++ b/rust/src/dns/lua.rs @@ -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; }