示例#1
0
        /// <summary>
        /// Returns a shallow clone of this instance.
        /// </summary>
        /// <returns></returns>
        public DnsResponse Clone()
        {
            var response = new DnsResponse();

            response.CopyFrom(this);
            return(response);
        }
示例#2
0
            public DnsResponse Response;                // The DNS response

            /// <summary>
            /// Constructor.
            /// </summary>
            /// <param name="dnsSocket">The socket used to transmit the request.</param>
            /// <param name="request">The DNS request.</param>
            /// <param name="timeout">The maximum time to wait.</param>
            /// <param name="callback">The delegate to call when the operation completes (or <c>null</c>).</param>
            /// <param name="state">The application state (or <c>null</c>).</param>
            public DnsAsyncResult(DnsSocket dnsSocket, DnsRequest request, TimeSpan timeout, AsyncCallback callback, object state)
                : base(null, callback, state)
            {
                this.DnsSocket  = dnsSocket;
                this.Request    = request;
                this.TTD        = SysTime.Now + timeout;
                this.TimerStart = 0;
                this.Response   = null;
            }
示例#3
0
            /// <summary>
            /// Constructor.
            /// </summary>
            /// <param name="request">The DNS request.</param>
            /// <param name="nameServer">The single name server IP address.</param>
            /// <param name="maxSendCount">
            /// The number of times a request should be sent to a
            /// particular name server.
            /// </param>
            /// <param name="timeout">The timeout to use for each request retry.</param>
            /// <param name="callback">The deleate to be called when the operation completes (or <c>null</c>).</param>
            /// <param name="state">The application defined state (or <c>null</c>).</param>
            /// <remarks>
            /// This initializes <see cref="SendCount" /> to 1 under the assumption
            /// that the first request will be sent out immediately.
            /// </remarks>
            public DnsRetryAsyncResult(DnsRequest request, IPAddress nameServer, int maxSendCount, TimeSpan timeout,
                                       AsyncCallback callback, object state)
                : base(null, callback, state)
            {
                this.NameServers = new List <IPAddress>(1);
                this.NameServers.Add(nameServer);

                this.Request      = request;
                this.Response     = null;
                this.MaxSendCount = maxSendCount;
                this.SendCount    = 1;
                this.Timeout      = timeout;
            }
示例#4
0
            /// <summary>
            /// Constructor.
            /// </summary>
            /// <param name="request">The DNS request.</param>
            /// <param name="nameServers">The name server IP addresses.</param>
            /// <param name="maxSendCount">
            /// The number of times a request should be sent to a
            /// particular name server.
            /// </param>
            /// <param name="timeout">The timeout to use for each request retry.</param>
            /// <param name="callback">The deleate to be called when the operation completes (or <c>null</c>).</param>
            /// <param name="state">The application defined state (or <c>null</c>).</param>
            /// <remarks>
            /// <para>
            /// Note that a random name server endpoint from the list will be
            /// selected and moved to the head of the list.
            /// </para>
            /// <para>
            /// This initializes <see cref="SendCount" /> to 1 under the assumption
            /// that the first request will be sent out immediately.
            /// </para>
            /// </remarks>
            public DnsRetryAsyncResult(DnsRequest request, IPAddress[] nameServers, int maxSendCount, TimeSpan timeout,
                                       AsyncCallback callback, object state)
                : base(null, callback, state)
            {
                this.NameServers = new List <IPAddress>(nameServers.Length);
                for (int i = 0; i < nameServers.Length; i++)
                {
                    this.NameServers.Add(nameServers[i]);
                }

                this.Request      = request;
                this.Response     = null;
                this.MaxSendCount = maxSendCount;
                this.SendCount    = 1;
                this.Timeout      = timeout;

                RandomizeNameServers();
            }
示例#5
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="remoteEP">The <see cref="IPEndPoint" /> of the DNS resolver making the request.</param>
 /// <param name="request">The received <see cref="DnsRequest" />.</param>
 internal DnsServerEventArgs(IPEndPoint remoteEP, DnsRequest request)
 {
     this.RemoteEP = remoteEP;
     this.Request  = request;
     this.Response = null;
 }
示例#6
0
        /// <summary>
        /// Called when an async packet receive operation completes on one of the DNS sockets.
        /// </summary>
        /// <param name="ar">The operation's async result instance.</param>
        private static void OnReceive(IAsyncResult ar)
        {
            DnsSocket      dnsSock = (DnsSocket)ar.AsyncState;
            DnsResponse    response;
            DnsAsyncResult arDns;
            int            cbPacket;
            int            requestKey;

            lock (syncLock)
            {
                try
                {
                    cbPacket = dnsSock.Socket.EndReceiveFrom(ar, ref dnsSock.FromEP);
                    response = new DnsResponse();
                    if (!response.ParsePacket(dnsSock.RecvPacket, cbPacket))
                    {
                        NetTrace.Write(TraceSubSystem, 0, "Bad DNS message", string.Empty,
                                       Helper.HexDump(dnsSock.RecvPacket, 0, cbPacket, 16, HexDumpOption.ShowAll));

                        BadPacket(dnsSock.RecvPacket, cbPacket);
                        return;
                    }

                    response.Trace(TraceSubSystem, 0, ((IPEndPoint)dnsSock.FromEP).Address, null);

                    // We've parsed a valid DNS response so attempt to match it
                    // up with the corresponding request and signal that the
                    // query operation is complete.

                    requestKey = GenRequestKey(dnsSock.SocketID, response.QID);
                    if (requests.TryGetValue(requestKey, out arDns))
                    {
                        if (response.RCode != DnsFlag.RCODE_OK)
                        {
                            arDns.Notify(new DnsException(response.RCode));
                            return;
                        }

                        response.Latency = HiResTimer.CalcTimeSpan(arDns.TimerStart);
                        arDns.Response   = response;
                        arDns.Notify();
                    }
                    else
                    {
                        response.Trace(TraceSubSystem, 0, ((IPEndPoint)dnsSock.FromEP).Address, "Orphan DNS Response");
                    }
                }
                catch (SocketException)
                {
                    // We're going to get SocketException(10054) "Connection Reset" errors if
                    // we send a packet to a port that's not actually open on the remote
                    // machine.  Ignore these exceptions and let the operation timeout.
                }
                finally
                {
                    if (dnsSock.Socket.IsOpen)
                    {
                        dnsSock.Socket.BeginReceiveFrom(dnsSock.RecvPacket, 0, 512, SocketFlags.None, ref dnsSock.FromEP, onReceive, dnsSock);
                    }
                }
            }
        }