public void TestConstructor() { ObjectIdentifier oid = new ObjectIdentifier(new byte[] { 0x2B, 0x06, 0x99, 0x37 }); Assert.AreEqual(new uint[] { 1, 3, 6, 3255 }, oid.ToNumerical()); var o = ObjectIdentifier.Create(new uint[] {1, 3, 6}, 3255); Assert.AreEqual(oid, o); }
public static string GetFullNameFromOid(ObjectIdentifier input, bool includeNumerical = true) { var oidParts = input.ToNumerical(); List <string> parts = new List <string>(); // if (includeNumerical) // parts.AddRange(oidParts.Take(2).Select((p) => p.ToString())); for (int i = 1; i < oidParts.Length; ++i) { var curOid = new ObjectIdentifier(oidParts.Take(i + 1).ToArray()); if (_Oids.ContainsKey(curOid)) { parts.Add(_Oids[curOid].Name); } else if (includeNumerical) { parts.Add(oidParts[i].ToString()); } } if (parts.Any()) { return(string.Join(".", parts)); } return(string.Join(".", oidParts)); }
public void TestToBytes() { List <Variable> vList = new List <Variable>(); vList.Add(new Variable( new ObjectIdentifier(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1001, 21, 0 }), new OctetString("TrapTest"))); Sequence a = Variable.Transform(vList); byte[] bytes = a.ToBytes(); ISnmpData data = DataFactory.CreateSnmpData(bytes); Assert.AreEqual(SnmpType.Sequence, data.TypeCode); Sequence array = (Sequence)data; Assert.AreEqual(1, array.Count); ISnmpData item = array[0]; Assert.AreEqual(SnmpType.Sequence, item.TypeCode); Sequence v = (Sequence)item; Assert.AreEqual(2, v.Count); Assert.AreEqual(SnmpType.ObjectIdentifier, v[0].TypeCode); ObjectIdentifier o = (ObjectIdentifier)v[0]; Assert.AreEqual(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1001, 21, 0 }, o.ToNumerical()); Assert.AreEqual(SnmpType.OctetString, v[1].TypeCode); Assert.AreEqual("TrapTest", v[1].ToString()); }
public void TestToBytes() { List <Variable> vList = new List <Variable> { new Variable( new ObjectIdentifier(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1001, 21, 0 }), new OctetString("TrapTest")) }; Sequence a = Variable.Transform(vList); Assert.Throws <ArgumentNullException>(() => a.AppendBytesTo(null)); Assert.AreEqual("SNMP SEQUENCE: SNMP SEQUENCE: .1.3.6.1.4.1.2162.1001.21.0; TrapTest; ; ", a.ToString()); byte[] bytes = a.ToBytes(); ISnmpData data = DataFactory.CreateSnmpData(bytes); Assert.AreEqual(SnmpType.Sequence, data.TypeCode); Sequence array = (Sequence)data; Assert.AreEqual(1, array.Length); ISnmpData item = array[0]; Assert.AreEqual(SnmpType.Sequence, item.TypeCode); Sequence v = (Sequence)item; Assert.AreEqual(2, v.Length); Assert.AreEqual(SnmpType.ObjectIdentifier, v[0].TypeCode); ObjectIdentifier o = (ObjectIdentifier)v[0]; Assert.AreEqual(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1001, 21, 0 }, o.ToNumerical()); Assert.AreEqual(SnmpType.OctetString, v[1].TypeCode); Assert.AreEqual("TrapTest", v[1].ToString()); }
public void TestConstructor() { ObjectIdentifier oid = new ObjectIdentifier(new byte[] { 0x2B, 0x06, 0x99, 0x37 }); Assert.Equal(new uint[] { 1, 3, 6, 3255 }, oid.ToNumerical()); var o = ObjectIdentifier.Create(new uint[] { 1, 3, 6 }, 3255); Assert.Equal(oid, o); }
/// <summary> /// Validates if an <see cref="ObjectIdentifier"/> is a table. /// </summary> /// <param name="identifier">The object identifier.</param> /// <returns></returns> public bool ValidateTable(ObjectIdentifier identifier) { if (identifier == null) { throw new ArgumentNullException("identifier"); } return(IsTableId(identifier.ToNumerical())); }
/// <summary> /// Validates if an <see cref="ObjectIdentifier"/> is a table. /// </summary> /// <param name="identifier">The object identifier.</param> /// <returns></returns> public bool ValidateTable(ObjectIdentifier identifier) { if (identifier == null) { throw new ArgumentNullException("identifier"); } return IsTableId(identifier.ToNumerical()); }
public void TestCreateObjectIdentifier2() { byte[] expected = new Byte[] { 0x06, 0x01, 0x00 }; ISnmpData data = DataFactory.CreateSnmpData(expected); Assert.Equal(SnmpType.ObjectIdentifier, data.TypeCode); ObjectIdentifier o = (ObjectIdentifier)data; Assert.Equal(new uint[] { 0, 0 }, o.ToNumerical()); }
public void TestCreateObjectIdentifier() { byte[] expected = new byte[] { 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x90, 0x72, 0x87, 0x68, 0x02 }; ISnmpData data = DataFactory.CreateSnmpData(expected); Assert.Equal(SnmpType.ObjectIdentifier, data.TypeCode); ObjectIdentifier o = (ObjectIdentifier)data; Assert.Equal(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1000, 2 }, o.ToNumerical()); }
static bool ParseNode(MIBNode parent, XmlNode node, ObjectIdentifier baseOid) { var oidParts = baseOid.ToNumerical(); var oid = ObjectIdentifier.Create(oidParts, uint.Parse(node.Attributes.GetNamedItem("id").Value)); var name = node.Attributes.GetNamedItem("name").Value; var mnode = new MIBNode { Name = name, Oid = oid, Parent = parent }; var typeStr = node.Attributes.GetNamedItem("type").Value.Replace("[]", "Array"); mnode.Type = (MIBType)Enum.Parse(typeof(MIBType), typeStr, true); _Oids[oid] = mnode; if (parent != null) { parent.AddChild(mnode); } foreach (var child in node.ChildNodes.OfType <XmlNode>().Where((n) => n.Name == "symbol")) { mnode.AddValueMapping(int.Parse(child.Attributes.GetNamedItem("value").Value), child.Attributes.GetNamedItem("name").Value); } foreach (var child in node.ChildNodes.OfType <XmlNode>().Where((n) => n.Name == "node")) { if (!ParseNode(mnode, child, oid)) { return(false); } } return(true); }
public void TestVarbind() { byte[] expected = new byte[] { 0x30, 0x17, 0x06, 0x0B, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x90, 0x72, 0x87, 0x69, 0x15, 0x00, 0x04, 0x08, 0x54, 0x72, 0x61, 0x70, 0x54, 0x65, 0x73, 0x74 }; ISnmpData data = DataFactory.CreateSnmpData(expected); Assert.Equal(SnmpType.Sequence, data.TypeCode); Sequence a = (Sequence)data; Assert.Equal(2, a.Length); ISnmpData oid = a[0]; ISnmpData name = a[1]; Assert.Equal(SnmpType.ObjectIdentifier, oid.TypeCode); Assert.Equal(SnmpType.OctetString, name.TypeCode); ObjectIdentifier o = (ObjectIdentifier)oid; Assert.Equal(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1001, 21, 0 }, o.ToNumerical()); OctetString s = (OctetString)name; Assert.Equal("TrapTest", s.ToString()); }
public void TestConstructor2() { ObjectIdentifier oid = new ObjectIdentifier(new byte[] { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x90, 0x72, 0x87, 0x68, 0x02 }); Assert.Equal(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1000, 2 }, oid.ToNumerical()); }
public void TestConstructor() { ObjectIdentifier oid = new ObjectIdentifier(new byte[] { 0x2B, 0x06, 0x99, 0x37 }); Assert.AreEqual(new uint[] { 1, 3, 6, 3255 }, oid.ToNumerical()); }
public static string GetTextual(ObjectIdentifier id, IObjectRegistry registry) { if (id == null) { throw new ArgumentNullException("id"); } IObjectRegistry objects = registry ?? ObjectRegistry.Default; return objects.Translate(id.ToNumerical()); }
public static string GetStringOf(ObjectIdentifier id, IObjectRegistry objects) { if (id == null) { throw new ArgumentNullException("id"); } if (objects == null) { return id.ToString(); } string result = objects.Tree.Search(id.ToNumerical()).AlternativeText; return string.IsNullOrEmpty(result) ? id.ToString() : result; }
public static DataTable ReadTable( VersionCode version, IPEndPoint endpoint, OctetString community, ObjectIdentifier table, int timeout, params uint[][] colonnesAPrendre) { string strOIDTable = table.ToString(); List <Variable> lstVariables = new List <Variable>(); int nResult = 0; if (colonnesAPrendre == null || colonnesAPrendre.Length == 0) { nResult = Walk(version, endpoint, community, table, lstVariables, timeout, WalkMode.WithinSubtree); } else { foreach (uint[] oidCol in colonnesAPrendre) { List <Variable> lstTmp = new List <Variable>(); nResult = Walk(version, endpoint, community, new ObjectIdentifier(oidCol), lstTmp, timeout, WalkMode.WithinSubtree); lstVariables.AddRange(lstTmp); } } //Création de la structure de la table DataTable dataTable = new DataTable(strOIDTable); DataColumn colIndex = new DataColumn("Index", typeof(string)); dataTable.Columns.Add(colIndex); uint[] nOidTable = table.ToNumerical(); Dictionary <string, DataRow> dicRows = new Dictionary <string, DataRow>(); foreach (Variable variable in lstVariables) { string strOIDVariable = ""; string strIndex = ""; uint[] nOidVariable = variable.Id.ToNumerical(); for (int nVar = 0; nVar < nOidVariable.Length; nVar++) { if (nVar <= nOidTable.Length + 1) { strOIDVariable += "." + nOidVariable[nVar]; } else { strIndex += nOidVariable[nVar] + "."; } } if (strIndex.Length > 0) { strIndex = strIndex.Substring(0, strIndex.Length - 1); } DataColumn col = dataTable.Columns[strOIDVariable]; if (col == null) { col = new DataColumn(strOIDVariable, variable.Data.GetTypeDotNet()); dataTable.Columns.Add(col); } DataRow row = null; if (!dicRows.TryGetValue(strIndex, out row)) { row = dataTable.NewRow(); row["Index"] = strIndex; dicRows[strIndex] = row; dataTable.Rows.Add(row); } row[strOIDVariable] = variable.Data.ConvertToTypeDotNet(); } return(dataTable); }
public void TestConstructor2() { ObjectIdentifier oid = new ObjectIdentifier(new byte[] { 0x2B, 0x06, 0x01, 0x04, 0x01, 0x90, 0x72, 0x87, 0x68, 0x02 }); Assert.AreEqual(new uint[] { 1, 3, 6, 1, 4, 1, 2162, 1000, 2 }, oid.ToNumerical()); }
/// <summary> /// Validates if an <see cref="ObjectIdentifier"/> is a table. /// </summary> /// <param name="identifier">The object identifier.</param> /// <returns></returns> public bool ValidateTable(ObjectIdentifier identifier) { if (identifier == null) { throw new ArgumentNullException("identifier"); } try { return IsTableId(identifier.ToNumerical()); } catch (ArgumentOutOfRangeException) { // if no matching definition found, refuse to continue. return false; } }