Пример #1
0
        /// <summary>BER encode class value.</summary>
        /// <param name="buffer">Target buffer. Value is appended to the end of it.</param>
        public override void Encode(MutableByte buffer)
        {
            MutableByte tmp = new MutableByte();

            byte[] b = BitConverter.GetBytes(value);

            for (int i = 3; i >= 0; i--)
            {
                if (b[i] != 0 || tmp.Length > 0)
                {
                    tmp.Append(b[i]);
                }
            }

            if (tmp.Length > 0 && (tmp[0] & 0x80) != 0)
            {
                tmp.Prepend(0);
            }
            else if (tmp.Length == 0)
            {
                tmp.Append(0);
            }

            BuildHeader(buffer, Type, tmp.Length);

            buffer.Append(tmp);
        }
Пример #2
0
 /// <summary>
 /// Wrap BER encoded SNMP information contained in the parameter <see cref="MutableByte"/> class.
 /// 
 /// Information in the parameter is prepended by the SNMP version field and wrapped in a sequence header.
 /// 
 /// Derived classes call this method to finalize SNMP packet encoding.
 /// </summary>
 /// <param name="buffer">Buffer containing BER encoded SNMP information</param>
 public virtual void encode(MutableByte buffer)
 {
     // Encode SNMP protocol version
     MutableByte temp = new MutableByte();
     _protocolVersion.encode(temp);
     buffer.Prepend(temp);
     temp.Reset();
     AsnType.BuildHeader(temp, SnmpConstants.SMI_SEQUENCE, buffer.Length);
     buffer.Prepend(temp);
 }
Пример #3
0
        /// <summary>ASN.1 encode SNMP version 1 trap</summary>
        /// <param name="buffer"><see cref="MutableByte"/> buffer to the end of which encoded values are appended.</param>
        public override void encode(MutableByte buffer)
        {
            MutableByte trapBuffer = new MutableByte();
            // encode the enterprise id & address
            _enterprise.encode(trapBuffer);

            _agentAddr.encode(trapBuffer);

            _generic.encode(trapBuffer);

            _specific.encode(trapBuffer);

            _timeStamp.encode(trapBuffer);

            _variables.encode(trapBuffer);
            MutableByte tmpBuffer = new MutableByte();

            BuildHeader(tmpBuffer, (byte)PduType.Trap, trapBuffer.Length);
            trapBuffer.Prepend(tmpBuffer);
            buffer.Append(trapBuffer);
        }
Пример #4
0
        /// <summary>
        /// Used to encode the integer value into an ASN.1 buffer.
        /// The passed encoder defines the method for encoding the
        /// data.
        /// </summary>
        /// <param name="buffer">Buffer target to write the encoded data</param>
        public override void Encode(MutableByte buffer)
        {
            int val = value;

            byte[] b = BitConverter.GetBytes(value);

            MutableByte tmp = new MutableByte();

            // if value is negative
            if (val < 0)
            {
                for (int i = 3; i >= 0; i--)
                {
                    if (tmp.Length > 0 || b[i] != 0xff)
                    {
                        tmp.Append(b[i]);
                    }
                }

                if (tmp.Length == 0)
                {
                    // if the value is -1 then all bytes in an integer are 0xff and will be skipped above
                    tmp.Append(0xff);
                }

                // make sure value is negative
                if ((tmp[0] & 0x80) == 0)
                {
                    tmp.Prepend(0xff);
                }
            }
            else if (val == 0)
            {
                // this is just a shortcut to save processing time
                tmp.Append(0);
            }
            else
            {
                // byte[] b = BitConverter.GetBytes(val);
                for (int i = 3; i >= 0; i--)
                {
                    if (b[i] != 0 || tmp.Length > 0)
                    {
                        tmp.Append(b[i]);
                    }
                }

                // if buffer length is 0 then value is 0 and we have to add it to the buffer
                if (tmp.Length == 0)
                {
                    tmp.Append(0);
                }
                else
                {
                    if ((tmp[0] & 0x80) != 0)
                    {
                        // first bit of the first byte has to be 0 otherwise value is negative.
                        tmp.Prepend(0);
                    }
                }
            }

            // check for 9 1s at the beginning of the encoded value
            if (tmp.Length > 1 && tmp[0] == 0xff && (tmp[1] & 0x80) != 0)
            {
                tmp.Prepend(0);
            }

            BuildHeader(buffer, Type, tmp.Length);

            buffer.Append(tmp);
        }
Пример #5
0
        /// <summary>
        /// Encode SNMP packet for sending.
        /// </summary>
        /// <returns>BER encoded SNMP packet.</returns>
        public override byte[] encode()
        {
            MutableByte buf = new MutableByte();

            if (this.Pdu.Type != PduType.Get && this.Pdu.Type != PduType.GetNext &&
                this.Pdu.Type != PduType.Set && this.Pdu.Type != PduType.V2Trap &&
                this.Pdu.Type != PduType.Response && this.Pdu.Type != PduType.GetBulk &&
                this.Pdu.Type != PduType.Inform)
                throw new SnmpInvalidPduTypeException("Invalid SNMP PDU type while attempting to encode PDU: " + string.Format("0x{0:x2}", this.Pdu.Type));

            // snmp version
            _protocolVersion.encode(buf);

            // community string
            _snmpCommunity.encode(buf);

            // pdu
            _pdu.encode(buf);

            // wrap the packet into a sequence
            MutableByte tmpBuf = new MutableByte();
            AsnType.BuildHeader(tmpBuf, SnmpConstants.SMI_SEQUENCE, buf.Length);

            buf.Prepend(tmpBuf);
            return buf;
        }