Exemplo n.º 1
0
        public void NoSuchName()
        {
            var handler = new GetNextV1MessageHandler();
            var store   = new ObjectStore();

            store.Add(new SysDescr());
            var context = SnmpContextFactory.Create(
                new GetNextRequestMessage(
                    300,
                    VersionCode.V1,
                    new OctetString("lextm"),
                    new List <Variable>
            {
                new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.2.0"))
            }
                    ),
                new IPEndPoint(IPAddress.Loopback, 100),
                new UserRegistry(),
                null,
                null);

            handler.Handle(context, store);
            var noSuchName = (ResponseMessage)context.Response;

            Assert.AreEqual(ErrorCode.NoSuchName, noSuchName.ErrorStatus);
        }
        public void NoError()
        {
            var handler = new GetNextV1MessageHandler();
            var context = SnmpContextFactory.Create(
                new GetNextRequestMessage(
                    300,
                    VersionCode.V1,
                    new OctetString("lextm"),
                    new List <Variable>
            {
                new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0"))
            }
                    ),
                new IPEndPoint(IPAddress.Loopback, 100),
                new UserRegistry(),
                null,
                null);
            var store = new ObjectStore();

            store.Add(new SysDescr());
            store.Add(new SysObjectId());
            Assert.Throws <ArgumentNullException>(() => handler.Handle(null, null));
            Assert.Throws <ArgumentNullException>(() => handler.Handle(context, null));
            handler.Handle(context, store);
            var noerror = (ResponseMessage)context.Response;

            Assert.Equal(ErrorCode.NoError, noerror.ErrorStatus);
            Assert.Equal(new ObjectIdentifier("1.3.6.1.2.1.1.2.0"), noerror.Variables()[0].Id);
        }
Exemplo n.º 3
0
        public void GenError()
        {
            var handler = new GetNextV1MessageHandler();
            var mock    = new Mock <ScalarObject>(new ObjectIdentifier("1.3.6.1.2.1.1.2.0"));

            mock.Setup(foo => foo.Data).Throws <Exception>();
            mock.Setup(foo => foo.MatchGet(new ObjectIdentifier("1.3.6.1.2.1.1.2.0"))).Returns(mock.Object);
            mock.Setup(foo => foo.MatchGetNext(new ObjectIdentifier("1.3.6.1.2.1.1.1.0"))).Returns(mock.Object);
            var store = new ObjectStore();

            store.Add(new SysDescr());
            store.Add(mock.Object);
            var context = SnmpContextFactory.Create(
                new GetNextRequestMessage(
                    300,
                    VersionCode.V1,
                    new OctetString("lextm"),
                    new List <Variable>
            {
                new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0"))
            }
                    ),
                new IPEndPoint(IPAddress.Loopback, 100),
                new UserRegistry(),
                null,
                null);

            handler.Handle(context, store);
            var genError = (ResponseMessage)context.Response;

            Assert.AreEqual(ErrorCode.GenError, genError.ErrorStatus);
        }
 public void GenError()
 {
     var handler = new GetNextV1MessageHandler();
     var mock = new Mock<ScalarObject>(new ObjectIdentifier("1.3.6.1.2.1.1.2.0"));
     mock.Setup(foo => foo.Data).Throws<Exception>();
     mock.Setup(foo => foo.MatchGet(new ObjectIdentifier("1.3.6.1.2.1.1.2.0"))).Returns(mock.Object);
     mock.Setup(foo => foo.MatchGetNext(new ObjectIdentifier("1.3.6.1.2.1.1.1.0"))).Returns(mock.Object);
     var store = new ObjectStore();
     store.Add(new SysDescr());
     store.Add(mock.Object);
     var context = SnmpContextFactory.Create(
         new GetNextRequestMessage(
             300,
             VersionCode.V1,
             new OctetString("lextm"),
             new List<Variable>
                 {
                     new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0"))
                 }
             ),
         new IPEndPoint(IPAddress.Loopback, 100),
         new UserRegistry(),
         null,
         null);
     handler.Handle(context, store);
     var genError = (ResponseMessage)context.Response;
     Assert.AreEqual(ErrorCode.GenError, genError.ErrorStatus);
 }
 public void NoError()
 {
     var handler = new GetNextV1MessageHandler();
     var context = SnmpContextFactory.Create(
         new GetNextRequestMessage(
             300,
             VersionCode.V1,
             new OctetString("lextm"),
             new List<Variable>
                 {
                     new Variable(new ObjectIdentifier("1.3.6.1.2.1.1.1.0"))
                 }
             ),
         new IPEndPoint(IPAddress.Loopback, 100),
         new UserRegistry(),
         null,
         null);
     var store = new ObjectStore();
     store.Add(new SysDescr());
     store.Add(new SysObjectId());
     Assert.Throws<ArgumentNullException>(() => handler.Handle(null, null));
     Assert.Throws<ArgumentNullException>(() => handler.Handle(context, null));
     handler.Handle(context, store);
     var noerror = (ResponseMessage)context.Response;
     Assert.AreEqual(ErrorCode.NoError, noerror.ErrorStatus);
     Assert.AreEqual(new ObjectIdentifier("1.3.6.1.2.1.1.2.0"), noerror.Variables()[0].Id);
 }
