public WrapNetworkInterface(NetworkInterface ni) { _networkInterface = ni; _physicalAddress = ni.GetPhysicalAddress().ToString(); _lastRestart = DateTime.Now; var search = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter WHERE GUID = '" + _networkInterface.Id + "'"); foreach (ManagementObject mo in search.Get()) _managementObject = mo; _physicalAdapter = false; try { _physicalAdapter = (bool)_managementObject["PhysicalAdapter"]; } catch { // exception if the value doesn't exist, only want True's anyway.. } }
public NetInterface(NetworkInterface adapterIn) { // set up the adapter adapter = adapterIn; stats = adapter.GetIPv4Statistics(); // set up the logging logPath = Path.Combine("logs", Path.Combine(adapter.Description, adapter.GetPhysicalAddress().ToString(), adapter.Id)); logHandler = new LogHandler(logPath); loadDataInstant(DateTime.UtcNow.Ticks); // set up the data tracking dataTransferStart = currentTicks(); bytesInSession = stats.BytesReceived; bytesOutSession = stats.BytesSent; properties = adapter.GetIPProperties(); //Console.WriteLine(adapter.Name + " " + adapter.Description + " " + adapter.OperationalStatus); Tracker = new Tracker(logHandler); }
private string GetDeviceInfo(NetworkInterface adapter) { if (adapter == null) { return String.Empty; } IPInterfaceProperties properties = adapter.GetIPProperties(); StringBuilder infoBuilder = new StringBuilder(); infoBuilder.Append(adapter.Description + "\n"); infoBuilder.Append("=================================================\n"); infoBuilder.AppendFormat(" ID ......................... : {0}\n", adapter.Id); infoBuilder.AppendFormat(" Name ....................... : {0}\n", adapter.Name); infoBuilder.AppendFormat(" Interface type ............. : {0}\n", adapter.NetworkInterfaceType); infoBuilder.AppendFormat(" Physical Address ........... : {0}\n", BitConverter.ToString(adapter.GetPhysicalAddress().GetAddressBytes())); infoBuilder.AppendFormat(" Operational status ......... : {0}\n", adapter.OperationalStatus); infoBuilder.AppendFormat(" Speed ...................... : {0} Mb/s\n", adapter.Speed / 1000000); string versions = String.Empty; // Create a display string for the supported IP versions. if (adapter.Supports(NetworkInterfaceComponent.IPv4)) { versions = "IPv4"; } if (adapter.Supports(NetworkInterfaceComponent.IPv6)) { if (versions.Length > 0) { versions += " "; } versions += "IPv6"; } infoBuilder.AppendFormat(" IP version ................. : {0}\n", versions); infoBuilder.Append(GetIPAddresses(properties)); // The following information is not useful for loopback adapters. if (adapter.NetworkInterfaceType == NetworkInterfaceType.Loopback) { return infoBuilder.ToString(); } infoBuilder.AppendFormat(" DNS suffix ................. : {0}\n", properties.DnsSuffix); if (adapter.Supports(NetworkInterfaceComponent.IPv4)) { IPv4InterfaceProperties ipv4 = properties.GetIPv4Properties(); infoBuilder.AppendFormat(" Index ...................... : {0}\n", ipv4.Index); infoBuilder.AppendFormat(" MTU ........................ : {0}\n", ipv4.Mtu); infoBuilder.AppendFormat(" APIPA active ............... : {0}\n", ipv4.IsAutomaticPrivateAddressingActive); infoBuilder.AppendFormat(" APIPA enabled .............. : {0}\n", ipv4.IsAutomaticPrivateAddressingEnabled); infoBuilder.AppendFormat(" DHCP enabled ............... : {0}\n", ipv4.IsDhcpEnabled); infoBuilder.AppendFormat(" Forwarding enabled.......... : {0}\n", ipv4.IsForwardingEnabled); infoBuilder.AppendFormat(" Uses WINS .................. : {0}\n", ipv4.UsesWins); if (ipv4.UsesWins) { IPAddressCollection winsServers = properties.WinsServersAddresses; if (winsServers.Count > 0) { foreach (IPAddress winsServer in winsServers) { infoBuilder.AppendFormat(" WINS Server ................ : {0}\n", winsServer); } } } } if (adapter.Supports(NetworkInterfaceComponent.IPv6)) { IPv6InterfaceProperties ipv6 = properties.GetIPv6Properties(); infoBuilder.AppendFormat(" Index ...................... : {0}\n", ipv6.Index); infoBuilder.AppendFormat(" MTU ........................ : {0}\n", ipv6.Mtu); } infoBuilder.AppendFormat(" DNS enabled ................ : {0}\n", properties.IsDnsEnabled); infoBuilder.AppendFormat(" Dynamically configured DNS . : {0}\n", properties.IsDynamicDnsEnabled); infoBuilder.AppendFormat(" Receive Only ............... : {0}\n", adapter.IsReceiveOnly); infoBuilder.AppendFormat(" Multicast .................. : {0}\n", adapter.SupportsMulticast); return infoBuilder.ToString(); }
static string ToString(NetworkInterface nic) { return "Name=" + nic.Name + "; NetworkInterfaceType=" + nic.NetworkInterfaceType.ToString() + "; Id=" + nic.Id + "; Description=" + nic.Description + "; OperationalStatus=" + nic.OperationalStatus.ToString() + "; GetPhysicalAddress()=" + nic.GetPhysicalAddress().ToString() + "; Speed=" + nic.Speed.ToString() + "; SupportsMulticast=" + nic.SupportsMulticast.ToString() // + "; LoopbackInterfaceIndex=" + nic.LoopbackInterfaceIndex.ToString() + "; IsReceiveOnly=" + nic.IsReceiveOnly.ToString() // + "; GetIsNetworkAvailable=" + nic.GetIsNetworkAvailable().ToString() ; }
/// <summary> /// Initializes a new instance of the <see cref="IfPhysAddress"/> class. /// </summary> /// <param name="index">The index.</param> /// <param name="networkInterface">The network interface.</param> public IfPhysAddress(int index, NetworkInterface networkInterface) : base("1.3.6.1.2.1.2.2.1.6.{0}", index) { _data = new OctetString(networkInterface.GetPhysicalAddress().GetAddressBytes()); }
/// <summary> /// Generate LLDP packet for adapter /// </summary> /// <param name="adapter"></param> private Packet CreateLLDPPacket(NetworkInterface adapter, PacketInfo pinfo) { Debug.IndentLevel = 2; PhysicalAddress MACAddress = adapter.GetPhysicalAddress(); IPInterfaceProperties ipProperties = adapter.GetIPProperties(); IPv4InterfaceProperties ipv4Properties = null; // Ipv4 IPv6InterfaceProperties ipv6Properties = null;// Ipv6 // IPv6 if (adapter.Supports(NetworkInterfaceComponent.IPv6)) { try { ipv6Properties = ipProperties.GetIPv6Properties(); } catch (NetworkInformationException e) { // Adapter doesn't probably have IPv6 enabled Debug.WriteLine(e.Message, EventLogEntryType.Warning); } } // IPv4 if (adapter.Supports(NetworkInterfaceComponent.IPv4)) { try { ipv4Properties = ipProperties.GetIPv4Properties(); } catch (NetworkInformationException e) { // Adapter doesn't probably have IPv4 enabled Debug.WriteLine(e.Message, EventLogEntryType.Warning); } } // System description Dictionary<string, string> systemDescription = new Dictionary<string, string>(); systemDescription.Add("OS", pinfo.OperatingSystem); systemDescription.Add("Ver", pinfo.OperatingSystemVersion); systemDescription.Add("User", pinfo.Username); systemDescription.Add("Uptime", pinfo.Uptime); // Port description Dictionary<string, string> portDescription = new Dictionary<string, string>(); // adapter.Description is for example "Intel(R) 82579V Gigabit Network Connection" // Registry: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards\<number>\Description value portDescription.Add("Vendor", adapter.Description); /* adapter.Id is GUID and can be found in several places: In this example it is "{87423023-7191-4C03-A049-B8E7DBB36DA4}" Registry: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion - \NetworkCards\<number>\ServiceName value (in same tree as adapter.Description!) - \NetworkList\Nla\Cache\Intranet\<adapter.Id> key - \NetworkList\Nla\Cache\Intranet\<domain>\<adapter.Id> key Registry: HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows NT\CurrentVersion - \NetworkCards\<number>\ServiceName value Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}\0007 {4D36E972-E325-11CE-BFC1-08002BE10318} == Network and Sharing Center -> viewing adapter's "Properties" and selecting "Device class GUID" from dropdown menu {4D36E972-E325-11CE-BFC1-08002BE10318}\0007 == Network and Sharing Center -> viewing adapter's "Properties" and selecting "Driver key" from dropdown menu - \NetCfgInstanceId value - \Linkage\Export value (part of) - \Linkage\FilterList value (part of) - \Linkage\RootDevice value Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\DeviceClasses\{ad498944-762f-11d0-8dcb-00c04fc3358c}\##?#PCI#VEN_8086&DEV_1503&SUBSYS_849C1043&REV_06#3&11583659&0&C8#{ad498944-762f-11d0-8dcb-00c04fc3358c}\#{87423023-7191-4C03-A049-B8E7DBB36DA4}\SymbolicLink value (part of) Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{ 4D36E972 - E325 - 11CE - BFC1 - 08002BE10318}\{ 87423023 - 7191 - 4C03 - A049 - B8E7DBB36DA4} Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\{ACB3F7A0-2E45-4435-854A-A4E120477E1D}\Connection\Name value (part of) Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\{ 87423023 - 7191 - 4C03 - A049 - B8E7DBB36DA4} Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\iphlpsvc\Parameters\Isatap\{ ACB3F7A0 - 2E45 - 4435 - 854A - A4E120477E1D}\InterfaceName value (part of) Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\<various names>\Linkage - \Bind value (part of) - \Export value (part of) - \Route value (part of) Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\NetBT\Parameters\Interfaces\Tcpip_{87423023-7191-4C03-A049-B8E7DBB36DA4} Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Psched\Parameters\NdisAdapters\{87423023-7191-4C03-A049-B8E7DBB36DA4} Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\RemoteAccess\Interfaces\<number>\InterfaceName value Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Adapters\{87423023-7191-4C03-A049-B8E7DBB36DA4}\IpConfig value (part of) IPv4 information: Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\{87423023-7191-4C03-A049-B8E7DBB36DA4} IPv6 information: Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\TCPIP6\Parameters\Interfaces\{87423023-7191-4c03-a049-b8e7dbb36da4} Registry: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\WfpLwf\Parameters\NdisAdapters\{87423023-7191-4C03-A049-B8E7DBB36DA4} */ portDescription.Add("ID", adapter.Id); // Gateway if (ipProperties.GatewayAddresses.Count > 0) { portDescription.Add("GW", String.Join(", ", ipProperties.GatewayAddresses.Select(i => i.Address.ToString()).ToArray())); } else { portDescription.Add("GW", "-"); } // CIDR if (ipProperties.UnicastAddresses.Count > 0) { int[] mask = ipProperties.UnicastAddresses .Where( w => w.IPv4Mask.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork ) .Select(x => getCIDRFromIPMaskAddress(x.IPv4Mask)) .ToArray() ; portDescription.Add("CIDR", String.Join(", ", mask)); } else { portDescription.Add("CIDR", "-"); } // DNS server(s) if (ipProperties.DnsAddresses.Count > 0) { portDescription.Add("DNS", String.Join(", ", ipProperties.DnsAddresses.Select(i => i.ToString()).ToArray())); } else { portDescription.Add("DNS", "-"); } // DHCP server if (ipProperties.DhcpServerAddresses.Count > 0) { portDescription.Add("DHCP", String.Join(", ", ipProperties.DhcpServerAddresses.Select(i => i.ToString()).ToArray())); } else { portDescription.Add("DHCP", "-"); } // WINS server(s) if (ipProperties.WinsServersAddresses.Count > 0) { portDescription.Add("WINS", String.Join(", ", ipProperties.WinsServersAddresses.Select(i => i.ToString()).ToArray())); } // Link speed portDescription.Add("Speed", ReadableSize(adapter.Speed) + "ps"); // Capabilities enabled List<CapabilityOptions> capabilitiesEnabled = new List<CapabilityOptions>(); capabilitiesEnabled.Add(CapabilityOptions.StationOnly); if (ipv4Properties.IsForwardingEnabled) { capabilitiesEnabled.Add(CapabilityOptions.Router); } ushort expectedSystemCapabilitiesCapability = GetCapabilityOptionsBits(GetCapabilityOptions()); ushort expectedSystemCapabilitiesEnabled = GetCapabilityOptionsBits(capabilitiesEnabled); // Constuct LLDP packet LLDPPacket lldpPacket = new LLDPPacket(); lldpPacket.TlvCollection.Add(new ChassisID(ChassisSubTypes.MACAddress, MACAddress)); lldpPacket.TlvCollection.Add(new PortID(PortSubTypes.LocallyAssigned, System.Text.Encoding.UTF8.GetBytes(adapter.Name))); lldpPacket.TlvCollection.Add(new TimeToLive(120)); lldpPacket.TlvCollection.Add(new PortDescription(CreateTlvString(portDescription))); lldpPacket.TlvCollection.Add(new SystemName(Environment.MachineName)); lldpPacket.TlvCollection.Add(new SystemDescription(CreateTlvString(systemDescription))); lldpPacket.TlvCollection.Add(new SystemCapabilities(expectedSystemCapabilitiesCapability, expectedSystemCapabilitiesEnabled)); // Management var managementAddressObjectIdentifier = "Management"; // Add management IPv4 address(es) if (null != ipv4Properties) { foreach (UnicastIPAddressInformation ip in ipProperties.UnicastAddresses) { if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { lldpPacket.TlvCollection.Add(new ManagementAddress(new NetworkAddress(ip.Address), InterfaceNumbering.SystemPortNumber, Convert.ToUInt32(ipv4Properties.Index), managementAddressObjectIdentifier)); } } } // Add management IPv6 address(es) if (null != ipv6Properties) { foreach (UnicastIPAddressInformation ip in ipProperties.UnicastAddresses) { if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) { lldpPacket.TlvCollection.Add(new ManagementAddress(new NetworkAddress(ip.Address), InterfaceNumbering.SystemPortNumber, Convert.ToUInt32(ipv6Properties.Index), managementAddressObjectIdentifier)); } } } // == Organization specific TLVs // Ethernet lldpPacket.TlvCollection.Add(new OrganizationSpecific(new byte[] { 0x0, 0x12, 0x0f }, 5, new byte[] { 0x5 })); var expectedOrganizationUniqueIdentifier = new byte[3] { 0, 0, 0 }; var expectedOrganizationSpecificBytes = new byte[] { 0, 0, 0, 0 }; //int orgSubType = 0; // IPv4 Information: //if (null != ipv4Properties) //{ // lldpPacket.TlvCollection.Add((new StringTLV(TLVTypes.OrganizationSpecific, String.Format("IPv4 DHCP: {0}", ipv4Properties.IsDhcpEnabled.ToString())))); //lldpPacket.TlvCollection.Add(new OrganizationSpecific(expectedOrganizationSpecificBytes, new StringTLV(), System.Text.Encoding.UTF8.GetBytes(String.Format("IPv4 DHCP: {0}", ipv4Properties.IsDhcpEnabled.ToString())))); //} // End of LLDP packet lldpPacket.TlvCollection.Add(new EndOfLLDPDU()); if (0 == lldpPacket.TlvCollection.Count) { throw new Exception("Couldn't construct LLDP TLVs."); } if (lldpPacket.TlvCollection.Last().GetType() != typeof(EndOfLLDPDU)) { throw new Exception("Last TLV must be type of 'EndOfLLDPDU'!"); } foreach (TLV tlv in lldpPacket.TlvCollection) { Debug.WriteLine(tlv.ToString(), EventLogEntryType.Information); } // Generate packet Packet packet = new EthernetPacket(MACAddress, destinationHW, EthernetPacketType.LLDP); packet.PayloadData = lldpPacket.Bytes; return packet; }
bool macsMatch(string mac, NetworkInterface nic) { byte[] macbytes = nic.GetPhysicalAddress().GetAddressBytes(); if (macbytes.Length != 6) // Looks like an Ethernet mac address { Debug.Print("Attempting to match non-ethernet physical address"); return false; } string macmatchstr = macbytes[0].ToString("x2") + ":" + macbytes[1].ToString("x2") + ":" + macbytes[2].ToString("x2") + ":" + macbytes[3].ToString("x2") + ":" + macbytes[4].ToString("x2") + ":" + macbytes[5].ToString("x2"); Debug.Print("Matching \"" + macmatchstr + "\" and \"" + mac.ToLower() + "\""); return (macmatchstr.Equals(mac.ToLower())); }
private static bool IsEthernet(NetworkInterface nic) { //return nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet; if (nic.NetworkInterfaceType != NetworkInterfaceType.Ethernet) return false; string key = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\"; // if ((nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet) //|| (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet3Megabit) //|| (nic.NetworkInterfaceType == NetworkInterfaceType.FastEthernetFx) //|| (nic.NetworkInterfaceType == NetworkInterfaceType.FastEthernetT) //|| (nic.NetworkInterfaceType == NetworkInterfaceType.GigabitEthernet)) if (nic.GetPhysicalAddress().ToString().Length != 0) { string fRegisteryKey = key + nic.Id + "\\Connection"; Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(fRegisteryKey, false); if (rk != null) { string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString(); int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0)); if (fPnpInstanceID.Length > 3 && fPnpInstanceID.Substring(0, 3) == "PCI") { return true; } } } return false; }
internal sNetworkCard(NetworkInterface ni) { _name = ni.Name; if (ni.NetworkInterfaceType == NetworkInterfaceType.Loopback) { _mac = null; _isDHCP = false; _ipAddress = "127.0.0.1"; _networkMask = "255.255.255.0"; _broadcast = "127.0.0.1"; _live = true; }else if (ni.NetworkInterfaceType != NetworkInterfaceType.Tunnel) { _mac = ni.GetPhysicalAddress().ToString(); _live = ni.OperationalStatus == OperationalStatus.Up; _isDHCP = ni.GetIPProperties().GetIPv4Properties().IsDhcpEnabled; if ((_live && _isDHCP) || !_isDHCP) { foreach (UnicastIPAddressInformation uipa in ni.GetIPProperties().UnicastAddresses) { if (uipa.Address.AddressFamily == global::System.Net.Sockets.AddressFamily.InterNetwork) { _ipAddress = uipa.Address.ToString(); try { _networkMask = uipa.IPv4Mask.ToString(); }catch(Exception e){ _networkMask = null; } break; } } foreach (GatewayIPAddressInformation gipi in ni.GetIPProperties().GatewayAddresses) { if (gipi.Address.AddressFamily == global::System.Net.Sockets.AddressFamily.InterNetwork) { _gateway = gipi.Address.ToString(); break; } } _network = null; _broadcast = null; } } }
private static string NormalizeMacAddress(NetworkInterface adapter) { var value = adapter.GetPhysicalAddress().ToString(); return string.IsNullOrWhiteSpace(value) ? "" : value.Replace(":", ""); }
byte[] GetHardwareAddress(MNetworkInterface inf) { byte[] bytes = inf.GetPhysicalAddress().GetAddressBytes(); // Map to android's idea of device address if (bytes.Length == 0 || inf.NetworkInterfaceType == NetworkInterfaceType.Tunnel) { return(null); } return(bytes); }
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(); }
protected void Page_Load(object sender, EventArgs e) { System.Net.NetworkInformation.NetworkInterface[] nics = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces(); System.Net.NetworkInformation.NetworkInterface adapter = nics[0]; System.Net.NetworkInformation.IPInterfaceProperties properties = adapter.GetIPProperties(); Startup_Util.UserMac = adapter.GetPhysicalAddress().ToString(); System.Net.IPHostEntry host; host = System.Net.Dns.GetHostByAddress(Request.ServerVariables["REMOTE_HOST"]); Startup_Util.UserPC = host.HostName; Startup_Util.UserIP = Request.ServerVariables["REMOTE_HOST"]; Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache); String ST = Startup_Util.DcryptionPWD(Request.QueryString[0].ToString()); string[] ARY; ARY = ST.Split('~'); string[] ARY1; ARY1 = ARY[0].Split('|'); Startup_Util.SSO_DB = ARY1[9].ToString(); //SINGLE SIGN ON CONNECTION STRING Startup_Util.SPDS_DB = ARY1[10].ToString(); //SPDS CONNECTION STRING Session["EXP_PAGE"] = ARY1[8].ToString(); Session["CompleteInformation"] = ARY1[5].ToString() + " | " + ARY1[6].ToString() + " | "; Label1.Text = Session["CompleteInformation"].ToString(); Session["LOG_CODE"] = ARY1[4].ToString(); Session["U_NAME"] = ARY1[5].ToString(); //I1.Attributes["src"] = "../MasterPage/Default2.aspx"; if (SSO_Control1.LoginStatus(Session["LOG_CODE"].ToString()) == 0) { Response.Redirect(Session["EXP_PAGE"].ToString()); } else { SSO_Control1.GearUp(ARY1[3].ToString(), ARY1[4].ToString()); } }
/// <summary> /// /// </summary> /// <param name="adapter"></param> /// <param name="payload"></param> /// <returns></returns> private bool sendRawPacket(NetworkInterface adapter, Packet payload) { Debug.WriteLine("Sending RAW packet", EventLogEntryType.Information); foreach (PcapDevice device in CaptureDeviceList.Instance) { if (adapter.GetPhysicalAddress().Equals(device.MacAddress)) { Debug.WriteLine("Device found!", EventLogEntryType.Information); if (!device.Opened) { Debug.WriteLine("Device is not open.", EventLogEntryType.Error); return false; } device.SendPacket(payload); return true; } } return false; }