I'm creating my own DNS server and host blocker, I want to get host from DNS request message byte[]
dns message hex dump:
e07901000001000000000000057961686f6f03636f6d0000010001
.y...........yahoo.com.....
code:
using System;
using System.Text;
public class Program
{
public static void Main()
{
string b64 = "4HkBAAABAAAAAAAABXlhaG9vA2NvbQAAAQAB";
int pad = b64.Length % 4;
if (pad > 0 )
{
b64 += new string('=', 4 - pad);
}
byte[] decoded = Convert.FromBase64String(b64);
int start = 13;
int end = start;
while(decoded[end] != 0){
end++;
}
int hostLength = end-start;
byte[] byteHost = new byte[hostLength];
Array.Copy(decoded, start, byteHost, 0, hostLength);
string host = Encoding.Default.GetString(byteHost);
Console.WriteLine(host); // yahoo♥com
}
}
The questions:
- is my method above to get host name right/efficient/fastest ?
- why I get weird character replacing the dot
yahoo♥com?
change to Encoding.ASCII or Encoding.UTF8 has no effect
new string(decoded.Select(b => (char)b).Where(char.IsLetterOrDigit).ToArray())? You can replaceIsLetterOrDigitby any logic.yahoo♥comand the snippetàyyahoocom. What do you expect in fact ?yahoo.com?