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;
2using System.Collections.Generic;
3using System.Net;
4using System.Net.NetworkInformation;
5using System.Net.Sockets;
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 await this.ReceiveBinary(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 await 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 await this.Exception(ex);
127 }
128 }
129
136 protected override Task SendAsync(byte[] Message, IPEndPoint Destination)
137 {
138 return this.udp.SendAsync(Message, Message.Length, Destination ?? this.dnsEndpoint);
139 }
140
142 public override void Dispose()
143 {
144 base.Dispose();
145
146 this.udp?.Dispose();
147 this.udp = null;
148 }
149
150 }
151}
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.
Definition: DnsClient.cs:21
virtual void Init()
Called when DNS client is ready to be initialized.
Definition: DnsClient.cs:63
virtual async Task ProcessIncomingMessage(DnsMessage Message)
Processes an incoming message.
Definition: DnsClient.cs:181
bool disposed
If the object has been disposed
Definition: DnsClient.cs:30
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(byte[] Message, IPEndPoint Destination)
Sends a message to a destination.
DNS resolver, as defined in:
Definition: DnsResolver.cs:32