/// <summary>
 /// Adds or Updates a Dns Cache Result to the list of entries
 /// </summary>
 /// <param name="Result"></param>
 public void AddOrUpdate(DnsCacheResult Result)
 {
     // Add if the entry already exists
     if (Entries.ContainsKey(Result.HostName))
     {
         Entries[Result.HostName] = Result;
     }
     else // Update entry
     {
         Entries.Add(Result.HostName, Result);
     }
 }
        /// <summary>
        /// Accepts responsibility for completely resolving the specified
        /// <paramref name="question"/> and returning the reply.
        /// </summary>
        /// <param name="question">The <see cref="AK.Net.Dns.DnsQuestion"/> to
        /// resolve.</param>
        /// <returns>The <see cref="AK.Net.Dns.DnsReply"/> containing the answer to
        /// the question.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// Thrown when <paramref name="question"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="AK.Net.Dns.DnsTransportException">
        /// Thrown when a transport error occurs.
        /// </exception>
        /// <exception cref="AK.Net.Dns.DnsResolutionException">
        /// Thrown when an error occurs during the resolution, such as the query
        /// not being answered.
        /// </exception>
        public virtual DnsReply Resolve(DnsQuestion question)
        {
            Guard.NotNull(question, "question");

            DnsReply       reply;
            DnsCacheResult cacheResult = this.Cache.Get(question);

            if (cacheResult.Type == DnsCacheResultType.Failed)
            {
                reply = Resolve(CreateQuery(question));
                this.Cache.Put(reply);
            }
            else
            {
                reply = cacheResult.Reply;
            }

            AssertIsPositiveReply(reply);

            return(reply);
        }
        /// <summary>
        /// Queries the DNS Cache for the Gamespy hosts, and verifies that the
        /// IP addresses in the DNS Cache match that of the desired redirect IP
        /// </summary>
        public static bool VerifyDNSCache(IProgress <DnsCacheResult> Progress = null)
        {
            // Nothing to do here if redirects are disabled
            if (!RedirectsEnabled)
            {
                return(false);
            }

            // Grab our saved hosts file IP addresses
            if (RedirectMethod != RedirectMode.DnsServer)
            {
                // Grab hosts file base
                HostsFile hFile = (RedirectMethod == RedirectMode.HostsFile)
                    ? HostsFileSys as HostsFile
                    : HostsFileIcs as HostsFile;

                // Fetch our saved IPAddresses
                GamespyServerAddress = (hFile.HasEntry("master.gamespy.com")) ? hFile.Get("master.gamespy.com") : null;
                StatsServerAddress   = (hFile.HasEntry(Bf2StatsHost)) ? hFile.Get(Bf2StatsHost) : null;
            }

            // Flush our cache report
            DnsCacheReport.Entries.Clear();

            // Verify Gamespy Server IP addresses
            if (GamespyServerAddress != null)
            {
                foreach (string address in GamespyHosts)
                {
                    // Create new report
                    DnsCacheResult Result = new DnsCacheResult(address, GamespyServerAddress);

                    // Add the result to the report
                    DnsCacheReport.AddOrUpdate(Result);

                    // Report progress if we have a progress object
                    if (Progress != null)
                    {
                        Progress.Report(Result);
                    }
                }
            }

            // Verify Stats Server address
            if (StatsServerAddress != null)
            {
                // Create new report
                DnsCacheResult Result = new DnsCacheResult(Bf2StatsHost, StatsServerAddress);

                // Add the result to the report
                DnsCacheReport.AddOrUpdate(Result);

                // Report progress if we have a progress object
                if (Progress != null)
                {
                    Progress.Report(Result);
                }
            }

            // Set internal
            DnsCacheReport.LastRefresh = DateTime.Now;
            return(DnsCacheReport.ErrorFree);
        }
