MailSharp / MailSharp.DNS / Records / RecordNSEC.cs
Code · 57 lines · 1549 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/*
 * https://www.rfc-editor.org/rfc/rfc4034.html
NSEC RDATA Wire Format

   The RDATA of the NSEC RR is as shown below:

                        1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   /                      Next Domain Name                         /
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   /                       Type Bit Maps                           /
   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 */

using MailSharp.DNS.Records;

namespace MailSharp.DNS.Records;

public record RecordNSEC : DnsRecord
{
	public string NextDomainName { get;init; }
	public List<DnsType> TypeBitMaps { get; init; } = [];

	public RecordNSEC(RecordReader rr) : base(rr)
	{
		// re-read length
		ushort rdLength = rr.ReadUInt16(-2);

		NextDomainName = rr.ReadDomainName();

		while (rr.Position < rdLength)
		{
			byte windowBlock = rr.ReadByte();// 0�255
			byte bitmapLength = rr.ReadByte();// 1�32
			for (int i = 0; i < bitmapLength * 8; i++)
			{
				if ((rr.ReadByte() & (0x80 >> (i % 8))) != 0)
				{
					TypeBitMaps.Add((DnsType)(windowBlock * 256 + i));
				}
			}
		}
	}

	public override string ToString()
	{
		if (TypeBitMaps.Count == 0)
			return $"{NextDomainName}";

		// Sorteer op type-nummer (verplicht in presentatieformaat)
		var sorted = TypeBitMaps.OrderBy(t => (ushort)t);

		return $"{NextDomainName} {string.Join(" ", sorted)}";
	}

}