Ejemplo n.º 1
0
        /// <summary>
        /// Create the security option from the different security field values.
        /// </summary>
        /// <param name="classificationLevel">
        /// This field specifies the (U.S.) classification level at which the datagram must be protected.
        /// The information in the datagram must be protected at this level.
        /// </param>
        /// <param name="protectionAuthorities">
        /// This field identifies the National Access Programs or Special Access Programs
        /// which specify protection rules for transmission and processing of the information contained in the datagram.
        /// </param>
        /// <param name="length">
        /// The number of bytes this option will take.
        /// </param>
        public IpV4OptionBasicSecurity(IpV4OptionSecurityClassificationLevel classificationLevel, IpV4OptionSecurityProtectionAuthorities protectionAuthorities, byte length)
            : base(IpV4OptionType.BasicSecurity)
        {
            if (length < OptionMinimumLength)
            {
                throw new ArgumentOutOfRangeException("length", length, "Minimum option length is " + OptionMinimumLength);
            }

            if (length == OptionMinimumLength && protectionAuthorities != IpV4OptionSecurityProtectionAuthorities.None)
            {
                throw new ArgumentException("Can't have a protection authority without minimum of " + (OptionValueMinimumLength + 1) + " length",
                                            "protectionAuthorities");
            }

            if (classificationLevel != IpV4OptionSecurityClassificationLevel.Confidential &&
                classificationLevel != IpV4OptionSecurityClassificationLevel.Secret &&
                classificationLevel != IpV4OptionSecurityClassificationLevel.TopSecret &&
                classificationLevel != IpV4OptionSecurityClassificationLevel.Unclassified)
            {
                throw new ArgumentException("Invalid classification level " + classificationLevel);
            }

            _classificationLevel   = classificationLevel;
            _protectionAuthorities = protectionAuthorities;
            _length = length;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Tries to read the option from a buffer starting from the option value (after the type and length).
        /// </summary>
        /// <param name="buffer">The buffer to read the option from.</param>
        /// <param name="offset">The offset to the first byte to read the buffer. Will be incremented by the number of bytes read.</param>
        /// <param name="valueLength">The number of bytes the option value should take according to the length field that was already read.</param>
        /// <returns>On success - the complex option read. On failure - null.</returns>
        Option IOptionComplexFactory.CreateInstance(byte[] buffer, ref int offset, byte valueLength)
        {
//            if (buffer == null)
//                throw new ArgumentNullException("buffer");

            if (valueLength < OptionValueMinimumLength)
            {
                return(null);
            }

            // Classification level
            IpV4OptionSecurityClassificationLevel classificationLevel = (IpV4OptionSecurityClassificationLevel)buffer[offset++];

            if (classificationLevel != IpV4OptionSecurityClassificationLevel.Confidential &&
                classificationLevel != IpV4OptionSecurityClassificationLevel.Secret &&
                classificationLevel != IpV4OptionSecurityClassificationLevel.TopSecret &&
                classificationLevel != IpV4OptionSecurityClassificationLevel.Unclassified)
            {
                return(null);
            }

            // Protection authorities
            int protectionAuthoritiesLength = valueLength - OptionValueMinimumLength;
            IpV4OptionSecurityProtectionAuthorities protectionAuthorities = IpV4OptionSecurityProtectionAuthorities.None;

            if (protectionAuthoritiesLength > 0)
            {
                for (int i = 0; i < protectionAuthoritiesLength - 1; ++i)
                {
                    if ((buffer[offset + i] & 0x01) == 0)
                    {
                        return(null);
                    }
                }
                if ((buffer[offset + protectionAuthoritiesLength - 1] & 0x01) != 0)
                {
                    return(null);
                }

                protectionAuthorities = (IpV4OptionSecurityProtectionAuthorities)(buffer[offset] & 0xFE);
            }
            offset += protectionAuthoritiesLength;

            return(new IpV4OptionBasicSecurity(classificationLevel, protectionAuthorities, (byte)(OptionMinimumLength + protectionAuthoritiesLength)));
        }
Ejemplo n.º 3
0
 public IpV4OptionBasicSecurity(IpV4OptionSecurityClassificationLevel classificationLevel, IpV4OptionSecurityProtectionAuthorities protectionAuthorities, byte length)
     : base(IpV4OptionType.BasicSecurity)
 {
     if ((int)length < 3)
     {
         throw new ArgumentOutOfRangeException("length", (object)length, "Minimum option length is " + (object)3);
     }
     if ((int)length == 3 && protectionAuthorities != IpV4OptionSecurityProtectionAuthorities.None)
     {
         throw new ArgumentException("Can't have a protection authority without minimum of " + (object)2 + " length", "protectionAuthorities");
     }
     if (classificationLevel != IpV4OptionSecurityClassificationLevel.Confidential && classificationLevel != IpV4OptionSecurityClassificationLevel.Secret && (classificationLevel != IpV4OptionSecurityClassificationLevel.TopSecret && classificationLevel != IpV4OptionSecurityClassificationLevel.Unclassified))
     {
         throw new ArgumentException("Invalid classification level " + (object)classificationLevel);
     }
     this._classificationLevel   = classificationLevel;
     this._protectionAuthorities = protectionAuthorities;
     this._length = length;
 }
Ejemplo n.º 4
0
        Option IOptionComplexFactory.CreateInstance(byte[] buffer, ref int offset, byte valueLength)
        {
            if ((int)valueLength < 1)
            {
                return((Option)null);
            }
            IpV4OptionSecurityClassificationLevel classificationLevel = (IpV4OptionSecurityClassificationLevel)buffer[offset++];

            switch (classificationLevel)
            {
            case IpV4OptionSecurityClassificationLevel.Confidential:
            case IpV4OptionSecurityClassificationLevel.Secret:
            case IpV4OptionSecurityClassificationLevel.TopSecret:
            case IpV4OptionSecurityClassificationLevel.Unclassified:
                int num = (int)valueLength - 1;
                IpV4OptionSecurityProtectionAuthorities protectionAuthorities = IpV4OptionSecurityProtectionAuthorities.None;
                if (num > 0)
                {
                    for (int index = 0; index < num - 1; ++index)
                    {
                        if (((int)buffer[offset + index] & 1) == 0)
                        {
                            return((Option)null);
                        }
                    }
                    if (((int)buffer[offset + num - 1] & 1) != 0)
                    {
                        return((Option)null);
                    }
                    protectionAuthorities = (IpV4OptionSecurityProtectionAuthorities)((uint)buffer[offset] & 254U);
                }
                offset += num;
                return((Option) new IpV4OptionBasicSecurity(classificationLevel, protectionAuthorities, (byte)(3 + num)));

            default:
                return((Option)null);
            }
        }
Ejemplo n.º 5
0
        public static IpV4Option NextIpV4Option(this Random random, int maximumOptionLength)
        {
            if (maximumOptionLength == 0)
            {
                throw new ArgumentOutOfRangeException("maximumOptionLength", maximumOptionLength, "option length must be positive");
            }

            if (maximumOptionLength >= IpV4OptionUnknown.OptionMinimumLength && random.Next(100) > 90)
            {
                return(random.NextIpV4OptionUnknown(maximumOptionLength));
            }

            // TODO: Support MTU Probe, MTU Reply and CommercialSecurity.
            List <IpV4OptionType> impossibleOptionTypes = new List <IpV4OptionType>
            {
                IpV4OptionType.MaximumTransmissionUnitProbe,
                IpV4OptionType.MaximumTransmissionUnitReply,
                IpV4OptionType.CommercialSecurity,
            };

            if (maximumOptionLength < IpV4OptionBasicSecurity.OptionMinimumLength)
            {
                impossibleOptionTypes.Add(IpV4OptionType.BasicSecurity);
            }
            if (maximumOptionLength < IpV4OptionRoute.OptionMinimumLength)
            {
                impossibleOptionTypes.Add(IpV4OptionType.LooseSourceRouting);
                impossibleOptionTypes.Add(IpV4OptionType.StrictSourceRouting);
                impossibleOptionTypes.Add(IpV4OptionType.RecordRoute);
            }
            if (maximumOptionLength < IpV4OptionStreamIdentifier.OptionLength)
            {
                impossibleOptionTypes.Add(IpV4OptionType.StreamIdentifier);
            }
            if (maximumOptionLength < IpV4OptionTimestamp.OptionMinimumLength)
            {
                impossibleOptionTypes.Add(IpV4OptionType.InternetTimestamp);
            }
            if (maximumOptionLength < IpV4OptionTraceRoute.OptionLength)
            {
                impossibleOptionTypes.Add(IpV4OptionType.TraceRoute);
            }
            if (maximumOptionLength < IpV4OptionQuickStart.OptionLength)
            {
                impossibleOptionTypes.Add(IpV4OptionType.QuickStart);
            }

            IpV4OptionType optionType = random.NextEnum <IpV4OptionType>(impossibleOptionTypes);

            switch (optionType)
            {
            case IpV4OptionType.EndOfOptionList:
                return(IpV4Option.End);

            case IpV4OptionType.NoOperation:
                return(IpV4Option.Nop);

            case IpV4OptionType.BasicSecurity:
                IpV4OptionSecurityProtectionAuthorities protectionAuthorities = IpV4OptionSecurityProtectionAuthorities.None;
                int protectionAuthorityLength = random.Next(maximumOptionLength - IpV4OptionBasicSecurity.OptionMinimumLength);
                if (protectionAuthorityLength > 0)
                {
                    protectionAuthorities = random.NextEnum <IpV4OptionSecurityProtectionAuthorities>();
                }

                return(new IpV4OptionBasicSecurity(random.NextEnum(IpV4OptionSecurityClassificationLevel.None),
                                                   protectionAuthorities,
                                                   (byte)(IpV4OptionBasicSecurity.OptionMinimumLength + protectionAuthorityLength)));

            case IpV4OptionType.LooseSourceRouting:
            case IpV4OptionType.StrictSourceRouting:
            case IpV4OptionType.RecordRoute:
                int           numAddresses = random.Next((maximumOptionLength - IpV4OptionRoute.OptionMinimumLength) / 4 + 1);
                IpV4Address[] addresses    = random.NextIpV4Addresses(numAddresses);

                byte pointedAddressIndex;
                if (random.NextBool())
                {
                    pointedAddressIndex = random.NextByte(IpV4OptionRoute.PointedAddressIndexMaxValue + 1);
                }
                else
                {
                    pointedAddressIndex = random.NextByte(10);
                }

                switch (optionType)
                {
                case IpV4OptionType.LooseSourceRouting:
                    return(new IpV4OptionLooseSourceRouting(addresses, pointedAddressIndex));

                case IpV4OptionType.StrictSourceRouting:
                    return(new IpV4OptionStrictSourceRouting(addresses, pointedAddressIndex));

                case IpV4OptionType.RecordRoute:
                    return(new IpV4OptionRecordRoute(pointedAddressIndex, addresses));

                default:
                    throw new InvalidOperationException("optionType = " + optionType);
                }

            case IpV4OptionType.StreamIdentifier:
                return(new IpV4OptionStreamIdentifier(random.NextUShort()));

            case IpV4OptionType.InternetTimestamp:
                IpV4OptionTimestampType timestampType = random.NextEnum <IpV4OptionTimestampType>();
                byte overflow = random.NextByte(IpV4OptionTimestamp.OverflowMaxValue + 1);
                byte pointedIndex;
                if (random.NextBool())
                {
                    pointedIndex = random.NextByte(IpV4OptionTimestamp.PointedIndexMaxValue + 1);
                }
                else
                {
                    pointedIndex = random.NextByte(10);
                }

                switch (timestampType)
                {
                case IpV4OptionTimestampType.TimestampOnly:
                    int             numTimestamps = random.Next((maximumOptionLength - IpV4OptionTimestamp.OptionMinimumLength) / 4 + 1);
                    IpV4TimeOfDay[] timestamps    = ((Func <IpV4TimeOfDay>)random.NextIpV4TimeOfDay).GenerateArray(numTimestamps);
                    return(new IpV4OptionTimestampOnly(overflow, pointedIndex, timestamps));

                case IpV4OptionTimestampType.AddressAndTimestamp:
                case IpV4OptionTimestampType.AddressPrespecified:
                    int numPairs = random.Next((maximumOptionLength - IpV4OptionTimestamp.OptionMinimumLength) / 8 + 1);
                    IpV4OptionTimedAddress[] pairs = new IpV4OptionTimedAddress[numPairs];
                    for (int i = 0; i != numPairs; ++i)
                    {
                        pairs[i] = new IpV4OptionTimedAddress(random.NextIpV4Address(), random.NextIpV4TimeOfDay());
                    }

                    return(new IpV4OptionTimestampAndAddress(timestampType, overflow, pointedIndex, pairs));

                default:
                    throw new InvalidOperationException("timestampType = " + timestampType);
                }

            case IpV4OptionType.TraceRoute:
                return(new IpV4OptionTraceRoute(random.NextUShort(), random.NextUShort(), random.NextUShort(), random.NextIpV4Address()));

            case IpV4OptionType.RouterAlert:
                return(new IpV4OptionRouterAlert(random.NextUShort()));

            case IpV4OptionType.QuickStart:
                return(new IpV4OptionQuickStart(random.NextEnum <IpV4OptionQuickStartFunction>(),
                                                random.NextByte(IpV4OptionQuickStart.RateMaximumValue + 1),
                                                random.NextByte(),
                                                random.NextUInt() & 0x3FFFFFFF));

            default:
                throw new InvalidOperationException("optionType = " + optionType);
            }
        }