Creating a simple trap receiver using snmp4j is not calling the processPdu method

Hi,

i have created a simple trap receiver process but somehow i dont see the processPdu method is getting invoked. Please suggest what is missing here.
i am not able to understand what is going with the debug logs

code
package snmpsimple;
import java.io.IOException;

import org.snmp4j.CommandResponder;
import org.snmp4j.CommandResponderEvent;
import org.snmp4j.MessageDispatcherImpl;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.log.ConsoleLogAdapter;
import org.snmp4j.log.ConsoleLogFactory;
import org.snmp4j.log.LogFactory;
import org.snmp4j.mp.MPv3;
import org.snmp4j.security.AuthMD5;
import org.snmp4j.security.AuthSHA;
import org.snmp4j.security.AuthenticationProtocol;
import org.snmp4j.security.PrivAES128;
import org.snmp4j.security.PrivDES;
import org.snmp4j.security.PrivacyProtocol;
import org.snmp4j.security.SecurityModels;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.USM;
import org.snmp4j.security.UsmUser;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.TcpAddress;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.transport.DefaultTcpTransportMapping;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.MultiThreadedMessageDispatcher;
import org.snmp4j.util.ThreadPool;

public class SnmpTrapReceiver {

static {
    LogFactory.setLogFactory(new ConsoleLogFactory());
    ConsoleLogAdapter.setDebugEnabled(true);
}


private static final String SNMP_PORT = System.getenv("SNMP_PORT");
private static final String SNMP_AUTH_PROTOCOL = System.getenv("SNMP_AUTH_PROTOCOL");
private static final String SNMP_PRIVACY_PROTOCOL = System.getenv("SNMP_PRIVACY_PROTOCOL");
private static final String SNMP_AUTH_PHRASE = System.getenv("SNMP_AUTH_PHRASE");
private static final String SNMP_PRIVACY_PHRASE = System.getenv("SNMP_PRIVACY_PHRASE");
private static final String SNMP_PROTOCOL = System.getenv("SNMP_PROTOCOL");
private static final String SNMP_USER=System.getenv("SNMP_USER");



public static void main(String[] args) {
    try {
    	
    	System.out.println(SNMP_PORT);
    	System.out.println(SNMP_AUTH_PROTOCOL);
    	System.out.println(SNMP_USER);
    	//MPv3.createLocalEngineID()
    	Address listenAddress;
    	ThreadPool threadPool = ThreadPool.create("Trap", 2);
    	MultiThreadedMessageDispatcher dispatcher =
    	        new MultiThreadedMessageDispatcher(threadPool,
    	                                           new MessageDispatcherImpl());
        // Create transport mapping
        TransportMapping<?> transport;
        if ("TCP".equalsIgnoreCase(SNMP_PROTOCOL)) {
        	listenAddress = GenericAddress.parse("tcp:0.0.0.0/"+SNMP_PORT);
            transport = new DefaultTcpTransportMapping((TcpAddress)listenAddress);
        } else {
        	listenAddress = GenericAddress.parse("udp:0.0.0.0/"+SNMP_PORT);
            transport = new DefaultUdpTransportMapping((UdpAddress)listenAddress);
        }
        transport.listen();

        // Create SNMP object
        Snmp snmp = new Snmp(dispatcher,transport);
        //snmp.addCommandResponder(new TrapListener());
        snmp.addNotificationListener(listenAddress, new TrapListener());

        // Set up security protocols
        SecurityProtocols securityProtocols = SecurityProtocols.getInstance();
        AuthenticationProtocol authProtocol = getAuthProtocol(SNMP_AUTH_PROTOCOL);
        PrivacyProtocol privProtocol = getPrivProtocol(SNMP_PRIVACY_PROTOCOL);

        if (authProtocol != null) {
            securityProtocols.addAuthenticationProtocol(authProtocol);
        }
        if (privProtocol != null) {
            securityProtocols.addPrivacyProtocol(privProtocol);
        }

        // Create user
        OctetString authKey = new OctetString(SNMP_AUTH_PHRASE);
        OctetString privKey = new OctetString(SNMP_PRIVACY_PHRASE);
        UsmUser  user = new UsmUser (new OctetString(SNMP_USER),
                authProtocol.getID(),
                authKey,
                privProtocol.getID(),
                privKey);
        byte[]engineID = MPv3.createLocalEngineID();
        
        StringBuilder hexString = new StringBuilder();
        for (byte b : engineID) {
            // Convert byte to integer, handle negative values correctly
            // (byte values range from -128 to 127)
            // (b & 0xFF) converts the signed byte to an unsigned integer (0-255)
            String hex = String.format("%02X", b & 0xFF);
            hexString.append(hex);
        }
        System.out.println("Local engineid:"+hexString.toString());
        USM usm = new USM(SecurityProtocols.getInstance(),
                new OctetString(engineID), 0);
        usm.addUser(user);
        SecurityModels.getInstance().addSecurityModel(usm);
        
        
        System.out.println("Listening for SNMP traps on port " + SNMP_PORT + " using " + SNMP_PROTOCOL);

        // Keep the application running
        while (true) {
            Thread.sleep(1000);
        }
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
    }
}

private static AuthenticationProtocol getAuthProtocol(String protocol) {
    switch (protocol.toUpperCase()) {
        case "SHA":
            return new AuthSHA();
        case "MD5":
            return new AuthMD5();
        default:
            System.err.println("Unsupported authentication protocol: " + protocol);
            return null;
    }
}

private static PrivacyProtocol getPrivProtocol(String protocol) {
    switch (protocol.toUpperCase()) {
        case "AES":
            return new PrivAES128();
        case "DES":
            return new PrivDES();
        default:
            System.err.println("Unsupported privacy protocol: " + protocol);
            return null;
    }
}


static class TrapListener implements CommandResponder {
	@Override
	public void processPdu(CommandResponderEvent arg0) {
		System.out.println("Received SNMP Trap:" +arg0.getPDU());
		
	}
}

}

Output:
$ java -cp “snmp4j-2.8.18.jar:.” snmpsimple.SnmpTrapReceiver
1171
SHA
notlsuser
2025-05-22 06:56:48.694 main INFO TCP address 0.0.0.0/1171 bound successfully
2025-05-22 06:56:48.701 main WARN Socket bind failed for 0.0.0.0/1171: Address already in use
2025-05-22 06:56:48.702 main WARN Failed to initialize notification listener for address ‘0.0.0.0/1171’: Address already in use
2025-05-22 06:56:48.717 main DEBUG Initialized Salt to e4648cc8cc532d47.
Local engineid:80001370017F00010124B3954A
2025-05-22 06:56:48.728 main DEBUG Adding user notlsuser = UsmUser[secName=notlsuser,authProtocol=1.3.6.1.6.3.10.1.1.3,authPassphrase=authPass,privProtocol=1.3.6.1.6.3.10.1.2.2,privPassphrase=privPass,localizationEngineID=null]
Listening for SNMP traps on port 1171 using TCP

2025-05-22 06:56:56.450 DefaultTCPTransportMapping_0.0.0.0/1171 DEBUG Key is acceptable
2025-05-22 06:56:56.454 DefaultTCPTransportMapping_0.0.0.0/1171 DEBUG Adding operation 1 for: SocketEntry[peerAddress=127.0.0.1/40108,socket=Socket[addr=/127.0.0.1,port=40108,localport=1171],lastUse=Sat Jan 03 09:49:43 UTC 1970,readBufferPosition=-1,socketTimeout=null]
2025-05-22 06:56:56.454 DefaultTCPTransportMapping_0.0.0.0/1171 DEBUG Firing transport state event: org.snmp4j.transport.TransportStateEvent[source=org.snmp4j.transport.DefaultTcpTransportMapping@3af2c440,peerAddress=127.0.0.1/40108,newState=1,cancelled=false,causingException=null]
2025-05-22 06:56:56.455 DefaultTCPTransportMapping_0.0.0.0/1171 DEBUG Key is reading
2025-05-22 06:56:56.455 DefaultTCPTransportMapping_0.0.0.0/1171 DEBUG Reading header 6 bytes from 127.0.0.1/40108
2025-05-22 06:56:56.456 DefaultTCPTransportMapping_0.0.0.0/1171 DEBUG Message length is org.snmp4j.transport.MessageLength[headerLength=3,payloadLength=160]
2025-05-22 06:56:56.456 DefaultTCPTransportMapping_0.0.0.0/1171 DEBUG Message completed with 163 bytes and 163 buffer limit
2025-05-22 06:56:56.456 DefaultTCPTransportMapping_0.0.0.0/1171 DEBUG Received message from 127.0.0.1/40108 with length 163: 30:81:a0:02:01:03:30:12:02:04:34:7d:eb:64:02:04:7f:ff:ff:ff:04:01:03:02:01:03:04:3d:30:3b:04:0d:80:00:13:70:01:7f:00:01:01:cd:93:cc:86:02:01:01:02:04:01:3d:a9:8c:04:09:6e:6f:74:6c:73:75:73:65:72:04:0c:1a:f2:f3:8e:c9:44:e4:b7:26:aa:5a:cf:04:08:00:00:00:01:57:1e:99:7d:04:48:d6:b7:71:14:d7:06:2f:ad:16:f7:c8:7c:09:c4:fc:39:18:b7:a6:8b:4f:7d:96:fe:0e:5e:a3:73:f9:11:1e:51:9e:5a:ac:2e:63:7d:04:48:0d:06:7c:24:6f:7d:c2:d6:10:2b:fe:66:d8:45:ed:6b:6f:81:d8:3b:80:5b:cc:e8:23:d0:17:2a:f4:39:e8:0e
2025-05-22 06:56:56.457 DefaultTCPTransportMapping_0.0.0.0/1171 DEBUG Key is readable
2025-05-22 06:56:56.457 DefaultTCPTransportMapping_0.0.0.0/1171 DEBUG Key is reading
2025-05-22 06:56:56.457 DefaultTCPTransportMapping_0.0.0.0/1171 DEBUG Reading header -1 bytes from 127.0.0.1/40108
2025-05-22 06:56:56.457 DefaultTCPTransportMapping_0.0.0.0/1171 DEBUG Socket closed remotely
2025-05-22 06:56:56.457 DefaultTCPTransportMapping_0.0.0.0/1171 DEBUG Firing transport state event: org.snmp4j.transport.TransportStateEvent[source=org.snmp4j.transport.DefaultTcpTransportMapping@3af2c440,peerAddress=127.0.0.1/40108,newState=2,cancelled=false,causingException=null]
2025-05-22 06:56:56.458 Trap.0 WARN SNMP version 3 is not supported
2025-05-22 06:57:56.506 Timer-0 DEBUG Socket has not been used for 60048 milliseconds, closing it
2025-05-22 06:57:56.507 Timer-0 INFO Socket to 127.0.0.1/40108 closed due to timeout

