From: Jason Ish Date: Tue, 3 Dec 2019 20:10:45 +0000 (-0600) Subject: flow: expose last time as a function X-Git-Tag: suricata-5.0.2~27 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=95a408f77d051ca8b3d18b57ab3b9e96eae9c886;p=thirdparty%2Fsuricata.git flow: expose last time as a function This function returns the individual components of the timeval in output pointers making it suitable for use over Rust FFI. --- diff --git a/rust/src/core.rs b/rust/src/core.rs index 06dda376aa..ef892e66e8 100644 --- a/rust/src/core.rs +++ b/rust/src/core.rs @@ -21,7 +21,6 @@ use std; use crate::filecontainer::*; /// Opaque C types. -pub enum Flow {} pub enum DetectEngineState {} pub enum AppLayerDecoderEvents {} @@ -183,3 +182,26 @@ pub fn sc_app_layer_decoder_events_free_events( } } } + +/// Opaque flow type (defined in C) +pub enum Flow {} + +/// Extern functions operating on Flow. +extern { + pub fn FlowGetLastTimeAsParts(flow: &Flow, secs: *mut u64, usecs: *mut u64); +} + +/// Rust implementation of Flow. +impl Flow { + + /// Return the time of the last flow update as a `Duration` + /// since the epoch. + pub fn get_last_time(&mut self) -> std::time::Duration { + unsafe { + let mut secs: u64 = 0; + let mut usecs: u64 = 0; + FlowGetLastTimeAsParts(self, &mut secs, &mut usecs); + std::time::Duration::new(secs, usecs as u32 * 1000) + } + } +} \ No newline at end of file diff --git a/src/flow.c b/src/flow.c index 5fcf531357..adde854642 100644 --- a/src/flow.c +++ b/src/flow.c @@ -1113,6 +1113,19 @@ void FlowUpdateState(Flow *f, enum FlowState s) } } +/** + * \brief Get flow last time as individual values. + * + * Instead of returning a pointer to the timeval copy the timeval + * parts into output pointers to make it simpler to call from Rust + * over FFI using only basic data types. + */ +void FlowGetLastTimeAsParts(Flow *flow, uint64_t *secs, uint64_t *usecs) +{ + *secs = (uint64_t)flow->lastts.tv_sec; + *usecs = (uint64_t)flow->lastts.tv_usec; +} + /************************************Unittests*******************************/ #ifdef UNITTESTS diff --git a/src/flow.h b/src/flow.h index cf6e729581..b2eb217964 100644 --- a/src/flow.h +++ b/src/flow.h @@ -537,6 +537,8 @@ uint64_t FlowGetMemuse(void); int GetFlowBypassInfoID(void); void RegisterFlowBypassInfo(void); +void FlowGetLastTimeAsParts(Flow *flow, uint64_t *secs, uint64_t *usecs); + /** ----- Inline functions ----- */ /** \brief Set the No Packet Inspection Flag without locking the flow.