Beispiel #1
0
        internal static DnsEntry Create(Linux.LDnsHeader head, Linux.LDnsReader reader)
        {
            switch (head.Type)
            {
            case (ushort)DnsRecordType.A:
            case (ushort)DnsRecordType.AAAA:
                return(new DnsHostEntry(head, reader));

            case (ushort)DnsRecordType.NS:
            case (ushort)DnsRecordType.PTR:
            case (ushort)DnsRecordType.CNAME:
                return(new DnsPointerEntry(head, reader));

            case (ushort)DnsRecordType.MX:
                return(new DnsMailExchangeEntry(head, reader));

            case (ushort)DnsRecordType.SRV:
                return(new DnsServiceEntry(head, reader));

            case (ushort)DnsRecordType.TXT:
                return(new DnsTextEntry(head, reader));

            default:
                //reader.Current += head.DataLen;
                throw new NotSupportedException();
            }
        }
Beispiel #2
0
        internal DnsEntry(Linux.LDnsHeader head)
        {
            Type = (DnsRecordType)head.Type;
            Name = head.Name;

            TimeToLive = TimeSpan.FromSeconds(head.TTL);
            Expires    = DateTime.UtcNow.Add(TimeToLive);
        }
Beispiel #3
0
 internal DnsServiceEntry(Linux.LDnsHeader head, Linux.LDnsReader reader) : base(head)
 {
     // Read Order Matters
     Priority = reader.UInt16();
     Weight   = reader.UInt16();
     Port     = reader.UInt16();
     Host     = reader.Name();
 }
Beispiel #4
0
        internal DnsHostEntry(Linux.LDnsHeader head, Linux.LDnsReader reader) : base(head)
        {
            int byteCount = (Type == DnsRecordType.AAAA) ? 16 : 4;

            byte[] addrBytes = reader.Bytes(byteCount);

            Address = new IPAddress(addrBytes);
        }
Beispiel #5
0
        internal unsafe DnsTextEntry(Linux.LDnsHeader head, Linux.LDnsReader reader) : base(head)
        {
            var strings = new List <String>();

            byte *pDataEnd = reader.Current + head.DataLen;

            while (reader.Current < pDataEnd)
            {
                byte strLen = reader.Bytes(1)[0];
                if (strLen > 0)
                {
                    string s = reader.Text(strLen);
                    strings.Add(s);
                }
            }

            Text = strings.ToArray();
        }
Beispiel #6
0
        public unsafe Task <DnsEntry[]> TryResolve()
        {
            var records = new List <DnsEntry>();

            byte[] dataBuffer = new byte[1024];
            int    dataLen    = LinuxLib.res_query(Query, C_IN, (int)Type, dataBuffer, dataBuffer.Length);

            if (dataLen > 0)
            {
                GCHandle handle = GCHandle.Alloc(dataBuffer, GCHandleType.Pinned);

                try
                {
                    fixed(byte *pBuffer = dataBuffer)
                    {
                        var reader = new LDnsReader
                        {
                            Buffer  = pBuffer,
                            Current = pBuffer,
                            End     = pBuffer + dataLen
                        };

                        // Response Header
                        int queryId = reader.UInt16();  // Query Identifier (Read & Ignore)

                        byte[] hBits = reader.Bytes(2); // Header Bits & Flags (Read & Ignore)

                        int qdCount = reader.UInt16();  // Question Count (Use Below)
                        int anCount = reader.UInt16();  // Answer Count (Use Below)

                        int nsCount = reader.UInt16();  // NameServer Count (Read & Ignore)
                        int arCount = reader.UInt16();  // Resource Count (Read & Ignore)

                        // Question Section (read and ignore)
                        for (int q = 0; q < qdCount && reader.OK(); q++)
                        {
                            string qName  = reader.Name();
                            ushort qType  = reader.UInt16();
                            ushort qClass = reader.UInt16();
                        }

                        // Answers (the good stuff)
                        for (int a = 0; a < anCount && reader.OK(); a++)
                        {
                            var      ansHead  = new LDnsHeader(reader);
                            DnsEntry dnsEntry = DnsEntry.Create(ansHead, reader);

                            records.Add(dnsEntry);
                        }
                    }
                }
                finally
                {
                    handle.Free();
                }
            }

            _allRecords  = records.ToArray();
            _typeRecords = records.Where(r => r.Type == Type).ToArray();

            if (Type == DnsRecordType.SRV || Type == DnsRecordType.MX)    // sort
            {
                _typeRecords = _typeRecords.OrderBy(r => ((IOrderedDnsEntry)r).SortOrder).ToArray();
            }

            return(Task.FromResult(_typeRecords));
        }
Beispiel #7
0
 internal DnsPointerEntry(Linux.LDnsHeader head, Linux.LDnsReader reader) : base(head)
 {
     Target = reader.Name();
 }
Beispiel #8
0
 internal DnsMailExchangeEntry(Linux.LDnsHeader head, Linux.LDnsReader reader) : base(head)
 {
     // Read Order Matters
     Preference = reader.UInt16();
     Exchanger  = reader.Name();
 }