nwfilter.c 5.08 KB
Newer Older
1 2 3 4 5
#include "nwfilter.h"
#include "util.h"

#include <libvirt/libvirt.h>

6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
static virNWFilterPtr
virtDBusNWFilterGetVirNWFilter(virtDBusConnect *connect,
                               const gchar *objectPath,
                               GError **error)
{
    virNWFilterPtr nwfilter;

    if (virtDBusConnectOpen(connect, error) < 0)
        return NULL;

    nwfilter = virtDBusUtilVirNWFilterFromBusPath(connect->connection,
                                                  objectPath,
                                                  connect->nwfilterPath);
    if (!nwfilter) {
        virtDBusUtilSetLastVirtError(error);
        return NULL;
    }

    return nwfilter;
}

static void
virtDBusNWFilterGetName(const gchar *objectPath,
                        gpointer userData,
                        GVariant **value,
                        GError **error)
{
    virtDBusConnect *connect = userData;
    g_autoptr(virNWFilter) nwfilter = NULL;
    const gchar *name;

    nwfilter = virtDBusNWFilterGetVirNWFilter(connect, objectPath, error);
    if (!nwfilter)
        return;

    name = virNWFilterGetName(nwfilter);
    if (!name)
        return virtDBusUtilSetLastVirtError(error);

    *value = g_variant_new("s", name);
}

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
static void
virtDBusNWFilterGetUUID(const gchar *objectPath,
                        gpointer userData,
                        GVariant **value,
                        GError **error)
{
    virtDBusConnect *connect = userData;
    g_autoptr(virNWFilter) nwfilter = NULL;
    gchar uuid[VIR_UUID_STRING_BUFLEN] = "";

    nwfilter = virtDBusNWFilterGetVirNWFilter(connect, objectPath, error);
    if (!nwfilter)
        return;

    if (virNWFilterGetUUIDString(nwfilter, uuid) < 0)
        return virtDBusUtilSetLastVirtError(error);

    *value = g_variant_new("s", uuid);
}

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
static void
virtDBusNWFilterGetXMLDesc(GVariant *inArgs,
                           GUnixFDList *inFDs G_GNUC_UNUSED,
                           const gchar *objectPath,
                           gpointer userData,
                           GVariant **outArgs,
                           GUnixFDList **outFDs G_GNUC_UNUSED,
                           GError **error)
{
    virtDBusConnect *connect = userData;
    g_autoptr(virNWFilter) nwfilter = NULL;
    g_autofree gchar *xml = NULL;
    guint flags;

    g_variant_get(inArgs, "(u)", &flags);

    nwfilter = virtDBusNWFilterGetVirNWFilter(connect, objectPath, error);
    if (!nwfilter)
        return;

    xml = virNWFilterGetXMLDesc(nwfilter, flags);
    if (!xml)
        return virtDBusUtilSetLastVirtError(error);

    *outArgs = g_variant_new("(s)", xml);
}

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
static void
virtDBusNWFilterUndefine(GVariant *inArgs G_GNUC_UNUSED,
                         GUnixFDList *inFDs G_GNUC_UNUSED,
                         const gchar *objectPath,
                         gpointer userData,
                         GVariant **outArgs G_GNUC_UNUSED,
                         GUnixFDList **outFDs G_GNUC_UNUSED,
                         GError **error)
{
    virtDBusConnect *connect = userData;
    g_autoptr(virNWFilter) nwfilter = NULL;

    nwfilter = virtDBusNWFilterGetVirNWFilter(connect, objectPath, error);
    if (!nwfilter)
        return;

    if (virNWFilterUndefine(nwfilter) < 0)
        virtDBusUtilSetLastVirtError(error);
}

115
static virtDBusGDBusPropertyTable virtDBusNWFilterPropertyTable[] = {
116
    { "Name", virtDBusNWFilterGetName, NULL },
117
    { "UUID", virtDBusNWFilterGetUUID, NULL },
118 119 120 121
    { 0 }
};

static virtDBusGDBusMethodTable virtDBusNWFilterMethodTable[] = {
122
    { "GetXMLDesc", virtDBusNWFilterGetXMLDesc },
123
    { "Undefine", virtDBusNWFilterUndefine},
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    { 0 }
};

static gchar **
virtDBusNWFilterEnumerate(gpointer userData)
{
    virtDBusConnect *connect = userData;
    g_autoptr(virNWFilterPtr) nwfilters = NULL;
    gint num = 0;
    gchar **ret = NULL;

    if (!virtDBusConnectOpen(connect, NULL))
        return NULL;

    num = virConnectListAllNWFilters(connect->connection, &nwfilters, 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] = virtDBusUtilBusPathForVirNWFilter(nwfilters[i],
                                                   connect->nwfilterPath);
    }

    return ret;
}

static GDBusInterfaceInfo *interfaceInfo;

void
virtDBusNWFilterRegister(virtDBusConnect *connect,
                         GError **error)
{
    connect->nwfilterPath = g_strdup_printf("%s/nwfilter",
                                            connect->connectPath);

    if (!interfaceInfo) {
        interfaceInfo = virtDBusGDBusLoadIntrospectData(VIRT_DBUS_NWFILTER_INTERFACE,
                                                        error);
        if (!interfaceInfo)
            return;
    }

    virtDBusGDBusRegisterSubtree(connect->bus,
                                 connect->nwfilterPath,
                                 interfaceInfo,
                                 virtDBusNWFilterEnumerate,
                                 virtDBusNWFilterMethodTable,
                                 virtDBusNWFilterPropertyTable,
                                 connect);
}