/// <summary> /// Resolves and returns a <see cref="AK.Net.Dns.DnsName"/> instance for /// each of the authoritative name servers for the specified /// <paramref name="domain"/>. /// </summary> /// <param name="domain">The domain.</param> /// <returns> /// The list of <see cref="AK.Net.Dns.DnsName"/> instances for each of the /// authoritative name servers for the specified <paramref name="domain"/>. /// </returns> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="domain"/> 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 DnsName[] GetNameServers(DnsName domain) { DnsName qname = AppendNameSuffix(domain); DnsReply reply = Resolve(new DnsQuestion(qname, DnsQueryType.NS, DnsQueryClass.IN)); return(reply.Answers.Where(F.IsNS).Select(x => F.ToNS(x).Domain).ToArray()); }
/// <summary> /// Sends the specified <see cref="AK.Net.Dns.DnsQuery"/> to the specified /// end point and return the <see cref="AK.Net.Dns.DnsReply"/>. /// </summary> /// <param name="query">The query to send.</param> /// <param name="endpoint">The transport end point.</param> /// <returns> /// The <see cref="AK.Net.Dns.DnsReply"/> to the <see cref="AK.Net.Dns.DnsQuery"/>. /// </returns> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="endpoint"/> is <see langword="null"/>. /// </exception> /// <exception cref="AK.Net.Dns.DnsTransportException"> /// Thrown when a transport error occurs. /// </exception> public override DnsReply Send(DnsQuery query, IPEndPoint endpoint) { Guard.NotNull(endpoint, "endpoint"); DnsReply reply = null; IDnsTransport transport = SelectTransport(query); try { reply = transport.Send(query, endpoint); } catch (DnsTransportException exc) { // If the selected transport was TCP, we cannot fail over. // NOTE this comparison is not thread safe, but at worst it would mean // the query being sent again. if (transport == this.TcpTransport) { throw; } this.Log.Warn("UDP transport failure, failing over to TCP:", exc); } if (reply != null) { if (!reply.Header.IsTruncated) { return(reply); } this.Log.InfoFormat("message truncated, failing over to TCP, question={0}", query.Question); } return(this.TcpTransport.Send(query, endpoint)); }
/// <summary> /// /// </summary> /// <param name="reply">The <see cref="AK.Net.Dns.DnsReply"/> to check.</param> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="reply"/> is <see langword="null"/>. /// </exception> /// <exception cref="AK.Net.Dns.DnsResolutionException"> /// Thrown when /// </exception> protected void AssertIsPositiveReply(DnsReply reply) { Guard.NotNull(reply, "reply"); if (reply.Header.ResponseCode != DnsResponseCode.NoError) { throw Guard.DnsResolverNegativeResponseCodeReceived( reply.Header.ResponseCode, reply.Questions.FirstOrDefault()); } }
public static void Main(string[] args) { var nameServers = AKDns.GetNameServers("rcmap.co.uk."); var mxInfo = AKDns.GetMXInfo("rcmap.co.uk."); IDnsTransport transport = new DnsUdpTransport(); DnsQuery query = new DnsQuery(); query.Questions.Add(new DnsQuestion("rcmap.co.uk.", DnsQueryType.A, DnsQueryClass.IN)); DnsReply reply = transport.Send(query, new IPEndPoint(IPAddress.Parse("8.8.8.8"), DnsTransport.DnsPort)); }
/// <summary> /// Attemps to read a <see cref="AK.Net.Dns.DnsReply"/> from the specified /// <paramref name="buffer"/> and returns a value indicating the result of /// the operation. /// </summary> /// <param name="buffer">The data buffer.</param> /// <param name="offset">The offset in <paramref name="buffer"/> at which /// reading begins.</param> /// <param name="count">The number of bytes available within /// <paramref name="buffer"/>.</param> /// <param name="reply">On success, this argument will contain the /// read <see cref="AK.Net.Dns.DnsReply"/>.</param> /// <returns><see langword="true"/> if a <see cref="AK.Net.Dns.DnsReply"/> /// was successfully read from the specified <paramref name="buffer"/>, /// otherwise; <see langword="false"/>.</returns> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="buffer"/> is <see langword="null"/>. /// </exception> /// <exception cref="System.ArgumentOutOfRangeException"> /// Thrown when /// <list type="bullet"> /// <item> /// <paramref name="offset"/> is negative or greater than the length /// of <paramref name="buffer"/> /// </item> /// <item> /// <paramref name="count"/> is negative or greater than the length of /// <paramref name="buffer"/> given <paramref name="offset"/>. /// </item> /// </list> /// </exception> protected virtual bool TryReadReply(byte[] buffer, int offset, int count, out DnsReply reply) { reply = new DnsReply(); try { using (DnsWireReader reader = new DnsWireReader(buffer, offset, count)) reply.Read(reader); } catch (DnsFormatException) { reply = null; } return(reply != null); }
/// <summary> /// Resolves and returns an <see cref="System.Net.IPHostEntry"/> for the /// specified <see cref="System.Net.IPAddress"/>. /// </summary> /// <param name="address">The host's IP address.</param> /// <returns>An <see cref="System.Net.IPHostEntry"/> containing information /// associated with the host.</returns> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="address"/> 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 IPHostEntry GetHostEntry(IPAddress address) { Guard.NotNull(address, "address"); DnsName qname = PtrRecord.MakeName(address); DnsReply reply = Resolve(new DnsQuestion(qname, DnsQueryType.Ptr, DnsQueryClass.IN)); return(new IPHostEntry() { AddressList = new IPAddress[] { address }, Aliases = DnsUtility.EMPTY_STRING_ARRAY, HostName = (from ptr in reply.Answers where F.IsPtr(ptr) && ptr.Owner.Equals(qname) select F.ToPtr(ptr).Domain).FirstOrDefault() ?? null }); }
/// <summary> /// Resolves and returns the <see cref="AK.Net.Dns.MXInfo"/> instance for the /// specified <paramref name="domain"/>. /// </summary> /// <param name="domain">The domain name.</param> /// <returns>The mail exchange information associated with the /// <paramref name="domain"/>.</returns> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="domain"/> 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 MXInfo GetMXInfo(DnsName domain) { DnsName qname = AppendNameSuffix(domain); DnsReply reply = Resolve(new DnsQuestion(qname, DnsQueryType.MX, DnsQueryClass.IN)); if (reply.Answers.Count > 0) { return(new MXInfo(qname, (from ans in reply.Answers where F.IsMX(ans) let mx = F.ToMX(ans) orderby mx select mx.Exchange).ToArray() )); } return(new MXInfo(domain)); }
/// <summary> /// Puts the records contained within the specified <paramref name="reply"/> /// into the cache. /// </summary> /// <param name="reply">The reply containing the records to cache.</param> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="reply"/> is <see langword="null"/>. /// </exception> public void Put(DnsReply reply) { Guard.NotNull(reply, "reply"); }
/// <summary> /// Attemps to read a <see cref="AK.Net.Dns.DnsReply"/> from the specified /// <paramref name="buffer"/> and returns a value indicating the result of /// the operation. /// </summary> /// <param name="buffer">The data buffer.</param> /// <param name="reply">On success, this argument will contain the /// read <see cref="AK.Net.Dns.DnsReply"/>.</param> /// <returns><see langword="true"/> if a <see cref="AK.Net.Dns.DnsReply"/> /// was successfully read from the specified <paramref name="buffer"/>, /// otherwise; <see langword="false"/>.</returns> /// <exception cref="System.ArgumentNullException"> /// Thrown when <paramref name="buffer"/> is <see langword="null"/>. /// </exception> protected virtual bool TryReadReply(byte[] buffer, out DnsReply reply) { Guard.NotNull(buffer, "buffer"); return(TryReadReply(buffer, 0, buffer.Length, out reply)); }