| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using IceRpc.Ice.Codec; |
| | | 4 | | using IceRpc.Ice.Internal; |
| | | 5 | | using IceRpc.Ice.Operations.Internal; |
| | | 6 | | using IceRpc.Internal; |
| | | 7 | | using System.Buffers; |
| | | 8 | | using System.Collections.Immutable; |
| | | 9 | | using System.Diagnostics; |
| | | 10 | | using System.Globalization; |
| | | 11 | | using System.Runtime.CompilerServices; |
| | | 12 | | |
| | | 13 | | namespace IceRpc.Ice.Operations; |
| | | 14 | | |
| | | 15 | | /// <summary>Provides extension methods for <see cref="IceDecoder" /> to decode proxies.</summary> |
| | | 16 | | public static class IceProxyIceDecoderExtensions |
| | | 17 | | { |
| | | 18 | | /// <summary>Decodes a proxy struct.</summary> |
| | | 19 | | /// <typeparam name="TProxy">The type of the proxy struct to decode.</typeparam> |
| | | 20 | | /// <param name="decoder">The Ice decoder.</param> |
| | | 21 | | /// <returns>The decoded proxy, or <see langword="null" />.</returns> |
| | | 22 | | public static TProxy? DecodeProxy<TProxy>(this ref IceDecoder decoder) where TProxy : struct, IIceProxy => |
| | 52 | 23 | | decoder.DecodeServiceAddress() is ServiceAddress serviceAddress ? |
| | 52 | 24 | | CreateProxy<TProxy>(serviceAddress, decoder.DecodingContext) : null; |
| | | 25 | | |
| | | 26 | | private static TProxy CreateProxy<TProxy>(ServiceAddress serviceAddress, object? decodingContext) |
| | | 27 | | where TProxy : struct, IIceProxy |
| | 44 | 28 | | { |
| | 44 | 29 | | Debug.Assert(serviceAddress.Protocol is not null, "The Ice encoding does not support relative proxies."); |
| | | 30 | | |
| | 44 | 31 | | if (decodingContext is null) |
| | 33 | 32 | | { |
| | 33 | 33 | | return new TProxy { Invoker = InvalidInvoker.Instance, ServiceAddress = serviceAddress }; |
| | | 34 | | } |
| | | 35 | | else |
| | 11 | 36 | | { |
| | 11 | 37 | | var baseProxy = (IIceProxy)decodingContext; |
| | 11 | 38 | | return new TProxy |
| | 11 | 39 | | { |
| | 11 | 40 | | EncodeOptions = baseProxy.EncodeOptions, |
| | 11 | 41 | | Invoker = baseProxy.Invoker, |
| | 11 | 42 | | ServiceAddress = serviceAddress |
| | 11 | 43 | | }; |
| | | 44 | | } |
| | 44 | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary>Decodes a service address.</summary> |
| | | 48 | | /// <param name="decoder">The Ice decoder.</param> |
| | | 49 | | /// <returns>The decoded service address, or <see langword="null" />.</returns> |
| | | 50 | | private static ServiceAddress? DecodeServiceAddress(this ref IceDecoder decoder) |
| | 52 | 51 | | { |
| | 52 | 52 | | string path = new Identity(ref decoder).ToPath(); |
| | 52 | 53 | | return path != "/" ? decoder.DecodeServiceAddressCore(path) : null; |
| | 52 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary>Decodes a server address.</summary> |
| | | 57 | | /// <param name="decoder">The Ice decoder.</param> |
| | | 58 | | /// <param name="protocol">The protocol of this server address.</param> |
| | | 59 | | /// <returns>The server address decoded by this decoder.</returns> |
| | | 60 | | private static ServerAddress DecodeServerAddress(this ref IceDecoder decoder, Protocol protocol) |
| | 33 | 61 | | { |
| | | 62 | | // With the Ice encoding, the ice server addresses are transport-specific, with a transport-specific encoding. |
| | | 63 | | |
| | 33 | 64 | | ServerAddress? serverAddress = null; |
| | 33 | 65 | | var transportCode = (TransportCode)decoder.DecodeShort(); |
| | | 66 | | |
| | 33 | 67 | | int size = decoder.DecodeInt(); |
| | 33 | 68 | | if (size < 6) |
| | 0 | 69 | | { |
| | 0 | 70 | | throw new InvalidDataException($"The Ice encapsulation's size ({size}) is too small."); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | // Remove 6 bytes from the encapsulation size (4 for encapsulation size, 2 for encoding). |
| | 33 | 74 | | size -= 6; |
| | | 75 | | |
| | 33 | 76 | | byte encodingMajor = decoder.DecodeByte(); |
| | 33 | 77 | | byte encodingMinor = decoder.DecodeByte(); |
| | | 78 | | |
| | 33 | 79 | | if (decoder.Remaining < size) |
| | 0 | 80 | | { |
| | 0 | 81 | | throw new InvalidDataException($"The Ice encapsulation's size ({size}) is too big."); |
| | | 82 | | } |
| | | 83 | | |
| | 33 | 84 | | if (encodingMajor == 1 && encodingMinor <= 1) |
| | 33 | 85 | | { |
| | 33 | 86 | | long oldPos = decoder.Consumed; |
| | | 87 | | |
| | 33 | 88 | | if (protocol == Protocol.Ice) |
| | 16 | 89 | | { |
| | 16 | 90 | | switch (transportCode) |
| | | 91 | | { |
| | | 92 | | case TransportCode.Tcp: |
| | 11 | 93 | | serverAddress = decoder.DecodeTcpServerAddressBody(IceProxyIceEncoderExtensions.TcpName); |
| | 11 | 94 | | break; |
| | | 95 | | |
| | | 96 | | case TransportCode.Ssl: |
| | 2 | 97 | | serverAddress = decoder.DecodeTcpServerAddressBody(IceProxyIceEncoderExtensions.SslName); |
| | 2 | 98 | | break; |
| | | 99 | | |
| | | 100 | | case TransportCode.Uri: |
| | 1 | 101 | | serverAddress = new ServerAddress(new Uri(decoder.DecodeString())); |
| | 1 | 102 | | if (serverAddress.Value.Protocol != protocol) |
| | 0 | 103 | | { |
| | 0 | 104 | | throw new InvalidDataException( |
| | 0 | 105 | | $"Expected {protocol} server address but received '{serverAddress.Value}'."); |
| | | 106 | | } |
| | 1 | 107 | | break; |
| | | 108 | | |
| | | 109 | | default: |
| | | 110 | | // Create a server address for transport opaque |
| | 2 | 111 | | ImmutableDictionary<string, string>.Builder builder = |
| | 2 | 112 | | ImmutableDictionary.CreateBuilder<string, string>(); |
| | | 113 | | |
| | 2 | 114 | | if (encodingMinor == 0) |
| | 1 | 115 | | { |
| | 1 | 116 | | builder.Add("e", "1.0"); |
| | 1 | 117 | | } |
| | | 118 | | // else no e |
| | | 119 | | |
| | 2 | 120 | | builder.Add("t", ((short)transportCode).ToString(CultureInfo.InvariantCulture)); |
| | 2 | 121 | | { |
| | 2 | 122 | | using IMemoryOwner<byte> memoryOwner = MemoryPool<byte>.Shared.Rent(size); |
| | 2 | 123 | | Span<byte> span = memoryOwner.Memory.Span[0..size]; |
| | 2 | 124 | | decoder.CopyTo(span); |
| | 2 | 125 | | string value = Convert.ToBase64String(span); |
| | 2 | 126 | | builder.Add("v", value); |
| | 2 | 127 | | decoder.IncreaseCollectionAllocation(value.Length, Unsafe.SizeOf<char>()); |
| | 2 | 128 | | } |
| | | 129 | | |
| | 2 | 130 | | serverAddress = new ServerAddress( |
| | 2 | 131 | | Protocol.Ice, |
| | 2 | 132 | | host: "opaque", // not a real host obviously |
| | 2 | 133 | | port: Protocol.Ice.DefaultPort, |
| | 2 | 134 | | transport: IceProxyIceEncoderExtensions.OpaqueName, |
| | 2 | 135 | | builder.ToImmutable()); |
| | 2 | 136 | | break; |
| | | 137 | | } |
| | 16 | 138 | | } |
| | 17 | 139 | | else if (transportCode == TransportCode.Uri) |
| | 17 | 140 | | { |
| | | 141 | | // The server addresses of an Ice-encoded icerpc proxies only use TransportCode.Uri. |
| | 17 | 142 | | serverAddress = new ServerAddress(new Uri(decoder.DecodeString())); |
| | 17 | 143 | | if (serverAddress.Value.Protocol != protocol) |
| | 0 | 144 | | { |
| | 0 | 145 | | throw new InvalidDataException( |
| | 0 | 146 | | $"Expected {protocol} server address but received '{serverAddress.Value}'."); |
| | | 147 | | } |
| | 17 | 148 | | } |
| | | 149 | | |
| | 33 | 150 | | if (serverAddress is not null) |
| | 33 | 151 | | { |
| | | 152 | | // Make sure we read the full encapsulation. |
| | 33 | 153 | | if (decoder.Consumed != oldPos + size) |
| | 0 | 154 | | { |
| | 0 | 155 | | throw new InvalidDataException( |
| | 0 | 156 | | $"There are {oldPos + size - decoder.Consumed} bytes left in server address encapsulation."); |
| | | 157 | | } |
| | 33 | 158 | | } |
| | 33 | 159 | | } |
| | | 160 | | |
| | 33 | 161 | | if (serverAddress is null) |
| | 0 | 162 | | { |
| | 0 | 163 | | throw new InvalidDataException( |
| | 0 | 164 | | $"Cannot decode server address for protocol '{protocol}' and transport '{transportCode.ToString().ToLowe |
| | | 165 | | } |
| | | 166 | | |
| | 33 | 167 | | return serverAddress.Value; |
| | 33 | 168 | | } |
| | | 169 | | |
| | | 170 | | /// <summary>Decodes a service address encoded with the Ice encoding.</summary> |
| | | 171 | | /// <param name="decoder">The Ice decoder.</param> |
| | | 172 | | /// <param name="path">The decoded path.</param> |
| | | 173 | | /// <returns>The decoded service address.</returns> |
| | | 174 | | private static ServiceAddress DecodeServiceAddressCore(this ref IceDecoder decoder, string path) |
| | 44 | 175 | | { |
| | | 176 | | // With the Ice encoding, a service address is encoded as a kind of discriminated union with: |
| | | 177 | | // - Identity |
| | | 178 | | // - If Identity is not the null identity: |
| | | 179 | | // - the fragment, invocation mode, secure, protocol major and minor, and the encoding major and minor |
| | | 180 | | // - a sequence of server addresses (can be empty) |
| | | 181 | | // - an adapter ID string present only when the sequence of server addresses is empty |
| | | 182 | | |
| | 44 | 183 | | string fragment = decoder.DecodeFacet().ToFragment(); |
| | 44 | 184 | | _ = decoder.DecodeInvocationMode(); |
| | 44 | 185 | | _ = decoder.DecodeBool(); |
| | 44 | 186 | | byte protocolMajor = decoder.DecodeByte(); |
| | 44 | 187 | | byte protocolMinor = decoder.DecodeByte(); |
| | 44 | 188 | | decoder.Skip(2); // skip encoding major and minor |
| | | 189 | | |
| | 44 | 190 | | if (protocolMajor == 0) |
| | 0 | 191 | | { |
| | 0 | 192 | | throw new InvalidDataException("Received service address with protocol set to 0."); |
| | | 193 | | } |
| | 44 | 194 | | if (protocolMinor != 0) |
| | 0 | 195 | | { |
| | 0 | 196 | | throw new InvalidDataException( |
| | 0 | 197 | | $"Received service address with invalid protocolMinor value: {protocolMinor}."); |
| | | 198 | | } |
| | | 199 | | |
| | 44 | 200 | | int count = decoder.DecodeSize(); |
| | | 201 | | |
| | 44 | 202 | | ServerAddress? serverAddress = null; |
| | 44 | 203 | | IEnumerable<ServerAddress> altServerAddresses = ImmutableList<ServerAddress>.Empty; |
| | 44 | 204 | | var protocol = Protocol.FromByteValue(protocolMajor); |
| | 44 | 205 | | ImmutableDictionary<string, string> serviceAddressParams = ImmutableDictionary<string, string>.Empty; |
| | | 206 | | |
| | 44 | 207 | | if (count == 0) |
| | 12 | 208 | | { |
| | 12 | 209 | | if (decoder.DecodeString() is string adapterId && adapterId.Length > 0) |
| | 5 | 210 | | { |
| | 5 | 211 | | serviceAddressParams = serviceAddressParams.Add("adapter-id", Uri.EscapeDataString(adapterId)); |
| | 5 | 212 | | } |
| | 12 | 213 | | } |
| | | 214 | | else |
| | 32 | 215 | | { |
| | 32 | 216 | | serverAddress = decoder.DecodeServerAddress(protocol); |
| | 32 | 217 | | if (count >= 2) |
| | 1 | 218 | | { |
| | | 219 | | // An Ice-encoded server address consumes at least 8 bytes (2 bytes for the server address type and 6 |
| | | 220 | | // bytes for the encapsulation header). SizeOf ServerAddress is large but less than 8 * 8. |
| | 1 | 221 | | decoder.IncreaseCollectionAllocation(count, Unsafe.SizeOf<ServerAddress>()); |
| | | 222 | | |
| | 1 | 223 | | var serverAddressArray = new ServerAddress[count - 1]; |
| | 4 | 224 | | for (int i = 0; i < count - 1; ++i) |
| | 1 | 225 | | { |
| | 1 | 226 | | serverAddressArray[i] = decoder.DecodeServerAddress(protocol); |
| | 1 | 227 | | } |
| | 1 | 228 | | altServerAddresses = serverAddressArray; |
| | 1 | 229 | | } |
| | 32 | 230 | | } |
| | | 231 | | |
| | | 232 | | try |
| | 44 | 233 | | { |
| | 44 | 234 | | if (!protocol.HasFragment && fragment.Length > 0) |
| | 0 | 235 | | { |
| | 0 | 236 | | throw new InvalidDataException($"Unexpected fragment in {protocol} service address."); |
| | | 237 | | } |
| | | 238 | | |
| | 44 | 239 | | return new ServiceAddress( |
| | 44 | 240 | | protocol, |
| | 44 | 241 | | path, |
| | 44 | 242 | | serverAddress, |
| | 44 | 243 | | altServerAddresses.ToImmutableList(), |
| | 44 | 244 | | serviceAddressParams, |
| | 44 | 245 | | fragment); |
| | | 246 | | } |
| | 0 | 247 | | catch (InvalidDataException) |
| | 0 | 248 | | { |
| | 0 | 249 | | throw; |
| | | 250 | | } |
| | 0 | 251 | | catch (Exception exception) |
| | 0 | 252 | | { |
| | 0 | 253 | | throw new InvalidDataException("Received invalid service address.", exception); |
| | | 254 | | } |
| | 44 | 255 | | } |
| | | 256 | | |
| | | 257 | | /// <summary>Decodes the body of a tcp or ssl server address.</summary> |
| | | 258 | | private static ServerAddress DecodeTcpServerAddressBody(this ref IceDecoder decoder, string transport) |
| | 13 | 259 | | { |
| | 13 | 260 | | var body = new TcpServerAddressBody(ref decoder); |
| | | 261 | | |
| | 13 | 262 | | if (Uri.CheckHostName(body.Host) == UriHostNameType.Unknown) |
| | 0 | 263 | | { |
| | 0 | 264 | | throw new InvalidDataException($"Received service address with invalid host '{body.Host}'."); |
| | | 265 | | } |
| | | 266 | | |
| | 13 | 267 | | ImmutableDictionary<string, string> parameters = ImmutableDictionary<string, string>.Empty; |
| | 13 | 268 | | if (body.Timeout != IceProxyIceEncoderExtensions.DefaultTcpTimeout) |
| | 4 | 269 | | { |
| | 4 | 270 | | parameters = parameters.Add("t", body.Timeout.ToString(CultureInfo.InvariantCulture)); |
| | 4 | 271 | | } |
| | 13 | 272 | | if (body.Compress) |
| | 1 | 273 | | { |
| | 1 | 274 | | parameters = parameters.Add("z", ""); |
| | 1 | 275 | | } |
| | | 276 | | |
| | | 277 | | try |
| | 13 | 278 | | { |
| | 13 | 279 | | return new ServerAddress(Protocol.Ice, body.Host, checked((ushort)body.Port), transport, parameters); |
| | | 280 | | } |
| | 0 | 281 | | catch (OverflowException exception) |
| | 0 | 282 | | { |
| | 0 | 283 | | throw new InvalidDataException( |
| | 0 | 284 | | "Cannot decode a server address with a port number larger than 65,535.", |
| | 0 | 285 | | exception); |
| | | 286 | | } |
| | 13 | 287 | | } |
| | | 288 | | } |