private bool IsWOLType(NetworkInterfaceType t) { return t == NetworkInterfaceType.Ethernet || t == NetworkInterfaceType.Ethernet3Megabit || t == NetworkInterfaceType.FastEthernetFx || t == NetworkInterfaceType.FastEthernetT || t == NetworkInterfaceType.GigabitEthernet; }
private static string Translate(NetworkInterfaceType key) { switch (key) { case NetworkInterfaceType.Ethernet: return "eth"; default: return key.ToString(); } }
private static void UpdateNetworkState(NetworkInterfaceType type, bool connected) { lock (internalLock) { if (type == NetworkInterfaceType.Wireless80211) isWiFiConnected = connected; else if (type == NetworkInterfaceType.MobileBroadbandCdma) isCdmaConnected = connected; else if (type == NetworkInterfaceType.MobileBroadbandGsm) isGsmConnected = connected; } }
void interfaceTypeWorker_DoWork(object sender, DoWorkEventArgs e) { isNetworkAvailable = NetworkInterface.GetIsNetworkAvailable(); if (isNetworkAvailable) { netType = NetworkInterface.NetworkInterfaceType; if (netType == NetworkInterfaceType.None) { isNetworkAvailable = false; } } }
private static NetworkInterface GetNetworkInterface(NetworkInterfaceType networkInterfaceType, string networkInterfaceName) { NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); NetworkInterface networkInterface = null; networkInterface = nics.FirstOrDefault( w => w.NetworkInterfaceType == networkInterfaceType && w.Name == networkInterfaceName && w.OperationalStatus == OperationalStatus.Up && !w.Description.Contains("VMware") && !w.Description.Contains("Bluetooth")); return(networkInterface); }
private static bool IsCompatibleInterface(NetworkInterfaceType nit) { switch (nit) { case NetworkInterfaceType.Loopback: case NetworkInterfaceType.HighPerformanceSerialBus: case NetworkInterfaceType.Ppp: return(false); default: return(true); } }
public static void InternalSetupHelper( NetworkInterface[] nis, NetworkInterfaceType targetNetworkInterface, IPConfiguration ipConfiguration) { foreach (var networkInterface in nis) { // target only the working interface type if (networkInterface.NetworkInterfaceType == targetNetworkInterface) { if (ipConfiguration != null) { Debug.WriteLine("Setting up static IP v4 configuration"); // setup static IPv4 networkInterface.EnableStaticIPv4( ipConfiguration.IPAddress, ipConfiguration.IPSubnetMask, ipConfiguration.IPGatewayAddress); // check if static DNS is to be configured if (ipConfiguration.IPDns != null && ipConfiguration.IPDns.Length > 0) { networkInterface.EnableStaticIPv4Dns(ipConfiguration.IPDns); } } else { if (networkInterface.IsDhcpEnabled) { networkInterface.EnableDhcp(); } else { // setup static IPv4 with network config networkInterface.EnableStaticIPv4( networkInterface.IPv4Address, networkInterface.IPv4SubnetMask, networkInterface.IPv4GatewayAddress); // check if static DNS is to be configured if (networkInterface.IPv4DnsAddresses.Length > 0) { networkInterface.EnableStaticIPv4Dns(networkInterface.IPv4DnsAddresses); } } } } } }
public static string GetIP(ADDRESSFAMILYTYPE addressfamilyType) { if (addressfamilyType == ADDRESSFAMILYTYPE.IPv6 && !Socket.OSSupportsIPv6) { return(null); } string ret = ""; //ローカルのネットワークを全部拾ってくる。 NetworkInterface[] interfaces = new NetworkInterface[NetworkInterface.GetAllNetworkInterfaces().Length]; interfaces = NetworkInterface.GetAllNetworkInterfaces(); for (var i = 0; i < interfaces.Length; i++) { var item = interfaces[i]; #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211; NetworkInterfaceType _type2 = NetworkInterfaceType.Ethernet; if ((item.NetworkInterfaceType == _type1 || item.NetworkInterfaceType == _type2) && item.OperationalStatus == OperationalStatus.Up) #endif { for (var index = 0; index < item.GetIPProperties().UnicastAddresses.Count; index++) { UnicastIPAddressInformation ip = item.GetIPProperties().UnicastAddresses[index]; //IPv4 if (addressfamilyType == ADDRESSFAMILYTYPE.IPv4) { if (ip.Address.AddressFamily == AddressFamily.InterNetwork) { ret = ip.Address.ToString(); } } //IPv6 else if (addressfamilyType == ADDRESSFAMILYTYPE.IPv6) { if (ip.Address.AddressFamily == AddressFamily.InterNetworkV6) { ret = ip.Address.ToString(); } } } } } return(ret); }
protected unsafe void ProcessLinkLayerAddress(Interop.Sys.LinkLayerAddressInfo *llAddr) { byte[] macAddress = new byte[llAddr->NumAddressBytes]; fixed(byte *macAddressPtr = macAddress) { Buffer.MemoryCopy(llAddr->AddressBytes, macAddressPtr, llAddr->NumAddressBytes, llAddr->NumAddressBytes); } PhysicalAddress physicalAddress = new PhysicalAddress(macAddress); _index = llAddr->InterfaceIndex; _physicalAddress = physicalAddress; _networkInterfaceType = (NetworkInterfaceType)llAddr->HardwareType; }
/// <summary> /// Initializes a new instance of the <see cref="NetworkInterface"/> class. /// </summary> /// <param name="interfaceIndex"></param> protected NetworkInterface(int interfaceIndex) { _interfaceIndex = interfaceIndex; _networkInterfaceType = NetworkInterfaceType.Unknown; _startupAddressMode = AddressMode.Invalid; _specificConfigId = uint.MaxValue; _automaticDns = true; _ipv6Address = new uint[4]; _ipv6NetMask = new uint[4]; _ipv6GatewayAddress = new uint[4]; _ipv6dnsAddress1 = new uint[4]; _ipv6dnsAddress2 = new uint[4]; }
public void CallBackToConnect() { try { string query = "Select * from Usertable Where username = '******' and passwo = '" + textBox2.Text.Trim() + "'"; //NetworkInterfaceType type = NetworkInterfaceType.Wireless80211; NetworkInterfaceType type = NetworkInterfaceType.Ethernet; string Local = form.networker.GetLocalIP(type); form.networker.Send("Login;" + query + ";" + Local, form.networker._client); s = textBox1.Text; } catch { } }
private void button1_Click(object sender, EventArgs e) { try { NetworkInterfaceType type = NetworkInterfaceType.Ethernet; string Local = form.networker.GetLocalIP(type); form.networker.Send("Login;" + textBox1.Text.Trim() + ";" + textBox2.Text.Trim() + ";" + Local); form.networker.GetLogin(this); s = textBox1.Text; Thread.Sleep(500); label3.Text = "The password you entered \n\t\t is incorrect. Please try again."; } catch { } }
public NetworkInterfaceInternal(System.Net.NetworkInformation.NetworkInterface nic) { if (nic == null) { return; } _id = nic.Id; _name = nic.Name; _descr = nic.Description; _interfaceType = nic.NetworkInterfaceType; _status = nic.OperationalStatus; _mac = nic.GetPhysicalAddress().GetAddressBytes(); }
public static string GetIP(ADDRESSFAM Addfam) { //Return null if ADDRESSFAM is Ipv6 but Os does not support it if (Addfam == ADDRESSFAM.IPv6 && !Socket.OSSupportsIPv6) { return(null); } string output = ""; foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) { #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211; NetworkInterfaceType _type2 = NetworkInterfaceType.Ethernet; if ((item.NetworkInterfaceType == _type1 || item.NetworkInterfaceType == _type2) && item.OperationalStatus == OperationalStatus.Up) //if (item.NetworkInterfaceType == _type1 && item.OperationalStatus == OperationalStatus.Up) #endif { foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses) { //IPv4 if (Addfam == ADDRESSFAM.IPv4) { if (ip.Address.AddressFamily == AddressFamily.InterNetwork) { output += ip.Address.ToString() + " (" + ip.PrefixOrigin.ToString() + ")\n\t "; //Debug.Log(ip.AddressValidLifetime); //Debug.Log(ip.PrefixOrigin.ToString()); //Debug.Log(ip.SuffixOrigin.ToString()); //break; } } //IPv6 else if (Addfam == ADDRESSFAM.IPv6) { if (ip.Address.AddressFamily == AddressFamily.InterNetworkV6) { output += ip.Address.ToString(); } } } //break; } } return(output); }
private bool getNetType(NetworkInterfaceType _Type) { bool flag = true; if (_Type == NetworkInterfaceType.Ethernet) { flag = true; } else if (_Type == NetworkInterfaceType.Wireless80211) { flag = false; } return(flag); }
public void InvokeSyncVarNetworkly(string locationName, object value, NetworkInterfaceType networkInterface = NetworkInterfaceType.TCP) { if (locationByType.TryGetValue(GetType(), out Dictionary <string, NetworkLocationExecuter> d)) { if (d.TryGetValue(locationName, out NetworkLocationExecuter networkMemberExecuter)) { networkMemberExecuter.InvokeMemberFromLocal(this, () => { OnInvokeLocationNetworkly.Invoke(this, networkInterface, locationName, Operations.GetObjectAsValue(value)); }); return; } } NetworkBehavior.PrintWarning("No location with name: {0} was not found", locationName); }
/// <summary> /// Returns a list of interface names that a profile can be applied to. They are /// not guaranteed to be valid and not all valid interfaces may be listed. /// </summary> /// <returns></returns> public static IEnumerable <string> ListInterfaces() { NetworkInterfaceType validTypes = NetworkInterfaceType.Ethernet | NetworkInterfaceType.Wireless80211 | NetworkInterfaceType.Ethernet3Megabit | NetworkInterfaceType.FastEthernetFx | NetworkInterfaceType.FastEthernetT | NetworkInterfaceType.GigabitEthernet; var interfaces = (from ifc in NetworkInterface.GetAllNetworkInterfaces() where (ifc.NetworkInterfaceType | validTypes) == validTypes select ifc.Name).ToList(); return(interfaces); }
public static string GetLocalIPv4(NetworkInterfaceType _type) { string output = ""; foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) { if (item.NetworkInterfaceType == _type && item.OperationalStatus == OperationalStatus.Up) { IPInterfaceProperties adapterProperties = item.GetIPProperties(); if (adapterProperties.GatewayAddresses.FirstOrDefault() != null) { foreach (UnicastIPAddressInformation ip in adapterProperties.UnicastAddresses) { if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { output = ip.Address.ToString(); } } } if (string.IsNullOrEmpty(output)) { NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211; foreach (NetworkInterface item1 in NetworkInterface.GetAllNetworkInterfaces()) { if (item1.NetworkInterfaceType == _type1 && item1.OperationalStatus == OperationalStatus.Up) { IPInterfaceProperties adapterProperties1 = item1.GetIPProperties(); if (adapterProperties1.GatewayAddresses.FirstOrDefault() != null) { foreach (UnicastIPAddressInformation ip in adapterProperties1.UnicastAddresses) { if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { output = ip.Address.ToString(); contype = true; } } } } } } } } return(output); }
//Courtesy of this legend: https://stackoverflow.com/a/53219111/5186798 public static IEnumerable <IPNetwork> GetNetworks(NetworkInterfaceType type) { foreach (var item in NetworkInterface.GetAllNetworkInterfaces() .Where(n => n.NetworkInterfaceType == type && n.OperationalStatus == OperationalStatus.Up) // get all operational networks of a given type .Select(n => n.GetIPProperties()) // get the IPs .Where(n => n.GatewayAddresses.Any())) // where the IPs have a gateway defined { var ipInfo = item.UnicastAddresses.FirstOrDefault(i => i.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork); // get the first cluster-facing IP address if (ipInfo == null) { continue; } // convert the mask to bits var maskBytes = ipInfo.IPv4Mask.GetAddressBytes(); if (!BitConverter.IsLittleEndian) { Array.Reverse(maskBytes); } var maskBits = new BitArray(maskBytes); // count the number of "true" bits to get the CIDR mask var cidrMask = maskBits.Cast <bool>().Count(b => b); // convert my application's ip address to bits var ipBytes = ipInfo.Address.GetAddressBytes(); if (!BitConverter.IsLittleEndian) { Array.Reverse(maskBytes); } var ipBits = new BitArray(ipBytes); // and the bits with the mask to get the start of the range var maskedBits = ipBits.And(maskBits); // Convert the masked IP back into an IP address var maskedIpBytes = new byte[4]; maskedBits.CopyTo(maskedIpBytes, 0); if (!BitConverter.IsLittleEndian) { Array.Reverse(maskedIpBytes); } var rangeStartIp = new IPAddress(maskedIpBytes); // return the start IP and CIDR mask yield return(new IPNetwork(rangeStartIp, cidrMask)); } }
public void Init() { name = nic.Name; desc = nic.Description; id = nic.Id; type = nic.NetworkInterfaceType; stat = nic.OperationalStatus; maxSpeed = nic.Speed; mac = nic.GetPhysicalAddress(); var ips = nic.GetIPProperties().DnsAddresses; ipv4 = (from ip in ips where ip.AddressFamily == AddressFamily.InterNetwork select ip).FirstOrDefault(); ipv6 = (from ip in ips where ip.AddressFamily == AddressFamily.InterNetworkV6 select ip).ToArray(); }
private List <NetworkInterface> getAvailableNetworkInterfaces() { if (availableNetworkInterfaces.Count == 0) { NetworkInterfaceType eth = NetworkInterfaceType.Ethernet; NetworkInterfaceType wifi = NetworkInterfaceType.Wireless80211; foreach (NetworkInterface interf in NetworkInterface.GetAllNetworkInterfaces()) { if ((interf.NetworkInterfaceType == eth || interf.NetworkInterfaceType == wifi) && (interf.OperationalStatus == OperationalStatus.Up)) { availableNetworkInterfaces.Add(interf); } } } return(availableNetworkInterfaces); }
public static void GetLocalIPv4(NetworkInterfaceType _type) { foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) { if (item.NetworkInterfaceType == _type && item.OperationalStatus == OperationalStatus.Up) { foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses) { if (ip.Address.AddressFamily == AddressFamily.InterNetwork) { Console.WriteLine(ip.Address.ToString() + " @ " + item.Name); // "Ethernet" || "Wi-Fi" } } } } }
public IEnumerable <string> GetAllLocalIPv4(NetworkInterfaceType type) { foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) { if (item.NetworkInterfaceType == type && item.OperationalStatus == OperationalStatus.Up) { foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses) { if (ip.Address.AddressFamily == AddressFamily.InterNetwork) { yield return(ip.Address.ToString()); } } } } }
private IPAddress GetFirstIpAddress(NetworkInterfaceType network = NetworkInterfaceType.Ethernet, AddressFamily addressFamily = AddressFamily.InterNetwork) { var addresses = Dns.GetHostAddresses(Environment.MachineName); if (!addresses.Any()) { throw new ArgumentException("No addresses found for the given host"); } return(NetworkInterface .GetAllNetworkInterfaces() .Where(i => i.NetworkInterfaceType == network) .SelectMany(i => i.GetIPProperties().UnicastAddresses) .Where(a => a.Address.AddressFamily == addressFamily) .Select(a => a.Address).ToArray().First()); }
static NetworkInterfaceType GetInterfaceByString(String type) { NetworkInterfaceType interfaceType = NetworkInterfaceType.Ethernet; switch (type.ToLower()) { case "ethernet": interfaceType = NetworkInterfaceType.Ethernet; break; case "wireless": interfaceType = NetworkInterfaceType.Wireless80211; break; } return(interfaceType); }
/// <summary> /// Get the IP address of a /// </summary> /// <param name="interfaceType"></param> /// <returns></returns> public static string GetLocalIPv4(NetworkInterfaceType interfaceType) { var addressInfo = NetworkInterface.GetAllNetworkInterfaces() .Where(c => c.NetworkInterfaceType == interfaceType && c.OperationalStatus == OperationalStatus.Up) .Select(c => c.GetIPProperties()) .Where(c => c.GatewayAddresses.Any()) .SelectMany(c => c.UnicastAddresses) .Where(c => c.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) .FirstOrDefault(); if (addressInfo == null || addressInfo.Address == null) { return(UNKNOWN_IP); } return(addressInfo.Address.ToString()); }
public static string GetName(this NetworkInterfaceType source) { switch (source) { case NetworkInterfaceType.Ethernet: return("Ethernet"); case NetworkInterfaceType.Wireless80211: return("Wireless802.11"); case NetworkInterfaceType.Unknown: return("Unknown"); default: return("Undefined"); } }
private static void FireGetNetworkTypeCompleted(NetworkInterfaceType type, bool hasTimeout = false) { lock (_synchronizationObject) { if (_isGettingNetworkType) { _isGettingNetworkType = false; NetworkTypeEventHandler networkTypeEventHandler = GetNetworkTypeCompleted; if (networkTypeEventHandler != null) { networkTypeEventHandler(null, new NetworkTypeEventArgs(type, hasTimeout)); } } } }
/// <summary> /// http://stackoverflow.com/a/24814027/1141876 /// </summary> public string[] GetAllLocalIPv4(NetworkInterfaceType type) { List<string> ipAddrList = new List<string>(); foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) { if (item.NetworkInterfaceType == type && item.OperationalStatus == OperationalStatus.Up) { foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses) { if (ip.Address.AddressFamily == AddressFamily.InterNetwork) { ipAddrList.Add(ip.Address.ToString()); } } } } return ipAddrList.ToArray(); }
private bool IsNetWorkConnected() { bool networkIsAvailable = NetworkInterface.GetIsNetworkAvailable(); if (!networkIsAvailable) { return(false); } NetworkInterfaceType currentNetworkType = NetworkInterface.NetworkInterfaceType; if (currentNetworkType == NetworkInterfaceType.None) { return(false); } return(true); }
public static IPAddress GetLocalIPv4(NetworkInterfaceType interfaceType) { foreach (var networkInterface in NetworkInterface.GetAllNetworkInterfaces()) { if (networkInterface.NetworkInterfaceType == interfaceType && networkInterface.OperationalStatus == OperationalStatus.Up) { foreach (UnicastIPAddressInformation ip in networkInterface.GetIPProperties().UnicastAddresses) { if (ip.Address.AddressFamily == AddressFamily.InterNetwork) { return(ip.Address); } } } } throw new Exception("Unable to determine local IP Address"); }
private static void UpdateNetworkState(NetworkInterfaceType type, bool connected) { lock (internalLock) { if (type == NetworkInterfaceType.Wireless80211) { isWiFiConnected = connected; } else if (type == NetworkInterfaceType.MobileBroadbandCdma) { isCdmaConnected = connected; } else if (type == NetworkInterfaceType.MobileBroadbandGsm) { isGsmConnected = connected; } } }
public static void CheckSelfIPAddress(AddressFam Addfam = AddressFam.IPv4) { //m_selfIpAddress = UnityEngine.Network.player.ipAddress; if (Addfam == AddressFam.IPv6 && !Socket.OSSupportsIPv6) { m_selfIpAddress = null; } string output = ""; foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) { #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211; NetworkInterfaceType _type2 = NetworkInterfaceType.Ethernet; if ((item.NetworkInterfaceType == _type1 || item.NetworkInterfaceType == _type2) && item.OperationalStatus == OperationalStatus.Up) #endif { foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses) { //IPv4 if (Addfam == AddressFam.IPv4) { if (ip.Address.AddressFamily == AddressFamily.InterNetwork) { output = ip.Address.ToString(); } } //IPv6 else if (Addfam == AddressFam.IPv6) { if (ip.Address.AddressFamily == AddressFamily.InterNetworkV6) { output = ip.Address.ToString(); } } } } } m_selfIpAddress = output; }
/// <summary> /// Check whether the interface type fits the class /// </summary> /// <param name="type"></param> /// <param name="netclass"></param> /// <returns></returns> public static bool IsInClass(this NetworkInterfaceType type, NetworkClass netclass) { switch (type) { case NetworkInterfaceType.Ethernet: case NetworkInterfaceType.Ethernet3Megabit: case NetworkInterfaceType.GigabitEthernet: case NetworkInterfaceType.FastEthernetT: case NetworkInterfaceType.FastEthernetFx: case NetworkInterfaceType.Slip: case NetworkInterfaceType.IPOverAtm: return((netclass & NetworkClass.Wired) != 0); case NetworkInterfaceType.BasicIsdn: case NetworkInterfaceType.PrimaryIsdn: case NetworkInterfaceType.Isdn: case NetworkInterfaceType.GenericModem: case NetworkInterfaceType.AsymmetricDsl: case NetworkInterfaceType.SymmetricDsl: case NetworkInterfaceType.RateAdaptDsl: case NetworkInterfaceType.VeryHighSpeedDsl: case NetworkInterfaceType.MultiRateSymmetricDsl: case NetworkInterfaceType.Ppp: return((netclass & NetworkClass.Modem) != 0); case NetworkInterfaceType.Wireless80211: case NetworkInterfaceType.Wman: case NetworkInterfaceType.Wwanpp: case NetworkInterfaceType.Wwanpp2: return((netclass & NetworkClass.Wireless) != 0); case NetworkInterfaceType.Tunnel: return((netclass & NetworkClass.Tunnel) != 0); case NetworkInterfaceType.TokenRing: case NetworkInterfaceType.HighPerformanceSerialBus: case NetworkInterfaceType.Fddi: case NetworkInterfaceType.Atm: case NetworkInterfaceType.Loopback: return(false); } return(false); }
public static string GetIP(IpVersion ipVersion) { if (ipVersion == IpVersion.IPv6 && !Socket.OSSupportsIPv6) { ipVersion = IpVersion.IPv4; // Fallback to IPv4 if OS does not support IPv6. } if (ipVersion == IpVersion.IPv4 && !Socket.OSSupportsIPv4) { return(null); } string output = string.Empty; foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) { #if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN NetworkInterfaceType _type1 = NetworkInterfaceType.Wireless80211; NetworkInterfaceType _type2 = NetworkInterfaceType.Ethernet; if ((item.NetworkInterfaceType == _type1 || item.NetworkInterfaceType == _type2) && item.OperationalStatus == OperationalStatus.Up) #endif { foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses) { if (ipVersion == IpVersion.IPv4) { if (ip.Address.AddressFamily == AddressFamily.InterNetwork) { output = ip.Address.ToString(); } } else if (ipVersion == IpVersion.IPv6) { if (ip.Address.AddressFamily == AddressFamily.InterNetworkV6) { output = ip.Address.ToString(); } } } } } return(output); }
internal static string GetLocalIPv4(NetworkInterfaceType _type) { string output = ""; foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) { if (item.NetworkInterfaceType == _type && item.OperationalStatus == OperationalStatus.Up) { IPInterfaceProperties adapterProperties = item.GetIPProperties(); if (adapterProperties.GatewayAddresses.FirstOrDefault() != null) { foreach (UnicastIPAddressInformation ip in adapterProperties.UnicastAddresses) { if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { output = ip.Address.ToString(); } } } } } return output; }
/// <summary> /// 获取当前本地连接列表,根据网络接口类型,和操作状态 /// </summary> /// <param name="type">网络接口类型</param> /// <param name="ipEnabled">网络接口的操作状态</param> public static List<NetworkVO> GetNetworkList(NetworkInterfaceType type, OperationalStatus status) { List<NetworkVO> list = new List<NetworkVO>(); NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in adapters) { //过滤网络接口类型 if (!NetworkInterfaceType.Unknown.Equals(type) && !type.Equals(adapter.NetworkInterfaceType)) { logger.Debug("跳过的其他类型网络,name=" + adapter.Name + ", NetworkInterfaceType=" + adapter.NetworkInterfaceType + ", OperationalStatus=" + adapter.OperationalStatus); continue; } //过滤网络接口的操作状态 if (!status.Equals(adapter.OperationalStatus)) { logger.Debug("跳过的不是上行网络,name=" + adapter.Name + ", NetworkInterfaceType=" + adapter.NetworkInterfaceType + ", OperationalStatus=" + adapter.OperationalStatus); continue; } NetworkVO vo = new NetworkVO(); vo.IpEnabled = true; IPInterfaceProperties property = adapter.GetIPProperties(); vo.DnsHostName = Dns.GetHostName();//本机名 vo.Name = adapter.Name; vo.Description = adapter.Description; vo.Speed = adapter.Speed; //macAddress if (adapter.GetPhysicalAddress() != null && adapter.GetPhysicalAddress().ToString().Length > 0) { char[] mac = adapter.GetPhysicalAddress().ToString().ToCharArray(); vo.MacAddress = mac[0] + mac[1] + "-" + mac[2] + mac[3] + "-" + mac[4] + mac[5] + "-" + mac[6] + mac[7] + "-" + mac[8] + mac[9] + "-" + mac[10] + mac[11]; } //ipAddress subnetMask if (property.UnicastAddresses != null && property.UnicastAddresses.Count > 0) { foreach (UnicastIPAddressInformation ip in property.UnicastAddresses) { if (ip.Address.AddressFamily.Equals(AddressFamily.InterNetwork)) { if (ip.Address != null) { vo.Address = ip.Address.ToString(); } if (ip.IPv4Mask != null) { vo.SubnetMask = ip.IPv4Mask.ToString(); } } } } // gateway if (property.GatewayAddresses != null && property.GatewayAddresses.Count > 0) { foreach (GatewayIPAddressInformation uip in property.GatewayAddresses) { if (uip.Address.AddressFamily.Equals(AddressFamily.InterNetwork)) { vo.Gateway = uip.Address.ToString(); } } } // dns server if (property.DnsAddresses != null && property.DnsAddresses.Count > 0) { vo.DnsServers = new List<string>(); foreach (IPAddress ip in property.DnsAddresses) { if (ip.AddressFamily.Equals(AddressFamily.InterNetwork)) { vo.DnsServers.Add(ip.ToString()); } } } // dhcp server if (property.DhcpServerAddresses != null && property.DhcpServerAddresses.Count > 0) { foreach (IPAddress ip in property.DhcpServerAddresses) { if (ip.AddressFamily.Equals(AddressFamily.InterNetwork)) { vo.DhcpServer = ip.ToString(); vo.DhcpEnabled = true; } } } else { vo.DhcpEnabled = false; } list.Add(vo); } return list; }
internal SystemNetworkInterface(Interop.IpHlpApi.FIXED_INFO fixedInfo, Interop.IpHlpApi.IpAdapterAddresses ipAdapterAddresses) { // Store the common API information. _id = ipAdapterAddresses.AdapterName; _name = ipAdapterAddresses.friendlyName; _description = ipAdapterAddresses.description; _index = ipAdapterAddresses.index; _physicalAddress = ipAdapterAddresses.address; _addressLength = ipAdapterAddresses.addressLength; _type = ipAdapterAddresses.type; _operStatus = ipAdapterAddresses.operStatus; _speed = (long)ipAdapterAddresses.receiveLinkSpeed; // API specific info. _ipv6Index = ipAdapterAddresses.ipv6Index; _adapterFlags = ipAdapterAddresses.flags; _interfaceProperties = new SystemIPInterfaceProperties(fixedInfo, ipAdapterAddresses); }
public void WhenGettingMobileShouldWorkForEachTypeOfMobileNetworkInterface(NetworkInterfaceType type) { _mockMobileNetworkInterface.Setup(ni => ni.NetworkInterfaceType).Returns(type); _mockMobileNetworkInterface.Setup(ni => ni.OperationalStatus).Returns(OperationalStatus.Up); Assert.True(ConnectionInfo.Mobile); }
protected unsafe void ProcessLinkLayerAddress(Interop.Sys.LinkLayerAddressInfo* llAddr) { byte[] macAddress = new byte[llAddr->NumAddressBytes]; fixed (byte* macAddressPtr = macAddress) { Buffer.MemoryCopy(llAddr->AddressBytes, macAddressPtr, llAddr->NumAddressBytes, llAddr->NumAddressBytes); } PhysicalAddress physicalAddress = new PhysicalAddress(macAddress); _index = llAddr->InterfaceIndex; _id = _index.ToString(); _physicalAddress = physicalAddress; _networkInterfaceType = (NetworkInterfaceType)llAddr->HardwareType; }
public string GetLocalIPv4(NetworkInterfaceType type) { string output = ""; foreach (NetworkInterface item in NetworkInterface.GetAllNetworkInterfaces()) { if (item.NetworkInterfaceType == type && item.OperationalStatus == OperationalStatus.Up) { foreach (UnicastIPAddressInformation ip in item.GetIPProperties().UnicastAddresses) { if (ip.Address.AddressFamily == AddressFamily.InterNetwork) { output = ip.Address.ToString(); } } } } return output; }
protected NetworkInterface(int interfaceIndex) { this._interfaceIndex = interfaceIndex; _networkInterfaceType = NetworkInterfaceType.Unknown; }
internal void SetLinkLayerInfo (int index, byte[] macAddress, NetworkInterfaceType type) { this.index = index; this.macAddress = macAddress; this.type = type; }
private void InitializeToIndeterminate() { _isNetworkAvailable = null; _interfaceType = NetworkInterfaceType.Unknown; }
private static IPAddress LoadIPAddress() { var interfaces = GetNetworkInterfaces(); Func<IEnumerable<NetworkInterfaceData>, IEnumerable<IPAddress>> getAddresses = interfaceList => interfaceList.SelectMany(x => x.Addresses) .OrderBy(x => x.AddressFamily != AddressFamily.InterNetwork); // Prefer IPv4 until working IPv6 detection is reliable // Even though the docs say that NetworkIntereface.Type only returns a subset of these types, I've seen some others // (for example Tunnel) in the wild, so let's just list all acceptable types. var preferedTypes = new NetworkInterfaceType[] { NetworkInterfaceType.Ethernet, NetworkInterfaceType.FastEthernetFx, NetworkInterfaceType.FastEthernetT, NetworkInterfaceType.GigabitEthernet, }; var lanAddresses = getAddresses(interfaces.Where(x => preferedTypes.Contains(x.Type))); if (lanAddresses.Any()) return lanAddresses.First(); var addresses = getAddresses(interfaces); if (addresses.Any(x => !_localhostNames.Contains(x.ToString()))) return addresses.First(x => !_localhostNames.Contains(x.ToString())); if (addresses.Any()) return addresses.First(); return IPAddress.Parse("127.0.0.1"); }
public static NetworkInterface [] ImplGetAllNetworkInterfaces () { var interfaces = new Dictionary <string, LinuxNetworkInterface> (); IntPtr ifap; if (getifaddrs (out ifap) != 0) throw new SystemException ("getifaddrs() failed"); try { IntPtr next = ifap; while (next != IntPtr.Zero) { ifaddrs addr = (ifaddrs) Marshal.PtrToStructure (next, typeof (ifaddrs)); IPAddress address = IPAddress.None; string name = addr.ifa_name; int index = -1; byte[] macAddress = null; NetworkInterfaceType type = NetworkInterfaceType.Unknown; if (addr.ifa_addr != IntPtr.Zero) { sockaddr_in sockaddr = (sockaddr_in) Marshal.PtrToStructure (addr.ifa_addr, typeof (sockaddr_in)); if (sockaddr.sin_family == AF_INET6) { sockaddr_in6 sockaddr6 = (sockaddr_in6) Marshal.PtrToStructure (addr.ifa_addr, typeof (sockaddr_in6)); address = new IPAddress (sockaddr6.sin6_addr.u6_addr8, sockaddr6.sin6_scope_id); } else if (sockaddr.sin_family == AF_INET) { address = new IPAddress (sockaddr.sin_addr); } else if (sockaddr.sin_family == AF_PACKET) { sockaddr_ll sockaddrll = (sockaddr_ll) Marshal.PtrToStructure (addr.ifa_addr, typeof (sockaddr_ll)); if (((int)sockaddrll.sll_halen) > sockaddrll.sll_addr.Length){ Console.Error.WriteLine ("Got a bad hardware address length for an AF_PACKET {0} {1}", sockaddrll.sll_halen, sockaddrll.sll_addr.Length); next = addr.ifa_next; continue; } macAddress = new byte [(int) sockaddrll.sll_halen]; Array.Copy (sockaddrll.sll_addr, 0, macAddress, 0, macAddress.Length); index = sockaddrll.sll_ifindex; int hwtype = (int)sockaddrll.sll_hatype; if (Enum.IsDefined (typeof (LinuxArpHardware), hwtype)) { switch ((LinuxArpHardware)hwtype) { case LinuxArpHardware.EETHER: goto case LinuxArpHardware.ETHER; case LinuxArpHardware.ETHER: type = NetworkInterfaceType.Ethernet; break; case LinuxArpHardware.PRONET: type = NetworkInterfaceType.TokenRing; break; case LinuxArpHardware.ATM: type = NetworkInterfaceType.Atm; break; case LinuxArpHardware.SLIP: type = NetworkInterfaceType.Slip; break; case LinuxArpHardware.PPP: type = NetworkInterfaceType.Ppp; break; case LinuxArpHardware.LOOPBACK: type = NetworkInterfaceType.Loopback; macAddress = null; break; case LinuxArpHardware.FDDI: type = NetworkInterfaceType.Fddi; break; case LinuxArpHardware.TUNNEL6: goto case LinuxArpHardware.TUNNEL; case LinuxArpHardware.TUNNEL: type = NetworkInterfaceType.Tunnel; break; } } } } LinuxNetworkInterface iface = null; if (!interfaces.TryGetValue (name, out iface)) { iface = new LinuxNetworkInterface (name); interfaces.Add (name, iface); } if (!address.Equals (IPAddress.None)) iface.AddAddress (address); if (macAddress != null || type == NetworkInterfaceType.Loopback) { if (type == NetworkInterfaceType.Ethernet) { if (Directory.Exists(iface.IfacePath + "wireless")) { type = NetworkInterfaceType.Wireless80211; } } iface.SetLinkLayerInfo (index, macAddress, type); } next = addr.ifa_next; } } finally { freeifaddrs (ifap); } NetworkInterface [] result = new NetworkInterface [interfaces.Count]; int x = 0; foreach (NetworkInterface thisInterface in interfaces.Values) { result [x] = thisInterface; x++; } return result; }
private static bool IsCompatibleInterface(NetworkInterfaceType nit) { switch (nit) { case NetworkInterfaceType.Loopback: case NetworkInterfaceType.HighPerformanceSerialBus: case NetworkInterfaceType.Ppp: return false; } return true; }
private IPAddress GetAddressFromInterfaceType(NetworkInterfaceType interfaceType) { foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces()) { if (adapter.SupportsMulticast && adapter.NetworkInterfaceType == interfaceType && adapter.OperationalStatus == OperationalStatus.Up) { IPInterfaceProperties ipProperties = adapter.GetIPProperties(); foreach (var ipAddress in ipProperties.UnicastAddresses) { if (ipAddress.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) return ipAddress.Address; } } } return null; }
// Vista+ internal SystemNetworkInterface(FixedInfo fixedInfo, IpAdapterAddresses ipAdapterAddresses) { //store the common api information id = ipAdapterAddresses.AdapterName; name = ipAdapterAddresses.friendlyName; description = ipAdapterAddresses.description; index = ipAdapterAddresses.index; physicalAddress = ipAdapterAddresses.address; addressLength = ipAdapterAddresses.addressLength; type = ipAdapterAddresses.type; operStatus = ipAdapterAddresses.operStatus; speed = (long)ipAdapterAddresses.receiveLinkSpeed; //api specific info ipv6Index = ipAdapterAddresses.ipv6Index; adapterFlags = ipAdapterAddresses.flags; interfaceProperties = new SystemIPInterfaceProperties(fixedInfo, ipAdapterAddresses); }