| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using System.Diagnostics; |
| | | 4 | | using System.Globalization; |
| | | 5 | | using System.Net; |
| | | 6 | | using System.Net.Security; |
| | | 7 | | |
| | | 8 | | namespace Ice.Internal; |
| | | 9 | | |
| | | 10 | | internal sealed class UdpEndpointI : IPEndpointI |
| | | 11 | | { |
| | | 12 | | public UdpEndpointI( |
| | | 13 | | ProtocolInstance instance, |
| | | 14 | | string host, |
| | | 15 | | int port, |
| | | 16 | | EndPoint sourceAddr, |
| | | 17 | | string mcastInterface, |
| | | 18 | | int mttl, |
| | | 19 | | string connectionId, |
| | | 20 | | bool compress) |
| | | 21 | | : base(instance, host, port, sourceAddr, connectionId) |
| | | 22 | | { |
| | | 23 | | _mcastInterface = mcastInterface; |
| | | 24 | | _mcastTtl = mttl; |
| | | 25 | | _compress = compress; |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | public UdpEndpointI(ProtocolInstance instance) |
| | | 29 | | : base(instance) |
| | | 30 | | { |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | public UdpEndpointI(ProtocolInstance instance, Ice.InputStream s) |
| | | 34 | | : base(instance, s) |
| | | 35 | | { |
| | | 36 | | if (s.getEncoding().Equals(Ice.Util.Encoding_1_0)) |
| | | 37 | | { |
| | | 38 | | s.readByte(); |
| | | 39 | | s.readByte(); |
| | | 40 | | s.readByte(); |
| | | 41 | | s.readByte(); |
| | | 42 | | } |
| | | 43 | | _compress = s.readBool(); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | // |
| | | 47 | | // Return the endpoint information. |
| | | 48 | | // |
| | | 49 | | public override EndpointInfo getInfo() |
| | | 50 | | { |
| | | 51 | | Debug.Assert(!secure()); |
| | | 52 | | Debug.Assert(type() == UDPEndpointType.value); |
| | | 53 | | |
| | | 54 | | return new UDPEndpointInfo( |
| | | 55 | | _compress, |
| | | 56 | | host_, |
| | | 57 | | port_, |
| | | 58 | | Network.endpointAddressToString(sourceAddr_), |
| | | 59 | | _mcastInterface, |
| | | 60 | | _mcastTtl); |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | // |
| | | 64 | | // Return the timeout for the endpoint in milliseconds. 0 means |
| | | 65 | | // non-blocking, -1 means no timeout. |
| | | 66 | | // |
| | | 67 | | public override int timeout() => -1; |
| | | 68 | | |
| | | 69 | | // |
| | | 70 | | // Return a new endpoint with a different timeout value, provided |
| | | 71 | | // that timeouts are supported by the endpoint. Otherwise the same |
| | | 72 | | // endpoint is returned. |
| | | 73 | | // |
| | | 74 | | public override EndpointI timeout(int timeout) => this; |
| | | 75 | | |
| | | 76 | | // |
| | | 77 | | // Return true if the endpoints support bzip2 compress, or false |
| | | 78 | | // otherwise. |
| | | 79 | | // |
| | | 80 | | public override bool compress() => _compress; |
| | | 81 | | |
| | | 82 | | // |
| | | 83 | | // Return a new endpoint with a different compression value, |
| | | 84 | | // provided that compression is supported by the |
| | | 85 | | // endpoint. Otherwise the same endpoint is returned. |
| | | 86 | | // |
| | | 87 | | public override EndpointI compress(bool compress) |
| | | 88 | | { |
| | | 89 | | if (compress == _compress) |
| | | 90 | | { |
| | | 91 | | return this; |
| | | 92 | | } |
| | | 93 | | else |
| | | 94 | | { |
| | | 95 | | return new UdpEndpointI( |
| | | 96 | | instance_, |
| | | 97 | | host_, |
| | | 98 | | port_, |
| | | 99 | | sourceAddr_, |
| | | 100 | | _mcastInterface, |
| | | 101 | | _mcastTtl, |
| | | 102 | | connectionId_, |
| | | 103 | | compress); |
| | | 104 | | } |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | // |
| | | 108 | | // Return true if the endpoint is datagram-based. |
| | | 109 | | // |
| | | 110 | | public override bool datagram() => true; |
| | | 111 | | |
| | | 112 | | // |
| | | 113 | | // Return a server side transceiver for this endpoint, or null if a |
| | | 114 | | // transceiver can only be created by an acceptor. |
| | | 115 | | // |
| | | 116 | | public override Transceiver transceiver() => new UdpTransceiver(this, instance_, host_, port_, _mcastInterface); |
| | | 117 | | |
| | | 118 | | // |
| | | 119 | | // Return an acceptor for this endpoint, or null if no acceptors |
| | | 120 | | // is available. |
| | | 121 | | // |
| | | 122 | | public override Acceptor acceptor(string adapterName, SslServerAuthenticationOptions serverAuthenticationOptions) |
| | | 123 | | { |
| | | 124 | | Debug.Assert(serverAuthenticationOptions is null); |
| | | 125 | | return null; |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | public override void initWithOptions(List<string> args, bool oaEndpoint) |
| | | 129 | | { |
| | | 130 | | base.initWithOptions(args, oaEndpoint); |
| | | 131 | | |
| | | 132 | | if (_mcastInterface == "*") |
| | | 133 | | { |
| | | 134 | | if (oaEndpoint) |
| | | 135 | | { |
| | | 136 | | _mcastInterface = ""; |
| | | 137 | | } |
| | | 138 | | else |
| | | 139 | | { |
| | | 140 | | throw new ParseException($"'--interface *' not valid for proxy endpoint '{this}'"); |
| | | 141 | | } |
| | | 142 | | } |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | public UdpEndpointI endpoint(UdpTransceiver transceiver) |
| | | 146 | | { |
| | | 147 | | int port = transceiver.effectivePort(); |
| | | 148 | | if (port == port_) |
| | | 149 | | { |
| | | 150 | | return this; |
| | | 151 | | } |
| | | 152 | | else |
| | | 153 | | { |
| | | 154 | | return new UdpEndpointI( |
| | | 155 | | instance_, |
| | | 156 | | host_, |
| | | 157 | | port, |
| | | 158 | | sourceAddr_, |
| | | 159 | | _mcastInterface, |
| | | 160 | | _mcastTtl, |
| | | 161 | | connectionId_, |
| | | 162 | | _compress); |
| | | 163 | | } |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | public override string options() |
| | | 167 | | { |
| | | 168 | | // |
| | | 169 | | // WARNING: Certain features, such as proxy validation in Glacier2, |
| | | 170 | | // depend on the format of proxy strings. Changes to toString() and |
| | | 171 | | // methods called to generate parts of the reference string could break |
| | | 172 | | // these features. Please review for all features that depend on the |
| | | 173 | | // format of proxyToString() before changing this and related code. |
| | | 174 | | // |
| | | 175 | | string s = base.options(); |
| | | 176 | | |
| | | 177 | | if (_mcastInterface.Length != 0) |
| | | 178 | | { |
| | | 179 | | bool addQuote = _mcastInterface.Contains(':', StringComparison.Ordinal); |
| | | 180 | | s += " --interface "; |
| | | 181 | | if (addQuote) |
| | | 182 | | { |
| | | 183 | | s += "\""; |
| | | 184 | | } |
| | | 185 | | s += _mcastInterface; |
| | | 186 | | if (addQuote) |
| | | 187 | | { |
| | | 188 | | s += "\""; |
| | | 189 | | } |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | if (_mcastTtl != -1) |
| | | 193 | | { |
| | | 194 | | s += " --ttl " + _mcastTtl; |
| | | 195 | | } |
| | | 196 | | |
| | | 197 | | if (_compress) |
| | | 198 | | { |
| | | 199 | | s += " -z"; |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | return s; |
| | | 203 | | } |
| | | 204 | | |
| | | 205 | | // |
| | | 206 | | // Compare endpoints for sorting purposes |
| | | 207 | | // |
| | | 208 | | public override int CompareTo(EndpointI obj) |
| | | 209 | | { |
| | | 210 | | if (!(obj is UdpEndpointI)) |
| | | 211 | | { |
| | | 212 | | return type() < obj.type() ? -1 : 1; |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | var p = (UdpEndpointI)obj; |
| | | 216 | | if (this == p) |
| | | 217 | | { |
| | | 218 | | return 0; |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | if (!_compress && p._compress) |
| | | 222 | | { |
| | | 223 | | return -1; |
| | | 224 | | } |
| | | 225 | | else if (!p._compress && _compress) |
| | | 226 | | { |
| | | 227 | | return 1; |
| | | 228 | | } |
| | | 229 | | |
| | | 230 | | int rc = string.Compare(_mcastInterface, p._mcastInterface, StringComparison.Ordinal); |
| | | 231 | | if (rc != 0) |
| | | 232 | | { |
| | | 233 | | return rc; |
| | | 234 | | } |
| | | 235 | | |
| | | 236 | | if (_mcastTtl < p._mcastTtl) |
| | | 237 | | { |
| | | 238 | | return -1; |
| | | 239 | | } |
| | | 240 | | else if (p._mcastTtl < _mcastTtl) |
| | | 241 | | { |
| | | 242 | | return 1; |
| | | 243 | | } |
| | | 244 | | |
| | | 245 | | return base.CompareTo(p); |
| | | 246 | | } |
| | | 247 | | |
| | | 248 | | // |
| | | 249 | | // Marshal the endpoint |
| | | 250 | | // |
| | | 251 | | public override void streamWriteImpl(Ice.OutputStream s) |
| | | 252 | | { |
| | | 253 | | base.streamWriteImpl(s); |
| | | 254 | | if (s.getEncoding().Equals(Ice.Util.Encoding_1_0)) |
| | | 255 | | { |
| | | 256 | | ProtocolVersion.ice_write(s, Protocol.Protocol_1_0); |
| | | 257 | | EncodingVersion.ice_write(s, Ice.Util.Encoding_1_0); |
| | | 258 | | } |
| | | 259 | | s.writeBool(_compress); |
| | | 260 | | } |
| | | 261 | | |
| | | 262 | | public override int GetHashCode() => HashCode.Combine(base.GetHashCode(), _mcastInterface, _mcastTtl, _compress); |
| | | 263 | | |
| | | 264 | | public override EndpointI toPublishedEndpoint(string publishedHost) => |
| | | 265 | | new UdpEndpointI( |
| | | 266 | | instance_, |
| | | 267 | | publishedHost.Length > 0 ? publishedHost : host_, |
| | | 268 | | port_, |
| | | 269 | | sourceAddr: null, |
| | | 270 | | mcastInterface: "", |
| | | 271 | | mttl: -1, |
| | | 272 | | connectionId: "", |
| | | 273 | | _compress); |
| | | 274 | | |
| | | 275 | | protected override bool checkOption(string option, string argument, string endpoint) |
| | | 276 | | { |
| | | 277 | | if (base.checkOption(option, argument, endpoint)) |
| | | 278 | | { |
| | | 279 | | return true; |
| | | 280 | | } |
| | | 281 | | |
| | | 282 | | if (option == "-z") |
| | | 283 | | { |
| | | 284 | | if (argument != null) |
| | | 285 | | { |
| | | 286 | | throw new ParseException( |
| | | 287 | | $"unexpected argument '{argument}' provided for -z option in endpoint '{endpoint}'"); |
| | | 288 | | } |
| | | 289 | | |
| | | 290 | | _compress = true; |
| | | 291 | | } |
| | | 292 | | else if (option == "-v" || option == "-e") |
| | | 293 | | { |
| | | 294 | | if (argument == null) |
| | | 295 | | { |
| | | 296 | | throw new ParseException($"no argument provided for {option} option in endpoint '{endpoint}'"); |
| | | 297 | | } |
| | | 298 | | |
| | | 299 | | try |
| | | 300 | | { |
| | | 301 | | Ice.EncodingVersion v = Protocol.stringToEncodingVersion(argument); |
| | | 302 | | if (v.major != 1 || v.minor != 0) |
| | | 303 | | { |
| | | 304 | | instance_.logger().warning("deprecated udp endpoint option: " + option); |
| | | 305 | | } |
| | | 306 | | } |
| | | 307 | | catch (ParseException ex) |
| | | 308 | | { |
| | | 309 | | throw new ParseException($"invalid version '{argument}' in endpoint '{endpoint}'", ex); |
| | | 310 | | } |
| | | 311 | | } |
| | | 312 | | else if (option == "--ttl") |
| | | 313 | | { |
| | | 314 | | if (argument == null) |
| | | 315 | | { |
| | | 316 | | throw new ParseException($"no argument provided for --ttl option in endpoint '{endpoint}'"); |
| | | 317 | | } |
| | | 318 | | |
| | | 319 | | try |
| | | 320 | | { |
| | | 321 | | _mcastTtl = int.Parse(argument, CultureInfo.InvariantCulture); |
| | | 322 | | } |
| | | 323 | | catch (FormatException ex) |
| | | 324 | | { |
| | | 325 | | throw new ParseException($"invalid TTL value '{argument}' in endpoint '{endpoint}'", ex); |
| | | 326 | | } |
| | | 327 | | |
| | | 328 | | if (_mcastTtl < 0) |
| | | 329 | | { |
| | | 330 | | throw new ParseException($"TTL value '{argument}' out of range in endpoint '{endpoint}'"); |
| | | 331 | | } |
| | | 332 | | } |
| | | 333 | | else if (option == "--interface") |
| | | 334 | | { |
| | | 335 | | _mcastInterface = argument ?? |
| | | 336 | | throw new ParseException($"no argument provided for --interface option in endpoint '{endpoint}'"); |
| | | 337 | | } |
| | | 338 | | else |
| | | 339 | | { |
| | | 340 | | return false; |
| | | 341 | | } |
| | | 342 | | |
| | | 343 | | return true; |
| | | 344 | | } |
| | | 345 | | |
| | | 346 | | protected override Connector createConnector(EndPoint addr, NetworkProxy proxy) => |
| | | 347 | | new UdpConnector(instance_, addr, sourceAddr_, _mcastInterface, _mcastTtl, connectionId_); |
| | | 348 | | |
| | | 349 | | protected override IPEndpointI createEndpoint(string host, int port, string connectionId) |
| | | 350 | | { |
| | | 351 | | return new UdpEndpointI( |
| | | 352 | | instance_, |
| | | 353 | | host, |
| | | 354 | | port, |
| | | 355 | | sourceAddr_, |
| | | 356 | | _mcastInterface, |
| | | 357 | | _mcastTtl, |
| | | 358 | | connectionId, |
| | | 359 | | _compress); |
| | | 360 | | } |
| | | 361 | | |
| | | 362 | | private string _mcastInterface = ""; |
| | | 363 | | private int _mcastTtl = -1; |
| | | 364 | | private bool _compress; |
| | | 365 | | } |
| | | 366 | | |
| | | 367 | | internal sealed class UdpEndpointFactory : EndpointFactory |
| | | 368 | | { |
| | 1 | 369 | | internal UdpEndpointFactory(ProtocolInstance instance) => _instance = instance; |
| | | 370 | | |
| | | 371 | | public void initialize() |
| | | 372 | | { |
| | 1 | 373 | | } |
| | | 374 | | |
| | 1 | 375 | | public short type() => _instance.type(); |
| | | 376 | | |
| | 1 | 377 | | public string protocol() => _instance.protocol(); |
| | | 378 | | |
| | | 379 | | public EndpointI create(List<string> args, bool oaEndpoint) |
| | | 380 | | { |
| | 1 | 381 | | IPEndpointI endpt = new UdpEndpointI(_instance); |
| | 1 | 382 | | endpt.initWithOptions(args, oaEndpoint); |
| | 1 | 383 | | return endpt; |
| | | 384 | | } |
| | | 385 | | |
| | 1 | 386 | | public EndpointI read(Ice.InputStream s) => new UdpEndpointI(_instance, s); |
| | | 387 | | |
| | 0 | 388 | | public EndpointFactory clone(ProtocolInstance instance) => new UdpEndpointFactory(instance); |
| | | 389 | | |
| | | 390 | | private readonly ProtocolInstance _instance; |
| | | 391 | | } |