From nobody Fri May 16 04:01:47 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 150162555604588.42396533889541; Tue, 1 Aug 2017 15:12:36 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 762EE101731; Tue, 1 Aug 2017 22:12:33 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4C73118174; Tue, 1 Aug 2017 22:12:33 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id DA74C1800C82; Tue, 1 Aug 2017 22:12:32 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v71LkVB4007886 for ; Tue, 1 Aug 2017 17:46:31 -0400 Received: by smtp.corp.redhat.com (Postfix) id 1997560476; Tue, 1 Aug 2017 21:46:31 +0000 (UTC) Received: from mx1.redhat.com (ext-mx07.extmail.prod.ext.phx2.redhat.com [10.5.110.31]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1140160462 for ; Tue, 1 Aug 2017 21:46:31 +0000 (UTC) Received: from mail-qk0-f193.google.com (mail-qk0-f193.google.com [209.85.220.193]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 60823C08C533 for ; Tue, 1 Aug 2017 21:46:23 +0000 (UTC) Received: by mail-qk0-f193.google.com with SMTP id d145so2929239qkc.0 for ; Tue, 01 Aug 2017 14:46:23 -0700 (PDT) Received: from dawid-fedora.datto.lan ([47.19.105.250]) by smtp.gmail.com with ESMTPSA id f56sm1892090qta.79.2017.08.01.14.46.21 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 01 Aug 2017 14:46:21 -0700 (PDT) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 762EE101731 Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 762EE101731 Authentication-Results: mx1.redhat.com; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="GZAWXn5p" DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 60823C08C533 Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=dzrudy@gmail.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 60823C08C533 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=vbD9T46peKuVuWVXgU4L562EvkcM3pBZQg2XGEQt4sI=; b=GZAWXn5pD8xJGgtBHGtXS9+H1HHc4tq8qQ+X/pWhXIzkHUmLXbnGzTUu5Vpr57P8zV oChzuxyDzmzG+ARdEVOjAtUWJGAv+kaYiEQtwP4Tn6yZclABmQxTpDirCzQP3JDCpZIQ G9K5OtDhsNQe3m42r+SYrLmbjlf7+HB0KSuyVLMVW/2YZWZMMNPxV2BzshFKeG9LxpqM Q0bR8pp59QecXm7F9mS+y9xM7qt+Qq95qQsAKdyNO2rASzxZ338rqmkkbe/xiX8VdHYz Tmjp8N7xKE4XfxQm8XCahbw8g8N3q5nRcVV6OTwfojC3CC7SHv9w28XqBDSA2AqXGFdc dVsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=vbD9T46peKuVuWVXgU4L562EvkcM3pBZQg2XGEQt4sI=; b=WrpW38Dj2siOAasarZU1ZIoFWTW8KX3oDJZx5QxhqSYG/b/JB4G07pywEo/qUypjhU xGx8wCubaDty75w+FXX2ncmyezESjtIh/XQdJ4x4t6I3N+XFDedQeFAOo+Sx6LH2kaTK 2ygvKlAHOlm0wNG05lOZlulcGcQxigOdaLTej0DaQhFu/4xw6LZjyIyqeGH0KL6pOdvn VTZygP9lD6IYlMhLCdWC4Roy5/oIIHNe7PslLyufgaF6gBZB47Xa2CJl4eMheur17jdX IcouFVflDs9oPeroRiGD2IsOsnIprkM7MpnLK1ykdUDquTn1+LAqvnl9pWtelzhcPIyi BGxA== X-Gm-Message-State: AIVw112jjSjJ0Kl2hJvtJbS3949YYFkK9pJ4OsIfLG740XIL+eOXjLO3 vvMBqwZ2EbaDKT3+4pg= X-Received: by 10.55.80.70 with SMTP id e67mr26873368qkb.40.1501623981936; Tue, 01 Aug 2017 14:46:21 -0700 (PDT) From: Dawid Zamirski To: libvir-list@redhat.com Date: Tue, 1 Aug 2017 17:46:09 -0400 Message-Id: <20170801214614.8915-9-dzrudy@gmail.com> In-Reply-To: <20170801214614.8915-1-dzrudy@gmail.com> References: <20170801214614.8915-1-dzrudy@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 01 Aug 2017 21:46:23 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 01 Aug 2017 21:46:23 +0000 (UTC) for IP:'209.85.220.193' DOMAIN:'mail-qk0-f193.google.com' HELO:'mail-qk0-f193.google.com' FROM:'dzrudy@gmail.com' RCPT:'' X-RedHat-Spam-Score: 0.37 (DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SORBS_SPAM, SPF_PASS) 209.85.220.193 mail-qk0-f193.google.com 209.85.220.193 mail-qk0-f193.google.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.31 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: libvir-list@redhat.com Subject: [libvirt] [libvirt-php PATCH 08/13] Split up the bindings for libvirt network API X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Tue, 01 Aug 2017 22:12:34 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" --- src/Makefile.am | 3 +- src/libvirt-network.c | 586 ++++++++++++++++++++++++++++++++++++++++++++++= ++ src/libvirt-network.h | 73 ++++++ src/libvirt-php.c | 610 +---------------------------------------------= ---- src/libvirt-php.h | 24 +- 5 files changed, 664 insertions(+), 632 deletions(-) create mode 100644 src/libvirt-network.c create mode 100644 src/libvirt-network.h diff --git a/src/Makefile.am b/src/Makefile.am index b8eae3a..4ae01db 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -25,7 +25,8 @@ libvirt_php_la_SOURCES =3D \ libvirt-connection.c libvirt-connection.h \ libvirt-node.c libvirt-node.h \ libvirt-stream.c libvirt-stream.h \ - libvirt-domain.c libvirt-domain.h + libvirt-domain.c libvirt-domain.h \ + libvirt-network.c libvirt-network.h=20 libvirt_php_la_CFLAGS =3D \ $(AM_CFLAGS) \ -DCOMPILE_DL_LIBVIRT=3D1 diff --git a/src/libvirt-network.c b/src/libvirt-network.c new file mode 100644 index 0000000..4316ee2 --- /dev/null +++ b/src/libvirt-network.c @@ -0,0 +1,586 @@ +/* + * libvirt-network.c: The PHP bindings to libvirt network API + * + * See COPYING for the license of this software + */ + +#include + +#include "libvirt-network.h" + +DEBUG_INIT("network"); + +void +php_libvirt_network_dtor(virt_resource *rsrc TSRMLS_DC) +{ + php_libvirt_network *network =3D (php_libvirt_network *)rsrc->ptr; + int rv =3D 0; + + if (network !=3D NULL) { + if (network->network !=3D NULL) { + if (!check_resource_allocation(network->conn->conn, INT_RESOUR= CE_NETWORK, network->network TSRMLS_CC)) { + network->network =3D NULL; + efree(network); + return; + } + rv =3D virNetworkFree(network->network); + if (rv !=3D 0) { + DPRINTF("%s: virNetworkFree(%p) returned %d (%s)\n", __FUN= CTION__, network->network, rv, LIBVIRT_G(last_error)); + php_error_docref(NULL TSRMLS_CC, E_WARNING, "virStorageVol= Free failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); + } else { + DPRINTF("%s: virNetworkFree(%p) completed successfully\n",= __FUNCTION__, network->network); + resource_change_counter(INT_RESOURCE_NETWORK, NULL, networ= k->network, 0 TSRMLS_CC); + } + network->network =3D NULL; + } + efree(network); + } +} + +/* + * Function name: libvirt_network_define_xml + * Since version: 0.4.2 + * Description: Function is used to define a new virtual network based= on the XML description + * Arguments: @res [resource]: libvirt connection resource + * @xml [string]: XML string definition of network to be = defined + * Returns: libvirt network resource of newly defined network + */ +PHP_FUNCTION(libvirt_network_define_xml) +{ + php_libvirt_connection *conn =3D NULL; + php_libvirt_network *res_net =3D NULL; + virNetwork *net; + zval *zconn; + char *xml =3D NULL; + strsize_t xml_len; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &xml, &xml_len); + + if ((net =3D virNetworkDefineXML(conn->conn, xml)) =3D=3D NULL) { + set_error_if_unset("Cannot define a new network" TSRMLS_CC); + RETURN_FALSE; + } + + res_net =3D (php_libvirt_network *)emalloc(sizeof(php_libvirt_network)= ); + res_net->network =3D net; + res_net->conn =3D conn; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_net->network); + resource_change_counter(INT_RESOURCE_NETWORK, conn->conn, res_net->net= work, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_net, le_libvirt_network); +} + +/* + * Function name: libvirt_network_get_xml_desc + * Since version: 0.4.1(-1) + * Description: Function is used to get the XML description for the ne= twork + * Arguments: @res [resource]: libvirt network resource + * @xpath [string]: optional xPath expression string to g= et just this entry, can be NULL + * Returns: network XML string or result of xPath expression + */ +PHP_FUNCTION(libvirt_network_get_xml_desc) +{ + php_libvirt_network *network; + zval *znetwork; + char *xml =3D NULL; + char *xpath =3D NULL; + char *tmp; + strsize_t xpath_len; + int retval =3D -1; + + GET_NETWORK_FROM_ARGS("r|s", &znetwork, &xpath, &xpath_len); + if (xpath_len < 1) + xpath =3D NULL; + + xml =3D virNetworkGetXMLDesc(network->network, 0); + + if (xml =3D=3D NULL) { + set_error_if_unset("Cannot get network XML" TSRMLS_CC); + RETURN_FALSE; + } + + tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); + if ((tmp =3D=3D NULL) || (retval < 0)) { + VIRT_RETVAL_STRING(xml); + } else { + VIRT_RETVAL_STRING(tmp); + } + + free(xml); + free(tmp); +} + +/* + * Function name: libvirt_network_undefine + * Since version: 0.4.2 + * Description: Function is used to undefine already defined network + * Arguments: @res [resource]: libvirt network resource + * Returns: TRUE for success, FALSE on error + */ +PHP_FUNCTION(libvirt_network_undefine) +{ + php_libvirt_network *network =3D NULL; + zval *znetwork; + + GET_NETWORK_FROM_ARGS("r", &znetwork); + + if (virNetworkUndefine(network->network) !=3D 0) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_network_get + * Since version: 0.4.1(-1) + * Description: Function is used to get the network resource from name + * Arguments: @res [resource]: libvirt connection resource + * @name [string]: network name string + * Returns: libvirt network resource + */ +PHP_FUNCTION(libvirt_network_get) +{ + php_libvirt_connection *conn =3D NULL; + php_libvirt_network *res_net =3D NULL; + virNetwork *net; + zval *zconn; + char *name; + strsize_t name_len; + + GET_CONNECTION_FROM_ARGS("rs", &zconn, &name, &name_len); + + if ((net =3D virNetworkLookupByName(conn->conn, name)) =3D=3D NULL) { + set_error_if_unset("Cannot get find requested network" TSRMLS_CC); + RETURN_FALSE; + } + + res_net =3D (php_libvirt_network *)emalloc(sizeof(php_libvirt_network)= ); + res_net->network =3D net; + res_net->conn =3D conn; + + DPRINTF("%s: returning %p\n", PHPFUNC, res_net->network); + resource_change_counter(INT_RESOURCE_NETWORK, conn->conn, res_net->net= work, 1 TSRMLS_CC); + + VIRT_REGISTER_RESOURCE(res_net, le_libvirt_network); +} + +/* + * Function name: libvirt_network_get_bridge + * Since version: 0.4.1(-1) + * Description: Function is used to get the bridge associated with the= network + * Arguments: @res [resource]: libvirt network resource + * Returns: bridge name string + */ +PHP_FUNCTION(libvirt_network_get_bridge) +{ + php_libvirt_network *network; + zval *znetwork; + char *name; + + GET_NETWORK_FROM_ARGS("r", &znetwork); + + name =3D virNetworkGetBridgeName(network->network); + + if (name =3D=3D NULL) { + set_error_if_unset("Cannot get network bridge name" TSRMLS_CC); + RETURN_FALSE; + } + + VIRT_RETVAL_STRING(name); + free(name); +} + +/* + * Function name: libvirt_network_get_active + * Since version: 0.4.1(-1) + * Description: Function is used to get the activity state of the netw= ork + * Arguments: @res [resource]: libvirt network resource + * Returns: 1 when active, 0 when inactive, FALSE on error + */ +PHP_FUNCTION(libvirt_network_get_active) +{ + php_libvirt_network *network; + zval *znetwork; + int res; + + GET_NETWORK_FROM_ARGS("r", &znetwork); + + res =3D virNetworkIsActive(network->network); + + if (res =3D=3D -1) { + set_error_if_unset("Error getting virtual network state" TSRMLS_CC= ); + RETURN_FALSE; + } + + RETURN_LONG(res); +} + +/* + * Function name: libvirt_network_set_active + * Since version: 0.4.1(-1) + * Description: Function is used to set the activity state of the netw= ork + * Arguments: @res [resource]: libvirt network resource + * @flags [int]: active + * Returns: TRUE if success, FALSE on error + */ +PHP_FUNCTION(libvirt_network_set_active) +{ + php_libvirt_network *network; + zval *znetwork; + zend_long act =3D 0; + + DPRINTF("%s: Setting network activity...\n", PHPFUNC); + + GET_NETWORK_FROM_ARGS("rl", &znetwork, &act); + + if ((act !=3D 0) && (act !=3D 1)) { + set_error("Invalid network activity state" TSRMLS_CC); + RETURN_FALSE; + } + + DPRINTF("%s: %sabling network...\n", PHPFUNC, (act =3D=3D 1) ? "En" : = "Dis"); + + if (act =3D=3D 1) { + if (virNetworkCreate(network->network) =3D=3D 0) { + // Network is up and running + RETURN_TRUE; + } else { + // We don't have to set error since it's caught by libvirt err= or handler itself + RETURN_FALSE; + } + } + + if (virNetworkDestroy(network->network) =3D=3D 0) { + // Network is down + RETURN_TRUE; + } else { + // Caught by libvirt error handler too + RETURN_FALSE; + } +} + +/* + * Function name: libvirt_network_get_information + * Since version: 0.4.1(-1) + * Description: Function is used to get the network information + * Arguments: @res [resource]: libvirt network resource + * Returns: network information array + */ +PHP_FUNCTION(libvirt_network_get_information) +{ + php_libvirt_network *network =3D NULL; + zval *znetwork; + int retval =3D 0; + char *xml =3D NULL; + char *name =3D NULL; + char *ipaddr =3D NULL; + char *netmask =3D NULL; + char *mode =3D NULL; + char *dev =3D NULL; + char *dhcp_start =3D NULL; + char *dhcp_end =3D NULL; + char fixedtemp[32] =3D { 0 }; + + GET_NETWORK_FROM_ARGS("r", &znetwork); + + xml =3D virNetworkGetXMLDesc(network->network, 0); + + if (xml =3D=3D NULL) { + set_error_if_unset("Cannot get network XML" TSRMLS_CC); + RETURN_FALSE; + } + + array_init(return_value); + + /* Get name */ + name =3D get_string_from_xpath(xml, "//network/name", NULL, &retval); + if (name =3D=3D NULL) { + set_error("Invalid XPath node for network name" TSRMLS_CC); + RETURN_FALSE; + } + + if (retval < 0) { + set_error("Cannot get XPath expression result for network name" TS= RMLS_CC); + RETURN_FALSE; + } + + VIRT_ADD_ASSOC_STRING(return_value, "name", name); + + /* Get gateway IP address */ + ipaddr =3D get_string_from_xpath(xml, "//network/ip/@address", NULL, &= retval); + if (ipaddr && retval > 0) + VIRT_ADD_ASSOC_STRING(return_value, "ip", ipaddr); + + /* Get netmask */ + netmask =3D get_string_from_xpath(xml, "//network/ip/@netmask", NULL, = &retval); + if (netmask && retval > 0) { + int subnet_bits =3D get_subnet_bits(netmask); + VIRT_ADD_ASSOC_STRING(return_value, "netmask", netmask); + add_assoc_long(return_value, "netmask_bits", (long) subnet_bits); + + /* Format CIDR address representation */ + ipaddr[strlen(ipaddr) - 1] =3D ipaddr[strlen(ipaddr) - 1] - 1; + snprintf(fixedtemp, sizeof(fixedtemp), "%s/%d", ipaddr, subnet_bit= s); + VIRT_ADD_ASSOC_STRING(return_value, "ip_range", fixedtemp); + } + + /* Get forwarding settings */ + mode =3D get_string_from_xpath(xml, "//network/forward/@mode", NULL, &= retval); + if (mode && retval > 0) + VIRT_ADD_ASSOC_STRING(return_value, "forwarding", mode); + + /* Get forwarding settings */ + dev =3D get_string_from_xpath(xml, "//network/forward/@dev", NULL, &re= tval); + if (dev && retval > 0) + VIRT_ADD_ASSOC_STRING(return_value, "forward_dev", dev); + + /* Get DHCP values */ + dhcp_start =3D get_string_from_xpath(xml, "//network/ip/dhcp/range/@st= art", NULL, &retval); + dhcp_end =3D get_string_from_xpath(xml, "//network/ip/dhcp/range/@end"= , NULL, &retval); + if (dhcp_start && dhcp_end && retval > 0) { + VIRT_ADD_ASSOC_STRING(return_value, "dhcp_start", dhcp_start); + VIRT_ADD_ASSOC_STRING(return_value, "dhcp_end", dhcp_end); + } + + free(dhcp_end); + free(dhcp_start); + free(dev); + free(mode); + free(netmask); + free(ipaddr); + free(name); + free(xml); +} + +/* + * Function name: libvirt_network_get_uuid_string + * Since version: 0.5.3 + * Description: Function is used to get network's UUID in string format + * Arguments: @res [resource]: libvirt network resource + * Returns: network UUID string or FALSE on failure + */ +PHP_FUNCTION(libvirt_network_get_uuid_string) +{ + php_libvirt_network *network =3D NULL; + zval *znetwork; + char *uuid =3D NULL; + int ret =3D -1; + + GET_NETWORK_FROM_ARGS("r", &znetwork); + + uuid =3D (char *) emalloc(VIR_UUID_STRING_BUFLEN); + ret =3D virNetworkGetUUIDString(network->network, uuid); + + DPRINTF("%s: virNetworkGetUUIDString(%p) returned %d (%s)\n", PHPFUNC, + network->network, ret, uuid); + + if (ret !=3D 0) + RETURN_FALSE; + + VIRT_RETURN_STRING(uuid); + efree(uuid); +} + +/* + * Function name: libvirt_network_get_uuid + * Since version: 0.5.3 + * Descirption: Function is used to get network's UUID in binary format + * Arguments: @res [resource]: libvirt netowrk resource + * Returns: network UUID in binary format or FALSE on failure + */ +PHP_FUNCTION(libvirt_network_get_uuid) +{ + php_libvirt_network *network =3D NULL; + zval *znetwork; + char *uuid =3D NULL; + int ret =3D -1; + + GET_NETWORK_FROM_ARGS("r", &znetwork); + + uuid =3D (char *) emalloc(VIR_UUID_BUFLEN); + ret =3D virNetworkGetUUID(network->network, (unsigned char *)uuid); + + DPRINTF("%s: virNetworkGetUUID(%p, %p) returned %d\n", PHPFUNC, + network->network, uuid, ret); + + if (ret !=3D 0) + RETURN_FALSE; + + VIRT_RETVAL_STRING(uuid); + efree(uuid); +} + +/* + * Function name: libvirt_network_get_name + * Since version: 0.5.3 + * Description: Function is used to get network's name + * Arguments: @res [resource]: libvirt network resource + * Returns: network name string or FALSE on failure + */ +PHP_FUNCTION(libvirt_network_get_name) +{ + php_libvirt_network *network =3D NULL; + zval *znetwork; + const char *name =3D NULL; + + GET_NETWORK_FROM_ARGS("r", &znetwork); + name =3D virNetworkGetName(network->network); + + DPRINTF("%s: virNetworkGetName(%p) returned %s\n", PHPFUNC, + network->network, name); + + if (name =3D=3D NULL) + RETURN_FALSE; + + /* name should not be freed as its lifetime is the same as network res= ource */ + VIRT_RETURN_STRING(name); +} + +/* + * Function name: libvirt_network_get_autostart + * Since version: 0.5.4 + * Description: Function is getting the autostart value for the network + * Arguments: @res [resource]: libvirt network resource + * Returns: autostart value or -1 on error + */ +PHP_FUNCTION(libvirt_network_get_autostart) +{ + php_libvirt_network *network =3D NULL; + zval *znetwork; + int autostart; + + GET_NETWORK_FROM_ARGS("r", &znetwork); + + if (virNetworkGetAutostart(network->network, &autostart) !=3D 0) + RETURN_LONG(-1); + + RETURN_LONG((long) autostart); +} + +/* + * Function name: libvirt_network_set_autostart + * Since version: 0.5.4 + * Description: Function is setting the autostart value for the network + * Arguments: @res [resource]: libvirt network resource + * @flags [int]: flag to enable/disable autostart + * Returns: TRUE on success, FALSE on error + */ +PHP_FUNCTION(libvirt_network_set_autostart) +{ + php_libvirt_network *network =3D NULL; + zval *znetwork; + zend_long autostart =3D 0; + + GET_NETWORK_FROM_ARGS("rl", &znetwork, &autostart); + + if (virNetworkSetAutostart(network->network, autostart) < 0) + RETURN_FALSE; + + RETURN_TRUE; +} + +/* + * Function name: libvirt_list_all_networks + * Since version: 0.5.3 + * Description: Function is used to list networks on the connection + * Arguments: @res [resource]: libvirt connection resource + * @flags [int]: optional flags to filter the results for= a smaller list of targetted networks (bitwise-OR VIR_CONNECT_LIST_NETWORKS= _* constants) + * Returns: libvirt network resources array for the connection + */ +PHP_FUNCTION(libvirt_list_all_networks) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + zend_long flags =3D VIR_CONNECT_LIST_NETWORKS_ACTIVE | + VIR_CONNECT_LIST_NETWORKS_INACTIVE; + int count =3D -1; + size_t i =3D 0; + virNetworkPtr *nets =3D NULL; + virNetworkPtr network =3D NULL; + php_libvirt_network *res_network; + + GET_CONNECTION_FROM_ARGS("r|l", &zconn, &flags); + + if ((count =3D virConnectListAllNetworks(conn->conn, &nets, flags)) < = 0) + RETURN_FALSE; + + DPRINTF("%s: Found %d networks\n", PHPFUNC, count); + + array_init(return_value); + + for (i =3D 0; i < count; i++) { + network =3D nets[i]; + res_network =3D (php_libvirt_network *) emalloc(sizeof(php_libvirt= _network)); + res_network->network =3D network; + res_network->conn =3D conn; + + VIRT_REGISTER_LIST_RESOURCE(network); + resource_change_counter(INT_RESOURCE_NETWORK, conn->conn, + res_network->network, 1 TSRMLS_CC); + } +} + +/* + * Function name: libvirt_list_networks + * Since version: 0.4.1(-1) + * Description: Function is used to list networks on the connection + * Arguments: @res [resource]: libvirt connection resource + * @flags [int]: flags whether to list active, inactive o= r all networks (VIR_NETWORKS_{ACTIVE|INACTIVE|ALL} constants) + * Returns: libvirt network names array for the connection + */ +PHP_FUNCTION(libvirt_list_networks) +{ + php_libvirt_connection *conn =3D NULL; + zval *zconn; + zend_long flags =3D VIR_NETWORKS_ACTIVE | VIR_NETWORKS_INACTIVE; + int count =3D -1; + int expectedcount =3D -1; + char **names; + int i, done =3D 0; + + GET_CONNECTION_FROM_ARGS("r|l", &zconn, &flags); + + array_init(return_value); + if (flags & VIR_NETWORKS_ACTIVE) { + if ((expectedcount =3D virConnectNumOfNetworks(conn->conn)) < 0) + RETURN_FALSE; + + names =3D (char **)emalloc(expectedcount*sizeof(char *)); + count =3D virConnectListNetworks(conn->conn, names, expectedcount); + if ((count !=3D expectedcount) || (count < 0)) { + efree(names); + RETURN_FALSE; + } + + for (i =3D 0; i < count; i++) { + VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); + free(names[i]); + } + + efree(names); + done++; + } + + if (flags & VIR_NETWORKS_INACTIVE) { + if ((expectedcount =3D virConnectNumOfDefinedNetworks(conn->conn))= < 0) + RETURN_FALSE; + names =3D (char **)emalloc(expectedcount*sizeof(char *)); + count =3D virConnectListDefinedNetworks(conn->conn, names, expecte= dcount); + if ((count !=3D expectedcount) || (count < 0)) { + efree(names); + RETURN_FALSE; + } + + for (i =3D 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-network.h b/src/libvirt-network.h new file mode 100644 index 0000000..604b803 --- /dev/null +++ b/src/libvirt-network.h @@ -0,0 +1,73 @@ +/* + * libvirt-network.h: The PHP bindings to libvirt network API + * + * See COPYING for the license of this software + */ + +#ifndef __LIBVIRT_NETWORK_H__ +# define __LIBVIRT_NETWORK_H__ + +# include "libvirt-connection.h" + +# define PHP_LIBVIRT_NETWORK_RES_NAME "Libvirt virtual network" +# define INT_RESOURCE_NETWORK 0x04 + +# define GET_NETWORK_FROM_ARGS(args, ...) = \ + do { = \ + reset_error(TSRMLS_C); = \ + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, = \ + args, = \ + __VA_ARGS__) =3D=3D FAILURE) { = \ + set_error("Invalid arguments" TSRMLS_CC); = \ + RETURN_FALSE; = \ + } = \ + = \ + VIRT_FETCH_RESOURCE(network, php_libvirt_network*, &znetwork, = \ + PHP_LIBVIRT_NETWORK_RES_NAME, le_libvirt_netwo= rk); \ + if (network =3D=3D NULL || network->network =3D=3D NULL) = \ + RETURN_FALSE; = \ + } while (0) = \ + +# define PHP_FE_LIBVIRT_NETWORK = \ + PHP_FE(libvirt_network_define_xml, arginfo_libvirt_conn_xml) = \ + PHP_FE(libvirt_network_get_xml_desc, arginfo_libvirt_conn_xpath) = \ + PHP_FE(libvirt_network_undefine, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_network_get, arginfo_libvirt_conn_name) = \ + PHP_FE(libvirt_network_get_active, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_network_set_active, arginfo_libvirt_conn_flags) = \ + PHP_FE(libvirt_network_get_bridge, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_network_get_information, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_network_get_uuid_string, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_network_get_uuid, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_network_get_name, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_network_get_autostart, arginfo_libvirt_conn) = \ + PHP_FE(libvirt_network_set_autostart, arginfo_libvirt_conn_flags) = \ + PHP_FE(libvirt_list_all_networks, arginfo_libvirt_conn_optflags)= \ + PHP_FE(libvirt_list_networks, arginfo_libvirt_conn_optflags) + +int le_libvirt_network; + +typedef struct _php_libvirt_network { + virNetworkPtr network; + php_libvirt_connection* conn; +} php_libvirt_network; + +void php_libvirt_network_dtor(virt_resource *rsrc TSRMLS_DC); + +PHP_FUNCTION(libvirt_network_define_xml); +PHP_FUNCTION(libvirt_network_get_xml_desc); +PHP_FUNCTION(libvirt_network_undefine); +PHP_FUNCTION(libvirt_network_get); +PHP_FUNCTION(libvirt_network_get_active); +PHP_FUNCTION(libvirt_network_set_active); +PHP_FUNCTION(libvirt_network_get_bridge); +PHP_FUNCTION(libvirt_network_get_information); +PHP_FUNCTION(libvirt_network_get_uuid_string); +PHP_FUNCTION(libvirt_network_get_uuid); +PHP_FUNCTION(libvirt_network_get_name); +PHP_FUNCTION(libvirt_network_get_autostart); +PHP_FUNCTION(libvirt_network_set_autostart); +PHP_FUNCTION(libvirt_list_all_networks); +PHP_FUNCTION(libvirt_list_networks); + +#endif diff --git a/src/libvirt-php.c b/src/libvirt-php.c index 05f36ae..9e43a71 100644 --- a/src/libvirt-php.c +++ b/src/libvirt-php.c @@ -24,6 +24,7 @@ #include "libvirt-node.h" #include "libvirt-stream.h" #include "libvirt-domain.h" +#include "libvirt-network.h" =20 DEBUG_INIT("core"); =20 @@ -39,7 +40,6 @@ const char *features_binaries[] =3D { NULL }; /* ZEND thread safe per request globals definition */ int le_libvirt_storagepool; int le_libvirt_volume; -int le_libvirt_network; int le_libvirt_nodedev; int le_libvirt_snapshot; int le_libvirt_nwfilter; @@ -517,20 +517,7 @@ static zend_function_entry libvirt_functions[] =3D { PHP_FE(libvirt_storagepool_build, arginfo_libvirt_conn) PHP_FE(libvirt_storagepool_delete, arginfo_libvirt_conn) /* Network functions */ - PHP_FE(libvirt_network_define_xml, arginfo_libvirt_conn_xml) - PHP_FE(libvirt_network_undefine, arginfo_libvirt_conn) - PHP_FE(libvirt_network_get, arginfo_libvirt_conn_name) - PHP_FE(libvirt_network_get_xml_desc, arginfo_libvirt_conn_xpat= h) - PHP_FE(libvirt_network_get_bridge, arginfo_libvirt_conn) - PHP_FE(libvirt_network_get_information, arginfo_libvirt_conn) - PHP_FE(libvirt_network_get_active, arginfo_libvirt_conn) - PHP_FE(libvirt_network_set_active, arginfo_libvirt_conn_flag= s) - PHP_FE(libvirt_network_get_uuid_string, arginfo_libvirt_conn) - PHP_FE(libvirt_network_get_uuid, arginfo_libvirt_conn) - PHP_FE(libvirt_network_get_name, arginfo_libvirt_conn) - PHP_FE(libvirt_network_get_autostart, arginfo_libvirt_conn) - PHP_FE(libvirt_network_set_autostart, arginfo_libvirt_conn_flag= s) - /* Node functions */ + PHP_FE_LIBVIRT_NETWORK PHP_FE_LIBVIRT_NODE /* Nodedev functions */ PHP_FE(libvirt_nodedev_get, arginfo_libvirt_conn) @@ -550,8 +537,6 @@ static zend_function_entry libvirt_functions[] =3D { /* List functions */ PHP_FE(libvirt_list_domain_snapshots, arginfo_libvirt_conn_optf= lags) PHP_FE(libvirt_list_nodedevs, arginfo_libvirt_conn_optc= ap) - PHP_FE(libvirt_list_all_networks, arginfo_libvirt_conn_optf= lags) - PHP_FE(libvirt_list_networks, arginfo_libvirt_conn_optf= lags) PHP_FE(libvirt_list_storagepools, arginfo_libvirt_conn) PHP_FE(libvirt_list_active_storagepools, arginfo_libvirt_conn) PHP_FE(libvirt_list_inactive_storagepools, arginfo_libvirt_conn) @@ -571,7 +556,6 @@ static zend_function_entry libvirt_functions[] =3D { PHP_FE_END }; =20 - /* Zend module basic definition */ zend_module_entry libvirt_module_entry =3D { #if ZEND_MODULE_API_NO >=3D 20010901 @@ -1293,33 +1277,6 @@ static void php_libvirt_volume_dtor(virt_resource *r= src TSRMLS_DC) } } =20 -/* Destructor for network resource */ -static void php_libvirt_network_dtor(virt_resource *rsrc TSRMLS_DC) -{ - php_libvirt_network *network =3D (php_libvirt_network *)rsrc->ptr; - int rv =3D 0; - - if (network !=3D NULL) { - if (network->network !=3D NULL) { - if (!check_resource_allocation(network->conn->conn, INT_RESOUR= CE_NETWORK, network->network TSRMLS_CC)) { - network->network =3D NULL; - efree(network); - return; - } - rv =3D virNetworkFree(network->network); - if (rv !=3D 0) { - DPRINTF("%s: virNetworkFree(%p) returned %d (%s)\n", __FUN= CTION__, network->network, rv, LIBVIRT_G(last_error)); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "virStorageVol= Free failed with %i on destructor: %s", rv, LIBVIRT_G(last_error)); - } else { - DPRINTF("%s: virNetworkFree(%p) completed successfully\n",= __FUNCTION__, network->network); - resource_change_counter(INT_RESOURCE_NETWORK, NULL, networ= k->network, 0 TSRMLS_CC); - } - network->network =3D NULL; - } - efree(network); - } -} - /* Destructor for nodedev resource */ static void php_libvirt_nodedev_dtor(virt_resource *rsrc TSRMLS_DC) { @@ -1727,19 +1684,6 @@ PHP_MSHUTDOWN_FUNCTION(libvirt) } =20 /* Macros for obtaining resources from arguments */ -#define GET_NETWORK_FROM_ARGS(args, ...) = \ - do { = \ - reset_error(TSRMLS_C); = \ - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, args, __VA_AR= GS__) =3D=3D FAILURE) { \ - set_error("Invalid arguments" TSRMLS_CC); = \ - RETURN_FALSE; = \ - } = \ - = \ - VIRT_FETCH_RESOURCE(network, php_libvirt_network*, &znetwork, PHP_= LIBVIRT_NETWORK_RES_NAME, le_libvirt_network);\ - if ((network =3D=3D NULL) || (network->network =3D=3D NULL)) = \ - RETURN_FALSE; = \ - } while (0) = \ - #define GET_NODEDEV_FROM_ARGS(args, ...) = \ do { = \ reset_error(TSRMLS_C); = \ @@ -4054,111 +3998,6 @@ PHP_FUNCTION(libvirt_list_inactive_storagepools) } =20 /* - * Function name: libvirt_list_all_networks - * Since version: 0.5.3 - * Description: Function is used to list networks on the connection - * Arguments: @res [resource]: libvirt connection resource - * @flags [int]: optional flags to filter the results for= a smaller list of targetted networks (bitwise-OR VIR_CONNECT_LIST_NETWORKS= _* constants) - * Returns: libvirt network resources array for the connection - */ -PHP_FUNCTION(libvirt_list_all_networks) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - zend_long flags =3D VIR_CONNECT_LIST_NETWORKS_ACTIVE | - VIR_CONNECT_LIST_NETWORKS_INACTIVE; - int count =3D -1; - size_t i =3D 0; - virNetworkPtr *nets =3D NULL; - virNetworkPtr network =3D NULL; - php_libvirt_network *res_network; - - GET_CONNECTION_FROM_ARGS("r|l", &zconn, &flags); - - if ((count =3D virConnectListAllNetworks(conn->conn, &nets, flags)) < = 0) - RETURN_FALSE; - - DPRINTF("%s: Found %d networks\n", PHPFUNC, count); - - array_init(return_value); - - for (i =3D 0; i < count; i++) { - network =3D nets[i]; - res_network =3D (php_libvirt_network *) emalloc(sizeof(php_libvirt= _network)); - res_network->network =3D network; - res_network->conn =3D conn; - - VIRT_REGISTER_LIST_RESOURCE(network); - resource_change_counter(INT_RESOURCE_NETWORK, conn->conn, - res_network->network, 1 TSRMLS_CC); - } -} - -/* - * Function name: libvirt_list_networks - * Since version: 0.4.1(-1) - * Description: Function is used to list networks on the connection - * Arguments: @res [resource]: libvirt connection resource - * @flags [int]: flags whether to list active, inactive o= r all networks (VIR_NETWORKS_{ACTIVE|INACTIVE|ALL} constants) - * Returns: libvirt network names array for the connection - */ -PHP_FUNCTION(libvirt_list_networks) -{ - php_libvirt_connection *conn =3D NULL; - zval *zconn; - zend_long flags =3D VIR_NETWORKS_ACTIVE | VIR_NETWORKS_INACTIVE; - int count =3D -1; - int expectedcount =3D -1; - char **names; - int i, done =3D 0; - - GET_CONNECTION_FROM_ARGS("r|l", &zconn, &flags); - - array_init(return_value); - if (flags & VIR_NETWORKS_ACTIVE) { - if ((expectedcount =3D virConnectNumOfNetworks(conn->conn)) < 0) - RETURN_FALSE; - - names =3D (char **)emalloc(expectedcount*sizeof(char *)); - count =3D virConnectListNetworks(conn->conn, names, expectedcount); - if ((count !=3D expectedcount) || (count < 0)) { - efree(names); - RETURN_FALSE; - } - - for (i =3D 0; i < count; i++) { - VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); - free(names[i]); - } - - efree(names); - done++; - } - - if (flags & VIR_NETWORKS_INACTIVE) { - if ((expectedcount =3D virConnectNumOfDefinedNetworks(conn->conn))= < 0) - RETURN_FALSE; - names =3D (char **)emalloc(expectedcount*sizeof(char *)); - count =3D virConnectListDefinedNetworks(conn->conn, names, expecte= dcount); - if ((count !=3D expectedcount) || (count < 0)) { - efree(names); - RETURN_FALSE; - } - - for (i =3D 0; i < count; i++) { - VIRT_ADD_NEXT_INDEX_STRING(return_value, names[i]); - free(names[i]); - } - - efree(names); - done++; - } - - if (!done) - RETURN_FALSE; -} - -/* * Function name: libvirt_list_nodedevs * Since version: 0.4.1(-1) * Description: Function is used to list node devices on the connection @@ -4546,451 +4385,6 @@ PHP_FUNCTION(libvirt_nodedev_get_information) RETURN_FALSE; } =20 -/* Network functions */ - -/* - * Function name: libvirt_network_define_xml - * Since version: 0.4.2 - * Description: Function is used to define a new virtual network based= on the XML description - * Arguments: @res [resource]: libvirt connection resource - * @xml [string]: XML string definition of network to be = defined - * Returns: libvirt network resource of newly defined network - */ -PHP_FUNCTION(libvirt_network_define_xml) -{ - php_libvirt_connection *conn =3D NULL; - php_libvirt_network *res_net =3D NULL; - virNetwork *net; - zval *zconn; - char *xml =3D NULL; - strsize_t xml_len; - - GET_CONNECTION_FROM_ARGS("rs", &zconn, &xml, &xml_len); - - if ((net =3D virNetworkDefineXML(conn->conn, xml)) =3D=3D NULL) { - set_error_if_unset("Cannot define a new network" TSRMLS_CC); - RETURN_FALSE; - } - - res_net =3D (php_libvirt_network *)emalloc(sizeof(php_libvirt_network)= ); - res_net->network =3D net; - res_net->conn =3D conn; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_net->network); - resource_change_counter(INT_RESOURCE_NETWORK, conn->conn, res_net->net= work, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_net, le_libvirt_network); -} - -/* - * Function name: libvirt_network_undefine - * Since version: 0.4.2 - * Description: Function is used to undefine already defined network - * Arguments: @res [resource]: libvirt network resource - * Returns: TRUE for success, FALSE on error - */ -PHP_FUNCTION(libvirt_network_undefine) -{ - php_libvirt_network *network =3D NULL; - zval *znetwork; - - GET_NETWORK_FROM_ARGS("r", &znetwork); - - if (virNetworkUndefine(network->network) !=3D 0) - RETURN_FALSE; - - RETURN_TRUE; -} - -/* - * Function name: libvirt_network_get - * Since version: 0.4.1(-1) - * Description: Function is used to get the network resource from name - * Arguments: @res [resource]: libvirt connection resource - * @name [string]: network name string - * Returns: libvirt network resource - */ -PHP_FUNCTION(libvirt_network_get) -{ - php_libvirt_connection *conn =3D NULL; - php_libvirt_network *res_net =3D NULL; - virNetwork *net; - zval *zconn; - char *name; - strsize_t name_len; - - GET_CONNECTION_FROM_ARGS("rs", &zconn, &name, &name_len); - - if ((net =3D virNetworkLookupByName(conn->conn, name)) =3D=3D NULL) { - set_error_if_unset("Cannot get find requested network" TSRMLS_CC); - RETURN_FALSE; - } - - res_net =3D (php_libvirt_network *)emalloc(sizeof(php_libvirt_network)= ); - res_net->network =3D net; - res_net->conn =3D conn; - - DPRINTF("%s: returning %p\n", PHPFUNC, res_net->network); - resource_change_counter(INT_RESOURCE_NETWORK, conn->conn, res_net->net= work, 1 TSRMLS_CC); - - VIRT_REGISTER_RESOURCE(res_net, le_libvirt_network); -} - -/* - * Function name: libvirt_network_get_bridge - * Since version: 0.4.1(-1) - * Description: Function is used to get the bridge associated with the= network - * Arguments: @res [resource]: libvirt network resource - * Returns: bridge name string - */ -PHP_FUNCTION(libvirt_network_get_bridge) -{ - php_libvirt_network *network; - zval *znetwork; - char *name; - - GET_NETWORK_FROM_ARGS("r", &znetwork); - - name =3D virNetworkGetBridgeName(network->network); - - if (name =3D=3D NULL) { - set_error_if_unset("Cannot get network bridge name" TSRMLS_CC); - RETURN_FALSE; - } - - VIRT_RETVAL_STRING(name); - free(name); -} - -/* - * Function name: libvirt_network_get_active - * Since version: 0.4.1(-1) - * Description: Function is used to get the activity state of the netw= ork - * Arguments: @res [resource]: libvirt network resource - * Returns: 1 when active, 0 when inactive, FALSE on error - */ -PHP_FUNCTION(libvirt_network_get_active) -{ - php_libvirt_network *network; - zval *znetwork; - int res; - - GET_NETWORK_FROM_ARGS("r", &znetwork); - - res =3D virNetworkIsActive(network->network); - - if (res =3D=3D -1) { - set_error_if_unset("Error getting virtual network state" TSRMLS_CC= ); - RETURN_FALSE; - } - - RETURN_LONG(res); -} - -/* - * Function name: libvirt_network_get_information - * Since version: 0.4.1(-1) - * Description: Function is used to get the network information - * Arguments: @res [resource]: libvirt network resource - * Returns: network information array - */ -PHP_FUNCTION(libvirt_network_get_information) -{ - php_libvirt_network *network =3D NULL; - zval *znetwork; - int retval =3D 0; - char *xml =3D NULL; - char *name =3D NULL; - char *ipaddr =3D NULL; - char *netmask =3D NULL; - char *mode =3D NULL; - char *dev =3D NULL; - char *dhcp_start =3D NULL; - char *dhcp_end =3D NULL; - char fixedtemp[32] =3D { 0 }; - - GET_NETWORK_FROM_ARGS("r", &znetwork); - - xml =3D virNetworkGetXMLDesc(network->network, 0); - - if (xml =3D=3D NULL) { - set_error_if_unset("Cannot get network XML" TSRMLS_CC); - RETURN_FALSE; - } - - array_init(return_value); - - /* Get name */ - name =3D get_string_from_xpath(xml, "//network/name", NULL, &retval); - if (name =3D=3D NULL) { - set_error("Invalid XPath node for network name" TSRMLS_CC); - RETURN_FALSE; - } - - if (retval < 0) { - set_error("Cannot get XPath expression result for network name" TS= RMLS_CC); - RETURN_FALSE; - } - - VIRT_ADD_ASSOC_STRING(return_value, "name", name); - - /* Get gateway IP address */ - ipaddr =3D get_string_from_xpath(xml, "//network/ip/@address", NULL, &= retval); - if (ipaddr && retval > 0) - VIRT_ADD_ASSOC_STRING(return_value, "ip", ipaddr); - - /* Get netmask */ - netmask =3D get_string_from_xpath(xml, "//network/ip/@netmask", NULL, = &retval); - if (netmask && retval > 0) { - int subnet_bits =3D get_subnet_bits(netmask); - VIRT_ADD_ASSOC_STRING(return_value, "netmask", netmask); - add_assoc_long(return_value, "netmask_bits", (long) subnet_bits); - - /* Format CIDR address representation */ - ipaddr[strlen(ipaddr) - 1] =3D ipaddr[strlen(ipaddr) - 1] - 1; - snprintf(fixedtemp, sizeof(fixedtemp), "%s/%d", ipaddr, subnet_bit= s); - VIRT_ADD_ASSOC_STRING(return_value, "ip_range", fixedtemp); - } - - /* Get forwarding settings */ - mode =3D get_string_from_xpath(xml, "//network/forward/@mode", NULL, &= retval); - if (mode && retval > 0) - VIRT_ADD_ASSOC_STRING(return_value, "forwarding", mode); - - /* Get forwarding settings */ - dev =3D get_string_from_xpath(xml, "//network/forward/@dev", NULL, &re= tval); - if (dev && retval > 0) - VIRT_ADD_ASSOC_STRING(return_value, "forward_dev", dev); - - /* Get DHCP values */ - dhcp_start =3D get_string_from_xpath(xml, "//network/ip/dhcp/range/@st= art", NULL, &retval); - dhcp_end =3D get_string_from_xpath(xml, "//network/ip/dhcp/range/@end"= , NULL, &retval); - if (dhcp_start && dhcp_end && retval > 0) { - VIRT_ADD_ASSOC_STRING(return_value, "dhcp_start", dhcp_start); - VIRT_ADD_ASSOC_STRING(return_value, "dhcp_end", dhcp_end); - } - - free(dhcp_end); - free(dhcp_start); - free(dev); - free(mode); - free(netmask); - free(ipaddr); - free(name); - free(xml); -} - -/* - * Function name: libvirt_network_set_active - * Since version: 0.4.1(-1) - * Description: Function is used to set the activity state of the netw= ork - * Arguments: @res [resource]: libvirt network resource - * @flags [int]: active - * Returns: TRUE if success, FALSE on error - */ -PHP_FUNCTION(libvirt_network_set_active) -{ - php_libvirt_network *network; - zval *znetwork; - zend_long act =3D 0; - - DPRINTF("%s: Setting network activity...\n", PHPFUNC); - - GET_NETWORK_FROM_ARGS("rl", &znetwork, &act); - - if ((act !=3D 0) && (act !=3D 1)) { - set_error("Invalid network activity state" TSRMLS_CC); - RETURN_FALSE; - } - - DPRINTF("%s: %sabling network...\n", PHPFUNC, (act =3D=3D 1) ? "En" : = "Dis"); - - if (act =3D=3D 1) { - if (virNetworkCreate(network->network) =3D=3D 0) { - // Network is up and running - RETURN_TRUE; - } else { - // We don't have to set error since it's caught by libvirt err= or handler itself - RETURN_FALSE; - } - } - - if (virNetworkDestroy(network->network) =3D=3D 0) { - // Network is down - RETURN_TRUE; - } else { - // Caught by libvirt error handler too - RETURN_FALSE; - } -} - -/* - * Function name: libvirt_network_get_xml_desc - * Since version: 0.4.1(-1) - * Description: Function is used to get the XML description for the ne= twork - * Arguments: @res [resource]: libvirt network resource - * @xpath [string]: optional xPath expression string to g= et just this entry, can be NULL - * Returns: network XML string or result of xPath expression - */ -PHP_FUNCTION(libvirt_network_get_xml_desc) -{ - php_libvirt_network *network; - zval *znetwork; - char *xml =3D NULL; - char *xpath =3D NULL; - char *tmp; - strsize_t xpath_len; - int retval =3D -1; - - GET_NETWORK_FROM_ARGS("r|s", &znetwork, &xpath, &xpath_len); - if (xpath_len < 1) - xpath =3D NULL; - - xml =3D virNetworkGetXMLDesc(network->network, 0); - - if (xml =3D=3D NULL) { - set_error_if_unset("Cannot get network XML" TSRMLS_CC); - RETURN_FALSE; - } - - tmp =3D get_string_from_xpath(xml, xpath, NULL, &retval); - if ((tmp =3D=3D NULL) || (retval < 0)) { - VIRT_RETVAL_STRING(xml); - } else { - VIRT_RETVAL_STRING(tmp); - } - - free(xml); - free(tmp); -} - -/* - * Function name: libvirt_network_get_uuid_string - * Since version: 0.5.3 - * Description: Function is used to get network's UUID in string format - * Arguments: @res [resource]: libvirt network resource - * Returns: network UUID string or FALSE on failure - */ -PHP_FUNCTION(libvirt_network_get_uuid_string) -{ - php_libvirt_network *network =3D NULL; - zval *znetwork; - char *uuid =3D NULL; - int ret =3D -1; - - GET_NETWORK_FROM_ARGS("r", &znetwork); - - uuid =3D (char *) emalloc(VIR_UUID_STRING_BUFLEN); - ret =3D virNetworkGetUUIDString(network->network, uuid); - - DPRINTF("%s: virNetworkGetUUIDString(%p) returned %d (%s)\n", PHPFUNC, - network->network, ret, uuid); - - if (ret !=3D 0) - RETURN_FALSE; - - VIRT_RETURN_STRING(uuid); - efree(uuid); -} - -/* - * Function name: libvirt_network_get_uuid - * Since version: 0.5.3 - * Descirption: Function is used to get network's UUID in binary format - * Arguments: @res [resource]: libvirt netowrk resource - * Returns: network UUID in binary format or FALSE on failure - */ -PHP_FUNCTION(libvirt_network_get_uuid) -{ - php_libvirt_network *network =3D NULL; - zval *znetwork; - char *uuid =3D NULL; - int ret =3D -1; - - GET_NETWORK_FROM_ARGS("r", &znetwork); - - uuid =3D (char *) emalloc(VIR_UUID_BUFLEN); - ret =3D virNetworkGetUUID(network->network, (unsigned char *)uuid); - - DPRINTF("%s: virNetworkGetUUID(%p, %p) returned %d\n", PHPFUNC, - network->network, uuid, ret); - - if (ret !=3D 0) - RETURN_FALSE; - - VIRT_RETVAL_STRING(uuid); - efree(uuid); -} - -/* - * Function name: libvirt_network_get_name - * Since version: 0.5.3 - * Description: Function is used to get network's name - * Arguments: @res [resource]: libvirt network resource - * Returns: network name string or FALSE on failure - */ -PHP_FUNCTION(libvirt_network_get_name) -{ - php_libvirt_network *network =3D NULL; - zval *znetwork; - const char *name =3D NULL; - - GET_NETWORK_FROM_ARGS("r", &znetwork); - name =3D virNetworkGetName(network->network); - - DPRINTF("%s: virNetworkGetName(%p) returned %s\n", PHPFUNC, - network->network, name); - - if (name =3D=3D NULL) - RETURN_FALSE; - - /* name should not be freed as its lifetime is the same as network res= ource */ - VIRT_RETURN_STRING(name); -} - -/* - * Function name: libvirt_network_get_autostart - * Since version: 0.5.4 - * Description: Function is getting the autostart value for the network - * Arguments: @res [resource]: libvirt network resource - * Returns: autostart value or -1 on error - */ -PHP_FUNCTION(libvirt_network_get_autostart) -{ - php_libvirt_network *network =3D NULL; - zval *znetwork; - int autostart; - - GET_NETWORK_FROM_ARGS("r", &znetwork); - - if (virNetworkGetAutostart(network->network, &autostart) !=3D 0) - RETURN_LONG(-1); - - RETURN_LONG((long) autostart); -} - -/* - * Function name: libvirt_network_set_autostart - * Since version: 0.5.4 - * Description: Function is setting the autostart value for the network - * Arguments: @res [resource]: libvirt network resource - * @flags [int]: flag to enable/disable autostart - * Returns: TRUE on success, FALSE on error - */ -PHP_FUNCTION(libvirt_network_set_autostart) -{ - php_libvirt_network *network =3D NULL; - zval *znetwork; - zend_long autostart =3D 0; - - GET_NETWORK_FROM_ARGS("rl", &znetwork, &autostart); - - if (virNetworkSetAutostart(network->network, autostart) < 0) - RETURN_FALSE; - - RETURN_TRUE; -} - /* NWFilter functions */ =20 /* diff --git a/src/libvirt-php.h b/src/libvirt-php.h index a27ed82..7670582 100644 --- a/src/libvirt-php.h +++ b/src/libvirt-php.h @@ -128,7 +128,6 @@ typedef uint64_t arch_uint; #define PHP_LIBVIRT_WORLD_EXTNAME "libvirt" =20 /* Internal resource identifier objects */ -#define INT_RESOURCE_NETWORK 0x04 #define INT_RESOURCE_NODEDEV 0x08 #define INT_RESOURCE_STORAGEPOOL 0x10 #define INT_RESOURCE_VOLUME 0x20 @@ -172,11 +171,6 @@ typedef struct _php_libvirt_snapshot { php_libvirt_domain* domain; } php_libvirt_snapshot; =20 -typedef struct _php_libvirt_network { - virNetworkPtr network; - php_libvirt_connection* conn; -} php_libvirt_network; - typedef struct _php_libvirt_nodedev { virNodeDevicePtr device; php_libvirt_connection* conn; @@ -247,10 +241,10 @@ int streamSink(virStreamPtr st ATTRIBUTE_UNUSED, const char *bytes, size_t nbytes, void *opaque); const char *get_feature_binary(const char *name); long get_next_free_numeric_value(virDomainPtr domain, char *xpath); +int get_subnet_bits(char *ip); =20 #define PHP_LIBVIRT_STORAGEPOOL_RES_NAME "Libvirt storagepool" #define PHP_LIBVIRT_VOLUME_RES_NAME "Libvirt volume" -#define PHP_LIBVIRT_NETWORK_RES_NAME "Libvirt virtual network" #define PHP_LIBVIRT_NODEDEV_RES_NAME "Libvirt node device" #define PHP_LIBVIRT_SNAPSHOT_RES_NAME "Libvirt domain snapshot" #define PHP_LIBVIRT_NWFILTER_RES_NAME "Libvirt nwfilter" @@ -302,20 +296,6 @@ PHP_FUNCTION(libvirt_storagepool_set_autostart); PHP_FUNCTION(libvirt_storagepool_get_autostart); PHP_FUNCTION(libvirt_storagepool_build); PHP_FUNCTION(libvirt_storagepool_delete); -/* Network functions */ -PHP_FUNCTION(libvirt_network_define_xml); -PHP_FUNCTION(libvirt_network_undefine); -PHP_FUNCTION(libvirt_network_get); -PHP_FUNCTION(libvirt_network_get_xml_desc); -PHP_FUNCTION(libvirt_network_get_bridge); -PHP_FUNCTION(libvirt_network_get_information); -PHP_FUNCTION(libvirt_network_get_active); -PHP_FUNCTION(libvirt_network_set_active); -PHP_FUNCTION(libvirt_network_get_uuid_string); -PHP_FUNCTION(libvirt_network_get_uuid); -PHP_FUNCTION(libvirt_network_get_name); -PHP_FUNCTION(libvirt_network_get_autostart); -PHP_FUNCTION(libvirt_network_set_autostart); /* Nodedev functions */ PHP_FUNCTION(libvirt_nodedev_get); PHP_FUNCTION(libvirt_nodedev_capabilities); @@ -333,8 +313,6 @@ 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_domain_snapshots); --=20 2.13.3 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list