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 the SNMPMessage class as a special case of SNMPSequence. Defines a
036: * top-level SNMP message, as per the following definitions from RFC 1157 and
037: * RFC 1901.
038:
039:
040: RFC1157-SNMP DEFINITIONS
041:
042: IMPORTS FROM RFC1155-SMI;
043:
044: -- top-level message
045:
046: Message ::=
047: SEQUENCE {
048: version -- version-1 for this RFC
049: INTEGER {
050: version-1(0)
051: },
052:
053: community -- community name
054: OCTET STRING,
055:
056: data -- e.g., PDUs if trivial
057: ANY -- authentication is being used
058: }
059:
060:
061: -- From RFC 1901:
062:
063: COMMUNITY-BASED-SNMPv2 DEFINITIONS ::= BEGIN
064:
065: -- top-level message
066:
067: Message ::=
068: SEQUENCE {
069: version
070: INTEGER {
071: version(1) -- modified from RFC 1157
072: },
073:
074: community -- community name
075: OCTET STRING,
076:
077: data -- PDUs as defined in [4]
078: ANY
079: }
080: }
081:
082: END
083:
084: */
085:
086: public class SNMPMessage extends SNMPSequence {
087:
088: /**
089: * Create an SNMP message with specified version, community, and pdu.
090: * Use version = 0 for SNMP version 1, or version = 1 for enhanced capapbilities
091: * provided through RFC 1157.
092: */
093:
094: public SNMPMessage(int version, String community, SNMPPDU pdu) {
095: super ();
096: Vector contents = new Vector();
097: contents.insertElementAt(new SNMPInteger(version), 0);
098: contents.insertElementAt(new SNMPOctetString(community), 1);
099: contents.insertElementAt(pdu, 2);
100:
101: try {
102: this .setValue(contents);
103: } catch (SNMPBadValueException e) {
104: // can't happen! all supplied Vector elements are SNMP Object subclasses
105: }
106: }
107:
108: /**
109: * Create an SNMP message with specified version, community, and trap pdu.
110: * Use version = 0 for SNMP version 1, or version = 1 for enhanced capapbilities
111: * provided through RFC 1157.
112: */
113:
114: public SNMPMessage(int version, String community, SNMPv1TrapPDU pdu) {
115: super ();
116: Vector contents = new Vector();
117: contents.insertElementAt(new SNMPInteger(version), 0);
118: contents.insertElementAt(new SNMPOctetString(community), 1);
119: contents.insertElementAt(pdu, 2);
120:
121: try {
122: this .setValue(contents);
123: } catch (SNMPBadValueException e) {
124: // can't happen! all supplied Vector elements are SNMP Object subclasses
125: }
126: }
127:
128: /**
129: * Create an SNMP message with specified version, community, and v2 trap pdu.
130: * Use version = 1.
131: */
132:
133: public SNMPMessage(int version, String community, SNMPv2TrapPDU pdu) {
134: super ();
135: Vector contents = new Vector();
136: contents.insertElementAt(new SNMPInteger(version), 0);
137: contents.insertElementAt(new SNMPOctetString(community), 1);
138: contents.insertElementAt(pdu, 2);
139:
140: try {
141: this .setValue(contents);
142: } catch (SNMPBadValueException e) {
143: // can't happen! all supplied Vector elements are SNMP Object subclasses
144: }
145: }
146:
147: /**
148: * Construct an SNMPMessage from a received ASN.1 byte representation.
149: * @throws SNMPBadValueException Indicates invalid SNMP message encoding supplied.
150: */
151:
152: protected SNMPMessage(byte[] enc) throws SNMPBadValueException {
153: super (enc);
154:
155: // validate the message: make sure we have the appropriate pieces
156: Vector contents = (Vector) (this .getValue());
157:
158: if (contents.size() != 3) {
159: throw new SNMPBadValueException("Bad SNMP message");
160: }
161:
162: if (!(contents.elementAt(0) instanceof SNMPInteger)) {
163: throw new SNMPBadValueException(
164: "Bad SNMP message: bad version");
165: }
166:
167: if (!(contents.elementAt(1) instanceof SNMPOctetString)) {
168: throw new SNMPBadValueException(
169: "Bad SNMP message: bad community name");
170: }
171:
172: if (!(contents.elementAt(2) instanceof SNMPPDU)
173: && !(contents.elementAt(2) instanceof SNMPv1TrapPDU)
174: && !(contents.elementAt(2) instanceof SNMPv2TrapPDU)) {
175: throw new SNMPBadValueException("Bad SNMP message: bad PDU");
176: }
177:
178: }
179:
180: /**
181: * Utility method which returns the PDU contained in the SNMP message as a plain Java Object.
182: * The pdu is the third component of the sequence, after the version and community name.
183: */
184:
185: public Object getPDUAsObject() throws SNMPBadValueException {
186: Vector contents = (Vector) (this .getValue());
187: Object pdu = contents.elementAt(2);
188: return pdu;
189: }
190:
191: /**
192: * Utility method which returns the PDU contained in the SNMP message. The pdu is the third component
193: * of the sequence, after the version and community name.
194: */
195:
196: public SNMPPDU getPDU() throws SNMPBadValueException {
197: Vector contents = (Vector) (this .getValue());
198: Object pdu = contents.elementAt(2);
199:
200: if (!(pdu instanceof SNMPPDU)) {
201: throw new SNMPBadValueException(
202: "Wrong PDU type in message: expected SNMPPDU, have "
203: + pdu.getClass().toString());
204: }
205:
206: return (SNMPPDU) pdu;
207: }
208:
209: /**
210: * Utility method which returns the PDU contained in the SNMP message as an SNMPv1TrapPDU. The pdu is the
211: * third component of the sequence, after the version and community name.
212: */
213:
214: public SNMPv1TrapPDU getv1TrapPDU() throws SNMPBadValueException {
215: Vector contents = (Vector) (this .getValue());
216: Object pdu = contents.elementAt(2);
217:
218: if (!(pdu instanceof SNMPv1TrapPDU)) {
219: throw new SNMPBadValueException(
220: "Wrong PDU type in message: expected SNMPTrapPDU, have "
221: + pdu.getClass().toString());
222: }
223:
224: return (SNMPv1TrapPDU) pdu;
225: }
226:
227: /**
228: * Utility method which returns the PDU contained in the SNMP message as an SNMPv2TrapPDU. The pdu is the
229: * third component of the sequence, after the version and community name.
230: */
231:
232: public SNMPv2TrapPDU getv2TrapPDU() throws SNMPBadValueException {
233: Vector contents = (Vector) (this .getValue());
234: Object pdu = contents.elementAt(2);
235:
236: if (!(pdu instanceof SNMPv2TrapPDU)) {
237: throw new SNMPBadValueException(
238: "Wrong PDU type in message: expected SNMPv2TrapPDU, have "
239: + pdu.getClass().toString());
240: }
241:
242: return (SNMPv2TrapPDU) pdu;
243: }
244:
245: /**
246: * Utility method which returns the community name contained in the SNMP message. The community name is the
247: * second component of the sequence, after the version.
248: */
249:
250: public String getCommunityName() throws SNMPBadValueException {
251: Vector contents = (Vector) (this .getValue());
252: Object communityName = contents.elementAt(1);
253:
254: if (!(communityName instanceof SNMPOctetString)) {
255: throw new SNMPBadValueException(
256: "Wrong SNMP type for community name in message: expected SNMPOctetString, have "
257: + communityName.getClass().toString());
258: }
259:
260: return ((SNMPOctetString) communityName).toString();
261: }
262:
263: }
|