KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > snmp > SNMPv1TrapPDU


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 SNMPTrapPDU class represents an SNMPv1 Trap PDU from RFC 1157, as indicated below. This
41 * forms the payload of an SNMP Trap 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           Trap-PDU ::=
105               [4]
106                  IMPLICIT SEQUENCE {
107                       enterprise -- type of object generating
108                                         -- trap, see sysObjectID in [5]
109
110                           OBJECT IDENTIFIER,
111
112                       agent-addr -- address of object generating
113                           NetworkAddress, -- trap
114
115                       generic-trap -- generic trap type
116                           INTEGER {
117                               coldStart(0),
118                               warmStart(1),
119                               linkDown(2),
120                               linkUp(3),
121                               authenticationFailure(4),
122                               egpNeighborLoss(5),
123                               enterpriseSpecific(6)
124                           },
125
126                       specific-trap -- specific code, present even
127                           INTEGER, -- if generic-trap is not
128                                      -- enterpriseSpecific
129
130                       time-stamp -- time elapsed between the last
131                           TimeTicks, -- (re)initialization of the
132                                         network
133                                      -- entity and the generation of the
134                                         trap
135
136                        variable-bindings -- "interesting" information
137                           VarBindList
138                   }
139           -- variable bindings
140
141           VarBind ::=
142                   SEQUENCE {
143                       name
144                           ObjectName,
145
146                       value
147                           ObjectSyntax
148                   }
149
150          VarBindList ::=
151                   SEQUENCE OF
152                      VarBind
153
154          END
155
156 */

