Пример #1
0
        public Scope(ISnmpPdu pdu)
        {
            if (pdu == null)
            {
                throw new ArgumentNullException("pdu");
            }

            Pdu = pdu;
        }
        public void TestResponseV1()
        {
            ISnmpMessage message = MessageFactory.ParseMessages(Properties.Resources.getresponse, new UserRegistry())[0];

            Assert.Equal(SnmpType.ResponsePdu, message.TypeCode());
            ISnmpPdu pdu = message.Pdu();

            Assert.Equal(SnmpType.ResponsePdu, pdu.TypeCode);
            ResponsePdu response = (ResponsePdu)pdu;

            Assert.Equal(Integer32.Zero, response.ErrorStatus);
            Assert.Equal(0, response.ErrorIndex.ToInt32());
            Assert.Equal(1, response.Variables.Count);
            Variable v = response.Variables[0];

            Assert.Equal(new uint[] { 1, 3, 6, 1, 2, 1, 1, 6, 0 }, v.Id.ToNumerical());
            Assert.Equal("Shanghai", v.Data.ToString());
        }
        private IEnumerable <DataUnit> ExtractMessageData(ISnmpPdu pdu)
        {
            var operation = DetectOperation(pdu.TypeCode);

            foreach (var item in pdu.Variables)
            {
                var      oid  = string.Join(".", item.Id.ToNumerical());
                DataType type = item.DetectDataType();

                DataUnit data = new DataUnit(oid, type, operation);

                if (operation == Operation.Set)
                {
                    data.Value = item.Data.ToString();
                }

                yield return(data);
            }
        }
Пример #4
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");
            }

            Version      = version;
            AgentAddress = agent;
            Community    = community;
            Enterprise   = enterprise;
            Generic      = generic;
            Specific     = specific;
            TimeStamp    = time;
            TrapV1Pdu 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();
        }
        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.GetAddressBytes()), new Integer32((int)Generic), new Integer32(Specific),
                                    new TimeTicks(TimeStamp), variables);

            _pdu       = pdu;
            Parameters = SecurityParameters.Create(Community);
        }
Пример #6
0
        public Scope(OctetString contextEngineId, OctetString contextName, ISnmpPdu pdu)
        {
            if (contextEngineId == null)
            {
                throw new ArgumentNullException(nameof(contextEngineId));
            }

            if (contextName == null)
            {
                throw new ArgumentNullException(nameof(contextName));
            }

            if (pdu == null)
            {
                throw new ArgumentNullException(nameof(pdu));
            }

            ContextEngineId = contextEngineId;
            ContextName     = contextName;
            Pdu             = pdu;
        }
Пример #7
0
        public Scope(OctetString contextEngineId, OctetString contextName, ISnmpPdu pdu)
        {
            if (contextEngineId == null)
            {
                throw new ArgumentNullException("contextEngineId");
            }

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

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

            ContextEngineId = contextEngineId;
            ContextName = contextName;
            Pdu = pdu;
        }
Пример #8
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();
        }
Пример #9
0
        public void TestResponseVersion3_2()
        {
            var engine = CreateEngine();

            engine.Listener.ClearBindings();
            var serverEndPoint = new IPEndPoint(IPAddress.Loopback, Port.NextId);

            engine.Listener.AddBinding(serverEndPoint);
            engine.Start();

            try
            {
                IAuthenticationProvider auth = new MD5AuthenticationProvider(new OctetString("authenticationauthentication"));
                IPrivacyProvider        priv = new DefaultPrivacyProvider(auth);

                var           timeout   = 3000;
                Discovery     discovery = Messenger.GetNextDiscovery(SnmpType.GetRequestPdu);
                ReportMessage report    = discovery.GetResponse(timeout, serverEndPoint);

                var expected = Messenger.NextRequestId;
                GetRequestMessage request = new GetRequestMessage(VersionCode.V3, Messenger.NextMessageId, expected, new OctetString("authen"), OctetString.Empty, new List <Variable> {
                    new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0"))
                }, priv, Messenger.MaxMessageSize, report);
                ISnmpMessage reply   = request.GetResponse(timeout, serverEndPoint);
                ISnmpPdu     snmpPdu = reply.Pdu();
                Assert.Equal(SnmpType.ResponsePdu, snmpPdu.TypeCode);
                Assert.Equal(expected, reply.RequestId());
                Assert.Equal(ErrorCode.NoError, snmpPdu.ErrorStatus.ToErrorCode());
            }
            finally
            {
                if (SnmpMessageExtension.IsRunningOnWindows)
                {
                    engine.Stop();
                }
            }
        }