Trap command:
$ snmptrap -v 3 -e 0x80001370017F000101CD93CC86 -u notlsuser -a SHA -A authPass -x DES -X privPass -l authPriv tcp:0.0.0.0:1171 40 “1.1.12”

The already bound address is the blocking error. You need to use a different port. :wink:

The above (AI generated code?) is probably better written as:

System.out.println("Local engineid:"+OctetString.fromByteArray(engineID).toHexString());

In addition, I recommend using SNMP4J 3.9.2 or later.

By using a different port for notification i still see the same problem that processPdu method is not invoked… Please suggest.

Output from program
$ java -cp “snmp4j-2.8.18.jar:.” snmpsimple.SnmpTrapReceiver
1168
SHA
notlsuser
2025-05-23 07:35:06.566 main INFO TCP address 0.0.0.0/1168 bound successfully
2025-05-23 07:35:06.573 main INFO TCP address 0.0.0.0/1163 bound successfully
2025-05-23 07:35:06.573 main INFO Added notification listener for address: 0.0.0.0/1163
2025-05-23 07:35:06.587 main DEBUG Initialized Salt to d050e5ce64fd594a.
Local engineid:80001370017F000101603E555B
2025-05-23 07:35:06.596 main DEBUG Adding user notlsuser = UsmUser[secName=notlsuser,authProtocol=1.3.6.1.6.3.10.1.1.3,authPassphrase=authPass,privProtocol=1.3.6.1.6.3.10.1.2.2,privPassphrase=privPass,localizationEngineID=null]
Listening for SNMP traps on port 1168 using TCP

2025-05-23 07:36:09.987 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is acceptable
2025-05-23 07:36:09.991 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Adding operation 1 for: SocketEntry[peerAddress=127.0.0.1/47000,socket=Socket[addr=/127.0.0.1,port=47000,localport=1168],lastUse=Sun Jan 04 10:28:56 UTC 1970,readBufferPosition=-1,socketTimeout=null]
2025-05-23 07:36:09.992 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Firing transport state event: org.snmp4j.transport.TransportStateEvent[source=org.snmp4j.transport.DefaultTcpTransportMapping@6455f47,peerAddress=127.0.0.1/47000,newState=1,cancelled=false,causingException=null]
2025-05-23 07:36:09.992 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is reading
2025-05-23 07:36:09.993 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Reading header 6 bytes from 127.0.0.1/47000
2025-05-23 07:36:09.994 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Message length is org.snmp4j.transport.MessageLength[headerLength=3,payloadLength=168]
2025-05-23 07:36:09.994 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Message completed with 171 bytes and 171 buffer limit
2025-05-23 07:36:09.994 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Received message from 127.0.0.1/47000 with length 171: 30:81:a8:02:01:03:30:12:02:04:36:2c:31:9f:02:04:7f:ff:ff:ff:04:01:03:02:01:03:04:3d:30:3b:04:0d:80:00:13:70:01:7f:00:01:01:60:3e:55:5b:02:01:01:02:04:01:c5:16:e6:04:09:6e:6f:74:6c:73:75:73:65:72:04:0c:66:16:36:bd:ce:2f:59:c0:56:69:6a:08:04:08:00:00:00:01:5f:ea:c3:2f:04:50:94:61:9f:c4:0e:4e:ab:e6:11:4d:d4:1b:7c:48:be:75:cf:1e:19:dd:84:5d:41:e8:30:e6:7a:e2:fe:04:02:02:38:0e:66:66:f2:58:93:b3:86:2a:a9:60:8c:d5:bf:ce:75:53:9f:16:cc:cd:12:08:12:48:16:82:a7:dd:dc:03:99:b1:57:0f:97:7b:fc:7a:a2:4a:03:66:90:cb:cc:c2
2025-05-23 07:36:09.996 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is readable
2025-05-23 07:36:09.996 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is reading
2025-05-23 07:36:09.996 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Reading header -1 bytes from 127.0.0.1/47000
2025-05-23 07:36:09.996 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Socket closed remotely
2025-05-23 07:36:09.996 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Firing transport state event: org.snmp4j.transport.TransportStateEvent[source=org.snmp4j.transport.DefaultTcpTransportMapping@6455f47,peerAddress=127.0.0.1/47000,newState=2,cancelled=false,causingException=null]
2025-05-23 07:36:09.997 Trap.0 WARN SNMP version 3 is not supported
2025-05-23 07:37:10.038 Timer-0 DEBUG Socket has not been used for 60041 milliseconds, closing it
2025-05-23 07:37:10.039 Timer-0 INFO Socket to 127.0.0.1/47000 closed due to timeout

Command:
$ snmptrap -v 3 -e 0x80001370017F000101603E555B -u notlsuser -a SHA -A authPass -x DES -X privPass -l authPriv tcp:0.0.0.0:1168 40 “1
.1.1.1”

You have not added a SNMPv3 message processing model (MPV3) to the Snmp instance.

I have added the MPv3 model to snmp and now i am seeing not in time window error in output and because of this i dont see the processPdu method getting invoked. Please suggest i have added the auto discovery for the USM .

Code:
package snmpsimple;
import java.io.IOException;

import org.snmp4j.CommandResponder;
import org.snmp4j.CommandResponderEvent;
import org.snmp4j.MessageDispatcherImpl;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.log.ConsoleLogAdapter;
import org.snmp4j.log.ConsoleLogFactory;
import org.snmp4j.log.LogFactory;
import org.snmp4j.mp.MPv3;
import org.snmp4j.security.AuthMD5;
import org.snmp4j.security.AuthSHA;
import org.snmp4j.security.AuthenticationProtocol;
import org.snmp4j.security.PrivAES128;
import org.snmp4j.security.PrivDES;
import org.snmp4j.security.PrivacyProtocol;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.USM;
import org.snmp4j.security.UsmUser;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.TcpAddress;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.transport.DefaultTcpTransportMapping;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.MultiThreadedMessageDispatcher;
import org.snmp4j.util.ThreadPool;

public class SnmpTrapReceiver {

static {
    LogFactory.setLogFactory(new ConsoleLogFactory());
    ConsoleLogAdapter.setDebugEnabled(true);
}


private static final String SNMP_PORT = System.getenv("SNMP_PORT");
private static final String SNMP_AUTH_PROTOCOL = System.getenv("SNMP_AUTH_PROTOCOL");
private static final String SNMP_PRIVACY_PROTOCOL = System.getenv("SNMP_PRIVACY_PROTOCOL");
private static final String SNMP_AUTH_PHRASE = System.getenv("SNMP_AUTH_PHRASE");
private static final String SNMP_PRIVACY_PHRASE = System.getenv("SNMP_PRIVACY_PHRASE");
private static final String SNMP_PROTOCOL = System.getenv("SNMP_PROTOCOL");
private static final String SNMP_USER=System.getenv("SNMP_USER");
private static final String NOTIFICATION_PORT = System.getProperty("NOTIFICATION_PORT","1163");



public static void main(String[] args) {
    try {
    	
    	System.out.println(SNMP_PORT);
    	System.out.println(SNMP_AUTH_PROTOCOL);
    	System.out.println(SNMP_USER);
    	//MPv3.createLocalEngineID()
    	Address listenAddress, notificationAddress;
    	ThreadPool threadPool = ThreadPool.create("Trap", 2);
    	MultiThreadedMessageDispatcher dispatcher =
    	        new MultiThreadedMessageDispatcher(threadPool,
    	                                           new MessageDispatcherImpl());
        // Create transport mapping
        TransportMapping<?> transport;
        if ("TCP".equalsIgnoreCase(SNMP_PROTOCOL)) {
        	listenAddress = GenericAddress.parse("tcp:0.0.0.0/"+SNMP_PORT);
        	notificationAddress = GenericAddress.parse("tcp:0.0.0.0/"+NOTIFICATION_PORT);
            transport = new DefaultTcpTransportMapping((TcpAddress)listenAddress);
        } else {
        	listenAddress = GenericAddress.parse("udp:0.0.0.0/"+SNMP_PORT);
            transport = new DefaultUdpTransportMapping((UdpAddress)listenAddress);
            notificationAddress = GenericAddress.parse("udp:0.0.0.0/"+NOTIFICATION_PORT);
        }
        transport.listen();
        
        SecurityProtocols securityProtocols = SecurityProtocols.getInstance();
        AuthenticationProtocol authProtocol = getAuthProtocol(SNMP_AUTH_PROTOCOL);
        PrivacyProtocol privProtocol = getPrivProtocol(SNMP_PRIVACY_PROTOCOL);

        if (authProtocol != null) {
		    securityProtocols.addAuthenticationProtocol(authProtocol);
		}
		if (privProtocol != null) {
		    securityProtocols.addPrivacyProtocol(privProtocol);
		}
		
		byte[]engineID = MPv3.createLocalEngineID();
		
		// Create user
		OctetString authKey = new OctetString(SNMP_AUTH_PHRASE);
		OctetString privKey = new OctetString(SNMP_PRIVACY_PHRASE);
		
	
        UsmUser  user = new UsmUser (new OctetString(SNMP_USER),
                authProtocol.getID(),
                authKey,
                privProtocol.getID(),
                privKey);
        
        
        System.out.println("Local engineid:"+OctetString.fromByteArray(engineID).toHexString());
        USM usm = new USM(securityProtocols,
        		new OctetString(engineID), 0);
        usm.setEngineDiscoveryEnabled(true);
        usm.addUser(user);
        //SecurityModels.getInstance().addSecurityModel(usm);

        //snmp.setLocalEngine(engineID, 0, 0);
        MPv3 mpv3=  new MPv3(usm);
        //mpv3.getSecurityModels().addSecurityModel(usm);
        dispatcher.addMessageProcessingModel(mpv3);
      
        // Create SNMP object
        Snmp snmp = new Snmp(dispatcher,transport); 
       
        //snmp.addCommandResponder(new TrapListener());
        snmp.addNotificationListener(notificationAddress, new TrapListener());
        snmp.discoverAuthoritativeEngineID(listenAddress, 5000);

        // Set up security protocols
          System.out.println("Listening for SNMP traps on port " + SNMP_PORT + " using " + SNMP_PROTOCOL);

        // Keep the application running
        while (true) {
            Thread.sleep(1000);
        }
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
    }
}

private static AuthenticationProtocol getAuthProtocol(String protocol) {
    switch (protocol.toUpperCase()) {
        case "SHA":
            return new AuthSHA();
        case "MD5":
            return new AuthMD5();
        default:
            System.err.println("Unsupported authentication protocol: " + protocol);
            return null;
    }
}

private static PrivacyProtocol getPrivProtocol(String protocol) {
    switch (protocol.toUpperCase()) {
        case "AES":
            return new PrivAES128();
        case "DES":
            return new PrivDES();
        default:
            System.err.println("Unsupported privacy protocol: " + protocol);
            return null;
    }
}


static class TrapListener implements CommandResponder {
	@Override
	public void processPdu(CommandResponderEvent arg0) {
		System.out.println("Received SNMP Trap:" +arg0.getPDU());
		
	}
}

}

