[SNMP4J] Sending PDU error

Frediano Sorvino sorfrena at gmail.com
Thu Feb 14 15:01:35 CET 2008


Hi at all! I have been looking for many example in this mailing list but
actually I can't figure out what's going wrong in my code...it's pretty
small and should do things already everyone is doing...but I can't have it
working.
Well, I am trying from a remote host sending the command: snmpget -v2c -c
public IP OID (naturally i put my ip and the oid i want)
What happens is that I can receive the remote request...I try to answer but
the remote machine tells me something like bad fragmentation. I was looking
for a full example in which an agent receives a snmp packet and answers, but
i couldn't find it. I'd be interested in understanding what I have to change
in the code whether I use version 1 or 2c of SNMP.
Any help is welcome...thank you
Freddy


package org.snmp4j;

import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.snmp4j.mp.MPv3;
import org.snmp4j.security.SecurityModels;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.USM;
import org.snmp4j.smi.OctetString;
import org.snmp4j.transport.DefaultUdpTransportMapping;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.MessageProcessingModel;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.SecurityLevel;
import org.snmp4j.smi.*;

public class PruebaSnmp extends Thread implements CommandResponder {

    private Snmp snmp;
    private UdpAddress udpAddress;

    public PruebaSnmp() {
        try {
            udpAddress = new UdpAddress("0.0.0.0/161");
            TransportMapping transport = new
DefaultUdpTransportMapping(udpAddress);
            snmp = new Snmp(transport);
            snmp.addCommandResponder(this);
            transport.listen();
            start();


        } catch (IOException ex) {
            Logger.getLogger(PruebaSnmp.class.getName()).log(Level.SEVERE,
null, ex);
        }
    }

    public void run() {
        while (true){
        }
    }
    public synchronized void processPdu(CommandResponderEvent e) {
        PDU command = e.getPDU();
        if (command != null) {

            CommunityTarget target = new CommunityTarget();
            target.setCommunity(new OctetString("public"));
            target.setAddress((UdpAddress) e.getPeerAddress());
            target.setRetries(10);
            target.setTimeout(10000);
            target.setVersion(SnmpConstants.version2c);
            try {
                Vector variableBindings = command.getVariableBindings();
                System.out.println("variableBindings size: " +
variableBindings.size());
                PDU responsePDU = new PDU();
                for (int i = 0; i < variableBindings.size(); i++) {
                    System.out.println("value numero " + i + " : ");
                    VariableBinding var = (VariableBinding)
variableBindings.get(i);
                    int[] value = new int[var.getOid().getValue().length +
1];
                    System.out.println("size request : " +
var.getOid().getValue().length
+ " size response: " + value.length);
                    for(int j = 0; j < var.getOid().getValue().length; j++){
                         value[j] = var.getOid().getValue()[j];
                    }
                    value[value.length - 1] = 1;
                    System.out.println("value oid request: ");
                    for (int j = 0; j < var.getOid().getValue().length; j++)

                        System.out.print(var.getOid().getValue()[j]);
                    System.out.println("value oid response: ");
                     for (int j = 0; j < value.length; j++)
                        System.out.print(value[j]);
                    var.setOid(new OID(value));
                    var.setVariable(new Integer32(5));
                    responsePDU.setType(command.getType());
                    responsePDU.add(var);
                    responsePDU.setRequestID(new Integer32(
command.getRequestID().getValue()));

                ResponseEvent response = snmp.send(responsePDU, target,
e.getTransportMapping());
                PDU answer = response.getResponse();

                ResponseEvent response = snmp.send(responsePDU, new
CommunityTarget(e.getPeerAddress(), new OctetString("public")));
            } catch (IOException ex) {
                Logger.getLogger(PruebaSnmp.class.getName()).log(
Level.SEVERE, null, ex);
            }
        }
    }


    public static void main(String [] args){
        PruebaSnmp prueba = new PruebaSnmp();
    }



}



More information about the SNMP4J mailing list