[SNMP4J] IPv6 Address with Scope

Senthil Muniswamy senthil.muniswamy at gmail.com
Thu Mar 19 01:10:26 CET 2009


Frank,
I am using SNMP4J-Agent and
TransportIpAddress.setTransportAddress(OctetString transportAddress)
causes exception if snmpTargetAddrTAddress is set with TAddress (IPv6
Address with Scope).

I have made below changes to TransportIpAddress to handle the scope,
could you please confirm if this is correct?

  public void setTransportAddress(OctetString transportAddress) throws
      UnknownHostException {
    OctetString inetAddr =
        transportAddress.substring(0, transportAddress.length()-2);
    byte[] addr = inetAddr.getValue();
    if(addr.length <= 16) {
    	setInetAddress(InetAddress.getByAddress(addr));
    }
    else {
        byte[] ip6addr = new byte[16];
        System.arraycopy(addr, 0, ip6addr, 0, ip6addr.length);
        int scope = ((addr[16] << 24) + ((addr[17] & 0xff) << 16) +
((addr[18] & 0xFF) << 8) +
                (addr[19] & 0xFF));
        setInetAddress(Inet6Address.getByAddress(null, ip6addr, scope));
    }
    port = ((transportAddress.get(transportAddress.length()-2) & 0xFF) << 8);
    port += (transportAddress.get(transportAddress.length()-1) & 0xFF);
  }

  public byte[] getValue() {
        byte[] addr = getInetAddress().getAddress();
        int size = 2;
        if(getInetAddress() instanceof Inet6Address &&
                ((Inet6Address)getInetAddress()).getScopeId() > 0) {
            size = size+4;
        }
        byte[] retval = new byte[addr.length+size];
        System.arraycopy(addr, 0, retval, 0, addr.length);
        if(size > 2) {
            int scope = ((Inet6Address)getInetAddress()).getScopeId();
            retval[addr.length] = (byte)((scope & 0xFF000000) >> 24);
            retval[addr.length+1] = (byte)((scope & 0x00FF0000) >> 16);
            retval[addr.length+2] = (byte)((scope & 0x0000FF00) >> 8);
            retval[addr.length+3] = (byte)(scope & 0x000000FF);
        }
        retval[retval.length-2] = (byte)((port >> 8) & 0xFF);
        retval[retval.length-1] = (byte)(port & 0xFF);
    return retval;
  }

  public int getBERLength() {
    return getValue().length;
  }


Thanks,
Senthil



More information about the SNMP4J mailing list