internal static void WriteHeader(byte[] buffer, int offset,
                                         byte typeOfService, ushort identification,
                                         IpV4Fragmentation fragmentation,
                                         byte ttl, IpV4Protocol protocol, ushort?headerChecksum,
                                         IpV4Address source, IpV4Address destination,
                                         IpV4Options options, int payloadLength)
        {
            int headerLength = HeaderMinimumLength + options.BytesLength;

            buffer[offset + Offset.VersionAndHeaderLength] = (byte)((DefaultVersion << 4) + headerLength / 4);
            buffer[offset + Offset.TypeOfService]          = typeOfService;
            buffer.Write(offset + Offset.TotalLength, (ushort)(headerLength + payloadLength), Endianity.Big);
            buffer.Write(offset + Offset.Identification, identification, Endianity.Big);
            fragmentation.Write(buffer, offset + Offset.Fragmentation);
            buffer[offset + Offset.Ttl]      = ttl;
            buffer[offset + Offset.Protocol] = (byte)protocol;

            buffer.Write(offset + Offset.Source, source, Endianity.Big);
            buffer.Write(offset + Offset.Destination, destination, Endianity.Big);
            options.Write(buffer, offset + Offset.Options);

            ushort headerChecksumValue =
                headerChecksum == null
                    ? Sum16BitsToChecksum(Sum16Bits(buffer, offset, headerLength))
                    : headerChecksum.Value;

            buffer.Write(offset + Offset.HeaderChecksum, headerChecksumValue, Endianity.Big);
        }
Exemple #2
0
        internal static void WriteHeader(byte[] buffer, int offset, byte typeOfService, ushort identification, IpV4Fragmentation fragmentation, byte ttl, IpV4Protocol protocol, ushort?headerChecksum, IpV4Address source, IpV4Address destination, IpV4Options options, int payloadLength)
        {
            int length = 20 + options.BytesLength;

            buffer[offset]     = (byte)(64 + length / 4);
            buffer[offset + 1] = typeOfService;
            ByteArrayExtensions.Write(buffer, offset + 2, (ushort)(length + payloadLength), Endianity.Big);
            ByteArrayExtensions.Write(buffer, offset + 4, identification, Endianity.Big);
            fragmentation.Write(buffer, offset + 6);
            buffer[offset + 8] = ttl;
            buffer[offset + 9] = (byte)protocol;
            ByteArrayExtensions.Write(buffer, offset + 12, source, Endianity.Big);
            ByteArrayExtensions.Write(buffer, offset + 16, destination, Endianity.Big);
            options.Write(buffer, offset + 20);
            ushort?nullable = headerChecksum;
            ushort num      = !(nullable.HasValue ? new int?((int)nullable.GetValueOrDefault()) : new int?()).HasValue ? DataSegment.Sum16BitsToChecksum(DataSegment.Sum16Bits(buffer, offset, length)) : headerChecksum.Value;

            ByteArrayExtensions.Write(buffer, offset + 10, num, Endianity.Big);
        }
        internal static IpV4Address CalculateDestination(IpV4Address currentDestination, IpV4Options options)
        {
            if (options == null)
            {
                return(currentDestination);
            }

            IpV4OptionRoute destinationControllerRouteOption =
                (IpV4OptionRoute)options.OptionsCollection.FirstOrDefault(option => option.OptionType == IpV4OptionType.LooseSourceRouting ||
                                                                          option.OptionType == IpV4OptionType.StrictSourceRouting);

            if (destinationControllerRouteOption != null)
            {
                ReadOnlyCollection <IpV4Address> route = destinationControllerRouteOption.Route;
                if (destinationControllerRouteOption.PointedAddressIndex < route.Count)
                {
                    return(route[route.Count - 1]);
                }
            }

            return(currentDestination);
        }
Exemple #4
0
        internal static IpV4Address CalculateDestination(IpV4Address currentDestination, IpV4Options options)
        {
            if (options == null)
            {
                return(currentDestination);
            }
            IpV4OptionRoute ipV4OptionRoute = (IpV4OptionRoute)Enumerable.FirstOrDefault <IpV4Option>((IEnumerable <IpV4Option>)options.OptionsCollection, (Func <IpV4Option, bool>)(option =>
            {
                if (option.OptionType != IpV4OptionType.LooseSourceRouting)
                {
                    return(option.OptionType == IpV4OptionType.StrictSourceRouting);
                }
                return(true);
            }));

            if (ipV4OptionRoute != null)
            {
                ReadOnlyCollection <IpV4Address> route = ipV4OptionRoute.Route;
                if ((int)ipV4OptionRoute.PointedAddressIndex < route.Count)
                {
                    return(route[route.Count - 1]);
                }
            }
            return(currentDestination);
        }