Example #1
0
        public static async Task SendTrapV2Async(int requestId, VersionCode version, EndPoint receiver, OctetString community, ObjectIdentifier enterprise, uint timestamp, IList <Variable> variables)
        {
            if (version != VersionCode.V2)
            {
                throw new NotSupportedException("Only SNMP v2c is supported");
            }

            var message = new TrapV2Message(requestId, version, community, enterprise, timestamp, variables);
            await message.SendAsync(receiver).ConfigureAwait(false);
        }
Example #2
0
        public static void SendTrapV2(int requestId, VersionCode version, EndPoint receiver, OctetString community, ObjectIdentifier enterprise, uint timestamp, IList <Variable> variables)
        {
            if (version != VersionCode.V2)
            {
                throw new NotSupportedException("Only SNMP v2c is supported");
            }

            var message = new TrapV2Message(requestId, version, community, enterprise, timestamp, variables);

            message.Send(receiver);
        }
Example #3
0
        public static void SendTrapV2(int requestId, VersionCode version, EndPoint receiver, OctetString community, ObjectIdentifier enterprise, uint timestamp, IList <Variable> variables)
        {
            if (version == VersionCode.V1)
            {
                throw new ArgumentException("SNMP v1 is not support", "version");
            }

            TrapV2Message message = new TrapV2Message(requestId, version, community, enterprise, timestamp, variables);

            message.Send(receiver);
        }
 public void TestToBytes()
 {
     var trap = new TrapV2Message(
         VersionCode.V3,
         528732060,
         1905687779,
         new OctetString("lextm"),
         new ObjectIdentifier("1.3.6"),
         0,
         new List<Variable>(),
         DefaultPrivacyProvider.DefaultPair,
         0x10000,
         new OctetString(ByteTool.Convert("80001F8880E9630000D61FF449")),
         0,
         0
        );
     Assert.AreEqual(ByteTool.Convert(Resources.trapv3), ByteTool.Convert(trap.ToBytes()));
 }
 public void TestToBytes2()
 {
     var privacy = new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("authentication")));
     var trap = new TrapV2Message(
         VersionCode.V3,
         318463383,
         1276263065,
         new OctetString("lextm"),
         new ObjectIdentifier("1.3.6"),
         0,
         new List<Variable>(),
         privacy,
         0x10000,
         new OctetString(ByteTool.Convert("80001F8880E9630000D61FF449")),
         0,
         0
        );
     Assert.AreEqual(ByteTool.Convert(Resources.trapv3auth), ByteTool.Convert(trap.ToBytes()));
 }
 public void TestToBytes3()
 {
     var privacy = new DESPrivacyProvider(new OctetString("privacyphrase"), new MD5AuthenticationProvider(new OctetString("authentication")));
     var trap = new TrapV2Message(
         VersionCode.V3,
         new Header(
             new Integer32(1004947569),
             new Integer32(0x10000),
             new OctetString(new[] {(byte) PrivacyProviderExtension.ToSecurityLevel(privacy)}),
             new Integer32(3)),
         new SecurityParameters(
             new OctetString(ByteTool.Convert("80001F8880E9630000D61FF449")),
             new Integer32(0),
             new Integer32(0),
             new OctetString("lextm"),
             new OctetString(ByteTool.Convert("61A9A486AF4A861BD5C0BB1F")), 
             new OctetString(ByteTool.Convert("0000000069D39B2A"))),
         new Scope(OctetString.Empty, OctetString.Empty,
                   new TrapV2Pdu(
                       234419641,
                       new ObjectIdentifier("1.3.6"),
                       0,
                       new List<Variable>())),
         privacy);         
     byte[] bytes = trap.ToBytes();
     UserRegistry registry = new UserRegistry();
     registry.Add(new OctetString("lextm"), privacy);
     IList<ISnmpMessage> messages = MessageFactory.ParseMessages(bytes, registry);
     Assert.AreEqual(1, messages.Count);
     ISnmpMessage message = messages[0];
     Assert.AreEqual("80001F8880E9630000D61FF449", message.Parameters.EngineId.ToHexString());
     Assert.AreEqual(0, message.Parameters.EngineBoots.ToInt32());
     Assert.AreEqual(0, message.Parameters.EngineTime.ToInt32());
     Assert.AreEqual("lextm", message.Parameters.UserName.ToString());
     Assert.AreEqual("61A9A486AF4A861BD5C0BB1F", message.Parameters.AuthenticationParameters.ToHexString());
     Assert.AreEqual("0000000069D39B2A", message.Parameters.PrivacyParameters.ToHexString());
     Assert.AreEqual("", message.Scope.ContextEngineId.ToHexString()); // SNMP#NET returns string.Empty here.
     Assert.AreEqual("", message.Scope.ContextName.ToHexString());
     Assert.AreEqual(0, message.Scope.Pdu.Variables.Count);
     Assert.AreEqual(1004947569, message.MessageId);
     Assert.AreEqual(234419641, message.RequestId);
 }
 public void Test()
 {
     var mock = new Mock<ISnmpContext>();
     var mock2 = new Mock<IListenerBinding>();
     IList<Variable> v = new List<Variable>();
     var message = new TrapV2Message(0, VersionCode.V2, new OctetString("community"), new ObjectIdentifier("1.3.6"), 0, v);
     mock.Setup(foo => foo.Binding).Returns(mock2.Object);
     mock.Setup(foo => foo.Request).Returns(message);
     mock.Setup(foo => foo.Sender).Returns(new IPEndPoint(IPAddress.Any, 0));
     mock.Setup(foo => foo.CopyRequest(ErrorCode.NoError, 0)).Verifiable("this must be called");
     var handler = new TrapV2MessageHandler();
     Assert.Throws<ArgumentNullException>(() => handler.Handle(null, null));
     Assert.Throws<ArgumentNullException>(() => handler.Handle(mock.Object, null));
     handler.MessageReceived += delegate(object args, TrapV2MessageReceivedEventArgs e)
     {
         Assert.AreEqual(mock2.Object, e.Binding);
         Assert.AreEqual(message, e.TrapV2Message);
         Assert.IsTrue(new IPEndPoint(IPAddress.Any, 0).Equals(e.Sender));
     };
     handler.Handle(mock.Object, new ObjectStore());
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="TrapV2MessageReceivedEventArgs"/> class.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="request">The request.</param>
        /// <param name="binding">The binding.</param>
        public TrapV2MessageReceivedEventArgs(IPEndPoint sender, TrapV2Message request, IListenerBinding binding)
        {
            if (sender == null)
            {
                throw new ArgumentNullException("sender");
            }

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

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

            Sender = sender;
            TrapV2Message = request;
            Binding = binding;
        }
Example #9
0
        public static void SendTrapV2(int requestId, VersionCode version, EndPoint receiver, OctetString community, ObjectIdentifier enterprise, uint timestamp, IList<Variable> variables)
        {
            if (version != VersionCode.V2)
            {
                throw new ArgumentException("Only SNMP v2c is supported", "version");
            }

            var message = new TrapV2Message(requestId, version, community, enterprise, timestamp, variables);
            message.Send(receiver);
        }
Example #10
0
        public static void Main(string[] args)
        {
            IPAddress address = args.Length == 1 ? IPAddress.Parse(args[0]) : IPAddress.Loopback;

            Messenger.SendTrapV1(
                new IPEndPoint(address, 162),
                IPAddress.Loopback,
                new OctetString("public"),
                new ObjectIdentifier(new uint[] { 1, 3, 6 }),
                GenericCode.ColdStart,
                0,
                0,
                new List<Variable>());

            //Thread.Sleep(50);


            Messenger.SendTrapV2(
                0,
                VersionCode.V2,
                new IPEndPoint(address, 162),
                new OctetString("public"),
                new ObjectIdentifier(new uint[] { 1, 3, 6 }),
                0,
                new List<Variable>());
            //Thread.Sleep(50);

            try
            {
                Messenger.SendInform(
                    0,
                    VersionCode.V2,
                    new IPEndPoint(address, 162),
                    new OctetString("public"),
                    new ObjectIdentifier(new uint[] { 1, 3, 6 }),
                    0,
                    new List<Variable>(),
                    2000,
                    null,
                    null);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            try
            {
                Discovery discovery = Messenger.GetNextDiscovery(SnmpType.InformRequestPdu);
                ReportMessage report = discovery.GetResponse(2000, new IPEndPoint(address, 162));

                Messenger.SendInform(
                    0,
                    VersionCode.V3,
                    new IPEndPoint(address, 162),
                    new OctetString("neither"),
                    new ObjectIdentifier(new uint[] { 1, 3, 6 }),
                    0,
                    new List<Variable>(),
                    2000,
                    DefaultPrivacyProvider.DefaultPair,
                    report);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            {
                var trap = new TrapV2Message(
                    VersionCode.V3,
                    528732060,
                    1905687779,
                    new OctetString("neither"),
                    new ObjectIdentifier("1.3.6"),
                    0,
                    new List<Variable>(),
                    DefaultPrivacyProvider.DefaultPair,
                    0x10000,
                    new OctetString(ByteTool.Convert("80001F8880E9630000D61FF449")),
                    0,
                    0);
                trap.Send(new IPEndPoint(address, 162));
            }

            {
                var trap = new TrapV2Message(
                    VersionCode.V3,
                    528732060,
                    1905687779,
                    new OctetString("privacy"),
                    new ObjectIdentifier("1.3.6"),
                    0,
                    new List<Variable>(),
                    new DESPrivacyProvider(
                        new OctetString("privacyphrase"),
                        new MD5AuthenticationProvider(new OctetString("authentication"))),
                    0x10000,
                    new OctetString(ByteTool.Convert("80001F8880E9630000D61FF449")),
                    0,
                    0);
                trap.Send(new IPEndPoint(address, 162));
            }
        }