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: * Defines class for holding physical 6-byte addresses.
036: */
037:
038: public class SNMPNSAPAddress extends SNMPOctetString {
039: // length limited to 6 octets
040:
041: /**
042: * Initialize address to 0.0.0.0.0.0.
043: */
044:
045: public SNMPNSAPAddress() {
046: tag = SNMPBERCodec.SNMPNSAPADDRESS;
047:
048: // initialize to 0.0.0.0.0.0
049: data = new byte[6];
050: for (int i = 0; i < 6; i++)
051: data[i] = 0;
052: }
053:
054: public SNMPNSAPAddress(String string) throws SNMPBadValueException {
055: tag = SNMPBERCodec.SNMPNSAPADDRESS;
056:
057: data = parseNSAPAddress(string);
058: }
059:
060: /**
061: * Used to initialize from the BER encoding, as received in a response from
062: * an SNMP device responding to an SNMPGetRequest, or from a supplied byte array
063: * containing the address components.
064: * @throws SNMPBadValueException Indicates an invalid array supplied: must have length 6.
065: */
066:
067: public SNMPNSAPAddress(byte[] enc) throws SNMPBadValueException {
068: tag = SNMPBERCodec.SNMPNSAPADDRESS;
069:
070: if (enc.length == 6) {
071: data = enc;
072: } else // wrong size
073: {
074: throw new SNMPBadValueException(
075: " NSAPAddress: bad BER encoding supplied to set value ");
076: }
077: }
078:
079: /**
080: * Used to set the value from a byte array containing the address.
081: * @throws SNMPBadValueException Indicates an incorrect object type supplied, or array of
082: * incorrect size.
083: */
084:
085: public void setValue(Object newAddress)
086: throws SNMPBadValueException {
087: if ((newAddress instanceof byte[])
088: && (((byte[]) newAddress).length == 6))
089: data = (byte[]) newAddress;
090: else if (newAddress instanceof String) {
091: data = parseNSAPAddress((String) newAddress);
092: } else
093: throw new SNMPBadValueException(
094: " NSAPAddress: bad length byte string supplied to set value ");
095: }
096:
097: /**
098: * Return pretty-printed (dash-separated) address.
099: */
100:
101: public String toString() {
102: StringBuffer returnStringBuffer = new StringBuffer();
103:
104: if (data.length > 0) {
105: int convert = data[0];
106: if (convert < 0)
107: convert += 256;
108: returnStringBuffer.append(Integer.toHexString(convert));
109:
110: for (int i = 1; i < data.length; i++) {
111: convert = data[i];
112: if (convert < 0)
113: convert += 256;
114: returnStringBuffer.append("-");
115: returnStringBuffer.append(Integer.toHexString(convert));
116: }
117: }
118:
119: return returnStringBuffer.toString();
120: }
121:
122: private byte[] parseNSAPAddress(String addressString)
123: throws SNMPBadValueException {
124: try {
125: StringTokenizer st = new StringTokenizer(addressString,
126: " .-"); // break on spaces, dots or dashes
127: int size = 0;
128:
129: while (st.hasMoreTokens()) {
130: // figure out how many values are in string
131: size++;
132: st.nextToken();
133: }
134:
135: if (size != 6) {
136: throw new SNMPBadValueException(
137: " NSAPAddress: wrong number of components supplied to set value ");
138: }
139:
140: byte[] returnBytes = new byte[size];
141:
142: st = new StringTokenizer(addressString, " .-");
143:
144: for (int i = 0; i < size; i++) {
145: int addressComponent = (Integer.parseInt(
146: st.nextToken(), 16));
147: if ((addressComponent < 0) || (addressComponent > 255))
148: throw new SNMPBadValueException(
149: " NSAPAddress: invalid component supplied to set value ");
150: returnBytes[i] = (byte) addressComponent;
151: }
152:
153: return returnBytes;
154:
155: } catch (NumberFormatException e) {
156: throw new SNMPBadValueException(
157: " NSAPAddress: invalid component supplied to set value ");
158: }
159:
160: }
161:
162: }
|