📄 snmpengineid.java
字号:
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."); } } /** * 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). * @param port The TCP/IP port the SNMPv3 Adaptor Server is listening to. * @return The generated engine Id. * @exception UnknownHostException if the local host name * used to calculate the id is unknown. */ public static SnmpEngineId createEngineId(int port) throws UnknownHostException { int suniana = 42; InetAddress address = null; address = InetAddress.getLocalHost(); return createEngineId(address, port, suniana); } /** * 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). * @param address The IP address the SNMPv3 Adaptor Server is listening to. * @param port The TCP/IP port the SNMPv3 Adaptor Server is listening to. * @return The generated engine Id. * @exception UnknownHostException. if the provided address is null. */ public static SnmpEngineId createEngineId(InetAddress address, int port) throws IllegalArgumentException { int suniana = 42; if(address == null) throw new IllegalArgumentException("InetAddress is null."); return createEngineId(address, port, suniana); } /** * 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. * @param port The TCP/IP port the SNMPv3 Adaptor Server is listening to. * @param iana Your enterprise IANA number. * @exception UnknownHostException if the local host name used to calculate the id is unknown. * @return The generated engine Id. */ public static SnmpEngineId createEngineId(int port, int iana) throws UnknownHostException { InetAddress address = null; address = InetAddress.getLocalHost(); return createEngineId(address, port, 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. * @param addr The IP address the SNMPv3 Adaptor Server is listening to. * @param port The TCP/IP port the SNMPv3 Adaptor Server is listening to. * @param iana Your enterprise IANA number. * @return The generated engine Id. * @exception UnknownHostException if the provided <CODE>InetAddress </CODE> 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 <CODE>InetAddress </CODE> 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 * <CODE>InetAddress</CODE> is null. */ public static SnmpEngineId createEngineId(InetAddress addr) { return createEngineId(42, addr); } /** * Tests <CODE>SnmpEngineId</CODE> instance equality. Two <CODE>SnmpEngineId</CODE> are equal if they have the same value. * @return <CODE>true</CODE> if the two <CODE>SnmpEngineId</CODE> are equals, <CODE>false</CODE> otherwise. */ public boolean equals(Object a) { if(!(a instanceof SnmpEngineId) ) return false; return hexString.equals(((SnmpEngineId) a).toString()); } public int hashCode() { return hexString.hashCode(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -