Beispiel #1
0
        public void AddQuestion(Question question)
		{
			questions.Add(question);
		}
Beispiel #2
0
        public Response Query(Question question)
        {
            Response response = null;

            if (CacheEnable)
            {
                response = fetch(question.ID);
            }

            if (response != null)
                return response;

            // RFC1035 max. size of a UDP datagram is 512 bytes
            byte[] receiveBuffer = new byte[512];

            Request request = new Request();

            request.AddQuestion(question);
            request.header.ID = (ushort)new Random().Next(65000);
            request.header.RD = Recursion;

            for (int n = 0; n < Retries; n++)
            {
                Random c = new Random(Guid.NewGuid().GetHashCode());

                int i = c.Next(0, this.ServerAddress.Count);

                Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, TimeOut * 1000);

                try
                {
                    socket.SendTo(request.Data, ServerAddress[i]);

                    int size = socket.Receive(receiveBuffer);

                    byte[] data = new byte[size];

                    Buffer.BlockCopy(receiveBuffer, 0, data, 0, size);

                    response = new Response(ServerAddress[i], data);

                    if (CacheEnable)
                    {
                        this.insert(response);
                    }

                    return response;
                }
                catch (SocketException)
                {
                    continue; // next try
                }
                finally
                {
                    socket.Close();
                }
            }

            Response responseTimeout = new Response();
            responseTimeout.Error = "Timeout Error";

            return responseTimeout;
        }
Beispiel #3
0
		/// <summary>
		/// Do an QClass=IN Query on specified DNS servers
		/// </summary>
		/// <param name="name">Name to query</param>
		/// <param name="qtype">Question type</param>
		/// <returns>Response of the query</returns>
		public Response Query(string name, QType qtype)
		{
			Question question = new Question(name, qtype, QClass.IN);
			Response response = SearchInCache(question);
			if (response != null)
				return response;

			Request request = new Request();
			request.AddQuestion(question);

            return Query(request);
		}
Beispiel #4
0
        public Response Query(string name)
        {
            Question q = new Question(name, QType.MX, QClass.IN);

            return Query(q);
        }
Beispiel #5
0
		private Response SearchInCache(Question question)
		{
            if (!IsUseCache)
				return null;

			string strKey = question.ID;

			Response response = null;

			lock (m_ResponseCache)
			{
				if (!m_ResponseCache.ContainsKey(strKey))
					return null;

				response = m_ResponseCache[strKey];
			}

			int TimeLived = (int)((DateTime.Now.Ticks - response.TimeStamp.Ticks) / TimeSpan.TicksPerSecond);

			foreach (RR rr in response.RecordsRR)
			{
				rr.TimeLived = TimeLived;

				// The TTL property calculates its actual time to live
				if (rr.TTL == 0)
					return null; // out of date
			}

			return response;
		}