Output:
$ java -cp “snmp4j-2.8.18.jar:.” snmpsimple.SnmpTrapReceiver
1168
SHA
notlsuser
2025-05-24 16:27:16.710 main INFO TCP address 0.0.0.0/1168 bound successfully
2025-05-24 16:27:16.724 main DEBUG Initialized Salt to 76f63054f8d731d5.
Local engineid:80:00:13:70:01:7f:00:01:01:bd:df:0c:88
2025-05-24 16:27:16.733 main DEBUG Adding user notlsuser = UsmUser[secName=notlsuser,authProtocol=1.3.6.1.6.3.10.1.1.3,authPassphrase=authPass,privProtocol=1.3.6.1.6.3.10.1.2.2,privPassphrase=privPass,localizationEngineID=null]
2025-05-24 16:27:16.739 main INFO TCP address 0.0.0.0/1163 bound successfully
2025-05-24 16:27:16.739 main INFO Added notification listener for address: 0.0.0.0/1163
2025-05-24 16:27:16.743 main DEBUG RFC3414 §3.1.4.b Outgoing message is not encrypted
2025-05-24 16:27:16.743 main DEBUG Adding cache entry: StateReference[msgID=45166,pduHandle=PduHandle[1796148982],securityEngineID=,securityModel=org.snmp4j.security.USM@108c4c35,securityName=,securityLevel=1,contextEngineID=,contextName=,retryMsgIDs=null]
2025-05-24 16:27:16.744 main DEBUG Running pending sync request with handle PduHandle[1796148982] and retry count left 0
2025-05-24 16:27:16.744 main DEBUG Looking up connection for destination ‘0.0.0.0/1168’ returned: null
2025-05-24 16:27:16.744 main DEBUG {}
2025-05-24 16:27:16.744 main DEBUG Socket for address ‘0.0.0.0/1168’ is closed, opening it…
2025-05-24 16:27:16.745 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is acceptable
2025-05-24 16:27:16.745 main DEBUG Trying to connect to 0.0.0.0/1168
2025-05-24 16:27:16.747 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Adding operation 1 for: SocketEntry[peerAddress=127.0.0.1/42174,socket=Socket[addr=/127.0.0.1,port=42174,localport=1168],lastUse=Mon Jan 05 15:58:29 UTC 1970,readBufferPosition=-1,socketTimeout=null]
2025-05-24 16:27:16.748 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Firing transport state event: org.snmp4j.transport.TransportStateEvent[source=org.snmp4j.transport.DefaultTcpTransportMapping@7bf6b6c1,peerAddress=127.0.0.1/42174,newState=1,cancelled=false,causingException=null]
2025-05-24 16:27:16.748 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is reading
2025-05-24 16:27:16.748 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Reading header 0 bytes from 127.0.0.1/42174
2025-05-24 16:27:16.749 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG No socket entry found for incoming address 127.0.0.1/42174 for incomplete message with length 0
2025-05-24 16:27:16.749 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Adding operation 8 for: SocketEntry[peerAddress=0.0.0.0/1168,socket=Socket[unconnected],lastUse=Mon Jan 05 15:58:29 UTC 1970,readBufferPosition=-1,socketTimeout=null]
2025-05-24 16:27:16.749 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is connectable
2025-05-24 16:27:16.749 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Connected to 0.0.0.0/1168
2025-05-24 16:27:16.749 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Adding operation 4 for: SocketEntry[peerAddress=0.0.0.0/1168,socket=Socket[addr=/0.0.0.0,port=1168,localport=42174],lastUse=Mon Jan 05 15:58:29 UTC 1970,readBufferPosition=-1,socketTimeout=org.snmp4j.transport.DefaultTcpTransportMapping$SocketTimeout@3ac39c1a]
2025-05-24 16:27:16.749 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Fire connected event for 0.0.0.0/1168
2025-05-24 16:27:16.749 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Firing transport state event: org.snmp4j.transport.TransportStateEvent[source=org.snmp4j.transport.DefaultTcpTransportMapping@7bf6b6c1,peerAddress=0.0.0.0/1168,newState=1,cancelled=false,causingException=null]
2025-05-24 16:27:16.749 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is writable
2025-05-24 16:27:16.749 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Sent message with length 60 to 0.0.0.0/1168: 30:3a:02:01:03:30:10:02:03:00:b0:6e:02:03:00:ff:ff:04:01:04:02:01:03:04:10:30:0e:04:00:02:01:00:02:01:00:04:00:04:00:04:00:30:11:04:00:04:00:a0:0b:02:01:00:02:01:00:02:01:00:30:00
2025-05-24 16:27:16.749 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Adding operation 1 for: SocketEntry[peerAddress=0.0.0.0/1168,socket=Socket[addr=/0.0.0.0,port=1168,localport=42174],lastUse=Mon Jan 05 15:58:29 UTC 1970,readBufferPosition=-1,socketTimeout=org.snmp4j.transport.DefaultTcpTransportMapping$SocketTimeout@3ac39c1a]
2025-05-24 16:27:16.750 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is writable
2025-05-24 16:27:16.750 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is readable
2025-05-24 16:27:16.750 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is reading
2025-05-24 16:27:16.750 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Reading header 6 bytes from 127.0.0.1/42174
2025-05-24 16:27:16.750 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Message length is org.snmp4j.transport.MessageLength[headerLength=2,payloadLength=58]
2025-05-24 16:27:16.750 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Message completed with 60 bytes and 60 buffer limit
2025-05-24 16:27:16.750 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Received message from 127.0.0.1/42174 with length 60: 30:3a:02:01:03:30:10:02:03:00:b0:6e:02:03:00:ff:ff:04:01:04:02:01:03:04:10:30:0e:04:00:02:01:00:02:01:00:04:00:04:00:04:00:30:11:04:00:04:00:a0:0b:02:01:00:02:01:00:02:01:00:30:00
2025-05-24 16:27:16.751 Trap.0 DEBUG SNMPv3 header decoded: msgId=45166, msgMaxSize=65535, msgFlags=04, secModel=3
2025-05-24 16:27:16.752 Trap.0 DEBUG RFC3414 §3.2.3 Unknown engine ID:
2025-05-24 16:27:16.752 Trap.0 DEBUG Adding cache entry: StateReference[msgID=45166,pduHandle=PduHandle[1796148983],securityEngineID=,securityModel=org.snmp4j.security.USM@108c4c35,securityName=,securityLevel=1,contextEngineID=,contextName=,retryMsgIDs=null]
2025-05-24 16:27:16.752 Trap.0 DEBUG Removed msgId retry cache sub-entry: 45166 from msgIdToPduHandleMapping: PduHandle[1796148983]
2025-05-24 16:27:16.752 Trap.0 DEBUG Removed cache entry: StateReference[msgID=45166,pduHandle=null,securityEngineID=,securityModel=org.snmp4j.security.USM@108c4c35,securityName=,securityLevel=1,contextEngineID=,contextName=,retryMsgIDs=null] for PduHandle PduHandle[1796148983]
2025-05-24 16:27:16.753 Trap.0 DEBUG Popped cache entry: StateReference[msgID=45166,pduHandle=PduHandle[1796148983],securityEngineID=,securityModel=org.snmp4j.security.USM@108c4c35,securityName=,securityLevel=1,contextEngineID=,contextName=,retryMsgIDs=null] for msgID 45166
2025-05-24 16:27:16.753 Trap.0 DEBUG RFC3414 §3.1.4.b Outgoing message is not encrypted
2025-05-24 16:27:16.753 Trap.0 DEBUG Looking up connection for destination ‘127.0.0.1/42174’ returned: SocketEntry[peerAddress=127.0.0.1/42174,socket=Socket[addr=/127.0.0.1,port=42174,localport=1168],lastUse=Mon Jan 05 15:58:29 UTC 1970,readBufferPosition=-1,socketTimeout=org.snmp4j.transport.DefaultTcpTransportMapping$SocketTimeout@77b7a02b]
2025-05-24 16:27:16.753 Trap.0 DEBUG {0.0.0.0/1168=SocketEntry[peerAddress=0.0.0.0/1168,socket=Socket[addr=/0.0.0.0,port=1168,localport=42174],lastUse=Mon Jan 05 15:58:29 UTC 1970,readBufferPosition=-1,socketTimeout=org.snmp4j.transport.DefaultTcpTransportMapping$SocketTimeout@3ac39c1a], 127.0.0.1/42174=SocketEntry[peerAddress=127.0.0.1/42174,socket=Socket[addr=/127.0.0.1,port=42174,localport=1168],lastUse=Mon Jan 05 15:58:29 UTC 1970,readBufferPosition=-1,socketTimeout=org.snmp4j.transport.DefaultTcpTransportMapping$SocketTimeout@77b7a02b]}
2025-05-24 16:27:16.753 Trap.0 DEBUG Waking up selector for new message
2025-05-24 16:27:16.753 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Adding operation 4 for: SocketEntry[peerAddress=127.0.0.1/42174,socket=Socket[addr=/127.0.0.1,port=42174,localport=1168],lastUse=Mon Jan 05 15:58:29 UTC 1970,readBufferPosition=-1,socketTimeout=org.snmp4j.transport.DefaultTcpTransportMapping$SocketTimeout@77b7a02b]
2025-05-24 16:27:16.754 Trap.0 INFO Message from 127.0.0.1/42174 not dispatched, reason: statusInfo=1.3.6.1.6.3.15.1.1.4.0 = 0, status=1410
2025-05-24 16:27:16.754 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is writable
2025-05-24 16:27:16.754 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Sent message with length 103 to 127.0.0.1/42174: 30:65:02:01:03:30:10:02:03:00:b0:6e:02:03:00:ff:ff:04:01:00:02:01:03:04:1d:30:1b:04:0d:80:00:13:70:01:7f:00:01:01:bd:df:0c:88:02:01:00:02:01:00:04:00:04:00:04:00:30:2f:04:0d:80:00:13:70:01:7f:00:01:01:bd:df:0c:88:04:00:a8:1c:02:01:00:02:01:00:02:01:00:30:11:30:0f:06:0a:2b:06:01:06:03:0f:01:01:04:00:41:01:00
2025-05-24 16:27:16.754 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is readable
2025-05-24 16:27:16.754 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is reading
2025-05-24 16:27:16.754 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Reading header 6 bytes from 0.0.0.0/1168
2025-05-24 16:27:16.754 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Message length is org.snmp4j.transport.MessageLength[headerLength=2,payloadLength=101]
2025-05-24 16:27:16.754 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Message completed with 103 bytes and 103 buffer limit
2025-05-24 16:27:16.754 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Received message from 0.0.0.0/1168 with length 103: 30:65:02:01:03:30:10:02:03:00:b0:6e:02:03:00:ff:ff:04:01:00:02:01:03:04:1d:30:1b:04:0d:80:00:13:70:01:7f:00:01:01:bd:df:0c:88:02:01:00:02:01:00:04:00:04:00:04:00:30:2f:04:0d:80:00:13:70:01:7f:00:01:01:bd:df:0c:88:04:00:a8:1c:02:01:00:02:01:00:02:01:00:30:11:30:0f:06:0a:2b:06:01:06:03:0f:01:01:04:00:41:01:00
2025-05-24 16:27:16.754 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is writable
2025-05-24 16:27:16.754 Trap.0 DEBUG SNMPv3 header decoded: msgId=45166, msgMaxSize=65535, msgFlags=00, secModel=3
2025-05-24 16:27:16.755 Trap.0 DEBUG Accepting zero length security name
2025-05-24 16:27:16.755 Trap.0 DEBUG No cache entry found for popEntry by msgID=45166
2025-05-24 16:27:16.755 Trap.0 DEBUG RFC3412 §7.2.10 - Received PDU (msgID=45166) is a response or internal class message, but cached information for the msgID could not be found
2025-05-24 16:27:16.755 Trap.0 INFO Message from 0.0.0.0/1168 not dispatched, reason: statusInfo=noError, status=-1409

