Example #1
0
        public UInt32 UInt32()
        {
            uint netValue  = ((uint *)Current)[0];
            uint hostValue = LinuxLib.ntohl(netValue);

            //byte* t_cp = (byte*)(Current);
            //uint s = (uint)(((uint)t_cp[0] << 24) | ((uint)t_cp[1] << 16) | ((uint)t_cp[2] << 8) | ((uint)t_cp[3]));

            Current += sizeof(uint);

            return(hostValue);
        }
Example #2
0
        public UInt16 UInt16()
        {
            ushort netValue  = ((ushort *)Current)[0];
            ushort hostValue = LinuxLib.ntohs(netValue);

            //byte* t_cp = (byte*)(Current);
            //ushort s = (ushort)(((ushort)t_cp[0] << 8) | ((ushort)t_cp[1]));

            Current += sizeof(ushort);

            return(hostValue);
        }
Example #3
0
        public string Name()
        {
            byte[] stringBuffer = new byte[256];
            int    len          = LinuxLib.dn_expand(Buffer, End, Current, stringBuffer, stringBuffer.Length);

            if (len < 0)
            {
                return(null);
            }

            Current += len;

            fixed(byte *pStrData = stringBuffer)
            return(new String((sbyte *)pStrData));
        }
Example #4
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));
        }