1use std::{net::SocketAddr, path::PathBuf};
11
12#[cfg(target_os = "linux")]
13mod linux;
14#[cfg(target_os = "macos")]
15mod macos;
16#[cfg(windows)]
17mod windows;
18
19#[derive(Clone, Debug, PartialEq, Eq)]
21pub struct SocketPeer {
22 pub pid: u32,
23 pub executable: PathBuf,
24}
25
26pub fn loopback_socket_peer(local: SocketAddr, peer: SocketAddr) -> Option<SocketPeer> {
35 lookup_with(local, peer, platform_lookup)
36}
37
38fn lookup_with(
39 local: SocketAddr,
40 peer: SocketAddr,
41 lookup: impl FnOnce(SocketAddr, SocketAddr) -> Option<SocketPeer>,
42) -> Option<SocketPeer> {
43 if !normalized(local).ip().is_loopback()
44 || !normalized(peer).ip().is_loopback()
45 || same_endpoint(local, peer)
46 {
47 return None;
48 }
49 lookup(local, peer)
50}
51
52fn platform_lookup(local: SocketAddr, peer: SocketAddr) -> Option<SocketPeer> {
53 #[cfg(target_os = "linux")]
54 return linux::lookup(local, peer);
55 #[cfg(target_os = "macos")]
56 return macos::lookup(local, peer);
57 #[cfg(windows)]
58 return windows::lookup(local, peer);
59 #[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
60 None
61}
62
63fn normalized(address: SocketAddr) -> SocketAddr {
64 match address {
65 SocketAddr::V6(v6) => v6
66 .ip()
67 .to_ipv4_mapped()
68 .map(|ip| SocketAddr::new(ip.into(), v6.port()))
69 .unwrap_or(address),
70 _ => address,
71 }
72}
73
74fn same_endpoint(left: SocketAddr, right: SocketAddr) -> bool {
75 let left = normalized(left);
76 let right = normalized(right);
77 left.ip() == right.ip() && left.port() == right.port()
78}
79
80#[cfg(test)]
81mod tests {
82 use super::*;
83
84 #[test]
85 fn non_loopback_and_identical_endpoints_are_not_looked_up() {
86 let local = "127.0.0.1:1234".parse().unwrap();
87 let must_not_lookup = |_, _| panic!("invalid endpoints reached OS lookup");
88 assert_eq!(lookup_with(local, local, must_not_lookup), None);
89 assert_eq!(
90 lookup_with(local, "192.0.2.1:5678".parse().unwrap(), must_not_lookup),
91 None
92 );
93 assert_eq!(
94 lookup_with("192.0.2.1:1234".parse().unwrap(), local, must_not_lookup),
95 None
96 );
97 }
98
99 #[test]
100 fn valid_loopback_endpoints_reach_lookup_in_accepted_order() {
101 let local = "127.0.0.1:1234".parse().unwrap();
102 let peer = "127.0.0.1:5678".parse().unwrap();
103 let caller = SocketPeer {
104 pid: 42,
105 executable: PathBuf::from("/bin/helper"),
106 };
107 assert_eq!(
108 lookup_with(local, peer, |seen_local, seen_peer| {
109 assert_eq!(seen_local, local);
110 assert_eq!(seen_peer, peer);
111 Some(caller.clone())
112 }),
113 Some(caller)
114 );
115 }
116
117 #[test]
118 fn ipv4_mapped_endpoints_match_ipv4_but_not_other_ports() {
119 assert!(same_endpoint(
120 "[::ffff:127.0.0.1]:1234".parse().unwrap(),
121 "127.0.0.1:1234".parse().unwrap()
122 ));
123 assert!(!same_endpoint(
124 "[::ffff:127.0.0.1]:1234".parse().unwrap(),
125 "127.0.0.1:5678".parse().unwrap()
126 ));
127 }
128}
129
130#[cfg(all(test, any(target_os = "linux", target_os = "macos")))]
131mod shared_tests;