コード例 #1
0
ファイル: Network.cs プロジェクト: fengliangwang/ice
        public static IPAddress[] getLocalAddresses(int protocol, bool includeLoopback)
        {
            List <IPAddress> addresses;
            int retry = 5;

repeatGetHostByName:
            try
            {
                addresses = new List <IPAddress>();
                NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface ni in nics)
                {
                    IPInterfaceProperties ipProps = ni.GetIPProperties();
                    UnicastIPAddressInformationCollection uniColl = ipProps.UnicastAddresses;
                    foreach (UnicastIPAddressInformation uni in uniColl)
                    {
                        if ((uni.Address.AddressFamily == AddressFamily.InterNetwork && protocol != EnableIPv6) ||
                            (uni.Address.AddressFamily == AddressFamily.InterNetworkV6 && protocol != EnableIPv4))
                        {
                            if (includeLoopback || !IPAddress.IsLoopback(uni.Address))
                            {
                                addresses.Add(uni.Address);
                            }
                        }
                    }
                }

                foreach (IPAddress a in Dns.GetHostAddresses(Dns.GetHostName()))
                {
                    if ((a.AddressFamily == AddressFamily.InterNetwork && protocol != EnableIPv6) ||
                        (a.AddressFamily == AddressFamily.InterNetworkV6 && protocol != EnableIPv4))
                    {
                        if (includeLoopback || !IPAddress.IsLoopback(a))
                        {
                            addresses.Add(a);
                        }
                    }
                }
            }
            catch (SocketException ex)
            {
                if (socketErrorCode(ex) == SocketError.TryAgain && --retry >= 0)
                {
                    goto repeatGetHostByName;
                }
                Ice.DNSException e = new Ice.DNSException(ex);
                e.host = "0.0.0.0";
                throw e;
            }
            catch (Exception ex)
            {
                Ice.DNSException e = new Ice.DNSException(ex);
                e.host = "0.0.0.0";
                throw e;
            }

            return(addresses.ToArray());
        }
コード例 #2
0
ファイル: Network.cs プロジェクト: skyccn/ice
        public static List <EndPoint> getAddresses(string host, int port, int protocol,
                                                   Ice.EndpointSelectionType selType, bool preferIPv6, bool blocking)
        {
            List <EndPoint> addresses = new List <EndPoint>();

            if (host.Length == 0)
            {
                foreach (IPAddress a in getLoopbackAddresses(protocol))
                {
                    addresses.Add(new IPEndPoint(a, port));
                }
                if (protocol == EnableBoth)
                {
                    if (preferIPv6)
                    {
                        IceUtilInternal.Collections.Sort(ref addresses, _preferIPv6Comparator);
                    }
                    else
                    {
                        IceUtilInternal.Collections.Sort(ref addresses, _preferIPv4Comparator);
                    }
                }
                return(addresses);
            }

            int retry = 5;

repeatGetHostByName:
            try
            {
                //
                // No need for lookup if host is ip address.
                //
                try
                {
                    IPAddress addr = IPAddress.Parse(host);
                    if ((addr.AddressFamily == AddressFamily.InterNetwork && protocol != EnableIPv6) ||
                        (addr.AddressFamily == AddressFamily.InterNetworkV6 && protocol != EnableIPv4))
                    {
                        addresses.Add(new IPEndPoint(addr, port));
                        return(addresses);
                    }
                    else
                    {
                        Ice.DNSException e = new Ice.DNSException();
                        e.host = host;
                        throw e;
                    }
                }
                catch (FormatException)
                {
                    if (!blocking)
                    {
                        return(addresses);
                    }
                }

                foreach (IPAddress a in Dns.GetHostAddresses(host))
                {
                    if ((a.AddressFamily == AddressFamily.InterNetwork && protocol != EnableIPv6) ||
                        (a.AddressFamily == AddressFamily.InterNetworkV6 && protocol != EnableIPv4))
                    {
                        addresses.Add(new IPEndPoint(a, port));
                    }
                }

                if (selType == Ice.EndpointSelectionType.Random)
                {
                    IceUtilInternal.Collections.Shuffle(ref addresses);
                }

                if (protocol == EnableBoth)
                {
                    if (preferIPv6)
                    {
                        IceUtilInternal.Collections.Sort(ref addresses, _preferIPv6Comparator);
                    }
                    else
                    {
                        IceUtilInternal.Collections.Sort(ref addresses, _preferIPv4Comparator);
                    }
                }
            }
            catch (SocketException ex)
            {
                if (socketErrorCode(ex) == SocketError.TryAgain && --retry >= 0)
                {
                    goto repeatGetHostByName;
                }
                Ice.DNSException e = new Ice.DNSException(ex);
                e.host = host;
                throw e;
            }
            catch (Exception ex)
            {
                Ice.DNSException e = new Ice.DNSException(ex);
                e.host = host;
                throw e;
            }

            //
            // No InterNetwork/InterNetworkV6 available.
            //
            if (addresses.Count == 0)
            {
                Ice.DNSException e = new Ice.DNSException();
                e.host = host;
                throw e;
            }
            return(addresses);
        }
