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: import java.math.*;
034:
035: /**
036: * The SNMPTrapPDU class represents an SNMPv1 Trap PDU from RFC 1157, as indicated below. This
037: * forms the payload of an SNMP Trap message.
038:
039: -- protocol data units
040:
041: PDUs ::=
042: CHOICE {
043: get-request
044: GetRequest-PDU,
045:
046: get-next-request
047: GetNextRequest-PDU,
048:
049: get-response
050: GetResponse-PDU,
051:
052: set-request
053: SetRequest-PDU,
054:
055: trap
056: Trap-PDU
057: }
058:
059: -- PDUs
060:
061: GetRequest-PDU ::=
062: [0]
063: IMPLICIT PDU
064:
065: GetNextRequest-PDU ::=
066: [1]
067: IMPLICIT PDU
068:
069: GetResponse-PDU ::=
070: [2]
071: IMPLICIT PDU
072:
073: SetRequest-PDU ::=
074: [3]
075: IMPLICIT PDU
076:
077: PDU ::=
078: SEQUENCE {
079: request-id
080: INTEGER,
081:
082: error-status -- sometimes ignored
083: INTEGER {
084: noError(0),
085: tooBig(1),
086: noSuchName(2),
087: badValue(3),
088: readOnly(4),
089: genErr(5)
090: },
091:
092: error-index -- sometimes ignored
093: INTEGER,
094:
095: variable-bindings -- values are sometimes ignored
096: VarBindList
097: }
098:
099:
100: Trap-PDU ::=
101: [4]
102: IMPLICIT SEQUENCE {
103: enterprise -- type of object generating
104: -- trap, see sysObjectID in [5]
105:
106: OBJECT IDENTIFIER,
107:
108: agent-addr -- address of object generating
109: NetworkAddress, -- trap
110:
111: generic-trap -- generic trap type
112: INTEGER {
113: coldStart(0),
114: warmStart(1),
115: linkDown(2),
116: linkUp(3),
117: authenticationFailure(4),
118: egpNeighborLoss(5),
119: enterpriseSpecific(6)
120: },
121:
122: specific-trap -- specific code, present even
123: INTEGER, -- if generic-trap is not
124: -- enterpriseSpecific
125:
126: time-stamp -- time elapsed between the last
127: TimeTicks, -- (re)initialization of the
128: network
129: -- entity and the generation of the
130: trap
131:
132: variable-bindings -- "interesting" information
133: VarBindList
134: }
135: -- variable bindings
136:
137: VarBind ::=
138: SEQUENCE {
139: name
140: ObjectName,
141:
142: value
143: ObjectSyntax
144: }
145:
146: VarBindList ::=
147: SEQUENCE OF
148: VarBind
149:
150: END
151:
152: */
153:
154: public class SNMPv1TrapPDU extends SNMPSequence {
155:
156: /**
157: * Create a new Trap PDU of the specified type, with given request ID, error status, and error index,
158: * and containing the supplied SNMP sequence as data.
159: */
160:
161: public SNMPv1TrapPDU(SNMPObjectIdentifier enterpriseOID,
162: SNMPIPAddress agentAddress, int genericTrap,
163: int specificTrap, SNMPTimeTicks timestamp,
164: SNMPSequence varList) throws SNMPBadValueException {
165: super ();
166:
167: tag = SNMPBERCodec.SNMPTRAP;
168:
169: Vector contents = new Vector();
170:
171: contents.addElement(enterpriseOID);
172: contents.addElement(agentAddress);
173: contents.addElement(new SNMPInteger(genericTrap));
174: contents.addElement(new SNMPInteger(specificTrap));
175: contents.addElement(timestamp);
176: contents.addElement(varList);
177:
178: this .setValue(contents);
179: }
180:
181: /**
182: * Create a new Trap PDU of the specified type, with given request ID, error status, and error index,
183: * and containing an empty SNMP sequence (VarBindList) as additional data.
184: */
185:
186: public SNMPv1TrapPDU(SNMPObjectIdentifier enterpriseOID,
187: SNMPIPAddress agentAddress, int genericTrap,
188: int specificTrap, SNMPTimeTicks timestamp)
189: throws SNMPBadValueException {
190: super ();
191:
192: tag = SNMPBERCodec.SNMPTRAP;
193:
194: Vector contents = new Vector();
195:
196: contents.addElement(enterpriseOID);
197: contents.addElement(agentAddress);
198: contents.addElement(new SNMPInteger(genericTrap));
199: contents.addElement(new SNMPInteger(specificTrap));
200: contents.addElement(timestamp);
201: contents.addElement(new SNMPVarBindList());
202:
203: this .setValue(contents);
204: }
205:
206: /**
207: * Create a new PDU of the specified type from the supplied BER encoding.
208: * @throws SNMPBadValueException Indicates invalid SNMP PDU encoding supplied in enc.
209: */
210:
211: protected SNMPv1TrapPDU(byte[] enc) throws SNMPBadValueException {
212: tag = SNMPBERCodec.SNMPTRAP;
213: extractFromBEREncoding(enc);
214:
215: // validate the message: make sure we have the appropriate pieces
216: Vector contents = (Vector) (this .getValue());
217:
218: if (contents.size() != 6) {
219: throw new SNMPBadValueException("Bad Trap PDU");
220: }
221:
222: if (!(contents.elementAt(0) instanceof SNMPObjectIdentifier)) {
223: throw new SNMPBadValueException(
224: "Bad Trap PDU: bad enterprise OID");
225: }
226:
227: if (!(contents.elementAt(1) instanceof SNMPIPAddress)) {
228: throw new SNMPBadValueException(
229: "Bad Trap PDU: bad agent address");
230: }
231:
232: if (!(contents.elementAt(2) instanceof SNMPInteger)) {
233: throw new SNMPBadValueException(
234: "Bad Trap PDU: bad generic trap code");
235: }
236:
237: if (!(contents.elementAt(3) instanceof SNMPInteger)) {
238: throw new SNMPBadValueException(
239: "Bad Trap PDU: bad specific trap code");
240: }
241:
242: if (!(contents.elementAt(4) instanceof SNMPTimeTicks)) {
243: throw new SNMPBadValueException(
244: "Bad Trap PDU: bad timestamp");
245: }
246:
247: if (!(contents.elementAt(5) instanceof SNMPSequence)) {
248: throw new SNMPBadValueException(
249: "Bad Trap PDU: bad variable binding list");
250: }
251:
252: // now validate the variable binding list: should be list of sequences which
253: // are (OID, value) pairs
254: SNMPSequence varBindList = this .getVarBindList();
255: for (int i = 0; i < varBindList.size(); i++) {
256: SNMPObject element = varBindList.getSNMPObjectAt(i);
257:
258: // must be a two-element sequence
259: if (!(element instanceof SNMPSequence)) {
260: throw new SNMPBadValueException(
261: "Bad Trap PDU: bad variable binding at index"
262: + i);
263: }
264:
265: // variable binding sequence must have 2 elements, first of which must be an object identifier
266: SNMPSequence varBind = (SNMPSequence) element;
267: if ((varBind.size() != 2)
268: || !(varBind.getSNMPObjectAt(0) instanceof SNMPObjectIdentifier)) {
269: throw new SNMPBadValueException(
270: "Bad Trap PDU: bad variable binding at index"
271: + i);
272: }
273: }
274: }
275:
276: /**
277: * A utility method that extracts the variable binding list from the pdu. Useful for retrieving
278: * the set of (object identifier, value) pairs returned in response to a request to an SNMP
279: * device. The variable binding list is just an SNMP sequence containing the identifier, value pairs.
280: * @see snmp.SNMPVarBindList
281: */
282:
283: public SNMPSequence getVarBindList() {
284: Vector contents = (Vector) (this .getValue());
285: return (SNMPSequence) (contents.elementAt(5));
286: }
287:
288: /**
289: * A utility method that extracts the enterprise OID from this PDU.
290: */
291:
292: public SNMPObjectIdentifier getEnterpriseOID() {
293: Vector contents = (Vector) (this .getValue());
294: return (SNMPObjectIdentifier) contents.elementAt(0);
295: }
296:
297: /**
298: * A utility method that extracts the sending agent address this PDU.
299: */
300:
301: public SNMPIPAddress getAgentAddress() {
302: Vector contents = (Vector) (this .getValue());
303: return (SNMPIPAddress) contents.elementAt(1);
304: }
305:
306: /**
307: * A utility method that returns the generic trap code for this PDU.
308: */
309:
310: public int getGenericTrap() {
311: Vector contents = (Vector) (this .getValue());
312: return ((BigInteger) ((SNMPInteger) (contents.elementAt(2)))
313: .getValue()).intValue();
314: }
315:
316: /**
317: * A utility method that returns the specific trap code for this PDU.
318: */
319:
320: public int getSpecificTrap() {
321: Vector contents = (Vector) (this .getValue());
322: return ((BigInteger) ((SNMPInteger) (contents.elementAt(3)))
323: .getValue()).intValue();
324: }
325:
326: /**
327: * A utility method that returns the timestamp for this PDU.
328: */
329:
330: public long getTimestamp() {
331: Vector contents = (Vector) (this .getValue());
332: return ((BigInteger) ((SNMPTimeTicks) (contents.elementAt(4)))
333: .getValue()).longValue();
334: }
335:
336: }
|