Example #1
0
        internal static DnsGateway CreateInstance(DnsGatewayType gatewayType, DnsDatagram dns, int offsetInDns, int length)
        {
            switch (gatewayType)
            {
            case DnsGatewayType.None:
                return(None);

            case DnsGatewayType.IpV4:
                if (length < IpV4Address.SizeOf)
                {
                    return(null);
                }
                return(new DnsGatewayIpV4(dns.ReadIpV4Address(offsetInDns, Endianity.Big)));

            case DnsGatewayType.IpV6:
                if (length < IpV6Address.SizeOf)
                {
                    return(null);
                }
                return(new DnsGatewayIpV6(dns.ReadIpV6Address(offsetInDns, Endianity.Big)));

            case DnsGatewayType.DomainName:
                DnsDomainName domainName;
                int           numBytesRead;
                if (!DnsDomainName.TryParse(dns, offsetInDns, length, out domainName, out numBytesRead))
                {
                    return(null);
                }
                return(new DnsGatewayDomainName(domainName));

            default:
                return(null);
            }
        }
        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;

            if (length > MaxTypeBitmapLength)
            {
                return(null);
            }

            DataSegment typeBitmap = dns.Subsegment(offsetInDns, length);

            if (length != 0 && typeBitmap.Last == 0)
            {
                return(null);
            }

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

            ushort priortiy = dns.ReadUShort(offsetInDns + Offset.Priority, Endianity.Big);
            ushort weight   = dns.ReadUShort(offsetInDns + Offset.Weight, Endianity.Big);
            ushort port     = dns.ReadUShort(offsetInDns + Offset.Port, Endianity.Big);

            DnsDomainName target;
            int           targetLength;

            if (!DnsDomainName.TryParse(dns, offsetInDns + Offset.Target, length - ConstantPartLength, out target, out targetLength))
            {
                return(null);
            }

            if (ConstantPartLength + targetLength != length)
            {
                return(null);
            }

            return(new DnsResourceDataServerSelection(priortiy, weight, port, target));
        }
        internal override DnsResourceData CreateInstance(DnsDatagram dns, int offsetInDns, int length)
        {
            DnsDomainName mainNameServer;
            int           domainNameLength;

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

            DnsDomainName responsibleMailbox;

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

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

            uint serial     = dns.ReadUInt(offsetInDns + Offset.Serial, Endianity.Big);;
            uint refresh    = dns.ReadUInt(offsetInDns + Offset.Refresh, Endianity.Big);;
            uint retry      = dns.ReadUInt(offsetInDns + Offset.Retry, Endianity.Big);;
            uint expire     = dns.ReadUInt(offsetInDns + Offset.Expire, Endianity.Big);;
            uint minimumTtl = dns.ReadUInt(offsetInDns + Offset.MinimumTtl, Endianity.Big);;

            return(new DnsResourceDataStartOfAuthority(mainNameServer, responsibleMailbox, serial, refresh, retry, expire, minimumTtl));
        }
        internal override DnsResourceData CreateInstance(DnsDatagram dns, int offsetInDns, int length)
        {
            if (length < MinimumLength)
            {
                return(null);
            }

            DnsDomainName previous;
            int           previousLength;

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

            DnsDomainName next;
            int           nextLength;

            if (!DnsDomainName.TryParse(dns, offsetInDns, length, out next, out nextLength))
            {
                return(null);
            }

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

            return(new DnsResourceDataTrustAnchorLink(previous, next));
        }
        internal override DnsResourceData CreateInstance(DnsDatagram dns, int offsetInDns, int length)
        {
            if (length < ConstantPartLength)
            {
                return(null);
            }

            DnsType      typeCovered         = (DnsType)dns.ReadUShort(offsetInDns + Offset.TypeCovered, Endianity.Big);
            DnsAlgorithm algorithm           = (DnsAlgorithm)dns[offsetInDns + Offset.Algorithm];
            byte         labels              = dns[offsetInDns + Offset.Labels];
            uint         originalTtl         = dns.ReadUInt(offsetInDns + Offset.OriginalTtl, Endianity.Big);
            uint         signatureExpiration = dns.ReadUInt(offsetInDns + Offset.SignatureExpiration, Endianity.Big);
            uint         signatureInception  = dns.ReadUInt(offsetInDns + Offset.SignatureInception, Endianity.Big);
            ushort       keyTag              = dns.ReadUShort(offsetInDns + Offset.KeyTag, Endianity.Big);

            offsetInDns += ConstantPartLength;
            length      -= ConstantPartLength;

            DnsDomainName signersName;
            int           signersNameLength;

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

            DataSegment signature = dns.Subsegment(offsetInDns, length);

            return(new DnsResourceDataSignature(typeCovered, algorithm, labels, originalTtl, signatureExpiration, signatureInception, keyTag, signersName, signature));
        }
        internal static bool TryRead(out ushort value, out DnsDomainName domainName,
                                     DnsDatagram dns, int offsetInDns, int length)
        {
            if (length < ConstantPartLength)
            {
                value      = 0;
                domainName = null;
                return(false);
            }
            value   = dns.ReadUShort(offsetInDns + Offset.Value, Endianity.Big);
            length -= ConstantPartLength;

            int domainNameLength;

            if (!DnsDomainName.TryParse(dns, offsetInDns + Offset.DomainName, length, out domainName, out domainNameLength))
            {
                return(false);
            }
            length -= domainNameLength;

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

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

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

            DataSegment data       = dns.Subsegment(offsetInDns + ConstantPartLength, length - ConstantPartLength);
            int         dataOffset = 0;

            DataSegment flags = ReadString(data, ref dataOffset);

            if (flags == null || !IsLegalFlags(flags))
            {
                return(null);
            }

            DataSegment services = ReadString(data, ref dataOffset);

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

            DataSegment regexp = ReadString(data, ref dataOffset);

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

            DnsDomainName replacement;
            int           replacementLength;

            if (!DnsDomainName.TryParse(dns, offsetInDns + ConstantPartLength + dataOffset, length - ConstantPartLength - dataOffset,
                                        out replacement, out replacementLength))
            {
                return(null);
            }

            if (ConstantPartLength + dataOffset + replacementLength != length)
            {
                return(null);
            }

            return(new DnsResourceDataNamingAuthorityPointer(order, preference, flags, services, regexp, replacement));
        }
        internal override DnsResourceData CreateInstance(DnsDatagram dns, int offsetInDns, int length)
        {
            if (length < ConstantPartLength + DnsDomainName.RootLength)
            {
                return(null);
            }

            DnsDomainName algorithm;
            int           algorithmLength;

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

            if (length < ConstantPartLength)
            {
                return(null);
            }

            uint inception              = dns.ReadUInt(offsetInDns + OffsetAfterAlgorithm.Inception, Endianity.Big);
            uint expiration             = dns.ReadUInt(offsetInDns + OffsetAfterAlgorithm.Expiration, Endianity.Big);
            DnsTransactionKeyMode mode  = (DnsTransactionKeyMode)dns.ReadUShort(offsetInDns + OffsetAfterAlgorithm.Mode, Endianity.Big);
            DnsResponseCode       error = (DnsResponseCode)dns.ReadUShort(offsetInDns + OffsetAfterAlgorithm.Error, Endianity.Big);

            int keySize = dns.ReadUShort(offsetInDns + OffsetAfterAlgorithm.KeySize, Endianity.Big);

            if (length < ConstantPartLength + keySize)
            {
                return(null);
            }
            DataSegment key = dns.Subsegment(offsetInDns + OffsetAfterAlgorithm.KeyData, keySize);

            int totalReadAfterAlgorithm = OffsetAfterAlgorithm.KeyData + keySize;

            offsetInDns += totalReadAfterAlgorithm;
            length      -= totalReadAfterAlgorithm;
            int otherSize = dns.ReadUShort(offsetInDns, Endianity.Big);

            if (length != sizeof(ushort) + otherSize)
            {
                return(null);
            }
            DataSegment other = dns.Subsegment(offsetInDns + sizeof(ushort), otherSize);

            return(new DnsResourceDataTransactionKey(algorithm, inception, expiration, mode, error, key, other));
        }
        internal override DnsResourceData CreateInstance(DnsDatagram dns, int offsetInDns, int length)
        {
            if (length < ConstantPartLength + DnsDomainName.RootLength)
            {
                return(null);
            }

            DnsDomainName algorithm;
            int           algorithmLength;

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

            if (length < ConstantPartLength)
            {
                return(null);
            }

            UInt48 timeSigned = dns.ReadUInt48(offsetInDns + OffsetAfterAlgorithm.TimeSigned, Endianity.Big);
            ushort fudge      = dns.ReadUShort(offsetInDns + OffsetAfterAlgorithm.Fudge, Endianity.Big);
            int    messageAuthenticationCodeLength = dns.ReadUShort(offsetInDns + OffsetAfterAlgorithm.MessageAuthenticationCodeSize, Endianity.Big);

            if (length < ConstantPartLength + messageAuthenticationCodeLength)
            {
                return(null);
            }
            DataSegment messageAuthenticationCode = dns.Subsegment(offsetInDns + OffsetAfterAlgorithm.MessageAuthenticationCode, messageAuthenticationCodeLength);
            int         totalReadAfterAlgorithm   = OffsetAfterAlgorithm.MessageAuthenticationCode + messageAuthenticationCodeLength;

            offsetInDns += totalReadAfterAlgorithm;
            length      -= totalReadAfterAlgorithm;

            ushort          originalId  = dns.ReadUShort(offsetInDns + OffsetAfterMessageAuthenticationCode.OriginalId, Endianity.Big);
            DnsResponseCode error       = (DnsResponseCode)dns.ReadUShort(offsetInDns + OffsetAfterMessageAuthenticationCode.Error, Endianity.Big);
            int             otherLength = dns.ReadUShort(offsetInDns + OffsetAfterMessageAuthenticationCode.OtherLength, Endianity.Big);

            if (length != OffsetAfterMessageAuthenticationCode.OtherData + otherLength)
            {
                return(null);
            }
            DataSegment other = dns.Subsegment(offsetInDns + OffsetAfterMessageAuthenticationCode.OtherData, otherLength);

            return(new DnsResourceDataTransactionSignature(algorithm, timeSigned, fudge, messageAuthenticationCode, originalId, error, other));
        }
Example #11
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));
        }
        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);
        }
Example #14
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)
        {
            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 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));
        }