internal static FixedInfo GetFixedInfo() { uint size = 0; SafeLocalFree buffer = null; FixedInfo fixedInfo = new FixedInfo(); //first we need to get the size of the buffer uint result = UnsafeNetInfoNativeMethods.GetNetworkParams(SafeLocalFree.Zero, ref size); while (result == IpHelperErrors.ErrorBufferOverflow) { try { //now we allocate the buffer and read the network parameters. buffer = SafeLocalFree.LocalAlloc((int)size); result = UnsafeNetInfoNativeMethods.GetNetworkParams(buffer, ref size); if (result == IpHelperErrors.Success) { fixedInfo = new FixedInfo((FIXED_INFO)Marshal.PtrToStructure(buffer.DangerousGetHandle(), typeof(FIXED_INFO))); } } finally { if (buffer != null) { buffer.Close(); } } } //if the result include there being no information, we'll still throw if (result != IpHelperErrors.Success) { throw new NetworkInformationException((int)result); } return(fixedInfo); }
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; } }
internal SystemIPInterfaceProperties(FixedInfo fixedInfo, IpAdapterAddresses ipAdapterAddresses) { this.dnsEnabled = fixedInfo.EnableDns; this.index = ipAdapterAddresses.index; this.name = ipAdapterAddresses.AdapterName; this.ipv6Index = ipAdapterAddresses.ipv6Index; if (this.index > 0) { this.versionSupported |= IPVersion.IPv4; } if (this.ipv6Index > 0) { this.versionSupported |= IPVersion.IPv6; } this.mtu = ipAdapterAddresses.mtu; this.adapterFlags = ipAdapterAddresses.flags; this.dnsSuffix = ipAdapterAddresses.dnsSuffix; this.dynamicDnsEnabled = (ipAdapterAddresses.flags & AdapterFlags.DnsEnabled) > 0; this.multicastAddresses = SystemMulticastIPAddressInformation.ToAddressInformationCollection(ipAdapterAddresses.FirstMulticastAddress); this.dnsAddresses = SystemIPAddressInformation.ToAddressCollection(ipAdapterAddresses.FirstDnsServerAddress, this.versionSupported); this.anycastAddresses = SystemIPAddressInformation.ToAddressInformationCollection(ipAdapterAddresses.FirstAnycastAddress, this.versionSupported); this.unicastAddresses = SystemUnicastIPAddressInformation.ToAddressInformationCollection(ipAdapterAddresses.FirstUnicastAddress); if (this.ipv6Index > 0) { this.ipv6Properties = new SystemIPv6InterfaceProperties(this.ipv6Index, this.mtu); } }
internal bool Update(FixedInfo fixedInfo, IpAdapterInfo ipAdapterInfo) { try { foreach (IPExtendedAddress address in ipAdapterInfo.ipAddressList.ToIPExtendedAddressArrayList()) { foreach (SystemUnicastIPAddressInformation information in this.unicastAddresses) { if (address.address.Equals(information.Address)) { information.ipv4Mask = address.mask; } } } this.ipv4Properties = new SystemIPv4InterfaceProperties(fixedInfo, ipAdapterInfo); if ((this.dnsAddresses == null) || (this.dnsAddresses.Count == 0)) { this.dnsAddresses = this.ipv4Properties.DnsAddresses; } } catch (NetworkInformationException exception) { if (((exception.ErrorCode != 0x57L) && (exception.ErrorCode != 13L)) && (((exception.ErrorCode != 0xe8L) && (exception.ErrorCode != 1L)) && (exception.ErrorCode != 2L))) { throw; } return(false); } return(true); }
// 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); } }
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; } }
internal static FixedInfo GetFixedInfo(){ uint size = 0; SafeLocalFree buffer = null; FixedInfo fixedInfo = new FixedInfo(); //first we need to get the size of the buffer uint result = UnsafeNetInfoNativeMethods.GetNetworkParams(SafeLocalFree.Zero,ref size); while (result == IpHelperErrors.ErrorBufferOverflow) { try { //now we allocate the buffer and read the network parameters. buffer = SafeLocalFree.LocalAlloc((int)size); result = UnsafeNetInfoNativeMethods.GetNetworkParams(buffer,ref size); if ( result == IpHelperErrors.Success ) { fixedInfo = new FixedInfo( (FIXED_INFO)Marshal.PtrToStructure(buffer.DangerousGetHandle(),typeof(FIXED_INFO))); } } finally { if(buffer != null){ buffer.Close(); } } } //if the result include there being no information, we'll still throw if (result != IpHelperErrors.Success) { throw new NetworkInformationException((int)result); } return fixedInfo; }
private static NetworkInterface[] GetNetworkInterfaces(AddressFamily family) { IpHelperErrors.CheckFamilyUnspecified(family); if (ComNetOS.IsPostWin2K) { return(PostWin2KGetNetworkInterfaces(family)); } FixedInfo fixedInfo = SystemIPGlobalProperties.GetFixedInfo(); if ((family != AddressFamily.Unspecified) && (family != AddressFamily.InterNetwork)) { throw new PlatformNotSupportedException(SR.GetString("WinXPRequired")); } SafeLocalFree pAdapterInfo = null; uint pOutBufLen = 0; ArrayList list = new ArrayList(); uint adaptersInfo = UnsafeNetInfoNativeMethods.GetAdaptersInfo(SafeLocalFree.Zero, ref pOutBufLen); while (adaptersInfo == 0x6f) { try { pAdapterInfo = SafeLocalFree.LocalAlloc((int)pOutBufLen); adaptersInfo = UnsafeNetInfoNativeMethods.GetAdaptersInfo(pAdapterInfo, ref pOutBufLen); if (adaptersInfo == 0) { IpAdapterInfo ipAdapterInfo = (IpAdapterInfo)Marshal.PtrToStructure(pAdapterInfo.DangerousGetHandle(), typeof(IpAdapterInfo)); list.Add(new SystemNetworkInterface(fixedInfo, ipAdapterInfo)); while (ipAdapterInfo.Next != IntPtr.Zero) { ipAdapterInfo = (IpAdapterInfo)Marshal.PtrToStructure(ipAdapterInfo.Next, typeof(IpAdapterInfo)); list.Add(new SystemNetworkInterface(fixedInfo, ipAdapterInfo)); } } continue; } finally { if (pAdapterInfo != null) { pAdapterInfo.Close(); } } } if (adaptersInfo == 0xe8) { return(new SystemNetworkInterface[0]); } if (adaptersInfo != 0) { throw new NetworkInformationException((int)adaptersInfo); } SystemNetworkInterface[] interfaceArray = new SystemNetworkInterface[list.Count]; for (int i = 0; i < list.Count; i++) { interfaceArray[i] = (SystemNetworkInterface)list[i]; } return(interfaceArray); }
// Vista+ internal SystemIPv4InterfaceProperties(FixedInfo fixedInfo, IpAdapterAddresses ipAdapterAddresses) { index = ipAdapterAddresses.index; routingEnabled = fixedInfo.EnableRouting; dhcpEnabled = ((ipAdapterAddresses.flags & AdapterFlags.DhcpEnabled) != 0); haveWins = (ipAdapterAddresses.firstWinsServerAddress != IntPtr.Zero); mtu = ipAdapterAddresses.mtu; GetPerAdapterInfo(ipAdapterAddresses.index); }
internal SystemNetworkInterface(FixedInfo fixedInfo, IpAdapterInfo ipAdapterInfo) { this.id = ipAdapterInfo.adapterName; this.name = string.Empty; this.description = ipAdapterInfo.description; this.index = ipAdapterInfo.index; this.physicalAddress = ipAdapterInfo.address; this.addressLength = ipAdapterInfo.addressLength; if (ComNetOS.IsWin2K && !ComNetOS.IsPostWin2K) { this.name = this.ReadAdapterName(this.id); } if (this.name.Length == 0) { this.name = this.description; } SystemIPv4InterfaceStatistics statistics = new SystemIPv4InterfaceStatistics((long)this.index); this.operStatus = statistics.OperationalStatus; switch (ipAdapterInfo.type) { case OldInterfaceType.Ppp: this.type = System.Net.NetworkInformation.NetworkInterfaceType.Ppp; break; case OldInterfaceType.Loopback: this.type = System.Net.NetworkInformation.NetworkInterfaceType.Loopback; break; case OldInterfaceType.Slip: this.type = System.Net.NetworkInformation.NetworkInterfaceType.Slip; break; case OldInterfaceType.Fddi: this.type = System.Net.NetworkInformation.NetworkInterfaceType.Fddi; break; case OldInterfaceType.Ethernet: this.type = System.Net.NetworkInformation.NetworkInterfaceType.Ethernet; break; case OldInterfaceType.TokenRing: this.type = System.Net.NetworkInformation.NetworkInterfaceType.TokenRing; break; default: this.type = System.Net.NetworkInformation.NetworkInterfaceType.Unknown; break; } this.interfaceProperties = new SystemIPInterfaceProperties(fixedInfo, ipAdapterInfo); }
internal SystemNetworkInterface(FixedInfo fixedInfo, IpAdapterInfo ipAdapterInfo) { this.id = ipAdapterInfo.adapterName; this.name = string.Empty; this.description = ipAdapterInfo.description; this.index = ipAdapterInfo.index; this.physicalAddress = ipAdapterInfo.address; this.addressLength = ipAdapterInfo.addressLength; if (ComNetOS.IsWin2K && !ComNetOS.IsPostWin2K) { this.name = this.ReadAdapterName(this.id); } if (this.name.Length == 0) { this.name = this.description; } SystemIPv4InterfaceStatistics statistics = new SystemIPv4InterfaceStatistics((long) this.index); this.operStatus = statistics.OperationalStatus; switch (ipAdapterInfo.type) { case OldInterfaceType.Ppp: this.type = System.Net.NetworkInformation.NetworkInterfaceType.Ppp; break; case OldInterfaceType.Loopback: this.type = System.Net.NetworkInformation.NetworkInterfaceType.Loopback; break; case OldInterfaceType.Slip: this.type = System.Net.NetworkInformation.NetworkInterfaceType.Slip; break; case OldInterfaceType.Fddi: this.type = System.Net.NetworkInformation.NetworkInterfaceType.Fddi; break; case OldInterfaceType.Ethernet: this.type = System.Net.NetworkInformation.NetworkInterfaceType.Ethernet; break; case OldInterfaceType.TokenRing: this.type = System.Net.NetworkInformation.NetworkInterfaceType.TokenRing; break; default: this.type = System.Net.NetworkInformation.NetworkInterfaceType.Unknown; break; } this.interfaceProperties = new SystemIPInterfaceProperties(fixedInfo, ipAdapterInfo); }
internal SystemNetworkInterface(FixedInfo fixedInfo, IpAdapterAddresses ipAdapterAddresses) { this.id = ipAdapterAddresses.AdapterName; this.name = ipAdapterAddresses.friendlyName; this.description = ipAdapterAddresses.description; this.index = ipAdapterAddresses.index; this.physicalAddress = ipAdapterAddresses.address; this.addressLength = ipAdapterAddresses.addressLength; this.type = ipAdapterAddresses.type; this.operStatus = ipAdapterAddresses.operStatus; this.ipv6Index = ipAdapterAddresses.ipv6Index; this.adapterFlags = ipAdapterAddresses.flags; this.interfaceProperties = new SystemIPInterfaceProperties(fixedInfo, ipAdapterAddresses); }
// 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); }
// 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); } }
internal SystemIPInterfaceProperties(FixedInfo fixedInfo, IpAdapterInfo ipAdapterInfo) { this.dnsEnabled = fixedInfo.EnableDns; this.name = ipAdapterInfo.adapterName; this.index = ipAdapterInfo.index; this.multicastAddresses = new MulticastIPAddressInformationCollection(); this.anycastAddresses = new IPAddressInformationCollection(); if (this.index > 0) { this.versionSupported |= IPVersion.IPv4; } if (ComNetOS.IsWin2K) { this.ReadRegDnsSuffix(); } this.unicastAddresses = new UnicastIPAddressInformationCollection(); foreach (IPExtendedAddress address in ipAdapterInfo.ipAddressList.ToIPExtendedAddressArrayList()) { this.unicastAddresses.InternalAdd(new SystemUnicastIPAddressInformation(ipAdapterInfo, address)); } try { this.ipv4Properties = new SystemIPv4InterfaceProperties(fixedInfo, ipAdapterInfo); if ((this.dnsAddresses == null) || (this.dnsAddresses.Count == 0)) { this.dnsAddresses = this.ipv4Properties.DnsAddresses; } } catch (NetworkInformationException exception) { if (exception.ErrorCode != 0x57L) { throw; } } }
private static SystemNetworkInterface[] PostWin2KGetNetworkInterfaces(AddressFamily family) { FixedInfo fixedInfo = SystemIPGlobalProperties.GetFixedInfo(); SystemNetworkInterface[] adaptersAddresses = null; Label_0008: try { adaptersAddresses = GetAdaptersAddresses(family, fixedInfo); } catch (NetworkInformationException exception) { if (exception.ErrorCode != 1L) { throw; } goto Label_0008; } if (Socket.OSSupportsIPv4) { uint pOutBufLen = 0; uint adaptersInfo = 0; SafeLocalFree pAdapterInfo = null; if ((family == AddressFamily.Unspecified) || (family == AddressFamily.InterNetwork)) { adaptersInfo = UnsafeNetInfoNativeMethods.GetAdaptersInfo(SafeLocalFree.Zero, ref pOutBufLen); int num3 = 0; while (adaptersInfo == 0x6f) { try { pAdapterInfo = SafeLocalFree.LocalAlloc((int)pOutBufLen); adaptersInfo = UnsafeNetInfoNativeMethods.GetAdaptersInfo(pAdapterInfo, ref pOutBufLen); if (adaptersInfo == 0) { IntPtr handle = pAdapterInfo.DangerousGetHandle(); while (handle != IntPtr.Zero) { IpAdapterInfo ipAdapterInfo = (IpAdapterInfo)Marshal.PtrToStructure(handle, typeof(IpAdapterInfo)); for (int i = 0; i < adaptersAddresses.Length; i++) { if ((adaptersAddresses[i] != null) && (ipAdapterInfo.index == adaptersAddresses[i].index)) { if (!adaptersAddresses[i].interfaceProperties.Update(fixedInfo, ipAdapterInfo)) { adaptersAddresses[i] = null; num3++; } break; } } handle = ipAdapterInfo.Next; } } continue; } finally { if (pAdapterInfo != null) { pAdapterInfo.Close(); } } } if (num3 != 0) { SystemNetworkInterface[] interfaceArray2 = new SystemNetworkInterface[adaptersAddresses.Length - num3]; int num5 = 0; for (int j = 0; j < adaptersAddresses.Length; j++) { if (adaptersAddresses[j] != null) { interfaceArray2[num5++] = adaptersAddresses[j]; } } adaptersAddresses = interfaceArray2; } } if ((adaptersInfo != 0) && (adaptersInfo != 0xe8)) { throw new NetworkInformationException((int)adaptersInfo); } } return(adaptersAddresses); }
private static SystemNetworkInterface[] GetAdaptersAddresses(AddressFamily family, FixedInfo fixedInfo) { uint outBufLen = 0; SafeLocalFree adapterAddresses = null; ArrayList list = new ArrayList(); SystemNetworkInterface[] interfaceArray = null; uint num2 = UnsafeNetInfoNativeMethods.GetAdaptersAddresses(family, 0, IntPtr.Zero, SafeLocalFree.Zero, ref outBufLen); while (num2 == 0x6f) { try { adapterAddresses = SafeLocalFree.LocalAlloc((int) outBufLen); num2 = UnsafeNetInfoNativeMethods.GetAdaptersAddresses(family, 0, IntPtr.Zero, adapterAddresses, ref outBufLen); if (num2 == 0) { IpAdapterAddresses ipAdapterAddresses = (IpAdapterAddresses) Marshal.PtrToStructure(adapterAddresses.DangerousGetHandle(), typeof(IpAdapterAddresses)); list.Add(new SystemNetworkInterface(fixedInfo, ipAdapterAddresses)); while (ipAdapterAddresses.next != IntPtr.Zero) { ipAdapterAddresses = (IpAdapterAddresses) Marshal.PtrToStructure(ipAdapterAddresses.next, typeof(IpAdapterAddresses)); list.Add(new SystemNetworkInterface(fixedInfo, ipAdapterAddresses)); } } continue; } finally { if (adapterAddresses != null) { adapterAddresses.Close(); } adapterAddresses = null; } } switch (num2) { case 0xe8: case 0x57: return new SystemNetworkInterface[0]; } if (num2 != 0) { throw new NetworkInformationException((int) num2); } interfaceArray = new SystemNetworkInterface[list.Count]; for (int i = 0; i < list.Count; i++) { interfaceArray[i] = (SystemNetworkInterface) list[i]; } return interfaceArray; }
internal static NetworkInterface[] GetNetworkInterfaces() { Contract.Ensures(Contract.Result <NetworkInterface[]>() != null); AddressFamily family = AddressFamily.Unspecified; uint bufferSize = 0; SafeLocalFree buffer = null; FixedInfo fixedInfo = SystemIPGlobalProperties.GetFixedInfo(); List <SystemNetworkInterface> interfaceList = new List <SystemNetworkInterface>(); GetAdaptersAddressesFlags flags = GetAdaptersAddressesFlags.IncludeGateways | GetAdaptersAddressesFlags.IncludeWins; // Figure out the right buffer size for the adapter information uint result = UnsafeNetInfoNativeMethods.GetAdaptersAddresses( family, (uint)flags, IntPtr.Zero, SafeLocalFree.Zero, ref bufferSize); while (result == IpHelperErrors.ErrorBufferOverflow) { try { // Allocate the buffer and get the adapter info buffer = SafeLocalFree.LocalAlloc((int)bufferSize); result = UnsafeNetInfoNativeMethods.GetAdaptersAddresses( family, (uint)flags, IntPtr.Zero, buffer, ref bufferSize); // If succeeded, we're going to add each new interface if (result == IpHelperErrors.Success) { // Linked list of interfaces IntPtr ptr = buffer.DangerousGetHandle(); while (ptr != IntPtr.Zero) { // Traverse the list, marshal in the native structures, and create new NetworkInterfaces IpAdapterAddresses adapterAddresses = (IpAdapterAddresses)Marshal.PtrToStructure(ptr, typeof(IpAdapterAddresses)); interfaceList.Add(new SystemNetworkInterface(fixedInfo, adapterAddresses)); ptr = adapterAddresses.next; } } } finally { if (buffer != null) { buffer.Close(); } buffer = null; } } // if we don't have any interfaces detected, return empty. if (result == IpHelperErrors.ErrorNoData || result == IpHelperErrors.ErrorInvalidParameter) { return(new SystemNetworkInterface[0]); } // Otherwise we throw on an error if (result != IpHelperErrors.Success) { throw new NetworkInformationException((int)result); } return(interfaceList.ToArray()); }
private static SystemNetworkInterface[] GetAdaptersAddresses(AddressFamily family, FixedInfo fixedInfo) { uint outBufLen = 0; SafeLocalFree adapterAddresses = null; ArrayList list = new ArrayList(); SystemNetworkInterface[] interfaceArray = null; uint num2 = UnsafeNetInfoNativeMethods.GetAdaptersAddresses(family, 0, IntPtr.Zero, SafeLocalFree.Zero, ref outBufLen); while (num2 == 0x6f) { try { adapterAddresses = SafeLocalFree.LocalAlloc((int)outBufLen); num2 = UnsafeNetInfoNativeMethods.GetAdaptersAddresses(family, 0, IntPtr.Zero, adapterAddresses, ref outBufLen); if (num2 == 0) { IpAdapterAddresses ipAdapterAddresses = (IpAdapterAddresses)Marshal.PtrToStructure(adapterAddresses.DangerousGetHandle(), typeof(IpAdapterAddresses)); list.Add(new SystemNetworkInterface(fixedInfo, ipAdapterAddresses)); while (ipAdapterAddresses.next != IntPtr.Zero) { ipAdapterAddresses = (IpAdapterAddresses)Marshal.PtrToStructure(ipAdapterAddresses.next, typeof(IpAdapterAddresses)); list.Add(new SystemNetworkInterface(fixedInfo, ipAdapterAddresses)); } } continue; } finally { if (adapterAddresses != null) { adapterAddresses.Close(); } adapterAddresses = null; } } switch (num2) { case 0xe8: case 0x57: return(new SystemNetworkInterface[0]); } if (num2 != 0) { throw new NetworkInformationException((int)num2); } interfaceArray = new SystemNetworkInterface[list.Count]; for (int i = 0; i < list.Count; i++) { interfaceArray[i] = (SystemNetworkInterface)list[i]; } return(interfaceArray); }
internal bool Update(FixedInfo fixedInfo, IpAdapterInfo ipAdapterInfo) { try { foreach (IPExtendedAddress address in ipAdapterInfo.ipAddressList.ToIPExtendedAddressArrayList()) { foreach (SystemUnicastIPAddressInformation information in this.unicastAddresses) { if (address.address.Equals(information.Address)) { information.ipv4Mask = address.mask; } } } this.ipv4Properties = new SystemIPv4InterfaceProperties(fixedInfo, ipAdapterInfo); if ((this.dnsAddresses == null) || (this.dnsAddresses.Count == 0)) { this.dnsAddresses = this.ipv4Properties.DnsAddresses; } } catch (NetworkInformationException exception) { if (((exception.ErrorCode != 0x57L) && (exception.ErrorCode != 13L)) && (((exception.ErrorCode != 0xe8L) && (exception.ErrorCode != 1L)) && (exception.ErrorCode != 2L))) { throw; } return false; } return true; }