コード例 #3
0
ファイル: Network.cs プロジェクト: bholl/zeroc-ice
        public static List<IPEndPoint> getAddresses(string host, int port, int protocol, bool blocking)
        {
            List<IPEndPoint> addresses = new List<IPEndPoint>();
            if(host.Length == 0)
            {
                if(protocol != EnableIPv4)
                {
                    addresses.Add(new IPEndPoint(IPAddress.IPv6Loopback, port));
                }

                if(protocol != EnableIPv6)
                {
                    addresses.Add(new IPEndPoint(IPAddress.Loopback, port));
                }
                return addresses;
            }
            else
            {
                int retry = 5;

                repeatGetHostByName:
                try
                {
                    //
                    // No need for lookup if host is ip address.
                    //
                    try
                    {
                        IPAddress addr = IPAddress.Parse(host);
                        if((addr.AddressFamily == AddressFamily.InterNetwork && protocol != EnableIPv6) ||
                           (addr.AddressFamily == AddressFamily.InterNetworkV6 && protocol != EnableIPv4))
                        {
                            addresses.Add(new IPEndPoint(addr, port));
                            return addresses;
                        }
                    }
                    catch(FormatException)
                    {
                        if(!blocking)
                        {
                            return addresses;
                        }
                    }

            #if COMPACT
                    foreach(IPAddress a in Dns.GetHostEntry(host).AddressList)
            #else
                    foreach(IPAddress a in Dns.GetHostAddresses(host))
            #endif
                    {
                        if((a.AddressFamily == AddressFamily.InterNetwork && protocol != EnableIPv6) ||
                           (a.AddressFamily == AddressFamily.InterNetworkV6 && protocol != EnableIPv4))
                        {
                            addresses.Add(new IPEndPoint(a, port));
                        }
                    }
                }
                catch(Win32Exception ex)
                {
                    if(ex.NativeErrorCode == WSATRY_AGAIN && --retry >= 0)
                    {
                        goto repeatGetHostByName;
                    }
                    Ice.DNSException e = new Ice.DNSException(ex);
                    e.host = host;
                    throw e;
                }
                catch(System.Exception ex)
                {
                    Ice.DNSException e = new Ice.DNSException(ex);
                    e.host = host;
                    throw e;
                }

                //
                // No InterNetwork/InterNetworkV6 available.
                //
                if(addresses.Count == 0)
                {
                    Ice.DNSException e = new Ice.DNSException();
                    e.host = host;
                    throw e;
                }
            }

            return addresses;
        }
