Esempio n. 1
0
        /// <summary>
        /// Will Compare two IResourceRecord
        /// </summary>
        /// <param name="first">Existing Entry in Cache</param>
        /// <param name="attempted">Entry to be inserted into Cache</param>
        /// <returns></returns>
        private bool Exists(IResourceRecord first, IResourceRecord attempted)
        {
            if (first.Name.TryTrim() != attempted.Name.TryTrim())
            {
                return(false);
            }
            if (first.Type != attempted.Type)
            {
                return(false);
            }
            if (first.Class != attempted.Class)
            {
                return(false);
            }
            if (first.Ttl != attempted.Ttl)
            {
                return(false);
            }
            if (first.RdLength != attempted.RdLength)
            {
                return(false);
            }
            if (!AreEqual(first.Rdata.ToArray(), attempted.Rdata.ToArray()))
            {
                return(false);
            }

            return(true);
        }
Esempio n. 2
0
        public void RequestWithAdditionalRecords()
        {
            byte[] content = Helper.ReadFixture("Request", "edns-test");

            Request request = Request.FromArray(content);

            Assert.Equal(11772, request.Id);
            Assert.Equal(true, request.RecursionDesired);
            Assert.Equal(39, request.Size);
            Assert.Equal(1, request.Questions.Count);
            Assert.Equal(1, request.AdditionalRecords.Count);

            Question question = request.Questions[0];

            Assert.Equal(RecordType.A, question.Type);
            Assert.Equal(RecordClass.IN, question.Class);
            Assert.Equal("google.com", question.Name.ToString());

            IResourceRecord record = request.AdditionalRecords[0];

            Assert.Equal("", record.Name.ToString());
            Assert.Equal(Helper.GetArray <byte>(), record.Data);
            Assert.Equal(RecordType.OPT, record.Type);
            Assert.Equal(4096, (int)record.Class);
            Assert.Equal(TimeSpan.FromSeconds(0), record.TimeToLive);
        }
Esempio n. 3
0
        private static string GetFileNameWithExtension(IResourceRecord resource)
        {
            var fileName = resource.Name;

            switch (resource.Type.ToUpperInvariant())
            {
            case "DNA":
            {
                fileName += ".dna";
                break;
            }

            case "ASSEMBLY":
            case "ASSEMBLY_LZMA":
            {
                fileName += ".dll";
                break;
            }

            case "TYPELIB":
            {
                fileName += ".tlb";
                break;
            }

            case "CONFIG":
            {
                fileName += ".config";
                break;
            }

            default:
            {
                var extension = Path.GetExtension(fileName);

                if (!string.IsNullOrWhiteSpace(extension))
                {
                    switch (extension.ToUpperInvariant())
                    {
                    case ".CS":
                    case ".VB":
                    case ".DNA":
                    case ".PNG":
                    case ".JPG":
                    case ".GIF":
                    {
                        fileName = Path.ChangeExtension(fileName, extension.ToLowerInvariant());
                        break;
                    }
                    }
                }

                break;
            }
            }

            return(fileName);
        }
        public ServiceResourceRecord(IResourceRecord record, byte[] message, int dataOffset) : base(record)
        {
            Head head = Marshalling.Struct.GetStruct <Head>(message, dataOffset, Head.SIZE);

            Priority = head.Priority;
            Weight   = head.Weight;
            Port     = head.Port;
            Target   = Domain.FromArray(message, dataOffset + Head.SIZE);
        }
Esempio n. 5
0
        public SerializableResourceRecord(IResourceRecord resourceRecord)
        {
            Name       = resourceRecord.Name;
            Type       = resourceRecord.Type;
            Class      = resourceRecord.Class;
            TimeToLive = resourceRecord.TimeToLive;
            Data       = resourceRecord.Data;

            CachedTime = DateTime.Now;
        }
Esempio n. 6
0
 internal virtual void AssertEquality(IResourceRecord expected, IResourceRecord actual)
 {
     Assert.AreEqual(expected.Name, actual.Name, "Should be equal");
     Assert.AreEqual(expected.Type, actual.Type, "Should be equal");
     Assert.AreEqual(expected.Class, actual.Class, "Should be equal");
     Assert.AreEqual(expected.Ttl, actual.Ttl, "Should be equal");
     Assert.AreEqual(expected.RdLength, actual.RdLength, "Should be equal");
     Assert.AreEqual(expected.Record.ToString(), actual.Record.ToString(), "Should be equal");
     AssertEquality(expected.Rdata, actual.Rdata);
     AssertEquality(expected.ToBytes(), actual.ToBytes());
 }
