private static void CreateRandomIpPayload(Random random, Layer ipLayer, List<ILayer> layers)
        {
            IpV6Layer ipV6Layer = ipLayer as IpV6Layer;
            if (ipV6Layer != null)
            {
                var headers = ipV6Layer.ExtensionHeaders.Headers;
                if (headers.Any() && headers.Last().Protocol == IpV4Protocol.EncapsulatingSecurityPayload)
                    return;
            }

            if (ipV6Layer != null ? ipV6Layer.LastNextHeader != null : random.NextBool(20))
            {
                // Finish with payload.
                PayloadLayer payloadLayer = random.NextPayloadLayer(random.Next(100));
                layers.Add(payloadLayer);
                return;
            }

            IpV4Layer ipV4Layer = ipLayer as IpV4Layer;
            if (ipV4Layer != null)
            {
                ipV4Layer.Protocol = null;
                if (random.NextBool())
                    ipV4Layer.Fragmentation = IpV4Fragmentation.None;
            }

            switch (random.Next(0, 11))
            {
                case 0: // IpV4.
                case 1:
                    IpV4Layer innerIpV4Layer = random.NextIpV4Layer();
                    layers.Add(innerIpV4Layer);
                    CreateRandomIpPayload(random, innerIpV4Layer, layers);
                    return;

                case 2: // IpV6.
                case 3:
                    IpV6Layer innerIpV6Layer = random.NextIpV6Layer(random.NextBool(20));
                    layers.Add(innerIpV6Layer);
                    CreateRandomIpPayload(random, innerIpV6Layer, layers);
                    return;

                case 4: // Igmp.
                    layers.Add(random.NextIgmpLayer());
                    return;

                case 5: // Icmp.
                    IcmpLayer icmpLayer = random.NextIcmpLayer();
                    layers.Add(icmpLayer);
                    layers.AddRange(random.NextIcmpPayloadLayers(icmpLayer));
                    return;

                case 6: // Gre.
                    GreLayer greLayer = random.NextGreLayer();
                    layers.Add(greLayer);
                    CreateRandomEthernetPayload(random, greLayer, layers);
                    return;
                    
                case 7: // Udp.
                case 8:
                    UdpLayer udpLayer = random.NextUdpLayer();
                    layers.Add(udpLayer);
                    CreateRandomUdpPayload(random, udpLayer, layers);
                    return;

                case 9: // Tcp.
                case 10:
                    TcpLayer tcpLayer = random.NextTcpLayer();
                    layers.Add(tcpLayer);
                    CreateRandomTcpPayload(random, tcpLayer, layers);
                    return;

                default:
                    throw new InvalidOperationException("Invalid value.");
            }
        }
        private static void CreateRandomIpV4Payload(Random random, IpV4Layer ipV4Layer, List<ILayer> layers)
        {
            if (random.NextBool(20))
            {
                // Finish with payload.
                PayloadLayer payloadLayer = random.NextPayloadLayer(random.Next(100));
                layers.Add(payloadLayer);
                return;
            }

            ipV4Layer.Protocol = null;
            if (random.NextBool())
                ipV4Layer.Fragmentation = IpV4Fragmentation.None;

            switch (random.Next(0, 9))
            {
                case 0: // IpV4.
                case 1:
                    IpV4Layer innerIpV4Layer = random.NextIpV4Layer();
                    layers.Add(innerIpV4Layer);
                    CreateRandomIpV4Payload(random, innerIpV4Layer, layers);
                    return;

                case 2: // Igmp.
                    layers.Add(random.NextIgmpLayer());
                    return;

                case 3: // Icmp.
                    IcmpLayer icmpLayer = random.NextIcmpLayer();
                    layers.Add(icmpLayer);
                    layers.AddRange(random.NextIcmpPayloadLayers(icmpLayer));
                    return;

                case 4: // Gre.
                    GreLayer greLayer = random.NextGreLayer();
                    layers.Add(greLayer);
                    CreateRandomEthernetPayload(random, greLayer, layers);
                    return;
                    
                case 5: // Udp.
                case 6:
                    UdpLayer udpLayer = random.NextUdpLayer();
                    layers.Add(udpLayer);
                    CreateRandomUdpPayload(random, udpLayer, layers);
                    return;

                case 7: // Tcp.
                case 8:
                    TcpLayer tcpLayer = random.NextTcpLayer();
                    layers.Add(tcpLayer);
                    CreateRandomTcpPayload(random, tcpLayer, layers);
                    return;

                default:
                    throw new InvalidOperationException("Invalid value.");
            }
        }
