-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathDnsNAPTRRecordData.cs
More file actions
278 lines (212 loc) · 9.52 KB
/
DnsNAPTRRecordData.cs
File metadata and controls
278 lines (212 loc) · 9.52 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
Technitium Library
Copyright (C) 2024 Shreyas Zare (shreyas@technitium.com)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using TechnitiumLibrary.IO;
namespace TechnitiumLibrary.Net.Dns.ResourceRecords
{
public class DnsNAPTRRecordData : DnsResourceRecordData
{
#region variables
ushort _order;
ushort _preference;
string _flags;
string _services;
string _regexp;
string _replacement;
byte[] _rData;
#endregion
#region constructor
public DnsNAPTRRecordData(ushort order, ushort preference, string flags, string services, string regexp, string replacement)
{
ArgumentNullException.ThrowIfNull(flags);
ArgumentNullException.ThrowIfNull(services);
ArgumentNullException.ThrowIfNull(regexp);
ArgumentNullException.ThrowIfNull(replacement);
// RFC 3403: REGEXP and REPLACEMENT are mutually exclusive
// If both are present (non-empty), the record is in error and MUST be rejected or ignored.
if (regexp.Length > 0 && replacement.Length > 0)
throw new ArgumentException(
"REGEXP and REPLACEMENT are mutually exclusive per RFC 3403.");
// RFC 3403: FLAGS validation
// Flags are single characters from A–Z and 0–9, case-insensitive.
for (int i = 0; i < flags.Length; i++)
{
char c = flags[i];
if (!(char.IsAsciiLetter(c) || char.IsAsciiDigit(c)))
throw new ArgumentException(
$"Invalid NAPTR flag '{c}'. Allowed set is A–Z and 0–9.",
nameof(flags));
}
// RFC 3403: SERVICES is a DNS <character-string>
// RFC intentionally does NOT define semantics here; only basic sanity checks.
// Enforce non-control UTF-16 chars; deeper validation is application-specific.
for (int i = 0; i < services.Length; i++)
{
if (char.IsControl(services[i]))
throw new ArgumentException(
"SERVICES contains control characters, which are not permitted.",
nameof(services));
}
// RFC 3403: REPLACEMENT must be a fully qualified domain name
if (replacement.Length > 0)
{
// Must end with a root label (trailing dot)
// But zoneFile.PopDomainAsync() removes the trailing dot.
// Therefore the check is useless.
// No name compression, no empty labels except the root
if (replacement.Contains("..", StringComparison.Ordinal))
throw new ArgumentException(
"REPLACEMENT contains empty DNS labels.",
nameof(replacement));
}
// RFC 3403: REGEXP sanity
// The RFC requires POSIX ERE semantics but does not mandate compile-time validation.
// Enforce only that it is non-empty when used.
if (regexp.Length == 0 && replacement.Length == 0)
throw new ArgumentException(
"Either REGEXP or REPLACEMENT must be specified per RFC 3403.");
// DNS <character-string> constraints
if (DnsClient.IsDomainNameUnicode(replacement))
replacement = DnsClient.ConvertDomainNameToAscii(replacement);
DnsClient.IsDomainNameValid(replacement, true);
_order = order;
_preference = preference;
_flags = flags;
_services = services;
_regexp = regexp;
_replacement = replacement;
Serialize();
}
public DnsNAPTRRecordData(Stream s)
: base(s)
{ }
#endregion
#region private
private void Serialize()
{
using (MemoryStream mS = new MemoryStream(UncompressedLength))
{
DnsDatagram.WriteUInt16NetworkOrder(_order, mS);
DnsDatagram.WriteUInt16NetworkOrder(_preference, mS);
mS.WriteShortString(_flags, Encoding.ASCII);
mS.WriteShortString(_services, Encoding.ASCII);
mS.WriteShortString(_regexp, Encoding.ASCII);
DnsDatagram.SerializeDomainName(_replacement, mS);
_rData = mS.ToArray();
}
}
#endregion
#region protected
protected override void ReadRecordData(Stream s)
{
_rData = s.ReadExactly(_rdLength);
using (MemoryStream mS = new MemoryStream(_rData))
{
_order = DnsDatagram.ReadUInt16NetworkOrder(mS);
_preference = DnsDatagram.ReadUInt16NetworkOrder(mS);
_flags = mS.ReadShortString(Encoding.ASCII);
_services = mS.ReadShortString(Encoding.ASCII);
_regexp = mS.ReadShortString(Encoding.ASCII);
_replacement = DnsDatagram.DeserializeDomainName(mS);
}
}
protected override void WriteRecordData(Stream s, List<DnsDomainOffset> domainEntries, bool canonicalForm)
{
s.Write(_rData);
}
#endregion
#region internal
internal static async Task<DnsNAPTRRecordData> FromZoneFileEntryAsync(ZoneFile zoneFile)
{
Stream rdata = await zoneFile.GetRData();
if (rdata is not null)
return new DnsNAPTRRecordData(rdata);
ushort order = ushort.Parse(await zoneFile.PopItemAsync());
ushort preference = ushort.Parse(await zoneFile.PopItemAsync());
string flags = DnsDatagram.DecodeCharacterString(await zoneFile.PopItemAsync());
string services = DnsDatagram.DecodeCharacterString(await zoneFile.PopItemAsync());
string regexp = DnsDatagram.DecodeCharacterString(await zoneFile.PopItemAsync());
string replacement = await zoneFile.PopDomainAsync();
return new DnsNAPTRRecordData(order, preference, flags, services, regexp, replacement);
}
internal override string ToZoneFileEntry(string originDomain = null)
{
return _order + " " + _preference + " " + DnsDatagram.EncodeCharacterString(_flags) + " " + DnsDatagram.EncodeCharacterString(_services) + " " + DnsDatagram.EncodeCharacterString(_regexp) + " " + DnsResourceRecord.GetRelativeDomainName(_replacement, originDomain);
}
#endregion
#region public
public override bool Equals(object obj)
{
if (obj is null)
return false;
if (ReferenceEquals(this, obj))
return true;
if (obj is DnsNAPTRRecordData other)
{
if (_order != other._order)
return false;
if (_preference != other._preference)
return false;
if (!_flags.Equals(other._flags, StringComparison.Ordinal))
return false;
if (!_services.Equals(other._services, StringComparison.Ordinal))
return false;
if (!_regexp.Equals(other._regexp, StringComparison.Ordinal))
return false;
if (!_replacement.Equals(other._replacement, StringComparison.Ordinal))
return false;
return true;
}
return false;
}
public override int GetHashCode()
{
return HashCode.Combine(_order, _preference, _flags, _services, _regexp, _replacement);
}
public override void SerializeTo(Utf8JsonWriter jsonWriter)
{
jsonWriter.WriteStartObject();
jsonWriter.WriteNumber("Order", _order);
jsonWriter.WriteNumber("Preference", _preference);
jsonWriter.WriteString("Flags", _flags);
jsonWriter.WriteString("Services", _services);
jsonWriter.WriteString("Regexp", _regexp);
jsonWriter.WriteString("Replacement", _replacement);
jsonWriter.WriteEndObject();
}
#endregion
#region properties
public ushort Order
{ get { return _order; } }
public ushort Preference
{ get { return _preference; } }
public string Flags
{ get { return _flags; } }
public string Services
{ get { return _services; } }
public string Regexp
{ get { return _regexp; } }
public string Replacement
{ get { return _replacement; } }
public override int UncompressedLength
{ get { return 2 + 2 + 1 + _flags.Length + 1 + _services.Length + 1 + _regexp.Length + DnsDatagram.GetSerializeDomainNameLength(_replacement); } }
#endregion
}
}