@@ -17,7 +17,7 @@ use s2n_quic_core::{
17
17
use std:: {
18
18
hash:: { BuildHasherDefault , Hasher } ,
19
19
net:: { Ipv4Addr , SocketAddr } ,
20
- sync:: { Arc , Mutex , Weak } ,
20
+ sync:: { Arc , Mutex , RwLock , Weak } ,
21
21
time:: Duration ,
22
22
} ;
23
23
64
64
// needed.
65
65
pub ( super ) peers : fixed_map:: Map < SocketAddr , Arc < Entry > > ,
66
66
67
- // Stores the set of SocketAddr for which we received a UnknownPathSecret packet.
68
- // When handshake_with is called we will allow a new handshake if this contains a socket, this
69
- // is a temporary solution until we implement proper background handshaking.
70
- pub ( super ) requested_handshakes : flurry:: HashSet < SocketAddr > ,
71
-
72
67
// All known entries.
73
68
pub ( super ) ids : fixed_map:: Map < Id , Arc < Entry > , BuildHasherDefault < NoopIdHasher > > ,
74
69
78
73
// FIXME: This will get replaced with sending on a handshake socket associated with the map.
79
74
pub ( super ) control_socket : Arc < std:: net:: UdpSocket > ,
80
75
76
+ #[ allow( clippy:: type_complexity) ]
77
+ pub ( super ) request_handshake : RwLock < Option < Box < dyn Fn ( SocketAddr ) + Send + Sync > > > ,
78
+
81
79
cleaner : Cleaner ,
82
80
83
81
init_time : Timestamp ,
@@ -131,13 +129,13 @@ where
131
129
rehandshake_period : Duration :: from_secs ( 3600 * 24 ) ,
132
130
peers : fixed_map:: Map :: with_capacity ( capacity, Default :: default ( ) ) ,
133
131
ids : fixed_map:: Map :: with_capacity ( capacity, Default :: default ( ) ) ,
134
- requested_handshakes : Default :: default ( ) ,
135
132
cleaner : Cleaner :: new ( ) ,
136
133
signer,
137
134
control_socket,
138
135
init_time,
139
136
clock,
140
137
subscriber,
138
+ request_handshake : RwLock :: new ( None ) ,
141
139
} ;
142
140
143
141
let state = Arc :: new ( state) ;
@@ -152,19 +150,36 @@ where
152
150
}
153
151
154
152
pub fn request_handshake ( & self , peer : SocketAddr ) {
155
- // The length is reset as part of cleanup to 5000.
156
- let handshakes = self . requested_handshakes . pin ( ) ;
157
- if handshakes. len ( ) <= 6000 {
158
- handshakes. insert ( peer) ;
159
- self . subscriber ( )
160
- . on_path_secret_map_background_handshake_requested (
161
- event:: builder:: PathSecretMapBackgroundHandshakeRequested {
162
- peer_address : SocketAddress :: from ( peer) . into_event ( ) ,
163
- } ,
164
- ) ;
153
+ self . subscriber ( )
154
+ . on_path_secret_map_background_handshake_requested (
155
+ event:: builder:: PathSecretMapBackgroundHandshakeRequested {
156
+ peer_address : SocketAddress :: from ( peer) . into_event ( ) ,
157
+ } ,
158
+ ) ;
159
+
160
+ // Normally we'd expect callers to use the Subscriber to register interest in this, but the
161
+ // Map is typically created *before* the s2n_quic::Client with the dc provider registered.
162
+ //
163
+ // Users of the state tracker typically register the callback when creating a new s2n-quic
164
+ // client to handshake into this map.
165
+ if let Some ( callback) = self
166
+ . request_handshake
167
+ . read ( )
168
+ . unwrap_or_else ( |e| e. into_inner ( ) )
169
+ . as_deref ( )
170
+ {
171
+ ( callback) ( peer) ;
165
172
}
166
173
}
167
174
175
+ fn register_request_handshake ( & self , cb : Box < dyn Fn ( SocketAddr ) + Send + Sync > ) {
176
+ // FIXME: Maybe panic if already initialized?
177
+ * self
178
+ . request_handshake
179
+ . write ( )
180
+ . unwrap_or_else ( |e| e. into_inner ( ) ) = Some ( cb) ;
181
+ }
182
+
168
183
fn handle_unknown_secret (
169
184
& self ,
170
185
packet : & control:: unknown_path_secret:: Packet ,
@@ -370,17 +385,10 @@ where
370
385
self . peers . contains_key ( peer)
371
386
}
372
387
373
- fn needs_handshake ( & self , peer : & SocketAddr ) -> bool {
374
- self . requested_handshakes . pin ( ) . contains ( peer)
375
- }
376
-
377
388
fn on_new_path_secrets ( & self , entry : Arc < Entry > ) {
378
389
let id = * entry. id ( ) ;
379
390
let peer = entry. peer ( ) ;
380
391
381
- // On insert clear our interest in a handshake.
382
- self . requested_handshakes . pin ( ) . remove ( peer) ;
383
-
384
392
let ( same, other) = self . ids . insert ( id, entry. clone ( ) ) ;
385
393
if same. is_some ( ) {
386
394
// FIXME: Make insertion fallible and fail handshakes instead?
@@ -445,6 +453,10 @@ where
445
453
} ) ;
446
454
}
447
455
456
+ fn register_request_handshake ( & self , cb : Box < dyn Fn ( SocketAddr ) + Send + Sync > ) {
457
+ self . register_request_handshake ( cb) ;
458
+ }
459
+
448
460
fn get_by_addr_untracked ( & self , peer : & SocketAddr ) -> Option < ReadGuard < Arc < Entry > > > {
449
461
self . peers . get_by_key ( peer)
450
462
}
0 commit comments