| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using IceRpc.Transports.Internal; |
| | | 4 | | using IceRpc.Transports.Tcp.Internal; |
| | | 5 | | using System.Net.Security; |
| | | 6 | | |
| | | 7 | | namespace IceRpc.Transports.Tcp; |
| | | 8 | | |
| | | 9 | | /// <summary>Implements <see cref="IDuplexClientTransport" /> for the tcp transport.</summary> |
| | | 10 | | public class TcpClientTransport : IDuplexClientTransport |
| | | 11 | | { |
| | | 12 | | /// <inheritdoc/> |
| | 7 | 13 | | public string DefaultName => "tcp"; |
| | | 14 | | |
| | | 15 | | /// <inheritdoc/> |
| | 16 | 16 | | public bool IsSslRequired(string? transportName) => transportName switch |
| | 16 | 17 | | { |
| | 16 | 18 | | null or "tcp" => false, |
| | 0 | 19 | | "ssl" => true, |
| | 0 | 20 | | _ => throw new NotSupportedException($"The TCP client transport does not support transport '{transportName}'.") |
| | 16 | 21 | | }; |
| | | 22 | | |
| | | 23 | | private readonly TcpClientTransportOptions _options; |
| | | 24 | | |
| | | 25 | | /// <summary>Constructs a <see cref="TcpClientTransport" />.</summary> |
| | | 26 | | public TcpClientTransport() |
| | 29 | 27 | | : this(new TcpClientTransportOptions()) |
| | 29 | 28 | | { |
| | 29 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary>Constructs a <see cref="TcpClientTransport" />.</summary> |
| | | 32 | | /// <param name="options">The transport options.</param> |
| | 244 | 33 | | public TcpClientTransport(TcpClientTransportOptions options) => _options = options; |
| | | 34 | | |
| | | 35 | | /// <inheritdoc/> |
| | | 36 | | public IDuplexConnection CreateConnection( |
| | | 37 | | TransportAddress transportAddress, |
| | | 38 | | DuplexConnectionOptions options, |
| | | 39 | | SslClientAuthenticationOptions? clientAuthenticationOptions) |
| | 141 | 40 | | { |
| | | 41 | | // "ssl" is only accepted for the ice protocol, identified by the ALPN. |
| | 141 | 42 | | if (transportAddress.TransportName == "ssl") |
| | 2 | 43 | | { |
| | 2 | 44 | | if (clientAuthenticationOptions is null) |
| | 1 | 45 | | { |
| | 1 | 46 | | throw new ArgumentNullException( |
| | 1 | 47 | | nameof(clientAuthenticationOptions), |
| | 1 | 48 | | "The SSL client transport requires the SSL client authentication options to be set."); |
| | | 49 | | } |
| | 1 | 50 | | else if (clientAuthenticationOptions.ApplicationProtocols is not List<SslApplicationProtocol> alpnProtocols |
| | 1 | 51 | | alpnProtocols.Count != 1 || |
| | 1 | 52 | | alpnProtocols[0] != new SslApplicationProtocol("ice")) |
| | 0 | 53 | | { |
| | 0 | 54 | | throw new NotSupportedException("The 'ssl' transport name is only supported with the ice protocol."); |
| | | 55 | | } |
| | 1 | 56 | | } |
| | 139 | 57 | | else if (transportAddress.TransportName is string name && name != "tcp") |
| | 1 | 58 | | { |
| | 1 | 59 | | throw new NotSupportedException($"The TCP client transport does not support transport '{name}'."); |
| | | 60 | | } |
| | | 61 | | |
| | 139 | 62 | | if (transportAddress.Params.Count > 0) |
| | 4 | 63 | | { |
| | 4 | 64 | | throw new ArgumentException( |
| | 4 | 65 | | "The transport address contains parameters that are not valid for the TCP client transport.", |
| | 4 | 66 | | nameof(transportAddress)); |
| | | 67 | | } |
| | | 68 | | |
| | 135 | 69 | | if (clientAuthenticationOptions is not null) |
| | 33 | 70 | | { |
| | 33 | 71 | | clientAuthenticationOptions = clientAuthenticationOptions.ShallowClone(); |
| | 33 | 72 | | clientAuthenticationOptions.TargetHost ??= transportAddress.Host; |
| | 33 | 73 | | } |
| | | 74 | | |
| | 135 | 75 | | return new TcpClientConnection( |
| | 135 | 76 | | transportAddress, |
| | 135 | 77 | | clientAuthenticationOptions, |
| | 135 | 78 | | options.Pool, |
| | 135 | 79 | | options.MinSegmentSize, |
| | 135 | 80 | | _options); |
| | 135 | 81 | | } |
| | | 82 | | } |