@@ -329,7 +329,7 @@ impl ContactInfo {
329
329
num_addrs : self . addrs . len ( ) ,
330
330
} ) ?;
331
331
let socket = SocketAddr :: new ( * addr, port) ;
332
- sanitize_socket ( & socket) ?;
332
+ sanitize_socket ( & socket, key ) ?;
333
333
return Ok ( socket) ;
334
334
}
335
335
}
@@ -349,7 +349,11 @@ impl ContactInfo {
349
349
}
350
350
351
351
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
+ }
353
357
// Remove the old entry associated with this key (if any).
354
358
self . remove_socket ( key) ;
355
359
// Find the index at which the new socket entry would be inserted into
@@ -463,7 +467,7 @@ impl ContactInfo {
463
467
// Only for tests and simulations.
464
468
pub fn new_with_socketaddr ( pubkey : & Pubkey , socket : & SocketAddr ) -> Self {
465
469
use Protocol :: { QUIC , UDP } ;
466
- assert_matches ! ( sanitize_socket( socket) , Ok ( ( ) ) ) ;
470
+ assert_matches ! ( sanitize_socket( socket, SOCKET_TAG_GOSSIP ) , Ok ( ( ) ) ) ;
467
471
let mut node = Self :: new (
468
472
* pubkey,
469
473
solana_time_utils:: timestamp ( ) , // wallclock,
@@ -576,7 +580,7 @@ impl TryFrom<ContactInfoLite> for ContactInfo {
576
580
continue ;
577
581
} ;
578
582
let socket = SocketAddr :: new ( addr, port) ;
579
- if sanitize_socket ( & socket) . is_ok ( ) {
583
+ if sanitize_socket ( & socket, key ) . is_ok ( ) {
580
584
* entry = socket;
581
585
}
582
586
}
@@ -593,20 +597,31 @@ impl Sanitize for ContactInfo {
593
597
}
594
598
}
595
599
596
- pub ( crate ) fn sanitize_socket ( socket : & SocketAddr ) -> Result < ( ) , Error > {
600
+ pub ( crate ) fn sanitize_socket ( socket : & SocketAddr , key : u8 ) -> Result < ( ) , Error > {
597
601
if socket. port ( ) == 0u16 {
598
602
return Err ( Error :: InvalidPort ( socket. port ( ) ) ) ;
599
603
}
600
604
let addr = socket. ip ( ) ;
601
605
if addr. is_unspecified ( ) {
602
606
return Err ( Error :: UnspecifiedIpAddr ( addr) ) ;
603
607
}
604
- if addr. is_multicast ( ) {
608
+ if key != SOCKET_TAG_TVU && addr. is_multicast ( ) {
605
609
return Err ( Error :: MulticastIpAddr ( addr) ) ;
606
610
}
607
611
Ok ( ( ) )
608
612
}
609
613
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
+
610
625
// Sanitizes deserialized IpAddr and socket entries.
611
626
fn sanitize_entries ( addrs : & [ IpAddr ] , sockets : & [ SocketEntry ] ) -> Result < ( ) , Error > {
612
627
// Verify that all IP addresses are unique.
@@ -863,7 +878,7 @@ mod tests {
863
878
let addr = addrs. choose ( & mut rng) . unwrap ( ) ;
864
879
let socket = SocketAddr :: new ( * addr, new_rand_port ( & mut rng) ) ;
865
880
let key = rng. gen_range ( KEYS . start ..KEYS . end ) ;
866
- if sanitize_socket ( & socket) . is_ok ( ) {
881
+ if sanitize_socket ( & socket, key ) . is_ok ( ) {
867
882
sockets. insert ( key, socket) ;
868
883
assert_matches ! ( node. set_socket( key, socket) , Ok ( ( ) ) ) ;
869
884
assert_matches ! ( sanitize_entries( & node. addrs, & node. sockets) , Ok ( ( ) ) ) ;
@@ -877,7 +892,7 @@ mod tests {
877
892
assert_eq ! (
878
893
node. cache[ usize :: from( key) ] ,
879
894
socket
880
- . filter( |socket| sanitize_socket( socket) . is_ok( ) )
895
+ . filter( |socket| sanitize_socket( socket, key ) . is_ok( ) )
881
896
. copied( )
882
897
. unwrap_or( SOCKET_ADDR_UNSPECIFIED ) ,
883
898
) ;
@@ -1036,7 +1051,7 @@ mod tests {
1036
1051
fn test_new_with_socketaddr ( ) {
1037
1052
let mut rng = rand:: thread_rng ( ) ;
1038
1053
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 ( ( ) ) ) )
1040
1055
. find ( |socket| socket. port ( ) . checked_add ( 11 ) . is_some ( ) )
1041
1056
. unwrap ( ) ;
1042
1057
let node = ContactInfo :: new_with_socketaddr ( & Keypair :: new ( ) . pubkey ( ) , & socket) ;
@@ -1047,7 +1062,7 @@ mod tests {
1047
1062
fn test_sanitize_quic_offset ( ) {
1048
1063
let mut rng = rand:: thread_rng ( ) ;
1049
1064
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 ( ( ) ) ) )
1051
1066
. find ( |socket| socket. port ( ) . checked_add ( QUIC_PORT_OFFSET ) . is_some ( ) )
1052
1067
. unwrap ( ) ;
1053
1068
let mut other = get_quic_socket ( & socket) . unwrap ( ) ;
@@ -1079,7 +1094,7 @@ mod tests {
1079
1094
rng. gen ( ) , // shred_version
1080
1095
) ;
1081
1096
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 ( ( ) ) ) )
1083
1098
. find ( |socket| socket. port ( ) . checked_add ( QUIC_PORT_OFFSET ) . is_some ( ) )
1084
1099
. unwrap ( ) ;
1085
1100
// TPU socket.
0 commit comments