/*
* Copyright (c) 2001, 2006, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.jmx.snmp;
import java.net.InetAddress;
import java.io.Serializable;
import java.net.UnknownHostException;
import java.util.StringTokenizer;
import java.util.Arrays;
import java.util.NoSuchElementException;
import com.sun.jmx.snmp.internal.SnmpTools;
/**
* This class is handling an SnmpEngineId
data. It copes with binary as well as String
representation of an engine Id. A string format engine is an hex string starting with 0x.
*
This API is a Sun Microsystems internal API and is subject * to change without notice.
* @since 1.5 */ public class SnmpEngineId implements Serializable { private static final long serialVersionUID = 5434729655830763317L; byte[] engineId = null; String hexString = null; String humanString = null; /** * NewSnmpEngineId
with an hex string value. Can handle engine Id format <host>:<port>.
* @param hexString Hexa string.
*/
SnmpEngineId(String hexString) {
engineId = SnmpTools.ascii2binary(hexString);
this.hexString = hexString.toLowerCase();
}
/**
* New SnmpEngineId
with a binary value. You can use SnmpTools
to convert from hex string to binary format.
* @param bin Binary value
*/
SnmpEngineId(byte[] bin) {
engineId = bin;
hexString = SnmpTools.binary2ascii(bin).toLowerCase();
}
/**
* If a string of the format <address>:<port>:<IANA number> has been provided at creation time, this string is returned.
* @return The Id as a readable string or null if not provided.
*/
public String getReadableId() {
return humanString;
}
/**
* Returns a string format engine Id.
* @return String format value.
*/
public String toString() {
return hexString;
}
/**
* Returns a binary engine Id.
* @return Binary value.
*/
public byte[] getBytes() {
return engineId;
}
/**
* In order to store the string used to create the engineId.
*/
void setStringValue(String val) {
humanString = val;
}
static void validateId(String str) throws IllegalArgumentException {
byte[] arr = SnmpTools.ascii2binary(str);
validateId(arr);
}
static void validateId(byte[] arr) throws IllegalArgumentException {
if(arr.length < 5) throw new IllegalArgumentException("Id size lower than 5 bytes.");
if(arr.length > 32) throw new IllegalArgumentException("Id size greater than 32 bytes.");
//octet strings with very first bit = 0 and length != 12 octets
if( ((arr[0] & 0x80) == 0) && arr.length != 12)
throw new IllegalArgumentException("Very first bit = 0 and length != 12 octets");
byte[] zeroedArrays = new byte[arr.length];
if(Arrays.equals(zeroedArrays, arr)) throw new IllegalArgumentException("Zeroed Id.");
byte[] FFArrays = new byte[arr.length];
Arrays.fill(FFArrays, (byte)0xFF);
if(Arrays.equals(FFArrays, arr)) throw new IllegalArgumentException("0xFF Id.");
}
/**
* Generates an engine Id based on the passed array.
* @return The created engine Id or null if given arr is null or its length == 0;
* @exception IllegalArgumentException when:
* Generates a unique engine Id. Hexadecimal strings as well as a textual description are supported. The textual format is as follow:
*
<address>:<port>:<IANA number>
The allowed formats :
*InetAddress
is null.
*/
public static SnmpEngineId createEngineId(InetAddress addr,
int port,
int iana) {
if(addr == null) throw new IllegalArgumentException("InetAddress is null.");
byte[] address = addr.getAddress();
byte[] engineid = new byte[9 + address.length];
engineid[0] = (byte) ( (iana & 0xFF000000) >> 24 );
engineid[0] |= 0x80;
engineid[1] = (byte) ( (iana & 0x00FF0000) >> 16 );
engineid[2] = (byte) ( (iana & 0x0000FF00) >> 8 );
engineid[3] = (byte) (iana & 0x000000FF);
engineid[4] = 0x05;
if(address.length == 4)
engineid[4] = 0x01;
if(address.length == 16)
engineid[4] = 0x02;
for(int i = 0; i < address.length; i++) {
engineid[i + 5] = address[i];
}
engineid[5 + address.length] = (byte) ( (port & 0xFF000000) >> 24 );
engineid[6 + address.length] = (byte) ( (port & 0x00FF0000) >> 16 );
engineid[7 + address.length] = (byte) ( (port & 0x0000FF00) >> 8 );
engineid[8 + address.length] = (byte) ( port & 0x000000FF );
return new SnmpEngineId(engineid);
}
/**
* Generates an engine Id based on an InetAddress. Handles IPv4 and IPv6 addresses. The creation algorithm uses the passed IANA number.
* @param iana Your enterprise IANA number.
* @param addr The IP address the SNMPv3 Adaptor Server is listening to.
* @return The generated engine Id.
* @since 1.5
* @exception UnknownHostException if the provided InetAddress
is null.
*/
public static SnmpEngineId createEngineId(int iana, InetAddress addr)
{
if(addr == null) throw new IllegalArgumentException("InetAddress is null.");
byte[] address = addr.getAddress();
byte[] engineid = new byte[5 + address.length];
engineid[0] = (byte) ( (iana & 0xFF000000) >> 24 );
engineid[0] |= 0x80;
engineid[1] = (byte) ( (iana & 0x00FF0000) >> 16 );
engineid[2] = (byte) ( (iana & 0x0000FF00) >> 8 );
engineid[3] = (byte) (iana & 0x000000FF);
if(address.length == 4)
engineid[4] = 0x01;
if(address.length == 16)
engineid[4] = 0x02;
for(int i = 0; i < address.length; i++) {
engineid[i + 5] = address[i];
}
return new SnmpEngineId(engineid);
}
/**
* Generates an engine Id based on an InetAddress. Handles IPv4 and IPv6
* addresses. The creation algorithm uses the sun IANA number (42).
* @param addr The IP address the SNMPv3 Adaptor Server is listening to.
* @return The generated engine Id.
* @since 1.5
* @exception UnknownHostException if the provided
* InetAddress
is null.
*/
public static SnmpEngineId createEngineId(InetAddress addr) {
return createEngineId(42, addr);
}
/**
* Tests SnmpEngineId
instance equality. Two SnmpEngineId
are equal if they have the same value.
* @return true
if the two SnmpEngineId
are equals, false
otherwise.
*/
public boolean equals(Object a) {
if(!(a instanceof SnmpEngineId) ) return false;
return hexString.equals(((SnmpEngineId) a).toString());
}
public int hashCode() {
return hexString.hashCode();
}
}