< Summary

Information
Class: Ice.Internal.Network
Assembly: Ice
File(s): /_/csharp/src/Ice/Internal/Network.cs
Tag: 91_21789722663
Line coverage
65%
Covered lines: 279
Uncovered lines: 150
Coverable lines: 429
Total lines: 1056
Line coverage: 65%
Branch coverage
61%
Covered branches: 144
Total branches: 236
Branch coverage: 61%
Method coverage
94%
Covered methods: 48
Total methods: 51
Method coverage: 94.1%

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%6.04690%
connectionRefused(...)100%11100%
recvTruncated(...)100%210%
timeout(...)100%210%
isMulticast(...)100%8.19885.71%
isIPv6Supported()100%1.04166.67%
createSocket(...)100%5.34456.25%
createServerSocket(...)100%4.59466.67%
closeSocketNoThrow(...)50%2.31257.14%
closeSocket(...)50%2.31257.14%
setTcpNoDelay(...)100%1.13150%
setBlock(...)100%1.13150%
setSendBufferSize(...)100%1.13150%
getSendBufferSize(...)100%1.13150%
setRecvBufferSize(...)100%1.13150%
getRecvBufferSize(...)100%1.13150%
setReuseAddress(...)100%1.13150%
setMcastInterface(...)50%2.09271.43%
setMcastGroup(...)80%11.821073.68%
setMcastTtl(...)0%620%
doBind(...)100%11100%
doListen(...)0%2.98237.5%
getProtocolSupport(...)100%11100%
getAddressForServer(...)100%44100%
getAddresses(...)81.58%43.253884.62%
getLocalAddresses(...)100%16.931684.62%
setTcpBufSize(...)50%2.01285.71%
setTcpBufSize(...)37.5%53.321647.37%
getInterfacesForMulticast(...)66.67%66100%
fdToString(...)62.5%8.51880%
fdToString(...)50%2.15266.67%
addrToString(...)100%11100%
localAddrToString(...)100%22100%
remoteAddrToString(...)100%22100%
getLocalAddress(...)100%1.13150%
getRemoteAddress(...)100%1.22140%
getInterfaceAddress(...)14.29%219.412837.5%
getInterfaceIndex(...)37.5%117.52445.45%
getNumericAddress(...)50%4.01490.91%
isWildcard(...)25%10.75425%
getLoopbackAddresses(...)100%44100%
addressEquals(...)33.33%12.72642.86%
endpointAddressToString(...)100%44100%
endpointPort(...)50%4.13480%
.ctor(...)100%11100%
Compare(...)75%12.421285.71%
.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        string ip = addr.Address.ToString().ToUpperInvariant();
 185        if (addr.AddressFamily == AddressFamily.InterNetwork)
 86        {
 187            char[] splitChars = { '.' };
 188            string[] arr = ip.Split(splitChars);
 89            try
 90            {
 191                int i = int.Parse(arr[0], CultureInfo.InvariantCulture);
 192                if (i >= 223 && i <= 239)
 93                {
 194                    return true;
 95                }
 196            }
 097            catch (FormatException)
 98            {
 099                return false;
 100            }
 101        }
 102        else // AddressFamily.InterNetworkV6
 103        {
 1104            if (ip.StartsWith("FF", StringComparison.Ordinal))
 105            {
 1106                return true;
 107            }
 108        }
 1109        return false;
 1110    }
 111
 112    internal static bool isIPv6Supported()
 113    {
 114        try
 115        {
 1116            using var socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
 1117            closeSocketNoThrow(socket);
 1118            return true;
 119        }
 0120        catch (System.Net.Sockets.SocketException)
 121        {
 0122            return false;
 123        }
 1124    }
 125
 126    internal static Socket createSocket(bool udp, AddressFamily family)
 127    {
 128        Socket socket;
 129
 130        try
 131        {
 1132            if (udp)
 133            {
 1134                socket = new Socket(family, SocketType.Dgram, ProtocolType.Udp);
 135            }
 136            else
 137            {
 1138                socket = new Socket(family, SocketType.Stream, ProtocolType.Tcp);
 139            }
 1140        }
 0141        catch (System.Net.Sockets.SocketException ex)
 142        {
 0143            throw new Ice.SocketException(ex);
 144        }
 0145        catch (ArgumentException ex)
 146        {
 0147            throw new Ice.SocketException(ex);
 148        }
 149
 1150        if (!udp)
 151        {
 152            try
 153            {
 1154                setTcpNoDelay(socket);
 1155                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, 1);
 1156            }
 0157            catch (System.Net.Sockets.SocketException ex)
 158            {
 0159                closeSocketNoThrow(socket);
 0160                throw new Ice.SocketException(ex);
 161            }
 162        }
 1163        return socket;
 164    }
 165
 166    internal static Socket createServerSocket(bool udp, AddressFamily family, int protocol)
 167    {
 1168        Socket socket = createSocket(udp, family);
 1169        if (family == AddressFamily.InterNetworkV6 && protocol != EnableIPv4)
 170        {
 171            try
 172            {
 1173                int flag = protocol == EnableIPv6 ? 1 : 0;
 1174                socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, flag);
 1175            }
 0176            catch (System.Net.Sockets.SocketException ex)
 177            {
 0178                closeSocketNoThrow(socket);
 0179                throw new Ice.SocketException(ex);
 180            }
 181        }
 1182        return socket;
 183    }
 184
 185    internal static void closeSocketNoThrow(Socket socket)
 186    {
 1187        if (socket == null)
 188        {
 0189            return;
 190        }
 191        try
 192        {
 1193            socket.Close();
 1194        }
 0195        catch (System.Net.Sockets.SocketException)
 196        {
 197            // Ignore
 0198        }
 1199    }
 200
 201    internal static void closeSocket(Socket socket)
 202    {
 1203        if (socket == null)
 204        {
 0205            return;
 206        }
 207        try
 208        {
 1209            socket.Close();
 1210        }
 0211        catch (System.Net.Sockets.SocketException ex)
 212        {
 0213            throw new Ice.SocketException(ex);
 214        }
 1215    }
 216
 217    internal static void setTcpNoDelay(Socket socket)
 218    {
 219        try
 220        {
 1221            socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, 1);
 1222        }
 0223        catch (System.Exception ex)
 224        {
 0225            closeSocketNoThrow(socket);
 0226            throw new Ice.SocketException(ex);
 227        }
 1228    }
 229
 230    internal static void setBlock(Socket socket, bool block)
 231    {
 232        try
 233        {
 1234            socket.Blocking = block;
 1235        }
 0236        catch (System.Net.Sockets.SocketException ex)
 237        {
 0238            closeSocketNoThrow(socket);
 0239            throw new Ice.SocketException(ex);
 240        }
 1241    }
 242
 243    internal static void setSendBufferSize(Socket socket, int sz)
 244    {
 245        try
 246        {
 1247            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, sz);
 1248        }
 0249        catch (System.Net.Sockets.SocketException ex)
 250        {
 0251            closeSocketNoThrow(socket);
 0252            throw new Ice.SocketException(ex);
 253        }
 1254    }
 255
 256    internal static int getSendBufferSize(Socket socket)
 257    {
 258        int sz;
 259        try
 260        {
 1261            sz = (int)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer);
 1262        }
 0263        catch (System.Net.Sockets.SocketException ex)
 264        {
 0265            closeSocketNoThrow(socket);
 0266            throw new Ice.SocketException(ex);
 267        }
 1268        return sz;
 269    }
 270
 271    internal static void setRecvBufferSize(Socket socket, int sz)
 272    {
 273        try
 274        {
 1275            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, sz);
 1276        }
 0277        catch (System.Net.Sockets.SocketException ex)
 278        {
 0279            closeSocketNoThrow(socket);
 0280            throw new Ice.SocketException(ex);
 281        }
 1282    }
 283
 284    internal static int getRecvBufferSize(Socket socket)
 285    {
 286        int sz;
 287        try
 288        {
 1289            sz = (int)socket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer);
 1290        }
 0291        catch (System.Net.Sockets.SocketException ex)
 292        {
 0293            closeSocketNoThrow(socket);
 0294            throw new Ice.SocketException(ex);
 295        }
 1296        return sz;
 297    }
 298
 299    internal static void setReuseAddress(Socket socket, bool reuse)
 300    {
 301        try
 302        {
 1303            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, reuse ? 1 : 0);
 1304        }
 0305        catch (System.Net.Sockets.SocketException ex)
 306        {
 0307            closeSocketNoThrow(socket);
 0308            throw new Ice.SocketException(ex);
 309        }
 1310    }
 311
 312    internal static void setMcastInterface(Socket socket, string iface, AddressFamily family)
 313    {
 314        try
 315        {
 1316            if (family == AddressFamily.InterNetwork)
 317            {
 1318                socket.SetSocketOption(
 1319                    SocketOptionLevel.IP,
 1320                    SocketOptionName.MulticastInterface,
 1321                    getInterfaceAddress(iface, family).GetAddressBytes());
 322            }
 323            else
 324            {
 0325                socket.SetSocketOption(
 0326                    SocketOptionLevel.IPv6,
 0327                    SocketOptionName.MulticastInterface,
 0328                    getInterfaceIndex(iface, family));
 329            }
 1330        }
 1331        catch (System.Exception ex)
 332        {
 1333            closeSocketNoThrow(socket);
 1334            throw new Ice.SocketException(ex);
 335        }
 1336    }
 337
 338    internal static void setMcastGroup(Socket s, IPAddress group, string iface)
 339    {
 340        try
 341        {
 1342            var indexes = new HashSet<int>();
 1343            foreach (string intf in getInterfacesForMulticast(iface, getProtocolSupport(group)))
 344            {
 1345                if (group.AddressFamily == AddressFamily.InterNetwork)
 346                {
 347                    MulticastOption option;
 1348                    IPAddress addr = getInterfaceAddress(intf, group.AddressFamily);
 1349                    if (addr == null)
 350                    {
 0351                        option = new MulticastOption(group);
 352                    }
 353                    else
 354                    {
 1355                        option = new MulticastOption(group, addr);
 356                    }
 1357                    s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, option);
 358                }
 359                else
 360                {
 1361                    int index = getInterfaceIndex(intf, group.AddressFamily);
 1362                    if (indexes.Add(index))
 363                    {
 364                        IPv6MulticastOption option;
 1365                        if (index == -1)
 366                        {
 0367                            option = new IPv6MulticastOption(group);
 368                        }
 369                        else
 370                        {
 1371                            option = new IPv6MulticastOption(group, index);
 372                        }
 1373                        s.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership, option);
 374                    }
 375                }
 376            }
 1377        }
 0378        catch (System.Exception ex)
 379        {
 0380            closeSocketNoThrow(s);
 0381            throw new Ice.SocketException(ex);
 382        }
 1383    }
 384
 385    internal static void setMcastTtl(Socket socket, int ttl, AddressFamily family)
 386    {
 387        try
 388        {
 0389            if (family == AddressFamily.InterNetwork)
 390            {
 0391                socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, ttl);
 392            }
 393            else
 394            {
 0395                socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.MulticastTimeToLive, ttl);
 396            }
 0397        }
 0398        catch (System.Net.Sockets.SocketException ex)
 399        {
 0400            closeSocketNoThrow(socket);
 0401            throw new Ice.SocketException(ex);
 402        }
 0403    }
 404
 405    internal static IPEndPoint doBind(Socket socket, EndPoint addr)
 406    {
 407        try
 408        {
 1409            socket.Bind(addr);
 1410            return (IPEndPoint)socket.LocalEndPoint;
 411        }
 1412        catch (System.Net.Sockets.SocketException ex)
 413        {
 1414            closeSocketNoThrow(socket);
 1415            throw new Ice.SocketException(ex);
 416        }
 1417    }
 418
 419    internal static void doListen(Socket socket, int backlog)
 420    {
 421    repeatListen:
 422
 423        try
 424        {
 1425            socket.Listen(backlog);
 1426        }
 0427        catch (System.Net.Sockets.SocketException ex)
 428        {
 0429            if (interrupted(ex))
 430            {
 0431                goto repeatListen;
 432            }
 433
 0434            closeSocketNoThrow(socket);
 0435            throw new Ice.SocketException(ex);
 436        }
 1437    }
 438
 439    internal static int getProtocolSupport(IPAddress addr) =>
 1440        addr.AddressFamily == AddressFamily.InterNetwork ? EnableIPv4 : EnableIPv6;
 441
 442    internal static EndPoint getAddressForServer(string host, int port, int protocol, bool preferIPv6)
 443    {
 1444        if (host.Length == 0)
 445        {
 1446            if (protocol != EnableIPv4)
 447            {
 1448                return new IPEndPoint(IPAddress.IPv6Any, port);
 449            }
 450            else
 451            {
 1452                return new IPEndPoint(IPAddress.Any, port);
 453            }
 454        }
 1455        return getAddresses(host, port, protocol, preferIPv6, true)[0];
 456    }
 457
 458    internal static List<EndPoint> getAddresses(
 459        string host,
 460        int port,
 461        int protocol,
 462        bool preferIPv6,
 463        bool blocking)
 464    {
 1465        var addresses = new List<EndPoint>();
 1466        if (host.Length == 0)
 467        {
 1468            foreach (IPAddress a in getLoopbackAddresses(protocol))
 469            {
 1470                addresses.Add(new IPEndPoint(a, port));
 471            }
 1472            if (protocol == EnableBoth)
 473            {
 1474                if (preferIPv6)
 475                {
 0476                    Ice.UtilInternal.Collections.Sort(ref addresses, _preferIPv6Comparator);
 477                }
 478                else
 479                {
 1480                    Ice.UtilInternal.Collections.Sort(ref addresses, _preferIPv4Comparator);
 481                }
 482            }
 1483            return addresses;
 484        }
 485
 1486        int retry = 5;
 487
 488    repeatGetHostByName:
 489        try
 490        {
 491            //
 492            // No need for lookup if host is ip address.
 493            //
 494            try
 495            {
 1496                var addr = IPAddress.Parse(host);
 1497                if ((addr.AddressFamily == AddressFamily.InterNetwork && protocol != EnableIPv6) ||
 1498                   (addr.AddressFamily == AddressFamily.InterNetworkV6 && protocol != EnableIPv4))
 499                {
 1500                    addresses.Add(new IPEndPoint(addr, port));
 1501                    return addresses;
 502                }
 503                else
 504                {
 0505                    throw new DNSException(host);
 506                }
 507            }
 1508            catch (FormatException)
 509            {
 1510                if (!blocking)
 511                {
 1512                    return addresses;
 513                }
 1514            }
 515
 1516            foreach (IPAddress a in Dns.GetHostAddresses(host))
 517            {
 1518                if ((a.AddressFamily == AddressFamily.InterNetwork && protocol != EnableIPv6) ||
 1519                   (a.AddressFamily == AddressFamily.InterNetworkV6 && protocol != EnableIPv4))
 520                {
 1521                    addresses.Add(new IPEndPoint(a, port));
 522                }
 523            }
 524
 1525            if (protocol == EnableBoth)
 526            {
 1527                if (preferIPv6)
 528                {
 1529                    Ice.UtilInternal.Collections.Sort(ref addresses, _preferIPv6Comparator);
 530                }
 531                else
 532                {
 1533                    Ice.UtilInternal.Collections.Sort(ref addresses, _preferIPv4Comparator);
 534                }
 535            }
 1536        }
 1537        catch (System.Net.Sockets.SocketException ex)
 538        {
 1539            if (socketErrorCode(ex) == SocketError.TryAgain && --retry >= 0)
 540            {
 0541                goto repeatGetHostByName;
 542            }
 1543            throw new DNSException(host, ex);
 544        }
 0545        catch (System.Exception ex)
 546        {
 0547            throw new DNSException(host, ex);
 548        }
 549
 550        //
 551        // No InterNetwork/InterNetworkV6 available.
 552        //
 1553        if (addresses.Count == 0)
 554        {
 0555            throw new DNSException(host);
 556        }
 1557        return addresses;
 1558    }
 559
 560    // Only used for multicast.
 561    private static IEnumerable<IPAddress> getLocalAddresses(int protocol)
 562    {
 563        try
 564        {
 1565            var result = new List<IPAddress>();
 1566            foreach (NetworkInterface p in NetworkInterface.GetAllNetworkInterfaces())
 567            {
 1568                if (p.SupportsMulticast && p.OperationalStatus == OperationalStatus.Up)
 569                {
 1570                    IPInterfaceProperties ipProps = p.GetIPProperties();
 1571                    foreach (UnicastIPAddressInformation uni in ipProps.UnicastAddresses)
 572                    {
 1573                        if ((uni.Address.AddressFamily == AddressFamily.InterNetwork && protocol != EnableIPv6) ||
 1574                            (uni.Address.AddressFamily == AddressFamily.InterNetworkV6 && protocol != EnableIPv4))
 575                        {
 1576                            result.Add(uni.Address);
 1577                            break; // We return at most one address per interface.
 578                        }
 579                    }
 580                }
 581            }
 1582            return result;
 583        }
 0584        catch (System.Exception ex)
 585        {
 0586            throw new SyscallException("Failed to enumerate the local network interfaces.", ex);
 587        }
 1588    }
 589
 590    internal static void setTcpBufSize(Socket socket, ProtocolInstance instance)
 591    {
 592        //
 593        // By default, on Windows we use a 128KB buffer size. On Unix
 594        // platforms, we use the system defaults.
 595        //
 1596        int dfltBufSize = 0;
 1597        if (AssemblyUtil.isWindows)
 598        {
 0599            dfltBufSize = 128 * 1024;
 600        }
 1601        int rcvSize = instance.properties().getPropertyAsIntWithDefault("Ice.TCP.RcvSize", dfltBufSize);
 1602        int sndSize = instance.properties().getPropertyAsIntWithDefault("Ice.TCP.SndSize", dfltBufSize);
 1603        setTcpBufSize(socket, rcvSize, sndSize, instance);
 1604    }
 605
 606    internal static void setTcpBufSize(Socket socket, int rcvSize, int sndSize, ProtocolInstance instance)
 607    {
 1608        if (rcvSize > 0)
 609        {
 610            //
 611            // Try to set the buffer size. The kernel will silently adjust
 612            // the size to an acceptable value. Then read the size back to
 613            // get the size that was actually set.
 614            //
 1615            setRecvBufferSize(socket, rcvSize);
 1616            int size = getRecvBufferSize(socket);
 1617            if (size < rcvSize)
 618            {
 619                // Warn if the size that was set is less than the requested size and
 620                // we have not already warned.
 0621                BufSizeWarnInfo winfo = instance.getBufSizeWarn(Ice.TCPEndpointType.value);
 0622                if (!winfo.rcvWarn || rcvSize != winfo.rcvSize)
 623                {
 0624                    instance.logger().warning("TCP receive buffer size: requested size of " + rcvSize +
 0625                                              " adjusted to " + size);
 0626                    instance.setRcvBufSizeWarn(Ice.TCPEndpointType.value, rcvSize);
 627                }
 628            }
 629        }
 630
 1631        if (sndSize > 0)
 632        {
 633            //
 634            // Try to set the buffer size. The kernel will silently adjust
 635            // the size to an acceptable value. Then read the size back to
 636            // get the size that was actually set.
 637            //
 1638            setSendBufferSize(socket, sndSize);
 1639            int size = getSendBufferSize(socket);
 1640            if (size < sndSize) // Warn if the size that was set is less than the requested size.
 641            {
 642                // Warn if the size that was set is less than the requested size and
 643                // we have not already warned.
 0644                BufSizeWarnInfo winfo = instance.getBufSizeWarn(Ice.TCPEndpointType.value);
 0645                if (!winfo.sndWarn || sndSize != winfo.sndSize)
 646                {
 0647                    instance.logger().warning("TCP send buffer size: requested size of " + sndSize +
 0648                                              " adjusted to " + size);
 0649                    instance.setSndBufSizeWarn(Ice.TCPEndpointType.value, sndSize);
 650                }
 651            }
 652        }
 1653    }
 654
 655    internal static List<string> getInterfacesForMulticast(string intf, int protocol) =>
 1656        isWildcard(intf, out bool ipv4Wildcard)
 1657            ? getLocalAddresses(ipv4Wildcard ? EnableIPv4 : protocol)
 1658                .Select(addr => addr.ToString())
 1659                .Distinct()
 1660                .ToList()
 1661            : [intf];
 662
 663    internal static string fdToString(Socket socket, NetworkProxy proxy, EndPoint target)
 664    {
 665        try
 666        {
 1667            if (socket == null)
 668            {
 0669                return "<closed>";
 670            }
 671
 1672            EndPoint remote = getRemoteAddress(socket);
 673
 1674            var s = new System.Text.StringBuilder();
 1675            s.Append("local address = " + localAddrToString(getLocalAddress(socket)));
 1676            if (proxy != null)
 677            {
 1678                remote ??= proxy.getAddress();
 1679                s.Append("\n" + proxy.getName() + " proxy address = " + remoteAddrToString(remote));
 1680                s.Append("\nremote address = " + remoteAddrToString(target));
 681            }
 682            else
 683            {
 1684                remote ??= target;
 1685                s.Append("\nremote address = " + remoteAddrToString(remote));
 686            }
 1687            return s.ToString();
 688        }
 0689        catch (ObjectDisposedException)
 690        {
 0691            return "<closed>";
 692        }
 1693    }
 694
 695    internal static string fdToString(Socket socket)
 696    {
 697        try
 698        {
 1699            if (socket == null)
 700            {
 0701                return "<closed>";
 702            }
 1703            var s = new System.Text.StringBuilder();
 1704            s.Append("local address = " + localAddrToString(getLocalAddress(socket)));
 1705            s.Append("\nremote address = " + remoteAddrToString(getRemoteAddress(socket)));
 1706            return s.ToString();
 707        }
 0708        catch (ObjectDisposedException)
 709        {
 0710            return "<closed>";
 711        }
 1712    }
 713
 714    internal static string
 1715    addrToString(EndPoint addr) => endpointAddressToString(addr) + ":" + endpointPort(addr);
 716
 717    internal static string localAddrToString(EndPoint endpoint)
 718    {
 1719        if (endpoint == null)
 720        {
 1721            return "<not bound>";
 722        }
 1723        return endpointAddressToString(endpoint) + ":" + endpointPort(endpoint);
 724    }
 725
 726    internal static string remoteAddrToString(EndPoint endpoint)
 727    {
 1728        if (endpoint == null)
 729        {
 1730            return "<not connected>";
 731        }
 1732        return endpointAddressToString(endpoint) + ":" + endpointPort(endpoint);
 733    }
 734
 735    internal static EndPoint getLocalAddress(Socket socket)
 736    {
 737        try
 738        {
 1739            return socket.LocalEndPoint;
 740        }
 0741        catch (System.Net.Sockets.SocketException ex)
 742        {
 0743            throw new Ice.SocketException(ex);
 744        }
 1745    }
 746
 747    internal static EndPoint getRemoteAddress(Socket socket)
 748    {
 749        try
 750        {
 1751            return socket.RemoteEndPoint;
 752        }
 0753        catch (System.Net.Sockets.SocketException)
 754        {
 0755        }
 0756        return null;
 1757    }
 758
 759    private static IPAddress getInterfaceAddress(string iface, AddressFamily family)
 760    {
 1761        if (iface.Length == 0)
 762        {
 0763            return null;
 764        }
 765
 766        //
 767        // The iface parameter must either be an IP address, an
 768        // index or the name of an interface. If it's an index we
 769        // just return it. If it's an IP address we search for an
 770        // interface which has this IP address. If it's a name we
 771        // search an interface with this name.
 772        //
 773
 774        try
 775        {
 1776            return IPAddress.Parse(iface);
 777        }
 1778        catch (FormatException)
 779        {
 1780        }
 781
 1782        NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
 783        try
 784        {
 1785            int index = int.Parse(iface, CultureInfo.InvariantCulture);
 0786            foreach (NetworkInterface ni in nics)
 787            {
 0788                IPInterfaceProperties ipProps = ni.GetIPProperties();
 0789                int interfaceIndex = -1;
 0790                if (family == AddressFamily.InterNetwork)
 791                {
 0792                    IPv4InterfaceProperties ipv4Props = ipProps.GetIPv4Properties();
 0793                    if (ipv4Props != null && ipv4Props.Index == index)
 794                    {
 0795                        interfaceIndex = ipv4Props.Index;
 796                    }
 797                }
 798                else
 799                {
 0800                    IPv6InterfaceProperties ipv6Props = ipProps.GetIPv6Properties();
 0801                    if (ipv6Props != null && ipv6Props.Index == index)
 802                    {
 0803                        interfaceIndex = ipv6Props.Index;
 804                    }
 805                }
 0806                if (interfaceIndex >= 0)
 807                {
 0808                    foreach (UnicastIPAddressInformation a in ipProps.UnicastAddresses)
 809                    {
 0810                        if (a.Address.AddressFamily == family)
 811                        {
 0812                            return a.Address;
 813                        }
 814                    }
 815                }
 816            }
 0817        }
 1818        catch (FormatException)
 819        {
 1820        }
 821
 1822        foreach (NetworkInterface ni in nics)
 823        {
 1824            if (ni.Name == iface)
 825            {
 0826                IPInterfaceProperties ipProps = ni.GetIPProperties();
 0827                foreach (UnicastIPAddressInformation a in ipProps.UnicastAddresses)
 828                {
 0829                    if (a.Address.AddressFamily == family)
 830                    {
 0831                        return a.Address;
 832                    }
 833                }
 834            }
 835        }
 836
 1837        throw new ArgumentException("couldn't find interface `" + iface + "'");
 1838    }
 839
 840    private static int getInterfaceIndex(string iface, AddressFamily family)
 841    {
 1842        if (iface.Length == 0)
 843        {
 0844            return -1;
 845        }
 846
 847        //
 848        // The iface parameter must either be an IP address, an
 849        // index or the name of an interface. If it's an index we
 850        // just return it. If it's an IP address we search for an
 851        // interface which has this IP address. If it's a name we
 852        // search an interface with this name.
 853        //
 854        try
 855        {
 1856            return int.Parse(iface, CultureInfo.InvariantCulture);
 857        }
 1858        catch (FormatException)
 859        {
 1860        }
 861
 1862        NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
 863        try
 864        {
 1865            var addr = IPAddress.Parse(iface);
 1866            foreach (NetworkInterface ni in nics)
 867            {
 1868                IPInterfaceProperties ipProps = ni.GetIPProperties();
 1869                foreach (UnicastIPAddressInformation uni in ipProps.UnicastAddresses)
 870                {
 1871                    if (uni.Address.Equals(addr))
 872                    {
 1873                        if (addr.AddressFamily == AddressFamily.InterNetwork)
 874                        {
 0875                            IPv4InterfaceProperties ipv4Props = ipProps.GetIPv4Properties();
 0876                            if (ipv4Props != null)
 877                            {
 0878                                return ipv4Props.Index;
 879                            }
 880                        }
 881                        else
 882                        {
 1883                            IPv6InterfaceProperties ipv6Props = ipProps.GetIPv6Properties();
 1884                            if (ipv6Props != null)
 885                            {
 1886                                return ipv6Props.Index;
 887                            }
 888                        }
 889                    }
 890                }
 891            }
 0892        }
 0893        catch (FormatException)
 894        {
 0895        }
 896
 0897        foreach (NetworkInterface ni in nics)
 898        {
 0899            if (ni.Name == iface)
 900            {
 0901                IPInterfaceProperties ipProps = ni.GetIPProperties();
 0902                if (family == AddressFamily.InterNetwork)
 903                {
 0904                    IPv4InterfaceProperties ipv4Props = ipProps.GetIPv4Properties();
 0905                    if (ipv4Props != null)
 906                    {
 0907                        return ipv4Props.Index;
 908                    }
 909                }
 910                else
 911                {
 0912                    IPv6InterfaceProperties ipv6Props = ipProps.GetIPv6Properties();
 0913                    if (ipv6Props != null)
 914                    {
 0915                        return ipv6Props.Index;
 916                    }
 917                }
 918            }
 919        }
 920
 0921        throw new ArgumentException("couldn't find interface `" + iface + "'");
 1922    }
 923
 924    internal static EndPoint getNumericAddress(string sourceAddress)
 925    {
 1926        EndPoint addr = null;
 1927        if (!string.IsNullOrEmpty(sourceAddress))
 928        {
 1929            List<EndPoint> addrs = getAddresses(
 1930                sourceAddress,
 1931                0,
 1932                EnableBoth,
 1933                preferIPv6: false,
 1934                blocking: false);
 1935            if (addrs.Count != 0)
 936            {
 1937                return addrs[0];
 938            }
 939        }
 0940        return addr;
 941    }
 942
 943    private static bool isWildcard(string address, out bool ipv4Wildcard)
 944    {
 1945        ipv4Wildcard = false;
 1946        if (address.Length == 0)
 947        {
 1948            return true;
 949        }
 950
 951        try
 952        {
 0953            var addr = IPAddress.Parse(address);
 0954            if (addr.Equals(IPAddress.Any))
 955            {
 0956                ipv4Wildcard = true;
 0957                return true;
 958            }
 0959            return addr.Equals(IPAddress.IPv6Any);
 960        }
 0961        catch (System.Exception)
 962        {
 0963        }
 964
 0965        return false;
 0966    }
 967
 968    internal static List<IPAddress> getLoopbackAddresses(int protocol)
 969    {
 1970        var addresses = new List<IPAddress>();
 1971        if (protocol != EnableIPv4)
 972        {
 1973            addresses.Add(IPAddress.IPv6Loopback);
 974        }
 1975        if (protocol != EnableIPv6)
 976        {
 1977            addresses.Add(IPAddress.Loopback);
 978        }
 1979        return addresses;
 980    }
 981
 982    internal static bool addressEquals(EndPoint addr1, EndPoint addr2)
 983    {
 1984        if (addr1 == null)
 985        {
 1986            if (addr2 == null)
 987            {
 1988                return true;
 989            }
 990            else
 991            {
 0992                return false;
 993            }
 994        }
 0995        else if (addr2 == null)
 996        {
 0997            return false;
 998        }
 999
 01000        return addr1.Equals(addr2);
 1001    }
 1002
 1003    internal static string endpointAddressToString(EndPoint endpoint)
 1004    {
 11005        if (endpoint != null)
 1006        {
 11007            if (endpoint is IPEndPoint)
 1008            {
 11009                var ipEndpoint = (IPEndPoint)endpoint;
 11010                return ipEndpoint.Address.ToString();
 1011            }
 1012        }
 11013        return "";
 1014    }
 1015
 1016    internal static int endpointPort(EndPoint endpoint)
 1017    {
 11018        if (endpoint != null)
 1019        {
 11020            if (endpoint is IPEndPoint)
 1021            {
 11022                var ipEndpoint = (IPEndPoint)endpoint;
 11023                return ipEndpoint.Port;
 1024            }
 1025        }
 01026        return -1;
 1027    }
 1028
 1029    private class EndPointComparator : IComparer<EndPoint>
 1030    {
 11031        public EndPointComparator(bool ipv6) => _ipv6 = ipv6;
 1032
 1033        public int Compare(EndPoint lhs, EndPoint rhs)
 1034        {
 11035            if (lhs.AddressFamily == AddressFamily.InterNetwork &&
 11036               rhs.AddressFamily == AddressFamily.InterNetworkV6)
 1037            {
 01038                return _ipv6 ? 1 : -1;
 1039            }
 11040            else if (lhs.AddressFamily == AddressFamily.InterNetworkV6 &&
 11041                    rhs.AddressFamily == AddressFamily.InterNetwork)
 1042            {
 11043                return _ipv6 ? -1 : 1;
 1044            }
 1045            else
 1046            {
 11047                return 0;
 1048            }
 1049        }
 1050
 1051        private readonly bool _ipv6;
 1052    }
 1053
 11054    private static readonly EndPointComparator _preferIPv4Comparator = new EndPointComparator(false);
 11055    private static readonly EndPointComparator _preferIPv6Comparator = new EndPointComparator(true);
 1056}

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)
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, Ice.Internal.ProtocolInstance)
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)
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()