1 20 21 package org.snmp4j.smi; 22 23 import java.io.*; 24 import java.net.*; 25 import org.snmp4j.asn1.BER; 26 import org.snmp4j.asn1.BERInputStream; 27 import org.snmp4j.log.LogFactory; 28 import org.snmp4j.log.LogAdapter; 29 30 37 public class IpAddress extends SMIAddress { 38 39 private static final long serialVersionUID = -146846354059565449L; 40 41 private static final LogAdapter logger = 42 LogFactory.getLogger(AbstractVariable.class); 43 44 private static final byte[] IPANYADDRESS = { 0,0,0,0 }; 45 46 public static final InetAddress ANY_IPADDRESS = createAnyAddress(); 47 48 private java.net.InetAddress inetAddress; 49 50 53 public IpAddress() { 54 this.inetAddress = ANY_IPADDRESS; 55 } 56 57 63 public IpAddress(InetAddress address) { 64 if (address == null) { 65 throw new NullPointerException (); 66 } 67 this.inetAddress = address; 68 } 69 70 76 public IpAddress(String address) { 77 if (!parseAddress(address)) { 78 throw new IllegalArgumentException (address); 79 } 80 } 81 82 public int getSyntax() { 83 return SMIConstants.SYNTAX_IPADDRESS; 84 } 85 86 public boolean isValid() { 87 return (inetAddress != null); 88 } 89 90 public String toString() { 91 String addressString = inetAddress.toString(); 92 return addressString.substring(addressString.indexOf('/')+1); 93 } 94 95 public int hashCode() { 96 if (inetAddress != null) { 97 return inetAddress.hashCode(); 98 } 99 return 0; 100 } 101 102 112 public static Address parse(String address) { 113 try { 114 InetAddress addr = InetAddress.getByName(address); 115 return new IpAddress(addr); 116 } 117 catch (Exception ex) { 118 logger.error("Unable to parse IpAddress from: "+address, ex); 119 return null; 120 } 121 } 122 123 public boolean parseAddress(String address) { 124 try { 125 inetAddress = InetAddress.getByName(address); 126 return true; 127 } 128 catch (UnknownHostException uhex) { 129 return false; 130 } 131 } 132 133 public int compareTo(Object o) { 134 OctetString a = new OctetString(inetAddress.getAddress()); 135 return a.compareTo(new OctetString(((IpAddress)o).getInetAddress().getAddress())); 136 } 137 138 public boolean equals(Object o) { 139 if (!(o instanceof IpAddress)) { 140 return false; 141 } 142 return (compareTo(o) == 0); 143 } 144 145 public void decodeBER(BERInputStream inputStream) throws java.io.IOException { 146 BER.MutableByte type = new BER.MutableByte(); 147 byte[] value = BER.decodeString(inputStream, type); 148 if (type.getValue() != BER.IPADDRESS) { 149 throw new IOException("Wrong type encountered when decoding Counter: "+ 150 type.getValue()); 151 } 152 if (value.length != 4) { 153 throw new IOException("IpAddress encoding error, wrong length: " + 154 value.length); 155 } 156 inetAddress = InetAddress.getByAddress(value); 157 } 158 159 public void encodeBER(OutputStream outputStream) throws java.io.IOException { 160 byte[] address = new byte[4]; 161 System.arraycopy(inetAddress.getAddress(), 0, address, 0, 4); 162 BER.encodeString(outputStream, BER.IPADDRESS, inetAddress.getAddress()); 163 } 164 165 public int getBERLength() { 166 return 6; 167 } 168 169 public void setAddress(byte[] rawValue) throws UnknownHostException { 170 this.inetAddress = InetAddress.getByAddress(rawValue); 171 } 172 173 public void setInetAddress(java.net.InetAddress inetAddress) { 174 this.inetAddress = inetAddress; 175 } 176 177 public InetAddress getInetAddress() { 178 return inetAddress; 179 } 180 181 private static InetAddress createAnyAddress() { 182 try { 183 return InetAddress.getByAddress(IPANYADDRESS); 184 } 185 catch (Exception ex) { 186 logger.error("Unable to create any IpAddress: "+ex.getMessage(), ex); 187 } 188 return null; 189 } 190 191 public Object clone() { 192 return new IpAddress(inetAddress); 193 } 194 195 public int toInt() { 196 throw new UnsupportedOperationException (); 197 } 198 199 public long toLong() { 200 throw new UnsupportedOperationException (); 201 } 202 203 public OID toSubIndex(boolean impliedLength) { 204 byte[] address = new byte[4]; 205 System.arraycopy(inetAddress.getAddress(), 0, address, 0, 4); 206 OID subIndex = new OID(new int[4]); 207 for (int i=0; i<address.length; i++) { 208 subIndex.set(i, address[i] & 0xFF); 209 } 210 return subIndex; 211 } 212 213 public void fromSubIndex(OID subIndex, boolean impliedLength) { 214 byte[] rawValue = new byte[4]; 215 for (int i=0; i<rawValue.length; i++) { 216 rawValue[i] = (byte)(subIndex.get(i) & 0xFF); 217 } 218 try { 219 setAddress(rawValue); 220 } 221 catch (UnknownHostException ex) { 222 ex.printStackTrace(); 223 } 224 } 225 226 public void setValue(String value) { 227 if (!parseAddress(value)) { 228 throw new IllegalArgumentException (value+" cannot be parsed by "+ 229 getClass().getName()); 230 } 231 } 232 233 } 234 235 | Popular Tags |