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: /**
033: * The SNMPv2TrapPDU class represents an SNMPv2 Trap PDU from RFC 1448, as indicated below. This
034: * forms the payload of an SNMPv2 Trap message.
035:
036: -- protocol data units
037:
038: 3. Definitions
039:
040: SNMPv2-PDU DEFINITIONS ::= BEGIN
041:
042: IMPORTS
043: ObjectName, ObjectSyntax, Integer32
044: FROM SNMPv2-SMI;
045:
046: -- protocol data units
047:
048: PDUs ::=
049: CHOICE {
050: get-request
051: GetRequest-PDU,
052:
053: get-next-request
054: GetNextRequest-PDU,
055:
056: get-bulk-request
057: GetBulkRequest-PDU,
058:
059: response
060: Response-PDU,
061:
062: set-request
063: SetRequest-PDU,
064:
065: inform-request
066: InformRequest-PDU,
067:
068: snmpV2-trap
069: SNMPv2-Trap-PDU
070: }
071:
072:
073: -- PDUs
074:
075: GetRequest-PDU ::=
076: [0]
077: IMPLICIT PDU
078:
079: GetNextRequest-PDU ::=
080: [1]
081: IMPLICIT PDU
082:
083: Response-PDU ::=
084: [2]
085: IMPLICIT PDU
086:
087: SetRequest-PDU ::=
088: [3]
089: IMPLICIT PDU
090:
091: -- [4] is obsolete
092:
093: GetBulkRequest-PDU ::=
094: [5]
095: IMPLICIT BulkPDU
096:
097: InformRequest-PDU ::=
098: [6]
099: IMPLICIT PDU
100:
101: SNMPv2-Trap-PDU ::=
102: [7]
103: IMPLICIT PDU
104:
105:
106: max-bindings
107: INTEGER ::= 2147483647
108:
109: PDU ::=
110: SEQUENCE {
111: request-id
112: Integer32,
113:
114: error-status -- sometimes ignored
115: INTEGER {
116: noError(0),
117: tooBig(1),
118: noSuchName(2), -- for proxy compatibility
119: badValue(3), -- for proxy compatibility
120: readOnly(4), -- for proxy compatibility
121: genErr(5),
122: noAccess(6),
123: wrongType(7),
124: wrongLength(8),
125: wrongEncoding(9),
126: wrongValue(10),
127: noCreation(11),
128: inconsistentValue(12),
129: resourceUnavailable(13),
130: commitFailed(14),
131: undoFailed(15),
132: authorizationError(16),
133: notWritable(17),
134: inconsistentName(18)
135: },
136:
137: error-index -- sometimes ignored
138: INTEGER (0..max-bindings),
139:
140: variable-bindings -- values are sometimes ignored
141: VarBindList
142: }
143:
144: */
145:
146: public class SNMPv2TrapPDU extends SNMPPDU {
147:
148: /**
149: * Create a new Trap PDU with given trapOID and sysUptime,
150: * and containing the supplied SNMP sequence as data.
151: */
152:
153: public SNMPv2TrapPDU(SNMPTimeTicks sysUptime,
154: SNMPObjectIdentifier snmpTrapOID, SNMPSequence varList)
155: throws SNMPBadValueException {
156: super (SNMPBERCodec.SNMPv2TRAP, 0, 0, 0, varList);
157:
158: // create a variable pair for sysUptime, and insert into varBindList
159: SNMPObjectIdentifier sysUptimeOID = new SNMPObjectIdentifier(
160: "1.3.6.1.2.1.1.3.0");
161: SNMPVariablePair sysUptimePair = new SNMPVariablePair(
162: sysUptimeOID, sysUptime);
163: varList.insertSNMPObjectAt(sysUptimePair, 0);
164:
165: // create a variable pair for snmpTrapOID, and insert into varBindList
166: SNMPObjectIdentifier snmpTrapOIDOID = new SNMPObjectIdentifier(
167: "1.3.6.1.6.3.1.1.4.1.0");
168: SNMPVariablePair snmpOIDPair = new SNMPVariablePair(
169: snmpTrapOIDOID, snmpTrapOID);
170: varList.insertSNMPObjectAt(snmpOIDPair, 1);
171:
172: }
173:
174: /**
175: * Create a new Trap PDU with given trapOID and sysUptime,
176: * and containing an empty SNMP sequence (VarBindList) as additional data.
177: */
178:
179: public SNMPv2TrapPDU(SNMPObjectIdentifier snmpTrapOID,
180: SNMPTimeTicks sysUptime) throws SNMPBadValueException {
181: this (sysUptime, snmpTrapOID, new SNMPSequence());
182: }
183:
184: /**
185: * Create a new PDU of the specified type from the supplied BER encoding.
186: * @throws SNMPBadValueException Indicates invalid SNMP PDU encoding supplied in enc.
187: */
188:
189: protected SNMPv2TrapPDU(byte[] enc) throws SNMPBadValueException {
190: super (enc, SNMPBERCodec.SNMPv2TRAP);
191:
192: // validate the message: make sure the first two components of the varBindList
193: // are the appropriate variable pairs
194: SNMPSequence varBindList = this .getVarBindList();
195:
196: if (varBindList.size() < 2) {
197: throw new SNMPBadValueException(
198: "Bad v2 Trap PDU: missing snmpTrapOID or sysUptime");
199: }
200:
201: // validate that the first variable binding is the sysUptime
202: SNMPSequence variablePair = (SNMPSequence) varBindList
203: .getSNMPObjectAt(0);
204: SNMPObjectIdentifier oid = (SNMPObjectIdentifier) variablePair
205: .getSNMPObjectAt(0);
206: SNMPObject value = variablePair.getSNMPObjectAt(1);
207: SNMPObjectIdentifier sysUptimeOID = new SNMPObjectIdentifier(
208: "1.3.6.1.2.1.1.3.0");
209: if (!(value instanceof SNMPTimeTicks)
210: || !oid.equals(sysUptimeOID)) {
211: throw new SNMPBadValueException(
212: "Bad v2 Trap PDU: bad sysUptime in variable binding list");
213: }
214:
215: // validate that the second variable binding is the snmpTrapOID
216: variablePair = (SNMPSequence) varBindList.getSNMPObjectAt(1);
217: oid = (SNMPObjectIdentifier) variablePair.getSNMPObjectAt(0);
218: value = variablePair.getSNMPObjectAt(1);
219: SNMPObjectIdentifier snmpTrapOIDOID = new SNMPObjectIdentifier(
220: "1.3.6.1.6.3.1.1.4.1.0");
221: if (!(value instanceof SNMPObjectIdentifier)
222: || !oid.equals(snmpTrapOIDOID)) {
223: throw new SNMPBadValueException(
224: "Bad v2 Trap PDU: bad snmpTrapOID in variable binding list");
225: }
226:
227: }
228:
229: /**
230: * A utility method that extracts the snmpTrapOID from the variable bind list (it's the second of the
231: * variable pairs).
232: */
233:
234: public SNMPObjectIdentifier getSNMPTrapOID() {
235: SNMPSequence contents = this .getVarBindList();
236: SNMPSequence variablePair = (SNMPSequence) contents
237: .getSNMPObjectAt(1);
238: return (SNMPObjectIdentifier) variablePair.getSNMPObjectAt(1);
239: }
240:
241: /**
242: * A utility method that extracts the sysUptime from the variable bind list (it's the first of the
243: * variable pairs).
244: */
245:
246: public SNMPTimeTicks getSysUptime() {
247: SNMPSequence contents = this .getVarBindList();
248: SNMPSequence variablePair = (SNMPSequence) contents
249: .getSNMPObjectAt(0);
250: return (SNMPTimeTicks) variablePair.getSNMPObjectAt(1);
251: }
252:
253: }
|