--- src/Makefile.am | 3 +- src/libvirt-nwfilter.c | 414 +++++++++++++++++++++++++++++++++++++++++++++ src/libvirt-nwfilter.h | 66 ++++++++ src/libvirt-php.c | 445 +------------------------------------------------ src/libvirt-php.h | 28 ---- 5 files changed, 486 insertions(+), 470 deletions(-) create mode 100644 src/libvirt-nwfilter.c create mode 100644 src/libvirt-nwfilter.h diff --git a/src/Makefile.am b/src/Makefile.am index 30bebad..707a1e8 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -29,7 +29,8 @@ libvirt_php_la_SOURCES = \ libvirt-snapshot.c libvirt-snapshot.h \ libvirt-storage.c libvirt-storage.h \ libvirt-network.c libvirt-network.h \ - libvirt-nodedev.c libvirt-nodedev.h + libvirt-nodedev.c libvirt-nodedev.h \ + libvirt-nwfilter.c libvirt-nwfilter.h libvirt_php_la_CFLAGS = \ $(AM_CFLAGS) \ -DCOMPILE_DL_LIBVIRT=1 diff --git a/src/libvirt-nwfilter.c b/src/libvirt-nwfilter.c new file mode 100644 index 0000000..f610f40 --- /dev/null +++ b/src/libvirt-nwfilter.c @@ -0,0 +1,414 @@ +/* + * libvirt-nwfilter.c: The PHP bindings to libvirt NWFilter API + * + * See COPYING for the license of this software + */ + +#include <libvirt/libvirt.h> + +#include "libvirt-nwfilter.h" + +DEBUG_INIT("nwfilter"); + +void +php_libvirt_nwfilter_dtor(virt_resource *rsrc TSRMLS_DC) +{ + php_libvirt_nwfilter *nwfilter = (php_libvirt_nwfilter *) rsrc->ptr; + int rv = 0; + + if (nwfilter != NULL) { + if (nwfilter->nwfilter != NULL) { + if (!check_resource_allocation(NULL, INT_RESOURCE_NWFILTER, nwfilter->nwfilter TSRMLS_CC)) { + nwfilter->nwfilter = NULL; + efree(nwfilter); + + return; + } + rv = virNWFilterFree(nwfilter->nwfilter); + if (rv != 0) { + DPRINTF("%s: virNWFilterFree(%p) returned %d\n", __FUNCTION__, nwfilter->nwfilter, rv); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "virNWFilterFree failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); + } else { + DPRINTF("%s: virNWFilterFee(%p) completed successfully\n", __FUNCTION__, nwfilter->nwfilter); + resource_change_counter(INT_RESOURCE_NWFILTER, nwfilter->conn->conn, nwfilter->nwfilter, 0 TSRMLS_CC); + } + nwfilter->nwfilter = NULL; + } + efree(nwfilter); + } +} + +/* + * Function name: libvirt_nwfilter_define_xml + * Since version: 0.5.4 + * Description: Function is used to define a new nwfilter based on the XML description + * Arguments: @res [resource]: libvirt connection resource + * @xml [string]: XML string definition of nwfilter to be defined + * Returns: libvirt nwfilter resource of newly defined nwfilter + */ +PHP_FUNCTION(libvirt_nwfilter_define_xml) +{ + php_libvirt_connection *conn = NULL; + php_libvirt_nwfilter *res_nwfilter = NULL; + virNWFilter *nwfilter; + zval *zconn; + char *xml = NULL; + strsize_t xml_len; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &xml, &xml_len); + + if ((nwfilter = virNWFilterDefineXML(conn->conn, xml)) == NULL) { + set_error_if_unset("Cannot define a new NWFilter" TSRMLS_CC); + RETURN_FALSE; + } + + res_nwfilter = (php_libvirt_nwfilter *) emalloc(sizeof(php_libvirt_nwfilter)); + res_nwfilter->nwfilter = nwfilter; + res_nwfilter->conn = conn; + + resource_change_counter(INT_RESOURCE_NWFILTER, conn->conn, + res_nwfilter->nwfilter, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_nwfilter, le_libvirt_nwfilter); +} + +/* + * Function name: libvirt_nwfilter_undefine + * Since version: 0.5.4 + * Description: Function is used to undefine already defined nwfilter + * Arguments: @res [resource]: libvirt nwfilter resource + * Returns: TRUE for success, FALSE on error + */ +PHP_FUNCTION(libvirt_nwfilter_undefine) +{ + php_libvirt_nwfilter *nwfilter = NULL; + zval *znwfilter; + + GET_NWFILTER_FROM_ARGS("r", &znwfilter); + + if (virNWFilterUndefine(nwfilter->nwfilter) != 0) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_nwfilter_get_xml_desc + * Since version: 0.5.4 + * Description: Function is used to get the XML description for the nwfilter + * Arguments: @res [resource]: libvirt nwfilter resource + * @xpath [string]: optional xPath expression string to get just this entry, can be NULL + * Returns: nwfilter XML string or result of xPath expression + */ +PHP_FUNCTION(libvirt_nwfilter_get_xml_desc) +{ + php_libvirt_nwfilter *nwfilter = NULL; + zval *znwfilter; + char *xml = NULL; + char *xpath = NULL; + char *tmp; + strsize_t xpath_len; + int retval = -1; + + GET_NWFILTER_FROM_ARGS("r|s", &znwfilter, &xpath, &xpath_len); + + if (xpath_len < 1) + xpath = NULL; + + xml = virNWFilterGetXMLDesc(nwfilter->nwfilter, 0); + + if (xml == NULL) { + set_error_if_unset("Cannot get nwfilter XML" TSRMLS_CC); + RETURN_FALSE; + } + + tmp = get_string_from_xpath(xml, xpath, NULL, &retval); + + if (tmp == NULL || retval < 0) + VIRT_RETVAL_STRING(xml); + else + VIRT_RETVAL_STRING(tmp); + + free(xml); + free(tmp); +} + +/* + * Function name: libvirt_nwfilter_get_uuid_string + * Since version: 0.5.4 + * Description: Function is used to get nwfilter's UUID in string format + * Arguments: @res [resource]: libvirt nwfilter resource + * Returns: nwfilter UUID string or FALSE on failure + */ +PHP_FUNCTION(libvirt_nwfilter_get_uuid_string) +{ + php_libvirt_nwfilter *nwfilter = NULL; + zval *znwfilter; + char *uuid = NULL; + int ret = -1; + + GET_NWFILTER_FROM_ARGS("r", &znwfilter); + + uuid = (char *) emalloc(VIR_UUID_STRING_BUFLEN); + ret = virNWFilterGetUUIDString(nwfilter->nwfilter, uuid); + + DPRINTF("%s: virNWFilterGetUUIDString(%p) returned %d (%s)\n", PHPFUNC, + nwfilter->nwfilter, ret, uuid); + + if (ret != 0) + RETURN_FALSE; + + VIRT_RETURN_STRING(uuid); + efree(uuid); +} + +/* + * Function name: libvirt_nwfilter_get_uuid + * Since version: 0.5.3 + * Descirption: Function is used to get nwfilter's UUID in binary format + * Arguments: @res [resource]: libvirt netowrk resource + * Returns: nwfilter UUID in binary format or FALSE on failure + */ +PHP_FUNCTION(libvirt_nwfilter_get_uuid) +{ + php_libvirt_nwfilter *nwfilter = NULL; + zval *znwfilter; + char *uuid = NULL; + int ret = -1; + + GET_NWFILTER_FROM_ARGS("r", &znwfilter); + + uuid = (char *) emalloc(VIR_UUID_BUFLEN); + ret = virNWFilterGetUUID(nwfilter->nwfilter, (unsigned char *) uuid); + + DPRINTF("%s: virNWFilterUUID(%p, %p) returned %d\n", PHPFUNC, + nwfilter->nwfilter, uuid, ret); + + if (ret != 0) + RETURN_FALSE; + + VIRT_RETVAL_STRING(uuid); + efree(uuid); +} + +/* + * Function name: libvirt_nwfilter_get_name + * Since version: 0.5.4 + * Description: Function is used to get nwfilter's name + * Arguments: @res [resource]: libvirt nwfilter resource + * Returns: nwfilter name string or FALSE on failure + */ +PHP_FUNCTION(libvirt_nwfilter_get_name) +{ + php_libvirt_nwfilter *nwfilter = NULL; + zval *znwfilter; + const char *name = NULL; + + GET_NWFILTER_FROM_ARGS("r", &znwfilter); + name = virNWFilterGetName(nwfilter->nwfilter); + + DPRINTF("%s: virNWFilterGetName(%p) returned %s\n", PHPFUNC, + nwfilter->nwfilter, name); + + if (name == NULL) + RETURN_FALSE; + + /* name should not be freed as its lifetime is the same as nwfilter resource */ + VIRT_RETURN_STRING(name); +} + +/* + * Function name: libvirt_nwfilter_lookup_by_name + * Since version: 0.5.4 + * Description: This functions is used to lookup for the nwfilter by it's name + * Arguments: @res [resource]: libvirt connection resource + * @name [string]: name of the nwfilter to get the resource + * Returns: libvirt nwfilter resource + */ +PHP_FUNCTION(libvirt_nwfilter_lookup_by_name) +{ + php_libvirt_nwfilter *res_nwfilter = NULL; + php_libvirt_connection *conn = NULL; + virNWFilterPtr nwfilter = NULL; + zval *zconn; + strsize_t name_len; + char *name = NULL; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &name, &name_len); + + if (name == NULL || name_len < 1) + RETURN_FALSE; + + nwfilter = virNWFilterLookupByName(conn->conn, name); + + if (nwfilter == NULL) + RETURN_FALSE; + + res_nwfilter = (php_libvirt_nwfilter *) emalloc(sizeof(php_libvirt_nwfilter)); + res_nwfilter->conn = conn; + res_nwfilter->nwfilter = nwfilter; + + resource_change_counter(INT_RESOURCE_NWFILTER, conn->conn, + res_nwfilter->nwfilter, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_nwfilter, le_libvirt_nwfilter); +} + +/* + * Function name: libvirt_nwfilter_lookup_by_uuid_string + * Since version: 0.5.4 + * Description: Function is used to lookup for nwfilter identified by UUID string + * Arguments: @res [resource]: libvirt connection resource + * @uuid [string]: UUID string to look for nwfilter + * Returns: libvirt nwfilter resource + */ +PHP_FUNCTION(libvirt_nwfilter_lookup_by_uuid_string) +{ + php_libvirt_nwfilter *res_nwfilter = NULL; + php_libvirt_connection *conn = NULL; + virNWFilterPtr nwfilter = NULL; + zval *zconn; + char *uuid = NULL; + strsize_t uuid_len; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &uuid, &uuid_len); + + if (uuid == NULL || uuid_len < 1) + RETURN_FALSE; + + nwfilter = virNWFilterLookupByUUIDString(conn->conn, uuid); + + if (nwfilter == NULL) + RETURN_FALSE; + + res_nwfilter = (php_libvirt_nwfilter *) emalloc(sizeof(php_libvirt_nwfilter)); + res_nwfilter->conn = conn; + res_nwfilter->nwfilter = nwfilter; + + resource_change_counter(INT_RESOURCE_NWFILTER, conn->conn, + res_nwfilter->nwfilter, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_nwfilter, le_libvirt_nwfilter); +} + +/* + * Function name: libvirt_nwfilter_lookup_by_uuid + * Since version: 0.5.4 + * Description: Function is used to lookup for nwfilter by it's UUID in the binary format + * Arguments: @res [resource]: libvirt connection resource from libvirt_connect() + * @uuid [string]: binary defined UUID to look for + * Returns: libvirt nwfilter resource + */ +PHP_FUNCTION(libvirt_nwfilter_lookup_by_uuid) +{ + php_libvirt_nwfilter *res_nwfilter = NULL; + php_libvirt_connection *conn = NULL; + virNWFilterPtr nwfilter = NULL; + zval *zconn; + strsize_t uuid_len; + unsigned char *uuid = NULL; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &uuid, &uuid_len); + + if ((uuid == NULL) || (uuid_len < 1)) + RETURN_FALSE; + + nwfilter = virNWFilterLookupByUUID(conn->conn, uuid); + + if (nwfilter == NULL) + RETURN_FALSE; + + res_nwfilter = (php_libvirt_nwfilter *) emalloc(sizeof(php_libvirt_nwfilter)); + res_nwfilter->conn = conn; + res_nwfilter->nwfilter = nwfilter; + + resource_change_counter(INT_RESOURCE_NWFILTER, conn->conn, + res_nwfilter->nwfilter, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_nwfilter, le_libvirt_nwfilter); +} + +/* + * Function name: libvirt_list_all_nwfilters + * Since version: 0.5.4 + * Description: Function is used to list nwfilters on the connection + * Arguments: @res [resource]: libvirt connection resource + * Returns: libvirt nwfilter resources array for the connection + */ +PHP_FUNCTION(libvirt_list_all_nwfilters) +{ + php_libvirt_nwfilter *res_nwfilter; + php_libvirt_connection *conn = NULL; + virNWFilterPtr *filters = NULL; + virNWFilterPtr nwfilter = NULL; + zval *zconn; + int count = -1; + size_t i = 0; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + /* in current libvirt version, flags are not used for this, so passing 0 */ + if ((count = virConnectListAllNWFilters(conn->conn, &filters, 0)) < 0) + RETURN_FALSE; + + DPRINTF("%s: Found %d nwfilters\n", PHPFUNC, count); + + array_init(return_value); + + for (i = 0; i < count; i++) { + nwfilter = filters[i]; + res_nwfilter = (php_libvirt_nwfilter *) emalloc(sizeof(php_libvirt_nwfilter)); + res_nwfilter->nwfilter = nwfilter; + res_nwfilter->conn = conn; + + resource_change_counter(INT_RESOURCE_NWFILTER, conn->conn, + res_nwfilter->nwfilter, 1 TSRMLS_CC); + VIRT_REGISTER_LIST_RESOURCE(nwfilter); + } +} + +/* + * Function name: libvirt_list_nwfilters + * Since version: 0.5.4 + * Description: Function is used to list nwfilters on the connection + * Arguments: @res [resource]: libvirt connection resource + * Returns: libvirt nwfilter names array for the connection + */ +PHP_FUNCTION(libvirt_list_nwfilters) +{ + php_libvirt_connection *conn = NULL; + zval *zconn; + int count = -1; + int expectedcount = -1; + char **names; + int i, done = 0; + + GET_CONNECTION_FROM_ARGS("r", &zconn); + + array_init(return_value); + + if ((expectedcount = virConnectNumOfNWFilters(conn->conn)) < 0) + RETURN_FALSE; + + names = (char **) emalloc(expectedcount * sizeof(char *)); + count = virConnectListNWFilters(conn->conn, names, expectedcount); + + if (count != expectedcount || count < 0) { + efree(names); + DPRINTF("%s: virConnectListNWFilters returned %d filters, while %d was " + "expected\n", PHPFUNC, count, expectedcount); + RETURN_FALSE; + } + + for (i = 0; i < count; i++) { + VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); + free(names[i]); + } + + efree(names); + done++; + + + if (!done) + RETURN_FALSE; +} diff --git a/src/libvirt-nwfilter.h b/src/libvirt-nwfilter.h new file mode 100644 index 0000000..c0cebb3 --- /dev/null +++ b/src/libvirt-nwfilter.h @@ -0,0 +1,66 @@ +/* + * libvirt-nwfilter.h: libvirt PHP binding for the NWFilter driver + * + * See COPYING for the license of this software + */ + +#ifndef __LIBVIRT_NWFILTER_H__ +# define __LIBVIRT_NWFILTER_H__ + +# include "libvirt-connection.h" + +# define PHP_LIBVIRT_NWFILTER_RES_NAME "Libvirt nwfilter" +# define INT_RESOURCE_NWFILTER 0x60 + +# define PHP_FE_LIBVIRT_NWFILTER \ + PHP_FE(libvirt_nwfilter_define_xml, arginfo_libvirt_conn_xml) \ + PHP_FE(libvirt_nwfilter_undefine, arginfo_libvirt_conn) \ + PHP_FE(libvirt_nwfilter_get_xml_desc, arginfo_libvirt_conn_xpath) \ + PHP_FE(libvirt_nwfilter_get_uuid_string, arginfo_libvirt_conn) \ + PHP_FE(libvirt_nwfilter_get_uuid, arginfo_libvirt_conn) \ + PHP_FE(libvirt_nwfilter_get_name, arginfo_libvirt_conn) \ + PHP_FE(libvirt_nwfilter_lookup_by_name, arginfo_libvirt_conn_name) \ + PHP_FE(libvirt_nwfilter_lookup_by_uuid_string, arginfo_libvirt_conn_uuid) \ + PHP_FE(libvirt_nwfilter_lookup_by_uuid, arginfo_libvirt_conn_uuid) \ + PHP_FE(libvirt_list_all_nwfilters, arginfo_libvirt_conn) \ + PHP_FE(libvirt_list_nwfilters, arginfo_libvirt_conn) + +# define GET_NWFILTER_FROM_ARGS(args, ...) \ + do { \ + reset_error(TSRMLS_C); \ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, \ + args, \ + __VA_ARGS__) == FAILURE) { \ + set_error("Invalid arguments" TSRMLS_CC); \ + RETURN_FALSE; \ + } \ + \ + VIRT_FETCH_RESOURCE(nwfilter, php_libvirt_nwfilter *, &znwfilter, \ + PHP_LIBVIRT_NWFILTER_RES_NAME, \ + le_libvirt_nwfilter); \ + if ((nwfilter == NULL) || (nwfilter->nwfilter == NULL)) \ + RETURN_FALSE; \ + } while (0) \ + +int le_libvirt_nwfilter; + +typedef struct _php_libvirt_nwfilter { + virNWFilterPtr nwfilter; + php_libvirt_connection* conn; +} php_libvirt_nwfilter; + +void php_libvirt_nwfilter_dtor(virt_resource *rsrc TSRMLS_DC); + +PHP_FUNCTION(libvirt_nwfilter_define_xml); +PHP_FUNCTION(libvirt_nwfilter_undefine); +PHP_FUNCTION(libvirt_nwfilter_get_xml_desc); +PHP_FUNCTION(libvirt_nwfilter_get_name); +PHP_FUNCTION(libvirt_nwfilter_get_uuid_string); +PHP_FUNCTION(libvirt_nwfilter_get_uuid); +PHP_FUNCTION(libvirt_nwfilter_lookup_by_name); +PHP_FUNCTION(libvirt_nwfilter_lookup_by_uuid_string); +PHP_FUNCTION(libvirt_nwfilter_lookup_by_uuid); +PHP_FUNCTION(libvirt_list_all_nwfilters); +PHP_FUNCTION(libvirt_list_nwfilters); + +#endif diff --git a/src/libvirt-php.c b/src/libvirt-php.c index b62bcac..4ef06b2 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -28,6 +28,7 @@ #include "libvirt-storage.h" #include "libvirt-network.h" #include "libvirt-nodedev.h" +#include "libvirt-nwfilter.h" DEBUG_INIT("core"); @@ -40,9 +41,6 @@ const char *features[] = { NULL }; const char *features_binaries[] = { NULL }; #endif -/* ZEND thread safe per request globals definition */ -int le_libvirt_nwfilter; - ZEND_BEGIN_ARG_INFO_EX(arginfo_libvirt_connect, 0, 0, 0) ZEND_ARG_INFO(0, url) ZEND_ARG_INFO(0, readonly) @@ -472,8 +470,6 @@ ZEND_ARG_INFO(0, flags) ZEND_END_ARG_INFO() static zend_function_entry libvirt_functions[] = { - /* Common functions */ - PHP_FE(libvirt_get_last_error, arginfo_libvirt_void) PHP_FE_LIBVIRT_CONNECTION PHP_FE_LIBVIRT_STREAM PHP_FE_LIBVIRT_DOMAIN @@ -482,19 +478,9 @@ static zend_function_entry libvirt_functions[] = { PHP_FE_LIBVIRT_NETWORK PHP_FE_LIBVIRT_NODE PHP_FE_LIBVIRT_NODEDEV - /* NWFilter functions */ - PHP_FE(libvirt_nwfilter_define_xml, arginfo_libvirt_conn_xml) - PHP_FE(libvirt_nwfilter_undefine, arginfo_libvirt_conn) - PHP_FE(libvirt_nwfilter_get_xml_desc, arginfo_libvirt_conn_xpath) - PHP_FE(libvirt_nwfilter_get_uuid_string, arginfo_libvirt_conn) - PHP_FE(libvirt_nwfilter_get_uuid, arginfo_libvirt_conn) - PHP_FE(libvirt_nwfilter_get_name, arginfo_libvirt_conn) - PHP_FE(libvirt_nwfilter_lookup_by_name, arginfo_libvirt_conn_name) - PHP_FE(libvirt_nwfilter_lookup_by_uuid_string, arginfo_libvirt_conn_uuid) - PHP_FE(libvirt_nwfilter_lookup_by_uuid, arginfo_libvirt_conn_uuid) - /* List functions */ - PHP_FE(libvirt_list_all_nwfilters, arginfo_libvirt_conn) - PHP_FE(libvirt_list_nwfilters, arginfo_libvirt_conn) + PHP_FE_LIBVIRT_NWFILTER + /* Common functions */ + PHP_FE(libvirt_get_last_error, arginfo_libvirt_void) /* Version information and common function */ PHP_FE(libvirt_version, arginfo_libvirt_opttype) PHP_FE(libvirt_check_version, arginfo_libvirt_check_version) @@ -1174,34 +1160,6 @@ int is_local_connection(virConnectPtr conn) #endif } -/* Destructor for nwfilter resource */ -static void php_libvirt_nwfilter_dtor(virt_resource *rsrc TSRMLS_DC) -{ - php_libvirt_nwfilter *nwfilter = (php_libvirt_nwfilter *) rsrc->ptr; - int rv = 0; - - if (nwfilter != NULL) { - if (nwfilter->nwfilter != NULL) { - if (!check_resource_allocation(NULL, INT_RESOURCE_NWFILTER, nwfilter->nwfilter TSRMLS_CC)) { - nwfilter->nwfilter = NULL; - efree(nwfilter); - - return; - } - rv = virNWFilterFree(nwfilter->nwfilter); - if (rv != 0) { - DPRINTF("%s: virNWFilterFree(%p) returned %d\n", __FUNCTION__, nwfilter->nwfilter, rv); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "virNWFilterFree failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); - } else { - DPRINTF("%s: virNWFilterFee(%p) completed successfully\n", __FUNCTION__, nwfilter->nwfilter); - resource_change_counter(INT_RESOURCE_NWFILTER, nwfilter->conn->conn, nwfilter->nwfilter, 0 TSRMLS_CC); - } - nwfilter->nwfilter = NULL; - } - efree(nwfilter); - } -} - /* ZEND Module inicialization function */ PHP_MINIT_FUNCTION(libvirt) { @@ -1526,22 +1484,6 @@ PHP_MSHUTDOWN_FUNCTION(libvirt) return SUCCESS; } -/* Macros for obtaining resources from arguments */ -#define GET_NWFILTER_FROM_ARGS(args, ...) \ - do { \ - reset_error(TSRMLS_C); \ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, args, __VA_ARGS__) == FAILURE) { \ - set_error("Invalid arguments" TSRMLS_CC); \ - RETURN_FALSE; \ - } \ - \ - VIRT_FETCH_RESOURCE(nwfilter, php_libvirt_nwfilter *, &znwfilter, \ - PHP_LIBVIRT_NWFILTER_RES_NAME, le_libvirt_nwfilter); \ - if ((nwfilter == NULL) || (nwfilter->nwfilter == NULL)) \ - RETURN_FALSE; \ - } while (0) \ - - /* Common functions */ /* @@ -2514,385 +2456,6 @@ void parse_array(zval *arr, tVMDisk *disk, tVMNetwork *network) } VIRT_FOREACH_END(); } -/* Listing functions */ - -/* - * Function name: libvirt_list_all_nwfilters - * Since version: 0.5.4 - * Description: Function is used to list nwfilters on the connection - * Arguments: @res [resource]: libvirt connection resource - * Returns: libvirt nwfilter resources array for the connection - */ -PHP_FUNCTION(libvirt_list_all_nwfilters) -{ - php_libvirt_nwfilter *res_nwfilter; - php_libvirt_connection *conn = NULL; - virNWFilterPtr *filters = NULL; - virNWFilterPtr nwfilter = NULL; - zval *zconn; - int count = -1; - size_t i = 0; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - /* in current libvirt version, flags are not used for this, so passing 0 */ - if ((count = virConnectListAllNWFilters(conn->conn, &filters, 0)) < 0) - RETURN_FALSE; - - DPRINTF("%s: Found %d nwfilters\n", PHPFUNC, count); - - array_init(return_value); - - for (i = 0; i < count; i++) { - nwfilter = filters[i]; - res_nwfilter = (php_libvirt_nwfilter *) emalloc(sizeof(php_libvirt_nwfilter)); - res_nwfilter->nwfilter = nwfilter; - res_nwfilter->conn = conn; - - resource_change_counter(INT_RESOURCE_NWFILTER, conn->conn, - res_nwfilter->nwfilter, 1 TSRMLS_CC); - VIRT_REGISTER_LIST_RESOURCE(nwfilter); - } -} - -/* - * Function name: libvirt_list_nwfilters - * Since version: 0.5.4 - * Description: Function is used to list nwfilters on the connection - * Arguments: @res [resource]: libvirt connection resource - * Returns: libvirt nwfilter names array for the connection - */ -PHP_FUNCTION(libvirt_list_nwfilters) -{ - php_libvirt_connection *conn = NULL; - zval *zconn; - int count = -1; - int expectedcount = -1; - char **names; - int i, done = 0; - - GET_CONNECTION_FROM_ARGS("r", &zconn); - - array_init(return_value); - - if ((expectedcount = virConnectNumOfNWFilters(conn->conn)) < 0) - RETURN_FALSE; - - names = (char **) emalloc(expectedcount * sizeof(char *)); - count = virConnectListNWFilters(conn->conn, names, expectedcount); - - if (count != expectedcount || count < 0) { - efree(names); - DPRINTF("%s: virConnectListNWFilters returned %d filters, while %d was " - "expected\n", PHPFUNC, count, expectedcount); - RETURN_FALSE; - } - - for (i = 0; i < count; i++) { - VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); - free(names[i]); - } - - efree(names); - done++; - - - if (!done) - RETURN_FALSE; -} - -/* NWFilter functions */ - -/* - * Function name: libvirt_nwfilter_define_xml - * Since version: 0.5.4 - * Description: Function is used to define a new nwfilter based on the XML description - * Arguments: @res [resource]: libvirt connection resource - * @xml [string]: XML string definition of nwfilter to be defined - * Returns: libvirt nwfilter resource of newly defined nwfilter - */ -PHP_FUNCTION(libvirt_nwfilter_define_xml) -{ - php_libvirt_connection *conn = NULL; - php_libvirt_nwfilter *res_nwfilter = NULL; - virNWFilter *nwfilter; - zval *zconn; - char *xml = NULL; - strsize_t xml_len; - - GET_CONNECTION_FROM_ARGS("rs", &zconn, &xml, &xml_len); - - if ((nwfilter = virNWFilterDefineXML(conn->conn, xml)) == NULL) { - set_error_if_unset("Cannot define a new NWFilter" TSRMLS_CC); - RETURN_FALSE; - } - - res_nwfilter = (php_libvirt_nwfilter *) emalloc(sizeof(php_libvirt_nwfilter)); - res_nwfilter->nwfilter = nwfilter; - res_nwfilter->conn = conn; - - resource_change_counter(INT_RESOURCE_NWFILTER, conn->conn, - res_nwfilter->nwfilter, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_nwfilter, le_libvirt_nwfilter); -} - -/* - * Function name: libvirt_nwfilter_undefine - * Since version: 0.5.4 - * Description: Function is used to undefine already defined nwfilter - * Arguments: @res [resource]: libvirt nwfilter resource - * Returns: TRUE for success, FALSE on error - */ -PHP_FUNCTION(libvirt_nwfilter_undefine) -{ - php_libvirt_nwfilter *nwfilter = NULL; - zval *znwfilter; - - GET_NWFILTER_FROM_ARGS("r", &znwfilter); - - if (virNWFilterUndefine(nwfilter->nwfilter) != 0) - RETURN_FALSE; - - RETURN_TRUE; -} - -/* - * Function name: libvirt_nwfilter_get_xml_desc - * Since version: 0.5.4 - * Description: Function is used to get the XML description for the nwfilter - * Arguments: @res [resource]: libvirt nwfilter resource - * @xpath [string]: optional xPath expression string to get just this entry, can be NULL - * Returns: nwfilter XML string or result of xPath expression - */ -PHP_FUNCTION(libvirt_nwfilter_get_xml_desc) -{ - php_libvirt_nwfilter *nwfilter = NULL; - zval *znwfilter; - char *xml = NULL; - char *xpath = NULL; - char *tmp; - strsize_t xpath_len; - int retval = -1; - - GET_NWFILTER_FROM_ARGS("r|s", &znwfilter, &xpath, &xpath_len); - - if (xpath_len < 1) - xpath = NULL; - - xml = virNWFilterGetXMLDesc(nwfilter->nwfilter, 0); - - if (xml == NULL) { - set_error_if_unset("Cannot get nwfilter XML" TSRMLS_CC); - RETURN_FALSE; - } - - tmp = get_string_from_xpath(xml, xpath, NULL, &retval); - - if (tmp == NULL || retval < 0) - VIRT_RETVAL_STRING(xml); - else - VIRT_RETVAL_STRING(tmp); - - free(xml); - free(tmp); -} - -/* - * Function name: libvirt_nwfilter_get_uuid_string - * Since version: 0.5.4 - * Description: Function is used to get nwfilter's UUID in string format - * Arguments: @res [resource]: libvirt nwfilter resource - * Returns: nwfilter UUID string or FALSE on failure - */ -PHP_FUNCTION(libvirt_nwfilter_get_uuid_string) -{ - php_libvirt_nwfilter *nwfilter = NULL; - zval *znwfilter; - char *uuid = NULL; - int ret = -1; - - GET_NWFILTER_FROM_ARGS("r", &znwfilter); - - uuid = (char *) emalloc(VIR_UUID_STRING_BUFLEN); - ret = virNWFilterGetUUIDString(nwfilter->nwfilter, uuid); - - DPRINTF("%s: virNWFilterGetUUIDString(%p) returned %d (%s)\n", PHPFUNC, - nwfilter->nwfilter, ret, uuid); - - if (ret != 0) - RETURN_FALSE; - - VIRT_RETURN_STRING(uuid); - efree(uuid); -} - -/* - * Function name: libvirt_nwfilter_get_uuid - * Since version: 0.5.3 - * Descirption: Function is used to get nwfilter's UUID in binary format - * Arguments: @res [resource]: libvirt netowrk resource - * Returns: nwfilter UUID in binary format or FALSE on failure - */ -PHP_FUNCTION(libvirt_nwfilter_get_uuid) -{ - php_libvirt_nwfilter *nwfilter = NULL; - zval *znwfilter; - char *uuid = NULL; - int ret = -1; - - GET_NWFILTER_FROM_ARGS("r", &znwfilter); - - uuid = (char *) emalloc(VIR_UUID_BUFLEN); - ret = virNWFilterGetUUID(nwfilter->nwfilter, (unsigned char *) uuid); - - DPRINTF("%s: virNWFilterUUID(%p, %p) returned %d\n", PHPFUNC, - nwfilter->nwfilter, uuid, ret); - - if (ret != 0) - RETURN_FALSE; - - VIRT_RETVAL_STRING(uuid); - efree(uuid); -} - -/* - * Function name: libvirt_nwfilter_get_name - * Since version: 0.5.4 - * Description: Function is used to get nwfilter's name - * Arguments: @res [resource]: libvirt nwfilter resource - * Returns: nwfilter name string or FALSE on failure - */ -PHP_FUNCTION(libvirt_nwfilter_get_name) -{ - php_libvirt_nwfilter *nwfilter = NULL; - zval *znwfilter; - const char *name = NULL; - - GET_NWFILTER_FROM_ARGS("r", &znwfilter); - name = virNWFilterGetName(nwfilter->nwfilter); - - DPRINTF("%s: virNWFilterGetName(%p) returned %s\n", PHPFUNC, - nwfilter->nwfilter, name); - - if (name == NULL) - RETURN_FALSE; - - /* name should not be freed as its lifetime is the same as nwfilter resource */ - VIRT_RETURN_STRING(name); -} - -/* - * Function name: libvirt_nwfilter_lookup_by_name - * Since version: 0.5.4 - * Description: This functions is used to lookup for the nwfilter by it's name - * Arguments: @res [resource]: libvirt connection resource - * @name [string]: name of the nwfilter to get the resource - * Returns: libvirt nwfilter resource - */ -PHP_FUNCTION(libvirt_nwfilter_lookup_by_name) -{ - php_libvirt_nwfilter *res_nwfilter = NULL; - php_libvirt_connection *conn = NULL; - virNWFilterPtr nwfilter = NULL; - zval *zconn; - strsize_t name_len; - char *name = NULL; - - GET_CONNECTION_FROM_ARGS("rs", &zconn, &name, &name_len); - - if (name == NULL || name_len < 1) - RETURN_FALSE; - - nwfilter = virNWFilterLookupByName(conn->conn, name); - - if (nwfilter == NULL) - RETURN_FALSE; - - res_nwfilter = (php_libvirt_nwfilter *) emalloc(sizeof(php_libvirt_nwfilter)); - res_nwfilter->conn = conn; - res_nwfilter->nwfilter = nwfilter; - - resource_change_counter(INT_RESOURCE_NWFILTER, conn->conn, - res_nwfilter->nwfilter, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_nwfilter, le_libvirt_nwfilter); -} - -/* - * Function name: libvirt_nwfilter_lookup_by_uuid_string - * Since version: 0.5.4 - * Description: Function is used to lookup for nwfilter identified by UUID string - * Arguments: @res [resource]: libvirt connection resource - * @uuid [string]: UUID string to look for nwfilter - * Returns: libvirt nwfilter resource - */ -PHP_FUNCTION(libvirt_nwfilter_lookup_by_uuid_string) -{ - php_libvirt_nwfilter *res_nwfilter = NULL; - php_libvirt_connection *conn = NULL; - virNWFilterPtr nwfilter = NULL; - zval *zconn; - char *uuid = NULL; - strsize_t uuid_len; - - GET_CONNECTION_FROM_ARGS("rs", &zconn, &uuid, &uuid_len); - - if (uuid == NULL || uuid_len < 1) - RETURN_FALSE; - - nwfilter = virNWFilterLookupByUUIDString(conn->conn, uuid); - - if (nwfilter == NULL) - RETURN_FALSE; - - res_nwfilter = (php_libvirt_nwfilter *) emalloc(sizeof(php_libvirt_nwfilter)); - res_nwfilter->conn = conn; - res_nwfilter->nwfilter = nwfilter; - - resource_change_counter(INT_RESOURCE_NWFILTER, conn->conn, - res_nwfilter->nwfilter, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_nwfilter, le_libvirt_nwfilter); -} - -/* - * Function name: libvirt_nwfilter_lookup_by_uuid - * Since version: 0.5.4 - * Description: Function is used to lookup for nwfilter by it's UUID in the binary format - * Arguments: @res [resource]: libvirt connection resource from libvirt_connect() - * @uuid [string]: binary defined UUID to look for - * Returns: libvirt nwfilter resource - */ -PHP_FUNCTION(libvirt_nwfilter_lookup_by_uuid) -{ - php_libvirt_nwfilter *res_nwfilter = NULL; - php_libvirt_connection *conn = NULL; - virNWFilterPtr nwfilter = NULL; - zval *zconn; - strsize_t uuid_len; - unsigned char *uuid = NULL; - - GET_CONNECTION_FROM_ARGS("rs", &zconn, &uuid, &uuid_len); - - if ((uuid == NULL) || (uuid_len < 1)) - RETURN_FALSE; - - nwfilter = virNWFilterLookupByUUID(conn->conn, uuid); - - if (nwfilter == NULL) - RETURN_FALSE; - - res_nwfilter = (php_libvirt_nwfilter *) emalloc(sizeof(php_libvirt_nwfilter)); - res_nwfilter->conn = conn; - res_nwfilter->nwfilter = nwfilter; - - resource_change_counter(INT_RESOURCE_NWFILTER, conn->conn, - res_nwfilter->nwfilter, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_nwfilter, le_libvirt_nwfilter); -} - /* * Function name: libvirt_version * Since version: 0.4.1(-1) diff --git a/src/libvirt-php.h b/src/libvirt-php.h index 676b29e..82b70dc 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -127,9 +127,6 @@ typedef uint64_t arch_uint; #define PHP_LIBVIRT_WORLD_VERSION VERSION #define PHP_LIBVIRT_WORLD_EXTNAME "libvirt" -/* Internal resource identifier objects */ -#define INT_RESOURCE_NWFILTER 0x60 - typedef struct tTokenizer { char **tokens; int numTokens; @@ -157,15 +154,6 @@ typedef struct tVMNetwork { char *model; } tVMNetwork; -/* TODO: temporary forward declaration until other parts are "modularized" */ -typedef struct _php_libvirt_connection php_libvirt_connection; - -/* Libvirt-php types */ -typedef struct _php_libvirt_nwfilter { - virNWFilterPtr nwfilter; - php_libvirt_connection* conn; -} php_libvirt_nwfilter; - typedef struct _php_libvirt_hash_key_info { char *name; unsigned int length; @@ -218,8 +206,6 @@ const char *get_feature_binary(const char *name); long get_next_free_numeric_value(virDomainPtr domain, char *xpath); int get_subnet_bits(char *ip); -#define PHP_LIBVIRT_NWFILTER_RES_NAME "Libvirt nwfilter" - PHP_MINIT_FUNCTION(libvirt); PHP_MSHUTDOWN_FUNCTION(libvirt); PHP_RINIT_FUNCTION(libvirt); @@ -228,20 +214,6 @@ PHP_MINFO_FUNCTION(libvirt); /* Common functions */ PHP_FUNCTION(libvirt_get_last_error); -/* NWFilter functions */ -PHP_FUNCTION(libvirt_nwfilter_define_xml); -PHP_FUNCTION(libvirt_nwfilter_undefine); -PHP_FUNCTION(libvirt_nwfilter_get_xml_desc); -PHP_FUNCTION(libvirt_nwfilter_get_name); -PHP_FUNCTION(libvirt_nwfilter_get_uuid_string); -PHP_FUNCTION(libvirt_nwfilter_get_uuid); -PHP_FUNCTION(libvirt_nwfilter_lookup_by_name); -PHP_FUNCTION(libvirt_nwfilter_lookup_by_uuid_string); -PHP_FUNCTION(libvirt_nwfilter_lookup_by_uuid); -/* Listing functions */ -PHP_FUNCTION(libvirt_list_all_nwfilters); -PHP_FUNCTION(libvirt_list_nwfilters); -/* Common functions */ PHP_FUNCTION(libvirt_version); PHP_FUNCTION(libvirt_check_version); PHP_FUNCTION(libvirt_has_feature); -- 2.13.3 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list