Example #1
0
        /// <summary>
        /// Sends query to server.
        /// </summary>
        /// <param name="qname">Query text.</param>
        /// <param name="qtype">Query type.</param>
        /// <param name="qclass">Query class.</param>
        /// <returns></returns>
        private DnsServerResponse QueryServer(string qname, QTYPE qtype, int qclass)
        {
            if (m_DnsServers == null || m_DnsServers.Length == 0)
            {
                throw new Exception("Dns server isn't specified !!!");
            }

            // See if query is in cache
            if (m_UseDnsCache)
            {
                DnsServerResponse resopnse = DnsCache.GetFromCache(qname, (int)qtype);
                if (resopnse != null)
                {
                    return(resopnse);
                }
            }

            int queryID = DnsEx.ID;

            byte[] query = CreateQuery(queryID, qname, qtype, qclass);

            int helper = 0;

            for (int i = 0; i < (m_DnsServers.Length * 2); i++)
            {
                if (helper >= m_DnsServers.Length)
                {
                    helper = 0;
                }

                try{
                    Socket udpClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                    udpClient.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 10000);
                    udpClient.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 10000);

                    udpClient.Connect(new IPEndPoint(IPAddress.Parse(m_DnsServers[helper]), 53));

                    //send query
                    udpClient.Send(query);

                    byte[] retVal        = new byte[512];
                    int    countRecieved = udpClient.Receive(retVal);

                    udpClient.Close();

                    // If reply is ok, return it
                    DnsServerResponse serverResponse = ParseAnswers(retVal, queryID);

                    // Cache query
                    if (m_UseDnsCache && serverResponse.ResponseCode == RCODE.NO_ERROR)
                    {
                        DnsCache.AddToCache(qname, (int)qtype, serverResponse);
                    }

                    return(serverResponse);
                }
                catch {
                }

                helper++;
            }

            // If we reach so far, we probably won't get connection to dsn server
            return(new DnsServerResponse(false, RCODE.SERVER_FAILURE, null));
        }
Example #2
0
        /// <summary>
        /// Sends query to server.
        /// </summary>
        /// <param name="qname">Query text.</param>
        /// <param name="qtype">Query type.</param>
        /// <param name="qclass">Query class.</param>
        /// <returns></returns>
        private ArrayList QueryServer(string qname, QTYPE qtype, int qclass)
        {
            // See if query is in cache
            if (m_UseDnsCache)
            {
                ArrayList entries = DnsCache.GetFromCache(qname, (int)qtype);
                if (entries != null)
                {
                    return(entries);
                }
            }

            int queryID = DnsEx.ID;

            byte[] query = CreateQuery(queryID, qname, qtype, qclass);

            int helper = 0;

            for (int i = 0; i < 10; i++)
            {
                if (helper > m_DnsServers.Length)
                {
                    helper = 0;
                }

                try{
                    IPEndPoint ipRemoteEndPoint = new IPEndPoint(IPAddress.Parse(m_DnsServers[helper]), 53);
                    Socket     udpClient        = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                    IPEndPoint ipLocalEndPoint = new IPEndPoint(IPAddress.Any, 0);
                    EndPoint   localEndPoint   = (EndPoint)ipLocalEndPoint;
                    udpClient.Bind(localEndPoint);

                    udpClient.Connect(ipRemoteEndPoint);

                    //send query
                    udpClient.Send(query);

                    // Wait until we have a reply
                    byte[] retVal = null;
                    if (udpClient.Poll(5 * 1000000, SelectMode.SelectRead))
                    {
                        retVal = new byte[512];
                        udpClient.Receive(retVal);
                    }

                    udpClient.Close();

                    // If reply is ok, return it
                    ArrayList answers = ParseAnswers(retVal, queryID);
                    if (answers != null)
                    {
                        // Cache query
                        if (m_UseDnsCache && answers.Count > 0)
                        {
                            DnsCache.AddToCache(qname, (int)qtype, answers);
                        }

                        return(answers);
                    }
                }
                catch {
                }

                helper++;
            }

            return(null);
        }