SnmpSharpNet.SnmpV1Packet.decode C# (CSharp) Метод

decode() публичный Метод

Decode received SNMP packet.
Thrown when invalid encoding has been found in the packet Thrown when parsed header points to more data then is available in the packet Thrown when parsed packet is not SNMP version 1 Thrown when received PDU is of a type not supported by SNMP version 1
public decode ( byte buffer, int length ) : int
buffer byte BER encoded packet buffer
length int BER encoded packet buffer length
Результат int
        public override int decode(byte[] buffer, int length)
        {
            MutableByte buf = new MutableByte(buffer, length);

            int headerLength;
            int offset = 0;

            offset = base.decode(buffer, buffer.Length);

            if (_protocolVersion.Value != (int)SnmpVersion.Ver1 )
                throw new SnmpInvalidVersionException("Invalid protocol version");

            offset = _snmpCommunity.decode(buf, offset);
            int tmpOffset = offset;
            byte asnType = AsnType.ParseHeader(buf, ref tmpOffset, out headerLength);

            // Check packet length
            if (headerLength + offset > buf.Length)
                throw new OverflowException("Insufficient data in packet");

            if (asnType != (byte)PduType.Get && asnType != (byte)PduType.GetNext && asnType != (byte)PduType.Set && asnType != (byte)PduType.Response)
                throw new SnmpInvalidPduTypeException("Invalid SNMP operation received: " + string.Format("0x{0:x2}", asnType));
            // Now process the Protocol Data Unit
            offset = this.Pdu.decode(buf, offset);
            return length;
        }

Usage Example

Пример #1
0
    /// <summary>
    /// Thread Functions which does all the SNMP Agent job
    /// </summary>
	public void ListenerThread() {
		mSock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
		mSock.ReceiveTimeout = 2000;
		IPEndPoint vEndPoint = new IPEndPoint(IPAddress.Any, 16100);
		mSock.Bind(vEndPoint);
		
		Debug.Log ("Agent: thread started");

		byte[] vBuff = new byte[4096];
		int vLen = 0;
		
		while (true) {
			if (this.mSock.Available > 0) {
				EndPoint vSender = (EndPoint)new IPEndPoint(IPAddress.Any, 0);
				vLen = mSock.ReceiveFrom(vBuff, ref vSender);
				//Debug.Log ("Agent: Data received (bytes): " + vLen);


                SnmpPacket vPacket = new SnmpV1Packet("" + "public");

				vPacket.decode(vBuff, vLen);
				//Debug.Log("Agent: PDU decoded: " + vPacket.Pdu.VbCount);
				Oid vOid = null;

                responsePacket = new SnmpV1Packet("" + "public");
                responsePacket.Pdu.ErrorStatus = 0; // no error

				if (vPacket.Pdu != null && vPacket.Pdu.VbList != null) {
					foreach (Vb vVb in vPacket.Pdu.VbList) {
						Debug.Log(vVb.ToString());
						vOid = vVb.Oid;
                        if (vPacket.Pdu.Type == PduType.Set)
                            ProcessSetRequest(vOid,vVb);

                        if (vPacket.Pdu.Type == PduType.GetNext){
                            GetNext(vOid);
                            break;
                        }
                        ProcessGetRequest(vOid);
					}
				}

                //Debug.Log(vOid.ToString());
                responsePacket.Pdu.Type = PduType.Response;
                responsePacket.Pdu.RequestId = vPacket.Pdu.RequestId;

                byte[] vOutBuff = responsePacket.encode();
                mSock.SendTo(vOutBuff, vSender);

                callPrint = true;

			}
			Thread.Sleep(1000);
		}
	}
All Usage Examples Of SnmpSharpNet.SnmpV1Packet::decode