Пример #10
0
        /// <summary>
        /// Creates a <see cref="TrapV1Message"/> instance with a message body.
        /// </summary>
        /// <param name="body">Message body</param>
        public TrapV1Message(Sequence body)
        {
            if (body == null)
            {
                throw new ArgumentNullException(nameof(body));
            }

            if (body.Length != 3)
            {
                throw new ArgumentException("Invalid message body.", nameof(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($"TRAP v1 is not supported in this SNMP version: {Version}.", nameof(body));
            }

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

            TrapV1Pdu trapPdu = (TrapV1Pdu)_pdu;

            Enterprise   = trapPdu.Enterprise;
            AgentAddress = new IPAddress(trapPdu.AgentAddress.GetRaw());
            Generic      = trapPdu.Generic;
            Specific     = trapPdu.Specific;
            TimeStamp    = trapPdu.TimeStamp.ToUInt32();
            Parameters   = SecurityParameters.Create(Community);
            _container   = body;
        }
Пример #11
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);
 }
Пример #12
0
        /// <summary>
        /// Creates a <see cref="TrapV1Message"/> instance with a message body.
        /// </summary>
        /// <param name="body">Message body</param>
        public TrapV1Message(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 = trapPdu.AgentAddress.ToIPAddress();
            Generic = trapPdu.Generic;
            Specific = trapPdu.Specific;
            TimeStamp = trapPdu.TimeStamp.ToUInt32();
            Parameters = SecurityParameters.Create(Community);
            _container = body;
        }
Пример #13
0
        public void Handle(ISnmpContext context, ObjectStore store)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

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

            ISnmpPdu         pdu    = context.Request.Pdu();
            IList <Variable> result = new List <Variable>();
            int index        = 0;
            int nonrepeaters = pdu.ErrorStatus.ToInt32();
            var variables    = pdu.Variables;

            for (int i = 0; i < nonrepeaters; i++)
            {
                Variable v = variables[i];
                index++;
                try
                {
                    ScalarObject next = store.GetNextObject(v.Id);
                    result.Add(next == null ? new Variable(v.Id, new EndOfMibView()) : next.Variable);
                }
                catch (Exception)
                {
                    context.CopyRequest(ErrorCode.GenError, index);
                    return;
                }
            }

            for (int j = nonrepeaters; j < variables.Count; j++)
            {
                Variable v = variables[j];
                index++;
                Variable temp       = v;
                int      repetition = pdu.ErrorIndex.ToInt32();
                while (repetition-- > 0)
                {
                    try
                    {
                        ScalarObject next = store.GetNextObject(temp.Id);
                        if (next == null)
                        {
                            temp = new Variable(temp.Id, new EndOfMibView());
                            result.Add(temp);
                            break;
                        }

                        // TODO: how to handle write only object here?
                        result.Add(next.Variable);
                        temp = next.Variable;
                    }
                    catch (Exception)
                    {
                        context.CopyRequest(ErrorCode.GenError, index);
                        return;
                    }
                }
            }

            context.GenerateResponse(result);
        }
Пример #14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Scope"/> class.
 /// </summary>
 /// <param name="pdu">The PDU.</param>
 public Scope(ISnmpPdu pdu)
 {
     Pdu = pdu ?? throw new ArgumentNullException(nameof(pdu));
 }
Пример #15
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Scope"/> class.
 /// </summary>
 /// <param name="contextEngineId">The context engine ID.</param>
 /// <param name="contextName">Name of the context.</param>
 /// <param name="pdu">The PDU.</param>
 public Scope(OctetString contextEngineId, OctetString contextName, ISnmpPdu pdu)
 {
     ContextEngineId = contextEngineId ?? throw new ArgumentNullException(nameof(contextEngineId));
     ContextName     = contextName ?? throw new ArgumentNullException(nameof(contextName));
     Pdu             = pdu ?? throw new ArgumentNullException(nameof(pdu));
 }