001: /*
002: * SNMP Package
003: *
004: * Copyright (C) 2004, Jonathan Sevy <jsevy@mcs.drexel.edu>
005: *
006: * This is free software. Redistribution and use in source and binary forms, with
007: * or without modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright notice, this
011: * list of conditions and the following disclaimer.
012: * 2. Redistributions in binary form must reproduce the above copyright notice,
013: * this list of conditions and the following disclaimer in the documentation
014: * and/or other materials provided with the distribution.
015: * 3. The name of the author may not be used to endorse or promote products
016: * derived from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
019: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
021: * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
023: * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
024: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
025: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
026: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: */
029:
030: package snmp;
031:
032: import java.util.*;
033:
034: /**
035: * Class to hold IP addresses; special case of SNMP Octet String.
036: */
037:
038: public class SNMPIPAddress extends SNMPOctetString {
039: // length limited to 4 octets
040:
041: /**
042: * Initialize to 0.0.0.0
043: */
044:
045: public SNMPIPAddress() {
046: // initialize to 0.0.0.0
047: tag = SNMPBERCodec.SNMPIPADDRESS;
048: data = new byte[4];
049: for (int i = 0; i < 4; i++)
050: data[i] = 0;
051: }
052:
053: /**
054: * Used to initialize from a string containing a standard "dotted" IP address.
055: * @throws SNMPBadValueException Indicates an invalid string supplied: more than 4 components,
056: * component values not between 0 and 255, etc.
057: */
058:
059: public SNMPIPAddress(String string) throws SNMPBadValueException {
060: tag = SNMPBERCodec.SNMPIPADDRESS;
061: this .data = parseIPAddress(string);
062: }
063:
064: /**
065: * Used to initialize from the BER encoding, as received in a response from
066: * an SNMP device responding to an SNMPGetRequest, or from a supplied byte array
067: * containing the address components.
068: * @throws SNMPBadValueException Indicates an invalid array supplied: must have length 4.
069: */
070:
071: public SNMPIPAddress(byte[] enc) throws SNMPBadValueException {
072:
073: tag = SNMPBERCodec.SNMPIPADDRESS;
074:
075: if (enc.length == 4) {
076: data = enc;
077: } else // wrong size
078: {
079: throw new SNMPBadValueException(
080: " IPAddress: bad BER encoding supplied to set value ");
081: }
082: }
083:
084: /**
085: * Used to set the value from a byte array containing the address.
086: * @throws SNMPBadValueException Indicates an incorrect object type supplied, or array of
087: * incorrect size.
088: */
089:
090: public void setValue(Object newAddress)
091: throws SNMPBadValueException {
092: if ((newAddress instanceof byte[])
093: && (((byte[]) newAddress).length == 4))
094: data = (byte[]) newAddress;
095: else if (newAddress instanceof String) {
096: data = parseIPAddress((String) newAddress);
097: } else
098: throw new SNMPBadValueException(
099: " IPAddress: bad data supplied to set value ");
100: }
101:
102: /**
103: * Return pretty-printed IP address.
104: */
105:
106: public String toString() {
107: StringBuffer returnStringBuffer = new StringBuffer();
108:
109: if (data.length > 0) {
110: int convert = data[0];
111: if (convert < 0)
112: convert += 256;
113: returnStringBuffer.append(convert);
114:
115: for (int i = 1; i < data.length; i++) {
116: convert = data[i];
117: if (convert < 0)
118: convert += 256;
119: returnStringBuffer.append(".");
120: returnStringBuffer.append(convert);
121: }
122: }
123:
124: return returnStringBuffer.toString();
125: }
126:
127: private byte[] parseIPAddress(String addressString)
128: throws SNMPBadValueException {
129: try {
130: StringTokenizer st = new StringTokenizer(addressString,
131: " .");
132: int size = 0;
133:
134: while (st.hasMoreTokens()) {
135: // figure out how many values are in string
136: size++;
137: st.nextToken();
138: }
139:
140: if (size != 4) {
141: throw new SNMPBadValueException(
142: " IPAddress: wrong number of components supplied to set value ");
143: }
144:
145: byte[] returnBytes = new byte[size];
146:
147: st = new StringTokenizer(addressString, " .");
148:
149: for (int i = 0; i < size; i++) {
150: int addressComponent = (Integer
151: .parseInt(st.nextToken()));
152: if ((addressComponent < 0) || (addressComponent > 255))
153: throw new SNMPBadValueException(
154: " IPAddress: invalid component supplied to set value ");
155: returnBytes[i] = (byte) addressComponent;
156: }
157:
158: return returnBytes;
159:
160: } catch (NumberFormatException e) {
161: throw new SNMPBadValueException(
162: " IPAddress: invalid component supplied to set value ");
163: }
164:
165: }
166:
167: }
|