⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snmp.c

📁 php-4.4.7学习linux时下载的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
	if ((ss = snmp_open(session)) == NULL) {		snmp_error(session, NULL, NULL, &err);		php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not open snmp connection: %s", err);		free(err);		RETURN_FALSE;	}	if (st >= 2) {		memmove((char *)name, (char *)root, rootlen * sizeof(oid));		name_length = rootlen;		switch(st) {			case 2:			case 3:				array_init(return_value);				break;			default:				RETVAL_TRUE;				break;		}	}	while (keepwalking) {		keepwalking = 0;		if (st == 1) {			pdu = snmp_pdu_create(SNMP_MSG_GET);			name_length = MAX_OID_LEN;			if (!snmp_parse_oid(objid, name, &name_length)) {				php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid object identifier: %s", objid);				snmp_close(ss);				RETURN_FALSE;			}			snmp_add_null_var(pdu, name, name_length);		} else if (st == 11) {			pdu = snmp_pdu_create(SNMP_MSG_SET);			if (snmp_add_var(pdu, name, name_length, type, value)) {#ifdef HAVE_NET_SNMP				snprint_objid(buf, sizeof(buf), name, name_length);#else				sprint_objid(buf, name, name_length);#endif				php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not add variable: %s %c %s", buf, type, value);				snmp_close(ss);				RETURN_FALSE;			}		} else if (st >= 2) {			pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);			snmp_add_null_var(pdu, name, name_length);		}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) {						MAKE_STD_ZVAL(snmpval);						php_snmp_getvalue(vars, snmpval TSRMLS_CC);					}					if (st == 1) {						*return_value = *snmpval;						zval_copy_ctor(return_value);						zval_ptr_dtor(&snmpval);						snmp_close(ss);						return;					} else if (st == 2) {						add_next_index_zval(return_value,snmpval); /* Add to returned array */					} else if (st == 3)  {#ifdef HAVE_NET_SNMP						snprint_objid(buf2, sizeof(buf2), vars->name, vars->name_length);#else						sprint_objid(buf2, vars->name, vars->name_length);#endif						add_assoc_zval(return_value,buf2,snmpval);					}					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_docref(NULL TSRMLS_CC, E_WARNING, "Error in packet: %s", 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) {#ifdef HAVE_NET_SNMP							snprint_objid(buf, sizeof(buf), vars->name, vars->name_length);#else							sprint_objid(buf,vars->name, vars->name_length);#endif						}						php_error_docref(NULL TSRMLS_CC, E_WARNING, "This name does not exist: %s",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;						}					}					snmp_close(ss);					RETURN_FALSE;				}			}		} else if (status == STAT_TIMEOUT) {			php_error_docref(NULL TSRMLS_CC, E_WARNING, "No response from %s", session->peername);			if (st == 2 || st == 3) {				zval_dtor(return_value);			}			snmp_close(ss);			RETURN_FALSE;		} else {    /* status == STAT_ERROR */			php_error_docref(NULL TSRMLS_CC, E_WARNING, "An error occurred, quitting");			if (st == 2 || st == 3) {				zval_dtor(return_value);			}			snmp_close(ss);			RETURN_FALSE;		}		if (response) {			snmp_free_pdu(response);		}	} /* keepwalking */	snmp_close(ss);}/* }}} *//* {{{ php_snmp** Generic community based SNMP handler for version 1 and 2.* This function makes use of the internal SNMP object fetcher.* The object fetcher is shared with SNMPv3.** 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 snmprealwalk() and snmpwalkoid() - walk the mib and return an *          array of oid,value pairs.* st=5-8 ** Reserved *** st=11  snmpset() - query an agent and set a single value**/static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version) {	zval **a1, **a2, **a3, **a4, **a5, **a6, **a7;	struct snmp_session session;	long timeout=SNMP_DEFAULT_TIMEOUT;	long retries=SNMP_DEFAULT_RETRIES;	int myargc = ZEND_NUM_ARGS();	char type = (char) 0;	char *value = (char *) 0;	char hostname[MAX_NAME_LEN];	int remote_port = 161;	char *pptr;	if (myargc < 3 || myargc > 7 ||		zend_get_parameters_ex(myargc, &a1, &a2, &a3, &a4, &a5, &a6, &a7) == FAILURE) {		WRONG_PARAM_COUNT;	}	convert_to_string_ex(a1);	convert_to_string_ex(a2);	convert_to_string_ex(a3);		if (st == 11) {		if (myargc < 5) {			WRONG_PARAM_COUNT;		}		convert_to_string_ex(a4);		convert_to_string_ex(a5);		if(myargc > 5) {			convert_to_long_ex(a6);			timeout = Z_LVAL_PP(a6);		}		if(myargc > 6) {			convert_to_long_ex(a7);			retries = Z_LVAL_PP(a7);		}		type = Z_STRVAL_PP(a4)[0];		value = Z_STRVAL_PP(a5);	} else {		if(myargc > 3) {			convert_to_long_ex(a4);			timeout = Z_LVAL_PP(a4);		}		if(myargc > 4) {			convert_to_long_ex(a5);			retries = Z_LVAL_PP(a5);		}	}	snmp_sess_init(&session);	strlcpy(hostname, Z_STRVAL_PP(a1), sizeof(hostname));	if ((pptr = strchr (hostname, ':'))) {		remote_port = strtol (pptr + 1, NULL, 0);	}	session.peername = hostname;	session.remote_port = remote_port;	session.version = version;	/*	* 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(Z_STRVAL_PP(a2)); /* memory freed by SNMP library, strdup NOT estrdup */#else	session.community = (u_char *)Z_STRVAL_PP(a2);#endif	session.community_len = Z_STRLEN_PP(a2);	session.retries = retries;	session.timeout = timeout;		session.authenticator = NULL;	php_snmp_internal(INTERNAL_FUNCTION_PARAM_PASSTHRU, st, &session, Z_STRVAL_PP(a3), type, value);}/* }}} *//* {{{ proto string snmpget(string host, string community, string object_id [, int timeout [, int retries]])    Fetch a SNMP object */PHP_FUNCTION(snmpget){	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU,1, SNMP_VERSION_1);}/* }}} *//* {{{ proto array snmpwalk(string host, string community, string object_id [, int timeout [, int retries]])    Return all objects under the specified object id */PHP_FUNCTION(snmpwalk){	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU,2, SNMP_VERSION_1);}/* }}} *//* {{{ proto array snmprealwalk(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(snmprealwalk){	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU,3, SNMP_VERSION_1);}/* }}} *//* {{{ proto bool snmp_get_quick_print(void)   Return the current status of quick_print */PHP_FUNCTION(snmp_get_quick_print){	if (ZEND_NUM_ARGS() != 0) {		WRONG_PARAM_COUNT;	}#ifdef HAVE_NET_SNMP	RETURN_BOOL(netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT));#else	RETURN_BOOL(snmp_get_quick_print());#endif}/* }}} *//* {{{ 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){	int argc = ZEND_NUM_ARGS();	long a1;	if (zend_parse_parameters(argc TSRMLS_CC, "l", &a1) == FAILURE) {		return;	}#ifdef HAVE_NET_SNMP	netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_QUICK_PRINT, (int) a1);#else	snmp_set_quick_print((int)a1);#endif}/* }}} */#ifdef HAVE_NET_SNMP/* {{{ proto void snmp_set_enum_print(int enum_print)   Return all values that are enums with their enum value instead of the raw integer */PHP_FUNCTION(snmp_set_enum_print){	int argc = ZEND_NUM_ARGS();	long a1;	if (zend_parse_parameters(argc TSRMLS_CC, "l", &a1) == FAILURE) {		return;	}	netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_PRINT_NUMERIC_ENUM, (int) a1);} /* }}} *//* {{{ proto void snmp_set_oid_numeric_print(int oid_numeric_print)   Return all objects including their respective object id withing the specified one */PHP_FUNCTION(snmp_set_oid_numeric_print){	int argc = ZEND_NUM_ARGS();	long a1;	if (zend_parse_parameters(argc TSRMLS_CC, "l", &a1) == FAILURE) {		return;	}	if ((int) a1 != 0) {		netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID,			NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,			NETSNMP_OID_OUTPUT_NUMERIC);	}} /* }}} */#endif/* {{{ 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){	php_snmp(INTERNAL_FUNCTION_PARAM_PASSTHRU,11, SNMP_VERSION_1);}/* }}} *//* {{{ int netsnmp_session_set_sec_name(struct snmp_session *s, char *name)   Set the security name in the snmpv3 session */static int netsnmp_session_set_sec_name(struct snmp_session *s, char *name){	if ((s) && (name)) {		s->securityName = strdup(name);		s->securityNameLen = strlen(s->securityName);		return (0);	}	return (-1);}/* }}} *//* {{{ int netsnmp_session_set_sec_level(struct snmp_session *s, char *level)   Set the security level in the snmpv3 session */static int netsnmp_session_set_sec_level(struct snmp_session *s, char *level TSRMLS_DC){	if ((s) && (level)) {		if (!strcasecmp(level, "noAuthNoPriv") || !strcasecmp(level, "nanp")) {			s->securityLevel = SNMP_SEC_LEVEL_NOAUTH;			return (0);		} else if (!strcasecmp(level, "authNoPriv") || !strcasecmp(level, "anp")) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -