PageRenderTime 71ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jre-1.6.0/src/com/sun/jmx/snmp/SnmpMsg.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 465 lines | 273 code | 36 blank | 156 comment | 58 complexity | af063e04db86bed01b623852cf31eb33 MD5 | raw file
  1. /*
  2. * %Z%file %M%
  3. * %Z%author Sun Microsystems, Inc.
  4. * %Z%version %I%
  5. * %Z%date %D%
  6. *
  7. * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
  8. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  9. *
  10. */
  11. package com.sun.jmx.snmp;
  12. import com.sun.jmx.snmp.SnmpSecurityParameters;
  13. // java imports
  14. //
  15. import java.util.Vector;
  16. import java.net.InetAddress;
  17. // import debug stuff
  18. //
  19. import com.sun.jmx.trace.Trace;
  20. import com.sun.jmx.snmp.SnmpStatusException;
  21. /**
  22. * A partially decoded representation of an SNMP packet. It contains
  23. * the information contained in any SNMP message (SNMPv1, SNMPv2 or
  24. * SNMPv3).
  25. * <p><b>This API is a Sun Microsystems internal API and is subject
  26. * to change without notice.</b></p>
  27. * @since 1.5
  28. */
  29. public abstract class SnmpMsg implements SnmpDefinitions {
  30. /**
  31. * The protocol version.
  32. * <P><CODE>decodeMessage</CODE> and <CODE>encodeMessage</CODE> do not
  33. * perform any check on this value.
  34. * <BR><CODE>decodeSnmpPdu</CODE> and <CODE>encodeSnmpPdu</CODE> only
  35. * accept the values 0 (for SNMPv1), 1 (for SNMPv2) and 3 (for SNMPv3).
  36. */
  37. public int version = 0;
  38. /**
  39. * Encoding of the PDU.
  40. * <P>This is usually the BER encoding of the PDU's syntax
  41. * defined in RFC1157 and RFC1902. However, this can be authenticated
  42. * or encrypted data (but you need to implemented your own
  43. * <CODE>SnmpPduFactory</CODE> class).
  44. */
  45. public byte[] data = null;
  46. /**
  47. * Number of useful bytes in the <CODE>data</CODE> field.
  48. */
  49. public int dataLength = 0;
  50. /**
  51. * Source or destination address.
  52. * <BR>For an incoming message it's the source.
  53. * For an outgoing message it's the destination.
  54. */
  55. public InetAddress address = null;
  56. /**
  57. * Source or destination port.
  58. * <BR>For an incoming message it's the source.
  59. * For an outgoing message it's the destination.
  60. */
  61. public int port = 0;
  62. /**
  63. * Security parameters. Contain informations according to Security Model (Usm, community string based, ...).
  64. */
  65. public SnmpSecurityParameters securityParameters = null;
  66. /**
  67. * Returns the encoded SNMP version present in the passed byte array.
  68. * @param data The unmarshalled SNMP message.
  69. * @return The SNMP version (0, 1 or 3).
  70. */
  71. public static int getProtocolVersion(byte[] data)
  72. throws SnmpStatusException {
  73. int version = 0;
  74. BerDecoder bdec = null;
  75. try {
  76. bdec = new BerDecoder(data);
  77. bdec.openSequence();
  78. version = bdec.fetchInteger();
  79. }
  80. catch(BerException x) {
  81. throw new SnmpStatusException("Invalid encoding") ;
  82. }
  83. try {
  84. bdec.closeSequence();
  85. }
  86. catch(BerException x) {
  87. }
  88. return version;
  89. }
  90. /**
  91. * Returns the associated request ID.
  92. * @param data The flat message.
  93. * @return The request ID.
  94. */
  95. public abstract int getRequestId(byte[] data) throws SnmpStatusException;
  96. /**
  97. * Encodes this message and puts the result in the specified byte array.
  98. * For internal use only.
  99. *
  100. * @param outputBytes An array to receive the resulting encoding.
  101. *
  102. * @exception ArrayIndexOutOfBoundsException If the result does not fit
  103. * into the specified array.
  104. */
  105. public abstract int encodeMessage(byte[] outputBytes)
  106. throws SnmpTooBigException;
  107. /**
  108. * Decodes the specified bytes and initializes this message.
  109. * For internal use only.
  110. *
  111. * @param inputBytes The bytes to be decoded.
  112. *
  113. * @exception SnmpStatusException If the specified bytes are not a valid encoding.
  114. */
  115. public abstract void decodeMessage(byte[] inputBytes, int byteCount)
  116. throws SnmpStatusException;
  117. /**
  118. * Initializes this message with the specified <CODE>pdu</CODE>.
  119. * <P>
  120. * This method initializes the data field with an array of
  121. * <CODE>maxDataLength</CODE> bytes. It encodes the <CODE>pdu</CODE>.
  122. * The resulting encoding is stored in the data field
  123. * and the length of the encoding is stored in <CODE>dataLength</CODE>.
  124. * <p>
  125. * If the encoding length exceeds <CODE>maxDataLength</CODE>,
  126. * the method throws an exception.
  127. *
  128. * @param pdu The PDU to be encoded.
  129. * @param maxDataLength The maximum length permitted for the data field.
  130. *
  131. * @exception SnmpStatusException If the specified <CODE>pdu</CODE> is not valid.
  132. * @exception SnmpTooBigException If the resulting encoding does not fit
  133. * into <CODE>maxDataLength</CODE> bytes.
  134. * @exception ArrayIndexOutOfBoundsException If the encoding exceeds <CODE>maxDataLength</CODE>.
  135. */
  136. public abstract void encodeSnmpPdu(SnmpPdu pdu, int maxDataLength)
  137. throws SnmpStatusException, SnmpTooBigException;
  138. /**
  139. * Gets the PDU encoded in this message.
  140. * <P>
  141. * This method decodes the data field and returns the resulting PDU.
  142. *
  143. * @return The resulting PDU.
  144. * @exception SnmpStatusException If the encoding is not valid.
  145. */
  146. public abstract SnmpPdu decodeSnmpPdu()
  147. throws SnmpStatusException;
  148. /**
  149. * Dumps the content of a byte buffer using hexadecimal form.
  150. *
  151. * @param b The buffer to dump.
  152. * @param offset The position of the first byte to be dumped.
  153. * @param len The number of bytes to be dumped starting from offset.
  154. *
  155. * @return The string containing the dump.
  156. */
  157. public static String dumpHexBuffer(byte [] b, int offset, int len) {
  158. StringBuffer buf = new StringBuffer(len << 1) ;
  159. int k = 1 ;
  160. int flen = offset + len ;
  161. for (int i = offset; i < flen ; i++) {
  162. int j = b[i] & 0xFF ;
  163. buf.append(Character.forDigit((j >>> 4) , 16)) ;
  164. buf.append(Character.forDigit((j & 0x0F), 16)) ;
  165. k++ ;
  166. if (k%16 == 0) {
  167. buf.append('\n') ;
  168. k = 1 ;
  169. } else
  170. buf.append(' ') ;
  171. }
  172. return buf.toString() ;
  173. }
  174. /**
  175. * Dumps this message in a string.
  176. *
  177. * @return The string containing the dump.
  178. */
  179. public String printMessage() {
  180. StringBuffer sb = new StringBuffer() ;
  181. sb.append("Version: ") ;
  182. sb.append(version) ;
  183. sb.append("\n") ;
  184. if (data == null) {
  185. sb.append("Data: null") ;
  186. }
  187. else {
  188. sb.append("Data: {\n") ;
  189. sb.append(dumpHexBuffer(data, 0, dataLength)) ;
  190. sb.append("\n}\n") ;
  191. }
  192. return sb.toString() ;
  193. }
  194. /**
  195. * For SNMP Runtime private use only.
  196. */
  197. public void encodeVarBindList(BerEncoder benc,
  198. SnmpVarBind[] varBindList)
  199. throws SnmpStatusException, SnmpTooBigException {
  200. //
  201. // Remember: the encoder does backward encoding
  202. //
  203. int encodedVarBindCount = 0 ;
  204. try {
  205. benc.openSequence() ;
  206. if (varBindList != null) {
  207. for (int i = varBindList.length - 1 ; i >= 0 ; i--) {
  208. SnmpVarBind bind = varBindList[i] ;
  209. if (bind != null) {
  210. benc.openSequence() ;
  211. encodeVarBindValue(benc, bind.value) ;
  212. benc.putOid(bind.oid.longValue()) ;
  213. benc.closeSequence() ;
  214. encodedVarBindCount++ ;
  215. }
  216. }
  217. }
  218. benc.closeSequence() ;
  219. }
  220. catch(ArrayIndexOutOfBoundsException x) {
  221. throw new SnmpTooBigException(encodedVarBindCount) ;
  222. }
  223. }
  224. /**
  225. * For SNMP Runtime private use only.
  226. */
  227. void encodeVarBindValue(BerEncoder benc,
  228. SnmpValue v)throws SnmpStatusException {
  229. if (v == null) {
  230. benc.putNull() ;
  231. }
  232. else if (v instanceof SnmpIpAddress) {
  233. benc.putOctetString(((SnmpIpAddress)v).byteValue(), SnmpValue.IpAddressTag) ;
  234. }
  235. else if (v instanceof SnmpCounter) {
  236. benc.putInteger(((SnmpCounter)v).longValue(), SnmpValue.CounterTag) ;
  237. }
  238. else if (v instanceof SnmpGauge) {
  239. benc.putInteger(((SnmpGauge)v).longValue(), SnmpValue.GaugeTag) ;
  240. }
  241. else if (v instanceof SnmpTimeticks) {
  242. benc.putInteger(((SnmpTimeticks)v).longValue(), SnmpValue.TimeticksTag) ;
  243. }
  244. else if (v instanceof SnmpOpaque) {
  245. benc.putOctetString(((SnmpOpaque)v).byteValue(), SnmpValue.OpaqueTag) ;
  246. }
  247. else if (v instanceof SnmpInt) {
  248. benc.putInteger(((SnmpInt)v).intValue()) ;
  249. }
  250. else if (v instanceof SnmpString) {
  251. benc.putOctetString(((SnmpString)v).byteValue()) ;
  252. }
  253. else if (v instanceof SnmpOid) {
  254. benc.putOid(((SnmpOid)v).longValue()) ;
  255. }
  256. else if (v instanceof SnmpCounter64) {
  257. if (version == snmpVersionOne) {
  258. throw new SnmpStatusException("Invalid value for SNMP v1 : " + v) ;
  259. }
  260. benc.putInteger(((SnmpCounter64)v).longValue(), SnmpValue.Counter64Tag) ;
  261. }
  262. else if (v instanceof SnmpNull) {
  263. int tag = ((SnmpNull)v).getTag() ;
  264. if ((version == snmpVersionOne) && (tag != SnmpValue.NullTag)) {
  265. throw new SnmpStatusException("Invalid value for SNMP v1 : " + v) ;
  266. }
  267. if ((version == snmpVersionTwo) &&
  268. (tag != SnmpValue.NullTag) &&
  269. (tag != SnmpVarBind.errNoSuchObjectTag) &&
  270. (tag != SnmpVarBind.errNoSuchInstanceTag) &&
  271. (tag != SnmpVarBind.errEndOfMibViewTag)) {
  272. throw new SnmpStatusException("Invalid value " + v) ;
  273. }
  274. benc.putNull(tag) ;
  275. }
  276. else {
  277. throw new SnmpStatusException("Invalid value " + v) ;
  278. }
  279. }
  280. /**
  281. * For SNMP Runtime private use only.
  282. */
  283. public SnmpVarBind[] decodeVarBindList(BerDecoder bdec)
  284. throws BerException {
  285. bdec.openSequence() ;
  286. Vector tmp = new Vector() ;
  287. while (bdec.cannotCloseSequence()) {
  288. SnmpVarBind bind = new SnmpVarBind() ;
  289. bdec.openSequence() ;
  290. bind.oid = new SnmpOid(bdec.fetchOid()) ;
  291. bind.setSnmpValue(decodeVarBindValue(bdec)) ;
  292. bdec.closeSequence() ;
  293. tmp.addElement(bind) ;
  294. }
  295. bdec.closeSequence() ;
  296. SnmpVarBind[] varBindList= new SnmpVarBind[tmp.size()] ;
  297. tmp.copyInto(varBindList);
  298. return varBindList ;
  299. }
  300. /**
  301. * For SNMP Runtime private use only.
  302. */
  303. SnmpValue decodeVarBindValue(BerDecoder bdec)
  304. throws BerException {
  305. SnmpValue result = null ;
  306. int tag = bdec.getTag() ;
  307. // bugId 4641696 : RuntimeExceptions must be transformed in
  308. // BerException.
  309. switch(tag) {
  310. //
  311. // Simple syntax
  312. //
  313. case BerDecoder.IntegerTag :
  314. try {
  315. result = new SnmpInt(bdec.fetchInteger()) ;
  316. } catch(RuntimeException r) {
  317. throw new BerException();
  318. // BerException("Can't build SnmpInt from decoded value.");
  319. }
  320. break ;
  321. case BerDecoder.OctetStringTag :
  322. try {
  323. result = new SnmpString(bdec.fetchOctetString()) ;
  324. } catch(RuntimeException r) {
  325. throw new BerException();
  326. // BerException("Can't build SnmpString from decoded value.");
  327. }
  328. break ;
  329. case BerDecoder.OidTag :
  330. try {
  331. result = new SnmpOid(bdec.fetchOid()) ;
  332. } catch(RuntimeException r) {
  333. throw new BerException();
  334. // BerException("Can't build SnmpOid from decoded value.");
  335. }
  336. break ;
  337. case BerDecoder.NullTag :
  338. bdec.fetchNull() ;
  339. try {
  340. result = new SnmpNull() ;
  341. } catch(RuntimeException r) {
  342. throw new BerException();
  343. // BerException("Can't build SnmpNull from decoded value.");
  344. }
  345. break ;
  346. //
  347. // Application syntax
  348. //
  349. case SnmpValue.IpAddressTag :
  350. try {
  351. result = new SnmpIpAddress(bdec.fetchOctetString(tag)) ;
  352. } catch (RuntimeException r) {
  353. throw new BerException();
  354. // BerException("Can't build SnmpIpAddress from decoded value.");
  355. }
  356. break ;
  357. case SnmpValue.CounterTag :
  358. try {
  359. result = new SnmpCounter(bdec.fetchIntegerAsLong(tag)) ;
  360. } catch(RuntimeException r) {
  361. throw new BerException();
  362. // BerException("Can't build SnmpCounter from decoded value.");
  363. }
  364. break ;
  365. case SnmpValue.GaugeTag :
  366. try {
  367. result = new SnmpGauge(bdec.fetchIntegerAsLong(tag)) ;
  368. } catch(RuntimeException r) {
  369. throw new BerException();
  370. // BerException("Can't build SnmpGauge from decoded value.");
  371. }
  372. break ;
  373. case SnmpValue.TimeticksTag :
  374. try {
  375. result = new SnmpTimeticks(bdec.fetchIntegerAsLong(tag)) ;
  376. } catch(RuntimeException r) {
  377. throw new BerException();
  378. // BerException("Can't build SnmpTimeticks from decoded value.");
  379. }
  380. break ;
  381. case SnmpValue.OpaqueTag :
  382. try {
  383. result = new SnmpOpaque(bdec.fetchOctetString(tag)) ;
  384. } catch(RuntimeException r) {
  385. throw new BerException();
  386. // BerException("Can't build SnmpOpaque from decoded value.");
  387. }
  388. break ;
  389. //
  390. // V2 syntaxes
  391. //
  392. case SnmpValue.Counter64Tag :
  393. if (version == snmpVersionOne) {
  394. throw new BerException(BerException.BAD_VERSION) ;
  395. }
  396. try {
  397. result = new SnmpCounter64(bdec.fetchIntegerAsLong(tag)) ;
  398. } catch(RuntimeException r) {
  399. throw new BerException();
  400. // BerException("Can't build SnmpCounter64 from decoded value.");
  401. }
  402. break ;
  403. case SnmpVarBind.errNoSuchObjectTag :
  404. if (version == snmpVersionOne) {
  405. throw new BerException(BerException.BAD_VERSION) ;
  406. }
  407. bdec.fetchNull(tag) ;
  408. result = SnmpVarBind.noSuchObject ;
  409. break ;
  410. case SnmpVarBind.errNoSuchInstanceTag :
  411. if (version == snmpVersionOne) {
  412. throw new BerException(BerException.BAD_VERSION) ;
  413. }
  414. bdec.fetchNull(tag) ;
  415. result = SnmpVarBind.noSuchInstance ;
  416. break ;
  417. case SnmpVarBind.errEndOfMibViewTag :
  418. if (version == snmpVersionOne) {
  419. throw new BerException(BerException.BAD_VERSION) ;
  420. }
  421. bdec.fetchNull(tag) ;
  422. result = SnmpVarBind.endOfMibView ;
  423. break ;
  424. default:
  425. throw new BerException() ;
  426. }
  427. return result ;
  428. }
  429. }