MailSharp / MailSharp.DNS / QuestionRecord.cs
Code · 68 lines · 2318 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
68using System.Buffers.Binary;

namespace MailSharp.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 sealed class QuestionRecord : DnsResourceRecord<DnsQType, DnsQClass>
{
	public QuestionRecord(string name, DnsQType type, DnsQClass @class = DnsQClass.IN)
		: base(name, type, @class)
	{
	}

	// Parsing constructor
	public QuestionRecord(ref RecordReader rr)
	{
		Name = rr.ReadDomainName();
		Type = (DnsQType)rr.ReadUInt16();
		Class = (DnsQClass)rr.ReadUInt16();
	}

	// Schrijft direct naar een Span<byte> → zero alloc
	public int WriteTo(Span<byte> destination)
	{
		int written = DnsWriter.WriteDomainName(destination, Name);
		BinaryPrimitives.WriteUInt16BigEndian(destination.Slice(written, 2), (ushort)Type);
		BinaryPrimitives.WriteUInt16BigEndian(destination.Slice(written + 2, 2), (ushort)Class);
		return written + 4;
	}
}