private void ConstructRequestData(DnsRequestMessage request, IDnsDatagramWriter writer) { DnsQuestion question = request.Question; /* * 1 1 1 1 1 1 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | ID | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ |QR| Opcode |AA|TC|RD|RA| Z | RCODE | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | QDCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | ANCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | NSCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | ARCOUNT | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ * */ // 4 more bytes for the type and class writer.WriteInt16NetworkOrder((short)request.Header.Identifier); writer.WriteUInt16NetworkOrder(request.Header.RawFlags); writer.WriteInt16NetworkOrder(1); // we support single question only... (as most DNS servers anyways). writer.WriteInt16NetworkOrder(0); writer.WriteInt16NetworkOrder(0); writer.WriteInt16NetworkOrder(1); // one additional for the Opt record. writer.WriteHostName(question.QueryName.Value); writer.WriteUInt16NetworkOrder((ushort)question.QuestionType); writer.WriteUInt16NetworkOrder((ushort)question.QuestionClass); /* +------------+--------------+------------------------------+ | Field Name | Field Type | Description | +------------+--------------+------------------------------+ | NAME | domain name | MUST be 0 (root domain) | | TYPE | u_int16_t | OPT (41) | | CLASS | u_int16_t | requester's UDP payload size | | TTL | u_int32_t | extended RCODE and flags | | RDLEN | u_int16_t | length of all RDATA | | RDATA | octet stream | {attribute,value} pairs | +------------+--------------+------------------------------+ * */ var opt = new OptRecord(); writer.WriteHostName(""); writer.WriteUInt16NetworkOrder((ushort)opt.RecordType); writer.WriteUInt16NetworkOrder((ushort)opt.RecordClass); writer.WriteUInt32NetworkOrder((ushort)opt.InitialTimeToLive); writer.WriteUInt16NetworkOrder(0); }
public void WriteHostName_should_write_query_name_in_the_data_array_using_utf8_encoding() { // Arrange. const string queryNameRoot = "www.microsoft.com."; // Act. writer.WriteHostName(queryNameRoot); // Assert. Assert.NotNull(writer.Data); string result = Encoding.UTF8.GetString(writer.Data.Array, 1, writer.Data.Count); Assert.AreEqual("www\tmicrosoft\u0003com\0\0", result); }