Example #1
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();
        }
Example #2
0
        byte[] PrepareDnsQuery()
        {
            // prepare the head
            DnsDiagramHead diagramHead = new DnsDiagramHead();

            diagramHead.id       = DnsDiagramID;
            diagramHead.flag     = 0;
            diagramHead.flag    |= DnsHeadFlag.QRquery | DnsHeadFlag.RD | DnsHeadFlag.TC;
            diagramHead.queryNum = 1;

            // prepare the body
            DnsQueryBody queryBody;

            queryBody.queryName = new byte[targetHostName.Length + 1];

            for (int i = 0, j = -1; i < targetHostName.Length; i++)
            {
                if (targetHostName[i] == '.')
                {
                    // j contains the the previous index value of  '.' in queryName
                    queryBody.queryName[j + 1] = (byte)(i - j - 1);
                    j = i;
                }
                else
                {
                    queryBody.queryName[i + 1] = (byte)targetHostName[i];
                }
            }
            queryBody.queryName[targetHostName.Length] = 0;

            queryBody.queryType  = 1; // 'A' address
            queryBody.queryClass = 1; // IP address

            int bufferSize = diagramHead.DiagramSize + queryBody.DiagramSize;

            byte[] buffer = new byte[bufferSize];
            int    index  = 0;

            diagramHead.ToByteArray().CopyTo(buffer, index);
            index += diagramHead.DiagramSize;
            queryBody.ToByteArray().CopyTo(buffer, index);
            index += queryBody.DiagramSize;

            return(buffer);
        }
Example #3
0
        DnsDiagramHead ParseResponseDiagramHead(byte[] buffer, int beginIndex, ref int parseCount)
        {
            DnsDiagramHead diagramHead = new DnsDiagramHead();
            int            index       = beginIndex;

            diagramHead.id           = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, index));
            index                   += sizeof(Int16);
            diagramHead.flag         = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, index));
            index                   += sizeof(Int16);
            diagramHead.queryNum     = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, index));
            index                   += sizeof(Int16);
            diagramHead.resRecNum    = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, index));
            index                   += sizeof(Int16);
            diagramHead.autResRecNum = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, index));
            index                   += sizeof(Int16);
            diagramHead.extResRecNum = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, index));
            index                   += sizeof(Int16);
            parseCount               = index - beginIndex;
            return(diagramHead);
        }