@@ -17,7 +17,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Quic.Internal;
1717internal sealed class QuicConnectionListener : IMultiplexedConnectionListener , IAsyncDisposable
1818{
1919 private readonly ILogger _log ;
20- private readonly TlsConnectionCallbackOptions _tlsConnectionOptions ;
20+ private readonly TlsConnectionCallbackOptions _tlsConnectionCallbackOptions ;
2121 private readonly QuicTransportContext _context ;
2222 private readonly QuicListenerOptions _quicListenerOptions ;
2323 private bool _disposed ;
@@ -28,7 +28,7 @@ public QuicConnectionListener(
2828 QuicTransportOptions options ,
2929 ILogger log ,
3030 EndPoint endpoint ,
31- TlsConnectionCallbackOptions tlsConnectionOptions )
31+ TlsConnectionCallbackOptions tlsConnectionCallbackOptions )
3232 {
3333 if ( ! QuicListener . IsSupported )
3434 {
@@ -40,35 +40,38 @@ public QuicConnectionListener(
4040 throw new InvalidOperationException ( $ "QUIC doesn't support listening on the configured endpoint type. Expected { nameof ( IPEndPoint ) } but got { endpoint . GetType ( ) . Name } .") ;
4141 }
4242
43- if ( tlsConnectionOptions . ApplicationProtocols . Count == 0 )
43+ if ( tlsConnectionCallbackOptions . ApplicationProtocols . Count == 0 )
4444 {
4545 throw new InvalidOperationException ( "No application protocols specified." ) ;
4646 }
4747
4848 _log = log ;
49- _tlsConnectionOptions = tlsConnectionOptions ;
49+ _tlsConnectionCallbackOptions = tlsConnectionCallbackOptions ;
5050 _context = new QuicTransportContext ( _log , options ) ;
5151 _quicListenerOptions = new QuicListenerOptions
5252 {
53- ApplicationProtocols = _tlsConnectionOptions . ApplicationProtocols ,
53+ ApplicationProtocols = _tlsConnectionCallbackOptions . ApplicationProtocols ,
5454 ListenEndPoint = listenEndPoint ,
5555 ListenBacklog = options . Backlog ,
5656 ConnectionOptionsCallback = async ( connection , helloInfo , cancellationToken ) =>
5757 {
58+ // Create the connection context inside the callback because it's passed
59+ // to the connection callback. The field is then read once AcceptConnectionAsync
60+ // finishes awaiting.
5861 _currentAcceptingConnection = new QuicConnectionContext ( connection , _context ) ;
5962
6063 var context = new TlsConnectionCallbackContext
6164 {
6265 ClientHelloInfo = helloInfo ,
63- State = _tlsConnectionOptions . OnConnectionState ,
66+ State = _tlsConnectionCallbackOptions . OnConnectionState ,
6467 Connection = _currentAcceptingConnection ,
6568 } ;
66- var serverAuthenticationOptions = await _tlsConnectionOptions . OnConnection ( context , cancellationToken ) ;
69+ var serverAuthenticationOptions = await _tlsConnectionCallbackOptions . OnConnection ( context , cancellationToken ) ;
6770
6871 // If the callback didn't set protocols then use the listener's list of protocols.
6972 if ( serverAuthenticationOptions . ApplicationProtocols == null )
7073 {
71- serverAuthenticationOptions . ApplicationProtocols = _tlsConnectionOptions . ApplicationProtocols ;
74+ serverAuthenticationOptions . ApplicationProtocols = _tlsConnectionCallbackOptions . ApplicationProtocols ;
7275 }
7376
7477 // If the SslServerAuthenticationOptions doesn't have a cert or protocols then the
@@ -114,7 +117,9 @@ public async ValueTask CreateListenerAsync()
114117 {
115118 _listener = await QuicListener . ListenAsync ( _quicListenerOptions ) ;
116119
117- // Listener endpoint will resolve an ephemeral port, e.g. 127.0.0.1:0, into the actual port.
120+ // EndPoint could be configured with an ephemeral port of 0.
121+ // Listener endpoint will resolve an ephemeral port, e.g. 127.0.0.1:0, into the actual port
122+ // so we need to update the public listener endpoint property.
118123 EndPoint = _listener . LocalEndPoint ;
119124 }
120125
@@ -128,6 +133,8 @@ public async ValueTask CreateListenerAsync()
128133 try
129134 {
130135 var quicConnection = await _listener . AcceptConnectionAsync ( cancellationToken ) ;
136+
137+ // _currentAcceptingConnection is set inside ConnectionOptionsCallback.
131138 var connectionContext = _currentAcceptingConnection ;
132139
133140 // Verify the connection context was created and set correctly.
0 commit comments