forked from griffina/SnmpSharpNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrapPdu.cs
285 lines (242 loc) · 6.88 KB
/
TrapPdu.cs
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// This file is part of SNMP#NET.
//
// SNMP#NET is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// SNMP#NET is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SNMP#NET. If not, see <http://www.gnu.org/licenses/>.
//
using System;
namespace SnmpSharpNet
{
/// <summary>SNMP version 1 TRAP Protocol Data Unit</summary>
/// <remarks>
/// Trap PDU for SNMP version 1 is a PDU with a unique layout requiring a dedicated class. SNMP versions
/// 2 and 3 use standard PDU type for V2TRAP notifications.
/// </remarks>
public class TrapPdu: AsnType, ICloneable
{
#region Internal variables
/// <summary>Trap enterprise Oid</summary>
protected Oid _enterprise;
/// <summary>The IP Address of the remote agent sending the trap.</summary>
protected IpAddress _agentAddr;
/// <summary>Generic trap code</summary>
protected Integer32 _generic;
/// <summary>Specific trap code.</summary>
protected Integer32 _specific;
/// <summary>sysUpTime timestamp of the trap event</summary>
protected TimeTicks _timeStamp;
/// <summary>Variable binding list</summary>
private VbCollection _variables;
#endregion Internal variables
#region Properties
/// <summary>Get remote agent's IP address.</summary>
virtual public IpAddress AgentAddress
{
get
{
return _agentAddr;
}
}
/// <summary>Get/Set generic code trap value object</summary>
virtual public Int32 Generic
{
get
{
return _generic.Value;
}
set
{
_generic.Value = value;
}
}
/// <summary>Get/Set specific code trap value object</summary>
virtual public Int32 Specific
{
get
{
return _specific.Value;
}
set
{
_specific.Value = value;
}
}
/// <summary>Get timeticks trap value object</summary>
virtual public UInt32 TimeStamp
{
get
{
return _timeStamp.Value;
}
set
{
_timeStamp.Value = value;
}
}
/// <summary> Returns the number oid/value pairs in the variable binding contained in the PDU</summary>
virtual public int Count
{
get
{
return _variables.Count;
}
}
#endregion Properties
/// <summary>Constructor</summary>
public TrapPdu()
{
_asnType = (byte)PduType.Trap;
_enterprise = new Oid();
_agentAddr = new IpAddress();
_generic = new Integer32();
_specific = new Integer32();
_timeStamp = new TimeTicks();
_variables = new VbCollection();
}
/// <summary> Constructs a new trap pdu that is identical to the
/// passed pdu.
/// </summary>
/// <param name="second">The object to copy.
/// </param>
public TrapPdu(TrapPdu second)
: this()
{
_enterprise.Set(second._enterprise);
_agentAddr.Set(second._agentAddr);
_generic.Value = second.Generic;
_specific.Value = second.Specific;
_timeStamp.Value = second.TimeStamp;
for (int x = 0; x < second._variables.Count; x++)
{
_variables = (VbCollection)second.VbList.Clone();
}
}
/// <summary>
/// Not implemented. Throws NotImplementedException.
/// </summary>
/// <param name="value">Irrelevant</param>
public void Set(string value)
{
throw new NotImplementedException();
}
/// <summary>
/// Get PDU type.
/// </summary>
/// <remarks>Always returns PduType.Trap</remarks>
public new PduType Type
{
get
{
return (PduType)_asnType;
}
}
/// <summary>
/// Initialize the class with values from another <see cref="TrapPdu"/> class.
/// </summary>
/// <param name="second">TrapPdu class whose values are used to initialize this class.</param>
public void Set(TrapPdu second)
{
if (second != null)
{
_enterprise.Set(second._enterprise);
_agentAddr.Set(second._agentAddr);
_generic.Value = second.Generic;
_specific.Value = second.Specific;
_timeStamp.Value = second.TimeStamp;
_variables.Clear();
for (int x = 0; x < second._variables.Count; x++)
{
_variables = (VbCollection)second.VbList.Clone();
}
}
else
throw new ArgumentException("Invalid argument type.", "value");
}
/// <summary>Get trap enterprise identifier</summary>
public Oid Enterprise
{
get
{
return _enterprise;
}
}
/// <summary>
/// Get <see cref="VbCollection"/> variable binding list.
/// </summary>
public VbCollection VbList
{
get
{
return _variables;
}
}
/// <summary>
/// Return number of entries in the VbList
/// </summary>
public int VbCount
{
get
{
return _variables.Count;
}
}
/// <summary>ASN.1 encode SNMP version 1 trap</summary>
/// <param name="buffer"><see cref="MutableByte"/> buffer to the end of which encoded values are appended.</param>
public override void encode(MutableByte buffer)
{
MutableByte trapBuffer = new MutableByte();
// encode the enterprise id & address
_enterprise.encode(trapBuffer);
_agentAddr.encode(trapBuffer);
_generic.encode(trapBuffer);
_specific.encode(trapBuffer);
_timeStamp.encode(trapBuffer);
_variables.encode(trapBuffer);
MutableByte tmpBuffer = new MutableByte();
BuildHeader(tmpBuffer, (byte)PduType.Trap, trapBuffer.Length);
trapBuffer.Prepend(tmpBuffer);
buffer.Append(trapBuffer);
}
/// <summary>Decode BER encoded SNMP version 1 trap packet</summary>
/// <param name="buffer">BER encoded buffer</param>
/// <param name="offset">Offset in the packet to start decoding from</param>
/// <returns>Buffer position after the decoded value.</returns>
/// <exception cref="SnmpException">Invalid SNMP Pdu type received. Not an SNMP version 1 Trap PDU.</exception>
/// <exception cref="SnmpException">Invalid Variable Binding list encoding.</exception>
public override int decode(byte[] buffer, int offset)
{
int headerLength;
byte asnType = ParseHeader(buffer, ref offset, out headerLength);
if (asnType != (byte)PduType.Trap)
throw new SnmpException("Invalid PDU type.");
if (headerLength > buffer.Length - offset)
throw new OverflowException("Packet is too short.");
offset = _enterprise.decode(buffer, offset);
offset = _agentAddr.decode(buffer, offset);
offset = _generic.decode(buffer, offset);
offset = _specific.decode(buffer, offset);
offset = _timeStamp.decode(buffer, offset);
// clean out the current variables
_variables.Clear();
offset = _variables.decode(buffer, offset);
return offset;
}
/// <summary>
/// Clone object
/// </summary>
/// <returns>Cloned copy of this object.</returns>
public override Object Clone()
{
return new TrapPdu(this);
}
}
}