Skip to content

Commit eba7ab0

Browse files
committed
Add pretty_print function to PeerAddress since can't override macro implementation of fmt::Display
Signed-off-by: Jacinta Ferrant <[email protected]>
1 parent 154910b commit eba7ab0

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

stacks-common/src/types/net.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,12 @@ impl PeerAddress {
118118
)
119119
}
120120

121-
/// Convert to SocketAddr
122-
pub fn to_socketaddr(&self, port: u16) -> SocketAddr {
121+
/// Convert to IpAddr
122+
pub fn to_ipaddr(&self) -> IpAddr {
123123
if self.is_ipv4() {
124-
SocketAddr::new(
125-
IpAddr::V4(Ipv4Addr::new(
126-
self.0[12], self.0[13], self.0[14], self.0[15],
127-
)),
128-
port,
129-
)
124+
IpAddr::V4(Ipv4Addr::new(
125+
self.0[12], self.0[13], self.0[14], self.0[15],
126+
))
130127
} else {
131128
let addr_words: [u16; 8] = [
132129
((self.0[0] as u16) << 8) | (self.0[1] as u16),
@@ -138,23 +135,25 @@ impl PeerAddress {
138135
((self.0[12] as u16) << 8) | (self.0[13] as u16),
139136
((self.0[14] as u16) << 8) | (self.0[15] as u16),
140137
];
141-
142-
SocketAddr::new(
143-
IpAddr::V6(Ipv6Addr::new(
144-
addr_words[0],
145-
addr_words[1],
146-
addr_words[2],
147-
addr_words[3],
148-
addr_words[4],
149-
addr_words[5],
150-
addr_words[6],
151-
addr_words[7],
152-
)),
153-
port,
154-
)
138+
IpAddr::V6(Ipv6Addr::new(
139+
addr_words[0],
140+
addr_words[1],
141+
addr_words[2],
142+
addr_words[3],
143+
addr_words[4],
144+
addr_words[5],
145+
addr_words[6],
146+
addr_words[7],
147+
))
155148
}
156149
}
157150

151+
/// Convert to SocketAddr
152+
pub fn to_socketaddr(&self, port: u16) -> SocketAddr {
153+
let ip_addr = self.to_ipaddr();
154+
SocketAddr::new(ip_addr, port)
155+
}
156+
158157
/// Convert from socket address
159158
pub fn from_socketaddr(addr: &SocketAddr) -> PeerAddress {
160159
PeerAddress::from_ip(&addr.ip())
@@ -228,6 +227,10 @@ impl PeerAddress {
228227
pub fn to_bin(&self) -> String {
229228
to_bin(&self.0)
230229
}
230+
231+
pub fn pretty_print(&self) -> String {
232+
self.to_ipaddr().to_string()
233+
}
231234
}
232235

233236
/// Peer address variants for the Host: header

stackslib/src/net/p2p.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,8 +2009,8 @@ impl PeerNetwork {
20092009
self.events.remove(&nk);
20102010
info!("Dropping neighbor!";
20112011
"event id" => %event_id,
2012-
"public address" => %pubkh,
2013-
"public key" => %nk.addrbytes,
2012+
"public key" => %pubkh,
2013+
"public addr" => %nk.addrbytes.pretty_print(),
20142014
"reason" => %reason
20152015
);
20162016

0 commit comments

Comments
 (0)