This commit is contained in:
Connection Refused
2019-12-02 19:51:12 +08:00
commit b2ea730984
229 changed files with 86605 additions and 0 deletions

View File

@@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.Runtime.InteropServices;
#if !NET45
namespace DnsClient.Windows.IpHlpApi
{
internal class FixedNetworkInformation
{
private FixedNetworkInformation()
{
}
public ICollection<IPAddress> DnsAddresses { get; private set; }
public string DomainName { get; private set; }
public string HostName { get; private set; }
public static FixedNetworkInformation GetFixedInformation()
{
var info = new FixedNetworkInformation();
uint size = 0;
DisposableIntPtr buffer = null;
Interop.IpHlpApi.FIXED_INFO fixedInfo = new Interop.IpHlpApi.FIXED_INFO();
uint result = Interop.IpHlpApi.GetNetworkParams(IntPtr.Zero, ref size);
while (result == Interop.IpHlpApi.ERROR_BUFFER_OVERFLOW)
{
using (buffer = DisposableIntPtr.Alloc((int)size))
{
if (buffer.IsValid)
{
result = Interop.IpHlpApi.GetNetworkParams(buffer.Ptr, ref size);
if (result == Interop.IpHlpApi.ERROR_SUCCESS)
{
fixedInfo = Marshal.PtrToStructure<Interop.IpHlpApi.FIXED_INFO>(buffer.Ptr);
}
else
{
throw new Win32Exception((int)result);
}
var dnsAddresses = new List<IPAddress>();
Interop.IpHlpApi.IP_ADDR_STRING addr = fixedInfo.DnsServerList;
IPAddress ip;
if (IPAddress.TryParse(addr.IpAddress, out ip))
{
dnsAddresses.Add(ip);
while (addr.Next != IntPtr.Zero)
{
addr = Marshal.PtrToStructure<Interop.IpHlpApi.IP_ADDR_STRING>(addr.Next);
if (IPAddress.TryParse(addr.IpAddress, out ip))
{
dnsAddresses.Add(ip);
}
}
}
info.HostName = fixedInfo.hostName;
info.DomainName = fixedInfo.domainName;
info.DnsAddresses = dnsAddresses.ToArray();
return info;
}
else
{
throw new OutOfMemoryException();
}
}
}
return info;
}
}
}
#endif

View File

@@ -0,0 +1,70 @@
using System;
using System.Net.Sockets;
using System.Runtime.InteropServices;
internal static partial class Interop
{
internal static partial class IpHlpApi
{
internal const uint ERROR_SUCCESS = 0;
internal const uint ERROR_INVALID_FUNCTION = 1;
internal const uint ERROR_NO_SUCH_DEVICE = 2;
internal const uint ERROR_INVALID_DATA = 13;
internal const uint ERROR_INVALID_PARAMETER = 87;
internal const uint ERROR_BUFFER_OVERFLOW = 111;
internal const uint ERROR_INSUFFICIENT_BUFFER = 122;
internal const uint ERROR_NO_DATA = 232;
internal const uint ERROR_IO_PENDING = 997;
internal const uint ERROR_NOT_FOUND = 1168;
[DllImport(Libraries.IpHlpApi)]
internal extern static uint GetAdaptersAddresses(
AddressFamily family,
uint flags,
IntPtr pReserved,
IntPtr adapterAddresses,
ref uint outBufLen);
[DllImport(Libraries.IpHlpApi, ExactSpelling = true)]
internal extern static uint GetNetworkParams(IntPtr pFixedInfo, ref uint pOutBufLen);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
internal struct FIXED_INFO
{
public const int MAX_HOSTNAME_LEN = 128;
public const int MAX_DOMAIN_NAME_LEN = 128;
public const int MAX_SCOPE_ID_LEN = 256;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_HOSTNAME_LEN + 4)]
public string hostName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_DOMAIN_NAME_LEN + 4)]
public string domainName;
public IntPtr currentDnsServer; // IpAddressList*
public IP_ADDR_STRING DnsServerList;
public uint nodeType;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_SCOPE_ID_LEN + 4)]
public string scopeId;
public bool enableRouting;
public bool enableProxy;
public bool enableDns;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
internal struct IP_ADDR_STRING
{
public IntPtr Next; // struct _IpAddressList*
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string IpAddress;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
public string IpMask;
public uint Context;
}
}
}