Example #1
0
        public void IpV4OptionTimedAddressTest()
        {
            IpV4OptionTimedAddress timedAddress1 = new IpV4OptionTimedAddress(new IpV4Address("1.2.3.4"), new IpV4TimeOfDay(new TimeSpan(1, 2, 3)));
            IpV4OptionTimedAddress timedAddress2 = new IpV4OptionTimedAddress(new IpV4Address("1.2.3.4"), new IpV4TimeOfDay(new TimeSpan(1, 2, 3)));

            Assert.AreEqual(timedAddress1, timedAddress2);
            Assert.IsTrue(timedAddress1 == timedAddress2);
            Assert.IsFalse(timedAddress1 != timedAddress2);
            Assert.IsFalse(timedAddress1.Equals(0));
        }
        internal static IpV4OptionTimestampAndAddress Read(IpV4OptionTimestampType timestampType, byte overflow, byte pointedIndex, byte[] buffer, ref int offset, int numValues)
        {
            if (numValues % 2 != 0)
                return null;

            IpV4OptionTimedAddress[] addressesAndTimestamps = new IpV4OptionTimedAddress[numValues / 2];
            for (int i = 0; i != numValues / 2; ++i)
            {
                addressesAndTimestamps[i] = new IpV4OptionTimedAddress(buffer.ReadIpV4Address(ref offset, Endianity.Big),
                                                                       buffer.ReadIpV4TimeOfDay(ref offset, Endianity.Big));
            }

            return new IpV4OptionTimestampAndAddress(timestampType, overflow, pointedIndex, addressesAndTimestamps);
        }
Example #3
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);
            }
        }
Example #4
0
        public void IpV4OptionTimedAddressTest()
        {
            IpV4OptionTimedAddress timedAddress1 = new IpV4OptionTimedAddress(new IpV4Address("1.2.3.4"), new IpV4TimeOfDay(new TimeSpan(1, 2, 3)));
            IpV4OptionTimedAddress timedAddress2 = new IpV4OptionTimedAddress(new IpV4Address("1.2.3.4"), new IpV4TimeOfDay(new TimeSpan(1, 2, 3)));

            Assert.AreEqual(timedAddress1, timedAddress2);
            Assert.IsTrue(timedAddress1 == timedAddress2);
            Assert.IsFalse(timedAddress1 != timedAddress2);
            Assert.IsFalse(timedAddress1.Equals(0));
        }
Example #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);
            }
        }