trace2e_core/transport/
mod.rs

1//! # Transport Module
2//!
3//! This module provides the networking and communication layer for the trace2e framework,
4//! enabling distributed traceability operations between middleware instances across
5//! different nodes in a networked environment.
6//!
7//! ## Overview
8//!
9//! The transport layer abstracts the underlying communication mechanisms used for
10//! machine-to-machine (M2M) interactions in the trace2e system. It supports multiple
11//! transport implementations including gRPC for production environments, loopback and
12//! no-op for testing purposes.
13//!
14//! ## Transport Implementations
15//!
16//! - **gRPC Transport**: Production-ready implementation using Protocol Buffers and gRPC
17//! - **Loopback Transport**: Local testing implementation that routes calls internally
18//!   between mock middleware instances
19//! - **No-op Transport**: Stub implementation that performs no actual communication
20//!
21//! ## Remote IP Evaluation
22//!
23//! The module provides utilities for extracting remote IP addresses from M2M requests,
24//! enabling proper routing and connection management for distributed operations.
25//!
26//! ## Architecture
27//!
28//! The transport layer operates between the traceability middleware instances,
29//! facilitating the exchange of compliance policies, provenance information,
30//! and authorization decisions across network boundaries.
31
32use std::net::SocketAddr;
33
34use crate::traceability::{
35    api::M2mRequest,
36    error::TraceabilityError,
37    naming::{Fd, Resource},
38};
39
40pub mod grpc;
41pub mod loopback;
42pub mod nop;
43
44/// Extracts the remote IP address from an M2M request for routing purposes.
45///
46/// This function analyzes the request type and destination resource to determine
47/// the appropriate remote IP address that should handle the request. For stream
48/// resources, it extracts the IP from the local socket address. For source
49/// compliance requests, it uses the provided authority IP.
50///
51/// # Arguments
52///
53/// * `req` - The M2M request to analyze
54///
55/// # Returns
56///
57/// The remote IP address as a string, or an error if the IP cannot be determined.
58///
59/// # Errors
60///
61/// Returns `TransportFailedToEvaluateRemote` if:
62/// - The destination is not a stream resource
63/// - The socket address cannot be parsed
64/// - The request type doesn't contain routing information
65pub fn eval_remote_ip(req: M2mRequest) -> Result<String, TraceabilityError> {
66    match req {
67        M2mRequest::GetDestinationCompliance { destination, .. }
68        | M2mRequest::UpdateProvenance { destination, .. } => match destination {
69            // Local socket is the remote socket of the remote stream
70            Resource::Fd(Fd::Stream(stream)) => match stream.local_socket.parse::<SocketAddr>() {
71                Ok(addr) => Ok(addr.ip().to_string()),
72                Err(_) => Err(TraceabilityError::TransportFailedToEvaluateRemote),
73            },
74            _ => Err(TraceabilityError::TransportFailedToEvaluateRemote),
75        },
76        M2mRequest::GetSourceCompliance { authority_ip, .. } => Ok(authority_ip),
77    }
78}