Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/jmx/snmp/SnmpEngineId.java
38924 views
/*1* Copyright (c) 2001, 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package com.sun.jmx.snmp;2526import java.net.InetAddress;27import java.io.Serializable;28import java.net.UnknownHostException;29import java.util.StringTokenizer;30import java.util.Arrays;31import java.util.NoSuchElementException;3233import com.sun.jmx.snmp.internal.SnmpTools;3435/**36* This class is handling an <CODE>SnmpEngineId</CODE> data. It copes with binary as well as <CODE>String</CODE> representation of an engine Id. A string format engine is an hex string starting with 0x.37* <p><b>This API is a Sun Microsystems internal API and is subject38* to change without notice.</b></p>39* @since 1.540*/41public class SnmpEngineId implements Serializable {42private static final long serialVersionUID = 5434729655830763317L;4344byte[] engineId = null;45String hexString = null;46String humanString = null;47/**48* New <CODE>SnmpEngineId</CODE> with an hex string value. Can handle engine Id format <host>:<port>.49* @param hexString Hexa string.50*/51SnmpEngineId(String hexString) {52engineId = SnmpTools.ascii2binary(hexString);53this.hexString = hexString.toLowerCase();54}55/**56* New <CODE>SnmpEngineId</CODE> with a binary value. You can use <CODE> SnmpTools </CODE> to convert from hex string to binary format.57* @param bin Binary value58*/59SnmpEngineId(byte[] bin) {60engineId = bin;61hexString = SnmpTools.binary2ascii(bin).toLowerCase();62}6364/**65* If a string of the format <address>:<port>:<IANA number> has been provided at creation time, this string is returned.66* @return The Id as a readable string or null if not provided.67*/68public String getReadableId() {69return humanString;70}7172/**73* Returns a string format engine Id.74* @return String format value.75*/76public String toString() {77return hexString;78}79/**80* Returns a binary engine Id.81* @return Binary value.82*/83public byte[] getBytes() {84return engineId;85}8687/**88* In order to store the string used to create the engineId.89*/90void setStringValue(String val) {91humanString = val;92}9394static void validateId(String str) throws IllegalArgumentException {95byte[] arr = SnmpTools.ascii2binary(str);96validateId(arr);97}9899static void validateId(byte[] arr) throws IllegalArgumentException {100101if(arr.length < 5) throw new IllegalArgumentException("Id size lower than 5 bytes.");102if(arr.length > 32) throw new IllegalArgumentException("Id size greater than 32 bytes.");103104//octet strings with very first bit = 0 and length != 12 octets105if( ((arr[0] & 0x80) == 0) && arr.length != 12)106throw new IllegalArgumentException("Very first bit = 0 and length != 12 octets");107108byte[] zeroedArrays = new byte[arr.length];109if(Arrays.equals(zeroedArrays, arr)) throw new IllegalArgumentException("Zeroed Id.");110byte[] FFArrays = new byte[arr.length];111Arrays.fill(FFArrays, (byte)0xFF);112if(Arrays.equals(FFArrays, arr)) throw new IllegalArgumentException("0xFF Id.");113114}115116/**117* Generates an engine Id based on the passed array.118* @return The created engine Id or null if given arr is null or its length == 0;119* @exception IllegalArgumentException when:120* <ul>121* <li>octet string lower than 5 bytes.</li>122* <li>octet string greater than 32 bytes.</li>123* <li>octet string = all zeros.</li>124* <li>octet string = all 'ff'H.</li>125* <li>octet strings with very first bit = 0 and length != 12 octets</li>126* </ul>127*/128public static SnmpEngineId createEngineId(byte[] arr) throws IllegalArgumentException {129if( (arr == null) || arr.length == 0) return null;130validateId(arr);131return new SnmpEngineId(arr);132}133134/**135* 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).136* @return The generated engine Id.137*/138public static SnmpEngineId createEngineId() {139byte[] address = null;140byte[] engineid = new byte[13];141int iana = 42;142long mask = 0xFF;143long time = System.currentTimeMillis();144145engineid[0] = (byte) ( (iana & 0xFF000000) >> 24 );146engineid[0] |= 0x80;147engineid[1] = (byte) ( (iana & 0x00FF0000) >> 16 );148engineid[2] = (byte) ( (iana & 0x0000FF00) >> 8 );149engineid[3] = (byte) (iana & 0x000000FF);150engineid[4] = 0x05;151152engineid[5] = (byte) ( (time & (mask << 56)) >>> 56 );153engineid[6] = (byte) ( (time & (mask << 48) ) >>> 48 );154engineid[7] = (byte) ( (time & (mask << 40) ) >>> 40 );155engineid[8] = (byte) ( (time & (mask << 32) ) >>> 32 );156engineid[9] = (byte) ( (time & (mask << 24) ) >>> 24 );157engineid[10] = (byte) ( (time & (mask << 16) ) >>> 16 );158engineid[11] = (byte) ( (time & (mask << 8) ) >>> 8 );159engineid[12] = (byte) (time & mask);160161return new SnmpEngineId(engineid);162}163164/**165* Translates an engine Id in an SnmpOid format. This is useful when dealing with USM MIB indexes.166* The oid format is : <engine Id length>.<engine Id binary octet1>....<engine Id binary octetn - 1>.<engine Id binary octetn>167* Eg: "0x8000002a05819dcb6e00001f96" ==> 13.128.0.0.42.5.129.157.203.110.0.0.31.150168*169* @return SnmpOid The oid.170*/171public SnmpOid toOid() {172long[] oid = new long[engineId.length + 1];173oid[0] = engineId.length;174for(int i = 1; i <= engineId.length; i++)175oid[i] = (long) (engineId[i-1] & 0xFF);176return new SnmpOid(oid);177}178179/**180* <P>Generates a unique engine Id. Hexadecimal strings as well as a textual description are supported. The textual format is as follow:181* <BR> <address>:<port>:<IANA number></P>182* <P>The allowed formats :</P>183* <ul>184* <li> <address>:<port>:<IANA number>185* <BR> 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.</li>186* <li> <address>:<port>187* <BR> The IANA number will be the SUN Microsystems one (42). </li>188* <li> address189* <BR> The port 161 will be used to generate the Id. IANA number will be the SUN Microsystems one (42). </li>190* <li> :port191* <BR> The host to use is localhost. IANA number will be the SUN Microsystems one (42). </li>192* <li> ::<IANA number> 193* <BR> The port 161 and localhost will be used to generate the Id. </li>194* <li> :<port>:<IANA number>195* <BR> The host to use is localhost. </li>196* <li> <address>::<IANA number>197* <BR> The port 161 will be used to generate the Id. </li>198* <li> :: 199* <BR> The port 161, localhost and the SUN Microsystems IANA number will be used to generate the Id. </li>200* </ul>201* @exception UnknownHostException if the host name contained in the textual format is unknown.202* @exception IllegalArgumentException when :203* <ul>204* <li>octet string lower than 5 bytes.</li>205* <li>octet string greater than 32 bytes.</li>206* <li>octet string = all zeros.</li>207* <li>octet string = all 'ff'H.</li>208* <li>octet strings with very first bit = 0 and length != 12 octets</li>209* <li>An IPv6 address format is used in conjonction with the ":" separator</li>210* </ul>211* @param str The string to parse.212* @return The generated engine Id or null if the passed string is null.213*214*/215public static SnmpEngineId createEngineId(String str)216throws IllegalArgumentException, UnknownHostException {217return createEngineId(str, null);218}219220/**221* Idem { @link222* com.sun.jmx.snmp.SnmpEngineId#createEngineId(java.lang.String) }223* with the ability to provide your own separator. This allows IPv6224* address format handling (eg: providing @ as separator).225* @param str The string to parse.226* @param separator the separator to use. If null is provided, the default227* separator ":" is used.228* @return The generated engine Id or null if the passed string is null.229* @exception UnknownHostException if the host name contained in the230* textual format is unknown.231* @exception IllegalArgumentException when :232* <ul>233* <li>octet string lower than 5 bytes.</li>234* <li>octet string greater than 32 bytes.</li>235* <li>octet string = all zeros.</li>236* <li>octet string = all 'ff'H.</li>237* <li>octet strings with very first bit = 0 and length != 12 octets</li>238* <li>An IPv6 address format is used in conjonction with the ":"239* separator</li>240* </ul>241* @since 1.5242*/243public static SnmpEngineId createEngineId(String str, String separator)244throws IllegalArgumentException, UnknownHostException {245if(str == null) return null;246247if(str.startsWith("0x") || str.startsWith("0X")) {248validateId(str);249return new SnmpEngineId(str);250}251separator = separator == null ? ":" : separator;252StringTokenizer token = new StringTokenizer(str,253separator,254true);255256String address = null;257String port = null;258String iana = null;259int objPort = 161;260int objIana = 42;261InetAddress objAddress = null;262SnmpEngineId eng = null;263try {264//Deal with address265try {266address = token.nextToken();267}catch(NoSuchElementException e) {268throw new IllegalArgumentException("Passed string is invalid : ["+str+"]");269}270if(!address.equals(separator)) {271objAddress = InetAddress.getByName(address);272try {273token.nextToken();274}catch(NoSuchElementException e) {275//No need to go further, no port.276eng = SnmpEngineId.createEngineId(objAddress,277objPort,278objIana);279eng.setStringValue(str);280return eng;281}282}283else284objAddress = InetAddress.getLocalHost();285286//Deal with port287try {288port = token.nextToken();289}catch(NoSuchElementException e) {290//No need to go further, no port.291eng = SnmpEngineId.createEngineId(objAddress,292objPort,293objIana);294eng.setStringValue(str);295return eng;296}297298if(!port.equals(separator)) {299objPort = Integer.parseInt(port);300try {301token.nextToken();302}catch(NoSuchElementException e) {303//No need to go further, no iana.304eng = SnmpEngineId.createEngineId(objAddress,305objPort,306objIana);307eng.setStringValue(str);308return eng;309}310}311312//Deal with iana313try {314iana = token.nextToken();315}catch(NoSuchElementException e) {316//No need to go further, no port.317eng = SnmpEngineId.createEngineId(objAddress,318objPort,319objIana);320eng.setStringValue(str);321return eng;322}323324if(!iana.equals(separator))325objIana = Integer.parseInt(iana);326327eng = SnmpEngineId.createEngineId(objAddress,328objPort,329objIana);330eng.setStringValue(str);331332return eng;333334} catch(Exception e) {335throw new IllegalArgumentException("Passed string is invalid : ["+str+"]. Check that the used separator ["+ separator + "] is compatible with IPv6 address format.");336}337338}339340/**341* Generates a unique engine Id. The engine Id unicity is based on342* the host IP address and port. The IP address used is the343* localhost one. The creation algorithm uses the SUN Microsystems IANA344* number (42).345* @param port The TCP/IP port the SNMPv3 Adaptor Server is listening to.346* @return The generated engine Id.347* @exception UnknownHostException if the local host name348* used to calculate the id is unknown.349*/350public static SnmpEngineId createEngineId(int port)351throws UnknownHostException {352int suniana = 42;353InetAddress address = null;354address = InetAddress.getLocalHost();355return createEngineId(address, port, suniana);356}357/**358* Generates a unique engine Id. The engine Id unicity is based on359* the host IP address and port. The IP address used is the passed360* one. The creation algorithm uses the SUN Microsystems IANA361* number (42).362* @param address The IP address the SNMPv3 Adaptor Server is listening to.363* @param port The TCP/IP port the SNMPv3 Adaptor Server is listening to.364* @return The generated engine Id.365* @exception UnknownHostException. if the provided address is null.366*/367public static SnmpEngineId createEngineId(InetAddress address, int port)368throws IllegalArgumentException {369int suniana = 42;370if(address == null)371throw new IllegalArgumentException("InetAddress is null.");372return createEngineId(address, port, suniana);373}374375/**376* Generates a unique engine Id. The engine Id unicity is based on377* the host IP address and port. The IP address is the localhost one.378* The creation algorithm uses the passed IANA number.379* @param port The TCP/IP port the SNMPv3 Adaptor Server is listening to.380* @param iana Your enterprise IANA number.381* @exception UnknownHostException if the local host name used to calculate the id is unknown.382* @return The generated engine Id.383*/384public static SnmpEngineId createEngineId(int port, int iana) throws UnknownHostException {385InetAddress address = null;386address = InetAddress.getLocalHost();387return createEngineId(address, port, iana);388}389390/**391* 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.392* @param addr The IP address the SNMPv3 Adaptor Server is listening to.393* @param port The TCP/IP port the SNMPv3 Adaptor Server is listening to.394* @param iana Your enterprise IANA number.395* @return The generated engine Id.396* @exception UnknownHostException if the provided <CODE>InetAddress </CODE> is null.397*/398public static SnmpEngineId createEngineId(InetAddress addr,399int port,400int iana) {401if(addr == null) throw new IllegalArgumentException("InetAddress is null.");402byte[] address = addr.getAddress();403byte[] engineid = new byte[9 + address.length];404engineid[0] = (byte) ( (iana & 0xFF000000) >> 24 );405engineid[0] |= 0x80;406engineid[1] = (byte) ( (iana & 0x00FF0000) >> 16 );407engineid[2] = (byte) ( (iana & 0x0000FF00) >> 8 );408409engineid[3] = (byte) (iana & 0x000000FF);410engineid[4] = 0x05;411412if(address.length == 4)413engineid[4] = 0x01;414415if(address.length == 16)416engineid[4] = 0x02;417418for(int i = 0; i < address.length; i++) {419engineid[i + 5] = address[i];420}421422engineid[5 + address.length] = (byte) ( (port & 0xFF000000) >> 24 );423engineid[6 + address.length] = (byte) ( (port & 0x00FF0000) >> 16 );424engineid[7 + address.length] = (byte) ( (port & 0x0000FF00) >> 8 );425engineid[8 + address.length] = (byte) ( port & 0x000000FF );426427return new SnmpEngineId(engineid);428}429430/**431* Generates an engine Id based on an InetAddress. Handles IPv4 and IPv6 addresses. The creation algorithm uses the passed IANA number.432* @param iana Your enterprise IANA number.433* @param addr The IP address the SNMPv3 Adaptor Server is listening to.434* @return The generated engine Id.435* @since 1.5436* @exception UnknownHostException if the provided <CODE>InetAddress </CODE> is null.437*/438public static SnmpEngineId createEngineId(int iana, InetAddress addr)439{440if(addr == null) throw new IllegalArgumentException("InetAddress is null.");441byte[] address = addr.getAddress();442byte[] engineid = new byte[5 + address.length];443engineid[0] = (byte) ( (iana & 0xFF000000) >> 24 );444engineid[0] |= 0x80;445engineid[1] = (byte) ( (iana & 0x00FF0000) >> 16 );446engineid[2] = (byte) ( (iana & 0x0000FF00) >> 8 );447448engineid[3] = (byte) (iana & 0x000000FF);449if(address.length == 4)450engineid[4] = 0x01;451452if(address.length == 16)453engineid[4] = 0x02;454455for(int i = 0; i < address.length; i++) {456engineid[i + 5] = address[i];457}458459return new SnmpEngineId(engineid);460}461462/**463* Generates an engine Id based on an InetAddress. Handles IPv4 and IPv6464* addresses. The creation algorithm uses the sun IANA number (42).465* @param addr The IP address the SNMPv3 Adaptor Server is listening to.466* @return The generated engine Id.467* @since 1.5468* @exception UnknownHostException if the provided469* <CODE>InetAddress</CODE> is null.470*/471public static SnmpEngineId createEngineId(InetAddress addr) {472return createEngineId(42, addr);473}474475476/**477* Tests <CODE>SnmpEngineId</CODE> instance equality. Two <CODE>SnmpEngineId</CODE> are equal if they have the same value.478* @return <CODE>true</CODE> if the two <CODE>SnmpEngineId</CODE> are equals, <CODE>false</CODE> otherwise.479*/480public boolean equals(Object a) {481if(!(a instanceof SnmpEngineId) ) return false;482return hexString.equals(((SnmpEngineId) a).toString());483}484485public int hashCode() {486return hexString.hashCode();487}488}489490491