-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathUdpTransport.cs
600 lines (579 loc) · 19.4 KB
/
UdpTransport.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
// 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;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace SnmpSharpNet
{
/// <summary>
/// Async delegate called on completion of Async SNMP request
/// </summary>
/// <param name="status">SNMP request status. If status is NoError then pdu will contain valid information</param>
/// <param name="peer">Peer IP and port number. This value is only valid if status is NoError</param>
/// <param name="buffer">Returned data. This value is only valid if status is NoError</param>
/// <param name="length">Length of the returned data. This value is only valid if status is NoError.</param>
internal delegate void SnmpAsyncCallback(AsyncRequestResult status, IPEndPoint peer, byte[] buffer, int length);
/// <summary>
/// IP/UDP transport class.
/// </summary>
public class UdpTransport: IDisposable
{
/// <summary>
/// Socket
/// </summary>
protected Socket _socket;
/// <summary>
/// Flag showing if class is using IPv6 or IPv4
/// </summary>
protected bool _isIPv6;
/// <summary>
/// Internal variable used to disable host IP address/port number check on received SNMP reply packets. If this option is disabled (default)
/// only replies from the IP address/port number combination to which the request was sent will be accepted as valid packets.
///
/// This value is set in the AgentParameters class and is only valid for SNMP v1 and v2c requests.
/// </summary>
protected bool _noSourceCheck;
/// <summary>
/// Constructor. Initializes and binds the Socket class
/// </summary>
/// <param name="useV6">Set to true if you wish to initialize the transport for IPv6</param>
public UdpTransport(bool useV6)
{
_isIPv6 = useV6;
_socket = null;
initSocket(_isIPv6);
}
/// <summary>
/// Destructor
/// </summary>
~UdpTransport()
{
if (_socket != null)
{
_socket.Close();
_socket = null;
}
}
/// <summary>
/// Flag used to determine if class is using IP version 6 (true) or IP version 4 (false)
/// </summary>
public bool IsIPv6
{
get { return _isIPv6; }
}
/// <summary>
/// Initialize class socket
/// </summary>
/// <param name="useV6">Should socket be initialized for IPv6 (true) of IPv4 (false)</param>
protected void initSocket(bool useV6)
{
if (_socket != null)
{
Close();
}
if (useV6)
{
_socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
}
else
{
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
}
IPEndPoint ipEndPoint = new IPEndPoint(_socket.AddressFamily == AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any, 0);
EndPoint ep = (EndPoint)ipEndPoint;
_socket.Bind(ep);
}
/// <summary>
/// Make sync request using IP/UDP with request timeouts and retries.
/// </summary>
/// <param name="peer">SNMP agent IP address</param>
/// <param name="port">SNMP agent port number</param>
/// <param name="buffer">Data to send to the agent</param>
/// <param name="bufferLength">Data length in the buffer</param>
/// <param name="timeout">Timeout in milliseconds</param>
/// <param name="retries">Maximum number of retries. 0 = make a single request with no retry attempts</param>
/// <returns>Byte array returned by the agent. Null on error</returns>
/// <exception cref="SnmpException">Thrown on request timed out. SnmpException.ErrorCode is set to
/// SnmpException.RequestTimedOut constant.</exception>
/// <exception cref="SnmpException">Thrown when IPv4 address is passed to the v6 socket or vice versa</exception>
public byte[] Request(IPAddress peer, int port, byte[] buffer, int bufferLength, int timeout, int retries)
{
if (_socket == null)
{
return null; // socket has been closed. no new operations are possible.
}
if (_socket.AddressFamily != peer.AddressFamily)
throw new SnmpException("Invalid address protocol version.");
IPEndPoint netPeer = new IPEndPoint(peer, port);
_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, timeout);
int recv = 0;
int retry = 0;
byte[] inbuffer = new byte[64 * 1024];
EndPoint remote = (EndPoint)new IPEndPoint(peer.AddressFamily == AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any,0);
while (true)
{
try
{
_socket.SendTo(buffer, bufferLength, SocketFlags.None, (EndPoint)netPeer);
recv = _socket.ReceiveFrom(inbuffer, ref remote);
}
catch (SocketException ex)
{
if (ex.ErrorCode == 10040)
{
recv = 0; // Packet too large
}
else if (ex.ErrorCode == 10050)
{
throw new SnmpNetworkException(ex, "Network error: Destination network is down.");
}
else if (ex.ErrorCode == 10051)
{
throw new SnmpNetworkException(ex, "Network error: destination network is unreachable.");
}
else if( ex.ErrorCode == 10054)
{
throw new SnmpNetworkException(ex, "Network error: connection reset by peer.");
}
else if (ex.ErrorCode == 10064)
{
throw new SnmpNetworkException(ex, "Network error: remote host is down.");
}
else if (ex.ErrorCode == 10065)
{
throw new SnmpNetworkException(ex, "Network error: remote host is unreachable.");
}
else if (ex.ErrorCode == 10061)
{
throw new SnmpNetworkException(ex, "Network error: connection refused.");
}
else if (ex.ErrorCode == 10060)
{
recv = 0; // Connection attempt timed out. Fall through to retry
}
else
{
// Assume it is a timeout
}
}
if (recv > 0)
{
IPEndPoint remEP = remote as IPEndPoint;
if ( ! _noSourceCheck && ! remEP.Equals(netPeer))
{
if (remEP.Address != netPeer.Address)
{
Console.WriteLine("Address miss-match {0} != {1}", remEP.Address, netPeer.Address);
}
if (remEP.Port != netPeer.Port)
{
Console.WriteLine("Port # miss-match {0} != {1}", remEP.Port, netPeer.Port);
}
/* Not good, we got a response from somebody other then who we requested a response from */
retry++;
if (retry > retries)
{
throw new SnmpException(SnmpException.RequestTimedOut, "Request has reached maximum retries.");
// return null;
}
}
else
{
MutableByte buf = new MutableByte(inbuffer, recv);
return buf;
}
}
else
{
retry++;
if (retry > retries)
{
throw new SnmpException(SnmpException.RequestTimedOut, "Request has reached maximum retries.");
}
}
}
}
/// <summary>
/// SNMP request internal callback
/// </summary>
internal event SnmpAsyncCallback _asyncCallback;
/// <summary>
/// Internal flag used to mark the class busy or available. Any request made when this
/// flag is set to true will fail with OperationInProgress error.
/// </summary>
internal bool _busy;
/// <summary>
/// Is class busy. This property is true when class is servicing another request, false if
/// ready to process a new request.
/// </summary>
public bool IsBusy
{
get { return _busy; }
}
/// <summary>
/// Async request state information.
/// </summary>
internal AsyncRequestState _requestState;
/// <summary>
/// Incoming data buffer
/// </summary>
internal byte[] _inBuffer;
/// <summary>
/// Receiver IP end point
/// </summary>
internal IPEndPoint _receivePeer;
/// <summary>
/// Begin an async SNMP request
/// </summary>
/// <param name="peer">Pdu to send to the agent</param>
/// <param name="port">Callback to receive response from the agent</param>
/// <param name="buffer">Buffer containing data to send to the peer</param>
/// <param name="bufferLength">Length of data in the buffer</param>
/// <param name="timeout">Request timeout in milliseconds</param>
/// <param name="retries">Maximum retry count. 0 = single request no further retries.</param>
/// <param name="asyncCallback">Callback that will receive the status and result of the operation</param>
/// <returns>Returns false if another request is already in progress or if socket used by the class
/// has been closed using Dispose() member, otherwise true</returns>
/// <exception cref="SnmpException">Thrown when IPv4 address is passed to the v6 socket or vice versa</exception>
internal bool RequestAsync(IPAddress peer, int port, byte[] buffer, int bufferLength, int timeout, int retries, SnmpAsyncCallback asyncCallback)
{
if (_busy == true)
{
return false;
}
if (_socket == null)
{
return false; // socket has been closed. no new operations are possible.
}
if (_socket.AddressFamily != peer.AddressFamily)
throw new SnmpException("Invalid address protocol version.");
_busy = true;
_asyncCallback = null;
_asyncCallback += asyncCallback;
_requestState = new AsyncRequestState(peer, port, retries, timeout);
_requestState.Packet = buffer;
_requestState.PacketLength = bufferLength;
// _socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, _requestState.Timeout);
_inBuffer = new byte[64 * 1024]; // create incoming data buffer
SendToBegin(); // Send the request
return true;
}
/// <summary>
/// Calls async version of the SendTo socket function.
/// </summary>
internal void SendToBegin()
{
if (_requestState == null)
{
_busy = false;
return;
}
// kill the timeout timer - there shouldn't be one active when we are sending a new request
if (_requestState.Timer != null)
{
_requestState.Timer.Dispose();
_requestState.Timer = null;
}
if (_socket == null)
{
_busy = false;
_requestState = null;
_asyncCallback(AsyncRequestResult.Terminated, new IPEndPoint(IPAddress.Any, 0), null, 0);
return; // socket has been closed. no new operations are possible.
}
try
{
_socket.BeginSendTo(_requestState.Packet, 0, _requestState.PacketLength, SocketFlags.None, _requestState.EndPoint, new AsyncCallback(SendToCallback), null);
}
catch
{
_busy = false;
_requestState = null;
_asyncCallback(AsyncRequestResult.SocketSendError, new IPEndPoint(_socket.AddressFamily == AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any, 0), null, 0);
}
}
/// <summary>
/// Callback member called on completion of BeginSendTo send data operation.
/// </summary>
/// <param name="ar">Async result</param>
internal void SendToCallback(IAsyncResult ar)
{
if (_socket == null || ! _busy || _requestState == null)
{
_busy = false;
_requestState = null;
_asyncCallback(AsyncRequestResult.Terminated, new IPEndPoint(IPAddress.Any, 0), null, 0);
return; // socket has been closed. no new operations are possible.
}
int sentLength = 0;
try
{
sentLength = _socket.EndSendTo(ar);
}
catch (NullReferenceException ex)
{
ex.GetType();
_busy = false;
_requestState = null;
_asyncCallback(AsyncRequestResult.Terminated, new IPEndPoint(_socket.AddressFamily == AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any, 0), null, 0);
return;
}
catch
{
sentLength = 0;
}
if (sentLength != _requestState.PacketLength)
{
_busy = false;
_requestState = null;
_asyncCallback(AsyncRequestResult.SocketSendError, new IPEndPoint(_socket.AddressFamily == AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any, 0), null, 0);
return;
}
// Start receive timer
ReceiveBegin(); // Initialize a receive call
}
/// <summary>
/// Begin async version of ReceiveFrom member of the socket class.
/// </summary>
internal void ReceiveBegin()
{
// kill the timeout timer
if(_requestState.Timer != null)
{
_requestState.Timer.Dispose();
_requestState.Timer = null;
}
if (_socket == null || !_busy || _requestState == null)
{
_busy = false;
_requestState = null;
if( _socket != null )
_asyncCallback(AsyncRequestResult.Terminated, new IPEndPoint(_socket.AddressFamily == AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any, 0), null, 0);
else
_asyncCallback(AsyncRequestResult.Terminated, new IPEndPoint(IPAddress.Any, 0), null, 0);
return;
// socket has been closed. no new operations are possible.
}
_receivePeer = new IPEndPoint(_socket.AddressFamily == AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any, 0);
EndPoint ep = (EndPoint)_receivePeer;
try
{
_socket.BeginReceiveFrom(_inBuffer, 0, _inBuffer.Length, SocketFlags.None, ref ep, new AsyncCallback(ReceiveFromCallback), null);
}
catch
{
// retry on every error. this can be done better by evaluating the returned
// error value but it's a lot of work and for a non-acked protocol, just send it again
// until you reach max retries.
RetryAsyncRequest();
return;
}
_requestState.Timer = new Timer(new TimerCallback(AsyncRequestTimerCallback), null, _requestState.Timeout, System.Threading.Timeout.Infinite);
}
/// <summary>
/// Internal retry function. Checks if request has reached maximum number of retries and either resends the request if not reached,
/// or sends request timed-out notification to the caller if maximum retry count has been reached and request has failed.
/// </summary>
internal void RetryAsyncRequest()
{
// kill the timer if one is active
if (_requestState.Timer != null)
{
_requestState.Timer.Dispose();
_requestState.Timer = null;
}
if (_socket == null || !_busy || _requestState == null)
{
_busy = false;
_requestState = null;
if( _socket != null )
_asyncCallback(AsyncRequestResult.Terminated, new IPEndPoint(_socket.AddressFamily == AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any, 0), null, 0);
else
_asyncCallback(AsyncRequestResult.Terminated, new IPEndPoint(IPAddress.Any, 0), null, 0);
return; // socket has been closed. no new operations are possible.
}
// We increment the retry counter before retry count. Initial CurrentRetry value is set to -1 so that
// MaxRetries value can be 0 (first request is not counted as a retry).
_requestState.CurrentRetry += 1;
if (_requestState.CurrentRetry >= _requestState.MaxRetries)
{
_busy = false;
_requestState = null;
_asyncCallback(AsyncRequestResult.Timeout, new IPEndPoint(_socket.AddressFamily == AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any, 0), null, 0);
return;
}
else
{
SendToBegin();
}
}
/// <summary>
/// Internal callback called as part of Socket.BeginReceiveFrom. Process incoming packets and notify caller
/// of results.
/// </summary>
/// <param name="ar">Async call result used by <seealso cref="Socket.EndReceiveFrom"/></param>
internal void ReceiveFromCallback(IAsyncResult ar)
{
// kill the timer if one is active
if (_requestState.Timer != null)
{
_requestState.Timer.Dispose();
_requestState.Timer = null;
}
if (_socket == null || !_busy || _requestState == null)
{
_busy = false;
_requestState = null;
if( _socket == null )
_asyncCallback(AsyncRequestResult.Terminated, new IPEndPoint(_socket.AddressFamily == AddressFamily.InterNetwork ? IPAddress.Any : IPAddress.IPv6Any, 0), null, 0);
else
_asyncCallback(AsyncRequestResult.Terminated, new IPEndPoint(IPAddress.Any, 0), null, 0);
return; // socket has been closed. no new operations are possible.
}
int inlen = 0;
EndPoint ep = (EndPoint)_receivePeer;
try
{
inlen = _socket.EndReceiveFrom(ar, ref ep);
}
catch (SocketException ex)
{
if (ex.ErrorCode == 10040)
{
inlen = 0; // Packet too large
}
else if (ex.ErrorCode == 10050)
{
_busy = false;
_requestState = null;
_asyncCallback(AsyncRequestResult.SocketReceiveError, null, null, -1);
return;
}
else if (ex.ErrorCode == 10051)
{
_busy = false;
_requestState = null;
_asyncCallback(AsyncRequestResult.SocketReceiveError, null, null, -1);
return;
}
else if (ex.ErrorCode == 10054)
{
_busy = false;
_requestState = null;
_asyncCallback(AsyncRequestResult.SocketReceiveError, null, null, -1);
return;
}
else if (ex.ErrorCode == 10064)
{
_busy = false;
_requestState = null;
_asyncCallback(AsyncRequestResult.SocketReceiveError, null, null, -1);
return;
}
else if (ex.ErrorCode == 10065)
{
_busy = false;
_requestState = null;
_asyncCallback(AsyncRequestResult.SocketReceiveError, null, null, -1);
return;
}
else if (ex.ErrorCode == 10061)
{
_busy = false;
_requestState = null;
_asyncCallback(AsyncRequestResult.SocketReceiveError, null, null, -1);
return;
}
else if (ex.ErrorCode == 10060)
{
inlen = 0; // Connection attempt timed out. Fall through to retry
}
else
{
// Assume it is a timeout
}
}
catch (ObjectDisposedException ex)
{
ex.GetType(); // this is to avoid the compilation warning
_asyncCallback(AsyncRequestResult.Terminated, null, null, -1);
return;
}
catch (NullReferenceException ex)
{
ex.GetType(); // this is to avoid the compilation warning
_asyncCallback(AsyncRequestResult.Terminated, null, null, -1);
return;
}
catch (Exception ex)
{
ex.GetType();
// we don't care what exception happened. We only want to know if we should retry the request
inlen = 0;
}
if (inlen == 0 )
{
RetryAsyncRequest();
}
else
{
// make a copy of the data from the internal buffer
byte[] buf = new byte[inlen];
Buffer.BlockCopy(_inBuffer, 0, buf, 0, inlen);
_busy = false;
_requestState = null;
_asyncCallback(AsyncRequestResult.NoError, _receivePeer, buf, buf.Length);
}
}
/// <summary>
/// Internal timer callback. Called by _asyncTimer when SNMP request timeout has expired
/// </summary>
/// <param name="stateInfo">State info. Always null</param>
internal void AsyncRequestTimerCallback(object stateInfo)
{
if (_socket != null || _requestState != null && _busy)
{
// Call retry function
RetryAsyncRequest();
}
}
/// <summary>
/// Dispose of the class.
/// </summary>
public void Dispose()
{
Close();
}
/// <summary>
/// Close network socket
/// </summary>
public void Close()
{
if( _socket != null )
{
try
{
_socket.Close();
}
catch
{
}
_socket = null;
}
}
}
}