Exemplo n.º 1
0
        private void GetResourceRecord(int i, ResourceRecordCollection Container)
        {
            //get resource (answer) name
            string ResourceName = GetName();

            //get resource type and class, usefull when using the ANY query
            //type: two octec field
            int   TypeID       = (data[position++] & byte.MaxValue) << 8 | data[position++] & byte.MaxValue;
            Types ResourceType = (Types)TypeID;

            //type: two octec field
            int     ClassID       = (data[position++] & byte.MaxValue) << 8 | data[position++] & byte.MaxValue;
            Classes ResourceClass = (Classes)ClassID;

            //ttl: unsigned integer
            int TTL_Seconds = (data[position++] & byte.MaxValue) << 24 | (data[position++] & byte.MaxValue) << 16 | (data[position++] & byte.MaxValue) << 8 | data[position++] & byte.MaxValue;

            //Get Resource Data Length
            int RDLength = (data[position++] & byte.MaxValue) << 8 | data[position++] & byte.MaxValue;

            //Parse Resource Data: 4 possible formats: A, Text, SOA and MX
            switch (ResourceType)
            {
            case Types.A:
                //Get IP Address Blocks
            {
                byte[] bs = new byte[] { (byte)(data[position++] & byte.MaxValue), (byte)(data[position++] & byte.MaxValue), (byte)(data[position++] & byte.MaxValue), (byte)(data[position++] & byte.MaxValue) };
                string ResourceAddress = String.Concat(new object[] { bs[0], ".", bs[1], ".", bs[2], ".", bs[3] });

                SupersocksR.Net.Dns.OpenDNS.Address rrA = new Address(ResourceName, ResourceType, ResourceClass, TTL_Seconds, ResourceAddress);
                Container.Add(rrA);
                break;
            }

            case Types.AAAA:
            {
                //Get IP Address Blocks
                ushort[] bs = new ushort[8];
                for (int j = 0; j < 8; ++j)
                {
                    bs[j] = (ushort)(((byte)(data[position + j * 2] & byte.MaxValue) << 8) | (byte)(data[position + j * 2 + 1] & byte.MaxValue));
                }
                position += 16;
                string ResourceAddress = String.Concat(new object[] {
                        Convert.ToString(bs[0], 16), ":",
                        Convert.ToString(bs[1], 16), ":",
                        Convert.ToString(bs[2], 16), ":",
                        Convert.ToString(bs[3], 16), ":",
                        Convert.ToString(bs[4], 16), ":",
                        Convert.ToString(bs[5], 16), ":",
                        Convert.ToString(bs[6], 16), ":",
                        Convert.ToString(bs[7], 16)
                    });

                SupersocksR.Net.Dns.OpenDNS.Address rrA = new Address(ResourceName, ResourceType, ResourceClass, TTL_Seconds, ResourceAddress);
                Container.Add(rrA);
                break;
            }

            case Types.SOA:
            {
                //Extract Text Fields
                string Server = GetName();
                string Email  = GetName();

                //32 bit fields
                long Serial  = (data[position++] & byte.MaxValue) << 24 | (data[position++] & byte.MaxValue) << 16 | (data[position++] & byte.MaxValue) << 8 | data[position++] & byte.MaxValue;
                long Refresh = (data[position++] & byte.MaxValue) << 24 | (data[position++] & byte.MaxValue) << 16 | (data[position++] & byte.MaxValue) << 8 | data[position++] & byte.MaxValue;
                long Retry   = (data[position++] & byte.MaxValue) << 24 | (data[position++] & byte.MaxValue) << 16 | (data[position++] & byte.MaxValue) << 8 | data[position++] & byte.MaxValue;
                long Expire  = (data[position++] & byte.MaxValue) << 24 | (data[position++] & byte.MaxValue) << 16 | (data[position++] & byte.MaxValue) << 8 | data[position++] & byte.MaxValue;
                long Minimum = (data[position++] & byte.MaxValue) << 24 | (data[position++] & byte.MaxValue) << 16 | (data[position++] & byte.MaxValue) << 8 | data[position++] & byte.MaxValue;

                SupersocksR.Net.Dns.OpenDNS.SOA rrSOA = new SOA(ResourceName, ResourceType, ResourceClass, TTL_Seconds, Server, Email, Serial, Refresh, Retry, Expire, Minimum);
                Container.Add(rrSOA);

                break;
            }

            case Types.CNAME:
            {
                string         ResourceDataText = GetName();
                ResourceRecord rrTXT            = new ResourceRecord(ResourceName, ResourceType,
                                                                     ResourceClass, TTL_Seconds, ResourceDataText);
                Container.Add(rrTXT);
                break;
            }

            case Types.MINFO:
            case Types.NS:
            case Types.PTR:
            case Types.TXT:
            {
                //Simplest RDATA format, just a text string, shared by many
                string         ResourceDataText = GetName();
                ResourceRecord rrTXT            = new ResourceRecord(ResourceName, ResourceType,
                                                                     ResourceClass, TTL_Seconds, ResourceDataText);
                Container.Add(rrTXT);
                break;
            }

            case Types.MX:
            {
                int    Rank     = (data[position++] << 8) | (data[position++] & byte.MaxValue);
                string Exchange = GetName();

                MX rrMX = new MX(ResourceName, ResourceType, ResourceClass, TTL_Seconds, Rank, Exchange);
                Container.Add(rrMX);

                break;
            }

            default:
                Trace.WriteLine("Resource type did not match: " + ResourceType.ToString(), "RUY QDNS");
                break;
            }
        }