summaryrefslogtreecommitdiff
path: root/examples/C/qt/change-ipv4-addresses.cpp
blob: 4c42a6b68e531a1ff8159311aaaf659715672fba (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
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2011 Red Hat, Inc.
 */

/*
 * This example shows how to set manual IPv4 addresses to a connection.
 * It uses Qt and D-Bus libraries to do that.
 *
 * Standalone compilation:
 * g++ -Wall `pkg-config --libs --cflags QtCore QtDBus QtNetwork` `pkg-config --cflags libnm` change-ipv4-addresses.cpp -o change-ipv4-addresses
 *
 * You don't need to have NetworkManager devel package installed; you can just
 * grab nm-dbus-interface.h and put it in the path
 */

#include <QtDBus/QDBusConnection>
#include <QtDBus/QDBusInterface>
#include <QtDBus/QDBusMetaType>
#include <QtDBus/QDBusReply>
#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QString>
#include <QtCore/QDebug>
#include <QtNetwork/QHostAddress>

#include <arpa/inet.h>

#include <nm-dbus-interface.h>

typedef QMap<QString, QMap<QString, QVariant> > Connection;
Q_DECLARE_METATYPE(Connection)
Q_DECLARE_METATYPE(QList<uint>);
Q_DECLARE_METATYPE(QList<QList<uint> >);

const QString NM_SETTING_CONNECTION_SETTING_NAME = "connection";
const QString NM_SETTING_CONNECTION_ID = "id";
const QString NM_SETTING_CONNECTION_UUID = "uuid";


const QString getConnection(const QString& connectionUuid, Connection *found_connection)
{
    Connection settings;
    QDBusInterface *ifaceForSettings;

    // Create a D-Bus proxy; NM_DBUS_* defined in NetworkManager.h
    QDBusInterface interface(
        NM_DBUS_SERVICE,
        NM_DBUS_PATH_SETTINGS,
        NM_DBUS_INTERFACE_SETTINGS,
        QDBusConnection::systemBus());

    // Get connection list and find the connection with 'connectionUuid'
    QDBusReply<QList<QDBusObjectPath> > result1 = interface.call("ListConnections");

    foreach (const QDBusObjectPath& connection, result1.value()) {
        ifaceForSettings = new QDBusInterface(
                                    NM_DBUS_SERVICE,
                                    connection.path(),
                                    NM_DBUS_INTERFACE_SETTINGS_CONNECTION,
                                    QDBusConnection::systemBus());
        QDBusReply<Connection> result2 = ifaceForSettings->call("GetSettings");
        delete ifaceForSettings;

        settings = result2.value();
        QVariantMap connectionSettings = settings.value(NM_SETTING_CONNECTION_SETTING_NAME);
        QString uuid = connectionSettings.value(NM_SETTING_CONNECTION_UUID).toString();

        if (uuid == connectionUuid) {
            // Connection found; set the settings to found_connection
            // connection object path
            *found_connection = settings;
            return connection.path();
        }
    }

    return QString();
}

void changeConnection(const QString& uuid)
{
    // Register types with D-Bus
    qDBusRegisterMetaType<Connection>();
    qDBusRegisterMetaType<QList<uint> >();
    qDBusRegisterMetaType<QList<QList<uint> > >();

    Connection connection;
    QString conPath;

    // Find connection by provided UUID
    conPath = getConnection(uuid, &connection);

    if (!conPath.isEmpty()) {
        QList<QList<uint> > addresses;
        QList<uint> addr1, addr2;

        // Add some addresses
        addr1 << htonl(QHostAddress("192.168.100.4").toIPv4Address()) << 24 << htonl(QHostAddress("192.168.100.1").toIPv4Address());
        addr2 << htonl(QHostAddress("10.0.1.222").toIPv4Address()) << 8 << htonl(QHostAddress("10.0.1.254").toIPv4Address());
        addresses << addr1 << addr2;

        // Set method to "Manual" and put addresses to the connection map
        connection["ipv4"]["method"] = "manual";
        connection["ipv4"]["addresses"] = QVariant::fromValue(addresses);

        QDBusInterface interface(
            NM_DBUS_SERVICE,
            conPath,
            NM_DBUS_INTERFACE_SETTINGS_CONNECTION,
            QDBusConnection::systemBus());

        // Call Update() D-Bus method to update connection
        QDBusReply<void> result = interface.call("Update", QVariant::fromValue(connection));
        if (result.isValid()) {
            qDebug() << QString("Connection successfully updated (path %1)").arg(conPath);
        } else {
           qDebug() << QString("Error: could not update connection: %1 %2").arg(result.error().name()).arg(result.error().message());
        }
    } else {
            qDebug() << QString("Error: connection with UUID '%1' not found").arg(uuid);
    }
}

int main(int argc, char *argv[])
{
    if (argc != 2) {
        qDebug() << QString("Usage: %1 <UUID>").arg(argv[0]);
        return -1;
    }

    changeConnection(argv[1]);
}