Esempio n. 7
0
        internal static bool IsExpired(this IResourceRecord value)
        {
            var expired = true;

            if (value != null)
            {
                expired = (value.TimeStamp.AddSeconds(value.Ttl).CompareTo(DateTime.Now) < 0);
            }

            return(expired);
        }
Esempio n. 8
0
        public string Reverse(IPAddress ip)
        {
            ClientResponse  response = Resolve(Domain.PointerName(ip), RecordType.PTR);
            IResourceRecord ptr      = response.AnswerRecords.FirstOrDefault(r => r.Type == RecordType.PTR);

            if (ptr == null)
            {
                throw new ResponseException(response, "No matching records");
            }

            return(((PointerResourceRecord)ptr).PointerDomainName.ToString());
        }
Esempio n. 9
0
        /// <summary>
        /// 反转查询
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <string> Reverse(IPAddress ip, CancellationToken cancellationToken = default(CancellationToken))
        {
            IResponse response = await Query(Domain.PointerName(ip), RecordType.PTR, cancellationToken);

            IResourceRecord ptr = response.AnswerRecords.FirstOrDefault(r => r.Type == RecordType.PTR);

            if (ptr == null)
            {
                throw new ResponseException(response, "No matching records");
            }

            return(((PointerResourceRecord)ptr).PointerDomainName.ToString());
        }
Esempio n. 10
0
        public StartOfAuthorityResourceRecord(IResourceRecord record, byte[] message, int dataOffset)
            : base(record)
        {
            MasterDomainName      = Domain.FromArray(message, dataOffset, out dataOffset);
            ResponsibleDomainName = Domain.FromArray(message, dataOffset, out dataOffset);

            Options tail = Marshalling.Struct.GetStruct <Options>(message, dataOffset, Options.SIZE);

            SerialNumber      = tail.SerialNumber;
            RefreshInterval   = tail.RefreshInterval;
            RetryInterval     = tail.RetryInterval;
            ExpireInterval    = tail.ExpireInterval;
            MinimumTimeToLive = tail.MinimumTimeToLive;
        }
        public MailExchangeResourceRecord(IResourceRecord record, byte[] message, int dataOffset)
            : base(record) {
            byte[] preference = new byte[MailExchangeResourceRecord.PREFERENCE_SIZE];
            Array.Copy(message, dataOffset, preference, 0, preference.Length);

            if (BitConverter.IsLittleEndian) {
                Array.Reverse(preference);
            }

            dataOffset += MailExchangeResourceRecord.PREFERENCE_SIZE;

            Preference = BitConverter.ToUInt16(preference, 0);
            ExchangeDomainName = Domain.FromArray(message, dataOffset);
        }
        public MailExchangeResourceRecord(IResourceRecord record, byte[] message, int dataOffset)
            : base(record)
        {
            byte[] preference = new byte[MailExchangeResourceRecord.PREFERENCE_SIZE];
            Array.Copy(message, dataOffset, preference, 0, preference.Length);

            if (BitConverter.IsLittleEndian) {
                Array.Reverse(preference);
            }

            dataOffset += MailExchangeResourceRecord.PREFERENCE_SIZE;

            Preference = BitConverter.ToUInt16(preference, 0);
            ExchangeDomainName = Domain.FromArray(message, dataOffset);
        }
