Commit 77749cd4 authored by Brickner_cp's avatar Brickner_cp

IpV4

parent 3fcd9057
...@@ -734,7 +734,7 @@ namespace PcapDotNet.Core.Test ...@@ -734,7 +734,7 @@ namespace PcapDotNet.Core.Test
MoreAssert.IsSmallerOrEqual<uint>(1, totalStatistics.PacketsCaptured, "PacketsCaptured"); MoreAssert.IsSmallerOrEqual<uint>(1, totalStatistics.PacketsCaptured, "PacketsCaptured");
Assert.AreEqual<uint>(0, totalStatistics.PacketsDroppedByDriver, "PacketsDroppedByDriver"); Assert.AreEqual<uint>(0, totalStatistics.PacketsDroppedByDriver, "PacketsDroppedByDriver");
Assert.AreEqual<uint>(0, totalStatistics.PacketsDroppedByInterface, "PacketsDroppedByInterface"); 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()); Assert.IsNotNull(totalStatistics.ToString());
communicator.SetKernelBufferSize(2 * 1024 * 1024); // 2 MB instead of 1 communicator.SetKernelBufferSize(2 * 1024 * 1024); // 2 MB instead of 1
communicator.SetKernelMinimumBytesToCopy(10); // 10 bytes minimum to copy communicator.SetKernelMinimumBytesToCopy(10); // 10 bytes minimum to copy
......
...@@ -108,7 +108,7 @@ namespace PcapDotNet.Packets.Test ...@@ -108,7 +108,7 @@ namespace PcapDotNet.Packets.Test
ushort ipV4Identification = random.NextUShort(); ushort ipV4Identification = random.NextUShort();
byte ipV4Ttl = random.NextByte(); byte ipV4Ttl = random.NextByte();
IpV4FragmentationOptions ipV4FragmentationOptions = random.NextEnum<IpV4FragmentationOptions>(); IpV4FragmentationOptions ipV4FragmentationOptions = random.NextEnum<IpV4FragmentationOptions>();
ushort ipV4FragmentationOffset = random.NextUShort(); ushort ipV4FragmentationOffset = (ushort)(random.NextUShort(ushort.MaxValue / 8) * 8);
IpV4Fragmentation ipV4Fragmentation = new IpV4Fragmentation(ipV4FragmentationOptions, ipV4FragmentationOffset); IpV4Fragmentation ipV4Fragmentation = new IpV4Fragmentation(ipV4FragmentationOptions, ipV4FragmentationOffset);
IpV4Protocol ipV4Protocol = random.NextEnum<IpV4Protocol>(); IpV4Protocol ipV4Protocol = random.NextEnum<IpV4Protocol>();
IpV4Address ipV4Source = new IpV4Address(random.NextUInt()); IpV4Address ipV4Source = new IpV4Address(random.NextUInt());
......
...@@ -70,7 +70,7 @@ namespace PcapDotNet.Packets.TestUtils ...@@ -70,7 +70,7 @@ namespace PcapDotNet.Packets.TestUtils
public static IpV4Options NextIpV4Options(this Random random) 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>(); List<IpV4Option> options = new List<IpV4Option>();
while (optionsLength > 0) while (optionsLength > 0)
{ {
...@@ -135,7 +135,7 @@ namespace PcapDotNet.Packets.TestUtils ...@@ -135,7 +135,7 @@ namespace PcapDotNet.Packets.TestUtils
break; break;
IpV4OptionTimestampType timestampType = random.NextEnum<IpV4OptionTimestampType>(); IpV4OptionTimestampType timestampType = random.NextEnum<IpV4OptionTimestampType>();
byte overflow = random.NextByte(16); byte overflow = random.NextByte(IpV4OptionTimestamp.OverflowMaxValue + 1);
byte pointedIndex; byte pointedIndex;
if (random.NextBool()) if (random.NextBool())
pointedIndex = random.NextByte(IpV4OptionTimestamp.PointedIndexMaxValue + 1); pointedIndex = random.NextByte(IpV4OptionTimestamp.PointedIndexMaxValue + 1);
...@@ -146,17 +146,17 @@ namespace PcapDotNet.Packets.TestUtils ...@@ -146,17 +146,17 @@ namespace PcapDotNet.Packets.TestUtils
{ {
case IpV4OptionTimestampType.TimestampOnly: case IpV4OptionTimestampType.TimestampOnly:
int numTimestamps = random.Next((optionsLength - IpV4OptionTimestamp.OptionMinimumLength) / 4 + 1); 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) 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); option = new IpV4OptionTimestampOnly(overflow, pointedIndex, timestamps);
break; break;
case IpV4OptionTimestampType.AddressAndTimestamp: case IpV4OptionTimestampType.AddressAndTimestamp:
int numPairs = random.Next((optionsLength - IpV4OptionTimestamp.OptionMinimumLength) / 8 + 1); 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) 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); option = new IpV4OptionTimestampAndAddress(timestampType, overflow, pointedIndex, pairs);
break; break;
......
...@@ -12,6 +12,11 @@ namespace PcapDotNet.Packets ...@@ -12,6 +12,11 @@ namespace PcapDotNet.Packets
public IpV4Fragmentation(IpV4FragmentationOptions options, ushort offset) public IpV4Fragmentation(IpV4FragmentationOptions options, ushort offset)
: this((ushort)((ushort)options | (offset / 8))) : 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 public IpV4FragmentationOptions Options
......
...@@ -84,6 +84,9 @@ namespace PcapDotNet.Packets ...@@ -84,6 +84,9 @@ namespace PcapDotNet.Packets
protected IpV4OptionRoute(IpV4OptionType optionType, IpV4Address[] addresses, byte pointedAddressIndex) protected IpV4OptionRoute(IpV4OptionType optionType, IpV4Address[] addresses, byte pointedAddressIndex)
: base(optionType) : base(optionType)
{ {
if (pointedAddressIndex > PointedAddressIndexMaxValue)
throw new ArgumentOutOfRangeException("pointedAddressIndex", pointedAddressIndex, "Maximum value is " + PointedAddressIndexMaxValue);
_addresses = addresses; _addresses = addresses;
_pointedAddressIndex = pointedAddressIndex; _pointedAddressIndex = pointedAddressIndex;
} }
......
...@@ -6,6 +6,7 @@ namespace PcapDotNet.Packets ...@@ -6,6 +6,7 @@ namespace PcapDotNet.Packets
{ {
public const int OptionMinimumLength = 4; public const int OptionMinimumLength = 4;
public const int OptionValueMinimumLength = OptionMinimumLength - OptionHeaderLength; public const int OptionValueMinimumLength = OptionMinimumLength - OptionHeaderLength;
public const int OverflowMaxValue = 15;
public const int PointedIndexMaxValue = byte.MaxValue / 4 - 1; public const int PointedIndexMaxValue = byte.MaxValue / 4 - 1;
public IpV4OptionTimestampType TimestampType public IpV4OptionTimestampType TimestampType
...@@ -105,6 +106,12 @@ namespace PcapDotNet.Packets ...@@ -105,6 +106,12 @@ namespace PcapDotNet.Packets
protected IpV4OptionTimestamp(IpV4OptionTimestampType timestampType, byte overflow, byte pointedIndex) protected IpV4OptionTimestamp(IpV4OptionTimestampType timestampType, byte overflow, byte pointedIndex)
: base(IpV4OptionType.InternetTimestamp) : 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; _timestampType = timestampType;
_overflow = overflow; _overflow = overflow;
_pointedIndex = pointedIndex; _pointedIndex = pointedIndex;
...@@ -116,11 +123,6 @@ namespace PcapDotNet.Packets ...@@ -116,11 +123,6 @@ namespace PcapDotNet.Packets
protected abstract void WriteValues(byte[] buffer, ref int offset); 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 IpV4OptionTimestampType _timestampType;
private readonly byte _overflow; private readonly byte _overflow;
private readonly byte _pointedIndex; private readonly byte _pointedIndex;
......
...@@ -6,9 +6,15 @@ namespace PcapDotNet.Packets ...@@ -6,9 +6,15 @@ namespace PcapDotNet.Packets
{ {
public class IpV4OptionTimestampAndAddress : IpV4OptionTimestamp 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) : base(timestampType, overflow, pointedIndex)
{ {
if (timestampType != IpV4OptionTimestampType.AddressAndTimestamp &&
timestampType != IpV4OptionTimestampType.AddressPrespecified)
{
throw new ArgumentException("Illegal timestamp type " + timestampType, "timestampType");
}
_addressesAndTimestamps = addressesAndTimestamps; _addressesAndTimestamps = addressesAndTimestamps;
} }
...@@ -17,7 +23,7 @@ namespace PcapDotNet.Packets ...@@ -17,7 +23,7 @@ namespace PcapDotNet.Packets
return base.GetHashCode() ^ return base.GetHashCode() ^
_addressesAndTimestamps.Aggregate(0, (value, pair) => value ^ _addressesAndTimestamps.Aggregate(0, (value, pair) => value ^
pair.Key.GetHashCode() ^ 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) internal static IpV4OptionTimestampAndAddress Read(IpV4OptionTimestampType timestampType, byte overflow, byte pointedIndex, byte[] buffer, ref int offset, int numValues)
...@@ -25,11 +31,11 @@ namespace PcapDotNet.Packets ...@@ -25,11 +31,11 @@ namespace PcapDotNet.Packets
if (numValues % 2 != 0) if (numValues % 2 != 0)
return null; 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) for (int i = 0; i != numValues / 2; ++i)
{ {
addressesAndTimestamps[i] = new KeyValuePair<IpV4Address, TimeSpan>(new IpV4Address(buffer.ReadUInt(ref offset, Endianity.Big)), addressesAndTimestamps[i] = new KeyValuePair<IpV4Address, uint>(buffer.ReadIpV4Address(ref offset, Endianity.Big),
ReadTimeOfDay(buffer, ref offset)); buffer.ReadUInt(ref offset, Endianity.Big));
} }
return new IpV4OptionTimestampAndAddress(timestampType, overflow, pointedIndex, addressesAndTimestamps); return new IpV4OptionTimestampAndAddress(timestampType, overflow, pointedIndex, addressesAndTimestamps);
...@@ -47,13 +53,13 @@ namespace PcapDotNet.Packets ...@@ -47,13 +53,13 @@ namespace PcapDotNet.Packets
protected override void WriteValues(byte[] buffer, ref int offset) 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, 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 ...@@ -5,7 +5,7 @@ namespace PcapDotNet.Packets
{ {
public class IpV4OptionTimestampOnly : IpV4OptionTimestamp 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) : base(IpV4OptionTimestampType.TimestampOnly, overflow, pointedIndex)
{ {
_timestamps = timestamps; _timestamps = timestamps;
...@@ -19,9 +19,9 @@ namespace PcapDotNet.Packets ...@@ -19,9 +19,9 @@ namespace PcapDotNet.Packets
internal static IpV4OptionTimestampOnly Read(byte overflow, byte pointedIndex, byte[] buffer, ref int offset, int numValues) 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) 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); return new IpV4OptionTimestampOnly(overflow, pointedIndex, timestamps);
} }
...@@ -38,10 +38,10 @@ namespace PcapDotNet.Packets ...@@ -38,10 +38,10 @@ namespace PcapDotNet.Packets
protected override void WriteValues(byte[] buffer, ref int offset) protected override void WriteValues(byte[] buffer, ref int offset)
{ {
foreach (TimeSpan timestamp in _timestamps) foreach (uint timestamp in _timestamps)
buffer.Write(ref offset, (uint)timestamp.TotalMilliseconds, Endianity.Big); 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 ...@@ -8,7 +8,7 @@ namespace PcapDotNet.Packets
{ {
public class IpV4Options : ReadOnlyCollection<IpV4Option>, IEquatable<IpV4Options> 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 public static IpV4Options None
{ {
...@@ -18,6 +18,8 @@ namespace PcapDotNet.Packets ...@@ -18,6 +18,8 @@ namespace PcapDotNet.Packets
public IpV4Options(IList<IpV4Option> options) public IpV4Options(IList<IpV4Option> options)
: this(EndOptions(options), true) : 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) public IpV4Options(params IpV4Option[] options)
......
...@@ -23,9 +23,14 @@ namespace PcapDotNet.TestUtils ...@@ -23,9 +23,14 @@ namespace PcapDotNet.TestUtils
return random.NextByte(byte.MaxValue + 1); 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) 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) 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