Esempio n. 1
0
        /// <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
            {
                IList <SRVRecord> srvRecords = DnsServiceFactory.Create().GetSrvRecords(prefix + domain);

                SRVRecord record = PickSRV(srvRecords.ToArray());
                host = record.NameNext;
                port = record.Port;
                Debug.WriteLine(string.Format("SRV found: {0}:{1}", host, port));
            }
            catch
            {
                host = domain;
            }
        }
Esempio n. 2
0
        /// <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
            {
                IList <TXTRecord> txtRecords = DnsServiceFactory.Create().GetTxtRecords(prefix + domain);
                string            attr       = attribute + "=";
                foreach (TXTRecord txt in txtRecords)
                {
                    if (txt.StringArray.StartsWith(attr))
                    {
                        Debug.WriteLine(string.Format("TXT found: {0}", txt.StringArray));
                        return(txt.StringArray.Substring(attr.Length));
                    }
                }
            }
            catch
            {
            }
            return(null);
        }