[PHP-CVS] svn: /php/php-src/trunk/ext/snmp/ php_snmp.h snmp.c tests/snmp-object-errno-errstr.phpt tests/snmp-object-error.phpt tests/snmp-object-properties.phpt tests/snmp-object-set_security_error.ph

2011-02-26 Thread Boris Lytochkin
lytboris Sat, 26 Feb 2011 08:27:26 +

Revision: http://svn.php.net/viewvc?view=revisionrevision=308703

Log:
* new methods get_errno, get_error to get errno and error string
of last SNMP-related error
* formatting markup
* some fixes in max_oids logic: NULL will set it to default value,
do not allow non-positive user-supplied values
* unit tests for changes

Changed paths:
U   php/php-src/trunk/ext/snmp/php_snmp.h
U   php/php-src/trunk/ext/snmp/snmp.c
A   php/php-src/trunk/ext/snmp/tests/snmp-object-errno-errstr.phpt
U   php/php-src/trunk/ext/snmp/tests/snmp-object-error.phpt
U   php/php-src/trunk/ext/snmp/tests/snmp-object-properties.phpt
U   php/php-src/trunk/ext/snmp/tests/snmp-object-set_security_error.phpt
U   php/php-src/trunk/ext/snmp/tests/snmp-object.phpt

Modified: php/php-src/trunk/ext/snmp/php_snmp.h
===
--- php/php-src/trunk/ext/snmp/php_snmp.h	2011-02-26 04:34:53 UTC (rev 308702)
+++ php/php-src/trunk/ext/snmp/php_snmp.h	2011-02-26 08:27:26 UTC (rev 308703)
@@ -79,17 +79,21 @@
 PHP_METHOD(SNMP, getnext);
 PHP_METHOD(SNMP, walk);
 PHP_METHOD(SNMP, set);
+PHP_METHOD(SNMP, get_errno);
+PHP_METHOD(SNMP, get_error);

 typedef struct _php_snmp_object {
-  zend_object zo;
-  struct snmp_session *session;
-  int max_oids;
-  int valueretrieval;
-  int quick_print;
+	zend_object zo;
+	struct snmp_session *session;
+	int max_oids;
+	int valueretrieval;
+	int quick_print;
 #ifdef HAVE_NET_SNMP
-  int enum_print;
-  int oid_output_format;
+	int enum_print;
+	int oid_output_format;
 #endif
+	int snmp_errno;
+	char snmp_errstr[128];
 } php_snmp_object;


@@ -97,14 +101,21 @@
 typedef int (*php_snmp_write_t)(php_snmp_object *snmp_object, zval *newval TSRMLS_DC);

 typedef struct _ptp_snmp_prop_handler {
-  const char *name;
-  size_t name_length;
-  php_snmp_read_t read_func;
-  php_snmp_write_t write_func;
+	const char *name;
+	size_t name_length;
+	php_snmp_read_t read_func;
+	php_snmp_write_t write_func;
 } php_snmp_prop_handler;

+typedef struct _snmpobjarg {
+	char *oid;
+	char type;
+	char *value;
+
+} snmpobjarg;
+
 ZEND_BEGIN_MODULE_GLOBALS(snmp)
-  int valueretrieval;
+	int valueretrieval;
 ZEND_END_MODULE_GLOBALS(snmp)

 #ifdef ZTS
@@ -113,6 +124,9 @@
 #define SNMP_G(v) (snmp_globals.v)
 #endif

+#define REGISTER_PDO_CLASS_CONST_LONG(const_name, value) \
+	zend_declare_class_constant_long(php_snmp_get_ce(), const_name, sizeof(const_name)-1, (long)value TSRMLS_CC);
+
 #else

 #define snmp_module_ptr NULL

Modified: php/php-src/trunk/ext/snmp/snmp.c
===
--- php/php-src/trunk/ext/snmp/snmp.c	2011-02-26 04:34:53 UTC (rev 308702)
+++ php/php-src/trunk/ext/snmp/snmp.c	2011-02-26 08:27:26 UTC (rev 308703)
@@ -161,8 +161,12 @@
 	} \
 }

+#define PHP_SNMP_ERRNO_NOERROR		0
+#define PHP_SNMP_ERRNO_GENERIC		1
+#define PHP_SNMP_ERRNO_TIMEOUT		2
+#define PHP_SNMP_ERRNO_ERROR_IN_REPLY	3
+#define PHP_SNMP_ERRNO_OID_NOT_INCREASING 4

-
 ZEND_DECLARE_MODULE_GLOBALS(snmp)
 static PHP_GINIT_FUNCTION(snmp);

@@ -177,6 +181,11 @@
 /* Class entries */
 zend_class_entry *php_snmp_class_entry;

+zend_class_entry *php_snmp_get_ce()
+{
+	return php_snmp_class_entry;
+}
+
 /* Class object properties */
 static HashTable php_snmp_properties;

@@ -409,13 +418,6 @@
 ZEND_END_ARG_INFO()
 /* }}} */

-typedef struct _snmpobjarg {
-	char *oid;
-	char type;
-	char *value;
-
-} snmpobjarg;
-
 struct objid_query {
 	int count;
 	int offset;
@@ -546,6 +548,39 @@

 }

+/* {{{ php_snmp_error
+ *
+ * Record last SNMP-related error in object
+ *
+ */
+static void php_snmp_error(zval *object, const char *docref TSRMLS_DC, int type, const char *format, ...)
+{
+	va_list args;
+	php_snmp_object *snmp_object;
+
+	if (object) {
+		snmp_object = (php_snmp_object *)zend_object_store_get_object(object TSRMLS_CC);
+		if (type == PHP_SNMP_ERRNO_NOERROR) {
+			memset(snmp_object-snmp_errstr, 0, sizeof(snmp_object-snmp_errstr));
+		} else {
+			va_start(args, format);
+			vsnprintf(snmp_object-snmp_errstr, sizeof(snmp_object-snmp_errstr) - 1, format, args);
+			va_end(args);
+		}
+		snmp_object-snmp_errno = type;
+	}
+
+	if (type == PHP_SNMP_ERRNO_NOERROR) {
+		return;
+	}
+
+	va_start(args, format);
+	php_verror(docref, , E_WARNING, format, args TSRMLS_CC);
+	va_end(args);
+}
+
+/* }}} */
+
 /* {{{ php_snmp_getvalue
 *
 * SNMP value to zval converter
@@ -697,6 +732,9 @@

 	/* we start with retval=FALSE. If any actual data is aquired, retval will be set to appropriate type */
 	RETVAL_FALSE;
+
+	/* reset errno and errstr */
+	php_snmp_error(getThis(), NULL TSRMLS_CC, PHP_SNMP_ERRNO_NOERROR, );

 	if (st  SNMP_CMD_WALK) {
 		if (objid_query-count  1) {
@@ -797,7 +835,7 @@
 		}
 		SNMP_SNPRINT_OBJID(buf, sizeof(buf), vars-name, vars-name_length);
 		SNMP_SNPRINT_VALUE(buf2, sizeof(buf2), 

[PHP-CVS] svn: /php/php-src/trunk/ext/snmp/ snmp.c

2011-02-26 Thread Boris Lytochkin
lytboris Sat, 26 Feb 2011 19:08:55 +

Revision: http://svn.php.net/viewvc?view=revisionrevision=308710

Log:
remove compiler warnings
fix various memory leaks seen with --enable-debug

Changed paths:
U   php/php-src/trunk/ext/snmp/snmp.c

Modified: php/php-src/trunk/ext/snmp/snmp.c
===
--- php/php-src/trunk/ext/snmp/snmp.c	2011-02-26 17:28:31 UTC (rev 308709)
+++ php/php-src/trunk/ext/snmp/snmp.c	2011-02-26 19:08:55 UTC (rev 308710)
@@ -140,17 +140,6 @@
 typedef struct snmp_session php_snmp_session;
 #define PHP_SNMP_SESSION_RES_NAME SNMP session

-#define PHP_SNMP_SESSION_FROM_OBJECT(session, object) \
-	{ \
-		php_snmp_object *snmp_object; \
-		snmp_object = (php_snmp_object *)zend_object_store_get_object(object TSRMLS_CC); \
-		session = snmp_object-session; \
-		if (!session) { \
-			php_error_docref(NULL TSRMLS_CC, E_WARNING, Invalid or uninitialized SNMP object); \
-			RETURN_FALSE; \
-		} \
-	}
-
 #define PHP_SNMP_ADD_PROPERTIES(a, b) \
 { \
 	int i = 0; \
@@ -614,6 +603,9 @@
 	if (valueretrieval == SNMP_VALUE_LIBRARY) {
 		SNMP_SNPRINT_VALUE(buf, buflen, vars-name, vars-name_length, vars);
 		ZVAL_STRING(snmpval, buf, 1);
+		if(dbuf){ /* malloc was used to store value */
+			efree(dbuf);
+		}
 		return;
 	}

@@ -621,12 +613,12 @@

 	switch (vars-type) {
 	case ASN_BIT_STR:		/* 0x03, asn1.h */
-		ZVAL_STRINGL(val, vars-val.bitstring, vars-val_len, 1);
+		ZVAL_STRINGL(val, (char *)vars-val.bitstring, vars-val_len, 1);
 		break;

 	case ASN_OCTET_STR:		/* 0x04, asn1.h */
 	case ASN_OPAQUE:		/* 0x44, snmp_impl.h */
-		ZVAL_STRINGL(val, vars-val.string, vars-val_len, 1);
+		ZVAL_STRINGL(val, (char *)vars-val.string, vars-val_len, 1);
 		break;

 	case ASN_NULL:			/* 0x05, asn1.h */
@@ -699,6 +691,8 @@
 		add_property_long(snmpval, type, vars-type);
 		add_property_zval(snmpval, value, val);
 	}
+	zval_ptr_dtor(val);
+
 	if(dbuf){ /* malloc was used to store value */
 		efree(dbuf);
 	}
@@ -747,7 +741,8 @@
 gotroot = 1;
 			} else {
 php_error_docref(NULL TSRMLS_CC, E_WARNING, Invalid object identifier: %s, objid_query-vars[0].oid);
-RETURN_FALSE;
+RETVAL_FALSE;
+return;
 			}
 		}

@@ -766,7 +761,8 @@
 		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;
+		RETVAL_FALSE;
+		return;
 	}

 	while (keepwalking) {
@@ -784,7 +780,8 @@
 			if(pdu-variables == NULL){
 snmp_free_pdu(pdu);
 snmp_close(ss);
-RETURN_FALSE;
+RETVAL_FALSE;
+return;
 			}
 		} else if (st  SNMP_CMD_SET) {
 			pdu = snmp_pdu_create(SNMP_MSG_SET);
@@ -794,14 +791,16 @@
 	php_error_docref(NULL TSRMLS_CC, E_WARNING, Invalid object identifier: %s, objid_query-vars[objid_query-offset].oid);
 	snmp_free_pdu(pdu);
 	snmp_close(ss);
-	RETURN_FALSE;
+	RETVAL_FALSE;
+	return;
 } else {
 	if ((snmp_errno = snmp_add_var(pdu, name, name_length, objid_query-vars[objid_query-offset].type, objid_query-vars[objid_query-offset].value))) {
 		SNMP_SNPRINT_OBJID(buf, sizeof(buf), name, name_length);
 		php_error_docref(NULL TSRMLS_CC, E_WARNING, 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));
 		snmp_free_pdu(pdu);
 		snmp_close(ss);
-		RETURN_FALSE;
+		RETVAL_FALSE;
+		return;
 	}
 }
 			}
