decode() public method

Decode received packet. This method overrides the base implementation that cannot be used with this type of the packet.
public decode ( byte buffer, int length ) : int
buffer byte Packet buffer
length int Buffer length
return int
        ///<summary>
        ///Actual work in backgroundworker thread
        ///</summary>
        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            // Construct a socket and bind it to the trap manager port 10162
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 162);
            EndPoint ep = (EndPoint)ipep;
            socket.Bind(ep);
            // Disable timeout processing. Just block until packet is received
            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 0);
            bool run = true;
            int inlen;
            int ver;
            while (run)
            {
                byte[] indata = new byte[16 * 1024];
                // 16KB receive buffer int inlen = 0;
                IPEndPoint peer = new IPEndPoint(IPAddress.Any, 0);
                EndPoint inep = (EndPoint)peer;
                try
                {
                    inlen = socket.ReceiveFrom(indata, ref inep);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception {0}", ex.Message);
                    inlen = -1;
                }
                if (inlen > 0)
                {
                    // Check protocol version int
                    ver = SnmpPacket.GetProtocolVersion(indata, inlen);
                    if (ver == (int)SnmpVersion.Ver1)
                    {
                        // Parse SNMP Version 1 TRAP packet
                        SnmpV1TrapPacket pkt = new SnmpV1TrapPacket();
                        pkt.decode(indata, inlen);
                        Console.WriteLine("** SNMP Version 1 TRAP received from {0}:", inep.ToString());
                        Console.WriteLine("*** Trap generic: {0}", pkt.Pdu.Generic);
                        Console.WriteLine("*** Trap specific: {0}", pkt.Pdu.Specific);
                        Console.WriteLine("*** Agent address: {0}", pkt.Pdu.AgentAddress.ToString());
                        Console.WriteLine("*** Timestamp: {0}", pkt.Pdu.TimeStamp.ToString());
                        Console.WriteLine("*** VarBind count: {0}", pkt.Pdu.VbList.Count);
                        Console.WriteLine("*** VarBind content:");

                        //Looking for 1.3.6.1.3.5.0 and refreshing Plot.
                        double voltage = Double.Parse(pkt.Pdu.VbList[4].Value.ToString());
                        voltageLine.Points.Add(new DataPoint(double.Parse(pkt.Pdu.TimeStamp.ToString()), voltage));
                        model.RefreshPlot(true);
                        Console.WriteLine("** End of SNMP Version 1 TRAP data.");
                    }
                }
                else
                {
                    if (inlen == 0)
                        Console.WriteLine("Zero length packet received.");
                }
            }
            Yield(1000000);
        }
    /// <summary>
    /// Thread that will keep listening for traps
    /// </summary>
    public void TrapThread()
    {
		// Construct a socket and bind it to the trap manager port 162 

		Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
		IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 16009);
		EndPoint ep = (EndPoint)ipep;
		socket.Bind(ep);
		// Disable timeout processing. Just block until packet is received 
		socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 0);
		run_trap_thread = true;
		int inlen = -1;

        while (run_trap_thread)
        {
			byte[] indata = new byte[16 * 1024];
			// 16KB receive buffer int inlen = 0;
			IPEndPoint peer = new IPEndPoint(IPAddress.Any, 0);
			EndPoint inep = (EndPoint)peer;
			
		    inlen = socket.ReceiveFrom(indata, ref inep);

			if (inlen > 0) {

				// Check protocol version int 
				int ver = SnmpPacket.GetProtocolVersion(indata, inlen);
					// Parse SNMP Version 1 TRAP packet 
					SnmpV1TrapPacket pkt = new SnmpV1TrapPacket();
					pkt.decode(indata, inlen);

                    string underAttackOid = "1";
                    string foundEnemyOid = "2";
                    string headOid = "5.1";
                    string legsOid = "5.2";
                    string armsOid = "5.3";


					foreach (Vb v in pkt.Pdu.VbList) {
                        Debug.Log("for each vb");

                        switch (v.Oid.ToString())
                        {
                            case "1.3.2.5." + "1":
                                underAttack = int.Parse(v.Value.ToString());
                                break;


                            // found enemy
                            case "1.3.2.5." + "2":
                                foundEnemy = int.Parse(v.Value.ToString());
                                break;
                            // head bellow 50
                            case "1.3.2.5." + "5.1":
                                healthWarning = 1;
                                HeadHealth = int.Parse(v.Value.ToString());
                                break;
                            // head bellow 25
                            case "1.3.2.5." + "5.2":
                                healthWarning = 1;
                                LegsHealth = int.Parse(v.Value.ToString());
                                break;
                            // head destroyed
                            case "1.3.2.5." + "5.3":
                                healthWarning = 1;
                                ArmHealth = int.Parse(v.Value.ToString());
                                break;

                            
                        }
                          

					}
				
			} else {
				if (inlen == 0)
					Console.WriteLine("Zero length packet received.");
			}
		}
        Debug.Log(" trap thread EXITT");

		
    }
