| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using IceRpc.Transports.Internal; |
| | | 4 | | using IceRpc.Transports.Quic.Internal; |
| | | 5 | | using System.Net; |
| | | 6 | | using System.Net.Quic; |
| | | 7 | | using System.Net.Security; |
| | | 8 | | using System.Runtime.Versioning; |
| | | 9 | | |
| | | 10 | | namespace IceRpc.Transports.Quic; |
| | | 11 | | |
| | | 12 | | /// <summary>Implements <see cref="IMultiplexedClientTransport"/> using QUIC.</summary> |
| | | 13 | | [SupportedOSPlatform("linux")] |
| | | 14 | | [SupportedOSPlatform("macos")] |
| | | 15 | | [SupportedOSPlatform("windows")] |
| | | 16 | | public class QuicClientTransport : IMultiplexedClientTransport |
| | | 17 | | { |
| | | 18 | | /// <inheritdoc/> |
| | 15 | 19 | | public string DefaultName => "quic"; |
| | | 20 | | |
| | | 21 | | /// <inheritdoc/> |
| | 0 | 22 | | public bool IsSslRequired(string? transportName) => transportName is null or "quic" ? true : |
| | 0 | 23 | | throw new NotSupportedException($"The QUIC client transport does not support transport '{transportName}'."); |
| | | 24 | | |
| | | 25 | | private readonly QuicClientTransportOptions _quicTransportOptions; |
| | | 26 | | |
| | | 27 | | /// <summary>Constructs a QUIC client transport.</summary> |
| | | 28 | | /// <param name="options">The options to configure the QUIC client transport.</param> |
| | 242 | 29 | | public QuicClientTransport(QuicClientTransportOptions options) => _quicTransportOptions = options; |
| | | 30 | | |
| | | 31 | | /// <summary>Constructs a QUIC client transport.</summary> |
| | | 32 | | public QuicClientTransport() |
| | 4 | 33 | | : this(new QuicClientTransportOptions()) |
| | 4 | 34 | | { |
| | 4 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc/> |
| | | 38 | | public IMultiplexedConnection CreateConnection( |
| | | 39 | | TransportAddress transportAddress, |
| | | 40 | | MultiplexedConnectionOptions options, |
| | | 41 | | SslClientAuthenticationOptions? clientAuthenticationOptions) |
| | 122 | 42 | | { |
| | 122 | 43 | | if (!QuicConnection.IsSupported) |
| | 0 | 44 | | { |
| | 0 | 45 | | throw new NotSupportedException( |
| | 0 | 46 | | "The QUIC client transport is not available on this system. Please review the Platform Dependencies for |
| | | 47 | | } |
| | | 48 | | |
| | 122 | 49 | | if (transportAddress.TransportName is string name && name != DefaultName) |
| | 0 | 50 | | { |
| | 0 | 51 | | throw new NotSupportedException($"The QUIC client transport does not support transport '{name}'."); |
| | | 52 | | } |
| | | 53 | | |
| | 122 | 54 | | if (transportAddress.Params.Count > 0) |
| | 1 | 55 | | { |
| | 1 | 56 | | throw new ArgumentException( |
| | 1 | 57 | | "The transport address contains parameters that are not valid for the QUIC client transport.", |
| | 1 | 58 | | nameof(transportAddress)); |
| | | 59 | | } |
| | | 60 | | |
| | 121 | 61 | | if (clientAuthenticationOptions is null) |
| | 0 | 62 | | { |
| | 0 | 63 | | throw new ArgumentNullException( |
| | 0 | 64 | | nameof(clientAuthenticationOptions), |
| | 0 | 65 | | "The QUIC client transport requires the SSL client authentication options to be set."); |
| | | 66 | | } |
| | | 67 | | |
| | 121 | 68 | | if (clientAuthenticationOptions.ApplicationProtocols |
| | 121 | 69 | | is not List<SslApplicationProtocol> applicationProtocols || applicationProtocols.Count == 0) |
| | 0 | 70 | | { |
| | 0 | 71 | | throw new ArgumentException( |
| | 0 | 72 | | "The QUIC client transport requires ApplicationProtocols to be set in the SSL client authentication opti |
| | 0 | 73 | | nameof(clientAuthenticationOptions)); |
| | | 74 | | } |
| | | 75 | | |
| | 121 | 76 | | clientAuthenticationOptions = clientAuthenticationOptions.ShallowClone(); |
| | 121 | 77 | | clientAuthenticationOptions.TargetHost ??= transportAddress.Host; |
| | | 78 | | |
| | 121 | 79 | | EndPoint endPoint = IPAddress.TryParse(transportAddress.Host, out IPAddress? ipAddress) ? |
| | 121 | 80 | | new IPEndPoint(ipAddress, transportAddress.Port) : |
| | 121 | 81 | | new DnsEndPoint(transportAddress.Host, transportAddress.Port); |
| | | 82 | | |
| | 121 | 83 | | var quicClientOptions = new QuicClientConnectionOptions |
| | 121 | 84 | | { |
| | 121 | 85 | | ClientAuthenticationOptions = clientAuthenticationOptions, |
| | 121 | 86 | | DefaultCloseErrorCode = (int)MultiplexedConnectionCloseError.Aborted, |
| | 121 | 87 | | DefaultStreamErrorCode = 0, |
| | 121 | 88 | | HandshakeTimeout = options.HandshakeTimeout, |
| | 121 | 89 | | IdleTimeout = _quicTransportOptions.IdleTimeout, |
| | 121 | 90 | | InitialReceiveWindowSizes = _quicTransportOptions.InitialReceiveWindowSizes, |
| | 121 | 91 | | KeepAliveInterval = _quicTransportOptions.KeepAliveInterval, |
| | 121 | 92 | | LocalEndPoint = _quicTransportOptions.LocalNetworkAddress, |
| | 121 | 93 | | RemoteEndPoint = endPoint, |
| | 121 | 94 | | MaxInboundBidirectionalStreams = options.MaxBidirectionalStreams, |
| | 121 | 95 | | MaxInboundUnidirectionalStreams = options.MaxUnidirectionalStreams |
| | 121 | 96 | | }; |
| | | 97 | | |
| | 121 | 98 | | return new QuicMultiplexedClientConnection(options, quicClientOptions); |
| | 121 | 99 | | } |
| | | 100 | | } |