Commit 16995cfa authored by Brickner_cp's avatar Brickner_cp

Code Analysis

parent 23b567f8
...@@ -44,5 +44,10 @@ namespace PcapDotNet.Base ...@@ -44,5 +44,10 @@ namespace PcapDotNet.Base
{ {
return sequence.SequenceToString(string.Empty); return sequence.SequenceToString(string.Empty);
} }
public static int SequenceGetHashCode<T>(this IEnumerable<T> sequence)
{
return sequence.Aggregate(0, (valueSoFar, element) => valueSoFar ^ element.GetHashCode());
}
} }
} }
\ No newline at end of file
namespace PcapDotNet.Base namespace PcapDotNet.Base
{ {
public class Tuple<T1, T2> public class Tuple<TValue1, TValue2>
{ {
public Tuple(T1 value1, T2 value2) public Tuple(TValue1 value1, TValue2 value2)
{ {
_value1 = value1; _value1 = value1;
_value2 = value2; _value2 = value2;
} }
public T1 Value1 public TValue1 Value1
{ {
get { return _value1; } get { return _value1; }
} }
public T2 Value2 public TValue2 Value2
{ {
get { return _value2; } get { return _value2; }
} }
private readonly T1 _value1; private readonly TValue1 _value1;
private readonly T2 _value2; private readonly TValue2 _value2;
} }
} }
\ No newline at end of file
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
...@@ -51,7 +52,7 @@ namespace PcapDotNet.Base ...@@ -51,7 +52,7 @@ namespace PcapDotNet.Base
public override string ToString() public override string ToString()
{ {
return ((int)this).ToString(); return ((int)this).ToString(CultureInfo.InvariantCulture);
} }
private UInt24(int value) private UInt24(int value)
......
using System.Globalization;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace PcapDotNet.Base namespace PcapDotNet.Base
...@@ -47,7 +48,7 @@ namespace PcapDotNet.Base ...@@ -47,7 +48,7 @@ namespace PcapDotNet.Base
public override string ToString() public override string ToString()
{ {
return ((long)this).ToString(); return ((long)this).ToString(CultureInfo.InvariantCulture);
} }
private UInt48(long value) private UInt48(long value)
......
...@@ -154,9 +154,9 @@ namespace PcapDotNet.Packets.TestUtils ...@@ -154,9 +154,9 @@ namespace PcapDotNet.Packets.TestUtils
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, uint>[] pairs = new KeyValuePair<IpV4Address, uint>[numPairs]; IpV4OptionTimedAddress[] pairs = new IpV4OptionTimedAddress[numPairs];
for (int i = 0; i != numPairs; ++i) for (int i = 0; i != numPairs; ++i)
pairs[i] = new KeyValuePair<IpV4Address, uint>(random.NextIpV4Address(), random.NextUInt()); pairs[i] = new IpV4OptionTimedAddress(random.NextIpV4Address(), random.NextUInt());
option = new IpV4OptionTimestampAndAddress(timestampType, overflow, pointedIndex, pairs); option = new IpV4OptionTimestampAndAddress(timestampType, overflow, pointedIndex, pairs);
break; break;
......
...@@ -46,7 +46,7 @@ namespace PcapDotNet.Packets ...@@ -46,7 +46,7 @@ namespace PcapDotNet.Packets
{ {
return base.GetHashCode() ^ return base.GetHashCode() ^
PointedAddressIndex ^ PointedAddressIndex ^
Route.Aggregate(0, (value, address) => value ^ address.GetHashCode()); Route.SequenceGetHashCode();
} }
public ReadOnlyCollection<IpV4Address> Route public ReadOnlyCollection<IpV4Address> Route
......
...@@ -5,7 +5,7 @@ namespace PcapDotNet.Packets ...@@ -5,7 +5,7 @@ namespace PcapDotNet.Packets
public class IpV4OptionStreamIdentifier : IpV4OptionComplex, IEquatable<IpV4OptionStreamIdentifier> public class IpV4OptionStreamIdentifier : IpV4OptionComplex, IEquatable<IpV4OptionStreamIdentifier>
{ {
public const int OptionLength = 4; public const int OptionLength = 4;
public const int OptionvalueLength = OptionLength - OptionHeaderLength; public const int OptionValueLength = OptionLength - OptionHeaderLength;
public IpV4OptionStreamIdentifier(ushort identifier) public IpV4OptionStreamIdentifier(ushort identifier)
: base(IpV4OptionType.StreamIdentifier) : base(IpV4OptionType.StreamIdentifier)
......
using System;
namespace PcapDotNet.Packets
{
public struct IpV4OptionTimedAddress : IEquatable<IpV4OptionTimedAddress>
{
public IpV4OptionTimedAddress(IpV4Address address, uint timestamp)
{
_address = address;
_millisecondsSinceMidnightUt = timestamp;
}
public IpV4Address Address
{
get { return _address; }
}
public uint MillisecondsSinceMidnight
{
get { return _millisecondsSinceMidnightUt; }
}
public TimeSpan TimeOfDay
{
get { return TimeSpan.FromMilliseconds(MillisecondsSinceMidnight); }
}
public bool Equals(IpV4OptionTimedAddress other)
{
return Address == other.Address &&
MillisecondsSinceMidnight == other.MillisecondsSinceMidnight;
}
public override bool Equals(object obj)
{
return (obj is IpV4OptionTimedAddress) &&
Equals((IpV4OptionTimedAddress)obj);
}
public static bool operator ==(IpV4OptionTimedAddress value1, IpV4OptionTimedAddress value2)
{
return value1.Equals(value2);
}
public static bool operator !=(IpV4OptionTimedAddress value1, IpV4OptionTimedAddress value2)
{
return !(value1 == value2);
}
public override int GetHashCode()
{
return _address.GetHashCode() ^
(int)_millisecondsSinceMidnightUt;
}
private readonly IpV4Address _address;
private readonly uint _millisecondsSinceMidnightUt;
}
}
\ No newline at end of file
...@@ -8,7 +8,7 @@ namespace PcapDotNet.Packets ...@@ -8,7 +8,7 @@ namespace PcapDotNet.Packets
{ {
public class IpV4OptionTimestampAndAddress : IpV4OptionTimestamp public class IpV4OptionTimestampAndAddress : IpV4OptionTimestamp
{ {
public IpV4OptionTimestampAndAddress(IpV4OptionTimestampType timestampType, byte overflow, byte pointedIndex, IList<KeyValuePair<IpV4Address, uint>> addressesAndTimestamps) public IpV4OptionTimestampAndAddress(IpV4OptionTimestampType timestampType, byte overflow, byte pointedIndex, IList<IpV4OptionTimedAddress> addressesAndTimestamps)
: base(timestampType, overflow, pointedIndex) : base(timestampType, overflow, pointedIndex)
{ {
if (timestampType != IpV4OptionTimestampType.AddressAndTimestamp && if (timestampType != IpV4OptionTimestampType.AddressAndTimestamp &&
...@@ -20,17 +20,14 @@ namespace PcapDotNet.Packets ...@@ -20,17 +20,14 @@ namespace PcapDotNet.Packets
_addressesAndTimestamps = addressesAndTimestamps.AsReadOnly(); _addressesAndTimestamps = addressesAndTimestamps.AsReadOnly();
} }
public ReadOnlyCollection<KeyValuePair<IpV4Address, uint>> TimedRoute public ReadOnlyCollection<IpV4OptionTimedAddress> TimedRoute
{ {
get { return _addressesAndTimestamps; } get { return _addressesAndTimestamps; }
} }
public override int GetHashCode() public override int GetHashCode()
{ {
return base.GetHashCode() ^ return base.GetHashCode() ^ TimedRoute.SequenceGetHashCode();
TimedRoute.Aggregate(0, (value, pair) => value ^
pair.Key.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)
...@@ -38,11 +35,11 @@ namespace PcapDotNet.Packets ...@@ -38,11 +35,11 @@ namespace PcapDotNet.Packets
if (numValues % 2 != 0) if (numValues % 2 != 0)
return null; return null;
KeyValuePair<IpV4Address, uint>[] addressesAndTimestamps = new KeyValuePair<IpV4Address, uint>[numValues / 2]; IpV4OptionTimedAddress[] addressesAndTimestamps = new IpV4OptionTimedAddress[numValues / 2];
for (int i = 0; i != numValues / 2; ++i) for (int i = 0; i != numValues / 2; ++i)
{ {
addressesAndTimestamps[i] = new KeyValuePair<IpV4Address, uint>(buffer.ReadIpV4Address(ref offset, Endianity.Big), addressesAndTimestamps[i] = new IpV4OptionTimedAddress(buffer.ReadIpV4Address(ref offset, Endianity.Big),
buffer.ReadUInt(ref offset, Endianity.Big)); buffer.ReadUInt(ref offset, Endianity.Big));
} }
return new IpV4OptionTimestampAndAddress(timestampType, overflow, pointedIndex, addressesAndTimestamps); return new IpV4OptionTimestampAndAddress(timestampType, overflow, pointedIndex, addressesAndTimestamps);
...@@ -60,13 +57,13 @@ namespace PcapDotNet.Packets ...@@ -60,13 +57,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, uint> addressAndTimestamp in TimedRoute) foreach (IpV4OptionTimedAddress addressAndTimestamp in TimedRoute)
{ {
buffer.Write(ref offset, addressAndTimestamp.Key, Endianity.Big); buffer.Write(ref offset, addressAndTimestamp.Address, Endianity.Big);
buffer.Write(ref offset, addressAndTimestamp.Value, Endianity.Big); buffer.Write(ref offset, addressAndTimestamp.MillisecondsSinceMidnight, Endianity.Big);
} }
} }
private readonly ReadOnlyCollection<KeyValuePair<IpV4Address, uint>> _addressesAndTimestamps; private readonly ReadOnlyCollection<IpV4OptionTimedAddress> _addressesAndTimestamps;
} }
} }
\ No newline at end of file
...@@ -26,8 +26,7 @@ namespace PcapDotNet.Packets ...@@ -26,8 +26,7 @@ namespace PcapDotNet.Packets
public override int GetHashCode() public override int GetHashCode()
{ {
return base.GetHashCode() ^ return base.GetHashCode() ^ Timestamps.SequenceGetHashCode();
_timestamps.Aggregate(0, (value, timestamp) => value ^ timestamp.GetHashCode());
} }
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)
...@@ -41,17 +40,17 @@ namespace PcapDotNet.Packets ...@@ -41,17 +40,17 @@ namespace PcapDotNet.Packets
protected override int ValuesLength protected override int ValuesLength
{ {
get { return _timestamps.Count * 4; } get { return Timestamps.Count * sizeof(uint); }
} }
protected override bool EqualValues(IpV4OptionTimestamp other) protected override bool EqualValues(IpV4OptionTimestamp other)
{ {
return _timestamps.SequenceEqual(((IpV4OptionTimestampOnly)other)._timestamps); return Timestamps.SequenceEqual(((IpV4OptionTimestampOnly)other).Timestamps);
} }
protected override void WriteValues(byte[] buffer, ref int offset) protected override void WriteValues(byte[] buffer, ref int offset)
{ {
foreach (uint timestamp in _timestamps) foreach (uint timestamp in Timestamps)
buffer.Write(ref offset, timestamp, Endianity.Big); buffer.Write(ref offset, timestamp, Endianity.Big);
} }
......
...@@ -56,7 +56,7 @@ namespace PcapDotNet.Packets ...@@ -56,7 +56,7 @@ namespace PcapDotNet.Packets
public override int GetHashCode() public override int GetHashCode()
{ {
return BytesLength.GetHashCode() ^ return BytesLength.GetHashCode() ^
this.Aggregate(0, (value, option) => value ^ option.GetHashCode()); this.SequenceGetHashCode();
} }
public override string ToString() public override string ToString()
......
...@@ -83,6 +83,7 @@ ...@@ -83,6 +83,7 @@
<Compile Include="IpV4\IpV4OptionSecurityLevel.cs" /> <Compile Include="IpV4\IpV4OptionSecurityLevel.cs" />
<Compile Include="IpV4\IpV4OptionStreamIdentifier.cs" /> <Compile Include="IpV4\IpV4OptionStreamIdentifier.cs" />
<Compile Include="IpV4\IpV4OptionStrictSourceRouting.cs" /> <Compile Include="IpV4\IpV4OptionStrictSourceRouting.cs" />
<Compile Include="IpV4\IpV4OptionTimedAddress.cs" />
<Compile Include="IpV4\IpV4OptionTimestamp.cs" /> <Compile Include="IpV4\IpV4OptionTimestamp.cs" />
<Compile Include="IpV4\IpV4OptionTimestampAndAddress.cs" /> <Compile Include="IpV4\IpV4OptionTimestampAndAddress.cs" />
<Compile Include="IpV4\IpV4OptionTimestampOnly.cs" /> <Compile Include="IpV4\IpV4OptionTimestampOnly.cs" />
......
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