Example #3
0
        public void zlapPulapke()
        {
            /*! 
		     *Kod pochodzi ze strony http://www.snmpsharpnet.com/?page_id=117
             */
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 162);
            EndPoint ep = (EndPoint)ipep;
            socket.Bind(ep);
            // Disable timeout processing. Just block until packet is received 
            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 0);
            bool run = true;
            int inlen = -1;
            while (run)
            {
                byte[] indata = new byte[16 * 1024];
                // 16KB receive buffer int inlen = 0;
                IPEndPoint peer = new IPEndPoint(IPAddress.Any, 0);
                EndPoint inep = (EndPoint)peer;
                try
                {
                    inlen = socket.ReceiveFrom(indata, ref inep);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception {0}", ex.Message);
                    inlen = -1;
                }
                if (inlen > 0)
                {
                    Console.WriteLine("TAK");

                    listaWierszyZeZlapanejPualpki.Clear();
                    // Check protocol version int 
                    int ver = SnmpPacket.GetProtocolVersion(indata, inlen);
                    if (ver == (int)SnmpVersion.Ver1)
                    {
                        // Parse SNMP Version 1 TRAP packet 
                        SnmpV1TrapPacket pkt = new SnmpV1TrapPacket();
                        pkt.decode(indata, inlen);
                        listaWierszyZeZlapanejPualpki.Add("** SNMP Version 1 TRAP otrzymane od " + inep.ToString());
                        listaWierszyZeZlapanejPualpki.Add("*** Trap generic: " + pkt.Pdu.Generic);
                        listaWierszyZeZlapanejPualpki.Add("*** Trap specific: " + pkt.Pdu.Specific);
                        listaWierszyZeZlapanejPualpki.Add("*** Agent address: " + pkt.Pdu.AgentAddress.ToString());
                        listaWierszyZeZlapanejPualpki.Add("*** Timestamp: " + pkt.Pdu.TimeStamp.ToString());
                        listaWierszyZeZlapanejPualpki.Add("*** VarBind count: " + pkt.Pdu.VbList.Count);
                        listaWierszyZeZlapanejPualpki.Add("*** VarBind content:");
                        foreach (Vb v in pkt.Pdu.VbList)
                        {
                            StringBuilder budowaStringa = new StringBuilder();
                            budowaStringa.Append("**** ");
                            budowaStringa.Append(v.Oid.ToString());
                            budowaStringa.Append(" " + SnmpConstants.GetTypeName(v.Value.Type));
                            budowaStringa.Append(": " + v.Value.ToString());
                            listaWierszyZeZlapanejPualpki.Add(budowaStringa.ToString());
                        }
                        listaWierszyZeZlapanejPualpki.Add("** Koniec SNMP Version 1 TRAP data.");
                        flagaWierszyZeZlapanejPulapki = true;
                        //return true;
                    }
                    else {
                        // Parse SNMP Version 2 TRAP packet 
                        SnmpV2Packet pkt = new SnmpV2Packet();
                        pkt.decode(indata, inlen);
                        listaWierszyZeZlapanejPualpki.Clear();
                        listaWierszyZeZlapanejPualpki.Add("** SNMP Version 2 TRAP otrzymane od " + inep.ToString());
                        if ((SnmpSharpNet.PduType)pkt.Pdu.Type != PduType.V2Trap)
                        {
                            listaWierszyZeZlapanejPualpki.Add("*** NOT an SNMPv2 trap ****");
                        }
                        else {
                            listaWierszyZeZlapanejPualpki.Add("*** Community: " + pkt.Community.ToString());
                            listaWierszyZeZlapanejPualpki.Add("*** VarBind count: " + pkt.Pdu.VbList.Count);
                            listaWierszyZeZlapanejPualpki.Add("*** VarBind content:");
                            foreach (Vb v in pkt.Pdu.VbList)
                            {
                                StringBuilder budowaStringa = new StringBuilder();
                                budowaStringa.Append("**** ");
                                budowaStringa.Append(v.Oid.ToString());
                                budowaStringa.Append(" " + SnmpConstants.GetTypeName(v.Value.Type));
                                budowaStringa.Append(": " + v.Value.ToString());
                                listaWierszyZeZlapanejPualpki.Add(budowaStringa.ToString());
                            }
                            listaWierszyZeZlapanejPualpki.Add("** Koniec SNMP Version 2 TRAP data.");
                        }
                        flagaWierszyZeZlapanejPulapki = true;
                        //return true;
                    }
                }
                else {
                    if (inlen == 0)
                    {
                        listaWierszyZeZlapanejPualpki.Clear();
                        listaWierszyZeZlapanejPualpki.Add("Przechwycono pusty pakiet");
                        flagaWierszyZeZlapanejPulapki = true;
                        //return true;
                    }
                }
            }
            //return false;
        }