Exemplo n.º 6
0
        private const int MaxTimeout = 5 * 60 * 1000; // 5 minutes


        private SnmpEngine CreateEngine(bool timeout = false)
        {
            // TODO: this is a hack. review it later.
            var store = new ObjectStore();

            store.Add(new SysDescr());
            store.Add(new SysObjectId());
            store.Add(new SysUpTime());
            store.Add(new SysContact());
            store.Add(new SysName());
            store.Add(new SysLocation());
            store.Add(new SysServices());
            store.Add(new SysORLastChange());
            store.Add(new SysORTable());
            store.Add(new IfNumber());
            store.Add(new IfTable());
            if (timeout)
            {
                store.Add(new TimeoutObject());
            }

            var users = new UserRegistry();

            users.Add(new OctetString("neither"), DefaultPrivacyProvider.DefaultPair);
            users.Add(new OctetString("authen"), new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("authentication"))));
            if (DESPrivacyProvider.IsSupported)
            {
                users.Add(new OctetString("privacy"), new DESPrivacyProvider(new OctetString("privacyphrase"),
                                                                             new MD5AuthenticationProvider(new OctetString("authentication"))));
            }

            var getv1        = new GetV1MessageHandler();
            var getv1Mapping = new HandlerMapping("v1", "GET", getv1);

            var getv23        = new GetMessageHandler();
            var getv23Mapping = new HandlerMapping("v2,v3", "GET", getv23);

            var setv1        = new SetV1MessageHandler();
            var setv1Mapping = new HandlerMapping("v1", "SET", setv1);

            var setv23        = new SetMessageHandler();
            var setv23Mapping = new HandlerMapping("v2,v3", "SET", setv23);

            var getnextv1        = new GetNextV1MessageHandler();
            var getnextv1Mapping = new HandlerMapping("v1", "GETNEXT", getnextv1);

            var getnextv23        = new GetNextMessageHandler();
            var getnextv23Mapping = new HandlerMapping("v2,v3", "GETNEXT", getnextv23);

            var getbulk        = new GetBulkMessageHandler();
            var getbulkMapping = new HandlerMapping("v2,v3", "GETBULK", getbulk);

            var v1             = new Version1MembershipProvider(new OctetString("public"), new OctetString("public"));
            var v2             = new Version2MembershipProvider(new OctetString("public"), new OctetString("public"));
            var v3             = new Version3MembershipProvider();
            var membership     = new ComposedMembershipProvider(new IMembershipProvider[] { v1, v2, v3 });
            var handlerFactory = new MessageHandlerFactory(new[]
            {
                getv1Mapping,
                getv23Mapping,
                setv1Mapping,
                setv23Mapping,
                getnextv1Mapping,
                getnextv23Mapping,
                getbulkMapping
            });

            var pipelineFactory = new SnmpApplicationFactory(store, membership, handlerFactory);
            var listener        = new Listener {
                Users = users
            };

            listener.ExceptionRaised += (sender, e) => { Assert.True(false, "unexpected exception"); };
            return(new SnmpEngine(pipelineFactory, listener, new EngineGroup()));
        }
        public MainForm()
        {
            // TODO: this is a hack. review it later.
            var store = new ObjectStore();

            store.Add(new SysDescr());
            store.Add(new SysObjectId());
            store.Add(new SysUpTime());
            store.Add(new SysContact());
            store.Add(new SysName());
            store.Add(new SysLocation());
            store.Add(new SysServices());
            store.Add(new SysORLastChange());
            store.Add(new SysORTable());
            store.Add(new IfNumber());
            store.Add(new IfTable());

            var users = new EngineUserRegistry();

            users.Add(new OctetString("neither"), DefaultPrivacyProvider.DefaultPair);
            users.Add(new OctetString("authen"), new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("authentication"))));
            if (DESPrivacyProvider.IsSupported)
            {
                users.Add(new OctetString("privacy"), new DESPrivacyProvider(new OctetString("privacyphrase"),
                                                                             new MD5AuthenticationProvider(new OctetString("authentication"))));
            }

            if (AESPrivacyProviderBase.IsSupported)
            {
                users.Add(new OctetString("aes"), new AESPrivacyProvider(new OctetString("privacyphrase"), new MD5AuthenticationProvider(new OctetString("authentication"))));
                users.Add(new OctetString("aes192"), new AES192PrivacyProvider(new OctetString("privacyphrase"), new MD5AuthenticationProvider(new OctetString("authentication"))));
                users.Add(new OctetString("aes256"), new AES256PrivacyProvider(new OctetString("privacyphrase"), new MD5AuthenticationProvider(new OctetString("authentication"))));
            }

            var getv1        = new GetV1MessageHandler();
            var getv1Mapping = new HandlerMapping("v1", "GET", getv1);

            var getv23        = new GetMessageHandler();
            var getv23Mapping = new HandlerMapping("v2,v3", "GET", getv23);

            var setv1        = new SetV1MessageHandler();
            var setv1Mapping = new HandlerMapping("v1", "SET", setv1);

            var setv23        = new SetMessageHandler();
            var setv23Mapping = new HandlerMapping("v2,v3", "SET", setv23);

            var getnextv1        = new GetNextV1MessageHandler();
            var getnextv1Mapping = new HandlerMapping("v1", "GETNEXT", getnextv1);

            var getnextv23        = new GetNextMessageHandler();
            var getnextv23Mapping = new HandlerMapping("v2,v3", "GETNEXT", getnextv23);

            var getbulk        = new GetBulkMessageHandler();
            var getbulkMapping = new HandlerMapping("v2,v3", "GETBULK", getbulk);

            var v1             = new Version1MembershipProvider(new OctetString("public"), new OctetString("public"));
            var v2             = new Version2MembershipProvider(new OctetString("public"), new OctetString("public"));
            var v3             = new Version3MembershipProvider();
            var membership     = new ComposedMembershipProvider(new IMembershipProvider[] { v1, v2, v3 });
            var handlerFactory = new MessageHandlerFactory(new[]
            {
                getv1Mapping,
                getv23Mapping,
                setv1Mapping,
                setv23Mapping,
                getnextv1Mapping,
                getnextv23Mapping,
                getbulkMapping
            });

            var pipelineFactory = new SnmpApplicationFactory(new RollingLogger(), store, membership, handlerFactory);

            _engine = new SnmpEngine(pipelineFactory, new Listener {
                Users = users
            }, new EngineGroup());
            _engine.ExceptionRaised += (sender, e) => MessageBox.Show(e.Exception.ToString());

            InitializeComponent();
            if (PlatformSupport.Platform == PlatformType.Windows)
            {
                // FIXME: work around a Mono WinForms issue.
                Icon = Properties.Resources.network_server;
            }

            toolStripButton1.Image = Properties.Resources.media_playback_start;
            tstxtPort.Text         = @"161";
            tscbIP.Items.Add(StrAllUnassigned);
            foreach (IPAddress address in Dns.GetHostEntry(string.Empty).AddressList.Where(address => !address.IsIPv6LinkLocal))
            {
                tscbIP.Items.Add(address);
            }

            tscbIP.SelectedIndex = 0;
        }
