Example #1
0
        private void UdpReceiveCompleted(IAsyncResult ar)
        {
            DnsAsyncState state = (DnsAsyncState)ar.AsyncState;

            if (state.Timer != null)
            {
                state.Timer.Dispose();
            }

            if (state.TimedOut)
            {
                state.ServerIndex++;
                UdpBeginSend(state);
            }
            else
            {
                byte[] responseData = state.UdpClient.EndReceive(ar, ref state.UdpEndpoint);

                state.UdpClient.Close();
                state.UdpClient   = null;
                state.UdpEndpoint = null;

                state.Response = new DnsMessage();
                state.Response.Parse(responseData, false, state.TSigKeySelector, state.TSigOriginalMac);

                if (state.Response.ReturnCode == ReturnCode.ServerFailure)
                {
                    if (state.ServerIndex++ < state.Servers.Count)
                    {
                        UdpBeginSend(state);
                        return;
                    }
                    else
                    {
                        state.SetCompleted();
                    }
                }
                else
                {
                    if (state.Response.IsTruncated)
                    {
                        TcpBeginConnect(state);
                        return;
                    }
                    else
                    {
                        state.SetCompleted();
                    }
                }
            }
        }
Example #2
0
        private void TcpBeginConnect(DnsAsyncState state)
        {
            if (state.ServerIndex == state.Servers.Count)
            {
                state.Response  = null;
                state.TcpStream = null;
                state.TcpClient = null;
                state.SetCompleted();
                return;
            }

            IPAddress server = state.Servers[state.ServerIndex];

            state.TcpClient = new TcpClient(server.AddressFamily);
            state.TimedOut  = false;

            IAsyncResult asyncResult = state.TcpClient.BeginConnect(server, _DNS_PORT, TcpConnectCompleted, state);

            state.Timer = new Timer(TcpTimedOut, asyncResult, QueryTimeout, Timeout.Infinite);
        }
Example #3
0
        private void UdpBeginSend(DnsAsyncState state)
        {
            if (state.ServerIndex == state.Servers.Count)
            {
                state.Response    = null;
                state.UdpClient   = null;
                state.UdpEndpoint = null;
                state.SetCompleted();
                return;
            }

            state.UdpEndpoint = new IPEndPoint(state.Servers[state.ServerIndex], _DNS_PORT);

            state.UdpClient = new UdpClient(state.UdpEndpoint.AddressFamily);
            state.UdpClient.Connect(state.UdpEndpoint);
            state.TimedOut = false;

            IAsyncResult asyncResult = state.UdpClient.BeginSend(state.QueryData, state.QueryLength, UdpSendCompleted, state);

            state.Timer = new Timer(UdpTimedOut, asyncResult, QueryTimeout, Timeout.Infinite);
        }
Example #4
0
        private void TcpReceiveCompleted(IAsyncResult ar)
        {
            DnsAsyncState state = (DnsAsyncState)ar.AsyncState;

            if (state.Timer != null)
            {
                state.Timer.Dispose();
            }

            if (state.TimedOut)
            {
                state.ServerIndex++;
                TcpBeginConnect(state);
            }
            else
            {
                state.TcpStream.EndRead(ar);

                state.TcpStream.Close();
                state.TcpClient.Close();
                state.TcpStream = null;
                state.TcpClient = null;

                state.TcpBuffer = null;

                state.Response = new DnsMessage();
                state.Response.Parse(state.TcpBuffer, false, state.TSigKeySelector, state.TSigOriginalMac);

                if ((state.Response.ReturnCode == ReturnCode.ServerFailure) && (state.ServerIndex++ < state.Servers.Count))
                {
                    TcpBeginConnect(state);
                    return;
                }
                else
                {
                    state.SetCompleted();
                }
            }
        }