MailSharp / MailSharp.DNS / Records / RecordMX.cs
Code · 48 lines · 1422 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
48using MailSharp.DNS.Records;

namespace MailSharp.DNS.Records;

/*
3.3.9. MX RDATA format

	+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
	|                  PREFERENCE                   |
	+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
	/                   EXCHANGE                    /
	/                                               /
	+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+

where:

PREFERENCE      A 16 bit integer which specifies the preference given to
				this RR among others at the same owner.  Lower values
				are preferred.

EXCHANGE        A <domain-name> which specifies a host willing to act as
				a mail exchange for the owner name.

MX records cause type A additional section processing for the host
specified by EXCHANGE.  The use of MX RRs is explained in detail in
[RFC-974].
*/


public record RecordMX(RecordReader rr) : DnsRecord(rr), IComparable<RecordMX>
{
	public ushort Preference { get; } = rr.ReadUInt16();
	public string Exchange { get; } = rr.ReadDomainName();

	public override string ToString() => $"{Preference} {Exchange}";

	public int CompareTo(RecordMX? other)
	{
		if (other is not RecordMX recordMX)
			return -1;
		else if (Preference > recordMX.Preference)
			return 1;
		else if (Preference < recordMX.Preference)
			return -1;
		else // they are the same, now compare case insensitive names
			return string.Compare(Exchange, recordMX.Exchange, true);
	}
}