@@ -17,7 +17,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Quic.Internal;
17
17
internal sealed class QuicConnectionListener : IMultiplexedConnectionListener , IAsyncDisposable
18
18
{
19
19
private readonly ILogger _log ;
20
- private readonly TlsConnectionCallbackOptions _tlsConnectionOptions ;
20
+ private readonly TlsConnectionCallbackOptions _tlsConnectionCallbackOptions ;
21
21
private readonly QuicTransportContext _context ;
22
22
private readonly QuicListenerOptions _quicListenerOptions ;
23
23
private bool _disposed ;
@@ -28,7 +28,7 @@ public QuicConnectionListener(
28
28
QuicTransportOptions options ,
29
29
ILogger log ,
30
30
EndPoint endpoint ,
31
- TlsConnectionCallbackOptions tlsConnectionOptions )
31
+ TlsConnectionCallbackOptions tlsConnectionCallbackOptions )
32
32
{
33
33
if ( ! QuicListener . IsSupported )
34
34
{
@@ -40,35 +40,38 @@ public QuicConnectionListener(
40
40
throw new InvalidOperationException ( $ "QUIC doesn't support listening on the configured endpoint type. Expected { nameof ( IPEndPoint ) } but got { endpoint . GetType ( ) . Name } .") ;
41
41
}
42
42
43
- if ( tlsConnectionOptions . ApplicationProtocols . Count == 0 )
43
+ if ( tlsConnectionCallbackOptions . ApplicationProtocols . Count == 0 )
44
44
{
45
45
throw new InvalidOperationException ( "No application protocols specified." ) ;
46
46
}
47
47
48
48
_log = log ;
49
- _tlsConnectionOptions = tlsConnectionOptions ;
49
+ _tlsConnectionCallbackOptions = tlsConnectionCallbackOptions ;
50
50
_context = new QuicTransportContext ( _log , options ) ;
51
51
_quicListenerOptions = new QuicListenerOptions
52
52
{
53
- ApplicationProtocols = _tlsConnectionOptions . ApplicationProtocols ,
53
+ ApplicationProtocols = _tlsConnectionCallbackOptions . ApplicationProtocols ,
54
54
ListenEndPoint = listenEndPoint ,
55
55
ListenBacklog = options . Backlog ,
56
56
ConnectionOptionsCallback = async ( connection , helloInfo , cancellationToken ) =>
57
57
{
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.
58
61
_currentAcceptingConnection = new QuicConnectionContext ( connection , _context ) ;
59
62
60
63
var context = new TlsConnectionCallbackContext
61
64
{
62
65
ClientHelloInfo = helloInfo ,
63
- State = _tlsConnectionOptions . OnConnectionState ,
66
+ State = _tlsConnectionCallbackOptions . OnConnectionState ,
64
67
Connection = _currentAcceptingConnection ,
65
68
} ;
66
- var serverAuthenticationOptions = await _tlsConnectionOptions . OnConnection ( context , cancellationToken ) ;
69
+ var serverAuthenticationOptions = await _tlsConnectionCallbackOptions . OnConnection ( context , cancellationToken ) ;
67
70
68
71
// If the callback didn't set protocols then use the listener's list of protocols.
69
72
if ( serverAuthenticationOptions . ApplicationProtocols == null )
70
73
{
71
- serverAuthenticationOptions . ApplicationProtocols = _tlsConnectionOptions . ApplicationProtocols ;
74
+ serverAuthenticationOptions . ApplicationProtocols = _tlsConnectionCallbackOptions . ApplicationProtocols ;
72
75
}
73
76
74
77
// If the SslServerAuthenticationOptions doesn't have a cert or protocols then the
@@ -114,7 +117,9 @@ public async ValueTask CreateListenerAsync()
114
117
{
115
118
_listener = await QuicListener . ListenAsync ( _quicListenerOptions ) ;
116
119
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.
118
123
EndPoint = _listener . LocalEndPoint ;
119
124
}
120
125
@@ -128,6 +133,8 @@ public async ValueTask CreateListenerAsync()
128
133
try
129
134
{
130
135
var quicConnection = await _listener . AcceptConnectionAsync ( cancellationToken ) ;
136
+
137
+ // _currentAcceptingConnection is set inside ConnectionOptionsCallback.
131
138
var connectionContext = _currentAcceptingConnection ;
132
139
133
140
// Verify the connection context was created and set correctly.
0 commit comments