KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > snmp > SnmpMsg


1 /*
2  * @(#)file SnmpMsg.java
3  * @(#)author Sun Microsystems, Inc.
4  * @(#)version 1.20
5  * @(#)date 08/02/09
6  *
7  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
8  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
9  *
10  */

11
12 package com.sun.jmx.snmp;
13
14 import com.sun.jmx.snmp.SnmpSecurityParameters;
15 // java imports
16
//
17
import java.util.Vector JavaDoc;
18 import java.net.InetAddress JavaDoc;
19
20 // import debug stuff
21
//
22
import com.sun.jmx.trace.Trace;
23
24 import com.sun.jmx.snmp.SnmpStatusException;
25 /**
26  * A partially decoded representation of an SNMP packet. It contains
27  * the information contained in any SNMP message (SNMPv1, SNMPv2 or
28  * SNMPv3).
29  * <p><b>This API is a Sun Microsystems internal API and is subject
30  * to change without notice.</b></p>
31  * @since 1.5
32  */

33 public abstract class SnmpMsg implements SnmpDefinitions {
34     /**
35      * The protocol version.
36      * <P><CODE>decodeMessage</CODE> and <CODE>encodeMessage</CODE> do not
37      * perform any check on this value.
38      * <BR><CODE>decodeSnmpPdu</CODE> and <CODE>encodeSnmpPdu</CODE> only
39      * accept the values 0 (for SNMPv1), 1 (for SNMPv2) and 3 (for SNMPv3).
40      */

41     public int version = 0;
42
43     /**
44      * Encoding of the PDU.
45      * <P>This is usually the BER encoding of the PDU's syntax
46      * defined in RFC1157 and RFC1902. However, this can be authenticated
47      * or encrypted data (but you need to implemented your own
48      * <CODE>SnmpPduFactory</CODE> class).
49      */

50     public byte[] data = null;
51
52     /**
53      * Number of useful bytes in the <CODE>data</CODE> field.
54      */

55     public int dataLength = 0;
56
57     /**
58      * Source or destination address.
59      * <BR>For an incoming message it's the source.
60      * For an outgoing message it's the destination.
61      */

62     public InetAddress JavaDoc address = null;
63
64     /**
65      * Source or destination port.
66      * <BR>For an incoming message it's the source.
67      * For an outgoing message it's the destination.
68      */

69     public int port = 0;
70     /**
71      * Security parameters. Contain informations according to Security Model (Usm, community string based, ...).
72      */

73     public SnmpSecurityParameters securityParameters = null;
74     /**
75      * Returns the encoded SNMP version present in the passed byte array.
76      * @param data The unmarshalled SNMP message.
77      * @return The SNMP version (0, 1 or 3).
78      */

79     public static int getProtocolVersion(byte[] data)
80     throws SnmpStatusException {
81     int version = 0;
82     BerDecoder bdec = null;
83     try {
84         bdec = new BerDecoder(data);
85         bdec.openSequence();
86         version = bdec.fetchInteger();
87     }
88         catch(BerException x) {
89             throw new SnmpStatusException("Invalid encoding") ;
90         }
91     try {
92         bdec.closeSequence();
93     }
94         catch(BerException x) {
95         }
96     return version;
97     }
98
99     /**
100      * Returns the associated request ID.
101      * @param data The flat message.
102      * @return The request ID.
103      */

104     public abstract int getRequestId(byte[] data) throws SnmpStatusException;
105     
106     /**
107      * Encodes this message and puts the result in the specified byte array.
108      * For internal use only.
109      *
110      * @param outputBytes An array to receive the resulting encoding.
111      *
112      * @exception ArrayIndexOutOfBoundsException If the result does not fit
113      * into the specified array.
114      */

115     public abstract int encodeMessage(byte[] outputBytes)
116     throws SnmpTooBigException;
117
118      /**
119      * Decodes the specified bytes and initializes this message.
120      * For internal use only.
121      *
122      * @param inputBytes The bytes to be decoded.
123      *
124      * @exception SnmpStatusException If the specified bytes are not a valid encoding.
125      */

126     public abstract void decodeMessage(byte[] inputBytes, int byteCount)
127         throws SnmpStatusException;
128     
129      /**
130      * Initializes this message with the specified <CODE>pdu</CODE>.
131      * <P>
132      * This method initializes the data field with an array of
133      * <CODE>maxDataLength</CODE> bytes. It encodes the <CODE>pdu</CODE>.
134      * The resulting encoding is stored in the data field
135      * and the length of the encoding is stored in <CODE>dataLength</CODE>.
136      * <p>
137      * If the encoding length exceeds <CODE>maxDataLength</CODE>,
138      * the method throws an exception.
139      *
140      * @param pdu The PDU to be encoded.
141      * @param maxDataLength The maximum length permitted for the data field.
142      *
143      * @exception SnmpStatusException If the specified <CODE>pdu</CODE> is not valid.
144      * @exception SnmpTooBigException If the resulting encoding does not fit
145      * into <CODE>maxDataLength</CODE> bytes.
146      * @exception ArrayIndexOutOfBoundsException If the encoding exceeds <CODE>maxDataLength</CODE>.
147      */

148     public abstract void encodeSnmpPdu(SnmpPdu pdu, int maxDataLength)
149         throws SnmpStatusException, SnmpTooBigException;
150     
151
152     /**
153      * Gets the PDU encoded in this message.
154      * <P>
155      * This method decodes the data field and returns the resulting PDU.
156      *
157      * @return The resulting PDU.
158      * @exception SnmpStatusException If the encoding is not valid.
159      */

160     public abstract SnmpPdu decodeSnmpPdu()
161         throws SnmpStatusException;
162
163     /**
164      * Dumps the content of a byte buffer using hexadecimal form.
165      *
166      * @param b The buffer to dump.
167      * @param offset The position of the first byte to be dumped.
168      * @param len The number of bytes to be dumped starting from offset.
169      *
170      * @return The string containing the dump.
171      */

172     public static String JavaDoc dumpHexBuffer(byte [] b, int offset, int len) {
173         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(len << 1) ;
174         int k = 1 ;
175         int flen = offset + len ;
176  
177         for (int i = offset; i < flen ; i++) {
178             int j = b[i] & 0xFF ;
179             buf.append(Character.forDigit((j >>> 4) , 16)) ;
180             buf.append(Character.forDigit((j & 0x0F), 16)) ;
181             k++ ;
182             if (k%16 == 0) {
183                 buf.append('\n') ;
184                 k = 1 ;
185             } else
186                 buf.append(' ') ;
187         }
188         return buf.toString() ;
189     }
190
191     /**
192      * Dumps this message in a string.
193      *
194      * @return The string containing the dump.
195      */

196     public String JavaDoc printMessage() {
197         StringBuffer JavaDoc sb = new StringBuffer JavaDoc() ;
198         sb.append("Version: ") ;
199         sb.append(version) ;
200         sb.append("\n") ;
201         if (data == null) {
202             sb.append("Data: null") ;
203         }
204         else {
205             sb.append("Data: {\n") ;
206             sb.append(dumpHexBuffer(data, 0, dataLength)) ;
207             sb.append("\n}\n") ;
208         }
209
210         return sb.toString() ;
211     }
212
213     /**
214      * For SNMP Runtime private use only.
215      */

216     public void encodeVarBindList(BerEncoder benc,
217                   SnmpVarBind[] varBindList)
218         throws SnmpStatusException, SnmpTooBigException {
219         //
220
// Remember: the encoder does backward encoding
221
//
222
int encodedVarBindCount = 0 ;
223         try {
224             benc.openSequence() ;
225             if (varBindList != null) {
226                 for (int i = varBindList.length - 1 ; i >= 0 ; i--) {
227                     SnmpVarBind bind = varBindList[i] ;
228                     if (bind != null) {
229                         benc.openSequence() ;
230                         encodeVarBindValue(benc, bind.value) ;
231                         benc.putOid(bind.oid.longValue()) ;
232                         benc.closeSequence() ;
233                         encodedVarBindCount++ ;
234                     }
235                 }
236             }
237             benc.closeSequence() ;
238         }
239         catch(ArrayIndexOutOfBoundsException JavaDoc x) {
240             throw new SnmpTooBigException(encodedVarBindCount) ;
241         }
242     }
243   
244     /**
245      * For SNMP Runtime private use only.
246      */

247     void encodeVarBindValue(BerEncoder benc,
248                 SnmpValue v)throws SnmpStatusException {
249         if (v == null) {
250             benc.putNull() ;
251         }
252         else if (v instanceof SnmpIpAddress) {
253             benc.putOctetString(((SnmpIpAddress)v).byteValue(), SnmpValue.IpAddressTag) ;
254         }
255         else if (v instanceof SnmpCounter) {
256             benc.putInteger(((SnmpCounter)v).longValue(), SnmpValue.CounterTag) ;
257         }
258         else if (v instanceof SnmpGauge) {
259             benc.putInteger(((SnmpGauge)v).longValue(), SnmpValue.GaugeTag) ;
260         }
261         else if (v instanceof SnmpTimeticks) {
262             benc.putInteger(((SnmpTimeticks)v).longValue(), SnmpValue.TimeticksTag) ;
263         }
264         else if (v instanceof SnmpOpaque) {
265             benc.putOctetString(((SnmpOpaque)v).byteValue(), SnmpValue.OpaqueTag) ;
266         }
267         else if (v instanceof SnmpInt) {
268             benc.putInteger(((SnmpInt)v).intValue()) ;
269         }
270         else if (v instanceof SnmpString) {
271             benc.putOctetString(((SnmpString)v).byteValue()) ;
272         }
273         else if (v instanceof SnmpOid) {
274             benc.putOid(((SnmpOid)v).longValue()) ;
275         }
276         else if (v instanceof SnmpCounter64) {
277             if (version == snmpVersionOne) {
278                 throw new SnmpStatusException("Invalid value for SNMP v1 : " + v) ;
279             }
280             benc.putInteger(((SnmpCounter64)v).longValue(), SnmpValue.Counter64Tag) ;
281         }
282         else if (v instanceof SnmpNull) {
283             int tag = ((SnmpNull)v).getTag() ;
284             if ((version == snmpVersionOne) && (tag != SnmpValue.NullTag)) {
285                 throw new SnmpStatusException("Invalid value for SNMP v1 : " + v) ;
286             }
287             if ((version == snmpVersionTwo) &&
288                 (tag != SnmpValue.NullTag) &&
289                 (tag != SnmpVarBind.errNoSuchObjectTag) &&
290                 (tag != SnmpVarBind.errNoSuchInstanceTag) &&
291                 (tag != SnmpVarBind.errEndOfMibViewTag)) {
292                 throw new SnmpStatusException("Invalid value " + v) ;
293             }
294             benc.putNull(tag) ;
295         }
296         else {
297             throw new SnmpStatusException("Invalid value " + v) ;
298         }
299     
300     }
301
302
303     /**
304      * For SNMP Runtime private use only.
305      */

306     public SnmpVarBind[] decodeVarBindList(BerDecoder bdec)
307     throws BerException {
308         bdec.openSequence() ;
309         Vector JavaDoc tmp = new Vector JavaDoc() ;
310         while (bdec.cannotCloseSequence()) {
311         SnmpVarBind bind = new SnmpVarBind() ;
312         bdec.openSequence() ;
313         bind.oid = new SnmpOid(bdec.fetchOid()) ;
314         bind.setSnmpValue(decodeVarBindValue(bdec)) ;
315         bdec.closeSequence() ;
316         tmp.addElement(bind) ;
317         }
318         bdec.closeSequence() ;
319         SnmpVarBind[] varBindList= new SnmpVarBind[tmp.size()] ;
320         tmp.copyInto(varBindList);
321         return varBindList ;
322     }
323     
324     
325     /**
326      * For SNMP Runtime private use only.
327      */

328     SnmpValue decodeVarBindValue(BerDecoder bdec)
329     throws BerException {
330         SnmpValue result = null ;
331         int tag = bdec.getTag() ;
332
333     // bugId 4641696 : RuntimeExceptions must be transformed in
334
// BerException.
335
switch(tag) {
336         
337             //
338
// Simple syntax
339
//
340
case BerDecoder.IntegerTag :
341         try {
342         result = new SnmpInt(bdec.fetchInteger()) ;
343         } catch(RuntimeException JavaDoc r) {
344         throw new BerException();
345         // BerException("Can't build SnmpInt from decoded value.");
346
}
347             break ;
348         case BerDecoder.OctetStringTag :
349         try {
350         result = new SnmpString(bdec.fetchOctetString()) ;
351         } catch(RuntimeException JavaDoc r) {
352         throw new BerException();
353         // BerException("Can't build SnmpString from decoded value.");
354
}
355             break ;
356         case BerDecoder.OidTag :
357         try {
358         result = new SnmpOid(bdec.fetchOid()) ;
359         } catch(RuntimeException JavaDoc r) {
360         throw new BerException();
361         // BerException("Can't build SnmpOid from decoded value.");
362
}
363             break ;
364         case BerDecoder.NullTag :
365             bdec.fetchNull() ;
366         try {
367         result = new SnmpNull() ;
368         } catch(RuntimeException JavaDoc r) {
369         throw new BerException();
370         // BerException("Can't build SnmpNull from decoded value.");
371
}
372             break ;
373
374             //
375
// Application syntax
376
//
377
case SnmpValue.IpAddressTag :
378         try {
379         result = new SnmpIpAddress(bdec.fetchOctetString(tag)) ;
380         } catch (RuntimeException JavaDoc r) {
381         throw new BerException();
382           // BerException("Can't build SnmpIpAddress from decoded value.");
383
}
384             break ;
385         case SnmpValue.CounterTag :
386         try {
387         result = new SnmpCounter(bdec.fetchIntegerAsLong(tag)) ;
388         } catch(RuntimeException JavaDoc r) {
389         throw new BerException();
390         // BerException("Can't build SnmpCounter from decoded value.");
391
}
392             break ;
393         case SnmpValue.GaugeTag :
394         try {
395         result = new SnmpGauge(bdec.fetchIntegerAsLong(tag)) ;
396         } catch(RuntimeException JavaDoc r) {
397         throw new BerException();
398         // BerException("Can't build SnmpGauge from decoded value.");
399
}
400             break ;
401         case SnmpValue.TimeticksTag :
402         try {
403         result = new SnmpTimeticks(bdec.fetchIntegerAsLong(tag)) ;
404         } catch(RuntimeException JavaDoc r) {
405         throw new BerException();
406          // BerException("Can't build SnmpTimeticks from decoded value.");
407
}
408             break ;
409         case SnmpValue.OpaqueTag :
410         try {
411         result = new SnmpOpaque(bdec.fetchOctetString(tag)) ;
412         } catch(RuntimeException JavaDoc r) {
413         throw new BerException();
414         // BerException("Can't build SnmpOpaque from decoded value.");
415
}
416             break ;
417       
418             //
419
// V2 syntaxes
420
//
421
case SnmpValue.Counter64Tag :
422             if (version == snmpVersionOne) {
423                 throw new BerException(BerException.BAD_VERSION) ;
424             }
425         try {
426         result = new SnmpCounter64(bdec.fetchIntegerAsLong(tag)) ;
427         } catch(RuntimeException JavaDoc r) {
428         throw new BerException();
429          // BerException("Can't build SnmpCounter64 from decoded value.");
430
}
431             break ;
432     
433         case SnmpVarBind.errNoSuchObjectTag :
434             if (version == snmpVersionOne) {
435                 throw new BerException(BerException.BAD_VERSION) ;
436             }
437             bdec.fetchNull(tag) ;
438             result = SnmpVarBind.noSuchObject ;
439             break ;
440         
441         case SnmpVarBind.errNoSuchInstanceTag :
442             if (version == snmpVersionOne) {
443                 throw new BerException(BerException.BAD_VERSION) ;
444             }
445             bdec.fetchNull(tag) ;
446             result = SnmpVarBind.noSuchInstance ;
447             break ;
448         
449         case SnmpVarBind.errEndOfMibViewTag :
450             if (version == snmpVersionOne) {
451                 throw new BerException(BerException.BAD_VERSION) ;
452             }
453             bdec.fetchNull(tag) ;
454             result = SnmpVarBind.endOfMibView ;
455             break ;
456         
457         default:
458             throw new BerException() ;
459
460         }
461     
462         return result ;
463     }
464
465 }
466
Popular Tags