/// <summary>
 /// Creates a new instance of this class
 /// </summary>
 public DNSResourceRecord()
 {
     dnsEnc   = new DNSNameEncoder();
     strName  = "";
     dnsType  = DNSResourceType.All;
     dnsClass = DNSResourceClass.Any;
 }
        /// <summary>
        /// Creates a new instance of this class by parsing the given data
        /// <seealso cref="DNSQuestion"/>
        /// <example><code>
        /// // For parsing DNS frames, set the index variable to the index where parsing should begin.
        /// // This is in case of DNS frames 12 + the length of all records before this record
        /// int iIndex = 12;
        ///
        /// // Parse all records
        /// while (lRecords.Count &lt; iCount)
        /// {
        ///     // Create a new DNS records from the data and pass the index as pointer to the constructor.
        ///     // The index will be increased during parsing so that it will point to the beginning of the next record.
        ///     DNSResourceRecord qLast = new DNSResourceRecord(bData, ref iIndex);
        ///     lRecords.Add(qLast);
        /// }
        /// </code></example>
        /// </summary>
        /// <param name="bData">The data to parse</param>
        /// <param name="iParserIndex">The index where parsing starts. This index will be incremented automatically during parsing</param>
        public DNSResourceRecord(byte[] bData, ref int iParserIndex)
        {
            dnsEnc = new DNSNameEncoder();
            int iStringLen = 0;

            strName       = DNSNameEncoder.DecodeDNSName(bData, iParserIndex, ref iStringLen).Substring(1);
            iParserIndex += iStringLen;
            dnsType       = (DNSResourceType)(((bData[iParserIndex]) << 8) + bData[iParserIndex + 1]);
            dnsClass      = (DNSResourceClass)(((bData[iParserIndex + 2]) << 8) + bData[iParserIndex + 3]);
            iTTL          = ((int)bData[iParserIndex + 4] << 24) + ((int)bData[iParserIndex + 5] << 16) + ((int)bData[iParserIndex + 6] << 8) + bData[iParserIndex + 7];
            int iRDLen = ((int)bData[iParserIndex + 8] << 8) + bData[iParserIndex + 9];

            iParserIndex += 10;
            if (dnsType != DNSResourceType.CNAME)
            {
                bResourceData = new byte[iRDLen];

                for (int iC1 = 0; iC1 < iRDLen; iC1++)
                {
                    bResourceData[iC1] = bData[iParserIndex + iC1];
                }
            }
            else
            {
                int iDummy = 0;
                bResourceData = ASCIIEncoding.ASCII.GetBytes(DNSNameEncoder.DecodeDNSName(bData, iParserIndex, ref iDummy).Substring(1));
            }

            iParserIndex += iRDLen;
        }
Example #3
0
 /// <summary>
 /// Creates a new instance of this class
 /// </summary>
 public DNSQuestion()
 {
     dnsEnc      = new DNSNameEncoder();
     strQuestion = "";
     qType       = DNSResourceType.All;
     qClass      = DNSResourceClass.Any;
 }
Example #4
0
        /// <summary>
        /// Creates a new instance of this class by parsing the given data
        /// <example><code>
        /// // For parsing DNS frames, set the index variable to 12, because the question section starts there
        /// int iIndex = 12;
        ///
        /// // Parse all questions
        /// while (lQuestions.Count &lt; iQCount)
        /// {
        ///     // Create a new DNS question from the data and pass the index as pointer to the constructor.
        ///     // The index will be increased during parsing so that it will point to the beginning of the next record.
        ///     DNSQuestion qLast = new DNSQuestion(bData, ref iIndex);
        ///     lQuestions.Add(qLast);
        /// }
        /// </code></example>
        /// </summary>
        /// <param name="bData">The data to parse</param>
        /// <param name="iParserIndex">The index where parsing begins. This index must be passed as pointer for it will be increased during parsing.</param>
        public DNSQuestion(byte[] bData, ref int iParserIndex)
        {
            dnsEnc = new DNSNameEncoder();
            int iStringLen = 0;

            strQuestion   = DNSNameEncoder.DecodeDNSName(bData, iParserIndex, ref iStringLen).Substring(1);
            iParserIndex += iStringLen;
            qType         = (DNSResourceType)(((bData[iParserIndex]) << 8) + bData[iParserIndex + 1]);
            qClass        = (DNSResourceClass)(((bData[iParserIndex + 2]) << 8) + bData[iParserIndex + 3]);
            iParserIndex += 4;
        }
