// INFORM request message 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: 2008/8/3
* Time: 15:37
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using System.Globalization;
using GSF.Net.Snmp.Security;
namespace GSF.Net.Snmp.Messaging
{
///
/// INFORM request message.
///
public sealed class InformRequestMessage : ISnmpMessage
{
private readonly byte[] _bytes;
///
/// Creates a with all contents.
///
/// The request id.
/// Protocol version.
/// Community name.
/// Enterprise.
/// Time ticks.
/// Variables.
[CLSCompliant(false)]
public InformRequestMessage(int requestId, VersionCode version, OctetString community, ObjectIdentifier enterprise, uint time, IList variables)
{
if (variables == null)
{
throw new ArgumentNullException(nameof(variables));
}
if (enterprise == null)
{
throw new ArgumentNullException(nameof(enterprise));
}
if (community == null)
{
throw new ArgumentNullException(nameof(community));
}
if (version == VersionCode.V3)
{
throw new ArgumentException("Only v1 and v2c are supported.", nameof(version));
}
Version = version;
Enterprise = enterprise;
TimeStamp = time;
Header = Header.Empty;
Parameters = SecurityParameters.Create(community);
InformRequestPdu pdu = new InformRequestPdu(
requestId,
enterprise,
time,
variables);
Scope = new Scope(pdu);
Privacy = DefaultPrivacyProvider.DefaultPair;
_bytes = this.PackMessage(null).ToBytes();
}
///
/// Initializes a new instance of the class.
///
/// The version.
/// The message id.
/// The request id.
/// Name of the user.
/// The enterprise.
/// The time.
/// The variables.
/// The privacy provider.
/// The report.
[CLSCompliant(false)]
[Obsolete("Please use other overloading ones.")]
public InformRequestMessage(VersionCode version, int messageId, int requestId, OctetString userName, ObjectIdentifier enterprise, uint time, IList variables, IPrivacyProvider privacy, ISnmpMessage report)
: this(version, messageId, requestId, userName, enterprise, time, variables, privacy, 0xFFE3, report)
{
}
///
/// Initializes a new instance of the class.
///
/// The version.
/// The message id.
/// The request id.
/// Name of the user.
/// The enterprise.
/// The time.
/// The variables.
/// The privacy provider.
/// Size of the max message.
/// The report.
[CLSCompliant(false)]
public InformRequestMessage(VersionCode version, int messageId, int requestId, OctetString userName, ObjectIdentifier enterprise, uint time, IList variables, IPrivacyProvider privacy, int maxMessageSize, ISnmpMessage report)
: this(version, messageId, requestId, userName, OctetString.Empty, enterprise, time, variables, privacy, maxMessageSize, report)
{ }
///
/// Initializes a new instance of the class.
///
/// The version.
/// The message id.
/// The request id.
/// Name of the user.
/// The context name.
/// The enterprise.
/// The time.
/// The variables.
/// The privacy provider.
/// Size of the max message.
/// The report.
[CLSCompliant(false)]
public InformRequestMessage(VersionCode version, int messageId, int requestId, OctetString userName, OctetString contextName, ObjectIdentifier enterprise, uint time, IList variables, IPrivacyProvider privacy, int maxMessageSize, ISnmpMessage report)
{
if (userName == null)
{
throw new ArgumentNullException(nameof(userName));
}
if (variables == null)
{
throw new ArgumentNullException(nameof(variables));
}
if (version != VersionCode.V3)
{
throw new ArgumentException("Only v3 is supported.", nameof(version));
}
if (contextName == null)
{
throw new ArgumentNullException(nameof(contextName));
}
if (enterprise == null)
{
throw new ArgumentNullException(nameof(enterprise));
}
if (report == null)
{
throw new ArgumentNullException(nameof(report));
}
if (privacy == null)
{
throw new ArgumentNullException(nameof(privacy));
}
Version = version;
Privacy = privacy;
Enterprise = enterprise;
TimeStamp = time;
Header = new Header(new Integer32(messageId), new Integer32(maxMessageSize), privacy.ToSecurityLevel() | Levels.Reportable);
SecurityParameters parameters = report.Parameters;
IAuthenticationProvider authenticationProvider = Privacy.AuthenticationProvider;
Parameters = new SecurityParameters(
parameters.EngineId,
parameters.EngineBoots,
parameters.EngineTime,
userName,
authenticationProvider.CleanDigest,
Privacy.Salt);
InformRequestPdu pdu = new InformRequestPdu(
requestId,
enterprise,
time,
variables);
Scope scope = report.Scope;
OctetString contextEngineId = scope.ContextEngineId == OctetString.Empty ? parameters.EngineId : scope.ContextEngineId;
Scope = new Scope(contextEngineId, contextName, pdu);
Privacy.ComputeHash(Version, Header, Parameters, Scope);
_bytes = this.PackMessage(null).ToBytes();
}
internal InformRequestMessage(VersionCode version, Header header, SecurityParameters parameters, Scope scope, IPrivacyProvider privacy, byte[] length)
{
if (scope == null)
{
throw new ArgumentNullException(nameof(scope));
}
if (parameters == null)
{
throw new ArgumentNullException(nameof(parameters));
}
if (header == null)
{
throw new ArgumentNullException(nameof(header));
}
if (privacy == null)
{
throw new ArgumentNullException(nameof(privacy));
}
Version = version;
Header = header;
Parameters = parameters;
Scope = scope;
Privacy = privacy;
InformRequestPdu pdu = (InformRequestPdu)scope.Pdu;
Enterprise = pdu.Enterprise;
TimeStamp = pdu.TimeStamp;
_bytes = this.PackMessage(length).ToBytes();
}
///
/// Gets the privacy provider.
///
/// The privacy provider.
public IPrivacyProvider Privacy { get; private set; }
///
/// Gets the version.
///
/// The version.
public VersionCode Version { get; private set; }
///
/// Gets the time stamp.
///
/// The time stamp.
[CLSCompliant(false), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "TimeStamp")]
public uint TimeStamp { get; private set; }
///
/// Enterprise.
///
public ObjectIdentifier Enterprise { get; private set; }
///
/// Gets the header.
///
public Header Header { get; private set; }
///
/// Converts to byte format.
///
///
public byte[] ToBytes()
{
return _bytes;
}
///
/// Gets the parameters.
///
/// The parameters.
public SecurityParameters Parameters { get; private set; }
///
/// Gets the scope.
///
/// The scope.
public Scope Scope { get; private set; }
///
/// Returns a that represents this .
///
///
public override string ToString()
{
return string.Format(
CultureInfo.InvariantCulture,
"INFORM request message: time stamp: {0}; community: {1}; enterprise: {2}; varbind count: {3}",
TimeStamp.ToString(CultureInfo.InvariantCulture),
this.Community(),
Enterprise,
this.Variables().Count.ToString(CultureInfo.InvariantCulture));
}
}
}