示例#1
0
        public TrapV1Message(VersionCode version, IPAddress agent, OctetString community, ObjectIdentifier enterprise, GenericCode generic, int specific, uint time, IList <Variable> variables)
        {
            if (variables == null)
            {
                throw new ArgumentNullException("variables");
            }

            if (enterprise == null)
            {
                throw new ArgumentNullException("enterprise");
            }

            if (community == null)
            {
                throw new ArgumentNullException("community");
            }

            if (agent == null)
            {
                throw new ArgumentNullException("agent");
            }

            if (version != VersionCode.V1)
            {
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "TRAP v1 is not supported in this SNMP version: {0}", version), "version");
            }

            Version      = version;
            AgentAddress = agent;
            Community    = community;
            Enterprise   = enterprise;
            Generic      = generic;
            Specific     = specific;
            TimeStamp    = time;
            var pdu = new TrapV1Pdu(
                Enterprise,
                new IP(AgentAddress),
                new Integer32((int)Generic),
                new Integer32(Specific),
                new TimeTicks(TimeStamp),
                variables);

            _pdu       = pdu;
            Parameters = SecurityParameters.Create(Community);

            _bytes = SnmpMessageExtension.PackMessage(Version, Community, pdu).ToBytes();
        }
示例#2
0
        /// <summary>
        /// Creates a <see cref="TrapV1Message"/> instance with a message body.
        /// </summary>
        /// <param name="body">Message body</param>
        public TrapV1Message(IPAddress senderIp, Sequence body)
        {
            if (body == null)
            {
                throw new ArgumentNullException("body");
            }

            if (body.Length != 3)
            {
                throw new ArgumentException("wrong message body");
            }

            Community = (OctetString)body[1];
            Version   = (VersionCode)((Integer32)body[0]).ToInt32();

            // IMPORTANT: comment this check out if you need to support
            if (Version != VersionCode.V1)
            {
                throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "TRAP v1 is not supported in this SNMP version: {0}", Version), "body");
            }

            _pdu = (ISnmpPdu)body[2];
            if (_pdu.TypeCode != SnmpType.TrapV1Pdu)
            {
                throw new ArgumentException("wrong message type");
            }

            var trapPdu = (TrapV1Pdu)_pdu;

            Enterprise   = trapPdu.Enterprise;
            AgentAddress = senderIp != null?senderIp:trapPdu.AgentAddress.ToIPAddress();
            Generic      = trapPdu.Generic;
            Specific     = trapPdu.Specific;
            TimeStamp    = trapPdu.TimeStamp.ToUInt32();
            Parameters   = SecurityParameters.Create(Community);

            _bytes = SnmpMessageExtension.PackMessage(Version, Community, _pdu).ToBytes();
        }