// Response 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/5/1
* Time: 18:13
*
* 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
{
///
/// Response message.
///
public sealed class ResponseMessage : ISnmpMessage
{
private readonly byte[] _bytes;
///
/// Creates a with all contents.
///
/// Request ID.
/// Protocol version.
/// Community name.
/// Error code.
/// Error index.
/// Variables.
public ResponseMessage(int requestId, VersionCode version, OctetString community, ErrorCode error, int index, IList variables)
{
if (variables == null)
{
throw new ArgumentNullException(nameof(variables));
}
if (community == null)
{
throw new ArgumentNullException(nameof(community));
}
if (version == VersionCode.V3)
{
throw new ArgumentException("Please use overload constructor for v3.", nameof(version));
}
Version = version;
Header = Header.Empty;
Parameters = SecurityParameters.Create(community);
ResponsePdu pdu = new ResponsePdu(
requestId,
error,
index,
variables);
Scope = new Scope(pdu);
Privacy = DefaultPrivacyProvider.DefaultPair;
_bytes = this.PackMessage(null).ToBytes();
}
///
/// Initializes a new instance of the class.
///
/// The version.
/// The header.
/// The parameters.
/// The scope.
/// The privacy provider.
/// if set to true, authentication is needed.
/// The length bytes.
public ResponseMessage(VersionCode version, Header header, SecurityParameters parameters, Scope scope, IPrivacyProvider privacy, bool needAuthentication, 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;
if (needAuthentication)
{
Privacy.ComputeHash(Version, Header, Parameters, Scope);
}
_bytes = this.PackMessage(length).ToBytes();
}
///
/// Gets the header.
///
public Header Header { get; private set; }
///
/// Gets the privacy provider.
///
/// The privacy provider.
public IPrivacyProvider Privacy { get; private set; }
///
/// Error status.
///
public ErrorCode ErrorStatus
{
get { return Scope.Pdu.ErrorStatus.ToErrorCode(); }
}
///
/// Error index.
///
public int ErrorIndex
{
get { return Scope.Pdu.ErrorIndex.ToInt32(); }
}
///
/// Gets the version.
///
/// The version.
public VersionCode Version { get; private set; }
///
/// Gets the parameters.
///
/// The parameters.
public SecurityParameters Parameters { get; private set; }
///
/// Gets the scope.
///
/// The scope.
public Scope Scope { get; private set; }
///
/// Converts to byte format.
///
///
public byte[] ToBytes()
{
return _bytes;
}
///
/// Returns a that represents this .
///
///
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "Response message: version: {0}; {1}; {2}", Version, Parameters.UserName, Scope.Pdu);
}
}
}