Пример #3
0
        public void RandomIgmpTest()
        {
            EthernetLayer ethernetLayer = new EthernetLayer
                                              {
                                                  Source = new MacAddress("00:01:02:03:04:05"),
                                                  Destination = new MacAddress("A0:A1:A2:A3:A4:A5")
                                              };

            Random random = new Random();

            IpV4Layer ipV4Layer = random.NextIpV4Layer(null);
            ipV4Layer.HeaderChecksum = null;

            for (int i = 0; i != 200; ++i)
            {
                IgmpLayer igmpLayer = random.NextIgmpLayer();

                Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, ipV4Layer, igmpLayer);

                Assert.IsTrue(packet.IsValid, "IsValid");

                // Ethernet
                ethernetLayer.EtherType = EthernetType.IpV4;
                Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer(), "Ethernet Layer");

                // IPv4
                ipV4Layer.Protocol = IpV4Protocol.InternetGroupManagementProtocol;
                ipV4Layer.HeaderChecksum = ((IpV4Layer)packet.Ethernet.IpV4.ExtractLayer()).HeaderChecksum;
                Assert.AreEqual(ipV4Layer, packet.Ethernet.IpV4.ExtractLayer(), "IPv4 Layer");
                ipV4Layer.HeaderChecksum = null;

                // IGMP
                Assert.IsTrue(packet.Ethernet.IpV4.Igmp.IsChecksumCorrect);
                Assert.AreEqual(igmpLayer, packet.Ethernet.IpV4.Igmp.ExtractLayer(), "IGMP Layer");
                Assert.AreEqual(igmpLayer.GetHashCode(), packet.Ethernet.IpV4.Igmp.ExtractLayer().GetHashCode(), "IGMP Layer");
                Assert.AreNotEqual(igmpLayer, null);
                Assert.AreNotEqual(igmpLayer, random.NextPayloadLayer(igmpLayer.Length));
                Assert.AreNotEqual(igmpLayer.GetHashCode(), random.NextPayloadLayer(igmpLayer.Length).GetHashCode());
                if (packet.Ethernet.IpV4.Igmp.QueryVersion != IgmpQueryVersion.Version3)
                    MoreAssert.IsSmallerOrEqual(IgmpDatagram.MaxMaxResponseTime, packet.Ethernet.IpV4.Igmp.MaxResponseTime);
                if (packet.Ethernet.IpV4.Igmp.MessageType != IgmpMessageType.MembershipQuery)
                    Assert.AreEqual(IgmpQueryVersion.None, packet.Ethernet.IpV4.Igmp.QueryVersion);
                switch (igmpLayer.MessageType)
                {
                    case IgmpMessageType.MembershipQuery:
                        switch (igmpLayer.QueryVersion)
                        {
                            case IgmpQueryVersion.Version1:
                                Assert.AreEqual(1, packet.Ethernet.IpV4.Igmp.Version);
                                break;

                            case IgmpQueryVersion.Version2:
                                Assert.AreEqual(2, packet.Ethernet.IpV4.Igmp.Version);
                                break;

                            case IgmpQueryVersion.Version3:
                                Assert.AreEqual(3, packet.Ethernet.IpV4.Igmp.Version);
                                break;

                            default:
                                Assert.Fail(igmpLayer.QueryVersion.ToString());
                                break;
                        }
                        break;

                    case IgmpMessageType.MembershipReportVersion1:
                        Assert.AreEqual(1, packet.Ethernet.IpV4.Igmp.Version);
                        break;

                    case IgmpMessageType.MembershipReportVersion2:
                    case IgmpMessageType.LeaveGroupVersion2:
                        Assert.AreEqual(2, packet.Ethernet.IpV4.Igmp.Version);
                        break;

                    case IgmpMessageType.MembershipReportVersion3:
                        Assert.AreEqual(3, packet.Ethernet.IpV4.Igmp.Version);
                        break;

                    default:
                        Assert.Fail(igmpLayer.MessageType.ToString());
                        break;
                }
                foreach (IgmpGroupRecordDatagram groupRecord in packet.Ethernet.IpV4.Igmp.GroupRecords)
                    Assert.IsNotNull(groupRecord.ToString());
            }
        }
