KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snmp > SNMPPDU


1 /*
2  * SNMP Package
3  *
4  * Copyright (C) 2004, Jonathan Sevy <jsevy@mcs.drexel.edu>
5  *
6  * This is free software. Redistribution and use in source and binary forms, with
7  * or without modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  * derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
19  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
20  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
21  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
23  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */

29
30
31 package snmp;
32
33 import java.util.*;
34 import java.math.*;
35
36
37
38
39 /**
40 * The SNMPPDU class represents an SNMP PDU from RFC 1157, as indicated below. This
41 * forms the payload of an SNMP message.
42
43 -- protocol data units
44
45           PDUs ::=
46                   CHOICE {
47                               get-request
48                                   GetRequest-PDU,
49
50                               get-next-request
51                                   GetNextRequest-PDU,
52
53                               get-response
54                                   GetResponse-PDU,
55
56                               set-request
57                                   SetRequest-PDU,
58
59                               trap
60                                   Trap-PDU
61                           }
62
63           -- PDUs
64
65           GetRequest-PDU ::=
66               [0]
67                   IMPLICIT PDU
68
69           GetNextRequest-PDU ::=
70               [1]
71                   IMPLICIT PDU
72
73           GetResponse-PDU ::=
74               [2]
75                   IMPLICIT PDU
76
77           SetRequest-PDU ::=
78               [3]
79                   IMPLICIT PDU
80
81           PDU ::=
82                   SEQUENCE {
83                      request-id
84                           INTEGER,
85
86                       error-status -- sometimes ignored
87                           INTEGER {
88                               noError(0),
89                               tooBig(1),
90                               noSuchName(2),
91                               badValue(3),
92                               readOnly(4),
93                               genErr(5)
94                           },
95
96                       error-index -- sometimes ignored
97                          INTEGER,
98
99                       variable-bindings -- values are sometimes ignored
100                           VarBindList
101                   }
102
103           
104
105           -- variable bindings
106
107           VarBind ::=
108                   SEQUENCE {
109                       name
110                           ObjectName,
111
112                       value
113                           ObjectSyntax
114                   }
115
116          VarBindList ::=
117                   SEQUENCE OF
118                      VarBind
119
120          END
121
122 */

123
124
125 public class SNMPPDU extends SNMPSequence
126 {
127     
128     
129     /**
130     * Create a new PDU of the specified type, with given request ID, error status, and error index,
131     * and containing the supplied SNMP sequence as data.
132     */

133     
134     public SNMPPDU(byte pduType, int requestID, int errorStatus, int errorIndex, SNMPSequence varList)
135         throws SNMPBadValueException
136     {
137         super();
138         Vector contents = new Vector();
139         tag = pduType;
140         contents.insertElementAt(new SNMPInteger(requestID), 0);
141         contents.insertElementAt(new SNMPInteger(errorStatus), 1);
142         contents.insertElementAt(new SNMPInteger(errorIndex), 2);
143         contents.insertElementAt(varList, 3);
144         this.setValue(contents);
145     }
146     
147     
148     
149     
150     /**
151     * Create a new PDU of the specified type from the supplied BER encoding.
152     * @throws SNMPBadValueException Indicates invalid SNMP PDU encoding supplied in enc.
153     */

154     
155     protected SNMPPDU(byte[] enc, byte pduType)
156         throws SNMPBadValueException
157     {
158         tag = pduType;
159         extractFromBEREncoding(enc);
160         
161         // validate the message: make sure we have the appropriate pieces
162
Vector contents = (Vector)(this.getValue());
163         
164         if (contents.size() != 4)
165         {
166             throw new SNMPBadValueException("Bad PDU");
167         }
168         
169         if (!(contents.elementAt(0) instanceof SNMPInteger))
170         {
171             throw new SNMPBadValueException("Bad PDU: bad request ID");
172         }
173         
174         if (!(contents.elementAt(1) instanceof SNMPInteger))
175         {
176             throw new SNMPBadValueException("Bad PDU: bad error status");
177         }
178         
179         if (!(contents.elementAt(2) instanceof SNMPInteger))
180         {
181             throw new SNMPBadValueException("Bad PDU: bad error index");
182         }
183         
184         if (!(contents.elementAt(3) instanceof SNMPSequence))
185         {
186             throw new SNMPBadValueException("Bad PDU: bad variable binding list");
187         }
188         
189         // now validate the variable binding list: should be list of sequences which
190
// are (OID, value) pairs
191
SNMPSequence varBindList = this.getVarBindList();
192         for (int i = 0; i < varBindList.size(); i++)
193         {
194             SNMPObject element = varBindList.getSNMPObjectAt(i);
195             
196             // must be a two-element sequence
197
if (!(element instanceof SNMPSequence))
198             {
199                 throw new SNMPBadValueException("Bad PDU: bad variable binding at index" + i);
200             }
201             
202             // variable binding sequence must have 2 elements, first of which must be an object identifier
203
SNMPSequence varBind = (SNMPSequence)element;
204             if ((varBind.size() != 2) || !(varBind.getSNMPObjectAt(0) instanceof SNMPObjectIdentifier))
205             {
206                 throw new SNMPBadValueException("Bad PDU: bad variable binding at index" + i);
207             }
208         }
209         
210         
211     }
212     
213     
214     
215     
216     /**
217     * A utility method that extracts the variable binding list from the pdu. Useful for retrieving
218     * the set of (object identifier, value) pairs returned in response to a request to an SNMP
219     * device. The variable binding list is just an SNMP sequence containing the identifier, value pairs.
220     * @see snmp.SNMPVarBindList
221     */

222     
223     public SNMPSequence getVarBindList()
224     {
225         Vector contents = (Vector)(this.getValue());
226         return (SNMPSequence)(contents.elementAt(3));
227     }
228     
229     
230     
231     /**
232     * A utility method that extracts the request ID number from this PDU.
233     */

234     
235     public int getRequestID()
236     {
237         Vector contents = (Vector)(this.getValue());
238         return ((BigInteger)((SNMPInteger)(contents.elementAt(0))).getValue()).intValue();
239     }
240     
241     
242     
243     /**
244     * A utility method that extracts the error status for this PDU; if nonzero, can get index of
245     * problematic variable using getErrorIndex().
246     */

247     
248     public int getErrorStatus()
249     {
250         Vector contents = (Vector)(this.getValue());
251         return ((BigInteger)((SNMPInteger)(contents.elementAt(1))).getValue()).intValue();
252     }
253     
254     
255     
256     /**
257     * A utility method that returns the error index for this PDU, identifying the problematic variable.
258     */

259     
260     public int getErrorIndex()
261     {
262         Vector contents = (Vector)(this.getValue());
263         return ((BigInteger)((SNMPInteger)(contents.elementAt(2))).getValue()).intValue();
264     }
265     
266     
267     
268     /**
269     * A utility method that returns the PDU type of this PDU.
270     */

271     
272     public byte getPDUType()
273     {
274         return tag;
275     }
276     
277     
278 }
Popular Tags