2025-05-24 16:27:21.840 main DEBUG Removed pending request with handle: PduHandle[1796148982]
Listening for SNMP traps on port 1168 using TCP
2025-05-24 16:28:16.768 Timer-0 DEBUG Socket has not been used for 60013 milliseconds, closing it
2025-05-24 16:28:16.769 Timer-0 INFO Socket to 127.0.0.1/42174 closed due to timeout
2025-05-24 16:28:16.769 Timer-0 DEBUG Socket has not been used for 60014 milliseconds, closing it
2025-05-24 16:28:16.769 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is readable
2025-05-24 16:28:16.769 Timer-0 INFO Socket to 0.0.0.0/1168 closed due to timeout
2025-05-24 16:28:16.769 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is reading
2025-05-24 16:28:16.770 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Selection key cancelled, skipping it
2025-05-24 16:28:36.011 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is acceptable
2025-05-24 16:28:36.011 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Adding operation 1 for: SocketEntry[peerAddress=127.0.0.1/56188,socket=Socket[addr=/127.0.0.1,port=56188,localport=1168],lastUse=Mon Jan 05 15:59:48 UTC 1970,readBufferPosition=-1,socketTimeout=null]
2025-05-24 16:28:36.011 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Firing transport state event: org.snmp4j.transport.TransportStateEvent[source=org.snmp4j.transport.DefaultTcpTransportMapping@7bf6b6c1,peerAddress=127.0.0.1/56188,newState=1,cancelled=false,causingException=null]
2025-05-24 16:28:36.011 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is reading
2025-05-24 16:28:36.012 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Reading header 6 bytes from 127.0.0.1/56188
2025-05-24 16:28:36.012 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Message length is org.snmp4j.transport.MessageLength[headerLength=3,payloadLength=168]
2025-05-24 16:28:36.012 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Message completed with 171 bytes and 171 buffer limit
2025-05-24 16:28:36.013 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Received message from 127.0.0.1/56188 with length 171: 30:81:a8:02:01:03:30:12:02:04:6b:68:30:c1:02:04:7f:ff:ff:ff:04:01:03:02:01:03:04:3d:30:3b:04:0d:80:00:13:70:01:7f:00:01:01:bd:df:0c:88:02:01:01:02:04:02:67:37:8e:04:09:6e:6f:74:6c:73:75:73:65:72:04:0c:6c:8b:33:f8:b8:62:87:5d:7c:80:69:93:04:08:00:00:00:01:b7:c9:0f:88:04:50:d0:57:6d:2b:d8:58:8d:80:82:d6:b8:a4:71:80:9f:e3:89:78:b2:47:69:bd:7a:18:3f:e5:d4:42:a4:c6:c7:42:8d:66:73:ae:03:4a:1c:95:a8:23:03:14:83:30:33:f8:be:d6:24:ab:ff:3d:71:ac:ff:b7:35:d6:ee:b5:55:d8:50:67:8c:54:64:c1:ab:04:1c:a2:ea:da:f3:88:28:c9
2025-05-24 16:28:36.013 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is readable
2025-05-24 16:28:36.013 Trap.0 DEBUG SNMPv3 header decoded: msgId=1801990337, msgMaxSize=2147483647, msgFlags=03, secModel=3
2025-05-24 16:28:36.013 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is reading
2025-05-24 16:28:36.013 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Reading header -1 bytes from 127.0.0.1/56188
2025-05-24 16:28:36.013 Trap.0 DEBUG getUser(engineID=80:00:13:70:01:7f:00:01:01:bd:df:0c:88, securityName=notlsuser)
2025-05-24 16:28:36.013 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Socket closed remotely
2025-05-24 16:28:36.014 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Firing transport state event: org.snmp4j.transport.TransportStateEvent[source=org.snmp4j.transport.DefaultTcpTransportMapping@7bf6b6c1,peerAddress=127.0.0.1/56188,newState=2,cancelled=false,causingException=null]
2025-05-24 16:28:36.031 Trap.0 DEBUG SHA-1First digest: 9a:6a:6e:75:a8:01:78:fc:37:db:eb:8c:92:8e:51:a2:cd:82:46:88
2025-05-24 16:28:36.031 Trap.0 DEBUG SHA-1localized key: 5c:8d:32:e3:1a:ad:d4:b0:41:69:4f:df:3b:17:f2:e3:6b:89:b5:64
2025-05-24 16:28:36.041 Trap.0 DEBUG SHA-1First digest: 8e:39:37:8a:e0:64:b9:5e:fc:7c:5f:e2:d9:cc:94:97:44:4c:97:eb
2025-05-24 16:28:36.041 Trap.0 DEBUG SHA-1localized key: 5b:5c:e0:fe:0f:6b:d9:d2:97:64:ce:ba:53:06:18:34:db:b8:81:32
2025-05-24 16:28:36.043 Trap.0 DEBUG Adding user notlsuser = UsmUser[secName=notlsuser,authProtocol=1.3.6.1.6.3.10.1.1.3,authPassphrase=5c:8d:32:e3:1a:ad:d4:b0:41:69:4f:df:3b:17:f2:e3:6b:89:b5:64,privProtocol=1.3.6.1.6.3.10.1.2.2,privPassphrase=5b:5c:e0:fe:0f:6b:d9:d2:97:64:ce:ba:53:06:18:34,localizationEngineID=80:00:13:70:01:7f:00:01:01:bd:df:0c:88]
2025-05-24 16:28:36.043 Trap.0 DEBUG CheckTime: received message outside time window (authoritative):engineBoots differ 0!=1
2025-05-24 16:28:36.043 Trap.0 DEBUG RFC3414 §3.2.7.a Not in time window; engineID=‘80:00:13:70:01:7f:00:01:01:bd:df:0c:88’, engineBoots=1, engineTime=40318862
2025-05-24 16:28:36.043 Trap.0 INFO Message from 127.0.0.1/56188 not dispatched, reason: statusInfo=1.3.6.1.6.3.15.1.1.2.0 = 0, status=1411
2025-05-24 16:29:36.196 Timer-0 DEBUG Socket has not been used for 60182 milliseconds, closing it
2025-05-24 16:29:36.197 Timer-0 INFO Socket to 127.0.0.1/56188 closed due to timeout

Command:

snmptrap -v 3 -e 0x80001370017f000101bddf0c88 -u notlsuser -a SHA -A authPass -x DES -X privPass -l authPriv -r 1 tcp:0.0.0.0:1168
40 “1.1.1.1”

This is now the error: engineBoot of sender and receiver are inconsistent!

