/*
* Copyright 2004-2006 Luke Quinane and Daniel Frampton
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
namespace NDns.Message{
/// <summary>
/// A question section entry in a DNS message.
/// </summary>
public class QuestionSection
{
/// <summary>
/// The string name being looked up.
/// </summary>
protected string queryName;
/// <summary>
/// The type of query being performed.
/// </summary>
protected QType queryType;
/// <summary>
/// The class of query.
/// </summary>
protected QClass queryClass = QClass.IN;
/// <summary>
/// Creates a new question section with the given string and type.
/// </summary>
/// <param name="queryName">The string to query.</param>
/// <param name="queryType">The type of query to perform.</param>
public QuestionSection(string queryName, QType queryType)
{
this.queryName = queryName;
this.queryType = queryType;
}
/// <summary>
/// Creates a question section from the given byte data.
/// </summary>
/// <param name="data">The data to create the question section from.</param>
/// <param name="start">The position to start reading the byte array from.</param>
/// <param name="length">The number of bytes read from the byte array.</param>
/// <param name="coder">The coder to use when parsing the data.</param>
public QuestionSection(byte[] data, ushort start, out ushort length, DomainCoder coder)
{
// get the query name
ushort position;
this.queryName = coder.DecodeDomain(data, start, out position);
position += start;
// get the query type
ushort type = (ushort) (data[position++] << 8);
type |= (ushort) data[position++];
this.queryType = (QType)type;
// ignore the class
position += 2;
this.queryClass = QClass.IN;
length = (ushort) (position - start);
}
#region ToByteArray
/// <summary>
/// Converts the question section into a byte array.
/// </summary>
/// <param name="coder">The string coder used to compress queries with.</param>
/// <param name="reference">A reference to the location of this entry in the message.</param>
/// <returns>The question section as a byte array.</returns>
public byte[] ToByteArray(DomainCoder coder, ushort reference)
{
byte[] data = new byte[512];
ushort position = 0;
// fill in the QNAME section
byte[] domainName = coder.EncodeDomain(this.queryName, reference);
domainName.CopyTo(data, position);
position += (ushort) domainName.Length;
// add the query type
data[position++] = (byte) ((byte) this.queryType >> 8);
data[position++] = (byte) this.queryType;
// add the query class
data[position++] = (byte) ((byte) this.queryClass >> 8);
data[position++] = (byte) this.queryClass;
// re-pack into correct sized array and return
byte[] result = new byte[position];
Array.Copy(data, 0, result, 0, position);
return result;
}
#endregion
}
}
|