2using System.Collections.Generic;
4using System.Net.NetworkInformation;
5using System.Net.Sockets;
6using System.Threading.Tasks;
15 private static readonly IPAddress[] defaultDnsAddresses =
new IPAddress[]
17 IPAddress.Parse(
"8.8.8.8"),
18 IPAddress.Parse(
"8.8.4.4"),
19 IPAddress.Parse(
"2001:4860:4860::8888"),
20 IPAddress.Parse(
"2001:4860:4860::8844")
23 private readonly IPEndPoint dnsEndpoint;
24 private UdpClient udp =
null;
32 KeyValuePair<IPAddress, IPAddress> P = this.FindDnsAddress();
33 IPAddress Address = P.Value;
34 AddressFamily Family = Address.AddressFamily;
38 this.udp =
new UdpClient(Family)
41 MulticastLoopback =
false,
44 this.udp.Client.Bind(
new IPEndPoint(Address, 0));
50 private KeyValuePair<IPAddress, IPAddress> FindDnsAddress()
52 IPAddress Result =
null;
53 IPAddress Local =
null;
56 for (Step = 0; Step < 2; Step++)
58 foreach (NetworkInterface Interface
in NetworkInterface.GetAllNetworkInterfaces())
60 if (Interface.OperationalStatus != OperationalStatus.Up)
63 if (Interface.NetworkInterfaceType == NetworkInterfaceType.Loopback)
66 IPInterfaceProperties Properties = Interface.GetIPProperties();
69 if ((c = Properties.DnsAddresses?.Count ?? 0) == 0 && Step == 0)
72 foreach (UnicastIPAddressInformation UnicastAddress
in Properties.UnicastAddresses)
74 IEnumerable<IPAddress> DnsAddresses = (IEnumerable<IPAddress>)Properties.DnsAddresses ?? defaultDnsAddresses;
76 foreach (IPAddress DnsAddress in DnsAddresses)
78 if (UnicastAddress.Address.AddressFamily != DnsAddress.AddressFamily)
82 Local = UnicastAddress.Address;
84 if (Result.AddressFamily == AddressFamily.InterNetwork)
85 return new KeyValuePair<IPAddress, IPAddress>(Result, Local);
91 if (!(Result is
null))
92 return new KeyValuePair<IPAddress, IPAddress>(Result, Local);
94 throw new NotSupportedException(
"No route to DNS server found.");
97 private async
void BeginReceive()
103 UdpReceiveResult Data = await this.udp.ReceiveAsync();
111 DnsMessage Message =
new DnsMessage(Data.Buffer);
116 await this.
Error(
"Unable to process DNS packet: " + ex.Message);
120 catch (ObjectDisposedException)
136 protected override Task
SendAsync(
byte[] Message, IPEndPoint Destination)
138 return this.udp.SendAsync(Message, Message.Length, Destination ??
this.dnsEndpoint);
Task Error(string Error)
Called to inform the viewer of an error state.
Task Exception(Exception Exception)
Called to inform the viewer of an exception state.
Task ReceiveBinary(byte[] Data)
Called when binary data has been received.
Abstract base class for DNS clients.
virtual void Init()
Called when DNS client is ready to be initialized.
virtual async Task ProcessIncomingMessage(DnsMessage Message)
Processes an incoming message.
bool disposed
If the object has been disposed
Implements a DNS UDP-based client.
override void Dispose()
IDisposable.Dispose
DnsUdpClient()
Implements a DNS UDP-based client.
override Task SendAsync(byte[] Message, IPEndPoint Destination)
Sends a message to a destination.
DNS resolver, as defined in:
const int DefaultDnsPort
53