any update on earlier post related to authentication failure?? it is still in pending state?
2025-05-27 03:09:16.747 Trap.0 DEBUG RFC3414 §3.2.6 Wrong digest → authentication failure: 6a:9c:79:a0:21:04:df:0f:b6:4f:1e:27

i have configured the engineid and now i am not seeing the outside time window error any more but rather i am seeing the below error in the logs i have configured the proper authentication phrase and authentication algorithm any inputs on this?

Code:
package snmpsimple;
import java.io.IOException;

import org.snmp4j.CommandResponder;
import org.snmp4j.CommandResponderEvent;
import org.snmp4j.MessageDispatcherImpl;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.log.ConsoleLogAdapter;
import org.snmp4j.log.ConsoleLogFactory;
import org.snmp4j.log.LogFactory;
import org.snmp4j.mp.MPv3;
import org.snmp4j.security.AuthMD5;
import org.snmp4j.security.AuthSHA;
import org.snmp4j.security.AuthenticationProtocol;
import org.snmp4j.security.PrivAES128;
import org.snmp4j.security.PrivDES;
import org.snmp4j.security.PrivacyProtocol;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.USM;
import org.snmp4j.security.UsmUser;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.TcpAddress;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.transport.DefaultTcpTransportMapping;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.MultiThreadedMessageDispatcher;
import org.snmp4j.util.ThreadPool;

public class SnmpTrapReceiver {

static {
    LogFactory.setLogFactory(new ConsoleLogFactory());
    ConsoleLogAdapter.setDebugEnabled(true);
}


private static final String SNMP_PORT = System.getenv("SNMP_PORT");
private static final String SNMP_AUTH_PROTOCOL = System.getenv("SNMP_AUTH_PROTOCOL");
private static final String SNMP_PRIVACY_PROTOCOL = System.getenv("SNMP_PRIVACY_PROTOCOL");
private static final String SNMP_AUTH_PHRASE = System.getenv("SNMP_AUTH_PHRASE");
private static final String SNMP_PRIVACY_PHRASE = System.getenv("SNMP_PRIVACY_PHRASE");
private static final String SNMP_PROTOCOL = System.getenv("SNMP_PROTOCOL");
private static final String SNMP_USER=System.getenv("SNMP_USER");
private static final String NOTIFICATION_PORT = System.getenv("NOTIFICATION_PORT");
private static final String SNMP_ENGINE_ID=System.getenv("SNMP_ENGINE_ID");



public static void main(String[] args) {
    try {
    	
    	System.out.println(SNMP_PORT);
    	System.out.println(SNMP_AUTH_PROTOCOL);
    	System.out.println(SNMP_USER);
    	System.out.println(SNMP_ENGINE_ID);
    	//MPv3.createLocalEngineID()
    	Address listenAddress, notificationAddress;
    	ThreadPool threadPool = ThreadPool.create("Trap", 2);
    	MultiThreadedMessageDispatcher dispatcher =
    	        new MultiThreadedMessageDispatcher(threadPool,
    	                                           new MessageDispatcherImpl());
        // Create transport mapping
        TransportMapping<?> transport;
        if ("TCP".equalsIgnoreCase(SNMP_PROTOCOL)) {
        	listenAddress = GenericAddress.parse("tcp:0.0.0.0/"+SNMP_PORT);
        	notificationAddress = GenericAddress.parse("tcp:0.0.0.0/"+NOTIFICATION_PORT);
            transport = new DefaultTcpTransportMapping((TcpAddress)listenAddress);
        } else {
        	listenAddress = GenericAddress.parse("udp:0.0.0.0/"+SNMP_PORT);
            transport = new DefaultUdpTransportMapping((UdpAddress)listenAddress);
            notificationAddress = GenericAddress.parse("udp:0.0.0.0/"+NOTIFICATION_PORT);
        }
        transport.listen();
        
        SecurityProtocols securityProtocols = SecurityProtocols.getInstance();
        AuthenticationProtocol authProtocol = getAuthProtocol(SNMP_AUTH_PROTOCOL);
        PrivacyProtocol privProtocol = getPrivProtocol(SNMP_PRIVACY_PROTOCOL);

        if (authProtocol != null) {
		    securityProtocols.addAuthenticationProtocol(authProtocol);
		}
		if (privProtocol != null) {
		    securityProtocols.addPrivacyProtocol(privProtocol);
		}
		
	
		OctetString engineId = OctetString.fromHexString(SNMP_ENGINE_ID);
		
		// Create user
		OctetString authKey = new OctetString(SNMP_AUTH_PHRASE);
		OctetString privKey = new OctetString(SNMP_PRIVACY_PHRASE);
		
	
        UsmUser  user = new UsmUser (new OctetString(SNMP_USER),
                authProtocol.getID(),
                authKey,
                privProtocol.getID(),
                privKey, engineId);
        
        
        System.out.println("Local engineid:"+SNMP_ENGINE_ID);
        USM usm = new USM(securityProtocols,
        		engineId, 1);
        
        usm.setEngineDiscoveryEnabled(true);
        usm.addUser(new OctetString(SNMP_USER),engineId,user);
        //SecurityModels.getInstance().addSecurityModel(usm);

        //snmp.setLocalEngine(engineID, 0, 0);
        MPv3 mpv3=  new MPv3(usm);
        //mpv3.getSecurityModels().addSecurityModel(usm);
        dispatcher.addMessageProcessingModel(mpv3);
      
        // Create SNMP object
        Snmp snmp = new Snmp(dispatcher,transport); 
       
        //snmp.addCommandResponder(new TrapListener());
        snmp.addNotificationListener(notificationAddress, new TrapListener());
        snmp.discoverAuthoritativeEngineID(listenAddress, 5000);

        // Set up security protocols
          System.out.println("Listening for SNMP traps on port " + SNMP_PORT + " using " + SNMP_PROTOCOL);

        // Keep the application running
        while (true) {
            Thread.sleep(1000);
        }
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
    }
}

private static AuthenticationProtocol getAuthProtocol(String protocol) {
    switch (protocol.toUpperCase()) {
        case "SHA":
            return new AuthSHA();
        case "MD5":
            return new AuthMD5();
        default:
            System.err.println("Unsupported authentication protocol: " + protocol);
            return null;
    }
}

private static PrivacyProtocol getPrivProtocol(String protocol) {
    switch (protocol.toUpperCase()) {
        case "AES":
            return new PrivAES128();
        case "DES":
            return new PrivDES();
        default:
            System.err.println("Unsupported privacy protocol: " + protocol);
            return null;
    }
}


static class TrapListener implements CommandResponder {
	@Override
	public void processPdu(CommandResponderEvent arg0) {
		System.out.println("Received SNMP Trap:" +arg0.getPDU());
		
	}
}

}

Output

