// // Busca as tarxetas de rede do equipo. // A idea o principio era cambiar a porta de enlace só da tarxeta de rede selecionada // De momento non se emprega para nada public static System.Object[] F_buscar_tarxetas_rede() { NetworkInterface[] TarxetasRede = NetworkInterface.GetAllNetworkInterfaces(); IPInterfaceProperties TarxetasRedePropiedades = null; System.Object[] listaTarxetas = new System.Object[100]; int i = 0; foreach (NetworkInterface AdaptadorRede in TarxetasRede) { TarxetasRedePropiedades = AdaptadorRede.GetIPProperties(); IPInterfaceProperties PropiedadesAdaptadores = AdaptadorRede.GetIPProperties(); GatewayIPAddressInformationCollection direccions = PropiedadesAdaptadores.GatewayAddresses; if ((direccions.Count > 0) && (AdaptadorRede.Description.IndexOf("Microsoft") != 0)) { foreach (GatewayIPAddressInformation direccion in direccions) { listaTarxetas[i] = AdaptadorRede.Description.ToString(); i++; } } } // Adaptase a matriz de saída para evitar que leve valores nulos Array.Resize(ref listaTarxetas, i); return(listaTarxetas); }
public static string GetLocalIPAddress() { string Localip = ""; foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces()) { var defaultGateway = from nics in NetworkInterface.GetAllNetworkInterfaces() from props in nics.GetIPProperties().GatewayAddresses where nics.OperationalStatus == OperationalStatus.Up select props.Address.ToString(); // this sets the default gateway in a variable GatewayIPAddressInformationCollection prop = netInterface.GetIPProperties().GatewayAddresses; if (defaultGateway.First() != null) { IPInterfaceProperties ipProps = netInterface.GetIPProperties(); foreach (UnicastIPAddressInformation addr in ipProps.UnicastAddresses) { // The IP address of the computer is always a bit equal to the default gateway except for the last group of numbers. This splits it and checks //if the ip without the last group matches the default gateway if (addr.Address.ToString().Contains(defaultGateway.First().Remove(defaultGateway.First().LastIndexOf(".")))) { if (Localip == "") // check if the string has been changed before { Localip = addr.Address.ToString(); // put the ip address in a string that you can use. } } } } } return(Localip); }
public void scanAdapter() { NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); list = new List <AdapterListInfo>(); foreach (NetworkInterface adapter in adapters) { if (adapter.NetworkInterfaceType != NetworkInterfaceType.Loopback) { IPInterfaceProperties ipProperties = adapter.GetIPProperties(); //获取IP配置 UnicastIPAddressInformationCollection ipCollection = ipProperties.UnicastAddresses; //获取单播地址集 GatewayIPAddressInformationCollection gwCollection = ipProperties.GatewayAddresses; foreach (UnicastIPAddressInformation ip in ipCollection) { if (ip.Address.AddressFamily == AddressFamily.InterNetwork) { Console.WriteLine("name:" + adapter.Name); Console.WriteLine("type:" + adapter.NetworkInterfaceType); Console.WriteLine("ip:" + ip.Address); Console.WriteLine("ipSubnet::" + ip.IPv4Mask); Console.WriteLine("mac:" + adapter.GetPhysicalAddress()); Console.WriteLine(); list.Add(new AdapterListInfo() { key = adapter.Name + "---" + ip.Address, value = new Adapter(adapter.Name, adapter.NetworkInterfaceType, ip.Address, ip.IPv4Mask, adapter.GetPhysicalAddress()) }); } } } } }
/// <summary> /// 得到网关地址 /// </summary> /// <returns></returns> public static string GetGateway() { string strGateway = ""; //获取所有网卡 NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); foreach (var netWork in nics) { IPInterfaceProperties ip = netWork.GetIPProperties(); GatewayIPAddressInformationCollection gateways = ip.GatewayAddresses; foreach (var gateWay in gateways) { if (IsPingIp(gateWay.Address.ToString())) { strGateway = gateWay.Address.ToString(); break; } } if (strGateway.Length > 0) { break; } } return(strGateway); }
private void Konekcija() { string gatewayadresa = " "; foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces()) { if (ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet) { IPInterfaceProperties adapterProperties = ni.GetIPProperties(); GatewayIPAddressInformationCollection adresses = adapterProperties.GatewayAddresses; if (adresses != null) { foreach (GatewayIPAddressInformation adress in adresses) { //IP = adress.Address.ToString(); if (adress.Address.ToString() == "0.0.0.0") { Debug.Log("konekcija na gateway: " + "-----"); } else { gatewayadresa = adress.Address.ToString(); Debug.Log("konekcija na gateway: " + gatewayadresa); } tekst.text = gatewayadresa; } } else { tekst.text = "Not Connected"; } } //if } // foreach } //metoda konekcija
private static void ShowGatewayIPAddressInformationCollection(GatewayIPAddressInformationCollection collection) { for (var i = 0; i < collection.Count; i++) { Console.WriteLine(" :" + GetIPAddressInfo(collection[i].Address)); } }
private static string FormatGatewayIPAddressList(GatewayIPAddressInformationCollection addressCollection) { if (addressCollection == null) { return("NONE"); } if (addressCollection.Count == 0) { return("NONE"); } string addresses = ""; foreach (GatewayIPAddressInformation ipAddress in addressCollection) { if (string.IsNullOrEmpty(addresses)) { addresses += ipAddress.Address.ToString(); } else { addresses += ", " + ipAddress.Address; } } return(addresses); }
private void ShowGatewayAddresses(GatewayIPAddressInformationCollection gateways) { foreach (GatewayIPAddressInformation gatewayAddress in gateways) { Emit(" Gateway.................................. : {0}", gatewayAddress.Address); } }
internal static GatewayIPAddressInformationCollection ToGatewayIpAddressInformationCollection(IPAddressCollection addresses) { GatewayIPAddressInformationCollection gatewayList = new GatewayIPAddressInformationCollection(); foreach (IPAddress address in addresses) { gatewayList.InternalAdd(new SystemGatewayIPAddressInformation(address)); } return gatewayList; }
public List <string> GetGatewayAddresses() { List <string> result = new List <string>(0); try { NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in adapters) { IPInterfaceProperties adapterProperties = adapter.GetIPProperties(); GatewayIPAddressInformationCollection addresses = adapterProperties.GatewayAddresses; if (addresses.Count > 0) { foreach (GatewayIPAddressInformation address in addresses) { if (!(address.Address.ToString().IndexOf(':') >= 0)) { result.Add(address.Address.ToString()); } } } } } catch (Exception ex) { window.WriteLine("Exception in: Server.GetGatewayAddresses"); window.WriteLine(ex.ToString()); } return(result); }
public xbs_sniffer(ICaptureDevice dev, bool use_special_mac_filter, List<PhysicalAddress> filter_special_macs, bool only_forward_special_macs, xbs_node_list node_list, xbs_nat NAT, GatewayIPAddressInformationCollection gateways, bool exclude_gateway_ips) { this.NAT = NAT; this.pdev_filter_use_special_macs = use_special_mac_filter; if (filter_special_macs!=null) this.pdev_filter_special_macs = filter_special_macs; this.pdev_filter_only_forward_special_macs = only_forward_special_macs; this.pdev_filter_exclude_gatway_ips = exclude_gateway_ips; create_gateway_filter(gateways); injected_macs_hash.Capacity = 40; sniffed_macs_hash.Capacity = 10; sniffed_macs.Capacity = 10; this.node_list = node_list; if (!(dev is SharpPcap.LibPcap.LibPcapLiveDevice)) throw new ArgumentException("pcap caputure device is not a LibPcapLiveDevice"); this.pdev = (SharpPcap.LibPcap.LibPcapLiveDevice)dev; pdev.OnPacketArrival += new PacketArrivalEventHandler(OnPacketArrival); pdev.Open(DeviceMode.Promiscuous, readTimeoutMilliseconds); setPdevFilter(); if (System.Environment.OSVersion.Platform == PlatformID.Win32NT && pdev is SharpPcap.WinPcap.WinPcapDevice) ((SharpPcap.WinPcap.WinPcapDevice)pdev).MinToCopy = 10; xbs_messages.addInfoMessage(" - sniffer created on device " + pdev.Description, xbs_message_sender.SNIFFER); dispatcher_thread = new Thread(new ThreadStart(dispatcher)); dispatcher_thread.IsBackground = true; dispatcher_thread.Priority = ThreadPriority.AboveNormal; dispatcher_thread.Start(); }
public void GetGateWay() { NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface network in nics) { string strTestMac = network.GetPhysicalAddress().ToString(); if (strTestMac == LocalMac) { IPInterfaceProperties ip = network.GetIPProperties(); GatewayIPAddressInformationCollection getways = ip.GatewayAddresses; foreach (var gateway in getways) { if (IsPingIP(gateway.Address.ToString())) { mGateWay = gateway.Address.ToString(); break; } } break; } else { mGateWay = "GateWay is Error"; } } }
private static bool PIAUp() { try { NetworkInterface ni = NetworkInterface.GetAllNetworkInterfaces()[0]; foreach (NetworkInterface NI in NetworkInterface.GetAllNetworkInterfaces()) { if (NI.Description.Contains("TAP-Windows")) { GatewayIPAddressInformationCollection addr = NI.GetIPProperties().GatewayAddresses; if (addr.Count() > 0) { return(true); } } } } catch (Exception e) { ShowBalloon("Network", e); } return(false); }
private string getLocalIP() { string Localip = "?"; foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces()) { var defaultGateway = from nics in NetworkInterface.GetAllNetworkInterfaces() from props in nics.GetIPProperties().GatewayAddresses where nics.OperationalStatus == OperationalStatus.Up select props.Address.ToString(); GatewayIPAddressInformationCollection prop = netInterface.GetIPProperties().GatewayAddresses; if (defaultGateway.First() != null) { IPInterfaceProperties ipProps = netInterface.GetIPProperties(); foreach (UnicastIPAddressInformation addr in ipProps.UnicastAddresses) { if (addr.Address.ToString().Contains(defaultGateway.First().Remove(defaultGateway.First().LastIndexOf(".")))) { if (Localip == "?") // check if the string has been changed before { Localip = addr.Address.ToString(); } } } } } return(Localip); }
internal SystemIPv4InterfaceProperties(FixedInfo fixedInfo, IpAdapterInfo ipAdapterInfo) { this.index = ipAdapterInfo.index; this.routingEnabled = fixedInfo.EnableRouting; this.dhcpEnabled = ipAdapterInfo.dhcpEnabled; this.haveWins = ipAdapterInfo.haveWins; this.gatewayAddresses = ipAdapterInfo.gatewayList.ToIPGatewayAddressCollection(); this.dhcpAddresses = ipAdapterInfo.dhcpServer.ToIPAddressCollection(); IPAddressCollection addresss = ipAdapterInfo.primaryWinsServer.ToIPAddressCollection(); IPAddressCollection addresss2 = ipAdapterInfo.secondaryWinsServer.ToIPAddressCollection(); this.winsServerAddresses = new IPAddressCollection(); foreach (IPAddress address in addresss) { this.winsServerAddresses.InternalAdd(address); } foreach (IPAddress address2 in addresss2) { this.winsServerAddresses.InternalAdd(address2); } SystemIPv4InterfaceStatistics statistics = new SystemIPv4InterfaceStatistics((long) this.index); this.mtu = (uint) statistics.Mtu; if (ComNetOS.IsWin2K) { this.GetPerAdapterInfo(ipAdapterInfo.index); } else { this.dnsAddresses = fixedInfo.DnsAddresses; } }
string Adapters() { string mip = "192.168.0.1"; NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in adapters) { IPInterfaceProperties adapterProperties = adapter.GetIPProperties(); GatewayIPAddressInformationCollection addresses = adapterProperties.GatewayAddresses; if (addresses.Count > 0) { foreach (GatewayIPAddressInformation address in addresses) { if (address.Address.ToString().StartsWith("192.168.")) { mip = address.Address.ToString(); break; } else { mip = address.Address.ToString(); } } } } return(mip); }
// This constructor is for Vista and newer internal SystemIPInterfaceProperties(FixedInfo fixedInfo, IpAdapterAddresses ipAdapterAddresses) { adapterFlags = ipAdapterAddresses.flags; dnsSuffix = ipAdapterAddresses.dnsSuffix; dnsEnabled = fixedInfo.EnableDns; dynamicDnsEnabled = ((ipAdapterAddresses.flags & AdapterFlags.DnsEnabled) > 0); multicastAddresses = SystemMulticastIPAddressInformation.ToMulticastIpAddressInformationCollection( IpAdapterAddress.MarshalIpAddressInformationCollection(ipAdapterAddresses.firstMulticastAddress)); dnsAddresses = IpAdapterAddress.MarshalIpAddressCollection(ipAdapterAddresses.firstDnsServerAddress); anycastAddresses = IpAdapterAddress.MarshalIpAddressInformationCollection( ipAdapterAddresses.firstAnycastAddress); unicastAddresses = SystemUnicastIPAddressInformation.MarshalUnicastIpAddressInformationCollection( ipAdapterAddresses.firstUnicastAddress); winsServersAddresses = IpAdapterAddress.MarshalIpAddressCollection( ipAdapterAddresses.firstWinsServerAddress); gatewayAddresses = SystemGatewayIPAddressInformation.ToGatewayIpAddressInformationCollection( IpAdapterAddress.MarshalIpAddressCollection(ipAdapterAddresses.firstGatewayAddress)); dhcpServers = new IPAddressCollection(); if (ipAdapterAddresses.dhcpv4Server.address != IntPtr.Zero) dhcpServers.InternalAdd(ipAdapterAddresses.dhcpv4Server.MarshalIPAddress()); if (ipAdapterAddresses.dhcpv6Server.address != IntPtr.Zero) dhcpServers.InternalAdd(ipAdapterAddresses.dhcpv6Server.MarshalIPAddress()); if ((adapterFlags & AdapterFlags.IPv4Enabled) != 0) { ipv4Properties = new SystemIPv4InterfaceProperties(fixedInfo, ipAdapterAddresses); } if ((adapterFlags & AdapterFlags.IPv6Enabled) != 0) { ipv6Properties = new SystemIPv6InterfaceProperties(ipAdapterAddresses.ipv6Index, ipAdapterAddresses.mtu, ipAdapterAddresses.zoneIndices); } }
} // end method /// <summary> /// Initialise the base IP address by determining what the gateway address is. /// </summary> /// <returns>True if address was initialised.</returns> private bool initialiseIpBase() { NetworkInterface networkInterface = getActiveEthernetInterface(); if (networkInterface != null) { GatewayIPAddressInformationCollection gateWayAddresses = networkInterface.GetIPProperties().GatewayAddresses; if (gateWayAddresses != null && gateWayAddresses.Count > 0) { string[] parts = gateWayAddresses[0].Address.ToString().Split('.'); if (parts.Length < 3) { ipAddressBase_m = null; } else { ipAddressBase_m = String.Format("{0}.{1}.{2}.", parts[0], parts[1], parts[2]); return(true); } // end if } // end if } // end if return(false); } // end method
private static unsafe GatewayIPAddressInformationCollection GetGatewayAddresses(int interfaceIndex) { HashSet<IPAddress> addressSet = new HashSet<IPAddress>(); if (Interop.Sys.EnumerateGatewayAddressesForInterface((uint)interfaceIndex, (gatewayAddressInfo) => { byte[] ipBytes = new byte[gatewayAddressInfo->NumAddressBytes]; fixed (byte* ipArrayPtr = ipBytes) { Buffer.MemoryCopy(gatewayAddressInfo->AddressBytes, ipArrayPtr, ipBytes.Length, ipBytes.Length); } IPAddress ipAddress = new IPAddress(ipBytes); addressSet.Add(ipAddress); }) == -1) { throw new NetworkInformationException(SR.net_PInvokeError); } GatewayIPAddressInformationCollection collection = new GatewayIPAddressInformationCollection(); foreach (IPAddress address in addressSet) { collection.InternalAdd(new SimpleGatewayIPAddressInformation(address)); } return collection; }
//得到网关地址 public static string GetGateway() { //网关地址 string strGateway = ""; //获取所有网卡 NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces(); //遍历数组 foreach (var netWork in nics) { //单个网卡的IP对象 IPInterfaceProperties ip = netWork.GetIPProperties(); //获取该IP对象的网关 GatewayIPAddressInformationCollection gateways = ip.GatewayAddresses; foreach (var gateWay in gateways) { //如果能够Ping通网关 if (IsPingIP(gateWay.Address.ToString())) { //得到网关地址 strGateway = gateWay.Address.ToString(); //跳出循环 break; } } //如果已经得到网关地址 if (strGateway.Length > 0) { //跳出循环 break; } } //返回网关地址 return(strGateway); }
public string getDefaultGateway() { try { string DefaultGateway = EMPTY; NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in adapters) { if (DefaultGateway != EMPTY) { break; } IPInterfaceProperties adapterProperties = adapter.GetIPProperties(); GatewayIPAddressInformationCollection addresses = adapterProperties.GatewayAddresses; if (addresses.Count > 0) { foreach (GatewayIPAddressInformation address in addresses) { DefaultGateway = address.Address.ToString(); } } } return(DefaultGateway); } catch (Exception ex) { return(ExceptionHandling.getExceptionMessage(ex)); } }
public static string GetGatewayAddresses() { const string ip = ""; NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in adapters) { if (adapter.OperationalStatus == OperationalStatus.Up) { IPInterfaceProperties adapterProperties = adapter.GetIPProperties(); GatewayIPAddressInformationCollection addresses = adapterProperties.GatewayAddresses; if (addresses.Count > 0) { foreach (GatewayIPAddressInformation ia in addresses) { if (ia.Address.AddressFamily == AddressFamily.InterNetwork) { return(ia.Address.ToString()); } } } } } return(ip); }
public static List <NCIInfo> GetNCIInfoList() { if (s_nicInfoListCache.Count != 0) { //考虑到网卡不是一直变,用缓存提高速度 return(s_nicInfoListCache); } NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in adapters) { if (adapter.NetworkInterfaceType != NetworkInterfaceType.Ethernet) { continue; } if (!adapter.Supports(NetworkInterfaceComponent.IPv4)) { continue; } IPInterfaceProperties ipIntProps = adapter.GetIPProperties(); IPAddress ipAddr = null; IPAddress ipMaskAddr = null; UnicastIPAddressInformationCollection ipAddrInfos = ipIntProps.UnicastAddresses; foreach (UnicastIPAddressInformation ipAddrInfo in ipAddrInfos) { IPAddress ipAddrTmp = ipAddrInfo.Address; if (ipAddrTmp.AddressFamily == AddressFamily.InterNetwork) { ipAddr = ipAddrTmp; ipMaskAddr = ipAddrInfo.IPv4Mask; break; } } GatewayIPAddressInformationCollection gwIps = ipIntProps.GatewayAddresses; IPAddress gwIp = null; if (gwIps.Count != 0) { gwIp = gwIps[0].Address; } if (ipAddr != null) { NCIInfo addr = new NCIInfo { Address = ipAddr, Mask = ipMaskAddr, GateWay = gwIp, MAC = adapter.GetPhysicalAddress().ToString(), Type = GetNetworkInterfaceType(adapter), Name = adapter.Name, }; s_nicInfoListCache.Add(addr); } } return(s_nicInfoListCache); }
private void AddToTableIPGatewyAddress(string name, GatewayIPAddressInformationCollection value) { for (int x = 0; x <= value.Count - 1; x++) { table.Rows.Add(); table.Rows[table.Rows.Count - 1][0] = name; table.Rows[table.Rows.Count - 1][1] = value[x].Address; } }
public void RefreshInfos() { listAdapter.Clear(); adapters = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in adapters) { var ipProp = adapter.GetIPProperties(); foreach (UnicastIPAddressInformation ip in ipProp.UnicastAddresses) { if ((adapter.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet) && ip.Address.AddressFamily == AddressFamily.InterNetwork) // { IPInterfaceProperties adapterProp = adapter.GetIPProperties(); IPv4InterfaceProperties adapterPropV4 = adapterProp.GetIPv4Properties(); GatewayIPAddressInformationCollection gate = adapter.GetIPProperties().GatewayAddresses; listAdapter.Add(new AdapterObject { Name = adapter.Name, Description = adapter.Description, NetworkInterfaceType = adapter.NetworkInterfaceType.ToString(), PhysicalAddress = adapter.GetPhysicalAddress().ToString(), IsReceiveOnly = adapter.IsReceiveOnly, SupportMulticast = adapter.SupportsMulticast, IsOperationalStatusUp = adapter.OperationalStatus == OperationalStatus.Up, Speed = adapter.Speed, IpAddress = ip.Address.ToString(), SubnetMask = ip.IPv4Mask.ToString(), Gateway = gate.Any() ? gate.FirstOrDefault().Address.ToString() : "", DnsSuffix = adapterProp.DnsSuffix, IsDnsEnabled = adapterProp.IsDnsEnabled, IsDynamicDnsEnabled = adapterProp.IsDynamicDnsEnabled, Index = adapterPropV4.Index, Mtu = adapterPropV4.Mtu, IsAutomaticPrivateAddressingActive = adapterPropV4.IsAutomaticPrivateAddressingActive, IsAutomaticPrivateAddressingEnabled = adapterPropV4.IsAutomaticPrivateAddressingEnabled, IsForwardingEnabled = adapterPropV4.IsForwardingEnabled, UsesWins = adapterPropV4.UsesWins, IsDHCPEnabled = adapterPropV4.IsDhcpEnabled, DHCPServer = adapterProp.DhcpServerAddresses.FirstOrDefault() != null ? adapterProp.DhcpServerAddresses.FirstOrDefault().ToString() : "", DNSServer1 = (adapterProp.DnsAddresses.Count > 0 && adapterProp.DnsAddresses[0].AddressFamily == AddressFamily.InterNetwork) ? adapterProp.DnsAddresses[0]?.ToString() : "", DNSServer2 = (adapterProp.DnsAddresses.Count > 1 && adapterProp.DnsAddresses[1].AddressFamily == AddressFamily.InterNetwork) ? adapterProp.DnsAddresses[1]?.ToString() : "", Internet = adapter.GetIPv4Statistics().BytesReceived > 0 && adapter.GetIPv4Statistics().BytesSent > 0 }); } } } }
/// <summary> /// Set the ip address properties of the <see cref="NetworkConnectionProperties"/> instance. /// </summary> /// <param name="properties">Element of the type <see cref="IPInterfaceProperties"/>.</param> private void SetIpProperties(IPInterfaceProperties properties) { DateTime t = DateTime.Now; UnicastIPAddressInformationCollection ipList = properties.UnicastAddresses; GatewayIPAddressInformationCollection gwList = properties.GatewayAddresses; DhcpServers = properties.DhcpServerAddresses; DnsServers = properties.DnsAddresses; WinsServers = properties.WinsServersAddresses; for (int i = 0; i < ipList.Count; i++) { IPAddress ip = ipList[i].Address; if (ip.AddressFamily == AddressFamily.InterNetwork) { IPv4 = ip.ToString(); IPv4Mask = ipList[i].IPv4Mask.ToString(); } else if (ip.AddressFamily == AddressFamily.InterNetworkV6) { if (string.IsNullOrEmpty(IPv6Primary)) { IPv6Primary = ip.ToString(); } if (ip.IsIPv6LinkLocal) { IPv6LinkLocal = ip.ToString(); } else if (ip.IsIPv6SiteLocal) { IPv6SiteLocal = ip.ToString(); } else if (ip.IsIPv6UniqueLocal) { IPv6UniqueLocal = ip.ToString(); } else if (ipList[i].SuffixOrigin == SuffixOrigin.Random) { IPv6Temporary = ip.ToString(); } else { IPv6Global = ip.ToString(); } } } for (int i = 0; i < gwList.Count; i++) { Gateways.Add(gwList[i].Address); } Debug.Print($"time for getting ips: {DateTime.Now - t}"); }
/// <summary> /// 获取所有适配器的状态、属性 /// </summary> /// <returns>每个适配器的泛型</returns> public static List <TSAdapter> GetAdapters() { List <TSAdapter> back = new List <TSAdapter>(); NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter"); ManagementObjectCollection wmiadps = searcher.Get(); foreach (NetworkInterface adapter in adapters) { TSAdapter adp = new TSAdapter(); adp.Name = adapter.Name; adp.Description = adapter.Description; adp.Status = adapter.OperationalStatus; adp.Type = adapter.NetworkInterfaceType; IPInterfaceProperties adapterProperties = adapter.GetIPProperties(); //网关 GatewayIPAddressInformationCollection addresses = adapterProperties.GatewayAddresses; if (addresses.Count > 0) { adp.Gateway = addresses[0].Address.ToString(); } //DNS IPAddressCollection dnsServers = adapterProperties.DnsAddresses; if (dnsServers.Count > 0) { foreach (IPAddress dns in dnsServers) { adp.DNS.Add(dns.ToString()); } } //WMI中的数据 foreach (ManagementObject mo in wmiadps) { if ((string)mo.GetPropertyValue("NetConnectionID") == adp.Name) { //接口(XP没有该属性!) try { adp.Interface = (UInt32)mo.GetPropertyValue("InterfaceIndex"); } catch { adp.Interface = null; } //服务名称 adp.ServiceName = (string)mo.GetPropertyValue("ServiceName"); } } back.Add(adp); } return(back); }
public LinuxIPInterfaceProperties(LinuxNetworkInterface lni) : base(lni) { _linuxNetworkInterface = lni; _gatewayAddresses = GetGatewayAddresses(); _dhcpServerAddresses = GetDhcpServerAddresses(); _winsServerAddresses = GetWinsServerAddresses(); _ipv4Properties = new LinuxIPv4InterfaceProperties(lni); _ipv6Properties = new LinuxIPv6InterfaceProperties(lni); }
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { FriendlyName = comboBox1.SelectedItem.ToString(); SelectedInterface = Nics.FirstOrDefault(x => x.Name == FriendlyName); materialLabel10.Text = SelectedInterface?.NetworkInterfaceType.ToString() ?? "Error"; //Show local IP foreach (var IP in SelectedInterface.GetIPProperties().UnicastAddresses) { if (IP.Address.AddressFamily == AddressFamily.InterNetwork) //Grab the IPV4 address of the selected NIC { materialLabel4.Text = IP?.Address?.ToString() ?? ""; Properties.Settings.Default.NetMask = IP.IPv4Mask.ToString(); Properties.Settings.Default.NetSize = IP.IPv4Mask.ToString().Count(c => c == '0'); break; } } //Show local MAC materialLabel5.Text = SelectedInterface? .GetPhysicalAddress()? .ToString().Insert(2, "-").Insert(5, "-").Insert(8, "-").Insert(11, "-").Insert(14, "-") ?? ""; //Show gateway IP GatewayIPAddressInformationCollection addresses = null; if ((addresses = SelectedInterface.GetIPProperties().GatewayAddresses).Count == 0) { materialLabel7.Text = ""; materialFlatButton1.Enabled = false; } else { foreach (var gateway in addresses) { if (gateway.Address.AddressFamily == AddressFamily.InterNetwork) { materialLabel7.Text = gateway?.Address?.ToString() ?? ""; materialFlatButton1.Enabled = true; break; } } } //Show connected wireless network (if there is one) if (SelectedInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) { materialLabel12.Text = GetConnectedNetworks(SelectedInterface) ?? ""; } else { materialLabel12.Text = ""; } }
static IPAddress DefaultGatewayAddress(IPInterfaceProperties properties) { GatewayIPAddressInformationCollection addresses = properties.GatewayAddresses; IPAddress result = null; if (addresses.Any()) { result = addresses.FirstOrDefault().Address; } return(result); }
internal static GatewayIPAddressInformationCollection ToGatewayIpAddressInformationCollection(IPAddressCollection addresses) { GatewayIPAddressInformationCollection gatewayList = new GatewayIPAddressInformationCollection(); foreach (IPAddress address in addresses) { gatewayList.InternalAdd(new SystemGatewayIPAddressInformation(address)); } return(gatewayList); }
/// <summary> /// 获取本机的Ip地址 /// </summary> /// <returns></returns> public static string GetLocalIPAddress() { if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) { return(string.Empty); } var withGetWayAddress = string.Empty; var localIpAddress = new List <string>(); foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces()) { if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211 || ni.NetworkInterfaceType == NetworkInterfaceType.Ethernet) { var ipProperties = ni.GetIPProperties(); foreach (UnicastIPAddressInformation ip in ipProperties.UnicastAddresses) { if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { var ipStr = ip.Address.ToString(); if (string.IsNullOrEmpty(ipStr)) { continue; } localIpAddress.Add(ipStr); GatewayIPAddressInformationCollection gateways = ipProperties.GatewayAddresses; var getway = gateways.FirstOrDefault(); if (getway != null && getway.Address != null) { var getwayAddress = getway.Address.ToString(); if (!string.IsNullOrEmpty(getwayAddress)) { withGetWayAddress = ipStr; } } } } } } //IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName()); //var ipaddress = host // .AddressList // .FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork); //if (ipaddress == null) return string.Empty; if (!string.IsNullOrEmpty(withGetWayAddress)) { return(withGetWayAddress); } return(localIpAddress.FirstOrDefault()); }
public void GatewayTest() { INetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces(); Assert.IsTrue(interfaces.Length > 0, "No interfaces available to test"); // make sure DHCP is not enabled bool wasDhcpEnabled = interfaces[0].GetIPProperties().GetIPv4Properties().IsDhcpEnabled; if (wasDhcpEnabled) { interfaces[0].GetIPProperties().GetIPv4Properties().IsDhcpEnabled = false; interfaces[0].Rebind(); } GatewayIPAddressInformationCollection gateways = interfaces[0].GetIPProperties().GatewayAddresses; if (gateways.Count == 0) { gateways.Add(IPAddress.Parse("10.1.1.1")); interfaces[0].Rebind(); } IPAddress oldGateway = gateways[0].Address; IPAddress newGateway = IPAddress.Parse("10.0.0.0"); // make sure it's new if (newGateway.Equals(oldGateway)) { newGateway = IPAddress.Parse("10.1.1.1"); } gateways[0].Address = newGateway; interfaces[0].Rebind(); // no exception == pass // read gateways = interfaces[0].GetIPProperties().GatewayAddresses; Assert.IsTrue(newGateway.Equals(gateways[0].Address), string.Format("Gateway read back ({0}) did not match what we wrote in ({1})", gateways[0].Address, newGateway.Address)); // restore if (wasDhcpEnabled) { interfaces[0].GetIPProperties().GetIPv4Properties().IsDhcpEnabled = true; } else { gateways[0].Address = oldGateway; interfaces[0].Rebind(); } }
public static void DisplayNetworkInfo() { NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in adapters) { IPInterfaceProperties adapterProperties = adapter.GetIPProperties(); GatewayIPAddressInformationCollection Gatewayaddresses = adapterProperties.GatewayAddresses; IPAddressCollection dhcpServers = adapterProperties.DhcpServerAddresses; IPAddressCollection dnsServers = adapterProperties.DnsAddresses; Console.WriteLine("네트워크 카드 : " + adapter.Description); Console.WriteLine(" Physical Address ............ : " + adapter.GetPhysicalAddress()); Console.WriteLine(" IP Address .................. : " + Get_MyIP()); if (Gatewayaddresses.Count > 0) { foreach (GatewayIPAddressInformation address in Gatewayaddresses) { Console.WriteLine(" Gateway Address ........... : " + address.Address.ToString()); } } if (dhcpServers.Count > 0) { foreach (IPAddress dhcp in dhcpServers) { Console.WriteLine(" DHCP Servers ............ : " + dhcp.ToString()); } } if (dnsServers.Count > 0) { foreach (IPAddress dns in dnsServers) { Console.WriteLine(" DNS Servers ............. : " + dns.ToString()); } } } while (true) { } }
public void AtLeastOneGatewayAddress() { int numGatewayAddresses = 0; NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces(); foreach (NetworkInterface adapter in adapters) { IPInterfaceProperties adapterProperties = adapter.GetIPProperties(); GatewayIPAddressInformationCollection gatewayAddresses = adapterProperties.GatewayAddresses; numGatewayAddresses += gatewayAddresses.Count; } Assert.IsTrue(numGatewayAddresses > 0); }
// ReSharper disable once InconsistentNaming /// <summary> /// Is the IPAddress IPv4 or default. /// </summary> /// <param name="gatewayIPAddressInformationCollection">The gateway ip address information collection.</param> /// <returns>GatewayIPAddressInformation.</returns> /// <autogeneratedoc /> public static GatewayIPAddressInformation IPv4OrDefault(this GatewayIPAddressInformationCollection gatewayIPAddressInformationCollection) { foreach (var gatewayIPAddressInformation in gatewayIPAddressInformationCollection) { // Looking for an IPv4 Gateway Address if (gatewayIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork) { return(gatewayIPAddressInformation); } } return(null); }
internal GatewayIPAddressInformationCollection ToIPGatewayAddressCollection() { IpAddrString str = this; GatewayIPAddressInformationCollection informations = new GatewayIPAddressInformationCollection(); if (str.IpAddress.Length != 0) { informations.InternalAdd(new SystemGatewayIPAddressInformation(IPAddress.Parse(str.IpAddress))); } while (str.Next != IntPtr.Zero) { str = (IpAddrString) Marshal.PtrToStructure(str.Next, typeof(IpAddrString)); if (str.IpAddress.Length != 0) { informations.InternalAdd(new SystemGatewayIPAddressInformation(IPAddress.Parse(str.IpAddress))); } } return informations; }
internal SystemIPInterfaceProperties(Interop.IpHlpApi.FIXED_INFO fixedInfo, Interop.IpHlpApi.IpAdapterAddresses ipAdapterAddresses) { _adapterFlags = ipAdapterAddresses.flags; _dnsSuffix = ipAdapterAddresses.dnsSuffix; _dnsEnabled = fixedInfo.enableDns; _dynamicDnsEnabled = ((ipAdapterAddresses.flags & Interop.IpHlpApi.AdapterFlags.DnsEnabled) > 0); _multicastAddresses = SystemMulticastIPAddressInformation.ToMulticastIpAddressInformationCollection( Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressInformationCollection(ipAdapterAddresses.firstMulticastAddress)); _dnsAddresses = Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressCollection(ipAdapterAddresses.firstDnsServerAddress); _anycastAddresses = Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressInformationCollection( ipAdapterAddresses.firstAnycastAddress); _unicastAddresses = SystemUnicastIPAddressInformation.MarshalUnicastIpAddressInformationCollection( ipAdapterAddresses.firstUnicastAddress); _winsServersAddresses = Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressCollection( ipAdapterAddresses.firstWinsServerAddress); _gatewayAddresses = SystemGatewayIPAddressInformation.ToGatewayIpAddressInformationCollection( Interop.IpHlpApi.IpAdapterAddress.MarshalIpAddressCollection(ipAdapterAddresses.firstGatewayAddress)); _dhcpServers = new InternalIPAddressCollection(); if (ipAdapterAddresses.dhcpv4Server.address != IntPtr.Zero) { _dhcpServers.InternalAdd(ipAdapterAddresses.dhcpv4Server.MarshalIPAddress()); } if (ipAdapterAddresses.dhcpv6Server.address != IntPtr.Zero) { _dhcpServers.InternalAdd(ipAdapterAddresses.dhcpv6Server.MarshalIPAddress()); } if ((_adapterFlags & Interop.IpHlpApi.AdapterFlags.IPv4Enabled) != 0) { _ipv4Properties = new SystemIPv4InterfaceProperties(fixedInfo, ipAdapterAddresses); } if ((_adapterFlags & Interop.IpHlpApi.AdapterFlags.IPv6Enabled) != 0) { _ipv6Properties = new SystemIPv6InterfaceProperties(ipAdapterAddresses.ipv6Index, ipAdapterAddresses.mtu, ipAdapterAddresses.zoneIndices); } }
// /proc/net/route contains some information about gateway addresses, // and seperates the information about by each interface. public GatewayIPAddressInformationCollection GetGatewayAddresses() { GatewayIPAddressInformationCollection collection = new GatewayIPAddressInformationCollection(); // Columns are as follows (first-line header): // Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT string[] fileLines = File.ReadAllLines(NetworkFiles.Ipv4RouteFile); foreach (string line in fileLines) { if (line.StartsWith(_linuxNetworkInterface.Name)) { StringParser parser = new StringParser(line, '\t', skipEmpty: true); parser.MoveNext(); parser.MoveNextOrFail(); string gatewayIPHex = parser.MoveAndExtractNext(); long addressValue = Convert.ToInt64(gatewayIPHex, 16); IPAddress address = new IPAddress(addressValue); collection.InternalAdd(new SimpleGatewayIPAddressInformation(address)); } } return collection; }
static void AddSubsequently (IntPtr head, GatewayIPAddressInformationCollection col) { Win32_IP_ADDR_STRING a; for (IntPtr p = head; p != IntPtr.Zero; p = a.Next) { a = (Win32_IP_ADDR_STRING) Marshal.PtrToStructure (p, typeof (Win32_IP_ADDR_STRING)); col.InternalAdd (new SystemGatewayIPAddressInformation (IPAddress.Parse (a.IpAddress))); } }
public OsxIpInterfaceProperties(OsxNetworkInterface oni, int mtu) : base(oni) { _ipv4Properties = new OsxIPv4InterfaceProperties(oni, mtu); _ipv6Properties = new OsxIPv6InterfaceProperties(oni, mtu); _gatewayAddresses = GetGatewayAddresses(oni.Index); }
private void create_gateway_filter( GatewayIPAddressInformationCollection gateways ) { if (gateways == null) return; if (gateways.Count == 0) return; String[] ips = new String[gateways.Count]; for (int i = 0; i < gateways.Count; i++) { ips[i] = gateways[i].Address.ToString(); gateway_ips.Add(gateways[i].Address); } pdev_filter_gateways = "host " + String.Join(" or host ", ips); }