]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
app-layer: more generic state trait
authorJason Ish <jason.ish@oisf.net>
Tue, 26 Apr 2022 19:21:40 +0000 (13:21 -0600)
committerVictor Julien <vjulien@oisf.net>
Sat, 30 Apr 2022 05:58:21 +0000 (07:58 +0200)
Instead of a method that is required to return a slice of transactions,
use 2 methods, one to return the number of transactions in the
collection, and another to get a transaction by its index in the
collection.

This allows for the transaction collection to not be a contiguous array
and instead can be a VecDeque, or possibly another collection type that
supports retrieval by index.

Ticket #5278

18 files changed:
rust/src/applayer.rs
rust/src/applayertemplate/template.rs
rust/src/dhcp/dhcp.rs
rust/src/dns/dns.rs
rust/src/http2/http2.rs
rust/src/ike/ike.rs
rust/src/krb/krb5.rs
rust/src/modbus/modbus.rs
rust/src/mqtt/mqtt.rs
rust/src/nfs/nfs.rs
rust/src/ntp/ntp.rs
rust/src/pgsql/pgsql.rs
rust/src/rdp/rdp.rs
rust/src/rfb/rfb.rs
rust/src/sip/sip.rs
rust/src/smb/smb.rs
rust/src/snmp/snmp.rs
rust/src/telnet/telnet.rs