Exemple #4
0
        /// <summary>
        /// This method ping's the gamepsy services and verifies that the HOSTS
        /// file redirects are working correctly
        /// </summary>
        protected void VerifyDnsCache()
        {
            // Loop through each service
            for (int i = 0; i < Services.Length; i++)
            {
                // Quit on cancel
                if (TaskSource.IsCancellationRequested)
                {
                    return;
                }

                // Make sure this service exists in the hosts file
                if (i < 4 && Redirector.GamespyServerAddress == null || Redirector.StatsServerAddress == null)
                {
                    SetStatus(i, "Skipped", Resources.question_button, "Entry not found in HOSTS file. Assumed redirect was not desired by user");
                    continue;
                }

                // Prepare for next service
                SetStatus(i, "Checking, Please Wait...", Resources.loading);

                // Ping server to get the IP address in the dns cache
                try
                {
                    IPAddress      HostsIp = (i == 4) ? Redirector.StatsServerAddress : Redirector.GamespyServerAddress;
                    DnsCacheResult Result  = new DnsCacheResult(Services[i], HostsIp);

                    // Update Gamespy Redirector Cache
                    Redirector.DnsCacheReport.AddOrUpdate(Result);

                    // Throw bad result
                    if (Result.IsFaulted)
                    {
                        // No such hosts is known?
                        if (Result.Error.InnerException != null)
                        {
                            SetStatus(i, "Error Occured", Resources.error, Result.Error.InnerException.Message);
                        }
                        else
                        {
                            SetStatus(i, "Error Occured", Resources.error, Result.Error.Message);
                        }
                    }

                    // Check for cancel before setting a form value
                    if (TaskSource.IsCancellationRequested)
                    {
                        return;
                    }

                    // Verify correct address
                    if (Result.GotExpectedResult)
                    {
                        SetStatus(i, HostsIp.ToString(), Resources.check);
                    }
                    else
                    {
                        SetStatus(i, Result.ResultAddresses[0].ToString(), Resources.warning, "Address expected: " + HostsIp.ToString());
                    }
                }
                catch
                {
                    // Check for cancel before setting a form value
                    if (TaskSource.IsCancellationRequested)
                    {
                        return;
                    }
                }
            }

            // Enable refresh btn
            if (IsHandleCreated)
            {
                Invoke((Action) delegate { RefreshBtn.Enabled = true; });
            }
        }
        /// <summary>
        /// This method ping's the gamepsy services and verifies that the Redirect
        /// services are working. This method effectivly replaces the <see cref="Redirector.VerifyDNSCache()"/>
        /// method.
        /// </summary>
        protected bool VerifyDnsCache()
        {
            // Set default as success
            bool DiagnosticResult = true;

            // Set the System.Net DNS Cache refresh timeout to 1 millisecond
            ServicePointManager.DnsRefreshTimeout = 1;

            // If using System Hosts, Wait 10 seconds to allow the Windows DNS Client to catch up
            if (SelectedMode == RedirectMode.HostsFile)
            {
                for (int i = 0; i < 10; i++)
                {
                    // Get second count
                    int o = 10 - i;

                    // Make sure form wasn't cancelled
                    if (!IsHandleCreated)
                    {
                        return(false);
                    }

                    // Tell the main form thread to update the second count
                    Invoke((Action) delegate
                    {
                        labelDnsText.Text = "Verifying DNS Cache Settings in " + o + " seconds...";
                    });

                    // Wait one
                    Thread.Sleep(1000);
                }

                // Make sure form wasn't cancelled
                if (!IsHandleCreated)
                {
                    return(false);
                }

                // Fix header to display no time
                Invoke((Action) delegate
                {
                    labelDnsText.Text = "Verifying DNS Cache Settings";
                });
            }

            // Clear Old Entries
            Redirector.DnsCacheReport.Entries.Clear();

            // Loop through each service
            for (int i = 0; i < Services.Length; i++)
            {
                // Make sure this service is enabled
                if ((!Bf2webCheckbox.Checked && i == 3) || (!GpcmCheckbox.Checked && i != 3))
                {
                    SetStatus(i, "Skipped", Resources.question_button, "Redirect was not enabled by user");
                    continue;
                }

                // Prepare for next service
                SetStatus(i, "Checking, Please Wait...", Resources.loading);
                Thread.Sleep(300);

                // Ping server to get the IP address in the dns cache
                try
                {
                    IPAddress      HostsIp = (i == 3) ? StatsServerAddress : GamespyServerAddress;
                    DnsCacheResult Result  = new DnsCacheResult(Services[i], HostsIp);

                    // Update Gamespy Redirector Cache
                    Redirector.DnsCacheReport.AddOrUpdate(Result);

                    // Throw bad result
                    if (Result.IsFaulted)
                    {
                        // No such hosts is known?
                        if (Result.Error.InnerException != null)
                        {
                            SetStatus(i, "Error Occured", Resources.error, Result.Error.InnerException.Message);
                        }
                        else
                        {
                            SetStatus(i, "Error Occured", Resources.error, Result.Error.Message);
                        }

                        // Flag error
                        DiagnosticResult = false;
                        continue;
                    }

                    // Verify correct address
                    if (!Result.GotExpectedResult)
                    {
                        SetStatus(i, Result.ResultAddresses[0].ToString(), Resources.warning, "Address expected: " + HostsIp.ToString());

                        // Flag error
                        DiagnosticResult = false;
                    }
                    else
                    {
                        SetStatus(i, HostsIp.ToString(), Resources.check);
                    }
                }
                catch (Exception e)
                {
                    // No such hosts is known?
                    if (e.InnerException != null)
                    {
                        SetStatus(i, "Error Occured", Resources.error, e.InnerException.Message);
                    }
                    else
                    {
                        SetStatus(i, "Error Occured", Resources.error, e.Message);
                    }

                    // Flag error
                    DiagnosticResult = false;
                }
            }

            return(DiagnosticResult);
        }