Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
DnsUdpClient.cs
1using System;
3using System.Net;
4using System.Net.NetworkInformation;
6using System.Threading.Tasks;
7
9{
13 public class DnsUdpClient : DnsClient
14 {
15 private static readonly IPAddress[] defaultDnsAddresses = new IPAddress[]
16 {
17 IPAddress.Parse("8.8.8.8"), // Use Google Public DNS IP addresses as default, if no DNS addresses found.
18 IPAddress.Parse("8.8.4.4"),
19 IPAddress.Parse("2001:4860:4860::8888"),
20 IPAddress.Parse("2001:4860:4860::8844")
21 };
22
23 private readonly IPEndPoint dnsEndpoint;
24 private UdpClient udp = null;
25
29 public DnsUdpClient()
30 : base()
31 {
32 KeyValuePair<IPAddress, IPAddress> P = this.FindDnsAddress();
33 IPAddress Address = P.Value;
34 AddressFamily Family = Address.AddressFamily;
35
36 this.dnsEndpoint = new IPEndPoint(P.Key, DnsResolver.DefaultDnsPort);
37
38 this.udp = new UdpClient(Family)
39 {
40 //DontFragment = true,
41 MulticastLoopback = false,
42 Ttl = 30
43 };
44 this.udp.Client.Bind(new IPEndPoint(Address, 0));
45
46 this.BeginReceive();
47 this.Init();
48 }
49
50 private KeyValuePair<IPAddress, IPAddress> FindDnsAddress()
51 {
52 IPAddress Result = null;
53 IPAddress Local = null;
54 int Step;
55
56 for (Step = 0; Step < 2; Step++)
57 {
58 foreach (NetworkInterface Interface in NetworkInterface.GetAllNetworkInterfaces())
59 {
60 if (Interface.OperationalStatus != OperationalStatus.Up)
61 continue;
62
63 if (Interface.NetworkInterfaceType == NetworkInterfaceType.Loopback)
64 continue;
65
66 IPInterfaceProperties Properties = Interface.GetIPProperties();
67 int c;
68
69 if ((c = Properties.DnsAddresses?.Count ?? 0) == 0 && Step == 0)
70 continue;
71
72 foreach (UnicastIPAddressInformation UnicastAddress in Properties.UnicastAddresses)
73 {
74 IEnumerable<IPAddress> DnsAddresses = (IEnumerable<IPAddress>)Properties.DnsAddresses ?? defaultDnsAddresses;
75
76 foreach (IPAddress DnsAddress in DnsAddresses)
77 {
78 if (UnicastAddress.Address.AddressFamily != DnsAddress.AddressFamily)
79 continue;
80
81 Result = DnsAddress;
82 Local = UnicastAddress.Address;
83
84 if (Result.AddressFamily == AddressFamily.InterNetwork)
85 return new KeyValuePair<IPAddress, IPAddress>(Result, Local);
86 }
87 }
88 }
89 }
90
91 if (!(Result is null))
92 return new KeyValuePair<IPAddress, IPAddress>(Result, Local);
93
94 throw new NotSupportedException("No route to DNS server found.");
95 }
96
97 private async void BeginReceive() // Starts parallel task
98 {
99 try
100 {
101 while (!this.disposed)
102 {
103 UdpReceiveResult Data = await this.udp.ReceiveAsync();
104 if (this.disposed)
105 return;
106
107 this.ReceiveBinary(true, Data.Buffer);
108
109 try
110 {
111 DnsMessage Message = new DnsMessage(Data.Buffer);
112 await this.ProcessIncomingMessage(Message);
113 }
114 catch (Exception ex)
115 {
116 this.Error("Unable to process DNS packet: " + ex.Message);
117 }
118 }
119 }
120 catch (ObjectDisposedException)
121 {
122 // Closed.
123 }
124 catch (Exception ex)
125 {
126 this.Exception(ex);
127 }
128 }
129
138 protected override Task SendAsync(bool ConstantBuffer, byte[] Message, IPEndPoint Destination)
139 {
140 return this.udp.SendAsync(Message, Message.Length, Destination ?? this.dnsEndpoint);
141 }
142
144 public override void Dispose()
145 {
146 base.Dispose();
147
148 this.udp?.Dispose();
149 this.udp = null;
150 }
151
152 }
153}
void Exception(Exception Exception)
Called to inform the viewer of an exception state.
void Error(string Error)
Called to inform the viewer of an error state.
void ReceiveBinary(int Count)
Called when binary data has been received.
Abstract base class for DNS clients.
Definition: DnsClient.cs:22
virtual void Init()
Called when DNS client is ready to be initialized.
Definition: DnsClient.cs:65
virtual async Task ProcessIncomingMessage(DnsMessage Message)
Processes an incoming message.
Definition: DnsClient.cs:188
bool disposed
If the object has been disposed
Definition: DnsClient.cs:31
Implements a DNS UDP-based client.
Definition: DnsUdpClient.cs:14
override void Dispose()
IDisposable.Dispose
DnsUdpClient()
Implements a DNS UDP-based client.
Definition: DnsUdpClient.cs:29
override Task SendAsync(bool ConstantBuffer, byte[] Message, IPEndPoint Destination)
Sends a message to a destination.
DNS resolver, as defined in:
Definition: DnsResolver.cs:32