Source for file rfc1905.php

Documentation is available at rfc1905.php

  1. <?php
  2. /**
  3. * phpsnmp - a PHP SNMP library
  4. *
  5. * Copyright (C) 2004 David Eder <david@eder,us>
  6. *
  7. * Based on snmp - a Python SNMP library
  8. * Copyright (C) 2003 Unicity Pty Ltd <libsnmp@unicity.com.au>
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * @author David Eder <david@eder.us>
  25. * @copyright 2004 David Eder
  26. * @package phpSNMP
  27. * @subpackage rfc1905
  28. * @version .7
  29. */
  30.  
  31. /**
  32. */
  33. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'rfc1155.php');
  34. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'rfc1157.php');
  35. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'rfc1902.php');
  36.  
  37. define('MAX_BINDINGS', 2147483647);
  38.  
  39. define('ASN_TAG_GETBULK', 0x05);
  40. define('ASN_TAG_INFORM', 0x06);
  41. define('ASN_TAG_TRAPV2', 0x07);
  42. define('ASN_TAG_REPORT', 0x08);
  43.  
  44. $ASN_TAG_DICT[0xa2] = 'rfc1905_Response';
  45. $ASN_TAG_DICT[0xa5] = 'rfc1905_GetBulk';
  46. $ASN_TAG_DICT[0xa6] = 'rfc1905_Inform';
  47. $ASN_TAG_DICT[0xa7] = 'rfc1905_TrapV2';
  48. $ASN_TAG_DICT[0xa8] = 'rfc1905_Report';
  49.  
  50. // ucd-snmp returns context-specific values at time
  51. define('ASN_TAG_NO_SUCH_OBJECT', 0x80);
  52. define('ASN_TAG_NO_SUCH_INSTANCE', 0x81);
  53. define('ASN_TAG_END_OF_MIB_VIEW', 0x82);
  54. $ASN_TAG_DICT[0x80] = 'rfc1905_NoSuchObject';
  55. $ASN_TAG_DICT[0x81] = 'rfc1905_NoSuchInstance';
  56. $ASN_TAG_DICT[0x82] = 'rfc1905_EndOfMibView';
  57.  
  58. /**
  59. * No Such Object
  60. *
  61. * This is a special type for ucd-snmp
  62. *
  63. * @package phpSNMP
  64. * @subpackage rfc1905
  65. */
  66. class rfc1905_NoSuchObject extends rfc1155_Null
  67. {
  68. /**
  69. * Constructor
  70. */
  71. function rfc1905_NoSuchObject()
  72. {
  73. parent::rfc1155_Null();
  74. $this->asnTagNumber = ASN_TAG_NO_SUCH_OBJECT;
  75. }
  76.  
  77. /**
  78. * ToString
  79. *
  80. * @return string value of this object
  81. */
  82. function toString()
  83. {
  84. return 'No such Object';
  85. }
  86. }
  87.  
  88. /**
  89. * No Such Instance
  90. *
  91. * This is a special type for ucd-snmp
  92. *
  93. * @package phpSNMP
  94. * @subpackage rfc1905
  95. */
  96. class rfc1905_NoSuchInstance extends rfc1155_Null
  97. {
  98. /**
  99. * Constructor
  100. */
  101. function rfc1905_NoSuchInstance()
  102. {
  103. parent::rfc1155_Null();
  104. $this->asnTagNumber = ASN_TAG_NO_SUCH_INSTANCE;
  105. }
  106.  
  107. /**
  108. * ToString
  109. *
  110. * @return string value of this object
  111. */
  112. function toString()
  113. {
  114. return 'No such Instance';
  115. }
  116. }
  117.  
  118. /**
  119. * End Of MIB View
  120. *
  121. * This is a special type for ucd-snmp
  122. *
  123. * @package phpSNMP
  124. * @subpackage rfc1905
  125. */
  126. class rfc1905_EndOfMibView extends rfc1155_Null
  127. {
  128. /**
  129. * Constructor
  130. */
  131. function rfc1905_EndOfMibView()
  132. {
  133. parent::rfc1155_Null();
  134. $this->asnTagNumber = ASN_TAG_END_OF_MIB_VIEW;
  135. }
  136.  
  137. /**
  138. * ToString
  139. *
  140. * @return string value of this object
  141. */
  142. function toString()
  143. {
  144. return 'End of MIB';
  145. }
  146. }
  147.  
  148. /**
  149. * Variable Binding List
  150. *
  151. * An SNMPv2 VarBindList has a maximum size of MAX_BINDINGS
  152. *
  153. * @package phpSNMP
  154. * @subpackage rfc1905
  155. */
  156. class rfc1905_VarBindList extends rfc1157_VarBindList
  157. {
  158. /**
  159. * Constructor
  160. *
  161. * @param array $value
  162. */
  163. function rfc1905_VarBindList($value=array())
  164. {
  165. if(count($value) > MAX_BINDINGS)
  166. trigger_error('A VarBindList must be shorter than ' . MAX_BINDINGS, E_USER_WARNING);
  167. parent::rfc1157_VarBindList($value);
  168. }
  169. }
  170.  
  171. /**
  172. * Message
  173. *
  174. * @package phpSNMP
  175. * @subpackage rfc1905
  176. */
  177. class rfc1905_Message extends rfc1157_Message
  178. {
  179. /**
  180. * Constructor
  181. *
  182. * @param integer $version
  183. * @param string $community
  184. * @param mixed $data
  185. */
  186. function rfc1905_Message($version=1, $community='public', $data=NULL)
  187. {
  188. parent::rfc1157_Message($version, $community, $data);
  189. }
  190. }
  191.  
  192. /**
  193. * Error Status
  194. *
  195. * An SNMPv2 Error status
  196. *
  197. * @package phpSNMP
  198. * @subpackage rfc1905
  199. */
  200. class rfc1905_ErrorStatus extends rfc1157_ErrorStatus
  201. {
  202. /**
  203. * Constructor
  204. *
  205. * @param integer $value
  206. */
  207. function rfc1905_ErrorStatus($value)
  208. {
  209. parent::rfc1157_ErrorStatus($value);
  210. }
  211.  
  212. /**
  213. * ToString
  214. *
  215. * @return string value of this object
  216. */
  217. function toString()
  218. {
  219. switch($this->value)
  220. {
  221. case 6: return 'Access is not permitted';
  222. case 7: return 'Type is incorrect';
  223. case 8: return 'Length is incorrect';
  224. case 9: return 'Encoding is incorrect';
  225. case 10: return 'Value is incorrect';
  226. case 11: return 'No creation';
  227. case 12: return 'Value is inconsistent';
  228. case 13: return 'Resourse Unavailable';
  229. case 14: return 'Commit Failed';
  230. case 15: return 'Undo Failed';
  231. case 16: return 'Authorization Error';
  232. case 17: return 'Not Writable';
  233. case 18: return 'Inconsistent Name';
  234. }
  235. return parent::toString();
  236. }
  237. }
  238.  
  239. /**
  240. * PDU
  241. *
  242. * SNMPv2 PDUs are very similar to SNMPv1 PDUs
  243. *
  244. * @package phpSNMP
  245. * @subpackage rfc1905
  246. */
  247. class rfc1905_PDU extends rfc1157_PDU
  248. {
  249. /**
  250. * Constructor
  251. *
  252. * @param integer $requestID
  253. * @param integer $errorStatus
  254. * @param integer $errorIndex
  255. * @param array $varBindList
  256. */
  257. function rfc1905_PDU($requestID=0, $errorStatus=0, $errorIndex=0, $varBindList=array())
  258. {
  259. parent::rfc1157_PDU();
  260. $this->asnTagClass = ASN_TAG_CLASS_CONTEXT;
  261.  
  262. if($errorIndex > MAX_BINDINGS)
  263. trigger_error('errorIndex must be <= ' . MAX_BINDINGS, E_USER_WARNING);
  264.  
  265. $this->value = array(new rfc1902_Integer32($requestID), new rfc1905_ErrorStatus($errorStatus),
  266. new rfc1155_Integer($errorIndex), new rfc1905_VarBindList($varBindList));
  267. }
  268.  
  269. /**
  270. * ToString
  271. *
  272. * @return string value of this object
  273. */
  274. function toString()
  275. {
  276. $req = $this->value[0]->toString();
  277. $err = $this->value[1]->tostring();
  278. $ei = $this->value[2]->toString();
  279. $vb = $this->value[3]->toString();
  280. return "rfc1905_PDU(RequestID:$req,ErrorStatus:$err,ErrorIndex:$ei,VarBind:$vb)";
  281. }
  282. }
  283.  
  284. /**
  285. * Bulk PDU
  286. *
  287. * BulkPDU is a new type of PDU specifically for doing GetBulk requests in SNMPv2.
  288. *
  289. * @package phpSNMP
  290. * @subpackage rfc1905
  291. */
  292. class rfc1905_BulkPDU extends rfc1155_Sequence
  293. {
  294. /**
  295. * Constructor
  296. *
  297. * @param integer $requestID
  298. * @param integer $nonRepeaters
  299. * @param integer $maxRepetitions
  300. * @param array $varBindList
  301. */
  302. function rfc1905_BulkPDU($requestID=0, $nonRepeaters=0, $maxRepetitions=0, $varBindList=array())
  303. {
  304. parent::rfc1155_Sequence();
  305. $this->asnTagClass = ASN_TAG_CLASS_CONTEXT;
  306.  
  307. if($nonRepeaters > MAX_BINDINGS)
  308. trigger_error('nonRepeaters must be <= ' . MAX_BINDINGS, E_USER_WARNING);
  309. if($maxRepetitions > MAX_BINDINGS)
  310. trigger_error('maxRepetitions must be <= ' . MAX_BINDINGS, E_USER_WARNING);
  311.  
  312. $this->value = array(new rfc1902_Integer32($requestID), new rfc1155_Integer($nonRepeaters),
  313. new rfc1155_Integer($maxRepetitions), new rfc1905_VarBindList($varBindList));
  314. }
  315.  
  316. /**
  317. * Get/Set Request ID
  318. *
  319. * @param integer $value
  320. * @return integer
  321. */
  322. function requestID($value=NULL)
  323. {
  324. if(!is_null($value)) $this->value[0]->value = $value;
  325. return $this->value[0]->value;
  326. }
  327.  
  328. /**
  329. * Get/Set Non Repeaters
  330. *
  331. * @param integer $value
  332. * @return integer
  333. */
  334. function nonRepeaters($value=NULL)
  335. {
  336. if(!is_null($value)) $this->value[1]->value = $value;
  337. return $this->value[1]->value;
  338. }
  339.  
  340. /**
  341. * Get/Set Max Repetitions
  342. *
  343. * @param integer $value
  344. * @return integer
  345. */
  346. function maxRepetitions($value=NULL)
  347. {
  348. if(!is_null($value)) $this->value[2]->value = $value;
  349. return $this->value[2]->value;
  350. }
  351.  
  352. /**
  353. * Get/Set Var Bind List
  354. *
  355. * @param rfc1905_VarBindList $value
  356. * @return rfc1905_VarBindList
  357. */
  358. function varBindList($value=NULL)
  359. {
  360. if(!is_null($value)) $this->value[3]->value = $value;
  361. return $this->value[3]->value;
  362. }
  363.  
  364. /**
  365. * Decode Contents
  366. *
  367. * Decode octet stream
  368. *
  369. * @param string $stream
  370. * @return rfc1905_BulkPDU
  371. */
  372. function decodeContents($stream) // Decode into a BulkPDU object
  373. {
  374. parent::decodeContents($stream);
  375. if(count($this->value) != 4)
  376. trigger_error('Malformed BulkPDU: Incorrect length ' . count($this->value), E_USER_WARNING);
  377. return $this;
  378. }
  379. }
  380.  
  381. /**
  382. * Get Request
  383. *
  384. * An SNMPv2 Get Request PDU
  385. *
  386. * @package phpSNMP
  387. * @subpackage rfc1905
  388. */
  389. class rfc1905_Get extends rfc1905_PDU
  390. {
  391. /**
  392. * Constructor
  393. *
  394. * @param integer $requestID
  395. * @param integer $errorStatus
  396. * @param integer $errorIndex
  397. * @param array $varBindList
  398. */
  399. function rfc1905_Get($requestID=0, $errorStatus=0, $errorIndex=0, $varBindList=array())
  400. {
  401. parent::rfc1905_PDU($requestID, $errorStatus, $errorIndex, $varBindList);
  402. $this->asnTagNumber = ASN_TAG_GET;
  403. }
  404. }
  405.  
  406. /**
  407. * Get Next Request
  408. *
  409. * An SNMPv2 Get Next Request PDU
  410. *
  411. * @package phpSNMP
  412. * @subpackage rfc1905
  413. */
  414. class rfc1905_GetNext extends rfc1905_PDU
  415. {
  416. /**
  417. * Constructor
  418. *
  419. * @param integer $requestID
  420. * @param integer $errorStatus
  421. * @param integer $errorIndex
  422. * @param array $varBindList
  423. */
  424. function rfc1905_GetNext($requestID=0, $errorStatus=0, $errorIndex=0, $varBindList=array())
  425. {
  426. parent::rfc1905_PDU($requestID, $errorStatus, $errorIndex, $varBindList);
  427. $this->asnTagNumber = ASN_TAG_GETNEXT;
  428. }
  429. }
  430.  
  431. /**
  432. * Response
  433. *
  434. * An SNMPv2 Response PDU
  435. *
  436. * @package phpSNMP
  437. * @subpackage rfc1905
  438. */
  439. class rfc1905_Response extends rfc1905_PDU
  440. {
  441. /**
  442. * Constructor
  443. *
  444. * @param integer $requestID
  445. * @param integer $errorStatus
  446. * @param integer $errorIndex
  447. * @param array $varBindList
  448. */
  449. function rfc1905_Response($requestID=0, $errorStatus=0, $errorIndex=0, $varBindList=array())
  450. {
  451. parent::rfc1905_PDU($requestID, $errorStatus, $errorIndex, $varBindList);
  452. $this->asnTagNumber = ASN_TAG_RESPONSE;
  453. }
  454. }
  455.  
  456. /**
  457. * Set Request
  458. *
  459. * An SNMPv2 set Request PDU
  460. *
  461. * @package phpSNMP
  462. * @subpackage rfc1905
  463. */
  464. class rfc1905_Set extends rfc1905_PDU
  465. {
  466. /**
  467. * Constructor
  468. *
  469. * @param integer $requestID
  470. * @param integer $errorStatus
  471. * @param integer $errorIndex
  472. * @param array $varBindList
  473. */
  474. function rfc1905_Set($requestID=0, $errorStatus=0, $errorIndex=0, $varBindList=array())
  475. {
  476. parent::rfc1905_PDU($requestID, $errorStatus, $errorIndex, $varBindList);
  477. $this->asnTagNumber = ASN_TAG_SET;
  478. }
  479. }
  480.  
  481. /**
  482. * Get Bulk Request
  483. *
  484. * An SNMPv2 Get Bulk Request PDU
  485. *
  486. * @package phpSNMP
  487. * @subpackage rfc1905
  488. */
  489. class rfc1905_GetBulk extends rfc1905_BulkPDU
  490. {
  491. /**
  492. * Constructor
  493. *
  494. * @param integer $requestID
  495. * @param integer $nonRepeaters
  496. * @param integer $maxRepetitions
  497. * @param array $varBindList
  498. */
  499. function rfc1905_GetBulk($requestID=0, $nonRepeaters=0, $maxRepetitions=0, $varBindList=array())
  500. {
  501. parent::rfc1905_BulkPDU($requestID, $nonRepeaters, $maxRepetitions, $varBindList);
  502. $this->asnTagNumber = ASN_TAG_GETBULK;
  503. }
  504. }
  505.  
  506. /**
  507. * Inform
  508. *
  509. * An SNMPv2 Inform PDU
  510. *
  511. * @package phpSNMP
  512. * @subpackage rfc1905
  513. */
  514. class rfc1905_Inform extends rfc1905_PDU
  515. {
  516. /**
  517. * Constructor
  518. *
  519. * @param integer $requestID
  520. * @param integer $errorStatus
  521. * @param integer $errorIndex
  522. * @param array $varBindList
  523. */
  524. function rfc1905_Inform($requestID=0, $errorStatus=0, $errorIndex=0, $varBindList=array())
  525. {
  526. parent::rfc1905_PDU($requestID, $errorStatus, $errorIndex, $varBindList);
  527. $this->asnTagNumber = ASN_TAG_INFORM;
  528. }
  529. }
  530.  
  531. /**
  532. * Trap
  533. *
  534. * An SNMPv2 Trap PDU
  535. *
  536. * @package phpSNMP
  537. * @subpackage rfc1905
  538. */
  539. class rfc1905_Trap extends rfc1905_PDU
  540. {
  541. /**
  542. * Constructor
  543. *
  544. * @param integer $requestID
  545. * @param integer $errorStatus
  546. * @param integer $errorIndex
  547. * @param array $varBindList
  548. */
  549. function rfc1905_Trap($requestID=0, $errorStatus=0, $errorIndex=0, $varBindList=array())
  550. {
  551. parent::rfc1905_PDU($requestID, $errorStatus, $errorIndex, $varBindList);
  552. $this->asnTagNumber = ASN_TAG_TRAPV2;
  553. }
  554. }
  555.  
  556. /**
  557. * Report
  558. *
  559. * An SNMPv2 Report PDU
  560. *
  561. * @package phpSNMP
  562. * @subpackage rfc1905
  563. */
  564. class rfc1905_Report extends rfc1905_PDU
  565. {
  566. /**
  567. * Constructor
  568. *
  569. * @param integer $requestID
  570. * @param integer $errorStatus
  571. * @param integer $errorIndex
  572. * @param array $varBindList
  573. */
  574. function rfc1905_Report($requestID=0, $errorStatus=0, $errorIndex=0, $varBindList=array())
  575. {
  576. parent::rfc1905_PDU($requestID, $errorStatus, $errorIndex, $varBindList);
  577. $this->asnTagNumber = ASN_TAG_REPORT;
  578. }
  579. }
  580. ?>

Documentation generated on Mon, 14 Nov 2005 17:55:27 -0700 by phpDocumentor 1.3.0RC3