/// <summary>
        /// Get the local ip address of the local machine
        /// </summary>
        /// <param name="dns">Dns lookup</param>
        /// <param name="addressFamily">Desired address family</param>
        /// <returns>Local ip address or null if unable to determine. If no address family match, falls back to an ipv6 attempt.</returns>
        public static async Task <System.Net.IPAddress> GetLocalIPAddress(this IDnsLookup dns, System.Net.Sockets.AddressFamily addressFamily = System.Net.Sockets.AddressFamily.InterNetwork)
        {
            try
            {
                // append ipv4 first, then the ipv6 then the remote ip
                System.Net.IPAddress[] ips = await dns.GetHostAddressesAsync(dns.GetHostName());

                foreach (System.Net.IPAddress ip in ips)
                {
                    if (ip.AddressFamily == addressFamily)
                    {
                        return(ip);
                    }
                }
                foreach (System.Net.IPAddress ip in ips)
                {
                    if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
                    {
                        return(ip);
                    }
                }
            }
            catch
            {
            }
            return(null);
        }
Exemple #2
0
 /// <summary>
 /// Get the local ip addresses of the local machine
 /// </summary>
 /// <param name="dns">Dns lookup</param>
 /// <param name="addressFamily">Desired address family or null for all</param>
 /// <returns>Local ip address or empty array if unable to determine. If no address family match, falls back to an ipv6 attempt.</returns>
 public static async Task <System.Net.IPAddress[]> GetLocalIPAddressesAsync(this IDnsLookup dns, System.Net.Sockets.AddressFamily?addressFamily = System.Net.Sockets.AddressFamily.InterNetwork)
 {
     try
     {
         // append ipv4 first, then the ipv6 then the remote ip
         return((await dns.GetHostAddressesAsync(dns.GetHostName())).Union(localHostIP).Where(i => addressFamily is null || i.AddressFamily == addressFamily).ToArray());
     }
     catch
     {
     }
     return(new System.Net.IPAddress[0]);
 }
Exemple #3
0
 /// <summary>
 /// Get the ip addresses of the local machine
 /// </summary>
 /// <param name="dns">Dns lookup</param>
 /// <param name="allowLocal">Whether to return localhost ip</param>
 /// <param name="addressFamily">Desired address family or null for all</param>
 /// <returns>Local ip address or empty array if unable to determine. If no address family match, falls back to an ipv6 attempt.</returns>
 public static async Task <System.Net.IPAddress[]> GetLocalIPAddressesAsync(this IDnsLookup dns, bool allowLocal = true, System.Net.Sockets.AddressFamily?addressFamily = System.Net.Sockets.AddressFamily.InterNetwork)
 {
     try
     {
         // append ipv4 first, then the ipv6 then the remote ip
         List <IPAddress> ips = new List <IPAddress>();
         ips.AddRange(await dns.GetHostAddressesAsync(dns.GetHostName()));
         if (allowLocal)
         {
             ips.AddRange(localHostIP);
         }
         return(ips.Where(ip => (allowLocal || ip.IsLocalHost()) ||
                          (addressFamily is null || ip.AddressFamily == addressFamily)).ToArray());
     }
     catch
     {
     }
     return(new System.Net.IPAddress[0]);
 }