index d5700268fe0c10b5101966ad17a6bb3445e86498..e0df43298f6c5d64813bff430a725e70eff164d9 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2017-2021 Open Information Security Foundation
+/* Copyright (C) 2017-2022 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
@@ -535,14 +535,17 @@ pub trait Transaction {
 }
 
 pub trait State<Tx: Transaction> {
-    fn get_transactions(&self) -> &[Tx];
+    /// Return the number of transactions in the state's transaction collection.
+    fn get_transaction_count(&self) -> usize;
+
+    /// Return a transaction by its index in the container.
+    fn get_transaction_by_index(&self, index: usize) -> Option<&Tx>;
 
     fn get_transaction_iterator(&self, min_tx_id: u64, state: &mut u64) -> AppLayerGetTxIterTuple {
         let mut index = *state as usize;
-        let transactions = self.get_transactions();
-        let len = transactions.len();
+        let len = self.get_transaction_count();
         while index < len {
-            let tx = &transactions[index];
+            let tx = self.get_transaction_by_index(index).unwrap();
             if tx.id() < min_tx_id + 1 {
                 index += 1;
                 continue;
index 7551cbf9eb166ef7a6d8b42ed6368b51d24bf0f9..2e7234c76f496527e88392583ffd3a674f417d6d 100644 (file)
@@ -60,8 +60,12 @@ pub struct TemplateState {
 }
 
 impl State<TemplateTransaction> for TemplateState {
-    fn get_transactions(&self) -> &[TemplateTransaction] {
-        &self.transactions
+    fn get_transaction_count(&self) -> usize {
+        self.transactions.len()
+    }
+
+    fn get_transaction_by_index(&self, index: usize) -> Option<&TemplateTransaction> {
+        self.transactions.get(index)
     }
 }
 
index 9dad3d2b71278baf5f8ace23926574dab41b9778..063f981f7ec9f2f40fcaaf20fc0825e703202e97 100644 (file)
@@ -109,8 +109,12 @@ pub struct DHCPState {
 }
 
 impl State<DHCPTransaction> for DHCPState {
-    fn get_transactions(&self) -> &[DHCPTransaction] {
-        &self.transactions
+    fn get_transaction_count(&self) -> usize {
+        self.transactions.len()
+    }
+
+    fn get_transaction_by_index(&self, index: usize) -> Option<&DHCPTransaction> {
+        self.transactions.get(index)
     }
 }
 
index 485127e04eb190e43c815f58ccc91735942e9307..4f5cfa85395574d19a4e5191362de45025b489e3 100644 (file)
@@ -321,8 +321,12 @@ pub struct DNSState {
 }
 
 impl State<DNSTransaction> for DNSState {
-    fn get_transactions(&self) -> &[DNSTransaction] {
-        &self.transactions
+    fn get_transaction_count(&self) -> usize {
+        self.transactions.len()
+    }
+
+    fn get_transaction_by_index(&self, index: usize) -> Option<&DNSTransaction> {
+        self.transactions.get(index)
     }
 }
 
index 0d18a2f455d9d443b421ac88afd2c39d7bf00974..135bad0849765ccffa762dfd91bdfe872a47d06b 100644 (file)
@@ -403,8 +403,12 @@ pub struct HTTP2State {
 }
 
 impl State<HTTP2Transaction> for HTTP2State {
-    fn get_transactions(&self) -> &[HTTP2Transaction] {
-        &self.transactions
+    fn get_transaction_count(&self) -> usize {
+        self.transactions.len()
+    }
+
+    fn get_transaction_by_index(&self, index: usize) -> Option<&HTTP2Transaction> {
+        self.transactions.get(index)
     }
 }
 
index cb7677ad36fb47e66318464dd306e606e7463aa2..c3591384774ce6aac9bafff89df1ed7d3b28b9f6 100644 (file)
@@ -144,8 +144,12 @@ pub struct IKEState {
 }
 
 impl State<IKETransaction> for IKEState {
-    fn get_transactions(&self) -> &[IKETransaction] {
-        &self.transactions
+    fn get_transaction_count(&self) -> usize {
+        self.transactions.len()
+    }
+
+    fn get_transaction_by_index(&self, index: usize) -> Option<&IKETransaction> {
+        self.transactions.get(index)
     }
 }
 
index b1c10fef064e41858b292c5c5e8635ed2444109d..99ad00089a3cb1223570f0a4d248be2ee195fa0b 100644 (file)
@@ -52,8 +52,12 @@ pub struct KRB5State {
 }
 
 impl State<KRB5Transaction> for KRB5State {
-    fn get_transactions(&self) -> &[KRB5Transaction] {
-        &self.transactions
+    fn get_transaction_count(&self) -> usize {
+        self.transactions.len()
+    }
+
+    fn get_transaction_by_index(&self, index: usize) -> Option<&KRB5Transaction> {
+        self.transactions.get(index)
     }
 }
 
index 37c584d505d2e6aedb49e63435dc4442e5430252..ac08703c64597fd01d2e74863ea397c4684329fc 100644 (file)
@@ -96,8 +96,12 @@ pub struct ModbusState {
 }
 
 impl State<ModbusTransaction> for ModbusState {
-    fn get_transactions(&self) -> &[ModbusTransaction] {
-        &self.transactions
+    fn get_transaction_count(&self) -> usize {
+        self.transactions.len()
+    }
+
+    fn get_transaction_by_index(&self, index: usize) -> Option<&ModbusTransaction> {
+        self.transactions.get(index)
     }
 }
 
index 2644612800a2bd5ed4c15d1e4df299c3a8be7b4a..1071957b48fd5b9a83e89a5d4c2eab345589e065 100644 (file)
@@ -106,8 +106,12 @@ pub struct MQTTState {
 }
 
 impl State<MQTTTransaction> for MQTTState {
-    fn get_transactions(&self) -> &[MQTTTransaction] {
-        &self.transactions
+    fn get_transaction_count(&self) -> usize {
+        self.transactions.len()
+    }
+
+    fn get_transaction_by_index(&self, index: usize) -> Option<&MQTTTransaction> {
+        self.transactions.get(index)
     }
 }
 
index 9a669680be601dfe9e1d6009629833af4278efa3..67a1d4e9c425790d5ffd9c23194c0893b3ab3f5c 100644 (file)
@@ -325,8 +325,12 @@ pub struct NFSState {
 }
 
 impl State<NFSTransaction> for NFSState {
-    fn get_transactions(&self) -> &[NFSTransaction] {
-        &self.transactions
+    fn get_transaction_count(&self) -> usize {
+        self.transactions.len()
+    }
+
+    fn get_transaction_by_index(&self, index: usize) -> Option<&NFSTransaction> {
+        self.transactions.get(index)
     }
 }
 
index ee28ac6ce5e8c3cf328040c245cc0f3c5dc532b7..12ec8c40a3a979903eaafaf9027bcfec5a7dbee8 100644 (file)
@@ -74,8 +74,12 @@ impl NTPState {
 }
 
 impl State<NTPTransaction> for NTPState {
-    fn get_transactions(&self) -> &[NTPTransaction] {
-        &self.transactions
+    fn get_transaction_count(&self) -> usize {
+        self.transactions.len()
+    }
+
+    fn get_transaction_by_index(&self, index: usize) -> Option<&NTPTransaction> {
+        self.transactions.get(index)
     }
 }
 
index a0bea1e596bae9872dd2f9a0fe6145679905be36..ced155aef5295db690a14f3cc6d41c84f191d5f4 100644 (file)
@@ -125,8 +125,12 @@ pub struct PgsqlState {
 }
 
 impl State<PgsqlTransaction> for PgsqlState {
-    fn get_transactions(&self) -> &[PgsqlTransaction] {
-        &self.transactions
+    fn get_transaction_count(&self) -> usize {
+        self.transactions.len()
+    }
+
+    fn get_transaction_by_index(&self, index: usize) -> Option<&PgsqlTransaction> {
+        self.transactions.get(index)
     }
 }
 
index b6d266f22097d6e85010b3d4d8b86ea939a52d27..383fa66fd100476cc4536786a4feb102100b4ece 100644 (file)
@@ -113,8 +113,12 @@ pub struct RdpState {
 }
 
 impl State<RdpTransaction> for RdpState {
-    fn get_transactions(&self) -> &[RdpTransaction] {
-        &self.transactions
+    fn get_transaction_count(&self) -> usize {
+        self.transactions.len()
+    }
+
+    fn get_transaction_by_index(&self, index: usize) -> Option<&RdpTransaction> {
+        self.transactions.get(index)
     }
 }
 
index e0e3cefa0add7e5781810921f054586cd4d56b2e..f0e3a25c86228a221a23ccc7158a1423894b1093 100644 (file)
@@ -84,9 +84,14 @@ pub struct RFBState {
 }
 
 impl State<RFBTransaction> for RFBState {
-    fn get_transactions(&self) -> &[RFBTransaction] {
-        &self.transactions
+    fn get_transaction_count(&self) -> usize {
+        self.transactions.len()
     }
+
+    fn get_transaction_by_index(&self, index: usize) -> Option<&RFBTransaction> {
+        self.transactions.get(index)
+    }
+
 }
 
 impl RFBState {
index c775e6536cc18357ef3314a5b9075c8ffd76893c..442a358d4d446bb2980b161b41efc593771901cd 100755 (executable)
@@ -51,8 +51,12 @@ pub struct SIPState {
 }
 
 impl State<SIPTransaction> for SIPState {
-    fn get_transactions(&self) -> &[SIPTransaction] {
-        &self.transactions
+    fn get_transaction_count(&self) -> usize {
+        self.transactions.len()
+    }
+
+    fn get_transaction_by_index(&self, index: usize) -> Option<&SIPTransaction> {
+        self.transactions.get(index)
     }
 }
 
index 6cdc5163b9505357af8176ce1410259857963a94..ffccf6fafb0ae7e7949319c6ee1a573bae85b05f 100644 (file)
@@ -795,8 +795,12 @@ pub struct SMBState<> {
 }
 
 impl State<SMBTransaction> for SMBState {
-    fn get_transactions(&self) -> &[SMBTransaction] {
-        &self.transactions
+    fn get_transaction_count(&self) -> usize {
+        self.transactions.len()
+    }
+
+    fn get_transaction_by_index(&self, index: usize) -> Option<&SMBTransaction> {
+        self.transactions.get(index)
     }
 }
 
index c12140db4e65d11aa2d08bd39ea340ea651e120d..9e05c3d4caf81fd039697a882ee6e9a192b2c86b 100644 (file)
@@ -108,8 +108,12 @@ impl<'a> Default for SNMPPduInfo<'a> {
 }
 
 impl<'a> State<SNMPTransaction<'a>> for SNMPState<'a> {
-    fn get_transactions(&self) -> &[SNMPTransaction<'a>] {
-        &self.transactions
+    fn get_transaction_count(&self) -> usize {
+        self.transactions.len()
+    }
+
+    fn get_transaction_by_index(&self, index: usize) -> Option<&SNMPTransaction<'a>> {
+        self.transactions.get(index)
     }
 }
 
index bada4a3b20ed35abea869b81229826d9f13e5612..3b5c7c511f5cfd38431ac2719258451c61b33fe9 100644 (file)
@@ -82,8 +82,12 @@ pub struct TelnetState {
 }
 
 impl State<TelnetTransaction> for TelnetState {
-    fn get_transactions(&self) -> &[TelnetTransaction] {
-        &self.transactions
+    fn get_transaction_count(&self) -> usize {
+        self.transactions.len()
+    }
+
+    fn get_transaction_by_index(&self, index: usize) -> Option<&TelnetTransaction> {
+        self.transactions.get(index)
     }
 }