示例#1
0
        /// <summary>
        /// Initialises and resolves the DNS Domain, and set to dnsCurrent the next
        /// SRV record to use
        /// </summary>
        /// <param name="domain">XMPP Domain</param>
        /// <returns>XMPP server hostname for the Domain</returns>
        private SrvRecord moveNextSrvDNS(string domain)    {
            
            domain.ThrowIfNullOrEmpty("domain");
            //If already a lookup has being made return
            if (dnsIsInit) {
                //If it is already init we remove the current 
                if (dnsRecordList != null && dnsCurrent != null) dnsRecordList.Remove(dnsCurrent);
                dnsCurrent = dnsRecordList.FirstOrDefault();
                return dnsCurrent;
            };
            dnsIsInit = true;

            DnsMessage dnsMessage = DnsClient.Default.Resolve("_xmpp-client._tcp." + domain, RecordType.Srv);
            if ((dnsMessage == null) || ((dnsMessage.ReturnCode != ReturnCode.NoError) && (dnsMessage.ReturnCode != ReturnCode.NxDomain)))
            {
                //If DNS SRV records lookup fails then continue with the host name 
                #if DEBUG
                System.Diagnostics.Debug.WriteLine("DNS Lookup Failed");
                #endif
                return null;
            }
            else
            {
                var tempList = new List<SrvRecord>();
                
                foreach (DnsRecordBase dnsRecord in dnsMessage.AnswerRecords)
                {
                    SrvRecord srvRecord = dnsRecord as SrvRecord;
                    if (srvRecord != null)
                    {
                        tempList.Add(srvRecord);
                        Console.WriteLine(srvRecord.ToString());
                        Console.WriteLine("  |--- Name " + srvRecord.Name);
                        Console.WriteLine("  |--- Port: " + srvRecord.Port);
                        Console.WriteLine("  |--- Priority" + srvRecord.Priority);
                        Console.WriteLine("  |--- Type " + srvRecord.RecordType);
                        Console.WriteLine("  |--- Target: " + srvRecord.Target);
                        Console.WriteLine();
                    }
                }
                dnsRecordList = tempList.OrderBy(o => o.Priority).ThenBy(order => order.Weight).ToList();

                dnsCurrent = dnsRecordList.FirstOrDefault();
                return dnsCurrent;
            }                   

        }
        } // End Class DbDnsRecord


        static async Task <bool> ResolveMessage(DnsMessage query, QueryReceivedEventArgs e, DnsMessage response)
        {
            DbDnsRecord rec = null;

            using (System.Data.Common.DbCommand cmd = s_sql.CreateCommand(@"
-- DECLARE @in_recordType int 
-- DECLARE @in_recordName varchar(4000) 

-- SET @in_recordType = 1 -- A 
-- SET @in_recordName = 'vortex.data.microsoft.com' 


SELECT 
	 REC_Id
	--,T_Records.REC_DOM_Id
	,T_Records.REC_RT_Id
	,T_Records.REC_Name
	,T_Records.REC_Content
	,T_Records.REC_ResponsibleName
	,COALESCE(T_Records.REC_TTL, 100) AS REC_TTL 
	,T_Records.REC_Prio
	,T_Records.REC_Weight
	,T_Records.REC_Port
	,T_Records.REC_SerialNumber
	,T_Records.REC_RefreshInterval
	,T_Records.REC_RetryInterval
	,T_Records.REC_ExpireInterval
	,T_Records.REC_NegativeCachingTTL
	,T_Records.REC_AfsSubType
	,T_Records.REC_ChangeDate
FROM T_Records 
WHERE REC_RT_Id	= @in_recordType 
AND T_Records.REC_Name = @in_recordName 
;
"))
            {
                try
                {
                    string name = query.Questions[0].Name.ToString();

                    s_sql.AddParameter(cmd, "in_recordType", (int)query.Questions[0].RecordType);
                    s_sql.AddParameter(cmd, "in_recordName", name);

                    // TODO: Can return multiple records...
                    rec = s_sql.GetClass <DbDnsRecord>(cmd);
                }
                catch (System.Exception ex)
                {
                    System.Console.WriteLine(ex.Message);
                    System.Console.WriteLine(ex.StackTrace);
                }
            } // End Using cmd

            if (rec != null)
            {
                int ttl = 3600;

                DnsRecordBase record = null;

                // https://blog.dnsimple.com/2015/04/common-dns-records/
                // https://en.wikipedia.org/wiki/List_of_DNS_record_types
                switch ((RecordType)rec.REC_RT_Id)
                {
                case RecordType.Soa:
                    // SoaRecord(DomainName name, int timeToLive, DomainName masterName
                    //  , DomainName responsibleName, uint serialNumber, int refreshInterval
                    //  , int retryInterval, int expireInterval, int negativeCachingTTL)
                    record = new ARSoft.Tools.Net.Dns.SoaRecord(
                        DomainName.Parse(rec.REC_Name)
                        , rec.REC_TTL.Value
                        , DomainName.Parse(rec.REC_Content)
                        , DomainName.Parse(rec.REC_ResponsibleName)
                        , rec.REC_SerialNumber.Value
                        , rec.REC_RefreshInterval.Value
                        , rec.REC_RetryInterval.Value
                        , rec.REC_ExpireInterval.Value
                        , rec.REC_NegativeCachingTTL.Value
                        );
                    break;

                case RecordType.Ns:
                    record = new ARSoft.Tools.Net.Dns.NsRecord(DomainName.Parse(rec.REC_Name), ttl, DomainName.Parse(rec.REC_Content));
                    break;

                case RecordType.Srv:
                    // SrvRecord(DomainName name, int timeToLive, ushort priority, ushort weight, ushort port, DomainName target)
                    record = new ARSoft.Tools.Net.Dns.SrvRecord(DomainName.Parse(rec.REC_Name), rec.REC_TTL.Value, (ushort)rec.REC_Prio.Value, (ushort)rec.REC_Weight.Value, (ushort)rec.REC_Port.Value, DomainName.Parse(rec.REC_Content));
                    break;

                // https://www.openafs.org/
                // https://en.wikipedia.org/wiki/OpenAFS
                // http://www.rjsystems.nl/en/2100-dns-discovery-openafs.php
                // OpenAFS is an open source implementation of the Andrew distributed file system(AFS).
                case RecordType.Afsdb:
                    // http://www.rjsystems.nl/en/2100-dns-discovery-openafs.php

                    record = new ARSoft.Tools.Net.Dns.AfsdbRecord(DomainName.Parse(rec.REC_Name), rec.REC_TTL.Value, (AfsdbRecord.AfsSubType)(uint) rec.REC_AfsSubType.Value, DomainName.Parse(rec.REC_Content));
                    break;

                // A DNS-based Authentication of Named Entities (DANE) method
                // for publishing and locating OpenPGP public keys in DNS
                // for a specific email address using an OPENPGPKEY DNS resource record.
                case RecordType.OpenPGPKey:
                    byte[] publicKey = null;

                    // hexdump(sha256(truncate(utf8(ocalpart), 28)
                    // https://www.huque.com/bin/openpgpkey
                    // The OPENPGPKEY DNS record is specied in RFC 7929.
                    // The localpart of the uid is encoded as a DNS label
                    // containing the hexdump of the SHA-256 hash
                    // of the utf-8 encoded localpart, truncated to 28 octets.
                    // Normally the "Standard" output format should be used.
                    // The "Generic Encoding" output format is provided to help work
                    // with older DNS software that does not yet understand the OPENPGPKEY record type.
                    record = new ARSoft.Tools.Net.Dns.OpenPGPKeyRecord(DomainName.Parse(rec.REC_Name), ttl, publicKey);
                    break;

                // Canonical name records, or CNAME records, are often called alias records because they map an alias to the canonical name. When a name server finds a CNAME record, it replaces the name with the canonical name and looks up the new name.
                case RecordType.CName:
                    record = new ARSoft.Tools.Net.Dns.CNameRecord(DomainName.Parse(rec.REC_Name), rec.REC_TTL.Value, DomainName.Parse(rec.REC_Content));
                    break;

                case RecordType.Ptr:
                    record = new ARSoft.Tools.Net.Dns.PtrRecord(DomainName.Parse(rec.REC_Name), rec.REC_TTL.Value, DomainName.Parse(rec.REC_Content));
                    break;

                case RecordType.A:
                    record = new ARSoft.Tools.Net.Dns.ARecord(DomainName.Parse(rec.REC_Name), rec.REC_TTL.Value, System.Net.IPAddress.Parse(rec.REC_Content));
                    break;

                case RecordType.Aaaa:
                    record = new ARSoft.Tools.Net.Dns.AaaaRecord(DomainName.Parse(rec.REC_Name), rec.REC_TTL.Value, System.Net.IPAddress.Parse(rec.REC_Content));
                    break;

                case RecordType.Mx:
                    record = new ARSoft.Tools.Net.Dns.MxRecord(DomainName.Parse(rec.REC_Name), rec.REC_TTL.Value, 0, DomainName.Parse(rec.REC_Content));
                    break;

                case RecordType.Txt:
                    record = new ARSoft.Tools.Net.Dns.TxtRecord(DomainName.Parse(rec.REC_Name), rec.REC_TTL.Value, rec.REC_Content);
                    break;

                case RecordType.SshFp:
                    // https://unix.stackexchange.com/questions/121880/how-do-i-generate-sshfp-records

                    // SshFpRecord(DomainName name, int timeToLive, SshFpAlgorithm algorithm
                    //     , SshFpFingerPrintType fingerPrintType, byte[] fingerPrint)
                    ARSoft.Tools.Net.Dns.SshFpRecord.SshFpAlgorithm sfa = ARSoft.Tools.Net.Dns.SshFpRecord
                                                                          .SshFpAlgorithm.Rsa;

                    ARSoft.Tools.Net.Dns.SshFpRecord.SshFpFingerPrintType sfp = ARSoft.Tools.Net.Dns.SshFpRecord
                                                                                .SshFpFingerPrintType.Sha256;

                    byte[] fp = null;

                    record = new ARSoft.Tools.Net.Dns.SshFpRecord(DomainName.Parse(rec.REC_Name), rec.REC_TTL.Value, sfa, sfp, fp);
                    break;

                default:
                    break;
                } // End Switch

                if (record != null)
                {
                    response.AnswerRecords.Add(record);
                }

                response.ReturnCode = ReturnCode.NoError;
                e.Response          = response;
                return(await Task <bool> .FromResult(true));
            } // End if (rec != null)

            return(await Task <bool> .FromResult(false));
        } // End Function ResolveMessage