Skip to content

Commit

Permalink
Corrected a few exceptions in Messenger class. Revised TRAP v2 in SNM…
Browse files Browse the repository at this point in the history
…P v3 processing in pipeline. Removed AuthenticationProviderExtension. Added new integration test cases.
  • Loading branch information
lextm committed Apr 23, 2017
1 parent e6e8072 commit f5a39ae
Show file tree
Hide file tree
Showing 110 changed files with 1,276 additions and 1,277 deletions.
2 changes: 1 addition & 1 deletion SharpSnmpLib/Messaging/GetBulkRequestMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public GetBulkRequestMessage(VersionCode version, int messageId, int requestId,
var contextEngineId = scope.ContextEngineId == OctetString.Empty ? parameters.EngineId : scope.ContextEngineId;
Scope = new Scope(contextEngineId, scope.ContextName, pdu);

authenticationProvider.ComputeHash(Version, Header, Parameters, Scope, Privacy);
Privacy.ComputeHash(Version, Header, Parameters, Scope);
_bytes = this.PackMessage(null).ToBytes();
}

Expand Down
30 changes: 15 additions & 15 deletions SharpSnmpLib/Messaging/GetNextRequestMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ namespace Lextm.SharpSnmpLib.Messaging
/// GETNEXT request message.
/// </summary>
public sealed class GetNextRequestMessage : ISnmpMessage
{
{
private readonly byte[] _bytes;

/// <summary>
/// Creates a <see cref="GetNextRequestMessage"/> with all contents.
/// </summary>
Expand All @@ -53,17 +53,17 @@ public GetNextRequestMessage(int requestId, VersionCode version, OctetString com
{
throw new ArgumentNullException("variables");
}

if (community == null)
{
throw new ArgumentNullException("community");
}

if (version == VersionCode.V3)
{
throw new ArgumentException("only v1 and v2c are supported", "version");
}

Version = version;
Header = Header.Empty;
Parameters = SecurityParameters.Create(community);
Expand Down Expand Up @@ -109,27 +109,27 @@ public GetNextRequestMessage(VersionCode version, int messageId, int requestId,
{
throw new ArgumentNullException("variables");
}

if (userName == null)
{
throw new ArgumentNullException("userName");
}

if (version != VersionCode.V3)
{
throw new ArgumentException("only v3 is supported", "version");
}

if (report == null)
{
throw new ArgumentNullException("report");
}

if (privacy == null)
{
throw new ArgumentNullException("privacy");
}

Version = version;
Privacy = privacy;

Expand All @@ -150,7 +150,7 @@ public GetNextRequestMessage(VersionCode version, int messageId, int requestId,
var contextEngineId = scope.ContextEngineId == OctetString.Empty ? parameters.EngineId : scope.ContextEngineId;
Scope = new Scope(contextEngineId, scope.ContextName, pdu);

authenticationProvider.ComputeHash(Version, Header, Parameters, Scope, Privacy);
Privacy.ComputeHash(Version, Header, Parameters, Scope);
_bytes = this.PackMessage(null).ToBytes();
}

Expand All @@ -160,17 +160,17 @@ internal GetNextRequestMessage(VersionCode version, Header header, SecurityParam
{
throw new ArgumentNullException("scope");
}

if (parameters == null)
{
throw new ArgumentNullException("parameters");
}

if (header == null)
{
throw new ArgumentNullException("header");
}

if (privacy == null)
{
throw new ArgumentNullException("privacy");
Expand All @@ -184,7 +184,7 @@ internal GetNextRequestMessage(VersionCode version, Header header, SecurityParam

_bytes = this.PackMessage(length).ToBytes();
}

/// <summary>
/// Gets the header.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion SharpSnmpLib/Messaging/GetRequestMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public GetRequestMessage(VersionCode version, int messageId, int requestId, Octe
var contextEngineId = scope.ContextEngineId == OctetString.Empty ? parameters.EngineId : scope.ContextEngineId;
Scope = new Scope(contextEngineId, scope.ContextName, pdu);

authenticationProvider.ComputeHash(Version, Header, Parameters, Scope, Privacy);
Privacy.ComputeHash(Version, Header, Parameters, Scope);
_bytes = this.PackMessage(null).ToBytes();
}

Expand Down
30 changes: 15 additions & 15 deletions SharpSnmpLib/Messaging/InformRequestMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,22 @@ public InformRequestMessage(int requestId, VersionCode version, OctetString comm
{
throw new ArgumentNullException("variables");
}

if (enterprise == null)
{
throw new ArgumentNullException("enterprise");
}

if (community == null)
{
throw new ArgumentNullException("community");
}

if (version == VersionCode.V3)
{
throw new ArgumentException("only v1 and v2c are supported", "version");
}

Version = version;
Enterprise = enterprise;
TimeStamp = time;
Expand Down Expand Up @@ -102,7 +102,7 @@ public InformRequestMessage(int requestId, VersionCode version, OctetString comm
[CLSCompliant(false)]
[Obsolete("Please use other overloading ones.")]
public InformRequestMessage(VersionCode version, int messageId, int requestId, OctetString userName, ObjectIdentifier enterprise, uint time, IList<Variable> variables, IPrivacyProvider privacy, ISnmpMessage report)
: this(version, messageId, requestId, userName, enterprise, time, variables, privacy, 0xFFE3, report)
: this(version, messageId, requestId, userName, enterprise, time, variables, privacy, 0xFFE3, report)
{
}

Expand All @@ -126,12 +126,12 @@ public InformRequestMessage(VersionCode version, int messageId, int requestId, O
{
throw new ArgumentNullException("userName");
}

if (variables == null)
{
throw new ArgumentNullException("variables");
}

if (version != VersionCode.V3)
{
throw new ArgumentException("only v3 is supported", "version");
Expand All @@ -146,7 +146,7 @@ public InformRequestMessage(VersionCode version, int messageId, int requestId, O
{
throw new ArgumentNullException("report");
}

if (privacy == null)
{
throw new ArgumentNullException("privacy");
Expand Down Expand Up @@ -176,7 +176,7 @@ public InformRequestMessage(VersionCode version, int messageId, int requestId, O
var contextEngineId = scope.ContextEngineId == OctetString.Empty ? parameters.EngineId : scope.ContextEngineId;
Scope = new Scope(contextEngineId, scope.ContextName, pdu);

authenticationProvider.ComputeHash(Version, Header, Parameters, Scope, Privacy);
Privacy.ComputeHash(Version, Header, Parameters, Scope);
_bytes = this.PackMessage(null).ToBytes();
}

Expand All @@ -186,17 +186,17 @@ internal InformRequestMessage(VersionCode version, Header header, SecurityParame
{
throw new ArgumentNullException("scope");
}

if (parameters == null)
{
throw new ArgumentNullException("parameters");
}

if (header == null)
{
throw new ArgumentNullException("header");
}

if (privacy == null)
{
throw new ArgumentNullException("privacy");
Expand All @@ -210,7 +210,7 @@ internal InformRequestMessage(VersionCode version, Header header, SecurityParame
var pdu = (InformRequestPdu)scope.Pdu;
Enterprise = pdu.Enterprise;
TimeStamp = pdu.TimeStamp;

_bytes = this.PackMessage(length).ToBytes();
}

Expand All @@ -237,12 +237,12 @@ internal InformRequestMessage(VersionCode version, Header header, SecurityParame
/// Enterprise.
/// </summary>
public ObjectIdentifier Enterprise { get; private set; }

/// <summary>
/// Gets the header.
/// </summary>
public Header Header { get; private set; }

/// <summary>
/// Converts to byte format.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion SharpSnmpLib/Messaging/MessageFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private static ISnmpMessage ParseMessage(int first, Stream stream, UserRegistry
throw new SnmpException(string.Format(CultureInfo.InvariantCulture, "invalid v3 packets scoped data: {0}", code));
}

if (!privacy.AuthenticationProvider.VerifyHash(version, header, parameters, body[3], privacy, body.GetLengthBytes()))
if (!privacy.VerifyHash(version, header, parameters, body[3], body.GetLengthBytes()))
{
parameters.IsInvalid = true;
}
Expand Down
28 changes: 23 additions & 5 deletions SharpSnmpLib/Messaging/Messenger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace Lextm.SharpSnmpLib.Messaging
/// Static methods in Manager or Agent class will be removed in the future.
/// </summary>
/// <remarks>
/// SNMP v3 is not supported by this class. Please use <see cref="ISnmpMessage" /> derived classes directly
/// SNMP v3 is not supported in many methods of this class. Please use <see cref="ISnmpMessage" /> derived classes directly
/// if you want to do v3 operations.
/// </remarks>
public static class Messenger
Expand Down Expand Up @@ -313,6 +313,7 @@ public static async Task<int> BulkWalkAsync(VersionCode version, IPEndPoint endp
/// <param name="specific">Specific code.</param>
/// <param name="timestamp">Timestamp.</param>
/// <param name="variables">Variable bindings.</param>
/// <remarks>This method only supports SNMP v1.</remarks>
[CLSCompliant(false)]
public static async Task SendTrapV1Async(EndPoint receiver, IPAddress agent, OctetString community, ObjectIdentifier enterprise, GenericCode generic, int specific, uint timestamp, IList<Variable> variables)
{
Expand All @@ -330,12 +331,13 @@ public static async Task SendTrapV1Async(EndPoint receiver, IPAddress agent, Oct
/// <param name="timestamp">Timestamp.</param>
/// <param name="variables">Variable bindings.</param>
/// <param name="requestId">Request ID.</param>
/// <remarks>This method only supports SNMP v2c.</remarks>
[CLSCompliant(false)]
public static async Task SendTrapV2Async(int requestId, VersionCode version, EndPoint receiver, OctetString community, ObjectIdentifier enterprise, uint timestamp, IList<Variable> variables)
{
if (version != VersionCode.V2)
{
throw new ArgumentException("Only SNMP v2c is supported", nameof(version));
throw new NotSupportedException("Only SNMP v2c is supported");
}

var message = new TrapV2Message(requestId, version, community, enterprise, timestamp, variables);
Expand All @@ -354,6 +356,7 @@ public static async Task SendTrapV2Async(int requestId, VersionCode version, End
/// <param name="variables">Variable bindings.</param>
/// <param name="privacy">The privacy provider.</param>
/// <param name="report">The report.</param>
/// <remarks>This method supports SNMP v2c and v3.</remarks>
[CLSCompliant(false)]
public static async Task SendInformAsync(int requestId, VersionCode version, IPEndPoint receiver, OctetString community, ObjectIdentifier enterprise, uint timestamp, IList<Variable> variables, IPrivacyProvider privacy, ISnmpMessage report)
{
Expand All @@ -377,6 +380,11 @@ public static async Task SendInformAsync(int requestId, VersionCode version, IPE
throw new ArgumentNullException(nameof(variables));
}

if (version == VersionCode.V1)
{
throw new NotSupportedException("SNMP v1 is not supported");
}

if (version == VersionCode.V3 && privacy == null)
{
throw new ArgumentNullException(nameof(privacy));
Expand Down Expand Up @@ -436,7 +444,7 @@ private static async Task<Tuple<bool, IList<Variable>, ISnmpMessage>> BulkHasNex
// TODO: report should be updated with latest message from agent.
if (version == VersionCode.V1)
{
throw new ArgumentException("v1 is not supported", nameof(version));
throw new NotSupportedException("SNMP v1 is not supported");
}

var variables = new List<Variable> { new Variable(seed.Id) };
Expand Down Expand Up @@ -700,6 +708,11 @@ public static int BulkWalk(VersionCode version, IPEndPoint endpoint, OctetString
throw new ArgumentNullException(nameof(list));
}

if (version == VersionCode.V1)
{
throw new NotSupportedException("SNMP v1 is not supported");
}

var tableV = new Variable(table);
var seed = tableV;
IList<Variable> next;
Expand Down Expand Up @@ -770,7 +783,7 @@ public static void SendTrapV2(int requestId, VersionCode version, EndPoint recei
{
if (version != VersionCode.V2)
{
throw new ArgumentException("Only SNMP v2c is supported", nameof(version));
throw new NotSupportedException("Only SNMP v2c is supported");
}

var message = new TrapV2Message(requestId, version, community, enterprise, timestamp, variables);
Expand Down Expand Up @@ -813,6 +826,11 @@ public static void SendInform(int requestId, VersionCode version, IPEndPoint rec
throw new ArgumentNullException(nameof(variables));
}

if (version == VersionCode.V1)
{
throw new NotSupportedException("SNMP v1 is not supported");
}

if (version == VersionCode.V3 && privacy == null)
{
throw new ArgumentNullException(nameof(privacy));
Expand Down Expand Up @@ -873,7 +891,7 @@ private static bool BulkHasNext(VersionCode version, IPEndPoint receiver, OctetS
{
if (version == VersionCode.V1)
{
throw new ArgumentException("v1 is not supported", nameof(version));
throw new NotSupportedException("SNMP v1 is not supported");
}

var variables = new List<Variable> { new Variable(seed.Id) };
Expand Down
10 changes: 5 additions & 5 deletions SharpSnmpLib/Messaging/ReportMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,22 @@ public ReportMessage(VersionCode version, Header header, SecurityParameters para
{
throw new ArgumentNullException("scope");
}

if (parameters == null)
{
throw new ArgumentNullException("parameters");
}

if (header == null)
{
throw new ArgumentNullException("header");
}

if (privacy == null)
{
throw new ArgumentNullException("privacy");
}

if (version != VersionCode.V3)
{
throw new ArgumentException("only v3 is supported", "version");
Expand All @@ -79,7 +79,7 @@ public ReportMessage(VersionCode version, Header header, SecurityParameters para
Parameters = parameters;
Scope = scope;
Privacy = privacy;
Privacy.AuthenticationProvider.ComputeHash(Version, Header, Parameters, Scope, Privacy);
Privacy.ComputeHash(Version, Header, Parameters, Scope);
_bytes = this.PackMessage(length).ToBytes();
}

Expand Down
2 changes: 1 addition & 1 deletion SharpSnmpLib/Messaging/ResponseMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public ResponseMessage(VersionCode version, Header header, SecurityParameters pa

if (needAuthentication)
{
Privacy.AuthenticationProvider.ComputeHash(Version, Header, Parameters, Scope, Privacy);
Privacy.ComputeHash(Version, Header, Parameters, Scope);
}

_bytes = this.PackMessage(length).ToBytes();
Expand Down
Loading

0 comments on commit f5a39ae

Please sign in to comment.