Example #1
0
        /// <summary>
        /// Writes the layer to the buffer.
        /// </summary>
        /// <param name="buffer">The buffer to write the layer to.</param>
        /// <param name="offset">The offset in the buffer to start writing the layer at.</param>
        /// <param name="payloadLength">The length of the layer's payload (the number of bytes after the layer in the packet).</param>
        /// <param name="previousLayer">The layer that comes before this layer. null if this is the first layer.</param>
        /// <param name="nextLayer">The layer that comes after this layer. null if this is the last layer.</param>
        public override void Write(byte[] buffer, int offset, int payloadLength, ILayer previousLayer, ILayer nextLayer)
        {
            IpV4Protocol?nextLayerProtocol = null;

            if (nextLayer != null)
            {
                IIpNextLayer ipNextLayer = nextLayer as IIpNextLayer;
                if (ipNextLayer != null)
                {
                    nextLayerProtocol = ipNextLayer.PreviousLayerProtocol;
                }
            }
            if (nextLayer != null && ExtensionHeaders.LastHeader == IpV4Protocol.EncapsulatingSecurityPayload)
            {
                throw new ArgumentException("Cannot have a layer after IpV6Layer with EncapsulatingSecurityPayload extension header.", "nextLayer");
            }

            if (payloadLength + ExtensionHeaders.BytesLength > ushort.MaxValue)
            {
                throw new ArgumentOutOfRangeException("payloadLength", payloadLength,
                                                      "Consider the extension headers, this must be no more than " +
                                                      (ushort.MaxValue - ExtensionHeaders.BytesLength));
            }

            IpV6Datagram.WriteHeader(buffer, offset,
                                     TrafficClass, FlowLabel, (ushort)(payloadLength + ExtensionHeaders.BytesLength), NextHeader, nextLayerProtocol, HopLimit, Source, CurrentDestination, ExtensionHeaders);
        }
        /// <summary>
        /// Writes the layer to the buffer.
        /// </summary>
        /// <param name="buffer">The buffer to write the layer to.</param>
        /// <param name="offset">The offset in the buffer to start writing the layer at.</param>
        /// <param name="payloadLength">The length of the layer's payload (the number of bytes after the layer in the packet).</param>
        /// <param name="previousLayer">The layer that comes before this layer. null if this is the first layer.</param>
        /// <param name="nextLayer">The layer that comes after this layer. null if this is the last layer.</param>
        public override void Write(byte[] buffer, int offset, int payloadLength, ILayer previousLayer, ILayer nextLayer)
        {
            IpV4Protocol protocol;

            if (Protocol == null)
            {
                if (nextLayer == null)
                {
                    throw new ArgumentException("Can't determine protocol automatically from next layer because there is no next layer");
                }
                IIpNextLayer ipNextLayer = nextLayer as IIpNextLayer;
                if (ipNextLayer == null)
                {
                    throw new ArgumentException("Can't determine protocol automatically from next layer (" + nextLayer.GetType() + ")");
                }
                protocol = ipNextLayer.PreviousLayerProtocol;
            }
            else
            {
                protocol = Protocol.Value;
            }

            IpV4Datagram.WriteHeader(buffer, offset,
                                     TypeOfService, Identification, Fragmentation,
                                     Ttl, protocol, HeaderChecksum,
                                     Source, CurrentDestination,
                                     Options, payloadLength);
        }