rust: bindgen flow ffi API

Ticket: 7667
pull/13884/head
Philippe Antoine 1 year ago committed by Victor Julien
parent bbf1424371
commit b59086210f

@ -15,20 +15,13 @@
* 02110-1301, USA.
*/
/// Flow API from C.
/// cbindgen:ignore
extern "C" {
fn FlowGetLastTimeAsParts(flow: &Flow, secs: *mut u64, usecs: *mut u64);
fn FlowGetFlags(flow: &Flow) -> u32;
fn FlowGetSourcePort(flow: &Flow) -> u16;
fn FlowGetDestinationPort(flow: &Flow) -> u16;
}
// Flow flags
pub const FLOW_DIR_REVERSED: u32 = BIT_U32!(26);
/// Opaque flow type (defined in C)
pub use suricata_sys::sys::Flow;
pub use suricata_sys::sys::{
Flow, SCFlowGetDestinationPort, SCFlowGetFlags, SCFlowGetLastTimeAsParts, SCFlowGetSourcePort,
};
/// Return the time of the last flow update as a `Duration`
/// since the epoch.
@ -36,17 +29,17 @@ pub fn flow_get_last_time(flow: &Flow) -> std::time::Duration {
unsafe {
let mut secs: u64 = 0;
let mut usecs: u64 = 0;
FlowGetLastTimeAsParts(flow, &mut secs, &mut usecs);
SCFlowGetLastTimeAsParts(flow, &mut secs, &mut usecs);
std::time::Duration::new(secs, usecs as u32 * 1000)
}
}
/// Return the flow flags.
pub fn flow_get_flags(flow: &Flow) -> u32 {
unsafe { FlowGetFlags(flow) }
unsafe { SCFlowGetFlags(flow) }
}
/// Return flow ports
pub fn flow_get_ports(flow: &Flow) -> (u16, u16) {
unsafe { (FlowGetSourcePort(flow), FlowGetDestinationPort(flow)) }
unsafe { (SCFlowGetSourcePort(flow), SCFlowGetDestinationPort(flow)) }
}

@ -761,3 +761,15 @@ extern "C" {
sid: u32, flags: u8,
) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn SCFlowGetLastTimeAsParts(flow: *const Flow, secs: *mut u64, usecs: *mut u64);
}
extern "C" {
pub fn SCFlowGetFlags(flow: *const Flow) -> u32;
}
extern "C" {
pub fn SCFlowGetSourcePort(flow: *const Flow) -> u16;
}
extern "C" {
pub fn SCFlowGetDestinationPort(flow: *const Flow) -> u16;
}

@ -336,6 +336,7 @@ noinst_HEADERS = \
detect.h \
device-storage.h \
feature.h \
flow-bindgen.h \
flow-bit.h \
flow-bypass.h \
flow-callbacks.h \

@ -52,4 +52,6 @@
#include "util-mpm.h"
#include "flow-bindgen.h"
#endif

@ -0,0 +1,29 @@
/* Copyright (C) 2025 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.
*/
#ifndef SURICATA_FLOW_BINDGEN_H
#define SURICATA_FLOW_BINDGEN_H
/* forward declaration for macset include */
typedef struct Flow_ Flow;
void SCFlowGetLastTimeAsParts(const Flow *flow, uint64_t *secs, uint64_t *usecs);
uint32_t SCFlowGetFlags(const Flow *flow);
uint16_t SCFlowGetSourcePort(const Flow *flow);
uint16_t SCFlowGetDestinationPort(const Flow *flow);
#endif /* SURICATA_FLOW_BINDGEN_H */

@ -36,6 +36,7 @@
#include "util-time.h"
#include "flow.h"
#include "flow-bindgen.h"
#include "flow-queue.h"
#include "flow-hash.h"
#include "flow-util.h"
@ -1189,7 +1190,7 @@ void FlowUpdateState(Flow *f, const enum FlowState s)
* 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)
void SCFlowGetLastTimeAsParts(const Flow *flow, uint64_t *secs, uint64_t *usecs)
{
*secs = (uint64_t)SCTIME_SECS(flow->lastts);
*usecs = (uint64_t)SCTIME_USECS(flow->lastts);
@ -1201,7 +1202,7 @@ void FlowGetLastTimeAsParts(Flow *flow, uint64_t *secs, uint64_t *usecs)
* A function to get the flow sport useful when the caller only has an
* opaque pointer to the flow structure.
*/
uint16_t FlowGetSourcePort(Flow *flow)
uint16_t SCFlowGetSourcePort(const Flow *flow)
{
return flow->sp;
}
@ -1213,7 +1214,7 @@ uint16_t FlowGetSourcePort(Flow *flow)
* opaque pointer to the flow structure.
*/
uint16_t FlowGetDestinationPort(Flow *flow)
uint16_t SCFlowGetDestinationPort(const Flow *flow)
{
return flow->dp;
}
@ -1224,7 +1225,7 @@ uint16_t FlowGetDestinationPort(Flow *flow)
* opaque pointer to the flow structure.
*/
uint32_t FlowGetFlags(Flow *flow)
uint32_t SCFlowGetFlags(const Flow *flow)
{
return flow->flags;
}

@ -574,11 +574,6 @@ enum ExceptionPolicy FlowGetMemcapExceptionPolicy(void);
FlowStorageId GetFlowBypassInfoID(void);
void RegisterFlowBypassInfo(void);
void FlowGetLastTimeAsParts(Flow *flow, uint64_t *secs, uint64_t *usecs);
uint32_t FlowGetFlags(Flow *flow);
uint16_t FlowGetSourcePort(Flow *flow);
uint16_t FlowGetDestinationPort(Flow *flow);
/** ----- Inline functions ----- */
static inline AppProto FlowGetAppProtocol(const Flow *f)

Loading…
Cancel
Save