Пример #4
0
        public void RandomIgmpTest()
        {
            EthernetLayer ethernetLayer = new EthernetLayer
                                              {
                                                  Source = new MacAddress("00:01:02:03:04:05"),
                                                  Destination = new MacAddress("A0:A1:A2:A3:A4:A5")
                                              };

            int seed = new Random().Next();
            Console.WriteLine("Seed: " + seed);
            Random random = new Random(seed);

            for (int i = 0; i != 200; ++i)
            {
                IpV4Layer ipV4Layer = random.NextIpV4Layer(null);
                ipV4Layer.HeaderChecksum = null;
                Layer ipLayer = random.NextBool() ? (Layer)ipV4Layer : random.NextIpV6Layer(IpV4Protocol.InternetGroupManagementProtocol, false);

                IgmpLayer igmpLayer = random.NextIgmpLayer();

                Packet packet = PacketBuilder.Build(DateTime.Now, ethernetLayer, ipLayer, igmpLayer);

                Assert.IsTrue(packet.IsValid, "IsValid");

                // Ethernet
                ethernetLayer.EtherType = ipLayer == ipV4Layer ? EthernetType.IpV4 : EthernetType.IpV6;
                Assert.AreEqual(ethernetLayer, packet.Ethernet.ExtractLayer(), "Ethernet Layer");
                ethernetLayer.EtherType = EthernetType.None;

                // IP.
                if (ipV4Layer == ipLayer)
                {
                    // IPv4.
                    ipV4Layer.Protocol = IpV4Protocol.InternetGroupManagementProtocol;
                    ipV4Layer.HeaderChecksum = ((IpV4Layer)packet.Ethernet.IpV4.ExtractLayer()).HeaderChecksum;
                    Assert.AreEqual(ipV4Layer, packet.Ethernet.IpV4.ExtractLayer(), "IPv4 Layer");
                    ipV4Layer.HeaderChecksum = null;
                }
                else
                {
                    // IPv6.
                    Assert.AreEqual(ipLayer, packet.Ethernet.IpV6.ExtractLayer(), "IPv6 Layer");
                }

                // IGMP
                Assert.IsTrue(packet.Ethernet.Ip.Igmp.IsChecksumCorrect);
                Assert.AreEqual(igmpLayer, packet.Ethernet.Ip.Igmp.ExtractLayer(), "IGMP Layer");
                Assert.AreEqual(igmpLayer.GetHashCode(), packet.Ethernet.Ip.Igmp.ExtractLayer().GetHashCode(), "IGMP Layer");
                Assert.AreNotEqual(igmpLayer, null);
                Assert.AreNotEqual(igmpLayer, random.NextPayloadLayer(igmpLayer.Length));
                Assert.AreNotEqual(igmpLayer.GetHashCode(), random.NextPayloadLayer(igmpLayer.Length).GetHashCode());
                if (packet.Ethernet.Ip.Igmp.QueryVersion != IgmpQueryVersion.Version3)
                    MoreAssert.IsSmallerOrEqual(IgmpDatagram.MaxMaxResponseTime, packet.Ethernet.Ip.Igmp.MaxResponseTime);
                if (packet.Ethernet.Ip.Igmp.MessageType != IgmpMessageType.MembershipQuery)
                    Assert.AreEqual(IgmpQueryVersion.None, packet.Ethernet.Ip.Igmp.QueryVersion);
                switch (igmpLayer.MessageTypeValue)
                {
                    case IgmpMessageType.CreateGroupRequestVersion0:
                    case IgmpMessageType.CreateGroupReplyVersion0:
                    case IgmpMessageType.JoinGroupRequestVersion0:
                    case IgmpMessageType.JoinGroupReplyVersion0:
                    case IgmpMessageType.LeaveGroupRequestVersion0:
                    case IgmpMessageType.LeaveGroupReplyVersion0:
                    case IgmpMessageType.ConfirmGroupRequestVersion0:
                    case IgmpMessageType.ConfirmGroupReplyVersion0:
                        Assert.AreEqual(0, packet.Ethernet.Ip.Igmp.Version);
                        IgmpVersion0Layer igmpVersion0Layer = (IgmpVersion0Layer)igmpLayer;
                        Assert.AreEqual(igmpVersion0Layer.IdentifierValue, packet.Ethernet.Ip.Igmp.Identifier);
                        Assert.AreEqual(igmpVersion0Layer.AccessKeyValue, packet.Ethernet.Ip.Igmp.AccessKey);

                        switch (igmpLayer.MessageTypeValue)
                        {
                            case IgmpMessageType.CreateGroupRequestVersion0:
                                Assert.AreEqual(((IgmpCreateGroupRequestVersion0Layer)igmpLayer).CreateGroupRequestCode, packet.Ethernet.Ip.Igmp.CreateGroupRequestCode);
                                break;

                            case IgmpMessageType.CreateGroupReplyVersion0:
                            case IgmpMessageType.JoinGroupReplyVersion0:
                            case IgmpMessageType.LeaveGroupReplyVersion0:
                            case IgmpMessageType.ConfirmGroupReplyVersion0:
                                IgmpReplyVersion0Layer igmpReplyVersion0Layer = (IgmpReplyVersion0Layer)igmpVersion0Layer;
                                Assert.AreEqual(igmpReplyVersion0Layer.Code, packet.Ethernet.Ip.Igmp.ReplyCode);
                                if (packet.Ethernet.Ip.Igmp.ReplyCode == IgmpVersion0ReplyCode.RequestPendingRetryInThisManySeconds)
                                    Assert.AreEqual(igmpReplyVersion0Layer.RetryInThisManySeconds, packet.Ethernet.Ip.Igmp.RetryInThisManySeconds);
                                break;
                        }

                        break;

                    case IgmpMessageType.MembershipQuery:
                        switch (igmpLayer.QueryVersion)
                        {
                            case IgmpQueryVersion.Version1:
                                Assert.AreEqual(1, packet.Ethernet.Ip.Igmp.Version);
                                break;

                            case IgmpQueryVersion.Version2:
                                Assert.AreEqual(2, packet.Ethernet.Ip.Igmp.Version);
                                break;

                            case IgmpQueryVersion.Version3:
                                Assert.AreEqual(3, packet.Ethernet.Ip.Igmp.Version);
                                break;

                            default:
                                Assert.Fail(igmpLayer.QueryVersion.ToString());
                                break;
                        }
                        break;

                    case IgmpMessageType.MembershipReportVersion1:
                        Assert.AreEqual(1, packet.Ethernet.Ip.Igmp.Version);
                        break;

                    case IgmpMessageType.MembershipReportVersion2:
                    case IgmpMessageType.LeaveGroupVersion2:
                        Assert.AreEqual(2, packet.Ethernet.Ip.Igmp.Version);
                        break;

                    case IgmpMessageType.MembershipReportVersion3:
                        Assert.AreEqual(3, packet.Ethernet.Ip.Igmp.Version);
                        break;

                    default:
                        Assert.Fail(igmpLayer.MessageTypeValue.ToString());
                        break;
                }
                foreach (IgmpGroupRecordDatagram groupRecord in packet.Ethernet.Ip.Igmp.GroupRecords)
                    Assert.IsNotNull(groupRecord.ToString());
            }
        }