4 * Copyright (C) 2004, Jonathan Sevy <jsevy@mcs.drexel.edu>
\r
6 * This is free software. Redistribution and use in source and binary forms, with
\r
7 * or without modification, are permitted provided that the following conditions
\r
10 * 1. Redistributions of source code must retain the above copyright notice, this
\r
11 * list of conditions and the following disclaimer.
\r
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
\r
13 * this list of conditions and the following disclaimer in the documentation
\r
14 * and/or other materials provided with the distribution.
\r
15 * 3. The name of the author may not be used to endorse or promote products
\r
16 * derived from this software without specific prior written permission.
\r
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
\r
19 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
\r
20 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
\r
21 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
\r
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
\r
23 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
\r
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
\r
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
\r
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
33 import java.math.BigInteger;
\r
34 import java.util.Vector;
\r
36 import org.snmpj.SNMPBadValueException;
\r
37 import org.snmpj.SNMPInteger;
\r
38 import org.snmpj.SNMPObject;
\r
39 import org.snmpj.SNMPSequence;
\r
45 * The SNMPPDU class represents an SNMP PDU from RFC 1157, as indicated below. This
\r
46 * forms the payload of an SNMP message.
\r
48 -- protocol data units
\r
74 GetNextRequest-PDU ::=
\r
91 error-status -- sometimes ignored
\r
101 error-index -- sometimes ignored
\r
104 variable-bindings -- values are sometimes ignored
\r
110 -- variable bindings
\r
130 public class SNMPPDU extends SNMPSequence
\r
135 * Create a new PDU of the specified type, with given request ID, error status, and error index,
\r
136 * and containing the supplied SNMP sequence as data.
\r
139 public SNMPPDU(byte pduType, int requestID, int errorStatus, int errorIndex, SNMPSequence varList)
\r
140 throws SNMPBadValueException
\r
143 Vector contents = new Vector();
\r
145 contents.insertElementAt(new SNMPInteger(requestID), 0);
\r
146 contents.insertElementAt(new SNMPInteger(errorStatus), 1);
\r
147 contents.insertElementAt(new SNMPInteger(errorIndex), 2);
\r
148 contents.insertElementAt(varList, 3);
\r
149 this.setValue(contents);
\r
156 * Create a new PDU of the specified type from the supplied BER encoding.
\r
157 * @throws SNMPBadValueException Indicates invalid SNMP PDU encoding supplied in enc.
\r
160 protected SNMPPDU(byte[] enc, byte pduType)
\r
161 throws SNMPBadValueException
\r
164 extractFromBEREncoding(enc);
\r
166 // validate the message: make sure we have the appropriate pieces
\r
167 Vector contents = (Vector)(this.getValue());
\r
169 if (contents.size() != 4)
\r
171 throw new SNMPBadValueException("Bad PDU");
\r
174 if (!(contents.elementAt(0) instanceof SNMPInteger))
\r
176 throw new SNMPBadValueException("Bad PDU: bad request ID");
\r
179 if (!(contents.elementAt(1) instanceof SNMPInteger))
\r
181 throw new SNMPBadValueException("Bad PDU: bad error status");
\r
184 if (!(contents.elementAt(2) instanceof SNMPInteger))
\r
186 throw new SNMPBadValueException("Bad PDU: bad error index");
\r
189 if (!(contents.elementAt(3) instanceof SNMPSequence))
\r
191 throw new SNMPBadValueException("Bad PDU: bad variable binding list");
\r
194 // now validate the variable binding list: should be list of sequences which
\r
195 // are (OID, value) pairs
\r
196 SNMPSequence varBindList = this.getVarBindList();
\r
197 for (int i = 0; i < varBindList.size(); i++)
\r
199 SNMPObject element = varBindList.getSNMPObjectAt(i);
\r
201 // must be a two-element sequence
\r
202 if (!(element instanceof SNMPSequence))
\r
204 throw new SNMPBadValueException("Bad PDU: bad variable binding at index" + i);
\r
207 // variable binding sequence must have 2 elements, first of which must be an object identifier
\r
208 SNMPSequence varBind = (SNMPSequence)element;
\r
209 if ((varBind.size() != 2) || !(varBind.getSNMPObjectAt(0) instanceof SNMPObjectIdentifier))
\r
211 throw new SNMPBadValueException("Bad PDU: bad variable binding at index" + i);
\r
222 * A utility method that extracts the variable binding list from the pdu. Useful for retrieving
\r
223 * the set of (object identifier, value) pairs returned in response to a request to an SNMP
\r
224 * device. The variable binding list is just an SNMP sequence containing the identifier, value pairs.
\r
225 * @see snmp.SNMPVarBindList
\r
228 public SNMPSequence getVarBindList()
\r
230 Vector contents = (Vector)(this.getValue());
\r
231 return (SNMPSequence)(contents.elementAt(3));
\r
237 * A utility method that extracts the request ID number from this PDU.
\r
240 public int getRequestID()
\r
242 Vector contents = (Vector)(this.getValue());
\r
243 return ((BigInteger)((SNMPInteger)(contents.elementAt(0))).getValue()).intValue();
\r
249 * A utility method that extracts the error status for this PDU; if nonzero, can get index of
\r
250 * problematic variable using getErrorIndex().
\r
253 public int getErrorStatus()
\r
255 Vector contents = (Vector)(this.getValue());
\r
256 return ((BigInteger)((SNMPInteger)(contents.elementAt(1))).getValue()).intValue();
\r
262 * A utility method that returns the error index for this PDU, identifying the problematic variable.
\r
265 public int getErrorIndex()
\r
267 Vector contents = (Vector)(this.getValue());
\r
268 return ((BigInteger)((SNMPInteger)(contents.elementAt(2))).getValue()).intValue();
\r
274 * A utility method that returns the PDU type of this PDU.
\r
277 public byte getPDUType()
\r