Im trying to implement a SNMP trap listener which based on the examples on the internet. I was able to make it work. My problem is that the V3 trap messages are not being captured. I'm aware that V3 has an authentication so I'm quite certain that the problem might relate to this, however the router sending the traps is configured to not use auth.
snmp-server host 192.168.199.190 version 3 noauth testuser mac-notification snmp
Here is my relevant code:
public class TrapHandler implements CommandResponder {
private static final String _V3_USERNAME = "testuser";
private static final String _V3_AUTHENTICATION_PASSPHRASE = "idontKnowThis";
private static final String _V3_PRIVACY_PASSPHRASE = "dontKnowThisEither";
@Override
public void processPdu(CommandResponderEvent crEvent) {
PDU pdu = crEvent.getPDU();
System.out.println("THE TYPE OF THE PDU: " + pdu.getType());
if (pdu.getType() == PDU.V1TRAP) {
PDUv1 pduV1 = (PDUv1) pdu;
System.out.println("");
System.out.println("===== NEW SNMP 1 TRAP RECEIVED ====");
System.out.println("agentAddr " + pduV1.getAgentAddress().toString());
System.out.println("enterprise " + pduV1.getEnterprise().toString());
System.out.println("timeStam" + String.valueOf(pduV1.getTimestamp()));
System.out.println("genericTrap" + String.valueOf(pduV1.getGenericTrap()));
System.out.println("specificTrap " + String.valueOf(pduV1.getSpecificTrap()));
System.out.println("snmpVersion " + String.valueOf(PDU.V1TRAP));
System.out.println("communityString " + new String(crEvent.getSecurityName()));
} else if (pdu.getType() == PDU.TRAP) {
System.out.println("");
System.out.println("===== NEW SNMP 2/3 TRAP RECEIVED ====");
System.out.println("errorStatus " + String.valueOf(pdu.getErrorStatus()));
System.out.println("errorIndex " + String.valueOf(pdu.getErrorIndex()));
System.out.println("requestID " + String.valueOf(pdu.getRequestID()));
System.out.println("snmpVersion " + String.valueOf(PDU.TRAP));
System.out.println("communityString " + new String(crEvent.getSecurityName()));
} else {
System.out.println("Received a strange type of PDU " + pdu.getType());
}
Vector<? extends VariableBinding> varBinds = pdu.getVariableBindings();
if (varBinds != null && !varBinds.isEmpty()) {
Iterator<? extends VariableBinding> varIter = varBinds.iterator();
StringBuilder resultset = new StringBuilder();
resultset.append("-----");
while (varIter.hasNext()) {
VariableBinding vb = varIter.next();
String syntaxstr = vb.getVariable().getSyntaxString();
int syntax = vb.getVariable().getSyntax();
System.out.println("OID: " + vb.getOid());
System.out.println("Value: " + vb.getVariable());
System.out.println("syntaxstring: " + syntaxstr);
System.out.println("syntax: " + syntax);
System.out.println("------");
}
}
System.out.println("==== TRAP END ===");
System.out.println("");
}
public synchronized void listen(TransportIpAddress address) throws IOException {
AbstractTransportMapping transport;
if (address instanceof TcpAddress) {
transport = new DefaultTcpTransportMapping((TcpAddress) address);
} else {
transport = new DefaultUdpTransportMapping((UdpAddress) address);
}
ThreadPool threadPool = ThreadPool.create("DispatcherPool", 10);
MessageDispatcher mtDispatcher = new MultiThreadedMessageDispatcher(threadPool, new MessageDispatcherImpl());
USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0); // maybe the issue is here?
usm.setEngineDiscoveryEnabled(true);
// add message processing models
mtDispatcher.addMessageProcessingModel(new MPv1());
mtDispatcher.addMessageProcessingModel(new MPv2c());
mtDispatcher.addMessageProcessingModel(new MPv3(usm));
// add all security protocols
SecurityProtocols.getInstance().addDefaultProtocols();
// SecurityProtocols.getInstance().addPrivacyProtocol(new PrivAES128());
SecurityModels.getInstance().addSecurityModel(usm);
// Create Target
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
Snmp snmp = new Snmp(mtDispatcher, transport);
UsmUser usmUser = new UsmUser(new OctetString("newUser"), AuthSHA.ID,
new OctetString(_V3_AUTHENTICATION_PASSPHRASE), PrivAES128.ID, new OctetString(_V3_PRIVACY_PASSPHRASE));
snmp.getUSM().addUser(new OctetString(_V3_USERNAME), usmUser); // or here
snmp.addCommandResponder(this);
transport.listen();
System.out.println("Listening on " + address);
try {
this.wait();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}