Ah I see, thanks for clarifying that. Once I have constructed a V3 Trap message as you have shown, what is the procedure to then send that message out to an endpoint?
Regards,
Neil
Regards,
Neil
Container = new UnityContainer().LoadConfiguration("snmptrapd");
var users = this.Container.Resolve<UserRegistry>();
users.Add(new OctetString("neither"), DefaultPrivacyProvider.DefaultPair);
users.Add(new OctetString("authen"), new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("authentication"))));
users.Add(new OctetString("privacy"), new DESPrivacyProvider(new OctetString("privacyphrase"), new MD5AuthenticationProvider(new OctetString("authentication"))));
var trapv1 = Container.Resolve<TrapV1MessageHandler>("TrapV1Handler");
trapv1.MessageReceived += WatcherTrapV1Received;
var trapv2 = Container.Resolve<TrapV2MessageHandler>("TrapV2Handler");
trapv2.MessageReceived += WatcherTrapV2Received;
var inform = Container.Resolve<InformRequestMessageHandler>("InformHandler");
inform.MessageReceived += WatcherInformRequestReceived;
Engine = Container.Resolve<SnmpEngine>();
Engine.Listener.AddBinding(new IPEndPoint(IPAddress.Any, snmpPort));
// The Engine is started later in the code as it is hosted in a service/console app
and here is my Trap sender code: var endpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 162);
//var privacy = DefaultPrivacyProvider.DefaultPair;
var privacy = new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("authentication")));
//var privacy = new DESPrivacyProvider(new OctetString("privacyphrase"), new MD5AuthenticationProvider(new OctetString("authentication")));
var trap = new TrapV2Message(
VersionCode.V3,
528732060,
1905687779,
new OctetString("authen"),
new ObjectIdentifier("1.3.6"),
0,
new List<Variable>(),
privacy,
0x10000,
new OctetString(ByteTool.Convert("80001F8880E9630000D61FF449")),
0,
0
);
trap.Send(endpoint);
As you can see I've tried all three types of PrivacyProvider from the samples but they all return authorizationError (16). Not quite sure what could be going wrong since my usernames, community and privacy phrases all seem to match up? if (parameters.EngineId != Group.EngineId)
{
// not from this engine.
return false;
}
And then noticed that the Engine ID is hard coded within the library! // TODO: make engine ID configurable from outside and unique.
private readonly OctetString _engineId =
new OctetString(new byte[] { 128, 0, 31, 136, 128, 233, 99, 0, 0, 214, 31, 244 });
So after tweaking my Trap message by copying the OctetString construction from above to the engineId parameter, it all started to work! Hurrah!"This variable controls how well-done is the ensuing toast. It
should be on a scale of 1 to 10. Toast made at 10 generally
is considered unfit for human consumption;
toast made at 1 is warmed lightly."
::= {toaster 4}internal static IUnityContainer Container { get; private set; }
static void Main(string[] args)
{
Container = new UnityContainer().LoadConfiguration("snmptrapd");
var users = Container.Resolve<UserRegistry>();
users.Add(new OctetString("??????????"), new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("??????????"))));
var trapv1 = Container.Resolve<TrapV1MessageHandler>("TrapV1Handler");
trapv1.MessageReceived += WatcherTrapV1Received;
var trapv2 = Container.Resolve<TrapV2MessageHandler>("TrapV2Handler");
trapv2.MessageReceived += WatcherTrapV2Received;
var inform = Container.Resolve<InformRequestMessageHandler>("InformHandler");
inform.MessageReceived += WatcherInformRequestReceived;
using (var engine = Container.Resolve<SnmpEngine>())
{
engine.ExceptionRaised += new EventHandler<Lextm.SharpSnmpLib.Messaging.ExceptionRaisedEventArgs>(engine_ExceptionRaised);
IPEndPoint endPoint = new IPEndPoint(new IPAddress(new byte[]{127,0,0,1}), 162);
engine.Listener.AddBinding(endPoint);
engine.Listener.ExceptionRaised += new EventHandler<Lextm.SharpSnmpLib.Messaging.ExceptionRaisedEventArgs>(Listener_ExceptionRaised);
engine.Start();
Console.WriteLine("Bound on " + endPoint.Address + ":" + endPoint.Port);
Console.WriteLine("#SNMP is available at http://sharpsnmplib.codeplex.com");
Console.WriteLine("Press any key to stop . . . ");
Console.Read();
engine.Stop();
}
}
static void Listener_ExceptionRaised(object sender, Lextm.SharpSnmpLib.Messaging.ExceptionRaisedEventArgs e)
{
Console.WriteLine(e.Exception.ToString());
}
static void engine_ExceptionRaised(object sender, Lextm.SharpSnmpLib.Messaging.ExceptionRaisedEventArgs e)
{
Console.WriteLine("Exception: " + e.Exception.ToString());
}
private static void WatcherInformRequestReceived(object sender, InformRequestMessageReceivedEventArgs e)
{
Console.WriteLine("Inform message received");
Console.WriteLine(e.InformRequestMessage);
}
private static void WatcherTrapV2Received(object sender, TrapV2MessageReceivedEventArgs e)
{
Console.WriteLine("Trap V2 message received");
Console.WriteLine(e.TrapV2Message);
}
private static void WatcherTrapV1Received(object sender, TrapV1MessageReceivedEventArgs e)
{
Console.WriteLine("Trap V1 message received");
Console.WriteLine(e.TrapV1Message);
}
This works fine with the Java code. I place (almost) exactly the same code in the original C# project, and it doesn't work. The changes are twofold: while (_running)
System.Threading.Thread.Sleep(500);
after engine.Start(). At the moment, the variable always has the value true, so this is in effect an infinite loop (although in the future we will need to be able to change it to stop the listener). ThreadStart ts = () =>
{
listener.Start();
};
Thread thread = new Thread(ts);
thread.Name = listener.Name;
thread.Start();
This is the only thread that utilizes #SNMP objects and there is only ever a single instance of the thread, simply to isolate it from the rest of the work that the program is doing.Discoverer
class.