Example #4
0
        private void button1_Click(object sender, EventArgs e)
        {
            // Construct a socket and bind it to the trap manager port 162
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 162);
            EndPoint ep = (EndPoint)ipep;
            socket.Bind(ep);
            // Disable timeout processing. Just block until packet is received
            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 0);
            bool run = true;
            int inlen = -1;
            while (run)
            {
                Application.DoEvents();
                Thread.Sleep(100);
                byte[] indata = new byte[16 * 1024];
                // 16KB receive buffer int inlen = 0;
                IPEndPoint peer = new IPEndPoint(IPAddress.Any, 0);
                EndPoint inep = (EndPoint)peer;
                try
                {

                    inlen = socket.ReceiveFrom(indata, ref inep);

                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception {0}", ex.Message);
                    inlen = -1;
                }
                if (inlen > 0)
                {
                    // Check protocol version int
                    int ver = SnmpPacket.GetProtocolVersion(indata, inlen);
                    if (ver == (int)SnmpVersion.Ver1)
                    {
                        // Parse SNMP Version 1 TRAP packet
                        SnmpV1TrapPacket pkt = new SnmpV1TrapPacket();
                        pkt.decode(indata, inlen);
                        Console.WriteLine("** SNMP Version 1 TRAP received from {0}:", inep.ToString());
                        Console.WriteLine("*** Trap generic: {0}", pkt.Pdu.Generic);
                        Console.WriteLine("*** Trap specific: {0}", pkt.Pdu.Specific);
                        Console.WriteLine("*** Agent address: {0}", pkt.Pdu.AgentAddress.ToString());
                        Console.WriteLine("*** Timestamp: {0}", pkt.Pdu.TimeStamp.ToString());
                        Console.WriteLine("*** VarBind count: {0}", pkt.Pdu.VbList.Count);
                        Console.WriteLine("*** VarBind content:");
                        foreach (Vb v in pkt.Pdu.VbList)
                        {
                            Console.WriteLine("**** {0} {1}: {2}", v.Oid.ToString(), SnmpConstants.GetTypeName(v.Value.Type), v.Value.ToString());
                            textBox1.Text += "\n" + v.Oid.ToString() + "|" + SnmpConstants.GetTypeName(v.Value.Type) + "|" + v.Value.ToString();
                        }
                        Console.WriteLine("** End of SNMP Version 1 TRAP data.");
                    }
                    else
                    {
                        // Parse SNMP Version 2 TRAP packet
                        SnmpV2Packet pkt = new SnmpV2Packet();
                        pkt.decode(indata, inlen);
                        Console.WriteLine("** SNMP Version 2 TRAP received from {0}:", inep.ToString());
                        if ((SnmpSharpNet.PduType)pkt.Pdu.Type != PduType.V2Trap)
                        {
                            Console.WriteLine("*** NOT an SNMPv2 trap ****");
                        }
                        else
                        {
                            Console.WriteLine("*** Community: {0}", pkt.Community.ToString());
                            Console.WriteLine("*** VarBind count: {0}", pkt.Pdu.VbList.Count);
                            Console.WriteLine("*** VarBind content:");
                            foreach (Vb v in pkt.Pdu.VbList)
                            {
                                Console.WriteLine("**** {0} {1}: {2}",
                                   v.Oid.ToString(), SnmpConstants.GetTypeName(v.Value.Type), v.Value.ToString());
                            }
                            Console.WriteLine("** End of SNMP Version 2 TRAP data.");
                        }
                    }
                }
                else
                {
                    if (inlen == 0)
                        Console.WriteLine("Zero length packet received.");
                }
            }
        }
        ///<summary>
        ///Actual work in backgroundworker thread
        ///</summary>
        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            // Construct a socket and bind it to the trap manager port 10162
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 10162);
            EndPoint ep = (EndPoint)ipep;
            socket.Bind(ep);
            // Disable timeout processing. Just block until packet is received
            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 0);
            bool run = true;
            int inlen;
            int ver;
            while (run)
            {
                byte[] indata = new byte[16 * 1024];
                // 16KB receive buffer int inlen = 0;
                IPEndPoint peer = new IPEndPoint(IPAddress.Any, 0);
                EndPoint inep = (EndPoint)peer;
                try
                {
                    inlen = socket.ReceiveFrom(indata, ref inep);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception {0}", ex.Message);
                    inlen = -1;
                }
                if (inlen > 0)
                {
                    // Check protocol version int
                    ver = SnmpPacket.GetProtocolVersion(indata, inlen);
                    if (ver == (int)SnmpVersion.Ver1)
                    {
                        // Parse SNMP Version 1 TRAP packet
                        SnmpV1TrapPacket pkt = new SnmpV1TrapPacket();
                        pkt.decode(indata, inlen);
                        Console.WriteLine("** SNMP Version 1 TRAP received from {0}:", inep.ToString());
                        Console.WriteLine("*** Trap generic: {0}", pkt.Pdu.Generic);
                        Console.WriteLine("*** Trap specific: {0}", pkt.Pdu.Specific);
                        Console.WriteLine("*** Agent address: {0}", pkt.Pdu.AgentAddress.ToString());
                        Console.WriteLine("*** Timestamp: {0}", pkt.Pdu.TimeStamp.ToString());
                        Console.WriteLine("*** VarBind count: {0}", pkt.Pdu.VbList.Count);
                        Console.WriteLine("*** VarBind content:");
                        foreach (Vb v in pkt.Pdu.VbList)
                        {
                            Console.WriteLine("**** {0} {1}: {2}", v.Oid.ToString(), SnmpConstants.GetTypeName(v.Value.Type), v.Value.ToString());
                            string voltage = v.Value.ToString();
                            double max = 3.3;
                            string[] values = voltage.Split('V');
                            _worker.ReportProgress((int)Math.Floor((double)((double.Parse(values[0], CultureInfo.InvariantCulture) / max * 100))));
                            currentVoltage = voltage;
                            double test = double.Parse(values[0]);
                            voltageLine.Points.Add(new DataPoint(double.Parse(pkt.Pdu.TimeStamp.ToString()), double.Parse(values[0], CultureInfo.InvariantCulture)));
                            model.RefreshPlot(true);
                        }
                        Console.WriteLine("** End of SNMP Version 1 TRAP data.");
                    }
                }
                else
                {
                    if (inlen == 0)
                        Console.WriteLine("Zero length packet received.");
                }
            }
            Yield(1000000);
        }
