Skip to content

Commit d655a26

Browse files
committed
allow multicast addresses for TVU
1 parent 4b2a5a5 commit d655a26

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed

gossip/src/contact_info.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ impl ContactInfo {
329329
num_addrs: self.addrs.len(),
330330
})?;
331331
let socket = SocketAddr::new(*addr, port);
332-
sanitize_socket(&socket)?;
332+
sanitize_socket(&socket, key)?;
333333
return Ok(socket);
334334
}
335335
}
@@ -349,7 +349,11 @@ impl ContactInfo {
349349
}
350350

351351
pub fn set_socket(&mut self, key: u8, socket: SocketAddr) -> Result<(), Error> {
352-
sanitize_socket(&socket)?;
352+
if key == SOCKET_TAG_TVU {
353+
sanitize_turbine_socket(&socket)?
354+
} else {
355+
sanitize_socket(&socket, key)?;
356+
}
353357
// Remove the old entry associated with this key (if any).
354358
self.remove_socket(key);
355359
// Find the index at which the new socket entry would be inserted into
@@ -463,7 +467,7 @@ impl ContactInfo {
463467
// Only for tests and simulations.
464468
pub fn new_with_socketaddr(pubkey: &Pubkey, socket: &SocketAddr) -> Self {
465469
use Protocol::{QUIC, UDP};
466-
assert_matches!(sanitize_socket(socket), Ok(()));
470+
assert_matches!(sanitize_socket(socket, SOCKET_TAG_GOSSIP), Ok(()));
467471
let mut node = Self::new(
468472
*pubkey,
469473
solana_time_utils::timestamp(), // wallclock,
@@ -576,7 +580,7 @@ impl TryFrom<ContactInfoLite> for ContactInfo {
576580
continue;
577581
};
578582
let socket = SocketAddr::new(addr, port);
579-
if sanitize_socket(&socket).is_ok() {
583+
if sanitize_socket(&socket, key).is_ok() {
580584
*entry = socket;
581585
}
582586
}
@@ -593,20 +597,31 @@ impl Sanitize for ContactInfo {
593597
}
594598
}
595599

596-
pub(crate) fn sanitize_socket(socket: &SocketAddr) -> Result<(), Error> {
600+
pub(crate) fn sanitize_socket(socket: &SocketAddr, key: u8) -> Result<(), Error> {
597601
if socket.port() == 0u16 {
598602
return Err(Error::InvalidPort(socket.port()));
599603
}
600604
let addr = socket.ip();
601605
if addr.is_unspecified() {
602606
return Err(Error::UnspecifiedIpAddr(addr));
603607
}
604-
if addr.is_multicast() {
608+
if key != SOCKET_TAG_TVU && addr.is_multicast() {
605609
return Err(Error::MulticastIpAddr(addr));
606610
}
607611
Ok(())
608612
}
609613

614+
pub(crate) fn sanitize_turbine_socket(socket: &SocketAddr) -> Result<(), Error> {
615+
if socket.port() == 0u16 {
616+
return Err(Error::InvalidPort(socket.port()));
617+
}
618+
let addr = socket.ip();
619+
if addr.is_unspecified() {
620+
return Err(Error::UnspecifiedIpAddr(addr));
621+
}
622+
Ok(())
623+
}
624+
610625
// Sanitizes deserialized IpAddr and socket entries.
611626
fn sanitize_entries(addrs: &[IpAddr], sockets: &[SocketEntry]) -> Result<(), Error> {
612627
// Verify that all IP addresses are unique.
@@ -863,7 +878,7 @@ mod tests {
863878
let addr = addrs.choose(&mut rng).unwrap();
864879
let socket = SocketAddr::new(*addr, new_rand_port(&mut rng));
865880
let key = rng.gen_range(KEYS.start..KEYS.end);
866-
if sanitize_socket(&socket).is_ok() {
881+
if sanitize_socket(&socket, key).is_ok() {
867882
sockets.insert(key, socket);
868883
assert_matches!(node.set_socket(key, socket), Ok(()));
869884
assert_matches!(sanitize_entries(&node.addrs, &node.sockets), Ok(()));
@@ -877,7 +892,7 @@ mod tests {
877892
assert_eq!(
878893
node.cache[usize::from(key)],
879894
socket
880-
.filter(|socket| sanitize_socket(socket).is_ok())
895+
.filter(|socket| sanitize_socket(socket, key).is_ok())
881896
.copied()
882897
.unwrap_or(SOCKET_ADDR_UNSPECIFIED),
883898
);
@@ -1036,7 +1051,7 @@ mod tests {
10361051
fn test_new_with_socketaddr() {
10371052
let mut rng = rand::thread_rng();
10381053
let socket = repeat_with(|| new_rand_socket(&mut rng))
1039-
.filter(|socket| matches!(sanitize_socket(socket), Ok(())))
1054+
.filter(|socket| matches!(sanitize_socket(socket, SOCKET_TAG_GOSSIP), Ok(())))
10401055
.find(|socket| socket.port().checked_add(11).is_some())
10411056
.unwrap();
10421057
let node = ContactInfo::new_with_socketaddr(&Keypair::new().pubkey(), &socket);
@@ -1047,7 +1062,7 @@ mod tests {
10471062
fn test_sanitize_quic_offset() {
10481063
let mut rng = rand::thread_rng();
10491064
let socket = repeat_with(|| new_rand_socket(&mut rng))
1050-
.filter(|socket| matches!(sanitize_socket(socket), Ok(())))
1065+
.filter(|socket| matches!(sanitize_socket(socket, SOCKET_TAG_GOSSIP), Ok(())))
10511066
.find(|socket| socket.port().checked_add(QUIC_PORT_OFFSET).is_some())
10521067
.unwrap();
10531068
let mut other = get_quic_socket(&socket).unwrap();
@@ -1079,7 +1094,7 @@ mod tests {
10791094
rng.gen(), // shred_version
10801095
);
10811096
let socket = repeat_with(|| new_rand_socket(&mut rng))
1082-
.filter(|socket| matches!(sanitize_socket(socket), Ok(())))
1097+
.filter(|socket| matches!(sanitize_socket(socket, SOCKET_TAG_GOSSIP), Ok(())))
10831098
.find(|socket| socket.port().checked_add(QUIC_PORT_OFFSET).is_some())
10841099
.unwrap();
10851100
// TPU socket.

gossip/src/legacy_contact_info.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ macro_rules! get_socket {
5353
#[cfg(test)]
5454
pub(crate) fn $name(&self) -> Option<SocketAddr> {
5555
let socket = self.$name;
56-
sanitize_socket(&socket).ok()?;
56+
sanitize_socket(&socket, 0).ok()?;
5757
Some(socket)
5858
}
5959
};
@@ -64,7 +64,7 @@ macro_rules! get_socket {
6464
Protocol::QUIC => self.$quic,
6565
Protocol::UDP => self.$name,
6666
};
67-
sanitize_socket(&socket).ok()?;
67+
sanitize_socket(&socket, 0).ok()?;
6868
Some(socket)
6969
}
7070
};
@@ -126,7 +126,7 @@ impl LegacyContactInfo {
126126

127127
pub(crate) fn gossip(&self) -> Option<SocketAddr> {
128128
let socket = self.gossip;
129-
crate::contact_info::sanitize_socket(&socket).ok()?;
129+
crate::contact_info::sanitize_socket(&socket, 0).ok()?;
130130
Some(socket)
131131
}
132132

0 commit comments

Comments
 (0)