$ java -cp “snmp4j-2.8.18.jar:.” snmpsimple.SnmpTrapReceiver
1168
SHA
notlsuser
80:00:13:70:01:7f:00:01:01:bd:df:0c:88
2025-05-26 05:50:54.795 main INFO TCP address 0.0.0.0/1168 bound successfully
2025-05-26 05:50:54.861 main DEBUG Initialized Salt to e2625cff870f8d6f.
Local engineid:80:00:13:70:01:7f:00:01:01:bd:df:0c:88
2025-05-26 05:50:54.871 main DEBUG Adding user notlsuser = UsmUser[secName=notlsuser,authProtocol=1.3.6.1.6.3.10.1.1.3,authPassphrase=authPass123,privProtocol=1.3.6.1.6.3.10.1.2.2,privPassphrase=privPass123,localizationEngineID=80:00:13:70:01:7f:00:01:01:bd:df:0c:88]
2025-05-26 05:50:54.883 main INFO TCP address 0.0.0.0/1163 bound successfully
2025-05-26 05:50:54.883 main INFO Added notification listener for address: 0.0.0.0/1163
2025-05-26 05:50:54.891 main DEBUG RFC3414 §3.1.4.b Outgoing message is not encrypted
2025-05-26 05:50:54.892 main DEBUG Adding cache entry: StateReference[msgID=128844,pduHandle=PduHandle[1641711580],securityEngineID=,securityModel=org.snmp4j.security.USM@108c4c35,securityName=,securityLevel=1,contextEngineID=,contextName=,retryMsgIDs=null]
2025-05-26 05:50:54.892 main DEBUG Running pending sync request with handle PduHandle[1641711580] and retry count left 0
2025-05-26 05:50:54.892 main DEBUG Looking up connection for destination ‘0.0.0.0/1168’ returned: null
2025-05-26 05:50:54.892 main DEBUG {}
2025-05-26 05:50:54.893 main DEBUG Socket for address ‘0.0.0.0/1168’ is closed, opening it…
2025-05-26 05:50:54.896 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is acceptable
2025-05-26 05:50:54.897 main DEBUG Trying to connect to 0.0.0.0/1168
2025-05-26 05:50:54.905 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Adding operation 1 for: SocketEntry[peerAddress=127.0.0.1/50466,socket=Socket[addr=/127.0.0.1,port=50466,localport=1168],lastUse=Tue Jan 06 06:48:23 UTC 1970,readBufferPosition=-1,socketTimeout=null]
2025-05-26 05:50:54.906 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Firing transport state event: org.snmp4j.transport.TransportStateEvent[source=org.snmp4j.transport.DefaultTcpTransportMapping@6fcd0951,peerAddress=127.0.0.1/50466,newState=1,cancelled=false,causingException=null]
2025-05-26 05:50:54.906 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is reading
2025-05-26 05:50:54.906 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Reading header 0 bytes from 127.0.0.1/50466
2025-05-26 05:50:54.906 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG No socket entry found for incoming address 127.0.0.1/50466 for incomplete message with length 0
2025-05-26 05:50:54.906 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Adding operation 8 for: SocketEntry[peerAddress=0.0.0.0/1168,socket=Socket[unconnected],lastUse=Tue Jan 06 06:48:23 UTC 1970,readBufferPosition=-1,socketTimeout=null]
2025-05-26 05:50:54.907 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is connectable
2025-05-26 05:50:54.907 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Connected to 0.0.0.0/1168
2025-05-26 05:50:54.907 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Adding operation 4 for: SocketEntry[peerAddress=0.0.0.0/1168,socket=Socket[addr=/0.0.0.0,port=1168,localport=50466],lastUse=Tue Jan 06 06:48:23 UTC 1970,readBufferPosition=-1,socketTimeout=org.snmp4j.transport.DefaultTcpTransportMapping$SocketTimeout@502119e5]
2025-05-26 05:50:54.907 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Fire connected event for 0.0.0.0/1168
2025-05-26 05:50:54.907 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Firing transport state event: org.snmp4j.transport.TransportStateEvent[source=org.snmp4j.transport.DefaultTcpTransportMapping@6fcd0951,peerAddress=0.0.0.0/1168,newState=1,cancelled=false,causingException=null]
2025-05-26 05:50:54.907 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is writable
2025-05-26 05:50:54.907 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Sent message with length 60 to 0.0.0.0/1168: 30:3a:02:01:03:30:10:02:03:01:f7:4c:02:03:00:ff:ff:04:01:04:02:01:03:04:10:30:0e:04:00:02:01:00:02:01:00:04:00:04:00:04:00:30:11:04:00:04:00:a0:0b:02:01:00:02:01:00:02:01:00:30:00
2025-05-26 05:50:54.907 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Adding operation 1 for: SocketEntry[peerAddress=0.0.0.0/1168,socket=Socket[addr=/0.0.0.0,port=1168,localport=50466],lastUse=Tue Jan 06 06:48:23 UTC 1970,readBufferPosition=-1,socketTimeout=org.snmp4j.transport.DefaultTcpTransportMapping$SocketTimeout@502119e5]
2025-05-26 05:50:54.907 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is readable
2025-05-26 05:50:54.907 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is reading
2025-05-26 05:50:54.908 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Reading header 6 bytes from 127.0.0.1/50466
2025-05-26 05:50:54.908 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Message length is org.snmp4j.transport.MessageLength[headerLength=2,payloadLength=58]
2025-05-26 05:50:54.908 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Message completed with 60 bytes and 60 buffer limit
2025-05-26 05:50:54.908 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Received message from 127.0.0.1/50466 with length 60: 30:3a:02:01:03:30:10:02:03:01:f7:4c:02:03:00:ff:ff:04:01:04:02:01:03:04:10:30:0e:04:00:02:01:00:02:01:00:04:00:04:00:04:00:30:11:04:00:04:00:a0:0b:02:01:00:02:01:00:02:01:00:30:00
2025-05-26 05:50:54.908 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is writable
2025-05-26 05:50:54.909 Trap.0 DEBUG SNMPv3 header decoded: msgId=128844, msgMaxSize=65535, msgFlags=04, secModel=3
2025-05-26 05:50:54.911 Trap.0 DEBUG RFC3414 §3.2.3 Unknown engine ID:
2025-05-26 05:50:54.911 Trap.0 DEBUG Adding cache entry: StateReference[msgID=128844,pduHandle=PduHandle[1641711581],securityEngineID=,securityModel=org.snmp4j.security.USM@108c4c35,securityName=,securityLevel=1,contextEngineID=,contextName=,retryMsgIDs=null]
2025-05-26 05:50:54.912 Trap.0 DEBUG Removed msgId retry cache sub-entry: 128844 from msgIdToPduHandleMapping: PduHandle[1641711581]
2025-05-26 05:50:54.912 Trap.0 DEBUG Removed cache entry: StateReference[msgID=128844,pduHandle=null,securityEngineID=,securityModel=org.snmp4j.security.USM@108c4c35,securityName=,securityLevel=1,contextEngineID=,contextName=,retryMsgIDs=null] for PduHandle PduHandle[1641711581]
2025-05-26 05:50:54.912 Trap.0 DEBUG Popped cache entry: StateReference[msgID=128844,pduHandle=PduHandle[1641711581],securityEngineID=,securityModel=org.snmp4j.security.USM@108c4c35,securityName=,securityLevel=1,contextEngineID=,contextName=,retryMsgIDs=null] for msgID 128844
2025-05-26 05:50:54.912 Trap.0 DEBUG RFC3414 §3.1.4.b Outgoing message is not encrypted
2025-05-26 05:50:54.912 Trap.0 DEBUG Looking up connection for destination ‘127.0.0.1/50466’ returned: SocketEntry[peerAddress=127.0.0.1/50466,socket=Socket[addr=/127.0.0.1,port=50466,localport=1168],lastUse=Tue Jan 06 06:48:23 UTC 1970,readBufferPosition=-1,socketTimeout=org.snmp4j.transport.DefaultTcpTransportMapping$SocketTimeout@77b7a02b]
2025-05-26 05:50:54.912 Trap.0 DEBUG {127.0.0.1/50466=SocketEntry[peerAddress=127.0.0.1/50466,socket=Socket[addr=/127.0.0.1,port=50466,localport=1168],lastUse=Tue Jan 06 06:48:23 UTC 1970,readBufferPosition=-1,socketTimeout=org.snmp4j.transport.DefaultTcpTransportMapping$SocketTimeout@77b7a02b], 0.0.0.0/1168=SocketEntry[peerAddress=0.0.0.0/1168,socket=Socket[addr=/0.0.0.0,port=1168,localport=50466],lastUse=Tue Jan 06 06:48:23 UTC 1970,readBufferPosition=-1,socketTimeout=org.snmp4j.transport.DefaultTcpTransportMapping$SocketTimeout@502119e5]}
2025-05-26 05:50:54.912 Trap.0 DEBUG Waking up selector for new message
2025-05-26 05:50:54.913 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Adding operation 4 for: SocketEntry[peerAddress=127.0.0.1/50466,socket=Socket[addr=/127.0.0.1,port=50466,localport=1168],lastUse=Tue Jan 06 06:48:23 UTC 1970,readBufferPosition=-1,socketTimeout=org.snmp4j.transport.DefaultTcpTransportMapping$SocketTimeout@77b7a02b]
2025-05-26 05:50:54.913 Trap.0 INFO Message from 127.0.0.1/50466 not dispatched, reason: statusInfo=1.3.6.1.6.3.15.1.1.4.0 = 0, status=1410
2025-05-26 05:50:54.913 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is writable
2025-05-26 05:50:54.913 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Sent message with length 103 to 127.0.0.1/50466: 30:65:02:01:03:30:10:02:03:01:f7:4c:02:03:00:ff:ff:04:01:00:02:01:03:04:1d:30:1b:04:0d:80:00:13:70:01:7f:00:01:01:bd:df:0c:88:02:01:00:02:01:00:04:00:04:00:04:00:30:2f:04:0d:80:00:13:70:01:7f:00:01:01:bd:df:0c:88:04:00:a8:1c:02:01:00:02:01:00:02:01:00:30:11:30:0f:06:0a:2b:06:01:06:03:0f:01:01:04:00:41:01:00
2025-05-26 05:50:54.913 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is writable
2025-05-26 05:50:54.913 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is readable
2025-05-26 05:50:54.913 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is reading
2025-05-26 05:50:54.913 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Reading header 6 bytes from 0.0.0.0/1168
2025-05-26 05:50:54.913 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Message length is org.snmp4j.transport.MessageLength[headerLength=2,payloadLength=101]
2025-05-26 05:50:54.913 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Message completed with 103 bytes and 103 buffer limit
2025-05-26 05:50:54.914 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Received message from 0.0.0.0/1168 with length 103: 30:65:02:01:03:30:10:02:03:01:f7:4c:02:03:00:ff:ff:04:01:00:02:01:03:04:1d:30:1b:04:0d:80:00:13:70:01:7f:00:01:01:bd:df:0c:88:02:01:00:02:01:00:04:00:04:00:04:00:30:2f:04:0d:80:00:13:70:01:7f:00:01:01:bd:df:0c:88:04:00:a8:1c:02:01:00:02:01:00:02:01:00:30:11:30:0f:06:0a:2b:06:01:06:03:0f:01:01:04:00:41:01:00
2025-05-26 05:50:54.914 Trap.0 DEBUG SNMPv3 header decoded: msgId=128844, msgMaxSize=65535, msgFlags=00, secModel=3
2025-05-26 05:50:54.914 Trap.0 DEBUG Accepting zero length security name
2025-05-26 05:50:54.915 Trap.0 DEBUG No cache entry found for popEntry by msgID=128844
2025-05-26 05:50:54.916 Trap.0 DEBUG RFC3412 §7.2.10 - Received PDU (msgID=128844) is a response or internal class message, but cached information for the msgID could not be found
2025-05-26 05:50:54.916 Trap.0 INFO Message from 0.0.0.0/1168 not dispatched, reason: statusInfo=noError, status=-1409