Esempio n. 13
0
        public void BasicQuestionResponseWithEmptyHeader()
        {
            byte[]   content  = Helper.ReadFixture("Response", "empty-header_basic");
            Response response = Response.FromArray(content);

            Assert.Equal(0, response.Id);
            Assert.False(response.RecursionAvailable);
            Assert.Equal(62, response.Size);
            Assert.Equal(1, response.Questions.Count);
            Assert.Equal(1, response.AnswerRecords.Count);
            Assert.Equal(1, response.AuthorityRecords.Count);
            Assert.Equal(1, response.AdditionalRecords.Count);

            Question question = response.Questions[0];

            Assert.Equal(RecordType.A, question.Type);
            Assert.Equal(RecordClass.IN, question.Class);
            Assert.Equal("", question.Name.ToString());

            IResourceRecord record = response.AnswerRecords[0];

            Assert.Equal("", record.Name.ToString());
            Assert.Equal(Helper.GetArray <byte>(0, 0, 0, 0), record.Data);
            Assert.Equal(RecordType.A, record.Type);
            Assert.Equal(RecordClass.IN, record.Class);
            Assert.Equal(new TimeSpan(0), record.TimeToLive);

            record = response.AuthorityRecords[0];

            Assert.Equal("", record.Name.ToString());
            Assert.Equal(Helper.GetArray <byte>(0, 0, 0, 0), record.Data);
            Assert.Equal(RecordType.A, record.Type);
            Assert.Equal(RecordClass.IN, record.Class);
            Assert.Equal(new TimeSpan(0), record.TimeToLive);

            record = response.AdditionalRecords[0];

            Assert.Equal("", record.Name.ToString());
            Assert.Equal(Helper.GetArray <byte>(0, 0, 0, 0), record.Data);
            Assert.Equal(RecordType.A, record.Type);
            Assert.Equal(RecordClass.IN, record.Class);
            Assert.Equal(new TimeSpan(0), record.TimeToLive);
        }
Esempio n. 14
0
        public void RequestWithHeaderAndResourceRecords()
        {
            byte[]   content  = Helper.ReadFixture("Response", "id-ra_all");
            Response response = Response.FromArray(content);

            Assert.Equal(1, response.Id);
            Assert.True(response.RecursionAvailable);
            Assert.Equal(101, response.Size);
            Assert.Equal(1, response.Questions.Count);
            Assert.Equal(1, response.AnswerRecords.Count);
            Assert.Equal(1, response.AuthorityRecords.Count);
            Assert.Equal(1, response.AdditionalRecords.Count);

            Question question = response.Questions[0];

            Assert.Equal(RecordType.A, question.Type);
            Assert.Equal(RecordClass.IN, question.Class);
            Assert.Equal("www.google.com", question.Name.ToString());

            IResourceRecord record = response.AnswerRecords[0];

            Assert.Equal("www.google.com", record.Name.ToString());
            Assert.Equal(Helper.GetArray <byte>(3, 119, 119, 119, 0), record.Data);
            Assert.Equal(RecordType.CNAME, record.Type);
            Assert.Equal(RecordClass.IN, record.Class);
            Assert.Equal(TimeSpan.FromSeconds(1), record.TimeToLive);

            record = response.AuthorityRecords[0];

            Assert.Equal("dr.dk", record.Name.ToString());
            Assert.Equal(Helper.GetArray <byte>(1, 1, 1, 1), record.Data);
            Assert.Equal(RecordType.A, record.Type);
            Assert.Equal(RecordClass.ANY, record.Class);
            Assert.Equal(TimeSpan.FromSeconds(0), record.TimeToLive);

            record = response.AdditionalRecords[0];

            Assert.Equal("www", record.Name.ToString());
            Assert.Equal(Helper.GetArray <byte>(192, 12), record.Data);
            Assert.Equal(RecordType.CNAME, record.Type);
            Assert.Equal(RecordClass.ANY, record.Class);
            Assert.Equal(TimeSpan.FromSeconds(1), record.TimeToLive);
        }
Esempio n. 15
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ResourceRecord"/> class by using the specified
        /// pointer.
        /// </summary>
        /// <param name="pointer">The position in the byte array of the record.</param>
        internal ResourceRecord(RecordPointer pointer) {
            domain_ = pointer.GetDomain();
            dns_type_ = (DnsType)pointer.GetShort();
            dns_class_ = (DnsClass)pointer.GetShort();
            ttl_ = pointer.GetInt();

            // the next short is the record length, we only use it for unrecognised record types.
            int length = pointer.GetShort();
            switch(dns_type_) {
                case DnsType.MX:
                    record_ = new MXRecord(pointer);
                    break;

                default:
                    // move the pointer over this unrecognised record
                    pointer += length;
                    break;
            }
        }
