From 02d04033e24841965816b871b89532d186206ceb Mon Sep 17 00:00:00 2001 From: Alan Edwardes Date: Sat, 30 Mar 2024 21:50:16 +0000 Subject: [PATCH] Removes trailing null terminator when is being encoded. --- src/Ae.Dns.Protocol/DnsByteExtensions.cs | 7 +++++-- src/Ae.Dns.Protocol/Records/DnsStringResource.cs | 6 +++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Ae.Dns.Protocol/DnsByteExtensions.cs b/src/Ae.Dns.Protocol/DnsByteExtensions.cs index 561a988..239bb9a 100644 --- a/src/Ae.Dns.Protocol/DnsByteExtensions.cs +++ b/src/Ae.Dns.Protocol/DnsByteExtensions.cs @@ -205,14 +205,17 @@ public static void ToBytes(string value, Memory buffer, ref int offset) offset += 1 + length; } - public static void ToBytes(ReadOnlySpan strings, Memory buffer, ref int offset) + public static void ToBytes(ReadOnlySpan strings, Memory buffer, ref int offset, bool nullTerminator = true) { for (int i = 0; i < strings.Length; i++) { ToBytes(strings[i], buffer, ref offset); } - buffer.Span[offset++] = 0; + if (nullTerminator) + { + buffer.Span[offset++] = 0; + } } public static void ToBytes(int value, Memory buffer, ref int offset) diff --git a/src/Ae.Dns.Protocol/Records/DnsStringResource.cs b/src/Ae.Dns.Protocol/Records/DnsStringResource.cs index c76cd22..8e8cdd4 100644 --- a/src/Ae.Dns.Protocol/Records/DnsStringResource.cs +++ b/src/Ae.Dns.Protocol/Records/DnsStringResource.cs @@ -35,6 +35,10 @@ public bool Equals(DnsStringResource? other) /// /// Describes whether this string resource can use string compression. + /// If this resource describes a <domain-name> as per RFC 1035 terminology, + /// then compression can be used. If it describes a <character-string> as per + /// RFC 1035 terminology, compression cannot be used. In addition, <domain-name> + /// entries end in a null terminator, whereas <character-string> resources do not. /// protected abstract bool CanUseCompression { get; } @@ -49,7 +53,7 @@ public virtual void ReadBytes(ReadOnlyMemory bytes, ref int offset, int le /// public virtual void WriteBytes(Memory bytes, ref int offset) { - DnsByteExtensions.ToBytes(Entries.ToArray(), bytes, ref offset); + DnsByteExtensions.ToBytes(Entries.ToArray(), bytes, ref offset, !CanUseCompression); } } }