// Engine group class.
// Copyright (C) 2009-2010 Lex Li
//
// 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.
using System;
using GSF.Net.Snmp.Messaging;
namespace GSF.Net.Snmp.Pipeline
{
///
/// Engine group which contains all related objects.
///
public sealed class EngineGroup
{
// TODO: make engine ID configurable from outside and unique.
private readonly OctetString _engineId =
new OctetString(new byte[] { 128, 0, 31, 136, 128, 233, 99, 0, 0, 214, 31, 244 });
private readonly DateTime _start;
private uint _counterNotInTimeWindow;
private uint _counterUnknownEngineId;
private uint _counterUnknownUserName;
private uint _counterDecryptionError;
private uint _counterUnknownSecurityLevel;
private uint _counterAuthenticationFailure;
///
/// Initializes a new instance of the class.
///
public EngineGroup()
{
_start = DateTime.UtcNow;
}
///
/// Gets the engine id.
///
/// The engine id.
internal OctetString EngineId
{
get { return _engineId; }
}
///
/// Gets or sets the engine boots.
///
/// The engine boots.
[Obsolete("Please use EngineTimeData")]
internal int EngineBoots { get; set; }
///
/// Gets the engine time.
///
/// The engine time.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
[Obsolete("Please use EngineTimeData")]
public int EngineTime { get; set; }
///
/// Gets the engine time data.
///
///
/// The engine time data. [0] is engine boots, [1] is engine time.
///
public int[] EngineTimeData
{
get
{
DateTime now = DateTime.UtcNow;
long seconds = (now - _start).Ticks / 10000000;
int engineTime = (int)(seconds % int.MaxValue);
int engineReboots = (int)(seconds / int.MaxValue);
return new[] { engineReboots, engineTime };
}
}
///
/// Verifies if the request comes in time.
///
/// The current time data.
/// The past reboots.
/// The past time.
///
/// true if the request is in time window; otherwise, false.
///
public static bool IsInTime(int[] currentTimeData, int pastReboots, int pastTime)
{
int currentReboots = currentTimeData[0];
int currentTime = currentTimeData[1];
// TODO: RFC 2574 page 27
if (currentReboots == int.MaxValue)
{
return false;
}
if (currentReboots != pastReboots)
{
return false;
}
if (currentTime == pastTime)
{
return true;
}
return Math.Abs(currentTime - pastTime) <= 150;
}
///
/// Gets not-in-time-window counter.
///
///
/// Counter variable.
///
public Variable NotInTimeWindow
{
get
{
return new Variable(Messenger.NotInTimeWindow, new Counter32(_counterNotInTimeWindow++));
}
}
///
/// Gets unknown engine ID counter.
///
///
/// Counter variable.
///
public Variable UnknownEngineId
{
get
{
return new Variable(Messenger.UnknownEngineId, new Counter32(_counterUnknownEngineId++));
}
}
///
/// Gets unknown security name counter.
///
public Variable UnknownSecurityName
{
get
{
return new Variable(Messenger.UnknownSecurityName, new Counter32(_counterUnknownUserName++));
}
}
///
/// Gets decryption error counter.
///
public Variable DecryptionError
{
get
{
return new Variable(Messenger.DecryptionError, new Counter32(_counterDecryptionError++));
}
}
///
/// Gets unsupported security level counter.
///
public Variable UnsupportedSecurityLevel
{
get
{
return new Variable(Messenger.UnsupportedSecurityLevel, new Counter32(_counterUnknownSecurityLevel++));
}
}
///
/// Gets authentication failure counter.
///
public Variable AuthenticationFailure
{
get
{
return new Variable(Messenger.AuthenticationFailure, new Counter32(_counterAuthenticationFailure++));
}
}
}
}