/// <summary> /// Look up a DNS SRV record, returning the best host and port number to connect to. /// </summary> /// <param name="prefix">The SRV prefix, ending with a dot. Example: "_xmpp-client._tcp."</param> /// <param name="domain">The domain to check</param> /// <param name="host">The host name to connect to</param> /// <param name="port">The port number to connect to</param> public static void LookupSRV(string prefix, string domain, ref string host, ref int port) { if (prefix == null) throw new ArgumentNullException("prefix"); if (domain == null) throw new ArgumentNullException("domain"); if (!prefix.EndsWith(".")) throw new ArgumentOutOfRangeException("Prefix must end in '.'", "prefix"); try { SRVRecord record; if (Environment.OSVersion.Platform != PlatformID.Unix) { DnsRequest request = new DnsRequest(prefix + domain); DnsResponse response = request.GetResponse(DnsRecordType.SRV); record = PickSRV(response.SRVRecords); } else { var records = UnixDnsResolver.ResolveSRV(prefix + domain); record = PickSRV(records); } host = record.NameNext; port = record.Port; Debug.WriteLine(string.Format("SRV found: {0}:{1}", host, port)); } catch { host = domain; } }
/// <summary> /// Look up a DNS TXT record. /// </summary> /// <param name="prefix">The prefix, ending in '.'. Example: "_xmppconnect."</param> /// <param name="domain">The domain to search</param> /// <param name="attribute">The attribute name to look for. Example: "_xmpp-client-xbosh"</param> /// <returns></returns> public static string LookupTXT(string prefix, string domain, string attribute) { if (prefix == null) throw new ArgumentNullException("prefix"); if (domain == null) throw new ArgumentNullException("domain"); if (attribute == null) throw new ArgumentNullException("attribute"); if (!prefix.EndsWith(".")) throw new ArgumentOutOfRangeException("Prefix must end in '.'", "prefix"); try { if (Environment.OSVersion.Platform != PlatformID.Unix) { DnsRequest request = new DnsRequest(prefix + domain); DnsResponse response = request.GetResponse(DnsRecordType.TEXT); string attr = attribute + "="; foreach (TXTRecord txt in response.TXTRecords) { if (txt.StringArray.StartsWith(attr)) { Debug.WriteLine(string.Format("TXT found: {0}", txt.StringArray)); return txt.StringArray.Substring(attr.Length); } } } else { // FIXME: Add TXT support to UnixDnsResolver... throw new NotImplementedException(); } } catch { } return null; }
/// <summary> /// Look up a DNS TXT record. /// </summary> /// <param name="prefix">The prefix, ending in '.'. Example: "_xmppconnect."</param> /// <param name="domain">The domain to search</param> /// <param name="attribute">The attribute name to look for. Example: "_xmpp-client-xbosh"</param> /// <returns></returns> public static string LookupTXT(string prefix, string domain, string attribute) { if (prefix == null) throw new ArgumentNullException("prefix"); if (domain == null) throw new ArgumentNullException("domain"); if (attribute == null) throw new ArgumentNullException("attribute"); if (!prefix.EndsWith(".")) throw new ArgumentOutOfRangeException("Prefix must end in '.'", "prefix"); try { DnsRequest request = new DnsRequest(prefix + domain); DnsResponse response = request.GetResponse(DnsRecordType.TEXT); string attr = attribute + "="; foreach (TXTRecord txt in response.TXTRecords) { if (txt.StringArray.StartsWith(attr)) { Debug.WriteLine(string.Format("TXT found: {0}", txt.StringArray)); return txt.StringArray.Substring(attr.Length); } } } catch { } return null; }