Exemple #1
0
        internal override DnsResourceData CreateInstance(DnsDatagram dns, int offsetInDns, int length)
        {
            if (length < MinimumLength)
            {
                return(null);
            }

            byte prefixLength = dns[offsetInDns + Offset.PrefixLength];

            if (prefixLength > MaxPrefixLength)
            {
                return(null);
            }
            offsetInDns += ConstantPartLength;
            length      -= ConstantPartLength;

            int addressSuffixLength = CalculateAddressSuffixLength(prefixLength);

            if (length < addressSuffixLength)
            {
                return(null);
            }
            IpV6Address addressSuffix = new IpV6Address((UInt128)dns.ReadUnsignedBigInteger(offsetInDns, addressSuffixLength, Endianity.Big));

            offsetInDns += addressSuffixLength;
            length      -= addressSuffixLength;

            if (IsAddressSuffixTooSmall(prefixLength, addressSuffix) || IsAddressSuffixTooBig(prefixLength, addressSuffix))
            {
                return(null);
            }

            DnsDomainName prefixName;
            int           prefixNameLength;

            if (!DnsDomainName.TryParse(dns, offsetInDns, length, out prefixName, out prefixNameLength))
            {
                return(null);
            }
            if (prefixNameLength != length)
            {
                return(null);
            }

            return(new DnsResourceDataA6(prefixLength, addressSuffix, prefixName));
        }
Exemple #2
0
        private static bool TryReadLabels(DnsDatagram dns, int offsetInDns, out int numBytesRead, List <DataSegment> labels)
        {
            numBytesRead = 0;
            byte labelLength;

            do
            {
                if (offsetInDns >= dns.Length)
                {
                    return(false);  // Can't read label's length.
                }
                labelLength = dns[offsetInDns];
                ++numBytesRead;
                if (labelLength > MaxLabelLength)
                {
                    // Compression.
                    if (offsetInDns + 1 >= dns.Length)
                    {
                        return(false);  // Can't read compression pointer.
                    }
                    int newOffsetInDns = dns.ReadUShort(offsetInDns, Endianity.Big) & OffsetMask;
                    if (newOffsetInDns >= offsetInDns)
                    {
                        return(false);  // Can't handle pointers that are not back pointers.
                    }
                    ++numBytesRead;
                    int internalBytesRead;
                    return(TryReadLabels(dns, newOffsetInDns, out internalBytesRead, labels));
                }

                if (labelLength != 0)
                {
                    ++offsetInDns;
                    if (offsetInDns + labelLength >= dns.Length)
                    {
                        return(false);  // Can't read label.
                    }
                    labels.Add(dns.Subsegment(offsetInDns, labelLength));
                    numBytesRead += labelLength;
                    offsetInDns  += labelLength;
                }
            } while (labelLength != 0);

            return(true);
        }
        internal override DnsResourceData CreateInstance(DnsDatagram dns, int offsetInDns, int length)
        {
            int           numBytesRead;
            DnsDomainName domainName;

            if (!DnsDomainName.TryParse(dns, offsetInDns, length, out domainName, out numBytesRead))
            {
                return(null);
            }
            length -= numBytesRead;

            if (length != 0)
            {
                return(null);
            }

            return(new DnsResourceDataDomainName(domainName));
        }
        internal static List <DnsDomainName> ReadDomainNames(DnsDatagram dns, int offsetInDns, int length, int numExpected = 0)
        {
            List <DnsDomainName> domainNames = new List <DnsDomainName>(numExpected);

            while (length != 0)
            {
                DnsDomainName domainName;
                int           domainNameLength;
                if (!DnsDomainName.TryParse(dns, offsetInDns, length, out domainName, out domainNameLength))
                {
                    return(null);
                }
                offsetInDns += domainNameLength;
                length      -= domainNameLength;
                domainNames.Add(domainName);
            }

            return(domainNames);
        }
