Source for file snmp.php

Documentation is available at snmp.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. * @version .7
  28. */
  29.  
  30. /**
  31. */
  32. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'rfc1155.php');
  33. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'rfc1157.php');
  34. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'rfc1902.php');
  35. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'rfc1905.php');
  36. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'rfc2104.php');
  37. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'rfc3411.php');
  38. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'rfc3412.php');
  39. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'rfc3414.php');
  40.  
  41. define('SNMP_VERSION_1', 0);
  42. define('SNMP_VERSION_2', 1);
  43. define('SNMP_VERSION_2C', 1);
  44. define('SNMP_VERSION_2U', 2); // doesn't work yet
  45. define('SNMP_VERSION_3', 3); // doesn't work yet
  46.  
  47. /**
  48. * SNMP
  49. *
  50. * @package phpSNMP
  51. */
  52. class snmp
  53. {
  54. var $version = SNMP_VERSION_1; // version can be SNMP_VERSION_1, SNMP_VERSION_2C, SNMP_VERSION_3
  55. var $timeout = 10.0; // timeout in seconds for waiting for a return packet
  56. var $default_security; // default security parameters
  57.  
  58.  
  59. /**
  60. * Constructor
  61. */
  62. function snmp()
  63. {
  64. $this->default_security = array('community'=>'public', 'v3_max_size'=>65507, 'v3_flags'=>SNMP_AUTH_NOPRIV,
  65. 'v3_security_model'=>SNMP_SECURITY_USM, 'v3_engine_id'=>'', 'v3_engine_boots'=>0,
  66. 'v3_engine_time'=>0, 'v3_user'=>'', 'v3_auth'=>'', 'v3_priv'=>'',
  67. 'v3_context_engine_id'=>'', 'v3_context_name'=>'', 'v3_hash'=>'md5',
  68. 'v3_crypt_algorithm'=>'des', 'v3_crypt_mode'=>'cbc');
  69. }
  70.  
  71. /**
  72. * get an oid from a single host
  73. *
  74. * @param string $host hostnames or ip addresses
  75. * @param mixed $target varbind array or oid (oids must be numeric)
  76. * @param array $security parameters
  77. * @return array in the format $oid=>$value
  78. */
  79. function get($host, $target, $security=NULL)
  80. {
  81. if(is_array($target))
  82. $varbind = $target;
  83. else
  84. $varbind = $this->build_varbind($target);
  85. return array_shift($this->exec($host, 'get', $varbind, $security));
  86. }
  87.  
  88. /**
  89. * get an oid from multiple hosts
  90. *
  91. * @param array $hosts hostnames or ip addresses
  92. * @param mixed $target varbind array or oid (oids must be numeric)
  93. * @param array $security parameters
  94. * @return array in the format $ip=>array($oid=>$value)
  95. */
  96. function multi_get($hosts, $target, $security=NULL)
  97. {
  98. if(is_array($target))
  99. $varbind = $target;
  100. else
  101. $varbind = $this->build_varbind($target);
  102. return $this->exec($hosts, 'get', $varbind, $security);
  103. }
  104.  
  105. /**
  106. * bulk get oids from a single host
  107. *
  108. * @param string $host hostname or ip address
  109. * @param array $oids (oids must be numeric)
  110. * @param array $security parameters
  111. * @return array in the format $oid=>$value
  112. */
  113. function bulk_get($host, $oids, $security=NULL)
  114. {
  115. return array_shift($this->exec($host, 'getbulk', $this->build_varbind($oids), $security));
  116. }
  117.  
  118. /**
  119. * bulk get oids from a mulitple hosts
  120. *
  121. * @param string $hosts hostnames or ip addresses
  122. * @param array $oids (oids must be numeric)
  123. * @param array $security parameters
  124. * @return array in the format $oid=>$value
  125. */
  126. function multi_bulk_get($hosts, $oids, $security=NULL)
  127. {
  128. return $this->exec($hosts, 'getbulk', $this->build_varbind($oids), $security);
  129. }
  130.  
  131. /**
  132. * walk an oid
  133. *
  134. * @param string $host hostnames or ip addresses
  135. * @param string $oid (oids must be numeric)
  136. * @param array $security parameters
  137. * @return array in the format $ip=>array($oid=>$value)
  138. */
  139. function walk($host, $oid, $security=NULL)
  140. {
  141. return array_shift($this->exec($host, 'getnext', $this->build_varbind($oid), $security, $oid));
  142. }
  143.  
  144. /**
  145. * walk an oid on multiple hosts
  146. *
  147. * @param array $hosts hostnames or ip addresses
  148. * @param sring $oid (oids must be numeric)
  149. * @param array $security parameters
  150. * @return array in the format $ip=>array($oid=>$value)
  151. */
  152. function multi_walk($hosts, $oid, $security=NULL)
  153. {
  154. return $this->exec($hosts, 'getnext', $this->build_varbind($oid), $security, $oid);
  155. }
  156.  
  157. /**
  158. * set a variable
  159. *
  160. * @param string $host hostname or ip address
  161. * @param mixed $target varbind array or oid (oids must be numeric)
  162. * @param mixed $value to set
  163. * @param string $type 'i' = integer; 't' = time ticks; 'x' = hex string; 's' = string; 'a' = IP address; 'o' = object ID; 'n' = null value
  164. * @param array $security parameters
  165. */
  166. function set($host, $target, $value=0, $type='i', $security=NULL)
  167. {
  168. if(is_array($target))
  169. $varbind = $target;
  170. else
  171. $varbind = $this->build_varbind($target, $value, $type);
  172. $this->exec($host, 'set', $varbind, $security);
  173. }
  174.  
  175. /**
  176. * set a variable
  177. *
  178. * @param array $hosts hostnames or ip addresses
  179. * @param mixed $target varbind array or oid (oids must be numeric)
  180. * @param mixed $value to set
  181. * @param string $type 'i' = integer; 't' = time ticks; 'x' = hex string; 's' = string; 'a' = IP address; 'o' = object ID; 'n' = null value
  182. * @param array $security parameters
  183. */
  184. function multi_set($hosts, $target, $value=0, $type='i', $security=NULL)
  185. {
  186. $this->set($hosts, $target, $value, $type, $security);
  187. }
  188.  
  189. /**
  190. * send a trap
  191. *
  192. * @param string $manager hostname or ip address of the manager
  193. * @param array $security parameters
  194. * @param array $varbinds created by build_varbind
  195. * @param string $enterprise oid (oids must be numeric) of the object generating the trap (this is only for version 1)
  196. * @param string $agent hostname or ip address of the agent generating the trap (this is only for version 1)
  197. * @param integer $trap_type from TRAP_COLDSTART, TRAP_WARMSTART, TRAP_LINKDOWN, TRAP_LINKUP,
  198. * TRAP_AUTH_FAIL, TRAP_EGP_NEIGHBOR_LOSS, TRAP_ENTERPRISE_SPECIFIC
  199. * (this is only for version 1)
  200. * @param integer $specific_trap_type (this is only for version 1)
  201. * @param integer $timestamp time since last restart (this is only for version 1)
  202. */
  203. function trap($manager, $security, $varbind, $enterprise='', $agent='', $trap_type=0, $specific_trap_type=0, $timestamp=0)
  204. {
  205. if(is_null($security))
  206. $security = $this->default_security;
  207. elseif(!is_array($security))
  208. {
  209. $s = $this->default_security;
  210. $s['community'] = $security;
  211. $security = $s;
  212. }
  213.  
  214. if($this->version == SNMP_VERSION_1)
  215. {
  216. $pdu = new rfc1157_TrapPDU($enterprise, $agent, $trap_type, $specific_trap_type, $timestamp, $varbind);
  217. $msg = new rfc1157_Message(SNMP_VERSION_1, $security['community'], $pdu);
  218. $packet = $msg->encode();
  219. }
  220. elseif($this->version == SNMP_VERSION_2C || $this->version == SNMP_VERSION_3)
  221. $packet = $this->build_packet($varbind, $security, 'trap');
  222. else
  223. {
  224. trigger_error("Unknown SNMP version [{$this->version}]", E_USER_WARNING);
  225. return;
  226. }
  227.  
  228. $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
  229. @socket_sendto($socket, $packet, strlen($packet), 0, $manager, 162);
  230. }
  231.  
  232. /**
  233. * build a variable binding
  234. *
  235. * @param string $oid (oids must be numeric)
  236. * @param mixed $value to set
  237. * @param string $type 'i' = integer; 't' = time ticks; 'x' = hex string; 's' = string; 'a' = IP address; 'o' = object ID; 'n' = null value
  238. * @return array varbind
  239. */
  240. function build_varbind($oid, $value=0, $type='n')
  241. {
  242. if(!is_array($oid)) $oid = array($oid);
  243.  
  244. if(!is_array($value))
  245. {
  246. $val = $value;
  247. $value = array();
  248. foreach(array_keys($oid) as $i)
  249. $value[$i] = $val;
  250. }
  251. if(!is_array($type))
  252. {
  253. $t = $type;
  254. $type = array();
  255. foreach(array_keys($oid) as $i)
  256. $type[$i] = $t;
  257. }
  258.  
  259. $varbind = array();
  260. foreach($oid as $i=>$o)
  261. {
  262. switch($type[$i])
  263. {
  264. case 'i': // integer
  265. $varbind[] = new <a href="../phpSNMP/rfc1157/rfc1157_VarBind.html">rfc1157_VarBind</a>(new <a href="../phpSNMP/rfc1155/rfc1155_ObjectID.html">rfc1155_ObjectID</a>($o), new <a href="../phpSNMP/rfc1155/rfc1155_Integer.html">rfc1155_Integer</a>($value[$i]));
  266. break;
  267. case 't': // time ticks
  268. $varbind[] = new <a href="../phpSNMP/rfc1157/rfc1157_VarBind.html">rfc1157_VarBind</a>(new <a href="../phpSNMP/rfc1155/rfc1155_ObjectID.html">rfc1155_ObjectID</a>($o), new <a href="../phpSNMP/rfc1155/rfc1155_TimeTicks.html">rfc1155_TimeTicks</a>($value[$i]));
  269. break;
  270. case 'x': // hex string
  271. $varbind[] = new <a href="../phpSNMP/rfc1157/rfc1157_VarBind.html">rfc1157_VarBind</a>(new <a href="../phpSNMP/rfc1155/rfc1155_ObjectID.html">rfc1155_ObjectID</a>($o), new <a href="../phpSNMP/rfc1155/rfc1155_OctetString.html">rfc1155_OctetString</a>(hexbin($value[$i])));
  272. break;
  273. case 's': // string
  274. $varbind[] = new <a href="../phpSNMP/rfc1157/rfc1157_VarBind.html">rfc1157_VarBind</a>(new <a href="../phpSNMP/rfc1155/rfc1155_ObjectID.html">rfc1155_ObjectID</a>($o), new <a href="../phpSNMP/rfc1155/rfc1155_OctetString.html">rfc1155_OctetString</a>($value[$i]));
  275. break;
  276. case 'a': // ip address
  277. $varbind[] = new <a href="../phpSNMP/rfc1157/rfc1157_VarBind.html">rfc1157_VarBind</a>(new <a href="../phpSNMP/rfc1155/rfc1155_ObjectID.html">rfc1155_ObjectID</a>($o), new <a href="../phpSNMP/rfc1155/rfc1155_IPAddress.html">rfc1155_IPAddress</a>($value[$i]));
  278. break;
  279. case 'o': // object id
  280. $varbind[] = new <a href="../phpSNMP/rfc1157/rfc1157_VarBind.html">rfc1157_VarBind</a>(new <a href="../phpSNMP/rfc1155/rfc1155_ObjectID.html">rfc1155_ObjectID</a>($o), new <a href="../phpSNMP/rfc1155/rfc1155_ObjectID.html">rfc1155_ObjectID</a>($value[$i]));
  281. break;
  282. case 'n': // null value
  283. $varbind[] = new <a href="../phpSNMP/rfc1157/rfc1157_VarBind.html">rfc1157_VarBind</a>(new <a href="../phpSNMP/rfc1155/rfc1155_ObjectID.html">rfc1155_ObjectID</a>($o), new <a href="../phpSNMP/rfc1155/rfc1155_Null.html">rfc1155_Null</a>());
  284. break;
  285. default:
  286. trigger_error("Unknown type $type", E_USER_WARNING);
  287. $varbind[] = new rfc1157_VarBind(new rfc1155_ObjectID($o), new rfc1155_Null());
  288. }
  289. }
  290. return $varbind;
  291. }
  292.  
  293. /**
  294. * execute a poll on hosts
  295. *
  296. * @param mixed $target
  297. * @param string $community string
  298. * @param string $type is either get, getnext, or set
  299. * @param string $value to use for set
  300. * @param string $value_type to use for set
  301. * @return string packet
  302. */
  303. function build_packet($varbind, $security=NULL, $type='get')
  304. {
  305. if(is_null($security))
  306. $security = $this->default_security;
  307. elseif(!is_array($security))
  308. {
  309. $s = $this->default_security;
  310. $s['community'] = $security;
  311. $security = $s;
  312. }
  313.  
  314. if($this->version == <a href="../phpSNMP/_snmp_php.html#defineSNMP_VERSION_1">SNMP_VERSION_1</a>)
  315. {
  316. if($type == 'get')
  317. $pdu = new <a href="../phpSNMP/rfc1157/rfc1157_Get.html">rfc1157_Get</a>($this->assignRequestID(), 0, 0, $varbind);
  318. elseif($type == 'getnext')
  319. $pdu = new <a href="../phpSNMP/rfc1157/rfc1157_GetNext.html">rfc1157_GetNext</a>($this->assignRequestID(), 0, 0, $varbind);
  320. elseif($type == 'set')
  321. $pdu = new <a href="../phpSNMP/rfc1157/rfc1157_Set.html">rfc1157_Set</a>($this->assignRequestID(), 0, 0, $varbind);
  322. else
  323. {
  324. trigger_error("Unknown request type: $type", E_USER_WARNING);
  325. return '';
  326. }
  327. $msg = new <a href="../phpSNMP/rfc1157/rfc1157_Message.html">rfc1157_Message</a>(<a href="../phpSNMP/_snmp_php.html#defineSNMP_VERSION_1">SNMP_VERSION_1</a>, $security['community'], $pdu);
  328. }
  329. elseif($this->version == <a href="../phpSNMP/_snmp_php.html#defineSNMP_VERSION_2C">SNMP_VERSION_2C</a> || $this->version == <a href="../phpSNMP/_snmp_php.html#defineSNMP_VERSION_3">SNMP_VERSION_3</a>)
  330. {
  331. $request_id = $this->assignRequestID();
  332. $reportable = <a href="../phpSNMP/rfc1155/_rfc3412_php.html#defineSNMP_REPORTABLE">SNMP_REPORTABLE</a>;
  333. if($type == 'get')
  334. $pdu = new <a href="../phpSNMP/rfc1905/rfc1905_Get.html">rfc1905_Get</a>($request_id, 0, 0, $varbind);
  335. elseif($type == 'getnext')
  336. $pdu = new <a href="../phpSNMP/rfc1905/rfc1905_GetNext.html">rfc1905_GetNext</a>($request_id, 0, 0, $varbind);
  337. elseif($type == 'set')
  338. $pdu = new <a href="../phpSNMP/rfc1905/rfc1905_Set.html">rfc1905_Set</a>($request_id, 0, 0, $varbind);
  339. elseif($type == 'getbulk')
  340. $pdu = new <a href="../phpSNMP/rfc1905/rfc1905_GetBulk.html">rfc1905_GetBulk</a>($request_id, count($varbind), 1, $varbind);
  341. elseif($type == 'inform')
  342. $pdu = new <a href="../phpSNMP/rfc1905/rfc1905_Inform.html">rfc1905_Inform</a>($request_id, 0, 0, $varbind);
  343. elseif($type == 'trap')
  344. {
  345. $pdu = new <a href="../phpSNMP/rfc1905/rfc1905_Trap.html">rfc1905_Trap</a>($request_id, 0, 0, $varbind);
  346. $reportable = 0;
  347. }
  348. elseif($type == 'report')
  349. {
  350. $pdu = new <a href="../phpSNMP/rfc1905/rfc1905_Report.html">rfc1905_Report</a>($request_id, 0, 0, $varbind);
  351. $reportable = 0;
  352. }
  353. else
  354. {
  355. trigger_error("Unknown request type: $type", E_USER_WARNING);
  356. return '';
  357. }
  358. if($this->version == <a href="../phpSNMP/_snmp_php.html#defineSNMP_VERSION_2C">SNMP_VERSION_2C</a>)
  359. $msg = new <a href="../phpSNMP/rfc1905/rfc1905_Message.html">rfc1905_Message</a>(<a href="../phpSNMP/_snmp_php.html#defineSNMP_VERSION_2C">SNMP_VERSION_2C</a>, $security['community'], $pdu);
  360. else
  361. {
  362. foreach($this->default_security as $key=>$value) if(!isset($security[$key])) $security[$key] = $value;
  363.  
  364. $header = new <a href="../phpSNMP/rfc3412/rfc3412_Header.html">rfc3412_Header</a>($request_id, $security['v3_max_size'],
  365. $security['v3_flags'] | $reportable, $security['v3_security_model']);
  366.  
  367. $usm = new <a href="../phpSNMP/rfc3414/rfc3414_USM.html">rfc3414_USM</a>($security['v3_engine_id'], $security['v3_engine_boots'],
  368. $security['v3_engine_time'], $security['v3_user']);
  369. $usm->auth_password = $security['v3_auth'];
  370. $usm->priv_password = $security['v3_priv'];
  371. $usm->hash_function = $security['v3_hash'];
  372. $usm->crypt_algorithm = $security['v3_crypt_algorithm'];
  373. $usm->crypt_mode = $security['v3_crypt_mode'];
  374.  
  375. $scopedpdu = new <a href="../phpSNMP/rfc3412/rfc3412_ScopedPDU.html">rfc3412_ScopedPDU</a>($security['v3_context_engine_id'], $security['v3_context_name'], $pdu);
  376. $msg = new <a href="../phpSNMP/rfc3412/rfc3412_Message.html">rfc3412_Message</a>(<a href="../phpSNMP/_snmp_php.html#defineSNMP_VERSION_3">SNMP_VERSION_3</a>, $header, $usm, $scopedpdu);
  377. }
  378. }
  379. else
  380. {
  381. trigger_error("Unknown SNMP version {$this->version}", E_USER_WARNING);
  382. return '';
  383. }
  384.  
  385. return $msg->encode();
  386. }
  387.  
  388. /**
  389. * execute a poll on hosts
  390. *
  391. * @param array $hosts hostnames or ip addresses
  392. * @param string $type is either get, getnext, or set
  393. * @param string $packet to send
  394. * @param array $security parameters
  395. * @param string $stop
  396. * @return array in the format $ip=>array($oid=>$value)
  397. */
  398. function exec($hosts, $type, $varbind, $security=NULL, $stop='')
  399. {
  400. $queue = array();
  401. $buffer = $port = NULL;
  402. $ret = array();
  403.  
  404. foreach($this->default_security as $key=>$value) if(!isset($security[$key])) $security[$key] = $value;
  405.  
  406. $packet = $this->build_packet($varbind, $security, $type);
  407.  
  408. // add each host to the queue
  409. if(!is_array($hosts)) $hosts = array($hosts);
  410. foreach($hosts as $host)
  411. {
  412. $h = ip2long($host);
  413. if($h == -1 || $h === false) $host = gethostbyname($host); // we don't like hostnames
  414. $queue[] = array($packet, $host);
  415. $ret[$host] = array();
  416. }
  417.  
  418. // create a message to decode with
  419. if($this->version == <a href="../phpSNMP/_snmp_php.html#defineSNMP_VERSION_1">SNMP_VERSION_1</a>)
  420. $msg = new <a href="../phpSNMP/rfc1157/rfc1157_Message.html">rfc1157_Message</a>();
  421. elseif($this->version == <a href="../phpSNMP/_snmp_php.html#defineSNMP_VERSION_2C">SNMP_VERSION_2C</a>)
  422. $msg = new <a href="../phpSNMP/rfc1905/rfc1905_Message.html">rfc1905_Message</a>();
  423. elseif($this->version == <a href="../phpSNMP/_snmp_php.html#defineSNMP_VERSION_3">SNMP_VERSION_3</a>)
  424. {
  425. $usm = new <a href="../phpSNMP/rfc3414/rfc3414_USM.html">rfc3414_USM</a>();
  426. $usm->auth_password = $security['v3_auth'];
  427. $usm->priv_password = $security['v3_priv'];
  428. $usm->hash_function = $security['v3_hash'];
  429. $usm->crypt_algorithm = $security['v3_crypt_algorithm'];
  430. $usm->crypt_mode = $security['v3_crypt_mode'];
  431. $msg = new <a href="../phpSNMP/rfc3412/rfc3412_Message.html">rfc3412_Message</a>(<a href="../phpSNMP/_snmp_php.html#defineSNMP_VERSION_3">SNMP_VERSION_3</a>, new <a href="../phpSNMP/rfc3412/rfc3412_Header.html">rfc3412_Header</a>(), $usm);
  432. }
  433. else
  434. {
  435. trigger_error("Unknown SNMP version {$this->version}", E_USER_WARNING);
  436. return array();
  437. }
  438.  
  439. $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
  440. if($socket === false)
  441. {
  442. trigger_error('Unable to create socket.', E_USER_WARNING);
  443. return array();
  444. }
  445. if(!socket_set_nonblock($socket))
  446. trigger_error('Unable to set socket to nonblocking.', E_USER_WARNING);
  447.  
  448. $sent = 0;
  449. $received = 0;
  450. $t = $this->microtime();
  451. $block_state = 0; // 0 = nonblock, 1 = block, 2 = failed block
  452. while(count($queue))
  453. {
  454. do
  455. {
  456. if(count($queue))
  457. {
  458. // send next queue entry
  459. $entry = array_shift($queue);
  460. if(strlen($entry[0]))
  461. {
  462. if($block_state == 1)
  463. {
  464. socket_set_nonblock($socket);
  465. $block_state = 0;
  466. }
  467.  
  468. if(@socket_sendto($socket, $entry[0], strlen($entry[0]), 0, $entry[1], 161) === false)
  469. trigger_error('Unable to send packet.', E_USER_WARNING);
  470. else
  471. $sent++;
  472. $t = $this->microtime();
  473. }
  474. }
  475. elseif($block_state == 0)
  476. {
  477. // we are done sending, try to set state to blocking I/O with a timeout
  478. if(@socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec'=>floor($this->timeout), 'usec'=>0)))
  479. {
  480. socket_set_block($socket);
  481. $block_state = 1;
  482. }
  483. else
  484. $block_state = 2;
  485. }
  486. elseif($block_state == 2) // sleep if we failed to set a timeout
  487. {
  488. usleep(10000);
  489. }
  490.  
  491. $buffer = $rhost = NULL;
  492. @socket_recvfrom($socket, $buffer, 4096, 0, $rhost, $port);
  493. if($buffer != '' && isset($ret[$rhost]))
  494. {
  495. $received++;
  496.  
  497. $msg = $msg->decode($buffer);
  498.  
  499. if($security['v3_context_engine_id'] == '' && $msg->version() == <a href="../phpSNMP/_snmp_php.html#defineSNMP_VERSION_3">SNMP_VERSION_3</a>)
  500. {
  501. $usm = $msg->usm_security();
  502. $security['v3_engine_id'] = $usm->engineID();
  503. $security['v3_engine_boots'] = $usm->engineBoots();
  504. $security['v3_engine_time'] = $usm->engineTime();
  505.  
  506. $spdu = $msg->scopedPDU();
  507. $security['v3_context_engine_id'] = $spdu->engineID();
  508. $security['v3_context_name'] = $spdu->name();
  509.  
  510. $queue[] = array($this->build_packet($varbind, $security, $type), $rhost);
  511. }
  512. else
  513. {
  514. $pdu = $msg->pdu();
  515.  
  516. if($pdu->errorStatus()) trigger_error($pdu->errorString(), E_USER_WARNING);
  517.  
  518. foreach($pdu->varBindList() as $val)
  519. {
  520. $oid = $val->value[0]->toString();
  521. if(($stop == '' || strpos(' '. $oid, $stop) != 0) && !isset($ret[$rhost][$oid]))
  522. {
  523. if($type == 'getnext')
  524. $queue[] = array($this->build_packet($this->build_varbind($oid), $security, 'getnext'), $rhost);
  525. $ret[$rhost][$oid] = $val->value[1]->toString();
  526. }
  527. }
  528. }
  529. }
  530. } while($sent != $received && $this->microtime() - $t <= $this->timeout);
  531. }
  532. return $ret;
  533. }
  534.  
  535. /**
  536. * Assign a unique requestID
  537. *
  538. * @return integer a request id
  539. */
  540. function assignRequestID()
  541. {
  542. static $nextRequestID = 0;
  543. if($nextRequestID == 0 || $nextRequestID >= 2147483647) $nextRequestID = mt_rand();
  544. return $nextRequestID++;
  545. }
  546.  
  547. /**
  548. * Get microtime as a float
  549. *
  550. * @return float microtime
  551. */
  552. function microtime()
  553. {
  554. list($usec, $sec) = explode(' ', microtime());
  555. return ((float)$usec + (float)$sec);
  556. }
  557. }

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