/// <summary> /// Gets the equality indicator of another MX record class. /// </summary> /// <param name="obj">The object to compare to.</param> /// <returns>True if the specified object is equal to this object.</returns> 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> /// 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(); _dnsType = (Nequeo.Net.Dns.DnsType)pointer.ReadShort(); _dnsClass = (Nequeo.Net.Dns.DnsClass)pointer.ReadShort(); _Ttl = pointer.ReadInt(); // the next short is the record length, we only use it for unrecognised record types int recordLength = pointer.ReadShort(); // and create the appropriate RDATA record based on the dnsType switch (_dnsType) { case Nequeo.Net.Dns.DnsType.NS: // Set the name server record. _record = new NSRecord(pointer); break; case Nequeo.Net.Dns.DnsType.MX: // Set the name mail exchange record. _record = new MXRecord(pointer); break; case Nequeo.Net.Dns.DnsType.A: // Set the name A name record. _record = new ARecord(pointer); break; case Nequeo.Net.Dns.DnsType.SOA: // Set the name SOA record. _record = new SoaRecord(pointer); break; case Nequeo.Net.Dns.DnsType.TXT: // Set the name TXT record. _record = new TxtRecord(pointer); break; case Nequeo.Net.Dns.DnsType.WKS: // Set the name WKS record. _record = new WksRecord(pointer); break; case Nequeo.Net.Dns.DnsType.PTR: // Set the name PTR record. _record = new PtrRecord(pointer); break; case Nequeo.Net.Dns.DnsType.HINFO: // Set the name HINFO record. _record = new HInfoRecord(pointer); break; default: // move the pointer over this unrecognised record pointer += recordLength; break; } }
/// <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="port">the port number used by the domain name server.</param> /// <param name="ipAddress">the ip address of the domain name server.</param> /// <param name="protocolType">the protocol type used by the domain name server.</param> /// <param name="useIPv4EndPoint">use only an IPv4 connection and disregard all other address families (IPv6).</param> /// <returns>An array of MXRecords</returns> internal MXRecord[] MXLookup(string domain, int port, string ipAddress, Nequeo.Net.Dns.ProtocolType protocolType, bool useIPv4EndPoint) { // check the inputs if (domain == null) { throw new ArgumentNullException("domain"); } if (port < 1) { throw new IndexOutOfRangeException("port"); } // 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, Nequeo.Net.Dns.DnsType.MX, Nequeo.Net.Dns.DnsClass.IN)); // fire it off Response response = Lookup(request, domain, port, ipAddress, protocolType, useIPv4EndPoint); // 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="obj">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)); }