]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
dns: cleanup: move event callbacks into Rust
authorJason Ish <jason.ish@oisf.net>
Thu, 9 Jan 2020 22:21:54 +0000 (16:21 -0600)
committerVictor Julien <victor@inliniac.net>
Sat, 14 Mar 2020 19:34:50 +0000 (20:34 +0100)
Remove app-layer-dns-common.c as its no longer needed.

rust/src/dns/dns.rs
src/Makefile.am
src/app-layer-dns-common.c [deleted file]
src/app-layer-dns-common.h
src/app-layer-dns-tcp.c
src/app-layer-dns-udp.c

index 3626f5b4422ac07817a8aee8b28adcd7deee734f..855a4ce326f7860d33fc34211e6c54cd0c41aabc 100644 (file)
@@ -127,10 +127,82 @@ const MAX_TRANSACTIONS: usize = 32;
 
 #[repr(u32)]
 pub enum DNSEvent {
-    MalformedData,
-    NotRequest,
-    NotResponse,
-    ZFlagSet,
+    MalformedData = 0,
+    NotRequest = 1,
+    NotResponse = 2,
+    ZFlagSet = 3,
+}
+
+impl DNSEvent {
+    pub fn to_cstring(&self) -> &str {
+        match *self {
+            DNSEvent::MalformedData => "MALFORMED_DATA\0",
+            DNSEvent::NotRequest => "NOT_A_REQUEST\0",
+            DNSEvent::NotResponse => "NOT_A_RESPONSE\0",
+            DNSEvent::ZFlagSet => "Z_FLAG_SET\0",
+        }
+    }
+
+    pub fn from_id(id: u32) -> Option<DNSEvent> {
+        match id {
+            0 => Some(DNSEvent::MalformedData),
+            1 => Some(DNSEvent::NotRequest),
+            2 => Some(DNSEvent::NotResponse),
+            4 => Some(DNSEvent::ZFlagSet),
+            _ => None,
+        }
+    }
+
+    pub fn from_string(s: &str) -> Option<DNSEvent> {
+        match s.to_lowercase().as_ref() {
+            "malformed_data" => Some(DNSEvent::MalformedData),
+            "not_a_request" => Some(DNSEvent::NotRequest),
+            "not_a_response" => Some(DNSEvent::NotRequest),
+            "z_flag_set" => Some(DNSEvent::ZFlagSet),
+            _ => None
+        }
+    }
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn rs_dns_state_get_event_info_by_id(
+    event_id: std::os::raw::c_int,
+    event_name: *mut *const std::os::raw::c_char,
+    event_type: *mut core::AppLayerEventType,
+) -> std::os::raw::c_int {
+    if let Some(e) = DNSEvent::from_id(event_id as u32) {
+        *event_name = e.to_cstring().as_ptr() as *const std::os::raw::c_char;
+        *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION;
+        return 0;
+    }
+    return -1;
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn rs_dns_state_get_event_info(
+    event_name: *const std::os::raw::c_char,
+    event_id: *mut std::os::raw::c_int,
+    event_type: *mut core::AppLayerEventType
+) -> std::os::raw::c_int {
+    if event_name == std::ptr::null() {
+        return -1;
+    }
+
+    let event_name = std::ffi::CStr::from_ptr(event_name);
+    if let Ok(event_name) = event_name.to_str() {
+        if let Some(event) = DNSEvent::from_string(event_name) {
+            *event_id = event as std::os::raw::c_int;
+            *event_type = core::APP_LAYER_EVENT_TYPE_TRANSACTION;
+        } else {
+            // Unknown event...
+            return -1;
+        }
+    } else {
+        // UTF-8 conversion failed. Should not happen.
+        return -1;
+    }
+
+    return 0;
 }
 
 #[derive(Debug,PartialEq)]
index b0e9cf4050d8d08c0d0d95b70804d79c5146e4ca..fcdc43ab4a125f5ef72d6004f8b396d1db946a0d 100755 (executable)
@@ -18,7 +18,7 @@ app-layer-dcerpc-udp.c app-layer-dcerpc-udp.h \
 app-layer-detect-proto.c app-layer-detect-proto.h \
 app-layer-dnp3.c app-layer-dnp3.h \
 app-layer-dnp3-objects.c app-layer-dnp3-objects.h \
-app-layer-dns-common.c app-layer-dns-common.h \
+app-layer-dns-common.h \
 app-layer-dns-tcp.c app-layer-dns-tcp.h \
 app-layer-dns-udp.c app-layer-dns-udp.h \
 app-layer-enip.c app-layer-enip.h \
diff --git a/src/app-layer-dns-common.c b/src/app-layer-dns-common.c
deleted file mode 100644 (file)
index 7d1b407..0000000
+++ /dev/null
@@ -1,80 +0,0 @@
-/* Copyright (C) 2013-2014 Open Information Security Foundation
- *
- * You can copy, redistribute or modify this Program under the terms of
- * the GNU General Public License version 2 as published by the Free
- * Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * version 2 along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
- */
-
-/**
- * \file
- *
- * \author Victor Julien <victor@inliniac.net>
- */
-
-#include "suricata-common.h"
-#include "app-layer-dns-common.h"
-
-SCEnumCharMap dns_decoder_event_table[ ] = {
-    { "MALFORMED_DATA",             DNS_DECODER_EVENT_MALFORMED_DATA, },
-    { "NOT_A_REQUEST",              DNS_DECODER_EVENT_NOT_A_REQUEST, },
-    { "NOT_A_RESPONSE",             DNS_DECODER_EVENT_NOT_A_RESPONSE, },
-    { "Z_FLAG_SET",                 DNS_DECODER_EVENT_Z_FLAG_SET, },
-
-    { NULL,                         -1 },
-};
-
-int DNSStateGetEventInfo(const char *event_name,
-                         int *event_id, AppLayerEventType *event_type)
-{
-    *event_id = SCMapEnumNameToValue(event_name, dns_decoder_event_table);
-    if (*event_id == -1) {
-        SCLogError(SC_ERR_INVALID_ENUM_MAP, "event \"%s\" not present in "
-                   "dns's enum map table.",  event_name);
-        /* this should be treated as fatal */
-        return -1;
-    }
-
-    *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
-
-    return 0;
-}
-
-int DNSStateGetEventInfoById(int event_id, const char **event_name,
-                             AppLayerEventType *event_type)
-{
-    *event_name = SCMapEnumValueToName(event_id, dns_decoder_event_table);
-    if (*event_name == NULL) {
-        SCLogError(SC_ERR_INVALID_ENUM_MAP, "event \"%d\" not present in "
-                   "dns's enum map table.",  event_id);
-        /* this should be treated as fatal */
-        return -1;
-    }
-
-    *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
-
-    return 0;
-}
-
-void DNSAppLayerRegisterGetEventInfo(uint8_t ipproto, AppProto alproto)
-{
-    AppLayerParserRegisterGetEventInfo(ipproto, alproto, DNSStateGetEventInfo);
-
-    return;
-}
-
-void DNSAppLayerRegisterGetEventInfoById(uint8_t ipproto, AppProto alproto)
-{
-    AppLayerParserRegisterGetEventInfoById(ipproto, alproto, DNSStateGetEventInfoById);
-
-    return;
-}
index 1f3e7e122b4feede6da155423235d40d63f490f0..0085c5dbda2049676b6b3bad1dd835d002afffd7 100644 (file)
 #include "app-layer-protos.h"
 #include "app-layer-parser.h"
 
-enum {
-    DNS_DECODER_EVENT_MALFORMED_DATA,
-    DNS_DECODER_EVENT_NOT_A_REQUEST,
-    DNS_DECODER_EVENT_NOT_A_RESPONSE,
-    DNS_DECODER_EVENT_Z_FLAG_SET,
-};
-
 /** Opaque Rust types. */
 
 /** \brief DNS packet header */
@@ -46,10 +39,6 @@ typedef struct DNSHeader_ {
     uint16_t additional_rr;
 } __attribute__((__packed__)) DNSHeader;
 
-int DNSStateGetEventInfo(const char *event_name,
-                         int *event_id, AppLayerEventType *event_type);
-int DNSStateGetEventInfoById(int event_id, const char **event_name,
-                             AppLayerEventType *event_type);
 void DNSAppLayerRegisterGetEventInfo(uint8_t ipproto, AppProto alproto);
 void DNSAppLayerRegisterGetEventInfoById(uint8_t ipproto, AppProto alproto);
 
index c47407c1cb25cbc89435e9589af6d2f5abdf6bd3..1617ff02343cb41d5a4f0606ba013b214837e4cf 100644 (file)
@@ -166,8 +166,10 @@ void RegisterDNSTCPParsers(void)
                 RustDNSGetAlstateProgress);
         AppLayerParserRegisterGetStateProgressCompletionStatus(ALPROTO_DNS,
                 rs_dns_state_progress_completion_status);
-        DNSAppLayerRegisterGetEventInfo(IPPROTO_TCP, ALPROTO_DNS);
-        DNSAppLayerRegisterGetEventInfoById(IPPROTO_TCP, ALPROTO_DNS);
+        AppLayerParserRegisterGetEventInfo(IPPROTO_TCP, ALPROTO_DNS,
+            rs_dns_state_get_event_info);
+        AppLayerParserRegisterGetEventInfoById(IPPROTO_TCP, ALPROTO_DNS,
+            rs_dns_state_get_event_info_by_id);
 
         /* This parser accepts gaps. */
         AppLayerParserRegisterOptionFlags(IPPROTO_TCP, ALPROTO_DNS,
index 69251b3a7ec63a0ccde291cdd52a507a73105394..d0f37169d706576cc00368029e7b84b7c5bb50e2 100644 (file)
@@ -180,9 +180,10 @@ void RegisterDNSUDPParsers(void)
         AppLayerParserRegisterGetStateProgressCompletionStatus(ALPROTO_DNS,
                 rs_dns_state_progress_completion_status);
 
-        DNSAppLayerRegisterGetEventInfo(IPPROTO_UDP, ALPROTO_DNS);
-        DNSAppLayerRegisterGetEventInfoById(IPPROTO_UDP, ALPROTO_DNS);
-
+        AppLayerParserRegisterGetEventInfo(IPPROTO_UDP, ALPROTO_DNS,
+            rs_dns_state_get_event_info);
+        AppLayerParserRegisterGetEventInfoById(IPPROTO_UDP, ALPROTO_DNS,
+            rs_dns_state_get_event_info_by_id);
     } else {
         SCLogConfig("Parsed disabled for %s protocol. Protocol detection"
                 "still on.", proto_name);