Commit 4fdf1ff5 authored by Brickner_cp's avatar Brickner_cp

Reafctoring - only empty constructors for all Layer types.

CA2227 (Usage): Collection properties should be read only - Can be suppressed only if the Collection is a ReadOnlyCollection<T>
parent 4863044c
...@@ -538,6 +538,8 @@ namespace PcapDotNet.Core.Test ...@@ -538,6 +538,8 @@ namespace PcapDotNet.Core.Test
{ {
communicator.SetFilter("ether src " + sourceMac + " and ether dst " + destinationMac); communicator.SetFilter("ether src " + sourceMac + " and ether dst " + destinationMac);
communicator.SetSamplingMethod(new SamplingMethodFirstAfterInterval(TimeSpan.FromSeconds(1))); communicator.SetSamplingMethod(new SamplingMethodFirstAfterInterval(TimeSpan.FromSeconds(1)));
int numPacketsGot;
communicator.ReceiveSomePackets(out numPacketsGot, 100, p => { });
Packet expectedPacket = _random.NextEthernetPacket(60, sourceMac, destinationMac); Packet expectedPacket = _random.NextEthernetPacket(60, sourceMac, destinationMac);
List<Packet> packets = new List<Packet>(6); List<Packet> packets = new List<Packet>(6);
Thread thread = new Thread(() => packets.AddRange(communicator.ReceivePackets(6))); Thread thread = new Thread(() => packets.AddRange(communicator.ReceivePackets(6)));
......
...@@ -77,7 +77,7 @@ namespace PcapDotNet.Packets.Test ...@@ -77,7 +77,7 @@ namespace PcapDotNet.Packets.Test
Assert.AreEqual(EthernetType.Arp, packet.Ethernet.EtherType, "Ethernet EtherType"); Assert.AreEqual(EthernetType.Arp, packet.Ethernet.EtherType, "Ethernet EtherType");
// Arp // Arp
Assert.AreEqual(ArpDatagram.HeaderBaseLength + 2 * arpLayer.SenderHardwareAddress.Count+ 2 * arpLayer.SenderProtocolAddress.Count, packet.Ethernet.Arp.Length, "Arp length"); Assert.AreEqual(ArpDatagram.HeaderBaseLength + 2 * arpLayer.SenderHardwareAddress.Count + 2 * arpLayer.SenderProtocolAddress.Count, packet.Ethernet.Arp.Length, "Arp length");
Assert.AreEqual(ArpHardwareType.Ethernet, packet.Ethernet.Arp.HardwareType, "Arp hardware type"); Assert.AreEqual(ArpHardwareType.Ethernet, packet.Ethernet.Arp.HardwareType, "Arp hardware type");
Assert.AreEqual(arpLayer, packet.Ethernet.Arp.ExtractLayer(), "ARP Layer"); Assert.AreEqual(arpLayer, packet.Ethernet.Arp.ExtractLayer(), "ARP Layer");
Assert.AreNotEqual(arpLayer, random.NextArpLayer(), "ARP Layer"); Assert.AreNotEqual(arpLayer, random.NextArpLayer(), "ARP Layer");
...@@ -90,15 +90,19 @@ namespace PcapDotNet.Packets.Test ...@@ -90,15 +90,19 @@ namespace PcapDotNet.Packets.Test
public void ArpProtocolIpV4Address() public void ArpProtocolIpV4Address()
{ {
Packet packet = PacketBuilder.Build(DateTime.Now, Packet packet = PacketBuilder.Build(DateTime.Now,
new EthernetLayer new EthernetLayer
{ {
Source = new MacAddress(), Source = new MacAddress(),
EtherType = EthernetType.QInQ EtherType = EthernetType.QInQ
}, },
new ArpLayer(new byte[8], new byte[] { 1, 2, 3, 4 }, new byte[8], new byte[] { 11, 22, 33, 44 }) new ArpLayer
{ {
Operation = ArpOperation.Request, SenderHardwareAddress = new byte[8].AsReadOnly(),
}); SenderProtocolAddress = new byte[] { 1, 2, 3, 4 }.AsReadOnly(),
TargetHardwareAddress = new byte[8].AsReadOnly(),
TargetProtocolAddress = new byte[] { 11, 22, 33, 44 }.AsReadOnly(),
Operation = ArpOperation.Request,
});
Assert.AreEqual(new IpV4Address("1.2.3.4"), packet.Ethernet.Arp.SenderProtocolIpV4Address); Assert.AreEqual(new IpV4Address("1.2.3.4"), packet.Ethernet.Arp.SenderProtocolIpV4Address);
Assert.AreEqual(new IpV4Address("11.22.33.44"), packet.Ethernet.Arp.TargetProtocolIpV4Address); Assert.AreEqual(new IpV4Address("11.22.33.44"), packet.Ethernet.Arp.TargetProtocolIpV4Address);
...@@ -109,15 +113,19 @@ namespace PcapDotNet.Packets.Test ...@@ -109,15 +113,19 @@ namespace PcapDotNet.Packets.Test
public void ArpIncosistentSenderAddressSizeTest() public void ArpIncosistentSenderAddressSizeTest()
{ {
Packet packet = PacketBuilder.Build(DateTime.Now, Packet packet = PacketBuilder.Build(DateTime.Now,
new EthernetLayer new EthernetLayer
{ {
Source = new MacAddress(), Source = new MacAddress(),
EtherType = EthernetType.IpV4 EtherType = EthernetType.IpV4
}, },
new ArpLayer(new byte[4], new byte[6], new byte[5], new byte[6]) new ArpLayer
{ {
Operation = ArpOperation.Request, SenderHardwareAddress = new byte[4].AsReadOnly(),
}); SenderProtocolAddress = new byte[6].AsReadOnly(),
TargetHardwareAddress = new byte[5].AsReadOnly(),
TargetProtocolAddress = new byte[6].AsReadOnly(),
Operation = ArpOperation.Request,
});
Assert.IsNull(packet); Assert.IsNull(packet);
Assert.Fail(); Assert.Fail();
} }
...@@ -132,8 +140,12 @@ namespace PcapDotNet.Packets.Test ...@@ -132,8 +140,12 @@ namespace PcapDotNet.Packets.Test
Source = new MacAddress(), Source = new MacAddress(),
EtherType = EthernetType.IpV4 EtherType = EthernetType.IpV4
}, },
new ArpLayer(new byte[4], new byte[6], new byte[4], new byte[7]) new ArpLayer
{ {
SenderHardwareAddress = new byte[4].AsReadOnly(),
SenderProtocolAddress = new byte[6].AsReadOnly(),
TargetHardwareAddress = new byte[4].AsReadOnly(),
TargetProtocolAddress = new byte[7].AsReadOnly(),
Operation = ArpOperation.Request, Operation = ArpOperation.Request,
}); });
Assert.IsNull(packet); Assert.IsNull(packet);
......
...@@ -262,14 +262,15 @@ namespace PcapDotNet.Packets.Test ...@@ -262,14 +262,15 @@ namespace PcapDotNet.Packets.Test
// non zero max response code report version 3 // non zero max response code report version 3
Packet reportVersion3 = PacketBuilder.Build(DateTime.Now, new EthernetLayer(), new IpV4Layer(), Packet reportVersion3 = PacketBuilder.Build(DateTime.Now, new EthernetLayer(), new IpV4Layer(),
new IgmpReportVersion3Layer( new IgmpReportVersion3Layer
new[] {
{ GroupRecords = new[]
new IgmpGroupRecord( {
IgmpRecordType.CurrentStateRecordModeIsExclude, new IgmpGroupRecord(
IpV4Address.Zero, new List<IpV4Address>(), Datagram.Empty) IgmpRecordType.CurrentStateRecordModeIsExclude,
} IpV4Address.Zero, new List<IpV4Address>(), Datagram.Empty)
)); }.AsReadOnly()
});
buffer = new byte[reportVersion3.Length]; buffer = new byte[reportVersion3.Length];
reportVersion3.Buffer.BlockCopy(0, buffer, 0, buffer.Length); reportVersion3.Buffer.BlockCopy(0, buffer, 0, buffer.Length);
......
...@@ -154,11 +154,15 @@ namespace PcapDotNet.Packets.Arp ...@@ -154,11 +154,15 @@ namespace PcapDotNet.Packets.Arp
/// </summary> /// </summary>
public override ILayer ExtractLayer() public override ILayer ExtractLayer()
{ {
return new ArpLayer(SenderHardwareAddress, SenderProtocolAddress, TargetHardwareAddress, TargetProtocolAddress) return new ArpLayer
{ {
ProtocolType = ProtocolType, SenderHardwareAddress = SenderHardwareAddress,
Operation = Operation, SenderProtocolAddress = SenderProtocolAddress,
}; TargetHardwareAddress = TargetHardwareAddress,
TargetProtocolAddress = TargetProtocolAddress,
ProtocolType = ProtocolType,
Operation = Operation,
};
} }
/// <summary> /// <summary>
......
...@@ -14,18 +14,10 @@ namespace PcapDotNet.Packets.Arp ...@@ -14,18 +14,10 @@ namespace PcapDotNet.Packets.Arp
public class ArpLayer : Layer, IEthernetNextLayer public class ArpLayer : Layer, IEthernetNextLayer
{ {
/// <summary> /// <summary>
/// Create an ARP layer by giving the different addresses. /// Create an ARP layer.
/// </summary> /// </summary>
/// <param name="senderHardwareAddress">Hardware address of the sender.</param> public ArpLayer()
/// <param name="senderProtocolAddress">Protocol address of the sender.</param>
/// <param name="targetHardwareAddress">Hardware address of the intended receiver. This field is ignored in requests.</param>
/// <param name="targetProtocolAddress">Protocol address of the intended receiver.</param>
public ArpLayer(IList<byte> senderHardwareAddress, IList<byte> senderProtocolAddress, IList<byte> targetHardwareAddress, IList<byte> targetProtocolAddress)
{ {
_senderHardwareAddress = senderHardwareAddress;
_senderProtocolAddress = senderProtocolAddress;
_targetHardwareAddress = targetHardwareAddress;
_targetProtocolAddress = targetProtocolAddress;
} }
/// <summary> /// <summary>
...@@ -41,23 +33,27 @@ namespace PcapDotNet.Packets.Arp ...@@ -41,23 +33,27 @@ namespace PcapDotNet.Packets.Arp
/// <summary> /// <summary>
/// Hardware address of the sender. /// Hardware address of the sender.
/// </summary> /// </summary>
public IList<byte> SenderHardwareAddress { get { return _senderHardwareAddress; } } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public ReadOnlyCollection<byte> SenderHardwareAddress { get; set; }
/// <summary> /// <summary>
/// Protocol address of the sender. /// Protocol address of the sender.
/// </summary> /// </summary>
public IList<byte> SenderProtocolAddress { get { return _senderProtocolAddress; } } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public ReadOnlyCollection<byte> SenderProtocolAddress { get; set; }
/// <summary> /// <summary>
/// Hardware address of the intended receiver. /// Hardware address of the intended receiver.
/// This field is ignored in requests. /// This field is ignored in requests.
/// </summary> /// </summary>
public IList<byte> TargetHardwareAddress { get { return _targetHardwareAddress; } } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public ReadOnlyCollection<byte> TargetHardwareAddress { get; set; }
/// <summary> /// <summary>
/// Protocol address of the intended receiver. /// Protocol address of the intended receiver.
/// </summary> /// </summary>
public IList<byte> TargetProtocolAddress { get { return _targetProtocolAddress; } } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public ReadOnlyCollection<byte> TargetProtocolAddress { get; set; }
/// <summary> /// <summary>
/// The Ethernet Type the Ethernet layer should write when this layer is the Ethernet payload. /// The Ethernet Type the Ethernet layer should write when this layer is the Ethernet payload.
...@@ -150,12 +146,6 @@ namespace PcapDotNet.Packets.Arp ...@@ -150,12 +146,6 @@ namespace PcapDotNet.Packets.Arp
{ {
return base.GetHashCode() ^ return base.GetHashCode() ^
(((ushort)ProtocolType << 16) + (ushort)Operation); (((ushort)ProtocolType << 16) + (ushort)Operation);
} }
private readonly IList<byte> _senderHardwareAddress;
private readonly IList<byte> _senderProtocolAddress;
private readonly IList<byte> _targetHardwareAddress;
private readonly IList<byte> _targetProtocolAddress;
} }
} }
\ No newline at end of file
...@@ -109,6 +109,7 @@ namespace PcapDotNet.Packets.Gre ...@@ -109,6 +109,7 @@ namespace PcapDotNet.Packets.Gre
/// The Routing field is a list of Source Route Entries (SREs). /// The Routing field is a list of Source Route Entries (SREs).
/// null iff the routing isn't present. /// null iff the routing isn't present.
/// </summary> /// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public ReadOnlyCollection<GreSourceRouteEntry> Routing { get; set; } public ReadOnlyCollection<GreSourceRouteEntry> Routing { get; set; }
/// <summary> /// <summary>
......
...@@ -108,11 +108,12 @@ namespace PcapDotNet.Packets.Icmp ...@@ -108,11 +108,12 @@ namespace PcapDotNet.Packets.Icmp
/// </summary> /// </summary>
public override ILayer ExtractLayer() public override ILayer ExtractLayer()
{ {
return new IcmpRouterAdvertisementLayer(Entries.ToList()) return new IcmpRouterAdvertisementLayer
{ {
Checksum = Checksum, Entries = Entries.ToList().AsReadOnly(),
Lifetime = Lifetime, Checksum = Checksum,
}; Lifetime = Lifetime,
};
} }
/// <summary> /// <summary>
......
...@@ -13,16 +13,10 @@ namespace PcapDotNet.Packets.Icmp ...@@ -13,16 +13,10 @@ namespace PcapDotNet.Packets.Icmp
public class IcmpRouterAdvertisementLayer : IcmpLayer public class IcmpRouterAdvertisementLayer : IcmpLayer
{ {
/// <summary> /// <summary>
/// Creates an instance with the given router advertisement entries. /// Creates an ICMP layer instance.
/// </summary> /// </summary>
/// <param name="entries"> public IcmpRouterAdvertisementLayer()
/// The pairs of sending router's IP address(es) on the interface from which this message is sent
/// and the preferability of each Router Address[i] as a default router address, relative to other router addresses on the same subnet.
/// A signed, twos-complement value; higher values mean more preferable.
/// </param>
public IcmpRouterAdvertisementLayer(IList<IcmpRouterAdvertisementEntry> entries)
{ {
_entries = entries;
} }
/// <summary> /// <summary>
...@@ -35,7 +29,8 @@ namespace PcapDotNet.Packets.Icmp ...@@ -35,7 +29,8 @@ namespace PcapDotNet.Packets.Icmp
/// and the preferability of each Router Address[i] as a default router address, relative to other router addresses on the same subnet. /// and the preferability of each Router Address[i] as a default router address, relative to other router addresses on the same subnet.
/// A signed, twos-complement value; higher values mean more preferable. /// A signed, twos-complement value; higher values mean more preferable.
/// </summary> /// </summary>
public IList<IcmpRouterAdvertisementEntry> Entries { get { return _entries; } } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public ReadOnlyCollection<IcmpRouterAdvertisementEntry> Entries { get; set; }
/// <summary> /// <summary>
/// The value of this field determines the format of the remaining data. /// The value of this field determines the format of the remaining data.
...@@ -93,7 +88,5 @@ namespace PcapDotNet.Packets.Icmp ...@@ -93,7 +88,5 @@ namespace PcapDotNet.Packets.Icmp
return other != null && return other != null &&
Entries.SequenceEqual(other.Entries); Entries.SequenceEqual(other.Entries);
} }
private readonly IList<IcmpRouterAdvertisementEntry> _entries;
} }
} }
\ No newline at end of file
...@@ -508,8 +508,9 @@ namespace PcapDotNet.Packets.Igmp ...@@ -508,8 +508,9 @@ namespace PcapDotNet.Packets.Igmp
}; };
case IgmpQueryVersion.Version3: case IgmpQueryVersion.Version3:
return new IgmpQueryVersion3Layer(SourceAddresses) return new IgmpQueryVersion3Layer
{ {
SourceAddresses = SourceAddresses,
MaxResponseTime = MaxResponseTime, MaxResponseTime = MaxResponseTime,
GroupAddress = GroupAddress, GroupAddress = GroupAddress,
IsSuppressRouterSideProcessing = IsSuppressRouterSideProcessing, IsSuppressRouterSideProcessing = IsSuppressRouterSideProcessing,
...@@ -542,7 +543,10 @@ namespace PcapDotNet.Packets.Igmp ...@@ -542,7 +543,10 @@ namespace PcapDotNet.Packets.Igmp
}; };
case IgmpMessageType.MembershipReportVersion3: case IgmpMessageType.MembershipReportVersion3:
return new IgmpReportVersion3Layer(GroupRecords.Select(record => record.ToGroupRecord()).ToList()); return new IgmpReportVersion3Layer
{
GroupRecords = GroupRecords.Select(record => record.ToGroupRecord()).ToList().AsReadOnly()
};
default: default:
throw new InvalidOperationException("Invalid message type " + MessageType); throw new InvalidOperationException("Invalid message type " + MessageType);
......
...@@ -14,22 +14,6 @@ namespace PcapDotNet.Packets.Igmp ...@@ -14,22 +14,6 @@ namespace PcapDotNet.Packets.Igmp
/// </summary> /// </summary>
public class IgmpQueryVersion3Layer : IgmpLayer, IIgmpLayerWithGroupAddress public class IgmpQueryVersion3Layer : IgmpLayer, IIgmpLayerWithGroupAddress
{ {
/// <summary>
/// A query on 0 source addresses.
/// </summary>
public IgmpQueryVersion3Layer()
:this(new List<IpV4Address>())
{
}
/// <summary>
/// A query on the given source addresses.
/// </summary>
public IgmpQueryVersion3Layer(IList<IpV4Address> sourceAddresses)
{
_sourceAddresses = sourceAddresses;
}
/// <summary> /// <summary>
/// The actual time allowed, called the Max Resp Time. /// The actual time allowed, called the Max Resp Time.
/// </summary> /// </summary>
...@@ -67,7 +51,8 @@ namespace PcapDotNet.Packets.Igmp ...@@ -67,7 +51,8 @@ namespace PcapDotNet.Packets.Igmp
/// The Source Address [i] fields are a vector of n IP unicast addresses, /// The Source Address [i] fields are a vector of n IP unicast addresses,
/// where n is the value in the Number of Sources (N) field. /// where n is the value in the Number of Sources (N) field.
/// </summary> /// </summary>
public IList<IpV4Address> SourceAddresses { get { return _sourceAddresses; } } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public ReadOnlyCollection<IpV4Address> SourceAddresses { get; set; }
/// <summary> /// <summary>
/// The number of bytes this layer will take. /// The number of bytes this layer will take.
...@@ -149,7 +134,5 @@ namespace PcapDotNet.Packets.Igmp ...@@ -149,7 +134,5 @@ namespace PcapDotNet.Packets.Igmp
QueryInterval.Divide(2) <= other.QueryInterval && QueryInterval.Multiply(2) >= other.QueryInterval && QueryInterval.Divide(2) <= other.QueryInterval && QueryInterval.Multiply(2) >= other.QueryInterval &&
SourceAddresses.SequenceEqual(other.SourceAddresses); SourceAddresses.SequenceEqual(other.SourceAddresses);
} }
private readonly IList<IpV4Address> _sourceAddresses;
} }
} }
\ No newline at end of file
...@@ -13,32 +13,11 @@ namespace PcapDotNet.Packets.Igmp ...@@ -13,32 +13,11 @@ namespace PcapDotNet.Packets.Igmp
/// </summary> /// </summary>
public class IgmpReportVersion3Layer : IgmpLayer public class IgmpReportVersion3Layer : IgmpLayer
{ {
/// <summary>
/// Creates an instance with no group records.
/// </summary>
public IgmpReportVersion3Layer()
:this(new List<IgmpGroupRecord>())
{
}
/// <summary>
/// Creates an instance with the given group records.
/// </summary>
/// <param name="groupRecords">
/// Each Group Record is a block of fields containing information pertaining to the sender's membership in a single multicast group on the interface from which the Report is sent.
/// </param>
public IgmpReportVersion3Layer(IList<IgmpGroupRecord> groupRecords)
{
_groupRecords = groupRecords;
}
/// <summary> /// <summary>
/// Each Group Record is a block of fields containing information pertaining to the sender's membership in a single multicast group on the interface from which the Report is sent. /// Each Group Record is a block of fields containing information pertaining to the sender's membership in a single multicast group on the interface from which the Report is sent.
/// </summary> /// </summary>
public IList<IgmpGroupRecord> GroupRecords [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
{ public ReadOnlyCollection<IgmpGroupRecord> GroupRecords { get; set;}
get { return _groupRecords; }
}
/// <summary> /// <summary>
/// The number of bytes this layer will take. /// The number of bytes this layer will take.
...@@ -100,7 +79,5 @@ namespace PcapDotNet.Packets.Igmp ...@@ -100,7 +79,5 @@ namespace PcapDotNet.Packets.Igmp
return other != null && return other != null &&
GroupRecords.SequenceEqual(other.GroupRecords); GroupRecords.SequenceEqual(other.GroupRecords);
} }
private readonly IList<IgmpGroupRecord> _groupRecords;
} }
} }
\ No newline at end of file
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