Skip to main content

rustnet_core/network/protocol/
mod.rs

1//! Transport layer protocol parsing
2//!
3//! This module handles transport layer protocols (Layer 4 of the OSI model):
4//! - TCP (Transmission Control Protocol)
5//! - UDP (User Datagram Protocol)
6//! - ICMP (Internet Control Message Protocol)
7//! - ICMPv6 (Internet Control Message Protocol for IPv6)
8//! - IGMP (Internet Group Management Protocol)
9//! - NDP (Neighbor Discovery Protocol, link-layer address extraction only)
10
11pub(crate) mod icmp;
12pub(crate) mod igmp;
13pub(crate) mod ndp;
14pub mod tcp;
15pub(crate) mod udp;
16
17use std::collections::HashSet;
18use std::net::{IpAddr, SocketAddr};
19
20/// Common parameters for transport layer parsing
21/// Note: Direction (is_outgoing) is determined by the protocol parsers
22/// based on local_ips, not passed as a parameter
23#[derive(Clone)]
24pub(crate) struct TransportParams {
25    pub src_ip: IpAddr,
26    pub dst_ip: IpAddr,
27    pub packet_len: usize,
28    pub process_name: Option<String>,
29    pub process_id: Option<u32>,
30}
31
32impl TransportParams {
33    pub(crate) fn new(
34        src_ip: IpAddr,
35        dst_ip: IpAddr,
36        packet_len: usize,
37        process_name: Option<String>,
38        process_id: Option<u32>,
39    ) -> Self {
40        Self {
41            src_ip,
42            dst_ip,
43            packet_len,
44            process_name,
45            process_id,
46        }
47    }
48}
49
50pub(super) fn orient_endpoints(
51    params: &TransportParams,
52    src_port: u16,
53    dst_port: u16,
54    local_ips: &HashSet<IpAddr>,
55) -> (SocketAddr, SocketAddr, bool) {
56    let is_outgoing = local_ips.contains(&params.src_ip);
57    let source = SocketAddr::new(params.src_ip, src_port);
58    let destination = SocketAddr::new(params.dst_ip, dst_port);
59    if is_outgoing {
60        (source, destination, true)
61    } else {
62        (destination, source, false)
63    }
64}