< Summary

Information
Class: Ice.Internal.Network
Assembly: Ice
File(s): /_/csharp/src/Ice/Internal/Network.cs
Tag: 125_37167941578
Line coverage
63%
Covered lines: 269
Uncovered lines: 152
Coverable lines: 421
Total lines: 1053
Line coverage: 63.8%
Branch coverage
59%
Covered branches: 137
Total branches: 232
Branch coverage: 59%
Method coverage
94%
Covered methods: 50
Fully covered methods: 18
Total methods: 53
Method coverage: 94.3%
Full method coverage: 33.9%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
socketErrorCode(...)100%11100%
interrupted(...)100%11100%
wouldBlock(...)100%11100%
connectionLost(...)37.5%88100%
connectionLost(...)83.33%6690%
connectionRefused(...)100%11100%
recvTruncated(...)100%210%
timeout(...)100%210%
isMulticast(...)100%44100%
isIPv6Supported()100%1166.67%
createSocket(...)100%5456.25%
createServerSocket(...)100%5466.67%
closeSocketNoThrow(...)50%2257.14%
closeSocket(...)50%2257.14%
setTcpNoDelay(...)100%1150%
setBlock(...)100%1150%
setSendBufferSize(...)100%1150%
getSendBufferSize(...)100%1150%
setRecvBufferSize(...)100%1150%
getRecvBufferSize(...)100%1150%
getSendBufferSizeNoThrow(...)100%1150%
getRecvBufferSizeNoThrow(...)100%1150%
setReuseAddress(...)100%1150%
setMcastInterface(...)50%2271.43%
setMcastGroup(...)80%121073.68%
setMcastTtl(...)0%620%
doBind(...)100%11100%
doListen(...)0%3237.5%
getProtocolSupport(...)100%11100%
getAddressForServer(...)100%44100%
getAddresses(...)81.58%433884.62%
getLocalAddresses(...)100%171684.62%
setTcpBufSize(...)37.5%531647.37%
getInterfacesForMulticast(...)66.67%66100%
fdToString(...)62.5%9880%
fdToString(...)50%2266.67%
addrToString(...)100%11100%
localAddrToString(...)100%22100%
remoteAddrToString(...)100%22100%
getLocalAddress(...)100%1150%
getRemoteAddress(...)100%1140%
addressAndPortToString(...)100%22100%
getInterfaceAddress(...)14.29%2192837.5%
getInterfaceIndex(...)37.5%1172445.45%
getNumericAddress(...)50%4490.91%
isWildcard(...)25%11425%
getLoopbackAddresses(...)100%44100%
addressEquals(...)33.33%13642.86%
endpointAddressToString(...)100%44100%
endpointPort(...)50%4480%
.ctor(...)100%11100%
Compare(...)41.67%151271.43%
.cctor()100%11100%

