| | 1 | | // Copyright (c) ZeroC, Inc. |
| | 2 | |
|
| | 3 | | using System.Globalization; |
| | 4 | | using System.Net; |
| | 5 | |
|
| | 6 | | namespace Ice.Internal; |
| | 7 | |
|
| | 8 | | public abstract class IPEndpointI : EndpointI |
| | 9 | | { |
| 1 | 10 | | protected IPEndpointI(ProtocolInstance instance, string host, int port, EndPoint sourceAddr, string connectionId) |
| | 11 | | { |
| 1 | 12 | | instance_ = instance; |
| 1 | 13 | | host_ = host; |
| 1 | 14 | | _normalizedHost = normalizeHost(host_); |
| 1 | 15 | | port_ = port; |
| 1 | 16 | | sourceAddr_ = sourceAddr; |
| 1 | 17 | | connectionId_ = connectionId; |
| 1 | 18 | | } |
| | 19 | |
|
| 1 | 20 | | protected IPEndpointI(ProtocolInstance instance) |
| | 21 | | { |
| 1 | 22 | | instance_ = instance; |
| 1 | 23 | | host_ = null; |
| 1 | 24 | | _normalizedHost = null; |
| 1 | 25 | | port_ = 0; |
| 1 | 26 | | sourceAddr_ = null; |
| 1 | 27 | | connectionId_ = ""; |
| 1 | 28 | | } |
| | 29 | |
|
| 1 | 30 | | protected IPEndpointI(ProtocolInstance instance, Ice.InputStream s) |
| | 31 | | { |
| 1 | 32 | | instance_ = instance; |
| 1 | 33 | | host_ = s.readString(); |
| 1 | 34 | | _normalizedHost = normalizeHost(host_); |
| 1 | 35 | | port_ = s.readInt(); |
| 1 | 36 | | sourceAddr_ = null; |
| 1 | 37 | | connectionId_ = ""; |
| 1 | 38 | | } |
| | 39 | |
|
| 1 | 40 | | public override short type() => instance_.type(); |
| | 41 | |
|
| 1 | 42 | | public override string protocol() => instance_.protocol(); |
| | 43 | |
|
| 1 | 44 | | public override bool secure() => instance_.secure(); |
| | 45 | |
|
| 1 | 46 | | public override string connectionId() => connectionId_; |
| | 47 | |
|
| | 48 | | public override EndpointI connectionId(string connectionId) |
| | 49 | | { |
| 1 | 50 | | if (connectionId.Equals(connectionId_, StringComparison.Ordinal)) |
| | 51 | | { |
| 1 | 52 | | return this; |
| | 53 | | } |
| | 54 | | else |
| | 55 | | { |
| 1 | 56 | | return createEndpoint(host_, port_, connectionId); |
| | 57 | | } |
| | 58 | | } |
| | 59 | |
|
| | 60 | | public override void connectors_async(EndpointI_connectors callback) => |
| 1 | 61 | | instance_.resolve(host_, port_, this, callback); |
| | 62 | |
|
| | 63 | | public override List<EndpointI> expandHost() |
| | 64 | | { |
| | 65 | | // If this endpoint has an empty host (wildcard address), don't expand, just return this endpoint. |
| 1 | 66 | | if (host_.Length == 0) |
| | 67 | | { |
| 1 | 68 | | return [this]; |
| | 69 | | } |
| | 70 | |
|
| 1 | 71 | | List<EndPoint> addresses = Network.getAddresses( |
| 1 | 72 | | host_, |
| 1 | 73 | | port_, |
| 1 | 74 | | instance_.protocolSupport(), |
| 1 | 75 | | instance_.preferIPv6(), |
| 1 | 76 | | true); |
| | 77 | |
|
| 1 | 78 | | return addresses.Select( |
| 1 | 79 | | addr => createEndpoint( |
| 1 | 80 | | Network.endpointAddressToString(addr), |
| 1 | 81 | | Network.endpointPort(addr), |
| 1 | 82 | | connectionId_) as EndpointI).ToList(); |
| | 83 | | } |
| | 84 | |
|
| | 85 | | // Empty host_ means the endpoint is a wildcard address. This method must be called only on an endpoint with an |
| | 86 | | // empty host or an IP address. |
| | 87 | | public override bool isLoopbackOrMulticast() |
| | 88 | | { |
| 1 | 89 | | if (host_.Length > 0) |
| | 90 | | { |
| 1 | 91 | | var ipEndPoint = IPEndPoint.Parse(host_); |
| 1 | 92 | | return IPAddress.IsLoopback(ipEndPoint.Address) || Network.isMulticast(ipEndPoint); |
| | 93 | | } |
| | 94 | | else |
| | 95 | | { |
| 1 | 96 | | return false; |
| | 97 | | } |
| | 98 | | } |
| | 99 | |
|
| | 100 | | public override bool equivalent(EndpointI endpoint) |
| | 101 | | { |
| 1 | 102 | | if (!(endpoint is IPEndpointI)) |
| | 103 | | { |
| 1 | 104 | | return false; |
| | 105 | | } |
| 1 | 106 | | var ipEndpointI = (IPEndpointI)endpoint; |
| 1 | 107 | | return ipEndpointI.type() == type() && |
| 1 | 108 | | ipEndpointI._normalizedHost == _normalizedHost && |
| 1 | 109 | | ipEndpointI.port_ == port_; |
| | 110 | | } |
| | 111 | |
|
| | 112 | | public virtual List<Connector> connectors(List<EndPoint> addresses, NetworkProxy proxy) |
| | 113 | | { |
| 1 | 114 | | var connectors = new List<Connector>(); |
| 1 | 115 | | foreach (EndPoint p in addresses) |
| | 116 | | { |
| 1 | 117 | | connectors.Add(createConnector(p, proxy)); |
| | 118 | | } |
| 1 | 119 | | return connectors; |
| | 120 | | } |
| | 121 | |
|
| | 122 | | public override string options() |
| | 123 | | { |
| | 124 | | // |
| | 125 | | // WARNING: Certain features, such as proxy validation in Glacier2, |
| | 126 | | // depend on the format of proxy strings. Changes to toString() and |
| | 127 | | // methods called to generate parts of the reference string could break |
| | 128 | | // these features. Please review for all features that depend on the |
| | 129 | | // format of proxyToString() before changing this and related code. |
| | 130 | | // |
| 1 | 131 | | string s = ""; |
| | 132 | |
|
| 1 | 133 | | if (host_ != null && host_.Length > 0) |
| | 134 | | { |
| 1 | 135 | | s += " -h "; |
| 1 | 136 | | bool addQuote = host_.Contains(':', StringComparison.Ordinal); |
| 1 | 137 | | if (addQuote) |
| | 138 | | { |
| 1 | 139 | | s += "\""; |
| | 140 | | } |
| 1 | 141 | | s += host_; |
| 1 | 142 | | if (addQuote) |
| | 143 | | { |
| 1 | 144 | | s += "\""; |
| | 145 | | } |
| | 146 | | } |
| | 147 | |
|
| 1 | 148 | | s += " -p " + port_; |
| | 149 | |
|
| 1 | 150 | | if (sourceAddr_ != null) |
| | 151 | | { |
| 1 | 152 | | string sourceAddr = Network.endpointAddressToString(sourceAddr_); |
| 1 | 153 | | bool addQuote = sourceAddr.Contains(':', StringComparison.Ordinal); |
| 1 | 154 | | s += " --sourceAddress "; |
| 1 | 155 | | if (addQuote) |
| | 156 | | { |
| 1 | 157 | | s += "\""; |
| | 158 | | } |
| 1 | 159 | | s += sourceAddr; |
| 1 | 160 | | if (addQuote) |
| | 161 | | { |
| 1 | 162 | | s += "\""; |
| | 163 | | } |
| | 164 | | } |
| | 165 | |
|
| 1 | 166 | | return s; |
| | 167 | | } |
| | 168 | |
|
| | 169 | | public override int GetHashCode() |
| | 170 | | { |
| 1 | 171 | | var hash = new HashCode(); |
| 1 | 172 | | hash.Add(type()); |
| 1 | 173 | | hash.Add(host_); |
| 1 | 174 | | hash.Add(port_); |
| 1 | 175 | | if (sourceAddr_ is not null) |
| | 176 | | { |
| 0 | 177 | | hash.Add(sourceAddr_); |
| | 178 | | } |
| 1 | 179 | | hash.Add(connectionId_); |
| 1 | 180 | | return hash.ToHashCode(); |
| | 181 | | } |
| | 182 | |
|
| | 183 | | public override int CompareTo(EndpointI other) |
| | 184 | | { |
| 1 | 185 | | if (!(other is IPEndpointI)) |
| | 186 | | { |
| 0 | 187 | | return type() < other.type() ? -1 : 1; |
| | 188 | | } |
| | 189 | |
|
| 1 | 190 | | var p = (IPEndpointI)other; |
| 1 | 191 | | if (this == p) |
| | 192 | | { |
| 0 | 193 | | return 0; |
| | 194 | | } |
| | 195 | |
|
| 1 | 196 | | int v = string.Compare(host_, p.host_, StringComparison.Ordinal); |
| 1 | 197 | | if (v != 0) |
| | 198 | | { |
| 0 | 199 | | return v; |
| | 200 | | } |
| | 201 | |
|
| 1 | 202 | | if (port_ < p.port_) |
| | 203 | | { |
| 1 | 204 | | return -1; |
| | 205 | | } |
| 1 | 206 | | else if (p.port_ < port_) |
| | 207 | | { |
| 0 | 208 | | return 1; |
| | 209 | | } |
| | 210 | |
|
| 1 | 211 | | int rc = string.Compare( |
| 1 | 212 | | Network.endpointAddressToString(sourceAddr_), |
| 1 | 213 | | Network.endpointAddressToString(p.sourceAddr_), |
| 1 | 214 | | StringComparison.Ordinal); |
| 1 | 215 | | if (rc != 0) |
| | 216 | | { |
| 0 | 217 | | return rc; |
| | 218 | | } |
| | 219 | |
|
| 1 | 220 | | return string.Compare(connectionId_, p.connectionId_, StringComparison.Ordinal); |
| | 221 | | } |
| | 222 | |
|
| | 223 | | public override void streamWriteImpl(Ice.OutputStream s) |
| | 224 | | { |
| 1 | 225 | | s.writeString(host_); |
| 1 | 226 | | s.writeInt(port_); |
| 1 | 227 | | } |
| | 228 | |
|
| | 229 | | public virtual void initWithOptions(List<string> args, bool oaEndpoint) |
| | 230 | | { |
| 1 | 231 | | initWithOptions(args); |
| | 232 | |
|
| 1 | 233 | | if (host_ == null || host_.Length == 0) |
| | 234 | | { |
| 1 | 235 | | host_ = instance_.defaultHost(); |
| 1 | 236 | | _normalizedHost = normalizeHost(host_); |
| | 237 | | } |
| 1 | 238 | | else if (host_ == "*") |
| | 239 | | { |
| 1 | 240 | | if (oaEndpoint) |
| | 241 | | { |
| 1 | 242 | | host_ = ""; |
| 1 | 243 | | _normalizedHost = ""; |
| | 244 | | } |
| | 245 | | else |
| | 246 | | { |
| 0 | 247 | | throw new ParseException($"'-h *' not valid for proxy endpoint '{ToString()}'"); |
| | 248 | | } |
| | 249 | | } |
| | 250 | |
|
| 1 | 251 | | if (host_ == null) |
| | 252 | | { |
| 1 | 253 | | host_ = ""; |
| 1 | 254 | | _normalizedHost = normalizeHost(host_); |
| | 255 | | } |
| | 256 | |
|
| 1 | 257 | | if (sourceAddr_ != null) |
| | 258 | | { |
| 1 | 259 | | if (oaEndpoint) |
| | 260 | | { |
| 0 | 261 | | throw new ParseException($"'--sourceAddress' not valid for object adapter endpoint '{ToString()}'"); |
| | 262 | | } |
| | 263 | | } |
| 1 | 264 | | else if (!oaEndpoint) |
| | 265 | | { |
| 1 | 266 | | sourceAddr_ = instance_.defaultSourceAddress(); |
| | 267 | | } |
| 1 | 268 | | } |
| | 269 | |
|
| | 270 | | protected override bool checkOption(string option, string argument, string endpoint) |
| | 271 | | { |
| 1 | 272 | | if (option == "-h") |
| | 273 | | { |
| 1 | 274 | | host_ = argument ?? throw new ParseException( |
| 1 | 275 | | $"no argument provided for -h option in endpoint '{endpoint}'"); |
| 1 | 276 | | _normalizedHost = normalizeHost(host_); |
| | 277 | | } |
| 1 | 278 | | else if (option == "-p") |
| | 279 | | { |
| 1 | 280 | | if (argument == null) |
| | 281 | | { |
| 0 | 282 | | throw new ParseException($"no argument provided for -p option in endpoint '{endpoint}'"); |
| | 283 | | } |
| | 284 | |
|
| | 285 | | try |
| | 286 | | { |
| 1 | 287 | | port_ = int.Parse(argument, CultureInfo.InvariantCulture); |
| 1 | 288 | | } |
| 0 | 289 | | catch (FormatException ex) |
| | 290 | | { |
| 0 | 291 | | throw new ParseException($"invalid port value '{argument}' in endpoint '{endpoint}'", ex); |
| | 292 | | } |
| | 293 | |
|
| 1 | 294 | | if (port_ < 0 || port_ > 65535) |
| | 295 | | { |
| 0 | 296 | | throw new ParseException($"port value '{argument}' out of range in endpoint '{endpoint}'"); |
| | 297 | | } |
| | 298 | | } |
| 1 | 299 | | else if (option == "--sourceAddress") |
| | 300 | | { |
| 1 | 301 | | if (argument == null) |
| | 302 | | { |
| 0 | 303 | | throw new ParseException($"no argument provided for --sourceAddress option in endpoint '{endpoint}'"); |
| | 304 | | } |
| 1 | 305 | | sourceAddr_ = Network.getNumericAddress(argument); |
| 1 | 306 | | if (sourceAddr_ == null) |
| | 307 | | { |
| 0 | 308 | | throw new ParseException( |
| 0 | 309 | | $"invalid IP address provided for --sourceAddress option in endpoint '{endpoint}'"); |
| | 310 | | } |
| | 311 | | } |
| | 312 | | else |
| | 313 | | { |
| 1 | 314 | | return false; |
| | 315 | | } |
| 1 | 316 | | return true; |
| | 317 | | } |
| | 318 | |
|
| | 319 | | private static string normalizeHost(string host) |
| | 320 | | { |
| 1 | 321 | | if (host is not null && host.Contains(':', StringComparison.Ordinal)) |
| | 322 | | { |
| | 323 | | // Could be an IPv6 address that we need to normalize. |
| | 324 | | try |
| | 325 | | { |
| 1 | 326 | | return IPAddress.Parse(host).ToString(); // normalized host |
| | 327 | | } |
| 0 | 328 | | catch |
| | 329 | | { |
| | 330 | | // Ignore - don't normalize host. |
| 0 | 331 | | } |
| | 332 | | } |
| 1 | 333 | | return host; |
| 1 | 334 | | } |
| | 335 | |
|
| | 336 | | protected abstract Connector createConnector(EndPoint addr, NetworkProxy proxy); |
| | 337 | |
|
| | 338 | | protected abstract IPEndpointI createEndpoint(string host, int port, string connectionId); |
| | 339 | |
|
| | 340 | | protected readonly ProtocolInstance instance_; |
| | 341 | | protected string host_; |
| | 342 | | protected int port_; |
| | 343 | | protected EndPoint sourceAddr_; |
| | 344 | | protected string connectionId_; |
| | 345 | | // Set when we set _host; used by the implementation of equivalent. |
| | 346 | | private string _normalizedHost; |
| | 347 | | } |