TryParse() public static method

public static TryParse ( string ipString, IPAddress &address ) : bool
ipString string
address IPAddress
return bool
        /// <summary>
        /// Get IPv4 address from notation (xxx.xxx.xxx.xxx) or hostname
        /// </summary>
        public static NetAddress Resolve(string ipOrHost)
        {
            if (string.IsNullOrEmpty(ipOrHost))
            {
                throw new ArgumentException("Supplied string must not be empty", "ipOrHost");
            }

            ipOrHost = ipOrHost.Trim();

            NetAddress ipAddress = null;

            if (NetAddress.TryParse(ipOrHost, out ipAddress))
            {
                if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
                {
                    return(ipAddress);
                }
                throw new ArgumentException("This method will not currently resolve other than ipv4 addresses");
            }

            // ok must be a host name
            try
            {
                //var addresses = Dns.GetHostAddresses(ipOrHost);
                var addressesTask = Dns.GetHostAddressesAsync(ipOrHost);
                addressesTask.RunSynchronously();
                //var address = addressesTask.Result;
                if (addressesTask.Result == null)
                {
                    return(null);
                }
                foreach (var address in addressesTask.Result)
                {
                    if (address.AddressFamily == AddressFamily.InterNetwork)
                    {
                        return(address);
                    }
                }
                return(null);
            }
            catch (SocketException ex)
            {
                if (ex.SocketErrorCode == SocketError.HostNotFound)
                {
                    //LogWrite(string.Format(CultureInfo.InvariantCulture, "Failed to resolve host '{0}'.", ipOrHost));
                    return(null);
                }
                else
                {
                    throw;
                }
            }
        }
        /// <summary>
        /// Get IPv4 address from notation (xxx.xxx.xxx.xxx) or hostname (asynchronous version)
        /// </summary>
        public static void ResolveAsync(string ipOrHost, ResolveAddressCallback callback)
        {
            if (string.IsNullOrEmpty(ipOrHost))
            {
                throw new ArgumentException("Supplied string must not be empty", "ipOrHost");
            }

            ipOrHost = ipOrHost.Trim();

            NetAddress ipAddress = null;

            if (NetAddress.TryParse(ipOrHost, out ipAddress))
            {
                if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
                {
                    callback(ipAddress);
                    return;
                }
                throw new ArgumentException("This method will not currently resolve other than ipv4 addresses");
            }

            // ok must be a host name
            IPHostEntry entry;

            try
            {
                var addrTask = Dns.GetHostEntryAsync(ipOrHost);
                addrTask.RunSynchronously();
                callback(addrTask.Result.AddressList[0]);
            }
            catch (SocketException ex)
            {
                if (ex.SocketErrorCode == SocketError.HostNotFound)
                {
                    //LogWrite(string.Format(CultureInfo.InvariantCulture, "Failed to resolve host '{0}'.", ipOrHost));
                    callback(null);
                }
                else
                {
                    throw;
                }
            }
        }
Ejemplo n.º 3
0
    private void UpdateClientAddress()
    {
        IPAddress ipAddress;

        if (IPAddress.TryParse(ClientIP, out ipAddress) && 0 <= Port && Port <= 65535)
        {
            if (oscClient != null)
            {
                oscClient.Close();
            }
            oscClient = new OSCClient(ipAddress, Port);
            // Formatting might have changed
            ClientIP = oscClient.ClientIPAddress.ToString();
            Debug.Log($"OSC Client address set to {ClientIP}:{Port}.");
        }
        else
        {
            Debug.LogWarning($"Unable to set OSC client address to invalid IP/port: {ClientIP}:{Port}. OSC is still being sent to {oscClient.ClientIPAddress}:{oscClient.Port}.");
        }
        currentClientIP = ClientIP;
    }
Ejemplo n.º 4
0
        /// <summary>
        /// Get IPv4 address from notation (xxx.xxx.xxx.xxx) or hostname (asynchronous version)
        /// </summary>
        public static void ResolveAsync(string ipOrHost, ResolveAddressCallback callback)
        {
            if (string.IsNullOrEmpty(ipOrHost))
            {
                throw new ArgumentException("Supplied string must not be empty", "ipOrHost");
            }

            ipOrHost = ipOrHost.Trim();

            NetAddress ipAddress = null;

            if (NetAddress.TryParse(ipOrHost, out ipAddress))
            {
                if (ipAddress.AddressFamily == AddressFamily.InterNetwork || ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
                {
                    callback(ipAddress);
                    return;
                }
                throw new ArgumentException("This method will not currently resolve other than ipv4 addresses");
            }

            // ok must be a host name
            IPHostEntry entry;

            try
            {
                Dns.BeginGetHostEntry(ipOrHost, delegate(IAsyncResult result)
                {
                    try
                    {
                        entry = Dns.EndGetHostEntry(result);
                    }
                    catch (SocketException ex)
                    {
                        if (ex.SocketErrorCode == SocketError.HostNotFound)
                        {
                            //LogWrite(string.Format(CultureInfo.InvariantCulture, "Failed to resolve host '{0}'.", ipOrHost));
                            callback(null);
                            return;
                        }
                        else
                        {
                            throw;
                        }
                    }

                    if (entry == null)
                    {
                        callback(null);
                        return;
                    }

                    // check each entry for a valid IP address
                    foreach (var ipCurrent in entry.AddressList)
                    {
                        if (ipCurrent.AddressFamily == AddressFamily.InterNetwork || ipCurrent.AddressFamily == AddressFamily.InterNetworkV6)
                        {
                            callback(ipCurrent);
                            return;
                        }
                    }

                    callback(null);
                }, null);
            }
            catch (SocketException ex)
            {
                if (ex.SocketErrorCode == SocketError.HostNotFound)
                {
                    //LogWrite(string.Format(CultureInfo.InvariantCulture, "Failed to resolve host '{0}'.", ipOrHost));
                    callback(null);
                }
                else
                {
                    throw;
                }
            }
        }
        internal void Resolve()
        {
            //
            // if we already resolved this name then don't do it again
            //

            if (cached)
            {
                return;
            }

            //
            // IP wildcards are not resolved
            //

            if (wildcard)
            {
                return;
            }

            //
            // IP addresses with wildcards are allowed in permissions
            //

            if (IsValidWildcard)
            {
                wildcard = true;
                cached   = true;
                return;
            }

            //
            // Check if the permission was specified as numeric IP.
            //
            IPAddress ipaddr;

            if (IPAddress.TryParse(hostname, out ipaddr))
            {
                address    = new IPAddress[1];
                address[0] = ipaddr;
                cached     = true;
                return;
            }

            //
            // Not numeric: use GetHostByName to determine addresses
            //
            try {
                IPHostEntry ipHostEntry;
                if (Dns.TryInternalResolve(hostname, out ipHostEntry))
                {
                    address = ipHostEntry.AddressList;
                }

                // NB: It never caches DNS responses
                //
            }
            catch (SecurityException) {
                throw;
            }
            catch {
                // ignore second exception
            }
        }