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));
        }
        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 override DnsResourceData CreateInstance(DnsDatagram dns, int offsetInDns, int length)
        {
            if (length < 18)
            {
                return((DnsResourceData)null);
            }
            DnsType      typeCovered = (DnsType)dns.ReadUShort(offsetInDns, Endianity.Big);
            DnsAlgorithm algorithm   = (DnsAlgorithm)dns[offsetInDns + 2];
            byte         labels      = dns[offsetInDns + 3];
            uint         originalTtl = dns.ReadUInt(offsetInDns + 4, Endianity.Big);
            uint         num1        = dns.ReadUInt(offsetInDns + 8, Endianity.Big);
            uint         num2        = dns.ReadUInt(offsetInDns + 12, Endianity.Big);
            ushort       keyTag      = dns.ReadUShort(offsetInDns + 16, Endianity.Big);

            offsetInDns += 18;
            length      -= 18;
            DnsDomainName domainName;
            int           numBytesRead;

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

            return((DnsResourceData) new DnsResourceDataSignature(typeCovered, algorithm, labels, originalTtl, (SerialNumber32)num1, (SerialNumber32)num2, keyTag, domainName, signature));
        }
        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));
        }
Esempio n. 6
0
        internal override DnsResourceData CreateInstance(DnsDatagram dns, int offsetInDns, int length)
        {
            if (length < 17)
            {
                return((DnsResourceData)null);
            }
            DnsDomainName domainName;
            int           numBytesRead;

            if (!DnsDomainName.TryParse(dns, offsetInDns, length - 16, out domainName, out numBytesRead))
            {
                return((DnsResourceData)null);
            }
            offsetInDns += numBytesRead;
            length      -= numBytesRead;
            if (length < 16)
            {
                return((DnsResourceData)null);
            }
            uint num1 = dns.ReadUInt(offsetInDns, Endianity.Big);
            uint num2 = dns.ReadUInt(offsetInDns + 4, Endianity.Big);
            DnsTransactionKeyMode mode  = (DnsTransactionKeyMode)dns.ReadUShort(offsetInDns + 8, Endianity.Big);
            DnsResponseCode       error = (DnsResponseCode)dns.ReadUShort(offsetInDns + 10, Endianity.Big);
            int length1 = (int)dns.ReadUShort(offsetInDns + 12, Endianity.Big);

            if (length < 16 + length1)
            {
                return((DnsResourceData)null);
            }
            DataSegment key  = dns.Subsegment(offsetInDns + 14, length1);
            int         num3 = 14 + length1;

            offsetInDns += num3;
            length      -= num3;
            int length2 = (int)dns.ReadUShort(offsetInDns, Endianity.Big);

            if (length != 2 + length2)
            {
                return((DnsResourceData)null);
            }
            DataSegment other = dns.Subsegment(offsetInDns + 2, length2);

            return((DnsResourceData) new DnsResourceDataTransactionKey(domainName, (SerialNumber32)num1, (SerialNumber32)num2, mode, error, key, other));
        }
Esempio n. 7
0
        internal override DnsResourceData CreateInstance(DnsDatagram dns, int offsetInDns, int length)
        {
            if (length < 17)
            {
                return((DnsResourceData)null);
            }
            DnsDomainName domainName;
            int           numBytesRead;

            if (!DnsDomainName.TryParse(dns, offsetInDns, length - 16, out domainName, out numBytesRead))
            {
                return((DnsResourceData)null);
            }
            offsetInDns += numBytesRead;
            length      -= numBytesRead;
            if (length < 16)
            {
                return((DnsResourceData)null);
            }
            UInt48 timeSigned = dns.ReadUInt48(offsetInDns, Endianity.Big);
            ushort fudge      = dns.ReadUShort(offsetInDns + 6, Endianity.Big);
            int    length1    = (int)dns.ReadUShort(offsetInDns + 8, Endianity.Big);

            if (length < 16 + length1)
            {
                return((DnsResourceData)null);
            }
            DataSegment messageAuthenticationCode = dns.Subsegment(offsetInDns + 10, length1);
            int         num = 10 + length1;

            offsetInDns += num;
            length      -= num;
            ushort          originalId = dns.ReadUShort(offsetInDns, Endianity.Big);
            DnsResponseCode error      = (DnsResponseCode)dns.ReadUShort(offsetInDns + 2, Endianity.Big);
            int             length2    = (int)dns.ReadUShort(offsetInDns + 4, Endianity.Big);

            if (length != 6 + length2)
            {
                return((DnsResourceData)null);
            }
            DataSegment other = dns.Subsegment(offsetInDns + 6, length2);

            return((DnsResourceData) new DnsResourceDataTransactionSignature(domainName, timeSigned, fudge, messageAuthenticationCode, originalId, error, other));
        }