2025-05-26 05:50:59.894 Timer-1 DEBUG Request timed out: 1641711580
2025-05-26 05:50:59.895 Timer-1 DEBUG Removed msgId retry cache sub-entry: 128844 from msgIdToPduHandleMapping: PduHandle[1641711580]
2025-05-26 05:50:59.895 Timer-1 DEBUG Removed cache entry: StateReference[msgID=128844,pduHandle=null,securityEngineID=,securityModel=org.snmp4j.security.USM@108c4c35,securityName=,securityLevel=1,contextEngineID=,contextName=,retryMsgIDs=null] for PduHandle PduHandle[1641711580]
2025-05-26 05:50:59.896 main DEBUG Removed pending request with handle: PduHandle[1641711580]
Listening for SNMP traps on port 1168 using TCP
2025-05-26 05:51:02.101 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is acceptable
2025-05-26 05:51:02.102 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Adding operation 1 for: SocketEntry[peerAddress=127.0.0.1/48318,socket=Socket[addr=/127.0.0.1,port=48318,localport=1168],lastUse=Tue Jan 06 06:48:30 UTC 1970,readBufferPosition=-1,socketTimeout=null]
2025-05-26 05:51:02.102 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Firing transport state event: org.snmp4j.transport.TransportStateEvent[source=org.snmp4j.transport.DefaultTcpTransportMapping@6fcd0951,peerAddress=127.0.0.1/48318,newState=1,cancelled=false,causingException=null]
2025-05-26 05:51:02.102 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is reading
2025-05-26 05:51:02.102 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Reading header 6 bytes from 127.0.0.1/48318
2025-05-26 05:51:02.103 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Message length is org.snmp4j.transport.MessageLength[headerLength=3,payloadLength=168]
2025-05-26 05:51:02.103 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Message completed with 171 bytes and 171 buffer limit
2025-05-26 05:51:02.103 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Received message from 127.0.0.1/48318 with length 171: 30:81:a8:02:01:03:30:12:02:04:76:d9:d7:f3:02:04:7f:ff:ff:ff:04:01:03:02:01:03:04:3d:30:3b:04:0d:80:00:13:70:01:7f:00:01:01:bd:df:0c:88:02:01:01:02:04:02:b8:94:51:04:09:6e:6f:74:6c:73:75:73:65:72:04:0c:fe:19:72:c5:4a:36:5f:8e:f2:f7:5c:32:04:08:00:00:00:01:ab:af:0f:03:04:50:f3:73:60:5f:dd:ad:d0:58:24:53:55:e8:94:c0:d5:95:31:4a:55:36:7f:d4:6f:76:8c:66:1e:87:fb:9e:bb:e9:81:e0:f6:45:1a:a4:4c:07:02:87:9c:9c:33:40:21:e1:e9:27:c5:24:54:fd:19:0c:b5:4d:f7:e1:41:23:73:7e:5c:b6:88:83:37:b7:d0:ec:71:1d:92:5b:07:3a:43:bb
2025-05-26 05:51:02.103 Trap.0 DEBUG SNMPv3 header decoded: msgId=1993988083, msgMaxSize=2147483647, msgFlags=03, secModel=3
2025-05-26 05:51:02.104 Trap.0 DEBUG getUser(engineID=80:00:13:70:01:7f:00:01:01:bd:df:0c:88, securityName=notlsuser)
2025-05-26 05:51:02.103 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is readable
2025-05-26 05:51:02.105 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Key is reading
2025-05-26 05:51:02.105 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Reading header -1 bytes from 127.0.0.1/48318
2025-05-26 05:51:02.105 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Socket closed remotely
2025-05-26 05:51:02.106 DefaultTCPTransportMapping_0.0.0.0/1168 DEBUG Firing transport state event: org.snmp4j.transport.TransportStateEvent[source=org.snmp4j.transport.DefaultTcpTransportMapping@6fcd0951,peerAddress=127.0.0.1/48318,newState=2,cancelled=false,causingException=null]
2025-05-26 05:51:02.107 Trap.0 DEBUG RFC3414 §3.2.6 Wrong digest → authentication failure: fe:19:72:c5:4a:36:5f:8e:f2:f7:5c:32
2025-05-26 05:51:02.107 Trap.0 INFO Message from 127.0.0.1/48318 not dispatched, reason: statusInfo=1.3.6.1.6.3.15.1.1.5.0 = 0, status=1408

command
$ snmptrap -v 3 -e 0x80001370017f000101bddf0c88 -u notlsuser -a SHA -A authPass123 -x DES -X privPass123 -l authPriv -r 1 tcp:0.0.0.0:1168 40 “1.1.1.1”

@AGENTPP any inputs on what is causing this error will really help?

any updates?? @AGENTPP

That means that your authentication passphrase is wrong, i.e., does not match with the trap sender passphrase. If the passphrase is the same, then you are probably have an error in the engine ID used. The trap sender is authoritative and therefore he engine ID of the notification sender must be used to localise the password to the key.

@AGENTPP i am setting the localization engine id when i am creating the usmuser so the authentication phrase and privacy phrase should be localized for the engineid right?

Then i am using the localization engineid to send the traps but this fails with the authentication failure not sure what i am missing here?

i have also brought up the snmptrap daemon with the same configuration and when i send the trap to net-snmp trap daemon i could see that the authentication is successful and the trap message is decrypted properly and information is getting printed in the trap daemon logs.

so the configuration seems to be ok what i am missing in my java implementation to have this working is what i am trying to figure out… any inputs on this is highly appreciated.

Output:

USM user:UsmUser[secName=notlsuser,authProtocol=1.3.6.1.6.3.10.1.1.3,authPassphrase=authPass123,privProtocol=1.3.6.1.6.3.10.1.2.2,privPassphrase=privPass123,localizationEngineID=80:00:1f:88:80:e7:65:f3:78:82:63:bd:67:00:00:00:00]
Local engineid:80:00:13:70:01:7f:00:01:01:bd:df:0c:88
2025-05-30 09:54:58.748 main DEBUG Adding user notlsuser = UsmUser[secName=notlsuser,authProtocol=1.3.6.1.6.3.10.1.1.3,authPassphrase=authPass123,privProtocol=1.3.6.1.6.3.10.1.2.2,privPassphrase=privPass123**,localizationEngineID=80:00:1f:88:80:e7:65:f3:78:82:63:bd:67:00:00:00:00]**

2025-05-30 09:56:16.069 DefaultTCPTransportMapping_0.0.0.0/1171 DEBUG Received message from 127.0.0.1/39680 with length 175: 30:81:ac:02:01:03:30:12:02:04:51:c5:3d:fc:02:04:7f:ff:ff:ff:04:01:03:02:01:03:04:41:30:3f:04:11:80:00:1f:88:80:e7:65:f3:78:82:63:bd:67:00:00:00:00:02:01:01:02:04:00:9b:84:aa:04:09:6e:6f:74:6c:73:75:73:65:72:04:0c:2c:44:b8:ef:23:39:a6:0b:1e:09:42:bc:04:08:00:00:00:01:c1:14:d2:dd:04:50:13:d6:74:6f:8f:25:a7:ec:98:50:98:e1:9c:7f:a9:75:e4:bc:f3:47:99:a8:06:da:36:20:a4:fb:cc:a6:e5:e2:af:39:21:fa:26:d5:51:33:d1:25:e5:46:ce:11:82:ef:23:94:e5:41:9f:17:10:9d:7e:5c:0c:a0:d5:be:2c:6f:4a:c9:47:0b:dc:7c:0e:42:9c:aa:be:21:13:1f:3c:02
2025-05-30 09:56:16.069 DefaultTCPTransportMapping_0.0.0.0/1171 DEBUG Key is readable
2025-05-30 09:56:16.069 Trap.0 DEBUG SNMPv3 header decoded: msgId=1371880956, msgMaxSize=2147483647, msgFlags=03, secModel=3
2025-05-30 09:56:16.069 DefaultTCPTransportMapping_0.0.0.0/1171 DEBUG Key is reading
2025-05-30 09:56:16.069 Trap.0 DEBUG getUser(engineID=80:00:1f:88:80:e7:65:f3:78:82:63:bd:67:00:00:00:00, securityName=notlsuser)
2025-05-30 09:56:16.070 DefaultTCPTransportMapping_0.0.0.0/1171 DEBUG Reading header -1 bytes from 127.0.0.1/39680
2025-05-30 09:56:16.070 DefaultTCPTransportMapping_0.0.0.0/1171 DEBUG Socket closed remotely
2025-05-30 09:56:16.070 Trap.0 DEBUG RFC3414 §3.2.6 Wrong digest → authentication failure: 2c:44:b8:ef:23:39:a6:0b:1e:09:42:bc
2025-05-30 09:56:16.070 Trap.0 INFO Message from 127.0.0.1/39680 not dispatched, reason: statusInfo=1.3.6.1.6.3.15.1.1.5.0 = 0, status=1408

Code:
package snmpsimple;
import java.io.IOException;

import org.snmp4j.CommandResponder;
import org.snmp4j.CommandResponderEvent;
import org.snmp4j.MessageDispatcherImpl;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.log.ConsoleLogAdapter;
import org.snmp4j.log.ConsoleLogFactory;
import org.snmp4j.log.LogFactory;
import org.snmp4j.mp.MPv3;
import org.snmp4j.security.AuthHMAC192SHA256;
import org.snmp4j.security.AuthMD5;
import org.snmp4j.security.AuthSHA;
import org.snmp4j.security.AuthenticationProtocol;
import org.snmp4j.security.PrivAES128;
import org.snmp4j.security.PrivDES;
import org.snmp4j.security.PrivacyProtocol;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.USM;
import org.snmp4j.security.UsmUser;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.TcpAddress;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.transport.DefaultTcpTransportMapping;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.util.MultiThreadedMessageDispatcher;
import org.snmp4j.util.ThreadPool;

