cvs: /php4/ext/snmp snmp.c php3_snmp.h
| From: | Steven Lawrance | Date: | Wed, 04 Aug 1999 21:12:58 +0000 |
| Subject: | cvs: /php4/ext/snmp snmp.c php3_snmp.h | ||
| Groups: | php.version4 | ||
| Request: | Send a blank email to php-version4+get-3123@lists.php.net to get a copy of this message | ||
slawrance Wed Aug 4 17:12:58 1999 EDT
Modified files:
/php4/ext/snmp snmp.c php3_snmp.h
Log:
Fixed a little boo-boo I made during the PHP3->PHP4 conversion.
Index: php4/ext/snmp/snmp.c
diff -u php4/ext/snmp/snmp.c:1.7 php4/ext/snmp/snmp.c:1.8
--- php4/ext/snmp/snmp.c:1.7 Wed Aug 4 16:56:33 1999
+++ php4/ext/snmp/snmp.c Wed Aug 4 17:12:57 1999
@@ -13,11 +13,11 @@
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
-| Mike Jackson <mhjack@tscnet.com> |
-| Steven Lawrance <slawrance@technologist.com> |
+| Mike Jackson <mhjack@tscnet.com> |
+| Steven Lawrance <slawrance@technologist.com> |
+----------------------------------------------------------------------+
*/
-/* $Id: snmp.c,v 1.7 1999/08/04 20:56:33 slawrance Exp $ */
+/* $Id: snmp.c,v 1.8 1999/08/04 21:12:57 slawrance Exp $ */
#include "php.h"
#if defined(COMPILE_DL)
@@ -35,11 +35,11 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
-#ifndef _OSD_POSIX
-#include <sys/errno.h>
-#else
-#include <errno.h> /* BS2000/OSD uses <errno.h>, not <sys/errno.h> */
-#endif
+#ifndef _OSD_POSIX
+#include <sys/errno.h>
+#else
+#include <errno.h> /* BS2000/OSD uses <errno.h>, not <sys/errno.h> */
+#endif
#include <netdb.h>
#endif
#ifdef HAVE_UNISTD_H
@@ -81,10 +81,10 @@
PHP_FE(snmpget, NULL)
PHP_FE(snmpwalk, NULL)
PHP_FE(snmprealwalk, NULL)
- PHP_FE(snmpwalkoid, NULL)
- PHP_FE(snmp_get_quick_print, NULL)
- PHP_FE(snmp_set_quick_print, NULL)
- PHP_FE(snmpset, NULL)
+ PHP_FE(snmpwalkoid, NULL)
+ PHP_FE(snmp_get_quick_print, NULL)
+ PHP_FE(snmp_set_quick_print, NULL)
+ PHP_FE(snmpset, NULL)
{NULL,NULL,NULL}
};
@@ -110,18 +110,18 @@
/*
* Generic SNMP object fetcher
+*
+* st=1 snmpget() - query an agent and return a single value.
+* st=2 snmpwalk() - walk the mib and return a single dimensional array
+* containing the values.
+* st=3,4 snmprealwalk() and snmpwalkoid() - walk the mib and return an
+* array of oid,value pairs.
+* st=5-8 ** Reserved **
+* st=9 snmp_get_quick_print() - Return the current value for quickprint
+* (default setting is 0 (false)).
+* st=10 snmp_set_quick_print() - Set the current value for quickprint
+* st=11 snmpset() - query an agent and set a single value
*
-* st=1 snmpget() - query an agent and return a single value.
-* st=2 snmpwalk() - walk the mib and return a single dimensional array
-* containing the values.
-* st=3,4 snmprealwalk() and snmpwalkoid() - walk the mib and return an
-* array of oid,value pairs.
-* st=5-8 ** Reserved **
-* st=9 snmp_get_quick_print() - Return the current value for quickprint
-* (default setting is 0 (false)).
-* st=10 snmp_set_quick_print() - Set the current value for quickprint
-* st=11 snmpset() - query an agent and set a single value
-*
*/
void _php3_snmp(INTERNAL_FUNCTION_PARAMETERS, int st) {
pval *a1, *a2, *a3, *a4, *a5, *a6, *a7;
@@ -139,184 +139,184 @@
long timeout=SNMP_DEFAULT_TIMEOUT;
long retries=SNMP_DEFAULT_RETRIES;
int myargc = ARG_COUNT(ht);
- char type;
- char *value;
-
- switch(st) {
- case 4:
- st = 3; /* This is temporary until snmprealwalk() is removed */
- break;
- case 9:
- RETURN_LONG(snmp_get_quick_print()?1:0);
- case 10:
- if(myargc != 1 || getParameters(ht, myargc, &a1)) WRONG_PARAM_COUNT;
- convert_to_long(a1);
- snmp_set_quick_print((int) a1->value.lval);
- RETURN_TRUE;
- }
-
- if (myargc<3 || myargc>7 || getParameters(ht, myargc, &a1, &a2, &a3, &a4,
&a5, &a6, &a7) == FAILURE) {
- WRONG_PARAM_COUNT;
- }
- convert_to_string(a1);
- convert_to_string(a2);
- convert_to_string(a3);
- if (st==11) {
- if (myargc<5) WRONG_PARAM_COUNT;
- convert_to_string(a4);
- convert_to_string(a5);
- if(myargc>5) {
- convert_to_long(a6);
- timeout=a6->value.lval;
- }
- if(myargc>6) {
- convert_to_long(a7);
- retries=a7->value.lval;
- }
- type = a4->value.str.val[0];
- value = a5->value.str.val;
- } else {
- if(myargc>3) {
- convert_to_long(a4);
- timeout=a4->value.lval;
- }
- if(myargc>4) {
- convert_to_long(a5);
- retries=a5->value.lval;
- }
- }
- objid=a3->value.str.val;
-
- if (st>=2) { /* walk */
- rootlen = MAX_NAME_LEN;
- if (strlen(objid)) { /* on a walk, an empty string means top of tree - no error */
- if (read_objid(objid, root, &rootlen)) {
- gotroot = 1;
- } else {
- php_error(E_WARNING,"Invalid object identifier: %s\n", objid);
- }
- }
- if (gotroot == 0) {
- memmove((char *)root, (char *)objid_mib, sizeof(objid_mib));
- rootlen = sizeof(objid_mib) / sizeof(oid);
- gotroot = 1;
- }
- }
-
- memset(&session, 0, sizeof(struct snmp_session));
- session.peername = a1->value.str.val;
-
- session.version = SNMP_VERSION_1;
- /*
- * FIXME: potential memory leak
- * This is a workaround for an "artifact" (Mike Slifcak)
- * in (at least) ucd-snmp 3.6.1 which frees
- * memory it did not allocate
- */
-#ifdef UCD_SNMP_HACK
- session.community = (u_char *) strdup(a2->value.str.val);
-#else
- session.community = (u_char *) a2->value.str.val;
-#endif
- session.community_len = a2->value.str.len;
- session.retries = retries;
- session.timeout = timeout;
-
- session.authenticator = NULL;
- snmp_synch_setup(&session);
- ss = snmp_open(&session);
- if (ss == NULL){
- php_error(E_WARNING,"Couldn't open snmp\n");
- RETURN_FALSE;
- }
- if (st>=2) {
- memmove((char *)name, (char *)root, rootlen * sizeof(oid));
- name_length = rootlen;
- /* prepare result array */
- array_init(return_value);
- }
-
- while(keepwalking) {
- keepwalking=0;
- if (st==1) pdu = snmp_pdu_create(SNMP_MSG_GET);
- else if (st==11) pdu = snmp_pdu_create(SNMP_MSG_SET);
- else if (st>=2) pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
-
- if (st==1) {
- name_length = MAX_NAME_LEN;
- if (!read_objid(objid, name, &name_length)) {
- php_error(E_WARNING,"Invalid object identifier: %s\n", objid);
- RETURN_FALSE;
- }
- }
- if (st!=11)
- snmp_add_null_var(pdu, name, name_length);
- else {
- if (snmp_add_var(pdu, name, name_length, type, value)) {
- php_error(E_WARNING,"Could not add variable: %s\n", name);
- RETURN_FALSE;
- }
- }
-
-retry:
- status = snmp_synch_response(ss, pdu, &response);
- if (status == STAT_SUCCESS) {
- if (response->errstat == SNMP_ERR_NOERROR) {
- for(vars = response->variables; vars; vars = vars->next_variable) {
- if (st>=2 && st!=11 && (vars->name_length < rootlen || memcmp(root,
vars->name, rootlen * sizeof(oid))))
- continue; /* not part of this subtree */
-
- if (st!=11)
- sprint_value(buf,vars->name, vars->name_length, vars);
-#if 0
- Debug("snmp response is: %s\n",buf);
-#endif
- if (st==1) {
- RETVAL_STRING(buf,1);
- } else if (st==2) {
- /* Add to returned array */
- add_next_index_string(return_value,buf,1);
- } else if (st==3) {
- sprint_objid(buf2, vars->name, vars->name_length);
- add_assoc_string(return_value,buf2,buf,1);
- }
- if (st>=2 && st!=11) {
- if (vars->type != SNMP_ENDOFMIBVIEW && vars->type != SNMP_NOSUCHOBJECT
&& vars->type != SNMP_NOSUCHINSTANCE) {
- memmove((char *)name, (char *)vars->name,vars->name_length * sizeof(oid));
- name_length = vars->name_length;
- keepwalking = 1;
- }
- }
- }
- } else {
- if (st!=2 || response->errstat != SNMP_ERR_NOSUCHNAME) {
- php_error(E_WARNING,"Error in packet.\nReason: %s\n",
snmp_errstring(response->errstat));
- if (response->errstat == SNMP_ERR_NOSUCHNAME) {
- for(count=1, vars = response->variables; vars && count != response->errindex;
- vars = vars->next_variable, count++);
- if (vars) sprint_objid(buf,vars->name, vars->name_length);
- php_error(E_WARNING,"This name does not exist: %s\n",buf);
- }
- if (st==1) {
- if ((pdu = snmp_fix_pdu(response, SNMP_MSG_GET)) != NULL) goto retry;
- } else if (st==11) {
- if ((pdu = snmp_fix_pdu(response, SNMP_MSG_SET)) != NULL) goto retry;
- } else if (st>=2) {
- if ((pdu = snmp_fix_pdu(response, SNMP_MSG_GETNEXT)) != NULL) goto retry;
- }
- RETURN_FALSE;
- }
- }
- } else if (status == STAT_TIMEOUT) {
- php_error(E_WARNING,"No Response from %s\n", a1->value.str.val);
- RETURN_FALSE;
- } else { /* status == STAT_ERROR */
- php_error(E_WARNING,"An error occurred, Quitting\n");
- RETURN_FALSE;
- }
- if (response) snmp_free_pdu(response);
- } /* keepwalking */
- snmp_close(ss);
+ char type;
+ char *value;
+
+ switch(st) {
+ case 4:
+ st = 3; /* This is temporary until snmprealwalk() is removed */
+ break;
+ case 9:
+ RETURN_LONG(snmp_get_quick_print()?1:0);
+ case 10:
+ if(myargc != 1 || getParameters(ht, myargc, &a1)) WRONG_PARAM_COUNT;
+ convert_to_long(a1);
+ snmp_set_quick_print((int) a1->value.lval);
+ RETURN_TRUE;
+ }
+
+ if (myargc<3 || myargc>7 || getParameters(ht, myargc, &a1, &a2, &a3, &a4,
&a5, &a6, &a7) == FAILURE) {
+ WRONG_PARAM_COUNT;
+ }
+ convert_to_string(a1);
+ convert_to_string(a2);
+ convert_to_string(a3);
+ if (st==11) {
+ if (myargc<5) WRONG_PARAM_COUNT;
+ convert_to_string(a4);
+ convert_to_string(a5);
+ if(myargc>5) {
+ convert_to_long(a6);
+ timeout=a6->value.lval;
+ }
+ if(myargc>6) {
+ convert_to_long(a7);
+ retries=a7->value.lval;
+ }
+ type = a4->value.str.val[0];
+ value = a5->value.str.val;
+ } else {
+ if(myargc>3) {
+ convert_to_long(a4);
+ timeout=a4->value.lval;
+ }
+ if(myargc>4) {
+ convert_to_long(a5);
+ retries=a5->value.lval;
+ }
+ }
+ objid=a3->value.str.val;
+
+ if (st>=2) { /* walk */
+ rootlen = MAX_NAME_LEN;
+ if (strlen(objid)) { /* on a walk, an empty string means top of tree - no error */
+ if (read_objid(objid, root, &rootlen)) {
+ gotroot = 1;
+ } else {
+ php_error(E_WARNING,"Invalid object identifier: %s\n", objid);
+ }
+ }
+ if (gotroot == 0) {
+ memmove((char *)root, (char *)objid_mib, sizeof(objid_mib));
+ rootlen = sizeof(objid_mib) / sizeof(oid);
+ gotroot = 1;
+ }
+ }
+
+ memset(&session, 0, sizeof(struct snmp_session));
+ session.peername = a1->value.str.val;
+
+ session.version = SNMP_VERSION_1;
+ /*
+ * FIXME: potential memory leak
+ * This is a workaround for an "artifact" (Mike Slifcak)
+ * in (at least) ucd-snmp 3.6.1 which frees
+ * memory it did not allocate
+ */
+#ifdef UCD_SNMP_HACK
+ session.community = (u_char *) strdup(a2->value.str.val);
+#else
+ session.community = (u_char *) a2->value.str.val;
+#endif
+ session.community_len = a2->value.str.len;
+ session.retries = retries;
+ session.timeout = timeout;
+
+ session.authenticator = NULL;
+ snmp_synch_setup(&session);
+ ss = snmp_open(&session);
+ if (ss == NULL){
+ php_error(E_WARNING,"Couldn't open snmp\n");
+ RETURN_FALSE;
+ }
+ if (st>=2) {
+ memmove((char *)name, (char *)root, rootlen * sizeof(oid));
+ name_length = rootlen;
+ /* prepare result array */
+ array_init(return_value);
+ }
+
+ while(keepwalking) {
+ keepwalking=0;
+ if (st==1) pdu = snmp_pdu_create(SNMP_MSG_GET);
+ else if (st==11) pdu = snmp_pdu_create(SNMP_MSG_SET);
+ else if (st>=2) pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
+
+ if (st==1) {
+ name_length = MAX_NAME_LEN;
+ if (!read_objid(objid, name, &name_length)) {
+ php_error(E_WARNING,"Invalid object identifier: %s\n", objid);
+ RETURN_FALSE;
+ }
+ }
+ if (st!=11)
+ snmp_add_null_var(pdu, name, name_length);
+ else {
+ if (snmp_add_var(pdu, name, name_length, type, value)) {
+ php_error(E_WARNING,"Could not add variable: %s\n", name);
+ RETURN_FALSE;
+ }
+ }
+
+retry:
+ status = snmp_synch_response(ss, pdu, &response);
+ if (status == STAT_SUCCESS) {
+ if (response->errstat == SNMP_ERR_NOERROR) {
+ for(vars = response->variables; vars; vars = vars->next_variable) {
+ if (st>=2 && st!=11 && (vars->name_length < rootlen || memcmp(root,
vars->name, rootlen * sizeof(oid))))
+ continue; /* not part of this subtree */
+
+ if (st!=11)
+ sprint_value(buf,vars->name, vars->name_length, vars);
+#if 0
+ Debug("snmp response is: %s\n",buf);
+#endif
+ if (st==1) {
+ RETVAL_STRING(buf,1);
+ } else if (st==2) {
+ /* Add to returned array */
+ add_next_index_string(return_value,buf,1);
+ } else if (st==3) {
+ sprint_objid(buf2, vars->name, vars->name_length);
+ add_assoc_string(return_value,buf2,buf,1);
+ }
+ if (st>=2 && st!=11) {
+ if (vars->type != SNMP_ENDOFMIBVIEW && vars->type != SNMP_NOSUCHOBJECT
&& vars->type != SNMP_NOSUCHINSTANCE) {
+ memmove((char *)name, (char *)vars->name,vars->name_length * sizeof(oid));
+ name_length = vars->name_length;
+ keepwalking = 1;
+ }
+ }
+ }
+ } else {
+ if (st!=2 || response->errstat != SNMP_ERR_NOSUCHNAME) {
+ php_error(E_WARNING,"Error in packet.\nReason: %s\n",
snmp_errstring(response->errstat));
+ if (response->errstat == SNMP_ERR_NOSUCHNAME) {
+ for(count=1, vars = response->variables; vars && count != response->errindex;
+ vars = vars->next_variable, count++);
+ if (vars) sprint_objid(buf,vars->name, vars->name_length);
+ php_error(E_WARNING,"This name does not exist: %s\n",buf);
+ }
+ if (st==1) {
+ if ((pdu = snmp_fix_pdu(response, SNMP_MSG_GET)) != NULL) goto retry;
+ } else if (st==11) {
+ if ((pdu = snmp_fix_pdu(response, SNMP_MSG_SET)) != NULL) goto retry;
+ } else if (st>=2) {
+ if ((pdu = snmp_fix_pdu(response, SNMP_MSG_GETNEXT)) != NULL) goto retry;
+ }
+ RETURN_FALSE;
+ }
+ }
+ } else if (status == STAT_TIMEOUT) {
+ php_error(E_WARNING,"No Response from %s\n", a1->value.str.val);
+ RETURN_FALSE;
+ } else { /* status == STAT_ERROR */
+ php_error(E_WARNING,"An error occurred, Quitting\n");
+ RETURN_FALSE;
+ }
+ if (response) snmp_free_pdu(response);
+ } /* keepwalking */
+ snmp_close(ss);
}
/* {{{ proto string snmpget(string host, string community, string object_id [, int timeout [, int
retries]])
@@ -339,38 +339,38 @@
{
return _php3_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU,3);
}
+/* }}} */
+
+/* {{{ proto string snmprealoid(string host, string community, string object_id [, int timeout [,
int retries]])
+Return all objects including their respective object id withing the specified one */
+PHP_FUNCTION(snmpwalkoid)
+{
+ _php3_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU,4);
+}
+/* }}} */
+
+/* {{{ proto int snmp_get_quick_print(void)
+Return the current status of quick_print */
+PHP_FUNCTION(snmp_get_quick_print)
+{
+ _php3_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU,9);
+}
+/* }}} */
+
+/* {{{ proto void snmp_set_quick_print(int quick_print)
+Return all objects including their respective object id withing the specified one */
+PHP_FUNCTION(snmp_set_quick_print)
+{
+ _php3_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU,10);
+}
+/* }}} */
+
+/* {{{ proto int snmpset(string host, string community, string object_id, string type, mixed value
[, int timeout [, int retries]])
+Set the value of a SNMP object */
+PHP_FUNCTION(snmpset) {
+ _php3_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU,11);
+}
/* }}} */
-
-/* {{{ proto string snmprealoid(string host, string community, string object_id [, int timeout [,
int retries]])
-Return all objects including their respective object id withing the specified one */
-PHP_FUNCTION(snmpwalkoid)
-{
- _php3_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU,4);
-}
-/* }}} */
-
-/* {{{ proto int snmp_get_quick_print(void)
-Return the current status of quick_print */
-PHP_FUNCTION(snmp_get_quick_print)
-{
- _php3_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU,9);
-}
-/* }}} */
-
-/* {{{ proto void snmp_set_quick_print(int quick_print)
-Return all objects including their respective object id withing the specified one */
-PHP_FUNCTION(snmp_set_quick_print)
-{
- _php3_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU,10);
-}
-/* }}} */
-
-/* {{{ proto int snmpset(string host, string community, string object_id, string type, mixed value
[, int timeout [, int retries]])
-Set the value of a SNMP object */
-PHP_FUNCTION(php3_snmpset) {
- _php3_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU,11);
-}
-/* }}} */
#endif
Index: php4/ext/snmp/php3_snmp.h
diff -u php4/ext/snmp/php3_snmp.h:1.4 php4/ext/snmp/php3_snmp.h:1.5
--- php4/ext/snmp/php3_snmp.h:1.4 Wed Aug 4 16:56:33 1999
+++ php4/ext/snmp/php3_snmp.h Wed Aug 4 17:12:57 1999
@@ -1,24 +1,24 @@
-/*
-+----------------------------------------------------------------------+
-| PHP version 4.0 |
-+----------------------------------------------------------------------+
-| Copyright (c) 1997, 1998, 1999 The PHP Group |
-+----------------------------------------------------------------------+
-| This source file is subject to version 2.0 of the PHP license, |
-| that is bundled with this package in the file LICENSE, and is |
-| available at through the world-wide-web at |
-| http://www.php.net/license/2_0.txt.
|
-| If you did not receive a copy of the PHP license and are unable to |
-| obtain it through the world-wide-web, please send a note to |
-| license@php.net so we can mail you a copy immediately. |
-+----------------------------------------------------------------------+
-| Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
-| Mike Jackson <mhjack@tscnet.com> |
-| Steven Lawrance <slawrance@technologist.com> |
-+----------------------------------------------------------------------+
-*/
-
-/* $Id: php3_snmp.h,v 1.4 1999/08/04 20:56:33 slawrance Exp $ */
+/*
++----------------------------------------------------------------------+
+| PHP version 4.0 |
++----------------------------------------------------------------------+
+| Copyright (c) 1997, 1998, 1999 The PHP Group |
++----------------------------------------------------------------------+
+| This source file is subject to version 2.0 of the PHP license, |
+| that is bundled with this package in the file LICENSE, and is |
+| available at through the world-wide-web at |
+| http://www.php.net/license/2_0.txt.
|
+| If you did not receive a copy of the PHP license and are unable to |
+| obtain it through the world-wide-web, please send a note to |
+| license@php.net so we can mail you a copy immediately. |
++----------------------------------------------------------------------+
+| Authors: Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
+| Mike Jackson <mhjack@tscnet.com> |
+| Steven Lawrance <slawrance@technologist.com> |
++----------------------------------------------------------------------+
+*/
+
+/* $Id: php3_snmp.h,v 1.5 1999/08/04 21:12:57 slawrance Exp $ */
#ifndef _PHP3_SNMP_H
#define _PHP3_SNMP_H
@@ -38,10 +38,10 @@
PHP_FUNCTION(snmpget);
PHP_FUNCTION(snmpwalk);
PHP_FUNCTION(snmprealwalk);
-PHP_FUNCTION(snmpwalkoid);
-PHP_FUNCTION(snmp_get_quick_print);
-PHP_FUNCTION(snmp_set_quick_print);
-PHP_FUNCTION(snmpset);
+PHP_FUNCTION(snmpwalkoid);
+PHP_FUNCTION(snmp_get_quick_print);
+PHP_FUNCTION(snmp_set_quick_print);
+PHP_FUNCTION(snmpset);
void php3_info_snmp(ZEND_MODULE_INFO_FUNC_ARGS);
#else