157
158
159 public class SNMPv1TrapPDU extends SNMPSequence
160 {
161     
162     /**
163     * Create a new Trap PDU of the specified type, with given request ID, error status, and error index,
164     * and containing the supplied SNMP sequence as data.
165     */

166     
167     public SNMPv1TrapPDU(SNMPObjectIdentifier enterpriseOID, SNMPIPAddress agentAddress, int genericTrap, int specificTrap, SNMPTimeTicks timestamp, SNMPSequence varList)
168         throws SNMPBadValueException
169     {
170         super();
171         
172         tag = SNMPBERCodec.SNMPTRAP;
173         
174         Vector contents = new Vector();
175         
176         contents.addElement(enterpriseOID);
177         contents.addElement(agentAddress);
178         contents.addElement(new SNMPInteger(genericTrap));
179         contents.addElement(new SNMPInteger(specificTrap));
180         contents.addElement(timestamp);
181         contents.addElement(varList);
182         
183         this.setValue(contents);
184     }
185     
186     
187     
188     /**
189     * Create a new Trap PDU of the specified type, with given request ID, error status, and error index,
190     * and containing an empty SNMP sequence (VarBindList) as additional data.
191     */

192     
193     public SNMPv1TrapPDU(SNMPObjectIdentifier enterpriseOID, SNMPIPAddress agentAddress, int genericTrap, int specificTrap, SNMPTimeTicks timestamp)
194         throws SNMPBadValueException
195     {
196         super();
197         
198         tag = SNMPBERCodec.SNMPTRAP;
199         
200         Vector contents = new Vector();
201         
202         contents.addElement(enterpriseOID);
203         contents.addElement(agentAddress);
204         contents.addElement(new SNMPInteger(genericTrap));
205         contents.addElement(new SNMPInteger(specificTrap));
206         contents.addElement(timestamp);
207         contents.addElement(new SNMPVarBindList());
208         
209         this.setValue(contents);
210     }
211     
212     
213     
214     
215     /**
216     * Create a new PDU of the specified type from the supplied BER encoding.
217     * @throws SNMPBadValueException Indicates invalid SNMP PDU encoding supplied in enc.
218     */

219     
220     protected SNMPv1TrapPDU(byte[] enc)
221         throws SNMPBadValueException
222     {
223         tag = SNMPBERCodec.SNMPTRAP;
224         extractFromBEREncoding(enc);
225         
226         // validate the message: make sure we have the appropriate pieces
227
Vector contents = (Vector)(this.getValue());
228         
229         if (contents.size() != 6)
230         {
231             throw new SNMPBadValueException("Bad Trap PDU");
232         }
233         
234         if (!(contents.elementAt(0) instanceof SNMPObjectIdentifier))
235         {
236             throw new SNMPBadValueException("Bad Trap PDU: bad enterprise OID");
237         }
238         
239         if (!(contents.elementAt(1) instanceof SNMPIPAddress))
240         {
241             throw new SNMPBadValueException("Bad Trap PDU: bad agent address");
242         }
243         
244         if (!(contents.elementAt(2) instanceof SNMPInteger))
245         {
246             throw new SNMPBadValueException("Bad Trap PDU: bad generic trap code");
247         }
248         
249         if (!(contents.elementAt(3) instanceof SNMPInteger))
250         {
251             throw new SNMPBadValueException("Bad Trap PDU: bad specific trap code");
252         }
253         
254         if (!(contents.elementAt(4) instanceof SNMPTimeTicks))
255         {
256             throw new SNMPBadValueException("Bad Trap PDU: bad timestamp");
257         }
258         
259         if (!(contents.elementAt(5) instanceof SNMPSequence))
260         {
261             throw new SNMPBadValueException("Bad Trap PDU: bad variable binding list");
262         }
263         
264         // now validate the variable binding list: should be list of sequences which
265
// are (OID, value) pairs
266
SNMPSequence varBindList = this.getVarBindList();
267         for (int i = 0; i < varBindList.size(); i++)
268         {
269             SNMPObject element = varBindList.getSNMPObjectAt(i);
270             
271             // must be a two-element sequence
272
if (!(element instanceof SNMPSequence))
273             {
274                 throw new SNMPBadValueException("Bad Trap PDU: bad variable binding at index" + i);
275             }
276             
277             // variable binding sequence must have 2 elements, first of which must be an object identifier
278
SNMPSequence varBind = (SNMPSequence)element;
279             if ((varBind.size() != 2) || !(varBind.getSNMPObjectAt(0) instanceof SNMPObjectIdentifier))
280             {
281                 throw new SNMPBadValueException("Bad Trap PDU: bad variable binding at index" + i);
282             }
283         }
284     }
285     
286     
287     
288     
289     /**
290     * A utility method that extracts the variable binding list from the pdu. Useful for retrieving
291     * the set of (object identifier, value) pairs returned in response to a request to an SNMP
292     * device. The variable binding list is just an SNMP sequence containing the identifier, value pairs.
293     * @see snmp.SNMPVarBindList
294     */

295     
296     public SNMPSequence getVarBindList()
297     {
298         Vector contents = (Vector)(this.getValue());
299         return (SNMPSequence)(contents.elementAt(5));
300     }
301     
302     
303     
304     /**
305     * A utility method that extracts the enterprise OID from this PDU.
306     */

307     
308     public SNMPObjectIdentifier getEnterpriseOID()
309     {
310         Vector contents = (Vector)(this.getValue());
311         return (SNMPObjectIdentifier)contents.elementAt(0);
312     }
313     
314     
315     
316     /**
317     * A utility method that extracts the sending agent address this PDU.
318     */

319     
320     public SNMPIPAddress getAgentAddress()
321     {
322         Vector contents = (Vector)(this.getValue());
323         return (SNMPIPAddress)contents.elementAt(1);
324     }
325     
326     
327     
328     /**
329     * A utility method that returns the generic trap code for this PDU.
330     */

331     
332     public int getGenericTrap()
333     {
334         Vector contents = (Vector)(this.getValue());
335         return ((BigInteger)((SNMPInteger)(contents.elementAt(2))).getValue()).intValue();
336     }
337     
338     
339     /**
340     * A utility method that returns the specific trap code for this PDU.
341     */

342     
343     public int getSpecificTrap()
344     {
345         Vector contents = (Vector)(this.getValue());
346         return ((BigInteger)((SNMPInteger)(contents.elementAt(3))).getValue()).intValue();
347     }
348     
349     
350     /**
351     * A utility method that returns the timestamp for this PDU.
352     */

353     
354     public long getTimestamp()
355     {
356         Vector contents = (Vector)(this.getValue());
357         return ((BigInteger)((SNMPTimeTicks)(contents.elementAt(4))).getValue()).longValue();
358     }
359     
360     
361 }
Popular Tags