public async Task TestTrapV2HandlerWithV3MessageAndNoEngineId() { var manualEvent = new ManualResetEventSlim(); // TODO: this is a hack. review it later. var engineId = new OctetString(ByteTool.Convert("80001F8880E9630000D61FF449")); 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 count = 0; var trapv1 = new TrapV1MessageHandler(); var trapv1Mapping = new HandlerMapping("v1", "TRAPV1", trapv1); var trapv2 = new TrapV2MessageHandler(); trapv2.MessageReceived += (sender, args) => { count++; }; var trapv2Mapping = new HandlerMapping("v2,v3", "TRAPV2", trapv2); var inform = new InformRequestMessageHandler(); var informMapping = new HandlerMapping("v2,v3", "INFORM", inform); var store = new ObjectStore(); 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[] { trapv1Mapping, trapv2Mapping, informMapping }); var logger = new TestLogger(); logger.Handler = (obj, args) => { manualEvent.Set(); }; var pipelineFactory = new SnmpApplicationFactory(logger, store, membership, handlerFactory); var group = new EngineGroup(); var engine = new SnmpEngine(pipelineFactory, new Listener { Users = users }, group); var daemonEndPoint = new IPEndPoint(IPAddress.Loopback, port.NextId); engine.Listener.AddBinding(daemonEndPoint); engine.Start(); try { var privacy = new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("authentication"))); var trap = new TrapV2Message( VersionCode.V3, 1004947569, 234419641, new OctetString("authen"), new ObjectIdentifier("1.3.6"), 0, new List <Variable>(), privacy, 0x10000, new OctetString(ByteTool.Convert("80001F8880E9630000D61FF450")), 0, 0); await trap.SendAsync(daemonEndPoint); manualEvent.Wait(); Assert.Equal(0, count); Assert.Equal(new Counter32(1), group.UnknownEngineId.Data); } finally { if (SnmpMessageExtension.IsRunningOnWindows) { engine.Stop(); } } }
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 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(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(); } }
public SnmpClient() { // TODO: Based on sample 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()); }
private const int MaxTimeout = 5 * 60 * 1000; // 5 minutes private SnmpEngine CreateEngine(bool timeout = false, bool max255chars = 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()); } if (max255chars) { store.Add(new Max255CharsObject()); } 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 async Task TestInformV2HandlerWithV3MessageDES() { var manualEvent = new ManualResetEventSlim(); // TODO: this is a hack. review it later. 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")))); } else { users.Add( new OctetString("privacy"), new BouncyCastle.BouncyCastleDESPrivacyProvider( new OctetString("privacyphrase"), new MD5AuthenticationProvider(new OctetString("authentication")))); } var count = 0; var trapv1 = new TrapV1MessageHandler(); var trapv1Mapping = new HandlerMapping("v1", "TRAPV1", trapv1); var trapv2 = new TrapV2MessageHandler(); var trapv2Mapping = new HandlerMapping("v2,v3", "TRAPV2", trapv2); var inform = new InformRequestMessageHandler(); inform.MessageReceived += (sender, args) => { count++; manualEvent.Set(); }; var informMapping = new HandlerMapping("v2,v3", "INFORM", inform); var store = new ObjectStore(); 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[] { trapv1Mapping, trapv2Mapping, informMapping }); var pipelineFactory = new SnmpApplicationFactory(store, membership, handlerFactory); var engine = new SnmpEngine(pipelineFactory, new Listener { Users = users }, new EngineGroup()); var daemonEndPoint = new IPEndPoint(IPAddress.Loopback, port.NextId); engine.Listener.AddBinding(daemonEndPoint); engine.Listener.ExceptionRaised += (sender, e) => { Assert.True(false, "unhandled exception"); }; engine.Listener.MessageReceived += (sender, e) => { Console.WriteLine(e.Message); }; engine.Start(); try { Discovery discovery = Messenger.GetNextDiscovery(SnmpType.InformRequestPdu); ReportMessage report = discovery.GetResponse(2000, daemonEndPoint); await Messenger.SendInformAsync(1, VersionCode.V3, daemonEndPoint, new OctetString("privacy"), OctetString.Empty, new ObjectIdentifier("1.3.6.1"), 500, new List <Variable>(), DESPrivacyProvider.IsSupported ?(IPrivacyProvider) new DESPrivacyProvider( new OctetString("privacyphrase"), new MD5AuthenticationProvider(new OctetString("authentication"))) : new BouncyCastle.BouncyCastleDESPrivacyProvider( new OctetString("privacyphrase"), new MD5AuthenticationProvider(new OctetString("authentication"))), report); manualEvent.Wait(); Assert.Equal(1, count); } finally { if (SnmpMessageExtension.IsRunningOnWindows) { engine.Stop(); } } }
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 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(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; } actEnabled.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; }