SNMP++: Receive traps via IPv4 and IPv6

When I provide IPv4 and IPv6 address to constructor, e.g. “0.0.0.0” and “::”, I can only receive traps via IPv4 and not via IPv6. According to the constructor code, this seems to be the expected behaviour, because it copies the provided IPv4 address (addr_v4) to Snmp::listen_address.

I my opinion traps should be received via IPv4 and IPv6 if both addresses are provided to the constructor.
Is this a bug or a design decision? If not the following small patch would fix the issue:

From a8ce6ae8cf6e2166b46bbeaabda524e342105dfa Mon Sep 17 00:00:00 2001
From: amartin755 <netnag@mailbox.org>
Date: Tue, 2 Jan 2024 20:32:54 +0100
Subject: [PATCH] receive traps via IPv4 and IPv6 if both are enabled

---
 src/uxsnmp.cpp | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/uxsnmp.cpp b/src/uxsnmp.cpp
index 4b4e520..24a7e50 100644
--- a/src/uxsnmp.cpp
+++ b/src/uxsnmp.cpp
@@ -686,9 +686,10 @@ Snmp::Snmp( int &status,  const UdpAddress& addr_v4,
 {
   IpAddress *addresses[2];
 
-  listen_address = addr_v4;
+  listen_address = addr_v6;
+  IpAddress address_v4((IpAddress)addr_v4);
   IpAddress address_v6((IpAddress)addr_v6);
-  addresses[0] = &listen_address;
+  addresses[0] = &address_v4;
   addresses[1] = &address_v6;
 
   init(status, addresses, addr_v4.get_port(), addr_v6.get_port());
-- 
2.43.0

Best regards
Andreas

Hi,

unfortunately the trap listening code only uses one socket, so if the IPv6 listening address does not include the IPv4 address, the packets to the IPv4 address will no longer be received.

For the addresses you use (“::” and “0.0.0.0”), it would be easier to just use the IPv6 address “::”, as such a socket will also be able to send and receive IPv4 packets.

I am unsure if we should set the listen_address to the IPv6 address in this constructor…

Kind regards,
Jochen

Hello Jochen,

yes, that’s the solution. Life can be so easy. Thank you.

Best regards
Andreas

Hello again Jochen,

unfortunately your suggested solution does only work on platforms where IPv6 sockets by default bind to IPv4 too. Means where the default value of socket option IPV6_V6ONLY is 0. This is true for linux but not for windows. Therefore the socketoption has to be explicitly set to 0 by SNMP++ to be consistent on all platforms.

If you agree, I can prepare a patch that sets the socketoption to 0 for the IPv6 socket.

Best regards
Andreas