Esempio n. 16
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ResourceRecord"/> class by using the specified
        /// pointer.
        /// </summary>
        /// <param name="pointer">The position in the byte array of the record.</param>
        internal ResourceRecord(RecordPointer pointer)
        {
            domain_    = pointer.GetDomain();
            dns_type_  = (DnsType)pointer.GetShort();
            dns_class_ = (DnsClass)pointer.GetShort();
            ttl_       = pointer.GetInt();

            // the next short is the record length, we only use it for unrecognised record types.
            int length = pointer.GetShort();

            switch (dns_type_)
            {
            case DnsType.MX:
                record_ = new MXRecord(pointer);
                break;

            default:
                // move the pointer over this unrecognised record
                pointer += length;
                break;
            }
        }
Esempio n. 17
0
        private void AddRecord(IResourceRecord record, Level level, int index = 1)
        {
            string[] labels = record.Name.Labels;

            if (labels.Length < index)
            {
                if (level.records == null)
                {
                    level.records = new List <IResourceRecord>();
                }
                level.records.Add(record);
                return;
            }

            string label = labels[labels.Length - index];

            if (label.Equals("*"))
            {
                level.wildcard = true;
                AddRecord(record, level, labels.Length + 1); // add the record here
                return;
            }

            if (level.entries == null)
            {
                level.entries = new Dictionary <string, Level>();
            }

            Level subLevel;

            if (!level.entries.TryGetValue(label, out subLevel))
            {
                subLevel = new Level();
                level.entries.Add(label, subLevel);
            }

            AddRecord(record, subLevel, index + 1);
        }
Esempio n. 18
0
        public async Task ServerLookup()
        {
            await Create(new IPAddressRequestResolver(), async server => {
                IRequest requestedRequest   = null;
                IRequest respondedRequest   = null;
                IResponse respondedResponse = null;
                Exception erroredException  = null;

                server.Requested += (serverRequest) => {
                    requestedRequest = serverRequest;
                };

                server.Responded += (serverRequest, serverResponse) => {
                    respondedRequest  = serverRequest;
                    respondedResponse = serverResponse;
                };

                server.Errored += (e) => {
                    erroredException = e;
                };

                IRequest clientRequest         = new Request();
                Question clientRequestQuestion = new Question(new Domain("google.com"), RecordType.A);

                clientRequest.Id = 1;
                clientRequest.Questions.Add(clientRequestQuestion);
                clientRequest.OperationCode = OperationCode.Query;

                IResponse clientResponse = await Resolve(clientRequest);

                Assert.Equal(1, clientResponse.Id);
                Assert.Equal(1, clientResponse.Questions.Count);
                Assert.Equal(1, clientResponse.AnswerRecords.Count);
                Assert.Equal(0, clientResponse.AuthorityRecords.Count);
                Assert.Equal(0, clientResponse.AdditionalRecords.Count);

                Question clientResponseQuestion = clientResponse.Questions[0];

                Assert.Equal(RecordType.A, clientResponseQuestion.Type);
                Assert.Equal("google.com", clientResponseQuestion.Name.ToString());

                IResourceRecord clientResponseRecord = clientResponse.AnswerRecords[0];

                Assert.Equal("google.com", clientResponseRecord.Name.ToString());
                Assert.Equal(Helper.GetArray <byte>(192, 168, 0, 1), clientResponseRecord.Data);
                Assert.Equal(RecordType.A, clientResponseRecord.Type);

                Assert.NotNull(requestedRequest);

                Assert.Equal(1, requestedRequest.Id);
                Assert.Equal(1, requestedRequest.Questions.Count);

                Question requestedRequestQuestion = requestedRequest.Questions[0];

                Assert.Equal(RecordType.A, requestedRequestQuestion.Type);
                Assert.Equal("google.com", requestedRequestQuestion.Name.ToString());

                Assert.Equal(requestedRequest, respondedRequest);

                Assert.NotNull(respondedResponse);

                Assert.Equal(1, respondedResponse.Id);
                Assert.Equal(1, respondedResponse.Questions.Count);
                Assert.Equal(1, respondedResponse.AnswerRecords.Count);
                Assert.Equal(0, respondedResponse.AuthorityRecords.Count);
                Assert.Equal(0, respondedResponse.AdditionalRecords.Count);

                Question respondedResponseQuestion = respondedResponse.Questions[0];

                Assert.Equal(RecordType.A, respondedResponseQuestion.Type);
                Assert.Equal("google.com", respondedResponseQuestion.Name.ToString());

                IResourceRecord respondedResponseRecord = respondedResponse.AnswerRecords[0];

                Assert.Equal("google.com", respondedResponseRecord.Name.ToString());
                Assert.Equal(Helper.GetArray <byte>(192, 168, 0, 1), respondedResponseRecord.Data);
                Assert.Equal(RecordType.A, respondedResponseRecord.Type);

                Assert.Null(erroredException);
            });
        }
