MailSharp / MailSharp.DNS / Request.cs
Code · 38 lines · 919 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
38using MailSharp.DNS;

namespace MailSharp.DNS;

public sealed class Request
{
	private const int MaxUdpSize = 512;
	private readonly byte[] buffer = new byte[MaxUdpSize];
	private int length = 12; // start na header

	public DnsHeader Header { get; }

	public Request()
	{
        Header = new DnsHeader(buffer)
        {
            OPCODE = OPCode.Query,
            RD = true
        };
    }

	public void AddQuestion(QuestionRecord question)
	{
		// Eerst droog simuleren
		int nameBytes = DnsWriter.MeasureDomainName(question.Name);
		int needed = nameBytes + 4; // + DnsQType + DnsQClass

		if (length + needed > MaxUdpSize)
			throw new InvalidOperationException(
				$"DNS UDP packet would exceed 512 bytes ({length + needed} > {MaxUdpSize})");

		int written = question.WriteTo(buffer.AsSpan(length));
		length += written;
		Header.QDCount++;
	}

	public Memory<byte> Data => buffer.AsMemory(0, length);
}