Skip to content

Commit

Permalink
fix(wire-format): allow UTF8 encoding for domain name
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Jul 22, 2019
1 parent 7384ded commit bbeba2d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
6 changes: 1 addition & 5 deletions src/WireReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,7 @@ List<string> ReadLabels()

// Read current label and remaining labels.
var buffer = ReadBytes(length);
if (buffer.Any(c => c > 0x7F))
{
throw new InvalidDataException("Only ASCII characters are allowed.");
}
labels.Add(Encoding.ASCII.GetString(buffer, 0, length));
labels.Add(Encoding.UTF8.GetString(buffer, 0, length));
labels.AddRange(ReadLabels());

// Add to compressed names.
Expand Down
3 changes: 2 additions & 1 deletion src/WireWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ public void WriteDomainName(DomainName name, bool uncompressed = false)
}

// Add the label
WriteString(label);
var bytes = Encoding.UTF8.GetBytes(label);
WriteByteLengthPrefixedBytes(bytes);
}

stream.WriteByte(0); // terminating byte
Expand Down
13 changes: 13 additions & 0 deletions test/MessageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,5 +311,18 @@ public void Stringify_Edns()
var text = m.ToString();
Console.WriteLine(text);
}

[TestMethod]
public void AppleMessage()
{
// A MDNS query from an Apple Host. It contains a UTF8 domain name
// and an EDNS OPT-4 option.
var sample = "AAAAAAAGAAAAAAABCF9ob21la2l0BF90Y3AFbG9jYWwAAAyAAQ9fY29tcGFuaW9uLWxpbmvAFQAMgAEIX2FpcnBsYXnAFQAMgAEFX3Jhb3DAFQAMgAEbQ2hyaXN0b3BoZXLigJlzIE1hY0Jvb2sgUHJvwCUAEIABDF9zbGVlcC1wcm94eQRfdWRwwBoADAABAAApBaAAABGUABIABAAOAJB6e4qbc5l4e4qbc5k=";
var buffer1 = Convert.FromBase64String(sample);
var m = new Message();
m.Read(buffer1);

Assert.AreEqual("Christopher’s MacBook Pro", m.Questions[4].Name.Labels[0]);
}
}
}

0 comments on commit bbeba2d

Please sign in to comment.