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