Example #1
0
        DnsQueryBody ParseDnsQueryBody(byte[] buffer, int beginIndex, ref int parseCount)
        {
            DnsQueryBody queryBody = new DnsQueryBody();
            // get the length of the query name
            int nameLength = 0;
            int sectionLen, index = beginIndex;

            while ((sectionLen = buffer[index]) != 0)
            {
                nameLength += sectionLen + 1;
                index      += sectionLen + 1;
            }
            nameLength += 1; // add the count of the last '0'
            index++;

            byte[] nameBuffer = new byte[nameLength];
            Array.Copy(buffer, beginIndex, nameBuffer, 0, nameLength);
            queryBody.queryName = nameBuffer;

            queryBody.queryType = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, index));
            index += sizeof(Int16);
            queryBody.queryClass = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, index));
            index     += sizeof(Int16);
            parseCount = index - beginIndex;
            return(queryBody);
        }
Example #2
0
        public void ExecuteTest()
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            byte[] buffer = PrepareDnsQuery();
            socket.SendTo(buffer, dnsServerEndPoint);
            // Receive the response from the server
            byte[]   dnsData  = new byte[1000];
            EndPoint endPoint = (EndPoint)dnsServerEndPoint;

            socket.ReceiveFrom(dnsData, ref endPoint);

            int            index = 0, count = 0;
            DnsDiagramHead diagramHead = ParseResponseDiagramHead(dnsData, index, ref count);

            index += count;
            #region ErrorHandling
            if (diagramHead.resRecNum == 0)
            {
                Console.WriteLine("Could not resolve the host name.");
                return;
            }
            // rcode nonzero means error is found
            if ((diagramHead.flag & 0x08) != 0)
            {
                Console.WriteLine("Dns Error! rcode =%d", (diagramHead.flag & 0x08));
                return;
            }
            #endregion
            // Make sure queryNum=1
            Debug.Assert(diagramHead.queryNum == 1);
            DnsQueryBody queryBody = ParseDnsQueryBody(dnsData, index, ref count);
            index += count;
            Console.WriteLine("Dns Query Name:  " + queryBody.GetQueryNameString());

            int resRecordCount = diagramHead.resRecNum;
            DnsResourceRecord[] resourceRecords = new DnsResourceRecord[resRecordCount];
            for (int i = 0; i < resRecordCount; i++)
            {
                resourceRecords[i] = ParseDnsResourceRecord(dnsData, index, ref count);
                index += count;
            }

            foreach (DnsResourceRecord record in resourceRecords)
            {
                Console.WriteLine("Result is " + record.GetResourceContentString());
            }

            Console.WriteLine("\nPress any  key to exit.");
            Console.ReadKey();
        }