Code
·
219 lines
·
6777 bytes
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219using MailSharp.DNS;
using System.Buffers.Binary;
namespace MailSharp.DNS;
#region RFC specification
/*
4.1.1. Header section format
The header contains the following fields:
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 |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
where:
ID A 16 bit identifier assigned by the program that
generates any kind of query. This identifier is copied
the corresponding reply and can be used by the requester
to match up replies to outstanding queries.
QR A one bit field that specifies whether this message is a
query (0), or a response (1).
OPCODE A four bit field that specifies kind of query in this
message. This value is set by the originator of a query
and copied into the response. The values are:
0 a standard query (QUERY)
1 an inverse query (IQUERY)
2 a server status request (STATUS)
3-15 reserved for future use
AA Authoritative Answer - this bit is valid in responses,
and specifies that the responding name server is an
authority for the domain name in question section.
Note that the contents of the answer section may have
multiple owner names because of aliases. The AA bit
corresponds to the name which matches the query name, or
the first owner name in the answer section.
TC TrunCation - specifies that this message was truncated
due to length greater than that permitted on the
transmission channel.
RD Recursion Desired - this bit may be set in a query and
is copied into the response. If RD is set, it directs
the name server to pursue the query recursively.
Recursive query support is optional.
RA Recursion Available - this be is set or cleared in a
response, and denotes whether recursive query support is
available in the name server.
Z Reserved for future use. Must be zero in all queries
and responses.
RCODE Response code - this 4 bit field is set as part of
responses. The values have the following
interpretation:
0 No error condition
1 Format error - The name server was
unable to interpret the query.
2 Server failure - The name server was
unable to process this query due to a
problem with the name server.
3 Name Error - Meaningful only for
responses from an authoritative name
server, this code signifies that the
domain name referenced in the query does
not exist.
4 Not Implemented - The name server does
not support the requested kind of query.
5 Refused - The name server refuses to
perform the specified operation for
policy reasons. For example, a name
server may not wish to provide the
information to the particular requester,
or a name server may not wish to perform
a particular operation (e.g., zone
transfer) for particular data.
6-15 Reserved for future use.
QDCOUNT an unsigned 16 bit integer specifying the number of
entries in the question section.
ANCOUNT an unsigned 16 bit integer specifying the number of
resource records in the answer section.
NSCOUNT an unsigned 16 bit integer specifying the number of name
server resource records in the authority records
section.
ARCOUNT an unsigned 16 bit integer specifying the number of
resource records in the additional records section.
*/
#endregion
public sealed class DnsHeader
{
private readonly Memory<byte> memory;
private Span<byte> Span => memory.Span;
public DnsHeader(byte[] buffer)
{
ArgumentNullException.ThrowIfNull(buffer);
if (buffer.Length < 12)
throw new ArgumentException("Buffer too small for DNS header", nameof(buffer));
memory = buffer.AsMemory(0,12);
}
public ushort ID
{
get => BinaryPrimitives.ReadUInt16BigEndian(Span.Slice(0, 2));
set => BinaryPrimitives.WriteUInt16BigEndian(Span.Slice(0, 2), value);
}
private ushort Flags
{
get => BinaryPrimitives.ReadUInt16BigEndian(Span.Slice(2, 2));
set => BinaryPrimitives.WriteUInt16BigEndian(Span.Slice(2, 2), value);
}
public ushort QDCount
{
get => BinaryPrimitives.ReadUInt16BigEndian(Span.Slice(4, 2));
set => BinaryPrimitives.WriteUInt16BigEndian(Span.Slice(4, 2), value);
}
public ushort ANCount
{
get => BinaryPrimitives.ReadUInt16BigEndian(Span.Slice(6, 2));
//set => BinaryPrimitives.WriteUInt16BigEndian(Span.Slice(6, 2), value);
}
public ushort NSCount
{
get => BinaryPrimitives.ReadUInt16BigEndian(Span.Slice(8, 2));
set => BinaryPrimitives.WriteUInt16BigEndian(Span.Slice(8, 2), value);
}
public ushort ARCount => BinaryPrimitives.ReadUInt16BigEndian(Span.Slice(10, 2));
// Bit fields � perfect zoals je ze hebt
public bool QR
{
get => (Flags & 0x8000) != 0;
set => Flags = (ushort)(value ? Flags | 0x8000 : Flags & ~0x8000);
}
public OPCode OPCODE
{
get => (OPCode)((Flags >> 11) & 0xF);
set => Flags = (ushort)(Flags & ~0x7800 | ((ushort)value << 11));
}
public bool AA
{
get => (Flags & 0x0400) != 0;
set => Flags = (ushort)(value ? Flags | 0x0400 : Flags & ~0x0400);
}
public bool TC
{
get => (Flags & 0x0200) != 0;
set => Flags = (ushort)(value ? Flags | 0x0200 : Flags & ~0x0200);
}
public bool RD
{
get => (Flags & 0x0100) != 0;
set => Flags = (ushort)(value ? Flags | 0x0100 : Flags & ~0x0100);
}
public bool RA
{
get => (Flags & 0x0080) != 0;
set => Flags = (ushort)(value ? Flags | 0x0080 : Flags & ~0x0080);
}
public ushort Z
{
get => (ushort)((Flags >> 4) & 0x7);
set => Flags = (ushort)(Flags & ~0x0070 | ((value & 0x7) << 4));
}
public RCode RCODE
{
get => (RCode)(Flags & 0x000F);
set => Flags = (ushort)(Flags & ~0x000F | (ushort)value);
}
public ReadOnlySpan<byte> AsReadOnlySpan() => Span;
}