public static DnsQuery ReadDnsQuery(BinaryReader reader) { String domainName = ReadDomainName(reader); QueryType qtype = (QueryType)ReadUInt16BE(reader); QueryClass qclass = (QueryClass)ReadUInt16BE(reader); DnsQuery query = new DnsQuery(domainName, qtype, qclass); return(query); }
/* 4.1.2. Question section format * 1 1 1 1 1 1 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | | | / QNAME / | / / +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | QTYPE | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | QCLASS | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | | QNAME | a domain name represented as a sequence of labels, where | each label consists of a length octet followed by that | number of octets. The domain name terminates with the | zero length octet for the null label of the root. Note | that this field may be an odd number of octets; no | padding is used. */ public static byte[] EncodeDnsQuery(DnsQuery query) { MemoryStream stream = new MemoryStream(256); BinaryWriter writer = new BinaryWriter(stream); //QNAME string[] labels = query.DomainName.Split('.'); foreach (string label in labels) { writer.Write((byte)label.Length); writer.Write(Encoding.ASCII.GetBytes(label)); } writer.Write((byte)0); //QTYPE WriteUInt16BE(writer, (UInt16)query.QueryType); //QCLASS WriteUInt16BE(writer, (UInt16)query.QueryClass); return(stream.ToArray()); }