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.math.BigInteger;
033: import java.util.Vector;
034:
035: /**
036: * The SNMPv2BulkRequestPDU class represents an SNMPv2 Bulk Request PDU from RFC 1905, as indicated below. This
037: * forms the payload of an SNMPv2 Bulk Request message.
038:
039: -- protocol data units
040:
041: 3. Definitions
042:
043: SNMPv2-PDU DEFINITIONS ::= BEGIN
044:
045: IMPORTS
046: ObjectName, ObjectSyntax, Integer32
047: FROM SNMPv2-SMI;
048:
049: -- protocol data units
050:
051: PDUs ::=
052: CHOICE {
053: get-request
054: GetRequest-PDU,
055:
056: get-next-request
057: GetNextRequest-PDU,
058:
059: get-bulk-request
060: GetBulkRequest-PDU,
061:
062: response
063: Response-PDU,
064:
065: set-request
066: SetRequest-PDU,
067:
068: inform-request
069: InformRequest-PDU,
070:
071: snmpV2-trap
072: SNMPv2-Trap-PDU
073: }
074:
075:
076: -- PDUs
077:
078: GetRequest-PDU ::=
079: [0]
080: IMPLICIT PDU
081:
082: GetNextRequest-PDU ::=
083: [1]
084: IMPLICIT PDU
085:
086: Response-PDU ::=
087: [2]
088: IMPLICIT PDU
089:
090: SetRequest-PDU ::=
091: [3]
092: IMPLICIT PDU
093:
094: -- [4] is obsolete
095:
096: GetBulkRequest-PDU ::=
097: [5]
098: IMPLICIT BulkPDU
099:
100: InformRequest-PDU ::=
101: [6]
102: IMPLICIT PDU
103:
104: SNMPv2-Trap-PDU ::=
105: [7]
106: IMPLICIT PDU
107:
108:
109: max-bindings
110: INTEGER ::= 2147483647
111:
112:
113: BulkPDU ::= -- MUST be identical in
114: SEQUENCE { -- structure to PDU
115:
116: request-id Integer32,
117:
118: non-repeaters INTEGER (0..max-bindings),
119:
120: max-repetitions INTEGER (0..max-bindings),
121:
122: variable-bindings VarBindList -- values are ignored
123:
124: }
125:
126: */
127:
128: public class SNMPv2BulkRequestPDU extends SNMPSequence {
129:
130: /**
131: * Create a new PDU of the specified type, with given request ID, non-repeaters, and max-repetitions fields,
132: * and containing the supplied SNMP sequence as data.
133: */
134:
135: public SNMPv2BulkRequestPDU(int requestID, int nonRepeaters,
136: int maxRepetitions, SNMPSequence varList)
137: throws SNMPBadValueException {
138: super ();
139: Vector contents = new Vector();
140: tag = SNMPBERCodec.SNMPv2BULKREQUEST;
141: contents.insertElementAt(new SNMPInteger(requestID), 0);
142: contents.insertElementAt(new SNMPInteger(nonRepeaters), 1);
143: contents.insertElementAt(new SNMPInteger(maxRepetitions), 2);
144: contents.insertElementAt(varList, 3);
145: this .setValue(contents);
146: }
147:
148: /**
149: * Create a new PDU of the specified type from the supplied BER encoding.
150: * @throws SNMPBadValueException Indicates invalid SNMP Bulk PDU encoding supplied in enc.
151: */
152:
153: protected SNMPv2BulkRequestPDU(byte[] enc, byte pduType)
154: throws SNMPBadValueException {
155: tag = pduType;
156: extractFromBEREncoding(enc);
157:
158: // validate the message: make sure we have the appropriate pieces
159: Vector contents = (Vector) (this .getValue());
160:
161: if (contents.size() != 4) {
162: throw new SNMPBadValueException("Bad Bulk Request PDU");
163: }
164:
165: if (!(contents.elementAt(0) instanceof SNMPInteger)) {
166: throw new SNMPBadValueException(
167: "Bad Bulk Request PDU: bad request ID");
168: }
169:
170: if (!(contents.elementAt(1) instanceof SNMPInteger)) {
171: throw new SNMPBadValueException(
172: "Bad Bulk Request PDU: bad non-repeaters field");
173: }
174:
175: if (!(contents.elementAt(2) instanceof SNMPInteger)) {
176: throw new SNMPBadValueException(
177: "Bad Bulk Request PDU: bad max-repetitions field");
178: }
179:
180: if (!(contents.elementAt(3) instanceof SNMPSequence)) {
181: throw new SNMPBadValueException(
182: "Bad Bulk Request PDU: bad variable binding list");
183: }
184:
185: // now validate the variable binding list: should be list of sequences which
186: // are (OID, value) pairs
187: SNMPSequence varBindList = this .getVarBindList();
188: for (int i = 0; i < varBindList.size(); i++) {
189: SNMPObject element = varBindList.getSNMPObjectAt(i);
190:
191: // must be a two-element sequence
192: if (!(element instanceof SNMPSequence)) {
193: throw new SNMPBadValueException(
194: "Bad Bulk Request PDU: bad variable binding at index"
195: + i);
196: }
197:
198: // variable binding sequence must have 2 elements, first of which must be an object identifier
199: SNMPSequence varBind = (SNMPSequence) element;
200: if ((varBind.size() != 2)
201: || !(varBind.getSNMPObjectAt(0) instanceof SNMPObjectIdentifier)) {
202: throw new SNMPBadValueException(
203: "Bad Bulk Request PDU: bad variable binding at index"
204: + i);
205: }
206: }
207:
208: }
209:
210: /**
211: * A utility method that extracts the variable binding list from the pdu. Useful for retrieving
212: * the set of (object identifier, value) pairs returned in response to a request to an SNMP
213: * device. The variable binding list is just an SNMP sequence containing the identifier, value pairs.
214: * @see snmp.SNMPVarBindList
215: */
216:
217: public SNMPSequence getVarBindList() {
218: Vector contents = (Vector) (this .getValue());
219: return (SNMPSequence) (contents.elementAt(3));
220: }
221:
222: /**
223: * A utility method that extracts the request ID number from this PDU.
224: */
225:
226: public int getRequestID() {
227: Vector contents = (Vector) (this .getValue());
228: return ((BigInteger) ((SNMPInteger) (contents.elementAt(0)))
229: .getValue()).intValue();
230: }
231:
232: /**
233: * A utility method that extracts the non-repeaters field for this PDU.
234: */
235:
236: public int getNonRepeaters() {
237: Vector contents = (Vector) (this .getValue());
238: return ((BigInteger) ((SNMPInteger) (contents.elementAt(1)))
239: .getValue()).intValue();
240: }
241:
242: /**
243: * A utility method that returns the max-repetitions field for this PDU.
244: */
245:
246: public int getMaxRepetitions() {
247: Vector contents = (Vector) (this .getValue());
248: return ((BigInteger) ((SNMPInteger) (contents.elementAt(2)))
249: .getValue()).intValue();
250: }
251:
252: /**
253: * A utility method that returns the PDU type of this PDU.
254: */
255:
256: public byte getPDUType() {
257: return tag;
258: }
259:
260: }
|