Skip to content

Commit b678ee3

Browse files
authored
Clean up socket binding reuse (solana-labs#30367)
1 parent 4639919 commit b678ee3

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

gossip/src/cluster_info.rs

+20-18
Original file line numberDiff line numberDiff line change
@@ -2819,28 +2819,30 @@ impl Node {
28192819
Self::new_localhost_with_pubkey(&pubkey)
28202820
}
28212821
pub fn new_localhost_with_pubkey(pubkey: &Pubkey) -> Self {
2822-
let bind_ip_addr = IpAddr::V4(Ipv4Addr::LOCALHOST);
2822+
let localhost_ip_addr = IpAddr::V4(Ipv4Addr::LOCALHOST);
2823+
let localhost_bind_addr = format!("{:?}:0", localhost_ip_addr);
2824+
let unspecified_bind_addr = format!("{:?}:0", IpAddr::V4(Ipv4Addr::UNSPECIFIED));
28232825
let port_range = (1024, 65535);
2826+
28242827
let ((_tpu_port, tpu), (_tpu_quic_port, tpu_quic)) =
2825-
bind_two_in_range_with_offset(bind_ip_addr, port_range, QUIC_PORT_OFFSET).unwrap();
2828+
bind_two_in_range_with_offset(localhost_ip_addr, port_range, QUIC_PORT_OFFSET).unwrap();
28262829
let (gossip_port, (gossip, ip_echo)) =
2827-
bind_common_in_range(bind_ip_addr, port_range).unwrap();
2828-
let gossip_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), gossip_port);
2829-
let tvu = UdpSocket::bind("127.0.0.1:0").unwrap();
2830-
let tvu_forwards = UdpSocket::bind("127.0.0.1:0").unwrap();
2830+
bind_common_in_range(localhost_ip_addr, port_range).unwrap();
2831+
let gossip_addr = SocketAddr::new(localhost_ip_addr, gossip_port);
2832+
let tvu = UdpSocket::bind(&localhost_bind_addr).unwrap();
2833+
let tvu_forwards = UdpSocket::bind(&localhost_bind_addr).unwrap();
28312834
let ((_tpu_forwards_port, tpu_forwards), (_tpu_forwards_quic_port, tpu_forwards_quic)) =
2832-
bind_two_in_range_with_offset(bind_ip_addr, port_range, QUIC_PORT_OFFSET).unwrap();
2833-
let tpu_vote = UdpSocket::bind("127.0.0.1:0").unwrap();
2834-
let repair = UdpSocket::bind("127.0.0.1:0").unwrap();
2835-
let rpc_port = find_available_port_in_range(bind_ip_addr, port_range).unwrap();
2836-
let rpc_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), rpc_port);
2837-
let rpc_pubsub_port = find_available_port_in_range(bind_ip_addr, port_range).unwrap();
2838-
let rpc_pubsub_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), rpc_pubsub_port);
2839-
2840-
let broadcast = vec![UdpSocket::bind("0.0.0.0:0").unwrap()];
2841-
let retransmit_socket = UdpSocket::bind("0.0.0.0:0").unwrap();
2842-
let serve_repair = UdpSocket::bind("127.0.0.1:0").unwrap();
2843-
let ancestor_hashes_requests = UdpSocket::bind("0.0.0.0:0").unwrap();
2835+
bind_two_in_range_with_offset(localhost_ip_addr, port_range, QUIC_PORT_OFFSET).unwrap();
2836+
let tpu_vote = UdpSocket::bind(&localhost_bind_addr).unwrap();
2837+
let repair = UdpSocket::bind(&localhost_bind_addr).unwrap();
2838+
let rpc_port = find_available_port_in_range(localhost_ip_addr, port_range).unwrap();
2839+
let rpc_addr = SocketAddr::new(localhost_ip_addr, rpc_port);
2840+
let rpc_pubsub_port = find_available_port_in_range(localhost_ip_addr, port_range).unwrap();
2841+
let rpc_pubsub_addr = SocketAddr::new(localhost_ip_addr, rpc_pubsub_port);
2842+
let broadcast = vec![UdpSocket::bind(&unspecified_bind_addr).unwrap()];
2843+
let retransmit_socket = UdpSocket::bind(&unspecified_bind_addr).unwrap();
2844+
let serve_repair = UdpSocket::bind(&localhost_bind_addr).unwrap();
2845+
let ancestor_hashes_requests = UdpSocket::bind(&unspecified_bind_addr).unwrap();
28442846

28452847
let mut info = ContactInfo::new(
28462848
*pubkey,

0 commit comments

Comments
 (0)