-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathDhcpOptionFactory.cs
More file actions
58 lines (53 loc) · 2.4 KB
/
DhcpOptionFactory.cs
File metadata and controls
58 lines (53 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using PcapDotNet.Packets.Dhcp.Options;
namespace PcapDotNet.Packets.Dhcp
{
internal static class DhcpOptionFactory
{
internal static DhcpOption CreateInstance(DataSegment data, ref int offset)
{
DhcpOptionCode optionCode = (DhcpOptionCode)data[offset++];
MethodInfo readMethod;
if (_optionReaders.TryGetValue(optionCode, out readMethod))
{
object[] args = new object[] { data, offset };
DhcpOption option = (DhcpOption)readMethod.Invoke(null, args);
offset = (int)args[1];
return option;
}
else
{
return DhcpAnyOption.Read(data, ref offset);
}
}
private static Dictionary<DhcpOptionCode, MethodInfo> InitializeComplexOptions()
{
Dictionary<DhcpOptionCode, MethodInfo> optionReaders = new Dictionary<DhcpOptionCode, MethodInfo>();
foreach (MethodInfo readMethod in Assembly.GetExecutingAssembly().GetTypes().
Where(type=> typeof(DhcpOption).IsAssignableFrom(type))
.SelectMany(type => type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)))
{
DhcpOptionReadRegistrationAttribute readRegistrationAttribute = readMethod.GetCustomAttribute<DhcpOptionReadRegistrationAttribute>(false);
if (readRegistrationAttribute == null)
continue;
if (typeof(DataSegment).IsAssignableFrom(readMethod.GetParameters()[0].ParameterType) &&
readMethod.GetParameters()[1].ParameterType == typeof(int).MakeByRefType() &&
typeof(DhcpOption).IsAssignableFrom(readMethod.ReturnType))
{
optionReaders.Add(readRegistrationAttribute.OptionCode, readMethod);
}
else
{
throw new NotSupportedException("Method " + readMethod + " has a DhcpOptionReadRegistrationAttribute but has no valid signature");
}
}
return optionReaders;
}
private static readonly Dictionary<DhcpOptionCode, MethodInfo> _optionReaders = InitializeComplexOptions();
}
}