Exemplo n.º 8
0
        public SnmpClient()
        {
            var store = new ObjectStore();

            store.Add(new SysDescr());
            store.Add(new SysObjectId());
            store.Add(new SysUpTime());
            store.Add(new SysContact());
            store.Add(new SysName());
            store.Add(new SysLocation());
            store.Add(new SysServices());
            store.Add(new SysORLastChange());
            store.Add(new SysORTable());
            store.Add(new IfNumber());
            store.Add(new IfTable());

            var users = new UserRegistry();

            users.Add(new OctetString("neither"), DefaultPrivacyProvider.DefaultPair);
            users.Add(new OctetString("authen"), new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("authentication"))));

            var getv1        = new GetV1MessageHandler();
            var getv1Mapping = new HandlerMapping("v1", "GET", getv1);

            var getv23        = new GetMessageHandler();
            var getv23Mapping = new HandlerMapping("v2,v3", "GET", getv23);

            var setv1        = new SetV1MessageHandler();
            var setv1Mapping = new HandlerMapping("v1", "SET", setv1);

            var setv23        = new SetMessageHandler();
            var setv23Mapping = new HandlerMapping("v2,v3", "SET", setv23);

            var getnextv1        = new GetNextV1MessageHandler();
            var getnextv1Mapping = new HandlerMapping("v1", "GETNEXT", getnextv1);

            var getnextv23        = new GetNextMessageHandler();
            var getnextv23Mapping = new HandlerMapping("v2,v3", "GETNEXT", getnextv23);

            var getbulk        = new GetBulkMessageHandler();
            var getbulkMapping = new HandlerMapping("v2,v3", "GETBULK", getbulk);

            var v1             = new Version1MembershipProvider(new OctetString("public"), new OctetString("public"));
            var v2             = new Version2MembershipProvider(new OctetString("public"), new OctetString("public"));
            var v3             = new Version3MembershipProvider();
            var membership     = new ComposedMembershipProvider(new IMembershipProvider[] { v1, v2, v3 });
            var handlerFactory = new MessageHandlerFactory(new[]
            {
                getv1Mapping,
                getv23Mapping,
                setv1Mapping,
                setv23Mapping,
                getnextv1Mapping,
                getnextv23Mapping,
                getbulkMapping
            });

            var pipelineFactory = new SnmpApplicationFactory(store, membership, handlerFactory);

            _engine = new SnmpEngine(pipelineFactory, new Listener {
                Users = users
            }, new EngineGroup());
            _engine.Listener.MessageReceived += Listener_MessageReceived;

            _engine.ExceptionRaised += (sender, e) =>
            {
                Debugger.Break();
            };
        }