File(s)

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

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using System.Globalization;
 4using System.Net;
 5using System.Net.NetworkInformation;
 6using System.Net.Sockets;
 7
 8namespace Ice.Internal;
 9
 10internal sealed class Network
 11{
 12    // ProtocolSupport
 13    public const int EnableIPv4 = 0;
 14    public const int EnableIPv6 = 1;
 15    public const int EnableBoth = 2;
 16
 117    internal static SocketError socketErrorCode(System.Net.Sockets.SocketException ex) => ex.SocketErrorCode;
 18
 19    internal static bool interrupted(System.Net.Sockets.SocketException ex) =>
 120        socketErrorCode(ex) == SocketError.Interrupted;
 21
 22    internal static bool wouldBlock(System.Net.Sockets.SocketException ex) =>
 123        socketErrorCode(ex) == SocketError.WouldBlock;
 24
 25    internal static bool connectionLost(System.Net.Sockets.SocketException ex)
 26    {
 127        SocketError error = socketErrorCode(ex);
 128        return error == SocketError.ConnectionReset ||
 129               error == SocketError.Shutdown ||
 130               error == SocketError.ConnectionAborted ||
 131               error == SocketError.NetworkDown ||
 132               error == SocketError.NetworkReset;
 33    }
 34
 35    internal static bool connectionLost(System.IO.IOException ex)
 36    {
 37        //
 38        // In some cases the IOException has an inner exception that we can pass directly
 39        // to the other overloading of connectionLost().
 40        //
 141        if (ex.InnerException != null && ex.InnerException is System.Net.Sockets.SocketException)
 42        {
 143            return connectionLost(ex.InnerException as System.Net.Sockets.SocketException);
 44        }
 45
 46        //
 47        // In other cases the IOException has no inner exception. We could examine the
 48        // exception's message, but that is fragile due to localization issues. We
 49        // resort to extracting the value of the protected HResult member via reflection.
 50        //
 151        int hr = (int)ex.GetType().GetProperty(
 152            "HResult",
 153            System.Reflection.BindingFlags.Instance |
 154            System.Reflection.BindingFlags.NonPublic |
 155            System.Reflection.BindingFlags.Public).GetValue(ex, null);
 56
 57        //
 58        // This value corresponds to the following errors:
 59        //
 60        // "Authentication failed because the remote party has closed the transport stream"
 61        //
 162        if (hr == -2146232800)
 63        {
 164            return true;
 65        }
 066        return false;
 67    }
 68
 69    internal static bool connectionRefused(System.Net.Sockets.SocketException ex) =>
 170        socketErrorCode(ex) == SocketError.ConnectionRefused;
 71
 72    internal static bool recvTruncated(System.Net.Sockets.SocketException ex) =>
 073        socketErrorCode(ex) == SocketError.MessageSize;
 74
 75    internal static bool timeout(System.IO.IOException ex) =>
 76        //
 77        // TODO: Instead of testing for an English substring, we need to examine the inner
 78        // exception (if there is one).
 79        //
 080        ex.Message.Contains("period of time", StringComparison.Ordinal);
 81
 82    internal static bool isMulticast(IPEndPoint addr)
 83    {
 184        if (addr.AddressFamily == AddressFamily.InterNetwork)
 85        {
 186            byte firstOctet = addr.Address.GetAddressBytes()[0];
 187            return firstOctet >= 224 && firstOctet <= 239;
 88        }
 89        else // AddressFamily.InterNetworkV6
 90        {
 191            return addr.Address.IsIPv6Multicast;
 92        }
 93    }
 94
 95    internal static bool isIPv6Supported()
 96    {
 97        try
 98        {
 199            using var socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
 1100            closeSocketNoThrow(socket);
 1101            return true;
 102        }
 0103        catch (System.Net.Sockets.SocketException)
 104        {
 0105            return false;
 106        }
 1107    }
 108
 109    internal static Socket createSocket(bool udp, AddressFamily family)
 110    {
 111        Socket socket;
 112
 113        try
 114        {
 1115            if (udp)
 116            {
 1117                socket = new Socket(family, SocketType.Dgram, ProtocolType.Udp);
 118            }
 119            else
 120            {
 1121                socket = new Socket(family, SocketType.Stream, ProtocolType.Tcp);
 122            }
 1123        }
 0124        catch (System.Net.Sockets.SocketException ex)
 125        {
 0126            throw new Ice.SocketException(ex);
 127        }
 0128        catch (ArgumentException ex)
 129        {
 0130            throw new Ice.SocketException(ex);
 131        }
 132
 1133        if (!udp)
 134        {
 135            try
 136            {
 1137                setTcpNoDelay(socket);
 1138                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1);
 1139            }
 0140            catch (System.Net.Sockets.SocketException ex)
 141            {
 0142                closeSocketNoThrow(socket);
 0143                throw new Ice.SocketException(ex);
 144            }
 145        }
 1146        return socket;
 147    }
 148
 149    internal static Socket createServerSocket(bool udp, AddressFamily family, int protocol)
 150    {
 1151        Socket socket = createSocket(udp, family);
 1152        if (family == AddressFamily.InterNetworkV6 && protocol != EnableIPv4)
 153        {
 154            try
 155            {
 1156                int flag = protocol == EnableIPv6 ? 1 : 0;
 1157                socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, flag);
 1158            }
 0159            catch (System.Net.Sockets.SocketException ex)
 160            {
 0161                closeSocketNoThrow(socket);
 0162                throw new Ice.SocketException(ex);
 163            }
 164        }
 1165        return socket;
 166    }
 167
 168    internal static void closeSocketNoThrow(Socket socket)
 169    {
 1170        if (socket == null)
 171        {
 0172            return;
 173        }
 174        try
 175        {
 1176            socket.Close();
 1177        }
 0178        catch (System.Net.Sockets.SocketException)
 179        {
 180            // Ignore
 0181        }
 1182    }
 183
 184    internal static void closeSocket(Socket socket)
 185    {
 1186        if (socket == null)
 187        {
 0188            return;
 189        }
 190        try
 191        {
 1192            socket.Close();
 1193        }
 0194        catch (System.Net.Sockets.SocketException ex)
 195        {
 0196            throw new Ice.SocketException(ex);
 197        }
 1198    }
 199
 200    internal static void setTcpNoDelay(Socket socket)
 201    {
 202        try
 203        {
 1204            socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, 1);
 1205        }
 0206        catch (System.Exception ex)
 207        {
 0208            closeSocketNoThrow(socket);
 0209            throw new Ice.SocketException(ex);
 210        }
 1211    }
 212
 213    internal static void setBlock(Socket socket, bool block)
 214    {
 215        try
 216        {
 1217            socket.Blocking = block;
 1218        }
 0219        catch (System.Net.Sockets.SocketException ex)
 220        {
 0221            closeSocketNoThrow(socket);
 0222            throw new Ice.SocketException(ex);
 223        }
 1224    }
 225
 226    internal static void setSendBufferSize(Socket socket, int sz)
 227    {
 228        try
 229        {
 1230            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, sz);
 1231        }
 0232        catch (System.Net.Sockets.SocketException ex)
 233        {
 0234            closeSocketNoThrow(socket);
 0235            throw new Ice.SocketException(ex);
 236        }
 1237    }
 238
 239    internal static int getSendBufferSize(Socket socket)
 240    {
 241        int sz;
 242        try
 243        {
 1244            sz = (int)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer);
 1245        }
 0246        catch (System.Net.Sockets.SocketException ex)
 247        {
 0248            closeSocketNoThrow(socket);
 0249            throw new Ice.SocketException(ex);
 250        }
 1251        return sz;
 252    }
 253
 254    internal static void setRecvBufferSize(Socket socket, int sz)
 255    {
 256        try
 257        {
 1258            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, sz);
 1259        }
 0260        catch (System.Net.Sockets.SocketException ex)
 261        {
 0262            closeSocketNoThrow(socket);
 0263            throw new Ice.SocketException(ex);
 264        }
 1265    }
 266
 267    internal static int getRecvBufferSize(Socket socket)
 268    {
 269        int sz;
 270        try
 271        {
 1272            sz = (int)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer);
 1273        }
 0274        catch (System.Net.Sockets.SocketException ex)
 275        {
 0276            closeSocketNoThrow(socket);
 0277            throw new Ice.SocketException(ex);
 278        }
 1279        return sz;
 280    }
 281
 282    // Non-throwing variants that return 0 on failure and never close the socket.
 283    internal static int getSendBufferSizeNoThrow(Socket socket)
 284    {
 285        try
 286        {
 1287            return (int)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer);
 288        }
 0289        catch
 290        {
 0291            return 0;
 292        }
 1293    }
 294
 295    internal static int getRecvBufferSizeNoThrow(Socket socket)
 296    {
 297        try
 298        {
 1299            return (int)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer);
 300        }
 0301        catch
 302        {
 0303            return 0;
 304        }
 1305    }
 306
 307    internal static void setReuseAddress(Socket socket, bool reuse)
 308    {
 309        try
 310        {
 1311            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, reuse ? 1 : 0);
 1312        }
 0313        catch (System.Net.Sockets.SocketException ex)
 314        {
 0315            closeSocketNoThrow(socket);
 0316            throw new Ice.SocketException(ex);
 317        }
 1318    }
 319
 320    internal static void setMcastInterface(Socket socket, string iface, AddressFamily family)
 321    {
 322        try
 323        {
 1324            if (family == AddressFamily.InterNetwork)
 325            {
 1326                socket.SetSocketOption(
 1327                    SocketOptionLevel.IP,
 1328                    SocketOptionName.MulticastInterface,
 1329                    getInterfaceAddress(iface, family).GetAddressBytes());
 330            }
 331            else
 332            {
 0333                socket.SetSocketOption(
 0334                    SocketOptionLevel.IPv6,
 0335                    SocketOptionName.MulticastInterface,
 0336                    getInterfaceIndex(iface, family));
 337            }
 1338        }
 1339        catch (System.Exception ex)
 340        {
 1341            closeSocketNoThrow(socket);
 1342            throw new Ice.SocketException(ex);
 343        }
 1344    }
 345
 346    internal static void setMcastGroup(Socket s, IPAddress group, string iface)
 347    {
 348        try
 349        {
 1350            var indexes = new HashSet<int>();
 1351            foreach (string intf in getInterfacesForMulticast(iface, getProtocolSupport(group)))
 352            {
 1353                if (group.AddressFamily == AddressFamily.InterNetwork)
 354                {
 355                    MulticastOption option;
 1356                    IPAddress addr = getInterfaceAddress(intf, group.AddressFamily);
 1357                    if (addr == null)
 358                    {
 0359                        option = new MulticastOption(group);
 360                    }
 361                    else
 362                    {
 1363                        option = new MulticastOption(group, addr);
 364                    }
 1365                    s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, option);
 366                }
 367                else
 368                {
 1369                    int index = getInterfaceIndex(intf, group.AddressFamily);
 1370                    if (indexes.Add(index))
 371                    {
 372                        IPv6MulticastOption option;
 1373                        if (index == -1)
 374                        {
 0375                            option = new IPv6MulticastOption(group);
 376                        }
 377                        else
 378                        {
 1379                            option = new IPv6MulticastOption(group, index);
 380                        }
 1381                        s.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership, option);
 382                    }
 383                }
 384            }
 1385        }
 0386        catch (System.Exception ex)
 387        {
 0388            closeSocketNoThrow(s);
 0389            throw new Ice.SocketException(ex);
 390        }
 1391    }
 392
 393    internal static void setMcastTtl(Socket socket, int ttl, AddressFamily family)
 394    {
 395        try
 396        {
 0397            if (family == AddressFamily.InterNetwork)
 398            {
 0399                socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, ttl);
 400            }
 401            else
 402            {
 0403                socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.MulticastTimeToLive, ttl);
 404            }
 0405        }
 0406        catch (System.Net.Sockets.SocketException ex)
 407        {
 0408            closeSocketNoThrow(socket);
 0409            throw new Ice.SocketException(ex);
 410        }
 0411    }
 412
 413    internal static IPEndPoint doBind(Socket socket, EndPoint addr)
 414    {
 415        try
 416        {
 1417            socket.Bind(addr);
 1418            return (IPEndPoint)socket.LocalEndPoint;
 419        }
 1420        catch (System.Net.Sockets.SocketException ex)
 421        {
 1422            closeSocketNoThrow(socket);
 1423            throw new Ice.SocketException(ex);
 424        }
 1425    }
 426
 427    internal static void doListen(Socket socket, int backlog)
 428    {
 429    repeatListen:
 430
 431        try
 432        {
 1433            socket.Listen(backlog);
 1434        }
 0435        catch (System.Net.Sockets.SocketException ex)
 436        {
 0437            if (interrupted(ex))
 438            {
 0439                goto repeatListen;
 440            }
 441
 0442            closeSocketNoThrow(socket);
 0443            throw new Ice.SocketException(ex);
 444        }
 1445    }
 446
 447    internal static int getProtocolSupport(IPAddress addr) =>
 1448        addr.AddressFamily == AddressFamily.InterNetwork ? EnableIPv4 : EnableIPv6;
 449
 450    internal static EndPoint getAddressForServer(string host, int port, int protocol, bool preferIPv6)
 451    {
 1452        if (host.Length == 0)
 453        {
 1454            if (protocol != EnableIPv4)
 455            {
 1456                return new IPEndPoint(IPAddress.IPv6Any, port);
 457            }
 458            else
 459            {
 1460                return new IPEndPoint(IPAddress.Any, port);
 461            }
 462        }
 1463        return getAddresses(host, port, protocol, preferIPv6, true)[0];
 464    }
 465
 466    internal static List<EndPoint> getAddresses(
 467        string host,
 468        int port,
 469        int protocol,
 470        bool preferIPv6,
 471        bool blocking)
 472    {
 1473        var addresses = new List<EndPoint>();
 1474        if (host.Length == 0)
 475        {
 1476            foreach (IPAddress a in getLoopbackAddresses(protocol))
 477            {
 1478                addresses.Add(new IPEndPoint(a, port));
 479            }
 1480            if (protocol == EnableBoth)
 481            {
 1482                if (preferIPv6)
 483                {
 0484                    Ice.UtilInternal.Collections.Sort(ref addresses, _preferIPv6Comparator);
 485                }
 486                else
 487                {
 1488                    Ice.UtilInternal.Collections.Sort(ref addresses, _preferIPv4Comparator);
 489                }
 490            }
 1491            return addresses;
 492        }
 493
 1494        int retry = 5;
 495
 496    repeatGetHostByName:
 497        try
 498        {
 499            //
 500            // No need for lookup if host is ip address.
 501            //
 502            try
 503            {
 1504                var addr = IPAddress.Parse(host);
 1505                if ((addr.AddressFamily == AddressFamily.InterNetwork && protocol != EnableIPv6) ||
 1506                   (addr.AddressFamily == AddressFamily.InterNetworkV6 && protocol != EnableIPv4))
 507                {
 1508                    addresses.Add(new IPEndPoint(addr, port));
 1509                    return addresses;
 510                }
 511                else
 512                {
 0513                    throw new DNSException(host);
 514                }
 515            }
 1516            catch (FormatException)
 517            {
 1518                if (!blocking)
 519                {
 1520                    return addresses;
 521                }
 1522            }
 523
 1524            foreach (IPAddress a in Dns.GetHostAddresses(host))
 525            {
 1526                if ((a.AddressFamily == AddressFamily.InterNetwork && protocol != EnableIPv6) ||
 1527                   (a.AddressFamily == AddressFamily.InterNetworkV6 && protocol != EnableIPv4))
 528                {
 1529                    addresses.Add(new IPEndPoint(a, port));
 530                }
 531            }
 532
 1533            if (protocol == EnableBoth)
 534            {
 1535                if (preferIPv6)
 536                {
 1537                    Ice.UtilInternal.Collections.Sort(ref addresses, _preferIPv6Comparator);
 538                }
 539                else
 540                {
 1541                    Ice.UtilInternal.Collections.Sort(ref addresses, _preferIPv4Comparator);
 542                }
 543            }
 1544        }
 1545        catch (System.Net.Sockets.SocketException ex)
 546        {
 1547            if (socketErrorCode(ex) == SocketError.TryAgain && --retry >= 0)
 548            {
 0549                goto repeatGetHostByName;
 550            }
 1551            throw new DNSException(host, ex);
 552        }
 0553        catch (System.Exception ex)
 554        {
 0555            throw new DNSException(host, ex);
 556        }
 557
 558        //
 559        // No InterNetwork/InterNetworkV6 available.
 560        //
 1561        if (addresses.Count == 0)
 562        {
 0563            throw new DNSException(host);
 564        }
 1565        return addresses;
 1566    }
 567
 568    // Only used for multicast.
 569    private static IEnumerable<IPAddress> getLocalAddresses(int protocol)
 570    {
 571        try
 572        {
 1573            var result = new List<IPAddress>();
 1574            foreach (NetworkInterface p in NetworkInterface.GetAllNetworkInterfaces())
 575            {
 1576                if (p.SupportsMulticast && p.OperationalStatus == OperationalStatus.Up)
 577                {
 1578                    IPInterfaceProperties ipProps = p.GetIPProperties();
 1579                    foreach (UnicastIPAddressInformation uni in ipProps.UnicastAddresses)
 580                    {
 1581                        if ((uni.Address.AddressFamily == AddressFamily.InterNetwork && protocol != EnableIPv6) ||
 1582                            (uni.Address.AddressFamily == AddressFamily.InterNetworkV6 && protocol != EnableIPv4))
 583                        {
 1584                            result.Add(uni.Address);
 1585                            break; // We return at most one address per interface.
 586                        }
 587                    }
 588                }
 589            }
 1590            return result;
 591        }
 0592        catch (System.Exception ex)
 593        {
 0594            throw new SyscallException("Failed to enumerate the local network interfaces.", ex);
 595        }
 1596    }
 597
 598    internal static void setTcpBufSize(Socket socket, int rcvSize, int sndSize, ProtocolInstance instance)
 599    {
 1600        if (rcvSize > 0)
 601        {
 602            //
 603            // Try to set the buffer size. The kernel will silently adjust
 604            // the size to an acceptable value. Then read the size back to
 605            // get the size that was actually set.
 606            //
 1607            setRecvBufferSize(socket, rcvSize);
 1608            int size = getRecvBufferSize(socket);
 1609            if (size < rcvSize)
 610            {
 611                // Warn if the size that was set is less than the requested size and
 612                // we have not already warned.
 0613                BufSizeWarnInfo winfo = instance.getBufSizeWarn(Ice.TCPEndpointType.value);
 0614                if (!winfo.rcvWarn || rcvSize != winfo.rcvSize)
 615                {
 0616                    instance.logger().warning("TCP receive buffer size: requested size of " + rcvSize +
 0617                                              " adjusted to " + size);
 0618                    instance.setRcvBufSizeWarn(Ice.TCPEndpointType.value, rcvSize);
 619                }
 620            }
 621        }
 622
 1623        if (sndSize > 0)
 624        {
 625            //
 626            // Try to set the buffer size. The kernel will silently adjust
 627            // the size to an acceptable value. Then read the size back to
 628            // get the size that was actually set.
 629            //
 1630            setSendBufferSize(socket, sndSize);
 1631            int size = getSendBufferSize(socket);
 1632            if (size < sndSize) // Warn if the size that was set is less than the requested size.
 633            {
 634                // Warn if the size that was set is less than the requested size and
 635                // we have not already warned.
 0636                BufSizeWarnInfo winfo = instance.getBufSizeWarn(Ice.TCPEndpointType.value);
 0637                if (!winfo.sndWarn || sndSize != winfo.sndSize)
 638                {
 0639                    instance.logger().warning("TCP send buffer size: requested size of " + sndSize +
 0640                                              " adjusted to " + size);
 0641                    instance.setSndBufSizeWarn(Ice.TCPEndpointType.value, sndSize);
 642                }
 643            }
 644        }
 1645    }
 646
 647    internal static List<string> getInterfacesForMulticast(string intf, int protocol) =>
 1648        isWildcard(intf, out bool ipv4Wildcard)
 1649            ? getLocalAddresses(ipv4Wildcard ? EnableIPv4 : protocol)
 1650                .Select(addr => addr.ToString())
 1651                .Distinct()
 1652                .ToList()
 1653            : [intf];
 654
 655    internal static string fdToString(Socket socket, NetworkProxy proxy, EndPoint target)
 656    {
 657        try
 658        {
 1659            if (socket == null)
 660            {
 0661                return "<closed>";
 662            }
 663
 1664            EndPoint remote = getRemoteAddress(socket);
 665
 1666            var s = new System.Text.StringBuilder();
 1667            s.Append("local address = " + localAddrToString(getLocalAddress(socket)));
 1668            if (proxy != null)
 669            {
 1670                remote ??= proxy.getAddress();
 1671                s.Append("\n" + proxy.getName() + " proxy address = " + remoteAddrToString(remote));
 1672                s.Append("\nremote address = " + remoteAddrToString(target));
 673            }
 674            else
 675            {
 1676                remote ??= target;
 1677                s.Append("\nremote address = " + remoteAddrToString(remote));
 678            }
 1679            return s.ToString();
 680        }
 0681        catch (ObjectDisposedException)
 682        {
 0683            return "<closed>";
 684        }
 1685    }
 686
 687    internal static string fdToString(Socket socket)
 688    {
 689        try
 690        {
 1691            if (socket == null)
 692            {
 0693                return "<closed>";
 694            }
 1695            var s = new System.Text.StringBuilder();
 1696            s.Append("local address = " + localAddrToString(getLocalAddress(socket)));
 1697            s.Append("\nremote address = " + remoteAddrToString(getRemoteAddress(socket)));
 1698            return s.ToString();
 699        }
 0700        catch (ObjectDisposedException)
 701        {
 0702            return "<closed>";
 703        }
 1704    }
 705
 706    internal static string
 1707    addrToString(EndPoint addr) => addressAndPortToString(endpointAddressToString(addr), endpointPort(addr));
 708
 709    internal static string localAddrToString(EndPoint endpoint)
 710    {
 1711        if (endpoint == null)
 712        {
 1713            return "<not bound>";
 714        }
 1715        return addressAndPortToString(endpointAddressToString(endpoint), endpointPort(endpoint));
 716    }
 717
 718    internal static string remoteAddrToString(EndPoint endpoint)
 719    {
 1720        if (endpoint == null)
 721        {
 1722            return "<not connected>";
 723        }
 1724        return addressAndPortToString(endpointAddressToString(endpoint), endpointPort(endpoint));
 725    }
 726
 727    internal static EndPoint getLocalAddress(Socket socket)
 728    {
 729        try
 730        {
 1731            return socket.LocalEndPoint;
 732        }
 0733        catch (System.Net.Sockets.SocketException ex)
 734        {
 0735            throw new Ice.SocketException(ex);
 736        }
 1737    }
 738
 739    internal static EndPoint getRemoteAddress(Socket socket)
 740    {
 741        try
 742        {
 1743            return socket.RemoteEndPoint;
 744        }
 0745        catch (System.Net.Sockets.SocketException)
 746        {
 0747        }
 0748        return null;
 1749    }
 750
 751    // Combines an address and port into a host:port string, enclosing an IPv6 address in square brackets,
 752    // e.g. [::1]:4061.
 753    private static string addressAndPortToString(string address, int port) =>
 1754        address.Contains(':', StringComparison.Ordinal) ? $"[{address}]:{port}" : $"{address}:{port}";
 755
 756    private static IPAddress getInterfaceAddress(string iface, AddressFamily family)
 757    {
 1758        if (iface.Length == 0)
 759        {
 0760            return null;
 761        }
 762
 763        //
 764        // The iface parameter must either be an IP address, an
 765        // index or the name of an interface. If it's an index we
 766        // just return it. If it's an IP address we search for an
 767        // interface which has this IP address. If it's a name we
 768        // search an interface with this name.
 769        //
 770
 771        try
 772        {
 1773            return IPAddress.Parse(iface);
 774        }
 1775        catch (FormatException)
 776        {
 1777        }
 778
 1779        NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
 780        try
 781        {
 1782            int index = int.Parse(iface, CultureInfo.InvariantCulture);
 0783            foreach (NetworkInterface ni in nics)
 784            {
 0785                IPInterfaceProperties ipProps = ni.GetIPProperties();
 0786                int interfaceIndex = -1;
 0787                if (family == AddressFamily.InterNetwork)
 788                {
 0789                    IPv4InterfaceProperties ipv4Props = ipProps.GetIPv4Properties();
 0790                    if (ipv4Props != null && ipv4Props.Index == index)
 791                    {
 0792                        interfaceIndex = ipv4Props.Index;
 793                    }
 794                }
 795                else
 796                {
 0797                    IPv6InterfaceProperties ipv6Props = ipProps.GetIPv6Properties();
 0798                    if (ipv6Props != null && ipv6Props.Index == index)
 799                    {
 0800                        interfaceIndex = ipv6Props.Index;
 801                    }
 802                }
 0803                if (interfaceIndex >= 0)
 804                {
 0805                    foreach (UnicastIPAddressInformation a in ipProps.UnicastAddresses)
 806                    {
 0807                        if (a.Address.AddressFamily == family)
 808                        {
 0809                            return a.Address;
 810                        }
 811                    }
 812                }
 813            }
 0814        }
 1815        catch (FormatException)
 816        {
 1817        }
 818
 1819        foreach (NetworkInterface ni in nics)
 820        {
 1821            if (ni.Name == iface)
 822            {
 0823                IPInterfaceProperties ipProps = ni.GetIPProperties();
 0824                foreach (UnicastIPAddressInformation a in ipProps.UnicastAddresses)
 825                {
 0826                    if (a.Address.AddressFamily == family)
 827                    {
 0828                        return a.Address;
 829                    }
 830                }
 831            }
 832        }
 833
 1834        throw new ArgumentException("couldn't find interface `" + iface + "'");
 1835    }
 836
 837    private static int getInterfaceIndex(string iface, AddressFamily family)
 838    {
 1839        if (iface.Length == 0)
 840        {
 0841            return -1;
 842        }
 843
 844        //
 845        // The iface parameter must either be an IP address, an
 846        // index or the name of an interface. If it's an index we
 847        // just return it. If it's an IP address we search for an
 848        // interface which has this IP address. If it's a name we
 849        // search an interface with this name.
 850        //
 851        try
 852        {
 1853            return int.Parse(iface, CultureInfo.InvariantCulture);
 854        }
 1855        catch (FormatException)
 856        {
 1857        }
 858
 1859        NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
 860        try
 861        {
 1862            var addr = IPAddress.Parse(iface);
 1863            foreach (NetworkInterface ni in nics)
 864            {
 1865                IPInterfaceProperties ipProps = ni.GetIPProperties();
 1866                foreach (UnicastIPAddressInformation uni in ipProps.UnicastAddresses)
 867                {
 1868                    if (uni.Address.Equals(addr))
 869                    {
 1870                        if (addr.AddressFamily == AddressFamily.InterNetwork)
 871                        {
 0872                            IPv4InterfaceProperties ipv4Props = ipProps.GetIPv4Properties();
 0873                            if (ipv4Props != null)
 874                            {
 0875                                return ipv4Props.Index;
 876                            }
 877                        }
 878                        else
 879                        {
 1880                            IPv6InterfaceProperties ipv6Props = ipProps.GetIPv6Properties();
 1881                            if (ipv6Props != null)
 882                            {
 1883                                return ipv6Props.Index;
 884                            }
 885                        }
 886                    }
 887                }
 888            }
 0889        }
 0890        catch (FormatException)
 891        {
 0892        }
 893
 0894        foreach (NetworkInterface ni in nics)
 895        {
 0896            if (ni.Name == iface)
 897            {
 0898                IPInterfaceProperties ipProps = ni.GetIPProperties();
 0899                if (family == AddressFamily.InterNetwork)
 900                {
 0901                    IPv4InterfaceProperties ipv4Props = ipProps.GetIPv4Properties();
 0902                    if (ipv4Props != null)
 903                    {
 0904                        return ipv4Props.Index;
 905                    }
 906                }
 907                else
 908                {
 0909                    IPv6InterfaceProperties ipv6Props = ipProps.GetIPv6Properties();
 0910                    if (ipv6Props != null)
 911                    {
 0912                        return ipv6Props.Index;
 913                    }
 914                }
 915            }
 916        }
 917
 0918        throw new ArgumentException("couldn't find interface `" + iface + "'");
 1919    }
 920
 921    internal static EndPoint getNumericAddress(string sourceAddress)
 922    {
 1923        EndPoint addr = null;
 1924        if (!string.IsNullOrEmpty(sourceAddress))
 925        {
 1926            List<EndPoint> addrs = getAddresses(
 1927                sourceAddress,
 1928                0,
 1929                EnableBoth,
 1930                preferIPv6: false,
 1931                blocking: false);
 1932            if (addrs.Count != 0)
 933            {
 1934                return addrs[0];
 935            }
 936        }
 0937        return addr;
 938    }
 939
 940    private static bool isWildcard(string address, out bool ipv4Wildcard)
 941    {
 1942        ipv4Wildcard = false;
 1943        if (address.Length == 0)
 944        {
 1945            return true;
 946        }
 947
 948        try
 949        {
 0950            var addr = IPAddress.Parse(address);
 0951            if (addr.Equals(IPAddress.Any))
 952            {
 0953                ipv4Wildcard = true;
 0954                return true;
 955            }
 0956            return addr.Equals(IPAddress.IPv6Any);
 957        }
 0958        catch (System.Exception)
 959        {
 0960        }
 961
 0962        return false;
 0963    }
 964
 965    internal static List<IPAddress> getLoopbackAddresses(int protocol)
 966    {
 1967        var addresses = new List<IPAddress>();
 1968        if (protocol != EnableIPv4)
 969        {
 1970            addresses.Add(IPAddress.IPv6Loopback);
 971        }
 1972        if (protocol != EnableIPv6)
 973        {
 1974            addresses.Add(IPAddress.Loopback);
 975        }
 1976        return addresses;
 977    }
 978
 979    internal static bool addressEquals(EndPoint addr1, EndPoint addr2)
 980    {
 1981        if (addr1 == null)
 982        {
 1983            if (addr2 == null)
 984            {
 1985                return true;
 986            }
 987            else
 988            {
 0989                return false;
 990            }
 991        }
 0992        else if (addr2 == null)
 993        {
 0994            return false;
 995        }
 996
 0997        return addr1.Equals(addr2);
 998    }
 999
 1000    internal static string endpointAddressToString(EndPoint endpoint)
 1001    {
 11002        if (endpoint != null)
 1003        {
 11004            if (endpoint is IPEndPoint)
 1005            {
 11006                var ipEndpoint = (IPEndPoint)endpoint;
 11007                return ipEndpoint.Address.ToString();
 1008            }
 1009        }
 11010        return "";
 1011    }
 1012
 1013    internal static int endpointPort(EndPoint endpoint)
 1014    {
 11015        if (endpoint != null)
 1016        {
 11017            if (endpoint is IPEndPoint)
 1018            {
 11019                var ipEndpoint = (IPEndPoint)endpoint;
 11020                return ipEndpoint.Port;
 1021            }
 1022        }
 01023        return -1;
 1024    }
 1025
 1026    private class EndPointComparator : IComparer<EndPoint>
 1027    {
 11028        public EndPointComparator(bool ipv6) => _ipv6 = ipv6;
 1029
 1030        public int Compare(EndPoint lhs, EndPoint rhs)
 1031        {
 11032            if (lhs.AddressFamily == AddressFamily.InterNetwork &&
 11033               rhs.AddressFamily == AddressFamily.InterNetworkV6)
 1034            {
 01035                return _ipv6 ? 1 : -1;
 1036            }
 11037            else if (lhs.AddressFamily == AddressFamily.InterNetworkV6 &&
 11038                    rhs.AddressFamily == AddressFamily.InterNetwork)
 1039            {
 11040                return _ipv6 ? -1 : 1;
 1041            }
 1042            else
 1043            {
 01044                return 0;
 1045            }
 1046        }
 1047
 1048        private readonly bool _ipv6;
 1049    }
 1050
 11051    private static readonly EndPointComparator _preferIPv4Comparator = new EndPointComparator(false);
 11052    private static readonly EndPointComparator _preferIPv6Comparator = new EndPointComparator(true);
 1053}

