Opc.Ua.ServerBase.NormalizeHostname C# (CSharp) Method

NormalizeHostname() protected method

Checks for IP address or well known hostnames that map to the computer.
protected NormalizeHostname ( string hostname ) : string
hostname string The hostname.
return string
        protected string NormalizeHostname(string hostname)
        {
            string computerName = System.Net.Dns.GetHostName();

            // substitute the computer name for localhost if localhost used by client.
            if (Utils.AreDomainsEqual(hostname, "localhost"))
            {
                return computerName.ToUpper();
            }

            // check if client is using an ip address.
            System.Net.IPAddress address = null;

            if (System.Net.IPAddress.TryParse(hostname, out address))
            {
                if (System.Net.IPAddress.IsLoopback(address))
                {
                    return computerName.ToUpper();
                }

                // substitute the computer name for any local IP if an IP is used by client.
                System.Net.IPAddress[] addresses = System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName());

                for (int ii = 0; ii < addresses.Length; ii++)
                {
                    if (addresses[ii].Equals(address))
                    {
                        return computerName.ToUpper();
                    }
                }

                // not a localhost IP address.
                return hostname.ToUpper(); 
            }

            // check for aliases.
            System.Net.IPHostEntry entry = System.Net.Dns.GetHostEntry(computerName);

            for (int ii = 0; ii < entry.Aliases.Length; ii++)
            {
                if (Utils.AreDomainsEqual(hostname, entry.Aliases[ii]))
                {
                    return computerName.ToUpper();
                }
            }

            // return normalized hostname.
            return hostname.ToUpper();
        }