Exemplo n.º 9
0
        public MainPage()
        {
            ObjectStore store = new ObjectStore();

            store.Add(new SysDescr());
            store.Add(new SysObjectId());
            store.Add(new SysUpTime());
            store.Add(new SysContact());
            store.Add(new SysName());
            store.Add(new SysLocation());
            store.Add(new SysServices());
            store.Add(new SysORLastChange());
            store.Add(new SysORTable());
            store.Add(new IfNumber());
            store.Add(new IfTable());

            UserRegistry users = new UserRegistry();

            users.Add(new OctetString("neither"), DefaultPrivacyProvider.DefaultPair);
            users.Add(new OctetString("authen"), new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("authentication"))));
            if (DESPrivacyProvider.IsSupported)
            {
                users.Add(new OctetString("privacy"), new DESPrivacyProvider(new OctetString("privacyphrase"),
                                                                             new MD5AuthenticationProvider(new OctetString("authentication"))));
            }

            if (AESPrivacyProviderBase.IsSupported)
            {
                users.Add(new OctetString("aes"), new AESPrivacyProvider(new OctetString("privacyphrase"), new MD5AuthenticationProvider(new OctetString("authentication"))));
                users.Add(new OctetString("aes192"), new AES192PrivacyProvider(new OctetString("privacyphrase"), new MD5AuthenticationProvider(new OctetString("authentication"))));
                users.Add(new OctetString("aes256"), new AES256PrivacyProvider(new OctetString("privacyphrase"), new MD5AuthenticationProvider(new OctetString("authentication"))));
            }

            GetV1MessageHandler getv1        = new GetV1MessageHandler();
            HandlerMapping      getv1Mapping = new HandlerMapping("v1", "GET", getv1);

            GetMessageHandler getv23        = new GetMessageHandler();
            HandlerMapping    getv23Mapping = new HandlerMapping("v2,v3", "GET", getv23);

            SetV1MessageHandler setv1        = new SetV1MessageHandler();
            HandlerMapping      setv1Mapping = new HandlerMapping("v1", "SET", setv1);

            SetMessageHandler setv23        = new SetMessageHandler();
            HandlerMapping    setv23Mapping = new HandlerMapping("v2,v3", "SET", setv23);

            GetNextV1MessageHandler getnextv1        = new GetNextV1MessageHandler();
            HandlerMapping          getnextv1Mapping = new HandlerMapping("v1", "GETNEXT", getnextv1);

            GetNextMessageHandler getnextv23        = new GetNextMessageHandler();
            HandlerMapping        getnextv23Mapping = new HandlerMapping("v2,v3", "GETNEXT", getnextv23);

            GetBulkMessageHandler getbulk        = new GetBulkMessageHandler();
            HandlerMapping        getbulkMapping = new HandlerMapping("v2,v3", "GETBULK", getbulk);

            Version1MembershipProvider v1             = new Version1MembershipProvider(new OctetString("public"), new OctetString("public"));
            Version2MembershipProvider v2             = new Version2MembershipProvider(new OctetString("public"), new OctetString("public"));
            Version3MembershipProvider v3             = new Version3MembershipProvider();
            ComposedMembershipProvider membership     = new ComposedMembershipProvider(new IMembershipProvider[] { v1, v2, v3 });
            MessageHandlerFactory      handlerFactory = new MessageHandlerFactory(new[]
            {
                getv1Mapping,
                getv23Mapping,
                setv1Mapping,
                setv23Mapping,
                getnextv1Mapping,
                getnextv23Mapping,
                getbulkMapping
            });

            SnmpApplicationFactory pipelineFactory = new SnmpApplicationFactory(new RollingLogger(), store, membership, handlerFactory);

            _engine = new SnmpEngine(pipelineFactory, new Listener {
                Users = users
            }, new EngineGroup());
            _engine.ExceptionRaised += (sender, e) => DisplayAlert("Exception Raised", e.Exception.ToString(), "OK");

            InitializeComponent();

            IpPicker.Items.Add("All Unassigned");
            foreach (IPAddress address in Dns.GetHostEntry(string.Empty).AddressList.Where(address => !address.IsIPv6LinkLocal))
            {
                IpPicker.Items.Add(address.ToString());
            }
            IpPicker.SelectedItem = IpPicker.Items.FirstOrDefault();
        }
