/* * public static bool operator<(MXRecord record1, MXRecord record2) * { * if (record1._preference > record2._preference) return false; * if (record1._domainName > record2._domainName) return false; * return false; * } * * public static bool operator>(MXRecord record1, MXRecord record2) * { * if (record1._preference < record2._preference) return false; * if (record1._domainName < record2._domainName) return false; * return false; * } */ public override bool Equals(object obj) { // this object isn't null if (obj == null) { return(false); } // must be of same type if (this.GetType() != obj.GetType()) { return(false); } MXRecord mxOther = (MXRecord)obj; // preference must match if (mxOther._preference != _preference) { return(false); } // and so must the domain name if (mxOther._domainName != _domainName) { return(false); } // its a match return(true); }
/// <summary> /// Shorthand form to make MX querying easier, essentially wraps up the retreival /// of the MX records, and sorts them by preference /// </summary> /// <param name="domain">domain name to retreive MX RRs for</param> /// <param name="dnsServer">the server we're going to ask</param> /// <returns>An array of MXRecords</returns> public static MXRecord[] MXLookup(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 MX IN lookup for the supplied domain request.AddQuestion(new Question(domain, DnsType.MX, 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 MX records ArrayList resourceRecords = new ArrayList(); // add each of the answers to the array foreach (Answer answer in response.Answers) { // if the answer is an MX record if (answer.Record.GetType() == typeof(MXRecord)) { // add it to our array resourceRecords.Add(answer.Record); } } // create array of MX records MXRecord[] mxRecords = new MXRecord[resourceRecords.Count]; // copy from the array list resourceRecords.CopyTo(mxRecords); // sort into lowest preference order Array.Sort(mxRecords); // and return return(mxRecords); }
/// <summary> /// Shorthand form to make MX querying easier, essentially wraps up the retreival /// of the MX records, and sorts them by preference /// </summary> /// <param name="domain">domain name to retreive MX RRs for</param> /// <param name="dnsServer">the server we're going to ask</param> /// <returns>An array of MXRecords</returns> public static MXRecord[] MXLookup(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 MX IN lookup for the supplied domain request.AddQuestion(new Question(domain, DnsType.MX, 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 MX records ArrayList resourceRecords = new ArrayList(); // add each of the answers to the array foreach(Answer answer in response.Answers) { // if the answer is an MX record if (answer.Record.GetType() == typeof(MXRecord)) { // add it to our array resourceRecords.Add(answer.Record); } } // create array of MX records MXRecord[] mxRecords = new MXRecord[resourceRecords.Count]; // copy from the array list resourceRecords.CopyTo(mxRecords); // sort into lowest preference order Array.Sort(mxRecords); // and return return mxRecords; }
/// <summary> /// Implements the IComparable interface so that we can sort the MX records by their /// lowest preference /// </summary> /// <param name="other">the other MxRecord to compare against</param> /// <returns>1, 0, -1</returns> public int CompareTo(object obj) { MXRecord mxOther = (MXRecord)obj; // we want to be able to sort them by preference if (mxOther._preference < _preference) { return(1); } if (mxOther._preference > _preference) { return(-1); } // order mail servers of same preference by name return(-mxOther._domainName.CompareTo(_domainName)); }
/// <summary> /// Construct a resource record from a pointer to a byte array /// </summary> /// <param name="pointer">the position in the byte array of the record</param> internal ResourceRecord(Pointer pointer) { // extract the domain, question type, question class and Ttl Domain = pointer.ReadDomain(); Type = (DnsType)pointer.ReadShort(); Class = (DnsClass)pointer.ReadShort(); Ttl = pointer.ReadInt(); // the next short is the record length, we only use it for unrecognized record types int recordLength = pointer.ReadShort(); // and create the appropriate RDATA record based on the dnsType switch (Type) { case DnsType.NS: Record = new NSRecord(pointer); break; case DnsType.MX: Record = new MXRecord(pointer); break; case DnsType.ANAME: Record = new ANameRecord(pointer); break; case DnsType.CNAME: Record = new CNameRecord(pointer); break; case DnsType.SOA: Record = new SoaRecord(pointer); break; case DnsType.TXT: Record = new TXTRecord(pointer, recordLength); break; default: { // move the pointer over this unrecognized record pointer.Seek(recordLength); break; } } }