@@ -659,7 +659,6 @@ static void php_snmp_internal(INTERNAL_FUNCTION_PARAMETERS, int st,
659
659
*
660
660
* OID parser (and type, value for SNMP_SET command)
661
661
*/
662
-
663
662
static int php_snmp_parse_oid (
664
663
zval * object , int st , struct objid_query * objid_query , zend_string * oid_str , HashTable * oid_ht ,
665
664
zend_string * type_str , HashTable * type_ht , zend_string * value_str , HashTable * value_ht
@@ -674,25 +673,33 @@ static int php_snmp_parse_oid(
674
673
objid_query -> vars = (snmpobjarg * )emalloc (sizeof (snmpobjarg ));
675
674
objid_query -> vars [objid_query -> count ].oid = ZSTR_VAL (oid_str );
676
675
if (st & SNMP_CMD_SET ) {
677
- if (type_str && value_str ) {
678
- if (ZSTR_LEN (type_str ) != 1 ) {
679
- php_error_docref (NULL , E_WARNING , "Bogus type '%s', should be single char, got %zu" , ZSTR_VAL (type_str ), ZSTR_LEN (type_str ));
680
- efree (objid_query -> vars );
681
- return FALSE;
682
- }
683
- pptr = ZSTR_VAL (type_str );
684
- objid_query -> vars [objid_query -> count ].type = * pptr ;
685
- objid_query -> vars [objid_query -> count ].value = ZSTR_VAL (value_str );
686
- } else {
687
- php_error_docref (NULL , E_WARNING , "Single objid and multiple type or values are not supported" );
676
+ if (type_ht ) {
677
+ zend_type_error ("Type must be of type string when object ID is a string" );
678
+ efree (objid_query -> vars );
679
+ return FALSE;
680
+ }
681
+ if (value_ht ) {
682
+ zend_type_error ("Value must be of type string when object ID is a string" );
688
683
efree (objid_query -> vars );
689
684
return FALSE;
690
685
}
686
+
687
+ /* Both type and value must be valid strings */
688
+ ZEND_ASSERT (type_str && value_str );
689
+
690
+ if (ZSTR_LEN (type_str ) != 1 ) {
691
+ zend_value_error ("Type must be a single character" );
692
+ efree (objid_query -> vars );
693
+ return FALSE;
694
+ }
695
+ pptr = ZSTR_VAL (type_str );
696
+ objid_query -> vars [objid_query -> count ].type = * pptr ;
697
+ objid_query -> vars [objid_query -> count ].value = ZSTR_VAL (value_str );
691
698
}
692
699
objid_query -> count ++ ;
693
700
} else if (oid_ht ) { /* we got objid array */
694
701
if (zend_hash_num_elements (oid_ht ) == 0 ) {
695
- php_error_docref ( NULL , E_WARNING , "Got empty OID array " );
702
+ zend_value_error ( "Array of object IDs cannot be empty " );
696
703
return FALSE;
697
704
}
698
705
objid_query -> vars = (snmpobjarg * )safe_emalloc (sizeof (snmpobjarg ), zend_hash_num_elements (oid_ht ), 0 );
@@ -715,7 +722,7 @@ static int php_snmp_parse_oid(
715
722
if (idx_type < type_ht -> nNumUsed ) {
716
723
convert_to_string_ex (tmp_type );
717
724
if (Z_STRLEN_P (tmp_type ) != 1 ) {
718
- php_error_docref ( NULL , E_WARNING , "'%s': bogus type '%s', should be single char, got %zu" , Z_STRVAL_P ( tmp_oid ), Z_STRVAL_P ( tmp_type ), Z_STRLEN_P ( tmp_type ) );
725
+ zend_value_error ( "Type must be a single character" );
719
726
efree (objid_query -> vars );
720
727
return FALSE;
721
728
}
@@ -916,6 +923,7 @@ static int netsnmp_session_set_sec_level(struct snmp_session *s, char *level)
916
923
} else if (!strcasecmp (level , "authPriv" ) || !strcasecmp (level , "ap" )) {
917
924
s -> securityLevel = SNMP_SEC_LEVEL_AUTHPRIV ;
918
925
} else {
926
+ zend_value_error ("Security level must be one of \"noAuthNoPriv\", \"authNoPriv\", or \"authPriv\"" );
919
927
return (-1 );
920
928
}
921
929
return (0 );
@@ -933,7 +941,7 @@ static int netsnmp_session_set_auth_protocol(struct snmp_session *s, char *prot)
933
941
s -> securityAuthProto = usmHMACSHA1AuthProtocol ;
934
942
s -> securityAuthProtoLen = USM_AUTH_PROTO_SHA_LEN ;
935
943
} else {
936
- php_error_docref ( NULL , E_WARNING , "Unknown authentication protocol '%s'" , prot );
944
+ zend_value_error ( "Authentication protocol must be either MD5 or SHA" );
937
945
return (-1 );
938
946
}
939
947
return (0 );
@@ -953,7 +961,11 @@ static int netsnmp_session_set_sec_protocol(struct snmp_session *s, char *prot)
953
961
s -> securityPrivProtoLen = USM_PRIV_PROTO_AES_LEN ;
954
962
#endif
955
963
} else {
956
- php_error_docref (NULL , E_WARNING , "Unknown security protocol '%s'" , prot );
964
+ #ifdef HAVE_AES
965
+ zend_value_error ("Security protocol must be one of DES, AES128, or AES" );
966
+ #else
967
+ zend_value_error ("Security protocol must be DES" );
968
+ #endif
957
969
return (-1 );
958
970
}
959
971
return (0 );
@@ -987,7 +999,7 @@ static int netsnmp_session_gen_sec_key(struct snmp_session *s, char *pass)
987
999
(u_char * )pass , strlen (pass ),
988
1000
s -> securityPrivKey , & (s -> securityPrivKeyLen )))) {
989
1001
php_error_docref (NULL , E_WARNING , "Error generating a key for privacy pass phrase '%s': %s" , pass , snmp_api_errstring (snmp_errno ));
990
- return (-2 );
1002
+ return (-1 );
991
1003
}
992
1004
return (0 );
993
1005
}
@@ -1001,6 +1013,7 @@ static int netsnmp_session_set_contextEngineID(struct snmp_session *s, char * co
1001
1013
u_char * ebuf = (u_char * ) emalloc (ebuf_len );
1002
1014
1003
1015
if (!snmp_hex_to_binary (& ebuf , & ebuf_len , & eout_len , 1 , contextEngineID )) {
1016
+ // TODO Promote to Error?
1004
1017
php_error_docref (NULL , E_WARNING , "Bad engine ID value '%s'" , contextEngineID );
1005
1018
efree (ebuf );
1006
1019
return (-1 );
@@ -1023,15 +1036,15 @@ static int netsnmp_session_set_security(struct snmp_session *session, char *sec_
1023
1036
1024
1037
/* Setting the security level. */
1025
1038
if (netsnmp_session_set_sec_level (session , sec_level )) {
1026
- php_error_docref ( NULL , E_WARNING , "Invalid security level '%s'" , sec_level );
1039
+ /* ValueError already generated, just bail out */
1027
1040
return (-1 );
1028
1041
}
1029
1042
1030
1043
if (session -> securityLevel == SNMP_SEC_LEVEL_AUTHNOPRIV || session -> securityLevel == SNMP_SEC_LEVEL_AUTHPRIV ) {
1031
1044
1032
1045
/* Setting the authentication protocol. */
1033
1046
if (netsnmp_session_set_auth_protocol (session , auth_protocol )) {
1034
- /* Warning message sent already, just bail out */
1047
+ /* ValueError already generated , just bail out */
1035
1048
return (-1 );
1036
1049
}
1037
1050
@@ -1044,7 +1057,7 @@ static int netsnmp_session_set_security(struct snmp_session *session, char *sec_
1044
1057
if (session -> securityLevel == SNMP_SEC_LEVEL_AUTHPRIV ) {
1045
1058
/* Setting the security protocol. */
1046
1059
if (netsnmp_session_set_sec_protocol (session , priv_protocol )) {
1047
- /* Warning message sent already, just bail out */
1060
+ /* ValueError already generated , just bail out */
1048
1061
return (-1 );
1049
1062
}
1050
1063
@@ -1220,9 +1233,9 @@ static void php_snmp(INTERNAL_FUNCTION_PARAMETERS, int st, int version)
1220
1233
snmp_object = Z_SNMP_P (object );
1221
1234
session = snmp_object -> session ;
1222
1235
if (!session ) {
1223
- php_error_docref (NULL , E_WARNING , "Invalid or uninitialized SNMP object" );
1236
+ zend_throw_error (NULL , "Invalid or uninitialized SNMP object" );
1224
1237
efree (objid_query .vars );
1225
- RETURN_FALSE ;
1238
+ RETURN_THROWS () ;
1226
1239
}
1227
1240
1228
1241
if (snmp_object -> max_oids > 0 ) {
@@ -1351,11 +1364,9 @@ PHP_FUNCTION(snmp_set_oid_output_format)
1351
1364
case NETSNMP_OID_OUTPUT_NONE :
1352
1365
netsnmp_ds_set_int (NETSNMP_DS_LIBRARY_ID , NETSNMP_DS_LIB_OID_OUTPUT_FORMAT , a1 );
1353
1366
RETURN_TRUE ;
1354
- break ;
1355
1367
default :
1356
- php_error_docref (NULL , E_WARNING , "Unknown SNMP output print format '%d'" , (int ) a1 );
1357
- RETURN_FALSE ;
1358
- break ;
1368
+ zend_argument_value_error (1 , "must be an SNMP_OID_OUTPUT_* constant" );
1369
+ RETURN_THROWS ();
1359
1370
}
1360
1371
}
1361
1372
/* }}} */
@@ -1443,8 +1454,8 @@ PHP_FUNCTION(snmp_set_valueretrieval)
1443
1454
SNMP_G (valueretrieval ) = method ;
1444
1455
RETURN_TRUE ;
1445
1456
} else {
1446
- php_error_docref ( NULL , E_WARNING , "Unknown SNMP value retrieval method '" ZEND_LONG_FMT "'" , method );
1447
- RETURN_FALSE ;
1457
+ zend_argument_value_error ( 1 , "must be a bitmask of SNMP_VALUE_LIBRARY, SNMP_VALUE_PLAIN, and SNMP_VALUE_OBJECT" );
1458
+ RETURN_THROWS () ;
1448
1459
}
1449
1460
}
1450
1461
/* }}} */
@@ -1827,48 +1838,46 @@ PHP_SNMP_LONG_PROPERTY_READER_FUNCTION(exceptions_enabled)
1827
1838
/* {{{ */
1828
1839
static int php_snmp_write_info (php_snmp_object * snmp_object , zval * newval )
1829
1840
{
1830
- php_error_docref (NULL , E_WARNING , " info property is read-only" );
1841
+ zend_throw_error (NULL , "SNMP::$ info property is read-only" );
1831
1842
return FAILURE ;
1832
1843
}
1833
1844
/* }}} */
1834
1845
1835
1846
/* {{{ */
1836
1847
static int php_snmp_write_max_oids (php_snmp_object * snmp_object , zval * newval )
1837
1848
{
1838
- int ret = SUCCESS ;
1839
1849
zend_long lval ;
1840
1850
1841
1851
if (Z_TYPE_P (newval ) == IS_NULL ) {
1842
1852
snmp_object -> max_oids = 0 ;
1843
- return ret ;
1853
+ return SUCCESS ;
1844
1854
}
1845
1855
1846
1856
lval = zval_get_long (newval );
1847
1857
1848
- if (lval > 0 ) {
1849
- snmp_object -> max_oids = lval ;
1850
- } else {
1851
- php_error_docref (NULL , E_WARNING , "max_oids should be positive integer or NULL, got " ZEND_LONG_FMT , lval );
1858
+ if (lval <= 0 ) {
1859
+ zend_value_error ("max_oids must be greater than 0 or null" );
1860
+ return FAILURE ;
1852
1861
}
1862
+ snmp_object -> max_oids = lval ;
1853
1863
1854
- return ret ;
1864
+ return SUCCESS ;
1855
1865
}
1856
1866
/* }}} */
1857
1867
1858
1868
/* {{{ */
1859
1869
static int php_snmp_write_valueretrieval (php_snmp_object * snmp_object , zval * newval )
1860
1870
{
1861
- int ret = SUCCESS ;
1862
1871
zend_long lval = zval_get_long (newval );
1863
1872
1864
1873
if (lval >= 0 && lval <= (SNMP_VALUE_LIBRARY |SNMP_VALUE_PLAIN |SNMP_VALUE_OBJECT )) {
1865
1874
snmp_object -> valueretrieval = lval ;
1866
1875
} else {
1867
- php_error_docref ( NULL , E_WARNING , "Unknown SNMP value retrieval method '" ZEND_LONG_FMT "'" , lval );
1868
- ret = FAILURE ;
1876
+ zend_value_error ( " SNMP retrieval method must be a bitmask of SNMP_VALUE_LIBRARY, SNMP_VALUE_PLAIN, and SNMP_VALUE_OBJECT" );
1877
+ return FAILURE ;
1869
1878
}
1870
1879
1871
- return ret ;
1880
+ return SUCCESS ;
1872
1881
}
1873
1882
/* }}} */
1874
1883
@@ -1892,7 +1901,6 @@ PHP_SNMP_BOOL_PROPERTY_WRITER_FUNCTION(oid_increasing_check)
1892
1901
/* {{{ */
1893
1902
static int php_snmp_write_oid_output_format (php_snmp_object * snmp_object , zval * newval )
1894
1903
{
1895
- int ret = SUCCESS ;
1896
1904
zend_long lval = zval_get_long (newval );
1897
1905
1898
1906
switch (lval ) {
@@ -1903,14 +1911,11 @@ static int php_snmp_write_oid_output_format(php_snmp_object *snmp_object, zval *
1903
1911
case NETSNMP_OID_OUTPUT_UCD :
1904
1912
case NETSNMP_OID_OUTPUT_NONE :
1905
1913
snmp_object -> oid_output_format = lval ;
1906
- break ;
1914
+ return SUCCESS ;
1907
1915
default :
1908
- php_error_docref (NULL , E_WARNING , "Unknown SNMP output print format '" ZEND_LONG_FMT "'" , lval );
1909
- ret = FAILURE ;
1910
- break ;
1916
+ zend_value_error ("SNMP output print format must be an SNMP_OID_OUTPUT_* constant" );
1917
+ return FAILURE ;
1911
1918
}
1912
-
1913
- return ret ;
1914
1919
}
1915
1920
/* }}} */
1916
1921
0 commit comments