1-
2- namespace Tx . Network . Snmp . Dynamic
1+ namespace Tx . Network . Snmp . Dynamic
32{
43 using System ;
54 using System . Collections . Generic ;
@@ -14,66 +13,56 @@ namespace Tx.Network.Snmp.Dynamic
1413 /// <summary>
1514 /// TypeMap implementation for SNMP attributed classes.
1615 /// </summary>
17- public sealed class TrapTypeMap : IPartitionableTypeMap < IpPacket , ObjectIdentifier >
16+ public sealed class TrapTypeMap : IPartitionableTypeMap < SnmpDatagram , ObjectIdentifier >
1817 {
1918 private static readonly Regex HexStringRegex = new Regex ( "^[0-9A-F.-]+$" , RegexOptions . Compiled | RegexOptions . CultureInvariant | RegexOptions . ExplicitCapture ) ;
2019
21- private static readonly ObjectIdentifier trapOid = new ObjectIdentifier ( "1.3.6.1.6.3.1.1.4.1.0" ) ;
20+ private static readonly ObjectIdentifier TrapOid = new ObjectIdentifier ( "1.3.6.1.6.3.1.1.4.1.0" ) ;
2221
2322 public TrapTypeMap ( )
2423 {
2524 this . TimeFunction = packet => packet . ReceivedTime ;
2625 this . Comparer = EqualityComparer < ObjectIdentifier > . Default ;
2726 }
2827
29- public Func < IpPacket , DateTimeOffset > TimeFunction { get ; private set ; }
28+ public Func < SnmpDatagram , DateTimeOffset > TimeFunction { get ; }
3029
31- public IEqualityComparer < ObjectIdentifier > Comparer { get ; private set ; }
30+ public IEqualityComparer < ObjectIdentifier > Comparer { get ; }
3231
33- public Func < IpPacket , object > GetTransform ( Type outputType )
32+ public Func < SnmpDatagram , object > GetTransform ( Type outputType )
3433 {
3534 return CreateTransform ( outputType ) ;
3635 }
3736
3837 public ObjectIdentifier GetTypeKey ( Type outputType )
3938 {
4039 var attribute = outputType . GetAttribute < SnmpTrapAttribute > ( ) ;
41- return attribute != null ? attribute . SnmpTrapOid : default ( ObjectIdentifier ) ;
40+ return attribute ? . SnmpTrapOid ?? default ( ObjectIdentifier ) ;
4241 }
4342
44- public ObjectIdentifier GetInputKey ( IpPacket evt )
43+ public ObjectIdentifier GetInputKey ( SnmpDatagram snmpDatagram )
4544 {
46- var snmpDatagram = GetSnmpDatagram ( evt ) ;
47-
48- if ( snmpDatagram == null || snmpDatagram . VarBinds == null )
45+ if ( snmpDatagram ? . VarBinds == null )
4946 {
5047 return default ( ObjectIdentifier ) ;
5148 }
5249
5350 VarBind trapVarBind ;
54- return snmpDatagram . VarBinds . SearchFirstSubOidWith ( trapOid , out trapVarBind )
51+ return snmpDatagram . VarBinds . SearchFirstSubOidWith ( TrapOid , out trapVarBind )
5552 ? ( ObjectIdentifier ) trapVarBind . Value
5653 : default ( ObjectIdentifier ) ;
5754 }
5855
59- internal static Func < IpPacket , object > CreateTransform ( Type outputTrapType )
56+ internal static Func < SnmpDatagram , object > CreateTransform ( Type outputTrapType )
6057 {
61- if ( outputTrapType . GetAttribute < SnmpTrapAttribute > ( ) == null )
62- {
63- return null ;
64- }
65-
66- var parameter = Expression . Parameter ( typeof ( IpPacket ) , "ipPacket" ) ;
67- var getPduCall = Expression . Call ( typeof ( TrapTypeMap ) . GetMethod ( "GetSnmpDatagram" , BindingFlags . Static | BindingFlags . NonPublic ) , parameter ) ;
68- var receivedTimestampProperty = typeof ( IpPacket ) . GetProperty ( "ReceivedTime" ,
58+ var parameter = Expression . Parameter ( typeof ( SnmpDatagram ) , "snmpDatagram" ) ;
59+ var receivedTimestampProperty = typeof ( SnmpDatagram ) . GetProperty ( "ReceivedTime" ,
6960 BindingFlags . DeclaredOnly | BindingFlags . Public | BindingFlags . Instance ) ;
7061
71- var pduVar = Expression . Variable ( typeof ( SnmpDatagram ) , "pdu" ) ;
72- var assignment = Expression . Assign ( pduVar , getPduCall ) ;
73- var pduVarBindsField = typeof ( SnmpDatagram ) . GetField (
62+ var pduVarBindsField = typeof ( SnmpDatagram ) . GetProperty (
7463 "VarBinds" ,
7564 BindingFlags . Instance | BindingFlags . Public | BindingFlags . DeclaredOnly ) ;
76- var sourceAddressProperty = typeof ( IpPacket ) . GetProperty (
65+ var sourceAddressProperty = typeof ( SnmpDatagram ) . GetProperty (
7766 "SourceIpAddress" ,
7867 BindingFlags . Instance | BindingFlags . Public | BindingFlags . DeclaredOnly ) ;
7968
@@ -94,9 +83,9 @@ internal static Func<IpPacket, object> CreateTransform(Type outputTrapType)
9483 p . GetCustomAttributes ( typeof ( NotificationObjectsAttribute ) , false )
9584 . OfType < NotificationObjectsAttribute > ( )
9685 . FirstOrDefault ( ) ;
97- if ( notificationObjects != null && p . PropertyType . IsAssignableFrom ( pduVarBindsField . FieldType ) )
86+ if ( notificationObjects != null && p . PropertyType . IsAssignableFrom ( pduVarBindsField . PropertyType ) )
9887 {
99- notificationObjectsExpression = Expression . Bind ( p , Expression . Field ( pduVar , pduVarBindsField ) ) ;
88+ notificationObjectsExpression = Expression . Bind ( p , Expression . Property ( parameter , pduVarBindsField ) ) ;
10089 }
10190
10291 var ipAddressAttribute =
@@ -106,11 +95,10 @@ internal static Func<IpPacket, object> CreateTransform(Type outputTrapType)
10695 if ( ipAddressAttribute != null )
10796 {
10897 Expression ipAddress = Expression . Property ( parameter , sourceAddressProperty ) ;
109- if ( p . PropertyType == typeof ( string ) )
98+ if ( p . PropertyType == typeof ( IPAddress ) )
11099 {
111- ipAddress = Expression . Call ( ipAddress , typeof ( IPAddress ) . GetMethod ( "ToString" ) ) ;
100+ ipAddress = Expression . Call ( typeof ( IPAddress ) . GetMethod ( "Parse" ) , ipAddress ) ;
112101 }
113-
114102 ipAddressExpresion = Expression . Bind ( p , ipAddress ) ;
115103 }
116104
@@ -126,7 +114,7 @@ internal static Func<IpPacket, object> CreateTransform(Type outputTrapType)
126114 continue ;
127115 }
128116
129- var foundValue = Expression . Call ( getVarBindMethod , Expression . Field ( pduVar , pduVarBindsField ) , Expression . Constant ( notificationObjectIdentifier . Oid ) , varbindVar ) ;
117+ var foundValue = Expression . Call ( getVarBindMethod , Expression . Property ( parameter , pduVarBindsField ) , Expression . Constant ( notificationObjectIdentifier . Oid ) , varbindVar ) ;
130118
131119 Expression convertedValue = Expression . Field ( varbindVar , varbindValueField ) ;
132120 if ( p . PropertyType . IsEnum || typeof ( int ) . IsAssignableFrom ( p . PropertyType ) )
@@ -170,18 +158,18 @@ internal static Func<IpPacket, object> CreateTransform(Type outputTrapType)
170158 var castToObject = Expression . Convert ( memberInitialization , typeof ( object ) ) ;
171159
172160 var nullCheck = Expression . Condition (
173- Expression . Equal ( Expression . Constant ( null , typeof ( IpPacket ) ) , parameter ) ,
161+ Expression . Equal ( Expression . Constant ( null , typeof ( SnmpDatagram ) ) , parameter ) ,
174162 Expression . Constant ( null , typeof ( object ) ) , castToObject ) ;
175163
176- var codeBlock = Expression . Block ( new [ ] { pduVar , varbindVar , } , assignment , nullCheck ) ;
177- var transformExpression = Expression . Lambda < Func < IpPacket , object > > ( codeBlock , parameter ) ;
164+ var codeBlock = Expression . Block ( new [ ] { varbindVar , } , nullCheck ) ;
165+ var transformExpression = Expression . Lambda < Func < SnmpDatagram , object > > ( codeBlock , parameter ) ;
178166
179167 return transformExpression . Compile ( ) ;
180168 }
181169
182170 public static SnmpDatagram GetSnmpDatagram ( IpPacket ipPacket )
183171 {
184- var udpDatagram = ipPacket . ToUdpDatagram ( true ) ;
172+ var udpDatagram = ipPacket . ToUdpDatagram ( ) ;
185173 if ( udpDatagram == default ( UdpDatagram ) )
186174 {
187175 return default ( SnmpDatagram ) ;
@@ -205,6 +193,7 @@ public static SnmpDatagram GetSnmpDatagram(IpPacket ipPacket)
205193 return default ( SnmpDatagram ) ;
206194 }
207195
196+ // ReSharper disable once UnusedMember.Local
208197 private static byte [ ] GetRawOctetStringBytes ( string octetString )
209198 {
210199 if ( string . IsNullOrEmpty ( octetString ) )
0 commit comments