Exemplo n.º 10
0
        public static void Main(string[] args)
        {
            if (args.Length != 0)
            {
                return;
            }

            var store = new ObjectStore();

            store.Add(new SysDescr());
            store.Add(new SysObjectId());
            store.Add(new SysUpTime());
            store.Add(new SysContact());
            store.Add(new SysName());
            store.Add(new SysLocation());
            store.Add(new SysServices());
            store.Add(new SysORLastChange());
            store.Add(new SysORTable());
            store.Add(new IfNumber());
            store.Add(new IfTable());

            var users = new UserRegistry();

            users.Add(new OctetString("neither"), DefaultPrivacyProvider.DefaultPair);
            users.Add(new OctetString("authen"), new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("authentication"))));
            if (DESPrivacyProvider.IsSupported)
            {
                users.Add(new OctetString("privacy"), new DESPrivacyProvider(new OctetString("privacyphrase"), new MD5AuthenticationProvider(new OctetString("authentication"))));
            }

            if (AESPrivacyProviderBase.IsSupported)
            {
                users.Add(new OctetString("aes"), new AESPrivacyProvider(new OctetString("privacyphrase"), new MD5AuthenticationProvider(new OctetString("authentication"))));
                users.Add(new OctetString("aes192"), new AES192PrivacyProvider(new OctetString("privacyphrase"), new MD5AuthenticationProvider(new OctetString("authentication"))));
                users.Add(new OctetString("aes256"), new AES256PrivacyProvider(new OctetString("privacyphrase"), new MD5AuthenticationProvider(new OctetString("authentication"))));
            }

            var getv1        = new GetV1MessageHandler();
            var getv1Mapping = new HandlerMapping("v1", "GET", getv1);

            var getv23        = new GetMessageHandler();
            var getv23Mapping = new HandlerMapping("v2,v3", "GET", getv23);

            var setv1        = new SetV1MessageHandler();
            var setv1Mapping = new HandlerMapping("v1", "SET", setv1);

            var setv23        = new SetMessageHandler();
            var setv23Mapping = new HandlerMapping("v2,v3", "SET", setv23);

            var getnextv1        = new GetNextV1MessageHandler();
            var getnextv1Mapping = new HandlerMapping("v1", "GETNEXT", getnextv1);

            var getnextv23        = new GetNextMessageHandler();
            var getnextv23Mapping = new HandlerMapping("v2,v3", "GETNEXT", getnextv23);

            var getbulk        = new GetBulkMessageHandler();
            var getbulkMapping = new HandlerMapping("v2,v3", "GETBULK", getbulk);

            var v1             = new Version1MembershipProvider(new OctetString("public"), new OctetString("public"));
            var v2             = new Version2MembershipProvider(new OctetString("public"), new OctetString("public"));
            var v3             = new Version3MembershipProvider();
            var membership     = new ComposedMembershipProvider(new IMembershipProvider[] { v1, v2, v3 });
            var handlerFactory = new MessageHandlerFactory(new[]
            {
                getv1Mapping,
                getv23Mapping,
                setv1Mapping,
                setv23Mapping,
                getnextv1Mapping,
                getnextv23Mapping,
                getbulkMapping
            });

            var pipelineFactory = new SnmpApplicationFactory(store, membership, handlerFactory);

            using (var engine = new SnmpEngine(pipelineFactory, new Listener {
                Users = users
            }, new EngineGroup()))
            {
                engine.Listener.AddBinding(new IPEndPoint(IPAddress.Any, 161));
                engine.Listener.ExceptionRaised += Engine_ExceptionRaised;
                engine.Listener.MessageReceived += RequestReceived;
                engine.Start();
                Console.WriteLine("#SNMP is available at https://sharpsnmp.com");
                Console.WriteLine("Press any key to stop . . . ");
                Console.Read();
                engine.Stop();
            }
        }