Methods/Properties

socketErrorCode(System.Net.Sockets.SocketException)
interrupted(System.Net.Sockets.SocketException)
wouldBlock(System.Net.Sockets.SocketException)
connectionLost(System.Net.Sockets.SocketException)
connectionLost(System.IO.IOException)
connectionRefused(System.Net.Sockets.SocketException)
recvTruncated(System.Net.Sockets.SocketException)
timeout(System.IO.IOException)
isMulticast(System.Net.IPEndPoint)
isIPv6Supported()
createSocket(bool, System.Net.Sockets.AddressFamily)
createServerSocket(bool, System.Net.Sockets.AddressFamily, int)
closeSocketNoThrow(System.Net.Sockets.Socket)
closeSocket(System.Net.Sockets.Socket)
setTcpNoDelay(System.Net.Sockets.Socket)
setBlock(System.Net.Sockets.Socket, bool)
setSendBufferSize(System.Net.Sockets.Socket, int)
getSendBufferSize(System.Net.Sockets.Socket)
setRecvBufferSize(System.Net.Sockets.Socket, int)
getRecvBufferSize(System.Net.Sockets.Socket)
getSendBufferSizeNoThrow(System.Net.Sockets.Socket)
getRecvBufferSizeNoThrow(System.Net.Sockets.Socket)
setReuseAddress(System.Net.Sockets.Socket, bool)
setMcastInterface(System.Net.Sockets.Socket, string, System.Net.Sockets.AddressFamily)
setMcastGroup(System.Net.Sockets.Socket, System.Net.IPAddress, string)
setMcastTtl(System.Net.Sockets.Socket, int, System.Net.Sockets.AddressFamily)
doBind(System.Net.Sockets.Socket, System.Net.EndPoint)
doListen(System.Net.Sockets.Socket, int)
getProtocolSupport(System.Net.IPAddress)
getAddressForServer(string, int, int, bool)
getAddresses(string, int, int, bool, bool)
getLocalAddresses(int)
setTcpBufSize(System.Net.Sockets.Socket, int, int, Ice.Internal.ProtocolInstance)
getInterfacesForMulticast(string, int)
fdToString(System.Net.Sockets.Socket, Ice.Internal.NetworkProxy, System.Net.EndPoint)
fdToString(System.Net.Sockets.Socket)
addrToString(System.Net.EndPoint)
localAddrToString(System.Net.EndPoint)
remoteAddrToString(System.Net.EndPoint)
getLocalAddress(System.Net.Sockets.Socket)
getRemoteAddress(System.Net.Sockets.Socket)
addressAndPortToString(string, int)
getInterfaceAddress(string, System.Net.Sockets.AddressFamily)
getInterfaceIndex(string, System.Net.Sockets.AddressFamily)
getNumericAddress(string)
isWildcard(string, out bool)
getLoopbackAddresses(int)
addressEquals(System.Net.EndPoint, System.Net.EndPoint)
endpointAddressToString(System.Net.EndPoint)
endpointPort(System.Net.EndPoint)
.ctor(bool)
Compare(System.Net.EndPoint, System.Net.EndPoint)
.cctor()