forked from rqx110/SnmpSharpNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrapAgent.cs
266 lines (260 loc) · 11.5 KB
/
TrapAgent.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
// 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/>.
//
// First introduced: 0.5.2
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
namespace SnmpSharpNet
{
/// <summary>
/// Send SNMP Trap notifications
/// </summary>
/// <remarks>
/// TrapAgent class is used to hide Socket operations from users and provide an easy method to send
/// Trap notifications.
///
/// To use the class, you can use the TrapAgent class protocol specific members, recommended when you
/// expect to send a lot of notifications, or a static helper TrapAgent.SendTrap method which will
/// construct a new socket for each call.
/// </remarks>
public class TrapAgent
{
/// <summary>
/// Internal Socket class
/// </summary>
protected Socket _sock;
/// <summary>
/// Constructor.
/// </summary>
/// <remarks>
/// Constructor initializes an internal Socket used to send traps. Socket is initialized by selecting a
/// random UDP port number.
/// </remarks>
public TrapAgent()
{
_sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
_sock.Bind(new IPEndPoint(IPAddress.Any, 0));
}
/// <summary>
/// Destructor.
/// </summary>
/// <remarks>Destructors only purpose is to close the Socket used by the class.</remarks>
~TrapAgent()
{
_sock.Close();
}
/// <summary>
/// Send SNMP version 1 Trap notification
/// </summary>
/// <param name="packet">SNMP v1 Trap packet class</param>
/// <param name="peer">Manager (receiver) IP address</param>
/// <param name="port">Manager (receiver) UDP port number</param>
public void SendV1Trap(SnmpV1TrapPacket packet, IpAddress peer, int port)
{
byte[] outBuffer = packet.encode();
_sock.SendTo(outBuffer, new IPEndPoint((IPAddress)peer, port));
}
/// <summary>
/// Construct and send SNMP v1 Trap
/// </summary>
/// <param name="receiver">Receiver IP address</param>
/// <param name="receiverPort">Receiver UDP port number</param>
/// <param name="community">SNMP community name</param>
/// <param name="senderSysObjectID">Senders sysObjectID</param>
/// <param name="senderIpAdress">Sender IP address</param>
/// <param name="genericTrap">Generic trap code</param>
/// <param name="specificTrap">Specific trap code</param>
/// <param name="senderUpTime">Senders sysUpTime</param>
/// <param name="varList">Variable binding list</param>
public void SendV1Trap(IpAddress receiver, int receiverPort, string community, Oid senderSysObjectID,
IpAddress senderIpAdress, Int32 genericTrap, Int32 specificTrap, UInt32 senderUpTime,
VbCollection varList)
{
SnmpV1TrapPacket packet = new SnmpV1TrapPacket(community);
packet.Pdu.Generic = genericTrap;
packet.Pdu.Specific = specificTrap;
packet.Pdu.AgentAddress.Set(senderIpAdress);
packet.Pdu.TimeStamp = senderUpTime;
packet.Pdu.VbList.Add(varList);
packet.Pdu.Enterprise.Set(senderSysObjectID);
SendV1Trap(packet, receiver, receiverPort);
}
/// <summary>
/// Send SNMP version 2 Trap notification
/// </summary>
/// <param name="packet">SNMP v2 Trap packet class</param>
/// <param name="peer">Manager (receiver) IP address</param>
/// <param name="port">Manager (receiver) UDP port number</param>
public void SendV2Trap(SnmpV2Packet packet, IpAddress peer, int port)
{
if (packet.Pdu.Type != PduType.V2Trap)
throw new SnmpInvalidPduTypeException("Invalid Pdu type.");
byte[] outBuffer = packet.encode();
_sock.SendTo(outBuffer, new IPEndPoint((IPAddress)peer, port));
}
/// <summary>
/// Construct and send SNMP v2 Trap
/// </summary>
/// <param name="receiver">Trap receiver IP address</param>
/// <param name="receiverPort">Trap receiver UDP port number</param>
/// <param name="community">SNMP community name</param>
/// <param name="senderUpTime">Sender sysUpTime</param>
/// <param name="trapObjectID">Trap ObjectID</param>
/// <param name="varList">Variable binding list</param>
public void SendV2Trap(IpAddress receiver, int receiverPort, string community, UInt32 senderUpTime,
Oid trapObjectID, VbCollection varList)
{
SnmpV2Packet packet = new SnmpV2Packet(community);
packet.Pdu.Type = PduType.V2Trap;
packet.Pdu.TrapObjectID = trapObjectID;
packet.Pdu.TrapSysUpTime.Value = senderUpTime;
packet.Pdu.SetVbList(varList);
SendV2Trap(packet, receiver, receiverPort);
}
/// <summary>
/// Send SNMP version 3 Trap notification
/// </summary>
/// <param name="packet">SNMP v3 Trap packet class</param>
/// <param name="peer">Manager (receiver) IP address</param>
/// <param name="port">Manager (receiver) UDP port number</param>
public void SendV3Trap(SnmpV3Packet packet, IpAddress peer, int port)
{
if( packet.Pdu.Type != PduType.V2Trap )
throw new SnmpInvalidPduTypeException("Invalid Pdu type.");
byte[] outBuffer = packet.encode();
_sock.SendTo(outBuffer, new IPEndPoint((IPAddress)peer, port));
}
/// <summary>
/// Construct and send SNMP v3 noAuthNoPriv Trap
/// </summary>
/// <param name="receiver">Trap receiver IP address</param>
/// <param name="receiverPort">Trap receiver UDP port number</param>
/// <param name="engineId">Sender SNMP engineId</param>
/// <param name="senderEngineBoots">Sender SNMP engine boots</param>
/// <param name="senderEngineTime">Sender SNMP engine time</param>
/// <param name="senderUserName">Security (user) name</param>
/// <param name="senderUpTime">Sender upTime</param>
/// <param name="trapObjectID">Trap object ID</param>
/// <param name="varList">Variable binding list</param>
public void SendV3Trap(IpAddress receiver, int receiverPort, byte[] engineId, Int32 senderEngineBoots,
Int32 senderEngineTime, string senderUserName, UInt32 senderUpTime, Oid trapObjectID, VbCollection varList)
{
SnmpV3Packet packet = new SnmpV3Packet();
packet.Pdu.Type = PduType.V2Trap;
packet.NoAuthNoPriv(ASCIIEncoding.UTF8.GetBytes(senderUserName));
packet.SetEngineId(engineId);
packet.SetEngineTime(senderEngineBoots, senderEngineTime);
packet.ScopedPdu.TrapObjectID.Set(trapObjectID);
packet.ScopedPdu.TrapSysUpTime.Value = senderUpTime;
packet.ScopedPdu.VbList.Add(varList);
packet.MsgFlags.Reportable = false;
SendV3Trap(packet, receiver, receiverPort);
}
/// <summary>
/// Construct and send SNMP v3 authNoPriv Trap
/// </summary>
/// <param name="receiver">Trap receiver IP address</param>
/// <param name="receiverPort">Trap receiver UDP port number</param>
/// <param name="engineId">Sender SNMP engineId</param>
/// <param name="senderEngineBoots">Sender SNMP engine boots</param>
/// <param name="senderEngineTime">Sender SNMP engine time</param>
/// <param name="senderUserName">Security (user) name</param>
/// <param name="senderUpTime">Sender upTime</param>
/// <param name="trapObjectID">Trap object ID</param>
/// <param name="varList">Variable binding list</param>
/// <param name="authDigest">Authentication digest. <see cref="AuthenticationDigests"/> enumeration for
/// available digests</param>
/// <param name="authSecret">Authentication secret</param>
public void SendV3Trap(IpAddress receiver, int receiverPort, byte[] engineId, Int32 senderEngineBoots,
Int32 senderEngineTime, string senderUserName, UInt32 senderUpTime, Oid trapObjectID, VbCollection varList,
AuthenticationDigests authDigest, byte[] authSecret)
{
SnmpV3Packet packet = new SnmpV3Packet();
packet.Pdu.Type = PduType.V2Trap;
packet.authNoPriv(ASCIIEncoding.UTF8.GetBytes(senderUserName), authSecret, authDigest);
packet.SetEngineId(engineId);
packet.SetEngineTime(senderEngineBoots, senderEngineTime);
packet.ScopedPdu.TrapObjectID.Set(trapObjectID);
packet.ScopedPdu.TrapSysUpTime.Value = senderUpTime;
packet.ScopedPdu.VbList.Add(varList);
packet.MsgFlags.Reportable = false;
SendV3Trap(packet, receiver, receiverPort);
}
/// <summary>
/// Construct and send SNMP v3 authPriv Trap
/// </summary>
/// <param name="receiver">Trap receiver IP address</param>
/// <param name="receiverPort">Trap receiver UDP port number</param>
/// <param name="engineId">Sender SNMP engineId</param>
/// <param name="senderEngineBoots">Sender SNMP engine boots</param>
/// <param name="senderEngineTime">Sender SNMP engine time</param>
/// <param name="senderUserName">Security (user) name</param>
/// <param name="senderUpTime">Sender upTime</param>
/// <param name="trapObjectID">Trap object ID</param>
/// <param name="varList">Variable binding list</param>
/// <param name="authDigest">Authentication digest. See <see cref="AuthenticationDigests"/> enumeration for
/// available digests</param>
/// <param name="authSecret">Authentication secret</param>
/// <param name="privProtocol">Privacy protocol. See <see cref="PrivacyProtocols"/> enumeration for
/// available privacy protocols.</param>
/// <param name="privSecret">Privacy secret</param>
public void SendV3Trap(IpAddress receiver, int receiverPort, byte[] engineId, Int32 senderEngineBoots,
Int32 senderEngineTime, string senderUserName, UInt32 senderUpTime, Oid trapObjectID, VbCollection varList,
AuthenticationDigests authDigest, byte[] authSecret, PrivacyProtocols privProtocol, byte[] privSecret)
{
SnmpV3Packet packet = new SnmpV3Packet();
packet.Pdu.Type = PduType.V2Trap;
packet.authPriv(ASCIIEncoding.UTF8.GetBytes(senderUserName), authSecret, authDigest,privSecret, privProtocol);
packet.SetEngineId(engineId);
packet.SetEngineTime(senderEngineBoots, senderEngineTime);
packet.ScopedPdu.TrapObjectID.Set(trapObjectID);
packet.ScopedPdu.TrapSysUpTime.Value = senderUpTime;
packet.ScopedPdu.VbList.Add(varList);
packet.MsgFlags.Reportable = false;
SendV3Trap(packet, receiver, receiverPort);
}
/// <summary>
/// Send SNMP Trap notification
/// </summary>
/// <remarks>
/// Helper function to allow for seamless sending of SNMP notifications for all protocol versions.
///
/// packet parameter should be appropriately formatted SNMP notification in SnmpV1TrapPacket,
/// SnmpV2Packet or SnmpV3Packet class cast as SnmpPacket class.
///
/// Function will determine which version of the notification is to be used by checking the type
/// of the packet parameter and call appropriate TrapAgent member function to send it.
/// </remarks>
/// <param name="packet">SNMP trap packet</param>
/// <param name="peer">Manager (receiver) IP address</param>
/// <param name="port">Manager (receiver) UDP port number</param>
public static void SendTrap(SnmpPacket packet, IpAddress peer, int port)
{
TrapAgent agent = new TrapAgent();
if (packet is SnmpV1TrapPacket)
agent.SendV1Trap((SnmpV1TrapPacket)packet, peer, port);
else if (packet is SnmpV2Packet)
agent.SendV2Trap((SnmpV2Packet)packet, peer, port);
else if (packet is SnmpV3Packet)
agent.SendV3Trap((SnmpV3Packet)packet, peer, port);
else
throw new SnmpException("Invalid SNMP packet type.");
}
}
}