private byte[] BuildQuery(string host)
{
string newHost;
int newLocation = 0;
int oldLocation = 0;
MemoryStream ms = new MemoryStream();
host = host.Trim();
// decide how to build this query based on type
switch (_nsType)
{
case NsType.PTR:
IPAddress queryIP = IPAddress.Parse(host);
// pointer should be translated as follows
// 209.115.22.3 -> 3.22.115.209.in-addr.arpa
char[] ipDelim = new char[] {'.'};
string[] s = host.Split(ipDelim,4);
newHost = String.Format("{0}.{1}.{2}.{3}.in-addr.arpa", s[3], s[2], s[1], s[0]);
break;
default:
newHost = host;
break;
}
// Package up the host
while(oldLocation < newHost.Length)
{
newLocation = newHost.IndexOf(".", oldLocation);
if (newLocation == -1) newLocation = newHost.Length;
byte subDomainLength = (byte)(newLocation - oldLocation);
char[] sub = newHost.Substring(oldLocation, subDomainLength).ToCharArray();
ms.WriteByte(subDomainLength);
ms.Write(Encoding.ASCII.GetBytes(sub, 0, sub.Length), 0, sub.Length);
oldLocation = newLocation + 1;
}
// Terminate the domain name w/ a 0x00.
ms.WriteByte(0x00);
return ms.ToArray();
}