So i'm building a snmp trap receiver to receive snmp trap messages. i'm using the sharpsnmp-net package from lexstudios aswell as the sample.engine with pipline from github. I'm creating a simple wpf application to just show messages on screen for testing all in .net 4.7.1.
So for the listening part i roughly used the sample snmpd.
public partial class MainWindow : Window
{
private SnmpEngine _engine;
private const string StrAllUnassigned = "All Unassigned";
public MainWindow()
{
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 setv1 = new SetV1MessageHandler();
var setv1Mapping = new HandlerMapping("v1", "SET", setv1);
var getnextv1 = new GetNextV1MessageHandler();
var getnextv1Mapping = new HandlerMapping("v1", "GETNEXT", getnextv1);
var v1 = new Version1MembershipProvider(new OctetString("public"), new OctetString("public"));
var membership = new ComposedMembershipProvider(new IMembershipProvider[] { v1 });
var handlerFactory = new MessageHandlerFactory(new[]
{
getv1Mapping,
setv1Mapping,
getnextv1Mapping
});
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();
txtIp.Text = @"162"; //port to receive snmp trap
cmbIp.Items.Add(StrAllUnassigned);
foreach (IPAddress address in Dns.GetHostEntry(string.Empty).AddressList.Where(address => !address.IsIPv6LinkLocal))
{
cmbIp.Items.Add(address);
}
cmbIp.SelectedIndex = 0;
}
public void StartListeners()
{
try
{
_engine.Listener.ClearBindings();
int port = int.Parse(txtIp.Text, CultureInfo.InvariantCulture);
/*
if (cmbIp.Text == StrAllUnassigned)
{
if (Socket.OSSupportsIPv4)
{
_engine.Listener.AddBinding(new IPEndPoint(IPAddress.Any, port));
}
if (Socket.OSSupportsIPv6)
{
_engine.Listener.AddBinding(new IPEndPoint(IPAddress.IPv6Any, port));
}
_engine.Start();
if (_engine.Active)
{
MessageBox.Show("Engine activated");
}
return;
}
*/
IPAddress address = IPAddress.Parse(cmbIp.Text);
if (address.AddressFamily == AddressFamily.InterNetwork)
{
if (!Socket.OSSupportsIPv4)
{
MessageBox.Show(Listener.ErrorIPv4NotSupported);
return;
}
_engine.Listener.AddBinding(new IPEndPoint(address, port));
_engine.Listener.MessageReceived += Listener_MessageReceived;
_engine.Start();
if (_engine.Active)
{
MessageBox.Show("Engine activated");
}
return;
}
if (!Socket.OSSupportsIPv6)
{
MessageBox.Show(Listener.ErrorIPv6NotSupported);
return;
}
_engine.Listener.AddBinding(new IPEndPoint(address, port));
_engine.Start();
}
catch (Exception ex)
{
MessageBox.Show("Exception has been thrown in start: " + ex);
}
}
private void Listener_MessageReceived(object sender, MessageReceivedEventArgs e)
{
MessageBox.Show("message received");
}
private void StopListeners()
{
_engine.Stop();
_engine.Dispose();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
//send trap
IPAddress ip = IPAddress.Parse("127.0.0.1");
Messenger.SendTrapV1(
new IPEndPoint(ip, 162),
IPAddress.Loopback, // here should be IP of the current machine.
new OctetString("public"),
new ObjectIdentifier(new uint[] { 1, 3, 6 }),
GenericCode.ColdStart,
0,
0,
new List<Variable>());
}
private void Start_Click(object sender, RoutedEventArgs e)
{
try
{
StartListeners();
}
catch (PortInUseException ex)
{
MessageBox.Show(@"Port is already in use: " + ex.Endpoint, @"Error");
}
}
private void stop_Click(object sender, RoutedEventArgs e)
{
if (_engine.Active)
{
try
{
_engine.Listener.MessageReceived -= Listener_MessageReceived;
StopListeners();
}
catch (Exception ex)
{
MessageBox.Show("exception in stop: " + ex);
}
}
}
}
So to receive snmp trap messages you also got to send one. I have got a textbox with port number combobox with ip address. a send trap button and start and stop button.
Send a trap works perfectly i can verify in wireshark i do have a snmp packet however icmp answer with port unreachable which means no one is listening.-> issue 1
When activating the engine i don't have any errors but regarding the icmp i checked in cmd with command : netstat -an to check if something was listening on port 162 but bad luck.
How can i verify the engine is working and or something is listening? I subscribed to the message_received event but i don't receive anything which could lead to the fact that no one is listening.
And last but not least when I press the stop button i do get a socketexception, but don't receive any of my own messages from try catch loops -> Should I assume this is in the package?
How to solve this problem. I want just to receive snmpv1 trap messages and the store them in a datatable so far.