Exemplo n.º 1
0
        /// <summary>
        /// The get ip address.
        /// </summary>
        /// <param name="user">
        /// The user.
        /// </param>
        /// <returns>
        /// The <see cref="IPAddress"/>.
        /// </returns>
        private IPAddress GetIpAddress(IUser user)
        {
            if (user.Hostname.Contains("/"))
            {
                // cloaked. hmm...
                // gateway, try username
                var validHexIp = new Regex("^[0-9A-Fa-f]{8}$");

                if (validHexIp.IsMatch(user.Username))
                {
                    return(user.Username.GetIpAddressFromHex());
                }

                // not a gateway cloak. Can't do anything.
                return(null);
            }

            // resolve hostname
            IPAddress[] addresses = new IPAddress[0];
            try
            {
                addresses = Dns.GetHostEntry(user.Hostname).AddressList;
            }
            catch (SocketException)
            {
            }

            return(addresses.FirstOrDefault());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Разрешает указанное имя хоста в IP-адрес.
        /// </summary>
        /// <param name="host">Имя хоста.</param>
        /// <param name="family">Желаемый тип сетевого адреса.</param>
        /// <returns>IP-адрес хоста или null если хост не найден.</returns>
        public static IPAddress Resolve(string host, AddressFamily family = AddressFamily.Unspecified)
        {
            IPAddress[] addresses;

            try
            {
                addresses = Dns.GetHostAddresses(host);
            }
            catch
            {
                addresses = new IPAddress[0];
            }

            return(addresses.FirstOrDefault(a => a.AddressFamily == family || AddressFamily.Unspecified == family));
        }