Example #6
0
        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker parentWorker = sender as BackgroundWorker;
            bool running = true;

            // Construct a socket and bind it to the trap manager port 162
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            IPAddress ip = IPAddress.Parse(LISTEN_IP);
            IPEndPoint ipep = new IPEndPoint(ip, LISTEN_PORT);
            EndPoint ep = (EndPoint)ipep;
            socket.Bind(ep);
            // Disable timeout processing. Just block until packet is received
            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 0);
            while (running) {
                byte[] indata = new byte[16 * 1024];
                // 16KB receive buffer int inlen = 0;
                IPEndPoint peer = new IPEndPoint(IPAddress.Any, 0);
                EndPoint inep = (EndPoint)peer;
                int inlen = 0;
                int ver;
                try {
                    inlen = socket.ReceiveFrom(indata, ref inep);
                } catch (Exception ex) {
                    Console.WriteLine("Exception {0}", ex.Message);
                    inlen = -1;
                }
                if (inlen > 0) {
                    Console.WriteLine("inlen > 0");
                    // Check protocol version int
                    ver = SnmpPacket.GetProtocolVersion(indata, inlen);
                    if (ver == (int)SnmpVersion.Ver1) {
                        // Parse SNMP Version 1 TRAP packet
                        SnmpV1TrapPacket pkt = new SnmpV1TrapPacket();
                        pkt.decode(indata, inlen);
                        Console.WriteLine("** SNMP Version 1 TRAP received from {0}:", inep.ToString());
                        Console.WriteLine("*** Trap generic: {0}", pkt.Pdu.Generic);
                        Console.WriteLine("*** Trap specific: {0}", pkt.Pdu.Specific);
                        Console.WriteLine("*** Agent address: {0}", pkt.Pdu.AgentAddress.ToString());
                        Console.WriteLine("*** Timestamp: {0}", pkt.Pdu.TimeStamp.ToString());
                        Console.WriteLine("*** VarBind count: {0}", pkt.Pdu.VbList.Count);
                        Console.WriteLine("*** VarBind content:");
                        foreach (Vb v in pkt.Pdu.VbList) {
                            Console.WriteLine("**** {0} {1}: {2}", v.Oid.ToString(), SnmpConstants.GetTypeName(v.Value.Type), v.Value.ToString());
                            if (v.Oid.ToString().Equals(SNMP_OID_VOLTAGE)) {
                                this.currentVoltage = double.Parse(v.Value.ToString(), CultureInfo.InvariantCulture);
                                this.lsVoltage.Points.Add(new DataPoint(pkt.Pdu.TimeStamp, this.currentVoltage));

                                dataPoints++;
                                if (dataPoints >= 50) {
                                    for (int i = 0; i < dataPoints - 50; i++) {
                                        this.lsVoltage.Points.RemoveAt(i);
                                        dataPoints--;
                                    }
                                }
                                this.pModel.RefreshPlot(true);
                                parentWorker.ReportProgress((int)Math.Floor((this.currentVoltage / MAX_VOLTAGE) * 100));
                            }
                        }
                        Console.WriteLine("** End of SNMP Version 1 TRAP data.");

                    }
                } else {
                    if (inlen == 0)
                        Console.WriteLine("Zero length packet received.");
                }
            }
        }