Re: Getting listening port number while processing request

Teus Benschop <[email protected]> Sun, 10 Sep 2023 08:48:26 +0200
Newsgroups gmane.network.net-snmp.devel
Message-ID <CAFueZ4yKDW54kxvp6P1JmON3BgtiqXnhCxuZUdMye+sCZ0TpAg@mail.gmail.com>
On Fri, 8 Sept 2023 at 08:27, Craig Small <[email protected]> wrote:
> I ran snmpd as
> snmpd -f -Le -Dnetsnmp_udp 2000,3000
> and got
> netsnmp_udp: recvfrom fd 6 got 41 bytes (from UDP: [127.0.0.1]:36600->[127.0.0.1]:2000)
> or port 3000 if I targeted that.
[...]

Hi, thank you for that. Initially I felt like "wow, how could I have
overlooked this". But then I realized that I had omitted some
information in the question, which is this: I run the SNMP daemon
listening on TCP instead of on UDP.

Running it with debug tokens "-Dnetsnmp_tcp,netsnmp_tcpbase" gives
this output on TCP:

netsnmp_tcpbase: recv fd 9 got 2461 bytes
Connection from TCP: [192.168.135.118]:46836->[0.0.0.0]:0

It means that it outputs the remote TCP port, and outputs the local,
listening TCP port as port 0.
It listens on port 2161 and 2162.

Since it is function "getsockname" that gets the port number from a
file descriptor, I searched the library for that call:

$ grep -R getsockname *
snmpSSHDomain.c:            if(getsockname(t->sock, to, &tolen) != 0){
snmpUDPBaseDomain.c:        int r2 = getsockname(s, dstip, dstlen);
snmpUDPIPv4BaseDomain.c:    rc = getsockname(t->sock, (struct
sockaddr*)&addr_pair->local_addr,
snmpUDPIPv6Domain.c:    rc = getsockname(t->sock, (struct
sockaddr*)&addr_pair->local_addr,
snmpUnixDomain.c:        if(getsockname(t->sock, to, &tolen) != 0){

It looks like it outputs the port number on domains SSH, UDP, and
Unix, but not on domain TCP.

If this analysis is correct, could this be translated into a feature
request for any next version of the Net-SNMP library?

Thanks for the hints,

Teus Benschop