| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using System.Diagnostics; |
| | 4 | | using System.Globalization; |
| | 5 | | using System.Net.Security; |
| | 6 | |
|
| | 7 | | namespace Ice.Internal; |
| | 8 | |
|
| | 9 | | internal sealed class OpaqueEndpointI : EndpointI |
| | 10 | | { |
| 1 | 11 | | public OpaqueEndpointI(List<string> args) |
| | 12 | | { |
| 1 | 13 | | _type = -1; |
| 1 | 14 | | _rawEncoding = Ice.Util.Encoding_1_0; |
| 1 | 15 | | _rawBytes = []; |
| | 16 | |
|
| 1 | 17 | | initWithOptions(args); |
| | 18 | |
|
| 1 | 19 | | if (_type < 0) |
| | 20 | | { |
| 1 | 21 | | throw new ParseException($"no -t option in endpoint '{this}'"); |
| | 22 | | } |
| 1 | 23 | | if (_rawBytes.Length == 0) |
| | 24 | | { |
| 1 | 25 | | throw new ParseException($"no -v option in endpoint '{this}'"); |
| | 26 | | } |
| 1 | 27 | | } |
| | 28 | |
|
| 0 | 29 | | public OpaqueEndpointI(short type, Ice.InputStream s) |
| | 30 | | { |
| 0 | 31 | | _type = type; |
| 0 | 32 | | _rawEncoding = s.getEncoding(); |
| 0 | 33 | | int sz = s.getEncapsulationSize(); |
| 0 | 34 | | _rawBytes = new byte[sz]; |
| 0 | 35 | | s.readBlob(_rawBytes); |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | | // |
| | 39 | | // Marshal the endpoint |
| | 40 | | // |
| | 41 | | public override void streamWrite(Ice.OutputStream s) |
| | 42 | | { |
| 1 | 43 | | s.startEncapsulation(_rawEncoding); |
| 1 | 44 | | s.writeBlob(_rawBytes); |
| 1 | 45 | | s.endEncapsulation(); |
| 1 | 46 | | } |
| | 47 | |
|
| | 48 | | public override void streamWriteImpl(Ice.OutputStream s) => Debug.Assert(false); |
| | 49 | |
|
| | 50 | | // |
| | 51 | | // Convert the endpoint to its string form |
| | 52 | | // |
| | 53 | | public override string ToString() |
| | 54 | | { |
| 1 | 55 | | string val = System.Convert.ToBase64String(_rawBytes); |
| 1 | 56 | | return "opaque -t " + _type + " -e " + Ice.Util.encodingVersionToString(_rawEncoding) + " -v " + val; |
| | 57 | | } |
| | 58 | |
|
| | 59 | | // |
| | 60 | | // Return the endpoint information. |
| | 61 | | // |
| 1 | 62 | | public override EndpointInfo getInfo() => new OpaqueEndpointInfo(_type, _rawEncoding, _rawBytes); |
| | 63 | |
|
| | 64 | | // |
| | 65 | | // Return the endpoint type |
| | 66 | | // |
| 1 | 67 | | public override short type() => _type; |
| | 68 | |
|
| | 69 | | // |
| | 70 | | // Return the protocol name; |
| | 71 | | // |
| 1 | 72 | | public override string protocol() => "opaque"; |
| | 73 | |
|
| | 74 | | // |
| | 75 | | // Return the timeout for the endpoint in milliseconds. 0 means |
| | 76 | | // non-blocking, -1 means no timeout. |
| | 77 | | // |
| 0 | 78 | | public override int timeout() => -1; |
| | 79 | |
|
| | 80 | | // |
| | 81 | | // Return a new endpoint with a different timeout value, provided |
| | 82 | | // that timeouts are supported by the endpoint. Otherwise the same |
| | 83 | | // endpoint is returned. |
| | 84 | | // |
| 0 | 85 | | public override EndpointI timeout(int t) => this; |
| | 86 | |
|
| 0 | 87 | | public override string connectionId() => ""; |
| | 88 | |
|
| | 89 | | // |
| | 90 | | // Return a new endpoint with a different connection id. |
| | 91 | | // |
| 0 | 92 | | public override EndpointI connectionId(string id) => this; |
| | 93 | |
|
| | 94 | | // |
| | 95 | | // Return true if the endpoints support bzip2 compress, or false |
| | 96 | | // otherwise. |
| | 97 | | // |
| 0 | 98 | | public override bool compress() => false; |
| | 99 | |
|
| | 100 | | // |
| | 101 | | // Return a new endpoint with a different compression value, |
| | 102 | | // provided that compression is supported by the |
| | 103 | | // endpoint. Otherwise the same endpoint is returned. |
| | 104 | | // |
| 0 | 105 | | public override EndpointI compress(bool compress) => this; |
| | 106 | |
|
| | 107 | | // |
| | 108 | | // Return true if the endpoint is datagram-based. |
| | 109 | | // |
| 0 | 110 | | public override bool datagram() => false; |
| | 111 | |
|
| | 112 | | // |
| | 113 | | // Return true if the endpoint is secure. |
| | 114 | | // |
| 0 | 115 | | public override bool secure() => false; |
| | 116 | |
|
| | 117 | | // |
| | 118 | | // Get the encoded endpoint. |
| | 119 | | // |
| 0 | 120 | | public byte[] rawBytes() => _rawBytes; |
| | 121 | |
|
| | 122 | | // |
| | 123 | | // Return a server side transceiver for this endpoint, or null if a |
| | 124 | | // transceiver can only be created by an acceptor. |
| | 125 | | // |
| 0 | 126 | | public override Transceiver transceiver() => null; |
| | 127 | |
|
| | 128 | | // |
| | 129 | | // Return connectors for this endpoint, or empty list if no connector |
| | 130 | | // is available. |
| | 131 | | // |
| 0 | 132 | | public override void connectors_async(EndpointI_connectors callback) => callback.connectors(new List<Connector>()); |
| | 133 | |
|
| | 134 | | // |
| | 135 | | // Return an acceptor for this endpoint, or null if no acceptors |
| | 136 | | // is available. |
| | 137 | | // |
| | 138 | | public override Acceptor acceptor(string adapterName, SslServerAuthenticationOptions serverAuthenticationOptions) => |
| 0 | 139 | | null; |
| | 140 | |
|
| 0 | 141 | | public override List<EndpointI> expandHost() => [this]; |
| | 142 | |
|
| 0 | 143 | | public override bool isLoopbackOrMulticast() => false; |
| | 144 | |
|
| 0 | 145 | | public override EndpointI toPublishedEndpoint(string host) => this; |
| | 146 | |
|
| | 147 | | // |
| | 148 | | // Check whether the endpoint is equivalent to another one. |
| | 149 | | // |
| 0 | 150 | | public override bool equivalent(EndpointI endpoint) => false; |
| | 151 | |
|
| | 152 | | public override int GetHashCode() |
| | 153 | | { |
| 1 | 154 | | var hash = new HashCode(); |
| 1 | 155 | | hash.Add(_type); |
| 1 | 156 | | hash.AddBytes(_rawBytes); |
| 1 | 157 | | return hash.ToHashCode(); |
| | 158 | | } |
| | 159 | |
|
| | 160 | | public override string options() |
| | 161 | | { |
| 0 | 162 | | string s = ""; |
| 0 | 163 | | if (_type > -1) |
| | 164 | | { |
| 0 | 165 | | s += " -t " + _type; |
| | 166 | | } |
| 0 | 167 | | s += " -e " + Ice.Util.encodingVersionToString(_rawEncoding); |
| 0 | 168 | | if (_rawBytes.Length > 0) |
| | 169 | | { |
| 0 | 170 | | s += " -v " + System.Convert.ToBase64String(_rawBytes); |
| | 171 | | } |
| 0 | 172 | | return s; |
| | 173 | | } |
| | 174 | |
|
| | 175 | | // |
| | 176 | | // Compare endpoints for sorting purposes |
| | 177 | | // |
| | 178 | | public override int CompareTo(EndpointI obj) |
| | 179 | | { |
| 0 | 180 | | if (!(obj is OpaqueEndpointI)) |
| | 181 | | { |
| 0 | 182 | | return type() < obj.type() ? -1 : 1; |
| | 183 | | } |
| | 184 | |
|
| 0 | 185 | | var p = (OpaqueEndpointI)obj; |
| 0 | 186 | | if (this == p) |
| | 187 | | { |
| 0 | 188 | | return 0; |
| | 189 | | } |
| | 190 | |
|
| 0 | 191 | | if (_type < p._type) |
| | 192 | | { |
| 0 | 193 | | return -1; |
| | 194 | | } |
| 0 | 195 | | else if (p._type < _type) |
| | 196 | | { |
| 0 | 197 | | return 1; |
| | 198 | | } |
| | 199 | |
|
| 0 | 200 | | if (_rawEncoding.major < p._rawEncoding.major) |
| | 201 | | { |
| 0 | 202 | | return -1; |
| | 203 | | } |
| 0 | 204 | | else if (p._rawEncoding.major < _rawEncoding.major) |
| | 205 | | { |
| 0 | 206 | | return 1; |
| | 207 | | } |
| | 208 | |
|
| 0 | 209 | | if (_rawEncoding.minor < p._rawEncoding.minor) |
| | 210 | | { |
| 0 | 211 | | return -1; |
| | 212 | | } |
| 0 | 213 | | else if (p._rawEncoding.minor < _rawEncoding.minor) |
| | 214 | | { |
| 0 | 215 | | return 1; |
| | 216 | | } |
| | 217 | |
|
| 0 | 218 | | if (_rawBytes.Length < p._rawBytes.Length) |
| | 219 | | { |
| 0 | 220 | | return -1; |
| | 221 | | } |
| 0 | 222 | | else if (p._rawBytes.Length < _rawBytes.Length) |
| | 223 | | { |
| 0 | 224 | | return 1; |
| | 225 | | } |
| 0 | 226 | | for (int i = 0; i < _rawBytes.Length; i++) |
| | 227 | | { |
| 0 | 228 | | if (_rawBytes[i] < p._rawBytes[i]) |
| | 229 | | { |
| 0 | 230 | | return -1; |
| | 231 | | } |
| 0 | 232 | | else if (p._rawBytes[i] < _rawBytes[i]) |
| | 233 | | { |
| 0 | 234 | | return 1; |
| | 235 | | } |
| | 236 | | } |
| | 237 | |
|
| 0 | 238 | | return 0; |
| | 239 | | } |
| | 240 | |
|
| | 241 | | protected override bool checkOption(string option, string argument, string endpoint) |
| | 242 | | { |
| 1 | 243 | | switch (option[1]) |
| | 244 | | { |
| | 245 | | case 't': |
| | 246 | | { |
| 1 | 247 | | if (_type > -1) |
| | 248 | | { |
| 1 | 249 | | throw new ParseException($"multiple -t options in endpoint '{endpoint}'"); |
| | 250 | | } |
| 1 | 251 | | if (argument == null) |
| | 252 | | { |
| 1 | 253 | | throw new ParseException($"no argument provided for -t option in endpoint '{endpoint}'"); |
| | 254 | | } |
| | 255 | |
|
| | 256 | | int t; |
| | 257 | | try |
| | 258 | | { |
| 1 | 259 | | t = int.Parse(argument, CultureInfo.InvariantCulture); |
| 1 | 260 | | } |
| 1 | 261 | | catch (FormatException ex) |
| | 262 | | { |
| 1 | 263 | | throw new ParseException($"invalid type value '{argument}' in endpoint '{endpoint}'", ex); |
| | 264 | | } |
| | 265 | |
|
| 1 | 266 | | if (t < 0 || t > 65535) |
| | 267 | | { |
| 0 | 268 | | throw new ParseException($"type value '{argument}' out of range in endpoint '{endpoint}'"); |
| | 269 | | } |
| | 270 | |
|
| 1 | 271 | | _type = (short)t; |
| 1 | 272 | | return true; |
| | 273 | | } |
| | 274 | |
|
| | 275 | | case 'v': |
| | 276 | | { |
| 1 | 277 | | if (_rawBytes.Length > 0) |
| | 278 | | { |
| 1 | 279 | | throw new ParseException($"multiple -v options in endpoint '{endpoint}'"); |
| | 280 | | } |
| 1 | 281 | | if (argument == null) |
| | 282 | | { |
| 1 | 283 | | throw new ParseException($"no argument provided for -v option in endpoint '{endpoint}'"); |
| | 284 | | } |
| | 285 | |
|
| | 286 | | try |
| | 287 | | { |
| 1 | 288 | | _rawBytes = System.Convert.FromBase64String(argument); |
| 1 | 289 | | } |
| 1 | 290 | | catch (System.FormatException ex) |
| | 291 | | { |
| 1 | 292 | | throw new ParseException($"invalid Base64 input in endpoint '{endpoint}'", ex); |
| | 293 | | } |
| | 294 | |
|
| 1 | 295 | | return true; |
| | 296 | | } |
| | 297 | |
|
| | 298 | | case 'e': |
| | 299 | | { |
| 1 | 300 | | if (argument == null) |
| | 301 | | { |
| 0 | 302 | | throw new ParseException($"no argument provided for -e option in endpoint '{endpoint}'"); |
| | 303 | | } |
| | 304 | |
|
| | 305 | | try |
| | 306 | | { |
| 1 | 307 | | _rawEncoding = Ice.Util.stringToEncodingVersion(argument); |
| 1 | 308 | | } |
| 0 | 309 | | catch (ParseException e) |
| | 310 | | { |
| 0 | 311 | | throw new ParseException($"invalid encoding version '{argument}' in endpoint '{endpoint}'", e); |
| | 312 | | } |
| 1 | 313 | | return true; |
| | 314 | | } |
| | 315 | |
|
| | 316 | | default: |
| | 317 | | { |
| 1 | 318 | | return false; |
| | 319 | | } |
| | 320 | | } |
| | 321 | | } |
| | 322 | |
|
| | 323 | | private short _type; |
| | 324 | | private Ice.EncodingVersion _rawEncoding; |
| | 325 | | private byte[] _rawBytes; |
| | 326 | | } |