示例#1
0
        internal int GetLength(DnsDomainNameCompressionData compressionData, int offsetInDns)
        {
            int length = 0;

            for (int i = 0; i != LabelsCount; ++i)
            {
                ListSegment <DataSegment> labels = new ListSegment <DataSegment>(_labels, i);
                if (compressionData.IsAvailable(labels))
                {
                    return(length + sizeof(ushort));
                }
                compressionData.AddCompressionData(labels, offsetInDns + length);
                length += sizeof(byte) + labels[0].Length;
            }
            return(length + sizeof(byte));
        }
示例#2
0
        public bool TryGetOffset(ListSegment <DataSegment> labels, out int offsetInDns)
        {
            switch (DomainNameCompressionMode)
            {
            case DnsDomainNameCompressionMode.All:
                return(_data.TryGetValue(labels, out offsetInDns));

            case DnsDomainNameCompressionMode.Nothing:
                offsetInDns = 0;
                return(false);

            default:
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Invalid Domain Name Compression Mode {0}",
                                                                  DomainNameCompressionMode));
            }
        }
示例#3
0
        internal int Write(byte[] buffer, int dnsOffset, DnsDomainNameCompressionData compressionData, int offsetInDns)
        {
            int length = 0;

            for (int i = 0; i != LabelsCount; ++i)
            {
                ListSegment <DataSegment> labels = new ListSegment <DataSegment>(_labels, i);
                int pointerOffset;
                if (compressionData.TryGetOffset(labels, out pointerOffset))
                {
                    buffer.Write(dnsOffset + offsetInDns + length, (ushort)(CompressionMarker | (ushort)pointerOffset), Endianity.Big);
                    return(length + sizeof(ushort));
                }
                DataSegment currentLabel = labels[0];
                compressionData.AddCompressionData(labels, offsetInDns + length);
                buffer.Write(dnsOffset + offsetInDns + length, (byte)currentLabel.Length);
                length += sizeof(byte);
                currentLabel.Write(buffer, dnsOffset + offsetInDns + length);
                length += currentLabel.Length;
            }
            return(length + sizeof(byte));
        }
示例#4
0
        public void AddCompressionData(ListSegment <DataSegment> labels, int dnsOffset)
        {
            if (dnsOffset > DnsDomainName.OffsetMask)
            {
                return;
            }

            switch (DomainNameCompressionMode)
            {
            case DnsDomainNameCompressionMode.All:
                if (!_data.ContainsKey(labels))
                {
                    _data.Add(labels, dnsOffset);
                }
                return;

            case DnsDomainNameCompressionMode.Nothing:
                return;

            default:
                throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Invalid Domain Name Compression Mode {0}",
                                                                  DomainNameCompressionMode));
            }
        }
示例#5
0
        public bool IsAvailable(ListSegment <DataSegment> labels)
        {
            int offsetInDns;

            return(TryGetOffset(labels, out offsetInDns));
        }