Пример #1
0
        public override void Write(byte[] buffer, int offset, int payloadLength, ILayer previousLayer, ILayer nextLayer)
        {
            EthernetType       ethernetType      = EthernetBaseLayer.GetEthernetType(this.EtherType, nextLayer);
            MacAddress         destination       = this.Destination;
            IEthernetNextLayer ethernetNextLayer = nextLayer as IEthernetNextLayer;

            if (destination == MacAddress.Zero && ethernetNextLayer != null && ethernetNextLayer.PreviousLayerDefaultDestination.HasValue)
            {
                destination = ethernetNextLayer.PreviousLayerDefaultDestination.Value;
            }
            EthernetDatagram.WriteHeader(buffer, offset, this.Source, destination, ethernetType);
        }
        private static void CreateRandomEthernetPayload(Random random, EthernetBaseLayer ethernetBaseLayer, List<ILayer> layers)
        {
            if (random.NextBool(20))
            {
                // Finish with payload.
                PayloadLayer payloadLayer = random.NextPayloadLayer(random.Next(100));
                layers.Add(payloadLayer);
                return;
            }

            ethernetBaseLayer.EtherType = EthernetType.None;
            switch (random.NextInt(0, 7))
            {
                case 0: // VLanTaggedFrame.
                case 1:
                    VLanTaggedFrameLayer vLanTaggedFrameLayer = random.NextVLanTaggedFrameLayer();
                    layers.Add(vLanTaggedFrameLayer);
                    CreateRandomEthernetPayload(random, vLanTaggedFrameLayer, layers);
                    return;

                case 2: // ARP.
                    EthernetLayer ethernetLayer = (ethernetBaseLayer as EthernetLayer);
                    if (ethernetLayer != null)
                        ethernetLayer.Destination = MacAddress.Zero;
                    layers.Add(random.NextArpLayer());
                    return;

                case 3: // IPv4.
                case 4:
                    IpV4Layer ipV4Layer = random.NextIpV4Layer();
                    layers.Add(ipV4Layer);
                    CreateRandomIpPayload(random, ipV4Layer, layers);
                    return;

                case 5: // IPv6
                case 6:
                    IpV6Layer ipV6Layer = random.NextIpV6Layer(random.NextBool(20));
                    layers.Add(ipV6Layer);
                    CreateRandomIpPayload(random, ipV6Layer, layers);
                    return;

                default:
                    throw new InvalidOperationException("Invalid value.");
            }
        }
        public override void Write(byte[] buffer, int offset, int payloadLength, ILayer previousLayer, ILayer nextLayer)
        {
            EthernetType ethernetType = EthernetBaseLayer.GetEthernetType(this.EtherType, nextLayer);

            VLanTaggedFrameDatagram.WriteHeader(buffer, offset, this.PriorityCodePoint, this.CanonicalFormatIndicator, this.VLanIdentifier, ethernetType);
        }