Exemple #5
0
        internal static bool TryParseBase(DnsDatagram dns, int offsetInDns,
                                          out DnsDomainName domainName, out DnsType type, out DnsClass dnsClass, out int numBytesRead)
        {
            type     = DnsType.Any;
            dnsClass = DnsClass.Any;
            if (!DnsDomainName.TryParse(dns, offsetInDns, dns.Length - offsetInDns, out domainName, out numBytesRead))
            {
                return(false);
            }

            if (offsetInDns + numBytesRead + MinimumLengthAfterDomainName > dns.Length)
            {
                return(false);
            }

            type          = (DnsType)dns.ReadUShort(offsetInDns + numBytesRead + OffsetAfterDomainName.Type, Endianity.Big);
            dnsClass      = (DnsClass)dns.ReadUShort(offsetInDns + numBytesRead + OffsetAfterDomainName.DnsClass, Endianity.Big);
            numBytesRead += MinimumLengthAfterDomainName;
            return(true);
        }
        internal override DnsResourceData CreateInstance(DnsDatagram dns, int offsetInDns, int length)
        {
            if (length < ConstPartLength)
            {
                return(null);
            }

            byte                  precedence  = dns[offsetInDns + Offset.Precedence];
            DnsGatewayType        gatewayType = (DnsGatewayType)dns[offsetInDns + Offset.GatewayType];
            DnsPublicKeyAlgorithm algorithm   = (DnsPublicKeyAlgorithm)dns[offsetInDns + Offset.Algorithm];
            DnsGateway            gateway     = DnsGateway.CreateInstance(gatewayType, dns, offsetInDns + Offset.Gateway, length - ConstPartLength);

            if (gateway == null)
            {
                return(null);
            }
            DataSegment publicKey = dns.Subsegment(offsetInDns + ConstPartLength + gateway.Length, length - ConstPartLength - gateway.Length);

            return(new DnsResourceDataIpSecKey(precedence, gateway, algorithm, publicKey));
        }
        internal override DnsResourceData CreateInstance(DnsDatagram dns, int offsetInDns, int length)
        {
            DnsDomainName nextDomainName;
            int           nextDomainNameLength;

            if (!DnsDomainName.TryParse(dns, offsetInDns, length, out nextDomainName, out nextDomainNameLength))
            {
                return(null);
            }
            offsetInDns += nextDomainNameLength;
            length      -= nextDomainNameLength;

            DnsTypeBitmaps typeBitmaps = DnsTypeBitmaps.CreateInstance(dns.Buffer, dns.StartOffset + offsetInDns, length);

            if (typeBitmaps == null)
            {
                return(null);
            }

            return(new DnsResourceDataNextDomainSecure(nextDomainName, typeBitmaps));
        }
        internal override DnsResourceData CreateInstance(DnsDatagram dns, int offsetInDns, int length)
        {
            if (length < ConstantPartLength)
            {
                return(null);
            }

            int hostIdentityTagLength = dns[offsetInDns + Offset.HostIdentityTagLength];
            DnsPublicKeyAlgorithm publicKeyAlgorithm = (DnsPublicKeyAlgorithm)dns[offsetInDns + Offset.PublicKeyAlgorithm];
            int publicKeyLength = dns.ReadUShort(offsetInDns + Offset.PublicKeyLength, Endianity.Big);

            if (length < ConstantPartLength + hostIdentityTagLength + publicKeyLength)
            {
                return(null);
            }
            DataSegment hostIdentityTag = dns.Subsegment(offsetInDns + Offset.HostIdentityTag, hostIdentityTagLength);
            int         publicKeyOffset = offsetInDns + ConstantPartLength + hostIdentityTagLength;
            DataSegment publicKey       = dns.Subsegment(publicKeyOffset, publicKeyLength);

            offsetInDns += ConstantPartLength + hostIdentityTagLength + publicKeyLength;
            length      -= ConstantPartLength + hostIdentityTagLength + publicKeyLength;

            List <DnsDomainName> rendezvousServers = new List <DnsDomainName>();

            while (length != 0)
            {
                DnsDomainName rendezvousServer;
                int           rendezvousServerLength;
                if (!DnsDomainName.TryParse(dns, offsetInDns, length, out rendezvousServer, out rendezvousServerLength))
                {
                    return(null);
                }
                rendezvousServers.Add(rendezvousServer);
                offsetInDns += rendezvousServerLength;
                length      -= rendezvousServerLength;
            }

            return(new DnsResourceDataHostIdentityProtocol(hostIdentityTag, publicKeyAlgorithm, publicKey, rendezvousServers));
        }
        internal static DnsDataResourceRecord Parse(DnsDatagram dns, int offsetInDns, out int numBytesRead)
        {
            DnsDomainName domainName;
            DnsType       type;
            DnsClass      dnsClass;

            if (!TryParseBase(dns, offsetInDns, out domainName, out type, out dnsClass, out numBytesRead))
            {
                return(null);
            }

            if (offsetInDns + numBytesRead + MinimumLengthAfterBase > dns.Length)
            {
                return(null);
            }

            int    ttl        = dns.ReadInt(offsetInDns + numBytesRead + OffsetAfterBase.Ttl, Endianity.Big);
            ushort dataLength = dns.ReadUShort(offsetInDns + numBytesRead + OffsetAfterBase.DataLength, Endianity.Big);

            numBytesRead += MinimumLengthAfterBase;
            if (offsetInDns + numBytesRead + dataLength > dns.Length)
            {
                return(null);
            }
            DnsResourceData data = DnsResourceData.Read(dns, type, offsetInDns + numBytesRead, dataLength);

            if (data == null)
            {
                return(null);
            }
            numBytesRead += dataLength;

            if (type == DnsType.Opt)
            {
                return(new DnsOptResourceRecord(domainName, dnsClass, ttl, data));
            }
            return(new DnsDataResourceRecord(domainName, type, dnsClass, ttl, data));
        }
        internal override DnsResourceData CreateInstance(DnsDatagram dns, int offsetInDns, int length)
        {
            if (length < ConstantPartLength)
            {
                return(null);
            }

            ushort preference = dns.ReadUShort(offsetInDns + Offset.Preference, Endianity.Big);

            offsetInDns += ConstantPartLength;
            length      -= ConstantPartLength;

            DnsDomainName map822;
            int           map822Length;

            if (!DnsDomainName.TryParse(dns, offsetInDns, length, out map822, out map822Length))
            {
                return(null);
            }
            offsetInDns += map822Length;
            length      -= map822Length;

            DnsDomainName mapX400;
            int           mapX400Length;

            if (!DnsDomainName.TryParse(dns, offsetInDns, length, out mapX400, out mapX400Length))
            {
                return(null);
            }
            length -= mapX400Length;

            if (length != 0)
            {
                return(null);
            }

            return(new DnsResourceDataX400Pointer(preference, map822, mapX400));
        }
Exemple #11
0
 internal abstract DnsResourceData CreateInstance(DnsDatagram dns, int offsetInDns, int length);
Exemple #12
0
 /// <summary>
 /// Writes the layer to the buffer.
 /// </summary>
 /// <param name="buffer">The buffer to write the layer to.</param>
 /// <param name="offset">The offset in the buffer to start writing the layer at.</param>
 protected override void Write(byte[] buffer, int offset)
 {
     DnsDatagram.Write(buffer, offset,
                       Id, IsResponse, OpCode, IsAuthoritativeAnswer, IsTruncated, IsRecursionDesired, IsRecursionAvailable, FutureUse, IsAuthenticData,
                       IsCheckingDisabled, ResponseCode, Queries, Answers, Authorities, Additionals, DomainNameCompressionMode);
 }