Methods Summary |
---|
public static com.sun.jmx.snmp.SnmpEngineId | createEngineId()Generates an engine Id that is unique to the host the agent is running on. The engine Id unicity is system time based. The creation algorithm uses the SUN Microsystems IANA number (42).
byte[] address = null;
byte[] engineid = new byte[13];
int iana = 42;
long mask = 0xFF;
long time = System.currentTimeMillis();
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;
engineid[5] = (byte) ( (time & (mask << 56)) >>> 56 );
engineid[6] = (byte) ( (time & (mask << 48) ) >>> 48 );
engineid[7] = (byte) ( (time & (mask << 40) ) >>> 40 );
engineid[8] = (byte) ( (time & (mask << 32) ) >>> 32 );
engineid[9] = (byte) ( (time & (mask << 24) ) >>> 24 );
engineid[10] = (byte) ( (time & (mask << 16) ) >>> 16 );
engineid[11] = (byte) ( (time & (mask << 8) ) >>> 8 );
engineid[12] = (byte) (time & mask);
return new SnmpEngineId(engineid);
|
public static com.sun.jmx.snmp.SnmpEngineId | createEngineId(java.lang.String str)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 :
- <address>:<port>:<IANA number>
All these parameters are used to generate the Id. WARNING, this method is not compliant with IPv6 address format. Use { @link com.sun.jmx.snmp.SnmpEngineId#createEngineId(java.lang.String,java.lang.String) } instead.
- <address>:<port>
The IANA number will be the SUN Microsystems one (42).
- address
The port 161 will be used to generate the Id. IANA number will be the SUN Microsystems one (42).
- :port
The host to use is localhost. IANA number will be the SUN Microsystems one (42).
- ::<IANA number>
The port 161 and localhost will be used to generate the Id.
- :<port>:<IANA number>
The host to use is localhost.
- <address>::<IANA number>
The port 161 will be used to generate the Id.
- ::
The port 161, localhost and the SUN Microsystems IANA number will be used to generate the Id.
return createEngineId(str, null);
|
public static com.sun.jmx.snmp.SnmpEngineId | createEngineId(java.lang.String str, java.lang.String separator)Idem { @link
com.sun.jmx.snmp.SnmpEngineId#createEngineId(java.lang.String) }
with the ability to provide your own separator. This allows IPv6
address format handling (eg: providing @ as separator).
if(str == null) return null;
if(str.startsWith("0x") || str.startsWith("0X")) {
validateId(str);
return new SnmpEngineId(str);
}
separator = separator == null ? ":" : separator;
StringTokenizer token = new StringTokenizer(str,
separator,
true);
String address = null;
String port = null;
String iana = null;
int objPort = 161;
int objIana = 42;
InetAddress objAddress = null;
SnmpEngineId eng = null;
try {
//Deal with address
try {
address = token.nextToken();
}catch(NoSuchElementException e) {
throw new IllegalArgumentException("Passed string is invalid : ["+str+"]");
}
if(!address.equals(separator)) {
objAddress = InetAddress.getByName(address);
try {
token.nextToken();
}catch(NoSuchElementException e) {
//No need to go further, no port.
eng = SnmpEngineId.createEngineId(objAddress,
objPort,
objIana);
eng.setStringValue(str);
return eng;
}
}
else
objAddress = InetAddress.getLocalHost();
//Deal with port
try {
port = token.nextToken();
}catch(NoSuchElementException e) {
//No need to go further, no port.
eng = SnmpEngineId.createEngineId(objAddress,
objPort,
objIana);
eng.setStringValue(str);
return eng;
}
if(!port.equals(separator)) {
objPort = Integer.parseInt(port);
try {
token.nextToken();
}catch(NoSuchElementException e) {
//No need to go further, no iana.
eng = SnmpEngineId.createEngineId(objAddress,
objPort,
objIana);
eng.setStringValue(str);
return eng;
}
}
//Deal with iana
try {
iana = token.nextToken();
}catch(NoSuchElementException e) {
//No need to go further, no port.
eng = SnmpEngineId.createEngineId(objAddress,
objPort,
objIana);
eng.setStringValue(str);
return eng;
}
if(!iana.equals(separator))
objIana = Integer.parseInt(iana);
eng = SnmpEngineId.createEngineId(objAddress,
objPort,
objIana);
eng.setStringValue(str);
return eng;
} catch(Exception e) {
throw new IllegalArgumentException("Passed string is invalid : ["+str+"]. Check that the used separator ["+ separator + "] is compatible with IPv6 address format.");
}
|
public static com.sun.jmx.snmp.SnmpEngineId | createEngineId(int port)Generates a unique engine Id. The engine Id unicity is based on
the host IP address and port. The IP address used is the
localhost one. The creation algorithm uses the SUN Microsystems IANA
number (42).
int suniana = 42;
InetAddress address = null;
address = InetAddress.getLocalHost();
return createEngineId(address, port, suniana);
|
public static com.sun.jmx.snmp.SnmpEngineId | createEngineId(java.net.InetAddress address, int port)Generates a unique engine Id. The engine Id unicity is based on
the host IP address and port. The IP address used is the passed
one. The creation algorithm uses the SUN Microsystems IANA
number (42).
int suniana = 42;
if(address == null)
throw new IllegalArgumentException("InetAddress is null.");
return createEngineId(address, port, suniana);
|
public static com.sun.jmx.snmp.SnmpEngineId | createEngineId(int port, int iana)Generates a unique engine Id. The engine Id unicity is based on
the host IP address and port. The IP address is the localhost one.
The creation algorithm uses the passed IANA number.
InetAddress address = null;
address = InetAddress.getLocalHost();
return createEngineId(address, port, iana);
|
public static com.sun.jmx.snmp.SnmpEngineId | createEngineId(java.net.InetAddress addr, int port, int iana)Generates a unique engine Id. The engine Id unicity is based on the host IP address and port. The IP address is the passed one, it handles IPv4 and IPv6 hosts. The creation algorithm uses the passed IANA number.
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);
|
public static com.sun.jmx.snmp.SnmpEngineId | createEngineId(int iana, java.net.InetAddress addr)Generates an engine Id based on an InetAddress. Handles IPv4 and IPv6 addresses. The creation algorithm uses the passed IANA number.
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);
|
public static com.sun.jmx.snmp.SnmpEngineId | createEngineId(java.net.InetAddress addr)Generates an engine Id based on an InetAddress. Handles IPv4 and IPv6
addresses. The creation algorithm uses the sun IANA number (42).
return createEngineId(42, addr);
|
public static com.sun.jmx.snmp.SnmpEngineId | createEngineId(byte[] arr)Generates an engine Id based on the passed array.
if( (arr == null) || arr.length == 0) return null;
validateId(arr);
return new SnmpEngineId(arr);
|
public boolean | equals(java.lang.Object a)Tests SnmpEngineId instance equality. Two SnmpEngineId are equal if they have the same value.
if(!(a instanceof SnmpEngineId) ) return false;
return hexString.equals(((SnmpEngineId) a).toString());
|
public byte[] | getBytes()Returns a binary engine Id.
return engineId;
|
public java.lang.String | getReadableId()If a string of the format <address>:<port>:<IANA number> has been provided at creation time, this string is returned.
return humanString;
|
public int | hashCode()
return hexString.hashCode();
|
void | setStringValue(java.lang.String val)In order to store the string used to create the engineId.
humanString = val;
|
public SnmpOid | toOid()Translates an engine Id in an SnmpOid format. This is useful when dealing with USM MIB indexes.
The oid format is : ......
Eg: "0x8000002a05819dcb6e00001f96" ==> 13.128.0.0.42.5.129.157.203.110.0.0.31.150
long[] oid = new long[engineId.length + 1];
oid[0] = engineId.length;
for(int i = 1; i <= engineId.length; i++)
oid[i] = (long) (engineId[i-1] & 0xFF);
return new SnmpOid(oid);
|
public java.lang.String | toString()Returns a string format engine Id.
return hexString;
|
static void | validateId(java.lang.String str)
byte[] arr = SnmpTools.ascii2binary(str);
validateId(arr);
|
static void | validateId(byte[] arr)
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.");
|