@@ -823,7 +822,8 @@
 if (st  SNMP_CMD_SET) {
 	snmp_free_pdu(response);
 	snmp_close(ss);
-	RETURN_TRUE;
+	RETVAL_TRUE;
+	return;
 }
 for (vars = response-variables; vars; vars = vars-next_variable) {
 	/* do not output errors as values */
@@ -918,7 +918,8 @@
 	if (objid_query-array_output) {
 		zval_dtor(return_value);
 	}
-	RETURN_FALSE;
+	RETVAL_FALSE;
+	return;
 }
 			}
 		} else if (status == STAT_TIMEOUT) {
@@ -927,7 +928,8 @@
 zval_dtor(return_value);
 			}
 			snmp_close(ss);
-			RETURN_FALSE;
+			RETVAL_FALSE;
+			return;
 		} else {/* status == STAT_ERROR */
 			snmp_error(ss, NULL, NULL, err);
 			php_snmp_error(getThis(), NULL TSRMLS_CC, PHP_SNMP_ERRNO_GENERIC, Fatal error: %s, err);
@@ -936,7 +938,8 @@
 zval_dtor(return_value);
 			}
 			snmp_close(ss);
-			RETURN_FALSE;
+			RETVAL_FALSE;
+			return;
 		}
 		if (response) {
 			snmp_free_pdu(response);
@@ -992,6 +995,7 @@
 		objid_query-vars = (snmpobjarg *)emalloc(sizeof(snmpobjarg));
 		if (objid_query-vars == NULL) {
 			php_error_docref(NULL TSRMLS_CC, E_WARNING, emalloc() failed while parsing oid: %s, strerror(errno));
+			efree(objid_query-vars);
 			return FALSE;
 		}
 		objid_query-vars[objid_query-count].oid = Z_STRVAL_PP(oid);
@@ -999,6 +1003,7 @@
 			if (Z_TYPE_PP(type) == IS_STRING  Z_TYPE_PP(value) == IS_STRING) {
 if (Z_STRLEN_PP(type) != 1) {
 	

Re: [PHP-CVS] svn: /php/php-src/trunk/ext/snmp/ snmp.c

2011-02-26 Thread Pierre Joye
btw, you can use valgrind as well with the tests suite. That should
help you to catch other possible leaks or bad memory access.

On Sat, Feb 26, 2011 at 8:08 PM, Boris Lytochkin lytbo...@php.net wrote:
 lytboris                                 Sat, 26 Feb 2011 19:08:55 +

 Revision: http://svn.php.net/viewvc?view=revisionrevision=308710

 Log:
 remove compiler warnings
 fix various memory leaks seen with --enable-debug

 Changed paths:
    U   php/php-src/trunk/ext/snmp/snmp.c


 --
 PHP CVS Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




-- 
Pierre

@pierrejoye | http://blog.thepimp.net | http://www.libgd.org

--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-CVS] svn: /php/php-src/trunk/ext/snmp/ snmp.c

2011-02-26 Thread Lytochkin Boris
Just to indicate that this is not php function return and some other
processing (in caller function) follows.

On Sat, Feb 26, 2011 at 11:30 PM, Antony Dovgal t...@daylessday.org wrote:
 -                       RETURN_FALSE;
 +                       RETVAL_FALSE;
 +                       return;

 What's the point?
 #define RETURN_FALSE                    { RETVAL_FALSE; return; }

 --
 Wbr,
 Antony Dovgal
 ---
 http://pinba.org - realtime statistics for PHP


-- 
Boris Lytochkin

--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-CVS] svn: /php/php-src/trunk/ext/snmp/ snmp.c

2011-02-26 Thread Antony Dovgal
-   RETURN_FALSE;
+   RETVAL_FALSE;
+   return;

What's the point?
#define RETURN_FALSE{ RETVAL_FALSE; return; }

On 02/26/2011 10:08 PM, Boris Lytochkin wrote:
 lytboris Sat, 26 Feb 2011 19:08:55 +
 
 Revision: http://svn.php.net/viewvc?view=revisionrevision=308710
 
 Log:
 remove compiler warnings
 fix various memory leaks seen with --enable-debug
 
 Changed paths:
 U   php/php-src/trunk/ext/snmp/snmp.c
 
 

-- 
Wbr,
Antony Dovgal
---
http://pinba.org - realtime statistics for PHP

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-CVS] svn: /php/php-src/trunk/ext/snmp/ snmp.c

2011-02-26 Thread Pierre Joye
can you open a bug at bugs.php.net with the patch please?

Thanks!

On Sat, Feb 26, 2011 at 9:31 PM, Lytochkin Boris lytbo...@gmail.com wrote:
 It is a pain to run it on FreeBSD, there is a bug in run-tests.php:
 ===
 --- run-tests.php       (revision 308673)
 +++ run-tests.php       (working copy)
 @@ -567,7 +567,7 @@
                                case 'm':
                                        $leak_check = true;
                                        $valgrind_cmd = valgrind --version;
 -                                       $valgrind_header =
 system_with_timeout($valgrind_cmd);
 +                                       $valgrind_header =
 system_with_timeout($valgrind_cmd, $_ENV);
                                        $replace_count = 0;
                                        if (!$valgrind_header) {
                                                error(Valgrind
 returned no version info, cannot proceed.\nPlease check if Valgrind is
 installed.);
 ===
 Without $_ENV system_with_timeout() will use bogus(system default)
 PATH environment variable and will not find valgrind executable.

 After fixing run-tests.php all 27 tests for ext/snmp passed with
 valgrind enabled.

 On Sat, Feb 26, 2011 at 10:19 PM, Pierre Joye pierre@gmail.com wrote:
 btw, you can use valgrind as well with the tests suite. That should
 help you to catch other possible leaks or bad memory access.

 On Sat, Feb 26, 2011 at 8:08 PM, Boris Lytochkin lytbo...@php.net wrote:
 lytboris                                 Sat, 26 Feb 2011 19:08:55 +

 Revision: http://svn.php.net/viewvc?view=revisionrevision=308710

 Log:
 remove compiler warnings
 fix various memory leaks seen with --enable-debug

 Changed paths:
    U   php/php-src/trunk/ext/snmp/snmp.c
 --
 Pierre

 @pierrejoye | http://blog.thepimp.net | http://www.libgd.org


 --
 Boris Lytochkin




-- 
Pierre

@pierrejoye | http://blog.thepimp.net | http://www.libgd.org

--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP-CVS] svn: /php/php-src/trunk/ext/snmp/ snmp.c

2011-02-26 Thread Lytochkin Boris
Sure,

#54108

On Sat, Feb 26, 2011 at 11:33 PM, Pierre Joye pierre@gmail.com wrote:
 can you open a bug at bugs.php.net with the patch please?

 --
 Pierre

 @pierrejoye | http://blog.thepimp.net | http://www.libgd.org


-- 
Boris Lytochkin

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php