|
17 | 17 |
|
18 | 18 | use std::ffi::CString; |
19 | 19 | use std::marker::PhantomData; |
| 20 | +use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; |
20 | 21 | use std::os::raw::c_void; |
| 22 | +use std::time::Duration; |
21 | 23 |
|
22 | 24 | use suricata_sys::sys::{ |
23 | 25 | self, Packet, SCFlowGetStorageById, SCFlowRegisterFinishCallback, SCFlowRegisterInitCallback, |
@@ -58,6 +60,92 @@ impl<'a> Flow<'a> { |
58 | 60 | fn as_mut_ptr(&mut self) -> *mut sys::Flow { |
59 | 61 | self.flow |
60 | 62 | } |
| 63 | + |
| 64 | + /// Return the time of the last flow update as a `Duration` since the epoch. |
| 65 | + pub fn last_time(&self) -> Duration { |
| 66 | + let mut secs: u64 = 0; |
| 67 | + let mut usecs: u64 = 0; |
| 68 | + unsafe { |
| 69 | + sys::SCFlowGetLastTimeAsParts(self.as_ptr(), &mut secs, &mut usecs); |
| 70 | + } |
| 71 | + Duration::new(secs, usecs as u32 * 1000) |
| 72 | + } |
| 73 | + |
| 74 | + /// Return the flow flags. |
| 75 | + pub fn flags(&self) -> u64 { |
| 76 | + unsafe { sys::SCFlowGetFlags(self.as_ptr()) } |
| 77 | + } |
| 78 | + |
| 79 | + /// Return true if the flow is IPv4. |
| 80 | + pub fn is_ipv4(&self) -> bool { |
| 81 | + unsafe { sys::SCFlowIsIPv4(self.as_ptr()) } |
| 82 | + } |
| 83 | + |
| 84 | + /// Return true if the flow is IPv6. |
| 85 | + pub fn is_ipv6(&self) -> bool { |
| 86 | + unsafe { sys::SCFlowIsIPv6(self.as_ptr()) } |
| 87 | + } |
| 88 | + |
| 89 | + /// Return the flow IP protocol. |
| 90 | + pub fn ip_protocol(&self) -> u8 { |
| 91 | + unsafe { sys::SCFlowGetIPProtocol(self.as_ptr()) } |
| 92 | + } |
| 93 | + |
| 94 | + /// Return the flow app-layer protocol. |
| 95 | + pub fn app_protocol(&self) -> sys::AppProto { |
| 96 | + unsafe { sys::SCFlowGetAppProtocol(self.as_ptr()) } |
| 97 | + } |
| 98 | + |
| 99 | + /// Return the flow source port. |
| 100 | + pub fn source_port(&self) -> u16 { |
| 101 | + unsafe { sys::SCFlowGetSourcePort(self.as_ptr()) } |
| 102 | + } |
| 103 | + |
| 104 | + /// Return the flow destination port. |
| 105 | + pub fn destination_port(&self) -> u16 { |
| 106 | + unsafe { sys::SCFlowGetDestinationPort(self.as_ptr()) } |
| 107 | + } |
| 108 | + |
| 109 | + /// Return the flow source address. |
| 110 | + pub fn source_address(&self) -> Option<IpAddr> { |
| 111 | + let ptr = unsafe { sys::SCFlowGetSourceAddressAsRawPtr(self.as_ptr()) }; |
| 112 | + self.address_from_ptr(ptr) |
| 113 | + } |
| 114 | + |
| 115 | + /// Return the flow destination address. |
| 116 | + pub fn destination_address(&self) -> Option<IpAddr> { |
| 117 | + let ptr = unsafe { sys::SCFlowGetDestinationAddressAsRawPtr(self.as_ptr()) }; |
| 118 | + self.address_from_ptr(ptr) |
| 119 | + } |
| 120 | + |
| 121 | + /// Return the number of packets seen to-server. |
| 122 | + pub fn to_server_packet_count(&self) -> u32 { |
| 123 | + unsafe { sys::SCFlowGetToServerPacketCount(self.as_ptr()) } |
| 124 | + } |
| 125 | + |
| 126 | + /// Return the number of packets seen to-client. |
| 127 | + pub fn to_client_packet_count(&self) -> u32 { |
| 128 | + unsafe { sys::SCFlowGetToClientPacketCount(self.as_ptr()) } |
| 129 | + } |
| 130 | + |
| 131 | + fn address_from_ptr(&self, ptr: *const u8) -> Option<IpAddr> { |
| 132 | + if ptr.is_null() { |
| 133 | + return None; |
| 134 | + } |
| 135 | + if self.is_ipv4() { |
| 136 | + let bytes = unsafe { std::slice::from_raw_parts(ptr, 4) }; |
| 137 | + Some(IpAddr::V4(Ipv4Addr::new( |
| 138 | + bytes[0], bytes[1], bytes[2], bytes[3], |
| 139 | + ))) |
| 140 | + } else if self.is_ipv6() { |
| 141 | + let bytes = unsafe { std::slice::from_raw_parts(ptr, 16) }; |
| 142 | + let mut addr = [0; 16]; |
| 143 | + addr.copy_from_slice(bytes); |
| 144 | + Some(IpAddr::V6(Ipv6Addr::from(addr))) |
| 145 | + } else { |
| 146 | + None |
| 147 | + } |
| 148 | + } |
61 | 149 | } |
62 | 150 |
|
63 | 151 | /// A typed handle to a per-flow storage slot. |
|
0 commit comments