/// <summary> /// Implements the IComparable interface so that we can sort the SRV records by their /// lowest priority /// </summary> /// <param name="other">the other SRVRecord to compare against</param> /// <returns>1, 0, -1</returns> public int CompareTo(object obj) { SRVRecord srvOther = (SRVRecord)obj; // we want to be able to sort them by priority from lowest to highest. if (m_Priority < srvOther.m_Priority) { return(-1); } if (m_Priority > srvOther.m_Priority) { return(1); } // if the priority is the same, sort by highest weight to lowest (higher // weighting means that server should get more of the requests) if (m_Weight > srvOther.m_Weight) { return(-1); } if (m_Weight < srvOther.m_Weight) { return(1); } return(0); }
/// <summary> /// Shorthand form to make SRV querying easier, essentially wraps up the retreival /// of the SRV records, and sorts them by preference /// </summary> /// <param name="domain">domain name to retreive SRV RRs for</param> /// <param name="dnsServer">the server we're going to ask</param> /// <returns>An array of SRVRecords</returns> public static SRVRecord[] SRVLookup(string domain, IPAddress dnsServer) { // check the inputs if (domain == null) { throw new ArgumentNullException("domain"); } if (dnsServer == null) { throw new ArgumentNullException("dnsServer"); } // create a request for this Request request = new Request(); // add one question - the SRV IN lookup for the supplied domain request.AddQuestion(new Question(domain, DnsType.SRV, DnsClass.IN)); // fire it off Response response = Lookup(request, dnsServer); // if we didn't get a response, then return null if (response == null) { return(null); } // create a growable array of SRV records ArrayList resourceRecords = new ArrayList(); // add each of the answers to the array foreach (Answer answer in response.Answers) { // if the answer is an SRV record if (answer.Type == DnsType.SRV) { // add it to our array resourceRecords.Add(answer.Record); } } // create array of MX records SRVRecord[] srvRecords = new SRVRecord[resourceRecords.Count]; // copy from the array list resourceRecords.CopyTo(srvRecords); // sort into lowest preference order Array.Sort(srvRecords); // and return return(srvRecords); }