[SNMP4J] Sending and receiving of SNMPv3 packets
Lulu
fasole4ka at gmail.com
Tue Mar 13 12:57:43 CET 2007
Hello!
I have two programms, first(Send.java) sends the snmpv3 packet and listens
for the reply. Second(Receive.java) acts like a small agent - it receives
packet and sends the reply. The problem I face is that the response is sent,
but it is not received by the manager. Here is the part of code I use.
Receive.java
class SNMPEmulator extends Thread implements CommandResponder {
SNMPEmulator(String host_) throws UnknownHostException {
// port=port_;
hostIp = InetAddress.getByName(host_);
// transport = new DefaultUdpTransportMapping(new
UdpAddress(hostIp,port_));
}
@Override
public void run() {
try {
/*
Sets the snmp listener to port
*/
TransportMapping transport = new
DefaultUdpTransportMapping(new UdpAddress(hostIp, port_));
Snmp snmp = new Snmp(transport);
if (version == SnmpConstants.version3) {
byte[] localEngineID =
((MPv3)snmp.getMessageProcessingModel(
MessageProcessingModel.MPv3)).createLocalEngineID();
USM usm = new USM(SecurityProtocols.getInstance(),
new OctetString(localEngineID),
0);
SecurityModels.getInstance().addSecurityModel(usm);
snmp.setLocalEngine(localEngineID, 0, 0);
// Add the configured user to the USM
snmp.getUSM ().addUser(new OctetString("MD5DES"),
new UsmUser(new
OctetString("MD5DES"),
AuthMD5.ID,
new
OctetString("MD5DESUserAuthPassword"),
PrivDES.ID ,
new
OctetString("MD5DESUserPrivPassword")));
}
snmp.addCommandResponder(this);
// md.addCommandResponder(this);
System.out.println("transportBinding..");
snmp.listen();
// transport.listen();
System.out.print("SNMP Listener init");
Address a = transport.getListenAddress();
System.out.println(a.toString());
} catch (IOException e) {
System.err.print(e);
}
}
// Processes incomming Pdu
public synchronized void processPdu(CommandResponderEvent e) {
System.out.println("Command");
PDU command;
if ( e.getPDU() instanceof ScopedPDU) {
command = (ScopedPDU) e.getPDU();
} else {
command = e.getPDU();
}
if (command != null) {
//send reply
PDU command = e.getPDU();
command.setType(PDU.RESPONSE);
StatusInformation st=new StatusInformation(new VariableBinding(),
e.getStateReference().getContextName(),e.getStateReference().getContextEngineID(),new
Integer32( e.getStateReference().getSecurityLevel()) );
StatusInformation statusInformation = new StatusInformation();
StateReference ref = e.getStateReference();
System.out.println(ref);
try {
e.getMessageDispatcher().returnResponsePdu(
e.getMessageProcessingModel(), e.getSecurityModel(),
e.getSecurityName(),
e.getSecurityLevel(),
command,
e.getMaxSizeResponsePDU(),
ref,
st);
System.out.println("RESPONSE SENT");
} catch (MessageException e1) {
e1.printStackTrace(); //To change body of catch statement use
File | Settings | File Templates.
}
}}
Send.java
public class Send {
Send(){}
public static void main(String[] args){
try {
ScopedPDU pdu = new ScopedPDU();
pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.2.1"))); // ifNumber
pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.2.2.1.10"))); // ifInOctets
pdu.add (new VariableBinding(new OID("1.3.6.1.2.1.2.2.1.16"))); //
ifOutOctets
pdu.setType(PDU.GETNEXT);
pdu.setMaxRepetitions(50);
// Get ifNumber only once
pdu.setNonRepeaters(1);
// set context non-default context (default context does not need to be set)
//pdu.setContextName(new OctetString("subSystemContextA"));
// set non-default context engine ID (to use targets authoritative engine ID
// use an empty (size == 0) octet string)
//pdu.setContextEngineID( OctetString.fromHexString
("80:00:13:70:c0:a8:01:0d"));
UserTarget target = new UserTarget();
Address targetAddress = GenericAddress.parse("127.0.0.1/8089
");
target.setAddress(targetAddress);
target.setRetries(1);
// set timeout to 500 milliseconds -> 2*500ms = 1s total timeout
target.setTimeout(500);
target.setVersion(SnmpConstants.version3);
target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
target.setSecurityName(new OctetString("MD5DES"));
Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
USM usm = new USM(SecurityProtocols.getInstance(),
new
OctetString(MPv3.createLocalEngineID()),
0);
SecurityModels.getInstance().addSecurityModel(usm);
snmp.getUSM().addUser(new OctetString("MD5DES"),
new UsmUser(new OctetString("MD5DES"),
AuthMD5.ID ,
new
OctetString("MD5DESUserAuthPassword"),
PrivDES.ID,
new
OctetString("MD5DESUserPrivPassword")));
snmp.listen();
ResponseEvent response = snmp.send(pdu, target);
// response = snmp.send(request, target);
if (response.getResponse() != null) {
PDU response1 = response.getResponse();
System.out.println("Received response after "+
(System.currentTimeMillis())+"
millis\n"+response1.toString());
} else{
System.out.println("NO RESPONSE");
}
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use
File | Settings | File Templates.
}
}
I would appreciate any help.
Thanks, Lesya
More information about the SNMP4J
mailing list