private static unsafe T TransformNetworkInterfacess <T>(Func <int, IntPtr, int, IntPtr, T> transform) { int interfaceCount = 0; int addressCount = 0; Interop.Sys.NetworkInterfaceInfo *networkInterfaceInfo = null; Interop.Sys.IpAddressInfo * addressInfo = null; if (Interop.Sys.GetNetworkInterfaces(ref interfaceCount, ref networkInterfaceInfo, ref addressCount, ref addressInfo) != 0) { string message = Interop.Sys.GetLastErrorInfo().GetErrorMessage(); throw new NetworkInformationException(message); } // the native implementation of Interop.Sys.GetNetworkInterfaces allocates one block of memory // for both networkInterfaceInfo and addressInfo so we only need to call free once pointing at // the start of the network interfaces list var globalMemory = (IntPtr)networkInterfaceInfo; try { return(transform(interfaceCount, (IntPtr)networkInterfaceInfo, addressCount, (IntPtr)addressInfo)); } finally { Marshal.FreeHGlobal(globalMemory); } }
internal unsafe AndroidNetworkInterface(string name, Interop.Sys.NetworkInterfaceInfo *networkInterfaceInfo) : base(name) { _index = networkInterfaceInfo->InterfaceIndex; if (networkInterfaceInfo->NumAddressBytes > 0) { _physicalAddress = new PhysicalAddress(new ReadOnlySpan <byte>(networkInterfaceInfo->AddressBytes, networkInterfaceInfo->NumAddressBytes).ToArray()); } _mtu = networkInterfaceInfo->Mtu; _ipProperties = new AndroidIPInterfaceProperties(this); OperationalStatus = (OperationalStatus)networkInterfaceInfo->OperationalState; Speed = networkInterfaceInfo->Speed; SupportsMulticast = networkInterfaceInfo->SupportsMulticast != 0; NetworkInterfaceType = (NetworkInterfaceType)networkInterfaceInfo->HardwareType; }
public static unsafe NetworkInterface[] GetLinuxNetworkInterfaces() { var systemProperties = new LinuxNetworkInterfaceSystemProperties(); int interfaceCount = 0; int addressCount = 0; Interop.Sys.NetworkInterfaceInfo *nii = null; Interop.Sys.IpAddressInfo * ai = null; IntPtr globalMemory = (IntPtr)null; if (Interop.Sys.GetNetworkInterfaces(ref interfaceCount, ref nii, ref addressCount, ref ai) != 0) { string message = Interop.Sys.GetLastErrorInfo().GetErrorMessage(); throw new NetworkInformationException(message); } globalMemory = (IntPtr)nii; try { NetworkInterface[] interfaces = new NetworkInterface[interfaceCount]; Dictionary <int, LinuxNetworkInterface> interfacesByIndex = new Dictionary <int, LinuxNetworkInterface>(interfaceCount); for (int i = 0; i < interfaceCount; i++) { var lni = new LinuxNetworkInterface(Marshal.PtrToStringAnsi((IntPtr)nii->Name) !, nii->InterfaceIndex, systemProperties); lni._interfaceType = (NetworkInterfaceType)nii->HardwareType; lni._speed = nii->Speed; lni._operationalStatus = (OperationalStatus)nii->OperationalState; lni._mtu = nii->Mtu; lni._supportsMulticast = nii->SupportsMulticast != 0; if (nii->NumAddressBytes > 0) { lni._physicalAddress = new PhysicalAddress(new ReadOnlySpan <byte>(nii->AddressBytes, nii->NumAddressBytes).ToArray()); } interfaces[i] = lni; interfacesByIndex.Add(nii->InterfaceIndex, lni); nii++; } while (addressCount != 0) { var address = new IPAddress(new ReadOnlySpan <byte>(ai->AddressBytes, ai->NumAddressBytes)); if (address.IsIPv6LinkLocal) { address.ScopeId = ai->InterfaceIndex; } if (interfacesByIndex.TryGetValue(ai->InterfaceIndex, out LinuxNetworkInterface? lni)) { lni.AddAddress(address, ai->PrefixLength); } ai++; addressCount--; } return(interfaces); } finally { Marshal.FreeHGlobal(globalMemory); } }