using System; using System.Collections.Generic; using System.Net.NetworkInformation; using static Vanara.PInvoke.IpHlpApi; namespace Vanara.Extensions { /// The interface access type. public enum NetworkInterfaceAccessType { /// /// Specifies the loopback access type. This access type indicates that the interface loops back transmit data as receive data. /// Loopback = 1, /// /// Specifies the LAN access type, which includes Ethernet. This access type indicates that the interface provides native support for /// multicast or broadcast services. /// Broadcast, /// Specifies point-to-point access that supports CoNDIS and WAN, except for non-broadcast multi-access (NBMA) interfaces. PointToPoint, /// /// Specifies point-to-multipoint access that supports non-broadcast multi-access (NBMA) media, including the "RAS Internal" /// interface, and native (non-LANE) ATM. /// PointToMultiPoint, } /// Specifies the NDIS network interface administrative status, as described in RFC 2863. /// For more information on RFC 2863, see "The Interfaces Group MIB". public enum NetworkInterfaceAdministrativeStatus { /// /// Specifies that the interface is initialized and enabled, but the interface is not necessarily ready to transmit and receive /// network data because that depends on the operational status of the interface. For more information about the operational status /// of an interface, see OID_GEN_OPERATIONAL_STATUS. /// Up = 1, /// Specifies that the interface is down, and this interface cannot be used to transmit or receive network data. Down, /// Specifies that the interface is in a test mode, and no network data can be transmitted or received. Testing, } /// Specifies the NDIS network interface connection type. public enum NetworkInterfaceConnectionType : uint { /// /// Specifies the dedicated connection type. The connection comes up automatically when media sense is TRUE. For example, an Ethernet /// connection is dedicated. /// Dedicated = 1, /// /// Specifies the passive connection type. The other end must bring up the connection to the local station. For example, the RAS /// interface is passive. /// Passive, /// /// Specifies the demand-dial connection type. A demand-dial connection comes up in response to a local action--for example, sending /// a packet. /// Demand, } /// Specifies the NDIS network interface direction type. public enum NetworkInterfaceDirectionType { /// /// Indicates the send and receive direction type. This direction type indicates that the NDIS network interface can send and receive data. /// SendReceive, /// /// Indicates the send only direction type. This direction type indicates that the NDIS network interface can only send data. /// SendOnly, /// /// Indicates the receive only direction type. This direction type indicates that the NDIS network interface can only receive data. /// ReceiveOnly, } /// The NDIS media type of a network interface. public enum NetworkInterfaceMediaType { /// Specifies an Ethernet (802.3) network. Ethernet802_3, /// Specifies a Token Ring (802.5) network. Not supported in Windows 8 or later. TokenRing, /// /// Specifies a Fiber Distributed Data Interface (FDDI) network. Not supported in Windows Vista/Windows Server 2008 /// or later. /// Fddi, /// /// Specifies a wide area network. This type covers various forms of point-to-point and WAN NICs, as well as variant address/header /// formats that must be negotiated between the protocol driver and the underlying driver after the binding is established. /// Wan, /// Specifies a LocalTalk network. LocalTalk, /// Specifies an Ethernet network for which the drivers use the DIX Ethernet header format. Dix, /// Specifies an ARCNET network. Not supported in Windows Vista/Windows Server 2008 or later. ArcnetRaw, /// Specifies an ARCNET (878.2) network. Not supported in Windows Vista/Windows Server 2008 or later. Arcnet878_2, /// /// Specifies an ATM network. Connection-oriented client protocol drivers can bind themselves to an underlying miniport driver that /// returns this value. Otherwise, legacy protocol drivers bind themselves to the system-supplied LanE intermediate driver, which /// reports its medium type as either of 802_3 or 802_5, depending on how the LanE driver is configured by the network administrator. /// Atm, /// /// Specifies a wireless network. NDIS 5.X miniport drivers that support wireless LAN (WLAN) or wireless WAN (WWAN) packets declare /// their medium as 802_3 and emulate Ethernet to higher-level NDIS drivers. Starting with Windows 7, this media /// type is supported and can be used for Mobile Broadband. /// Wireless, /// Specifies an infrared (IrDA) network. IrDA, /// Specifies a broadcast PC network. Broadcast, /// Specifies a wide area network in a connection-oriented environment. CoWAN, /// /// Specifies an IEEE 1394 (fire wire) network. Not supported in Windows Vista/Windows Server 2008 or later. /// Ieee1394, /// Specifies an InfiniBand network. InfiniBand, /// Specifies a tunnel network. Tunnel, /// Specifies a native IEEE 802.11 network. Native802_11, /// Specifies an NDIS loopback network. Loopback, /// Specifies a WiMAX network. WiMAX, /// Specifies a generic medium that is capable of sending and receiving raw IP packets. IP, } /// The NDIS physical medium type. public enum NetworkInterfacePhysicalMedium { /// /// The physical medium is none of the below values. For example, a one-way satellite feed is an unspecified physical medium. /// Unspecified = 0, /// Packets are transferred over a wireless LAN network through a miniport driver that conforms to the 802.11 interface. WirelessLan = 1, /// Packets are transferred over a DOCSIS-based cable network. CableModem = 2, /// Packets are transferred over standard phone lines. This includes HomePNA media, for example. PhoneLine = 3, /// Packets are transferred over wiring that is connected to a power distribution system. PowerLine = 4, /// /// Packets are transferred over a Digital Subscriber Line (DSL) network. This includes ADSL, UADSL (G.Lite), and SDSL, for example. /// DSL = 5, /// Packets are transferred over a Fibre Channel interconnect. FibreChannel = 6, /// Packets are transferred over an IEEE 1394 bus. Ieee1394 = 7, /// /// Packets are transferred over a Wireless WAN link. This includes mobile broadband devices that support CDPD, CDMA, GSM, and GPRS, /// for example. /// WirelessWan = 8, /// /// Packets are transferred over a wireless LAN network through a miniport driver that conforms to the Native 802.11 interface. The Native 802.11 interface is supported in NDIS 6.0 and later versions. /// Native802_11 = 9, /// /// Packets are transferred over a Bluetooth network. Bluetooth is a short-range wireless technology that uses the 2.4 GHz spectrum. /// Bluetooth = 10, /// Packets are transferred over an Infiniband interconnect. InfiniBand = 11, /// Packets are transferred over a WiMax network. WiMAX = 12, /// Packets are transferred over an ultra wide band network. UWB = 13, /// Packets are transferred over an Ethernet (802.3) network. Ethernet802_3 = 14, /// Packets are transferred over a Token Ring (802.5) network. TokenRing = 15, /// Packets are transferred over an infrared (IrDA) network. IrDA = 16, /// Packets are transferred over a wired WAN network. WiredWAN = 17, /// Packets are transferred over a wide area network in a connection-oriented environment. WiredCoWAN = 18, /// Packets are transferred over a network that is not described by other possible values. Other = 19, } /// Extension methods for various network related interfaces methods. public static class NetworkInterfaceExt { private static Dictionary adapters = new Dictionary(); /// Gets the interface access type. public static NetworkInterfaceAccessType GetAccessType(this IPInterfaceProperties ifprop) => (NetworkInterfaceAccessType)GetIfEntry(ifprop.GetIPv4Properties().Index).AccessType; /// Gets the administrative status for the interface as defined in RFC 2863. For more information, see http://www.ietf.org/rfc/rfc2863.txt. public static NetworkInterfaceAdministrativeStatus GetAdminStatus(this IPInterfaceProperties ifprop) => (NetworkInterfaceAdministrativeStatus)GetIfEntry(ifprop.GetIPv4Properties().Index).AdminStatus; /// Gets the NDIS network interface connection type. public static NetworkInterfaceConnectionType GetConnectionType(this IPInterfaceProperties ifprop) => (NetworkInterfaceConnectionType)GetIfEntry(ifprop.GetIPv4Properties().Index).ConnectionType; /// Gets the interface direction type. public static NetworkInterfaceDirectionType GetDirectionType(this IPInterfaceProperties ifprop) => (NetworkInterfaceDirectionType)GetIfEntry(ifprop.GetIPv4Properties().Index).DirectionType; /// Gets the GUID for the network interface. public static Guid GetGuid(this IPInterfaceProperties ifprop) => GetIfEntry(ifprop.GetIPv4Properties().Index).InterfaceGuid; /// Gets the speed in bits per second of the transmit link and receive link. public static void GetLinkSpeeds(this NetworkInterface intf, out ulong transmitSpeed, out ulong receiveSpeed) { var e = GetIfEntry(intf.GetIPProperties().GetIPv4Properties().Index); transmitSpeed = e.TransmitLinkSpeed; receiveSpeed = e.ReceiveLinkSpeed; } /// Gets the NDIS media type for the interface. public static NetworkInterfaceMediaType GetMediaType(this IPInterfaceProperties ifprop) => (NetworkInterfaceMediaType)GetIfEntry(ifprop.GetIPv4Properties().Index).MediaType; /// Gets the permanent physical hardware address of the adapter for this network interface. public static PhysicalAddress GetPermanentPhysicalAddress(this NetworkInterface intf) { var e = GetIfEntry(intf.GetIPProperties().GetIPv4Properties().Index); var newAddr = new byte[e.physicalAddressLength]; Array.Copy(e.PermanentPhysicalAddress, newAddr, e.physicalAddressLength); return new PhysicalAddress(newAddr); } /// Gets the NDIS physical medium type. public static NetworkInterfacePhysicalMedium GetPhysicalMediumType(this IPInterfaceProperties ifprop) => (NetworkInterfacePhysicalMedium)GetIfEntry(ifprop.GetIPv4Properties().Index).PhysicalMediumType; private static MIB_IF_ROW2 GetIfEntry(int index) { if (adapters.TryGetValue(index, out var row)) return row; row = new MIB_IF_ROW2((uint)index); GetIfEntry2(ref row).ThrowIfFailed(); adapters.Add(index, row); return row; } } }