Example #5
0
        /// <summary>
        /// Returns the compressed bytes of this DNS question
        /// <example><code>
        /// // For constucting DNS frames, set the index variable to 12, because the question section starts there
        /// int iIndex = 12;
        /// // Create a new, empty dictionary
        /// Dictionary&lt;string, int&gt; dictCompression = new Dictionary&lt;string, int&gt;();
        ///
        /// // For all questions...
        /// foreach (DNSQuestion q in lQuestions)
        /// {
        ///     // Get the compressed bytes by passing the index at which this record will be inserted in the DNS frame and the dictionary to the corresponding method.
        ///     bData = q.GetCompressedBytes(dictCompression, iIndex);
        ///
        ///     // Increase the index value
        ///     iIndex += bData.Length;
        ///
        ///     // ... Do something with the data ...
        /// }
        ///
        /// // For a maximum compression factor re-use the same dictionary in the answer, authorotive and additional section of this frame.
        /// </code></example>
        /// </summary>
        /// <param name="dictCompression">A dictionary containing strings and their corresponding indexes from a DNS frame. If this is the first call to this function for a specific DNS frame, an empty instance of
        /// Dictionar&lt;string, int&gt; should be passed, which can be reused in further calls of this method</param>
        /// <param name="iStartIndex">The index at which this record will be inseted</param>
        /// <returns>This DNS question compressed as byte array</returns>
        public byte[] GetCompressedBytes(Dictionary <string, int> dictCompression, int iStartIndex)
        {
            MemoryStream msStream = new MemoryStream();

            byte[] bName = DNSNameEncoder.CompressDNSName(strQuestion, dictCompression, iStartIndex);
            msStream.Write(bName, 0, bName.Length);
            byte[] bData = new byte[4];
            bData[0] = (byte)(((int)qType >> 8) & 0xFF);
            bData[1] = (byte)(((int)qType) & 0xFF);
            bData[2] = (byte)(((int)qClass >> 8) & 0xFF);
            bData[3] = (byte)(((int)qClass) & 0xFF);
            msStream.Write(bData, 0, bData.Length);

            return(msStream.ToArray());
        }
        /// <summary>
        /// Returns the compressed bytes of this DNS record
        /// <example><code>
        /// // For parsing DNS frames, set the index variable to the index where parsing should begin.
        /// // This is in case of DNS frames 12 + the length of all records before this record
        /// int iIndex = 12;
        /// // If available, you should use the Dictionary created when compression the DNS questions. Else create a new, empty dictionary
        /// Dictionary&lt;string, int&gt; dictCompression = new Dictionary&lt;string, int&gt;();
        ///
        /// // For all recirds...
        /// foreach (DNSResourceRecord r in lRecords)
        /// {
        ///     // Get the compressed bytes by passing the index at which this record will be inserted in the DNS frame and the dictionary to the corresponding method.
        ///     bData = r.GetCompressedBytes(dictCompression, iIndex);
        ///
        ///     // Increase the index value
        ///     iIndex += bData.Length;
        ///
        ///     // ... Do something with the data ...
        /// }
        ///
        /// // For a maximum compression factor re-use the same dictionary in the other resource sections of the frame.
        /// </code></example>
        /// </summary>
        /// <param name="dictCompression">A dictionary containing strings and their corresponding indexes from a DNS frame. If this is the first call to this function for a specific DNS frame, an empty instance of
        /// Dictionar&lt;string, int&gt; should be passed, which can be reused in further calls of this method</param>
        /// <param name="iStartIndex">The index at which this record will be inseted</param>
        /// <returns>This DNS question compressed as byte array</returns>
        public byte[] GetCompressedBytes(Dictionary <string, int> dictCompression, int iStartIndex)
        {
            MemoryStream msStream = new MemoryStream();

            byte[] bName = DNSNameEncoder.CompressDNSName(strName, dictCompression, iStartIndex);
            msStream.Write(bName, 0, bName.Length);
            byte[] bData = new byte[10];
            bData[0] = (byte)(((int)dnsType >> 8) & 0xFF);
            bData[1] = (byte)(((int)dnsType) & 0xFF);
            bData[2] = (byte)(((int)dnsClass >> 8) & 0xFF);
            bData[3] = (byte)(((int)dnsClass) & 0xFF);

            bData[4] = (byte)((iTTL >> 24) & 0xFF);
            bData[5] = (byte)((iTTL >> 16) & 0xFF);
            bData[6] = (byte)((iTTL >> 8) & 0xFF);
            bData[7] = (byte)((iTTL) & 0xFF);

            bData[8] = (byte)((bResourceData.Length >> 8) & 0xFF);
            bData[9] = (byte)((bResourceData.Length) & 0xFF);
            msStream.Write(bData, 0, bData.Length);
            msStream.Write(bResourceData, 0, bResourceData.Length);

            return(msStream.ToArray());
        }