|
| 1 | +use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; |
| 2 | + |
| 3 | +use ts_storage::{DataValue, IpTuple}; |
| 4 | + |
| 5 | +use crate::{db_writer::DBOperation, flow_tracker::{EventIndexer, AF_INET}, reader::FromBuffer}; |
| 6 | + |
| 7 | +use arrayref::array_ref; |
| 8 | + |
| 9 | +#[repr(C)] |
| 10 | +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Default)] |
| 11 | +pub struct sock_trace_entry { |
| 12 | + pub time: u64, |
| 13 | + pub addr_v4: u64, |
| 14 | + pub src_v6: [u8; 16], |
| 15 | + pub dst_v6: [u8; 16], |
| 16 | + pub ports: u32, |
| 17 | + pub family: u16, |
| 18 | + // SOCK Stats |
| 19 | + pub pacing_rate: u64, |
| 20 | + pub max_pacing_rate: u64, |
| 21 | + // INET_CONN Stats |
| 22 | + pub backoff: u8, |
| 23 | + pub rto: u32, |
| 24 | + // INET_CONN -> icsk_ack |
| 25 | + pub ato: u32, |
| 26 | + pub rcv_mss: u16, |
| 27 | + // TCP_SOCK Stats |
| 28 | + pub snd_cwnd: u32, |
| 29 | + pub bytes_acked: u64, |
| 30 | + pub snd_ssthresh: u32, |
| 31 | + pub total_retrans: u32, |
| 32 | + pub probes: u8, |
| 33 | + pub lost: u32, |
| 34 | + pub sacked_out: u32, |
| 35 | + pub retrans: u32, |
| 36 | + pub rcv_ssthresh: u32, |
| 37 | + pub rttvar: u32, |
| 38 | + pub advmss: u16, |
| 39 | + pub reordering: u32, |
| 40 | + pub rcv_rtt: u32, |
| 41 | + pub rcv_space: u32, |
| 42 | + pub bytes_received: u64, |
| 43 | + pub segs_out: u32, |
| 44 | + pub segs_in: u32, |
| 45 | + // TCP_SOCK -> tcp_options_received |
| 46 | + pub snd_wscale: u16, |
| 47 | + pub rcv_wscale: u16, |
| 48 | + pub div: u32 |
| 49 | +} |
| 50 | + |
| 51 | +impl FromBuffer for sock_trace_entry { |
| 52 | + fn from_buffer(buf: &Vec<u8>) -> Self { |
| 53 | + unsafe { *(buf.as_ptr() as *const sock_trace_entry) } |
| 54 | + |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +impl EventIndexer for sock_trace_entry { |
| 59 | + fn get_field(&self, index: usize) -> Option<DataValue> { |
| 60 | + match index { |
| 61 | + _ => None, // TODO: better error handling |
| 62 | + } |
| 63 | + } |
| 64 | + fn get_default_field(&self, index: usize) -> DataValue { |
| 65 | + match index { |
| 66 | + _ => panic!("Tried to access out of bounds index!"), // TODO: better error handling |
| 67 | + } |
| 68 | + } |
| 69 | + fn get_field_name(&self, index: usize) -> &str { |
| 70 | + match index { |
| 71 | + _ => panic!("Tried to access out of bounds index!"), // TODO: better error handling |
| 72 | + } |
| 73 | + } |
| 74 | + fn get_ip_tuple(&self) -> IpTuple { |
| 75 | + let src: IpAddr; |
| 76 | + let dst: IpAddr; |
| 77 | + |
| 78 | + //print!("Family: {}",self.family); |
| 79 | + |
| 80 | + |
| 81 | + if self.family == AF_INET { |
| 82 | + // TODO: check offsets |
| 83 | + let mut bytes = self.addr_v4.to_be_bytes(); |
| 84 | + |
| 85 | + let mut srcbytes = array_ref![bytes,0,4].clone(); |
| 86 | + let mut dstbytes = array_ref![bytes,4,4].clone(); |
| 87 | + //srcbytes.reverse(); |
| 88 | + |
| 89 | + srcbytes.reverse(); |
| 90 | + dstbytes.reverse(); |
| 91 | + src = IpAddr::V4(Ipv4Addr::from(srcbytes)); |
| 92 | + dst = IpAddr::V4(Ipv4Addr::from(dstbytes)); |
| 93 | + } else { |
| 94 | + src = IpAddr::V6(Ipv6Addr::from(self.src_v6)); |
| 95 | + dst = IpAddr::V6(Ipv6Addr::from(self.dst_v6)); |
| 96 | + } |
| 97 | + |
| 98 | + let port_bytes = self.ports.to_be_bytes(); |
| 99 | + |
| 100 | + let srcbytes = array_ref![port_bytes,0,2].clone(); |
| 101 | + let dstbytes = array_ref![port_bytes,2,2].clone(); |
| 102 | + |
| 103 | + // TODO: check byte order if ports are correct |
| 104 | + // Dport could be be bytes |
| 105 | + let sport = u16::from_le_bytes(srcbytes); |
| 106 | + let dport = u16::from_le_bytes(dstbytes); |
| 107 | + |
| 108 | + IpTuple { |
| 109 | + src: src, |
| 110 | + dst: dst, |
| 111 | + sport: sport as i64, |
| 112 | + dport: dport as i64, |
| 113 | + l4proto: 6, |
| 114 | + } |
| 115 | + } |
| 116 | + fn get_max_index(&self) -> usize { |
| 117 | + 0 |
| 118 | + } |
| 119 | + fn get_timestamp(&self) -> f64 { |
| 120 | + self.time as f64 |
| 121 | + } |
| 122 | + fn as_db_op(self) -> DBOperation { |
| 123 | + DBOperation::Socket(self) |
| 124 | + } |
| 125 | +} |
0 commit comments