0

I'm using the lib SnmpSharpNet in .NET 7 with C# 10 to retreive all the snmp traps in the network. The code is almost all copied from the official documentation and work without problems on linux (Ubuntu LTS), without any modification to the system.

On windows, I'm unable to capture SNMP traps, that's a problem cause I can't debug my application using my pc.

That's the code:

using SnmpSharpNet;
    
    Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 162);
    EndPoint ep = (EndPoint)ipep;
    socket.Bind(ep);
    // Disable timeout processing. Just block until packet is received
    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 0);
    bool run = true;
    int inlen = -1;
    while (run)
    {
        byte[] indata = new byte[16 * 1024];
        // 16KB receive buffer int inlen = 0;
        IPEndPoint peer = new IPEndPoint(IPAddress.Any, 0);
        EndPoint inep = (EndPoint)peer;
        try
        {
            inlen = socket.ReceiveFrom(indata, ref inep);
        }
        catch (Exception ex)
        {
            inlen = -1;
        }
        if (inlen > 0)
        {
            // Check protocol version int
            int ver = SnmpPacket.GetProtocolVersion(indata, inlen);
            if (ver == (int)SnmpVersion.Ver1)
            {
                SnmpV1TrapPacket pkt = new SnmpV1TrapPacket();
                pkt.decode(indata, inlen);
                //Do things...
            }
            else
            {
                SnmpV2Packet pkt = new SnmpV2Packet();
                pkt.decode(indata, inlen);
                //Do things...
            }
        }
        else
        {
            if (inlen == 0)
                Trace.WriteLine("Zero length packet received.");
        }
    }

Now, first time I tried, I haven't changed any setting on windows 11, searching online, I found out that Windows 11 does not support SNMP by default, so I installed the SNMP service from the Settings > Apps > Optional functionalities > Add optional functionality but the code still not receive any trap.

Majico
  • 475
  • 1
  • 6
  • 20

1 Answers1

1

Found the solution while I was writing the question, basically, after the installation of the SNMP service from Settings > Apps > Optional functionalities > Add optional functionality, windows automatically started the "SNMP service" service and the "SNMP Traps" service and the "SNMP Traps" service was preventing other service/application to use the port 162, so I disabled it and now the code works.

Majico
  • 475
  • 1
  • 6
  • 20
  • But if this is the cause, your program should crash and raise an exception as it couldn't hook to port 162. However, you didn't mention that part in the question body. Besides, port 162 is privileged, so your code must run as administrator or be granted the access by Windows Firewall, or it cannot hook to port 162 either. – Lex Li Feb 21 '23 at 19:29
  • I didn't add too much information about firewall and other configuration I tried because, as said in the answer, I found the solution while i was writing the question, but the way , when I ran for the first time the app, windows prompt a window asking if i want to add a rule in firewall for the app, this didn't work (because it wasn't the problem) and I tried also disabling the windows firewall. – Majico Feb 22 '23 at 14:49
  • This block of code was inside a `try catch` and didn't put the breakpoint in the catch but only in the line `if (inlen > 0)` so I didn't notice, I found the issue running the same code without a `try catch` in another pc... Fortunately it was a stupid mistake. – Majico Feb 22 '23 at 14:53