Signed-off-by: Katerina Koukiou <kkoukiou(a)redhat.com>
---
data/Makefile.am | 3 ++-
data/org.libvirt.Network.xml | 7 +++++
src/Makefile.am | 3 ++-
src/connect.c | 5 ++++
src/connect.h | 1 +
src/network.c | 64 ++++++++++++++++++++++++++++++++++++++++++++
src/network.h | 9 +++++++
src/util.c | 20 ++++++++++++++
src/util.h | 10 +++++++
9 files changed, 120 insertions(+), 2 deletions(-)
create mode 100644 data/org.libvirt.Network.xml
create mode 100644 src/network.c
create mode 100644 src/network.h
diff --git a/data/Makefile.am b/data/Makefile.am
index dd60713..61702df 100644
--- a/data/Makefile.am
+++ b/data/Makefile.am
@@ -20,7 +20,8 @@ polkit_DATA = $(polkit_files:.rules.in=.rules)
interfaces_files = \
org.libvirt.Connect.xml \
- org.libvirt.Domain.xml
+ org.libvirt.Domain.xml \
+ org.libvirt.Network.xml
interfacesdir = $(DBUS_INTERFACES_DIR)
interfaces_DATA = $(interfaces_files)
diff --git a/data/org.libvirt.Network.xml b/data/org.libvirt.Network.xml
new file mode 100644
index 0000000..2b7c4f7
--- /dev/null
+++ b/data/org.libvirt.Network.xml
@@ -0,0 +1,7 @@
+<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection
1.0//EN"
+"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
+
+<node name="/org/libvirt/network">
+ <interface name="org.libvirt.Network">
+ </interface>
+</node>
diff --git a/src/Makefile.am b/src/Makefile.am
index 7248561..158398a 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -8,7 +8,8 @@ DAEMON_SOURCES = \
util.c util.h \
domain.c domain.h \
events.c events.h \
- gdbus.c gdbus.h
+ gdbus.c gdbus.h \
+ network.c network.h
EXTRA_DIST = \
$(DAEMON_SOURCES)
diff --git a/src/connect.c b/src/connect.c
index 2350736..7b3f834 100644
--- a/src/connect.c
+++ b/src/connect.c
@@ -1,6 +1,7 @@
#include "connect.h"
#include "domain.h"
#include "events.h"
+#include "network.h"
#include "util.h"
#include <glib/gprintf.h>
@@ -345,6 +346,10 @@ virtDBusConnectNew(virtDBusConnect **connectp,
if (error && *error)
return;
+ virtDBusNetworkRegister(connect, error);
+ if (error && *error)
+ return;
+
*connectp = connect;
connect = NULL;
}
diff --git a/src/connect.h b/src/connect.h
index 9572857..7ed84b8 100644
--- a/src/connect.h
+++ b/src/connect.h
@@ -13,6 +13,7 @@ struct virtDBusConnect {
const gchar *uri;
const gchar *connectPath;
gchar *domainPath;
+ gchar *networkPath;
virConnectPtr connection;
GMutex lock;
diff --git a/src/network.c b/src/network.c
new file mode 100644
index 0000000..0d0e992
--- /dev/null
+++ b/src/network.c
@@ -0,0 +1,64 @@
+#include "network.h"
+#include "util.h"
+
+#include <libvirt/libvirt.h>
+
+static virtDBusGDBusPropertyTable virtDBusNetworkPropertyTable[] = {
+ { 0 }
+};
+
+static virtDBusGDBusMethodTable virtDBusNetworkMethodTable[] = {
+ { 0 }
+};
+
+static gchar **
+virtDBusNetworkEnumerate(gpointer userData)
+{
+ virtDBusConnect *connect = userData;
+ g_autoptr(virNetworkPtr) networks = NULL;
+ gint num = 0;
+ gchar **ret = NULL;
+
+ if (!virtDBusConnectOpen(connect, NULL))
+ return NULL;
+
+ num = virConnectListAllNetworks(connect->connection, &networks, 0);
+ if (num < 0)
+ return NULL;
+
+ if (num == 0)
+ return NULL;
+
+ ret = g_new0(gchar *, num + 1);
+
+ for (gint i = 0; i < num; i++) {
+ ret[i] = virtDBusUtilBusPathForVirNetwork(networks[i],
+ connect->networkPath);
+ }
+
+ return ret;
+}
+
+static GDBusInterfaceInfo *interfaceInfo = NULL;
+
+void
+virtDBusNetworkRegister(virtDBusConnect *connect,
+ GError **error)
+{
+ connect->networkPath = g_strdup_printf("%s/network",
connect->connectPath);
+
+ if (!interfaceInfo) {
+ interfaceInfo = virtDBusGDBusLoadIntrospectData(VIRT_DBUS_NETWORK_INTERFACE,
+ error);
+ if (!interfaceInfo)
+ return;
+ }
+
+ virtDBusGDBusRegisterSubtree(connect->bus,
+ connect->networkPath,
+ interfaceInfo,
+ virtDBusNetworkEnumerate,
+ virtDBusNetworkMethodTable,
+ virtDBusNetworkPropertyTable,
+ connect);
+}
diff --git a/src/network.h b/src/network.h
new file mode 100644
index 0000000..fc53b28
--- /dev/null
+++ b/src/network.h
@@ -0,0 +1,9 @@
+#pragma once
+
+#include "connect.h"
+
+#define VIRT_DBUS_NETWORK_INTERFACE "org.libvirt.Network"
+
+void
+virtDBusNetworkRegister(virtDBusConnect *connect,
+ GError **error);
diff --git a/src/util.c b/src/util.c
index be65172..fe40a96 100644
--- a/src/util.c
+++ b/src/util.c
@@ -151,3 +151,23 @@ virtDBusUtilEnumFromString(const gchar *const *types,
return -1;
}
+
+gchar *
+virtDBusUtilBusPathForVirNetwork(virNetworkPtr network,
+ const gchar *networkPath)
+{
+ gchar uuid[VIR_UUID_STRING_BUFLEN] = "";
+ g_autofree gchar *newUuid = NULL;
+ virNetworkGetUUIDString(network, uuid);
+ newUuid = virtDBusUtilEncodeUUID(uuid);
+ return g_strdup_printf("%s/%s", networkPath, newUuid);
+}
+
+void
+virtDBusUtilVirNetworkListFree(virNetworkPtr *networks)
+{
+ for (gint i = 0; networks[i] != NULL; i += 1)
+ virNetworkFree(networks[i]);
+
+ g_free(networks);
+}
diff --git a/src/util.h b/src/util.h
index c9d9cfd..fac080c 100644
--- a/src/util.h
+++ b/src/util.h
@@ -65,3 +65,13 @@ virtDBusUtilEnumToString(const gchar *const *types,
#define VIRT_DBUS_ENUM_DECL(name) \
const gchar *name ##TypeToString(gint type) G_GNUC_PURE; \
gint name ##TypeFromString(const gchar *type) G_GNUC_PURE;
+
+gchar *
+virtDBusUtilBusPathForVirNetwork(virNetworkPtr network,
+ const gchar *networkPath);
+
+void
+virtDBusUtilVirNetworkListFree(virNetworkPtr *networks);
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(virNetwork, virNetworkFree);
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(virNetworkPtr, virtDBusUtilVirNetworkListFree);
--
2.15.0