Commit 60722bbf authored by Brickner_cp's avatar Brickner_cp

Separate different ICMP classes to different files.

parent ab9ff77a
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using PcapDotNet.Base;
namespace PcapDotNet.Packets.Icmp namespace PcapDotNet.Packets.Icmp
{ {
...@@ -194,57 +190,4 @@ namespace PcapDotNet.Packets.Icmp ...@@ -194,57 +190,4 @@ namespace PcapDotNet.Packets.Icmp
private bool? _isChecksumCorrect; private bool? _isChecksumCorrect;
private Datagram _payload; private Datagram _payload;
} }
internal static class IcmpDatagramFactory
{
internal static IcmpDatagram CreateInstance(IcmpMessageType messageType, byte[] buffer, int offset, int length)
{
IcmpDatagram prototype;
if (!_prototypes.TryGetValue(messageType, out prototype))
return new IcmpUnknownDatagram(buffer, offset, length);
return prototype.CreateInstance(buffer, offset, length);
}
private static Dictionary<IcmpMessageType, IcmpDatagram> InitializeComplexOptions()
{
var prototypes =
from type in Assembly.GetExecutingAssembly().GetTypes()
where typeof(IcmpDatagram).IsAssignableFrom(type) &&
GetRegistrationAttribute(type) != null
let constructor =
type.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public, null, new[] {typeof(byte[]), typeof(int), typeof(int)}, null)
select new
{
GetRegistrationAttribute(type).MessageType,
Datagram = (IcmpDatagram)constructor.Invoke(new object[] {null, 0, 0})
};
return prototypes.ToDictionary(prototype => prototype.MessageType, prototype => prototype.Datagram);
}
private static IcmpDatagramRegistrationAttribute GetRegistrationAttribute(Type type)
{
var registrationAttributes =
from attribute in type.GetCustomAttributes<IcmpDatagramRegistrationAttribute>(false)
select attribute;
if (!registrationAttributes.Any())
return null;
return registrationAttributes.First();
}
private static readonly Dictionary<IcmpMessageType, IcmpDatagram> _prototypes = InitializeComplexOptions();
}
internal sealed class IcmpDatagramRegistrationAttribute : Attribute
{
public IcmpDatagramRegistrationAttribute(IcmpMessageType messageType)
{
MessageType = messageType;
}
public IcmpMessageType MessageType { get; private set; }
}
} }
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using PcapDotNet.Base;
namespace PcapDotNet.Packets.Icmp
{
internal static class IcmpDatagramFactory
{
internal static IcmpDatagram CreateInstance(IcmpMessageType messageType, byte[] buffer, int offset, int length)
{
IcmpDatagram prototype;
if (!_prototypes.TryGetValue(messageType, out prototype))
return new IcmpUnknownDatagram(buffer, offset, length);
return prototype.CreateInstance(buffer, offset, length);
}
private static Dictionary<IcmpMessageType, IcmpDatagram> InitializeComplexOptions()
{
var prototypes =
from type in Assembly.GetExecutingAssembly().GetTypes()
where typeof(IcmpDatagram).IsAssignableFrom(type) &&
GetRegistrationAttribute(type) != null
let constructor =
type.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public, null, new[] {typeof(byte[]), typeof(int), typeof(int)}, null)
select new
{
GetRegistrationAttribute(type).MessageType,
Datagram = (IcmpDatagram)constructor.Invoke(new object[] {null, 0, 0})
};
return prototypes.ToDictionary(prototype => prototype.MessageType, prototype => prototype.Datagram);
}
private static IcmpDatagramRegistrationAttribute GetRegistrationAttribute(Type type)
{
var registrationAttributes =
from attribute in type.GetCustomAttributes<IcmpDatagramRegistrationAttribute>(false)
select attribute;
if (!registrationAttributes.Any())
return null;
return registrationAttributes.First();
}
private static readonly Dictionary<IcmpMessageType, IcmpDatagram> _prototypes = InitializeComplexOptions();
}
}
\ No newline at end of file
using System;
namespace PcapDotNet.Packets.Icmp
{
internal sealed class IcmpDatagramRegistrationAttribute : Attribute
{
public IcmpDatagramRegistrationAttribute(IcmpMessageType messageType)
{
MessageType = messageType;
}
public IcmpMessageType MessageType { get; private set; }
}
}
\ No newline at end of file
...@@ -243,6 +243,8 @@ ...@@ -243,6 +243,8 @@
<Compile Include="Icmp\IcmpCodeTimeExceeded.cs" /> <Compile Include="Icmp\IcmpCodeTimeExceeded.cs" />
<Compile Include="Icmp\IcmpCodeTraceRoute.cs" /> <Compile Include="Icmp\IcmpCodeTraceRoute.cs" />
<Compile Include="Icmp\IcmpConversionFailedLayer.cs" /> <Compile Include="Icmp\IcmpConversionFailedLayer.cs" />
<Compile Include="Icmp\IcmpDatagramFactory.cs" />
<Compile Include="Icmp\IcmpDatagramRegistrationAttribute.cs" />
<Compile Include="Icmp\IcmpDestinationUnreachableDatagram.cs" /> <Compile Include="Icmp\IcmpDestinationUnreachableDatagram.cs" />
<Compile Include="Icmp\IcmpDestinationUnreachableLayer.cs" /> <Compile Include="Icmp\IcmpDestinationUnreachableLayer.cs" />
<Compile Include="Icmp\IcmpDomainNameRequestDatagram.cs" /> <Compile Include="Icmp\IcmpDomainNameRequestDatagram.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