[libvirt-php PATCH 7/7] implement NWFilter API bindings.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



adds the following functions:

* libvirt_nwfilter_define_xml
* libvirt_nwfilter_undefine
* libvirt_nwfilter_get_xml_desc
* libvirt_nwfilter_get_name
* libvirt_nwfilter_get_uuid
* libvirt_nwfilter_get_uuid_string
* libvirt_nwfilter_lookup_by_name
* libvirt_nwfilter_lookup_by_uuid
* libvirt_nwfilter_lookup_by_uuid_string
* libvirt_list_nwfilters
* libvirt_list_all_nwfilters
---
 src/libvirt-php.c | 390 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/libvirt-php.h |  12 ++
 2 files changed, 402 insertions(+)

diff --git a/src/libvirt-php.c b/src/libvirt-php.c
index 535d321..5893742 100644
--- a/src/libvirt-php.c
+++ b/src/libvirt-php.c
@@ -645,6 +645,16 @@ static zend_function_entry libvirt_functions[] = {
     PHP_FE(libvirt_nodedev_capabilities,         arginfo_libvirt_conn)
     PHP_FE(libvirt_nodedev_get_xml_desc,         arginfo_libvirt_conn_xpath)
     PHP_FE(libvirt_nodedev_get_information,      arginfo_libvirt_conn)
+    /* 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_domains,                 arginfo_libvirt_conn)
     PHP_FE(libvirt_list_domain_snapshots,        arginfo_libvirt_conn_optflags)
@@ -659,6 +669,8 @@ static zend_function_entry libvirt_functions[] = {
     PHP_FE(libvirt_list_active_domains,          arginfo_libvirt_conn)
     PHP_FE(libvirt_list_active_domain_ids,       arginfo_libvirt_conn)
     PHP_FE(libvirt_list_inactive_domains,        arginfo_libvirt_conn)
+    PHP_FE(libvirt_list_all_nwfilters,           arginfo_libvirt_conn)
+    PHP_FE(libvirt_list_nwfilters,               arginfo_libvirt_conn)
     /* Version information and common function */
     PHP_FE(libvirt_version,                      arginfo_libvirt_opttype)
     PHP_FE(libvirt_check_version,                arginfo_libvirt_check_version)
@@ -9074,7 +9086,93 @@ PHP_FUNCTION(libvirt_list_nodedevs)
     efree(names);
 }
 
+
+/*
+ * 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;
+}
 /* Nodedev functions */
+
 /*
  * Function name:   libvirt_nodedev_get
  * Since version:   0.4.1(-1)
@@ -9782,6 +9880,298 @@ PHP_FUNCTION(libvirt_network_set_autostart)
     RETURN_TRUE;
 }
 
+/* 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 2e7a3ca..7962e33 100644
--- a/src/libvirt-php.h
+++ b/src/libvirt-php.h
@@ -558,10 +558,22 @@ PHP_FUNCTION(libvirt_nodedev_get);
 PHP_FUNCTION(libvirt_nodedev_capabilities);
 PHP_FUNCTION(libvirt_nodedev_get_xml_desc);
 PHP_FUNCTION(libvirt_nodedev_get_information);
+/* 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_nodedevs);
 PHP_FUNCTION(libvirt_list_all_networks);
 PHP_FUNCTION(libvirt_list_networks);
+PHP_FUNCTION(libvirt_list_all_nwfilters);
+PHP_FUNCTION(libvirt_list_nwfilters);
 PHP_FUNCTION(libvirt_list_domains);
 PHP_FUNCTION(libvirt_list_domain_snapshots);
 PHP_FUNCTION(libvirt_list_domain_resources);
-- 
2.13.0

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]
  Powered by Linux