Esempio n. 1
0
        /// <summary>
        /// Create a new DnsResponse
        /// </summary>
        public DnsResponse(DnsQuery query, List <DnsAnswer> answers)
        {
            this.Query      = query;
            this.Answers    = answers;
            this.StatusCode = DnsQueryStatusCode.OK;

            // Add the OPT record
            this.Answers.Add(new DnsAnswer()
            {
                Record = new OptDnsRecord()
            });

            this.CountAnswers();
        }
Esempio n. 2
0
		/// <summary>
		/// Create a new DnsResponse
		/// </summary>
		public DnsResponse (DnsQuery query, List<DnsAnswer> answers)
		{
			this.Query = query;
			this.Answers = answers;
			this.StatusCode = DnsQueryStatusCode.OK;

            // Add the OPT record
            this.Answers.Add(new DnsAnswer()
            {
                Record = new OptDnsRecord()
            });

            this.CountAnswers();
        }
Esempio n. 3
0
        /// <summary>
        /// Parse the DnsQuery from the given UdpReceiveResult
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static DnsQuery Parse(byte[] buffer, IPAddress remoteEndpoint)
        {
            // Create the query
            var query = new DnsQuery();

            query.Status          = DnsQueryStatus.OK;
            query.ClientIPAddress = remoteEndpoint;

            // Parse the dns request header
            query.Header = DnsMessageHeader.Parse(buffer, 0);

            // DNS QUESTIONS:
            int offset = 12;

            for (int i = 0; i < query.Header.QuestionCount; i++)
            {
                try
                {
                    // Save the question
                    DnsQuestion question;
                    offset += DnsQuestion.Parse(out question, buffer, offset);
                    if (question != null)
                    {
                        query.Questions.Add(question);
                    }
                }
                catch (Exception e) {
                    Console.WriteLine(e.Message);
                    Console.WriteLine(e.StackTrace);
                    query.Status = DnsQueryStatus.ParsingError;
                    break;
                }
            }

            // Save the original question data to include it in the answer
            query.OriginalQuestionData = new byte[offset];
            Buffer.BlockCopy(buffer, 0, query.OriginalQuestionData, 0, offset);

            // Parse the additional EDNS question
            // For now, we only support a single additional question
            if (query.Header.AdditionalQuestionCount > 0)
            {
                //Console.WriteLine("Reading OPT");
                try
                {
                    int bytesProcessed = 0;
                    query.EdnsQuestion = EdnsQuestion.Parse(buffer, offset, ref bytesProcessed);
                    offset            += bytesProcessed;

                    // If the question was validely parsed, try using the subnet to assign the IP based on the user's location instead of the
                    // dns proxy server
                    if (query.EdnsQuestion != null)
                    {
                        var clientSubnetOption = (EdnsClientSubnetOption)query.EdnsQuestion.GetOption(EdnsOptionCode.ClientSubnet);
                        if (clientSubnetOption != null)
                        {
                            query.ClientIPAddress = clientSubnetOption.IPAddress;
                        }
                    }
                }
                catch (Exception e) {
                    Console.WriteLine(e.Message);
                    Console.WriteLine(e.StackTrace);
                    // If this fails, we still try to proceed normally
                    // TODO: log
                }
            }

            return(query);
        }
Esempio n. 4
0
        /// <summary>
        /// Parse the DnsQuery from the given UdpReceiveResult
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static DnsQuery Parse(byte[] buffer, IPAddress remoteEndpoint)
        {
            // Create the query
            var query = new DnsQuery();
			query.Status = DnsQueryStatus.OK;
            query.ClientIPAddress = remoteEndpoint;

            // Parse the dns request header
            query.Header = DnsMessageHeader.Parse (buffer, 0);

            // DNS QUESTIONS:
            int offset = 12;
			for (int i = 0; i < query.Header.QuestionCount; i++) 
			{
				try
				{
					// Save the question
					DnsQuestion question;
					offset += DnsQuestion.Parse(out question, buffer, offset);
					if(question != null)
					{
						query.Questions.Add (question);
					}
				}
				catch (Exception e) {
                    Console.WriteLine(e.Message);
                    Console.WriteLine(e.StackTrace);
					query.Status = DnsQueryStatus.ParsingError;
					break;
				}
			}

            // Save the original question data to include it in the answer
            query.OriginalQuestionData = new byte[offset];
            Buffer.BlockCopy(buffer, 0, query.OriginalQuestionData, 0, offset);

            // Parse the additional EDNS question
            // For now, we only support a single additional question
            if (query.Header.AdditionalQuestionCount > 0)
            {
                //Console.WriteLine("Reading OPT");
                try
                {
                    int bytesProcessed = 0;
                    query.EdnsQuestion = EdnsQuestion.Parse(buffer, offset, ref bytesProcessed);
                    offset += bytesProcessed;

                    // If the question was validely parsed, try using the subnet to assign the IP based on the user's location instead of the
                    // dns proxy server
                    if (query.EdnsQuestion != null)
                    {
                        var clientSubnetOption = (EdnsClientSubnetOption)query.EdnsQuestion.GetOption(EdnsOptionCode.ClientSubnet);
                        if(clientSubnetOption != null)
                        {
                            query.ClientIPAddress = clientSubnetOption.IPAddress;
                        }
                    }
                }
                catch (Exception e) {
                    Console.WriteLine(e.Message);
                    Console.WriteLine(e.StackTrace);
                    // If this fails, we still try to proceed normally
                    // TODO: log
                }
            }

            return query;
        }