Example #1
0
        /// <summary>
        /// Tries to read the option from a buffer starting from the option value (after the type and length).
        /// </summary>
        /// <param name="buffer">The buffer to read the option from.</param>
        /// <param name="offset">The offset to the first byte to read the buffer. Will be incremented by the number of bytes read.</param>
        /// <param name="valueLength">The number of bytes the option value should take according to the length field that was already read.</param>
        /// <returns>On success - the complex option read. On failure - null.</returns>
        Option IOptionComplexFactory.CreateInstance(byte[] buffer, ref int offset, byte valueLength)
        {
            if (valueLength != OptionValueLength)
            {
                return(null);
            }

            ushort      identification      = buffer.ReadUShort(ref offset, Endianity.Big);
            ushort      outboundHopCount    = buffer.ReadUShort(ref offset, Endianity.Big);
            ushort      returnHopCount      = buffer.ReadUShort(ref offset, Endianity.Big);
            IpV4Address originatorIpAddress = buffer.ReadIpV4Address(ref offset, Endianity.Big);

            return(new IpV4OptionTraceRoute(identification, outboundHopCount, returnHopCount, originatorIpAddress));
        }
Example #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);
        }
Example #3
0
        private static ushort CalculateTransportChecksum(byte[] buffer, int offset, int headerLength, ushort transportLength, int transportChecksumOffset, bool isChecksumOptional, IpV4Address destination)
        {
            int  offsetAfterChecksum = offset + headerLength + transportChecksumOffset + sizeof(ushort);
            uint sum = Sum16Bits(buffer, offset + Offset.Source, IpV4Address.SizeOf) +
                       Sum16Bits(destination) +
                       buffer[offset + Offset.Protocol] + transportLength +
                       Sum16Bits(buffer, offset + headerLength, transportChecksumOffset) +
                       Sum16Bits(buffer, offsetAfterChecksum, transportLength - transportChecksumOffset - sizeof(ushort));

            ushort checksumResult = Sum16BitsToChecksum(sum);

            if (checksumResult == 0 && isChecksumOptional)
            {
                return(0xFFFF);
            }
            return(checksumResult);
        }
Example #4
0
        internal static void WriteTransportChecksum(byte[] buffer, int offset, int headerLength, ushort transportLength, int transportChecksumOffset, bool isChecksumOptional, ushort?checksum, IpV4Address destination)
        {
            ushort checksumValue =
                checksum == null
                    ? CalculateTransportChecksum(buffer, offset, headerLength, transportLength, transportChecksumOffset, isChecksumOptional, destination)
                    : checksum.Value;

            buffer.Write(offset + headerLength + transportChecksumOffset, checksumValue, Endianity.Big);
        }
Example #5
0
 /// <summary>
 /// Create the trace route option from the trace route option values.
 /// </summary>
 /// <param name="identification">
 /// An arbitrary number used by the originator of the Outbound Packet to identify the ICMP Traceroute messages.
 /// It is NOT related to the ID number in the IP header.
 /// </param>
 /// <param name="outboundHopCount">
 /// Outbound Hop Count (OHC)
 /// The number of routers through which the Outbound Packet has passed.
 /// This field is not incremented by the Outbound Packet's destination.
 /// </param>
 /// <param name="returnHopCount"></param>
 /// Return Hop Count (RHC)
 /// The number of routers through which the Return Packet has passed.
 /// This field is not incremented by the Return Packet's destination.
 /// <param name="originatorIpAddress">
 /// The IP address of the originator of the Outbound Packet.
 /// This isneeded so the routers know where to send the ICMP Traceroute message for Return Packets.
 /// It is also needed for Outbound Packets which have a Source Route option.
 /// </param>
 public IpV4OptionTraceRoute(ushort identification, ushort outboundHopCount, ushort returnHopCount, IpV4Address originatorIpAddress)
     : base(IpV4OptionType.TraceRoute)
 {
     _identification      = identification;
     _outboundHopCount    = outboundHopCount;
     _returnHopCount      = returnHopCount;
     _originatorIpAddress = originatorIpAddress;
 }
Example #6
0
 /// <summary>
 /// Two addresses are equal if the have the exact same value.
 /// </summary>
 public bool Equals(IpV4Address other)
 {
     return(ToValue() == other.ToValue());
 }
Example #7
0
 /// <summary>
 /// Create a timed address accroding to the given values.
 /// </summary>
 /// <param name="address">The address in the pair.</param>
 /// <param name="timeOfDay">The time passed since midnight UT.</param>
 public IpV4OptionTimedAddress(IpV4Address address, IpV4TimeOfDay timeOfDay)
 {
     _address   = address;
     _timeOfDay = timeOfDay;
 }