Repository navigation
Expand file tree
/
Copy pathDnsMessageHandler.cs
More file actions
174 lines (149 loc) · 6.84 KB
/
Copy pathDnsMessageHandler.cs
File metadata and controls
174 lines (149 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using DnsClient.Protocol.Options;
namespace DnsClient
{
internal enum DnsMessageHandleType
{
None = 0,
UDP,
TCP
}
internal abstract class DnsMessageHandler
{
public abstract DnsMessageHandleType Type { get; }
public abstract DnsResponseMessage Query(IPEndPoint endpoint, DnsRequestMessage request, TimeSpan timeout);
public abstract Task<DnsResponseMessage> QueryAsync(
IPEndPoint endpoint,
DnsRequestMessage request,
CancellationToken cancellationToken);
// Transient errors will be retried on the same NameServer before the resolver moves on
// to the next configured NameServer (if any).
public static bool IsTransientException(Exception exception)
{
if (exception is IOException)
{
exception = exception?.InnerException ?? exception;
}
if (exception is SocketException socketException)
{
// I think those are reasonable socket errors which can be retried
// with the same NameServer. Hard to tell though and might change...
// Any other socket exception will cause the LookupClient to move on to the next server.
switch (socketException.SocketErrorCode)
{
case SocketError.TimedOut:
case SocketError.ConnectionAborted:
case SocketError.ConnectionReset:
case SocketError.OperationAborted:
case SocketError.TryAgain:
return true;
}
}
return false;
}
protected static void ValidateResponse(DnsRequestMessage request, DnsResponseMessage response)
{
if (request is null)
{
throw new ArgumentNullException(nameof(request));
}
if (response is null)
{
throw new ArgumentNullException(nameof(response));
}
if (request.Header.Id != response.Header.Id)
{
throw new DnsXidMismatchException(request.Header.Id, response.Header.Id);
}
}
public virtual void GetRequestData(DnsRequestMessage request, DnsDatagramWriter writer)
{
var question = request.Question;
/*
1 1 1 1 1 1
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ID |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|QR| Opcode |AA|TC|RD|RA| Z | RCODE |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| QDCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ANCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| NSCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| ARCOUNT |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
* */
// 4 more bytes for the type and class
writer.WriteInt16NetworkOrder((short)request.Header.Id);
writer.WriteUInt16NetworkOrder(request.Header.RawFlags);
writer.WriteInt16NetworkOrder(1); // we support single question only... (as most DNS servers anyways).
writer.WriteInt16NetworkOrder(0);
writer.WriteInt16NetworkOrder(0);
if (request.QuerySettings.UseExtendedDns)
{
writer.WriteInt16NetworkOrder(1); // one additional for the Opt record.
}
else
{
writer.WriteInt16NetworkOrder(0);
}
writer.WriteHostName(question.QueryName);
writer.WriteUInt16NetworkOrder((ushort)question.QuestionType);
writer.WriteUInt16NetworkOrder((ushort)question.QuestionClass);
if (request.QuerySettings.UseExtendedDns)
{
var opt = new OptRecord(size: request.QuerySettings.ExtendedDnsBufferSize, doFlag: request.QuerySettings.RequestDnsSecRecords);
writer.WriteHostName("");
writer.WriteUInt16NetworkOrder((ushort)opt.RecordType);
writer.WriteUInt16NetworkOrder((ushort)opt.RecordClass);
writer.WriteUInt32NetworkOrder((ushort)opt.InitialTimeToLive);
writer.WriteUInt16NetworkOrder(0);
}
}
public virtual DnsResponseMessage GetResponseMessage(ArraySegment<byte> responseData)
{
var reader = new DnsDatagramReader(responseData);
var factory = new DnsRecordFactory(reader);
var id = reader.ReadUInt16NetworkOrder();
var flags = reader.ReadUInt16NetworkOrder();
var questionCount = reader.ReadUInt16NetworkOrder();
var answerCount = reader.ReadUInt16NetworkOrder();
var nameServerCount = reader.ReadUInt16NetworkOrder();
var additionalCount = reader.ReadUInt16NetworkOrder();
var header = new DnsResponseHeader(id, flags, questionCount, answerCount, additionalCount, nameServerCount);
var response = new DnsResponseMessage(header, responseData.Count);
for (int questionIndex = 0; questionIndex < questionCount; questionIndex++)
{
var question = new DnsQuestion(reader.ReadQuestionQueryString(), (QueryType)reader.ReadUInt16NetworkOrder(), (QueryClass)reader.ReadUInt16NetworkOrder());
response.AddQuestion(question);
}
for (int answerIndex = 0; answerIndex < answerCount; answerIndex++)
{
var info = factory.ReadRecordInfo();
var record = factory.GetRecord(info);
response.AddAnswer(record);
}
for (int serverIndex = 0; serverIndex < nameServerCount; serverIndex++)
{
var info = factory.ReadRecordInfo();
var record = factory.GetRecord(info);
response.AddAuthority(record);
}
for (int additionalIndex = 0; additionalIndex < additionalCount; additionalIndex++)
{
var info = factory.ReadRecordInfo();
var record = factory.GetRecord(info);
response.AddAdditional(record);
}
return response;
}
}
}