Esempio n. 8
0
        internal static DnsResourceData Read(DnsDatagram dns, DnsType type, int offsetInDns, int length)
        {
            DnsResourceData prototype = DnsResourceData.TryGetPrototype(type);

            if (prototype != null)
            {
                return(prototype.CreateInstance(dns, offsetInDns, length));
            }
            return((DnsResourceData) new DnsResourceDataAnything(dns.Subsegment(offsetInDns, length)));
        }
        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 < 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 < 4)
            {
                return((DnsResourceData)null);
            }
            int length1 = (int)dns[offsetInDns];
            DnsPublicKeyAlgorithm publicKeyAlgorithm = (DnsPublicKeyAlgorithm)dns[offsetInDns + 1];
            int length2 = (int)dns.ReadUShort(offsetInDns + 2, Endianity.Big);

            if (length < 4 + length1 + length2)
            {
                return((DnsResourceData)null);
            }
            DataSegment hostIdentityTag = dns.Subsegment(offsetInDns + 4, length1);
            int         offset          = offsetInDns + 4 + length1;
            DataSegment publicKey       = dns.Subsegment(offset, length2);

            offsetInDns += 4 + length1 + length2;
            length      -= 4 + length1 + length2;
            List <DnsDomainName> list = new List <DnsDomainName>();

            while (length != 0)
            {
                DnsDomainName domainName;
                int           numBytesRead;
                if (!DnsDomainName.TryParse(dns, offsetInDns, length, out domainName, out numBytesRead))
                {
                    return((DnsResourceData)null);
                }
                list.Add(domainName);
                offsetInDns += numBytesRead;
                length      -= numBytesRead;
            }
            return((DnsResourceData) new DnsResourceDataHostIdentityProtocol(hostIdentityTag, publicKeyAlgorithm, publicKey, (IEnumerable <DnsDomainName>)list));
        }
Esempio n. 12
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)
        {
            if (length < 3)
            {
                return((DnsResourceData)null);
            }
            byte                  precedence  = dns[offsetInDns];
            DnsGatewayType        gatewayType = (DnsGatewayType)dns[offsetInDns + 1];
            DnsPublicKeyAlgorithm algorithm   = (DnsPublicKeyAlgorithm)dns[offsetInDns + 2];
            DnsGateway            instance    = DnsGateway.CreateInstance(gatewayType, dns, offsetInDns + 3, length - 3);

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

            return((DnsResourceData) new DnsResourceDataIpSecKey(precedence, instance, algorithm, publicKey));
        }
        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));
        }
Esempio n. 15
0
        internal override DnsResourceData CreateInstance(DnsDatagram dns, int offsetInDns, int length)
        {
            if (length < 8)
            {
                return((DnsResourceData)null);
            }
            ushort      order      = dns.ReadUShort(offsetInDns, Endianity.Big);
            ushort      preference = dns.ReadUShort(offsetInDns + 2, Endianity.Big);
            DataSegment data       = dns.Subsegment(offsetInDns + 4, length - 4);
            int         offset     = 0;
            DataSegment flags      = DnsResourceData.ReadString(data, ref offset);

            if (flags == null || !DnsResourceDataNamingAuthorityPointer.IsLegalFlags(flags))
            {
                return((DnsResourceData)null);
            }
            DataSegment services = DnsResourceData.ReadString(data, ref offset);

            if (services == null)
            {
                return((DnsResourceData)null);
            }
            DataSegment regularExpression = DnsResourceData.ReadString(data, ref offset);

            if (regularExpression == null)
            {
                return((DnsResourceData)null);
            }
            DnsDomainName domainName;
            int           numBytesRead;

            if (!DnsDomainName.TryParse(dns, offsetInDns + 4 + offset, length - 4 - offset, out domainName, out numBytesRead))
            {
                return((DnsResourceData)null);
            }
            if (4 + offset + numBytesRead != length)
            {
                return((DnsResourceData)null);
            }
            return((DnsResourceData) new DnsResourceDataNamingAuthorityPointer(order, preference, flags, services, regularExpression, domainName));
        }
Esempio n. 16
0
 private static bool TryReadLabels(DnsDatagram dns, int offsetInDns, out int numBytesRead, List <DataSegment> labels)
 {
     numBytesRead = 0;
     while (offsetInDns < dns.Length)
     {
         byte num = dns[offsetInDns];
         ++numBytesRead;
         if ((int)num > 63)
         {
             if (offsetInDns + 1 >= dns.Length)
             {
                 return(false);
             }
             int offsetInDns1 = (int)dns.ReadUShort(offsetInDns, Endianity.Big) & 16383;
             if (offsetInDns1 >= offsetInDns)
             {
                 return(false);
             }
             ++numBytesRead;
             int numBytesRead1;
             return(DnsDomainName.TryReadLabels(dns, offsetInDns1, out numBytesRead1, labels));
         }
         if ((int)num != 0)
         {
             ++offsetInDns;
             if (offsetInDns + (int)num >= dns.Length)
             {
                 return(false);
             }
             labels.Add(dns.Subsegment(offsetInDns, (int)num));
             numBytesRead += (int)num;
             offsetInDns  += (int)num;
         }
         if ((int)num == 0)
         {
             return(true);
         }
     }
     return(false);
 }
Esempio n. 17
0
        internal override DnsResourceData CreateInstance(DnsDatagram dns, int offsetInDns, int length)
        {
            DnsDomainName domainName;
            int           numBytesRead;

            if (!DnsDomainName.TryParse(dns, offsetInDns, length, out domainName, out numBytesRead))
            {
                return((DnsResourceData)null);
            }
            offsetInDns += numBytesRead;
            length      -= numBytesRead;
            if (length > 16)
            {
                return((DnsResourceData)null);
            }
            DataSegment typeBitmap = dns.Subsegment(offsetInDns, length);

            if (length != 0 && (int)typeBitmap.Last == 0)
            {
                return((DnsResourceData)null);
            }
            return((DnsResourceData) new DnsResourceDataNextDomain(domainName, typeBitmap));
        }
 internal override sealed DnsResourceData CreateInstance(DnsDatagram dns, int offsetInDns, int length)
 {
     return(this.CreateInstance(dns.Subsegment(offsetInDns, length)));
 }