public class SnmpTrapReceiver {

static {
    LogFactory.setLogFactory(new ConsoleLogFactory());
    ConsoleLogAdapter.setDebugEnabled(true);
}


private static final String SNMP_PORT = System.getenv("SNMP_PORT");
private static final String SNMP_AUTH_PROTOCOL = System.getenv("SNMP_AUTH_PROTOCOL");
private static final String SNMP_PRIVACY_PROTOCOL = System.getenv("SNMP_PRIVACY_PROTOCOL");
private static final String SNMP_AUTH_PHRASE = System.getenv("SNMP_AUTH_PHRASE");
private static final String SNMP_PRIVACY_PHRASE = System.getenv("SNMP_PRIVACY_PHRASE");
private static final String SNMP_PROTOCOL = System.getenv("SNMP_PROTOCOL");
private static final String SNMP_USER=System.getenv("SNMP_USER");
private static final String NOTIFICATION_PORT = System.getenv("NOTIFICATION_PORT");
private static final String SNMP_ENGINE_ID=System.getenv("SNMP_ENGINE_ID");
private static final String LOCAL_ENGINE_ID="80:00:1f:88:80:e7:65:f3:78:82:63:bd:67:00:00:00:00";



public static void main(String[] args) {
    try {
    	
    	System.out.println(SNMP_PORT);
    	System.out.println(SNMP_AUTH_PROTOCOL);
    	System.out.println(SNMP_USER);
    	System.out.println(SNMP_ENGINE_ID);
    	System.out.println("::"+SNMP_AUTH_PHRASE+"::");
    	System.out.println("::"+SNMP_PRIVACY_PHRASE+"::");
    	
    	//MPv3.createLocalEngineID()
    	Address listenAddress, notificationAddress;
    	ThreadPool threadPool = ThreadPool.create("Trap", 2);
    	MultiThreadedMessageDispatcher dispatcher =
    	        new MultiThreadedMessageDispatcher(threadPool,
    	                                           new MessageDispatcherImpl());
        // Create transport mapping
        TransportMapping<?> transport;
        if ("TCP".equalsIgnoreCase(SNMP_PROTOCOL)) {
        	listenAddress = GenericAddress.parse("tcp:0.0.0.0/"+SNMP_PORT);
        	notificationAddress = GenericAddress.parse("tcp:0.0.0.0/"+NOTIFICATION_PORT);
            transport = new DefaultTcpTransportMapping((TcpAddress)listenAddress);
        } else {
        	listenAddress = GenericAddress.parse("udp:0.0.0.0/"+SNMP_PORT);
            transport = new DefaultUdpTransportMapping((UdpAddress)listenAddress);
            notificationAddress = GenericAddress.parse("udp:0.0.0.0/"+NOTIFICATION_PORT);
        }
        transport.listen();
        
        SecurityProtocols securityProtocols = SecurityProtocols.getInstance();
        securityProtocols.addDefaultProtocols();
        
        AuthenticationProtocol authProtocol = getAuthProtocol(SNMP_AUTH_PROTOCOL);
        PrivacyProtocol privProtocol = getPrivProtocol(SNMP_PRIVACY_PROTOCOL);
        
        System.out.println("auth:"+ new OctetString(SNMP_AUTH_PHRASE).getValue());
        System.out.println("priv:"+ new OctetString(SNMP_PRIVACY_PHRASE).getValue());

        byte[] authLocalKey = securityProtocols.passwordToKey(authProtocol.getID(), new OctetString(SNMP_AUTH_PHRASE), LOCAL_ENGINE_ID.getBytes());
        byte[] privLocalKey = securityProtocols.passwordToKey(authProtocol.getID(), new OctetString(SNMP_PRIVACY_PHRASE), LOCAL_ENGINE_ID.getBytes());
        System.out.println(authLocalKey);
        System.out.println(privLocalKey);
        if (authProtocol != null) {
        	System.out.println(authProtocol);
		    securityProtocols.addAuthenticationProtocol(authProtocol);
		}
		if (privProtocol != null) {
		    securityProtocols.addPrivacyProtocol(privProtocol);
		}
		
		OctetString engineId = OctetString.fromHexString(SNMP_ENGINE_ID);
		
		// Create user
		OctetString authKey = new OctetString(authLocalKey);
		OctetString privKey = new OctetString(privLocalKey);
		
		
	
        UsmUser  user = new UsmUser (new OctetString(SNMP_USER),
                authProtocol.getID(),
                new OctetString(SNMP_AUTH_PHRASE),
                privProtocol.getID(),
                new OctetString(SNMP_PRIVACY_PHRASE),OctetString.fromHexString(LOCAL_ENGINE_ID));
        
        System.out.println("USM user:"+user.toString());
        
        System.out.println("Local engineid:"+SNMP_ENGINE_ID);
        USM usm = new USM(securityProtocols,
        		OctetString.fromHexString(LOCAL_ENGINE_ID), 0);
        
       
        usm.setEngineDiscoveryEnabled(true);
        usm.addUser(new OctetString(SNMP_USER),OctetString.fromHexString(LOCAL_ENGINE_ID),user);
        //SecurityModels.getInstance().addSecurityModel(usm);

        //snmp.setLocalEngine(engineID, 0, 0);
        MPv3 mpv3=  new MPv3(usm);
        //mpv3.getSecurityModels().addSecurityModel(usm);
        dispatcher.addMessageProcessingModel(mpv3);
      
        // Create SNMP object
        Snmp snmp = new Snmp(dispatcher,transport); 
        
       
        //snmp.addCommandResponder(new TrapListener());
        snmp.addNotificationListener(notificationAddress, new TrapListener());
        snmp.discoverAuthoritativeEngineID(listenAddress, 5000);

        // Set up security protocols
          System.out.println("Listening for SNMP traps on port " + SNMP_PORT + " using " + SNMP_PROTOCOL);

        // Keep the application running
        while (true) {
            Thread.sleep(1000);
        }
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
    }
}

private static AuthenticationProtocol getAuthProtocol(String protocol) {
    switch (protocol.toUpperCase()) {
        case "SHA":
            return new AuthSHA();
        case "MD5":
            return new AuthMD5();
        case "SHA256":
        	return new AuthHMAC192SHA256();
     
        default:
            System.err.println("Unsupported authentication protocol: " + protocol);
            return null;
    }
}

private static PrivacyProtocol getPrivProtocol(String protocol) {
    switch (protocol.toUpperCase()) {
        case "AES":
            return new PrivAES128();
        case "DES":
            return new PrivDES();
        default:
            System.err.println("Unsupported privacy protocol: " + protocol);
            return null;
    }
}


static class TrapListener implements CommandResponder {
	@Override
	public void processPdu(CommandResponderEvent arg0) {
		System.out.println("Received SNMP Trap:" +arg0.getPDU());
		
	}
}

}

You are using the local Engine ID of the notification receiver for the USM user that should receive a notification. That is wrong, because you must use the engine ID of the notification sender instead.

Trapd conf
#for v3 with engineid without tls
createUser -e 0x80001f8880e765f3788263bd6700000000 notlsuser SHA authPass123 DES privPass123
authUser log,execute,net notlsuser

Daemon running on 1187
$ ps -eaf | grep snmp
Debian-+ 162 1 0 May29 ? 00:00:26 /usr/sbin/snmpd -LOw -u Debian-snmp -g Debian-snmp -I -smux mteTrigger mteTriggerConf -f
pavithra 104074 81428 0 09:45 pts/7 00:00:00 snmptrapd -f -d -p /tmp/snmpd.pid -Lf /tmp/snmptrapd.log -C -c /home/pavithra/trapd.conf udp:127.0.0.1:1187 tcp:127.0.0.1:1187
pavithra 104425 62402 0 09:54 pts/5 00:00:05 java -cp snmp4j-2.8.18.jar:. snmpsimple.SnmpTrapReceiver
pavithra 105895 81428 0 10:38 pts/7 00:00:00 grep --color=auto snmp
pavithra@wsl2-u24.04-nokia:~
$ snmptrap -v 3 -e 0x80001f8880e765f3788263bd6700000000 -u notlsuser -a SHA -A authPass123 -x DES -X privPass123 -l authPriv -r 1 tcp:0.0.0.0:1187 40 ‘1.1.1.1.1.2’
pavithra@wsl2-u24.04-nokia:~
$ tail -f /tmp/snmptrapd.log
0064: 73 75 73 65 72 04 0C DF 70 25 CB F2 C2 20 11 13 suser…p%… …
0080: 00 EB 48 04 08 00 00 00 01 83 CD C7 DA 04 50 55 …H…PU
0096: F0 AB 4E DC 21 01 68 ED 17 F4 DD 10 31 58 C2 02 …N.!.h…1X…
0112: 0C 64 21 F5 8C 1A D5 73 92 3E 05 9F C9 C4 9C 30 .d!..s.>…0
0128: A5 0A EB 31 B4 AE 38 2A 06 DB B0 CE CC A0 26 90 …1…8*…&.
0144: E6 89 D1 31 C1 AA A8 8E 0A 09 CE EA 1E 91 CE EB …1…
0160: 6D 9B FD 3D F9 EB C7 5B 42 C1 FD 61 D4 E6 AD m…=…[B…a…

2025-05-30 10:39:16 localhost [TCP: [127.0.0.1]:39688->[0.0.0.0]:0]:
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (40) 0:00:00.40 SNMPv2-MIB::snmpTrapOID.0 = OID: iso.1.1.1.1.2
^C
pavithra@wsl2-u24.04-nokia:~
$ date
Fri May 30 10:39:46 UTC 2025
pavithra@wsl2-u24.04-nokia:~
$

@AGENTPP thanks for your inputs. now i am seeing the below error this messages work fine when I start the java application but after 150 seconds the messages are dropped but with the snmptrap daemon this works fine it never complains is there something that the notification sender needs to take care off. or is there anything that has to be done on the java application(notification receiver)

2025-05-30 17:18:45.470 Trap.0 DEBUG CheckTime: received message outside time window (authoritative):2617 > 150
2025-05-30 17:18:45.470 Trap.0 DEBUG RFC3414 §3.2.7.a Not in time window; engineID=‘80:00:1f:88:80:e7:65:f3:78:82:63:bd:67:00:00:00:00’, engineBoots=0, engineTime=0

You have to increase and set the engineBoot counter of the USM after each restart of your application.

when my application(java based notification receiver) comes up the engineBoots is harded code to 0 for the USM and the the problem is not between restarts after the notification receiver comes up it receives the trap only for 150 seconds and start ignoring the packets with the usm not in time window error. But as per the RFC if the unconfirmed pdu class (trap) the trap sender is authoritative right so what ever he sends should be accepted correct?

USM usm = new USM(securityProtocols,
OctetString.fromHexString(LOCAL_ENGINE_ID), 0);

Yes, the trap sender is authoritative, but the receiver will not accept messages that are too old. After the trap receiver has received a message with values for engineBoots and engineTime, it will expect that a message received N seconds later to have either an increased engineBoots value or to have the same engineBoots value and engineTime increased by N. The time window of 150 seconds exists in order to compensate inaccurate clocks. The authors of the USM specification expect that between two SNMP messages the clocks of the involved entities will not differ by more than 150 seconds.

1 Like

@AGENTPP /@jkatz
from application which is sending the inform request to java doesnt know the engineid so it sends the getrequest to the java application and in response report pdu contains only the engineid it doesnt have the actual value for engineboots and enginetime they are “0”.

so because of this reason the application is sending back the same request with the engineboots and enginetime as “0” and the java application is rejecting the request with out of time window.

Does the java application doesnt have to respond back to the getrequest with proper engineboots and enginetime?

Do let me know what is missing in java code or what needs to be configured to have it send the proper engineboots and enginetime?