001: /*_############################################################################
002: _##
003: _## SNMP4J - IpAddress.java
004: _##
005: _## Copyright (C) 2003-2008 Frank Fock and Jochen Katz (SNMP4J.org)
006: _##
007: _## Licensed under the Apache License, Version 2.0 (the "License");
008: _## you may not use this file except in compliance with the License.
009: _## You may obtain a copy of the License at
010: _##
011: _## http://www.apache.org/licenses/LICENSE-2.0
012: _##
013: _## Unless required by applicable law or agreed to in writing, software
014: _## distributed under the License is distributed on an "AS IS" BASIS,
015: _## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: _## See the License for the specific language governing permissions and
017: _## limitations under the License.
018: _##
019: _##########################################################################*/
020:
021: package org.snmp4j.smi;
022:
023: import java.io.*;
024: import java.net.*;
025: import org.snmp4j.asn1.BER;
026: import org.snmp4j.asn1.BERInputStream;
027: import org.snmp4j.log.LogFactory;
028: import org.snmp4j.log.LogAdapter;
029:
030: /**
031: * The <code>IpAddress</code> class represents an IPv4 address SNMP variable.
032: *
033: * @author Frank Fock
034: * @version 1.7
035: * @since 1.0
036: */
037: public class IpAddress extends SMIAddress {
038:
039: private static final long serialVersionUID = -146846354059565449L;
040:
041: private static final LogAdapter logger = LogFactory
042: .getLogger(AbstractVariable.class);
043:
044: private static final byte[] IPANYADDRESS = { 0, 0, 0, 0 };
045:
046: public static final InetAddress ANY_IPADDRESS = createAnyAddress();
047:
048: private java.net.InetAddress inetAddress;
049:
050: /**
051: * Creates a <code>0.0.0.0</code> IP address.
052: */
053: public IpAddress() {
054: this .inetAddress = ANY_IPADDRESS;
055: }
056:
057: /**
058: * Creates an IP address from an <code>InetAddress</code>
059: * @param address
060: * an <code>InetAddress</code> instance (must not necessarily be a IPv4
061: * address).
062: */
063: public IpAddress(InetAddress address) {
064: if (address == null) {
065: throw new NullPointerException();
066: }
067: this .inetAddress = address;
068: }
069:
070: /**
071: * Create an IP address from an address string.
072: * @param address
073: * an IP address String.
074: * @see Address#parseAddress(String address)
075: */
076: public IpAddress(String address) {
077: if (!parseAddress(address)) {
078: throw new IllegalArgumentException(address);
079: }
080: }
081:
082: public int getSyntax() {
083: return SMIConstants.SYNTAX_IPADDRESS;
084: }
085:
086: public boolean isValid() {
087: return (inetAddress != null);
088: }
089:
090: public String toString() {
091: String addressString = inetAddress.toString();
092: return addressString.substring(addressString.indexOf('/') + 1);
093: }
094:
095: public int hashCode() {
096: if (inetAddress != null) {
097: return inetAddress.hashCode();
098: }
099: return 0;
100: }
101:
102: /**
103: * Parses an IP address string and returns the corresponding
104: * <code>IpAddress</code> instance.
105: * @param address
106: * an IP address string which may be a host name or a numerical IP address.
107: * @return
108: * an <code>IpAddress</code> instance or <code>null</code> if
109: * <code>address</code> cannot not be parsed.
110: * @see Address#parseAddress(String address)
111: */
112: public static Address parse(String address) {
113: try {
114: InetAddress addr = InetAddress.getByName(address);
115: return new IpAddress(addr);
116: } catch (Exception ex) {
117: logger.error("Unable to parse IpAddress from: " + address,
118: ex);
119: return null;
120: }
121: }
122:
123: public boolean parseAddress(String address) {
124: try {
125: inetAddress = InetAddress.getByName(address);
126: return true;
127: } catch (UnknownHostException uhex) {
128: return false;
129: }
130: }
131:
132: public int compareTo(Object o) {
133: OctetString a = new OctetString(inetAddress.getAddress());
134: return a.compareTo(new OctetString(((IpAddress) o)
135: .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)
146: throws java.io.IOException {
147: BER.MutableByte type = new BER.MutableByte();
148: byte[] value = BER.decodeString(inputStream, type);
149: if (type.getValue() != BER.IPADDRESS) {
150: throw new IOException(
151: "Wrong type encountered when decoding Counter: "
152: + type.getValue());
153: }
154: if (value.length != 4) {
155: throw new IOException(
156: "IpAddress encoding error, wrong length: "
157: + value.length);
158: }
159: inetAddress = InetAddress.getByAddress(value);
160: }
161:
162: public void encodeBER(OutputStream outputStream)
163: throws java.io.IOException {
164: byte[] address = new byte[4];
165: System.arraycopy(inetAddress.getAddress(), 0, address, 0, 4);
166: BER.encodeString(outputStream, BER.IPADDRESS, inetAddress
167: .getAddress());
168: }
169:
170: public int getBERLength() {
171: return 6;
172: }
173:
174: public void setAddress(byte[] rawValue) throws UnknownHostException {
175: this .inetAddress = InetAddress.getByAddress(rawValue);
176: }
177:
178: public void setInetAddress(java.net.InetAddress inetAddress) {
179: this .inetAddress = inetAddress;
180: }
181:
182: public InetAddress getInetAddress() {
183: return inetAddress;
184: }
185:
186: private static InetAddress createAnyAddress() {
187: try {
188: return InetAddress.getByAddress(IPANYADDRESS);
189: } catch (Exception ex) {
190: logger.error("Unable to create any IpAddress: "
191: + ex.getMessage(), ex);
192: }
193: return null;
194: }
195:
196: public Object clone() {
197: return new IpAddress(inetAddress);
198: }
199:
200: public int toInt() {
201: throw new UnsupportedOperationException();
202: }
203:
204: public long toLong() {
205: throw new UnsupportedOperationException();
206: }
207:
208: public OID toSubIndex(boolean impliedLength) {
209: byte[] address = new byte[4];
210: System.arraycopy(inetAddress.getAddress(), 0, address, 0, 4);
211: OID subIndex = new OID(new int[4]);
212: for (int i = 0; i < address.length; i++) {
213: subIndex.set(i, address[i] & 0xFF);
214: }
215: return subIndex;
216: }
217:
218: public void fromSubIndex(OID subIndex, boolean impliedLength) {
219: byte[] rawValue = new byte[4];
220: for (int i = 0; i < rawValue.length; i++) {
221: rawValue[i] = (byte) (subIndex.get(i) & 0xFF);
222: }
223: try {
224: setAddress(rawValue);
225: } catch (UnknownHostException ex) {
226: ex.printStackTrace();
227: }
228: }
229:
230: public void setValue(String value) {
231: if (!parseAddress(value)) {
232: throw new IllegalArgumentException(value
233: + " cannot be parsed by " + getClass().getName());
234: }
235: }
236:
237: }
|