-
Notifications
You must be signed in to change notification settings - Fork 0
/
Question.cs
129 lines (111 loc) · 3.16 KB
/
Question.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
using System;
using System.IO;
using System.Collections.Generic;
using System.Net;
using System.Text;
namespace Heijden.DNS
{
#region Rfc 1034/1035
/*
4.1.2. Question section format
The question section is used to carry the "question" in most queries,
i.e., the parameters that define what is being asked. The section
contains QDCOUNT (usually 1) entries, each of the following format:
1 1 1 1 1 1
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| |
/ QNAME /
/ /
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| QTYPE |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| QCLASS |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
where:
QNAME a domain name represented as a sequence of labels, where
each label consists of a length octet followed by that
number of octets. The domain name terminates with the
zero length octet for the null label of the root. Note
that this field may be an odd number of octets; no
padding is used.
QTYPE a two octet code which specifies the type of the query.
The values for this field include all codes valid for a
TYPE field, together with some more general codes which
can match more than one type of RR.
QCLASS a two octet code that specifies the class of the query.
For example, the QCLASS field is IN for the Internet.
*/
#endregion
public class Question
{
private string m_QName;
public string QName
{
get
{
return m_QName;
}
set
{
m_QName = value;
if (!m_QName.EndsWith("."))
m_QName += ".";
}
}
public QType QType;
public QClass QClass;
public Question(string QName,QType QType,QClass QClass)
{
this.QName = QName;
this.QType = QType;
this.QClass = QClass;
}
public Question(RecordReader rr)
{
QName = rr.ReadDomainName();
QType = (QType)rr.ReadUInt16();
QClass = (QClass)rr.ReadUInt16();
}
private byte[] WriteName(string src)
{
if (!src.EndsWith("."))
src += ".";
if (src == ".")
return new byte[1];
StringBuilder sb = new StringBuilder();
int intI, intJ, intLen = src.Length;
sb.Append('\0');
for (intI = 0, intJ = 0; intI < intLen; intI++, intJ++)
{
sb.Append(src[intI]);
if (src[intI] == '.')
{
sb[intI - intJ] = (char)(intJ & 0xff);
intJ = -1;
}
}
sb[sb.Length - 1] = '\0';
return System.Text.Encoding.ASCII.GetBytes(sb.ToString());
}
public byte[] Data
{
get
{
List<byte> data = new List<byte>();
data.AddRange(WriteName(QName));
data.AddRange(WriteShort((ushort)QType));
data.AddRange(WriteShort((ushort)QClass));
return data.ToArray();
}
}
private byte[] WriteShort(ushort sValue)
{
return BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)sValue));
}
public override string ToString()
{
return string.Format("{0,-32}\t{1}\t{2}", QName, QClass, QType);
}
}
}