AddToCache() public static method

Adds dns records to cache. If old entry exists, it is replaced.
public static AddToCache ( string qname, int qtype, DnsServerResponse answers ) : void
qname string
qtype int
answers DnsServerResponse
return void
Exemplo n.º 1
0
        /// <summary>
        /// Is called when IPv6 socket has received data.
        /// </summary>
        /// <param name="ar">The result of the asynchronous operation.</param>
        private void IPv6ReceiveCompleted(IAsyncResult ar)
        {
            try{
                if (m_IsDisposed)
                {
                    return;
                }

                EndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);
                int      count    = m_pIPv6Socket.EndReceiveFrom(ar, ref remoteEP);

                DnsServerResponse serverResponse = ParseQuery((byte[])ar.AsyncState);
                DnsTransaction    transaction    = null;
                // Pass response to transaction.
                if (m_pTransactions.TryGetValue(serverResponse.ID, out transaction))
                {
                    transaction.ProcessResponse(serverResponse);
                }
                // No such transaction or transaction has timed out before answer received.
                //else{
                //}

                // Cache query.
                if (m_UseDnsCache && serverResponse.ResponseCode == RCODE.NO_ERROR)
                {
                    DnsCache.AddToCache(transaction.QName, transaction.QType, serverResponse);
                }
            }
            catch {
                // Skip receiving socket errors.
            }

            try{
                StartWaitingIPv6Packet();
            }
            catch {
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Sends query to server.
        /// </summary>
        /// <param name="timeout">Query timeout in milli seconds.</param>
        /// <param name="qname">Query text.</param>
        /// <param name="qtype">Query type.</param>
        /// <param name="qclass">Query class.</param>
        /// <returns></returns>
        private DnsServerResponse QueryServer(int timeout, 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 = Dns_Client.ID;

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

            // Create sending UDP socket.
            Socket udpClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            udpClient.SendTimeout = 500;

            // Send parallel query to all dns servers and get first answer.
            DateTime startTime = DateTime.Now;

            while (startTime.AddMilliseconds(timeout) > DateTime.Now)
            {
                foreach (IPAddress dnsServer in m_DnsServers)
                {
                    try{
                        udpClient.SendTo(query, new IPEndPoint(dnsServer, 53));
                    }
                    catch {
                    }
                }

                // Wait 10 ms response to arrive, if no response, retransmit query.
                if (udpClient.Poll(10, SelectMode.SelectRead))
                {
                    try{
                        byte[] retVal        = new byte[1024];
                        int    countRecieved = udpClient.Receive(retVal);

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

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

                        return(serverResponse);
                    }
                    catch {
                    }
                }
            }

            udpClient.Close();

            // If we reach so far, we probably won't get connection to dsn server
            return(new DnsServerResponse(false, RCODE.SERVER_FAILURE, new List <DNS_rr_base>(), new List <DNS_rr_base>(), new List <DNS_rr_base>()));
        }