< Summary

Information
Class: Ice.Internal.Network
Assembly: Ice
File(s): /home/runner/work/ice/ice/csharp/src/Ice/Internal/Network.cs
Tag: 71_18251537082
Line coverage
64%
Covered lines: 283
Uncovered lines: 155
Coverable lines: 438
Total lines: 1082
Line coverage: 64.6%
Branch coverage
60%
Covered branches: 148
Total branches: 244
Branch coverage: 60.6%
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(...)80%30.82070%
setTcpBufSize(...)50%2.01285.71%
setTcpBufSize(...)37.5%53.321647.37%
getInterfacesForMulticast(...)75%8.19885.71%
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)

/home/runner/work/ice/ice/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    internal static IPAddress[] getLocalAddresses(int protocol)
 562    {
 563        List<IPAddress> addresses;
 1564        int retry = 5;
 565
 566    repeatGetHostByName:
 567        try
 568        {
 1569            addresses = new List<IPAddress>();
 1570            IEnumerable<NetworkInterface> nics = NetworkInterface.GetAllNetworkInterfaces().Where(
 1571                nic => nic.SupportsMulticast && nic.OperationalStatus == OperationalStatus.Up);
 572
 1573            foreach (NetworkInterface ni in nics)
 574            {
 1575                IPInterfaceProperties ipProps = ni.GetIPProperties();
 1576                UnicastIPAddressInformationCollection uniColl = ipProps.UnicastAddresses;
 1577                foreach (UnicastIPAddressInformation uni in uniColl)
 578                {
 1579                    if ((uni.Address.AddressFamily == AddressFamily.InterNetwork && protocol != EnableIPv6) ||
 1580                       (uni.Address.AddressFamily == AddressFamily.InterNetworkV6 && protocol != EnableIPv4))
 581                    {
 1582                        if (!addresses.Contains(uni.Address))
 583                        {
 1584                            addresses.Add(uni.Address);
 1585                            break; // need only one address per interface
 586                        }
 587                    }
 588                }
 589            }
 1590        }
 0591        catch (System.Net.Sockets.SocketException ex)
 592        {
 0593            if (socketErrorCode(ex) == SocketError.TryAgain && --retry >= 0)
 594            {
 0595                goto repeatGetHostByName;
 596            }
 0597            throw new DNSException("0.0.0.0", ex);
 598        }
 0599        catch (System.Exception ex)
 600        {
 0601            throw new DNSException("0.0.0.0", ex);
 602        }
 603
 1604        return addresses.ToArray();
 605    }
 606
 607    internal static void setTcpBufSize(Socket socket, ProtocolInstance instance)
 608    {
 609        //
 610        // By default, on Windows we use a 128KB buffer size. On Unix
 611        // platforms, we use the system defaults.
 612        //
 1613        int dfltBufSize = 0;
 1614        if (AssemblyUtil.isWindows)
 615        {
 0616            dfltBufSize = 128 * 1024;
 617        }
 1618        int rcvSize = instance.properties().getPropertyAsIntWithDefault("Ice.TCP.RcvSize", dfltBufSize);
 1619        int sndSize = instance.properties().getPropertyAsIntWithDefault("Ice.TCP.SndSize", dfltBufSize);
 1620        setTcpBufSize(socket, rcvSize, sndSize, instance);
 1621    }
 622
 623    internal static void setTcpBufSize(Socket socket, int rcvSize, int sndSize, ProtocolInstance instance)
 624    {
 1625        if (rcvSize > 0)
 626        {
 627            //
 628            // Try to set the buffer size. The kernel will silently adjust
 629            // the size to an acceptable value. Then read the size back to
 630            // get the size that was actually set.
 631            //
 1632            setRecvBufferSize(socket, rcvSize);
 1633            int size = getRecvBufferSize(socket);
 1634            if (size < rcvSize)
 635            {
 636                // Warn if the size that was set is less than the requested size and
 637                // we have not already warned.
 0638                BufSizeWarnInfo winfo = instance.getBufSizeWarn(Ice.TCPEndpointType.value);
 0639                if (!winfo.rcvWarn || rcvSize != winfo.rcvSize)
 640                {
 0641                    instance.logger().warning("TCP receive buffer size: requested size of " + rcvSize +
 0642                                              " adjusted to " + size);
 0643                    instance.setRcvBufSizeWarn(Ice.TCPEndpointType.value, rcvSize);
 644                }
 645            }
 646        }
 647
 1648        if (sndSize > 0)
 649        {
 650            //
 651            // Try to set the buffer size. The kernel will silently adjust
 652            // the size to an acceptable value. Then read the size back to
 653            // get the size that was actually set.
 654            //
 1655            setSendBufferSize(socket, sndSize);
 1656            int size = getSendBufferSize(socket);
 1657            if (size < sndSize) // Warn if the size that was set is less than the requested size.
 658            {
 659                // Warn if the size that was set is less than the requested size and
 660                // we have not already warned.
 0661                BufSizeWarnInfo winfo = instance.getBufSizeWarn(Ice.TCPEndpointType.value);
 0662                if (!winfo.sndWarn || sndSize != winfo.sndSize)
 663                {
 0664                    instance.logger().warning("TCP send buffer size: requested size of " + sndSize +
 0665                                              " adjusted to " + size);
 0666                    instance.setSndBufSizeWarn(Ice.TCPEndpointType.value, sndSize);
 667                }
 668            }
 669        }
 1670    }
 671
 672    internal static List<string> getInterfacesForMulticast(string intf, int protocol)
 673    {
 1674        var interfaces = new List<string>();
 1675        if (isWildcard(intf, out bool ipv4Wildcard))
 676        {
 1677            foreach (IPAddress a in getLocalAddresses(ipv4Wildcard ? EnableIPv4 : protocol))
 678            {
 1679                interfaces.Add(a.ToString());
 680            }
 681        }
 1682        if (interfaces.Count == 0)
 683        {
 0684            interfaces.Add(intf);
 685        }
 1686        return interfaces;
 687    }
 688
 689    internal static string fdToString(Socket socket, NetworkProxy proxy, EndPoint target)
 690    {
 691        try
 692        {
 1693            if (socket == null)
 694            {
 0695                return "<closed>";
 696            }
 697
 1698            EndPoint remote = getRemoteAddress(socket);
 699
 1700            var s = new System.Text.StringBuilder();
 1701            s.Append("local address = " + localAddrToString(getLocalAddress(socket)));
 1702            if (proxy != null)
 703            {
 1704                remote ??= proxy.getAddress();
 1705                s.Append("\n" + proxy.getName() + " proxy address = " + remoteAddrToString(remote));
 1706                s.Append("\nremote address = " + remoteAddrToString(target));
 707            }
 708            else
 709            {
 1710                remote ??= target;
 1711                s.Append("\nremote address = " + remoteAddrToString(remote));
 712            }
 1713            return s.ToString();
 714        }
 0715        catch (ObjectDisposedException)
 716        {
 0717            return "<closed>";
 718        }
 1719    }
 720
 721    internal static string fdToString(Socket socket)
 722    {
 723        try
 724        {
 1725            if (socket == null)
 726            {
 0727                return "<closed>";
 728            }
 1729            var s = new System.Text.StringBuilder();
 1730            s.Append("local address = " + localAddrToString(getLocalAddress(socket)));
 1731            s.Append("\nremote address = " + remoteAddrToString(getRemoteAddress(socket)));
 1732            return s.ToString();
 733        }
 0734        catch (ObjectDisposedException)
 735        {
 0736            return "<closed>";
 737        }
 1738    }
 739
 740    internal static string
 1741    addrToString(EndPoint addr) => endpointAddressToString(addr) + ":" + endpointPort(addr);
 742
 743    internal static string localAddrToString(EndPoint endpoint)
 744    {
 1745        if (endpoint == null)
 746        {
 1747            return "<not bound>";
 748        }
 1749        return endpointAddressToString(endpoint) + ":" + endpointPort(endpoint);
 750    }
 751
 752    internal static string remoteAddrToString(EndPoint endpoint)
 753    {
 1754        if (endpoint == null)
 755        {
 1756            return "<not connected>";
 757        }
 1758        return endpointAddressToString(endpoint) + ":" + endpointPort(endpoint);
 759    }
 760
 761    internal static EndPoint getLocalAddress(Socket socket)
 762    {
 763        try
 764        {
 1765            return socket.LocalEndPoint;
 766        }
 0767        catch (System.Net.Sockets.SocketException ex)
 768        {
 0769            throw new Ice.SocketException(ex);
 770        }
 1771    }
 772
 773    internal static EndPoint getRemoteAddress(Socket socket)
 774    {
 775        try
 776        {
 1777            return socket.RemoteEndPoint;
 778        }
 0779        catch (System.Net.Sockets.SocketException)
 780        {
 0781        }
 0782        return null;
 1783    }
 784
 785    private static IPAddress getInterfaceAddress(string iface, AddressFamily family)
 786    {
 1787        if (iface.Length == 0)
 788        {
 0789            return null;
 790        }
 791
 792        //
 793        // The iface parameter must either be an IP address, an
 794        // index or the name of an interface. If it's an index we
 795        // just return it. If it's an IP address we search for an
 796        // interface which has this IP address. If it's a name we
 797        // search an interface with this name.
 798        //
 799
 800        try
 801        {
 1802            return IPAddress.Parse(iface);
 803        }
 1804        catch (FormatException)
 805        {
 1806        }
 807
 1808        NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
 809        try
 810        {
 1811            int index = int.Parse(iface, CultureInfo.InvariantCulture);
 0812            foreach (NetworkInterface ni in nics)
 813            {
 0814                IPInterfaceProperties ipProps = ni.GetIPProperties();
 0815                int interfaceIndex = -1;
 0816                if (family == AddressFamily.InterNetwork)
 817                {
 0818                    IPv4InterfaceProperties ipv4Props = ipProps.GetIPv4Properties();
 0819                    if (ipv4Props != null && ipv4Props.Index == index)
 820                    {
 0821                        interfaceIndex = ipv4Props.Index;
 822                    }
 823                }
 824                else
 825                {
 0826                    IPv6InterfaceProperties ipv6Props = ipProps.GetIPv6Properties();
 0827                    if (ipv6Props != null && ipv6Props.Index == index)
 828                    {
 0829                        interfaceIndex = ipv6Props.Index;
 830                    }
 831                }
 0832                if (interfaceIndex >= 0)
 833                {
 0834                    foreach (UnicastIPAddressInformation a in ipProps.UnicastAddresses)
 835                    {
 0836                        if (a.Address.AddressFamily == family)
 837                        {
 0838                            return a.Address;
 839                        }
 840                    }
 841                }
 842            }
 0843        }
 1844        catch (FormatException)
 845        {
 1846        }
 847
 1848        foreach (NetworkInterface ni in nics)
 849        {
 1850            if (ni.Name == iface)
 851            {
 0852                IPInterfaceProperties ipProps = ni.GetIPProperties();
 0853                foreach (UnicastIPAddressInformation a in ipProps.UnicastAddresses)
 854                {
 0855                    if (a.Address.AddressFamily == family)
 856                    {
 0857                        return a.Address;
 858                    }
 859                }
 860            }
 861        }
 862
 1863        throw new ArgumentException("couldn't find interface `" + iface + "'");
 1864    }
 865
 866    private static int getInterfaceIndex(string iface, AddressFamily family)
 867    {
 1868        if (iface.Length == 0)
 869        {
 0870            return -1;
 871        }
 872
 873        //
 874        // The iface parameter must either be an IP address, an
 875        // index or the name of an interface. If it's an index we
 876        // just return it. If it's an IP address we search for an
 877        // interface which has this IP address. If it's a name we
 878        // search an interface with this name.
 879        //
 880        try
 881        {
 1882            return int.Parse(iface, CultureInfo.InvariantCulture);
 883        }
 1884        catch (FormatException)
 885        {
 1886        }
 887
 1888        NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
 889        try
 890        {
 1891            var addr = IPAddress.Parse(iface);
 1892            foreach (NetworkInterface ni in nics)
 893            {
 1894                IPInterfaceProperties ipProps = ni.GetIPProperties();
 1895                foreach (UnicastIPAddressInformation uni in ipProps.UnicastAddresses)
 896                {
 1897                    if (uni.Address.Equals(addr))
 898                    {
 1899                        if (addr.AddressFamily == AddressFamily.InterNetwork)
 900                        {
 0901                            IPv4InterfaceProperties ipv4Props = ipProps.GetIPv4Properties();
 0902                            if (ipv4Props != null)
 903                            {
 0904                                return ipv4Props.Index;
 905                            }
 906                        }
 907                        else
 908                        {
 1909                            IPv6InterfaceProperties ipv6Props = ipProps.GetIPv6Properties();
 1910                            if (ipv6Props != null)
 911                            {
 1912                                return ipv6Props.Index;
 913                            }
 914                        }
 915                    }
 916                }
 917            }
 0918        }
 0919        catch (FormatException)
 920        {
 0921        }
 922
 0923        foreach (NetworkInterface ni in nics)
 924        {
 0925            if (ni.Name == iface)
 926            {
 0927                IPInterfaceProperties ipProps = ni.GetIPProperties();
 0928                if (family == AddressFamily.InterNetwork)
 929                {
 0930                    IPv4InterfaceProperties ipv4Props = ipProps.GetIPv4Properties();
 0931                    if (ipv4Props != null)
 932                    {
 0933                        return ipv4Props.Index;
 934                    }
 935                }
 936                else
 937                {
 0938                    IPv6InterfaceProperties ipv6Props = ipProps.GetIPv6Properties();
 0939                    if (ipv6Props != null)
 940                    {
 0941                        return ipv6Props.Index;
 942                    }
 943                }
 944            }
 945        }
 946
 0947        throw new ArgumentException("couldn't find interface `" + iface + "'");
 1948    }
 949
 950    internal static EndPoint getNumericAddress(string sourceAddress)
 951    {
 1952        EndPoint addr = null;
 1953        if (!string.IsNullOrEmpty(sourceAddress))
 954        {
 1955            List<EndPoint> addrs = getAddresses(
 1956                sourceAddress,
 1957                0,
 1958                EnableBoth,
 1959                preferIPv6: false,
 1960                blocking: false);
 1961            if (addrs.Count != 0)
 962            {
 1963                return addrs[0];
 964            }
 965        }
 0966        return addr;
 967    }
 968
 969    private static bool isWildcard(string address, out bool ipv4Wildcard)
 970    {
 1971        ipv4Wildcard = false;
 1972        if (address.Length == 0)
 973        {
 1974            return true;
 975        }
 976
 977        try
 978        {
 0979            var addr = IPAddress.Parse(address);
 0980            if (addr.Equals(IPAddress.Any))
 981            {
 0982                ipv4Wildcard = true;
 0983                return true;
 984            }
 0985            return addr.Equals(IPAddress.IPv6Any);
 986        }
 0987        catch (System.Exception)
 988        {
 0989        }
 990
 0991        return false;
 0992    }
 993
 994    internal static List<IPAddress> getLoopbackAddresses(int protocol)
 995    {
 1996        var addresses = new List<IPAddress>();
 1997        if (protocol != EnableIPv4)
 998        {
 1999            addresses.Add(IPAddress.IPv6Loopback);
 1000        }
 11001        if (protocol != EnableIPv6)
 1002        {
 11003            addresses.Add(IPAddress.Loopback);
 1004        }
 11005        return addresses;
 1006    }
 1007
 1008    internal static bool addressEquals(EndPoint addr1, EndPoint addr2)
 1009    {
 11010        if (addr1 == null)
 1011        {
 11012            if (addr2 == null)
 1013            {
 11014                return true;
 1015            }
 1016            else
 1017            {
 01018                return false;
 1019            }
 1020        }
 01021        else if (addr2 == null)
 1022        {
 01023            return false;
 1024        }
 1025
 01026        return addr1.Equals(addr2);
 1027    }
 1028
 1029    internal static string endpointAddressToString(EndPoint endpoint)
 1030    {
 11031        if (endpoint != null)
 1032        {
 11033            if (endpoint is IPEndPoint)
 1034            {
 11035                var ipEndpoint = (IPEndPoint)endpoint;
 11036                return ipEndpoint.Address.ToString();
 1037            }
 1038        }
 11039        return "";
 1040    }
 1041
 1042    internal static int endpointPort(EndPoint endpoint)
 1043    {
 11044        if (endpoint != null)
 1045        {
 11046            if (endpoint is IPEndPoint)
 1047            {
 11048                var ipEndpoint = (IPEndPoint)endpoint;
 11049                return ipEndpoint.Port;
 1050            }
 1051        }
 01052        return -1;
 1053    }
 1054
 1055    private class EndPointComparator : IComparer<EndPoint>
 1056    {
 11057        public EndPointComparator(bool ipv6) => _ipv6 = ipv6;
 1058
 1059        public int Compare(EndPoint lhs, EndPoint rhs)
 1060        {
 11061            if (lhs.AddressFamily == AddressFamily.InterNetwork &&
 11062               rhs.AddressFamily == AddressFamily.InterNetworkV6)
 1063            {
 01064                return _ipv6 ? 1 : -1;
 1065            }
 11066            else if (lhs.AddressFamily == AddressFamily.InterNetworkV6 &&
 11067                    rhs.AddressFamily == AddressFamily.InterNetwork)
 1068            {
 11069                return _ipv6 ? -1 : 1;
 1070            }
 1071            else
 1072            {
 11073                return 0;
 1074            }
 1075        }
 1076
 1077        private readonly bool _ipv6;
 1078    }
 1079
 11080    private static readonly EndPointComparator _preferIPv4Comparator = new EndPointComparator(false);
 11081    private static readonly EndPointComparator _preferIPv6Comparator = new EndPointComparator(true);
 1082}

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()