< Summary

Information
Class: Ice.Internal.IPEndpointI
Assembly: Ice
File(s): /_/csharp/src/Ice/Internal/IPEndpointI.cs
Tag: 125_37167941578
Line coverage
91%
Covered lines: 143
Uncovered lines: 13
Coverable lines: 156
Total lines: 348
Line coverage: 91.6%
Branch coverage
80%
Covered branches: 71
Total branches: 88
Branch coverage: 80.6%
Method coverage
100%
Covered methods: 20
Fully covered methods: 14
Total methods: 20
Method coverage: 100%
Full method coverage: 70%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)50%4490%
type()100%11100%
protocol()100%11100%
secure()100%11100%
connectionId()100%11100%
connectionId(...)100%22100%
connectors_async(...)100%11100%
expandHost()100%22100%
isLoopbackOrMulticast()100%44100%
equivalent(...)100%66100%
connectors(...)100%22100%
options()100%1414100%
GetHashCode()50%2287.5%
CompareTo(...)57.14%161478.95%
streamWriteImpl(...)100%11100%
initWithOptions(...)87.5%161688.89%
checkOption(...)66.67%191885.71%
normalizeHost(...)100%5466.67%

File(s)

/_/csharp/src/Ice/Internal/IPEndpointI.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using System.Globalization;
 4using System.Net;
 5
 6namespace Ice.Internal;
 7
 8public abstract class IPEndpointI : EndpointI
 9{
 110    protected IPEndpointI(ProtocolInstance instance, string host, int port, EndPoint sourceAddr, string connectionId)
 11    {
 112        instance_ = instance;
 113        host_ = host;
 114        _normalizedHost = normalizeHost(host_);
 115        port_ = port;
 116        sourceAddr_ = sourceAddr;
 117        connectionId_ = connectionId;
 118    }
 19
 120    protected IPEndpointI(ProtocolInstance instance)
 21    {
 122        instance_ = instance;
 123        host_ = null;
 124        _normalizedHost = null;
 125        port_ = 0;
 126        sourceAddr_ = null;
 127        connectionId_ = "";
 128    }
 29
 130    protected IPEndpointI(ProtocolInstance instance, Ice.InputStream s)
 31    {
 132        instance_ = instance;
 133        host_ = s.readString();
 134        _normalizedHost = normalizeHost(host_);
 135        port_ = s.readInt();
 136        if (port_ < 0 || port_ > 65535)
 37        {
 038            throw new MarshalException($"port value '{port_}' is out of range");
 39        }
 140        sourceAddr_ = null;
 141        connectionId_ = "";
 142    }
 43
 144    public override short type() => instance_.type();
 45
 146    public override string protocol() => instance_.protocol();
 47
 148    public override bool secure() => instance_.secure();
 49
 150    public override string connectionId() => connectionId_;
 51
 52    public override EndpointI connectionId(string connectionId)
 53    {
 154        if (connectionId.Equals(connectionId_, StringComparison.Ordinal))
 55        {
 156            return this;
 57        }
 58        else
 59        {
 160            return createEndpoint(host_, port_, connectionId);
 61        }
 62    }
 63
 64    public override void connectors_async(EndpointI_connectors callback) =>
 165        instance_.resolve(host_, port_, this, callback);
 66
 67    public override List<EndpointI> expandHost()
 68    {
 69        // If this endpoint has an empty host (wildcard address), don't expand, just return this endpoint.
 170        if (host_.Length == 0)
 71        {
 172            return [this];
 73        }
 74
 175        List<EndPoint> addresses = Network.getAddresses(
 176            host_,
 177            port_,
 178            instance_.protocolSupport(),
 179            instance_.preferIPv6(),
 180            true);
 81
 182        return addresses.Select(
 183            addr => createEndpoint(
 184                        Network.endpointAddressToString(addr),
 185                        Network.endpointPort(addr),
 186                        connectionId_) as EndpointI).ToList();
 87    }
 88
 89    // Empty host_ means the endpoint is a wildcard address. This method must be called only on an endpoint with an
 90    // empty host or an IP address.
 91    public override bool isLoopbackOrMulticast()
 92    {
 193        if (host_.Length > 0)
 94        {
 195            var ipEndPoint = IPEndPoint.Parse(host_);
 196            return IPAddress.IsLoopback(ipEndPoint.Address) || Network.isMulticast(ipEndPoint);
 97        }
 98        else
 99        {
 1100            return false;
 101        }
 102    }
 103
 104    public override bool equivalent(EndpointI endpoint)
 105    {
 1106        if (!(endpoint is IPEndpointI))
 107        {
 1108            return false;
 109        }
 1110        var ipEndpointI = (IPEndpointI)endpoint;
 1111        return ipEndpointI.type() == type() &&
 1112            ipEndpointI._normalizedHost == _normalizedHost &&
 1113            ipEndpointI.port_ == port_;
 114    }
 115
 116    public virtual List<Connector> connectors(List<EndPoint> addresses, NetworkProxy proxy)
 117    {
 1118        var connectors = new List<Connector>();
 1119        foreach (EndPoint p in addresses)
 120        {
 1121            connectors.Add(createConnector(p, proxy));
 122        }
 1123        return connectors;
 124    }
 125
 126    public override string options()
 127    {
 128        //
 129        // WARNING: Certain features, such as proxy validation in Glacier2,
 130        // depend on the format of proxy strings. Changes to toString() and
 131        // methods called to generate parts of the reference string could break
 132        // these features. Please review for all features that depend on the
 133        // format of proxyToString() before changing this and related code.
 134        //
 1135        string s = "";
 136
 1137        if (host_ != null && host_.Length > 0)
 138        {
 1139            s += " -h ";
 1140            bool addQuote = host_.Contains(':', StringComparison.Ordinal);
 1141            if (addQuote)
 142            {
 1143                s += "\"";
 144            }
 1145            s += host_;
 1146            if (addQuote)
 147            {
 1148                s += "\"";
 149            }
 150        }
 151
 1152        s += " -p " + port_;
 153
 1154        if (sourceAddr_ != null)
 155        {
 1156            string sourceAddr = Network.endpointAddressToString(sourceAddr_);
 1157            bool addQuote = sourceAddr.Contains(':', StringComparison.Ordinal);
 1158            s += " --sourceAddress ";
 1159            if (addQuote)
 160            {
 1161                s += "\"";
 162            }
 1163            s += sourceAddr;
 1164            if (addQuote)
 165            {
 1166                s += "\"";
 167            }
 168        }
 169
 1170        return s;
 171    }
 172
 173    public override int GetHashCode()
 174    {
 1175        var hash = new HashCode();
 1176        hash.Add(type());
 1177        hash.Add(host_);
 1178        hash.Add(port_);
 1179        if (sourceAddr_ is not null)
 180        {
 0181            hash.Add(sourceAddr_);
 182        }
 1183        hash.Add(connectionId_);
 1184        return hash.ToHashCode();
 185    }
 186
 187    public override int CompareTo(EndpointI other)
 188    {
 1189        if (!(other is IPEndpointI))
 190        {
 0191            return type() < other.type() ? -1 : 1;
 192        }
 193
 1194        var p = (IPEndpointI)other;
 1195        if (this == p)
 196        {
 0197            return 0;
 198        }
 199
 1200        int v = string.Compare(host_, p.host_, StringComparison.Ordinal);
 1201        if (v != 0)
 202        {
 1203            return v;
 204        }
 205
 1206        if (port_ < p.port_)
 207        {
 1208            return -1;
 209        }
 1210        else if (p.port_ < port_)
 211        {
 0212            return 1;
 213        }
 214
 1215        int rc = string.Compare(
 1216            Network.endpointAddressToString(sourceAddr_),
 1217            Network.endpointAddressToString(p.sourceAddr_),
 1218            StringComparison.Ordinal);
 1219        if (rc != 0)
 220        {
 0221            return rc;
 222        }
 223
 1224        return string.Compare(connectionId_, p.connectionId_, StringComparison.Ordinal);
 225    }
 226
 227    public override void streamWriteImpl(Ice.OutputStream s)
 228    {
 1229        s.writeString(host_);
 1230        s.writeInt(port_);
 1231    }
 232
 233    public virtual void initWithOptions(List<string> args, bool oaEndpoint)
 234    {
 1235        initWithOptions(args);
 236
 1237        if (host_ == null || host_.Length == 0)
 238        {
 1239            host_ = instance_.defaultHost();
 1240            _normalizedHost = normalizeHost(host_);
 241        }
 1242        else if (host_ == "*")
 243        {
 1244            if (oaEndpoint)
 245            {
 1246                host_ = "";
 1247                _normalizedHost = "";
 248            }
 249            else
 250            {
 0251                throw new ParseException($"'-h *' not valid for proxy endpoint '{ToString()}'");
 252            }
 253        }
 254
 1255        if (host_ == null)
 256        {
 1257            host_ = "";
 1258            _normalizedHost = normalizeHost(host_);
 259        }
 260
 1261        if (sourceAddr_ != null)
 262        {
 1263            if (oaEndpoint)
 264            {
 0265                throw new ParseException($"'--sourceAddress' not valid for object adapter endpoint '{ToString()}'");
 266            }
 267        }
 1268        else if (!oaEndpoint)
 269        {
 1270            sourceAddr_ = instance_.defaultSourceAddress();
 271        }
 1272    }
 273
 274    protected override bool checkOption(string option, string argument, string endpoint)
 275    {
 1276        if (option == "-h")
 277        {
 1278            host_ = argument ?? throw new ParseException(
 1279                $"no argument provided for -h option in endpoint '{endpoint}'");
 1280            _normalizedHost = normalizeHost(host_);
 281        }
 1282        else if (option == "-p")
 283        {
 1284            if (argument == null)
 285            {
 0286                throw new ParseException($"no argument provided for -p option in endpoint '{endpoint}'");
 287            }
 288
 289            try
 290            {
 1291                port_ = int.Parse(argument, CultureInfo.InvariantCulture);
 1292            }
 1293            catch (System.Exception ex) when (ex is FormatException or OverflowException)
 294            {
 1295                throw new ParseException($"invalid port value '{argument}' in endpoint '{endpoint}'", ex);
 296            }
 297
 1298            if (port_ < 0 || port_ > 65535)
 299            {
 0300                throw new ParseException($"port value '{argument}' out of range in endpoint '{endpoint}'");
 301            }
 302        }
 1303        else if (option == "--sourceAddress")
 304        {
 1305            if (argument == null)
 306            {
 0307                throw new ParseException($"no argument provided for --sourceAddress option in endpoint '{endpoint}'");
 308            }
 1309            sourceAddr_ = Network.getNumericAddress(argument) ??
 1310                throw new ParseException(
 1311                    $"invalid IP address provided for --sourceAddress option in endpoint '{endpoint}'");
 312        }
 313        else
 314        {
 1315            return false;
 316        }
 1317        return true;
 318    }
 319
 320    private static string normalizeHost(string host)
 321    {
 1322        if (host is not null && host.Contains(':', StringComparison.Ordinal))
 323        {
 324            // Could be an IPv6 address that we need to normalize.
 325            try
 326            {
 1327                return IPAddress.Parse(host).ToString(); // normalized host
 328            }
 0329            catch
 330            {
 331                // Ignore - don't normalize host.
 0332            }
 333        }
 1334        return host;
 1335    }
 336
 337    protected abstract Connector createConnector(EndPoint addr, NetworkProxy proxy);
 338
 339    protected abstract IPEndpointI createEndpoint(string host, int port, string connectionId);
 340
 341    protected readonly ProtocolInstance instance_;
 342    protected string host_;
 343    protected int port_;
 344    protected EndPoint sourceAddr_;
 345    protected string connectionId_;
 346    // Set when we set _host; used by the implementation of equivalent.
 347    private string _normalizedHost;
 348}