2019-03-04 06:50:05 +01:00
|
|
|
|
// /**
|
|
|
|
|
// * File: Variable.cs
|
|
|
|
|
// * Author: haraldwolff
|
|
|
|
|
// *
|
|
|
|
|
// * This file and it's content is copyrighted by the Author and / or copyright holder.
|
|
|
|
|
// * Any use wihtout proper permission is illegal and may lead to legal actions.
|
|
|
|
|
// *
|
|
|
|
|
// *
|
|
|
|
|
// **/
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2019-03-11 08:57:19 +01:00
|
|
|
|
using ln.snmp.asn1;
|
|
|
|
|
using ln.logging;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.CodeDom;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Linq;
|
2019-03-04 06:50:05 +01:00
|
|
|
|
namespace ln.snmp.types
|
|
|
|
|
{
|
|
|
|
|
public abstract class Variable
|
|
|
|
|
{
|
|
|
|
|
public Identifier Identifier { get; private set; }
|
|
|
|
|
|
|
|
|
|
public Variable(Identifier identifier)
|
|
|
|
|
{
|
|
|
|
|
Identifier = identifier;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract byte[] Bytes { get; set; }
|
|
|
|
|
public abstract object Value { get; set; }
|
|
|
|
|
|
2019-03-11 08:57:19 +01:00
|
|
|
|
public void Dump()
|
2019-03-04 06:50:05 +01:00
|
|
|
|
{
|
2019-03-11 08:57:19 +01:00
|
|
|
|
Dump("");
|
2019-03-04 06:50:05 +01:00
|
|
|
|
}
|
2019-03-11 08:57:19 +01:00
|
|
|
|
public void Dump(string prefix)
|
2019-03-04 12:06:45 +01:00
|
|
|
|
{
|
2019-03-11 08:57:19 +01:00
|
|
|
|
if (this is AbstractSequence)
|
|
|
|
|
{
|
|
|
|
|
AbstractSequence sequence = this as AbstractSequence;
|
|
|
|
|
Logging.Log(LogLevel.DEBUG, "{0}{1}", prefix, this.ToString());
|
|
|
|
|
foreach (Variable item in sequence.Items)
|
|
|
|
|
{
|
|
|
|
|
if (item != null)
|
|
|
|
|
item.Dump(prefix + " ");
|
|
|
|
|
else
|
|
|
|
|
Logging.Log(LogLevel.DEBUG, "{0}NULL", prefix);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Logging.Log(LogLevel.DEBUG, "{0}{1} {2}", prefix, this.ToString(), BitConverter.ToString(this.Bytes));
|
|
|
|
|
}
|
2019-03-04 12:06:45 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-03-04 06:50:05 +01:00
|
|
|
|
|
2019-03-11 08:57:19 +01:00
|
|
|
|
public static implicit operator ASN1Value(Variable variable)
|
2019-03-04 12:06:45 +01:00
|
|
|
|
{
|
2019-03-11 08:57:19 +01:00
|
|
|
|
ASN1Value value = new ASN1Value(variable.Identifier);
|
|
|
|
|
if (variable is AbstractSequence)
|
|
|
|
|
{
|
|
|
|
|
AbstractSequence sequence = variable as AbstractSequence;
|
|
|
|
|
foreach (Variable item in sequence.Items)
|
|
|
|
|
value.Add(item);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
value.Bytes = variable.Bytes;
|
|
|
|
|
}
|
|
|
|
|
return value;
|
2019-03-04 06:50:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-11 08:57:19 +01:00
|
|
|
|
public static implicit operator Variable(ASN1Value value)
|
2019-03-04 06:50:05 +01:00
|
|
|
|
{
|
2019-03-11 08:57:19 +01:00
|
|
|
|
try
|
2019-03-04 06:50:05 +01:00
|
|
|
|
{
|
2019-03-11 08:57:19 +01:00
|
|
|
|
if (knownTypes.ContainsKey(value.Identifier))
|
2019-03-04 06:50:05 +01:00
|
|
|
|
{
|
2019-03-11 08:57:19 +01:00
|
|
|
|
Type type = knownTypes[value.Identifier];
|
|
|
|
|
|
|
|
|
|
//Logging.Log(LogLevel.DEBUGDETAIL, "ASN1Value->Variable: {0} -> {1}", value, type.Name);
|
|
|
|
|
|
|
|
|
|
Variable variable = Activator.CreateInstance(type,true) as Variable;
|
|
|
|
|
if (variable is AbstractSequence)
|
|
|
|
|
{
|
|
|
|
|
AbstractSequence sequence = variable as AbstractSequence;
|
|
|
|
|
sequence.Items = value.Items.Select((x) => (Variable)x).ToArray();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
variable.Bytes = value.Bytes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return variable;
|
2019-03-04 12:06:45 +01:00
|
|
|
|
}
|
2019-03-11 08:57:19 +01:00
|
|
|
|
throw new ArgumentException(String.Format("{0}",value.Identifier),nameof(value));
|
2019-03-04 12:06:45 +01:00
|
|
|
|
}
|
2019-03-11 08:57:19 +01:00
|
|
|
|
catch (Exception e)
|
2019-03-04 12:06:45 +01:00
|
|
|
|
{
|
2019-03-11 08:57:19 +01:00
|
|
|
|
Logging.Log(e);
|
|
|
|
|
throw e;
|
2019-03-04 06:50:05 +01:00
|
|
|
|
}
|
2019-03-11 08:57:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 06:50:05 +01:00
|
|
|
|
|
2019-03-11 08:57:19 +01:00
|
|
|
|
static Dictionary<Identifier, Type> knownTypes = new Dictionary<Identifier, Type>();
|
|
|
|
|
static bool __initialized__ = initialize();
|
|
|
|
|
|
|
|
|
|
static void registerKnownType<T>() where T:Variable, new()
|
|
|
|
|
{
|
|
|
|
|
T i = new T();
|
|
|
|
|
knownTypes.Add(i.Identifier, typeof(T));
|
2019-03-04 06:50:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-11 08:57:19 +01:00
|
|
|
|
static bool initialize()
|
|
|
|
|
{
|
|
|
|
|
registerKnownType<NullValue>();
|
|
|
|
|
registerKnownType<Boolean>();
|
|
|
|
|
registerKnownType<Integer>();
|
|
|
|
|
registerKnownType<OctetString>();
|
|
|
|
|
registerKnownType<Unsigned32>();
|
2019-03-18 08:12:58 +01:00
|
|
|
|
registerKnownType<Gauge32>();
|
|
|
|
|
registerKnownType<TimeTicks>();
|
2019-03-11 08:57:19 +01:00
|
|
|
|
registerKnownType<Counter32>();
|
|
|
|
|
registerKnownType<Counter64>();
|
|
|
|
|
registerKnownType<Sequence>();
|
|
|
|
|
registerKnownType<ObjectIdentifier>();
|
|
|
|
|
|
2019-03-12 00:54:39 +01:00
|
|
|
|
registerKnownType<IPAddr>();
|
|
|
|
|
|
2019-03-11 08:57:19 +01:00
|
|
|
|
registerKnownType<GetRequest>();
|
|
|
|
|
registerKnownType<GetNextRequest>();
|
|
|
|
|
registerKnownType<GetResponse>();
|
|
|
|
|
registerKnownType<GetBulkRequest>();
|
|
|
|
|
registerKnownType<Report>();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2019-03-04 06:50:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-12 00:54:39 +01:00
|
|
|
|
public static class VariableExtensions
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public static Variable[] ToVariableArray(this ASN1Value[] me)
|
|
|
|
|
{
|
|
|
|
|
return me.Select((x) => (Variable)x).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 06:50:05 +01:00
|
|
|
|
}
|