summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJoakim Hulthe <joakim@hulthe.net>2025-03-24 19:45:26 +0100
committerJoakim Hulthe <joakim.hulthe@mullvad.net>2025-04-23 15:35:50 +0200
commitaabe5cedec3385ad160779dda49594cb7bdbced8 (patch)
tree9967b797bc6e4f0d3259236f73d0f2fba1b52f2b
parent4657cc02c930beccd8cd654271830701a8268b41 (diff)
downloadmullvadvpn-aabe5cedec3385ad160779dda49594cb7bdbced8.tar.xz
mullvadvpn-aabe5cedec3385ad160779dda49594cb7bdbced8.zip
Implement Debug for RouteSocketAddress manually
-rw-r--r--talpid-routing/src/unix/macos/data.rs36
1 files changed, 35 insertions, 1 deletions
diff --git a/talpid-routing/src/unix/macos/data.rs b/talpid-routing/src/unix/macos/data.rs
index cb408a5503..9221ec16a9 100644
--- a/talpid-routing/src/unix/macos/data.rs
+++ b/talpid-routing/src/unix/macos/data.rs
@@ -6,6 +6,7 @@ use nix::{
use std::{
collections::BTreeMap,
ffi::{c_int, c_uchar, c_ushort},
+ fmt::{self, Debug},
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6},
};
@@ -789,7 +790,7 @@ bitflags::bitflags! {
}
}
-#[derive(Debug, Clone, PartialEq)]
+#[derive(Clone, PartialEq)]
pub enum RouteSocketAddress {
/// Corresponds to RTA_DST
Destination(Option<SockaddrStorage>),
@@ -809,6 +810,39 @@ pub enum RouteSocketAddress {
Broadcast(Option<SockaddrStorage>),
}
+/// Custom Debug-impl that uses the Display-impl of [SockaddrStorage] since its Debug-impl is
+/// basically unreadable.
+impl Debug for RouteSocketAddress {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ let (variant, sockaddr) = match self {
+ Self::Destination(sockaddr) => ("Destination", sockaddr),
+ Self::Gateway(sockaddr) => ("Gateway", sockaddr),
+ Self::Netmask(sockaddr) => ("Netmask", sockaddr),
+ Self::CloningMask(sockaddr) => ("CloningMask", sockaddr),
+ Self::IfName(sockaddr) => ("IfName", sockaddr),
+ Self::IfSockaddr(sockaddr) => ("IfSockaddr", sockaddr),
+ Self::RedirectAuthor(sockaddr) => ("RedirectAuthor", sockaddr),
+ Self::Broadcast(sockaddr) => ("Broadcast", sockaddr),
+ };
+
+ if let Some(sockaddr) = sockaddr {
+ if let Some(link_addr) = sockaddr.as_link_addr() {
+ // The default Display impl for LinkAddrs does not print ifindex
+ write!(f, "{variant}(")?;
+ f.debug_struct("LinkAddr")
+ .field("addr", &link_addr.addr())
+ .field("iface", &link_addr.ifindex())
+ .finish()?;
+ write!(f, ")")
+ } else {
+ write!(f, "{variant}({sockaddr})")
+ }
+ } else {
+ write!(f, "{variant}(None)")
+ }
+ }
+}
+
impl RouteSocketAddress {
// Returns a new route socket address and number of bytes read from the buffer
pub fn new(flag: AddressFlag, buf: &[u8]) -> Result<(Self, u8)> {