-
-
Notifications
You must be signed in to change notification settings - Fork 131
/
DnsQueryResponse.cs
185 lines (150 loc) · 6.26 KB
/
DnsQueryResponse.cs
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
// Copyright 2024 Michael Conrad.
// Licensed under the Apache License, Version 2.0.
// See LICENSE file for details.
using System;
using System.Collections.Generic;
using System.Linq;
using DnsClient.Protocol;
namespace DnsClient
{
internal class TruncatedQueryResponse : IDnsQueryResponse
{
public IReadOnlyList<DnsQuestion> Questions => throw new NotImplementedException();
public IReadOnlyList<DnsResourceRecord> Additionals => throw new NotImplementedException();
public IEnumerable<DnsResourceRecord> AllRecords => throw new NotImplementedException();
public IReadOnlyList<DnsResourceRecord> Answers => throw new NotImplementedException();
public IReadOnlyList<DnsResourceRecord> Authorities => throw new NotImplementedException();
public string AuditTrail => throw new NotImplementedException();
public string ErrorMessage => throw new NotImplementedException();
public bool HasError => throw new NotImplementedException();
public DnsResponseHeader Header => throw new NotImplementedException();
public int MessageSize => throw new NotImplementedException();
public NameServer NameServer => throw new NotImplementedException();
public DnsQuerySettings Settings => throw new NotImplementedException();
}
/// <summary>
/// The response returned by any query performed by <see cref="IDnsQuery"/> with all answer sections, header and message information.
/// </summary>
/// <seealso cref="IDnsQuery"/>
/// <seealso cref="ILookupClient"/>
public class DnsQueryResponse : IDnsQueryResponse
{
private int? _hashCode;
/// <summary>
/// Gets the name server which responded with this result.
/// </summary>
/// <value>
/// The name server.
/// </value>
public NameServer NameServer { get; }
/// <summary>
/// Gets a list of additional records.
/// </summary>
public IReadOnlyList<DnsResourceRecord> Additionals { get; }
/// <summary>
/// Gets a list of all answers, additional and authority records.
/// </summary>
public IEnumerable<DnsResourceRecord> AllRecords
{
get
{
return Answers.Concat(Additionals).Concat(Authorities);
}
}
/// <summary>
/// Gets the audit trail if <see cref="DnsQueryOptions.EnableAuditTrail"/>. as set to <c>true</c>, <c>null</c> otherwise.
/// </summary>
/// <value>
/// The audit trail.
/// </value>
public string AuditTrail { get; private set; }
/// <summary>
/// Gets a list of answer records.
/// </summary>
public IReadOnlyList<DnsResourceRecord> Answers { get; }
/// <summary>
/// Gets a list of authority records.
/// </summary>
public IReadOnlyList<DnsResourceRecord> Authorities { get; }
/// <summary>
/// Returns a string value representing the error response code in case an error occurred,
/// otherwise '<see cref="DnsResponseCode.NoError"/>'.
/// </summary>
public string ErrorMessage => DnsResponseCodeText.GetErrorText((DnsResponseCode)Header.ResponseCode);
/// <summary>
/// A flag indicating if the header contains a response code other than <see cref="DnsResponseCode.NoError"/>.
/// </summary>
public bool HasError => Header?.ResponseCode != DnsHeaderResponseCode.NoError;
/// <summary>
/// Gets the header of the response.
/// </summary>
public DnsResponseHeader Header { get; }
/// <summary>
/// Gets the list of questions.
/// </summary>
public IReadOnlyList<DnsQuestion> Questions { get; }
/// <summary>
/// Gets the size of the message.
/// </summary>
/// <value>
/// The size of the message.
/// </value>
public int MessageSize { get; }
/// <summary>
/// Gets the settings used to produce this response.
/// </summary>
public DnsQuerySettings Settings { get; }
internal DnsQueryResponse(DnsResponseMessage dnsResponseMessage, NameServer nameServer, DnsQuerySettings settings)
{
if (dnsResponseMessage == null)
{
throw new ArgumentNullException(nameof(dnsResponseMessage));
}
Header = dnsResponseMessage.Header;
MessageSize = dnsResponseMessage.MessageSize;
Questions = dnsResponseMessage.Questions.ToArray();
Answers = dnsResponseMessage.Answers.ToArray();
Additionals = dnsResponseMessage.Additionals.ToArray();
Authorities = dnsResponseMessage.Authorities.ToArray();
NameServer = nameServer ?? throw new ArgumentNullException(nameof(nameServer));
Settings = settings;
}
/// <inheritdoc />
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
if (obj is not DnsQueryResponse response)
{
return false;
}
return
Header.ToString().Equals(response.Header.ToString(), StringComparison.OrdinalIgnoreCase)
&& string.Join("", Questions).Equals(string.Join("", response.Questions), StringComparison.OrdinalIgnoreCase)
&& string.Join("", AllRecords).Equals(string.Join("", response.AllRecords), StringComparison.OrdinalIgnoreCase);
}
/// <inheritdoc />
public override int GetHashCode()
{
if (!_hashCode.HasValue)
{
var value = (Header.ToString() + string.Join("", Questions) + string.Join("", AllRecords));
#if NET6_0_OR_GREATER || NETSTANDARD2_1
_hashCode = value.GetHashCode(StringComparison.Ordinal);
#else
_hashCode = value.GetHashCode();
#endif
}
return _hashCode.Value;
}
internal static void SetAuditTrail(IDnsQueryResponse response, string value)
{
if (response is DnsQueryResponse queryResponse)
{
queryResponse.AuditTrail = value;
}
}
}
}