// Discovery type.
// Copyright (C) 2008-2010 Malcolm Crowe, Lex Li, and other contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
/*
* Created by SharpDevelop.
* User: lextm
* Date: 5/24/2009
* Time: 11:56 AM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using GSF.Net.Snmp.Security;
namespace GSF.Net.Snmp.Messaging
{
///
/// Discovery class that participates in SNMP v3 discovery process.
///
public sealed class Discovery
{
private readonly ISnmpMessage _discovery;
private static readonly UserRegistry Empty = new UserRegistry();
private static readonly SecurityParameters DefaultSecurityParameters =
new SecurityParameters(
OctetString.Empty,
Integer32.Zero,
Integer32.Zero,
OctetString.Empty,
OctetString.Empty,
OctetString.Empty);
///
/// Initializes a new instance of the class.
///
/// The request id.
/// The message id.
/// The max size of message.
public Discovery(int messageId, int requestId, int maxMessageSize)
{
_discovery = new GetRequestMessage(
VersionCode.V3,
new Header(
new Integer32(messageId),
new Integer32(maxMessageSize),
Levels.Reportable),
DefaultSecurityParameters,
new Scope(
OctetString.Empty,
OctetString.Empty,
new GetRequestPdu(requestId, new List())),
DefaultPrivacyProvider.DefaultPair,
null);
}
///
/// Initializes a new instance of the class.
///
/// The request id.
/// The message id.
/// The max size of message.
/// Message type.
public Discovery(int messageId, int requestId, int maxMessageSize, SnmpType type)
{
switch (type)
{
case SnmpType.GetRequestPdu:
{
_discovery = new GetRequestMessage(
VersionCode.V3,
new Header(
new Integer32(messageId),
new Integer32(maxMessageSize),
Levels.Reportable),
DefaultSecurityParameters,
new Scope(
OctetString.Empty,
OctetString.Empty,
new GetRequestPdu(requestId, new List())),
DefaultPrivacyProvider.DefaultPair,
null);
break;
}
case SnmpType.GetNextRequestPdu:
{
_discovery = new GetNextRequestMessage(
VersionCode.V3,
new Header(
new Integer32(messageId),
new Integer32(maxMessageSize),
Levels.Reportable),
DefaultSecurityParameters,
new Scope(
OctetString.Empty,
OctetString.Empty,
new GetNextRequestPdu(requestId, new List())),
DefaultPrivacyProvider.DefaultPair,
null);
break;
}
case SnmpType.GetBulkRequestPdu:
{
_discovery = new GetBulkRequestMessage(
VersionCode.V3,
new Header(
new Integer32(messageId),
new Integer32(maxMessageSize),
Levels.Reportable),
DefaultSecurityParameters,
new Scope(
OctetString.Empty,
OctetString.Empty,
new GetBulkRequestPdu(requestId, 0, 0, new List())),
DefaultPrivacyProvider.DefaultPair,
null);
break;
}
case SnmpType.SetRequestPdu:
{
_discovery = new SetRequestMessage(
VersionCode.V3,
new Header(
new Integer32(messageId),
new Integer32(maxMessageSize),
Levels.Reportable),
DefaultSecurityParameters,
new Scope(
OctetString.Empty,
OctetString.Empty,
new SetRequestPdu(requestId, new List())),
DefaultPrivacyProvider.DefaultPair,
null);
break;
}
case SnmpType.InformRequestPdu:
{
_discovery = new InformRequestMessage(
VersionCode.V3,
new Header(
new Integer32(messageId),
new Integer32(maxMessageSize),
Levels.Reportable),
DefaultSecurityParameters,
new Scope(
OctetString.Empty,
OctetString.Empty,
new InformRequestPdu(requestId)),
DefaultPrivacyProvider.DefaultPair,
null);
break;
}
default:
throw new ArgumentException("Discovery message must be a request.", nameof(type));
}
}
///
/// Gets the response.
///
/// The time-out value, in milliseconds. The default value is 0, which indicates an infinite time-out period. Specifying -1 also indicates an infinite time-out period.
/// The receiver.
///
public ReportMessage GetResponse(int timeout, IPEndPoint receiver)
{
if (receiver == null)
{
throw new ArgumentNullException(nameof(receiver));
}
using (Socket socket = receiver.GetSocket())
{
return (ReportMessage)_discovery.GetResponse(timeout, receiver, Empty, socket);
}
}
///
/// Gets the response.
///
/// The receiver.
///
public async Task GetResponseAsync(IPEndPoint receiver)
{
if (receiver == null)
{
throw new ArgumentNullException(nameof(receiver));
}
using (Socket socket = receiver.GetSocket())
{
return (ReportMessage)await _discovery.GetResponseAsync(receiver, Empty, socket).ConfigureAwait(false);
}
}
///
/// Converts to the bytes.
///
///
public byte[] ToBytes()
{
return _discovery.ToBytes();
}
///
/// Returns a that represents this instance.
///
///
/// A that represents this instance.
///
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "discovery class: message id: {0}; request id: {1}", _discovery.MessageId(), _discovery.RequestId());
}
}
}