/// <summary> /// Parses resource record from reply data. /// </summary> /// <param name="reply">DNS server reply data.</param> /// <param name="offset">Current offset in reply data.</param> /// <param name="rdLength">Resource record data length.</param> /// <param name="ttl">Time to live in seconds.</param> public static DNS_rr_TXT Parse(byte[] reply, ref int offset, int rdLength, int ttl) { // TXT RR string text = Dns_Client.ReadCharacterString(reply, ref offset); return(new DNS_rr_TXT(text, ttl)); }
/// <summary> /// Parses resource record from reply data. /// </summary> /// <param name="reply">DNS server reply data.</param> /// <param name="offset">Current offset in reply data.</param> /// <param name="rdLength">Resource record data length.</param> /// <param name="ttl">Time to live in seconds.</param> public static DNS_rr_CNAME Parse(byte[] reply, ref int offset, int rdLength, int ttl) { string name = ""; if (Dns_Client.GetQName(reply, ref offset, ref name)) { return(new DNS_rr_CNAME(name, ttl)); } else { throw new ArgumentException("Invalid CNAME resource record data !"); } }
/// <summary> /// Parses resource record from reply data. /// </summary> /// <param name="reply">DNS server reply data.</param> /// <param name="offset">Current offset in reply data.</param> /// <param name="rdLength">Resource record data length.</param> /// <param name="ttl">Time to live in seconds.</param> public static DNS_rr_SRV Parse(byte[] reply, ref int offset, int rdLength, int ttl) { // Priority Weight Port Target // Priority int priority = reply[offset++] << 8 | reply[offset++]; // Weight int weight = reply[offset++] << 8 | reply[offset++]; // Port int port = reply[offset++] << 8 | reply[offset++]; // Target string target = ""; Dns_Client.GetQName(reply, ref offset, ref target); return new DNS_rr_SRV(priority, weight, port, target, ttl); }
/// <summary> /// Resolves host name to IP addresses. /// </summary> /// <param name="host">Host name or IP address.</param> /// <returns>Return specified host IP addresses.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>host</b> is null.</exception> public static IPAddress[] Resolve(string host) { if (host == null) { throw new ArgumentNullException("host"); } // If hostName_IP is IP try { return(new[] { IPAddress.Parse(host) }); } catch {} // This is probably NetBios name if (host.IndexOf(".") == -1) { return(Dns.GetHostEntry(host).AddressList); } else { // hostName_IP must be host name, try to resolve it's IP Dns_Client dns = new Dns_Client(); DnsServerResponse resp = dns.Query(host, QTYPE.A); if (resp.ResponseCode == RCODE.NO_ERROR) { DNS_rr_A[] records = resp.GetARecords(); IPAddress[] retVal = new IPAddress[records.Length]; for (int i = 0; i < records.Length; i++) { retVal[i] = records[i].IP; } return(retVal); } else { throw new Exception(resp.ResponseCode.ToString()); } } }
/// <summary> /// Parses resource record from reply data. /// </summary> /// <param name="reply">DNS server reply data.</param> /// <param name="offset">Current offset in reply data.</param> /// <param name="rdLength">Resource record data length.</param> /// <param name="ttl">Time to live in seconds.</param> public static DNS_rr_NAPTR Parse(byte[] reply, ref int offset, int rdLength, int ttl) { /* RFC 3403. * The packet format for the NAPTR record is as follows * 1 1 1 1 1 1 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | ORDER | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | PREFERENCE | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | / FLAGS / +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | / SERVICES / +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | / REGEXP / +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | / REPLACEMENT / | / / +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ */ int order = reply[offset++] << 8 | reply[offset++]; int preference = reply[offset++] << 8 | reply[offset++]; string flags = Dns_Client.ReadCharacterString(reply, ref offset); string services = Dns_Client.ReadCharacterString(reply, ref offset); string regexp = Dns_Client.ReadCharacterString(reply, ref offset); string replacement = ""; Dns_Client.GetQName(reply, ref offset, ref replacement); return(new DNS_rr_NAPTR(order, preference, flags, services, regexp, replacement, ttl)); }
/// <summary> /// Parses resource record from reply data. /// </summary> /// <param name="reply">DNS server reply data.</param> /// <param name="offset">Current offset in reply data.</param> /// <param name="rdLength">Resource record data length.</param> /// <param name="ttl">Time to live in seconds.</param> public static DNS_rr_MX Parse(byte[] reply, ref int offset, int rdLength, int ttl) { /* RFC 1035 3.3.9. MX RDATA format * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | PREFERENCE | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | / EXCHANGE / | / / +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | | where: | | PREFERENCE | A 16 bit integer which specifies the preference given to | this RR among others at the same owner. Lower values | are preferred. | | EXCHANGE | A <domain-name> which specifies a host willing to act as | a mail exchange for the owner name. */ int pref = reply[offset++] << 8 | reply[offset++]; string name = ""; if (Dns_Client.GetQName(reply, ref offset, ref name)) { return(new DNS_rr_MX(pref, name, ttl)); } else { throw new ArgumentException("Invalid MX resource record data !"); } }
/// <summary> /// Parses resource record from reply data. /// </summary> /// <param name="reply">DNS server reply data.</param> /// <param name="offset">Current offset in reply data.</param> /// <param name="rdLength">Resource record data length.</param> /// <param name="ttl">Time to live in seconds.</param> public static DNS_rr_SOA Parse(byte[] reply, ref int offset, int rdLength, int ttl) { /* RFC 1035 3.3.13. SOA RDATA format * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ * / MNAME / * / / +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ * / RNAME / +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | SERIAL | | | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | REFRESH | | | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | RETRY | | | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | EXPIRE | | | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | MINIMUM | | | +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ | | where: | | MNAME The <domain-name> of the name server that was the | original or primary source of data for this zone. | | RNAME A <domain-name> which specifies the mailbox of the | person responsible for this zone. | | SERIAL The unsigned 32 bit version number of the original copy | of the zone. Zone transfers preserve this value. This | value wraps and should be compared using sequence space | arithmetic. | | REFRESH A 32 bit time interval before the zone should be | refreshed. | | RETRY A 32 bit time interval that should elapse before a | failed refresh should be retried. | | EXPIRE A 32 bit time value that specifies the upper limit on | the time interval that can elapse before the zone is no | longer authoritative. | | MINIMUM The unsigned 32 bit minimum TTL field that should be | exported with any RR from this zone. */ //---- Parse record -------------------------------------------------------------// // MNAME string nameserver = ""; Dns_Client.GetQName(reply, ref offset, ref nameserver); // RNAME string adminMailBox = ""; Dns_Client.GetQName(reply, ref offset, ref adminMailBox); char[] adminMailBoxAr = adminMailBox.ToCharArray(); for (int i = 0; i < adminMailBoxAr.Length; i++) { if (adminMailBoxAr[i] == '.') { adminMailBoxAr[i] = '@'; break; } } adminMailBox = new string(adminMailBoxAr); // SERIAL long serial = reply[offset++] << 24 | reply[offset++] << 16 | reply[offset++] << 8 | reply[offset++]; // REFRESH long refresh = reply[offset++] << 24 | reply[offset++] << 16 | reply[offset++] << 8 | reply[offset++]; // RETRY long retry = reply[offset++] << 24 | reply[offset++] << 16 | reply[offset++] << 8 | reply[offset++]; // EXPIRE long expire = reply[offset++] << 24 | reply[offset++] << 16 | reply[offset++] << 8 | reply[offset++]; // MINIMUM long minimum = reply[offset++] << 24 | reply[offset++] << 16 | reply[offset++] << 8 | reply[offset++]; //--------------------------------------------------------------------------------// return(new DNS_rr_SOA(nameserver, adminMailBox, serial, refresh, retry, expire, minimum, ttl)); }
/// <summary> /// Resolves host name to IP addresses. /// </summary> /// <param name="host">Host name or IP address.</param> /// <returns>Return specified host IP addresses.</returns> /// <exception cref="ArgumentNullException">Is raised when <b>host</b> is null.</exception> public static IPAddress[] Resolve(string host) { if (host == null) { throw new ArgumentNullException("host"); } // If hostName_IP is IP try { return new[] {IPAddress.Parse(host)}; } catch {} // This is probably NetBios name if (host.IndexOf(".") == -1) { return Dns.GetHostEntry(host).AddressList; } else { // hostName_IP must be host name, try to resolve it's IP Dns_Client dns = new Dns_Client(); DnsServerResponse resp = dns.Query(host, QTYPE.A); if (resp.ResponseCode == RCODE.NO_ERROR) { DNS_rr_A[] records = resp.GetARecords(); IPAddress[] retVal = new IPAddress[records.Length]; for (int i = 0; i < records.Length; i++) { retVal[i] = records[i].IP; } return retVal; } else { throw new Exception(resp.ResponseCode.ToString()); } } }