Commit 77749cd4 authored by Brickner_cp's avatar Brickner_cp

IpV4

parent 3fcd9057
......@@ -734,7 +734,7 @@ namespace PcapDotNet.Core.Test
MoreAssert.IsSmallerOrEqual<uint>(1, totalStatistics.PacketsCaptured, "PacketsCaptured");
Assert.AreEqual<uint>(0, totalStatistics.PacketsDroppedByDriver, "PacketsDroppedByDriver");
Assert.AreEqual<uint>(0, totalStatistics.PacketsDroppedByInterface, "PacketsDroppedByInterface");
Assert.AreEqual<uint>(0, totalStatistics.PacketsReceived, "PacketsReceived");
MoreAssert.IsSmallerOrEqual<uint>(1, totalStatistics.PacketsReceived, "PacketsReceived");
Assert.IsNotNull(totalStatistics.ToString());
communicator.SetKernelBufferSize(2 * 1024 * 1024); // 2 MB instead of 1
communicator.SetKernelMinimumBytesToCopy(10); // 10 bytes minimum to copy
......
......@@ -108,7 +108,7 @@ namespace PcapDotNet.Packets.Test
ushort ipV4Identification = random.NextUShort();
byte ipV4Ttl = random.NextByte();
IpV4FragmentationOptions ipV4FragmentationOptions = random.NextEnum<IpV4FragmentationOptions>();
ushort ipV4FragmentationOffset = random.NextUShort();
ushort ipV4FragmentationOffset = (ushort)(random.NextUShort(ushort.MaxValue / 8) * 8);
IpV4Fragmentation ipV4Fragmentation = new IpV4Fragmentation(ipV4FragmentationOptions, ipV4FragmentationOffset);
IpV4Protocol ipV4Protocol = random.NextEnum<IpV4Protocol>();
IpV4Address ipV4Source = new IpV4Address(random.NextUInt());
......
......@@ -70,7 +70,7 @@ namespace PcapDotNet.Packets.TestUtils
public static IpV4Options NextIpV4Options(this Random random)
{
int optionsLength = random.Next(IpV4Options.MaximumLength) / 4 * 4;
int optionsLength = random.Next(IpV4Options.MaximumBytesLength) / 4 * 4;
List<IpV4Option> options = new List<IpV4Option>();
while (optionsLength > 0)
{
......@@ -135,7 +135,7 @@ namespace PcapDotNet.Packets.TestUtils
break;
IpV4OptionTimestampType timestampType = random.NextEnum<IpV4OptionTimestampType>();
byte overflow = random.NextByte(16);
byte overflow = random.NextByte(IpV4OptionTimestamp.OverflowMaxValue + 1);
byte pointedIndex;
if (random.NextBool())
pointedIndex = random.NextByte(IpV4OptionTimestamp.PointedIndexMaxValue + 1);
......@@ -146,17 +146,17 @@ namespace PcapDotNet.Packets.TestUtils
{
case IpV4OptionTimestampType.TimestampOnly:
int numTimestamps = random.Next((optionsLength - IpV4OptionTimestamp.OptionMinimumLength) / 4 + 1);
TimeSpan[] timestamps = new TimeSpan[numTimestamps];
uint[] timestamps = new uint[numTimestamps];
for (int i = 0; i != numTimestamps; ++i)
timestamps[i] = TimeSpan.FromMilliseconds((uint)random.NextDateTime().TimeOfDay.TotalMilliseconds);
timestamps[i] = random.NextUInt();
option = new IpV4OptionTimestampOnly(overflow, pointedIndex, timestamps);
break;
case IpV4OptionTimestampType.AddressAndTimestamp:
int numPairs = random.Next((optionsLength - IpV4OptionTimestamp.OptionMinimumLength) / 8 + 1);
KeyValuePair<IpV4Address, TimeSpan>[] pairs = new KeyValuePair<IpV4Address, TimeSpan>[numPairs];
KeyValuePair<IpV4Address, uint>[] pairs = new KeyValuePair<IpV4Address, uint>[numPairs];
for (int i = 0; i != numPairs; ++i)
pairs[i] = new KeyValuePair<IpV4Address, TimeSpan>(random.NextIpV4Address(), TimeSpan.FromMilliseconds((uint)random.NextDateTime().TimeOfDay.TotalMilliseconds));
pairs[i] = new KeyValuePair<IpV4Address, uint>(random.NextIpV4Address(), random.NextUInt());
option = new IpV4OptionTimestampAndAddress(timestampType, overflow, pointedIndex, pairs);
break;
......
......@@ -12,6 +12,11 @@ namespace PcapDotNet.Packets
public IpV4Fragmentation(IpV4FragmentationOptions options, ushort offset)
: this((ushort)((ushort)options | (offset / 8)))
{
if (offset % 8 != 0)
throw new ArgumentException("offset must divide by 8", "offset");
if (((ushort)options & 0x1FFF) != 0)
throw new ArgumentException("invalid options " + options);
}
public IpV4FragmentationOptions Options
......
......@@ -84,6 +84,9 @@ namespace PcapDotNet.Packets
protected IpV4OptionRoute(IpV4OptionType optionType, IpV4Address[] addresses, byte pointedAddressIndex)
: base(optionType)
{
if (pointedAddressIndex > PointedAddressIndexMaxValue)
throw new ArgumentOutOfRangeException("pointedAddressIndex", pointedAddressIndex, "Maximum value is " + PointedAddressIndexMaxValue);
_addresses = addresses;
_pointedAddressIndex = pointedAddressIndex;
}
......
......@@ -6,6 +6,7 @@ namespace PcapDotNet.Packets
{
public const int OptionMinimumLength = 4;
public const int OptionValueMinimumLength = OptionMinimumLength - OptionHeaderLength;
public const int OverflowMaxValue = 15;
public const int PointedIndexMaxValue = byte.MaxValue / 4 - 1;
public IpV4OptionTimestampType TimestampType
......@@ -105,6 +106,12 @@ namespace PcapDotNet.Packets
protected IpV4OptionTimestamp(IpV4OptionTimestampType timestampType, byte overflow, byte pointedIndex)
: base(IpV4OptionType.InternetTimestamp)
{
if (overflow > OverflowMaxValue)
throw new ArgumentOutOfRangeException("overflow", overflow, "Maximum value is " + OverflowMaxValue);
if (pointedIndex > PointedIndexMaxValue)
throw new ArgumentOutOfRangeException("pointedIndex", pointedIndex, "Maximum value is " + PointedIndexMaxValue);
_timestampType = timestampType;
_overflow = overflow;
_pointedIndex = pointedIndex;
......@@ -116,11 +123,6 @@ namespace PcapDotNet.Packets
protected abstract void WriteValues(byte[] buffer, ref int offset);
protected static TimeSpan ReadTimeOfDay(byte[] buffer, ref int offset)
{
return TimeSpan.FromMilliseconds(buffer.ReadUInt(ref offset, Endianity.Big));
}
private readonly IpV4OptionTimestampType _timestampType;
private readonly byte _overflow;
private readonly byte _pointedIndex;
......
......@@ -6,9 +6,15 @@ namespace PcapDotNet.Packets
{
public class IpV4OptionTimestampAndAddress : IpV4OptionTimestamp
{
public IpV4OptionTimestampAndAddress(IpV4OptionTimestampType timestampType, byte overflow, byte pointedIndex, KeyValuePair<IpV4Address, TimeSpan>[] addressesAndTimestamps)
public IpV4OptionTimestampAndAddress(IpV4OptionTimestampType timestampType, byte overflow, byte pointedIndex, KeyValuePair<IpV4Address, uint>[] addressesAndTimestamps)
: base(timestampType, overflow, pointedIndex)
{
if (timestampType != IpV4OptionTimestampType.AddressAndTimestamp &&
timestampType != IpV4OptionTimestampType.AddressPrespecified)
{
throw new ArgumentException("Illegal timestamp type " + timestampType, "timestampType");
}
_addressesAndTimestamps = addressesAndTimestamps;
}
......@@ -17,7 +23,7 @@ namespace PcapDotNet.Packets
return base.GetHashCode() ^
_addressesAndTimestamps.Aggregate(0, (value, pair) => value ^
pair.Key.GetHashCode() ^
pair.Value.GetHashCode());
(int)pair.Value);
}
internal static IpV4OptionTimestampAndAddress Read(IpV4OptionTimestampType timestampType, byte overflow, byte pointedIndex, byte[] buffer, ref int offset, int numValues)
......@@ -25,11 +31,11 @@ namespace PcapDotNet.Packets
if (numValues % 2 != 0)
return null;
KeyValuePair<IpV4Address, TimeSpan>[] addressesAndTimestamps = new KeyValuePair<IpV4Address, TimeSpan>[numValues / 2];
KeyValuePair<IpV4Address, uint>[] addressesAndTimestamps = new KeyValuePair<IpV4Address, uint>[numValues / 2];
for (int i = 0; i != numValues / 2; ++i)
{
addressesAndTimestamps[i] = new KeyValuePair<IpV4Address, TimeSpan>(new IpV4Address(buffer.ReadUInt(ref offset, Endianity.Big)),
ReadTimeOfDay(buffer, ref offset));
addressesAndTimestamps[i] = new KeyValuePair<IpV4Address, uint>(buffer.ReadIpV4Address(ref offset, Endianity.Big),
buffer.ReadUInt(ref offset, Endianity.Big));
}
return new IpV4OptionTimestampAndAddress(timestampType, overflow, pointedIndex, addressesAndTimestamps);
......@@ -47,13 +53,13 @@ namespace PcapDotNet.Packets
protected override void WriteValues(byte[] buffer, ref int offset)
{
foreach (KeyValuePair<IpV4Address, TimeSpan> addressAndTimestamp in _addressesAndTimestamps)
foreach (KeyValuePair<IpV4Address, uint> addressAndTimestamp in _addressesAndTimestamps)
{
buffer.Write(ref offset, addressAndTimestamp.Key, Endianity.Big);
buffer.Write(ref offset, (uint)addressAndTimestamp.Value.TotalMilliseconds, Endianity.Big);
buffer.Write(ref offset, addressAndTimestamp.Value, Endianity.Big);
}
}
private readonly KeyValuePair<IpV4Address, TimeSpan>[] _addressesAndTimestamps;
private readonly KeyValuePair<IpV4Address, uint>[] _addressesAndTimestamps;
}
}
\ No newline at end of file
......@@ -5,7 +5,7 @@ namespace PcapDotNet.Packets
{
public class IpV4OptionTimestampOnly : IpV4OptionTimestamp
{
public IpV4OptionTimestampOnly(byte overflow, byte pointedIndex, params TimeSpan[] timestamps)
public IpV4OptionTimestampOnly(byte overflow, byte pointedIndex, params uint[] timestamps)
: base(IpV4OptionTimestampType.TimestampOnly, overflow, pointedIndex)
{
_timestamps = timestamps;
......@@ -19,9 +19,9 @@ namespace PcapDotNet.Packets
internal static IpV4OptionTimestampOnly Read(byte overflow, byte pointedIndex, byte[] buffer, ref int offset, int numValues)
{
TimeSpan[] timestamps = new TimeSpan[numValues];
uint[] timestamps = new uint[numValues];
for (int i = 0; i != numValues; ++i)
timestamps[i] = ReadTimeOfDay(buffer, ref offset);
timestamps[i] = buffer.ReadUInt(ref offset, Endianity.Big);
return new IpV4OptionTimestampOnly(overflow, pointedIndex, timestamps);
}
......@@ -38,10 +38,10 @@ namespace PcapDotNet.Packets
protected override void WriteValues(byte[] buffer, ref int offset)
{
foreach (TimeSpan timestamp in _timestamps)
buffer.Write(ref offset, (uint)timestamp.TotalMilliseconds, Endianity.Big);
foreach (uint timestamp in _timestamps)
buffer.Write(ref offset, timestamp, Endianity.Big);
}
private readonly TimeSpan[] _timestamps;
private readonly uint[] _timestamps;
}
}
\ No newline at end of file
......@@ -8,7 +8,7 @@ namespace PcapDotNet.Packets
{
public class IpV4Options : ReadOnlyCollection<IpV4Option>, IEquatable<IpV4Options>
{
public const int MaximumLength = IpV4Datagram.HeaderMaximumLength - IpV4Datagram.HeaderMinimumLength;
public const int MaximumBytesLength = IpV4Datagram.HeaderMaximumLength - IpV4Datagram.HeaderMinimumLength;
public static IpV4Options None
{
......@@ -18,6 +18,8 @@ namespace PcapDotNet.Packets
public IpV4Options(IList<IpV4Option> options)
: this(EndOptions(options), true)
{
if (BytesLength > MaximumBytesLength)
throw new ArgumentException("given options take " + BytesLength + " bytes and maximum number of bytes for options is " + MaximumBytesLength, "options");
}
public IpV4Options(params IpV4Option[] options)
......
......@@ -23,9 +23,14 @@ namespace PcapDotNet.TestUtils
return random.NextByte(byte.MaxValue + 1);
}
public static ushort NextUShort(this Random random, int maxValue)
{
return (ushort)random.Next(maxValue);
}
public static ushort NextUShort(this Random random)
{
return (ushort)random.Next(ushort.MaxValue + 1);
return random.NextUShort(ushort.MaxValue + 1);
}
public static UInt24 NextUInt24(this Random random)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment