Ejemplo n.º 1
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());
        }