summaryrefslogtreecommitdiff
path: root/examples/C/glib/add-connection-gdbus.c
blob: 890d8819766ce941628902ae920ee47bfa302ab1 (plain)
1
2
3
4
5
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2011, 2014 Red Hat, Inc.
 */

/*
 * The example shows how to call the AddConnection() D-Bus method to add a
 * connection to the system settings service. It uses GDBus, plus a few defines
 * from the NetworkManager headers. Contrast add-connection-libnm, which is
 * higher level because it uses libnm.
 *
 * Compile with:
 *   gcc -Wall add-connection-gdbus.c -o add-connection-gdbus `pkg-config --cflags --libs libnm uuid`
 */

#include <gio/gio.h>
#include <uuid/uuid.h>

#include <nm-dbus-interface.h>

/* include NetworkManager.h for the defines, but we don't link against
 * libnm. */
#include <NetworkManager.h>

/* copied from libnm-core/nm-utils.c */
char *
nm_utils_uuid_generate(void)
{
    uuid_t uuid;
    char * buf;

    buf = g_malloc0(37);
    uuid_generate_random(uuid);
    uuid_unparse_lower(uuid, &buf[0]);
    return buf;
}

static void
add_connection(GDBusProxy *proxy, const char *con_name)
{
    GVariantBuilder connection_builder;
    GVariantBuilder setting_builder;
    char *          uuid;
    const char *    new_con_path;
    GVariant *      ret;
    GError *        error = NULL;

    /* Initialize connection GVariantBuilder */
    g_variant_builder_init(&connection_builder, G_VARIANT_TYPE("a{sa{sv}}"));

    /* Build up the 'connection' Setting */
    g_variant_builder_init(&setting_builder, G_VARIANT_TYPE("a{sv}"));

    uuid = nm_utils_uuid_generate();
    g_variant_builder_add(&setting_builder,
                          "{sv}",
                          NM_SETTING_CONNECTION_UUID,
                          g_variant_new_string(uuid));
    g_free(uuid);

    g_variant_builder_add(&setting_builder,
                          "{sv}",
                          NM_SETTING_CONNECTION_ID,
                          g_variant_new_string(con_name));
    g_variant_builder_add(&setting_builder,
                          "{sv}",
                          NM_SETTING_CONNECTION_TYPE,
                          g_variant_new_string(NM_SETTING_WIRED_SETTING_NAME));

    g_variant_builder_add(&connection_builder,
                          "{sa{sv}}",
                          NM_SETTING_CONNECTION_SETTING_NAME,
                          &setting_builder);

    /* Add the (empty) 'wired' Setting */
    g_variant_builder_init(&setting_builder, G_VARIANT_TYPE("a{sv}"));
    g_variant_builder_add(&connection_builder,
                          "{sa{sv}}",
                          NM_SETTING_WIRED_SETTING_NAME,
                          &setting_builder);

    /* Build up the 'ipv4' Setting */
    g_variant_builder_init(&setting_builder, G_VARIANT_TYPE("a{sv}"));
    g_variant_builder_add(&setting_builder,
                          "{sv}",
                          NM_SETTING_IP_CONFIG_METHOD,
                          g_variant_new_string(NM_SETTING_IP4_CONFIG_METHOD_AUTO));
    g_variant_builder_add(&connection_builder,
                          "{sa{sv}}",
                          NM_SETTING_IP4_CONFIG_SETTING_NAME,
                          &setting_builder);

    /* Call AddConnection with the connection dictionary as argument.
	 * (g_variant_new() will consume the floating GVariant returned from
	 * &connection_builder, and g_dbus_proxy_call_sync() will consume the
	 * floating variant returned from g_variant_new(), so no cleanup is needed.
	 */
    ret = g_dbus_proxy_call_sync(proxy,
                                 "AddConnection",
                                 g_variant_new("(a{sa{sv}})", &connection_builder),
                                 G_DBUS_CALL_FLAGS_NONE,
                                 -1,
                                 NULL,
                                 &error);
    if (ret) {
        g_variant_get(ret, "(&o)", &new_con_path);
        g_print("Added: %s\n", new_con_path);
        g_variant_unref(ret);
    } else {
        g_dbus_error_strip_remote_error(error);
        g_print("Error adding connection: %s\n", error->message);
        g_clear_error(&error);
    }
}

int
main(int argc, char *argv[])
{
    GDBusProxy *proxy;
    GError *    error = NULL;

    /* Create a D-Bus proxy; NM_DBUS_* defined in nm-dbus-interface.h */
    proxy = g_dbus_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,
                                          G_DBUS_PROXY_FLAGS_NONE,
                                          NULL,
                                          NM_DBUS_SERVICE,
                                          NM_DBUS_PATH_SETTINGS,
                                          NM_DBUS_INTERFACE_SETTINGS,
                                          NULL,
                                          &error);
    if (!proxy) {
        g_dbus_error_strip_remote_error(error);
        g_print("Could not create NetworkManager D-Bus proxy: %s\n", error->message);
        g_error_free(error);
        return 1;
    }

    /* Add a connection */
    add_connection(proxy, "__Test connection__");

    g_object_unref(proxy);

    return 0;
}