Example #1
0
        /// <summary>
        /// Resolves this request into a response using the provided DNS information. The given
        /// request strategy is used to retrieve the response.
        /// </summary>
        /// <exception cref="DnsResponseException">Throw if a malformed response is received from the server</exception>
        /// <exception cref="IOException">Thrown if a IO error occurs</exception>
        /// <exception cref="SocketException">Thrown if the reading or writing to the socket fails</exception>
        /// <exception cref="OperationCanceledException">Thrown if reading or writing to the socket timeouts</exception>
        /// <returns>The response received from server</returns>
        public async Task <IDnsResponse> Resolve()
        {
            try
            {
                IDnsResponse response = await resolver.Resolve(this);

                if (response.Id != this.Id)
                {
                    throw new DnsResponseException(response, "Mismatching request/response IDs");
                }

                if (response.ResponseCode != DnsResponseCode.NoError)
                {
                    throw new DnsResponseException(response);
                }

                return(response);
            }
            catch (ArgumentException e)
            {
                throw new DnsResponseException("Invalid response", e);
            }
            catch (IndexOutOfRangeException e)
            {
                throw new DnsResponseException("Invalid response", e);
            }
        }
Example #2
0
        public async Task <IDnsResponse> Resolve(IDnsRequest request)
        {
            using (UdpClient udp = new UdpClient())
            {
                await udp
                .SendAsync(request.ToArray(), request.Size, dns)
                .WithCancellationTimeout(timeout);

                UdpReceiveResult result = await udp.ReceiveAsync().WithCancellationTimeout(timeout);

                if (!result.RemoteEndPoint.Equals(dns))
                {
                    throw new IOException("Remote endpoint mismatch");
                }
                byte[]             buffer   = result.Buffer;
                DefaultDnsResponse response = DefaultDnsResponse.FromArray(buffer);

                if (response.Truncated)
                {
                    return(await fallback.Resolve(request));
                }

                return(new ClientDnsResponse(request, response, buffer));
            }
        }