Source for file rfc1157.php

Documentation is available at rfc1157.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 rfc1157
  28. * @version .7
  29. */
  30.  
  31. /**
  32. */
  33. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'rfc1155.php');
  34.  
  35. define('ASN_TAG_GET', 0x00);
  36. define('ASN_TAG_GETNEXT', 0x01);
  37. define('ASN_TAG_RESPONSE', 0x02);
  38. define('ASN_TAG_SET', 0x03);
  39. define('ASN_TAG_TRAP', 0x04);
  40.  
  41. $ASN_TAG_DICT[0xa0] = 'rfc1157_Get';
  42. $ASN_TAG_DICT[0xa1] = 'rfc1157_GetNext';
  43. $ASN_TAG_DICT[0xa2] = 'rfc1157_Response';
  44. $ASN_TAG_DICT[0xa3] = 'rfc1157_Set';
  45. $ASN_TAG_DICT[0xa4] = 'rfc1157_TrapPDU';
  46.  
  47. /**
  48. * Error Status
  49. *
  50. * @package phpSNMP
  51. * @subpackage rfc1157
  52. */
  53. class rfc1157_ErrorStatus extends rfc1155_Integer
  54. {
  55. /**
  56. * Constructor
  57. *
  58. * @param integer $value
  59. */
  60. function rfc1157_ErrorStatus($value)
  61. {
  62. parent::rfc1155_Integer($value);
  63. }
  64.  
  65. /**
  66. * ToString
  67. *
  68. * @return string value of this object
  69. */
  70. function toString()
  71. {
  72. switch($this->value)
  73. {
  74. case 0: return 'No Error';
  75. case 1: return 'Response message would have been too large';
  76. case 2: return 'There is no such variable name in this MIB';
  77. case 3: return 'The value given has the wrong type';
  78. case 4: return 'Object is Read Only';
  79. }
  80. return 'An unknown error occurred';
  81. }
  82. }
  83.  
  84. /**
  85. * Variable Binding
  86. *
  87. * This binds a name to an object
  88. *
  89. * @package phpSNMP
  90. * @subpackage rfc1157
  91. */
  92. class rfc1157_VarBind extends rfc1155_Sequence
  93. {
  94. /**
  95. * Constructor
  96. *
  97. * @param rfc1155_ObjectID $name
  98. * @param rfc1155_Asn1Object $value
  99. */
  100. function rfc1157_VarBind($name=NULL, $value=NULL)
  101. {
  102. if($name && !is_a($name, 'rfc1155_ObjectID'))
  103. trigger_error('name must be an rfc1155_ObjectID', E_USER_WARNING);
  104. if($value && !is_a($value, 'rfc1155_Asn1Object'))
  105. trigger_error('value must be an rfc1155_Asn1Object', E_USER_WARNING);
  106. parent::rfc1155_Sequence(array($name, $value));
  107. }
  108. }
  109.  
  110. /**
  111. * Variable Binding List
  112. *
  113. * A Sequence of VarBinds
  114. *
  115. * @package phpSNMP
  116. * @subpackage rfc1157
  117. */
  118. class rfc1157_VarBindList extends rfc1155_SequenceOf
  119. {
  120. /**
  121. * Constructor
  122. *
  123. * @param array $value of rfc1157_VarBind
  124. */
  125. function rfc1157_VarBindList($value=array())
  126. {
  127. parent::rfc1155_SequenceOf('rfc1157_VarBind', $value);
  128. }
  129. }
  130.  
  131. /**
  132. * Message
  133. *
  134. * A Message is the base comms type for all SNMP messages
  135. *
  136. * @package phpSNMP
  137. * @subpackage rfc1157
  138. */
  139. class rfc1157_Message extends rfc1155_Sequence
  140. {
  141. /**
  142. * Constructor
  143. *
  144. * @param integer $version
  145. * @param string $community
  146. * @param rfc1157_PDU $pdu
  147. */
  148. function rfc1157_Message($version=0, $community='public', $pdu=NULL)
  149. {
  150. parent::rfc1155_Sequence();
  151. if(is_null($pdu)) $pdu = new rfc1157_PDU();
  152. $this->value = array(new rfc1155_Integer($version), new rfc1155_OctetString($community), $pdu);
  153. }
  154.  
  155. /**
  156. * Get/Set version
  157. *
  158. * @param integer $value
  159. * @return integer
  160. */
  161. function version($value=NULL)
  162. {
  163. if(!is_null($value)) $this->value[0] = new rfc1155_Integer($value);
  164. return $this->value[0]->value;
  165. }
  166.  
  167. /**
  168. * Get/Set community
  169. *
  170. * @param string $value
  171. * @return string
  172. */
  173. function community($value=NULL)
  174. {
  175. if(!is_null($value)) $this->value[1] = new rfc1155_OctetString($value);
  176. return $this->value[1]->value;
  177. }
  178.  
  179. /**
  180. * Get/Set PDU
  181. *
  182. * @param rfc1157_PDU $value
  183. * @return rfc1157_PDU
  184. */
  185. function pdu($value=NULL)
  186. {
  187. if(!is_null($value)) $this->value[2] = $value;
  188. return $this->value[2];
  189. }
  190.  
  191. /**
  192. * Decode Stream
  193. *
  194. * decode() an octet stream into a sequence of Asn1Objects
  195. *
  196. * @param string $stream
  197. * @return rfc1157_Message
  198. */
  199. function decode($stream)
  200. {
  201. $this->value = parent::decode($stream);
  202. if(count($this->value) != 1)
  203. trigger_error('Malformed Message: More than one object decoded.', E_USER_WARNING);
  204. $this->value = $this->value[0]->value;
  205. if(count($this->value) != 3)
  206. trigger_error('Malformed Message: Incorrect sequence length ' . count($this->value[0]->value), E_USER_WARNING);
  207. return $this;
  208. }
  209. }
  210.  
  211. /**
  212. * PDU
  213. *
  214. * Base clss for a non-trap PDU
  215. *
  216. * @package phpSNMP
  217. * @subpackage rfc1157
  218. */
  219. class rfc1157_PDU extends rfc1155_Sequence // Base class for a non-trap PDU
  220.  
  221. {
  222. /**
  223. * Constructor
  224. *
  225. * @param integer $requestID
  226. * @param integer $errorStatus
  227. * @param integer $errorIndex
  228. * @param array $varBindList
  229. */
  230. function rfc1157_PDU($requestID=0, $errorStatus=0, $errorIndex=0, $varBindList=array())
  231. {
  232. /* this allows you to create a new object with no arguments, arguments of the class ultimately desired (eg Integer)
  233. or, to make like easier, it will convert basic strings and ints into the ultimately desired objects. */
  234.  
  235. parent::rfc1155_Sequence();
  236. $this->asnTagClass = ASN_TAG_CLASS_CONTEXT;
  237. $this->value = array(new rfc1155_Integer($requestID), new rfc1157_ErrorStatus($errorStatus),
  238. new rfc1155_Integer($errorIndex), new rfc1157_VarBindList($varBindList));
  239. }
  240.  
  241. /**
  242. * Get/Set Request ID
  243. *
  244. * @param integer $value
  245. * @return integer
  246. */
  247. function requestID($value=NULL)
  248. {
  249. if(!is_null($value)) $this->value[0] = new rfc1155_Integer($value);
  250. return $this->value[0]->value;
  251. }
  252.  
  253. /**
  254. * Get/Set Error Status
  255. *
  256. * @param integer $value
  257. * @return integer
  258. */
  259. function errorStatus($value=NULL)
  260. {
  261. if(!is_null($value)) $this->value[1] = new rfc1157_ErrorStatus($value);
  262. return $this->value[1]->value;
  263. }
  264.  
  265. /**
  266. * Get Error String
  267. *
  268. * @return string
  269. */
  270. function errorString()
  271. {
  272. return $this->value[1]->toString();
  273. }
  274.  
  275. /**
  276. * Get/Set Error Index
  277. *
  278. * @param integer $value
  279. * @return integer
  280. */
  281. function errorIndex($value=NULL)
  282. {
  283. if(!is_null($value)) $this->value[2] = new rfc1155_Integer($value);
  284. return $this->value[2]->value;
  285. }
  286.  
  287. /**
  288. * Get/Set Var Bind List
  289. *
  290. * @param rfc1157_VarBindList $value
  291. * @return rfc1157_VarBindList
  292. */
  293. function varBindList($value=NULL)
  294. {
  295. if(!is_null($value)) $this->value[3] = new rfc1157_VarBindList($value);
  296. return $this->value[3]->value;
  297. }
  298.  
  299. /**
  300. * Decode into a PDU Object
  301. *
  302. * @param string $stream
  303. * @return rfc1157_PDU
  304. */
  305. function decodeContents($stream)
  306. {
  307. parent::decodeContents($stream);
  308. if(count($this->value) != 4)
  309. trigger_error('Malformed PDU: Incorrect length ' . count($this->value), E_USER_WARNING);
  310.  
  311. $this->value[1] = new rfc1157_ErrorStatus($this->value[1]->value);
  312. $this->value[3] = new rfc1157_VarBindList($this->value[3]->value);
  313. return $this;
  314. }
  315. }
  316.  
  317. /**
  318. * GET request
  319. *
  320. * A Get Request PDU
  321. *
  322. * @package phpSNMP
  323. * @subpackage rfc1157
  324. */
  325. class rfc1157_Get extends rfc1157_PDU
  326. {
  327. /**
  328. * Constructor
  329. *
  330. * @param integer $requestID
  331. * @param integer $errorStatus
  332. * @param integer $errorIndex
  333. * @param array $varBindList
  334. */
  335. function rfc1157_Get($requestID=0, $errorStatus=0, $errorIndex=0, $varBindList=array())
  336. {
  337. parent::rfc1157_PDU($requestID, $errorStatus, $errorIndex, $varBindList);
  338. $this->asnTagNumber = ASN_TAG_GET;
  339. }
  340. }
  341.  
  342. /**
  343. * GETNEXT request
  344. *
  345. * A GetNext Request PDU
  346. *
  347. * @package phpSNMP
  348. * @subpackage rfc1157
  349. */
  350. class rfc1157_GetNext extends rfc1157_PDU
  351. {
  352. /**
  353. * Constructor
  354. *
  355. * @param integer $requestID
  356. * @param integer $errorStatus
  357. * @param integer $errorIndex
  358. * @param array $varBindList
  359. */
  360. function rfc1157_GetNext($requestID=0, $errorStatus=0, $errorIndex=0, $varBindList=array())
  361. {
  362. parent::rfc1157_PDU($requestID, $errorStatus, $errorIndex, $varBindList);
  363. $this->asnTagNumber = ASN_TAG_GETNEXT;
  364. }
  365. }
  366.  
  367. /**
  368. * RESPONSE request
  369. *
  370. * A Response PDU
  371. *
  372. * @package phpSNMP
  373. * @subpackage rfc1157
  374. */
  375. class rfc1157_Response extends rfc1157_PDU
  376. {
  377. /**
  378. * Constructor
  379. *
  380. * @param integer $requestID
  381. * @param integer $errorStatus
  382. * @param integer $errorIndex
  383. * @param array $varBindList
  384. */
  385. function rfc1157_Response($requestID=0, $errorStatus=0, $errorIndex=0, $varBindList=array())
  386. {
  387. parent::rfc1157_PDU($requestID, $errorStatus, $errorIndex, $varBindList);
  388. $this->asnTagNumber = ASN_TAG_GET;
  389. }
  390. }
  391.  
  392. /**
  393. * SET request
  394. *
  395. * A Set Request PDU
  396. *
  397. * @package phpSNMP
  398. * @subpackage rfc1157
  399. */
  400. class rfc1157_Set extends rfc1157_PDU
  401. {
  402. /**
  403. * Constructor
  404. *
  405. * @param integer $requestID
  406. * @param integer $errorStatus
  407. * @param integer $errorIndex
  408. * @param array $varBindList
  409. */
  410. function rfc1157_Set($requestID=0, $errorStatus=0, $errorIndex=0, $varBindList=array())
  411. {
  412. parent::rfc1157_PDU($requestID, $errorStatus, $errorIndex, $varBindList);
  413. $this->asnTagNumber = ASN_TAG_SET;
  414. }
  415. }
  416.  
  417. define('TRAP_COLDSTART', 0);
  418. define('TRAP_WARMSTART', 1);
  419. define('TRAP_LINKDOWN', 2);
  420. define('TRAP_LINKUP', 3);
  421. define('TRAP_AUTH_FAIL', 4);
  422. define('TRAP_EGP_NEIGHBOR_LOSS', 5);
  423. define('TRAP_ENTERPRISE_SPECIFIC', 6);
  424.  
  425. /**
  426. * Generic Trap
  427. *
  428. * @package phpSNMP
  429. * @subpackage rfc1157
  430. */
  431. class rfc1157_GenericTrap extends rfc1155_Integer
  432. {
  433. var $genericTraps;
  434.  
  435. /**
  436. * Constructor
  437. *
  438. * @param integer $value
  439. */
  440. function rfc1157_GenericTrap($value)
  441. {
  442. parent::rfc1155_Integer($value);
  443. }
  444. }
  445.  
  446. /**
  447. * Trap PDU
  448. *
  449. * @package phpSNMP
  450. * @subpackage rfc1157
  451. */
  452. class rfc1157_TrapPDU extends rfc1155_Sequence
  453. {
  454. /**
  455. * Constructor
  456. *
  457. * @param string $enterprise
  458. * @param string $agentAddr
  459. * @param integer $genericTrap
  460. * @param integer $specificTrap
  461. * @param integer $timestamp
  462. * @param array $varBindList
  463. */
  464. function rfc1157_TrapPDU($enterprise=NULL, $agentAddr=NULL, $genericTrap=NULL, $specificTrap=NULL, $timestamp=NULL, $varBindList=array())
  465. {
  466. parent::rfc1155_Sequence();
  467. $this->asnTagClass = ASN_TAG_CLASS_CONTEXT;
  468. $this->asnTagNumber = ASN_TAG_TRAP;
  469. $this->value = array(new rfc1155_ObjectID($enterprise), new rfc1155_NetworkAddress($agentAddr),
  470. new rfc1157_GenericTrap($genericTrap), new rfc1155_Integer($specificTrap),
  471. new rfc1155_TimeTicks($timestamp), new rfc1157_VarBindList($varBindList));
  472. }
  473.  
  474. /**
  475. * Get/Set Enterprise OID
  476. *
  477. * @param string $value
  478. * @return string
  479. */
  480. function enterprise($value=NULL)
  481. {
  482. if(!is_null($value)) $this->value[0] = new rfc1155_ObjectID($value);
  483. return $this->value[0]->value;
  484. }
  485.  
  486. /**
  487. * Get/Set Agent Address
  488. *
  489. * @param string $value
  490. * @return string
  491. */
  492. function agentAddr($value=NULL)
  493. {
  494. if(!is_null($value)) $this->value[1] = new rfc1155_NetworkAddress($value);
  495. return $this->value[1]->value;
  496. }
  497.  
  498. /**
  499. * Get/Set Generic Trap
  500. *
  501. * @param integer $value
  502. * @return integer
  503. */
  504. function genericTrap($value=NULL)
  505. {
  506. if(!is_null($value)) $this->value[2]->value = $value;
  507. return $this->value[2]->value;
  508. }
  509.  
  510. /**
  511. * Get/Set Specific Trap
  512. *
  513. * @param integer $value
  514. * @return integer
  515. */
  516. function specificTrap($value=NULL)
  517. {
  518. if(!is_null($value)) $this->value[3]->value = $value;
  519. return $this->value[3]->value;
  520. }
  521.  
  522. /**
  523. * Get/Set Timestamp
  524. *
  525. * @param integer $value
  526. * @return integer
  527. */
  528. function timestamp($value=NULL)
  529. {
  530. if(!is_null($value)) $this->value[4]->value = $value;
  531. return $this->value[4]->value;
  532. }
  533.  
  534. /**
  535. * Get/Set Var Bind List
  536. *
  537. * @param rfc1157_VarBindList $value
  538. * @return rfc1157_VarBindList
  539. */
  540. function VarBindList($value=NULL)
  541. {
  542. if(!is_null($value)) $this->value[5] = $value;
  543. return $this->value[5];
  544. }
  545.  
  546. /**
  547. * Decode into a Get PDU Object
  548. *
  549. * @param string $stream
  550. * @return rfc1157_TrapPDU
  551. */
  552. function decodeContents($stream)
  553. {
  554. parent::decodeContents($stream);
  555. if(count($this->value) != 6)
  556. trigger_error('Malformed TrapPDU: Incorrect length ' . count($this->value), E_USER_WARNING);
  557.  
  558. $this->value[1] = new rfc1155_NetworkAddress($this->value[1]->value);
  559. $this->value[5] = new rfc1157_VarBindList($this->value[5]->value);
  560.  
  561. return $this;
  562. }
  563. }
  564. ?>

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