コード例 #4
0
ファイル: Network.cs プロジェクト: bholl/zeroc-ice
        public static IPAddress[] getLocalAddresses(int protocol)
        {
            ArrayList addresses;

            int retry = 5;

            repeatGetHostByName:
            try
            {
                addresses = new ArrayList();
            #if !COMPACT
                if(AssemblyUtil.runtime_ != AssemblyUtil.Runtime.Mono)
                {
                    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
                    foreach(NetworkInterface ni in nics)
                    {
                        IPInterfaceProperties ipProps = ni.GetIPProperties();
                        UnicastIPAddressInformationCollection uniColl = ipProps.UnicastAddresses;
                        foreach(UnicastIPAddressInformation uni in uniColl)
                        {
                            if((uni.Address.AddressFamily == AddressFamily.InterNetwork && protocol != EnableIPv6) ||
                               (uni.Address.AddressFamily == AddressFamily.InterNetworkV6 && protocol != EnableIPv4))
                            {
                                if(!IPAddress.IsLoopback(uni.Address))
                                {
                                    addresses.Add(uni.Address);
                                }
                            }
                        }
                    }
                }
                else
            #endif
                {
            #if COMPACT
                    foreach(IPAddress a in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
            #else
                    foreach(IPAddress a in Dns.GetHostAddresses(Dns.GetHostName()))
            #endif
                    {
                        if((a.AddressFamily == AddressFamily.InterNetwork && protocol != EnableIPv6) ||
                           (a.AddressFamily == AddressFamily.InterNetworkV6 && protocol != EnableIPv4))
                        {
                            if(!IPAddress.IsLoopback(a))
                            {
                                addresses.Add(a);
                            }
                        }
                    }
                }
            }
            catch(Win32Exception ex)
            {
                if(ex.NativeErrorCode == WSATRY_AGAIN && --retry >= 0)
                {
                    goto repeatGetHostByName;
                }
                Ice.DNSException e = new Ice.DNSException(ex);
                e.host = "0.0.0.0";
                throw e;
            }
            catch(System.Exception ex)
            {
                Ice.DNSException e = new Ice.DNSException(ex);
                e.host = "0.0.0.0";
                throw e;
            }

            return (IPAddress[])addresses.ToArray(typeof(IPAddress));
        }
コード例 #5
0
ファイル: Network.cs プロジェクト: bholl/zeroc-ice
        private static IPEndPoint getAddressImpl(string host, int port, int protocol, bool server)
        {
            if(host.Length == 0)
            {
                if(server)
                {
                    if(protocol != EnableIPv4)
                    {
                        return new IPEndPoint(IPAddress.IPv6Any, port);
                    }
                    else
                    {
                        return new IPEndPoint(IPAddress.Any, port);
                    }
                }
                else
                {
                    if(protocol != EnableIPv4)
                    {
                        return new IPEndPoint(IPAddress.IPv6Loopback, port);
                    }
                    else
                    {
                        return new IPEndPoint(IPAddress.Loopback, port);
                    }
                }
            }

            int retry = 5;

            repeatGetHostByName:
            try
            {
                try
                {
                    IPAddress addr = IPAddress.Parse(host);
                    if((addr.AddressFamily == AddressFamily.InterNetwork && protocol != EnableIPv6) ||
                       (addr.AddressFamily == AddressFamily.InterNetworkV6 && protocol != EnableIPv4))
                    {
                        return new IPEndPoint(addr, port);
                    }
                }
                catch (FormatException)
                {
                }

            #if COMPACT
                foreach(IPAddress a in Dns.GetHostEntry(host).AddressList)
            #else
                foreach(IPAddress a in Dns.GetHostAddresses(host))
            #endif
                {
                    if((a.AddressFamily == AddressFamily.InterNetwork && protocol != EnableIPv6) ||
                       (a.AddressFamily == AddressFamily.InterNetworkV6 && protocol != EnableIPv4))
                    {
                        return new IPEndPoint(a, port);
                    }
                }
            }
            catch(Win32Exception ex)
            {
                if(ex.NativeErrorCode == WSATRY_AGAIN && --retry >= 0)
                {
                    goto repeatGetHostByName;
                }
                Ice.DNSException e = new Ice.DNSException(ex);
                e.host = host;
                throw e;
            }
            catch(System.Exception ex)
            {
                Ice.DNSException e = new Ice.DNSException(ex);
                e.host = host;
                throw e;
            }

            //
            // No InterNetwork/InterNetworkV6 address available.
            //
            Ice.DNSException dns = new Ice.DNSException();
            dns.host = host;
            throw dns;
        }