1. /*
  2. * @(#)file SnmpIpAddress.java
  3. * @(#)author Sun Microsystems, Inc.
  4. * @(#)version 4.10
  5. * @(#)date 04/09/15
  6. *
  7. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  8. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  9. *
  10. */
  11. package com.sun.jmx.snmp;
  12. /**
  13. * Represents an SNMP IpAddress.
  14. *
  15. * <p><b>This API is a Sun Microsystems internal API and is subject
  16. * to change without notice.</b></p>
  17. * @version 4.10 12/19/03
  18. * @author Sun Microsystems, Inc
  19. */
  20. public class SnmpIpAddress extends SnmpOid {
  21. // CONSTRUCTORS
  22. //-------------
  23. /**
  24. * Constructs a new <CODE>SnmpIpAddress</CODE> from the specified bytes array.
  25. * @param bytes The four bytes composing the address.
  26. * @exception IllegalArgumentException The length of the array is not equal to four.
  27. */
  28. public SnmpIpAddress(byte[] bytes) throws IllegalArgumentException {
  29. buildFromByteArray(bytes);
  30. }
  31. /**
  32. * Constructs a new <CODE>SnmpIpAddress</CODE> from the specified long value.
  33. * @param addr The initialization value.
  34. */
  35. public SnmpIpAddress(long addr) {
  36. int address = (int)addr ;
  37. byte[] ipaddr = new byte[4];
  38. ipaddr[0] = (byte) ((address >>> 24) & 0xFF);
  39. ipaddr[1] = (byte) ((address >>> 16) & 0xFF);
  40. ipaddr[2] = (byte) ((address >>> 8) & 0xFF);
  41. ipaddr[3] = (byte) (address & 0xFF);
  42. buildFromByteArray(ipaddr);
  43. }
  44. /**
  45. * Constructs a new <CODE>SnmpIpAddress</CODE> from a dot-formatted <CODE>String</CODE>.
  46. * The dot-formatted <CODE>String</CODE> is formulated x.x.x.x .
  47. * @param dotAddress The initialization value.
  48. * @exception IllegalArgumentException The string does not correspond to an ip address.
  49. */
  50. public SnmpIpAddress(String dotAddress) throws IllegalArgumentException {
  51. super(dotAddress) ;
  52. if ((componentCount > 4) ||
  53. (components[0] > 255) ||
  54. (components[1] > 255) ||
  55. (components[2] > 255) ||
  56. (components[3] > 255)) {
  57. throw new IllegalArgumentException(dotAddress) ;
  58. }
  59. }
  60. /**
  61. * Constructs a new <CODE>SnmpIpAddress</CODE> from four long values.
  62. * @param b1 Byte 1.
  63. * @param b2 Byte 2.
  64. * @param b3 Byte 3.
  65. * @param b4 Byte 4.
  66. * @exception IllegalArgumentException A value is outside of [0-255].
  67. */
  68. public SnmpIpAddress(long b1, long b2, long b3, long b4) {
  69. super(b1, b2, b3, b4) ;
  70. if ((components[0] > 255) ||
  71. (components[1] > 255) ||
  72. (components[2] > 255) ||
  73. (components[3] > 255)) {
  74. throw new IllegalArgumentException() ;
  75. }
  76. }
  77. // PUBLIC METHODS
  78. //---------------
  79. /**
  80. * Converts the address value to its byte array form.
  81. * @return The byte array representation of the value.
  82. */
  83. public byte[] byteValue() {
  84. byte[] result = new byte[4] ;
  85. result[0] = (byte)components[0] ;
  86. result[1] = (byte)components[1] ;
  87. result[2] = (byte)components[2] ;
  88. result[3] = (byte)components[3] ;
  89. return result ;
  90. }
  91. /**
  92. * Converts the address to its <CODE>String</CODE> form.
  93. * Same as <CODE>toString()</CODE>. Exists only to follow a naming scheme.
  94. * @return The <CODE>String</CODE> representation of the value.
  95. */
  96. public String stringValue() {
  97. return toString() ;
  98. }
  99. /**
  100. * Extracts the ip address from an index OID and returns its
  101. * value converted as an <CODE>SnmpOid</CODE>.
  102. * @param index The index array.
  103. * @param start The position in the index array.
  104. * @return The OID representing the ip address value.
  105. * @exception SnmpStatusException There is no ip address value
  106. * available at the start position.
  107. */
  108. public static SnmpOid toOid(long[] index, int start) throws SnmpStatusException {
  109. if (start + 4 <= index.length) {
  110. try {
  111. return new SnmpOid(
  112. index[start],
  113. index[start+1],
  114. index[start+2],
  115. index[start+3]) ;
  116. }
  117. catch(IllegalArgumentException e) {
  118. throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
  119. }
  120. }
  121. else {
  122. throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
  123. }
  124. }
  125. /**
  126. * Scans an index OID, skips the address value and returns the position
  127. * of the next value.
  128. * @param index The index array.
  129. * @param start The position in the index array.
  130. * @return The position of the next value.
  131. * @exception SnmpStatusException There is no address value
  132. * available at the start position.
  133. */
  134. public static int nextOid(long[] index, int start) throws SnmpStatusException {
  135. if (start + 4 <= index.length) {
  136. return start + 4 ;
  137. }
  138. else {
  139. throw new SnmpStatusException(SnmpStatusException.noSuchName) ;
  140. }
  141. }
  142. /**
  143. * Appends an <CODE>SnmpOid</CODE> representing an <CODE>SnmpIpAddress</CODE> to another OID.
  144. * @param source An OID representing an <CODE>SnmpIpAddress</CODE> value.
  145. * @param dest Where source should be appended.
  146. */
  147. public static void appendToOid(SnmpOid source, SnmpOid dest) {
  148. if (source.getLength() != 4) {
  149. throw new IllegalArgumentException() ;
  150. }
  151. dest.append(source) ;
  152. }
  153. /**
  154. * Returns a textual description of the type object.
  155. * @return ASN.1 textual description.
  156. */
  157. final public String getTypeName() {
  158. return name ;
  159. }
  160. // PRIVATE METHODS
  161. //----------------
  162. /**
  163. * Build Ip address from byte array.
  164. */
  165. private void buildFromByteArray(byte[] bytes) {
  166. if (bytes.length != 4) {
  167. throw new IllegalArgumentException() ;
  168. }
  169. components = new long[4] ;
  170. componentCount= 4;
  171. components[0] = (bytes[0] >= 0) ? bytes[0] : bytes[0] + 256 ;
  172. components[1] = (bytes[1] >= 0) ? bytes[1] : bytes[1] + 256 ;
  173. components[2] = (bytes[2] >= 0) ? bytes[2] : bytes[2] + 256 ;
  174. components[3] = (bytes[3] >= 0) ? bytes[3] : bytes[3] + 256 ;
  175. }
  176. // VARIABLES
  177. //----------
  178. /**
  179. * Name of the type.
  180. */
  181. final static String name = "IpAddress" ;
  182. }