/// <summary> /// FUNCTION: Close /// PURPOSE : To close a session /// </summary> public bool Close() { bool Success = false; // init success to failure try { SNMPAPI_STATUS Result = new SNMPAPI_STATUS(); // set up a result item // close the session Result = SnmpAPI.SnmpClose(Session); if (Result != SNMPAPI_STATUS.SNMPAPI_SUCCESS) { throw new Exception("SNMP Close Error"); } // call clean up for garbage collection Result = SnmpAPI.SnmpCleanup(); if (Result != SNMPAPI_STATUS.SNMPAPI_SUCCESS) { throw new Exception("SNMP Ternination Error"); } Success = true; } catch (Exception Err) { ErrMess = Err.Message; } return(Success); // return success flag }
public static Hashtable VblToHashTable(IntPtr vbl) { int count = 0; ArrayList al = new ArrayList(); SMIOID name = new SMIOID(); SMIVALUE val = new SMIVALUE(); Hashtable ht = new Hashtable(); SNMPAPI_STATUS rc; // // Get the variable count and set the capacity of the arraylist. This // speeds our processing as there's no need to expand the list as // we add entries. // rc = SnmpAPI.SnmpCountVbl(vbl); if (rc != SNMPAPI_STATUS.SNMPAPI_FAILURE) { count = (int)rc; } al.Capacity = count; for (int i = 1; i <= count; i++) { rc = SnmpAPI.SnmpGetVb(vbl, i, ref name, ref val); if (rc != SNMPAPI_STATUS.SNMPAPI_SUCCESS) { break; } // // Build the variable name // string soid = OidToString(ref name); string v = VbToString(ref val); if (soid != null) { if (ht.ContainsKey(soid)) { ht[soid] = ht[soid] + " " + v; } else { ht.Add(soid, v); } } } return(ht); }
public static ArrayList VblToArrayList(IntPtr vbl) { int count = 0; ArrayList al = new ArrayList(); SMIOID name = new SMIOID(); SMIVALUE val = new SMIVALUE(); SNMPAPI_STATUS rc; // // Get the variable count and set the capacity of the arraylist. This // speeds our processing as there's no need to expand the list as // we add entries. // rc = SnmpAPI.SnmpCountVbl(vbl); if (rc != SNMPAPI_STATUS.SNMPAPI_FAILURE) { count = (int)rc; } al.Capacity = count; for (int i = 1; i <= count; i++) { rc = SnmpAPI.SnmpGetVb(vbl, i, ref name, ref val); if (rc != SNMPAPI_STATUS.SNMPAPI_SUCCESS) { break; } // // Build the variable name // string soid = OidToString(ref name); string v = VbToString(ref val); if (soid != null) { al.Add(soid + oidDelimiter + v + oidDelimiter); } } return(al); }
public static string OidToString(ref SMIOID oid) { string soid = null; IntPtr OTSBuffer = Marshal.AllocHGlobal(1408); try { SNMPAPI_STATUS rc = SnmpAPI.SnmpOidToStr(ref oid, 1408, OTSBuffer); if (rc != SNMPAPI_STATUS.SNMPAPI_FAILURE) { soid = Marshal.PtrToStringAnsi(OTSBuffer); } SnmpAPI.SnmpFreeDescriptor((int)SNMPAPI_SYNTAX.SNMP_SYNTAX_OID, ref oid); } catch (Exception e) { Console.WriteLine("OidToString Exception:\n" + e.Message + "\n" + e.StackTrace); } //Free Marshal.FreeHGlobal(OTSBuffer); return(soid); }
public bool Open(string RemoteMachine) { bool Success = false; // init success to failure try { SNMPAPI_STATUS Result = new SNMPAPI_STATUS(); // set up a result item // this is our call-back function SnmpAPI.SnmpCallback SnmpCB = new SnmpAPI.SnmpCallback(OnSnmpMessage); // get the local and remote ip addresses this.LocalAddr = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0].ToString(); this.RemoteAddr = Dns.GetHostEntry(RemoteMachine).AddressList[0].ToString(); // initialize the snmp api system Result = SnmpAPI.SnmpStartup(out major, out minor, out level, out translate, out retran); if (Result != SNMPAPI_STATUS.SNMPAPI_SUCCESS) { throw new Exception("SNMP Initialization Error"); } // create the session Session = SnmpAPI.SnmpCreateSession(IntPtr.Zero, 0, SnmpCB, IntPtr.Zero); if (Result == SNMPAPI_STATUS.SNMPAPI_FAILURE) { throw new Exception("SNMP Create Session Error"); } Success = true; } catch (Exception Err) { ErrMess = Err.Message; } return(Success); // return success flag }
/// <summary> /// FUNCTION: Send /// PURPOSE : To send a pdu in order to set the MIB values /// </summary> public bool Send() { bool Success = false; // init success to failure try { if (VBLInit) { if (Vbl.Item.Count > 0) { SNMPAPI_STATUS Result = new SNMPAPI_STATUS(); // set up a result item // create entities for the source and destination addresses IntPtr Source = SnmpAPI.SnmpStrToEntity(Session, LocalAddr); IntPtr Destin = SnmpAPI.SnmpStrToEntity(Session, RemoteAddr); // create arrays to hold Variable Info SMIOID[] OID = new SMIOID[Vbl.Item.Count]; // an array of oids SMIVALUE[] Val = new SMIVALUE[Vbl.Item.Count]; // an array of values SMIOCTETS[] Oct = new SMIOCTETS[Vbl.Item.Count]; // an array of octets SMIOCTETS DestOct = new SMIOCTETS(); // the destination octect GCHandle[] PinnedArray = new GCHandle[Vbl.Item.Count]; // an array of pinned memory byte[][] ba = new byte[Vbl.Item.Count][]; // a multi-dimensional byte array IntPtr VbL1 = new IntPtr(); // pointer to variable bindings list IntPtr Context = new IntPtr(); // pointer to a context object // start looping through all items in the variable bindings list for (int i = 0; i < Vbl.Item.Count; i++) { ba[i] = new byte[((VARIABLE_ITEM)Vbl.Item[i]).Value.Length]; // new byte array // check to see if the user has passed the separator if (Vbl.ParentEntity.Substring(Vbl.ParentEntity.Length - 1, 1) != "." && ((VARIABLE_ITEM)Vbl.Item[i]).Entity.Substring(0, 1) != ".") { Result = SnmpAPI.SnmpStrToOid(Vbl.ParentEntity + "." + ((VARIABLE_ITEM)Vbl.Item[i]).Entity, ref OID[i]); } else { Result = SnmpAPI.SnmpStrToOid(Vbl.ParentEntity + ((VARIABLE_ITEM)Vbl.Item[i]).Entity, ref OID[i]); } // check result if (Result == SNMPAPI_STATUS.SNMPAPI_FAILURE) { throw new Exception("SNMP OID Creation Failure"); } // create the octet and value for this variable Oct[i].size = (uint)((VARIABLE_ITEM)Vbl.Item[i]).Value.Length; // set the octet size ba[i] = Encoding.ASCII.GetBytes(((VARIABLE_ITEM)Vbl.Item[i]).Value); // encode string to byte array PinnedArray[i] = GCHandle.Alloc(ba[i], GCHandleType.Pinned); // this creates a static memory location Oct[i].octets = PinnedArray[i].AddrOfPinnedObject(); // set the octet pointer // now, check what type of variable this is and set the value accordingly switch (((VARIABLE_ITEM)Vbl.Item[i]).Type) { case VARIABLE_TYPE.VARIABLE_TYPE_INT: Val[i].type = SNMPAPI_SYNTAX.SNMP_SYNTAX_INT; Val[i].val.sNumber = int.Parse(((VARIABLE_ITEM)Vbl.Item[i]).Value); break; case VARIABLE_TYPE.VARIABLE_TYPE_INT32: Val[i].type = SNMPAPI_SYNTAX.SNMP_SYNTAX_INT32; Val[i].val.hNumber = long.Parse(((VARIABLE_ITEM)Vbl.Item[i]).Value); break; case VARIABLE_TYPE.VARIABLE_TYPE_OCTECT: Val[i].type = SNMPAPI_SYNTAX.SNMP_SYNTAX_OCTETS; Val[i].val.str = Oct[i]; break; } // check to see if this is the first item, or an item to append if (i == 0) { VbL1 = SnmpAPI.SnmpCreateVbl(Session, ref OID[i], ref Val[i]); } else { Result = SnmpAPI.SnmpSetVb(VbL1, 0, ref OID[i], ref Val[i]); } } // now, set up the protocol description unit IntPtr Pdu = SnmpAPI.SnmpCreatePdu(Session, (int)SNMPAPI_PDU.SNMP_PDU_SET, Vbl.RequestID, 0, 0, VbL1); // set the destination octet (we only need to set the first one, the rest will flow accordingly) DestOct.size = (uint)((VARIABLE_ITEM)Vbl.Item[0]).Value.Length; DestOct.octets = PinnedArray[0].AddrOfPinnedObject(); Context = SnmpAPI.SnmpStrToContext(Session, ref DestOct); // set the port to 162 Result = SnmpAPI.SnmpSetPort(Destin, 162); // now send the messages Result = SnmpAPI.SnmpSendMsg(Session, Source, Destin, Context, Pdu); // SNMPAPI use requires us to handle some garbage collecting ourselves, else suffer memory leakage. // free the context Result = SnmpAPI.SnmpFreeContext(Context); // free the pdu Result = SnmpAPI.SnmpFreePdu(Pdu); // free the variable lists SnmpAPI.SnmpFreeVbl(VbL1); for (int t = 0; t < Vbl.Item.Count; t++) { // free the pinned arrays PinnedArray[t].Free(); // free the oids SnmpAPI.SnmpFreeDescriptor((int)SNMPAPI_SYNTAX.SNMP_SYNTAX_OID, ref OID[t]); } // finally, free the entities SnmpAPI.SnmpFreeEntity(Source); SnmpAPI.SnmpFreeEntity(Destin); Success = true; } else { throw new Exception("Variable Binding List Empty."); } } else { throw new Exception("Variable Binding Does Not Exist."); } } catch (Exception Err) { ErrMess = Err.Message; } return(Success); // return success flag }
public bool Listen(string RemoteMachine) { bool Success = false; // init success to failure try { SNMPAPI_STATUS Result = new SNMPAPI_STATUS(); // set up a result item // this is our call-back function SnmpAPI.SnmpCallback SnmpCB1 = new SnmpAPI.SnmpCallback(OnSnmpMessage); // get the local and remote ip addresses this.LocalAddr = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0].ToString(); this.RemoteAddr = Dns.GetHostEntry(RemoteMachine).AddressList[0].ToString(); // initialize the snmp api system Result = SnmpAPI.SnmpStartup(out major, out minor, out level, out translate, out retran); if (Result != SNMPAPI_STATUS.SNMPAPI_SUCCESS) { throw new Exception("SNMP Initialization Error"); } // create the session Session = SnmpAPI.SnmpCreateSession(IntPtr.Zero, 0, SnmpCB1, IntPtr.Zero); if (Result == SNMPAPI_STATUS.SNMPAPI_FAILURE) { throw new Exception("SNMP Create Session Error"); } Result = SnmpRegister(Session, System.IntPtr.Zero, System.IntPtr.Zero, System.IntPtr.Zero, System.IntPtr.Zero, (int)SNMPAPI_STATE.SNMPAPI_ON); if (Result != SNMPAPI_STATUS.SNMPAPI_SUCCESS) { throw new Exception("SNMP Initialization Error"); } IntPtr VbL1 = new IntPtr(); // pointer to variable bindings list SMIOID[] OID = new SMIOID[1]; // an array of oids SMIVALUE[] Val = new SMIVALUE[1]; // an array of values // create entities for the source and destination addresses IntPtr Source = SnmpAPI.SnmpStrToEntity(Session, LocalAddr); IntPtr Destin = SnmpAPI.SnmpStrToEntity(Session, RemoteAddr); VbL1 = SnmpAPI.SnmpCreateVbl(Session, ref OID[0], ref Val[0]); VARIABLE_ITEM VariableListItem = new VARIABLE_ITEM(); VariableListItem.Entity = ".1"; VariableListItem.Type = VARIABLE_TYPE.VARIABLE_TYPE_OCTECT; VariableListItem.Value = "GetAlarmDescription(AlarmCode)"; AddVariableListItem(VariableListItem); IntPtr Pdu = SnmpAPI.SnmpCreatePdu(Session, (int)SNMPAPI_PDU.SNMP_PDU_GETBULK, Int16.MinValue, Int16.MinValue, Int16.MinValue, VbL1); GCHandle[] PinnedArray = new GCHandle[1]; // an array of pinned memory SMIOCTETS DestOct = new SMIOCTETS(); // the destination octect // set the destination octet (we only need to set the first one, the rest will flow accordingly) DestOct.size = (uint)(VariableListItem.Value.Length); //DestOct.octets = PinnedArray[0].AddrOfPinnedObject(); IntPtr Context = SnmpAPI.SnmpStrToContext(Session, ref DestOct); // set the port to 162 Result = SnmpAPI.SnmpSetPort(Destin, 162); while (!Success) { Result = SnmpAPI.SnmpRecvMsg(Session, out Source, out Destin, out Context, out Pdu); if (Result == SNMPAPI_STATUS.SNMPAPI_SUCCESS) //throw new Exception("SNMP Initialization Error"); { int teste1; int teste0; int teste3; int teste4; IntPtr vlb; Result = SnmpAPI.SnmpGetPduData(Pdu, out teste0, out teste1, out teste3, out teste4, out vlb); //VARIABLE_BINDING_LIST teste11 = teste; Success = true; break; } Thread.Sleep(100); } } catch (Exception Err) { ErrMess = Err.Message; } return(Success); }
public static string VbToString(ref SMIVALUE sval) { string val = ""; // // Convert all types to a string value // switch (sval.type) { // // Standard 32 bit integer conversion // case SNMPAPI_SYNTAX.SNMP_SYNTAX_INT: val = sval.val.sNumber.ToString(); break; // // Standard 32 bit unsigned integer conversion // case SNMPAPI_SYNTAX.SNMP_SYNTAX_UINT32: case SNMPAPI_SYNTAX.SNMP_SYNTAX_CNTR32: case SNMPAPI_SYNTAX.SNMP_SYNTAX_GAUGE32: val = sval.val.uNumber.ToString(); break; // // Timeticks are in hundredths of a second (.01). We need to multiply // by ten to send ms (.001) to TimeSpan. // case SNMPAPI_SYNTAX.SNMP_SYNTAX_TIMETICKS: val = TimeSpan.FromMilliseconds((long)sval.val.uNumber * 10).ToString(); val = val.Substring(0, val.Length - 4); break; // // Standard 64 bit integer conversion // case SNMPAPI_SYNTAX.SNMP_SYNTAX_CNTR64: val = sval.val.hNumber.ToString(); break; // // Byte array conversion. SNMP doesn't differentiate between // ASCII and binary data, so we perform a check ourselves and // convert binary data to ASCII (using ToBase64String, which is uuencode). // case SNMPAPI_SYNTAX.SNMP_SYNTAX_BITS: case SNMPAPI_SYNTAX.SNMP_SYNTAX_OPAQUE: case SNMPAPI_SYNTAX.SNMP_SYNTAX_OCTETS: if (sval.val.str.size > 0) { int i = 0; byte[] bits = new byte[sval.val.str.size]; Marshal.Copy(sval.val.str.octets, bits, 0, (int)sval.val.str.size); for (i = 0; i < bits.Length; i++) { if (char.IsControl((char)bits[i]) && (i < bits.Length - 1 || bits[i] != 0)) { val = Convert.ToBase64String(bits, 0, bits.Length); break; } } if (i == bits.Length) { val = Marshal.PtrToStringAnsi(sval.val.str.octets, (int)sval.val.str.size); } SnmpAPI.SnmpFreeDescriptor((int)sval.type, ref sval.val.str); } break; // // Ip Address conversion // case SNMPAPI_SYNTAX.SNMP_SYNTAX_NSAPADDR: case SNMPAPI_SYNTAX.SNMP_SYNTAX_IPADDR: #if (WIN32) IPAddress addr = new IPAddress((long)(UInt32)Marshal.ReadIntPtr(sval.val.str.octets)); #else IPAddress addr = null; try { byte[] bytes = new byte[sval.val.str.size]; Marshal.Copy(sval.val.str.octets, bytes, 0, (int)sval.val.str.size); addr = new IPAddress(bytes); val = addr.ToString(); } catch (Exception ex) { throw new Exception("Problem To Convert IPADDRESS From SNMPApi " + ex.ToString()); } #endif break; // // SNMP OID conversion // case SNMPAPI_SYNTAX.SNMP_SYNTAX_OID: val = OidToString(ref sval.val.oid); break; // // Catch the rest // case SNMPAPI_SYNTAX.SNMP_SYNTAX_NULL: case SNMPAPI_SYNTAX.SNMP_SYNTAX_NOSUCHOBJECT: case SNMPAPI_SYNTAX.SNMP_SYNTAX_NOSUCHINSTANCE: case SNMPAPI_SYNTAX.SNMP_SYNTAX_ENDOFMIBVIEW: val = "(null)"; break; } return(val); }