Skip to content

Commit

Permalink
Removes trailing null terminator when <character-string> is being enc…
Browse files Browse the repository at this point in the history
…oded.
  • Loading branch information
alanedwardes committed Mar 30, 2024
1 parent 3448fb1 commit 02d0403
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/Ae.Dns.Protocol/DnsByteExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,17 @@ public static void ToBytes(string value, Memory<byte> buffer, ref int offset)
offset += 1 + length;
}

public static void ToBytes(ReadOnlySpan<string> strings, Memory<byte> buffer, ref int offset)
public static void ToBytes(ReadOnlySpan<string> strings, Memory<byte> 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<byte> buffer, ref int offset)
Expand Down
6 changes: 5 additions & 1 deletion src/Ae.Dns.Protocol/Records/DnsStringResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public bool Equals(DnsStringResource? other)

/// <summary>
/// Describes whether this string resource can use string compression.
/// If this resource describes a &lt;domain-name&gt; as per RFC 1035 terminology,
/// then compression can be used. If it describes a &lt;character-string&gt; as per
/// RFC 1035 terminology, compression cannot be used. In addition, &lt;domain-name&gt;
/// entries end in a null terminator, whereas &lt;character-string&gt; resources do not.
/// </summary>
protected abstract bool CanUseCompression { get; }

Expand All @@ -49,7 +53,7 @@ public virtual void ReadBytes(ReadOnlyMemory<byte> bytes, ref int offset, int le
/// <inheritdoc/>
public virtual void WriteBytes(Memory<byte> bytes, ref int offset)
{
DnsByteExtensions.ToBytes(Entries.ToArray(), bytes, ref offset);
DnsByteExtensions.ToBytes(Entries.ToArray(), bytes, ref offset, !CanUseCompression);
}
}
}

0 comments on commit 02d0403

Please sign in to comment.