Fixed lan ip determination for virtual physical nics

This commit is contained in:
d3agle 2016-08-11 13:45:21 -05:00
parent d291490181
commit 507e1dd74d
1 changed files with 13 additions and 8 deletions

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Linq;
using System.Management; using System.Management;
using System.Net.NetworkInformation; using System.Net.NetworkInformation;
using System.Net.Sockets; using System.Net.Sockets;
@ -143,17 +144,21 @@ namespace xClient.Core.Helper
{ {
foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces()) foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{ {
if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || GatewayIPAddressInformation gatewayAddress = ni.GetIPProperties().GatewayAddresses.FirstOrDefault();
ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet && if (gatewayAddress != null) //exclude virtual physical nic with no default gateway
ni.OperationalStatus == OperationalStatus.Up)
{ {
foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses) if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 ||
ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet &&
ni.OperationalStatus == OperationalStatus.Up)
{ {
if (ip.Address.AddressFamily != AddressFamily.InterNetwork || foreach (UnicastIPAddressInformation ip in ni.GetIPProperties().UnicastAddresses)
ip.AddressPreferredLifetime == UInt32.MaxValue) // exclude virtual network addresses {
continue; if (ip.Address.AddressFamily != AddressFamily.InterNetwork ||
ip.AddressPreferredLifetime == UInt32.MaxValue) // exclude virtual network addresses
continue;
return ip.Address.ToString(); return ip.Address.ToString();
}
} }
} }
} }