Esempio n. 19
0
 public IPAddressResourceRecord(IResourceRecord record) : base(record)
 {
     IPAddress = new IPAddress(Data);
 }
Esempio n. 20
0
 public TextResourceRecord(IResourceRecord record) :
     base(record)
 {
     TextData = CharacterString.GetAllFromArray(Data, 0);
 }
Esempio n. 21
0
 public BaseResourceRecord(IResourceRecord record)
 {
     _Record = record;
 }
Esempio n. 22
0
 public static bool IsLzmaCompressed(this IResourceRecord resource)
 {
     return(resource.Type.EndsWith("_LZMA", StringComparison.InvariantCultureIgnoreCase));
 }
Esempio n. 23
0
 public TextResourceRecord(IResourceRecord record, byte[] message, int dataOffset) :
     base(record)
 {
     TextData = CharacterString.GetAllFromArray(message, dataOffset);
 }
 public IPAddressResourceRecord(IResourceRecord record)
     : base(record)
 {
     IPAddress = new IPAddress(Data);
 }
Esempio n. 25
0
 public void Remove(IResourceRecord entry)
 {
     entries.Remove(entry);
 }
Esempio n. 26
0
 public SrvResourceRecord(IResourceRecord record) : base(record)
 {
     //parse record
     Service = Name;
 }
 public CanonicalNameResourceRecord(IResourceRecord record, byte[] message, int dataOffset)
     : base(record)
 {
     CanonicalDomainName = Domain.FromArray(message, dataOffset);
 }
Esempio n. 28
0
 public void Add(IResourceRecord entry)
 {
     entries.Add(entry);
 }
Esempio n. 29
0
 public BaseResourceRecord(IResourceRecord record)
 {
     this.record = record;
 }
 public CanonicalNameResourceRecord(IResourceRecord record, byte[] message, int dataOffset)
     : base(record)
 {
     CanonicalDomainName = Domain.FromArray(message, dataOffset);
 }
 public NameServerResourceRecord(IResourceRecord record, byte[] message, int dataOffset)
     : base(record)
 {
     NSDomainName = Domain.FromArray(message, dataOffset);
 }
Esempio n. 32
0
 public AbstractResourceRecord(IResourceRecord record)
 {
     this.record = record;
 }
Esempio n. 33
0
 public PointerResourceRecord(IResourceRecord record, byte[] message, int dataOffset)
     : base(record)
 {
     PointerDomainName = Domain.FromArray(message, dataOffset);
 }
Esempio n. 34
0
        private IList <IResourceRecord> WhenLoad_AndStreamContainsEntry_ThenEntryIsPopulated(IResourceRecord testResourceRecord, Question question)
        {
            // Arrange.
            using (var stream = new MemoryStream())
            {
                var masterFile = new DnsSeedMasterFile();

                IList <IResourceRecord> testResourceRecords = new List <IResourceRecord>()
                {
                    testResourceRecord
                };

                JsonSerializer serializer = this.CreateSerializer();

                using (var streamWriter = new StreamWriter(stream))
                {
                    using (var jsonTextWriter = new JsonTextWriter(streamWriter))
                    {
                        serializer.Serialize(jsonTextWriter, testResourceRecords);

                        jsonTextWriter.Flush();
                        stream.Seek(0, SeekOrigin.Begin);

                        // Act.
                        masterFile.Load(stream);
                    }
                }

                // Assert.
                return(masterFile.Get(question));
            }
        }
Esempio n. 35
0
 public void Add(IResourceRecord entry)
 {
     entries.Add(entry);
 }
Esempio n. 36
0
 public void Add(IResourceRecord entry)
 {
     AddRecord(entry, TopLevel);
 }