示例#1
0
 /// <summary>
 /// Devuelve el primer DNS que funcione para un dominio dado
 /// </summary>
 /// <param name="r"></param>
 /// <param name="domain"></param>
 /// <returns></returns>
 public static string GetNSServer(Resolver r, string domain)
 {
     try
     {
         Response response = r.Query(domain, QType.NS);
         if (response.RecordsNS.Length > 0)
         {
             //Se busca hasta encontrar el DNS autoritativo
             if (response.header.AA == false)
             {
                 foreach (RecordNS rNS in response.RecordsNS)
                 {
                     List <string> NSServers = GetNSServer(r, domain, rNS.NSDNAME);
                     if (NSServers.Count > 0)
                     {
                         foreach (string dns in NSServers)
                         {
                             //Se devuelve el primero de ellos
                             return(DNSUtil.RemoveLastPoint(dns));
                         }
                     }
                 }
             }
             else
             {
                 foreach (RecordNS rNS in response.RecordsNS)
                 {
                     //Si no es una respuesta autoritativa volver a preguntar
                     string dns = RemoveLastPoint(rNS.NSDNAME);
                     if (TestDNS(dns))
                     {
                         return(dns);
                     }
                 }
             }
         }
         //Hay servidores autoritativos para esta petición
         else if (response.Authorities.Count > 0)
         {
             try
             {
                 //Se devuelve el servidor DNS autoritativo
                 if (((RR)response.Authorities[0]).RECORD is RecordSOA)
                 {
                     string dns = RemoveLastPoint(((RecordSOA)((RR)response.Authorities[0]).RECORD).MNAME);
                     if (TestDNS(dns))
                     {
                         return(dns);
                     }
                 }
                 if (((RR)response.Authorities[0]).RECORD is RecordNS)
                 {
                     string dns = RemoveLastPoint(((RecordNS)((RR)response.Authorities[0]).RECORD).NSDNAME);
                     if (TestDNS(dns))
                     {
                         return(dns);
                     }
                 }
             }
             catch { }
         }
     }
     catch { }
     //Devuelve el primer DNS local que funcione...
     foreach (IPEndPoint dns in GetLocalNSServer())
     {
         if (TestDNS(dns.Address.ToString()))
         {
             return(dns.Address.ToString());
         }
     }
     //Si ninguno a funcionado devolver el primero...
     return(GetLocalNSServer()[0].Address.ToString());
 }