This source file includes following definitions.
- ZEND_BEGIN_ARG_INFO_EX
- ZEND_GET_MODULE
- netsnmp_session_free
- php_snmp_session_destructor
- php_snmp_object_free_storage
- php_snmp_object_new
- php_snmp_error
- php_snmp_getvalue
- php_snmp_internal
- php_snmp_parse_oid
- netsnmp_session_init
- netsnmp_session_set_sec_level
- netsnmp_session_set_auth_protocol
- netsnmp_session_set_sec_protocol
- netsnmp_session_gen_auth_key
- netsnmp_session_gen_sec_key
- netsnmp_session_set_contextEngineID
- netsnmp_session_set_security
- php_snmp
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_FUNCTION
- PHP_METHOD
- PHP_METHOD
- PHP_METHOD
- PHP_METHOD
- PHP_METHOD
- PHP_METHOD
- PHP_METHOD
- PHP_METHOD
- PHP_METHOD
- php_snmp_add_property
- php_snmp_read_property
- php_snmp_write_property
- php_snmp_has_property
- php_snmp_get_properties
- php_snmp_read_info
- php_snmp_read_max_oids
- PHP_SNMP_LONG_PROPERTY_READER_FUNCTION
- php_snmp_write_max_oids
- php_snmp_write_valueretrieval
- PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION
- php_snmp_write_exceptions_enabled
- free_php_snmp_properties
- PHP_MINIT_FUNCTION
- PHP_MSHUTDOWN_FUNCTION
- PHP_MINFO_FUNCTION
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "php.h"
31 #include "main/php_network.h"
32 #include "ext/standard/info.h"
33 #include "php_snmp.h"
34
35 #include "zend_exceptions.h"
36
37 #if HAVE_SPL
38 #include "ext/spl/spl_exceptions.h"
39 #endif
40
41 #if HAVE_SNMP
42
43 #include <sys/types.h>
44 #ifdef PHP_WIN32
45 #include <winsock2.h>
46 #include <errno.h>
47 #include <process.h>
48 #include "win32/time.h"
49 #elif defined(NETWARE)
50 #ifdef USE_WINSOCK
51 #include <novsock2.h>
52 #else
53 #include <sys/socket.h>
54 #endif
55 #include <errno.h>
56 #include <sys/timeval.h>
57 #else
58 #include <sys/socket.h>
59 #include <netinet/in.h>
60 #include <arpa/inet.h>
61 #ifndef _OSD_POSIX
62 #include <sys/errno.h>
63 #else
64 #include <errno.h>
65 #endif
66 #include <netdb.h>
67 #endif
68 #ifdef HAVE_UNISTD_H
69 #include <unistd.h>
70 #endif
71
72 #ifndef __P
73 #ifdef __GNUC__
74 #define __P(args) args
75 #else
76 #define __P(args) ()
77 #endif
78 #endif
79
80 #include <net-snmp/net-snmp-config.h>
81 #include <net-snmp/net-snmp-includes.h>
82
83
84 #ifndef HAVE_SHUTDOWN_SNMP_LOGGING
85 extern netsnmp_log_handler *logh_head;
86 #define shutdown_snmp_logging() \
87 { \
88 snmp_disable_log(); \
89 while(NULL != logh_head) \
90 netsnmp_remove_loghandler( logh_head ); \
91 }
92 #endif
93
94 #define SNMP_VALUE_LIBRARY (0 << 0)
95 #define SNMP_VALUE_PLAIN (1 << 0)
96 #define SNMP_VALUE_OBJECT (1 << 1)
97
98 typedef struct snmp_session php_snmp_session;
99 #define PHP_SNMP_SESSION_RES_NAME "SNMP session"
100
101 #define PHP_SNMP_ADD_PROPERTIES(a, b) \
102 { \
103 int i = 0; \
104 while (b[i].name != NULL) { \
105 php_snmp_add_property((a), (b)[i].name, (b)[i].name_length, \
106 (php_snmp_read_t)(b)[i].read_func, (php_snmp_write_t)(b)[i].write_func); \
107 i++; \
108 } \
109 }
110
111 #define PHP_SNMP_ERRNO_NOERROR 0
112 #define PHP_SNMP_ERRNO_GENERIC (1 << 1)
113 #define PHP_SNMP_ERRNO_TIMEOUT (1 << 2)
114 #define PHP_SNMP_ERRNO_ERROR_IN_REPLY (1 << 3)
115 #define PHP_SNMP_ERRNO_OID_NOT_INCREASING (1 << 4)
116 #define PHP_SNMP_ERRNO_OID_PARSING_ERROR (1 << 5)
117 #define PHP_SNMP_ERRNO_MULTIPLE_SET_QUERIES (1 << 6)
118 #define PHP_SNMP_ERRNO_ANY ( \
119 PHP_SNMP_ERRNO_GENERIC | \
120 PHP_SNMP_ERRNO_TIMEOUT | \
121 PHP_SNMP_ERRNO_ERROR_IN_REPLY | \
122 PHP_SNMP_ERRNO_OID_NOT_INCREASING | \
123 PHP_SNMP_ERRNO_OID_PARSING_ERROR | \
124 PHP_SNMP_ERRNO_MULTIPLE_SET_QUERIES | \
125 PHP_SNMP_ERRNO_NOERROR \
126 )
127
128 ZEND_DECLARE_MODULE_GLOBALS(snmp)
129 static PHP_GINIT_FUNCTION(snmp);
130
131
132 static oid objid_mib[] = {1, 3, 6, 1, 2, 1};
133
134 static int le_snmp_session;
135
136
137 static zend_object_handlers php_snmp_object_handlers;
138
139
140 zend_class_entry *php_snmp_ce;
141 zend_class_entry *php_snmp_exception_ce;
142
143
144 static HashTable php_snmp_properties;
145
146
147
148 ZEND_BEGIN_ARG_INFO_EX(arginfo_snmpget, 0, 0, 3)
149 ZEND_ARG_INFO(0, host)
150 ZEND_ARG_INFO(0, community)
151 ZEND_ARG_INFO(0, object_id)
152 ZEND_ARG_INFO(0, timeout)
153 ZEND_ARG_INFO(0, retries)
154 ZEND_END_ARG_INFO()
155
156 ZEND_BEGIN_ARG_INFO_EX(arginfo_snmpgetnext, 0, 0, 3)
157 ZEND_ARG_INFO(0, host)
158 ZEND_ARG_INFO(0, community)
159 ZEND_ARG_INFO(0, object_id)
160 ZEND_ARG_INFO(0, timeout)
161 ZEND_ARG_INFO(0, retries)
162 ZEND_END_ARG_INFO()
163
164 ZEND_BEGIN_ARG_INFO_EX(arginfo_snmpwalk, 0, 0, 3)
165 ZEND_ARG_INFO(0, host)
166 ZEND_ARG_INFO(0, community)
167 ZEND_ARG_INFO(0, object_id)
168 ZEND_ARG_INFO(0, timeout)
169 ZEND_ARG_INFO(0, retries)
170 ZEND_END_ARG_INFO()
171
172 ZEND_BEGIN_ARG_INFO_EX(arginfo_snmprealwalk, 0, 0, 3)
173 ZEND_ARG_INFO(0, host)
174 ZEND_ARG_INFO(0, community)
175 ZEND_ARG_INFO(0, object_id)
176 ZEND_ARG_INFO(0, timeout)
177 ZEND_ARG_INFO(0, retries)
178 ZEND_END_ARG_INFO()
179
180 ZEND_BEGIN_ARG_INFO_EX(arginfo_snmpset, 0, 0, 5)
181 ZEND_ARG_INFO(0, host)
182 ZEND_ARG_INFO(0, community)
183 ZEND_ARG_INFO(0, object_id)
184 ZEND_ARG_INFO(0, type)
185 ZEND_ARG_INFO(0, value)
186 ZEND_ARG_INFO(0, timeout)
187 ZEND_ARG_INFO(0, retries)
188 ZEND_END_ARG_INFO()
189
190
191 ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_get_quick_print, 0, 0, 1)
192 ZEND_ARG_INFO(0, d)
193 ZEND_END_ARG_INFO()
194
195 ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_set_quick_print, 0, 0, 1)
196 ZEND_ARG_INFO(0, quick_print)
197 ZEND_END_ARG_INFO()
198
199 ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_set_enum_print, 0, 0, 1)
200 ZEND_ARG_INFO(0, enum_print)
201 ZEND_END_ARG_INFO()
202
203 ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_set_oid_output_format, 0, 0, 1)
204 ZEND_ARG_INFO(0, oid_format)
205 ZEND_END_ARG_INFO()
206
207 ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_get, 0, 0, 3)
208 ZEND_ARG_INFO(0, host)
209 ZEND_ARG_INFO(0, community)
210 ZEND_ARG_INFO(0, object_id)
211 ZEND_ARG_INFO(0, timeout)
212 ZEND_ARG_INFO(0, retries)
213 ZEND_END_ARG_INFO()
214
215 ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_getnext, 0, 0, 3)
216 ZEND_ARG_INFO(0, host)
217 ZEND_ARG_INFO(0, community)
218 ZEND_ARG_INFO(0, object_id)
219 ZEND_ARG_INFO(0, timeout)
220 ZEND_ARG_INFO(0, retries)
221 ZEND_END_ARG_INFO()
222
223 ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_walk, 0, 0, 3)
224 ZEND_ARG_INFO(0, host)
225 ZEND_ARG_INFO(0, community)
226 ZEND_ARG_INFO(0, object_id)
227 ZEND_ARG_INFO(0, timeout)
228 ZEND_ARG_INFO(0, retries)
229 ZEND_END_ARG_INFO()
230
231 ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_real_walk, 0, 0, 3)
232 ZEND_ARG_INFO(0, host)
233 ZEND_ARG_INFO(0, community)
234 ZEND_ARG_INFO(0, object_id)
235 ZEND_ARG_INFO(0, timeout)
236 ZEND_ARG_INFO(0, retries)
237 ZEND_END_ARG_INFO()
238
239 ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp2_set, 0, 0, 5)
240 ZEND_ARG_INFO(0, host)
241 ZEND_ARG_INFO(0, community)
242 ZEND_ARG_INFO(0, object_id)
243 ZEND_ARG_INFO(0, type)
244 ZEND_ARG_INFO(0, value)
245 ZEND_ARG_INFO(0, timeout)
246 ZEND_ARG_INFO(0, retries)
247 ZEND_END_ARG_INFO()
248
249 ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_get, 0, 0, 8)
250 ZEND_ARG_INFO(0, host)
251 ZEND_ARG_INFO(0, sec_name)
252 ZEND_ARG_INFO(0, sec_level)
253 ZEND_ARG_INFO(0, auth_protocol)
254 ZEND_ARG_INFO(0, auth_passphrase)
255 ZEND_ARG_INFO(0, priv_protocol)
256 ZEND_ARG_INFO(0, priv_passphrase)
257 ZEND_ARG_INFO(0, object_id)
258 ZEND_ARG_INFO(0, timeout)
259 ZEND_ARG_INFO(0, retries)
260 ZEND_END_ARG_INFO()
261
262 ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_getnext, 0, 0, 8)
263 ZEND_ARG_INFO(0, host)
264 ZEND_ARG_INFO(0, sec_name)
265 ZEND_ARG_INFO(0, sec_level)
266 ZEND_ARG_INFO(0, auth_protocol)
267 ZEND_ARG_INFO(0, auth_passphrase)
268 ZEND_ARG_INFO(0, priv_protocol)
269 ZEND_ARG_INFO(0, priv_passphrase)
270 ZEND_ARG_INFO(0, object_id)
271 ZEND_ARG_INFO(0, timeout)
272 ZEND_ARG_INFO(0, retries)
273 ZEND_END_ARG_INFO()
274
275 ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_walk, 0, 0, 8)
276 ZEND_ARG_INFO(0, host)
277 ZEND_ARG_INFO(0, sec_name)
278 ZEND_ARG_INFO(0, sec_level)
279 ZEND_ARG_INFO(0, auth_protocol)
280 ZEND_ARG_INFO(0, auth_passphrase)
281 ZEND_ARG_INFO(0, priv_protocol)
282 ZEND_ARG_INFO(0, priv_passphrase)
283 ZEND_ARG_INFO(0, object_id)
284 ZEND_ARG_INFO(0, timeout)
285 ZEND_ARG_INFO(0, retries)
286 ZEND_END_ARG_INFO()
287
288 ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_real_walk, 0, 0, 8)
289 ZEND_ARG_INFO(0, host)
290 ZEND_ARG_INFO(0, sec_name)
291 ZEND_ARG_INFO(0, sec_level)
292 ZEND_ARG_INFO(0, auth_protocol)
293 ZEND_ARG_INFO(0, auth_passphrase)
294 ZEND_ARG_INFO(0, priv_protocol)
295 ZEND_ARG_INFO(0, priv_passphrase)
296 ZEND_ARG_INFO(0, object_id)
297 ZEND_ARG_INFO(0, timeout)
298 ZEND_ARG_INFO(0, retries)
299 ZEND_END_ARG_INFO()
300
301 ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp3_set, 0, 0, 10)
302 ZEND_ARG_INFO(0, host)
303 ZEND_ARG_INFO(0, sec_name)
304 ZEND_ARG_INFO(0, sec_level)
305 ZEND_ARG_INFO(0, auth_protocol)
306 ZEND_ARG_INFO(0, auth_passphrase)
307 ZEND_ARG_INFO(0, priv_protocol)
308 ZEND_ARG_INFO(0, priv_passphrase)
309 ZEND_ARG_INFO(0, object_id)
310 ZEND_ARG_INFO(0, type)
311 ZEND_ARG_INFO(0, value)
312 ZEND_ARG_INFO(0, timeout)
313 ZEND_ARG_INFO(0, retries)
314 ZEND_END_ARG_INFO()
315
316 ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_set_valueretrieval, 0, 0, 1)
317 ZEND_ARG_INFO(0, method)
318 ZEND_END_ARG_INFO()
319
320 ZEND_BEGIN_ARG_INFO(arginfo_snmp_get_valueretrieval, 0)
321 ZEND_END_ARG_INFO()
322
323 ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_read_mib, 0, 0, 1)
324 ZEND_ARG_INFO(0, filename)
325 ZEND_END_ARG_INFO()
326
327
328
329 ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_create, 0, 0, 3)
330 ZEND_ARG_INFO(0, version)
331 ZEND_ARG_INFO(0, host)
332 ZEND_ARG_INFO(0, community)
333 ZEND_ARG_INFO(0, timeout)
334 ZEND_ARG_INFO(0, retries)
335 ZEND_END_ARG_INFO()
336
337 ZEND_BEGIN_ARG_INFO(arginfo_snmp_void, 0)
338 ZEND_END_ARG_INFO()
339
340 ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_setSecurity, 0, 0, 8)
341 ZEND_ARG_INFO(0, sec_level)
342 ZEND_ARG_INFO(0, auth_protocol)
343 ZEND_ARG_INFO(0, auth_passphrase)
344 ZEND_ARG_INFO(0, priv_protocol)
345 ZEND_ARG_INFO(0, priv_passphrase)
346 ZEND_ARG_INFO(0, contextName)
347 ZEND_ARG_INFO(0, contextEngineID)
348 ZEND_END_ARG_INFO()
349
350 ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_get, 0, 0, 1)
351 ZEND_ARG_INFO(0, object_id)
352 ZEND_ARG_INFO(0, use_orignames)
353 ZEND_END_ARG_INFO()
354
355 ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_walk, 0, 0, 4)
356 ZEND_ARG_INFO(0, object_id)
357 ZEND_ARG_INFO(0, suffix_keys)
358 ZEND_ARG_INFO(0, max_repetitions)
359 ZEND_ARG_INFO(0, non_repeaters)
360 ZEND_END_ARG_INFO()
361
362 ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_set, 0, 0, 3)
363 ZEND_ARG_INFO(0, object_id)
364 ZEND_ARG_INFO(0, type)
365 ZEND_ARG_INFO(0, value)
366 ZEND_END_ARG_INFO()
367
368 ZEND_BEGIN_ARG_INFO_EX(arginfo_snmp_class_set_quick_print, 0, 0, 1)
369 ZEND_ARG_INFO(0, quick_print)
370 ZEND_END_ARG_INFO()
371
372
373 struct objid_query {
374 int count;
375 int offset;
376 int step;
377 zend_long non_repeaters;
378 zend_long max_repetitions;
379 int valueretrieval;
380 int array_output;
381 int oid_increasing_check;
382 snmpobjarg *vars;
383 };
384
385
386
387 const zend_function_entry snmp_functions[] = {
388 PHP_FE(snmpget, arginfo_snmpget)
389 PHP_FE(snmpgetnext, arginfo_snmpgetnext)
390 PHP_FE(snmpwalk, arginfo_snmpwalk)
391 PHP_FE(snmprealwalk, arginfo_snmprealwalk)
392 PHP_FALIAS(snmpwalkoid, snmprealwalk, arginfo_snmprealwalk)
393 PHP_FE(snmpset, arginfo_snmpset)
394 PHP_FE(snmp_get_quick_print, arginfo_snmp_get_quick_print)
395 PHP_FE(snmp_set_quick_print, arginfo_snmp_set_quick_print)
396 PHP_FE(snmp_set_enum_print, arginfo_snmp_set_enum_print)
397 PHP_FE(snmp_set_oid_output_format, arginfo_snmp_set_oid_output_format)
398 PHP_FALIAS(snmp_set_oid_numeric_print, snmp_set_oid_output_format, arginfo_snmp_set_oid_output_format)
399
400 PHP_FE(snmp2_get, arginfo_snmp2_get)
401 PHP_FE(snmp2_getnext, arginfo_snmp2_getnext)
402 PHP_FE(snmp2_walk, arginfo_snmp2_walk)
403 PHP_FE(snmp2_real_walk, arginfo_snmp2_real_walk)
404 PHP_FE(snmp2_set, arginfo_snmp2_set)
405
406 PHP_FE(snmp3_get, arginfo_snmp3_get)
407 PHP_FE(snmp3_getnext, arginfo_snmp3_getnext)
408 PHP_FE(snmp3_walk, arginfo_snmp3_walk)
409 PHP_FE(snmp3_real_walk, arginfo_snmp3_real_walk)
410 PHP_FE(snmp3_set, arginfo_snmp3_set)
411 PHP_FE(snmp_set_valueretrieval, arginfo_snmp_set_valueretrieval)
412 PHP_FE(snmp_get_valueretrieval, arginfo_snmp_get_valueretrieval)
413
414 PHP_FE(snmp_read_mib, arginfo_snmp_read_mib)
415 PHP_FE_END
416 };
417
418
419
420 #define SNMP_CMD_GET (1<<0)
421
422 #define SNMP_CMD_GETNEXT (1<<1)
423
424 #define SNMP_CMD_SET (1<<2)
425
426 #define SNMP_CMD_WALK (1<<3)
427
428 #define SNMP_NUMERIC_KEYS (1<<7)
429
430 #define SNMP_ORIGINAL_NAMES_AS_KEYS (1<<8)
431
432 #define SNMP_USE_SUFFIX_AS_KEYS (1<<9)
433
434 #ifdef COMPILE_DL_SNMP
435 ZEND_GET_MODULE(snmp)
436 #endif
437
438
439
440
441
442 static PHP_GINIT_FUNCTION(snmp)
443 {
444 snmp_globals->valueretrieval = SNMP_VALUE_LIBRARY;
445 }
446
447
448 #define PHP_SNMP_SESSION_FREE(a) { \
449 if ((*session)->a) { \
450 efree((*session)->a); \
451 (*session)->a = NULL; \
452 } \
453 }
454
455 static void netsnmp_session_free(php_snmp_session **session)
456 {
457 if (*session) {
458 PHP_SNMP_SESSION_FREE(peername);
459 PHP_SNMP_SESSION_FREE(community);
460 PHP_SNMP_SESSION_FREE(securityName);
461 PHP_SNMP_SESSION_FREE(contextEngineID);
462 efree(*session);
463 *session = NULL;
464 }
465 }
466
467
468 static void php_snmp_session_destructor(zend_resource *rsrc)
469 {
470 php_snmp_session *session = (php_snmp_session *)rsrc->ptr;
471 netsnmp_session_free(&session);
472 }
473
474
475 static void php_snmp_object_free_storage(zend_object *object)
476 {
477 php_snmp_object *intern = php_snmp_fetch_object(object);
478
479 if (!intern) {
480 return;
481 }
482
483 netsnmp_session_free(&(intern->session));
484
485 zend_object_std_dtor(&intern->zo);
486 }
487
488
489 static zend_object *php_snmp_object_new(zend_class_entry *class_type)
490 {
491 php_snmp_object *intern;
492
493
494 intern = ecalloc(1, sizeof(php_snmp_object) + zend_object_properties_size(class_type));
495
496 zend_object_std_init(&intern->zo, class_type);
497 object_properties_init(&intern->zo, class_type);
498
499 intern->zo.handlers = &php_snmp_object_handlers;
500
501 return &intern->zo;
502
503 }
504
505
506
507
508
509
510
511 static void php_snmp_error(zval *object, const char *docref, int type, const char *format, ...)
512 {
513 va_list args;
514 php_snmp_object *snmp_object = NULL;
515
516 if (object) {
517 snmp_object = Z_SNMP_P(object);
518 if (type == PHP_SNMP_ERRNO_NOERROR) {
519 memset(snmp_object->snmp_errstr, 0, sizeof(snmp_object->snmp_errstr));
520 } else {
521 va_start(args, format);
522 vsnprintf(snmp_object->snmp_errstr, sizeof(snmp_object->snmp_errstr) - 1, format, args);
523 va_end(args);
524 }
525 snmp_object->snmp_errno = type;
526 }
527
528 if (type == PHP_SNMP_ERRNO_NOERROR) {
529 return;
530 }
531
532 if (object && (snmp_object->exceptions_enabled & type)) {
533 zend_throw_exception_ex(php_snmp_exception_ce, type, "%s", snmp_object->snmp_errstr);
534 } else {
535 va_start(args, format);
536 php_verror(docref, "", E_WARNING, format, args);
537 va_end(args);
538 }
539 }
540
541
542
543
544
545
546
547
548 static void php_snmp_getvalue(struct variable_list *vars, zval *snmpval, int valueretrieval)
549 {
550 zval val;
551 char sbuf[512];
552 char *buf = &(sbuf[0]);
553 char *dbuf = (char *)NULL;
554 int buflen = sizeof(sbuf) - 1;
555 int val_len = vars->val_len;
556
557
558
559
560
561
562 while ((valueretrieval & SNMP_VALUE_PLAIN) == 0) {
563 *buf = '\0';
564 if (snprint_value(buf, buflen, vars->name, vars->name_length, vars) == -1) {
565 if (val_len > 512*1024) {
566 php_error_docref(NULL, E_WARNING, "snprint_value() asks for a buffer more than 512k, Net-SNMP bug?");
567 break;
568 }
569
570 val_len *= 2;
571 } else {
572 break;
573 }
574
575 if (buf == dbuf) {
576 dbuf = (char *)erealloc(dbuf, val_len + 1);
577 } else {
578 dbuf = (char *)emalloc(val_len + 1);
579 }
580
581 if (!dbuf) {
582 php_error_docref(NULL, E_WARNING, "emalloc() failed: %s, fallback to static buffer", strerror(errno));
583 buf = &(sbuf[0]);
584 buflen = sizeof(sbuf) - 1;
585 break;
586 }
587
588 buf = dbuf;
589 buflen = val_len;
590 }
591
592 if((valueretrieval & SNMP_VALUE_PLAIN) && val_len > buflen){
593 if ((dbuf = (char *)emalloc(val_len + 1))) {
594 buf = dbuf;
595 buflen = val_len;
596 } else {
597 php_error_docref(NULL, E_WARNING, "emalloc() failed: %s, fallback to static buffer", strerror(errno));
598 }
599 }
600
601 if (valueretrieval & SNMP_VALUE_PLAIN) {
602 *buf = 0;
603 switch (vars->type) {
604 case ASN_BIT_STR:
605 ZVAL_STRINGL(&val, (char *)vars->val.bitstring, vars->val_len);
606 break;
607
608 case ASN_OCTET_STR:
609 case ASN_OPAQUE:
610 ZVAL_STRINGL(&val, (char *)vars->val.string, vars->val_len);
611 break;
612
613 case ASN_NULL:
614 ZVAL_NULL(&val);
615 break;
616
617 case ASN_OBJECT_ID:
618 snprint_objid(buf, buflen, vars->val.objid, vars->val_len / sizeof(oid));
619 ZVAL_STRING(&val, buf);
620 break;
621
622 case ASN_IPADDRESS:
623 snprintf(buf, buflen, "%d.%d.%d.%d",
624 (vars->val.string)[0], (vars->val.string)[1],
625 (vars->val.string)[2], (vars->val.string)[3]);
626 buf[buflen]=0;
627 ZVAL_STRING(&val, buf);
628 break;
629
630 case ASN_COUNTER:
631 case ASN_GAUGE:
632
633 case ASN_TIMETICKS:
634 case ASN_UINTEGER:
635 snprintf(buf, buflen, "%lu", *vars->val.integer);
636 buf[buflen]=0;
637 ZVAL_STRING(&val, buf);
638 break;
639
640 case ASN_INTEGER:
641 snprintf(buf, buflen, "%ld", *vars->val.integer);
642 buf[buflen]=0;
643 ZVAL_STRING(&val, buf);
644 break;
645
646 #if defined(NETSNMP_WITH_OPAQUE_SPECIAL_TYPES) || defined(OPAQUE_SPECIAL_TYPES)
647 case ASN_OPAQUE_FLOAT:
648 snprintf(buf, buflen, "%f", *vars->val.floatVal);
649 ZVAL_STRING(&val, buf);
650 break;
651
652 case ASN_OPAQUE_DOUBLE:
653 snprintf(buf, buflen, "%Lf", *vars->val.doubleVal);
654 ZVAL_STRING(&val, buf);
655 break;
656
657 case ASN_OPAQUE_I64:
658 printI64(buf, vars->val.counter64);
659 ZVAL_STRING(&val, buf);
660 break;
661
662 case ASN_OPAQUE_U64:
663 #endif
664 case ASN_COUNTER64:
665 printU64(buf, vars->val.counter64);
666 ZVAL_STRING(&val, buf);
667 break;
668
669 default:
670 ZVAL_STRING(&val, "Unknown value type");
671 php_error_docref(NULL, E_WARNING, "Unknown value type: %u", vars->type);
672 break;
673 }
674 } else {
675
676 ZVAL_STRING(&val, buf);
677 }
678
679 if (valueretrieval & SNMP_VALUE_OBJECT) {
680 object_init(snmpval);
681 add_property_long(snmpval, "type", vars->type);
682 add_property_zval(snmpval, "value", &val);
683 } else {
684 ZVAL_COPY(snmpval, &val);
685 }
686 zval_ptr_dtor(&val);
687
688 if (dbuf){
689 efree(dbuf);
690 }
691 }
692
693
694
695
696
697
698
699 static void php_snmp_internal(INTERNAL_FUNCTION_PARAMETERS, int st,
700 struct snmp_session *session,
701 struct objid_query *objid_query)
702 {
703 struct snmp_session *ss;
704 struct snmp_pdu *pdu=NULL, *response;
705 struct variable_list *vars;
706 oid root[MAX_NAME_LEN];
707 size_t rootlen = 0;
708 int status, count, found;
709 char buf[2048];
710 char buf2[2048];
711 int keepwalking=1;
712 char *err;
713 zval snmpval;
714 int snmp_errno;
715
716
717 RETVAL_FALSE;
718
719
720 php_snmp_error(getThis(), NULL, PHP_SNMP_ERRNO_NOERROR, "");
721
722 if (st & SNMP_CMD_WALK) {
723 memmove((char *)root, (char *)(objid_query->vars[0].name), (objid_query->vars[0].name_length) * sizeof(oid));
724 rootlen = objid_query->vars[0].name_length;
725 objid_query->offset = objid_query->count;
726 }
727
728 if ((ss = snmp_open(session)) == NULL) {
729 snmp_error(session, NULL, NULL, &err);
730 php_error_docref(NULL, E_WARNING, "Could not open snmp connection: %s", err);
731 free(err);
732 RETVAL_FALSE;
733 return;
734 }
735
736 if ((st & SNMP_CMD_SET) && objid_query->count > objid_query->step) {
737 php_snmp_error(getThis(), NULL, PHP_SNMP_ERRNO_MULTIPLE_SET_QUERIES, "Can not fit all OIDs for SET query into one packet, using multiple queries");
738 }
739
740 while (keepwalking) {
741 keepwalking = 0;
742 if (st & SNMP_CMD_WALK) {
743 if (session->version == SNMP_VERSION_1) {
744 pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
745 } else {
746 pdu = snmp_pdu_create(SNMP_MSG_GETBULK);
747 pdu->non_repeaters = objid_query->non_repeaters;
748 pdu->max_repetitions = objid_query->max_repetitions;
749 }
750 snmp_add_null_var(pdu, objid_query->vars[0].name, objid_query->vars[0].name_length);
751 } else {
752 if (st & SNMP_CMD_GET) {
753 pdu = snmp_pdu_create(SNMP_MSG_GET);
754 } else if (st & SNMP_CMD_GETNEXT) {
755 pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
756 } else if (st & SNMP_CMD_SET) {
757 pdu = snmp_pdu_create(SNMP_MSG_SET);
758 } else {
759 snmp_close(ss);
760 php_error_docref(NULL, E_ERROR, "Unknown SNMP command (internals)");
761 RETVAL_FALSE;
762 return;
763 }
764 for (count = 0; objid_query->offset < objid_query->count && count < objid_query->step; objid_query->offset++, count++){
765 if (st & SNMP_CMD_SET) {
766 if ((snmp_errno = snmp_add_var(pdu, objid_query->vars[objid_query->offset].name, objid_query->vars[objid_query->offset].name_length, objid_query->vars[objid_query->offset].type, objid_query->vars[objid_query->offset].value))) {
767 snprint_objid(buf, sizeof(buf), objid_query->vars[objid_query->offset].name, objid_query->vars[objid_query->offset].name_length);
768 php_snmp_error(getThis(), NULL, PHP_SNMP_ERRNO_OID_PARSING_ERROR, "Could not add variable: OID='%s' type='%c' value='%s': %s", buf, objid_query->vars[objid_query->offset].type, objid_query->vars[objid_query->offset].value, snmp_api_errstring(snmp_errno));
769 snmp_free_pdu(pdu);
770 snmp_close(ss);
771 RETVAL_FALSE;
772 return;
773 }
774 } else {
775 snmp_add_null_var(pdu, objid_query->vars[objid_query->offset].name, objid_query->vars[objid_query->offset].name_length);
776 }
777 }
778 if(pdu->variables == NULL){
779 snmp_free_pdu(pdu);
780 snmp_close(ss);
781 RETVAL_FALSE;
782 return;
783 }
784 }
785
786 retry:
787 status = snmp_synch_response(ss, pdu, &response);
788 if (status == STAT_SUCCESS) {
789 if (response->errstat == SNMP_ERR_NOERROR) {
790 if (st & SNMP_CMD_SET) {
791 if (objid_query->offset < objid_query->count) {
792 keepwalking = 1;
793 continue;
794 }
795 snmp_free_pdu(response);
796 snmp_close(ss);
797 RETVAL_TRUE;
798 return;
799 }
800 for (vars = response->variables; vars; vars = vars->next_variable) {
801
802 if ( vars->type == SNMP_ENDOFMIBVIEW ||
803 vars->type == SNMP_NOSUCHOBJECT ||
804 vars->type == SNMP_NOSUCHINSTANCE ) {
805 if ((st & SNMP_CMD_WALK) && Z_TYPE_P(return_value) == IS_ARRAY) {
806 break;
807 }
808 snprint_objid(buf, sizeof(buf), vars->name, vars->name_length);
809 snprint_value(buf2, sizeof(buf2), vars->name, vars->name_length, vars);
810 php_snmp_error(getThis(), NULL, PHP_SNMP_ERRNO_ERROR_IN_REPLY, "Error in packet at '%s': %s", buf, buf2);
811 continue;
812 }
813
814 if ((st & SNMP_CMD_WALK) &&
815 (vars->name_length < rootlen || memcmp(root, vars->name, rootlen * sizeof(oid)))) {
816 if (Z_TYPE_P(return_value) == IS_ARRAY) {
817 keepwalking = 0;
818 } else {
819
820 st |= SNMP_CMD_GET;
821 st ^= SNMP_CMD_WALK;
822 objid_query->offset = 0;
823 keepwalking = 1;
824 }
825 break;
826 }
827
828 ZVAL_NULL(&snmpval);
829 php_snmp_getvalue(vars, &snmpval, objid_query->valueretrieval);
830
831 if (objid_query->array_output) {
832 if (Z_TYPE_P(return_value) == IS_TRUE || Z_TYPE_P(return_value) == IS_FALSE) {
833 array_init(return_value);
834 }
835 if (st & SNMP_NUMERIC_KEYS) {
836 add_next_index_zval(return_value, &snmpval);
837 } else if (st & SNMP_ORIGINAL_NAMES_AS_KEYS && st & SNMP_CMD_GET) {
838 found = 0;
839 for (count = 0; count < objid_query->count; count++) {
840 if (objid_query->vars[count].name_length == vars->name_length && snmp_oid_compare(objid_query->vars[count].name, objid_query->vars[count].name_length, vars->name, vars->name_length) == 0) {
841 found = 1;
842 objid_query->vars[count].name_length = 0;
843 break;
844 }
845 }
846 if (found) {
847 add_assoc_zval(return_value, objid_query->vars[count].oid, &snmpval);
848 } else {
849 snprint_objid(buf2, sizeof(buf2), vars->name, vars->name_length);
850 php_error_docref(NULL, E_WARNING, "Could not find original OID name for '%s'", buf2);
851 }
852 } else if (st & SNMP_USE_SUFFIX_AS_KEYS && st & SNMP_CMD_WALK) {
853 snprint_objid(buf2, sizeof(buf2), vars->name, vars->name_length);
854 if (rootlen <= vars->name_length && snmp_oid_compare(root, rootlen, vars->name, rootlen) == 0) {
855 buf2[0] = '\0';
856 count = rootlen;
857 while(count < vars->name_length){
858 sprintf(buf, "%lu.", vars->name[count]);
859 strcat(buf2, buf);
860 count++;
861 }
862 buf2[strlen(buf2) - 1] = '\0';
863 }
864 add_assoc_zval(return_value, buf2, &snmpval);
865 } else {
866 snprint_objid(buf2, sizeof(buf2), vars->name, vars->name_length);
867 add_assoc_zval(return_value, buf2, &snmpval);
868 }
869 } else {
870 ZVAL_COPY_VALUE(return_value, &snmpval);
871 break;
872 }
873
874
875 if (st & SNMP_CMD_WALK) {
876 if (objid_query->oid_increasing_check == TRUE && snmp_oid_compare(objid_query->vars[0].name, objid_query->vars[0].name_length, vars->name, vars->name_length) >= 0) {
877 snprint_objid(buf2, sizeof(buf2), vars->name, vars->name_length);
878 php_snmp_error(getThis(), NULL, PHP_SNMP_ERRNO_OID_NOT_INCREASING, "Error: OID not increasing: %s", buf2);
879 keepwalking = 0;
880 } else {
881 memmove((char *)(objid_query->vars[0].name), (char *)vars->name, vars->name_length * sizeof(oid));
882 objid_query->vars[0].name_length = vars->name_length;
883 keepwalking = 1;
884 }
885 }
886 }
887 if (objid_query->offset < objid_query->count) {
888 keepwalking = 1;
889 }
890 } else {
891 if (st & SNMP_CMD_WALK && response->errstat == SNMP_ERR_TOOBIG && objid_query->max_repetitions > 1) {
892 objid_query->max_repetitions /= 2;
893 snmp_free_pdu(response);
894 keepwalking = 1;
895 continue;
896 }
897 if (!(st & SNMP_CMD_WALK) || response->errstat != SNMP_ERR_NOSUCHNAME || Z_TYPE_P(return_value) == IS_TRUE || Z_TYPE_P(return_value) == IS_FALSE) {
898 for (count=1, vars = response->variables;
899 vars && count != response->errindex;
900 vars = vars->next_variable, count++);
901
902 if (st & (SNMP_CMD_GET | SNMP_CMD_GETNEXT) && response->errstat == SNMP_ERR_TOOBIG && objid_query->step > 1) {
903 objid_query->offset = ((objid_query->offset > objid_query->step) ? (objid_query->offset - objid_query->step) : 0 );
904 objid_query->step /= 2;
905 snmp_free_pdu(response);
906 keepwalking = 1;
907 continue;
908 }
909 if (vars) {
910 snprint_objid(buf, sizeof(buf), vars->name, vars->name_length);
911 php_snmp_error(getThis(), NULL, PHP_SNMP_ERRNO_ERROR_IN_REPLY, "Error in packet at '%s': %s", buf, snmp_errstring(response->errstat));
912 } else {
913 php_snmp_error(getThis(), NULL, PHP_SNMP_ERRNO_ERROR_IN_REPLY, "Error in packet at %u object_id: %s", response->errindex, snmp_errstring(response->errstat));
914 }
915 if (st & (SNMP_CMD_GET | SNMP_CMD_GETNEXT)) {
916 if ((pdu = snmp_fix_pdu(response, ((st & SNMP_CMD_GET) ? SNMP_MSG_GET : SNMP_MSG_GETNEXT) )) != NULL) {
917 snmp_free_pdu(response);
918 goto retry;
919 }
920 }
921 snmp_free_pdu(response);
922 snmp_close(ss);
923 if (objid_query->array_output) {
924 zval_ptr_dtor(return_value);
925 }
926 RETVAL_FALSE;
927 return;
928 }
929 }
930 } else if (status == STAT_TIMEOUT) {
931 php_snmp_error(getThis(), NULL, PHP_SNMP_ERRNO_TIMEOUT, "No response from %s", session->peername);
932 if (objid_query->array_output) {
933 zval_ptr_dtor(return_value);
934 }
935 snmp_close(ss);
936 RETVAL_FALSE;
937 return;
938 } else {
939 snmp_error(ss, NULL, NULL, &err);
940 php_snmp_error(getThis(), NULL, PHP_SNMP_ERRNO_GENERIC, "Fatal error: %s", err);
941 free(err);
942 if (objid_query->array_output) {
943 zval_ptr_dtor(return_value);
944 }
945 snmp_close(ss);
946 RETVAL_FALSE;
947 return;
948 }
949 if (response) {
950 snmp_free_pdu(response);
951 }
952 }
953 snmp_close(ss);
954 }
955
956
957
958
959
960
961
962 static int php_snmp_parse_oid(zval *object, int st, struct objid_query *objid_query, zval *oid, zval *type, zval *value)
963 {
964 char *pptr;
965 uint32_t idx_type = 0, idx_value = 0;
966 zval *tmp_oid, *tmp_type, *tmp_value;
967
968 if (Z_TYPE_P(oid) != IS_ARRAY) {
969 convert_to_string_ex(oid);
970 }
971
972 if (st & SNMP_CMD_SET) {
973 if (Z_TYPE_P(type) != IS_ARRAY) {
974 convert_to_string_ex(type);
975 }
976
977 if (Z_TYPE_P(value) != IS_ARRAY) {
978 convert_to_string_ex(value);
979 }
980 }
981
982 objid_query->count = 0;
983 objid_query->array_output = ((st & SNMP_CMD_WALK) ? TRUE : FALSE);
984 if (Z_TYPE_P(oid) == IS_STRING) {
985 objid_query->vars = (snmpobjarg *)emalloc(sizeof(snmpobjarg));
986 if (objid_query->vars == NULL) {
987 php_error_docref(NULL, E_WARNING, "emalloc() failed while parsing oid: %s", strerror(errno));
988 efree(objid_query->vars);
989 return FALSE;
990 }
991 objid_query->vars[objid_query->count].oid = Z_STRVAL_P(oid);
992 if (st & SNMP_CMD_SET) {
993 if (Z_TYPE_P(type) == IS_STRING && Z_TYPE_P(value) == IS_STRING) {
994 if (Z_STRLEN_P(type) != 1) {
995 php_error_docref(NULL, E_WARNING, "Bogus type '%s', should be single char, got %u", Z_STRVAL_P(type), Z_STRLEN_P(type));
996 efree(objid_query->vars);
997 return FALSE;
998 }
999 pptr = Z_STRVAL_P(type);
1000 objid_query->vars[objid_query->count].type = *pptr;
1001 objid_query->vars[objid_query->count].value = Z_STRVAL_P(value);
1002 } else {
1003 php_error_docref(NULL, E_WARNING, "Single objid and multiple type or values are not supported");
1004 efree(objid_query->vars);
1005 return FALSE;
1006 }
1007 }
1008 objid_query->count++;
1009 } else if (Z_TYPE_P(oid) == IS_ARRAY) {
1010 if (zend_hash_num_elements(Z_ARRVAL_P(oid)) == 0) {
1011 php_error_docref(NULL, E_WARNING, "Got empty OID array");
1012 return FALSE;
1013 }
1014 objid_query->vars = (snmpobjarg *)emalloc(sizeof(snmpobjarg) * zend_hash_num_elements(Z_ARRVAL_P(oid)));
1015 if (objid_query->vars == NULL) {
1016 php_error_docref(NULL, E_WARNING, "emalloc() failed while parsing oid array: %s", strerror(errno));
1017 efree(objid_query->vars);
1018 return FALSE;
1019 }
1020 objid_query->array_output = ( (st & SNMP_CMD_SET) ? FALSE : TRUE );
1021 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(oid), tmp_oid) {
1022 convert_to_string_ex(tmp_oid);
1023 objid_query->vars[objid_query->count].oid = Z_STRVAL_P(tmp_oid);
1024 if (st & SNMP_CMD_SET) {
1025 if (Z_TYPE_P(type) == IS_STRING) {
1026 pptr = Z_STRVAL_P(type);
1027 objid_query->vars[objid_query->count].type = *pptr;
1028 } else if (Z_TYPE_P(type) == IS_ARRAY) {
1029 while (idx_type < Z_ARRVAL_P(type)->nNumUsed) {
1030 tmp_type = &Z_ARRVAL_P(type)->arData[idx_type].val;
1031 if (Z_TYPE_P(tmp_type) != IS_UNDEF) {
1032 break;
1033 }
1034 idx_type++;
1035 }
1036 if (idx_type < Z_ARRVAL_P(type)->nNumUsed) {
1037 convert_to_string_ex(tmp_type);
1038 if (Z_STRLEN_P(tmp_type) != 1) {
1039 php_error_docref(NULL, E_WARNING, "'%s': bogus type '%s', should be single char, got %u", Z_STRVAL_P(tmp_oid), Z_STRVAL_P(tmp_type), Z_STRLEN_P(tmp_type));
1040 efree(objid_query->vars);
1041 return FALSE;
1042 }
1043 pptr = Z_STRVAL_P(tmp_type);
1044 objid_query->vars[objid_query->count].type = *pptr;
1045 idx_type++;
1046 } else {
1047 php_error_docref(NULL, E_WARNING, "'%s': no type set", Z_STRVAL_P(tmp_oid));
1048 efree(objid_query->vars);
1049 return FALSE;
1050 }
1051 }
1052
1053 if (Z_TYPE_P(value) == IS_STRING) {
1054 objid_query->vars[objid_query->count].value = Z_STRVAL_P(value);
1055 } else if (Z_TYPE_P(value) == IS_ARRAY) {
1056 while (idx_value < Z_ARRVAL_P(value)->nNumUsed) {
1057 tmp_value = &Z_ARRVAL_P(value)->arData[idx_value].val;
1058 if (Z_TYPE_P(tmp_value) != IS_UNDEF) {
1059 break;
1060 }
1061 idx_value++;
1062 }
1063 if (idx_value < Z_ARRVAL_P(value)->nNumUsed) {
1064 convert_to_string_ex(tmp_value);
1065 objid_query->vars[objid_query->count].value = Z_STRVAL_P(tmp_value);
1066 idx_value++;
1067 } else {
1068 php_error_docref(NULL, E_WARNING, "'%s': no value set", Z_STRVAL_P(tmp_oid));
1069 efree(objid_query->vars);
1070 return FALSE;
1071 }
1072 }
1073 }
1074 objid_query->count++;
1075 } ZEND_HASH_FOREACH_END();
1076 }
1077
1078
1079 if (st & SNMP_CMD_WALK) {
1080 if (objid_query->count > 1) {
1081 php_snmp_error(object, NULL, PHP_SNMP_ERRNO_OID_PARSING_ERROR, "Multi OID walks are not supported!");
1082 efree(objid_query->vars);
1083 return FALSE;
1084 }
1085 objid_query->vars[0].name_length = MAX_NAME_LEN;
1086 if (strlen(objid_query->vars[0].oid)) {
1087 if (!snmp_parse_oid(objid_query->vars[0].oid, objid_query->vars[0].name, &(objid_query->vars[0].name_length))) {
1088 php_snmp_error(object, NULL, PHP_SNMP_ERRNO_OID_PARSING_ERROR, "Invalid object identifier: %s", objid_query->vars[0].oid);
1089 efree(objid_query->vars);
1090 return FALSE;
1091 }
1092 } else {
1093 memmove((char *)objid_query->vars[0].name, (char *)objid_mib, sizeof(objid_mib));
1094 objid_query->vars[0].name_length = sizeof(objid_mib) / sizeof(oid);
1095 }
1096 } else {
1097 for (objid_query->offset = 0; objid_query->offset < objid_query->count; objid_query->offset++) {
1098 objid_query->vars[objid_query->offset].name_length = MAX_OID_LEN;
1099 if (!snmp_parse_oid(objid_query->vars[objid_query->offset].oid, objid_query->vars[objid_query->offset].name, &(objid_query->vars[objid_query->offset].name_length))) {
1100 php_snmp_error(object, NULL, PHP_SNMP_ERRNO_OID_PARSING_ERROR, "Invalid object identifier: %s", objid_query->vars[objid_query->offset].oid);
1101 efree(objid_query->vars);
1102 return FALSE;
1103 }
1104 }
1105 }
1106 objid_query->offset = 0;
1107 objid_query->step = objid_query->count;
1108 return (objid_query->count > 0);
1109 }
1110
1111
1112
1113
1114
1115 static int netsnmp_session_init(php_snmp_session **session_p, int version, char *hostname, char *community, int timeout, int retries)
1116 {
1117 php_snmp_session *session;
1118 char *pptr, *host_ptr;
1119 int force_ipv6 = FALSE;
1120 int n;
1121 struct sockaddr **psal;
1122 struct sockaddr **res;
1123
1124 *session_p = (php_snmp_session *)emalloc(sizeof(php_snmp_session));
1125 session = *session_p;
1126 if (session == NULL) {
1127 php_error_docref(NULL, E_WARNING, "emalloc() failed allocating session");
1128 return (-1);
1129 }
1130 memset(session, 0, sizeof(php_snmp_session));
1131
1132 snmp_sess_init(session);
1133
1134 session->version = version;
1135 session->remote_port = SNMP_PORT;
1136
1137 session->peername = emalloc(MAX_NAME_LEN);
1138 if (session->peername == NULL) {
1139 php_error_docref(NULL, E_WARNING, "emalloc() failed while copying hostname");
1140 return (-1);
1141 }
1142
1143 strlcpy(session->peername, hostname, MAX_NAME_LEN);
1144 host_ptr = session->peername;
1145
1146
1147 if (*host_ptr == '[') {
1148 force_ipv6 = TRUE;
1149 host_ptr++;
1150 if ((pptr = strchr(host_ptr, ']'))) {
1151 if (pptr[1] == ':') {
1152 session->remote_port = atoi(pptr + 2);
1153 }
1154 *pptr = '\0';
1155 } else {
1156 php_error_docref(NULL, E_WARNING, "malformed IPv6 address, closing square bracket missing");
1157 return (-1);
1158 }
1159 } else {
1160 if ((pptr = strchr(host_ptr, ':'))) {
1161 session->remote_port = atoi(pptr + 1);
1162 *pptr = '\0';
1163 }
1164 }
1165
1166
1167
1168 if ((n = php_network_getaddresses(host_ptr, SOCK_DGRAM, &psal, NULL)) == 0) {
1169
1170 return (-1);
1171 }
1172
1173
1174 *(session->peername) = '\0';
1175 res = psal;
1176 while (n-- > 0) {
1177 pptr = session->peername;
1178 #if HAVE_GETADDRINFO && HAVE_IPV6 && HAVE_INET_NTOP
1179 if (force_ipv6 && (*res)->sa_family != AF_INET6) {
1180 res++;
1181 continue;
1182 }
1183 if ((*res)->sa_family == AF_INET6) {
1184 strcpy(session->peername, "udp6:[");
1185 pptr = session->peername + strlen(session->peername);
1186 inet_ntop((*res)->sa_family, &(((struct sockaddr_in6*)(*res))->sin6_addr), pptr, MAX_NAME_LEN);
1187 strcat(pptr, "]");
1188 } else if ((*res)->sa_family == AF_INET) {
1189 inet_ntop((*res)->sa_family, &(((struct sockaddr_in*)(*res))->sin_addr), pptr, MAX_NAME_LEN);
1190 } else {
1191 res++;
1192 continue;
1193 }
1194 #else
1195 if ((*res)->sa_family != AF_INET) {
1196 res++;
1197 continue;
1198 }
1199 strcat(pptr, inet_ntoa(((struct sockaddr_in*)(*res))->sin_addr));
1200 #endif
1201 break;
1202 }
1203
1204 if (strlen(session->peername) == 0) {
1205 php_error_docref(NULL, E_WARNING, "Unknown failure while resolving '%s'", hostname);
1206 return (-1);
1207 }
1208
1209
1210
1211
1212
1213 if (session->remote_port != SNMP_PORT) {
1214 pptr = session->peername + strlen(session->peername);
1215 sprintf(pptr, ":%d", session->remote_port);
1216 }
1217
1218 php_network_freeaddresses(psal);
1219
1220 if (version == SNMP_VERSION_3) {
1221
1222 session->securityName = estrdup(community);
1223 session->securityNameLen = strlen(session->securityName);
1224 } else {
1225 session->authenticator = NULL;
1226 session->community = (u_char *)estrdup(community);
1227 session->community_len = strlen(community);
1228 }
1229
1230 session->retries = retries;
1231 session->timeout = timeout;
1232 return (0);
1233 }
1234
1235
1236
1237
1238 static int netsnmp_session_set_sec_level(struct snmp_session *s, char *level)
1239 {
1240 if (!strcasecmp(level, "noAuthNoPriv") || !strcasecmp(level, "nanp")) {
1241 s->securityLevel = SNMP_SEC_LEVEL_NOAUTH;
1242 } else if (!strcasecmp(level, "authNoPriv") || !strcasecmp(level, "anp")) {
1243 s->securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;
1244 } else if (!strcasecmp(level, "authPriv") || !strcasecmp(level, "ap")) {
1245 s->securityLevel = SNMP_SEC_LEVEL_AUTHPRIV;
1246 } else {
1247 return (-1);
1248 }
1249 return (0);
1250 }
1251
1252
1253
1254
1255 static int netsnmp_session_set_auth_protocol(struct snmp_session *s, char *prot)
1256 {
1257 if (!strcasecmp(prot, "MD5")) {
1258 s->securityAuthProto = usmHMACMD5AuthProtocol;
1259 s->securityAuthProtoLen = USM_AUTH_PROTO_MD5_LEN;
1260 } else if (!strcasecmp(prot, "SHA")) {
1261 s->securityAuthProto = usmHMACSHA1AuthProtocol;
1262 s->securityAuthProtoLen = USM_AUTH_PROTO_SHA_LEN;
1263 } else {
1264 php_error_docref(NULL, E_WARNING, "Unknown authentication protocol '%s'", prot);
1265 return (-1);
1266 }
1267 return (0);
1268 }
1269
1270
1271
1272
1273 static int netsnmp_session_set_sec_protocol(struct snmp_session *s, char *prot)
1274 {
1275 if (!strcasecmp(prot, "DES")) {
1276 s->securityPrivProto = usmDESPrivProtocol;
1277 s->securityPrivProtoLen = USM_PRIV_PROTO_DES_LEN;
1278 #ifdef HAVE_AES
1279 } else if (!strcasecmp(prot, "AES128") || !strcasecmp(prot, "AES")) {
1280 s->securityPrivProto = usmAESPrivProtocol;
1281 s->securityPrivProtoLen = USM_PRIV_PROTO_AES_LEN;
1282 #endif
1283 } else {
1284 php_error_docref(NULL, E_WARNING, "Unknown security protocol '%s'", prot);
1285 return (-1);
1286 }
1287 return (0);
1288 }
1289
1290
1291
1292
1293 static int netsnmp_session_gen_auth_key(struct snmp_session *s, char *pass)
1294 {
1295 int snmp_errno;
1296 s->securityAuthKeyLen = USM_AUTH_KU_LEN;
1297 if ((snmp_errno = generate_Ku(s->securityAuthProto, s->securityAuthProtoLen,
1298 (u_char *) pass, strlen(pass),
1299 s->securityAuthKey, &(s->securityAuthKeyLen)))) {
1300 php_error_docref(NULL, E_WARNING, "Error generating a key for authentication pass phrase '%s': %s", pass, snmp_api_errstring(snmp_errno));
1301 return (-1);
1302 }
1303 return (0);
1304 }
1305
1306
1307
1308
1309 static int netsnmp_session_gen_sec_key(struct snmp_session *s, char *pass)
1310 {
1311 int snmp_errno;
1312
1313 s->securityPrivKeyLen = USM_PRIV_KU_LEN;
1314 if ((snmp_errno = generate_Ku(s->securityAuthProto, s->securityAuthProtoLen,
1315 (u_char *)pass, strlen(pass),
1316 s->securityPrivKey, &(s->securityPrivKeyLen)))) {
1317 php_error_docref(NULL, E_WARNING, "Error generating a key for privacy pass phrase '%s': %s", pass, snmp_api_errstring(snmp_errno));
1318 return (-2);
1319 }
1320 return (0);
1321 }
1322
1323
1324
1325
1326 static int netsnmp_session_set_contextEngineID(struct snmp_session *s, char * contextEngineID)
1327 {
1328 size_t ebuf_len = 32, eout_len = 0;
1329 u_char *ebuf = (u_char *) emalloc(ebuf_len);
1330
1331 if (ebuf == NULL) {
1332 php_error_docref(NULL, E_WARNING, "malloc failure setting contextEngineID");
1333 return (-1);
1334 }
1335 if (!snmp_hex_to_binary(&ebuf, &ebuf_len, &eout_len, 1, contextEngineID)) {
1336 php_error_docref(NULL, E_WARNING, "Bad engine ID value '%s'", contextEngineID);
1337 efree(ebuf);
1338 return (-1);
1339 }
1340
1341 if (s->contextEngineID) {
1342 efree(s->contextEngineID);
1343 }
1344
1345 s->contextEngineID = ebuf;
1346 s->contextEngineIDLen = eout_len;
1347 return (0);
1348 }
1349
1350
1351
1352
1353 static int netsnmp_session_set_security(struct snmp_session *session, char *sec_level, char *auth_protocol, char *auth_passphrase, char *priv_protocol, char *priv_passphrase, char *contextName, char *contextEngineID)
1354 {
1355
1356
1357 if (netsnmp_session_set_sec_level(session, sec_level)) {
1358 php_error_docref(NULL, E_WARNING, "Invalid security level '%s'", sec_level);
1359 return (-1);
1360 }
1361
1362 if (session->securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV || session->securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
1363
1364
1365 if (netsnmp_session_set_auth_protocol(session, auth_protocol)) {
1366
1367 return (-1);
1368 }
1369
1370
1371 if (netsnmp_session_gen_auth_key(session, auth_passphrase)) {
1372
1373 return (-1);
1374 }
1375
1376 if (session->securityLevel == SNMP_SEC_LEVEL_AUTHPRIV) {
1377
1378 if (netsnmp_session_set_sec_protocol(session, priv_protocol)) {
1379
1380 return (-1);
1381 }
1382
1383
1384 if (netsnmp_session_gen_sec_key(session, priv_passphrase)) {
1385
1386 return (-1);
1387 }
1388 }
1389 }
1390
1391
1392 if (contextName) {
1393 session->contextName = contextName;
1394 session->contextNameLen = strlen(contextName);
1395 }
1396
1397
1398 if (contextEngineID && strlen(contextEngineID) && netsnmp_session_set_contextEngineID(session, contextEngineID)) {
1399
1400 return (-1);
1401 }
1402
1403 return (0);
1404 }
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414 static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
1415 {
1416 zval *oid, *value, *type;
1417 char *a1, *a2, *a3, *a4, *a5, *a6, *a7;
1418 size_t a1_len, a2_len, a3_len, a4_len, a5_len, a6_len, a7_len;
1419 zend_bool use_orignames = 0, suffix_keys = 0;
1420 zend_long timeout = SNMP_DEFAULT_TIMEOUT;
1421 zend_long retries = SNMP_DEFAULT_RETRIES;
1422 int argc = ZEND_NUM_ARGS();
1423 struct objid_query objid_query;
1424 php_snmp_session *session;
1425 int session_less_mode = (getThis() == NULL);
1426 php_snmp_object *snmp_object;
1427 php_snmp_object glob_snmp_object;
1428
1429 objid_query.max_repetitions = -1;
1430 objid_query.non_repeaters = 0;
1431 objid_query.valueretrieval = SNMP_G(valueretrieval);
1432 objid_query.oid_increasing_check = TRUE;
1433
1434 if (session_less_mode) {
1435 if (version == SNMP_VERSION_3) {
1436 if (st & SNMP_CMD_SET) {
1437 if (zend_parse_parameters(argc, "ssssssszzz|ll", &a1, &a1_len, &a2, &a2_len, &a3, &a3_len,
1438 &a4, &a4_len, &a5, &a5_len, &a6, &a6_len, &a7, &a7_len, &oid, &type, &value, &timeout, &retries) == FAILURE) {
1439 RETURN_FALSE;
1440 }
1441 } else {
1442
1443
1444
1445
1446 if (zend_parse_parameters(argc, "sssssssz|ll", &a1, &a1_len, &a2, &a2_len, &a3, &a3_len,
1447 &a4, &a4_len, &a5, &a5_len, &a6, &a6_len, &a7, &a7_len, &oid, &timeout, &retries) == FAILURE) {
1448 RETURN_FALSE;
1449 }
1450 }
1451 } else {
1452 if (st & SNMP_CMD_SET) {
1453 if (zend_parse_parameters(argc, "sszzz|ll", &a1, &a1_len, &a2, &a2_len, &oid, &type, &value, &timeout, &retries) == FAILURE) {
1454 RETURN_FALSE;
1455 }
1456 } else {
1457
1458
1459
1460
1461 if (zend_parse_parameters(argc, "ssz|ll", &a1, &a1_len, &a2, &a2_len, &oid, &timeout, &retries) == FAILURE) {
1462 RETURN_FALSE;
1463 }
1464 }
1465 }
1466 } else {
1467 if (st & SNMP_CMD_SET) {
1468 if (zend_parse_parameters(argc, "zzz", &oid, &type, &value) == FAILURE) {
1469 RETURN_FALSE;
1470 }
1471 } else if (st & SNMP_CMD_WALK) {
1472 if (zend_parse_parameters(argc, "z|bll", &oid, &suffix_keys, &(objid_query.max_repetitions), &(objid_query.non_repeaters)) == FAILURE) {
1473 RETURN_FALSE;
1474 }
1475 if (suffix_keys) {
1476 st |= SNMP_USE_SUFFIX_AS_KEYS;
1477 }
1478 } else if (st & SNMP_CMD_GET) {
1479 if (zend_parse_parameters(argc, "z|b", &oid, &use_orignames) == FAILURE) {
1480 RETURN_FALSE;
1481 }
1482 if (use_orignames) {
1483 st |= SNMP_ORIGINAL_NAMES_AS_KEYS;
1484 }
1485 } else {
1486
1487
1488 if (zend_parse_parameters(argc, "z", &oid) == FAILURE) {
1489 RETURN_FALSE;
1490 }
1491 }
1492 }
1493
1494 if (!php_snmp_parse_oid(getThis(), st, &objid_query, oid, type, value)) {
1495 RETURN_FALSE;
1496 }
1497
1498 if (session_less_mode) {
1499 if (netsnmp_session_init(&session, version, a1, a2, timeout, retries)) {
1500 efree(objid_query.vars);
1501 netsnmp_session_free(&session);
1502 RETURN_FALSE;
1503 }
1504 if (version == SNMP_VERSION_3 && netsnmp_session_set_security(session, a3, a4, a5, a6, a7, NULL, NULL)) {
1505 efree(objid_query.vars);
1506 netsnmp_session_free(&session);
1507
1508 RETURN_FALSE;
1509 }
1510 } else {
1511 zval *object = getThis();
1512 snmp_object = Z_SNMP_P(object);
1513 session = snmp_object->session;
1514 if (!session) {
1515 php_error_docref(NULL, E_WARNING, "Invalid or uninitialized SNMP object");
1516 efree(objid_query.vars);
1517 RETURN_FALSE;
1518 }
1519
1520 if (snmp_object->max_oids > 0) {
1521 objid_query.step = snmp_object->max_oids;
1522 if (objid_query.max_repetitions < 0) {
1523 objid_query.max_repetitions = snmp_object->max_oids;
1524 }
1525 }
1526 objid_query.oid_increasing_check = snmp_object->oid_increasing_check;
1527 objid_query.valueretrieval = snmp_object->valueretrieval;
1528 glob_snmp_object.enum_print = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM);
1529 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM, snmp_object->enum_print);
1530 glob_snmp_object.quick_print = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT);
1531 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, snmp_object->quick_print);
1532 glob_snmp_object.oid_output_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
1533 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, snmp_object->oid_output_format);
1534 }
1535
1536 if (objid_query.max_repetitions < 0) {
1537 objid_query.max_repetitions = 20;
1538 }
1539
1540 php_snmp_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU, st, session, &objid_query);
1541
1542 efree(objid_query.vars);
1543
1544 if (session_less_mode) {
1545 netsnmp_session_free(&session);
1546 } else {
1547 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM, glob_snmp_object.enum_print);
1548 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, glob_snmp_object.quick_print);
1549 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, glob_snmp_object.oid_output_format);
1550 }
1551 }
1552
1553
1554
1555
1556 PHP_FUNCTION(snmpget)
1557 {
1558 php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GET, SNMP_VERSION_1);
1559 }
1560
1561
1562
1563
1564 PHP_FUNCTION(snmpgetnext)
1565 {
1566 php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GETNEXT, SNMP_VERSION_1);
1567 }
1568
1569
1570
1571
1572 PHP_FUNCTION(snmpwalk)
1573 {
1574 php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, (SNMP_CMD_WALK | SNMP_NUMERIC_KEYS), SNMP_VERSION_1);
1575 }
1576
1577
1578
1579
1580 PHP_FUNCTION(snmprealwalk)
1581 {
1582 php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_WALK, SNMP_VERSION_1);
1583 }
1584
1585
1586
1587
1588 PHP_FUNCTION(snmpset)
1589 {
1590 php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_SET, SNMP_VERSION_1);
1591 }
1592
1593
1594
1595
1596 PHP_FUNCTION(snmp_get_quick_print)
1597 {
1598 if (zend_parse_parameters_none() == FAILURE) {
1599 return;
1600 }
1601
1602 RETURN_BOOL(netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT));
1603 }
1604
1605
1606
1607
1608 PHP_FUNCTION(snmp_set_quick_print)
1609 {
1610 zend_long a1;
1611
1612 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &a1) == FAILURE) {
1613 RETURN_FALSE;
1614 }
1615
1616 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, (int)a1);
1617 RETURN_TRUE;
1618 }
1619
1620
1621
1622
1623 PHP_FUNCTION(snmp_set_enum_print)
1624 {
1625 zend_long a1;
1626
1627 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &a1) == FAILURE) {
1628 RETURN_FALSE;
1629 }
1630
1631 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM, (int) a1);
1632 RETURN_TRUE;
1633 }
1634
1635
1636
1637
1638 PHP_FUNCTION(snmp_set_oid_output_format)
1639 {
1640 zend_long a1;
1641
1642 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &a1) == FAILURE) {
1643 RETURN_FALSE;
1644 }
1645
1646 switch((int) a1) {
1647 case NETSNMP_OID_OUTPUT_SUFFIX:
1648 case NETSNMP_OID_OUTPUT_MODULE:
1649 case NETSNMP_OID_OUTPUT_FULL:
1650 case NETSNMP_OID_OUTPUT_NUMERIC:
1651 case NETSNMP_OID_OUTPUT_UCD:
1652 case NETSNMP_OID_OUTPUT_NONE:
1653 netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT, a1);
1654 RETURN_TRUE;
1655 break;
1656 default:
1657 php_error_docref(NULL, E_WARNING, "Unknown SNMP output print format '%d'", (int) a1);
1658 RETURN_FALSE;
1659 break;
1660 }
1661 }
1662
1663
1664
1665
1666 PHP_FUNCTION(snmp2_get)
1667 {
1668 php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GET, SNMP_VERSION_2c);
1669 }
1670
1671
1672
1673
1674 PHP_FUNCTION(snmp2_getnext)
1675 {
1676 php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GETNEXT, SNMP_VERSION_2c);
1677 }
1678
1679
1680
1681
1682 PHP_FUNCTION(snmp2_walk)
1683 {
1684 php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, (SNMP_CMD_WALK | SNMP_NUMERIC_KEYS), SNMP_VERSION_2c);
1685 }
1686
1687
1688
1689
1690 PHP_FUNCTION(snmp2_real_walk)
1691 {
1692 php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_WALK, SNMP_VERSION_2c);
1693 }
1694
1695
1696
1697
1698 PHP_FUNCTION(snmp2_set)
1699 {
1700 php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_SET, SNMP_VERSION_2c);
1701 }
1702
1703
1704
1705
1706 PHP_FUNCTION(snmp3_get)
1707 {
1708 php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GET, SNMP_VERSION_3);
1709 }
1710
1711
1712
1713
1714 PHP_FUNCTION(snmp3_getnext)
1715 {
1716 php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GETNEXT, SNMP_VERSION_3);
1717 }
1718
1719
1720
1721
1722 PHP_FUNCTION(snmp3_walk)
1723 {
1724 php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, (SNMP_CMD_WALK | SNMP_NUMERIC_KEYS), SNMP_VERSION_3);
1725 }
1726
1727
1728
1729
1730 PHP_FUNCTION(snmp3_real_walk)
1731 {
1732 php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_WALK, SNMP_VERSION_3);
1733 }
1734
1735
1736
1737
1738 PHP_FUNCTION(snmp3_set)
1739 {
1740 php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_SET, SNMP_VERSION_3);
1741 }
1742
1743
1744
1745
1746 PHP_FUNCTION(snmp_set_valueretrieval)
1747 {
1748 zend_long method;
1749
1750 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &method) == FAILURE) {
1751 RETURN_FALSE;
1752 }
1753
1754 if (method >= 0 && method <= (SNMP_VALUE_LIBRARY|SNMP_VALUE_PLAIN|SNMP_VALUE_OBJECT)) {
1755 SNMP_G(valueretrieval) = method;
1756 RETURN_TRUE;
1757 } else {
1758 php_error_docref(NULL, E_WARNING, "Unknown SNMP value retrieval method '%pd'", method);
1759 RETURN_FALSE;
1760 }
1761 }
1762
1763
1764
1765
1766 PHP_FUNCTION(snmp_get_valueretrieval)
1767 {
1768 if (zend_parse_parameters_none() == FAILURE) {
1769 RETURN_FALSE;
1770 }
1771
1772 RETURN_LONG(SNMP_G(valueretrieval));
1773 }
1774
1775
1776
1777
1778 PHP_FUNCTION(snmp_read_mib)
1779 {
1780 char *filename;
1781 size_t filename_len;
1782
1783 if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &filename, &filename_len) == FAILURE) {
1784 RETURN_FALSE;
1785 }
1786
1787 if (!read_mib(filename)) {
1788 char *error = strerror(errno);
1789 php_error_docref(NULL, E_WARNING, "Error while reading MIB file '%s': %s", filename, error);
1790 RETURN_FALSE;
1791 }
1792 RETURN_TRUE;
1793 }
1794
1795
1796
1797
1798 PHP_METHOD(snmp, __construct)
1799 {
1800 php_snmp_object *snmp_object;
1801 zval *object = getThis();
1802 char *a1, *a2;
1803 size_t a1_len, a2_len;
1804 zend_long timeout = SNMP_DEFAULT_TIMEOUT;
1805 zend_long retries = SNMP_DEFAULT_RETRIES;
1806 zend_long version = SNMP_DEFAULT_VERSION;
1807 int argc = ZEND_NUM_ARGS();
1808
1809 snmp_object = Z_SNMP_P(object);
1810
1811 if (zend_parse_parameters_throw(argc, "lss|ll", &version, &a1, &a1_len, &a2, &a2_len, &timeout, &retries) == FAILURE) {
1812 return;
1813 }
1814
1815 switch (version) {
1816 case SNMP_VERSION_1:
1817 case SNMP_VERSION_2c:
1818 case SNMP_VERSION_3:
1819 break;
1820 default:
1821 zend_throw_exception(zend_ce_exception, "Unknown SNMP protocol version", 0);
1822 return;
1823 }
1824
1825
1826 if (snmp_object->session) {
1827 netsnmp_session_free(&(snmp_object->session));
1828 }
1829
1830 if (netsnmp_session_init(&(snmp_object->session), version, a1, a2, timeout, retries)) {
1831 return;
1832 }
1833 snmp_object->max_oids = 0;
1834 snmp_object->valueretrieval = SNMP_G(valueretrieval);
1835 snmp_object->enum_print = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM);
1836 snmp_object->oid_output_format = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT);
1837 snmp_object->quick_print = netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT);
1838 snmp_object->oid_increasing_check = TRUE;
1839 snmp_object->exceptions_enabled = 0;
1840 }
1841
1842
1843
1844
1845 PHP_METHOD(snmp, close)
1846 {
1847 php_snmp_object *snmp_object;
1848 zval *object = getThis();
1849
1850 snmp_object = Z_SNMP_P(object);
1851
1852 if (zend_parse_parameters_none() == FAILURE) {
1853 RETURN_FALSE;
1854 }
1855
1856 netsnmp_session_free(&(snmp_object->session));
1857
1858 RETURN_TRUE;
1859 }
1860
1861
1862
1863
1864 PHP_METHOD(snmp, get)
1865 {
1866 php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GET, (-1));
1867 }
1868
1869
1870
1871
1872 PHP_METHOD(snmp, getnext)
1873 {
1874 php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_GETNEXT, (-1));
1875 }
1876
1877
1878
1879
1880 PHP_METHOD(snmp, walk)
1881 {
1882 php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_WALK, (-1));
1883 }
1884
1885
1886
1887
1888 PHP_METHOD(snmp, set)
1889 {
1890 php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU, SNMP_CMD_SET, (-1));
1891 }
1892
1893
1894
1895
1896 PHP_METHOD(snmp, setSecurity)
1897 {
1898 php_snmp_object *snmp_object;
1899 zval *object = getThis();
1900 char *a1 = "", *a2 = "", *a3 = "", *a4 = "", *a5 = "", *a6 = "", *a7 = "";
1901 size_t a1_len = 0, a2_len = 0, a3_len = 0, a4_len = 0, a5_len = 0, a6_len = 0, a7_len = 0;
1902 int argc = ZEND_NUM_ARGS();
1903
1904 snmp_object = Z_SNMP_P(object);
1905
1906 if (zend_parse_parameters(argc, "s|ssssss", &a1, &a1_len, &a2, &a2_len, &a3, &a3_len,
1907 &a4, &a4_len, &a5, &a5_len, &a6, &a6_len, &a7, &a7_len) == FAILURE) {
1908 RETURN_FALSE;
1909 }
1910
1911 if (netsnmp_session_set_security(snmp_object->session, a1, a2, a3, a4, a5, a6, a7)) {
1912
1913 RETURN_FALSE;
1914 }
1915 RETURN_TRUE;
1916 }
1917
1918
1919
1920
1921 PHP_METHOD(snmp, getErrno)
1922 {
1923 php_snmp_object *snmp_object;
1924 zval *object = getThis();
1925
1926 snmp_object = Z_SNMP_P(object);
1927
1928 RETVAL_LONG(snmp_object->snmp_errno);
1929 return;
1930 }
1931
1932
1933
1934
1935 PHP_METHOD(snmp, getError)
1936 {
1937 php_snmp_object *snmp_object;
1938 zval *object = getThis();
1939
1940 snmp_object = Z_SNMP_P(object);
1941
1942 RETURN_STRING(snmp_object->snmp_errstr);
1943 }
1944
1945
1946
1947 void php_snmp_add_property(HashTable *h, const char *name, size_t name_length, php_snmp_read_t read_func, php_snmp_write_t write_func)
1948 {
1949 php_snmp_prop_handler p;
1950
1951 p.name = (char*) name;
1952 p.name_length = name_length;
1953 p.read_func = (read_func) ? read_func : NULL;
1954 p.write_func = (write_func) ? write_func : NULL;
1955 zend_hash_str_add_mem(h, (char *)name, name_length, &p, sizeof(php_snmp_prop_handler));
1956 }
1957
1958
1959
1960
1961 zval *php_snmp_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv)
1962 {
1963 zval tmp_member;
1964 zval *retval;
1965 php_snmp_object *obj;
1966 php_snmp_prop_handler *hnd;
1967 int ret;
1968
1969 obj = Z_SNMP_P(object);
1970
1971 if (Z_TYPE_P(member) != IS_STRING) {
1972 ZVAL_COPY(&tmp_member, member);
1973 convert_to_string(&tmp_member);
1974 member = &tmp_member;
1975 }
1976
1977 hnd = zend_hash_find_ptr(&php_snmp_properties, Z_STR_P(member));
1978
1979 if (hnd && hnd->read_func) {
1980 ret = hnd->read_func(obj, rv);
1981 if (ret == SUCCESS) {
1982 retval = rv;
1983 } else {
1984 retval = &EG(uninitialized_zval);
1985 }
1986 } else {
1987 zend_object_handlers * std_hnd = zend_get_std_object_handlers();
1988 retval = std_hnd->read_property(object, member, type, cache_slot, rv);
1989 }
1990
1991 if (member == &tmp_member) {
1992 zval_ptr_dtor(member);
1993 }
1994
1995 return retval;
1996 }
1997
1998
1999
2000
2001 void php_snmp_write_property(zval *object, zval *member, zval *value, void **cache_slot)
2002 {
2003 zval tmp_member;
2004 php_snmp_object *obj;
2005 php_snmp_prop_handler *hnd;
2006
2007 if (Z_TYPE_P(member) != IS_STRING) {
2008 ZVAL_COPY(&tmp_member, member);
2009 convert_to_string(&tmp_member);
2010 member = &tmp_member;
2011 }
2012
2013 obj = Z_SNMP_P(object);
2014
2015 hnd = zend_hash_find_ptr(&php_snmp_properties, Z_STR_P(member));
2016
2017 if (hnd && hnd->write_func) {
2018 hnd->write_func(obj, value);
2019
2020
2021
2022
2023
2024
2025 } else {
2026 zend_object_handlers * std_hnd = zend_get_std_object_handlers();
2027 std_hnd->write_property(object, member, value, cache_slot);
2028 }
2029
2030 if (member == &tmp_member) {
2031 zval_ptr_dtor(member);
2032 }
2033 }
2034
2035
2036
2037
2038 static int php_snmp_has_property(zval *object, zval *member, int has_set_exists, void **cache_slot)
2039 {
2040 zval rv;
2041 php_snmp_prop_handler *hnd;
2042 int ret = 0;
2043
2044 if ((hnd = zend_hash_find_ptr(&php_snmp_properties, Z_STR_P(member))) != NULL) {
2045 switch (has_set_exists) {
2046 case 2:
2047 ret = 1;
2048 break;
2049 case 0: {
2050 zval *value = php_snmp_read_property(object, member, BP_VAR_IS, cache_slot, &rv);
2051 if (value != &EG(uninitialized_zval)) {
2052 ret = Z_TYPE_P(value) != IS_NULL? 1 : 0;
2053 zval_ptr_dtor(value);
2054 }
2055 break;
2056 }
2057 default: {
2058 zval *value = php_snmp_read_property(object, member, BP_VAR_IS, cache_slot, &rv);
2059 if (value != &EG(uninitialized_zval)) {
2060 convert_to_boolean(value);
2061 ret = Z_TYPE_P(value) == IS_TRUE? 1:0;
2062 }
2063 break;
2064 }
2065 }
2066 } else {
2067 zend_object_handlers *std_hnd = zend_get_std_object_handlers();
2068 ret = std_hnd->has_property(object, member, has_set_exists, cache_slot);
2069 }
2070 return ret;
2071 }
2072
2073
2074
2075
2076 static HashTable *php_snmp_get_properties(zval *object)
2077 {
2078 php_snmp_object *obj;
2079 php_snmp_prop_handler *hnd;
2080 HashTable *props;
2081 zval rv;
2082 zend_string *key;
2083 zend_ulong num_key;
2084
2085 obj = Z_SNMP_P(object);
2086 props = zend_std_get_properties(object);
2087
2088 ZEND_HASH_FOREACH_KEY_PTR(&php_snmp_properties, num_key, key, hnd) {
2089 if (!hnd->read_func || hnd->read_func(obj, &rv) != SUCCESS) {
2090 ZVAL_NULL(&rv);
2091 }
2092 zend_hash_update(props, key, &rv);
2093 } ZEND_HASH_FOREACH_END();
2094
2095 return obj->zo.properties;
2096 }
2097
2098
2099
2100 static int php_snmp_read_info(php_snmp_object *snmp_object, zval *retval)
2101 {
2102 zval val;
2103
2104 array_init(retval);
2105
2106 if (snmp_object->session == NULL) {
2107 return SUCCESS;
2108 }
2109
2110 ZVAL_STRINGL(&val, snmp_object->session->peername, strlen(snmp_object->session->peername));
2111 add_assoc_zval(retval, "hostname", &val);
2112
2113 ZVAL_LONG(&val, snmp_object->session->remote_port);
2114 add_assoc_zval(retval, "port", &val);
2115
2116 ZVAL_LONG(&val, snmp_object->session->timeout);
2117 add_assoc_zval(retval, "timeout", &val);
2118
2119 ZVAL_LONG(&val, snmp_object->session->retries);
2120 add_assoc_zval(retval, "retries", &val);
2121
2122 return SUCCESS;
2123 }
2124
2125
2126
2127 static int php_snmp_read_max_oids(php_snmp_object *snmp_object, zval *retval)
2128 {
2129 if (snmp_object->max_oids > 0) {
2130 ZVAL_LONG(retval, snmp_object->max_oids);
2131 } else {
2132 ZVAL_NULL(retval);
2133 }
2134 return SUCCESS;
2135 }
2136
2137
2138 #define PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION(name) \
2139 static int php_snmp_read_##name(php_snmp_object *snmp_object, zval *retval) \
2140 { \
2141 ZVAL_BOOL(retval, snmp_object->name); \
2142 return SUCCESS; \
2143 }
2144
2145 PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION(oid_increasing_check)
2146 PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION(quick_print)
2147 PHP_SNMP_BOOL_PROPERTY_READER_FUNCTION(enum_print)
2148
2149 #define PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(name) \
2150 static int php_snmp_read_##name(php_snmp_object *snmp_object, zval *retval) \
2151 { \
2152 ZVAL_LONG(retval, snmp_object->name); \
2153 return SUCCESS; \
2154 }
2155
2156 PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(valueretrieval)
2157 PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(oid_output_format)
2158 PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(exceptions_enabled)
2159
2160
2161 static int php_snmp_write_info(php_snmp_object *snmp_object, zval *newval)
2162 {
2163 php_error_docref(NULL, E_WARNING, "info property is read-only");
2164 return FAILURE;
2165 }
2166
2167
2168
2169 static int php_snmp_write_max_oids(php_snmp_object *snmp_object, zval *newval)
2170 {
2171 zval ztmp;
2172 int ret = SUCCESS;
2173
2174 if (Z_TYPE_P(newval) == IS_NULL) {
2175 snmp_object->max_oids = 0;
2176 return ret;
2177 }
2178
2179 if (Z_TYPE_P(newval) != IS_LONG) {
2180 ztmp = *newval;
2181 zval_copy_ctor(&ztmp);
2182 convert_to_long(&ztmp);
2183 newval = &ztmp;
2184 }
2185
2186 if (Z_LVAL_P(newval) > 0) {
2187 snmp_object->max_oids = Z_LVAL_P(newval);
2188 } else {
2189 php_error_docref(NULL, E_WARNING, "max_oids should be positive integer or NULL, got %pd", Z_LVAL_P(newval));
2190 }
2191
2192 if (newval == &ztmp) {
2193 zval_dtor(newval);
2194 }
2195
2196 return ret;
2197 }
2198
2199
2200
2201 static int php_snmp_write_valueretrieval(php_snmp_object *snmp_object, zval *newval)
2202 {
2203 zval ztmp;
2204 int ret = SUCCESS;
2205
2206 if (Z_TYPE_P(newval) != IS_LONG) {
2207 ztmp = *newval;
2208 zval_copy_ctor(&ztmp);
2209 convert_to_long(&ztmp);
2210 newval = &ztmp;
2211 }
2212
2213 if (Z_LVAL_P(newval) >= 0 && Z_LVAL_P(newval) <= (SNMP_VALUE_LIBRARY|SNMP_VALUE_PLAIN|SNMP_VALUE_OBJECT)) {
2214 snmp_object->valueretrieval = Z_LVAL_P(newval);
2215 } else {
2216 php_error_docref(NULL, E_WARNING, "Unknown SNMP value retrieval method '%pd'", Z_LVAL_P(newval));
2217 ret = FAILURE;
2218 }
2219
2220 if (newval == &ztmp) {
2221 zval_dtor(newval);
2222 }
2223
2224 return ret;
2225 }
2226
2227
2228 #define PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(name) \
2229 static int php_snmp_write_##name(php_snmp_object *snmp_object, zval *newval) \
2230 { \
2231 zval ztmp; \
2232 ZVAL_COPY(&ztmp, newval); \
2233 convert_to_boolean(&ztmp); \
2234 newval = &ztmp; \
2235 \
2236 snmp_object->name = Z_TYPE_P(newval) == IS_TRUE? 1 : 0; \
2237 \
2238 return SUCCESS; \
2239 }
2240
2241 PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(quick_print)
2242 PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(enum_print)
2243 PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(oid_increasing_check)
2244
2245
2246 static int php_snmp_write_oid_output_format(php_snmp_object *snmp_object, zval *newval)
2247 {
2248 zval ztmp;
2249 int ret = SUCCESS;
2250 if (Z_TYPE_P(newval) != IS_LONG) {
2251 ZVAL_COPY(&ztmp, newval);
2252 convert_to_long(&ztmp);
2253 newval = &ztmp;
2254 }
2255
2256 switch(Z_LVAL_P(newval)) {
2257 case NETSNMP_OID_OUTPUT_SUFFIX:
2258 case NETSNMP_OID_OUTPUT_MODULE:
2259 case NETSNMP_OID_OUTPUT_FULL:
2260 case NETSNMP_OID_OUTPUT_NUMERIC:
2261 case NETSNMP_OID_OUTPUT_UCD:
2262 case NETSNMP_OID_OUTPUT_NONE:
2263 snmp_object->oid_output_format = Z_LVAL_P(newval);
2264 break;
2265 default:
2266 php_error_docref(NULL, E_WARNING, "Unknown SNMP output print format '%pd'", Z_LVAL_P(newval));
2267 ret = FAILURE;
2268 break;
2269 }
2270
2271 if (newval == &ztmp) {
2272 zval_ptr_dtor(newval);
2273 }
2274 return ret;
2275 }
2276
2277
2278
2279 static int php_snmp_write_exceptions_enabled(php_snmp_object *snmp_object, zval *newval)
2280 {
2281 zval ztmp;
2282 int ret = SUCCESS;
2283 if (Z_TYPE_P(newval) != IS_LONG) {
2284 ZVAL_COPY(&ztmp, newval);
2285 convert_to_long(&ztmp);
2286 newval = &ztmp;
2287 }
2288
2289 snmp_object->exceptions_enabled = Z_LVAL_P(newval);
2290
2291 if (newval == &ztmp) {
2292 zval_ptr_dtor(newval);
2293 }
2294 return ret;
2295 }
2296
2297
2298 static void free_php_snmp_properties(zval *el)
2299 {
2300 pefree(Z_PTR_P(el), 1);
2301 }
2302
2303
2304
2305 static zend_function_entry php_snmp_class_methods[] = {
2306 PHP_ME(snmp, __construct, arginfo_snmp_create, ZEND_ACC_PUBLIC)
2307 PHP_ME(snmp, close, arginfo_snmp_void, ZEND_ACC_PUBLIC)
2308 PHP_ME(snmp, setSecurity, arginfo_snmp_setSecurity, ZEND_ACC_PUBLIC)
2309
2310 PHP_ME(snmp, get, arginfo_snmp_get, ZEND_ACC_PUBLIC)
2311 PHP_ME(snmp, getnext, arginfo_snmp_get, ZEND_ACC_PUBLIC)
2312 PHP_ME(snmp, walk, arginfo_snmp_walk, ZEND_ACC_PUBLIC)
2313 PHP_ME(snmp, set, arginfo_snmp_set, ZEND_ACC_PUBLIC)
2314 PHP_ME(snmp, getErrno, arginfo_snmp_void, ZEND_ACC_PUBLIC)
2315 PHP_ME(snmp, getError, arginfo_snmp_void, ZEND_ACC_PUBLIC)
2316
2317 PHP_FE_END
2318 };
2319
2320 #define PHP_SNMP_PROPERTY_ENTRY_RECORD(name) \
2321 { "" #name "", sizeof("" #name "") - 1, php_snmp_read_##name, php_snmp_write_##name }
2322
2323 const php_snmp_prop_handler php_snmp_property_entries[] = {
2324 PHP_SNMP_PROPERTY_ENTRY_RECORD(info),
2325 PHP_SNMP_PROPERTY_ENTRY_RECORD(max_oids),
2326 PHP_SNMP_PROPERTY_ENTRY_RECORD(valueretrieval),
2327 PHP_SNMP_PROPERTY_ENTRY_RECORD(quick_print),
2328 PHP_SNMP_PROPERTY_ENTRY_RECORD(enum_print),
2329 PHP_SNMP_PROPERTY_ENTRY_RECORD(oid_output_format),
2330 PHP_SNMP_PROPERTY_ENTRY_RECORD(oid_increasing_check),
2331 PHP_SNMP_PROPERTY_ENTRY_RECORD(exceptions_enabled),
2332 { NULL, 0, NULL, NULL}
2333 };
2334
2335
2336
2337
2338 PHP_MINIT_FUNCTION(snmp)
2339 {
2340 netsnmp_log_handler *logh;
2341 zend_class_entry ce, cex;
2342
2343 le_snmp_session = zend_register_list_destructors_ex(php_snmp_session_destructor, NULL, PHP_SNMP_SESSION_RES_NAME, module_number);
2344
2345 init_snmp("snmpapp");
2346
2347 #ifdef NETSNMP_DS_LIB_DONT_PERSIST_STATE
2348
2349 netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_PERSIST_STATE, 1);
2350 #endif
2351
2352
2353 shutdown_snmp_logging();
2354 logh = netsnmp_register_loghandler(NETSNMP_LOGHANDLER_NONE, LOG_ERR);
2355 if (logh) {
2356 logh->pri_max = LOG_ERR;
2357 }
2358
2359 memcpy(&php_snmp_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
2360 php_snmp_object_handlers.read_property = php_snmp_read_property;
2361 php_snmp_object_handlers.write_property = php_snmp_write_property;
2362 php_snmp_object_handlers.has_property = php_snmp_has_property;
2363 php_snmp_object_handlers.get_properties = php_snmp_get_properties;
2364
2365
2366 INIT_CLASS_ENTRY(ce, "SNMP", php_snmp_class_methods);
2367 ce.create_object = php_snmp_object_new;
2368 php_snmp_object_handlers.offset = XtOffsetOf(php_snmp_object, zo);
2369 php_snmp_object_handlers.clone_obj = NULL;
2370 php_snmp_object_handlers.free_obj = php_snmp_object_free_storage;
2371 php_snmp_ce = zend_register_internal_class(&ce);
2372
2373
2374 zend_hash_init(&php_snmp_properties, 0, NULL, free_php_snmp_properties, 1);
2375 PHP_SNMP_ADD_PROPERTIES(&php_snmp_properties, php_snmp_property_entries);
2376
2377 REGISTER_LONG_CONSTANT("SNMP_OID_OUTPUT_SUFFIX", NETSNMP_OID_OUTPUT_SUFFIX, CONST_CS | CONST_PERSISTENT);
2378 REGISTER_LONG_CONSTANT("SNMP_OID_OUTPUT_MODULE", NETSNMP_OID_OUTPUT_MODULE, CONST_CS | CONST_PERSISTENT);
2379 REGISTER_LONG_CONSTANT("SNMP_OID_OUTPUT_FULL", NETSNMP_OID_OUTPUT_FULL, CONST_CS | CONST_PERSISTENT);
2380 REGISTER_LONG_CONSTANT("SNMP_OID_OUTPUT_NUMERIC", NETSNMP_OID_OUTPUT_NUMERIC, CONST_CS | CONST_PERSISTENT);
2381 REGISTER_LONG_CONSTANT("SNMP_OID_OUTPUT_UCD", NETSNMP_OID_OUTPUT_UCD, CONST_CS | CONST_PERSISTENT);
2382 REGISTER_LONG_CONSTANT("SNMP_OID_OUTPUT_NONE", NETSNMP_OID_OUTPUT_NONE, CONST_CS | CONST_PERSISTENT);
2383
2384 REGISTER_LONG_CONSTANT("SNMP_VALUE_LIBRARY", SNMP_VALUE_LIBRARY, CONST_CS | CONST_PERSISTENT);
2385 REGISTER_LONG_CONSTANT("SNMP_VALUE_PLAIN", SNMP_VALUE_PLAIN, CONST_CS | CONST_PERSISTENT);
2386 REGISTER_LONG_CONSTANT("SNMP_VALUE_OBJECT", SNMP_VALUE_OBJECT, CONST_CS | CONST_PERSISTENT);
2387
2388 REGISTER_LONG_CONSTANT("SNMP_BIT_STR", ASN_BIT_STR, CONST_CS | CONST_PERSISTENT);
2389 REGISTER_LONG_CONSTANT("SNMP_OCTET_STR", ASN_OCTET_STR, CONST_CS | CONST_PERSISTENT);
2390 REGISTER_LONG_CONSTANT("SNMP_OPAQUE", ASN_OPAQUE, CONST_CS | CONST_PERSISTENT);
2391 REGISTER_LONG_CONSTANT("SNMP_NULL", ASN_NULL, CONST_CS | CONST_PERSISTENT);
2392 REGISTER_LONG_CONSTANT("SNMP_OBJECT_ID", ASN_OBJECT_ID, CONST_CS | CONST_PERSISTENT);
2393 REGISTER_LONG_CONSTANT("SNMP_IPADDRESS", ASN_IPADDRESS, CONST_CS | CONST_PERSISTENT);
2394 REGISTER_LONG_CONSTANT("SNMP_COUNTER", ASN_GAUGE, CONST_CS | CONST_PERSISTENT);
2395 REGISTER_LONG_CONSTANT("SNMP_UNSIGNED", ASN_UNSIGNED, CONST_CS | CONST_PERSISTENT);
2396 REGISTER_LONG_CONSTANT("SNMP_TIMETICKS", ASN_TIMETICKS, CONST_CS | CONST_PERSISTENT);
2397 REGISTER_LONG_CONSTANT("SNMP_UINTEGER", ASN_UINTEGER, CONST_CS | CONST_PERSISTENT);
2398 REGISTER_LONG_CONSTANT("SNMP_INTEGER", ASN_INTEGER, CONST_CS | CONST_PERSISTENT);
2399 REGISTER_LONG_CONSTANT("SNMP_COUNTER64", ASN_COUNTER64, CONST_CS | CONST_PERSISTENT);
2400
2401 REGISTER_SNMP_CLASS_CONST_LONG("VERSION_1", SNMP_VERSION_1);
2402 REGISTER_SNMP_CLASS_CONST_LONG("VERSION_2c", SNMP_VERSION_2c);
2403 REGISTER_SNMP_CLASS_CONST_LONG("VERSION_2C", SNMP_VERSION_2c);
2404 REGISTER_SNMP_CLASS_CONST_LONG("VERSION_3", SNMP_VERSION_3);
2405
2406 REGISTER_SNMP_CLASS_CONST_LONG("ERRNO_NOERROR", PHP_SNMP_ERRNO_NOERROR);
2407 REGISTER_SNMP_CLASS_CONST_LONG("ERRNO_ANY", PHP_SNMP_ERRNO_ANY);
2408 REGISTER_SNMP_CLASS_CONST_LONG("ERRNO_GENERIC", PHP_SNMP_ERRNO_GENERIC);
2409 REGISTER_SNMP_CLASS_CONST_LONG("ERRNO_TIMEOUT", PHP_SNMP_ERRNO_TIMEOUT);
2410 REGISTER_SNMP_CLASS_CONST_LONG("ERRNO_ERROR_IN_REPLY", PHP_SNMP_ERRNO_ERROR_IN_REPLY);
2411 REGISTER_SNMP_CLASS_CONST_LONG("ERRNO_OID_NOT_INCREASING", PHP_SNMP_ERRNO_OID_NOT_INCREASING);
2412 REGISTER_SNMP_CLASS_CONST_LONG("ERRNO_OID_PARSING_ERROR", PHP_SNMP_ERRNO_OID_PARSING_ERROR);
2413 REGISTER_SNMP_CLASS_CONST_LONG("ERRNO_MULTIPLE_SET_QUERIES", PHP_SNMP_ERRNO_MULTIPLE_SET_QUERIES);
2414
2415
2416 INIT_CLASS_ENTRY(cex, "SNMPException", NULL);
2417 #ifdef HAVE_SPL
2418 php_snmp_exception_ce = zend_register_internal_class_ex(&cex, spl_ce_RuntimeException);
2419 #else
2420 php_snmp_exception_ce = zend_register_internal_class_ex(&cex, zend_ce_exception);
2421 #endif
2422
2423 return SUCCESS;
2424 }
2425
2426
2427
2428
2429 PHP_MSHUTDOWN_FUNCTION(snmp)
2430 {
2431 snmp_shutdown("snmpapp");
2432
2433 zend_hash_destroy(&php_snmp_properties);
2434
2435 return SUCCESS;
2436 }
2437
2438
2439
2440
2441 PHP_MINFO_FUNCTION(snmp)
2442 {
2443 php_info_print_table_start();
2444 php_info_print_table_row(2, "NET-SNMP Support", "enabled");
2445 php_info_print_table_row(2, "NET-SNMP Version", netsnmp_get_version());
2446 php_info_print_table_row(2, "PHP SNMP Version", PHP_SNMP_VERSION);
2447 php_info_print_table_end();
2448 }
2449
2450
2451
2452
2453 #if ZEND_MODULE_API_NO >= 20050922
2454 static const zend_module_dep snmp_module_deps[] = {
2455 #ifdef HAVE_SPL
2456 ZEND_MOD_REQUIRED("spl")
2457 #endif
2458 ZEND_MOD_END
2459 };
2460 #endif
2461
2462
2463
2464
2465 zend_module_entry snmp_module_entry = {
2466 STANDARD_MODULE_HEADER_EX,
2467 NULL,
2468 snmp_module_deps,
2469 "snmp",
2470 snmp_functions,
2471 PHP_MINIT(snmp),
2472 PHP_MSHUTDOWN(snmp),
2473 NULL,
2474 NULL,
2475 PHP_MINFO(snmp),
2476 PHP_SNMP_VERSION,
2477 PHP_MODULE_GLOBALS(snmp),
2478 PHP_GINIT(snmp),
2479 NULL,
2480 NULL,
2481 STANDARD_MODULE_PROPERTIES_EX
2482 };
2483
2484
2485 #endif
2486
2487
2488
2489
2490
2491
2492
2493
2494