Ejemplo n.º 1
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 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);
        }
Ejemplo n.º 2
0
        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);
        }